bpf.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Andrii Nakryiko <andrii.nakryiko@gmail.com>
To: Martin KaFai Lau <kafai@fb.com>
Cc: Andrii Nakryiko <andriin@fb.com>, bpf <bpf@vger.kernel.org>,
	Networking <netdev@vger.kernel.org>,
	Alexei Starovoitov <ast@fb.com>,
	Daniel Borkmann <daniel@iogearbox.net>,
	Kernel Team <kernel-team@fb.com>
Subject: Re: [Potential Spoof] [PATCH bpf-next 4/4] selftests/bpf: add vmlinux.h selftest exercising tracing of syscalls
Date: Fri, 13 Mar 2020 10:16:24 -0700	[thread overview]
Message-ID: <CAEf4BzbZ0rQ=Lwvvy1Kk8jQqRdHzPNodcXTHD=i1px7DKa-YsA@mail.gmail.com> (raw)
In-Reply-To: <20200313170212.lf4lnljwtvhypkew@kafai-mbp>

On Fri, Mar 13, 2020 at 10:02 AM Martin KaFai Lau <kafai@fb.com> wrote:
>
> On Fri, Mar 13, 2020 at 12:54:41AM -0700, Andrii Nakryiko wrote:
> > Add vmlinux.h generation to selftest/bpf's Makefile. Use it from newly added
> > test_vmlinux to trace nanosleep syscall using 5 different types of programs:
> >   - tracepoint;
> >   - raw tracepoint;
> >   - raw tracepoint w/ direct memory reads (tp_btf);
> >   - kprobe;
> >   - fentry.
> >
> > These programs are realistic variants of real-life tracing programs,
> > excercising vmlinux.h's usage with tracing applications.
> >
> > Signed-off-by: Andrii Nakryiko <andriin@fb.com>
> > ---
> [ ... ]
>
> > diff --git a/tools/testing/selftests/bpf/prog_tests/vmlinux.c b/tools/testing/selftests/bpf/prog_tests/vmlinux.c
> > new file mode 100644
> > index 000000000000..04939eda1325
> > --- /dev/null
> > +++ b/tools/testing/selftests/bpf/prog_tests/vmlinux.c
> > @@ -0,0 +1,43 @@
> > +// SPDX-License-Identifier: GPL-2.0
> > +/* Copyright (c) 2020 Facebook */
> > +
> > +#include <test_progs.h>
> > +#include <time.h>
> > +#include "test_vmlinux.skel.h"
> > +
> > +#define MY_TV_NSEC 1337
> > +
> > +static void nsleep()
> > +{
> > +     struct timespec ts = { .tv_nsec = MY_TV_NSEC };
> > +
> > +     (void)nanosleep(&ts, NULL);
> > +}
> > +
> > +void test_vmlinux(void)
> > +{
> > +     int duration = 0, err;
> > +     struct test_vmlinux* skel;
> > +     struct test_vmlinux__bss *bss;
> > +
> > +     skel = test_vmlinux__open_and_load();
> > +     if (CHECK(!skel, "skel_open", "failed to open skeleton\n"))
> > +             return;
> > +     bss = skel->bss;
> > +
> > +     err = test_vmlinux__attach(skel);
> > +     if (CHECK(err, "skel_attach", "skeleton attach failed: %d\n", err))
> > +             goto cleanup;
> > +
> > +     /* trigger everything */
> > +     nsleep();
> > +
> > +     CHECK(!bss->tp_called, "tp", "not called\n");
> > +     CHECK(!bss->raw_tp_called, "raw_tp", "not called\n");
> > +     CHECK(!bss->tp_btf_called, "tp_btf", "not called\n");
> > +     CHECK(!bss->kprobe_called, "kprobe", "not called\n");
> > +     CHECK(!bss->fentry_called, "fentry", "not called\n");
> > +
> > +cleanup:
> > +     test_vmlinux__destroy(skel);
> > +}
> > diff --git a/tools/testing/selftests/bpf/progs/test_vmlinux.c b/tools/testing/selftests/bpf/progs/test_vmlinux.c
> > new file mode 100644
> > index 000000000000..5cc2bf8011b0
> > --- /dev/null
> > +++ b/tools/testing/selftests/bpf/progs/test_vmlinux.c
> > @@ -0,0 +1,98 @@
> > +// SPDX-License-Identifier: GPL-2.0
> > +/* Copyright (c) 2020 Facebook */
> > +
> > +#include "vmlinux.h"
> > +#include <asm/unistd.h>
> > +#include <bpf/bpf_helpers.h>
> > +#include <bpf/bpf_tracing.h>
> > +#include <bpf/bpf_core_read.h>
> > +
> > +#define MY_TV_NSEC 1337
> > +
> > +bool tp_called = false;
> > +bool raw_tp_called = false;
> > +bool tp_btf_called = false;
> > +bool kprobe_called = false;
> > +bool fentry_called = false;
> > +
> > +SEC("tp/syscalls/sys_enter_nanosleep")
> > +int handle__tp(struct trace_event_raw_sys_enter *args)
> > +{
> > +     struct __kernel_timespec *ts;
> > +
> > +     if (args->id != __NR_nanosleep)
> > +             return 0;
> > +
> > +     ts = (void *)args->args[0];
> > +     if (BPF_CORE_READ(ts, tv_nsec) != MY_TV_NSEC)
> > +             return 0;
> > +
> > +     tp_called = true;
> > +     return 0;
> > +}
> > +
> > +static bool __always_inline handle_probed(struct pt_regs *regs, long id)
> It is not used, may be removing it?
>

Oh, did I really leave it around?... Sigh, will post v2 without it.


> > +{
> > +     struct __kernel_timespec *ts;
> > +
> > +     if (id != __NR_nanosleep)
> > +             return false;
> > +
> > +     ts = (void *)PT_REGS_PARM1_CORE(regs);
> > +     if (BPF_CORE_READ(ts, tv_nsec) != MY_TV_NSEC)
> > +             return false;
> > +
> > +     return true;
> > +}

      reply	other threads:[~2020-03-13 17:16 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-03-13  7:54 [PATCH bpf-next 0/4] CO-RE candidate matching fix and tracing test Andrii Nakryiko
2020-03-13  7:54 ` [PATCH bpf-next 1/4] selftests/bpf: ensure consistent test failure output Andrii Nakryiko
2020-03-13  7:54 ` [PATCH bpf-next 2/4] libbpf: ignore incompatible types with matching name during CO-RE relocation Andrii Nakryiko
2020-03-13  7:54 ` [PATCH bpf-next 3/4] libbpf: provide CO-RE variants of PT_REGS macros Andrii Nakryiko
2020-03-13  7:54 ` [PATCH bpf-next 4/4] selftests/bpf: add vmlinux.h selftest exercising tracing of syscalls Andrii Nakryiko
2020-03-13 17:02   ` [Potential Spoof] " Martin KaFai Lau
2020-03-13 17:16     ` Andrii Nakryiko [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CAEf4BzbZ0rQ=Lwvvy1Kk8jQqRdHzPNodcXTHD=i1px7DKa-YsA@mail.gmail.com' \
    --to=andrii.nakryiko@gmail.com \
    --cc=andriin@fb.com \
    --cc=ast@fb.com \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=kafai@fb.com \
    --cc=kernel-team@fb.com \
    --cc=netdev@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).