[libc-commits] [PATCH] D146981: [libc] Fix longjmp's fake return in x86_64

Mikhail Ramalho via Phabricator via libc-commits libc-commits at lists.llvm.org
Mon Mar 27 13:16:34 PDT 2023


mikhail.ramalho updated this revision to Diff 508774.
mikhail.ramalho added a comment.

Fix variable name


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D146981/new/

https://reviews.llvm.org/D146981

Files:
  libc/src/setjmp/x86_64/longjmp.cpp
  libc/test/src/setjmp/setjmp_test.cpp


Index: libc/test/src/setjmp/setjmp_test.cpp
===================================================================
--- libc/test/src/setjmp/setjmp_test.cpp
+++ libc/test/src/setjmp/setjmp_test.cpp
@@ -10,21 +10,38 @@
 #include "src/setjmp/setjmp_impl.h"
 #include "test/UnitTest/Test.h"
 
-jmp_buf buf;
 constexpr int MAX_LOOP = 123;
+int longjmp_called = 0;
 
-void jump_back(int n) {
+void jump_back(jmp_buf buf, int n) {
+  longjmp_called++;
   __llvm_libc::longjmp(buf, n); // Will return |n| out of setjmp
 }
 
 TEST(LlvmLibcSetJmpTest, SetAndJumpBack) {
+  jmp_buf buf;
+  longjmp_called = 0;
+
   // Local variables in setjmp scope should be declared volatile.
   volatile int n = 0;
   // The first time setjmp is called, it should return 0.
   // Subsequent calls will return the value passed to jump_back below.
   if (__llvm_libc::setjmp(buf) <= MAX_LOOP) {
     ++n;
-    jump_back(n);
+    jump_back(buf, n);
   }
+  ASSERT_EQ(longjmp_called, n);
   ASSERT_EQ(n, MAX_LOOP + 1);
 }
+
+TEST(LlvmLibcSetJmpTest, SetAndJumpBackValOne) {
+  jmp_buf buf;
+  longjmp_called = 0;
+
+  int val = __llvm_libc::setjmp(buf);
+  if (val == 0)
+    jump_back(buf, val);
+
+  ASSERT_EQ(longjmp_called, 1);
+  ASSERT_EQ(val, 1);
+}
Index: libc/src/setjmp/x86_64/longjmp.cpp
===================================================================
--- libc/src/setjmp/x86_64/longjmp.cpp
+++ libc/src/setjmp/x86_64/longjmp.cpp
@@ -29,6 +29,7 @@
   // |val| in rax. Note that this has to happen before we restore the registers
   // from values in |buf|. Otherwise, once rsp and rbp are updated, we cannot
   // read |val|.
+  val = val == 0 ? 1 : val;
   LIBC_INLINE_ASM("mov %1, %0\n\t" : "=r"(rax) : "m"(val) :);
   LIBC_INLINE_ASM("mov %1, %0\n\t" : "=r"(rbx) : "m"(buf->rbx) :);
   LIBC_INLINE_ASM("mov %1, %0\n\t" : "=r"(rbp) : "m"(buf->rbp) :);


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D146981.508774.patch
Type: text/x-patch
Size: 1838 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/libc-commits/attachments/20230327/86e154b6/attachment.bin>


More information about the libc-commits mailing list