[DTrace-devel] [PATCH] cg: do not perform runtime bounds checking on arrays of size 0 or 1

Kris Van Hees kris.van.hees at oracle.com
Wed Nov 22 20:58:36 UTC 2023


Commit c7d73146 "parser: do not bounds-check arrays of size 0 or 1"
already disabled compile time bounds checking but there are cases
where the bounds checking was done at runtime instead.  Those also
need to ignore arrays of size 0 or 1, for the same reason.

Signed-off-by: Kris Van Hees <kris.van.hees at oracle.com>
---
 libdtrace/dt_cg.c | 14 ++++++++++----
 1 file changed, 10 insertions(+), 4 deletions(-)

diff --git a/libdtrace/dt_cg.c b/libdtrace/dt_cg.c
index 843ac165..79ee76fe 100644
--- a/libdtrace/dt_cg.c
+++ b/libdtrace/dt_cg.c
@@ -3849,11 +3849,17 @@ dt_cg_arithmetic_op(dt_node_t *dnp, dt_irlist_t *dlp, dt_regset_t *drp,
 
 		elem_size = ctf_type_size(ctfp, r.ctr_contents);
 
-		emit(dlp,  BPF_BRANCH_IMM(BPF_JLT, dnp->dn_right->dn_reg, r.ctr_nelems * elem_size, L1));
+		/*
+		 * Arrays of size 0 or 1 should not cause bounds checking as
+		 * they are usually an anchor for dynamically sized arrays.
+		 */
+		if (r.ctr_nelems > 1) {
+			emit(dlp,  BPF_BRANCH_IMM(BPF_JLT, dnp->dn_right->dn_reg, r.ctr_nelems * elem_size, L1));
 
-		/* Report out-of-bounds fault on the index. */
-		emit(dlp,  BPF_ALU64_IMM(BPF_DIV, dnp->dn_right->dn_reg, elem_size));
-		dt_cg_probe_error(yypcb, DTRACEFLT_BADINDEX, DT_ISREG, dnp->dn_right->dn_reg);
+			/* Report out-of-bounds fault on the index. */
+			emit(dlp,  BPF_ALU64_IMM(BPF_DIV, dnp->dn_right->dn_reg, elem_size));
+			dt_cg_probe_error(yypcb, DTRACEFLT_BADINDEX, DT_ISREG, dnp->dn_right->dn_reg);
+		}
 
 		emitl(dlp, L1,
 			   BPF_ALU64_REG(op, dnp->dn_left->dn_reg, dnp->dn_right->dn_reg));
-- 
2.39.3




More information about the DTrace-devel mailing list