Skip to content

Commit a55b7d3

Browse files
viktormalikAlexei Starovoitov
authored andcommitted
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 <[email protected]> Link: https://lore.kernel.org/r/a6c6b551ae0575351faa7b7a1df52f9341a5cbe8.1750917800.git.vmalik@redhat.com Signed-off-by: Alexei Starovoitov <[email protected]>
1 parent e913705 commit a55b7d3

File tree

3 files changed

+19
-23
lines changed

3 files changed

+19
-23
lines changed

tools/testing/selftests/bpf/progs/bpf_misc.h

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,11 @@
8383
* expect return value to match passed parameter:
8484
* - a decimal number
8585
* - 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
8991
* __retval_unpriv Same, but load program in unprivileged mode.
9092
*
9193
* __description Text to be used instead of a program name for display
@@ -125,8 +127,8 @@
125127
#define __success_unpriv __attribute__((btf_decl_tag("comment:test_expect_success_unpriv")))
126128
#define __log_level(lvl) __attribute__((btf_decl_tag("comment:test_log_level="#lvl)))
127129
#define __flag(flag) __attribute__((btf_decl_tag("comment:test_prog_flags="#flag)))
128-
#define __retval(val) __attribute__((btf_decl_tag("comment:test_retval="#val)))
129-
#define __retval_unpriv(val) __attribute__((btf_decl_tag("comment:test_retval_unpriv="#val)))
130+
#define __retval(val) __attribute__((btf_decl_tag("comment:test_retval="XSTR(val))))
131+
#define __retval_unpriv(val) __attribute__((btf_decl_tag("comment:test_retval_unpriv="XSTR(val))))
130132
#define __auxiliary __attribute__((btf_decl_tag("comment:test_auxiliary")))
131133
#define __auxiliary_unpriv __attribute__((btf_decl_tag("comment:test_auxiliary_unpriv")))
132134
#define __btf_path(path) __attribute__((btf_decl_tag("comment:test_btf_path=" path)))
@@ -155,7 +157,7 @@
155157
#define __imm_insn(name, expr) [name]"i"(*(long *)&(expr))
156158

157159
/* Magic constants used with __retval() */
158-
#define POINTER_VALUE 0xcafe4all
160+
#define POINTER_VALUE 0xbadcafe
159161
#define TEST_DATA_LEN 64
160162

161163
#ifndef __used

tools/testing/selftests/bpf/progs/verifier_div_overflow.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ l0_%=: exit; \
7777

7878
SEC("tc")
7979
__description("MOD32 overflow, check 1")
80-
__success __retval(INT_MIN)
80+
__success __retval(_INT_MIN)
8181
__naked void mod32_overflow_check_1(void)
8282
{
8383
asm volatile (" \
@@ -92,7 +92,7 @@ __naked void mod32_overflow_check_1(void)
9292

9393
SEC("tc")
9494
__description("MOD32 overflow, check 2")
95-
__success __retval(INT_MIN)
95+
__success __retval(_INT_MIN)
9696
__naked void mod32_overflow_check_2(void)
9797
{
9898
asm volatile (" \

tools/testing/selftests/bpf/test_loader.c

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
#define TEST_TAG_LOAD_MODE_PFX "comment:load_mode="
4141

4242
/* Warning: duplicated in bpf_misc.h */
43-
#define POINTER_VALUE 0xcafe4all
43+
#define POINTER_VALUE 0xbadcafe
4444
#define TEST_DATA_LEN 64
4545

4646
#ifdef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
@@ -318,20 +318,14 @@ static int parse_caps(const char *str, __u64 *val, const char *name)
318318

319319
static int parse_retval(const char *str, int *val, const char *name)
320320
{
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;
335329
return 0;
336330
}
337331

0 commit comments

Comments
 (0)