From 05b586be3d70cd51c809c52a67d36517fb4b8f6f Mon Sep 17 00:00:00 2001 From: Paul Kirth Date: Tue, 23 Jul 2024 10:42:55 -0700 Subject: [PATCH] [libc][RISCV] Add naked attribute to setjmp/longjmp (#100036) We want to avoid any possibility that the compiler will insert a prologue/epilogue violating the calling contracts for these special functions, potentially clobbering registers that must be preserved. To do that they should be marked naked, as is already the case on ARM. See #87837 for further context. --- libc/src/setjmp/riscv/longjmp.cpp | 1 + libc/src/setjmp/riscv/setjmp.cpp | 1 + 2 files changed, 2 insertions(+) diff --git a/libc/src/setjmp/riscv/longjmp.cpp b/libc/src/setjmp/riscv/longjmp.cpp index 0f9537ccc41510c..b14f636659ac320 100644 --- a/libc/src/setjmp/riscv/longjmp.cpp +++ b/libc/src/setjmp/riscv/longjmp.cpp @@ -30,6 +30,7 @@ namespace LIBC_NAMESPACE_DECL { +[[gnu::naked]] LLVM_LIBC_FUNCTION(void, longjmp, (__jmp_buf * buf, int val)) { LOAD(ra, buf->__pc); LOAD(s0, buf->__regs[0]); diff --git a/libc/src/setjmp/riscv/setjmp.cpp b/libc/src/setjmp/riscv/setjmp.cpp index 12def578b56f347..92982cc9d74d42e 100644 --- a/libc/src/setjmp/riscv/setjmp.cpp +++ b/libc/src/setjmp/riscv/setjmp.cpp @@ -29,6 +29,7 @@ namespace LIBC_NAMESPACE_DECL { +[[gnu::naked]] LLVM_LIBC_FUNCTION(int, setjmp, (__jmp_buf * buf)) { STORE(ra, buf->__pc); STORE(s0, buf->__regs[0]);