Skip to content

Commit

Permalink
Merge branch 'support-flex-arrays'
Browse files Browse the repository at this point in the history
Andrii Nakryiko says:

====================
Add support for flexible array accesses in a relocatable manner in BPF CO-RE.
It's a typical pattern in C, and kernel in particular, to provide
a fixed-length struct with zero-sized or dimensionless array at the end. In
such cases variable-sized array contents follows immediately after the end of
a struct. This patch set adds support for such access pattern by allowing
accesses to such arrays.

Patch #1 adds libbpf support. Patch #2 adds few test cases for validation.
====================

Signed-off-by: Alexei Starovoitov <[email protected]>
  • Loading branch information
Alexei Starovoitov committed Dec 16, 2019
2 parents 01c6f7a + 5f2eece commit 0849e10
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 9 deletions.
34 changes: 29 additions & 5 deletions tools/lib/bpf/libbpf.c
Original file line number Diff line number Diff line change
Expand Up @@ -3108,6 +3108,21 @@ static bool str_is_empty(const char *s)
return !s || !s[0];
}

static bool is_flex_arr(const struct btf *btf,
const struct bpf_core_accessor *acc,
const struct btf_array *arr)
{
const struct btf_type *t;

/* not a flexible array, if not inside a struct or has non-zero size */
if (!acc->name || arr->nelems > 0)
return false;

/* has to be the last member of enclosing struct */
t = btf__type_by_id(btf, acc->type_id);
return acc->idx == btf_vlen(t) - 1;
}

/*
* Turn bpf_field_reloc into a low- and high-level spec representation,
* validating correctness along the way, as well as calculating resulting
Expand Down Expand Up @@ -3145,6 +3160,7 @@ static int bpf_core_spec_parse(const struct btf *btf,
struct bpf_core_spec *spec)
{
int access_idx, parsed_len, i;
struct bpf_core_accessor *acc;
const struct btf_type *t;
const char *name;
__u32 id;
Expand Down Expand Up @@ -3192,6 +3208,7 @@ static int bpf_core_spec_parse(const struct btf *btf,
return -EINVAL;

access_idx = spec->raw_spec[i];
acc = &spec->spec[spec->len];

if (btf_is_composite(t)) {
const struct btf_member *m;
Expand All @@ -3209,18 +3226,23 @@ static int bpf_core_spec_parse(const struct btf *btf,
if (str_is_empty(name))
return -EINVAL;

spec->spec[spec->len].type_id = id;
spec->spec[spec->len].idx = access_idx;
spec->spec[spec->len].name = name;
acc->type_id = id;
acc->idx = access_idx;
acc->name = name;
spec->len++;
}

id = m->type;
} else if (btf_is_array(t)) {
const struct btf_array *a = btf_array(t);
bool flex;

t = skip_mods_and_typedefs(btf, a->type, &id);
if (!t || access_idx >= a->nelems)
if (!t)
return -EINVAL;

flex = is_flex_arr(btf, acc - 1, a);
if (!flex && access_idx >= a->nelems)
return -EINVAL;

spec->spec[spec->len].type_id = id;
Expand Down Expand Up @@ -3525,12 +3547,14 @@ static int bpf_core_spec_match(struct bpf_core_spec *local_spec,
*/
if (i > 0) {
const struct btf_array *a;
bool flex;

if (!btf_is_array(targ_type))
return 0;

a = btf_array(targ_type);
if (local_acc->idx >= a->nelems)
flex = is_flex_arr(targ_btf, targ_acc - 1, a);
if (!flex && local_acc->idx >= a->nelems)
return 0;
if (!skip_mods_and_typedefs(targ_btf, a->type,
&targ_id))
Expand Down
4 changes: 4 additions & 0 deletions tools/testing/selftests/bpf/prog_tests/core_reloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@
.b123 = 2, \
.c1c = 3, \
.d00d = 4, \
.f10c = 0, \
}, \
.output_len = sizeof(struct core_reloc_arrays_output) \
}
Expand Down Expand Up @@ -308,12 +309,15 @@ static struct core_reloc_test_case test_cases[] = {
ARRAYS_CASE(arrays),
ARRAYS_CASE(arrays___diff_arr_dim),
ARRAYS_CASE(arrays___diff_arr_val_sz),
ARRAYS_CASE(arrays___equiv_zero_sz_arr),
ARRAYS_CASE(arrays___fixed_arr),

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),
ARRAYS_ERR_CASE(arrays___err_bad_zero_sz_arr),

/* enum/ptr/int handling scenarios */
PRIMITIVES_CASE(primitives),
Expand Down
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___equiv_zero_sz_arr 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_bad_zero_sz_arr 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___fixed_arr x) {}
39 changes: 39 additions & 0 deletions tools/testing/selftests/bpf/progs/core_reloc_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,7 @@ struct core_reloc_arrays_output {
char b123;
int c1c;
int d00d;
int f10c;
};

struct core_reloc_arrays_substruct {
Expand All @@ -339,6 +340,7 @@ struct core_reloc_arrays {
char b[2][3][4];
struct core_reloc_arrays_substruct c[3];
struct core_reloc_arrays_substruct d[1][2];
struct core_reloc_arrays_substruct f[][2];
};

/* bigger array dimensions */
Expand All @@ -347,6 +349,7 @@ struct core_reloc_arrays___diff_arr_dim {
char b[3][4][5];
struct core_reloc_arrays_substruct c[4];
struct core_reloc_arrays_substruct d[2][3];
struct core_reloc_arrays_substruct f[1][3];
};

/* different size of array's value (struct) */
Expand All @@ -363,34 +366,70 @@ struct core_reloc_arrays___diff_arr_val_sz {
int d;
int __padding2;
} d[1][2];
struct {
int __padding1;
int c;
int __padding2;
} f[][2];
};

struct core_reloc_arrays___equiv_zero_sz_arr {
int a[5];
char b[2][3][4];
struct core_reloc_arrays_substruct c[3];
struct core_reloc_arrays_substruct d[1][2];
/* equivalent to flexible array */
struct core_reloc_arrays_substruct f[0][2];
};

struct core_reloc_arrays___fixed_arr {
int a[5];
char b[2][3][4];
struct core_reloc_arrays_substruct c[3];
struct core_reloc_arrays_substruct d[1][2];
/* not a flexible array anymore, but within access bounds */
struct core_reloc_arrays_substruct f[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_substruct f[][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_substruct f[][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_substruct f[][2];
};

struct core_reloc_arrays___err_wrong_val_type {
int a[5];
char b[2][3][4];
int c[3]; /* value is not a struct */
struct core_reloc_arrays_substruct d[1][2];
struct core_reloc_arrays_substruct f[][2];
};

struct core_reloc_arrays___err_bad_zero_sz_arr {
/* zero-sized array, but not at the end */
struct core_reloc_arrays_substruct f[0][2];
int a[5];
char b[2][3][4];
struct core_reloc_arrays_substruct c[3];
struct core_reloc_arrays_substruct d[1][2];
};

/*
Expand Down
8 changes: 4 additions & 4 deletions tools/testing/selftests/bpf/progs/test_core_reloc_arrays.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ struct core_reloc_arrays_output {
char b123;
int c1c;
int d00d;
int f01c;
};

struct core_reloc_arrays_substruct {
Expand All @@ -30,6 +31,7 @@ struct core_reloc_arrays {
char b[2][3][4];
struct core_reloc_arrays_substruct c[3];
struct core_reloc_arrays_substruct d[1][2];
struct core_reloc_arrays_substruct f[][2];
};

#define CORE_READ(dst, src) bpf_core_read(dst, sizeof(*(dst)), src)
Expand All @@ -40,18 +42,16 @@ 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 (CORE_READ(&out->a2, &in->a[2]))
return 1;
/* in->b[1][2][3] */
if (CORE_READ(&out->b123, &in->b[1][2][3]))
return 1;
/* in->c[1].c */
if (CORE_READ(&out->c1c, &in->c[1].c))
return 1;
/* in->d[0][0].d */
if (CORE_READ(&out->d00d, &in->d[0][0].d))
return 1;
if (CORE_READ(&out->f01c, &in->f[0][1].c))
return 1;

return 0;
}
Expand Down

0 comments on commit 0849e10

Please sign in to comment.