Skip to content

Commit

Permalink
WIP: Add tests to relocs_read.c. Fix bitfield to no-bitfield relo.
Browse files Browse the repository at this point in the history
Signed-off-by: Jussi Maki <jussi@isovalent.com>
  • Loading branch information
joamaki committed Mar 8, 2022
1 parent 626d09b commit b002f00
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 4 deletions.
2 changes: 1 addition & 1 deletion internal/btf/core.go
Expand Up @@ -681,7 +681,7 @@ func coreFindField(local Type, localAcc coreAccessor, target Type) (coreField, c
} else {
// Going from a bitfield to a normal field. Since the original BTF had it as a bitfield, we'll
// need to "emulate" a bitfield in target to compute the shifts correctly.
targetBitfieldSize = uint32(targetSize)
targetBitfieldSize = uint32(targetSize * 8)
}

if err := coreAreMembersCompatible(local, target); err != nil {
Expand Down
Binary file modified internal/btf/testdata/relocs_read-eb.elf
Binary file not shown.
Binary file modified internal/btf/testdata/relocs_read-el.elf
Binary file not shown.
72 changes: 70 additions & 2 deletions internal/btf/testdata/relocs_read.c
@@ -1,7 +1,10 @@
#include "../../../testdata/common.h"
#include "bpf_core_read.h"

#define core_access __builtin_preserve_access_index

char _license[] __attribute__((section(("license")), used)) = "GPL";

// Struct with the members declared in the wrong order. Accesses need
// a successful CO-RE relocation against the type in relocs_read_tgt.c
// for the test below to pass.
Expand All @@ -10,6 +13,22 @@ struct s {
char a;
};

typedef unsigned char u8;
typedef unsigned short u16;
typedef unsigned int u32;
typedef unsigned long u64;

// Struct with bitfields.
struct bits {
int x;
u8 a:4, b:2;
u16 c:1;
unsigned int d:2;
enum { ZERO = 0, ONE = 1 } e:1;
u64 f:16, g:30;
};


// Perform a read from a subprog to ensure CO-RE relocations
// occurring there are tracked and executed in the final linked program.
__attribute__((noinline)) int read_subprog() {
Expand All @@ -21,8 +40,57 @@ __attribute__((noinline)) int read_subprog() {
if (core_access(foo.a) == 0)
return __LINE__;

if (core_access(foo.b) == 1)
return __LINE__;
if (core_access(foo.b) == 1)
return __LINE__;

struct bits bar;
char *p = (char *)&bar;
/* Target:
* [4] STRUCT 'bits' size=8 vlen=7
* 'b' type_id=5 bits_offset=0 bitfield_size=2
* 'a' type_id=5 bits_offset=2 bitfield_size=4
* 'd' type_id=7 bits_offset=6 bitfield_size=2
* 'c' type_id=9 bits_offset=8 bitfield_size=1
* 'e' type_id=11 bits_offset=9 bitfield_size=1
* 'f' type_id=9 bits_offset=16
* 'g' type_id=12 bits_offset=32 bitfield_size=30
*/
*p++ = 0xff; // a, b, d
*p++ = 0x00; // c, e
*p++ = 0x56; // f
*p++ = 0x56; // f
#ifdef __BIG_ENDIAN__
*p++ = 0x55; // g
*p++ = 0x44; // g
*p++ = 0x33; // g
*p++ = 0x22; // g
#else
*p++ = 0x22; // g
*p++ = 0x33; // g
*p++ = 0x44; // g
*p++ = 0x55; // g
#endif

if (BPF_CORE_READ_BITFIELD(&bar, a) != (1<<4)-1)
return __LINE__;

if (BPF_CORE_READ_BITFIELD(&bar, b) != (1<<2)-1)
return __LINE__;

if (BPF_CORE_READ_BITFIELD(&bar, d) != (1<<2)-1)
return __LINE__;

if (BPF_CORE_READ_BITFIELD(&bar, c) != 0)
return __LINE__;

if (BPF_CORE_READ_BITFIELD(&bar, e) != 0)
return __LINE__;

if (BPF_CORE_READ_BITFIELD(&bar, f) != 0x5656)
return __LINE__;

if (BPF_CORE_READ_BITFIELD(&bar, g) != 0x15443322)
return __LINE__;

return 0;
}
Expand Down
Binary file modified internal/btf/testdata/relocs_read_tgt-eb.elf
Binary file not shown.
Binary file modified internal/btf/testdata/relocs_read_tgt-el.elf
Binary file not shown.
19 changes: 18 additions & 1 deletion internal/btf/testdata/relocs_read_tgt.c
Expand Up @@ -11,6 +11,23 @@ struct s {
char b;
};

typedef unsigned int my_u32;
typedef unsigned char u8;
typedef unsigned short u16;
typedef unsigned int u32;
typedef unsigned long u64;

struct bits {
/*int x;*/
u8 b:2, a:4; /* a was before b */
my_u32 d:2; /* was 'unsigned int' */
u16 c:1; /* was before d */
enum { ZERO = 0, ONE = 1 } e:1;
u16 f; /* was: u64 f:16 */
u32 g:30; /* was: u64 g:30 */
};

int dummy() {
return core_access((struct s){}.a);
return core_access((struct s){}.a) +
core_access((struct bits){}.a);
}

0 comments on commit b002f00

Please sign in to comment.