All of lore.kernel.org
 help / color / mirror / Atom feed
From: will.deacon@arm.com (Will Deacon)
To: linux-arm-kernel@lists.infradead.org
Subject: [PATCH] arm64: bpf: fix signedness bug in loading 64-bit immediate
Date: Fri, 8 May 2015 09:38:57 +0100	[thread overview]
Message-ID: <20150508083856.GA2125@arm.com> (raw)
In-Reply-To: <1431063591-16668-1-git-send-email-xi.wang@gmail.com>

On Fri, May 08, 2015 at 06:39:51AM +0100, Xi Wang wrote:
> Consider "(u64)insn1.imm << 32 | imm" in the arm64 JIT.  Since imm is
> signed 32-bit, it is sign-extended to 64-bit, losing the high 32 bits.
> The fix is to convert imm to u32 first and zero-extend it to u64.
> 
> Also extend test_bpf to catch this JIT bug; the interpreter is correct.
> 
> Before:
> test_bpf: #58 load 64-bit immediate ret -1 != 1 FAIL (1 times)
> 
> After:
> test_bpf: #58 load 64-bit immediate 74 PASS
> 
> Fixes: 30d3d94cc3d5 ("arm64: bpf: add 'load 64-bit immediate' instruction")
> Cc: Zi Shen Lim <zlim.lnx@gmail.com>
> Cc: Alexei Starovoitov <ast@plumgrid.com>
> Cc: Catalin Marinas <catalin.marinas@arm.com>
> Cc: Will Deacon <will.deacon@arm.com>
> Signed-off-by: Xi Wang <xi.wang@gmail.com>
> ---
>  arch/arm64/net/bpf_jit_comp.c | 2 +-
>  lib/test_bpf.c                | 3 ++-
>  2 files changed, 3 insertions(+), 2 deletions(-)
> 
> diff --git a/arch/arm64/net/bpf_jit_comp.c b/arch/arm64/net/bpf_jit_comp.c
> index edba042b2325..14cdc099fda0 100644
> --- a/arch/arm64/net/bpf_jit_comp.c
> +++ b/arch/arm64/net/bpf_jit_comp.c
> @@ -487,7 +487,7 @@ emit_cond_jmp:
>  			return -EINVAL;
>  		}
>  
> -		imm64 = (u64)insn1.imm << 32 | imm;
> +		imm64 = ((u64)(u32)insn1.imm) << 32 | (u64)(u32)imm;

This seems a bit convoluted to me. Don't you just need to add a (u32)
cast to imm and that's it? The (u64)(u32) looks redundant.

>  		emit_a64_mov_i64(dst, imm64, ctx);
>  
>  		return 1;
> diff --git a/lib/test_bpf.c b/lib/test_bpf.c
> index 80d78c51f65f..9f6849891b5f 100644
> --- a/lib/test_bpf.c
> +++ b/lib/test_bpf.c
> @@ -1755,7 +1755,8 @@ static struct bpf_test tests[] = {
>  			BPF_EXIT_INSN(),
>  			BPF_JMP_IMM(BPF_JEQ, R3, 0x1234, 1),
>  			BPF_EXIT_INSN(),
> -			BPF_ALU64_IMM(BPF_MOV, R0, 1),
> +			BPF_LD_IMM64(R0, 0x1ffffffffLL),
> +			BPF_ALU64_IMM(BPF_RSH, R0, 32), /* R0 = 1 */
>  			BPF_EXIT_INSN(),

This hunk should probably be a separate patch, unless you get Alexei's ack
for me to take it via the arm64 tree too.

Will

WARNING: multiple messages have this Message-ID (diff)
From: Will Deacon <will.deacon@arm.com>
To: Xi Wang <xi.wang@gmail.com>
Cc: "linux-arm-kernel@lists.infradead.org" 
	<linux-arm-kernel@lists.infradead.org>,
	"linux-kernel@vger.kernel.org" <linux-kernel@vger.kernel.org>,
	Zi Shen Lim <zlim.lnx@gmail.com>,
	Alexei Starovoitov <ast@plumgrid.com>,
	Catalin Marinas <Catalin.Marinas@arm.com>
Subject: Re: [PATCH] arm64: bpf: fix signedness bug in loading 64-bit immediate
Date: Fri, 8 May 2015 09:38:57 +0100	[thread overview]
Message-ID: <20150508083856.GA2125@arm.com> (raw)
In-Reply-To: <1431063591-16668-1-git-send-email-xi.wang@gmail.com>

On Fri, May 08, 2015 at 06:39:51AM +0100, Xi Wang wrote:
> Consider "(u64)insn1.imm << 32 | imm" in the arm64 JIT.  Since imm is
> signed 32-bit, it is sign-extended to 64-bit, losing the high 32 bits.
> The fix is to convert imm to u32 first and zero-extend it to u64.
> 
> Also extend test_bpf to catch this JIT bug; the interpreter is correct.
> 
> Before:
> test_bpf: #58 load 64-bit immediate ret -1 != 1 FAIL (1 times)
> 
> After:
> test_bpf: #58 load 64-bit immediate 74 PASS
> 
> Fixes: 30d3d94cc3d5 ("arm64: bpf: add 'load 64-bit immediate' instruction")
> Cc: Zi Shen Lim <zlim.lnx@gmail.com>
> Cc: Alexei Starovoitov <ast@plumgrid.com>
> Cc: Catalin Marinas <catalin.marinas@arm.com>
> Cc: Will Deacon <will.deacon@arm.com>
> Signed-off-by: Xi Wang <xi.wang@gmail.com>
> ---
>  arch/arm64/net/bpf_jit_comp.c | 2 +-
>  lib/test_bpf.c                | 3 ++-
>  2 files changed, 3 insertions(+), 2 deletions(-)
> 
> diff --git a/arch/arm64/net/bpf_jit_comp.c b/arch/arm64/net/bpf_jit_comp.c
> index edba042b2325..14cdc099fda0 100644
> --- a/arch/arm64/net/bpf_jit_comp.c
> +++ b/arch/arm64/net/bpf_jit_comp.c
> @@ -487,7 +487,7 @@ emit_cond_jmp:
>  			return -EINVAL;
>  		}
>  
> -		imm64 = (u64)insn1.imm << 32 | imm;
> +		imm64 = ((u64)(u32)insn1.imm) << 32 | (u64)(u32)imm;

This seems a bit convoluted to me. Don't you just need to add a (u32)
cast to imm and that's it? The (u64)(u32) looks redundant.

>  		emit_a64_mov_i64(dst, imm64, ctx);
>  
>  		return 1;
> diff --git a/lib/test_bpf.c b/lib/test_bpf.c
> index 80d78c51f65f..9f6849891b5f 100644
> --- a/lib/test_bpf.c
> +++ b/lib/test_bpf.c
> @@ -1755,7 +1755,8 @@ static struct bpf_test tests[] = {
>  			BPF_EXIT_INSN(),
>  			BPF_JMP_IMM(BPF_JEQ, R3, 0x1234, 1),
>  			BPF_EXIT_INSN(),
> -			BPF_ALU64_IMM(BPF_MOV, R0, 1),
> +			BPF_LD_IMM64(R0, 0x1ffffffffLL),
> +			BPF_ALU64_IMM(BPF_RSH, R0, 32), /* R0 = 1 */
>  			BPF_EXIT_INSN(),

This hunk should probably be a separate patch, unless you get Alexei's ack
for me to take it via the arm64 tree too.

Will

  reply	other threads:[~2015-05-08  8:38 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-05-08  5:39 [PATCH] arm64: bpf: fix signedness bug in loading 64-bit immediate Xi Wang
2015-05-08  5:39 ` Xi Wang
2015-05-08  8:38 ` Will Deacon [this message]
2015-05-08  8:38   ` Will Deacon
2015-05-08  8:45   ` Xi Wang
2015-05-08  8:45     ` Xi Wang
2015-05-08 15:17     ` Will Deacon
2015-05-08 15:17       ` Will Deacon
2015-05-08 15:30       ` Alexei Starovoitov
2015-05-08 15:30         ` Alexei Starovoitov

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=20150508083856.GA2125@arm.com \
    --to=will.deacon@arm.com \
    --cc=linux-arm-kernel@lists.infradead.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 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.