]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
selftests/bpf: Allow macros in __retval
authorViktor Malik <vmalik@redhat.com>
Thu, 26 Jun 2025 06:08:30 +0000 (08:08 +0200)
committerAlexei Starovoitov <ast@kernel.org>
Thu, 26 Jun 2025 16:44:46 +0000 (09:44 -0700)
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>
tools/testing/selftests/bpf/progs/bpf_misc.h
tools/testing/selftests/bpf/progs/verifier_div_overflow.c
tools/testing/selftests/bpf/test_loader.c

index a678463e972c36011a84b57fc730fa210e0e5185..20dce508d8e0e962f075d4e2bb0d00ab355d22ab 100644 (file)
  *                   expect return value to match passed parameter:
  *                   - a decimal number
  *                   - a hexadecimal number, when starts from 0x
- *                   - literal INT_MIN
- *                   - literal POINTER_VALUE (see definition below)
- *                   - literal TEST_DATA_LEN (see definition below)
+ *                   - a macro which expands to one of the above
+ *                   - literal _INT_MIN (expands to INT_MIN)
+ *                   In addition, two special macros are defined below:
+ *                   - POINTER_VALUE
+ *                   - TEST_DATA_LEN
  * __retval_unpriv   Same, but load program in unprivileged mode.
  *
  * __description     Text to be used instead of a program name for display
 #define __success_unpriv       __attribute__((btf_decl_tag("comment:test_expect_success_unpriv")))
 #define __log_level(lvl)       __attribute__((btf_decl_tag("comment:test_log_level="#lvl)))
 #define __flag(flag)           __attribute__((btf_decl_tag("comment:test_prog_flags="#flag)))
-#define __retval(val)          __attribute__((btf_decl_tag("comment:test_retval="#val)))
-#define __retval_unpriv(val)   __attribute__((btf_decl_tag("comment:test_retval_unpriv="#val)))
+#define __retval(val)          __attribute__((btf_decl_tag("comment:test_retval="XSTR(val))))
+#define __retval_unpriv(val)   __attribute__((btf_decl_tag("comment:test_retval_unpriv="XSTR(val))))
 #define __auxiliary            __attribute__((btf_decl_tag("comment:test_auxiliary")))
 #define __auxiliary_unpriv     __attribute__((btf_decl_tag("comment:test_auxiliary_unpriv")))
 #define __btf_path(path)       __attribute__((btf_decl_tag("comment:test_btf_path=" path)))
 #define __imm_insn(name, expr) [name]"i"(*(long *)&(expr))
 
 /* Magic constants used with __retval() */
-#define POINTER_VALUE  0xcafe4all
+#define POINTER_VALUE  0xbadcafe
 #define TEST_DATA_LEN  64
 
 #ifndef __used
index 458984da804c93bf24f4edba4073b70eaaf5d4ed..34e0c012ee769537ecf05488226af07cbebafa8c 100644 (file)
@@ -77,7 +77,7 @@ l0_%=:        exit;                                           \
 
 SEC("tc")
 __description("MOD32 overflow, check 1")
-__success __retval(INT_MIN)
+__success __retval(_INT_MIN)
 __naked void mod32_overflow_check_1(void)
 {
        asm volatile ("                                 \
@@ -92,7 +92,7 @@ __naked void mod32_overflow_check_1(void)
 
 SEC("tc")
 __description("MOD32 overflow, check 2")
-__success __retval(INT_MIN)
+__success __retval(_INT_MIN)
 __naked void mod32_overflow_check_2(void)
 {
        asm volatile ("                                 \
index 2c7e9729d5feb59f708591c57d065c1a5a316713..78423cf89e01bb90e2c780ce68657465507996c7 100644 (file)
@@ -40,7 +40,7 @@
 #define TEST_TAG_LOAD_MODE_PFX "comment:load_mode="
 
 /* Warning: duplicated in bpf_misc.h */
-#define POINTER_VALUE  0xcafe4all
+#define POINTER_VALUE  0xbadcafe
 #define TEST_DATA_LEN  64
 
 #ifdef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
@@ -318,20 +318,14 @@ static int parse_caps(const char *str, __u64 *val, const char *name)
 
 static int parse_retval(const char *str, int *val, const char *name)
 {
-       struct {
-               char *name;
-               int val;
-       } named_values[] = {
-               { "INT_MIN"      , INT_MIN },
-               { "POINTER_VALUE", POINTER_VALUE },
-               { "TEST_DATA_LEN", TEST_DATA_LEN },
-       };
-       int i;
-
-       for (i = 0; i < ARRAY_SIZE(named_values); ++i) {
-               if (strcmp(str, named_values[i].name) != 0)
-                       continue;
-               *val = named_values[i].val;
+       /*
+        * INT_MIN is defined as (-INT_MAX -1), i.e. it doesn't expand to a
+        * single int and cannot be parsed with strtol, so we handle it
+        * separately here. In addition, it expands to different expressions in
+        * different compilers so we use a prefixed _INT_MIN instead.
+        */
+       if (strcmp(str, "_INT_MIN") == 0) {
+               *val = INT_MIN;
                return 0;
        }