2024-04-13 21:13:22

by Quentin Deslandes

[permalink] [raw]
Subject: [PATCH 0/2] libbpf: fixes for character arrays dump

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


2024-04-13 21:13:38

by Quentin Deslandes

[permalink] [raw]
Subject: [PATCH 1/2] libbpf: fix misaligned array closing bracket

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 <[email protected]>
---
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


2024-04-13 21:14:22

by Quentin Deslandes

[permalink] [raw]
Subject: [PATCH 2/2] libbpf: fix dump of subsequent char arrays

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 <[email protected]>
---
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


2024-04-17 13:33:03

by patchwork-bot+netdevbpf

[permalink] [raw]
Subject: Re: [PATCH 0/2] libbpf: fixes for character arrays dump

Hello:

This series was applied to bpf/bpf-next.git (master)
by Daniel Borkmann <[email protected]>:

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