Skip to content

Commit 4eb93fe

Browse files
anakryikoAlexei Starovoitov
authored andcommitted
selftests/bpf: add test for LDX/STX/ST relocations over array field
Add a simple repro for the issue of miscalculating LDX/STX/ST CO-RE relocation size adjustment when the CO-RE relocation target type is an ARRAY. We need to make sure that compiler generates LDX/STX/ST instruction with CO-RE relocation against entire ARRAY type, not ARRAY's element. With the code pattern in selftest, we get this: 59: 61 71 00 00 00 00 00 00 w1 = *(u32 *)(r7 + 0x0) 00000000000001d8: CO-RE <byte_off> [5] struct core_reloc_arrays::a (0:0) Where offset of `int a[5]` is embedded (through CO-RE relocation) into memory load instruction itself. Signed-off-by: Andrii Nakryiko <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Alexei Starovoitov <[email protected]>
1 parent 06096d1 commit 4eb93fe

File tree

4 files changed

+22
-2
lines changed

4 files changed

+22
-2
lines changed

tools/testing/selftests/bpf/prog_tests/core_reloc.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,11 +85,11 @@ static int duration = 0;
8585
#define NESTING_ERR_CASE(name) { \
8686
NESTING_CASE_COMMON(name), \
8787
.fails = true, \
88-
.run_btfgen_fails = true, \
88+
.run_btfgen_fails = true, \
8989
}
9090

9191
#define ARRAYS_DATA(struct_name) STRUCT_TO_CHAR_PTR(struct_name) { \
92-
.a = { [2] = 1 }, \
92+
.a = { [2] = 1, [3] = 11 }, \
9393
.b = { [1] = { [2] = { [3] = 2 } } }, \
9494
.c = { [1] = { .c = 3 } }, \
9595
.d = { [0] = { [0] = { .d = 4 } } }, \
@@ -108,6 +108,7 @@ static int duration = 0;
108108
.input_len = sizeof(struct core_reloc_##name), \
109109
.output = STRUCT_TO_CHAR_PTR(core_reloc_arrays_output) { \
110110
.a2 = 1, \
111+
.a3 = 12, \
111112
.b123 = 2, \
112113
.c1c = 3, \
113114
.d00d = 4, \
@@ -602,6 +603,7 @@ static const struct core_reloc_test_case test_cases[] = {
602603
ARRAYS_ERR_CASE(arrays___err_non_array),
603604
ARRAYS_ERR_CASE(arrays___err_wrong_val_type),
604605
ARRAYS_ERR_CASE(arrays___err_bad_zero_sz_arr),
606+
ARRAYS_ERR_CASE(arrays___err_bad_signed_arr_elem_sz),
605607

606608
/* enum/ptr/int handling scenarios */
607609
PRIMITIVES_CASE(primitives),
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#include "core_reloc_types.h"
2+
3+
void f(struct core_reloc_arrays___err_bad_signed_arr_elem_sz x) {}

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,7 @@ struct core_reloc_nesting___err_too_deep {
347347
*/
348348
struct core_reloc_arrays_output {
349349
int a2;
350+
int a3;
350351
char b123;
351352
int c1c;
352353
int d00d;
@@ -455,6 +456,15 @@ struct core_reloc_arrays___err_bad_zero_sz_arr {
455456
struct core_reloc_arrays_substruct d[1][2];
456457
};
457458

459+
struct core_reloc_arrays___err_bad_signed_arr_elem_sz {
460+
/* int -> short (signed!): not supported case */
461+
short a[5];
462+
char b[2][3][4];
463+
struct core_reloc_arrays_substruct c[3];
464+
struct core_reloc_arrays_substruct d[1][2];
465+
struct core_reloc_arrays_substruct f[][2];
466+
};
467+
458468
/*
459469
* PRIMITIVES
460470
*/

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ struct {
1515

1616
struct core_reloc_arrays_output {
1717
int a2;
18+
int a3;
1819
char b123;
1920
int c1c;
2021
int d00d;
@@ -41,6 +42,7 @@ int test_core_arrays(void *ctx)
4142
{
4243
struct core_reloc_arrays *in = (void *)&data.in;
4344
struct core_reloc_arrays_output *out = (void *)&data.out;
45+
int *a;
4446

4547
if (CORE_READ(&out->a2, &in->a[2]))
4648
return 1;
@@ -53,6 +55,9 @@ int test_core_arrays(void *ctx)
5355
if (CORE_READ(&out->f01c, &in->f[0][1].c))
5456
return 1;
5557

58+
a = __builtin_preserve_access_index(({ in->a; }));
59+
out->a3 = a[0] + a[1] + a[2] + a[3];
60+
5661
return 0;
5762
}
5863

0 commit comments

Comments
 (0)