[DTrace-devel] [PATCH 19/61] Handle high-order bits of rand() properly

eugene.loh at oracle.com eugene.loh at oracle.com
Fri Jul 8 14:45:03 UTC 2022


From: Eugene Loh <eugene.loh at oracle.com>

The D rand() function is declared to be "int" -- a 4-byte, signed
integer.  Internally, D represents integers as 64-bit quantities.
Therefore, after calling the BPF helper function to get 32 random
bits, we should sign-extend the upper bits.

Meanwhile, the rand_intra test was checking if the uppermost 32 bits
of "x = rand()" were 0.  This makes no sense since x is a signed,
4-byte integer and should be populated with 0xffffffff half of the
time.  The test would fail once x is correctly sign-extended (by a
future patch).  Eliminate the check on the upper bits.

Signed-off-by: Eugene Loh <eugene.loh at oracle.com>
---
 libdtrace/dt_cg.c                     |  4 +++-
 test/unittest/funcs/tst.rand_intra.sh | 15 +++++----------
 2 files changed, 8 insertions(+), 11 deletions(-)

diff --git a/libdtrace/dt_cg.c b/libdtrace/dt_cg.c
index 4729710d..633e4613 100644
--- a/libdtrace/dt_cg.c
+++ b/libdtrace/dt_cg.c
@@ -3907,7 +3907,9 @@ dt_cg_subr_rand(dt_node_t *dnp, dt_irlist_t *dlp, dt_regset_t *drp)
 	dt_regset_free_args(drp);
 	emit(dlp,  BPF_MOV_REG(dnp->dn_reg, BPF_REG_0));
 	dt_regset_free(drp, BPF_REG_0);
-	emit(dlp,  BPF_ALU64_IMM(BPF_AND, dnp->dn_reg, 0xffffffff));
+
+	emit(dlp,  BPF_ALU64_IMM(BPF_LSH, dnp->dn_reg, 32));
+	emit(dlp,  BPF_ALU64_IMM(BPF_ARSH, dnp->dn_reg, 32));
 
 	TRACE_REGSET("    subr-rand:End  ");
 }
diff --git a/test/unittest/funcs/tst.rand_intra.sh b/test/unittest/funcs/tst.rand_intra.sh
index 0d263fd5..ac1f43e0 100755
--- a/test/unittest/funcs/tst.rand_intra.sh
+++ b/test/unittest/funcs/tst.rand_intra.sh
@@ -14,7 +14,7 @@ niter=25000
 # is, use lquantize to look at the distribution of 4-bit blocks.
 
 $dtrace $dt_flags -q -o $tmpfile -c test/triggers/bogus-ioctl -n '
-BEGIN { nuperr = n = 0 }
+BEGIN { n = 0 }
 syscall::ioctl:entry
 /pid == $target/
 {
@@ -29,13 +29,12 @@ syscall::ioctl:entry
 	@g = lquantize((x >> 24) & 0xf, 0, 16, 1);
 	@h = lquantize((x >> 28) & 0xf, 0, 16, 1);
 
-	nuperr += (x & 0xffffffff00000000) ? 1 : 0;
 	n++;
 }
 syscall::ioctl:entry
 /pid == $target && n >= '$niter'/
 {
-	printf("# of upper-bit errors: %d out of %d\n", nuperr, n);
+	printf("number of iterations: %d\n", n);
 	exit(0);
 }'
 
@@ -53,13 +52,9 @@ awk '
         nbins = 16;
     }
 
-    # process line: "# of upper-bit errors: 0 out of ..."
-    /upper-bit errors/ {
-        if (int($5) != 0) {
-            print "ERROR: found some upper-bit errors";
-            exit 1;
-        }
-        n = int($8);
+    # process line: "number of iterations: ..."
+    /number of iterations:/ {
+        n = int($4);
         if (n != '$niter') {
             print "ERROR: unexpected amount of data";
             exit 1;
-- 
2.18.4




More information about the DTrace-devel mailing list