Skip to content

Commit da43109

Browse files
tracing: expand uprobe unit test
To cover reading and matching against string type args. Pointers passed to functions hooked by uprobes may point to memory that has been paged out. This test forces this scenario to confirm that we will be able to reach a paged out argument. The problem being tested here is not specific to strings. This test uses a string as an example, and as a starting point. But, we need to cut over the reading of all pointer types to use a sleepable program in order to ensure that users will always be able to access the pointer's data. Signed-off-by: Andy Strohman <[email protected]>
1 parent 18213e6 commit da43109

File tree

1 file changed

+26
-9
lines changed

1 file changed

+26
-9
lines changed

pkg/sensors/tracing/uprobe_test.go

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1919
"sigs.k8s.io/yaml"
2020

21+
"github.com/cilium/tetragon/pkg/bpf"
2122
"github.com/cilium/tetragon/pkg/kernels"
2223
bc "github.com/cilium/tetragon/pkg/matchers/bytesmatcher"
2324

@@ -847,7 +848,7 @@ func TestUprobeArgsWithAddress(t *testing.T) {
847848
testUprobeArgs(t, checkers, tp)
848849
}
849850

850-
func uprobeArgsMatch(t *testing.T, argOneVal int, expectCheckerFailure bool) error {
851+
func uprobeArgsMatch(t *testing.T, symbol string, arg_type string, argOneVal string, expectCheckerFailure bool) error {
851852
uprobeTest1 := testutils.RepoRootPath("contrib/tester-progs/uprobe-test-1")
852853
libUprobe := testutils.RepoRootPath("contrib/tester-progs/libuprobe.so")
853854

@@ -860,16 +861,16 @@ spec:
860861
uprobes:
861862
- path: "` + libUprobe + `"
862863
symbols:
863-
- "uprobe_test_lib_arg1"
864+
- "` + symbol + `"
864865
args:
865866
- index: 0
866-
type: "int"
867+
type: "` + arg_type + `"
867868
selectors:
868869
- matchArgs:
869870
- args: [0]
870871
operator: "Equal"
871872
values:
872-
- "` + strconv.Itoa(argOneVal) + `"
873+
- "` + argOneVal + `"
873874
`
874875

875876
pathConfigHook := []byte(pathHook)
@@ -881,7 +882,7 @@ spec:
881882
upChecker := ec.NewProcessUprobeChecker("UPROBE_BINARIES_MATCH").
882883
WithProcess(ec.NewProcessChecker().
883884
WithBinary(sm.Full(uprobeTest1))).
884-
WithSymbol(sm.Full("uprobe_test_lib_arg1"))
885+
WithSymbol(sm.Full(symbol))
885886
checker := ec.NewUnorderedEventChecker(upChecker)
886887

887888
var doneWG, readyWG sync.WaitGroup
@@ -904,13 +905,29 @@ spec:
904905
return jsonchecker.JsonTestCheckExpect(t, checker, expectCheckerFailure)
905906
}
906907

907-
func TestUprobeArgsMatch(t *testing.T) {
908-
err := uprobeArgsMatch(t, 123, false)
908+
func TestUprobeIntArgMatch(t *testing.T) {
909+
err := uprobeArgsMatch(t, "uprobe_test_lib_arg1", "int", "123", false)
909910
require.NoError(t, err)
910911
}
911912

912-
func TestUprobeArgsMatchNot(t *testing.T) {
913-
err := uprobeArgsMatch(t, 124, true)
913+
func TestUprobeIntArgMatchNot(t *testing.T) {
914+
err := uprobeArgsMatch(t, "uprobe_test_lib_arg1", "int", "124", true)
915+
require.NoError(t, err)
916+
}
917+
918+
func TestUprobeStringArgMatch(t *testing.T) {
919+
if !bpf.HasKfunc("bpf_copy_from_user_str") {
920+
t.Skip("this test requires bpf_copy_from_user_str kfunc support")
921+
}
922+
err := uprobeArgsMatch(t, "uprobe_test_lib_string_arg", "string", "hello world!", false)
923+
require.NoError(t, err)
924+
}
925+
926+
func TestUprobeStringArgMatchNot(t *testing.T) {
927+
if !bpf.HasKfunc("bpf_copy_from_user_str") {
928+
t.Skip("this test requires bpf_copy_from_user_str kfunc support")
929+
}
930+
err := uprobeArgsMatch(t, "uprobe_test_lib_string_arg", "string", "hi world!", true)
914931
require.NoError(t, err)
915932
}
916933

0 commit comments

Comments
 (0)