Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

selftests/bpf: Allow macros in __retval

Allow macro expansion for values passed to the `__retval` and
`__retval_unpriv` attributes. This is especially useful for testing
programs which return various error codes.

With this change, the code for parsing special literals can be made
simpler, as the literals are defined via macros. The only exception is
INT_MIN which expands to (-INT_MAX -1), which is not single number and
cannot be parsed by strtol. So, we instead use a prefixed literal
_INT_MIN in __retval and handle it separately (assign the expected
return to INT_MIN). Also, strtol cannot handle the "ll" suffix so change
the value of POINTER_VALUE from 0xcafe4all to 0xbadcafe.

Signed-off-by: Viktor Malik <vmalik@redhat.com>
Link: https://lore.kernel.org/r/a6c6b551ae0575351faa7b7a1df52f9341a5cbe8.1750917800.git.vmalik@redhat.com
Signed-off-by: Alexei Starovoitov <ast@kernel.org>

authored by

Viktor Malik and committed by
Alexei Starovoitov
a55b7d39 e9137055

+19 -23
+8 -6
tools/testing/selftests/bpf/progs/bpf_misc.h
··· 83 83 * expect return value to match passed parameter: 84 84 * - a decimal number 85 85 * - a hexadecimal number, when starts from 0x 86 - * - literal INT_MIN 87 - * - literal POINTER_VALUE (see definition below) 88 - * - literal TEST_DATA_LEN (see definition below) 86 + * - a macro which expands to one of the above 87 + * - literal _INT_MIN (expands to INT_MIN) 88 + * In addition, two special macros are defined below: 89 + * - POINTER_VALUE 90 + * - TEST_DATA_LEN 89 91 * __retval_unpriv Same, but load program in unprivileged mode. 90 92 * 91 93 * __description Text to be used instead of a program name for display ··· 127 125 #define __success_unpriv __attribute__((btf_decl_tag("comment:test_expect_success_unpriv"))) 128 126 #define __log_level(lvl) __attribute__((btf_decl_tag("comment:test_log_level="#lvl))) 129 127 #define __flag(flag) __attribute__((btf_decl_tag("comment:test_prog_flags="#flag))) 130 - #define __retval(val) __attribute__((btf_decl_tag("comment:test_retval="#val))) 131 - #define __retval_unpriv(val) __attribute__((btf_decl_tag("comment:test_retval_unpriv="#val))) 128 + #define __retval(val) __attribute__((btf_decl_tag("comment:test_retval="XSTR(val)))) 129 + #define __retval_unpriv(val) __attribute__((btf_decl_tag("comment:test_retval_unpriv="XSTR(val)))) 132 130 #define __auxiliary __attribute__((btf_decl_tag("comment:test_auxiliary"))) 133 131 #define __auxiliary_unpriv __attribute__((btf_decl_tag("comment:test_auxiliary_unpriv"))) 134 132 #define __btf_path(path) __attribute__((btf_decl_tag("comment:test_btf_path=" path))) ··· 157 155 #define __imm_insn(name, expr) [name]"i"(*(long *)&(expr)) 158 156 159 157 /* Magic constants used with __retval() */ 160 - #define POINTER_VALUE 0xcafe4all 158 + #define POINTER_VALUE 0xbadcafe 161 159 #define TEST_DATA_LEN 64 162 160 163 161 #ifndef __used
+2 -2
tools/testing/selftests/bpf/progs/verifier_div_overflow.c
··· 77 77 78 78 SEC("tc") 79 79 __description("MOD32 overflow, check 1") 80 - __success __retval(INT_MIN) 80 + __success __retval(_INT_MIN) 81 81 __naked void mod32_overflow_check_1(void) 82 82 { 83 83 asm volatile (" \ ··· 92 92 93 93 SEC("tc") 94 94 __description("MOD32 overflow, check 2") 95 - __success __retval(INT_MIN) 95 + __success __retval(_INT_MIN) 96 96 __naked void mod32_overflow_check_2(void) 97 97 { 98 98 asm volatile (" \
+9 -15
tools/testing/selftests/bpf/test_loader.c
··· 40 40 #define TEST_TAG_LOAD_MODE_PFX "comment:load_mode=" 41 41 42 42 /* Warning: duplicated in bpf_misc.h */ 43 - #define POINTER_VALUE 0xcafe4all 43 + #define POINTER_VALUE 0xbadcafe 44 44 #define TEST_DATA_LEN 64 45 45 46 46 #ifdef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS ··· 318 318 319 319 static int parse_retval(const char *str, int *val, const char *name) 320 320 { 321 - struct { 322 - char *name; 323 - int val; 324 - } named_values[] = { 325 - { "INT_MIN" , INT_MIN }, 326 - { "POINTER_VALUE", POINTER_VALUE }, 327 - { "TEST_DATA_LEN", TEST_DATA_LEN }, 328 - }; 329 - int i; 330 - 331 - for (i = 0; i < ARRAY_SIZE(named_values); ++i) { 332 - if (strcmp(str, named_values[i].name) != 0) 333 - continue; 334 - *val = named_values[i].val; 321 + /* 322 + * INT_MIN is defined as (-INT_MAX -1), i.e. it doesn't expand to a 323 + * single int and cannot be parsed with strtol, so we handle it 324 + * separately here. In addition, it expands to different expressions in 325 + * different compilers so we use a prefixed _INT_MIN instead. 326 + */ 327 + if (strcmp(str, "_INT_MIN") == 0) { 328 + *val = INT_MIN; 335 329 return 0; 336 330 } 337 331