Skip to content

[libc][setjmp] fix setjmp test via naked fn attr #88054

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Oct 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 0 additions & 6 deletions libc/src/setjmp/x86_64/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,6 @@ add_entrypoint_object(
libc.hdr.types.jmp_buf
COMPILE_OPTIONS
-O3
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems weird to hardcode the optimization level, but that seems to be llvm-libc style so OK.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I've discussed this with the team a bit. I'd prefer to "just do what libc++ does in terms of configuring the build for size vs performance." Looking at their docs it seems that CMAKE_BUILD_TYPE is perhaps the only way to signal that you'd like a release build, vs MinSizeRel.

Either way, we should file a new issue and discuss this there.

-fno-omit-frame-pointer
# TODO: Remove once one of these lands:
# https://github.com/llvm/llvm-project/pull/87837
# https://github.com/llvm/llvm-project/pull/88054
# https://github.com/llvm/llvm-project/pull/88157
-ftrivial-auto-var-init=uninitialized
)

add_entrypoint_object(
Expand Down
60 changes: 24 additions & 36 deletions libc/src/setjmp/x86_64/setjmp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
//
//===----------------------------------------------------------------------===//

#include "include/llvm-libc-macros/offsetof-macro.h"
#include "src/__support/common.h"
#include "src/__support/macros/config.h"
#include "src/setjmp/setjmp_impl.h"
Expand All @@ -16,42 +17,29 @@

namespace LIBC_NAMESPACE_DECL {

LLVM_LIBC_FUNCTION(int, setjmp, (jmp_buf buf)) {
register __UINT64_TYPE__ rbx __asm__("rbx");
register __UINT64_TYPE__ r12 __asm__("r12");
register __UINT64_TYPE__ r13 __asm__("r13");
register __UINT64_TYPE__ r14 __asm__("r14");
register __UINT64_TYPE__ r15 __asm__("r15");

// We want to store the register values as is. So, we will suppress the
// compiler warnings about the uninitialized variables declared above.
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wuninitialized"
LIBC_INLINE_ASM("mov %1, %0\n\t" : "=m"(buf->rbx) : "r"(rbx) :);
LIBC_INLINE_ASM("mov %1, %0\n\t" : "=m"(buf->r12) : "r"(r12) :);
LIBC_INLINE_ASM("mov %1, %0\n\t" : "=m"(buf->r13) : "r"(r13) :);
LIBC_INLINE_ASM("mov %1, %0\n\t" : "=m"(buf->r14) : "r"(r14) :);
LIBC_INLINE_ASM("mov %1, %0\n\t" : "=m"(buf->r15) : "r"(r15) :);
#pragma GCC diagnostic pop

// We want the rbp of the caller, which is what __builtin_frame_address(1)
// should return. But, compilers generate a warning that calling
// __builtin_frame_address with non-zero argument is unsafe. So, we use
// the knowledge of the x86_64 ABI to fetch the callers rbp. As per the ABI,
// the rbp of the caller is pushed on to the stack and then new top is saved
// in this function's rbp. So, we fetch it from location at which this
// functions's rbp is pointing.
buf->rbp = *reinterpret_cast<__UINTPTR_TYPE__ *>(__builtin_frame_address(0));

// The callers stack address is exactly 2 pointer widths ahead of the current
// frame pointer - between the current frame pointer and the rsp of the caller
// are the return address (pushed by the x86_64 call instruction) and the
// previous stack pointer as required by the x86_64 ABI.
// The stack pointer is ahead because the stack grows down on x86_64.
buf->rsp = reinterpret_cast<__UINTPTR_TYPE__>(__builtin_frame_address(0)) +
sizeof(__UINTPTR_TYPE__) * 2;
buf->rip = reinterpret_cast<__UINTPTR_TYPE__>(__builtin_return_address(0));
return 0;
[[gnu::naked]]
LLVM_LIBC_FUNCTION(int, setjmp, (__jmp_buf * buf)) {
asm(R"(
mov %%rbx, %c[rbx](%%rdi)
mov %%rbp, %c[rbp](%%rdi)
mov %%r12, %c[r12](%%rdi)
mov %%r13, %c[r13](%%rdi)
mov %%r14, %c[r14](%%rdi)
mov %%r15, %c[r15](%%rdi)

lea 8(%%rsp), %%rax
mov %%rax, %c[rsp](%%rdi)

mov (%%rsp), %%rax
mov %%rax, %c[rip](%%rdi)

xorl %%eax, %%eax
retq)" ::[rbx] "i"(offsetof(__jmp_buf, rbx)),
[rbp] "i"(offsetof(__jmp_buf, rbp)), [r12] "i"(offsetof(__jmp_buf, r12)),
[r13] "i"(offsetof(__jmp_buf, r13)), [r14] "i"(offsetof(__jmp_buf, r14)),
[r15] "i"(offsetof(__jmp_buf, r15)), [rsp] "i"(offsetof(__jmp_buf, rsp)),
[rip] "i"(offsetof(__jmp_buf, rip))
: "rax");
}

} // namespace LIBC_NAMESPACE_DECL
Loading