From patchwork Fri May 8 05:39:51 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Xi Wang X-Patchwork-Id: 6362451 Return-Path: X-Original-To: patchwork-linux-arm@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork2.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork2.web.kernel.org (Postfix) with ESMTP id 75BEBBEEE1 for ; Fri, 8 May 2015 05:44:10 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id A748520389 for ; Fri, 8 May 2015 05:44:09 +0000 (UTC) Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.9]) (using TLSv1.2 with cipher DHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id C690020149 for ; Fri, 8 May 2015 05:44:08 +0000 (UTC) Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.80.1 #2 (Red Hat Linux)) id 1Yqb1M-0003qt-9Y; Fri, 08 May 2015 05:40:24 +0000 Received: from mail-qc0-x229.google.com ([2607:f8b0:400d:c01::229]) by bombadil.infradead.org with esmtps (Exim 4.80.1 #2 (Red Hat Linux)) id 1Yqb1I-0002jN-8R for linux-arm-kernel@lists.infradead.org; Fri, 08 May 2015 05:40:20 +0000 Received: by qcyk17 with SMTP id k17so32553253qcy.1 for ; Thu, 07 May 2015 22:39:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=from:to:cc:subject:date:message-id; bh=0qFx7I/t1jvZ/nYMy9VASOTb9J0wDH/qiqJUDBdidI4=; b=CwivDrPbe0wGEVQsFkNO/gFtizY1M9CWFC6bJVoakmqlpbZD1GhE9UNGQ8Zmssq/9w Kiz/RRxO3biCKZjTpAthu4mbCD+Q2H2xWTYqJu47mgavEqW3UAF1VQRCxvlLZHkQUyC+ 00U0gCOck6rdgCLc6xO2FQS1ukplnHmka3KJEKUxjQEyH4hle71Z0I6K5BVfsUqJCaHt e/95dGWTzyQg7LOZI1MIoyFIWDZAMefhyfGHhobrlRPdtHB3JJfNE9aRbiHvxUuNtdYw 2d0DVVP9EaaNYP3kKal1H92jC8/B5zbXgN26X3yxAQlF5Qw/wPpnlsjpCc/2QqPvi4mN RaVA== X-Received: by 10.55.31.5 with SMTP id f5mr4757944qkf.42.1431063596772; Thu, 07 May 2015 22:39:56 -0700 (PDT) Received: from hchen.csail.mit.edu (hchen.csail.mit.edu. [18.26.5.5]) by mx.google.com with ESMTPSA id f131sm2957748qhc.47.2015.05.07.22.39.55 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Thu, 07 May 2015 22:39:56 -0700 (PDT) From: Xi Wang To: linux-arm-kernel@lists.infradead.org Subject: [PATCH] arm64: bpf: fix signedness bug in loading 64-bit immediate Date: Fri, 8 May 2015 01:39:51 -0400 Message-Id: <1431063591-16668-1-git-send-email-xi.wang@gmail.com> X-Mailer: git-send-email 1.9.1 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20150507_224020_374944_A6F08352 X-CRM114-Status: GOOD ( 10.84 ) X-Spam-Score: -0.8 (/) Cc: Zi Shen Lim , Will Deacon , linux-kernel@vger.kernel.org, Catalin Marinas , Xi Wang , Alexei Starovoitov X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.18-1 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+patchwork-linux-arm=patchwork.kernel.org@lists.infradead.org X-Spam-Status: No, score=-4.1 required=5.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED, FREEMAIL_FROM, RCVD_IN_DNSWL_MED, T_DKIM_INVALID, T_RP_MATCHES_RCVD, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP 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 Cc: Alexei Starovoitov Cc: Catalin Marinas Cc: Will Deacon Signed-off-by: Xi Wang --- 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; 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(), }, INTERNAL,