[DTrace-devel] [PATCH] Fix tstring length

Kris Van Hees kris.van.hees at oracle.com
Mon Nov 15 18:26:44 UTC 2021


The tstring area was being allocated without accounting for the NUL byte
at the end of strings.

The tstring reset code was calculating the allocation size per string at
every iteration rather than once.

Signed-off-by: Kris Van Hees <kris.van.hees at oracle.com>
---
 libdtrace/dt_bpf.c | 9 +++++----
 libdtrace/dt_cg.c  | 9 +++++----
 2 files changed, 10 insertions(+), 8 deletions(-)

diff --git a/libdtrace/dt_bpf.c b/libdtrace/dt_bpf.c
index 3be73809..8c1b5c5a 100644
--- a/libdtrace/dt_bpf.c
+++ b/libdtrace/dt_bpf.c
@@ -295,12 +295,13 @@ dt_bpf_gmap_create(dtrace_hdl_t *dtp)
 	 *	- size of the DTrace machine state, rounded up to the nearest
 	 *	  multiple of 8
 	 *	- 8 bytes padding for trace buffer alignment purposes
-	 *	- maximum trace buffer record size, rounded up to the
+	 *	- maximum trace buffer record size, rounded up to the nearest
 	 *	  multiple of 8
 	 *	- the greater of:
 	 *		+ the maximum stack trace size
-	 *		+ four times the maximum string size (incl. length
-	 *		  and allowing round up to multiple of 8)
+	 *		+ four times the maximum string storage size (incl.
+	 *		  length prefix and terminating NUL byte) rounded up to
+	 *		  the nearest multiple of 8),
 	 *		  plus the maximum string size (to accomodate the BPF
 	 *		  verifier)
 	 */
@@ -310,7 +311,7 @@ dt_bpf_gmap_create(dtrace_hdl_t *dtp)
 		MAX(sizeof(uint64_t) * dtp->dt_options[DTRACEOPT_MAXFRAMES],
 		    DT_TSTRING_SLOTS *
 			roundup(DT_STRLEN_BYTES +
-				dtp->dt_options[DTRACEOPT_STRSIZE], 8) +
+				dtp->dt_options[DTRACEOPT_STRSIZE] + 1, 8) +
 		    dtp->dt_options[DTRACEOPT_STRSIZE] + 1
 		);
 	if (create_gmap(dtp, "mem", BPF_MAP_TYPE_PERCPU_ARRAY,
diff --git a/libdtrace/dt_cg.c b/libdtrace/dt_cg.c
index 6a4500a5..455e5440 100644
--- a/libdtrace/dt_cg.c
+++ b/libdtrace/dt_cg.c
@@ -806,18 +806,19 @@ dt_cg_tstring_reset(dtrace_hdl_t *dtp)
 {
 	int		i;
 	dt_tstring_t	*ts;
+	uint64_t	size = roundup(DT_STRLEN_BYTES +
+				       dtp->dt_options[DTRACEOPT_STRSIZE] + 1,
+				       8);
 
 	if (dtp->dt_tstrings == NULL) {
 		dtp->dt_tstrings = dt_calloc(dtp, DT_TSTRING_SLOTS,
-					    sizeof(dt_tstring_t));
+					     sizeof(dt_tstring_t));
 		if (dtp->dt_tstrings == NULL)
 			longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
 
 		ts = dtp->dt_tstrings;
 		for (i = 0; i < DT_TSTRING_SLOTS; i++, ts++)
-			ts->offset = i *
-				roundup(DT_STRLEN_BYTES +
-					dtp->dt_options[DTRACEOPT_STRSIZE], 8);
+			ts->offset = i * size;
 	}
 
 	ts = dtp->dt_tstrings;
-- 
2.33.0




More information about the DTrace-devel mailing list