On Wed, Aug 11, 2021 at 2:47 PM Andrii Nakryiko <andrii.nakryiko@xxxxxxxxx> wrote: > > On Tue, Aug 10, 2021 at 11:22 AM Hao Luo <haoluo@xxxxxxxxxx> wrote: > > > > Currently weak typeless ksyms have default value zero, when they don't > > exist in the kernel. However, weak typed ksyms are rejected by libbpf > > if they can not be resolved. This means that if a bpf object contains > > the declaration of a nonexistent weak typed ksym, it will be rejected > > even if there is no program that references the symbol. > > > > Nonexistent weak typed ksyms can also default to zero just like > > typeless ones. This allows programs that access weak typed ksyms to be > > accepted by verifier, if the accesses are guarded. For example, > > > > extern const int bpf_link_fops3 __ksym __weak; > > > > /* then in BPF program */ > > > > if (&bpf_link_fops3) { > > /* use bpf_link_fops3 */ > > } > > > > If actual use of nonexistent typed ksym is not guarded properly, > > verifier would see that register is not PTR_TO_BTF_ID and wouldn't > > allow to use it for direct memory reads or passing it to BPF helpers. > > > > Signed-off-by: Hao Luo <haoluo@xxxxxxxxxx> > > --- > > Selftests are failing ([0]). Did they work locally?.. > > test_weak_syms:PASS:test_ksyms_weak__open_and_load 0 nsec > test_weak_syms:FAIL:existing typed ksym unexpected existing typed > ksym: actual -1 != expected 0 > test_weak_syms:FAIL:existing typeless ksym unexpected existing > typeless ksym: actual -1 == expected -1 > test_weak_syms:FAIL:nonexistent typeless ksym unexpected nonexistent > typeless ksym: actual -1 != expected 0 > test_weak_syms:FAIL:nonexistent typed ksym unexpected nonexistent > typed ksym: actual -1 != expected 0 > #59/3 weak_ksyms:FAIL > #59 ksyms_btf:FAIL > Doh, in the test I accidentally removed the line of attachment in the last minute of sending out... :) Let me resend quickly. > > [0] https://github.com/kernel-patches/bpf/pull/1611/checks?check_run_id=3305288074 > > > Changes since v1: > > - Weak typed symbols default to zero, as suggested by Andrii. > > - Use ASSERT_XXX() for tets. > > > > tools/lib/bpf/libbpf.c | 17 ++++-- > > .../selftests/bpf/prog_tests/ksyms_btf.c | 25 ++++++++ > > .../selftests/bpf/progs/test_ksyms_weak.c | 57 +++++++++++++++++++ > > 3 files changed, 94 insertions(+), 5 deletions(-) > > create mode 100644 tools/testing/selftests/bpf/progs/test_ksyms_weak.c > > > > [...]