This patch adds runtime tests for BPF target.
These tests are based on existing compile-time tests and depends on
bpf.exp baseboard
To run the testsuite:

  make check-gcc RUNTESTFLAGS="--target_board=bpf bpf-torture.exp 
KERNEL_VERSION=6.15 LOG_LEVEL=ERROR"

Options:
        KERNEL_VERSION (default: 6.15)
        LOG_LEVEL (default: ERROR)
Note: VMTEST_DIR must be set in the environment.

See the bpf-vmtest-tool README for details.

gcc/testsuite/ChangeLog:

        * gcc.target/bpf/torture/bpf-torture.exp: New testsuite.
        * gcc.target/bpf/torture/invalid-memory-access.c: New test.
        * gcc.target/bpf/torture/memcpy.c: New test.
        * gcc.target/bpf/torture/memmove.c: New test.
        * gcc.target/bpf/torture/memset.c: New test.
        * gcc.target/bpf/torture/naked.c: New test.
        * gcc.target/bpf/torture/nop.c: New test.
        * gcc.target/bpf/torture/trace_openat.c: New test.
---
 .../gcc.target/bpf/torture/bpf-torture.exp    | 46 +++++++++++++++++++
 .../bpf/torture/invalid-memory-access.c       | 19 ++++++++
 gcc/testsuite/gcc.target/bpf/torture/memcpy.c |  5 ++
 .../gcc.target/bpf/torture/memmove.c          |  5 ++
 gcc/testsuite/gcc.target/bpf/torture/memset.c |  5 ++
 gcc/testsuite/gcc.target/bpf/torture/naked.c  |  3 ++
 gcc/testsuite/gcc.target/bpf/torture/nop.c    |  4 ++
 .../gcc.target/bpf/torture/trace_openat.c     | 24 ++++++++++
 8 files changed, 111 insertions(+)
 create mode 100644 gcc/testsuite/gcc.target/bpf/torture/bpf-torture.exp
 create mode 100644 gcc/testsuite/gcc.target/bpf/torture/invalid-memory-access.c
 create mode 100644 gcc/testsuite/gcc.target/bpf/torture/memcpy.c
 create mode 100644 gcc/testsuite/gcc.target/bpf/torture/memmove.c
 create mode 100644 gcc/testsuite/gcc.target/bpf/torture/memset.c
 create mode 100644 gcc/testsuite/gcc.target/bpf/torture/naked.c
 create mode 100644 gcc/testsuite/gcc.target/bpf/torture/nop.c
 create mode 100644 gcc/testsuite/gcc.target/bpf/torture/trace_openat.c

diff --git a/gcc/testsuite/gcc.target/bpf/torture/bpf-torture.exp 
b/gcc/testsuite/gcc.target/bpf/torture/bpf-torture.exp
new file mode 100644
index 00000000000..ad5fa58fdc3
--- /dev/null
+++ b/gcc/testsuite/gcc.target/bpf/torture/bpf-torture.exp
@@ -0,0 +1,46 @@
+# Copyright (C) 2019-2025 Free Software Foundation, Inc.
+
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with GCC; see the file COPYING3.  If not see
+# <http://www.gnu.org/licenses/>.
+
+# GCC testsuite that uses the `dg.exp' driver.
+
+# Exit immediately if this isn't an eBPF target.
+if ![istarget bpf-*-*] then {
+  return
+}
+
+# Default values for torture testing
+set TORTURE_OPTIONS [ list {-O1 -g} {-O2 -g} {-O3 -g} ]
+set LTO_TORTURE_OPTIONS ""
+
+# Load support procs.
+load_lib gcc-dg.exp
+
+# If a testcase doesn't have special options, use these.
+global DEFAULT_CFLAGS
+if ![info exists DEFAULT_CFLAGS] then {
+    set DEFAULT_CFLAGS "-Wall -Werror"
+}
+
+# Initialize `dg'.
+dg-init
+
+
+# Main loop.
+gcc-dg-runtest [lsort [glob -nocomplain $srcdir/$subdir/*.\[cS\]]] \
+       "" $DEFAULT_CFLAGS
+
+# All done.
+dg-finish
diff --git a/gcc/testsuite/gcc.target/bpf/torture/invalid-memory-access.c 
b/gcc/testsuite/gcc.target/bpf/torture/invalid-memory-access.c
new file mode 100644
index 00000000000..f50adc86e72
--- /dev/null
+++ b/gcc/testsuite/gcc.target/bpf/torture/invalid-memory-access.c
@@ -0,0 +1,19 @@
+// { dg-do run { xfail *-*-* } }
+// { dg-options -Wall }
+
+#include "vmlinux.h"
+#include <bpf/bpf_helpers.h>
+#include <bpf/bpf_tracing.h>
+
+char LICENSE[] SEC("license") = "GPL";
+
+SEC("tracepoint/syscalls/sys_enter_openat")
+int bpf_prog(struct trace_event_raw_sys_enter *ctx) {
+    int arr[4] = {1, 2, 3, 4};
+
+    // Invalid memory access: out-of-bounds
+    int val = arr[5];  // This causes the verifier to fail
+
+    return val;
+}
+
diff --git a/gcc/testsuite/gcc.target/bpf/torture/memcpy.c 
b/gcc/testsuite/gcc.target/bpf/torture/memcpy.c
new file mode 100644
index 00000000000..b58c8729439
--- /dev/null
+++ b/gcc/testsuite/gcc.target/bpf/torture/memcpy.c
@@ -0,0 +1,5 @@
+/* Ensure memcpy is expanded inline rather than emitting a libcall.  */
+
+/* { dg-do run } */
+
+#include "../memcpy-1.c"
diff --git a/gcc/testsuite/gcc.target/bpf/torture/memmove.c 
b/gcc/testsuite/gcc.target/bpf/torture/memmove.c
new file mode 100644
index 00000000000..2ac9abcf9ef
--- /dev/null
+++ b/gcc/testsuite/gcc.target/bpf/torture/memmove.c
@@ -0,0 +1,5 @@
+/* Ensure memmove is expanded inline rather than emitting a libcall.  */
+
+/* { dg-do run } */
+
+#include "../memmove-1.c"
diff --git a/gcc/testsuite/gcc.target/bpf/torture/memset.c 
b/gcc/testsuite/gcc.target/bpf/torture/memset.c
new file mode 100644
index 00000000000..041cb457827
--- /dev/null
+++ b/gcc/testsuite/gcc.target/bpf/torture/memset.c
@@ -0,0 +1,5 @@
+/* Ensure memset is expanded inline rather than emitting a libcall. */
+
+/* { dg-do run } */
+
+#include "../memset-1.c"
diff --git a/gcc/testsuite/gcc.target/bpf/torture/naked.c 
b/gcc/testsuite/gcc.target/bpf/torture/naked.c
new file mode 100644
index 00000000000..802201d6e24
--- /dev/null
+++ b/gcc/testsuite/gcc.target/bpf/torture/naked.c
@@ -0,0 +1,3 @@
+/* { dg-do run } */
+
+#include "../naked-2.c"
diff --git a/gcc/testsuite/gcc.target/bpf/torture/nop.c 
b/gcc/testsuite/gcc.target/bpf/torture/nop.c
new file mode 100644
index 00000000000..3e4f81b6754
--- /dev/null
+++ b/gcc/testsuite/gcc.target/bpf/torture/nop.c
@@ -0,0 +1,4 @@
+/* { dg-do run } */
+/* { dg-options "--patchable-function-entry=2,1 -masm=normal" } */
+
+#include "../nop-1.c"
diff --git a/gcc/testsuite/gcc.target/bpf/torture/trace_openat.c 
b/gcc/testsuite/gcc.target/bpf/torture/trace_openat.c
new file mode 100644
index 00000000000..ced1892f6ef
--- /dev/null
+++ b/gcc/testsuite/gcc.target/bpf/torture/trace_openat.c
@@ -0,0 +1,24 @@
+// { dg-do run  }
+// { dg-options -Wall }
+
+#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 successfully loaded" } */
-- 
2.52.0

Reply via email to