All of lore.kernel.org
 help / color / mirror / Atom feed
From: yang.shi@linaro.org (Shi, Yang)
To: linux-arm-kernel@lists.infradead.org
Subject: [PATCH] arm64: bpf: fix div-by-zero case
Date: Wed, 04 Nov 2015 10:41:53 -0800	[thread overview]
Message-ID: <563A5171.5070608@linaro.org> (raw)
In-Reply-To: <CABg9mcuAuoSBg4zWYHnaTzAGsH4BK5-VhD=HRDmsHJRCUrnJUw@mail.gmail.com>

On 11/4/2015 10:25 AM, Z Lim wrote:
> On Wed, Nov 4, 2015 at 10:21 AM, Shi, Yang <yang.shi@linaro.org> wrote:
>> On 11/3/2015 11:04 PM, Xi Wang wrote:
>>>
>>> On Tue, Nov 3, 2015 at 10:56 PM, Zi Shen Lim <zlim.lnx@gmail.com> wrote:
>>>>
>>>>           case BPF_ALU | BPF_DIV | BPF_X:
>>>>           case BPF_ALU64 | BPF_DIV | BPF_X:
>>>> +       {
>>>> +               const u8 r0 = bpf2a64[BPF_REG_0];
>>>> +
>>>> +               /* if (src == 0) return 0 */
>>>> +               jmp_offset = 3; /* skip ahead to else path */
>>>> +               check_imm19(jmp_offset);
>>>> +               emit(A64_CBNZ(is64, src, jmp_offset), ctx);
>>>> +               emit(A64_MOVZ(1, r0, 0, 0), ctx);
>>>> +               jmp_offset = epilogue_offset(ctx);
>>>> +               check_imm26(jmp_offset);
>>>> +               emit(A64_B(jmp_offset), ctx);
>>>> +               /* else */
>>>>                   emit(A64_UDIV(is64, dst, dst, src), ctx);
>>>>                   break;
>>>> +       }
>>>>           case BPF_ALU | BPF_MOD | BPF_X:
>>>>           case BPF_ALU64 | BPF_MOD | BPF_X:
>>>
>>>
>>> BPF_MOD might need the same fix.
>
> I'll post a fix for this case as well.
>
>>
>>
>> Agreed, and we may need add one more test cases in test_bpf module to cover
>> MOD?
>
> Let me know if you have a test case ready :)

Does the below change look like a valid test?

+               "MOD default X",
+               .u.insns = {
+                       /*
+                        * A = 0x42
+                        * A = A mod X ; this halt the filter execution 
if X is 0
+                        * ret 0x42
+                        */
+                       BPF_STMT(BPF_LD | BPF_IMM, 0x42),
+                       BPF_STMT(BPF_ALU | BPF_MOD | BPF_X, 0),
+                       BPF_STMT(BPF_RET | BPF_K, 0x42),
+               },
+               CLASSIC | FLAG_NO_DATA,
+               {},
+               { {0x1, 0x0 } },
+       },
+       {
+               "MOD default A",
+               .u.insns = {
+                       /*
+                        * A = A mod 1
+                        * ret A
+                        */
+                       BPF_STMT(BPF_ALU | BPF_MOD | BPF_K, 0x1),
+                       BPF_STMT(BPF_RET | BPF_A, 0x0),
+               },
+               CLASSIC | FLAG_NO_DATA,
+               {},
+               { {0x1, 0x0 } },
+       },

My test result with it:
test_bpf: #284 MOD default X jited:1 ret 66 != 0 FAIL (1 times)
test_bpf: #285 MOD default A jited:1 102 PASS

If it looks right, I will post a patch to add the test cases.

Thanks,
Yang

>
>>
>> Yang
>>
>>>
>>> _______________________________________________
>>> linux-arm-kernel mailing list
>>> linux-arm-kernel at lists.infradead.org
>>> http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
>>>
>>

WARNING: multiple messages have this Message-ID (diff)
From: "Shi, Yang" <yang.shi@linaro.org>
To: Z Lim <zlim.lnx@gmail.com>
Cc: Xi Wang <xi.wang@gmail.com>,
	Catalin Marinas <catalin.marinas@arm.com>,
	stable@vger.kernel.org, LKML <linux-kernel@vger.kernel.org>,
	"linux-arm-kernel@lists.infradead.org" 
	<linux-arm-kernel@lists.infradead.org>,
	Alexei Starovoitov <ast@plumgrid.com>
Subject: Re: [PATCH] arm64: bpf: fix div-by-zero case
Date: Wed, 04 Nov 2015 10:41:53 -0800	[thread overview]
Message-ID: <563A5171.5070608@linaro.org> (raw)
In-Reply-To: <CABg9mcuAuoSBg4zWYHnaTzAGsH4BK5-VhD=HRDmsHJRCUrnJUw@mail.gmail.com>

On 11/4/2015 10:25 AM, Z Lim wrote:
> On Wed, Nov 4, 2015 at 10:21 AM, Shi, Yang <yang.shi@linaro.org> wrote:
>> On 11/3/2015 11:04 PM, Xi Wang wrote:
>>>
>>> On Tue, Nov 3, 2015 at 10:56 PM, Zi Shen Lim <zlim.lnx@gmail.com> wrote:
>>>>
>>>>           case BPF_ALU | BPF_DIV | BPF_X:
>>>>           case BPF_ALU64 | BPF_DIV | BPF_X:
>>>> +       {
>>>> +               const u8 r0 = bpf2a64[BPF_REG_0];
>>>> +
>>>> +               /* if (src == 0) return 0 */
>>>> +               jmp_offset = 3; /* skip ahead to else path */
>>>> +               check_imm19(jmp_offset);
>>>> +               emit(A64_CBNZ(is64, src, jmp_offset), ctx);
>>>> +               emit(A64_MOVZ(1, r0, 0, 0), ctx);
>>>> +               jmp_offset = epilogue_offset(ctx);
>>>> +               check_imm26(jmp_offset);
>>>> +               emit(A64_B(jmp_offset), ctx);
>>>> +               /* else */
>>>>                   emit(A64_UDIV(is64, dst, dst, src), ctx);
>>>>                   break;
>>>> +       }
>>>>           case BPF_ALU | BPF_MOD | BPF_X:
>>>>           case BPF_ALU64 | BPF_MOD | BPF_X:
>>>
>>>
>>> BPF_MOD might need the same fix.
>
> I'll post a fix for this case as well.
>
>>
>>
>> Agreed, and we may need add one more test cases in test_bpf module to cover
>> MOD?
>
> Let me know if you have a test case ready :)

Does the below change look like a valid test?

+               "MOD default X",
+               .u.insns = {
+                       /*
+                        * A = 0x42
+                        * A = A mod X ; this halt the filter execution 
if X is 0
+                        * ret 0x42
+                        */
+                       BPF_STMT(BPF_LD | BPF_IMM, 0x42),
+                       BPF_STMT(BPF_ALU | BPF_MOD | BPF_X, 0),
+                       BPF_STMT(BPF_RET | BPF_K, 0x42),
+               },
+               CLASSIC | FLAG_NO_DATA,
+               {},
+               { {0x1, 0x0 } },
+       },
+       {
+               "MOD default A",
+               .u.insns = {
+                       /*
+                        * A = A mod 1
+                        * ret A
+                        */
+                       BPF_STMT(BPF_ALU | BPF_MOD | BPF_K, 0x1),
+                       BPF_STMT(BPF_RET | BPF_A, 0x0),
+               },
+               CLASSIC | FLAG_NO_DATA,
+               {},
+               { {0x1, 0x0 } },
+       },

My test result with it:
test_bpf: #284 MOD default X jited:1 ret 66 != 0 FAIL (1 times)
test_bpf: #285 MOD default A jited:1 102 PASS

If it looks right, I will post a patch to add the test cases.

Thanks,
Yang

>
>>
>> Yang
>>
>>>
>>> _______________________________________________
>>> linux-arm-kernel mailing list
>>> linux-arm-kernel@lists.infradead.org
>>> http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
>>>
>>


  reply	other threads:[~2015-11-04 18:41 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-11-04  6:56 [PATCH] arm64: bpf: fix div-by-zero case Zi Shen Lim
2015-11-04  6:56 ` Zi Shen Lim
2015-11-04  7:04 ` Xi Wang
2015-11-04  7:04   ` Xi Wang
2015-11-04 18:21   ` Shi, Yang
2015-11-04 18:21     ` Shi, Yang
2015-11-04 18:25     ` Z Lim
2015-11-04 18:25       ` Z Lim
2015-11-04 18:41       ` Shi, Yang [this message]
2015-11-04 18:41         ` Shi, Yang
2015-11-04 19:39         ` Daniel Borkmann
2015-11-04 19:39           ` Daniel Borkmann
2015-11-04 18:03 ` Shi, Yang
2015-11-04 18:03   ` Shi, Yang

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=563A5171.5070608@linaro.org \
    --to=yang.shi@linaro.org \
    --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.