Skip to content

Commit 27e64e6

Browse files
committed
selftests/bpf: Add testcases for BPF_ADD and BPF_SUB
The previous commit improves the precision in scalar(32)_min_max_add, and scalar(32)_min_max_sub. The improvement in precision occurs in cases when all outcomes overflow or underflow, respectively. This commit adds selftests that exercise those cases. Signed-off-by: Harishankar Vishwanathan <[email protected]>
1 parent 04033ef commit 27e64e6

File tree

1 file changed

+85
-0
lines changed

1 file changed

+85
-0
lines changed

tools/testing/selftests/bpf/progs/verifier_bounds.c

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1371,4 +1371,89 @@ __naked void mult_sign_ovf(void)
13711371
__imm(bpf_skb_store_bytes)
13721372
: __clobber_all);
13731373
}
1374+
1375+
SEC("socket")
1376+
__description("64-bit addition overflow, all outcomes overflow")
1377+
__success __log_level(2)
1378+
__msg("7: (0f) r5 += r3 {{.*}} R5_w=scalar(smin=0x800003d67e960f7d,umin=0x551ee3d67e960f7d,umax=0xc0149fffffffffff,smin32=0xfe960f7d,umin32=0x7e960f7d,var_off=(0x3d67e960f7d; 0xfffffc298169f082))")
1379+
__retval(0)
1380+
__naked void add64_ovf(void)
1381+
{
1382+
asm volatile (
1383+
"call %[bpf_get_prandom_u32];"
1384+
"r3 = r0;"
1385+
"r4 = 0x950a43d67e960f7d ll;"
1386+
"r3 |= r4;"
1387+
"r5 = 0xc014a00000000000 ll;"
1388+
"r5 += r3;"
1389+
"r0 = 0;"
1390+
"exit"
1391+
:
1392+
: __imm(bpf_get_prandom_u32)
1393+
: __clobber_all);
1394+
}
1395+
1396+
SEC("socket")
1397+
__description("32-bit addition overflow, all outcomes overflow")
1398+
__success __log_level(2)
1399+
__msg("5: (0c) w5 += w3 {{.*}} R5_w=scalar(smin=umin=umin32=0x20130018,smax=umax=umax32=0x8000ffff,smin32=0x80000018,var_off=(0x18; 0xffffffe7))")
1400+
__retval(0)
1401+
__naked void add32_ovf(void)
1402+
{
1403+
asm volatile (
1404+
"call %[bpf_get_prandom_u32];"
1405+
"r3 = r0;"
1406+
"w4 = 0xa0120018;"
1407+
"w3 |= w4;"
1408+
"w5 = 0x80010000;"
1409+
"w5 += w3;"
1410+
"r0 = 0;"
1411+
"exit"
1412+
:
1413+
: __imm(bpf_get_prandom_u32)
1414+
: __clobber_all);
1415+
}
1416+
1417+
SEC("socket")
1418+
__description("64-bit subtraction overflow, all outcomes underflow")
1419+
__success __log_level(2)
1420+
__msg("6: (1f) r3 -= r1 {{.*}} R3_w=scalar(umin=1,umax=0x8000000000000000)")
1421+
__retval(0)
1422+
__naked void sub64_ovf(void)
1423+
{
1424+
asm volatile (
1425+
"call %[bpf_get_prandom_u32];"
1426+
"r1 = r0;"
1427+
"r2 = 0x8000000000000000 ll;"
1428+
"r1 |= r2;"
1429+
"r3 = 0x0;"
1430+
"r3 -= r1;"
1431+
"r0 = 0;"
1432+
"exit"
1433+
:
1434+
: __imm(bpf_get_prandom_u32)
1435+
: __clobber_all);
1436+
}
1437+
1438+
SEC("socket")
1439+
__description("32-bit subtraction overflow, all outcomes underflow")
1440+
__success __log_level(2)
1441+
__msg("5: (1c) w3 -= w1 {{.*}} R3_w=scalar(smin=umin=umin32=1,smax=umax=umax32=0x80000000,var_off=(0x0; 0xffffffff))")
1442+
__retval(0)
1443+
__naked void sub32_ovf(void)
1444+
{
1445+
asm volatile (
1446+
"call %[bpf_get_prandom_u32];"
1447+
"r1 = r0;"
1448+
"w2 = 0x80000000;"
1449+
"w1 |= w2;"
1450+
"r3 = 0x0;"
1451+
"w3 -= w1;"
1452+
"r0 = 0;"
1453+
"exit"
1454+
:
1455+
: __imm(bpf_get_prandom_u32)
1456+
: __clobber_all);
1457+
}
1458+
13741459
char _license[] SEC("license") = "GPL";

0 commit comments

Comments
 (0)