Skip to content

Commit

Permalink
selftests/bpf: add CO-RE relocs array tests
Browse files Browse the repository at this point in the history
Add tests for various array handling/relocation scenarios.

Signed-off-by: Andrii Nakryiko <andriin@fb.com>
Acked-by: Song Liu <songliubraving@fb.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
  • Loading branch information
Andrii Nakryiko authored and Alexei Starovoitov committed Aug 7, 2019
1 parent ec6438a commit 20a9ad2
Show file tree
Hide file tree
Showing 11 changed files with 201 additions and 0 deletions.
41 changes: 41 additions & 0 deletions tools/testing/selftests/bpf/prog_tests/core_reloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,36 @@
.fails = true, \
}

#define ARRAYS_DATA(struct_name) STRUCT_TO_CHAR_PTR(struct_name) { \
.a = { [2] = 1 }, \
.b = { [1] = { [2] = { [3] = 2 } } }, \
.c = { [1] = { .c = 3 } }, \
.d = { [0] = { [0] = { .d = 4 } } }, \
}

#define ARRAYS_CASE_COMMON(name) \
.case_name = #name, \
.bpf_obj_file = "test_core_reloc_arrays.o", \
.btf_src_file = "btf__core_reloc_" #name ".o"

#define ARRAYS_CASE(name) { \
ARRAYS_CASE_COMMON(name), \
.input = ARRAYS_DATA(core_reloc_##name), \
.input_len = sizeof(struct core_reloc_##name), \
.output = STRUCT_TO_CHAR_PTR(core_reloc_arrays_output) { \
.a2 = 1, \
.b123 = 2, \
.c1c = 3, \
.d00d = 4, \
}, \
.output_len = sizeof(struct core_reloc_arrays_output) \
}

#define ARRAYS_ERR_CASE(name) { \
ARRAYS_CASE_COMMON(name), \
.fails = true, \
}

struct core_reloc_test_case {
const char *case_name;
const char *bpf_obj_file;
Expand Down Expand Up @@ -96,6 +126,17 @@ static struct core_reloc_test_case test_cases[] = {
NESTING_ERR_CASE(nesting___err_dup_incompat_types),
NESTING_ERR_CASE(nesting___err_partial_match_dups),
NESTING_ERR_CASE(nesting___err_too_deep),

/* various array access relocation scenarios */
ARRAYS_CASE(arrays),
ARRAYS_CASE(arrays___diff_arr_dim),
ARRAYS_CASE(arrays___diff_arr_val_sz),

ARRAYS_ERR_CASE(arrays___err_too_small),
ARRAYS_ERR_CASE(arrays___err_too_shallow),
ARRAYS_ERR_CASE(arrays___err_non_array),
ARRAYS_ERR_CASE(arrays___err_wrong_val_type1),
ARRAYS_ERR_CASE(arrays___err_wrong_val_type2),
};

struct data {
Expand Down
3 changes: 3 additions & 0 deletions tools/testing/selftests/bpf/progs/btf__core_reloc_arrays.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#include "core_reloc_types.h"

void f(struct core_reloc_arrays x) {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#include "core_reloc_types.h"

void f(struct core_reloc_arrays___diff_arr_dim x) {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#include "core_reloc_types.h"

void f(struct core_reloc_arrays___diff_arr_val_sz x) {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#include "core_reloc_types.h"

void f(struct core_reloc_arrays___err_non_array x) {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#include "core_reloc_types.h"

void f(struct core_reloc_arrays___err_too_shallow x) {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#include "core_reloc_types.h"

void f(struct core_reloc_arrays___err_too_small x) {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#include "core_reloc_types.h"

void f(struct core_reloc_arrays___err_wrong_val_type1 x) {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#include "core_reloc_types.h"

void f(struct core_reloc_arrays___err_wrong_val_type2 x) {}
81 changes: 81 additions & 0 deletions tools/testing/selftests/bpf/progs/core_reloc_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -306,3 +306,84 @@ struct core_reloc_nesting___err_too_deep {
} b;
} b;
};

/*
* ARRAYS
*/
struct core_reloc_arrays_output {
int a2;
char b123;
int c1c;
int d00d;
};

struct core_reloc_arrays_substruct {
int c;
int d;
};

struct core_reloc_arrays {
int a[5];
char b[2][3][4];
struct core_reloc_arrays_substruct c[3];
struct core_reloc_arrays_substruct d[1][2];
};

/* bigger array dimensions */
struct core_reloc_arrays___diff_arr_dim {
int a[7];
char b[3][4][5];
struct core_reloc_arrays_substruct c[4];
struct core_reloc_arrays_substruct d[2][3];
};

/* different size of array's value (struct) */
struct core_reloc_arrays___diff_arr_val_sz {
int a[5];
char b[2][3][4];
struct {
int __padding1;
int c;
int __padding2;
} c[3];
struct {
int __padding1;
int d;
int __padding2;
} d[1][2];
};

struct core_reloc_arrays___err_too_small {
int a[2]; /* this one is too small */
char b[2][3][4];
struct core_reloc_arrays_substruct c[3];
struct core_reloc_arrays_substruct d[1][2];
};

struct core_reloc_arrays___err_too_shallow {
int a[5];
char b[2][3]; /* this one lacks one dimension */
struct core_reloc_arrays_substruct c[3];
struct core_reloc_arrays_substruct d[1][2];
};

struct core_reloc_arrays___err_non_array {
int a; /* not an array */
char b[2][3][4];
struct core_reloc_arrays_substruct c[3];
struct core_reloc_arrays_substruct d[1][2];
};

struct core_reloc_arrays___err_wrong_val_type1 {
char a[5]; /* char instead of int */
char b[2][3][4];
struct core_reloc_arrays_substruct c[3];
struct core_reloc_arrays_substruct d[1][2];
};

struct core_reloc_arrays___err_wrong_val_type2 {
int a[5];
char b[2][3][4];
int c[3]; /* value is not a struct */
struct core_reloc_arrays_substruct d[1][2];
};
55 changes: 55 additions & 0 deletions tools/testing/selftests/bpf/progs/test_core_reloc_arrays.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// 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_arrays_output {
int a2;
char b123;
int c1c;
int d00d;
};

struct core_reloc_arrays_substruct {
int c;
int d;
};

struct core_reloc_arrays {
int a[5];
char b[2][3][4];
struct core_reloc_arrays_substruct c[3];
struct core_reloc_arrays_substruct d[1][2];
};

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

/* in->a[2] */
if (BPF_CORE_READ(&out->a2, &in->a[2]))
return 1;
/* in->b[1][2][3] */
if (BPF_CORE_READ(&out->b123, &in->b[1][2][3]))
return 1;
/* in->c[1].c */
if (BPF_CORE_READ(&out->c1c, &in->c[1].c))
return 1;
/* in->d[0][0].d */
if (BPF_CORE_READ(&out->d00d, &in->d[0][0].d))
return 1;

return 0;
}

0 comments on commit 20a9ad2

Please sign in to comment.