Follow @Openwall on Twitter for new release announcements and other news
[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <20261018003525.GX23438@brightrain.aerifal.cx>
Date: Sat, 17 Oct 2026 20:35:25 -0400
From: Rich Felker <dalias@...ifal.cx>
To: Alex Rønne Petersen <alex@...xrp.com>
Cc: musl@...ts.openwall.com
Subject: Re: [PATCH v2] riscv: declare all vector registers as
 clobbers of syscalls

On Sat, Oct 17, 2026 at 03:11:13PM +0200, Alex Rønne Petersen wrote:
> The kernel intentionally clobbers vector registers (by setting them to
> all 1s). musl didn't declare this, so with a compiler targeting the V
> extension, this could lead to all sorts of breakage that at first
> glance looks like miscompilations.
> 
> Because the history of vector clobber support in compilers is messy,
> we detect support for them at configure time. If the clobbers we need
> are not supported, we instead declare the syscall functions noinline
> and rely on the calling convention to mark the registers clobbered.
> 
> Co-authored-by: Rich Felker <dalias@...ifal.cx>
> ---
>  arch/riscv32/syscall_arch.h | 34 ++++++++++++++++++++++++++--------
>  arch/riscv64/syscall_arch.h | 34 ++++++++++++++++++++++++++--------
>  configure                   |  8 ++++++++
>  3 files changed, 60 insertions(+), 16 deletions(-)
> 
> diff --git a/arch/riscv32/syscall_arch.h b/arch/riscv32/syscall_arch.h
> index 70d2773f..2baa6e65 100644
> --- a/arch/riscv32/syscall_arch.h
> +++ b/arch/riscv32/syscall_arch.h
> @@ -3,26 +3,44 @@
>  ((union { long long ll; long l[2]; }){ .ll = x }).l[1]
>  #define __SYSCALL_LL_O(x) __SYSCALL_LL_E((x))
>  
> +#ifndef MISSING_RVV_CLOBBERS
> +#define SYSCALL_CLOBBERLIST "memory", \
> +	"v0",  "v1",  "v2",  "v3",  "v4",  "v5",  "v6",  "v7", \
> +	"v8",  "v9",  "v10", "v11", "v12", "v13", "v14", "v15", \
> +	"v16", "v17", "v18", "v19", "v20", "v21", "v22", "v23", \
> +	"v24", "v25", "v26", "v27", "v28", "v29", "v30", "v31", \
> +	"vl", "vtype", "vxsat", "vxrm"
> +#else
> +#define SYSCALL_CLOBBERLIST "memory"
> +#if defined(__riscv_v) || defined(__riscv_vector)
> +#define SYSCALL_FUNCTION_ATTRS __attribute__((__noinline__, __unused__)) static
> +#endif
> +#endif
> +
> +#ifndef SYSCALL_FUNCTION_ATTRS
> +#define SYSCALL_FUNCTION_ATTRS static inline
> +#endif
> +
>  #define __asm_syscall(...) \
>  	__asm__ __volatile__ ("ecall\n\t" \
> -	: "=r"(a0) : __VA_ARGS__ : "memory"); \
> +	: "=r"(a0) : __VA_ARGS__ : SYSCALL_CLOBBERLIST); \
>  	return a0; \
>  
> -static inline long __syscall0(long n)
> +SYSCALL_FUNCTION_ATTRS long __syscall0(long n)

Could you keep the static out of the macro so that it's clear to
someone reading it that it's there without looking up how the macro
gets defined?

-static inline long __syscall0(long n)
+SYSCALL_FUNCTION_ATTRS
+static long __syscall0(long n)

I'm guessing inline clashed (or you were worried a compiler might
treat it as clashing?) with the noinline attribute, so moving it out
like you did is fine if needed.

> diff --git a/configure b/configure
> index bc9fbe48..e5377589 100755
> --- a/configure
> +++ b/configure
> @@ -731,6 +731,14 @@ fi
>  if test "$ARCH" = "riscv64" -o "$ARCH" = "riscv32" ; then
>  trycppif __riscv_float_abi_soft "$t" && SUBARCH=${SUBARCH}-sf
>  trycppif __riscv_float_abi_single "$t" && SUBARCH=${SUBARCH}-sp
> +printf "checking whether compiler supports vector clobbers... "
> +echo 'void f(void) { __asm__("":::"v0","vl","vtype","vxsat","vxrm","memory"); }' > "$tmpc"
> +if $CC $CFLAGS_C99FSE $CPPFLAGS $CFLAGS -c -o /dev/null "$tmpc" >/dev/null 2>&1 ; then
> +printf "yes\n"
> +else
> +printf "no\n"
> +CFLAGS_AUTO="$CFLAGS_AUTO -DMISSING_RVV_CLOBBERS"
> +fi
>  fi

This looks like an ok test program to compile to check for the
clobbers.

I just noticed you used -o in the test(1) expression, which was
previously obsolescent and now removed in POSIX-2024, but we already
did the same elsewhere for rv32/64 so I think it's ok. If we want to
fix that it can be a separate fix.

Rich

Powered by blists - more mailing lists

Confused about mailing lists and their use? Read about mailing lists on Wikipedia and check out these guidelines on proper formatting of your messages.