[libc-commits] [libc] [libc] Add `returns_twice` attribute to `setjmp(3)` (PR #124370)
Alan Zhao via libc-commits
libc-commits at lists.llvm.org
Mon Jan 27 11:22:41 PST 2025
https://github.com/alanzhao1 updated https://github.com/llvm/llvm-project/pull/124370
>From 5cc318716fd50e1acd685f5731d75acfb02292b7 Mon Sep 17 00:00:00 2001
From: Alan Zhao <ayzhao at google.com>
Date: Fri, 24 Jan 2025 15:49:10 -0800
Subject: [PATCH 1/2] [libc] Add `returns_twice` attribute to `setjmp(3)`
This is to work around a clang bug where clang will incorrectly add a
tail call optimization to `setjmp(3)` if `-fno-builtins` is set
(#122840).
---
libc/include/__llvm-libc-common.h | 6 ++++++
libc/include/setjmp.yaml | 2 ++
2 files changed, 8 insertions(+)
diff --git a/libc/include/__llvm-libc-common.h b/libc/include/__llvm-libc-common.h
index b5a23c5765f4d8..58006a1ffc24fc 100644
--- a/libc/include/__llvm-libc-common.h
+++ b/libc/include/__llvm-libc-common.h
@@ -45,6 +45,9 @@
#define __NOEXCEPT throw()
#endif
+#undef _Returns_twice
+#define _Returns_twice [[gnu::returns_twice]]
+
#else // not __cplusplus
#undef __BEGIN_C_DECLS
@@ -80,6 +83,9 @@
#define __NOEXCEPT
#endif
+#undef _Returns_twice
+#define _Returns_twice __attribute__((returns_twice))
+
#endif // __cplusplus
#endif // LLVM_LIBC_COMMON_H
diff --git a/libc/include/setjmp.yaml b/libc/include/setjmp.yaml
index 2c4f7fb6dfcc70..5fbb9eb2a47e5b 100644
--- a/libc/include/setjmp.yaml
+++ b/libc/include/setjmp.yaml
@@ -17,5 +17,7 @@ functions:
standards:
- stdc
return_type: int
+ attributes:
+ - _Returns_twice
arguments:
- type: jmp_buf
>From 3b19f8b8770aaa51c592a4a961fc3320a8619f48 Mon Sep 17 00:00:00 2001
From: Alan Zhao <ayzhao at google.com>
Date: Mon, 27 Jan 2025 11:22:27 -0800
Subject: [PATCH 2/2] add attribute to setjmp_impl.h
---
libc/src/setjmp/setjmp_impl.h | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
diff --git a/libc/src/setjmp/setjmp_impl.h b/libc/src/setjmp/setjmp_impl.h
index d035409e581954..c89d6bc07c900b 100644
--- a/libc/src/setjmp/setjmp_impl.h
+++ b/libc/src/setjmp/setjmp_impl.h
@@ -29,7 +29,8 @@ namespace LIBC_NAMESPACE_DECL {
#ifdef LIBC_COMPILER_IS_GCC
[[gnu::nothrow]]
#endif
-int setjmp(jmp_buf buf);
+[[gnu::returns_twice]] int
+setjmp(jmp_buf buf);
} // namespace LIBC_NAMESPACE_DECL
More information about the libc-commits
mailing list