Skip to content

Commit

Permalink
bpf: rename BPF_F_TEST_SANITY_STRICT to BPF_F_TEST_REG_INVARIANTS
Browse files Browse the repository at this point in the history
Rename verifier internal flag BPF_F_TEST_SANITY_STRICT to more neutral
BPF_F_TEST_REG_INVARIANTS. This is a follow up to [0].

A few selftests and veristat need to be adjusted in the same patch as
well.

  [0] https://patchwork.kernel.org/project/netdevbpf/patch/[email protected]/

Signed-off-by: Andrii Nakryiko <[email protected]>
Link: https://lore.kernel.org/r/[email protected]
Signed-off-by: Alexei Starovoitov <[email protected]>
  • Loading branch information
anakryiko authored and Alexei Starovoitov committed Nov 17, 2023
1 parent 5fa201f commit ff8867a
Show file tree
Hide file tree
Showing 13 changed files with 24 additions and 25 deletions.
2 changes: 1 addition & 1 deletion include/linux/bpf_verifier.h
Original file line number Diff line number Diff line change
Expand Up @@ -602,7 +602,7 @@ struct bpf_verifier_env {
int stack_size; /* number of states to be processed */
bool strict_alignment; /* perform strict pointer alignment checks */
bool test_state_freq; /* test verifier with different pruning frequency */
bool test_sanity_strict; /* fail verification on sanity violations */
bool test_reg_invariants; /* fail verification on register invariants violations */
struct bpf_verifier_state *cur_state; /* current verifier state */
struct bpf_verifier_state_list **explored_states; /* search pruning optimization */
struct bpf_verifier_state_list *free_list;
Expand Down
2 changes: 1 addition & 1 deletion include/uapi/linux/bpf.h
Original file line number Diff line number Diff line change
Expand Up @@ -1201,7 +1201,7 @@ enum bpf_perf_event_type {
#define BPF_F_XDP_DEV_BOUND_ONLY (1U << 6)

/* The verifier internal test flag. Behavior is undefined */
#define BPF_F_TEST_SANITY_STRICT (1U << 7)
#define BPF_F_TEST_REG_INVARIANTS (1U << 7)

/* link_create.kprobe_multi.flags used in LINK_CREATE command for
* BPF_TRACE_KPROBE_MULTI attach type to create return probe.
Expand Down
2 changes: 1 addition & 1 deletion kernel/bpf/syscall.c
Original file line number Diff line number Diff line change
Expand Up @@ -2574,7 +2574,7 @@ static int bpf_prog_load(union bpf_attr *attr, bpfptr_t uattr, u32 uattr_size)
BPF_F_TEST_RND_HI32 |
BPF_F_XDP_HAS_FRAGS |
BPF_F_XDP_DEV_BOUND_ONLY |
BPF_F_TEST_SANITY_STRICT))
BPF_F_TEST_REG_INVARIANTS))
return -EINVAL;

if (!IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) &&
Expand Down
6 changes: 3 additions & 3 deletions kernel/bpf/verifier.c
Original file line number Diff line number Diff line change
Expand Up @@ -2608,14 +2608,14 @@ static int reg_bounds_sanity_check(struct bpf_verifier_env *env,

return 0;
out:
verbose(env, "REG SANITY VIOLATION (%s): %s u64=[%#llx, %#llx] "
verbose(env, "REG INVARIANTS VIOLATION (%s): %s u64=[%#llx, %#llx] "
"s64=[%#llx, %#llx] u32=[%#x, %#x] s32=[%#x, %#x] var_off=(%#llx, %#llx)\n",
ctx, msg, reg->umin_value, reg->umax_value,
reg->smin_value, reg->smax_value,
reg->u32_min_value, reg->u32_max_value,
reg->s32_min_value, reg->s32_max_value,
reg->var_off.value, reg->var_off.mask);
if (env->test_sanity_strict)
if (env->test_reg_invariants)
return -EFAULT;
__mark_reg_unbounded(reg);
return 0;
Expand Down Expand Up @@ -20791,7 +20791,7 @@ int bpf_check(struct bpf_prog **prog, union bpf_attr *attr, bpfptr_t uattr, __u3

if (is_priv)
env->test_state_freq = attr->prog_flags & BPF_F_TEST_STATE_FREQ;
env->test_sanity_strict = attr->prog_flags & BPF_F_TEST_SANITY_STRICT;
env->test_reg_invariants = attr->prog_flags & BPF_F_TEST_REG_INVARIANTS;

env->explored_states = kvcalloc(state_htab_size(env),
sizeof(struct bpf_verifier_state_list *),
Expand Down
2 changes: 1 addition & 1 deletion tools/include/uapi/linux/bpf.h
Original file line number Diff line number Diff line change
Expand Up @@ -1201,7 +1201,7 @@ enum bpf_perf_event_type {
#define BPF_F_XDP_DEV_BOUND_ONLY (1U << 6)

/* The verifier internal test flag. Behavior is undefined */
#define BPF_F_TEST_SANITY_STRICT (1U << 7)
#define BPF_F_TEST_REG_INVARIANTS (1U << 7)

/* link_create.kprobe_multi.flags used in LINK_CREATE command for
* BPF_TRACE_KPROBE_MULTI attach type to create return probe.
Expand Down
2 changes: 1 addition & 1 deletion tools/testing/selftests/bpf/prog_tests/bpf_verif_scale.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ static int check_load(const char *file, enum bpf_prog_type type)
}

bpf_program__set_type(prog, type);
bpf_program__set_flags(prog, BPF_F_TEST_RND_HI32 | BPF_F_TEST_SANITY_STRICT);
bpf_program__set_flags(prog, BPF_F_TEST_RND_HI32 | BPF_F_TEST_REG_INVARIANTS);
bpf_program__set_log_level(prog, 4 | extra_prog_load_log_flags);

err = bpf_object__load(obj);
Expand Down
2 changes: 1 addition & 1 deletion tools/testing/selftests/bpf/prog_tests/reg_bounds.c
Original file line number Diff line number Diff line change
Expand Up @@ -838,7 +838,7 @@ static int load_range_cmp_prog(struct range x, struct range y, enum op op,
.log_level = 2,
.log_buf = log_buf,
.log_size = log_sz,
.prog_flags = BPF_F_TEST_SANITY_STRICT,
.prog_flags = BPF_F_TEST_REG_INVARIANTS,
);

/* ; skip exit block below
Expand Down
4 changes: 2 additions & 2 deletions tools/testing/selftests/bpf/progs/verifier_bounds.c
Original file line number Diff line number Diff line change
Expand Up @@ -965,7 +965,7 @@ l0_%=: r0 = 0; \
SEC("xdp")
__description("bound check with JMP_JSLT for crossing 64-bit signed boundary")
__success __retval(0)
__flag(!BPF_F_TEST_SANITY_STRICT) /* known sanity violation */
__flag(!BPF_F_TEST_REG_INVARIANTS) /* known invariants violation */
__naked void crossing_64_bit_signed_boundary_2(void)
{
asm volatile (" \
Expand Down Expand Up @@ -1047,7 +1047,7 @@ l0_%=: r0 = 0; \
SEC("xdp")
__description("bound check with JMP32_JSLT for crossing 32-bit signed boundary")
__success __retval(0)
__flag(!BPF_F_TEST_SANITY_STRICT) /* known sanity violation */
__flag(!BPF_F_TEST_REG_INVARIANTS) /* known invariants violation */
__naked void crossing_32_bit_signed_boundary_2(void)
{
asm volatile (" \
Expand Down
6 changes: 3 additions & 3 deletions tools/testing/selftests/bpf/test_loader.c
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ static int parse_test_spec(struct test_loader *tester,
memset(spec, 0, sizeof(*spec));

spec->prog_name = bpf_program__name(prog);
spec->prog_flags = BPF_F_TEST_SANITY_STRICT; /* by default be strict */
spec->prog_flags = BPF_F_TEST_REG_INVARIANTS; /* by default be strict */

btf = bpf_object__btf(obj);
if (!btf) {
Expand Down Expand Up @@ -280,8 +280,8 @@ static int parse_test_spec(struct test_loader *tester,
update_flags(&spec->prog_flags, BPF_F_SLEEPABLE, clear);
} else if (strcmp(val, "BPF_F_XDP_HAS_FRAGS") == 0) {
update_flags(&spec->prog_flags, BPF_F_XDP_HAS_FRAGS, clear);
} else if (strcmp(val, "BPF_F_TEST_SANITY_STRICT") == 0) {
update_flags(&spec->prog_flags, BPF_F_TEST_SANITY_STRICT, clear);
} else if (strcmp(val, "BPF_F_TEST_REG_INVARIANTS") == 0) {
update_flags(&spec->prog_flags, BPF_F_TEST_REG_INVARIANTS, clear);
} else /* assume numeric value */ {
err = parse_int(val, &flags, "test prog flags");
if (err)
Expand Down
3 changes: 1 addition & 2 deletions tools/testing/selftests/bpf/test_sock_addr.c
Original file line number Diff line number Diff line change
Expand Up @@ -679,8 +679,7 @@ static int load_path(const struct sock_addr_test *test, const char *path)

bpf_program__set_type(prog, BPF_PROG_TYPE_CGROUP_SOCK_ADDR);
bpf_program__set_expected_attach_type(prog, test->expected_attach_type);
bpf_program__set_flags(prog, BPF_F_TEST_RND_HI32);
bpf_program__set_flags(prog, BPF_F_TEST_SANITY_STRICT);
bpf_program__set_flags(prog, BPF_F_TEST_RND_HI32 | BPF_F_TEST_REG_INVARIANTS);

err = bpf_object__load(obj);
if (err) {
Expand Down
2 changes: 1 addition & 1 deletion tools/testing/selftests/bpf/test_verifier.c
Original file line number Diff line number Diff line change
Expand Up @@ -1588,7 +1588,7 @@ static void do_test_single(struct bpf_test *test, bool unpriv,
if (fixup_skips != skips)
return;

pflags = BPF_F_TEST_RND_HI32 | BPF_F_TEST_SANITY_STRICT;
pflags = BPF_F_TEST_RND_HI32 | BPF_F_TEST_REG_INVARIANTS;
if (test->flags & F_LOAD_WITH_STRICT_ALIGNMENT)
pflags |= BPF_F_STRICT_ALIGNMENT;
if (test->flags & F_NEEDS_EFFICIENT_UNALIGNED_ACCESS)
Expand Down
4 changes: 2 additions & 2 deletions tools/testing/selftests/bpf/testing_helpers.c
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ int bpf_prog_test_load(const char *file, enum bpf_prog_type type,
if (type != BPF_PROG_TYPE_UNSPEC && bpf_program__type(prog) != type)
bpf_program__set_type(prog, type);

flags = bpf_program__flags(prog) | BPF_F_TEST_RND_HI32 | BPF_F_TEST_SANITY_STRICT;
flags = bpf_program__flags(prog) | BPF_F_TEST_RND_HI32 | BPF_F_TEST_REG_INVARIANTS;
bpf_program__set_flags(prog, flags);

err = bpf_object__load(obj);
Expand All @@ -299,7 +299,7 @@ int bpf_test_load_program(enum bpf_prog_type type, const struct bpf_insn *insns,
{
LIBBPF_OPTS(bpf_prog_load_opts, opts,
.kern_version = kern_version,
.prog_flags = BPF_F_TEST_RND_HI32 | BPF_F_TEST_SANITY_STRICT,
.prog_flags = BPF_F_TEST_RND_HI32 | BPF_F_TEST_REG_INVARIANTS,
.log_level = extra_prog_load_log_flags,
.log_buf = log_buf,
.log_size = log_buf_sz,
Expand Down
12 changes: 6 additions & 6 deletions tools/testing/selftests/bpf/veristat.c
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ static struct env {
bool debug;
bool quiet;
bool force_checkpoints;
bool strict_range_sanity;
bool force_reg_invariants;
enum resfmt out_fmt;
bool show_version;
bool comparison_mode;
Expand Down Expand Up @@ -225,8 +225,8 @@ static const struct argp_option opts[] = {
{ "filter", 'f', "FILTER", 0, "Filter expressions (or @filename for file with expressions)." },
{ "test-states", 't', NULL, 0,
"Force frequent BPF verifier state checkpointing (set BPF_F_TEST_STATE_FREQ program flag)" },
{ "test-sanity", 'r', NULL, 0,
"Force strict BPF verifier register sanity behavior (BPF_F_TEST_SANITY_STRICT program flag)" },
{ "test-reg-invariants", 'r', NULL, 0,
"Force BPF verifier failure on register invariant violation (BPF_F_TEST_REG_INVARIANTS program flag)" },
{},
};

Expand Down Expand Up @@ -299,7 +299,7 @@ static error_t parse_arg(int key, char *arg, struct argp_state *state)
env.force_checkpoints = true;
break;
case 'r':
env.strict_range_sanity = true;
env.force_reg_invariants = true;
break;
case 'n':
errno = 0;
Expand Down Expand Up @@ -1028,8 +1028,8 @@ static int process_prog(const char *filename, struct bpf_object *obj, struct bpf

if (env.force_checkpoints)
bpf_program__set_flags(prog, bpf_program__flags(prog) | BPF_F_TEST_STATE_FREQ);
if (env.strict_range_sanity)
bpf_program__set_flags(prog, bpf_program__flags(prog) | BPF_F_TEST_SANITY_STRICT);
if (env.force_reg_invariants)
bpf_program__set_flags(prog, bpf_program__flags(prog) | BPF_F_TEST_REG_INVARIANTS);

err = bpf_object__load(obj);
env.progs_processed++;
Expand Down

0 comments on commit ff8867a

Please sign in to comment.