Blame SOURCES/rhbz1643997.0009-stapbpf-assembler-WIP-8-bpf-asm.exp-driver-and-more-.patch

e4e640
From 17e0a28b75e8a5961fcfae52b631eebf9e9fd118 Mon Sep 17 00:00:00 2001
e4e640
From: Serhei Makarov <smakarov@redhat.com>
e4e640
Date: Wed, 24 Oct 2018 13:30:29 -0400
e4e640
Subject: [PATCH 09/32] stapbpf assembler WIP #8 :: bpf-asm.exp driver and more
e4e640
 testcases
e4e640
e4e640
---
e4e640
 testsuite/systemtap.bpf/asm_tests/branch.stp       |  14 ++-
e4e640
 .../systemtap.bpf/asm_tests/err-printf_args.stp    |   9 ++
e4e640
 testsuite/systemtap.bpf/asm_tests/err_numeric.stp  |   1 -
e4e640
 testsuite/systemtap.bpf/asm_tests/leniency.stp     |  14 ++-
e4e640
 testsuite/systemtap.bpf/asm_tests/printf-basic.stp |   9 ++
e4e640
 testsuite/systemtap.bpf/asm_tests/printf.stp       |  16 ++++
e4e640
 testsuite/systemtap.bpf/asm_tests/simple.stp       |   9 +-
e4e640
 testsuite/systemtap.bpf/asm_tests/string.stp       |  21 +++--
e4e640
 testsuite/systemtap.bpf/asm_tests/temporary.stp    |   9 +-
e4e640
 testsuite/systemtap.bpf/asm_tests/unreachable.stp  |  17 ++++
e4e640
 testsuite/systemtap.bpf/bpf-asm.exp                | 105 +++++++++++++++++++++
e4e640
 11 files changed, 205 insertions(+), 19 deletions(-)
e4e640
 create mode 100644 testsuite/systemtap.bpf/asm_tests/err-printf_args.stp
e4e640
 create mode 100644 testsuite/systemtap.bpf/asm_tests/printf-basic.stp
e4e640
 create mode 100644 testsuite/systemtap.bpf/asm_tests/printf.stp
e4e640
 create mode 100644 testsuite/systemtap.bpf/asm_tests/unreachable.stp
e4e640
 create mode 100644 testsuite/systemtap.bpf/bpf-asm.exp
e4e640
e4e640
diff --git a/testsuite/systemtap.bpf/asm_tests/branch.stp b/testsuite/systemtap.bpf/asm_tests/branch.stp
e4e640
index aa22bf195..0e49213df 100644
e4e640
--- a/testsuite/systemtap.bpf/asm_tests/branch.stp
e4e640
+++ b/testsuite/systemtap.bpf/asm_tests/branch.stp
e4e640
@@ -6,7 +6,6 @@ function foo:long (x:long) %{ /* bpf */ /* pure */
e4e640
   label, _bar;
e4e640
   0xb7, $$, -, -, 50; /* mov $$, 50 */
e4e640
   label, _done;
e4e640
-  /* 0xbf, $$, $$, -, -; /* dummy op */
e4e640
 %}
e4e640
 
e4e640
 function bar:long (x:long) {
e4e640
@@ -14,9 +13,14 @@ function bar:long (x:long) {
e4e640
 }
e4e640
 
e4e640
 probe begin {
e4e640
-  printf("foo(1)=%d should be %d\n", foo(1), bar(1))
e4e640
-  printf("foo(8)=%d should be %d\n", foo(8), bar(8))
e4e640
-  printf("foo(15)=%d should be %d\n", foo(15), bar(15))
e4e640
-  exit()
e4e640
+  printf("U foo(1)=%d should be %d\n", foo(1), bar(1))
e4e640
+  printf("U foo(8)=%d should be %d\n", foo(8), bar(8))
e4e640
+  printf("U foo(15)=%d should be %d\n", foo(15), bar(15))
e4e640
 }
e4e640
 
e4e640
+probe kernel.function("vfs_read") {
e4e640
+  printf("K foo(1)=%d should be %d\n", foo(1), bar(1))
e4e640
+  printf("K foo(8)=%d should be %d\n", foo(8), bar(8))
e4e640
+  printf("K foo(15)=%d should be %d\n", foo(15), bar(15))
e4e640
+  exit()
e4e640
+}
e4e640
diff --git a/testsuite/systemtap.bpf/asm_tests/err-printf_args.stp b/testsuite/systemtap.bpf/asm_tests/err-printf_args.stp
e4e640
new file mode 100644
e4e640
index 000000000..eb4adc00f
e4e640
--- /dev/null
e4e640
+++ b/testsuite/systemtap.bpf/asm_tests/err-printf_args.stp
e4e640
@@ -0,0 +1,9 @@
e4e640
+function foo:long (x:long) %{ /* bpf */
e4e640
+  call, -, printf, "x = 0x%p causing exit\n", $x, $x, $x, $x;
e4e640
+  0xb7, $$, -, -, 0x0; /* mov $$, 0 */
e4e640
+%}
e4e640
+
e4e640
+probe begin {
e4e640
+  printf("U x = 10 should print:\n"); foo(10)
e4e640
+  exit()
e4e640
+}
e4e640
diff --git a/testsuite/systemtap.bpf/asm_tests/err_numeric.stp b/testsuite/systemtap.bpf/asm_tests/err_numeric.stp
e4e640
index 9428e5704..ed82b32a1 100644
e4e640
--- a/testsuite/systemtap.bpf/asm_tests/err_numeric.stp
e4e640
+++ b/testsuite/systemtap.bpf/asm_tests/err_numeric.stp
e4e640
@@ -15,4 +15,3 @@ probe begin {
e4e640
   printf("foo(15)=%d should be %d\n", foo(15), bar(15))
e4e640
   exit()
e4e640
 }
e4e640
-
e4e640
diff --git a/testsuite/systemtap.bpf/asm_tests/leniency.stp b/testsuite/systemtap.bpf/asm_tests/leniency.stp
e4e640
index 939061158..1db1ec7fd 100644
e4e640
--- a/testsuite/systemtap.bpf/asm_tests/leniency.stp
e4e640
+++ b/testsuite/systemtap.bpf/asm_tests/leniency.stp
e4e640
@@ -9,9 +9,17 @@ function foo:long (x:long) %{ /* bpf */ /* pure */
e4e640
   label, _done;
e4e640
 %}
e4e640
 
e4e640
+function bar:long (x:long) {
e4e640
+  if (x < 10) return 17 else return 16
e4e640
+}
e4e640
+
e4e640
 probe begin {
e4e640
-  printf("foo(1)=%d\n", foo(1))
e4e640
-  printf("foo(15)=%d\n", foo(15))
e4e640
-  exit()
e4e640
+  printf("U foo(1)=%d should be %d\n", foo(1), bar(1))
e4e640
+  printf("U foo(15)=%d should be %d\n", foo(15), bar(15))
e4e640
 }
e4e640
 
e4e640
+probe kernel.function("vfs_read") {
e4e640
+  printf("K foo(1)=%d should be %d\n", foo(1), bar(1))
e4e640
+  printf("K foo(15)=%d should be %d\n", foo(15), bar(15))
e4e640
+  exit()
e4e640
+}
e4e640
diff --git a/testsuite/systemtap.bpf/asm_tests/printf-basic.stp b/testsuite/systemtap.bpf/asm_tests/printf-basic.stp
e4e640
new file mode 100644
e4e640
index 000000000..ffa8e01eb
e4e640
--- /dev/null
e4e640
+++ b/testsuite/systemtap.bpf/asm_tests/printf-basic.stp
e4e640
@@ -0,0 +1,9 @@
e4e640
+function foo:long (x:long) %{ /* bpf */
e4e640
+  call, -, printf, "x = %p \n", $x;
e4e640
+  0xb7, $$, -, -, 0x0; /* mov $$, 0 */
e4e640
+%}
e4e640
+
e4e640
+probe begin {
e4e640
+  printf("U 'x = 0xa' should print:\n"); foo(10)
e4e640
+  exit()
e4e640
+}
e4e640
diff --git a/testsuite/systemtap.bpf/asm_tests/printf.stp b/testsuite/systemtap.bpf/asm_tests/printf.stp
e4e640
new file mode 100644
e4e640
index 000000000..4bfa34648
e4e640
--- /dev/null
e4e640
+++ b/testsuite/systemtap.bpf/asm_tests/printf.stp
e4e640
@@ -0,0 +1,16 @@
e4e640
+function foo:long (x:long) %{ /* bpf */ /* calls:exit */
e4e640
+  0xb5, $x, -, _skip, 20; /* jle n, 20, _skip */
e4e640
+  call, -, printf, "x = %d causing exit\n", $x; /* like error() */
e4e640
+  call, -, exit;
e4e640
+  label, _skip;
e4e640
+  call, -, printf, "x = %d not causing exit\n", $x;
e4e640
+  0xb7, $$, -, -, 0x0; /* mov $$, 0 */
e4e640
+%}
e4e640
+
e4e640
+probe begin {
e4e640
+  printf("U 'x = 10' should print:\n"); foo(10)
e4e640
+}
e4e640
+
e4e640
+probe kernel.function("vfs_read") {
e4e640
+  printf("K 'x = 25' should print:\n"); foo(25)
e4e640
+}
e4e640
diff --git a/testsuite/systemtap.bpf/asm_tests/simple.stp b/testsuite/systemtap.bpf/asm_tests/simple.stp
e4e640
index 17184a139..f8dd693c5 100644
e4e640
--- a/testsuite/systemtap.bpf/asm_tests/simple.stp
e4e640
+++ b/testsuite/systemtap.bpf/asm_tests/simple.stp
e4e640
@@ -5,7 +5,12 @@ function foo:long (x:long) %{ /* bpf */ /* pure */
e4e640
 %}
e4e640
 
e4e640
 probe begin {
e4e640
-  printf("foo(1)=%d, should be 99\n", foo(1))
e4e640
-  printf("foo(15)=%d, should be 85\n", foo(15))
e4e640
+  printf("U foo(1)=%d, should be 99\n", foo(1))
e4e640
+  printf("U foo(15)=%d, should be 85\n", foo(15))
e4e640
+}
e4e640
+
e4e640
+probe kernel.function("vfs_read") {
e4e640
+  printf("K foo(1)=%d, should be 99\n", foo(1))
e4e640
+  printf("K foo(15)=%d, should be 85\n", foo(15))
e4e640
   exit()
e4e640
 }
e4e640
diff --git a/testsuite/systemtap.bpf/asm_tests/string.stp b/testsuite/systemtap.bpf/asm_tests/string.stp
e4e640
index dce665c14..6ecbe08da 100644
e4e640
--- a/testsuite/systemtap.bpf/asm_tests/string.stp
e4e640
+++ b/testsuite/systemtap.bpf/asm_tests/string.stp
e4e640
@@ -1,4 +1,4 @@
e4e640
-function foo:long (x:long) %{ /* bpf */ /* pure */
e4e640
+function foo:string (x:long) %{ /* bpf */ /* pure */
e4e640
   /* if x <= 10 then "fifty" else "one-hundred" */
e4e640
   0xd5, $x, -, _bar, 10; /* jsle $x, 10, _bar */
e4e640
   0xbf, $$, "one-hundred", -, -; /* mov $$, "one-hundred" */
e4e640
@@ -9,14 +9,23 @@ function foo:long (x:long) %{ /* bpf */ /* pure */
e4e640
   /* 0xbf, $$, $$, -, -; /* dummy op */
e4e640
 %}
e4e640
 
e4e640
-function bar:long (x:long) {
e4e640
-  if (x <= 10) return 50 else return 100
e4e640
+function bar:string (x:long) {
e4e640
+  if (x <= 10) return "fifty" else return "one-hundred"
e4e640
 }
e4e640
 
e4e640
 probe begin {
e4e640
-  printf("foo(1)=%d should be %d\n", foo(1), bar(1))
e4e640
-  printf("foo(8)=%d should be %d\n", foo(8), bar(8))
e4e640
-  printf("foo(15)=%d should be %d\n", foo(15), bar(15))
e4e640
+  printf("U foo(1)=%s should be %s\n", foo(1), bar(1))
e4e640
+  printf("U foo(8)=%s should be %s\n", foo(8), bar(8))
e4e640
+  printf("U foo(15)=%s should be %s\n", foo(15), bar(15))
e4e640
+}
e4e640
+
e4e640
+probe kernel.function("vfs_read") {
e4e640
+  printf("K foo(1)=%s should be %s\n", bar(1), bar(1))
e4e640
+  printf("K foo(8)=%s should be %s\n", bar(8), bar(8))
e4e640
+  printf("K foo(15)=%s should be %s\n", bar(15), bar(15))  
e4e640
+  # printf("K foo(1)=%s should be %s\n", foo(1), bar(1))
e4e640
+  # printf("K foo(8)=%s should be %s\n", foo(8), bar(8))
e4e640
+  # printf("K foo(15)=%s should be %s\n", foo(15), bar(15))
e4e640
   exit()
e4e640
 }
e4e640
 
e4e640
diff --git a/testsuite/systemtap.bpf/asm_tests/temporary.stp b/testsuite/systemtap.bpf/asm_tests/temporary.stp
e4e640
index 153c759ba..7cec89bb3 100644
e4e640
--- a/testsuite/systemtap.bpf/asm_tests/temporary.stp
e4e640
+++ b/testsuite/systemtap.bpf/asm_tests/temporary.stp
e4e640
@@ -8,7 +8,12 @@ function foo:long (x:long) %{ /* bpf */ /* pure */
e4e640
 %}
e4e640
 
e4e640
 probe begin {
e4e640
-  printf("foo(1)=%d, should be 99*3=297\n", foo(1))
e4e640
-  printf("foo(15)=%d, should be 85*18=1530\n", foo(15))
e4e640
+  printf("U foo(1)=%d, should be 99*3=297\n", foo(1))
e4e640
+  printf("U foo(15)=%d, should be 85*18=1530\n", foo(15))
e4e640
+}
e4e640
+
e4e640
+probe kernel.function("vfs_read") {
e4e640
+  printf("K foo(1)=%d, should be 99*3=297\n", foo(1))
e4e640
+  printf("K foo(15)=%d, should be 85*18=1530\n", foo(15))
e4e640
   exit()
e4e640
 }
e4e640
diff --git a/testsuite/systemtap.bpf/asm_tests/unreachable.stp b/testsuite/systemtap.bpf/asm_tests/unreachable.stp
e4e640
new file mode 100644
e4e640
index 000000000..b629fd5cb
e4e640
--- /dev/null
e4e640
+++ b/testsuite/systemtap.bpf/asm_tests/unreachable.stp
e4e640
@@ -0,0 +1,17 @@
e4e640
+/* testcase for an early bug that would generate duplicate jump */
e4e640
+function foo:long (x:long) %{ /* bpf */
e4e640
+  /* the code in the middle is unreachable */
e4e640
+  0xd5, $x, -, _bar, 20; /* jsle $x, 20, _done */
e4e640
+  0xb7, $$, -, -, 100; /* mov $$, 100 */
e4e640
+  0x05, -, -, _done, -;
e4e640
+  label, _bar;
e4e640
+  0xb7, $$, -, -, 50; /* mov $$, 50 */
e4e640
+  label, _done;
e4e640
+%}
e4e640
+
e4e640
+probe kernel.function("vfs_read") {
e4e640
+  x = 10
e4e640
+  if (x > 12) printf("unreachable\n")
e4e640
+  printf ("got %d (should be 50)\n", foo(x))
e4e640
+  exit()
e4e640
+}
e4e640
diff --git a/testsuite/systemtap.bpf/bpf-asm.exp b/testsuite/systemtap.bpf/bpf-asm.exp
e4e640
new file mode 100644
e4e640
index 000000000..a53c7bcc4
e4e640
--- /dev/null
e4e640
+++ b/testsuite/systemtap.bpf/bpf-asm.exp
e4e640
@@ -0,0 +1,105 @@
e4e640
+# bpf-asm.exp
e4e640
+#
e4e640
+# TODO: Very basic test driver. Need to work out a way of signaling correctness.
e4e640
+
e4e640
+set testdir "$srcdir/$subdir/asm_tests"
e4e640
+
e4e640
+proc stapbpf_run { TEST_NAME args } {
e4e640
+    global rc
e4e640
+    set rc -1
e4e640
+
e4e640
+    # return codes
e4e640
+    set pass 0
e4e640
+    #set fail 1
e4e640
+    #set bad_output 2
e4e640
+    set eof_start 3
e4e640
+    set eof_end 4
e4e640
+    set timeout_start 5
e4e640
+    set timeout_end 6
e4e640
+    set invalid_prog 7
e4e640
+    set comp_err 8
e4e640
+
e4e640
+    set cmd [concat stap -vg --runtime=bpf $args]
e4e640
+    send_log "executing: $cmd\n"
e4e640
+    eval spawn $cmd
e4e640
+    set mypid [exp_pid -i $spawn_id]
e4e640
+    expect {
e4e640
+        -timeout 30
e4e640
+        -re {Pass 5: starting run} {
e4e640
+            expect {
e4e640
+                -timeout 20
e4e640
+                -re {Pass 5: run completed} {
e4e640
+                    set rc $pass
e4e640
+                }
e4e640
+                -re "bpf program load failed:" { set rc $invalid_prog }
e4e640
+                default {
e4e640
+                    set rc $bad_output
e4e640
+                }
e4e640
+                timeout {
e4e640
+                    set rc $timeout_end
e4e640
+                    kill -INT -$mypid
e4e640
+                }
e4e640
+                eof {
e4e640
+                    set rc $eof_end
e4e640
+                }
e4e640
+            }
e4e640
+        }
e4e640
+        -re "semantic error:" { set rc $comp_err }
e4e640
+        -re "bpf program load failed:" { set rc $invalid_prog }
e4e640
+        timeout {
e4e640
+            set rc $timeout_start
e4e640
+            kill -INT -$mypid
e4e640
+        }
e4e640
+        eof {
e4e640
+            set rc $eof_start
e4e640
+        }
e4e640
+    }
e4e640
+    # again for good measure with KILL after 3s
e4e640
+    kill -INT -$mypid 3
e4e640
+    catch close
e4e640
+    wait
e4e640
+    return $rc
e4e640
+}
e4e640
+
e4e640
+set stap_files [lsort [glob -nocomplain $testdir/*.stp]]
e4e640
+
e4e640
+foreach file $stap_files {
e4e640
+    global mypid
e4e640
+    set mypid 0
e4e640
+    set test [file tail $file]
e4e640
+    if {! [installtest_p]} { untested $test; continue }
e4e640
+    if {! [bpf_p]} { untested $test; continue }
e4e640
+
e4e640
+    set errtest_p [regexp {^err} [file tail $file]]
e4e640
+    verbose -log "Running $file"
e4e640
+    switch [stapbpf_run $test $file] {
e4e640
+        0 {
e4e640
+            if $errtest_p {
e4e640
+                fail "$test unexpected success"
e4e640
+            } else {
e4e640
+                pass $test
e4e640
+            }
e4e640
+        }
e4e640
+        1 { fail "$test incorrect result" }
e4e640
+        2 { fail "$test unexpected output" }
e4e640
+        3 { fail "$test eof (startup)" }
e4e640
+        4 { fail "$test eof (shutdown)" }
e4e640
+        5 { fail "$test timeout (startup)" }
e4e640
+        6 { fail "$test timeout (startup)" }
e4e640
+        7 { fail "$test invalid bpf program" }
e4e640
+        8 {
e4e640
+            if $errtest_p {
e4e640
+                pass $test
e4e640
+            } else {
e4e640
+                fail "$test compilation"
e4e640
+            }
e4e640
+        }
e4e640
+        default { fail "$test unknown return value" }
e4e640
+    }
e4e640
+
e4e640
+    if { $mypid > 0 } {
e4e640
+        kill -INT -$mypid 3
e4e640
+        catch close
e4e640
+        wait
e4e640
+    }
e4e640
+}
e4e640
-- 
e4e640
2.14.5
e4e640