Skip to content

Commit fd4c4ed

Browse files
[libc] Add returns_twice attribute to setjmp(3) (#124370)
This is to ensure that calls to `setjmp(3)` result in correct code generation that respects `setjmp(3)`'s `returns_twice` behavior. Otherwise, we might run into bugs (for example, Clang may perform tail-call optimization on this function if `-fno-builtins` is set (#122840)). --------- Co-authored-by: Nick Desaulniers <[email protected]>
1 parent 5f7acf7 commit fd4c4ed

File tree

3 files changed

+6
-1
lines changed

3 files changed

+6
-1
lines changed

libc/include/__llvm-libc-common.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,9 @@
8080
#define __NOEXCEPT
8181
#endif
8282

83+
#undef _Returns_twice
84+
#define _Returns_twice __attribute__((returns_twice))
85+
8386
#endif // __cplusplus
8487

8588
#endif // LLVM_LIBC_COMMON_H

libc/include/setjmp.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,7 @@ functions:
1717
standards:
1818
- stdc
1919
return_type: int
20+
attributes:
21+
- _Returns_twice
2022
arguments:
2123
- type: jmp_buf

libc/src/setjmp/setjmp_impl.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ namespace LIBC_NAMESPACE_DECL {
2929
#ifdef LIBC_COMPILER_IS_GCC
3030
[[gnu::nothrow]]
3131
#endif
32-
int setjmp(jmp_buf buf);
32+
__attribute__((returns_twice)) int setjmp(jmp_buf buf);
3333

3434
} // namespace LIBC_NAMESPACE_DECL
3535

0 commit comments

Comments
 (0)