Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /* Copyright (c) 2020 Facebook */
0003 #include <test_progs.h>
0004 #include "test_subprogs.skel.h"
0005 #include "test_subprogs_unused.skel.h"
0006 
0007 struct toggler_ctx {
0008     int fd;
0009     bool stop;
0010 };
0011 
0012 static void *toggle_jit_harden(void *arg)
0013 {
0014     struct toggler_ctx *ctx = arg;
0015     char two = '2';
0016     char zero = '0';
0017 
0018     while (!ctx->stop) {
0019         lseek(ctx->fd, SEEK_SET, 0);
0020         write(ctx->fd, &two, sizeof(two));
0021         lseek(ctx->fd, SEEK_SET, 0);
0022         write(ctx->fd, &zero, sizeof(zero));
0023     }
0024 
0025     return NULL;
0026 }
0027 
0028 static void test_subprogs_with_jit_harden_toggling(void)
0029 {
0030     struct toggler_ctx ctx;
0031     pthread_t toggler;
0032     int err;
0033     unsigned int i, loop = 10;
0034 
0035     ctx.fd = open("/proc/sys/net/core/bpf_jit_harden", O_RDWR);
0036     if (!ASSERT_GE(ctx.fd, 0, "open bpf_jit_harden"))
0037         return;
0038 
0039     ctx.stop = false;
0040     err = pthread_create(&toggler, NULL, toggle_jit_harden, &ctx);
0041     if (!ASSERT_OK(err, "new toggler"))
0042         goto out;
0043 
0044     /* Make toggler thread to run */
0045     usleep(1);
0046 
0047     for (i = 0; i < loop; i++) {
0048         struct test_subprogs *skel = test_subprogs__open_and_load();
0049 
0050         if (!ASSERT_OK_PTR(skel, "skel open"))
0051             break;
0052         test_subprogs__destroy(skel);
0053     }
0054 
0055     ctx.stop = true;
0056     pthread_join(toggler, NULL);
0057 out:
0058     close(ctx.fd);
0059 }
0060 
0061 static void test_subprogs_alone(void)
0062 {
0063     struct test_subprogs *skel;
0064     struct test_subprogs_unused *skel2;
0065     int err;
0066 
0067     skel = test_subprogs__open_and_load();
0068     if (!ASSERT_OK_PTR(skel, "skel_open"))
0069         return;
0070 
0071     err = test_subprogs__attach(skel);
0072     if (!ASSERT_OK(err, "skel attach"))
0073         goto cleanup;
0074 
0075     usleep(1);
0076 
0077     ASSERT_EQ(skel->bss->res1, 12, "res1");
0078     ASSERT_EQ(skel->bss->res2, 17, "res2");
0079     ASSERT_EQ(skel->bss->res3, 19, "res3");
0080     ASSERT_EQ(skel->bss->res4, 36, "res4");
0081 
0082     skel2 = test_subprogs_unused__open_and_load();
0083     ASSERT_OK_PTR(skel2, "unused_progs_skel");
0084     test_subprogs_unused__destroy(skel2);
0085 
0086 cleanup:
0087     test_subprogs__destroy(skel);
0088 }
0089 
0090 void test_subprogs(void)
0091 {
0092     if (test__start_subtest("subprogs_alone"))
0093         test_subprogs_alone();
0094     if (test__start_subtest("subprogs_and_jit_harden"))
0095         test_subprogs_with_jit_harden_toggling();
0096 }