All of lore.kernel.org
 help / color / mirror / Atom feed
From: Jiri Olsa <jolsa@redhat.com>
To: Andrii Nakryiko <andrii.nakryiko@gmail.com>
Cc: Andrii Nakryiko <andriin@fb.com>,
	Alexei Starovoitov <ast@kernel.org>,
	Daniel Borkmann <daniel@iogearbox.net>,
	Networking <netdev@vger.kernel.org>, bpf <bpf@vger.kernel.org>,
	Martin KaFai Lau <kafai@fb.com>, Song Liu <songliubraving@fb.com>,
	Yonghong Song <yhs@fb.com>,
	John Fastabend <john.fastabend@gmail.com>,
	KP Singh <kpsingh@chromium.org>,
	Andrii Nakryiko <andrii@kernel.org>
Subject: Re: [PATCH bpf-next 3/3] selftests/bpf: Add tests for get_func_[arg|ret|arg_cnt] helpers
Date: Wed, 8 Dec 2021 17:38:14 +0100	[thread overview]
Message-ID: <YbDfdiW3/uPT3J1O@krava> (raw)
In-Reply-To: <CAEf4BzbAVO-SGub8+haDayhnL_VLyYAof8eUB_d6Qp18yC2Adw@mail.gmail.com>

On Tue, Dec 07, 2021 at 02:54:33PM -0800, Andrii Nakryiko wrote:

SNIP

> > > > +__u64 test1_result = 0;
> > > > +SEC("fentry/bpf_fentry_test1")
> > > > +int BPF_PROG(test1)
> > > > +{
> > > > +   __u64 cnt = bpf_get_func_arg_cnt(ctx);
> > > > +   __u64 a = 0, z = 0, ret = 0;
> > > > +   __s64 err;
> > > > +
> > > > +   test1_result = cnt == 1;
> > > > +
> > > > +   /* valid arguments */
> > > > +   err = bpf_get_func_arg(ctx, 0, &a);
> > > > +   test1_result &= err == 0 && (int) a == 1;
> > >
> > >
> > > int cast unnecessary? but some ()'s wouldn't hurt...
> >
> > it is, 'a' is int and trampoline saves it with 32-bit register like:
> >
> >   mov    %edi,-0x8(%rbp)
> >
> > so the upper 4 bytes are not zeroed
> 
> oh, this is definitely worth a comment, it's quite a big gotcha we'll
> need to remember


ok, will add comment for that

jirka

> 
> 
> >
> > >
> > >
> > > > +
> > > > +   /* not valid argument */
> > > > +   err = bpf_get_func_arg(ctx, 1, &z);
> > > > +   test1_result &= err == -EINVAL;
> > > > +
> > > > +   /* return value fails in fentry */
> > > > +   err = bpf_get_func_ret(ctx, &ret);
> > > > +   test1_result &= err == -EINVAL;
> > > > +   return 0;
> > > > +}
> > > > +
> > > > +__u64 test2_result = 0;
> > > > +SEC("fexit/bpf_fentry_test2")
> > > > +int BPF_PROG(test2)
> > > > +{
> > > > +   __u64 cnt = bpf_get_func_arg_cnt(ctx);
> > > > +   __u64 a = 0, b = 0, z = 0, ret = 0;
> > > > +   __s64 err;
> > > > +
> > > > +   test2_result = cnt == 2;
> > > > +
> > > > +   /* valid arguments */
> > > > +   err = bpf_get_func_arg(ctx, 0, &a);
> > > > +   test2_result &= err == 0 && (int) a == 2;
> > > > +
> > > > +   err = bpf_get_func_arg(ctx, 1, &b);
> > > > +   test2_result &= err == 0 && b == 3;
> > > > +
> > > > +   /* not valid argument */
> > > > +   err = bpf_get_func_arg(ctx, 2, &z);
> > > > +   test2_result &= err == -EINVAL;
> > > > +
> > > > +   /* return value */
> > > > +   err = bpf_get_func_ret(ctx, &ret);
> > > > +   test2_result &= err == 0 && ret == 5;
> > > > +   return 0;
> > > > +}
> > > > +
> > > > +__u64 test3_result = 0;
> > > > +SEC("fmod_ret/bpf_modify_return_test")
> > > > +int BPF_PROG(fmod_ret_test, int _a, int *_b, int _ret)
> > > > +{
> > > > +   __u64 cnt = bpf_get_func_arg_cnt(ctx);
> > > > +   __u64 a = 0, b = 0, z = 0, ret = 0;
> > > > +   __s64 err;
> > > > +
> > > > +   test3_result = cnt == 2;
> > > > +
> > > > +   /* valid arguments */
> > > > +   err = bpf_get_func_arg(ctx, 0, &a);
> > > > +   test3_result &= err == 0 && (int) a == 1;
> > > > +
> > > > +   err = bpf_get_func_arg(ctx, 1, &b);
> > > > +   test3_result &= err == 0;
> > >
> > >
> > > why no checking of b value here?
> >
> > right, ok
> >
> > >
> > > > +
> > > > +   /* not valid argument */
> > > > +   err = bpf_get_func_arg(ctx, 2, &z);
> > > > +   test3_result &= err == -EINVAL;
> > > > +
> > > > +   /* return value */
> > > > +   err = bpf_get_func_ret(ctx, &ret);
> > > > +   test3_result &= err == 0 && ret == 0;
> > > > +   return 1234;
> > > > +}
> > > > +
> > > > +__u64 test4_result = 0;
> > > > +SEC("fexit/bpf_modify_return_test")
> > > > +int BPF_PROG(fexit_test, int _a, __u64 _b, int _ret)
> > > > +{
> > > > +   __u64 cnt = bpf_get_func_arg_cnt(ctx);
> > > > +   __u64 a = 0, b = 0, z = 0, ret = 0;
> > > > +   __s64 err;
> > > > +
> > > > +   test4_result = cnt == 2;
> > > > +
> > > > +   /* valid arguments */
> > > > +   err = bpf_get_func_arg(ctx, 0, &a);
> > > > +   test4_result &= err == 0 && (int) a == 1;
> > > > +
> > > > +   err = bpf_get_func_arg(ctx, 1, &b);
> > > > +   test4_result &= err == 0;
> > >
> > >
> > > same, for consistency, b should have been checked, no?
> >
> > ok
> >
> > thanks,
> > jirka
> >
> 


      reply	other threads:[~2021-12-08 16:39 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-12-04 14:06 [PATCH bpf-next 0/3] bpf: Add helpers to access traced function arguments Jiri Olsa
2021-12-04 14:06 ` [PATCH bpf-next 1/3] bpf, x64: Replace some stack_size usage with offset variables Jiri Olsa
2021-12-06 19:19   ` John Fastabend
2021-12-06 21:26   ` Andrii Nakryiko
2021-12-06 21:41   ` Andrii Nakryiko
2021-12-07 14:25     ` Jiri Olsa
2021-12-04 14:06 ` [PATCH bpf-next 2/3] bpf: Add get_func_[arg|ret|arg_cnt] helpers Jiri Olsa
2021-12-06 19:39   ` John Fastabend
2021-12-06 20:17     ` Jiri Olsa
2021-12-06 21:54   ` Andrii Nakryiko
2021-12-07 17:23     ` Jiri Olsa
2021-12-04 14:07 ` [PATCH bpf-next 3/3] selftests/bpf: Add tests for " Jiri Olsa
2021-12-06 22:03   ` Andrii Nakryiko
2021-12-07 18:14     ` Jiri Olsa
2021-12-07 22:54       ` Andrii Nakryiko
2021-12-08 16:38         ` Jiri Olsa [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=YbDfdiW3/uPT3J1O@krava \
    --to=jolsa@redhat.com \
    --cc=andrii.nakryiko@gmail.com \
    --cc=andrii@kernel.org \
    --cc=andriin@fb.com \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=john.fastabend@gmail.com \
    --cc=kafai@fb.com \
    --cc=kpsingh@chromium.org \
    --cc=netdev@vger.kernel.org \
    --cc=songliubraving@fb.com \
    --cc=yhs@fb.com \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.