Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 #define _GNU_SOURCE
0003 #include <test_progs.h>
0004 
0005 #define MAX_TRAMP_PROGS 38
0006 
0007 struct inst {
0008     struct bpf_object *obj;
0009     struct bpf_link   *link;
0010 };
0011 
0012 static struct bpf_program *load_prog(char *file, char *name, struct inst *inst)
0013 {
0014     struct bpf_object *obj;
0015     struct bpf_program *prog;
0016     int err;
0017 
0018     obj = bpf_object__open_file(file, NULL);
0019     if (!ASSERT_OK_PTR(obj, "obj_open_file"))
0020         return NULL;
0021 
0022     inst->obj = obj;
0023 
0024     err = bpf_object__load(obj);
0025     if (!ASSERT_OK(err, "obj_load"))
0026         return NULL;
0027 
0028     prog = bpf_object__find_program_by_name(obj, name);
0029     if (!ASSERT_OK_PTR(prog, "obj_find_prog"))
0030         return NULL;
0031 
0032     return prog;
0033 }
0034 
0035 /* TODO: use different target function to run in concurrent mode */
0036 void serial_test_trampoline_count(void)
0037 {
0038     char *file = "test_trampoline_count.o";
0039     char *const progs[] = { "fentry_test", "fmod_ret_test", "fexit_test" };
0040     struct inst inst[MAX_TRAMP_PROGS + 1] = {};
0041     struct bpf_program *prog;
0042     struct bpf_link *link;
0043     int prog_fd, err, i;
0044     LIBBPF_OPTS(bpf_test_run_opts, opts);
0045 
0046     /* attach 'allowed' trampoline programs */
0047     for (i = 0; i < MAX_TRAMP_PROGS; i++) {
0048         prog = load_prog(file, progs[i % ARRAY_SIZE(progs)], &inst[i]);
0049         if (!prog)
0050             goto cleanup;
0051 
0052         link = bpf_program__attach(prog);
0053         if (!ASSERT_OK_PTR(link, "attach_prog"))
0054             goto cleanup;
0055 
0056         inst[i].link = link;
0057     }
0058 
0059     /* and try 1 extra.. */
0060     prog = load_prog(file, "fmod_ret_test", &inst[i]);
0061     if (!prog)
0062         goto cleanup;
0063 
0064     /* ..that needs to fail */
0065     link = bpf_program__attach(prog);
0066     if (!ASSERT_ERR_PTR(link, "attach_prog")) {
0067         inst[i].link = link;
0068         goto cleanup;
0069     }
0070 
0071     /* with E2BIG error */
0072     if (!ASSERT_EQ(libbpf_get_error(link), -E2BIG, "E2BIG"))
0073         goto cleanup;
0074     if (!ASSERT_EQ(link, NULL, "ptr_is_null"))
0075         goto cleanup;
0076 
0077     /* and finaly execute the probe */
0078     prog_fd = bpf_program__fd(prog);
0079     if (!ASSERT_GE(prog_fd, 0, "bpf_program__fd"))
0080         goto cleanup;
0081 
0082     err = bpf_prog_test_run_opts(prog_fd, &opts);
0083     if (!ASSERT_OK(err, "bpf_prog_test_run_opts"))
0084         goto cleanup;
0085 
0086     ASSERT_EQ(opts.retval & 0xffff, 4, "bpf_modify_return_test.result");
0087     ASSERT_EQ(opts.retval >> 16, 1, "bpf_modify_return_test.side_effect");
0088 
0089 cleanup:
0090     for (; i >= 0; i--) {
0091         bpf_link__destroy(inst[i].link);
0092         bpf_object__close(inst[i].obj);
0093     }
0094 }