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: Andrii Nakryiko <andriin@fb.com>
Acked-by: Song Liu <songliubraving@fb.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
This commit is contained in:
Andrii Nakryiko 2019-08-07 14:39:59 -07:00 committed by Alexei Starovoitov
parent 9654e2ae90
commit d698f9dbdb
5 changed files with 69 additions and 0 deletions

View file

@ -129,6 +129,22 @@
.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 {
const char *case_name;
const char *bpf_obj_file;
@ -200,6 +216,10 @@ static struct core_reloc_test_case test_cases[] = {
MODS_CASE(mods),
MODS_CASE(mods___mod_swap),
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 {

View file

@ -0,0 +1,3 @@
#include "core_reloc_types.h"
void f(struct core_reloc_ptr_as_arr x) {}

View file

@ -0,0 +1,3 @@
#include "core_reloc_types.h"
void f(struct core_reloc_ptr_as_arr___diff_sz x) {}

View file

@ -526,3 +526,16 @@ struct core_reloc_mods___typedefs {
int3_t b;
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;
};

View file

@ -0,0 +1,30 @@
// 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;
}