Skip to contentSkip to conversation

bpftool: Preserve unsigned enum64 values in data dumps

PATCH Superseded Hungyu Lin 3 participants 3 messages Oct 31

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Hungyu Lin

author
#0 lore raw

PATCH 1/1 bpftool: Preserve unsigned enum64 values in data dumps

When an enum64 data value does not match a named enumerator, btf_dumper_enum64() always prints it as signed. Unsigned values with bit 63 set therefore appear negative in BTF-formatted map dump and lookup output, in both plain and JSON modes. For example, 0x8000000000000001 is printed as -9223372036854775807 instead of 9223372036854775809.

Choose the numeric writer according to the enum's signedness. Keep signed values and named enumerators unchanged.

Fixes 58a53978fdf6 ("bpftool: Add btf enum64 support")
---
 tools/bpf/bpftool/btf_dumper.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
tools/bpf/bpftool/btf_dumper.c +4 -1
index 3267163119dc..88c0da29f246 100644
@@ -204,7 +204,10 @@ static int btf_dumper_enum64(const struct btf_dumper *d,
 		}
 	}
 
-	jsonw_int(d->jw, value);
+	if (btf_kflag(t))
+		jsonw_int(d->jw, value);
+	else
+		jsonw_uint(d->jw, value);
 	return 0;
 }
 

base-commit: eaae576c34d7e001767b6845a5950b0b8db3e68a

Signature · 1 line
2.43.0
#1 lore raw

Review static int btf_dumper_enum64(const struct btf_dumper *d, · line 207

Quoted 6 lines · Hungyu Lin
(quoted diff) +4 -1
@@ -204,7 +204,10 @@ static int btf_dumper_enum64(const struct btf_dumper *d,
-	jsonw_int(d->jw, value);
+	if (btf_kflag(t))
+		jsonw_int(d->jw, value);
+	else
+		jsonw_uint(d->jw, value);

btf_dumper_enum() has the same problem.

Fix all in one patch:

tools/bpf/bpftool/btf_dumper.c +15 -7
index 3267163..09dd428 100644
@@ -149,7 +149,8 @@ static int btf_dumper_enum(const struct btf_dumper *d,
 			    const void *data)
 {
 	const struct btf_enum *enums = btf_enum(t);
-	__s64 value;
+	bool is_signed = btf_kflag(t);
+	__s64 value, val;
 	__u32 i;
 
 	switch (t->size) {
@@ -157,20 +158,21 @@ static int btf_dumper_enum(const struct btf_dumper *d,
 		value = *(__s64 *)data;
 		break;
 	case 4:
-		value = *(__s32 *)data;
+		value = is_signed ? (__s64)*(__s32 *)data : *(__u32 *)data;
 		break;
 	case 2:
-		value = *(__s16 *)data;
+		value = is_signed ? *(__s16 *)data : *(__u16 *)data;
 		break;
 	case 1:
-		value = *(__s8 *)data;
+		value = is_signed ? *(__s8 *)data : *(__u8 *)data;
 		break;
 	default:
 		return -EINVAL;
 	}
 
 	for (i = 0; i < btf_vlen(t); i++) {
-		if (value == enums[i].val) {
+		val = is_signed ? (__s64)enums[i].val : (__u32)enums[i].val;
+		if (value == val) {
 			jsonw_string(d->jw,
 				     btf__name_by_offset(d->btf,
 							 enums[i].name_off));
@@ -178,7 +180,10 @@ static int btf_dumper_enum(const struct btf_dumper *d,
 		}
 	}
 
-	jsonw_int(d->jw, value);
+	if (is_signed)
+		jsonw_int(d->jw, value);
+	else
+		jsonw_uint(d->jw, value);
 	return 0;
 }
 
@@ -204,7 +209,10 @@ static int btf_dumper_enum64(const struct btf_dumper *d,
 		}
 	}
 
-	jsonw_int(d->jw, value);
+	if (btf_kflag(t))
+		jsonw_int(d->jw, value);
+	else
+		jsonw_uint(d->jw, value);
 	return 0;
 }
 

pw-bot: cr

#2 lore raw
In reply to Hungyu Lin
TL;DR: Possible incomplete fix: as requested in the v1 review, btf_dumper_enum() was left unchanged, so an unmatched unsigned 4-byte enum value like 0xffffffff may still print as -1 in bpftool dumps.
Quoted 1 line

bpftool: Preserve unsigned enum64 values in data dumps

TL;DR: Possible incomplete fix: as requested in the v1 review, btf_dumper_enum() was left unchanged, so an unmatched unsigned 4-byte enum value like 0xffffffff may still print as -1 in bpftool dumps.

Quoted 4 lines
tools/bpf/bpftool/btf_dumper.c +0 -0
index 3267163119dc..88c0da29f246 100644

[ ... ]

tools/bpf/bpftool/btf_dumper.c static int btf_dumper_enum64(const struct btf_dumper *d, · line 213

Quoted 12 lines
tools/bpf/bpftool/btf_dumper.c +4 -1
@@ -204,7 +204,10 @@ static int btf_dumper_enum64(const struct btf_dumper *d,
 		}
 	}
 
-	jsonw_int(d->jw, value);
+	if (btf_kflag(t))
+		jsonw_int(d->jw, value);
+	else
+		jsonw_uint(d->jw, value);
 	return 0;
 }
 

Alexei Starovoitov replied to v1 that btf_dumper_enum() has the same problem and asked for both functions to be fixed in one patch, and he supplied a diff for it. I could not find a v2 on lore, and this patch still only touches btf_dumper_enum64().

Was the request to cover btf_dumper_enum() intentionally left out?

btf_dumper_enum() still sign-extends the 1, 2 and 4 byte loads and always prints an unmatched value with jsonw_int():

static int btf_dumper_enum(const struct btf_dumper *d,
const struct btf_type *t, const void *data)

{
	...
	switch (t->size) {
	case 4:
		value = *(__s32 *)data;
		break;
	...
	}
	...
	jsonw_int(d->jw, value);
	return 0;
}

For example, an unsigned 4-byte enum holding 0xffffffff that matches no enumerator would still be printed as -1. Should btf_dumper_enum() pick the zero- or sign-extending load based on btf_kflag(t), compare enumerators with matching signedness, and call jsonw_uint() for unsigned enums, as in the diff from the v1 review?

---
AI reviewed your patch. Please fix the bug or email reply why it's not a bug.
See: https://github.com/kernel-patches/vmtest/blob/master/ci/claude/README.md

CI run summary: https://github.com/kernel-patches/bpf/actions/runs/41249994972