[PATCH v22 28/28] kselftest/riscv: kselftest for user mode cfi

Joel Stanley joel at jms.id.au
Mon Nov 3 23:12:04 PST 2025


On Fri, 24 Oct 2025 at 03:33, Deepak Gupta via B4 Relay
<devnull+debug.rivosinc.com at kernel.org> wrote:

> diff --git a/tools/testing/selftests/riscv/cfi/Makefile b/tools/testing/selftests/riscv/cfi/Makefile
> new file mode 100644
> index 000000000000..55165a93845f
> --- /dev/null
> +++ b/tools/testing/selftests/riscv/cfi/Makefile
> @@ -0,0 +1,16 @@
> +CFLAGS += -I$(top_srcdir)/tools/include
> +
> +CFLAGS += -march=rv64gc_zicfilp_zicfiss -fcf-protection=full
> +
> +ifeq ($(shell $(CC) $(CFLAGS) -nostdlib -xc /dev/null -o /dev/null > /dev/null 2>&1; echo $$?),0)
> +TEST_GEN_PROGS := cfitests|

I hacked this up a bit to get it working for me.

I added this at the top to use the local headers from a `make
headers`. Without this the selftest lacked the required ptrace
changes:

+CFLAGS += $(KHDR_INCLUDES)

I needed this before the ifeq so it used the cross compiler to perform
the test, otherwise the test was always false:

+# Check for zicfli* extensions needs cross compiler
+# which is not set until lib.mk is included
+ifeq ($(LLVM)$(CC),cc)
+CC := $(CROSS_COMPILE)gcc
+endif
+#nedif

Findally, a cosmetic one. If we set the name of the main test file to
be the same as the test, the rule from lib.mk will give us neat
output:

     make[2]: Entering directory 'tools/testing/selftests/riscv/cfi'
       CC       cfitests

--- a/tools/testing/selftests/riscv/cfi/Makefile
+++ b/tools/testing/selftests/riscv/cfi/Makefile
@@ -12,12 +12,11 @@ endif
 ifeq ($(shell $(CC) $(CFLAGS) -nostdlib -xc /dev/null -o /dev/null >
/dev/null 2>&1; echo $$?),0)
 TEST_GEN_PROGS := cfitests

-include ../../lib.mk
+$(OUTPUT)/cfitests: cfitests.c shadowstack.c

-$(OUTPUT)/cfitests: riscv_cfi_test.c shadowstack.c
-       $(CC) -o$@ $(CFLAGS) $(LDFLAGS) $^
 else
-include ../../lib.mk

 $(shell echo "Toolchain doesn't support CFI, skipping CFI kselftest." >&2)
 endif
+
+include ../../lib.mk

I can push up a branch with my changes if you'd like.



More information about the linux-riscv mailing list