[libc-commits] [libc] [libc][i386] setjmp/longjmp (PR #112437)

Nick Desaulniers via libc-commits libc-commits at lists.llvm.org
Tue Oct 15 14:05:06 PDT 2024


================
@@ -6,16 +6,44 @@
 //
 //===----------------------------------------------------------------------===//
 
-#include "src/setjmp/longjmp.h"
+#include "include/llvm-libc-macros/offsetof-macro.h"
 #include "src/__support/common.h"
 #include "src/__support/macros/config.h"
+#include "src/setjmp/longjmp.h"
 
-#if !defined(LIBC_TARGET_ARCH_IS_X86_64)
+#if !defined(LIBC_TARGET_ARCH_IS_X86)
 #error "Invalid file include"
 #endif
 
 namespace LIBC_NAMESPACE_DECL {
 
+#ifdef __i386__
+[[noreturn]]
+LLVM_LIBC_FUNCTION(void, longjmp, (jmp_buf, int val)) {
+  asm(R"(
+      mov 4(%%esp), %%edx
+
+      mov %c[ebx](%%edx), %%ebx
+      mov %c[esi](%%edx), %%esi
+      mov %c[edi](%%edx), %%edi
+      mov %c[ebp](%%edx), %%ebp
+      mov %c[esp](%%edx), %%esp
+
+      jmp *%c[eip](%%edx)
+      )"
+      ::
+      [ebx] "i"(offsetof(__jmp_buf, ebx)),
+      [esi] "i"(offsetof(__jmp_buf, esi)),
+      [edi] "i"(offsetof(__jmp_buf, edi)),
+      [ebp] "i"(offsetof(__jmp_buf, ebp)),
+      [esp] "i"(offsetof(__jmp_buf, esp)),
+      [eip] "i"(offsetof(__jmp_buf, eip)),
+      [val] "a"(val == 0 ? 1 : val) :
+      "edx");
----------------
nickdesaulniers wrote:

We can avoid a specific clobber by putting `buf` param as an input:
```diff
diff --git a/libc/src/setjmp/x86_64/longjmp.cpp b/libc/src/setjmp/x86_64/longjmp.cpp
index f27e5f055d6f..d0145e5f4fe9 100644
--- a/libc/src/setjmp/x86_64/longjmp.cpp
+++ b/libc/src/setjmp/x86_64/longjmp.cpp
@@ -19,17 +19,15 @@ namespace LIBC_NAMESPACE_DECL {
 
 #ifdef __i386__
 [[noreturn]]
-LLVM_LIBC_FUNCTION(void, longjmp, (jmp_buf, int val)) {
+LLVM_LIBC_FUNCTION(void, longjmp, (jmp_buf buf, int val)) {
   asm(R"(
-      mov 4(%%esp), %%edx
-
-      mov %c[ebx](%%edx), %%ebx
-      mov %c[esi](%%edx), %%esi
-      mov %c[edi](%%edx), %%edi
-      mov %c[ebp](%%edx), %%ebp
-      mov %c[esp](%%edx), %%esp
+      mov %c[ebx](%[buf]), %%ebx
+      mov %c[esi](%[buf]), %%esi
+      mov %c[edi](%[buf]), %%edi
+      mov %c[ebp](%[buf]), %%ebp
+      mov %c[esp](%[buf]), %%esp
 
-      jmp *%c[eip](%%edx)
+      jmp *%c[eip](%[buf])
       )"
       ::
       [ebx] "i"(offsetof(__jmp_buf, ebx)),
@@ -38,6 +36,7 @@ LLVM_LIBC_FUNCTION(void, longjmp, (jmp_buf, int val)) {
       [ebp] "i"(offsetof(__jmp_buf, ebp)),
       [esp] "i"(offsetof(__jmp_buf, esp)),
       [eip] "i"(offsetof(__jmp_buf, eip)),
+      [buf] "r"(buf),
       [val] "a"(val == 0 ? 1 : val) :
       "edx");
   __builtin_unreachable();
```

https://github.com/llvm/llvm-project/pull/112437


More information about the libc-commits mailing list