From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net [23.128.96.19]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id EFCF01DA43 for ; Fri, 10 Nov 2023 16:11:22 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=none Received: from mx0b-00082601.pphosted.com (mx0b-00082601.pphosted.com [67.231.153.30]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9F4E73BF3C for ; Fri, 10 Nov 2023 08:11:21 -0800 (PST) Received: from pps.filterd (m0109332.ppops.net [127.0.0.1]) by mx0a-00082601.pphosted.com (8.17.1.19/8.17.1.19) with ESMTP id 3AAAwvkc021941 for ; Fri, 10 Nov 2023 08:11:20 -0800 Received: from maileast.thefacebook.com ([163.114.130.16]) by mx0a-00082601.pphosted.com (PPS) with ESMTPS id 3u9k82sx6n-3 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128 verify=NOT) for ; Fri, 10 Nov 2023 08:11:20 -0800 Received: from twshared58712.02.prn6.facebook.com (2620:10d:c0a8:1b::2d) by mail.thefacebook.com (2620:10d:c0a8:83::8) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.34; Fri, 10 Nov 2023 08:11:19 -0800 Received: by devbig019.vll3.facebook.com (Postfix, from userid 137359) id 99C613B49983A; Fri, 10 Nov 2023 08:11:07 -0800 (PST) From: Andrii Nakryiko To: , , , CC: , Subject: [PATCH bpf-next 4/8] bpf: print spilled register state in stack slot Date: Fri, 10 Nov 2023 08:10:53 -0800 Message-ID: <20231110161057.1943534-5-andrii@kernel.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20231110161057.1943534-1-andrii@kernel.org> References: <20231110161057.1943534-1-andrii@kernel.org> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-FB-Internal: Safe Content-Type: text/plain X-Proofpoint-GUID: 9lwqIBlbdNje0phDU1N8M2StLFmUAo3j X-Proofpoint-ORIG-GUID: 9lwqIBlbdNje0phDU1N8M2StLFmUAo3j X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.272,Aquarius:18.0.987,Hydra:6.0.619,FMLib:17.11.176.26 definitions=2023-11-10_13,2023-11-09_01,2023-05-22_02 Print the same register state representation when printing stack state, as we do for normal registers. Note that if stack slot contains subregister spill (1, 2, or 4 byte long), we'll still emit "m0?" mask for those bytes that are not part of spilled register. While means we can get something like fp-8=3D0000scalar() for a 4-byte spill with other 4 bytes still being STACK_ZERO. Some example before and after, taken from the log of pyperf_subprogs.bpf.o: 49: (7b) *(u64 *)(r10 -256) =3D r1 ; frame1: R1_w=3Dctx(off=3D0,imm=3D= 0) R10=3Dfp0 fp-256_w=3Dctx 49: (7b) *(u64 *)(r10 -256) =3D r1 ; frame1: R1_w=3Dctx(off=3D0,imm=3D= 0) R10=3Dfp0 fp-256_w=3Dctx(off=3D0,imm=3D0) 150: (7b) *(u64 *)(r10 -264) =3D r0 ; frame1: R0_w=3Dmap_value_or_nul= l(id=3D6,off=3D0,ks=3D192,vs=3D4,imm=3D0) R10=3Dfp0 fp-264_w=3Dmap_value_= or_null 150: (7b) *(u64 *)(r10 -264) =3D r0 ; frame1: R0_w=3Dmap_value_or_nul= l(id=3D6,off=3D0,ks=3D192,vs=3D4,imm=3D0) R10=3Dfp0 fp-264_w=3Dmap_value_= or_null(id=3D6,off=3D0,ks=3D192,vs=3D4,imm=3D0) 5192: (61) r1 =3D *(u32 *)(r10 -272) ; frame1: R1_w=3Dscalar(smin=3Dsm= in32=3D0,smax=3Dumax=3Dsmax32=3Dumax32=3D15,var_off=3D(0x0; 0xf)) R10=3Df= p0 fp-272=3D 5192: (61) r1 =3D *(u32 *)(r10 -272) ; frame1: R1_w=3Dscalar(smin=3Dsm= in32=3D0,smax=3Dumax=3Dsmax32=3Dumax32=3D15,var_off=3D(0x0; 0xf)) R10=3Df= p0 fp-272=3D????scalar(smin=3Dsmin32=3D0,smax=3Dumax=3Dsmax32=3Dumax32=3D= 15,var_off=3D(0x0; 0xf)) While at it, do a few other simple clean ups: - skip slot if it's not scratched before detecting whether it's valid; - move taking spilled_reg pointer outside of switch (only DYNPTR has to adjust that to get to the "main" slot); - don't recalculate types_buf second time for MISC/ZERO/default case. Signed-off-by: Andrii Nakryiko --- kernel/bpf/log.c | 36 +++++++++++++++++------------------- 1 file changed, 17 insertions(+), 19 deletions(-) diff --git a/kernel/bpf/log.c b/kernel/bpf/log.c index 05d737e2fab3..97a1641e848e 100644 --- a/kernel/bpf/log.c +++ b/kernel/bpf/log.c @@ -618,7 +618,6 @@ void print_verifier_state(struct bpf_verifier_env *en= v, const struct bpf_func_st bool print_all) { const struct bpf_reg_state *reg; - enum bpf_reg_type t; int i; =20 if (state->frameno) @@ -637,32 +636,38 @@ void print_verifier_state(struct bpf_verifier_env *= env, const struct bpf_func_st for (i =3D 0; i < state->allocated_stack / BPF_REG_SIZE; i++) { char types_buf[BPF_REG_SIZE + 1]; bool valid =3D false; + u8 slot_type; int j; =20 + if (!print_all && !stack_slot_scratched(env, i)) + continue; + for (j =3D 0; j < BPF_REG_SIZE; j++) { - if (state->stack[i].slot_type[j] !=3D STACK_INVALID) + slot_type =3D state->stack[i].slot_type[j]; + if (slot_type !=3D STACK_INVALID) valid =3D true; - types_buf[j] =3D slot_type_char[state->stack[i].slot_type[j]]; + types_buf[j] =3D slot_type_char[slot_type]; } types_buf[BPF_REG_SIZE] =3D 0; if (!valid) continue; - if (!print_all && !stack_slot_scratched(env, i)) - continue; + + reg =3D &state->stack[i].spilled_ptr; switch (state->stack[i].slot_type[BPF_REG_SIZE - 1]) { case STACK_SPILL: - reg =3D &state->stack[i].spilled_ptr; - t =3D reg->type; + /* print MISC/ZERO/INVALID slots above subreg spill */ + for (j =3D 0; j < BPF_REG_SIZE; j++) + if (state->stack[i].slot_type[j] =3D=3D STACK_SPILL) + break; + types_buf[j] =3D '\0'; =20 verbose(env, " fp%d", (-i - 1) * BPF_REG_SIZE); print_liveness(env, reg->live); - verbose(env, "=3D%s", t =3D=3D SCALAR_VALUE ? "" : reg_type_str(env, = t)); - if (t =3D=3D SCALAR_VALUE && reg->precise) - verbose(env, "P"); - if (t =3D=3D SCALAR_VALUE && tnum_is_const(reg->var_off)) - verbose(env, "%lld", reg->var_off.value + reg->off); + verbose(env, "=3D%s", types_buf); + print_reg_state(env, reg); break; case STACK_DYNPTR: + /* skip to main dynptr slot */ i +=3D BPF_DYNPTR_NR_SLOTS - 1; reg =3D &state->stack[i].spilled_ptr; =20 @@ -674,7 +679,6 @@ void print_verifier_state(struct bpf_verifier_env *en= v, const struct bpf_func_st break; case STACK_ITER: /* only main slot has ref_obj_id set; skip others */ - reg =3D &state->stack[i].spilled_ptr; if (!reg->ref_obj_id) continue; =20 @@ -688,12 +692,6 @@ void print_verifier_state(struct bpf_verifier_env *e= nv, const struct bpf_func_st case STACK_MISC: case STACK_ZERO: default: - reg =3D &state->stack[i].spilled_ptr; - - for (j =3D 0; j < BPF_REG_SIZE; j++) - types_buf[j] =3D slot_type_char[state->stack[i].slot_type[j]]; - types_buf[BPF_REG_SIZE] =3D 0; - verbose(env, " fp%d", (-i - 1) * BPF_REG_SIZE); print_liveness(env, reg->live); verbose(env, "=3D%s", types_buf); --=20 2.34.1