All of lore.kernel.org
 help / color / mirror / Atom feed
From: John Fastabend <john.fastabend@gmail.com>
To: Alexei Starovoitov <alexei.starovoitov@gmail.com>,
	John Fastabend <john.fastabend@gmail.com>
Cc: Daniel Borkmann <daniel@iogearbox.net>, bpf <bpf@vger.kernel.org>,
	Yonghong Song <yhs@fb.com>, Alexei Starovoitov <ast@kernel.org>
Subject: Re: [bpf PATCH v3] bpf: verifier, do_refine_retval_range may clamp umin to 0 incorrectly
Date: Fri, 31 Jan 2020 09:16:03 -0800	[thread overview]
Message-ID: <5e3460d3a3fb1_4a9b2ab23eff45b82c@john-XPS-13-9370.notmuch> (raw)
In-Reply-To: <CAADnVQL+hBuz8AgJ-Tv8iWFoGdpXwSmdqHVzX5NgR_1Lfpx3Yw@mail.gmail.com>

Alexei Starovoitov wrote:
> On Thu, Jan 30, 2020 at 9:48 PM John Fastabend <john.fastabend@gmail.com> wrote:
> > at the moment. I'll take a look in the morning. That fragment 55,56,
> > 57 are coming from a zext in llvm though.
> 

OK I see the disconnect now. I don't get the same instructions at all. I only
have the <<32 >> 32, no signed shift s>>32.

> I don't think so. Here is how IR looks after all optimizations
> and right before instruction selection:

Same llvm ir though

>   %call12 = call i32 inttoptr (i64 67 to i32 (i8*, i8*, i32,
> i64)*)(i8* %ctx, i8* nonnull %call8, i32 800, i64 256) #2
>   %cmp = icmp slt i32 %call12, 0
>   br i1 %cmp, label %cleanup, label %if.end15
> 
> if.end15:                                         ; preds = %if.end11
>   %idx.ext70 = zext i32 %call12 to i64
>   %add.ptr = getelementptr i8, i8* %call8, i64 %idx.ext70
>   %sub = sub nsw i32 800, %call12
>   %call16 = call i32 inttoptr (i64 67 to i32 (i8*, i8*, i32,
> i64)*)(i8* %ctx, i8* %add.ptr, i32 %sub, i64 0) #2
>   %cmp17 = icmp slt i32 %call16, 0
>   br i1 %cmp17, label %cleanup, label %if.end20
> 

 %26 = call i32 inttoptr (i64 67 to i32 (i8*, i8*, i32, i64)*)(i8* %0, i8* nonnull %23, i32 800, i64 256) #3, !dbg !166
  %27 = icmp slt i32 %26, 0, !dbg !167
  br i1 %27, label %41, label %28, !dbg !169

28:                                               ; preds = %25
  %29 = zext i32 %26 to i64, !dbg !170
  %30 = getelementptr i8, i8* %23, i64 %29, !dbg !170


> and corresponding C code:
>         usize = bpf_get_stack(ctx, raw_data, max_len, BPF_F_USER_STACK);
>         if (usize < 0)
>                 return 0;
> 
>         ksize = bpf_get_stack(ctx, raw_data + usize, max_len - usize, 0);
>         if (ksize < 0)

same as well. But my object code only has this (unpatched llvm)

      56:       bc 81 00 00 00 00 00 00 w1 = w8
      57:       67 01 00 00 20 00 00 00 r1 <<= 32
      58:       77 01 00 00 20 00 00 00 r1 >>= 32

> 
> %idx.ext70 = zext i32 %call12 to i64
> that you see is a part of 'raw_data + usize' math.
> The result of first bpf_get_stack() is directly passed into
> "icmp slt i32 %call12, 0"
> and during instruction selection the backend does
> sign extension with <<32 s>>32.

Assuming latest bpf tree and llvm master branch? 

> 
> I agree that peephole zext->mov32_64 is correct and a nice optimization,
> but I still don't see how it helps this case.

Also don't mind to build pseudo instruction here for signed extension
but its not clear to me why we are getting different instruction
selections? Its not clear to me why sext is being chosen in your case?

.John

  reply	other threads:[~2020-01-31 17:16 UTC|newest]

Thread overview: 27+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-01-27 19:29 [bpf PATCH v3] bpf: verifier, do_refine_retval_range may clamp umin to 0 incorrectly John Fastabend
2020-01-29 16:25 ` Daniel Borkmann
2020-01-29 19:28   ` Alexei Starovoitov
2020-01-29 22:20     ` Daniel Borkmann
2020-01-29 22:52       ` John Fastabend
2020-01-30  0:04         ` Alexei Starovoitov
2020-01-30 17:38           ` John Fastabend
2020-01-30 17:59             ` Alexei Starovoitov
2020-01-30 23:34               ` John Fastabend
2020-01-31  0:15                 ` Yonghong Song
2020-01-31  0:44                   ` John Fastabend
2020-01-31  0:52                     ` Yonghong Song
2020-01-31  2:50                     ` Alexei Starovoitov
2020-01-31  0:28                 ` Yonghong Song
2020-01-31  0:48                   ` John Fastabend
2020-01-31  2:46                 ` Alexei Starovoitov
2020-01-31  5:48                   ` John Fastabend
2020-01-31  6:18                     ` Alexei Starovoitov
2020-01-31 17:16                       ` John Fastabend [this message]
2020-01-31 21:36                         ` Alexei Starovoitov
2020-02-04 19:55                           ` John Fastabend
2020-02-05  1:21                             ` Yonghong Song
2020-02-05  3:05                               ` John Fastabend
2020-02-06  1:24                                 ` Yonghong Song
2020-02-07 20:47                                   ` John Fastabend
2020-02-08  6:23                                     ` Yonghong Song
2020-04-09 15:03 ` Lorenzo Fontana

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=5e3460d3a3fb1_4a9b2ab23eff45b82c@john-XPS-13-9370.notmuch \
    --to=john.fastabend@gmail.com \
    --cc=alexei.starovoitov@gmail.com \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --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.