Commit d698f9db authored by Andrii Nakryiko's avatar Andrii Nakryiko Committed by Alexei Starovoitov
Browse files

selftests/bpf: add CO-RE relocs ptr-as-array tests



Add test validating correct relocation handling for cases where pointer
to something is used as an array. E.g.:

  int *ptr = ...;
  int x = ptr[42];

Signed-off-by: default avatarAndrii Nakryiko <andriin@fb.com>
Acked-by: default avatarSong Liu <songliubraving@fb.com>
Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
parent 9654e2ae
Loading
Loading
Loading
Loading
+20 −0
Original line number Original line Diff line number Diff line
@@ -129,6 +129,22 @@
	.output_len = sizeof(struct core_reloc_mods_output),		\
	.output_len = sizeof(struct core_reloc_mods_output),		\
}
}


#define PTR_AS_ARR_CASE(name) {						\
	.case_name = #name,						\
	.bpf_obj_file = "test_core_reloc_ptr_as_arr.o",			\
	.btf_src_file = "btf__core_reloc_" #name ".o",			\
	.input = (const char *)&(struct core_reloc_##name []){		\
		{ .a = 1 },						\
		{ .a = 2 },						\
		{ .a = 3 },						\
	},								\
	.input_len = 3 * sizeof(struct core_reloc_##name),		\
	.output = STRUCT_TO_CHAR_PTR(core_reloc_ptr_as_arr) {		\
		.a = 3,							\
	},								\
	.output_len = sizeof(struct core_reloc_ptr_as_arr),		\
}

struct core_reloc_test_case {
struct core_reloc_test_case {
	const char *case_name;
	const char *case_name;
	const char *bpf_obj_file;
	const char *bpf_obj_file;
@@ -200,6 +216,10 @@ static struct core_reloc_test_case test_cases[] = {
	MODS_CASE(mods),
	MODS_CASE(mods),
	MODS_CASE(mods___mod_swap),
	MODS_CASE(mods___mod_swap),
	MODS_CASE(mods___typedefs),
	MODS_CASE(mods___typedefs),

	/* handling "ptr is an array" semantics */
	PTR_AS_ARR_CASE(ptr_as_arr),
	PTR_AS_ARR_CASE(ptr_as_arr___diff_sz),
};
};


struct data {
struct data {
+3 −0
Original line number Original line Diff line number Diff line
#include "core_reloc_types.h"

void f(struct core_reloc_ptr_as_arr x) {}
+3 −0
Original line number Original line Diff line number Diff line
#include "core_reloc_types.h"

void f(struct core_reloc_ptr_as_arr___diff_sz x) {}
+13 −0
Original line number Original line Diff line number Diff line
@@ -526,3 +526,16 @@ struct core_reloc_mods___typedefs {
	int3_t b;
	int3_t b;
	int3_t a;
	int3_t a;
};
};

/*
 * PTR_AS_ARR
 */
struct core_reloc_ptr_as_arr {
	int a;
};

struct core_reloc_ptr_as_arr___diff_sz {
	int :32; /* padding */
	char __some_more_padding;
	int a;
};
+30 −0
Original line number Original line Diff line number Diff line
// SPDX-License-Identifier: GPL-2.0
// Copyright (c) 2019 Facebook

#include <linux/bpf.h>
#include <stdint.h>
#include "bpf_helpers.h"

char _license[] SEC("license") = "GPL";

static volatile struct data {
	char in[256];
	char out[256];
} data;

struct core_reloc_ptr_as_arr {
	int a;
};

SEC("raw_tracepoint/sys_enter")
int test_core_ptr_as_arr(void *ctx)
{
	struct core_reloc_ptr_as_arr *in = (void *)&data.in;
	struct core_reloc_ptr_as_arr *out = (void *)&data.out;

	if (BPF_CORE_READ(&out->a, &in[2].a))
		return 1;

	return 0;
}