Commit 441420a1 authored by Andrii Nakryiko's avatar Andrii Nakryiko Committed by Alexei Starovoitov

bpf: Reliably preserve btf_trace_xxx types

btf_trace_xxx types, crucial for tp_btf BPF programs (raw tracepoint with
verifier-checked direct memory access), have to be preserved in kernel BTF to
allow verifier do its job and enforce type/memory safety. It was reported
([0]) that for kernels built with Clang current type-casting approach doesn't
preserve these types.

This patch fixes it by declaring an anonymous union for each registered
tracepoint, capturing both struct bpf_raw_event_map information, as well as
recording btf_trace_##call type reliably. Structurally, it's still the same
content as for a plain struct bpf_raw_event_map, so no other changes are
necessary.

  [0] https://github.com/iovisor/bcc/issues/2770#issuecomment-591007692

Fixes: e8c423fb ("bpf: Add typecast to raw_tracepoints to help BTF generation")
Reported-by: default avatarWenbo Zhang <ethercflow@gmail.com>
Signed-off-by: default avatarAndrii Nakryiko <andriin@fb.com>
Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
Acked-by: default avatarYonghong Song <yhs@fb.com>
Link: https://lore.kernel.org/bpf/20200301081045.3491005-2-andriin@fb.com
parent 82dbbfc8
...@@ -75,13 +75,17 @@ static inline void bpf_test_probe_##call(void) \ ...@@ -75,13 +75,17 @@ static inline void bpf_test_probe_##call(void) \
check_trace_callback_type_##call(__bpf_trace_##template); \ check_trace_callback_type_##call(__bpf_trace_##template); \
} \ } \
typedef void (*btf_trace_##call)(void *__data, proto); \ typedef void (*btf_trace_##call)(void *__data, proto); \
static struct bpf_raw_event_map __used \ static union { \
__attribute__((section("__bpf_raw_tp_map"))) \ struct bpf_raw_event_map event; \
__bpf_trace_tp_map_##call = { \ btf_trace_##call handler; \
} __bpf_trace_tp_map_##call __used \
__attribute__((section("__bpf_raw_tp_map"))) = { \
.event = { \
.tp = &__tracepoint_##call, \ .tp = &__tracepoint_##call, \
.bpf_func = (void *)(btf_trace_##call)__bpf_trace_##template, \ .bpf_func = __bpf_trace_##template, \
.num_args = COUNT_ARGS(args), \ .num_args = COUNT_ARGS(args), \
.writable_size = size, \ .writable_size = size, \
}, \
}; };
#define FIRST(x, ...) x #define FIRST(x, ...) x
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment