* [PATCH bpf-next 1/4] bpf: Keep the lowest offset of a stack pointer that moves up
2026-12-03 19:58 [PATCH bpf-next 0/4] bpf: Keep fewer states apart because of pointers Alexei Starovoitov
@ 2026-12-03 19:58 ` Alexei Starovoitov
2026-12-03 20:31 ` sashiko-bot
2026-12-03 19:58 ` [PATCH bpf-next 2/4] bpf: Let bpf_dynptr_from_mem() take memory that nothing invalidates Alexei Starovoitov
` (2 subsequent siblings)
3 siblings, 1 reply; 7+ messages in thread
From: Alexei Starovoitov @ 2026-12-03 19:58 UTC (permalink / raw)
To: bpf; +Cc: daniel, andrii, eddyz87, memxor
From: Alexei Starovoitov <ast@kernel.org>
LLVM walks an array on the stack with a pointer:
r8 = fp - 32
1: r7 = *(u64 *)(r8 - 8)
..
r8 += 16
if .. goto 1b
After four offsets the liveness of the stack forgets them, and the load
can fill a register from any slot of the frame. So pointers that the
compiler spilled and nothing fills any more stay pointers, and states
with and without such a pointer stay apart. The number of these spills
depends on the allocation of registers. That is why wprof costs more
with 0 than with a variable.
Add one level to the lattice: offset V or more. It becomes unknown if the
pointer moves down. The main pass can widen a range below V, so the bound
limits only the fills. The bytes of the frame stay live.
A fill at or above the frame pointer reads no slot.
wprof_task_switch, zero / 0: 174164 / 241778 -> 127680 / 128858 insns.
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
---
kernel/bpf/liveness.c | 73 +++++++++++++++++++++++++++++--------------
1 file changed, 50 insertions(+), 23 deletions(-)
diff --git a/kernel/bpf/liveness.c b/kernel/bpf/liveness.c
index 93f8d1686496..628f6f2d407e 100644
--- a/kernel/bpf/liveness.c
+++ b/kernel/bpf/liveness.c
@@ -828,10 +828,12 @@ static int print_instances(struct bpf_verifier_env *env)
* Tracks which frame's FP a value is derived from
* and the byte offset from that frame's FP.
*
- * The .frame field forms a lattice with three levels of precision:
+ * The .frame field forms a lattice with four levels of precision:
*
* precise {frame=N, off=V} -- known absolute frame index and byte offset
* |
+ * lower bound {frame=N, off=V, cnt=-1}
+ * | -- V or more
* offset-imprecise {frame=N, cnt=0}
* | -- known frame identity, unknown offset
* fully-imprecise {frame=ARG_IMPRECISE, mask=bitmask}
@@ -841,12 +843,13 @@ static int print_instances(struct bpf_verifier_env *env)
*
* At CFG merge points, arg_track_join() moves down the lattice:
* - same frame + same offset -> precise
- * - same frame + different offset -> offset-imprecise
+ * - same frame + more than 4 offsets -> lower bound, offset-imprecise if it moves down
* - different frames -> fully-imprecise (bitmask OR)
*
* At memory access sites (LDX/STX/ST), offset-imprecise marks the known
* frame as fully read, while fully-imprecise iterates bits in the bitmask
- * and routes each frame to its target.
+ * and routes each frame to its target. A lower bound marks the whole frame too,
+ * but it fills registers from V and up only.
*/
#define MAX_ARG_OFFSETS 4
@@ -856,7 +859,7 @@ struct arg_track {
u64 mask; /* arg bitmask if ARG_IMPRECISE. As wide as off[] for memcmp() */
};
s8 frame; /* absolute frame index, or enum arg_track_state */
- s8 off_cnt; /* 0 = offset-imprecise, 1-4 = # of precise offsets */
+ s8 off_cnt; /* 0 = offset-imprecise, 1-4 = # of precise offsets, -1 = lower bound */
} __packed __aligned(2);
enum arg_track_state {
@@ -912,6 +915,8 @@ static void verbose_arg_track(struct bpf_verifier_env *env, struct arg_track *at
/* frame >= 0: absolute frame index */
if (at->off_cnt == 0) {
verbose(env, "fp%d ?", at->frame);
+ } else if (at->off_cnt < 0) {
+ verbose(env, "fp%d%+d..", at->frame, at->off[0]);
} else {
for (i = 0; i < at->off_cnt; i++) {
if (i)
@@ -935,7 +940,7 @@ static bool arg_track_eq(const struct arg_track *a, const struct arg_track *b)
return true;
if (a->off_cnt != b->off_cnt)
return false;
- for (i = 0; i < a->off_cnt; i++)
+ for (i = 0; i < abs(a->off_cnt); i++)
if (a->off[i] != b->off[i])
return false;
return true;
@@ -953,15 +958,22 @@ static struct arg_track arg_single(s8 arg, s16 off)
/*
* Merge two sorted offset arrays, deduplicate.
- * Returns off_cnt=0 if the result exceeds MAX_ARG_OFFSETS.
- * Both args must have the same frame and off_cnt > 0.
+ * Returns the lower bound if the result exceeds MAX_ARG_OFFSETS.
+ * Both args must have the same frame and off_cnt != 0. @a is the old value.
*/
static struct arg_track arg_merge_offsets(struct arg_track a, struct arg_track b)
{
struct arg_track result = { .frame = a.frame };
- struct arg_track imp = { .frame = a.frame };
+ struct arg_track lower = arg_single(a.frame, min(a.off[0], b.off[0]));
int i = 0, j = 0, k = 0;
+ lower.off_cnt = -1;
+ /* a pointer that moves up in a loop keeps its lowest offset */
+ if (a.off_cnt < 0)
+ return a.off[0] <= b.off[0] ? a : (struct arg_track){ .frame = a.frame };
+ if (b.off_cnt < 0)
+ return lower;
+
while (i < a.off_cnt && j < b.off_cnt) {
s16 v;
@@ -975,17 +987,17 @@ static struct arg_track arg_merge_offsets(struct arg_track a, struct arg_track b
if (k > 0 && result.off[k - 1] == v)
continue;
if (k >= MAX_ARG_OFFSETS)
- return imp;
+ return lower;
result.off[k++] = v;
}
while (i < a.off_cnt) {
if (k >= MAX_ARG_OFFSETS)
- return imp;
+ return lower;
result.off[k++] = a.off[i++];
}
while (j < b.off_cnt) {
if (k >= MAX_ARG_OFFSETS)
- return imp;
+ return lower;
result.off[k++] = b.off[j++];
}
result.off_cnt = k;
@@ -1039,7 +1051,7 @@ static struct arg_track __arg_track_join(struct arg_track a, struct arg_track b)
/* Both offset-imprecise: stay imprecise */
if (a.off_cnt == 0 || b.off_cnt == 0)
return (struct arg_track){ .frame = a.frame };
- /* Merge offset sets; falls back to off_cnt=0 if >4 */
+ /* Merge offset sets; falls back to a lower bound if >4 */
return arg_merge_offsets(a, b);
}
@@ -1168,7 +1180,7 @@ static void arg_padd(struct arg_track *at, s64 delta)
if (at->off_cnt == 0)
return;
- for (i = 0; i < at->off_cnt; i++) {
+ for (i = 0; i < abs(at->off_cnt); i++) {
s16 new_off;
if (arg_add(at->off[i], delta, &new_off)) {
@@ -1260,12 +1272,23 @@ static struct arg_track fill_from_stack(struct bpf_insn *insn,
cnt = at_out[reg].off_cnt;
if (cnt == 0)
return imp;
+ if (cnt < 0) {
+ i = at_out[reg].off[0] + insn->off;
+ if (i < -(int)(nslots * 8))
+ return imp;
+ for (i = DIV_ROUND_UP(-i, 8) - 1; i >= 0; i--)
+ result = __arg_track_join(result, at_stack_out[i]);
+ return result;
+ }
for (i = 0; i < cnt; i++) {
s16 fp_off, slot;
if (arg_add(at_out[reg].off[i], insn->off, &fp_off))
return imp;
+ /* out of the frame, see record_stack_access_off() */
+ if (fp_off >= 0)
+ continue;
slot = fp_off_to_slot(fp_off, nslots);
if (slot < 0)
return imp;
@@ -1296,7 +1319,7 @@ static void spill_to_stack(struct bpf_insn *insn, struct arg_track *at_out,
return;
}
cnt = at_out[reg].off_cnt;
- if (cnt == 0) {
+ if (cnt <= 0) {
for (int slot = 0; slot < nslots; slot++)
at_stack_out[slot] = __arg_track_join(at_stack_out[slot], new_val);
return;
@@ -1358,8 +1381,8 @@ static void clear_stack_for_all_offs(struct bpf_insn *insn,
return;
}
cnt = at_out[reg].off_cnt;
- if (cnt == 0) {
- clear_overlapping_stack_slots(at_stack_out, nslots, 0, sz, cnt);
+ if (cnt <= 0) {
+ clear_overlapping_stack_slots(at_stack_out, nslots, 0, sz, 0);
return;
}
for (i = 0; i < cnt; i++) {
@@ -1682,9 +1705,10 @@ static int record_stack_access(struct bpf_verifier_env *env,
if (!info.size)
return 0;
- if (arg->off_cnt == 0) {
+ if (arg->off_cnt <= 0) {
if (info.may_read) {
- err = mark_stack_read_all(env, instance, frame, insn_idx, !info.bytes_only);
+ err = mark_stack_read_all(env, instance, frame, insn_idx,
+ !info.bytes_only && !arg->off_cnt);
if (err)
return err;
}
@@ -1693,9 +1717,12 @@ static int record_stack_access(struct bpf_verifier_env *env,
if (err)
return err;
}
- return 0;
+ if (!arg->off_cnt)
+ return 0;
+ /* The main pass can widen the range below the bound. It finds bytes there. */
+ info.size = U32_MAX;
}
- for (i = 0; i < arg->off_cnt; i++) {
+ for (i = 0; i < abs(arg->off_cnt); i++) {
err = record_stack_access_off(instance, arg, i, info, frame, insn_idx);
if (err)
return err;
@@ -1793,10 +1820,10 @@ static int record_load_store_access(struct bpf_verifier_env *env,
}
/* Resolve offsets: fold insn->off into arg_track */
- if (ptr->off_cnt > 0) {
+ if (ptr->off_cnt) {
resolved.off_cnt = ptr->off_cnt;
resolved.frame = ptr->frame;
- for (oi = 0; oi < ptr->off_cnt; oi++) {
+ for (oi = 0; oi < abs(ptr->off_cnt); oi++) {
if (arg_add(ptr->off[oi], insn->off, &resolved.off[oi])) {
resolved.off_cnt = 0;
break;
@@ -1809,7 +1836,7 @@ static int record_load_store_access(struct bpf_verifier_env *env,
info.may_write = write;
info.must_write = write;
info.size = bpf_size_to_bytes(BPF_SIZE(insn->code));
- info.bytes_only = class == BPF_LDX && info.size < BPF_REG_SIZE && !ptr->off_cnt;
+ info.bytes_only = class == BPF_LDX && info.size < BPF_REG_SIZE && ptr->off_cnt <= 0;
if (ptr->frame >= 0 && ptr->frame <= depth)
return record_stack_access(env, instance, ptr, info, ptr->frame, insn_idx);
if (ptr->frame == ARG_IMPRECISE)
--
2.55.0
^ permalink raw reply related [flat|nested] 7+ messages in thread* Re: [PATCH bpf-next 1/4] bpf: Keep the lowest offset of a stack pointer that moves up
2026-12-03 19:58 ` [PATCH bpf-next 1/4] bpf: Keep the lowest offset of a stack pointer that moves up Alexei Starovoitov
@ 2026-12-03 20:31 ` sashiko-bot
0 siblings, 0 replies; 7+ messages in thread
From: sashiko-bot @ 2026-12-03 20:31 UTC (permalink / raw)
To: Alexei Starovoitov, bpf
Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] bpf: record_stack_access() and fill_from_stack() miss
narrowed fills below V
--
commit a30553fe2082bb3349a0b621d201a327d7a706aa
Author: Alexei Starovoitov <ast@kernel.org>
bpf: Keep the lowest offset of a stack pointer that moves up
This commit adds a lower-bound level (off_cnt = -1) to the arg_track lattice
in kernel/bpf/liveness.c so stack pointers that move up past four offsets
restrict register fills to slots at or above the lowest seen offset.
> diff --git a/kernel/bpf/liveness.c b/kernel/bpf/liveness.c
> index 93f8d16864965..628f6f2d407eb 100644
> --- a/kernel/bpf/liveness.c
> +++ b/kernel/bpf/liveness.c
[ ... ]
> @@ -1682,9 +1705,10 @@ static int record_stack_access(struct bpf_verifier_env *env,
>
> if (!info.size)
> return 0;
> - if (arg->off_cnt == 0) {
> + if (arg->off_cnt <= 0) {
> if (info.may_read) {
> - err = mark_stack_read_all(env, instance, frame, insn_idx, !info.bytes_only);
> + err = mark_stack_read_all(env, instance, frame, insn_idx,
> + !info.bytes_only && !arg->off_cnt);
> if (err)
> return err;
> }
> @@ -1693,9 +1717,12 @@ static int record_stack_access(struct bpf_verifier_env *env,
> if (err)
> return err;
> }
> - return 0;
> + if (!arg->off_cnt)
> + return 0;
> + /* The main pass can widen the range below the bound. It finds bytes there. */
> + info.size = U32_MAX;
> }
> - for (i = 0; i < arg->off_cnt; i++) {
> + for (i = 0; i < abs(arg->off_cnt); i++) {
> err = record_stack_access_off(instance, arg, i, info, frame, insn_idx);
> if (err)
> return err;
[Severity: Medium]
Can a widened stack pointer still perform a register fill below the lower
bound?
In a loop leftover, join_range() can widen a PTR_TO_STACK downward to
-BPF_MAX_VAR_OFF + 1, which stays within the comparison range of
is_branch_taken().
Comparing it for equality against another same-frame PTR_TO_STACK at a
constant offset below arg->off[0] lets is_scalar_branch_taken() ->
scalars_refine_cond_op() narrow var_off back to that constant, so
check_stack_read() dispatches to check_stack_read_fixed_off().
Because mark_stack_read_all() clears fill when arg->off_cnt < 0,
__clean_func_state() degrades a spilled pointer in that slot to STACK_MISC
under allow_ptr_leaks, causing a regression where valid programs fail
verification with -EACCES.
fill_from_stack() has the same issue for spilled PTR_TO_STACK values below
off[0]: skipping those slots leaves downstream accesses through the filled
pointer without FM_LIVE_BEFORE marks, so __clean_func_state() poisons the
target slot with STACK_POISON.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261203195823.4058490-1-alexei.starovoitov@gmail.com?part=1
^ permalink raw reply [flat|nested] 7+ messages in thread
* [PATCH bpf-next 2/4] bpf: Let bpf_dynptr_from_mem() take memory that nothing invalidates
2026-12-03 19:58 [PATCH bpf-next 0/4] bpf: Keep fewer states apart because of pointers Alexei Starovoitov
2026-12-03 19:58 ` [PATCH bpf-next 1/4] bpf: Keep the lowest offset of a stack pointer that moves up Alexei Starovoitov
@ 2026-12-03 19:58 ` Alexei Starovoitov
2026-12-03 19:58 ` [PATCH bpf-next 3/4] bpf: Join pointers to values of two maps Alexei Starovoitov
2026-12-03 19:58 ` [PATCH bpf-next 4/4] selftests/bpf: Test walks over the stack and joins of map values Alexei Starovoitov
3 siblings, 0 replies; 7+ messages in thread
From: Alexei Starovoitov @ 2026-12-03 19:58 UTC (permalink / raw)
To: bpf; +Cc: daniel, andrii, eddyz87, memxor
From: Alexei Starovoitov <ast@kernel.org>
__weak int parse(struct msg *m __arg_nonnull)
{
bpf_dynptr_from_mem(m, sizeof(*m), 0, &dptr);
The helper takes only map values, so it refuses 'm', also when the caller
passes a map value.
A dynptr gives the same access as the pointer. It must not outlive the
memory. Take mem that has no flags, no reference and no parent: no insn
invalidates such a pointer.
The next patch makes such mem of the values of two maps.
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
---
include/uapi/linux/bpf.h | 3 ++-
kernel/bpf/verifier.c | 5 ++++-
tools/include/uapi/linux/bpf.h | 3 ++-
3 files changed, 8 insertions(+), 3 deletions(-)
diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h
index 9f41c63f1831..4cc342c8163a 100644
--- a/include/uapi/linux/bpf.h
+++ b/include/uapi/linux/bpf.h
@@ -5870,7 +5870,8 @@ union bpf_attr {
* Description
* Get a dynptr to local memory *data*.
*
- * *data* must be a ptr to a map value.
+ * *data* must be a ptr to a map value or a memory argument of
+ * a global function.
* The maximum *size* supported is DYNPTR_MAX_SIZE.
* *flags* is currently unused.
* Return
diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c
index d4598b6cd440..7cdd1ba36805 100644
--- a/kernel/bpf/verifier.c
+++ b/kernel/bpf/verifier.c
@@ -13914,7 +13914,10 @@ static int check_helper_call(struct bpf_verifier_env *env, struct bpf_insn *insn
}
break;
case BPF_FUNC_dynptr_from_mem:
- if (regs[BPF_REG_1].type != PTR_TO_MAP_VALUE) {
+ /* no insn invalidates such memory while the dynptr is alive */
+ if (regs[BPF_REG_1].type != PTR_TO_MAP_VALUE &&
+ (regs[BPF_REG_1].type != PTR_TO_MEM ||
+ reg_lifetime_id(env, ®s[BPF_REG_1]))) {
verbose(env, "Unsupported reg type %s for bpf_dynptr_from_mem data\n",
reg_type_str(env, regs[BPF_REG_1].type));
return -EACCES;
diff --git a/tools/include/uapi/linux/bpf.h b/tools/include/uapi/linux/bpf.h
index 9f41c63f1831..4cc342c8163a 100644
--- a/tools/include/uapi/linux/bpf.h
+++ b/tools/include/uapi/linux/bpf.h
@@ -5870,7 +5870,8 @@ union bpf_attr {
* Description
* Get a dynptr to local memory *data*.
*
- * *data* must be a ptr to a map value.
+ * *data* must be a ptr to a map value or a memory argument of
+ * a global function.
* The maximum *size* supported is DYNPTR_MAX_SIZE.
* *flags* is currently unused.
* Return
--
2.55.0
^ permalink raw reply related [flat|nested] 7+ messages in thread* [PATCH bpf-next 3/4] bpf: Join pointers to values of two maps
2026-12-03 19:58 [PATCH bpf-next 0/4] bpf: Keep fewer states apart because of pointers Alexei Starovoitov
2026-12-03 19:58 ` [PATCH bpf-next 1/4] bpf: Keep the lowest offset of a stack pointer that moves up Alexei Starovoitov
2026-12-03 19:58 ` [PATCH bpf-next 2/4] bpf: Let bpf_dynptr_from_mem() take memory that nothing invalidates Alexei Starovoitov
@ 2026-12-03 19:58 ` Alexei Starovoitov
2026-12-03 20:42 ` bot+bpf-ci
2026-12-03 19:58 ` [PATCH bpf-next 4/4] selftests/bpf: Test walks over the stack and joins of map values Alexei Starovoitov
3 siblings, 1 reply; 7+ messages in thread
From: Alexei Starovoitov @ 2026-12-03 19:58 UTC (permalink / raw)
To: bpf; +Cc: daniel, andrii, eddyz87, memxor
From: Alexei Starovoitov <ast@kernel.org>
s = idle ? bpf_map_lookup_elem(&hash, &key) :
bpf_task_storage_get(&storage, task, ..);
The join of two objects is unreadable, so such states stay apart, and N
pointers give 2^N states.
A value with no special fields that the program can read and write is
only a number of bytes. So is mem that nothing invalidates. Join two
pointers to such memory to mem of the smaller size, and let this mem
subsume them. They need the same id: it tells which pointers have one
object. The value of an array with one element is known by its map, so
it is not joined. Objects of two sizes stay apart until the budget of
live types is used up. The masks against speculation need the map, so
the join needs bypass_spec_v1.
If an insn that reads such mem is unproven, the next pass keeps the maps
apart, as for an unreadable register.
Numbers that go with the map are joined now, as they are for one map.
wprof_task_switch: 128858 -> 94077 insns, Cilium: -4 %.
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
---
Documentation/bpf/verifier.rst | 7 +++---
include/linux/bpf_verifier.h | 13 ++++++++++++
kernel/bpf/join.c | 20 ++++++++++++++++-
kernel/bpf/precision.c | 23 +++++++++++++++++---
kernel/bpf/states.c | 39 +++++++++++++++++++++++++++++++++-
kernel/bpf/verifier.c | 8 +------
6 files changed, 95 insertions(+), 15 deletions(-)
diff --git a/Documentation/bpf/verifier.rst b/Documentation/bpf/verifier.rst
index 4351fe4b7010..dbe61497acc1 100644
--- a/Documentation/bpf/verifier.rst
+++ b/Documentation/bpf/verifier.rst
@@ -347,6 +347,7 @@ The *join* of two states has all values of both, see ``bpf_states_join()``::
buf + 0 | buf + 8 -> buf + 0..8
map_value | scalar -> unreadable
buf | other_buf -> unreadable
+ map_value | other map's -> mem, see bpf_plain_mem_size()
ctx + 0 | ctx + 8 -> unreadable, ctx has no range of offsets
If an insn reads the unreadable xreg, the insn is unproven, and the next pass
@@ -399,7 +400,7 @@ This code is common::
If the two states are joined, an error is almost sure, and it costs a pass. So
states with two types or two objects in a live xreg stay apart already in pass
0, see ``bpf_hash_live_types()``. The same holds for a packet pointer with and
-without checked bytes.
+without checked bytes. Values of two maps that have a join to mem are joined.
N such xregs can make 2^N states. So this rule has a budget. If a pass is over
it, the pass joins from there on, and so do the next passes. The states that the
@@ -1192,8 +1193,8 @@ calls this trace partitioning. More precision costs more. So
this list that adds something.
ADD_DEPS
- Add the deps of the unproven insns. If the insn reads an unreadable xreg,
- keep only the types apart first.
+ Add the deps of the unproven insns. If the insn reads an unreadable xreg
+ or joined mem, keep only the types apart first.
PEEL
Peel the suspects. If the last trip is known: all trips. Else one trip, then
diff --git a/include/linux/bpf_verifier.h b/include/linux/bpf_verifier.h
index acef0bd410b1..39a0868c66be 100644
--- a/include/linux/bpf_verifier.h
+++ b/include/linux/bpf_verifier.h
@@ -1964,6 +1964,7 @@ struct bpf_verifier_env {
bool faults_late; /* a site is not what it seemed: see check_arena_site() */
bool pad_releases; /* see release_frame_resources() */
bool calls_joined; /* see flow_may_add_instance() */
+ bool joined_mem; /* see join_plain_mem() */
bool extra_values; /* a state can have constants that no run has */
bool new_linear_xreg; /* see bpf_same_insns_unproven() */
/* from here on: of all main progs */
@@ -2323,6 +2324,18 @@ bool bpf_add_pkt_pair(struct bpf_verifier_env *env, const struct bpf_pkt_pair *p
bool bpf_state_subsumes(struct bpf_verifier_env *env, const struct bpf_verifier_state *old,
const struct bpf_verifier_state *cur);
bool bpf_scalar_subsumes(const struct bpf_reg_state *old, const struct bpf_reg_state *cur);
+
+/*
+ * A plain single-element array has one stable value address,
+ * but not when BPF_F_INNER_MAP is set, see arraymap.c:array_map_meta_equal().
+ */
+static inline bool bpf_map_has_one_value(const struct bpf_map *map)
+{
+ return map->map_type == BPF_MAP_TYPE_ARRAY && map->max_entries == 1 &&
+ !(map->map_flags & BPF_F_INNER_MAP);
+}
+
+u32 bpf_plain_mem_size(struct bpf_verifier_env *env, const struct bpf_reg_state *reg);
bool bpf_same_type_and_object(struct bpf_verifier_env *env, const struct bpf_reg_state *a,
const struct bpf_reg_state *b);
bool bpf_same_join_key(const struct bpf_verifier_state *a, const struct bpf_verifier_state *b);
diff --git a/kernel/bpf/join.c b/kernel/bpf/join.c
index 466693fa0b80..1383eb531567 100644
--- a/kernel/bpf/join.c
+++ b/kernel/bpf/join.c
@@ -9,6 +9,7 @@
* p+1, p+2, ... at header -> p+unknown -+
*
* Values of one type and object differ only in a number, see bpf_same_type_and_object().
+ * Two objects that are plain memory give mem, see join_plain_mem().
* Numbers are joined. At a loop header they are widened, so the sequence of states ends.
*
* bpf_clean_verifier_state() cleans the inputs: dead xregs are the same in both.
@@ -130,11 +131,28 @@ static void join_pkt(struct join *j, struct bpf_reg_state *dst, const struct bpf
dst->id = bpf_new_id(j->env);
}
+/* Two pointers to plain memory point to mem of the smaller size */
+static bool join_plain_mem(struct bpf_verifier_env *env, struct bpf_reg_state *dst,
+ const struct bpf_reg_state *src)
+{
+ u32 size = min(bpf_plain_mem_size(env, dst), bpf_plain_mem_size(env, src));
+
+ if (!size || type_flag(dst->type) != type_flag(src->type) || dst->id != src->id)
+ return false;
+ dst->type = PTR_TO_MEM | type_flag(dst->type);
+ memset(&dst->raw, 0, sizeof(dst->raw));
+ dst->mem_size = size;
+ dst->map_uid = 0;
+ env->joined_mem = true;
+ return true;
+}
+
static void join_reg(struct join *j, struct bpf_reg_state *dst, const struct bpf_reg_state *src)
{
bool from_unproven = dst->from_unproven || (src && src->from_unproven);
- if (!src || dst->type == NOT_INIT || !bpf_same_type_and_object(j->env, dst, src))
+ if (!src || dst->type == NOT_INIT ||
+ !(bpf_same_type_and_object(j->env, dst, src) || join_plain_mem(j->env, dst, src)))
bpf_mark_reg_not_init(j->env, dst);
else if (type_is_pkt_pointer(dst->type))
join_pkt(j, dst, src);
diff --git a/kernel/bpf/precision.c b/kernel/bpf/precision.c
index a5cc813817c4..afd04c774141 100644
--- a/kernel/bpf/precision.c
+++ b/kernel/bpf/precision.c
@@ -155,6 +155,8 @@ static u64 hash_type(const struct bpf_reg_state *reg, u64 hash)
* \ /
* if (r6 == 0) goto out; two states
*
+ * Plain memory of one size has a join, see join_plain_mem().
+ *
* N such xregs can give 1 << N states. If a pass is over the budget, it joins from there on,
* and so do the next passes. Then an error selects the xregs, see add_deps().
*/
@@ -167,6 +169,7 @@ u64 bpf_hash_live_types(struct bpf_verifier_env *env, const struct bpf_verifier_
unsigned long live[XREG_WORDS];
struct bpf_reg_state tmp;
u32 fr, xregno, bits;
+ u64 size;
if (env->path_sensitive)
return hash;
@@ -182,7 +185,15 @@ u64 bpf_hash_live_types(struct bpf_verifier_env *env, const struct bpf_verifier_
/* a slot with no spill hashes like a slot that is not allocated */
if (!reg || reg == &tmp || reg->type == SCALAR_VALUE)
continue;
- hash = hash_type(reg, hash + fr * MAX_BPF_XREG + xregno);
+ hash += fr * MAX_BPF_XREG + xregno;
+ size = bpf_plain_mem_size(env, reg);
+ if (size) {
+ size |= (u64)reg->id << 32;
+ hash = bpf_hash64(&size, sizeof(size),
+ hash + type_flag(reg->type));
+ continue;
+ }
+ hash = hash_type(reg, hash);
if (type_is_pkt_pointer(reg->type) && pkt_has_bytes_left(reg) &&
pkt_bytes_left(reg) > 0)
hash = ~hash;
@@ -611,6 +622,7 @@ void bpf_note_unproven_insn(struct bpf_verifier_env *env,
struct bpf_subprog_info *sub = bpf_find_containing_subprog(env, idx);
struct bpf_precision *prec = env->precision;
const struct loop_stack_entry *e;
+ const struct bpf_reg_state *reg;
u32 i, fr;
if (!sub)
@@ -638,11 +650,16 @@ void bpf_note_unproven_insn(struct bpf_verifier_env *env,
__set_bit(idx, prec->bad_value_insns);
if (!st->holds_too_much)
__set_bit(idx, prec->err_insns);
- for (i = BPF_REG_0; st->frame[0] && i < BPF_REG_FP; i++)
+ for (i = BPF_REG_0; st->frame[0] && i < BPF_REG_FP; i++) {
+ reg = &st->frame[st->curframe]->regs[i];
+ /* the insn can need what the join of two maps forgot */
if ((env->insn_aux_data[idx].use_regs & BIT(i)) &&
!test_bit(idx, prec->bad_read_insns_done) &&
- st->frame[st->curframe]->regs[i].type == NOT_INIT)
+ (reg->type == NOT_INIT ||
+ (env->joined_mem && base_type(reg->type) == PTR_TO_MEM &&
+ bpf_plain_mem_size(env, reg))))
__set_bit(idx, prec->this_pass.bad_read_insns);
+ }
}
/* The state that set the fixup is gone. The leftover of a loop possibly made it up. */
diff --git a/kernel/bpf/states.c b/kernel/bpf/states.c
index 9201e3cbc448..4a2dbe48b26d 100644
--- a/kernel/bpf/states.c
+++ b/kernel/bpf/states.c
@@ -299,6 +299,42 @@ bool bpf_same_type_and_object(struct bpf_verifier_env *env, const struct bpf_reg
return scalar_equal(scalar_of(a), scalar_of(b));
}
+/*
+ * The size of the object, if @reg points to plain memory: bytes that the program can read and
+ * write, with no special field, and that no insn invalidates. A join can forget the map of such
+ * a pointer. The id stays: it tells which pointers have one object. Else 0.
+ * The masks against speculation need the map.
+ */
+u32 bpf_plain_mem_size(struct bpf_verifier_env *env, const struct bpf_reg_state *reg)
+{
+ struct bpf_map *map = reg->map_ptr;
+
+ if (!env->bypass_spec_v1 || reg->parent_id || !bpf_id_is_xreg(reg->id))
+ return 0;
+ switch (reg->type & ~PTR_MAYBE_NULL) {
+ case PTR_TO_MEM:
+ return reg->mem_size;
+ case PTR_TO_MAP_VALUE:
+ if (!IS_ERR_OR_NULL(map->record) || map->map_type == BPF_MAP_TYPE_INSN_ARRAY ||
+ bpf_map_has_one_value(map) ||
+ (map->map_flags & (BPF_F_RDONLY_PROG | BPF_F_WRONLY_PROG)))
+ return 0;
+ return map->value_size;
+ default:
+ return 0;
+ }
+}
+
+/* See join_plain_mem() */
+static bool plain_mem_subsumes(struct bpf_verifier_env *env, const struct bpf_reg_state *old,
+ const struct bpf_reg_state *cur)
+{
+ u32 size = bpf_plain_mem_size(env, old);
+
+ return size && size <= bpf_plain_mem_size(env, cur) && old->id == cur->id &&
+ old->type == (PTR_TO_MEM | type_flag(cur->type));
+}
+
/* Returns true if (rold safe implies rcur safe) */
static bool regsafe(struct bpf_verifier_env *env, const struct bpf_reg_state *rold,
const struct bpf_reg_state *rcur)
@@ -307,7 +343,8 @@ static bool regsafe(struct bpf_verifier_env *env, const struct bpf_reg_state *ro
if (rold->type == NOT_INIT)
/* nothing can be done with it */
return true;
- if (!rcur || !bpf_same_type_and_object(env, rold, rcur))
+ if (!rcur || !(bpf_same_type_and_object(env, rold, rcur) ||
+ plain_mem_subsumes(env, rold, rcur)))
return false;
/* The offset is not tracked. */
if (base_type(rold->type) == PTR_TO_ARENA)
diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c
index 7cdd1ba36805..832f2efcbce4 100644
--- a/kernel/bpf/verifier.c
+++ b/kernel/bpf/verifier.c
@@ -18916,13 +18916,7 @@ bool bpf_same_memory_origin(const struct bpf_reg_state *reg_a,
return false;
if (reg_a->id && reg_a->id == reg_b->id)
return true;
- /*
- * A plain single-element array has one stable value address,
- * but not when BPF_F_INNER_MAP is set, see arraymap.c:array_map_meta_equal().
- */
- if (reg_a->map_ptr->map_type == BPF_MAP_TYPE_ARRAY &&
- reg_a->map_ptr->max_entries == 1 &&
- !(reg_a->map_ptr->map_flags & BPF_F_INNER_MAP))
+ if (bpf_map_has_one_value(reg_a->map_ptr))
return true;
/*
* The rules above can be simplified / relaxed if:
--
2.55.0
^ permalink raw reply related [flat|nested] 7+ messages in thread* Re: [PATCH bpf-next 3/4] bpf: Join pointers to values of two maps
2026-12-03 19:58 ` [PATCH bpf-next 3/4] bpf: Join pointers to values of two maps Alexei Starovoitov
@ 2026-12-03 20:42 ` bot+bpf-ci
0 siblings, 0 replies; 7+ messages in thread
From: bot+bpf-ci @ 2026-12-03 20:42 UTC (permalink / raw)
To: alexei.starovoitov, bpf
Cc: daniel, andrii, eddyz87, memxor, ast, andrii, daniel, martin.lau,
eddyz87, yonghong.song, mason, ihor.solodrai
[-- Attachment #1: Type: text/plain, Size: 1899 bytes --]
> bpf: Join pointers to values of two maps
TL;DR: Documentation nit only: the new verifier.rst sentence says values of
two maps are joined, but it may be inaccurate, since values of different sizes
stay apart until the live-types budget is used up.
> diff --git a/Documentation/bpf/verifier.rst b/Documentation/bpf/verifier.rst
> --- a/Documentation/bpf/verifier.rst
> +++ b/Documentation/bpf/verifier.rst
[ ... ]
> @@ -399,7 +400,7 @@ This code is common::
> If the two states are joined, an error is almost sure, and it costs a pass. So
> states with two types or two objects in a live xreg stay apart already in pass
> 0, see ``bpf_hash_live_types()``. The same holds for a packet pointer with and
> -without checked bytes.
> +without checked bytes. Values of two maps that have a join to mem are joined.
Is this sentence accurate for values of different sizes?
bpf_hash_live_types() puts the size into the hash for plain memory:
size = bpf_plain_mem_size(env, reg);
if (size) {
size |= (u64)reg->id << 32;
hash = bpf_hash64(&size, sizeof(size), hash + type_flag(reg->type));
continue;
}
So two values of different sizes (say 2048 and 8 bytes) get different
hashes and stay apart in pass 0 until live_types_over is set, even though
join_plain_mem() would join them to the smaller size. The commit message
and the comment in precision.c both say this ("Objects of two sizes stay
apart until the budget of live types is used up"), but the sentence here
leaves out the size condition.
Would something like "Values of two maps of one size are joined to mem;
values of two sizes stay apart until the budget is used up." be more
accurate?
---
AI reviewed your patch. Please fix the bug or email reply why it's not a bug.
See: https://github.com/kernel-patches/vmtest/blob/master/ci/claude/README.md
CI run summary: https://github.com/kernel-patches/bpf/actions/runs/47192680468
^ permalink raw reply [flat|nested] 7+ messages in thread
* [PATCH bpf-next 4/4] selftests/bpf: Test walks over the stack and joins of map values
2026-12-03 19:58 [PATCH bpf-next 0/4] bpf: Keep fewer states apart because of pointers Alexei Starovoitov
` (2 preceding siblings ...)
2026-12-03 19:58 ` [PATCH bpf-next 3/4] bpf: Join pointers to values of two maps Alexei Starovoitov
@ 2026-12-03 19:58 ` Alexei Starovoitov
3 siblings, 0 replies; 7+ messages in thread
From: Alexei Starovoitov @ 2026-12-03 19:58 UTC (permalink / raw)
To: bpf; +Cc: daniel, andrii, eddyz87, memxor
From: Alexei Starovoitov <ast@kernel.org>
Check that a pointer that moves up does not fill from the slots below it,
that a widened range can still read and write there, and that values of
two maps are joined only if both are plain and have one id.
Make a dynptr of the argument of a global function.
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
---
.../testing/selftests/bpf/progs/dynptr_fail.c | 19 ++
.../selftests/bpf/progs/verifier_partition.c | 248 +++++++++++++++++-
2 files changed, 266 insertions(+), 1 deletion(-)
diff --git a/tools/testing/selftests/bpf/progs/dynptr_fail.c b/tools/testing/selftests/bpf/progs/dynptr_fail.c
index 232886dcfc57..186be986d412 100644
--- a/tools/testing/selftests/bpf/progs/dynptr_fail.c
+++ b/tools/testing/selftests/bpf/progs/dynptr_fail.c
@@ -708,6 +708,25 @@ int dynptr_from_mem_invalid_api(void *ctx)
return 0;
}
+__weak int write_through_dynptr(long *p __arg_nonnull)
+{
+ struct bpf_dynptr ptr;
+ long v = 42;
+
+ bpf_dynptr_from_mem(p, sizeof(*p), 0, &ptr);
+ return bpf_dynptr_write(&ptr, 0, &v, sizeof(v), 0);
+}
+
+SEC("?syscall")
+__success __retval(42)
+int dynptr_from_global_func_arg(void *ctx)
+{
+ long x = 0;
+
+ write_through_dynptr(&x);
+ return x;
+}
+
/* Cannot create dynptr from dynptr data */
SEC("?raw_tp")
__failure __msg("Unsupported reg type mem for bpf_dynptr_from_mem data")
diff --git a/tools/testing/selftests/bpf/progs/verifier_partition.c b/tools/testing/selftests/bpf/progs/verifier_partition.c
index dec2cd3dd95a..456684bdb0d9 100644
--- a/tools/testing/selftests/bpf/progs/verifier_partition.c
+++ b/tools/testing/selftests/bpf/progs/verifier_partition.c
@@ -28,7 +28,8 @@ char small[14] SEC(".data.small");
__imm_addr(map2k), __imm(bpf_get_prandom_u32), \
__imm(bpf_map_lookup_elem), __imm(bpf_loop), __imm_addr(ringbuf), \
__imm(bpf_ringbuf_reserve), __imm(bpf_ringbuf_discard), \
- __imm(bpf_get_current_comm), __imm_addr(small) : __clobber_all)
+ __imm(bpf_get_current_comm), __imm_addr(small), \
+ __imm(bpf_ringbuf_output) : __clobber_all)
/* r9 = value of map2k */
#define VALUE_IN_R9 \
@@ -1307,6 +1308,92 @@ __naked void atomic_through_ptr(void)
ASM(ONE_OF_TWO_PTRS("r9", "r9") "r2 = 1; lock *(u64 *)(r1 + 8) += r2;" EXIT_AT_9);
}
+/* r6 goes around in fp-264..fp-8. The widened range has fp-272, which nothing reads. */
+SEC("kprobe")
+__description("widen: the cursor of a ring has a dead slot in its range")
+__success INSNS(4)
+__naked void ring_above_dead_slot(void)
+{
+ ASM(VALUE_IN_R9
+ "*(u64 *)(r10 - 272) = 1;"
+ "r1 = r10; r1 += -264; r2 = 256; call %[bpf_get_current_comm];"
+ "r7 = 0; r8 = 0; r6 = r10; r6 += -136;"
+"1: r1 = *(u32 *)(r6 + 0); r8 += r1; r6 += 4;"
+ "r1 = r10; r1 += -8;"
+ "if r6 < r1 goto +2; r6 = r10; r6 += -264;"
+ "r0 = *(u8 *)(r9 + 18);"
+ "if r0 != 1 goto +2; r6 = r10; r6 += -256;"
+ "if r7 != 1000 goto +2; r6 = r10; r6 += -264;"
+ "r7 += 1;"
+ "r0 = *(u8 *)(r9 + 19);"
+ "if r0 != 0 goto 1b;"
+ "*(u64 *)(r9 + 48) = r8;"
+ EXIT_AT_9);
+}
+
+SEC("tc")
+__description("widen: the cursor of a ring stores above a spilled pointer")
+__success
+__naked void ring_above_spill(void)
+{
+ ASM(VALUE_IN_R9
+ "*(u64 *)(r10 - 72) = r9;"
+ "r7 = r10; r7 += -64;"
+ "r1 = *(u64 *)(r9 + 8);"
+ "if r1 == 0 goto 2f;"
+ "r8 = r10; r6 = r10; r6 += -16;"
+ "goto 3f;"
+"1: r1 = *(u64 *)(r9 + 8);"
+ "if r1 != 0 goto 3f;"
+"2: r1 = %[ringbuf] ll; r2 = r7; r3 = 64; r4 = 0; call %[bpf_ringbuf_output];"
+ "r1 = *(u64 *)(r10 - 72);"
+ "r0 = *(u8 *)(r1 + 0);"
+ "exit;"
+"3: *(u64 *)(r6 + 0) = r1; r6 += 8;"
+ "if r6 < r8 goto 1b;"
+ "r6 = r7;"
+ "goto 1b;"
+ EXIT_AT_9);
+}
+
+/* r6 goes over the words from @first in steps of @step. fp-40 is set on one path only. */
+#define WALK_STACK(first, step) \
+ VALUE_IN_R9 \
+ "*(u64 *)(r10 - 16) = r9; *(u64 *)(r10 - 24) = r9; *(u64 *)(r10 - 32) = r9;" \
+ RANDOM_BIT \
+ "if r0 == 0 goto +1;" \
+ "*(u64 *)(r10 - 40) = r9;" \
+ "r6 = r10; r6 += " #first "; r7 = 0;" \
+"1: r1 = *(u64 *)(r6 + 0);" \
+ "r0 = *(u8 *)(r1 + 0);" \
+ "r6 += " #step "; r7 += 1;" \
+ "if r7 < 3 goto 1b;" \
+ EXIT_AT_9
+
+SEC("tc")
+__description("fork: a pointer that moves up does not fill from the slots below")
+__success __log_level(2) __msg("from 15 to 16: pruned")
+__naked void walk_stack_up(void)
+{
+ ASM(WALK_STACK(-32, 8));
+}
+
+SEC("tc")
+__description("fork: a pointer that moves down")
+__success
+__naked void walk_stack_down(void)
+{
+ ASM(WALK_STACK(-16, -8));
+}
+
+SEC("tc")
+__description("fork: a pointer that moves down to a slot that is not set")
+__failure __msg("R1 invalid mem access 'scalar'")
+__naked void walk_stack_down_bad(void)
+{
+ ASM(WALK_STACK(-24, -8));
+}
+
char large[64] SEC(".data.large");
#define SUM_BY_BPF_FOR(off) \
@@ -1335,6 +1422,165 @@ int index_from_bpf_for_bad(void *ctx)
SUM_BY_BPF_FOR(14);
}
+#define HASH(name, flags, value_type) \
+struct { \
+ __uint(type, BPF_MAP_TYPE_HASH); \
+ __uint(map_flags, flags); \
+ __uint(max_entries, 2); \
+ __type(key, int); \
+ __type(value, value_type); \
+} name SEC(".maps")
+
+struct locked2k {
+ struct bpf_spin_lock lock;
+ char b[2044];
+};
+
+HASH(first2k, 0, struct bytes2k);
+HASH(hash2k, 0, struct bytes2k);
+HASH(rdonly2k, BPF_F_RDONLY_PROG, struct bytes2k);
+HASH(locked2k, 0, struct locked2k);
+HASH(hash8, 0, long);
+
+#define LOOKUP(map) "r1 = %[" #map "] ll; r2 = r10; r2 += -8; call %[bpf_map_lookup_elem];"
+#define RANDOM_KEY "call %[bpf_get_prandom_u32]; *(u64 *)(r10 - 8) = r0;"
+
+#define MEM_ASM(map, ...) \
+ asm volatile (__VA_ARGS__ EXIT_AT_9 \
+ :: __imm_addr(first2k), __imm_addr(hash2k), [other]"i"(&map), \
+ __imm_addr(map2k), \
+ __imm(bpf_get_prandom_u32), __imm(bpf_map_lookup_elem), \
+ __imm(bpf_dynptr_from_mem) : __clobber_all)
+
+/* r0 = value of first2k or of @map */
+#define FIRST_OR(map, ...) \
+ MEM_ASM(map, RANDOM_KEY \
+ "if r0 > 9 goto 1f;" LOOKUP(first2k) "goto 2f;" \
+"1:" LOOKUP(other) \
+"2: if r0 == 0 goto 9f;" __VA_ARGS__)
+
+SEC("tc")
+__description("mem: values of two maps are joined")
+__success __log_level(2) __msg("R0=mem(id={{[0-9]+}},sz=2048)") __not_msg("pass 1")
+__naked void two_maps(void)
+{
+ FIRST_OR(hash2k, "*(u8 *)(r0 + 2047) = 0;");
+}
+
+SEC("tc")
+__description("mem: each trip has a new value")
+__success __log_level(2) __msg("R0=mem(id={{[0-9]+}},sz=2048)") __not_msg("pass 1")
+__naked void two_maps_loop(void)
+{
+ MEM_ASM(hash2k, RANDOM_KEY LOOKUP(first2k)
+"1: if r0 == 0 goto 9f; *(u8 *)(r0 + 2047) = 0;" LOOKUP(other) MAY_GOTO(1) "goto 1b;");
+}
+
+SEC("tc")
+__description("mem: out of both values")
+__failure __msg("value_size=2048 off=2048 size=1")
+__naked void two_maps_bad(void)
+{
+ FIRST_OR(hash2k, "*(u8 *)(r0 + 2048) = 0;");
+}
+
+SEC("tc")
+__description("mem: dynptr to a value of one of two maps")
+__success __log_level(2) __msg("R0=mem(id={{[0-9]+}},sz=2048)") __not_msg("pass 1")
+__naked void two_maps_dynptr(void)
+{
+ FIRST_OR(hash2k, "r1 = r0; r2 = 2048; r3 = 0; r4 = r10; r4 += -24;"
+ "call %[bpf_dynptr_from_mem];");
+}
+
+/* Only the maps are kept apart, not the 512 pairs of numbers */
+SEC("tc")
+__description("mem: the offset goes with the map")
+__success INSNS(3)
+__naked void offset_goes_with_map(void)
+{
+ MEM_ASM(hash2k, RANDOM_KEY
+ "if r0 > 9 goto 1f;" LOOKUP(first2k) "r6 = 0; r7 = 1000; goto 2f;"
+"1:" LOOKUP(other) "r6 = 1000; r7 = 0;"
+"2: if r0 == 0 goto 9f;"
+ ".irp n, 1, 2, 4, 8, 16, 32, 64, 128, 256;"
+ "r3 = *(u8 *)(r0 + \\n);"
+ "if r3 == 0 goto +2; r6 += \\n; goto +1; r7 += \\n;"
+ ".endr;"
+ "r0 += r6; r0 += r7; *(u8 *)(r0 + 0) = 0;");
+}
+
+struct {
+ __uint(type, BPF_MAP_TYPE_ARRAY);
+ __uint(max_entries, 1);
+ __type(key, int);
+ __type(value, struct bytes2k);
+} array2k SEC(".maps");
+
+SEC("tc")
+__description("mem: the map tells that two pointers have one object")
+__success __log_level(2) __not_msg("pass 1")
+__naked void two_maps_one_value(void)
+{
+ MEM_ASM(array2k, RANDOM_KEY
+ "if r0 > 9 goto 1f;" LOOKUP(map2k) "r6 = r0;" LOOKUP(map2k) "goto 2f;"
+"1:" LOOKUP(other) "r6 = r0;" LOOKUP(other)
+"2: if r0 == 0 goto 9f; if r6 == 0 goto 9f;"
+ "r6 += 2048;"
+"3: *(u8 *)(r0 + 0) = 0; r0 += 1;"
+ "if r0 < r6 goto 3b;");
+}
+
+SEC("tc")
+__description("mem: one value is read-only")
+__failure __msg("write into map forbidden")
+__naked void two_maps_rdonly(void)
+{
+ FIRST_OR(rdonly2k, "*(u8 *)(r0 + 0) = 0;");
+}
+
+SEC("tc")
+__description("mem: one value has a lock")
+__failure __msg("bpf_spin_lock cannot be accessed directly by load/store")
+__naked void two_maps_lock(void)
+{
+ FIRST_OR(locked2k, "*(u8 *)(r0 + 0) = 0;");
+}
+
+/* 1 << 40 states are over the budget of live types. From there on all states meet. */
+#define OVER_BUDGET \
+ "r6 = r1; r8 = r10;" \
+ ".rept 40;" \
+ "call %[bpf_get_prandom_u32];" \
+ "r8 += -8; *(u64 *)(r8 - 8) = 0;" \
+ "if r0 == 0 goto +1; *(u64 *)(r8 - 8) = r6;" \
+ ".endr;" \
+ ".rept 40; r1 = *(u64 *)(r8 - 8); r8 += 8; .endr;"
+
+SEC("tc")
+__description("mem: the small value comes after the join of two large ones")
+__failure __msg("value_size=8 off=8 size=1")
+__naked void two_sizes_bad(void)
+{
+ MEM_ASM(hash8, OVER_BUDGET RANDOM_KEY
+ "if r0 > 9 goto 1f;" LOOKUP(other) "goto 3f;"
+"1: if r0 > 99 goto 2f;" LOOKUP(first2k) "goto 3f;"
+"2:" LOOKUP(hash2k)
+"3: if r0 == 0 goto 9f; *(u8 *)(r0 + 8) = 0;");
+}
+
+SEC("tc")
+__description("mem: two objects come after the join of two pointers to one object")
+__failure __msg("R0 invalid mem access 'map_value_or_null'")
+__naked void one_object_or_two(void)
+{
+ MEM_ASM(hash2k, OVER_BUDGET RANDOM_KEY
+ "if r0 > 9 goto 1f;" LOOKUP(first2k) "r6 = r0;" LOOKUP(first2k) "goto 3f;"
+"1: if r0 > 99 goto 2f;" LOOKUP(first2k) "r6 = r0; goto 3f;"
+"2:" LOOKUP(hash2k) "r6 = r0;"
+"3: if r6 == 0 goto 9f; *(u8 *)(r0 + 0) = 0;");
+}
+
static __naked __noinline __used void below_2k(void)
{
asm volatile ("r0 = 0; if r1 >= 2048 goto +1; r0 = 1; exit;");
--
2.55.0
^ permalink raw reply related [flat|nested] 7+ messages in thread