On Thu, Feb 10, 2022 at 2:18 PM Yonghong Song <yhs@xxxxxx> wrote: > > > > On 2/9/22 4:36 PM, Delyan Kratunov wrote: > > As reported in [0], kernel and userspace can sometimes disagree > > on the definition of a typedef (in particular, the size). > > This leads to trouble when userspace maps the memory of a bpf program > > and reads/writes to it assuming a different memory layout. > > > > This series resolves most int-like typedefs and rewrites them as > > standard int16_t-like types. In particular, we don't touch > > __u32-like types, char, and _Bool, as changing them changes cast > > semantics and would break too many pre-existing programs. For example, > > int8_t* is not convertible to char* because int8_t is explicitly signed. > > Build with clang (adding LLVM=1 to build kernel and selftests), > several btf_dump subtests failed. Please take a look. > > btf_dump_data:PASS:find type id 0 nsec > btf_dump_data:PASS:failed/unexpected type_sz 0 nsec > btf_dump_data:FAIL:ensure expected/actual match unexpected ensure > expected/actual match: actual '(int32_t)1234' != expected '(int)1234' > btf_dump_data:PASS:find type id 0 nsec > btf_dump_data:PASS:failed/unexpected type_sz 0 nsec > btf_dump_data:PASS:ensure expected/actual match 0 nsec > btf_dump_data:PASS:find type id 0 nsec > btf_dump_data:PASS:failed/unexpected type_sz 0 nsec > btf_dump_data:FAIL:ensure expected/actual match unexpected ensure > expected/actual match: actual '(int32_t)1234' != expected '(int)1234' > btf_dump_data:PASS:find type id 0 nsec > btf_dump_data:PASS:failed/unexpected type_sz 0 nsec > btf_dump_data:FAIL:ensure expected/actual match unexpected ensure > expected/actual match: actual '(int32_t)0' != expected '(int)0' > ... > btf_dump_data:FAIL:ensure expected/actual match unexpected ensure > expected/actual match: actual '(uint128_t)0xffffffffffffffff' != > expected '(unsigned __int128)0xffffffffffffffff' > btf_dump_data:PASS:find type id 0 nsec > btf_dump_data:PASS:failed/unexpected type_sz 0 nsec > btf_dump_data:FAIL:ensure expected/actual match unexpected ensure > expected/actual match: actual > '(uint128_t)0xfffffffffffffffffffffffffffffffe' != expected '(unsigned > __int128)0xfffffffffffffffffffffffffffff' > test_btf_dump_int_data:FAIL:dump unsigned __int128 unexpected error: -14 > (errno 7) > #20/9 btf_dump/btf_dump: int_data:FAIL > ... > btf_dump_data:FAIL:ensure expected/actual match unexpected ensure > expected/actual match: actual '(uint64_t)1' != expected '(u64)1' > btf_dump_data:PASS:find type id 0 nsec > btf_dump_data:PASS:failed/unexpected type_sz 0 nsec > btf_dump_data:FAIL:ensure expected/actual match unexpected ensure > expected/actual match: actual '(uint64_t)0' != expected '(u64)0' > btf_dump_data:PASS:find type id 0 nsec > ... > btf_dump_data:FAIL:ensure expected/actual match unexpected ensure > expected/actual match: actual '(atomic_t){ > .counter = (int32_t)0, > }' != expected '(atomic_t){ > .counter = (int)0, > }' > btf_dump_data:PASS:find type id 0 nsec > btf_dump_data:PASS:failed to return -E2BIG 0 nsec > btf_dump_data:PASS:ensure expected/actual match 0 nsec > #20/12 btf_dump/btf_dump: typedef_data:FAIL > ... > test_btf_dump_struct_data:FAIL:file_operations unexpected > file_operations: actual '(struct file_operations){ > .owner = (struct module *)0xffffffffffffffff, > .llseek = (int64_t (*)(struct file *, int64_t, > int32_t))0xfffffffffff' != expected '(struct file_operations){ > .owner = (struct module *)0xffffffffffffffff, > .llseek = (loff_t (*)(struct file *, loff_t, > int))0xffffffffffffffff,' > ... > ... > This is due to unconditional normalization in btf_dump_emit_type_cast(), we shouldn't do it. So this will "autofix" itself when we fix that issue. > > > > [0]: https://github.com/iovisor/bcc/pull/3777 > > > > Delyan Kratunov (3): > > libbpf: btf_dump can produce explicitly sized ints > > bpftool: skeleton uses explicitly sized ints > > selftests/bpf: add test case for userspace and bpf type size mismatch > > > > tools/bpf/bpftool/gen.c | 3 + > > tools/lib/bpf/btf.h | 4 +- > > tools/lib/bpf/btf_dump.c | 80 ++++++++++++++++++- > > .../selftests/bpf/prog_tests/skeleton.c | 22 +++-- > > .../selftests/bpf/progs/test_skeleton.c | 8 ++ > > 5 files changed, 107 insertions(+), 10 deletions(-) > > > > -- > > 2.34.1