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

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