Skip to content

Commit 5705d70

Browse files
anakryikoAlexei Starovoitov
authored andcommitted
selftests/bpf: Correct various core_reloc 64-bit assumptions
Ensure that types are memory layout- and field alignment-compatible regardless of 32/64-bitness mix of libbpf and BPF architecture. Signed-off-by: Andrii Nakryiko <[email protected]> Signed-off-by: Alexei Starovoitov <[email protected]> Link: https://lore.kernel.org/bpf/[email protected]
1 parent 4c01925 commit 5705d70

File tree

2 files changed

+47
-42
lines changed

2 files changed

+47
-42
lines changed

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

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -237,8 +237,8 @@
237237
.union_sz = sizeof(((type *)0)->union_field), \
238238
.arr_sz = sizeof(((type *)0)->arr_field), \
239239
.arr_elem_sz = sizeof(((type *)0)->arr_field[0]), \
240-
.ptr_sz = sizeof(((type *)0)->ptr_field), \
241-
.enum_sz = sizeof(((type *)0)->enum_field), \
240+
.ptr_sz = 8, /* always 8-byte pointer for BPF */ \
241+
.enum_sz = sizeof(((type *)0)->enum_field), \
242242
}
243243

244244
#define SIZE_CASE(name) { \
@@ -432,20 +432,20 @@ static struct core_reloc_test_case test_cases[] = {
432432
.sb4 = -1,
433433
.sb20 = -0x17654321,
434434
.u32 = 0xBEEF,
435-
.s32 = -0x3FEDCBA987654321,
435+
.s32 = -0x3FEDCBA987654321LL,
436436
}),
437437
BITFIELDS_CASE(bitfields___bitfield_vs_int, {
438-
.ub1 = 0xFEDCBA9876543210,
438+
.ub1 = 0xFEDCBA9876543210LL,
439439
.ub2 = 0xA6,
440-
.ub7 = -0x7EDCBA987654321,
441-
.sb4 = -0x6123456789ABCDE,
442-
.sb20 = 0xD00D,
440+
.ub7 = -0x7EDCBA987654321LL,
441+
.sb4 = -0x6123456789ABCDELL,
442+
.sb20 = 0xD00DLL,
443443
.u32 = -0x76543,
444-
.s32 = 0x0ADEADBEEFBADB0B,
444+
.s32 = 0x0ADEADBEEFBADB0BLL,
445445
}),
446446
BITFIELDS_CASE(bitfields___just_big_enough, {
447-
.ub1 = 0xF,
448-
.ub2 = 0x0812345678FEDCBA,
447+
.ub1 = 0xFLL,
448+
.ub2 = 0x0812345678FEDCBALL,
449449
}),
450450
BITFIELDS_ERR_CASE(bitfields___err_too_big_bitfield),
451451

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

Lines changed: 37 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
#include <stdint.h>
22
#include <stdbool.h>
3+
4+
void preserce_ptr_sz_fn(long x) {}
5+
6+
#define __bpf_aligned __attribute__((aligned(8)))
7+
38
/*
49
* KERNEL
510
*/
@@ -444,59 +449,59 @@ struct core_reloc_primitives {
444449
char a;
445450
int b;
446451
enum core_reloc_primitives_enum c;
447-
void *d;
448-
int (*f)(const char *);
452+
void *d __bpf_aligned;
453+
int (*f)(const char *) __bpf_aligned;
449454
};
450455

451456
struct core_reloc_primitives___diff_enum_def {
452457
char a;
453458
int b;
454-
void *d;
455-
int (*f)(const char *);
459+
void *d __bpf_aligned;
460+
int (*f)(const char *) __bpf_aligned;
456461
enum {
457462
X = 100,
458463
Y = 200,
459-
} c; /* inline enum def with differing set of values */
464+
} c __bpf_aligned; /* inline enum def with differing set of values */
460465
};
461466

462467
struct core_reloc_primitives___diff_func_proto {
463-
void (*f)(int); /* incompatible function prototype */
464-
void *d;
465-
enum core_reloc_primitives_enum c;
468+
void (*f)(int) __bpf_aligned; /* incompatible function prototype */
469+
void *d __bpf_aligned;
470+
enum core_reloc_primitives_enum c __bpf_aligned;
466471
int b;
467472
char a;
468473
};
469474

470475
struct core_reloc_primitives___diff_ptr_type {
471-
const char * const d; /* different pointee type + modifiers */
472-
char a;
476+
const char * const d __bpf_aligned; /* different pointee type + modifiers */
477+
char a __bpf_aligned;
473478
int b;
474479
enum core_reloc_primitives_enum c;
475-
int (*f)(const char *);
480+
int (*f)(const char *) __bpf_aligned;
476481
};
477482

478483
struct core_reloc_primitives___err_non_enum {
479484
char a[1];
480485
int b;
481486
int c; /* int instead of enum */
482-
void *d;
483-
int (*f)(const char *);
487+
void *d __bpf_aligned;
488+
int (*f)(const char *) __bpf_aligned;
484489
};
485490

486491
struct core_reloc_primitives___err_non_int {
487492
char a[1];
488-
int *b; /* ptr instead of int */
489-
enum core_reloc_primitives_enum c;
490-
void *d;
491-
int (*f)(const char *);
493+
int *b __bpf_aligned; /* ptr instead of int */
494+
enum core_reloc_primitives_enum c __bpf_aligned;
495+
void *d __bpf_aligned;
496+
int (*f)(const char *) __bpf_aligned;
492497
};
493498

494499
struct core_reloc_primitives___err_non_ptr {
495500
char a[1];
496501
int b;
497502
enum core_reloc_primitives_enum c;
498503
int d; /* int instead of ptr */
499-
int (*f)(const char *);
504+
int (*f)(const char *) __bpf_aligned;
500505
};
501506

502507
/*
@@ -507,7 +512,7 @@ struct core_reloc_mods_output {
507512
};
508513

509514
typedef const int int_t;
510-
typedef const char *char_ptr_t;
515+
typedef const char *char_ptr_t __bpf_aligned;
511516
typedef const int arr_t[7];
512517

513518
struct core_reloc_mods_substruct {
@@ -523,9 +528,9 @@ typedef struct {
523528
struct core_reloc_mods {
524529
int a;
525530
int_t b;
526-
char *c;
531+
char *c __bpf_aligned;
527532
char_ptr_t d;
528-
int e[3];
533+
int e[3] __bpf_aligned;
529534
arr_t f;
530535
struct core_reloc_mods_substruct g;
531536
core_reloc_mods_substruct_t h;
@@ -535,9 +540,9 @@ struct core_reloc_mods {
535540
struct core_reloc_mods___mod_swap {
536541
int b;
537542
int_t a;
538-
char *d;
543+
char *d __bpf_aligned;
539544
char_ptr_t c;
540-
int f[3];
545+
int f[3] __bpf_aligned;
541546
arr_t e;
542547
struct {
543548
int y;
@@ -555,7 +560,7 @@ typedef arr1_t arr2_t;
555560
typedef arr2_t arr3_t;
556561
typedef arr3_t arr4_t;
557562

558-
typedef const char * const volatile fancy_char_ptr_t;
563+
typedef const char * const volatile fancy_char_ptr_t __bpf_aligned;
559564

560565
typedef core_reloc_mods_substruct_t core_reloc_mods_substruct_tt;
561566

@@ -567,7 +572,7 @@ struct core_reloc_mods___typedefs {
567572
arr4_t e;
568573
fancy_char_ptr_t d;
569574
fancy_char_ptr_t c;
570-
int3_t b;
575+
int3_t b __bpf_aligned;
571576
int3_t a;
572577
};
573578

@@ -739,19 +744,19 @@ struct core_reloc_bitfields___bit_sz_change {
739744
int8_t sb4: 1; /* 4 -> 1 */
740745
int32_t sb20: 30; /* 20 -> 30 */
741746
/* non-bitfields */
742-
uint16_t u32; /* 32 -> 16 */
743-
int64_t s32; /* 32 -> 64 */
747+
uint16_t u32; /* 32 -> 16 */
748+
int64_t s32 __bpf_aligned; /* 32 -> 64 */
744749
};
745750

746751
/* turn bitfield into non-bitfield and vice versa */
747752
struct core_reloc_bitfields___bitfield_vs_int {
748753
uint64_t ub1; /* 3 -> 64 non-bitfield */
749754
uint8_t ub2; /* 20 -> 8 non-bitfield */
750-
int64_t ub7; /* 7 -> 64 non-bitfield signed */
751-
int64_t sb4; /* 4 -> 64 non-bitfield signed */
752-
uint64_t sb20; /* 20 -> 16 non-bitfield unsigned */
753-
int32_t u32: 20; /* 32 non-bitfield -> 20 bitfield */
754-
uint64_t s32: 60; /* 32 non-bitfield -> 60 bitfield */
755+
int64_t ub7 __bpf_aligned; /* 7 -> 64 non-bitfield signed */
756+
int64_t sb4 __bpf_aligned; /* 4 -> 64 non-bitfield signed */
757+
uint64_t sb20 __bpf_aligned; /* 20 -> 16 non-bitfield unsigned */
758+
int32_t u32: 20; /* 32 non-bitfield -> 20 bitfield */
759+
uint64_t s32: 60 __bpf_aligned; /* 32 non-bitfield -> 60 bitfield */
755760
};
756761

757762
struct core_reloc_bitfields___just_big_enough {

0 commit comments

Comments
 (0)