Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 #include <test_progs.h>
0003 
0004 void test_obj_name(void)
0005 {
0006     struct {
0007         const char *name;
0008         int success;
0009         int expected_errno;
0010     } tests[] = {
0011         { "", 1, 0 },
0012         { "_123456789ABCDE", 1, 0 },
0013         { "_123456789ABCDEF", 0, EINVAL },
0014         { "_123456789ABCD\n", 0, EINVAL },
0015     };
0016     struct bpf_insn prog[] = {
0017         BPF_ALU64_IMM(BPF_MOV, BPF_REG_0, 0),
0018         BPF_EXIT_INSN(),
0019     };
0020     __u32 duration = 0;
0021     int i;
0022 
0023     for (i = 0; i < ARRAY_SIZE(tests); i++) {
0024         size_t name_len = strlen(tests[i].name) + 1;
0025         union bpf_attr attr;
0026         size_t ncopy;
0027         int fd;
0028 
0029         /* test different attr.prog_name during BPF_PROG_LOAD */
0030         ncopy = name_len < sizeof(attr.prog_name) ?
0031             name_len : sizeof(attr.prog_name);
0032         bzero(&attr, sizeof(attr));
0033         attr.prog_type = BPF_PROG_TYPE_SCHED_CLS;
0034         attr.insn_cnt = 2;
0035         attr.insns = ptr_to_u64(prog);
0036         attr.license = ptr_to_u64("");
0037         memcpy(attr.prog_name, tests[i].name, ncopy);
0038 
0039         fd = syscall(__NR_bpf, BPF_PROG_LOAD, &attr, sizeof(attr));
0040         CHECK((tests[i].success && fd < 0) ||
0041               (!tests[i].success && fd >= 0) ||
0042               (!tests[i].success && errno != tests[i].expected_errno),
0043               "check-bpf-prog-name",
0044               "fd %d(%d) errno %d(%d)\n",
0045                fd, tests[i].success, errno, tests[i].expected_errno);
0046 
0047         if (fd >= 0)
0048             close(fd);
0049 
0050         /* test different attr.map_name during BPF_MAP_CREATE */
0051         ncopy = name_len < sizeof(attr.map_name) ?
0052             name_len : sizeof(attr.map_name);
0053         bzero(&attr, sizeof(attr));
0054         attr.map_type = BPF_MAP_TYPE_ARRAY;
0055         attr.key_size = 4;
0056         attr.value_size = 4;
0057         attr.max_entries = 1;
0058         attr.map_flags = 0;
0059         memcpy(attr.map_name, tests[i].name, ncopy);
0060         fd = syscall(__NR_bpf, BPF_MAP_CREATE, &attr, sizeof(attr));
0061         CHECK((tests[i].success && fd < 0) ||
0062               (!tests[i].success && fd >= 0) ||
0063               (!tests[i].success && errno != tests[i].expected_errno),
0064               "check-bpf-map-name",
0065               "fd %d(%d) errno %d(%d)\n",
0066               fd, tests[i].success, errno, tests[i].expected_errno);
0067 
0068         if (fd >= 0)
0069             close(fd);
0070     }
0071 }