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-vmtest 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.
--- /dev/null
+# 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
--- /dev/null
+// { 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;
+}
+
--- /dev/null
+/* Ensure memcpy is expanded inline rather than emitting a libcall. */
+
+/* { dg-do run } */
+
+#include "../memcpy-1.c"
--- /dev/null
+/* Ensure memmove is expanded inline rather than emitting a libcall. */
+
+/* { dg-do run } */
+
+#include "../memmove-1.c"
--- /dev/null
+/* Ensure memset is expanded inline rather than emitting a libcall. */
+
+/* { dg-do run } */
+
+#include "../memset-1.c"
--- /dev/null
+/* { dg-do run } */
+
+#include "../naked-2.c"
--- /dev/null
+/* { dg-do run } */
+/* { dg-options "--patchable-function-entry=2,1 -masm=normal" } */
+
+#include "../nop-1.c"
--- /dev/null
+// { 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" } */