[DTrace-devel] [PATCH 16/16] dis: add support for variables used in trampoline code
Kris Van Hees
kris.van.hees at oracle.com
Sat Jan 13 00:24:48 UTC 2024
Signed-off-by: Kris Van Hees <kris.van.hees at oracle.com>
---
libdtrace/dt_dis.c | 54 ++++++++++++++--------
test/unittest/disasm/tst.ann-tramp-lvar.r | 2 +
test/unittest/disasm/tst.ann-tramp-lvar.sh | 27 +++++++++++
test/unittest/disasm/tst.ann-tramp-tvar.r | 3 ++
test/unittest/disasm/tst.ann-tramp-tvar.sh | 19 ++++++++
5 files changed, 85 insertions(+), 20 deletions(-)
create mode 100644 test/unittest/disasm/tst.ann-tramp-lvar.r
create mode 100755 test/unittest/disasm/tst.ann-tramp-lvar.sh
create mode 100644 test/unittest/disasm/tst.ann-tramp-tvar.r
create mode 100755 test/unittest/disasm/tst.ann-tramp-tvar.sh
diff --git a/libdtrace/dt_dis.c b/libdtrace/dt_dis.c
index d3785d4e..88b41ce1 100644
--- a/libdtrace/dt_dis.c
+++ b/libdtrace/dt_dis.c
@@ -1,6 +1,6 @@
/*
* Oracle Linux DTrace.
- * Copyright (c) 2005, 2023, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2005, 2024, Oracle and/or its affiliates. All rights reserved.
* Licensed under the Universal Permissive License v 1.0 as shown at
* http://oss.oracle.com/licenses/upl.
*/
@@ -90,6 +90,11 @@ dt_dis_varname_off(const dtrace_difo_t *dp, uint_t off, uint_t scope, uint_t add
*
* For variables, the sequence of instructions we are looking for is:
* insn code dst src offset imm
+ * -1: ld dst %r9 DCTX_*VARS 00000000
+ * 0: ld xxx dst var_offset 00000000
+ * 0: st dst src var_offset 00000000
+ * 0: add dst 0 0 var_offset
+ * -or-
* -2: ld dst %fp DT_STK_DCTX 00000000
* -1: ld dst dst DCTX_*VARS 00000000
* 0: ld xxx dst var_offset 00000000
@@ -103,6 +108,9 @@ dt_dis_varname_off(const dtrace_difo_t *dp, uint_t off, uint_t scope, uint_t add
*
* For string constants, the sequence of instructions we are looking for is:
* insn code dst src offset imm
+ * -1: ld dst %r9 DCTX_STRTAB 00000000
+ * 0: add dst 0 0 var_offset
+ * -or-
* -2: ld dst %fp DT_STK_DCTX 00000000
* -1: ld dst dst DCTX_STRTAB 00000000
* 0: add dst 0 0 var_offset
@@ -125,13 +133,28 @@ dt_dis_refname(const dtrace_difo_t *dp, const struct bpf_insn *in, uint_t addr,
goto out;
}
- /* make sure in[-2] and in[-1] exist */
- if (addr < 2)
+ /* make sure in[-1] exists and is a ld instruction */
+ if (addr < 1 || in[-1].code != ldcode || in[-1].imm != 0)
goto out;
- /* get dst and scope */
+ /* get dst register */
dst = in[-1].dst_reg;
+ /* trampoline code uses %r9, regular code needs to load dctx */
+ if (in[-1].src_reg != BPF_REG_9) {
+ /* make sure in[-2] exists and is a ld instruction */
+ if (addr < 2 || in[-2].code != ldcode || in[-2].imm != 0)
+ goto out;
+
+ /* check preceding instructions */
+ if (in[-2].dst_reg != dst ||
+ in[-2].src_reg != BPF_REG_FP ||
+ in[-2].off != DT_STK_DCTX ||
+ in[-1].code != ldcode ||
+ in[-1].src_reg != dst)
+ goto out;
+ }
+
if (in[-1].off == DCTX_GVARS)
scope = DIFV_SCOPE_GLOBAL;
else if (in[-1].off == DCTX_LVARS)
@@ -139,17 +162,6 @@ dt_dis_refname(const dtrace_difo_t *dp, const struct bpf_insn *in, uint_t addr,
else if (in[-1].off != DCTX_STRTAB)
goto out;
- /* check preceding instructions */
- if (in[-2].code != ldcode ||
- in[-2].dst_reg != dst ||
- in[-2].src_reg != BPF_REG_FP ||
- in[-2].off != DT_STK_DCTX ||
- in[-2].imm != 0 ||
- in[-1].code != ldcode ||
- in[-1].src_reg != dst ||
- in[-1].imm != 0)
- goto out;
-
/* check the current instruction and read var_offset */
if (in[-1].off == DCTX_STRTAB) {
if (in->dst_reg == dst &&
@@ -391,12 +403,14 @@ dt_dis_bpf_args(const dtrace_difo_t *dp, const char *fn,
return buf;
} else if (strcmp(fn, "dt_get_tvar") == 0) {
/*
- * We know that the previous six instructions exist and
- * move the variable id to a register in the first instruction
- * of that seqeuence (because we wrote the code generator to
- * emit the instructions in this exact order.)
+ * The preceding 5 or 6 instructions must exist and move the
+ * variable id to a register in the first instruction of that
+ * sequence.
+ * A 5 instruction sequence is used in trampoline code, and can
+ * be identified by checking that the source register of the
+ * lddw instruction in the one-before-last instruction is %r9.
*/
- in -= 6;
+ in -= in[-2].src_reg == BPF_REG_9 ? 5 : 6;
snprintf(buf, len, "self->%s",
dt_dis_varname_id(dp, in->imm + DIF_VAR_OTHER_UBASE,
DIFV_SCOPE_THREAD, addr));
diff --git a/test/unittest/disasm/tst.ann-tramp-lvar.r b/test/unittest/disasm/tst.ann-tramp-lvar.r
new file mode 100644
index 00000000..116c0ae2
--- /dev/null
+++ b/test/unittest/disasm/tst.ann-tramp-lvar.r
@@ -0,0 +1,2 @@
+07 N N 0000 00000000 add %rX, 0 ! this->-io-bio
+07 N N 0000 00000000 add %rX, 0 ! this->-io-bio
diff --git a/test/unittest/disasm/tst.ann-tramp-lvar.sh b/test/unittest/disasm/tst.ann-tramp-lvar.sh
new file mode 100755
index 00000000..03f67ad6
--- /dev/null
+++ b/test/unittest/disasm/tst.ann-tramp-lvar.sh
@@ -0,0 +1,27 @@
+#!/bin/bash
+#
+# Oracle Linux DTrace.
+# Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved.
+# Licensed under the Universal Permissive License v 1.0 as shown at
+# http://oss.oracle.com/licenses/upl.
+#
+
+dtrace=$1
+
+$dtrace $dt_flags -xdisasm=2 -Sn '
+BEGIN,
+io:::start
+{
+ exit(0);
+}
+' 2>&1 | awk '/this->/ {
+ sub(/^[^:]+: /, "");
+
+ gsub(/%r[0-9]/, "%rX");
+ $2 = "N";
+ $3 = "N";
+
+ print;
+ }'
+
+exit $?
diff --git a/test/unittest/disasm/tst.ann-tramp-tvar.r b/test/unittest/disasm/tst.ann-tramp-tvar.r
new file mode 100644
index 00000000..351450e2
--- /dev/null
+++ b/test/unittest/disasm/tst.ann-tramp-tvar.r
@@ -0,0 +1,3 @@
+85 0 1 0000 ffffffff call dt_get_tvar ! self->-io-bio-ptr
+85 0 1 0000 ffffffff call dt_get_tvar ! self->-io-bio-ptr
+85 0 1 0000 ffffffff call dt_get_tvar ! self->-io-bio-ptr
diff --git a/test/unittest/disasm/tst.ann-tramp-tvar.sh b/test/unittest/disasm/tst.ann-tramp-tvar.sh
new file mode 100755
index 00000000..c66e264c
--- /dev/null
+++ b/test/unittest/disasm/tst.ann-tramp-tvar.sh
@@ -0,0 +1,19 @@
+#!/bin/bash
+#
+# Oracle Linux DTrace.
+# Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved.
+# Licensed under the Universal Permissive License v 1.0 as shown at
+# http://oss.oracle.com/licenses/upl.
+#
+
+dtrace=$1
+
+$dtrace $dt_flags -xdisasm=2 -Sn '
+BEGIN,
+io:::wait-done
+{
+ exit(0);
+}
+' 2>&1 | awk '/ call dt_get_tvar/ { sub(/^[^:]+: /, ""); print; }'
+
+exit $?
--
2.42.0
More information about the DTrace-devel
mailing list