test/bpf: fix undefined behavior with clang
authorKonstantin Ananyev <konstantin.ananyev@intel.com>
Mon, 18 Oct 2021 13:40:52 +0000 (14:40 +0100)
committerDavid Marchand <david.marchand@redhat.com>
Wed, 20 Oct 2021 19:07:47 +0000 (21:07 +0200)
commit3ac2dffae88e8eb5c374b1fdd40d605014526510
tree8198418dcb545d2d65581d2c21afab3174987844
parent28dde5da503ed09f10cdfb295e390b114df7330a
test/bpf: fix undefined behavior with clang

test_shift1_check() function fails with clang build.
The reason for that is that clang uses 64-bit shift instruction for
what expected to be 32-bit operation.
To be more specific, this C code:
r2 = (uint32_t)r2 >> r4;
With clang produces:
41a4eb:       48 d3 ef                shr    %cl,%rdi
In that particular case it is an allowed choice, as from one side
left-operand value is known to fit into 32 bits, from other side
according to 'C' standard:
"...if the value of the right operand is negative or is greater than
or equal to the width of the promoted left operand, the behavior is
undefined."
The problem is that on x86 behavior for 64-bit and 32-bit shift
operation might differ.
The fix avoids undefined behavior by making sure
that right operand will not exceed width of the promoted left operand.

Bugzilla ID: 811
Fixes: 9f8f9d91a701 ("test/bpf: introduce functional test")
Cc: stable@dpdk.org
Reported-by: Stephen Hemminger <stephen@networkplumber.org>
Signed-off-by: Konstantin Ananyev <konstantin.ananyev@intel.com>
Acked-by: Stephen Hemminger <stephen@networkplumber.org>
app/test/test_bpf.c