Add programs to exceptions_cleanup that keep their resources only in registers when they throw: - three records, two frames up from the throw: one in a register that the callee saves and overwrites, one in a register that no frame touches, and one of the callee's in a register that the throwing frame does not save; - a record in R3 in the frame that throws; - a popped list node, a pointer into its object, in R6, or NULL. Check in verifier_frame_desc that the verifier reports a site descriptor and no slot for a record in R6 or R0 at the throw, and for a record in R6 in the caller of a subprog that may throw. A record in R2 there takes a slot. Also check that a register that is not read again is still found at the throw when two paths join before it. Signed-off-by: Kumar Kartikeya Dwivedi --- .../bpf/prog_tests/exceptions_cleanup.c | 5 + .../selftests/bpf/progs/exceptions_cleanup.c | 164 ++++++++++++++++++ .../selftests/bpf/progs/verifier_frame_desc.c | 155 +++++++++++++++++ 3 files changed, 324 insertions(+) diff --git a/tools/testing/selftests/bpf/prog_tests/exceptions_cleanup.c b/tools/testing/selftests/bpf/prog_tests/exceptions_cleanup.c index 63909280e349..a88cbdafdf77 100644 --- a/tools/testing/selftests/bpf/prog_tests/exceptions_cleanup.c +++ b/tools/testing/selftests/bpf/prog_tests/exceptions_cleanup.c @@ -227,6 +227,11 @@ static void run_suite(const char *prefix) THROW(throw_callee_left_rcu, 1, "0"); RUN_AS(throw_rcu_some_paths, "throw_rcu_some_paths_in", THROWN, 0, "s", .param = 1); RUN_AS(throw_rcu_some_paths, "throw_rcu_some_paths_out", THROWN, 0, "", .param = 0); + /* What the frames keep in registers at the throw is released from them. */ + THROW(throw_regs_callers, 3, "201"); + THROW(throw_reg_caller_saved, 1, "0"); + RUN_AS(throw_reg_node, "throw_reg_node_popped", THROWN, 1, "0", .param = 0); + RUN_AS(throw_reg_node, "throw_reg_node_empty", THROWN, 0, "", .param = 1); /* A file and the reader of its dynptr, which goes first. */ snprintf(name, sizeof(name), "%sthrow_file_dynptr_held", prefix); if (test__start_subtest(name)) diff --git a/tools/testing/selftests/bpf/progs/exceptions_cleanup.c b/tools/testing/selftests/bpf/progs/exceptions_cleanup.c index addcc509c6c2..10397d157a48 100644 --- a/tools/testing/selftests/bpf/progs/exceptions_cleanup.c +++ b/tools/testing/selftests/bpf/progs/exceptions_cleanup.c @@ -1399,4 +1399,168 @@ __naked int throw_rcu_some_paths(void) : __clobber_all); } +/* ---- what the frames keep in registers at the throw ---- */ + +/* Throws, and uses no register but R1. */ +__used __naked static __noinline int throw_leaf(void) +{ + asm volatile ( + "r1 = 0xeB9F;" + "call %[bpf_throw];" + "exit;" + : + : __imm(bpf_throw) + : __clobber_all); +} + +/* + * Holds a record of its own in R7 and overwrites R6, which its prologue has + * to save for its caller then, before it calls the subprog that throws. + */ +__used __naked static __noinline int throw_mid(void) +{ + asm volatile ( + "r1 = %[ringbuf] ll;" + "r2 = 8;" + "r3 = 0;" + "call %[bpf_ringbuf_reserve];" + "if r0 == 0 goto 1f;" + "r7 = r0;" + "r1 = %[acquired] ll;" + "*(u64 *)(r1 + 16) = r0;" + "r2 = 3;" + "r1 = %[acquired_cnt] ll;" + "*(u32 *)(r1 + 0) = r2;" + "r6 = 42;" + "call throw_leaf;" + "r1 = r7;" + "r2 = 0;" + "call %[bpf_ringbuf_discard];" + "1: r0 = 0;" + "exit;" + : + : __imm_addr(ringbuf), + __imm_addr(acquired), + __imm_addr(acquired_cnt), + __imm(bpf_ringbuf_reserve), + __imm(bpf_ringbuf_discard) + : __clobber_all); +} + +/* + * Three records in registers only, two frames up from the throw. The main + * program has one in R6, which its callee saves and overwrites, and one in + * R8, which no frame touches. The callee has one in R7, which the frame that + * throws does not save. The unwinder has the registers of the throw, and + * those of each caller from where the frame it called saved them: the frame + * that throws holds nothing, its caller's record goes first, then the two of + * the main program in the order it acquired them. + */ +SEC("tc") +__naked int throw_regs_callers(void) +{ + asm volatile ( + "r1 = %[ringbuf] ll;" + "r2 = 8;" + "r3 = 0;" + "call %[bpf_ringbuf_reserve];" + "if r0 == 0 goto 2f;" + "r6 = r0;" + "r1 = %[ringbuf] ll;" + "r2 = 8;" + "r3 = 0;" + "call %[bpf_ringbuf_reserve];" + "if r0 == 0 goto 1f;" + "r8 = r0;" + "r1 = %[acquired] ll;" + "*(u64 *)(r1 + 0) = r6;" + "*(u64 *)(r1 + 8) = r8;" + "r2 = 2;" + "r1 = %[acquired_cnt] ll;" + "*(u32 *)(r1 + 0) = r2;" + "call throw_mid;" + "r1 = r8;" + "r2 = 0;" + "call %[bpf_ringbuf_discard];" + "1: r1 = r6;" + "r2 = 0;" + "call %[bpf_ringbuf_discard];" + "2: r0 = 0;" + "exit;" + : + : __imm_addr(ringbuf), + __imm_addr(acquired), + __imm_addr(acquired_cnt), + __imm(bpf_ringbuf_reserve), + __imm(bpf_ringbuf_discard) + : __clobber_all); +} + +/* + * The record is in R3 only when the frame throws, a register no call + * preserves: the unwinder has every register of the frame that throws. + */ +SEC("tc") +__naked int throw_reg_caller_saved(void) +{ + asm volatile ( + "r1 = %[ringbuf] ll;" + "r2 = 8;" + "r3 = 0;" + "call %[bpf_ringbuf_reserve];" + "if r0 == 0 goto 1f;" + "r3 = r0;" + "r1 = %[acquired] ll;" + "*(u64 *)(r1 + 0) = r0;" + "r2 = 1;" + "r1 = %[acquired_cnt] ll;" + "*(u32 *)(r1 + 0) = r2;" + "r0 = 0;" + "r1 = 0xeB9F;" + "call %[bpf_throw];" + "1: r0 = 0;" + "exit;" + : + : __imm_addr(ringbuf), + __imm_addr(acquired), + __imm_addr(acquired_cnt), + __imm(bpf_ringbuf_reserve), + __imm(bpf_throw) + : __clobber_all); +} + +/* + * The node that was popped is in R6 at the throw, a pointer 8 bytes into its + * object or NULL when the list was empty: the unwinder releases the object + * the register points into, and nothing when it holds NULL. + */ +SEC("tc") +__naked int throw_reg_node(void) +{ + asm volatile ( + "r1 = %[param] ll;" + "r1 = *(u32 *)(r1 + 0);" + "if r1 != 0 goto 1f;" + "call push_one;" + "1: r1 = %[pop_lock] ll;" + "call %[bpf_spin_lock];" + "r1 = %[pop_head] ll;" + "call %[bpf_list_pop_front];" + "r6 = r0;" + "r1 = %[pop_lock] ll;" + "call %[bpf_spin_unlock];" + "r1 = 0xeB9F;" + "call %[bpf_throw];" + "exit;" + : + : __imm_addr(param), + __imm_addr(pop_lock), + __imm_addr(pop_head), + __imm(bpf_spin_lock), + __imm(bpf_spin_unlock), + __imm(bpf_list_pop_front), + __imm(bpf_throw) + : __clobber_all); +} + char _license[] SEC("license") = "GPL"; diff --git a/tools/testing/selftests/bpf/progs/verifier_frame_desc.c b/tools/testing/selftests/bpf/progs/verifier_frame_desc.c index 00a6dce52dfb..5f2425194b1a 100644 --- a/tools/testing/selftests/bpf/progs/verifier_frame_desc.c +++ b/tools/testing/selftests/bpf/progs/verifier_frame_desc.c @@ -299,6 +299,161 @@ __naked int dead_spill_at_call(void) : __clobber_all); } +/* + * The record is in R6 when the frame throws: the descriptor of the throw + * names the register. + */ +SEC("tc") +__description("frame_desc: a record in R6 at the throw has no slot") +__arch_x86_64 __arch_arm64 +__success __log_level(5) +__msg("frame descriptors: slots 0 insns 0 sites 1 classes 1") +__naked int record_in_r6_at_throw(void) +{ + asm volatile ( + "r1 = %[ringbuf] ll;" + "r2 = 8;" + "r3 = 0;" + "call %[bpf_ringbuf_reserve];" + "if r0 == 0 goto 1f;" + "r6 = r0;" + "r0 = 0;" + "r1 = 0xeB9F;" + "call %[bpf_throw];" + "1: r0 = 0;" + "exit;" + : + : __imm_addr(ringbuf), + __imm(bpf_ringbuf_reserve), + __imm(bpf_throw) + : __clobber_all); +} + +/* + * The record is in R0 only, where the call that reserved it left it: the + * unwinder has every register of the frame that throws. + */ +SEC("tc") +__description("frame_desc: a record in R0 at the throw has no slot") +__arch_x86_64 __arch_arm64 +__success __log_level(5) +__msg("frame descriptors: slots 0 insns 0 sites 1 classes 1") +__naked int record_in_r0_at_throw(void) +{ + asm volatile ( + "r1 = %[ringbuf] ll;" + "r2 = 8;" + "r3 = 0;" + "call %[bpf_ringbuf_reserve];" + "if r0 == 0 goto 1f;" + "r1 = 0xeB9F;" + "call %[bpf_throw];" + "1: r0 = 0;" + "exit;" + : + : __imm_addr(ringbuf), + __imm(bpf_ringbuf_reserve), + __imm(bpf_throw) + : __clobber_all); +} + +/* + * The record is in R6 in the caller of the subprog that may throw: R6 is + * callee-saved, so the unwinder has it for a frame in a call, and the + * descriptor of the call names it. + */ +SEC("tc") +__description("frame_desc: a record in R6 in the caller of a throw has no slot") +__arch_x86_64 __arch_arm64 +__success __log_level(5) +__msg("frame descriptors: slots 0 insns 0 sites 1 classes 1") +__naked int record_in_r6_in_caller(void) +{ + asm volatile ( + "r1 = %[ringbuf] ll;" + "r2 = 8;" + "r3 = 0;" + "call %[bpf_ringbuf_reserve];" + "if r0 == 0 goto 1f;" + "r6 = r0;" + "call throw_maybe;" + "r1 = r6;" + "r2 = 0;" + "call %[bpf_ringbuf_discard];" + "1: r0 = 0;" + "exit;" + : + : __imm_addr(ringbuf), + __imm(bpf_ringbuf_reserve), + __imm(bpf_ringbuf_discard) + : __clobber_all); +} + +/* + * The record is in R2 only in the caller of the subprog that throws, a + * register the call does not preserve: the unwinder has it for the frame that + * throws only, so the record is kept in a slot. + */ +SEC("tc") +__description("frame_desc: a record in R2 in the caller of a throw keeps its slot") +__arch_x86_64 __arch_arm64 +__success __log_level(5) +__msg("frame descriptors: slots 1 insns 2 sites 0 classes 1") +__naked int record_in_r2_in_caller(void) +{ + asm volatile ( + "r1 = %[ringbuf] ll;" + "r2 = 8;" + "r3 = 0;" + "call %[bpf_ringbuf_reserve];" + "if r0 == 0 goto 1f;" + "r2 = r0;" + "r0 = 0;" + "call throw_unconditionally;" + "1: r0 = 0;" + "exit;" + : + : __imm_addr(ringbuf), + __imm(bpf_ringbuf_reserve) + : __clobber_all); +} + +/* + * The record is in R6, which nothing reads again, and two paths join in front + * of the throw. The verifier forgets what dead registers hold where paths + * join, so the throw reads the registers: the record is still known to be in + * R6 at the throw, and its descriptor names the register. + */ +SEC("tc") +__description("frame_desc: a register nothing reads again is found at the throw") +__arch_x86_64 __arch_arm64 +__success __log_level(5) __flag(BPF_F_TEST_STATE_FREQ) +__msg("frame descriptors: slots 0 insns 0 sites 1 classes 1") +__naked int dead_reg_at_throw(void) +{ + asm volatile ( + "r8 = *(u32 *)(r1 + %[len]);" + "r1 = %[ringbuf] ll;" + "r2 = 8;" + "r3 = 0;" + "call %[bpf_ringbuf_reserve];" + "if r0 == 0 goto 2f;" + "r6 = r0;" + "r0 = 0;" + "if r8 == 0 goto 1f;" + "r7 = 0;" + "1: r1 = 0xeB9F;" + "call %[bpf_throw];" + "2: r0 = 0;" + "exit;" + : + : __imm_addr(ringbuf), + __imm(bpf_ringbuf_reserve), + __imm(bpf_throw), + [len]"i"(offsetof(struct __sk_buff, len)) + : __clobber_all); +} + /* An iterator is on the stack wherever it is held: the descriptor of the throw names it. */ SEC("tc") __description("frame_desc: an iterator at the throw has no slot") -- 2.53.0