Skip to content

Commit ad13baf

Browse files
Hou TaoAlexei Starovoitov
Hou Tao
authored and
Alexei Starovoitov
committed
selftests/bpf: Test subprog jit when toggle bpf_jit_harden repeatedly
When bpf_jit_harden is toggled between 0 and 2, subprog jit may fail due to inconsistent twice read values of bpf_jit_harden during jit. So add a test to ensure the problem is fixed. Signed-off-by: Hou Tao <[email protected]> Signed-off-by: Alexei Starovoitov <[email protected]> Link: https://lore.kernel.org/bpf/[email protected]
1 parent d2a3b7c commit ad13baf

File tree

1 file changed

+68
-9
lines changed

1 file changed

+68
-9
lines changed
Lines changed: 68 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,83 @@
11
// SPDX-License-Identifier: GPL-2.0
22
/* Copyright (c) 2020 Facebook */
33
#include <test_progs.h>
4-
#include <time.h>
54
#include "test_subprogs.skel.h"
65
#include "test_subprogs_unused.skel.h"
76

8-
static int duration;
7+
struct toggler_ctx {
8+
int fd;
9+
bool stop;
10+
};
911

10-
void test_subprogs(void)
12+
static void *toggle_jit_harden(void *arg)
13+
{
14+
struct toggler_ctx *ctx = arg;
15+
char two = '2';
16+
char zero = '0';
17+
18+
while (!ctx->stop) {
19+
lseek(ctx->fd, SEEK_SET, 0);
20+
write(ctx->fd, &two, sizeof(two));
21+
lseek(ctx->fd, SEEK_SET, 0);
22+
write(ctx->fd, &zero, sizeof(zero));
23+
}
24+
25+
return NULL;
26+
}
27+
28+
static void test_subprogs_with_jit_harden_toggling(void)
29+
{
30+
struct toggler_ctx ctx;
31+
pthread_t toggler;
32+
int err;
33+
unsigned int i, loop = 10;
34+
35+
ctx.fd = open("/proc/sys/net/core/bpf_jit_harden", O_RDWR);
36+
if (!ASSERT_GE(ctx.fd, 0, "open bpf_jit_harden"))
37+
return;
38+
39+
ctx.stop = false;
40+
err = pthread_create(&toggler, NULL, toggle_jit_harden, &ctx);
41+
if (!ASSERT_OK(err, "new toggler"))
42+
goto out;
43+
44+
/* Make toggler thread to run */
45+
usleep(1);
46+
47+
for (i = 0; i < loop; i++) {
48+
struct test_subprogs *skel = test_subprogs__open_and_load();
49+
50+
if (!ASSERT_OK_PTR(skel, "skel open"))
51+
break;
52+
test_subprogs__destroy(skel);
53+
}
54+
55+
ctx.stop = true;
56+
pthread_join(toggler, NULL);
57+
out:
58+
close(ctx.fd);
59+
}
60+
61+
static void test_subprogs_alone(void)
1162
{
1263
struct test_subprogs *skel;
1364
struct test_subprogs_unused *skel2;
1465
int err;
1566

1667
skel = test_subprogs__open_and_load();
17-
if (CHECK(!skel, "skel_open", "failed to open skeleton\n"))
68+
if (!ASSERT_OK_PTR(skel, "skel_open"))
1869
return;
1970

2071
err = test_subprogs__attach(skel);
21-
if (CHECK(err, "skel_attach", "failed to attach skeleton: %d\n", err))
72+
if (!ASSERT_OK(err, "skel attach"))
2273
goto cleanup;
2374

2475
usleep(1);
2576

26-
CHECK(skel->bss->res1 != 12, "res1", "got %d, exp %d\n", skel->bss->res1, 12);
27-
CHECK(skel->bss->res2 != 17, "res2", "got %d, exp %d\n", skel->bss->res2, 17);
28-
CHECK(skel->bss->res3 != 19, "res3", "got %d, exp %d\n", skel->bss->res3, 19);
29-
CHECK(skel->bss->res4 != 36, "res4", "got %d, exp %d\n", skel->bss->res4, 36);
77+
ASSERT_EQ(skel->bss->res1, 12, "res1");
78+
ASSERT_EQ(skel->bss->res2, 17, "res2");
79+
ASSERT_EQ(skel->bss->res3, 19, "res3");
80+
ASSERT_EQ(skel->bss->res4, 36, "res4");
3081

3182
skel2 = test_subprogs_unused__open_and_load();
3283
ASSERT_OK_PTR(skel2, "unused_progs_skel");
@@ -35,3 +86,11 @@ void test_subprogs(void)
3586
cleanup:
3687
test_subprogs__destroy(skel);
3788
}
89+
90+
void test_subprogs(void)
91+
{
92+
if (test__start_subtest("subprogs_alone"))
93+
test_subprogs_alone();
94+
if (test__start_subtest("subprogs_and_jit_harden"))
95+
test_subprogs_with_jit_harden_toggling();
96+
}

0 commit comments

Comments
 (0)