Skip to content

[libc] Add proxy header for the jmp_buf type #107712

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 4 commits into from
Sep 9, 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
9 changes: 9 additions & 0 deletions libc/hdr/types/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -190,3 +190,12 @@ add_proxy_header_library(
libc.include.signal
)

add_proxy_header_library(
jmp_buf
HDRS
jmp_buf.h
FULL_BUILD_DEPENDS
libc.include.llvm-libc-types.jmp_buf
libc.include.setjmp
)

22 changes: 22 additions & 0 deletions libc/hdr/types/jmp_buf.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//===-- Definition of jmp_buf.h ------------------------------------------===//
//
// Part of the LLVM Project, under the Apahce License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_LIBC_HDR_JMP_BUF_H
#define LLVM_LIBC_HDR_JMP_BUF_H

#ifdef LIBC_FULL_BUILD

#include "include/llvm-libc-types/jmp_buf.h"

#else // overlay mode

#include <setjmp.h>

#endif // LLVM_LIBC_FULL_BUILD

#endif // LLVM_LIBC_HDR_TYPES_JMP_BUF_H
2 changes: 1 addition & 1 deletion libc/hdr/types/stack_t.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//===-- Definition of stack_t.h -------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https:llvm.or/LICENSE.txt for license information.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//
Expand Down
4 changes: 2 additions & 2 deletions libc/src/setjmp/aarch64/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ add_entrypoint_object(
HDRS
../setjmp_impl.h
DEPENDS
libc.include.setjmp
libc.hdr.types.jmp_buf
${setjmp_config_options}
)

Expand All @@ -23,6 +23,6 @@ add_entrypoint_object(
HDRS
../longjmp.h
DEPENDS
libc.include.setjmp
libc.hdr.types.jmp_buf
${setjmp_config_options}
)
2 changes: 1 addition & 1 deletion libc/src/setjmp/aarch64/longjmp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ namespace LIBC_NAMESPACE_DECL {
// them.)

[[gnu::naked]] LLVM_LIBC_FUNCTION(void, longjmp,
([[maybe_unused]] __jmp_buf * buf,
([[maybe_unused]] jmp_buf buf,
[[maybe_unused]] int val)) {
// If BTI branch protection is in use, the compiler will automatically insert
// a BTI here, so we don't need to make any extra effort to do so.
Expand Down
3 changes: 1 addition & 2 deletions libc/src/setjmp/aarch64/setjmp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@

namespace LIBC_NAMESPACE_DECL {

[[gnu::naked]] LLVM_LIBC_FUNCTION(int, setjmp,
([[maybe_unused]] __jmp_buf * buf)) {
[[gnu::naked]] LLVM_LIBC_FUNCTION(int, setjmp, ([[maybe_unused]] jmp_buf buf)) {
// If BTI branch protection is in use, the compiler will automatically insert
// a BTI here, so we don't need to make any extra effort to do so.

Expand Down
4 changes: 2 additions & 2 deletions libc/src/setjmp/arm/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ add_entrypoint_object(
HDRS
../setjmp_impl.h
DEPENDS
libc.include.setjmp
libc.hdr.types.jmp_buf
)

add_entrypoint_object(
Expand All @@ -15,5 +15,5 @@ add_entrypoint_object(
HDRS
../longjmp.h
DEPENDS
libc.include.setjmp
libc.hdr.types.jmp_buf
)
8 changes: 4 additions & 4 deletions libc/src/setjmp/arm/longjmp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ namespace LIBC_NAMESPACE_DECL {

#if defined(__thumb__) && __ARM_ARCH_ISA_THUMB == 1

[[gnu::naked, gnu::target("thumb")]]
LLVM_LIBC_FUNCTION(void, longjmp, (__jmp_buf * buf, int val)) {
[[gnu::naked, gnu::target("thumb")]] LLVM_LIBC_FUNCTION(void, longjmp,
(jmp_buf buf,
int val)) {
asm(R"(
# Reload r4, r5, r6, r7.
ldmia r0!, {r4-r7}
Expand Down Expand Up @@ -53,8 +54,7 @@ LLVM_LIBC_FUNCTION(void, longjmp, (__jmp_buf * buf, int val)) {
// TODO(https://github.com/llvm/llvm-project/issues/94061): fp registers
// (d0-d16)
// TODO(https://github.com/llvm/llvm-project/issues/94062): pac+bti
[[gnu::naked]]
LLVM_LIBC_FUNCTION(void, longjmp, (__jmp_buf * buf, int val)) {
[[gnu::naked]] LLVM_LIBC_FUNCTION(void, longjmp, (jmp_buf buf, int val)) {
asm(R"(
# While sp may appear in a register list for ARM mode, it may not for
# Thumb2 mode. Just load the previous value of sp into r12 then move it
Expand Down
7 changes: 3 additions & 4 deletions libc/src/setjmp/arm/setjmp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ namespace LIBC_NAMESPACE_DECL {

#if defined(__thumb__) && __ARM_ARCH_ISA_THUMB == 1

[[gnu::naked, gnu::target("thumb")]]
LLVM_LIBC_FUNCTION(int, setjmp, (__jmp_buf * buf)) {
[[gnu::naked, gnu::target("thumb")]] LLVM_LIBC_FUNCTION(int, setjmp,
(jmp_buf buf)) {
asm(R"(
# Store r4, r5, r6, and r7 into buf.
stmia r0!, {r4-r7}
Expand Down Expand Up @@ -44,8 +44,7 @@ LLVM_LIBC_FUNCTION(int, setjmp, (__jmp_buf * buf)) {
// TODO(https://github.com/llvm/llvm-project/issues/94061): fp registers
// (d0-d16)
// TODO(https://github.com/llvm/llvm-project/issues/94062): pac+bti
[[gnu::naked]]
LLVM_LIBC_FUNCTION(int, setjmp, (__jmp_buf * buf)) {
[[gnu::naked]] LLVM_LIBC_FUNCTION(int, setjmp, (jmp_buf buf)) {
asm(R"(
# While sp may appear in a register list for ARM mode, it may not for
# Thumb2 mode. Just move it into r12 then stm that, so that this code
Expand Down
4 changes: 2 additions & 2 deletions libc/src/setjmp/longjmp.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@
#ifndef LLVM_LIBC_SRC_SETJMP_LONGJMP_H
#define LLVM_LIBC_SRC_SETJMP_LONGJMP_H

#include "hdr/types/jmp_buf.h"
#include "src/__support/macros/config.h"
#include <setjmp.h>

namespace LIBC_NAMESPACE_DECL {

void longjmp(__jmp_buf *buf, int val);
void longjmp(jmp_buf buf, int val);

} // namespace LIBC_NAMESPACE_DECL

Expand Down
4 changes: 2 additions & 2 deletions libc/src/setjmp/riscv/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ add_entrypoint_object(
HDRS
../setjmp_impl.h
DEPENDS
libc.include.setjmp
libc.hdr.types.jmp_buf
COMPILE_OPTIONS
-O3
-fomit-frame-pointer
Expand All @@ -18,7 +18,7 @@ add_entrypoint_object(
HDRS
../longjmp.h
DEPENDS
libc.include.setjmp
libc.hdr.types.jmp_buf
COMPILE_OPTIONS
-O3
-fomit-frame-pointer
Expand Down
4 changes: 1 addition & 3 deletions libc/src/setjmp/riscv/longjmp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@
#include "src/__support/macros/config.h"
#include "src/__support/macros/properties/architectures.h"

#include <setjmp.h>

#if !defined(LIBC_TARGET_ARCH_IS_ANY_RISCV)
#error "Invalid file include"
#endif
Expand All @@ -30,7 +28,7 @@

namespace LIBC_NAMESPACE_DECL {

LLVM_LIBC_FUNCTION(void, longjmp, (__jmp_buf * buf, int val)) {
LLVM_LIBC_FUNCTION(void, longjmp, (jmp_buf buf, int val)) {
LOAD(ra, buf->__pc);
LOAD(s0, buf->__regs[0]);
LOAD(s1, buf->__regs[1]);
Expand Down
4 changes: 1 addition & 3 deletions libc/src/setjmp/riscv/setjmp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
#include "src/__support/macros/config.h"
#include "src/setjmp/setjmp_impl.h"

#include <setjmp.h>

#if !defined(LIBC_TARGET_ARCH_IS_ANY_RISCV)
#error "Invalid file include"
#endif
Expand All @@ -29,7 +27,7 @@

namespace LIBC_NAMESPACE_DECL {

LLVM_LIBC_FUNCTION(int, setjmp, (__jmp_buf * buf)) {
LLVM_LIBC_FUNCTION(int, setjmp, (jmp_buf buf)) {
STORE(ra, buf->__pc);
STORE(s0, buf->__regs[0]);
STORE(s1, buf->__regs[1]);
Expand Down
4 changes: 2 additions & 2 deletions libc/src/setjmp/setjmp_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@

// This header has the _impl prefix in its name to avoid conflict with the
// public header setjmp.h which is also included. here.
#include "hdr/types/jmp_buf.h"
#include "src/__support/macros/config.h"
#include <setjmp.h>

namespace LIBC_NAMESPACE_DECL {

int setjmp(__jmp_buf *buf);
int setjmp(jmp_buf buf);

} // namespace LIBC_NAMESPACE_DECL

Expand Down
4 changes: 2 additions & 2 deletions libc/src/setjmp/x86_64/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ add_entrypoint_object(
HDRS
../setjmp_impl.h
DEPENDS
libc.include.setjmp
libc.hdr.types.jmp_buf
COMPILE_OPTIONS
-O3
-fno-omit-frame-pointer
Expand All @@ -23,7 +23,7 @@ add_entrypoint_object(
HDRS
../longjmp.h
DEPENDS
libc.include.setjmp
libc.hdr.types.jmp_buf
COMPILE_OPTIONS
-O3
-fomit-frame-pointer
Expand Down
2 changes: 1 addition & 1 deletion libc/src/setjmp/x86_64/longjmp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

namespace LIBC_NAMESPACE_DECL {

LLVM_LIBC_FUNCTION(void, longjmp, (__jmp_buf * buf, int val)) {
LLVM_LIBC_FUNCTION(void, longjmp, (jmp_buf buf, int val)) {
register __UINT64_TYPE__ rbx __asm__("rbx");
register __UINT64_TYPE__ rbp __asm__("rbp");
register __UINT64_TYPE__ r12 __asm__("r12");
Expand Down
2 changes: 1 addition & 1 deletion libc/src/setjmp/x86_64/setjmp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

namespace LIBC_NAMESPACE_DECL {

LLVM_LIBC_FUNCTION(int, setjmp, (__jmp_buf * buf)) {
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");
Expand Down
1 change: 0 additions & 1 deletion libc/test/src/setjmp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ add_libc_unittest(
SRCS
setjmp_test.cpp
DEPENDS
libc.include.setjmp
libc.src.setjmp.longjmp
libc.src.setjmp.setjmp
)
Loading