Commit 785c3342 authored by Andrii Nakryiko's avatar Andrii Nakryiko Committed by Daniel Borkmann
Browse files

selftests/bpf: Add bpf_core_field_offset() tests



Add test cases for bpf_core_field_offset() helper.

Signed-off-by: default avatarAndrii Nakryiko <andrii@kernel.org>
Signed-off-by: default avatarDaniel Borkmann <daniel@iogearbox.net>
Link: https://lore.kernel.org/bpf/20220509004148.1801791-7-andrii@kernel.org
parent 7715f549
Loading
Loading
Loading
Loading
+11 −2
Original line number Diff line number Diff line
@@ -277,13 +277,21 @@ static int duration = 0;
#define SIZE_OUTPUT_DATA(type)						\
	STRUCT_TO_CHAR_PTR(core_reloc_size_output) {			\
		.int_sz = sizeof(((type *)0)->int_field),		\
		.int_off = offsetof(type, int_field),			\
		.struct_sz = sizeof(((type *)0)->struct_field),		\
		.struct_off = offsetof(type, struct_field),		\
		.union_sz = sizeof(((type *)0)->union_field),		\
		.union_off = offsetof(type, union_field),		\
		.arr_sz = sizeof(((type *)0)->arr_field),		\
		.arr_elem_sz = sizeof(((type *)0)->arr_field[0]),	\
		.arr_off = offsetof(type, arr_field),			\
		.arr_elem_sz = sizeof(((type *)0)->arr_field[1]),	\
		.arr_elem_off = offsetof(type, arr_field[1]),		\
		.ptr_sz = 8, /* always 8-byte pointer for BPF */	\
		.ptr_off = offsetof(type, ptr_field),			\
		.enum_sz = sizeof(((type *)0)->enum_field),		\
		.enum_off = offsetof(type, enum_field),			\
		.float_sz = sizeof(((type *)0)->float_field),		\
		.float_off = offsetof(type, float_field),		\
	}

#define SIZE_CASE(name) {						\
@@ -714,9 +722,10 @@ static const struct core_reloc_test_case test_cases[] = {
	}),
	BITFIELDS_ERR_CASE(bitfields___err_too_big_bitfield),

	/* size relocation checks */
	/* field size and offset relocation checks */
	SIZE_CASE(size),
	SIZE_CASE(size___diff_sz),
	SIZE_CASE(size___diff_offs),
	SIZE_ERR_CASE(size___err_ambiguous),

	/* validate type existence and size relocations */
+3 −0
Original line number Diff line number Diff line
#include "core_reloc_types.h"

void f(struct core_reloc_size___diff_offs x) {}
+18 −0
Original line number Diff line number Diff line
@@ -785,13 +785,21 @@ struct core_reloc_bitfields___err_too_big_bitfield {
 */
struct core_reloc_size_output {
	int int_sz;
	int int_off;
	int struct_sz;
	int struct_off;
	int union_sz;
	int union_off;
	int arr_sz;
	int arr_off;
	int arr_elem_sz;
	int arr_elem_off;
	int ptr_sz;
	int ptr_off;
	int enum_sz;
	int enum_off;
	int float_sz;
	int float_off;
};

struct core_reloc_size {
@@ -814,6 +822,16 @@ struct core_reloc_size___diff_sz {
	double float_field;
};

struct core_reloc_size___diff_offs {
	float float_field;
	enum { YET_OTHER_VALUE = 123 } enum_field;
	void *ptr_field;
	int arr_field[4];
	union { int x; } union_field;
	struct { int x; } struct_field;
	int int_field;
};

/* Error case of two candidates with the fields (int_field) at the same
 * offset, but with differing final relocation values: size 4 vs size 1
 */
+24 −1
Original line number Diff line number Diff line
@@ -15,13 +15,21 @@ struct {

struct core_reloc_size_output {
	int int_sz;
	int int_off;
	int struct_sz;
	int struct_off;
	int union_sz;
	int union_off;
	int arr_sz;
	int arr_off;
	int arr_elem_sz;
	int arr_elem_off;
	int ptr_sz;
	int ptr_off;
	int enum_sz;
	int enum_off;
	int float_sz;
	int float_off;
};

struct core_reloc_size {
@@ -41,13 +49,28 @@ int test_core_size(void *ctx)
	struct core_reloc_size_output *out = (void *)&data.out;

	out->int_sz = bpf_core_field_size(in->int_field);
	out->int_off = bpf_core_field_offset(in->int_field);

	out->struct_sz = bpf_core_field_size(in->struct_field);
	out->struct_off = bpf_core_field_offset(in->struct_field);

	out->union_sz = bpf_core_field_size(in->union_field);
	out->union_off = bpf_core_field_offset(in->union_field);

	out->arr_sz = bpf_core_field_size(in->arr_field);
	out->arr_elem_sz = bpf_core_field_size(struct core_reloc_size, arr_field[0]);
	out->arr_off = bpf_core_field_offset(in->arr_field);

	out->arr_elem_sz = bpf_core_field_size(struct core_reloc_size, arr_field[1]);
	out->arr_elem_off = bpf_core_field_offset(struct core_reloc_size, arr_field[1]);

	out->ptr_sz = bpf_core_field_size(struct core_reloc_size, ptr_field);
	out->ptr_off = bpf_core_field_offset(struct core_reloc_size, ptr_field);

	out->enum_sz = bpf_core_field_size(struct core_reloc_size, enum_field);
	out->enum_off = bpf_core_field_offset(struct core_reloc_size, enum_field);

	out->float_sz = bpf_core_field_size(struct core_reloc_size, float_field);
	out->float_off = bpf_core_field_offset(struct core_reloc_size, float_field);

	return 0;
}