Skip to content

Commit 5779a43

Browse files
committed
Fix integer signedness in BTF parser
Signed-off-by: Cong Wang <cwang@multikernel.io>
1 parent 485950e commit 5779a43

File tree

1 file changed

+26
-6
lines changed

1 file changed

+26
-6
lines changed

src/btf_stubs.c

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,17 @@
2929
/* #define DEBUG_PRINT(...) fprintf(stderr, __VA_ARGS__) */
3030
#define DEBUG_PRINT(...)
3131

32+
/* BTF integer encoding macros (if not already defined in libbpf) */
33+
#ifndef BTF_INT_ENCODING
34+
#define BTF_INT_ENCODING(VAL) (((VAL) & 0x0f000000) >> 24)
35+
#endif
36+
#ifndef BTF_INT_OFFSET
37+
#define BTF_INT_OFFSET(VAL) (((VAL) & 0x00ff0000) >> 16)
38+
#endif
39+
#ifndef BTF_INT_BITS
40+
#define BTF_INT_BITS(VAL) ((VAL) & 0x000000ff)
41+
#endif
42+
3243
/* Custom block for BTF handle */
3344
#define BTF_HANDLE_TAG 0
3445

@@ -267,14 +278,23 @@ static char* resolve_type_to_string(struct btf *btf, int type_id) {
267278
}
268279

269280
switch (kind) {
270-
case BTF_KIND_INT:
281+
case BTF_KIND_INT: {
282+
/* Check encoding to determine if signed or unsigned */
283+
__u32 *info_ptr = (__u32 *)(t + 1);
284+
__u32 info = *info_ptr;
285+
__u32 encoding = BTF_INT_ENCODING(info);
286+
287+
/* BTF_INT_SIGNED is defined as 0x1 in BTF specification */
288+
int is_signed = (encoding & 0x1) != 0;
289+
271290
switch (t->size) {
272-
case 1: return strdup("u8");
273-
case 2: return strdup("u16");
274-
case 4: return strdup("u32");
275-
case 8: return strdup("u64");
276-
default: return strdup("u32");
291+
case 1: return strdup(is_signed ? "i8" : "u8");
292+
case 2: return strdup(is_signed ? "i16" : "u16");
293+
case 4: return strdup(is_signed ? "i32" : "u32");
294+
case 8: return strdup(is_signed ? "i64" : "u64");
295+
default: return strdup(is_signed ? "i32" : "u32");
277296
}
297+
}
278298
case BTF_KIND_STRUCT:
279299
case BTF_KIND_UNION:
280300
case BTF_KIND_ENUM: {

0 commit comments

Comments
 (0)