Relocation processing of STBSZ in ERROR's get_bvar() is too early
PATCH 1/1 Relocation processing of STBSZ in ERROR's get_bvar() is too early
From: Eugene Loh <[email protected]>
When we call dtrace_go(), we do something like this:
dt_bpf_make_progs()
dt_program_construct() // just for ERROR
dt_link()
dt_link_construct()
dt_bpf_gmap_create()
dt_bpf_load_progs() // other
dt_link()
dt_link_construct()In dt_link_construct() we dive down and find dt_get_bvar(). One of the relocations is to supply the value of STBSZ. The first dt_link() is for ERROR, while the subsequent calls in dt_bpf_load_progs() are for other clauses -- that is, two separate versions of dt_get_bvar() are used. Meanwhile, the value of STBSZ is not set until dt_bpf_gmap_create(). This means that the ERROR copy of dt_get_bvar() does not have STBSZ set properly. This means that if ERROR accesses probeprov or probename, dt_get_bvar() returns the beginning of the string table, which is a NUL terminator.
Change dt_bpf_reloc_prog() -- which performs relocation processing for BPF maps, necessarily after dt_bpf_gmap_create()! -- to check STBSZ and set its value if necessary.
--- libdtrace/dt_bpf.c | 17 +++++++++++ test/unittest/builtinvar/tst.probe_dtrace.d | 34 +++++++++++++++++++++ test/unittest/builtinvar/tst.probe_dtrace.r | 6 ++++ 3 files changed, 57 insertions(+) create mode 100644 test/unittest/builtinvar/tst.probe_dtrace.d create mode 100644 test/unittest/builtinvar/tst.probe_dtrace.r
libdtrace/dt_bpf.c +17 -0
@@ -1124,6 +1124,23 @@ dt_bpf_reloc_prog(dtrace_hdl_t *dtp, const dtrace_difo_t *dp) text[ioff +1 ].imm =0 ; }else if (rp->dofr_type ==R_BPF_64_32 ) text[ioff].imm = val; + }else if (idp->di_kind ==DT_IDENT_SCALAR && + idp->di_id ==DT_CONST_STBSZ ) { +/* +* There is one other case. Although STBSZ is not a map +* pointer, the string table size is set when the BPF maps +* are created. So we may have to set its value here. +*/ +assert (strcmp (name,"STBSZ" ) ==0 );// FIXME: extra check for development + +/* +* Worry only about the cases that have not yet +* been handled. +*/ +if (rp->dofr_type !=R_BPF_64_64 && rp->dofr_data ==0 ) { + text[ioff].imm = dtp->dt_strmax; + text[ioff +1 ].imm =0 ; + } } } }
test/unittest/builtinvar/tst.probe_dtrace.d new file +34 -0
@@ -0,0 +1,34 @@ +/* + * 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. + */ + +/* + * ASSERTION: + * Built-in variables listing provider, module, function, and name + * are correct from dtrace-provider probes. + * + * SECTION: Variables/Built-in Variables + */ + +#pragma D option quiet + +/* Check build-in variables from the dtrace-provider probes. */ +BEGIN,ERROR,END +{ + printf("%s:%s:%s:%s\n", probeprov, probemod, probefunc, probename); +} + +/* Cause the ERROR probe to fire. */ +BEGIN +{ + *((int*)0); +} + +/* Cause the END probe to fire. */ +BEGIN,ERROR +{ + exit(0); +}
test/unittest/builtinvar/tst.probe_dtrace.r new file +6 -0
@@ -0,0 +1,6 @@ +dtrace:::BEGIN +dtrace:::ERROR +dtrace:::END + +-- @@stderr -- +dtrace: error in dt_clause_3 for probe ID 1 (dtrace:::BEGIN): invalid address ({ptr}) at BPF pc NNN
Signature · 1 line
2.43.5
I don't thibk this is the right solution. There are other values that are resolved as a relocation in dt_link_construct() and that could be found in an ERROR clause and thus get resolved before the correct value is known.
Instead, I think we need to simply move the construction of the dt_error program into dt_bpf_load_progs(). I should have done that when I split up program construction and loading into two distinct phases.
I'll post an alternative patch in a moment...
Quoted 124 lines · eugene.loh--- via DTrace-devel
From: Eugene Loh <[email protected]>
When we call
dtrace_go(), we do something like this:dt_bpf_make_progs() dt_program_construct() // just for ERROR dt_link() dt_link_construct() dt_bpf_gmap_create() dt_bpf_load_progs() // other dt_link() dt_link_construct()In
dt_link_construct()we dive down and finddt_get_bvar(). One of the relocations is to supply the value of STBSZ. The firstdt_link()is for ERROR, while the subsequent calls indt_bpf_load_progs()are for other clauses -- that is, two separate versions ofdt_get_bvar()are used. Meanwhile, the value of STBSZ is not set untildt_bpf_gmap_create(). This means that the ERROR copy ofdt_get_bvar()does not have STBSZ set properly. This means that if ERROR accesses probeprov or probename,dt_get_bvar()returns the beginning of the string table, which is a NUL terminator.Change
dt_bpf_reloc_prog()-- which performs relocation processing for BPF maps, necessarily afterdt_bpf_gmap_create()! -- to check STBSZ and set its value if necessary.Signed-off-by Eugene Loh <[email protected]>--- libdtrace/dt_bpf.c | 17 +++++++++++ test/unittest/builtinvar/tst.probe_dtrace.d | 34 +++++++++++++++++++++ test/unittest/builtinvar/tst.probe_dtrace.r | 6 ++++ 3 files changed, 57 insertions(+) create mode 100644 test/unittest/builtinvar/tst.probe_dtrace.d create mode 100644 test/unittest/builtinvar/tst.probe_dtrace.rlibdtrace/dt_bpf.c +17
-0@@ -1124,6 +1124,23 @@ dt_bpf_reloc_prog(dtrace_hdl_t *dtp, const dtrace_difo_t *dp) text[ioff +1 ].imm =0 ; }else if (rp->dofr_type ==R_BPF_64_32 ) text[ioff].imm = val; + }else if (idp->di_kind ==DT_IDENT_SCALAR && + idp->di_id ==DT_CONST_STBSZ ) { +/* +* There is one other case. Although STBSZ is not a map +* pointer, the string table size is set when the BPF maps +* are created. So we may have to set its value here. +*/ +assert (strcmp (name,"STBSZ" ) ==0 );// FIXME: extra check for development + +/* +* Worry only about the cases that have not yet +* been handled. +*/ +if (rp->dofr_type !=R_BPF_64_64 && rp->dofr_data ==0 ) { + text[ioff].imm = dtp->dt_strmax; + text[ioff +1 ].imm =0 ; + } } } }test/unittest/builtinvar/tst.probe_dtrace.d new file +34
-0@@ -0,0 +1,34 @@ +/* + * 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. + */ + +/* + * ASSERTION: + * Built-in variables listing provider, module, function, and name + * are correct from dtrace-provider probes. + * + * SECTION: Variables/Built-in Variables + */ + +#pragma D option quiet + +/* Check build-in variables from the dtrace-provider probes. */ +BEGIN,ERROR,END +{ + printf("%s:%s:%s:%s\n", probeprov, probemod, probefunc, probename); +} + +/* Cause the ERROR probe to fire. */ +BEGIN +{ + *((int*)0); +} + +/* Cause the END probe to fire. */ +BEGIN,ERROR +{ + exit(0); +}test/unittest/builtinvar/tst.probe_dtrace.r new file +6
-0@@ -0,0 +1,6 @@ +dtrace:::BEGIN +dtrace:::ERROR +dtrace:::END + +-- @@stderr -- +dtrace: error in dt_clause_3 for probe ID 1 (dtrace:::BEGIN): invalid address ({ptr}) at BPF pc NNNSignature · 7 lines
2.43.5 _______________________________________________ DTrace-devel mailing list [email protected] https://oss.oracle.com/mailman/listinfo/dtrace-devel
Quoted 9 lines · Kris Van Hees
I don't thibk this is the right solution. There are other values that are resolved as a relocation in
dt_link_construct()and that could be found in an ERROR clause and thus get resolved before the correct value is known.Instead, I think we need to simply move the construction of the
dt_errorprogram intodt_bpf_load_progs(). I should have done that when I split up program construction and loading into two distinct phases.I'll post an alternative patch in a moment...
Thanks. I confess I posted this patch impatiently, with the uneasy suspicion that the relocation was simply being performed at the wrong time.
Quoted 27 lines · eugene.loh--- via DTrace-devel
Quoted 26 lines
From: Eugene Loh <[email protected]>
When we call
dtrace_go(), we do something like this:dt_bpf_make_progs() dt_program_construct() // just for ERROR dt_link() dt_link_construct() dt_bpf_gmap_create() dt_bpf_load_progs() // other dt_link() dt_link_construct()In
dt_link_construct()we dive down and finddt_get_bvar(). One of the relocations is to supply the value of STBSZ. The firstdt_link()is for ERROR, while the subsequent calls indt_bpf_load_progs()are for other clauses -- that is, two separate versions ofdt_get_bvar()are used. Meanwhile, the value of STBSZ is not set untildt_bpf_gmap_create(). This means that the ERROR copy ofdt_get_bvar()does not have STBSZ set properly. This means that if ERROR accesses probeprov or probename,dt_get_bvar()returns the beginning of the string table, which is a NUL terminator.Change
dt_bpf_reloc_prog()-- which performs relocation processing for BPF maps, necessarily afterdt_bpf_gmap_create()! -- to check STBSZ and set its value if necessary.