This patch adds new DejaGnu target board for BPF that enables execution testing
of BPF programs using the `bpf-vmtest-tool` script in the contrib directory.

Signed-off-by: Piyush Raj <piyushraj92...@gmail.com>
---
 gcc/Makefile.in                               |  1 +
 gcc/testsuite/boards/bpf.exp                  | 63 +++++++++++++++++++
 .../gcc.target/bpf/bpf-testool-demo.c         | 25 ++++++++
 3 files changed, 89 insertions(+)
 create mode 100644 gcc/testsuite/boards/bpf.exp
 create mode 100644 gcc/testsuite/gcc.target/bpf/bpf-testool-demo.c

diff --git a/gcc/Makefile.in b/gcc/Makefile.in
index 56fa5ac25ff..e3c4e6e14a3 100644
--- a/gcc/Makefile.in
+++ b/gcc/Makefile.in
@@ -4562,6 +4562,7 @@ site.exp: ./config.status Makefile
        fi
        echo "set tmpdir $(objdir)/testsuite" >> ./site.tmp
        @echo "set srcdir \"\$${srcdir}/testsuite\"" >> ./site.tmp
+       @echo "lappend boards_dir \"\$${srcdir}/boards\"" >> ./site.tmp
        @if [ "X$(ALT_CC_UNDER_TEST)" != "X" ] ; then \
          echo "set ALT_CC_UNDER_TEST \"$(ALT_CC_UNDER_TEST)\"" >> ./site.tmp; \
        else true; \
diff --git a/gcc/testsuite/boards/bpf.exp b/gcc/testsuite/boards/bpf.exp
new file mode 100644
index 00000000000..ed99d4a0b83
--- /dev/null
+++ b/gcc/testsuite/boards/bpf.exp
@@ -0,0 +1,63 @@
+load_lib "standard.exp"
+set_board_info target_install {bpf-unknown-none}
+set_board_info generic_name bpf
+set_board_info mathlib ""
+unset_board_info isremote
+set_board_info isremote "0"
+
+set BPF_GCC_FLAGS "-D__TARGET_ARCH_x86_64 -gbtf -std=gnu17 
-I/usr/local/include -I/usr/include"
+proc bpf_compile {source destfile type options} {
+    global BPF_GCC_FLAGS
+
+    if { $type == "executable" } {
+        # We don't actually link now; we just need the object file
+        set type "object"
+        lappend options "additional_flags=$BPF_GCC_FLAGS"
+        puts $options
+        return [default_target_compile $source $destfile $type $options]
+    }
+    return [default_target_compile $source $destfile $type $options]
+}
+
+proc bpf_load {dest prog args} {
+    global srcdir
+    # Construct command
+    set python3 [find_python3]
+    if {$python3 == ""} {
+        error "Cannot find python3 in $PATH"
+    }
+    set script_path "$srcdir/../../contrib/bpf-vmtest-tool/main.py"
+    set bpf_object $prog
+
+    set args [list $script_path -k 6.15 --bpf-obj $prog]
+    set status [remote_exec $dest $python3 $args]
+    set exit_code [lindex $status 0]
+    set output [lindex $status 1]
+
+    if { $exit_code < 0 } {
+        verbose -log "Couldn't execute $prog: $output"
+        return "unresolved"
+    }
+
+    verbose -log "Executed $prog, exit_code $exit_code" 
+    if {$output ne ""} {
+        verbose -log -- $output 2
+    }
+
+    if { $exit_code == 0 } {
+        return [list "pass" $output]
+    } else {
+        return [list "fail" $output]
+    }
+}
+
+
+proc find_python3 {} {
+    foreach candidate {python3 python} {
+        set path [which $candidate]
+        if {$path != ""} {
+            return $path
+        }
+    }
+    return ""
+}
\ No newline at end of file
diff --git a/gcc/testsuite/gcc.target/bpf/bpf-testool-demo.c 
b/gcc/testsuite/gcc.target/bpf/bpf-testool-demo.c
new file mode 100644
index 00000000000..8d3d0c56909
--- /dev/null
+++ b/gcc/testsuite/gcc.target/bpf/bpf-testool-demo.c
@@ -0,0 +1,25 @@
+// { dg-do run  }
+// how to do tourture testing ?
+/* { dg-options "-O2" }*/
+
+#include "vmlinux.h"
+#include <bpf/bpf_helpers.h>
+#include <bpf/bpf_tracing.h>
+#include <bpf/bpf_core_read.h>
+
+char LICENSE[] SEC("license") = "GPL";
+
+int example_pid = 0;
+
+SEC("tracepoint/syscalls/sys_enter_openat")
+int handle_openat(struct trace_event_raw_sys_enter *ctx)
+{
+       int pid = bpf_get_current_pid_tgid() >> 32;
+       char filename[256]; // filename buffer
+       bpf_probe_read_user(&filename, sizeof(filename), (void *)ctx->args[1]);
+       bpf_printk("sys_enter_openat() called from PID %d for file: %s\n", pid, 
filename);
+
+       return 0;
+}
+
+/* { dg-output "BPF programs succesfully loaded" } */
-- 
2.50.1

Reply via email to