BPF List
 help / color / mirror / Atom feed
* [PATCH 0/2] libbpf: fixes for character arrays dump
@ 2024-04-13 21:12 Quentin Deslandes
  2024-04-13 21:12 ` [PATCH 1/2] libbpf: fix misaligned array closing bracket Quentin Deslandes
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Quentin Deslandes @ 2024-04-13 21:12 UTC (permalink / raw)
  To: bpf
  Cc: Andrii Nakryiko, Eduard Zingerman, Alexei Starovoitov,
	Daniel Borkmann, Martin KaFai Lau, Song Liu, Yonghong Song,
	John Fastabend, KP Singh, Stanislav Fomichev, Hao Luo, Jiri Olsa,
	linux-kernel, kernel-team, Quentin Deslandes

Patch #1 fixes an issue where character arrays in dumps would
have their last bracket misaligned if a '\0' was found in the
array.

Patch #2 fixes an issue where only the first character array
containing '\0' would be printed, the other ones would be
dumped as empty.

Quentin Deslandes (2):
  libbpf: fix misaligned array closing bracket
  libbpf: fix dump of subsequent char arrays

 tools/lib/bpf/btf_dump.c | 5 +++++
 1 file changed, 5 insertions(+)

--
2.44.0

^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH 1/2] libbpf: fix misaligned array closing bracket
  2024-04-13 21:12 [PATCH 0/2] libbpf: fixes for character arrays dump Quentin Deslandes
@ 2024-04-13 21:12 ` Quentin Deslandes
  2024-04-13 21:12 ` [PATCH 2/2] libbpf: fix dump of subsequent char arrays Quentin Deslandes
  2024-04-17 13:30 ` [PATCH 0/2] libbpf: fixes for character arrays dump patchwork-bot+netdevbpf
  2 siblings, 0 replies; 4+ messages in thread
From: Quentin Deslandes @ 2024-04-13 21:12 UTC (permalink / raw)
  To: bpf
  Cc: Andrii Nakryiko, Eduard Zingerman, Alexei Starovoitov,
	Daniel Borkmann, Martin KaFai Lau, Song Liu, Yonghong Song,
	John Fastabend, KP Singh, Stanislav Fomichev, Hao Luo, Jiri Olsa,
	linux-kernel, kernel-team, Quentin Deslandes

In btf_dump_array_data(), libbpf will call btf_dump_dump_type_data() for
each element. For an array of characters, each element will be
processed the following way:
- btf_dump_dump_type_data() is called to print the character
- btf_dump_data_pfx() prefixes the current line with the proper number
  of indentations
- btf_dump_int_data() is called to print the character
- After the last character is printed, btf_dump_dump_type_data() calls
  btf_dump_data_pfx() before writing the closing bracket

However, for an array containing characters, btf_dump_int_data() won't
print any '\0' and subsequent characters. This leads to situations where
the line prefix is written, no character is added, then the prefix is
written again before adding the closing bracket:

(struct sk_metadata){
    .str_array = (__u8[14])[
        'H',
        'e',
        'l',
        'l',
        'o',
                ],

This change solves this issue by printing the '\0' character, which
has two benefits:
- The bracket closing the array is properly aligned
- It's clear from a user point of view that libbpf uses '\0' as a
  terminator for arrays of characters.

Signed-off-by: Quentin Deslandes <qde@naccy.de>
---
 tools/lib/bpf/btf_dump.c | 1 +
 1 file changed, 1 insertion(+)

diff --git a/tools/lib/bpf/btf_dump.c b/tools/lib/bpf/btf_dump.c
index 4d9f30bf7f01..6a37e8517435 100644
--- a/tools/lib/bpf/btf_dump.c
+++ b/tools/lib/bpf/btf_dump.c
@@ -1929,6 +1929,7 @@ static int btf_dump_int_data(struct btf_dump *d,
 			if (d->typed_dump->is_array_terminated)
 				break;
 			if (*(char *)data == '\0') {
+				btf_dump_type_values(d, "'\\0'");
 				d->typed_dump->is_array_terminated = true;
 				break;
 			}
-- 
2.44.0


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* [PATCH 2/2] libbpf: fix dump of subsequent char arrays
  2024-04-13 21:12 [PATCH 0/2] libbpf: fixes for character arrays dump Quentin Deslandes
  2024-04-13 21:12 ` [PATCH 1/2] libbpf: fix misaligned array closing bracket Quentin Deslandes
@ 2024-04-13 21:12 ` Quentin Deslandes
  2024-04-17 13:30 ` [PATCH 0/2] libbpf: fixes for character arrays dump patchwork-bot+netdevbpf
  2 siblings, 0 replies; 4+ messages in thread
From: Quentin Deslandes @ 2024-04-13 21:12 UTC (permalink / raw)
  To: bpf
  Cc: Andrii Nakryiko, Eduard Zingerman, Alexei Starovoitov,
	Daniel Borkmann, Martin KaFai Lau, Song Liu, Yonghong Song,
	John Fastabend, KP Singh, Stanislav Fomichev, Hao Luo, Jiri Olsa,
	linux-kernel, kernel-team, Quentin Deslandes

When dumping a character array, libbpf will watch for a '\0' and set
is_array_terminated=true if found. This prevents libbpf from printing
the remaining characters of the array, treating it as a nul-terminated
string.

However, once this flag is set, it's never reset, leading to subsequent
characters array not being printed properly:

.str_multi = (__u8[2][16])[
    [
        'H',
        'e',
        'l',
    ],
],

This patch saves the is_array_terminated flag and restores its
default (false) value before looping over the elements of an array, then
restores it afterward. This way, libbpf's behavior is unchanged when
dumping the characters of an array, but subsequent arrays are printed
properly:

.str_multi = (__u8[2][16])[
    [
        'H',
        'e',
        'l',
    ],
    [
        'l',
        'o',
    ],
],

Signed-off-by: Quentin Deslandes <qde@naccy.de>
---
 tools/lib/bpf/btf_dump.c | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/tools/lib/bpf/btf_dump.c b/tools/lib/bpf/btf_dump.c
index 6a37e8517435..5dbca76b953f 100644
--- a/tools/lib/bpf/btf_dump.c
+++ b/tools/lib/bpf/btf_dump.c
@@ -2032,6 +2032,7 @@ static int btf_dump_array_data(struct btf_dump *d,
 	__u32 i, elem_type_id;
 	__s64 elem_size;
 	bool is_array_member;
+	bool is_array_terminated;
 
 	elem_type_id = array->type;
 	elem_type = skip_mods_and_typedefs(d->btf, elem_type_id, NULL);
@@ -2067,12 +2068,15 @@ static int btf_dump_array_data(struct btf_dump *d,
 	 */
 	is_array_member = d->typed_dump->is_array_member;
 	d->typed_dump->is_array_member = true;
+	is_array_terminated = d->typed_dump->is_array_terminated;
+	d->typed_dump->is_array_terminated = false;
 	for (i = 0; i < array->nelems; i++, data += elem_size) {
 		if (d->typed_dump->is_array_terminated)
 			break;
 		btf_dump_dump_type_data(d, NULL, elem_type, elem_type_id, data, 0, 0);
 	}
 	d->typed_dump->is_array_member = is_array_member;
+	d->typed_dump->is_array_terminated = is_array_terminated;
 	d->typed_dump->depth--;
 	btf_dump_data_pfx(d);
 	btf_dump_type_values(d, "]");
-- 
2.44.0


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH 0/2] libbpf: fixes for character arrays dump
  2024-04-13 21:12 [PATCH 0/2] libbpf: fixes for character arrays dump Quentin Deslandes
  2024-04-13 21:12 ` [PATCH 1/2] libbpf: fix misaligned array closing bracket Quentin Deslandes
  2024-04-13 21:12 ` [PATCH 2/2] libbpf: fix dump of subsequent char arrays Quentin Deslandes
@ 2024-04-17 13:30 ` patchwork-bot+netdevbpf
  2 siblings, 0 replies; 4+ messages in thread
From: patchwork-bot+netdevbpf @ 2024-04-17 13:30 UTC (permalink / raw)
  To: Quentin Deslandes
  Cc: bpf, andrii, eddyz87, ast, daniel, martin.lau, song,
	yonghong.song, john.fastabend, kpsingh, sdf, haoluo, jolsa,
	linux-kernel, kernel-team

Hello:

This series was applied to bpf/bpf-next.git (master)
by Daniel Borkmann <daniel@iogearbox.net>:

On Sat, 13 Apr 2024 23:12:56 +0200 you wrote:
> Patch #1 fixes an issue where character arrays in dumps would
> have their last bracket misaligned if a '\0' was found in the
> array.
> 
> Patch #2 fixes an issue where only the first character array
> containing '\0' would be printed, the other ones would be
> dumped as empty.
> 
> [...]

Here is the summary with links:
  - [1/2] libbpf: fix misaligned array closing bracket
    https://git.kernel.org/bpf/bpf-next/c/9213e52970a5
  - [2/2] libbpf: fix dump of subsequent char arrays
    https://git.kernel.org/bpf/bpf-next/c/e739e01d8df8

You are awesome, thank you!
-- 
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html



^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2024-04-17 13:30 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-04-13 21:12 [PATCH 0/2] libbpf: fixes for character arrays dump Quentin Deslandes
2024-04-13 21:12 ` [PATCH 1/2] libbpf: fix misaligned array closing bracket Quentin Deslandes
2024-04-13 21:12 ` [PATCH 2/2] libbpf: fix dump of subsequent char arrays Quentin Deslandes
2024-04-17 13:30 ` [PATCH 0/2] libbpf: fixes for character arrays dump patchwork-bot+netdevbpf

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox