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

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