Skip to content
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

[libc] Support configurable errno modes #98287

Merged
merged 10 commits into from
Jul 13, 2024
Merged
Show file tree
Hide file tree
Changes from 8 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
5 changes: 5 additions & 0 deletions libc/config/baremetal/config.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
{
"errno": {
"LIBC_CONF_ERRNO_MODE": {
"value": "LIBC_ERRNO_MODE_EXTERNAL"
frobtech marked this conversation as resolved.
Show resolved Hide resolved
}
},
"printf": {
"LIBC_CONF_PRINTF_DISABLE_FLOAT": {
"value": true
Expand Down
6 changes: 6 additions & 0 deletions libc/config/config.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
{
"errno": {
"LIBC_CONF_ERRNO_MODE": {
"value": "",
Copy link
Contributor

Choose a reason for hiding this comment

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

it would probably be best to make the default value LIBC_ERRNO_MODE_THREAD_LOCAL

Copy link
Member Author

Choose a reason for hiding this comment

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

I agree but the problem is that we currently don't have any mechanism to distinguish the overlay from the full build mode, and in the overlay mode we want to default to LIBC_ERRNO_MODE_SYSTEM.

"doc": "The implementation used for errno, acceptable values are LIBC_ERRNO_MODE_UNDEFINED, LIBC_ERRNO_MODE_THREAD_LOCAL, LIBC_ERRNO_MODE_SHARED, LIBC_ERRNO_MODE_EXTERNAL, and LIBC_ERRNO_MODE_SYSTEM."
}
},
"printf": {
"LIBC_CONF_PRINTF_DISABLE_FLOAT": {
"value": false,
Expand Down
5 changes: 5 additions & 0 deletions libc/config/gpu/config.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
{
"errno": {
"LIBC_CONF_ERRNO_MODE": {
"value": "LIBC_ERRNO_MODE_SHARED"
}
},
"printf": {
"LIBC_CONF_PRINTF_DISABLE_FLOAT": {
"value": true
Expand Down
7 changes: 7 additions & 0 deletions libc/docs/configure.rst
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@ to learn about the defaults for your platform and target.
* **"codegen" options**
- ``LIBC_CONF_ENABLE_STRONG_STACK_PROTECTOR``: Enable -fstack-protector-strong to defend against stack smashing attack.
- ``LIBC_CONF_KEEP_FRAME_POINTER``: Keep frame pointer in functions for better debugging experience.
* **"errno" options**
- ``LIBC_CONF_ERRNO_MODE``: The implementation used for errno, acceptable values are:
Copy link
Contributor

Choose a reason for hiding this comment

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

this file gets auto-generated, so the docs here need to be also in the json file

Copy link
Member Author

Choose a reason for hiding this comment

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

I don't think JSON is particularly suited for multiline strings so I moved the explanation of different modes as comments to the implementation file.

- ``LIBC_ERRNO_MODE_UNDEFINED``: libc never stores a value; ``errno`` macro uses get link-time failure,
- ``LIBC_ERRNO_MODE_THREAD_LOCAL``: libc maintains per-thread state (requires C++ ``thread_local`` support for libc implementation code),
- ``LIBC_ERRNO_MODE_SHARED``: libc maintains shared state used by all threads, contrary to standard C semantics unless always single-threaded; nothing prevents data races,
- ``LIBC_ERRNO_MODE_EXTERNAL``: embedder must define ``int *__llvm_libc_errno(void);`` C function,
- ``LIBC_ERRNO_MODE_SYSTEM``: in overlay mode, system ``<errno.h>`` ``errno`` macro is used directly; in fullbuild mode, effectively the same as ``LIBC_ERRNO_MODE_EXTERNAL``.
* **"malloc" options**
- ``LIBC_CONF_FREELIST_MALLOC_BUFFER_SIZE``: Default size for the constinit freelist buffer used for the freelist malloc implementation (default 1o 1GB).
* **"math" options**
Expand Down
18 changes: 6 additions & 12 deletions libc/include/errno.h.def
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,12 @@
#include "llvm-libc-macros/generic-error-number-macros.h"
#endif

#if defined(__AMDGPU__) || defined(__NVPTX__)
extern int __llvmlibc_errno; // Not thread_local!
#else
#ifdef __cplusplus
extern "C" {
extern thread_local int __llvmlibc_errno;
}
#else
extern _Thread_local int __llvmlibc_errno;
#endif // __cplusplus
#endif
__BEGIN_C_DECLS

int *__llvm_libc_errno(void) __NOEXCEPT;

__END_C_DECLS

#define errno __llvmlibc_errno
#define errno (*__llvm_libc_errno())

#endif // LLVM_LIBC_ERRNO_H
5 changes: 5 additions & 0 deletions libc/src/errno/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ if(LLVM_LIBC_FULL_BUILD)
set(full_build_flag "-DLIBC_FULL_BUILD")
endif()

if(LIBC_CONF_ERRNO_MODE)
set(errno_config_copts "-DLIBC_ERRNO_MODE=${LIBC_CONF_ERRNO_MODE}")
endif()

add_entrypoint_object(
errno
SRCS
Expand All @@ -17,6 +21,7 @@ add_entrypoint_object(
libc_errno.h # Include this
COMPILE_OPTIONS
${full_build_flag}
${errno_config_copts}
DEPENDS
libc.hdr.errno_macros
libc.src.__support.common
Expand Down
14 changes: 14 additions & 0 deletions libc/src/errno/errno.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//===-- Implementation header for errno -------------------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache 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_SRC_ERRNO_ERRNO_H
#define LLVM_LIBC_SRC_ERRNO_ERRNO_H

extern "C" int *__llvm_libc_errno();

#endif // LLVM_LIBC_SRC_ERRNO_ERRNO_H
93 changes: 64 additions & 29 deletions libc/src/errno/libc_errno.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,46 +7,81 @@
//===----------------------------------------------------------------------===//

#include "libc_errno.h"
#include "src/__support/CPP/atomic.h"
#include "src/errno/errno.h"

#ifdef LIBC_TARGET_ARCH_IS_GPU
// LIBC_THREAD_LOCAL on GPU currently does nothing. So essentially this is just
// a global errno for gpu to use for now.
extern "C" {
LIBC_THREAD_LOCAL LIBC_NAMESPACE::cpp::Atomic<int> __llvmlibc_errno;
}
#define LIBC_ERRNO_MODE_UNDEFINED 1
#define LIBC_ERRNO_MODE_THREAD_LOCAL 2
#define LIBC_ERRNO_MODE_SHARED 3
#define LIBC_ERRNO_MODE_EXTERNAL 4
#define LIBC_ERRNO_MODE_SYSTEM 5

#ifndef LIBC_ERRNO_MODE
#if defined(LIBC_FULL_BUILD) || !defined(LIBC_COPT_PUBLIC_PACKAGING)
#define LIBC_ERRNO_MODE LIBC_ERRNO_MODE_THREAD_LOCAL
#else
#define LIBC_ERRNO_MODE LIBC_ERRNO_MODE_SYSTEM
#endif
#endif // LIBC_ERRNO_MODE

#if LIBC_ERRNO_MODE != LIBC_ERRNO_MODE_UNDEFINED && \
michaelrj-google marked this conversation as resolved.
Show resolved Hide resolved
LIBC_ERRNO_MODE != LIBC_ERRNO_MODE_THREAD_LOCAL && \
LIBC_ERRNO_MODE != LIBC_ERRNO_MODE_SHARED && \
LIBC_ERRNO_MODE != LIBC_ERRNO_MODE_EXTERNAL && \
LIBC_ERRNO_MODE != LIBC_ERRNO_MODE_SYSTEM
#error LIBC_ERRNO_MODE must be one of the following values: \
LIBC_ERRNO_MODE_UNDEFINED, \
LIBC_ERRNO_MODE_THREAD_LOCAL, \
LIBC_ERRNO_MODE_SHARED, \
LIBC_ERRNO_MODE_EXTERNAL, \
LIBC_ERRNO_MODE_SYSTEM
#endif

namespace LIBC_NAMESPACE {
Copy link
Contributor

Choose a reason for hiding this comment

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

Don't forget to update to LIBC_NAMESPACE_DECL when that stuff lands. (I'm not sure it technically matters as long as it was done in libc_errno.h, but I think uniformity it what we're going for regardless.)

Copy link
Member Author

Choose a reason for hiding this comment

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

Ack, I expect this change is going to land after that one.


// Define the global `libc_errno` instance.
Errno libc_errno;

#if LIBC_ERRNO_MODE == LIBC_ERRNO_MODE_UNDEFINED

void Errno::operator=(int) {}
Errno::operator int() { return 0; }

void LIBC_NAMESPACE::Errno::operator=(int a) {
__llvmlibc_errno.store(a, cpp::MemoryOrder::RELAXED);
#elif LIBC_ERRNO_MODE == LIBC_ERRNO_MODE_THREAD_LOCAL
petrhosek marked this conversation as resolved.
Show resolved Hide resolved

namespace {
LIBC_THREAD_LOCAL int thread_errno;
}
LIBC_NAMESPACE::Errno::operator int() {
return __llvmlibc_errno.load(cpp::MemoryOrder::RELAXED);

extern "C" {
int *__llvm_libc_errno() { return &thread_errno; }
}

#elif !defined(LIBC_COPT_PUBLIC_PACKAGING)
// This mode is for unit testing. We just use our internal errno.
LIBC_THREAD_LOCAL int __llvmlibc_internal_errno;
void Errno::operator=(int a) { thread_errno = a; }
Errno::operator int() { return thread_errno; }

void LIBC_NAMESPACE::Errno::operator=(int a) { __llvmlibc_internal_errno = a; }
LIBC_NAMESPACE::Errno::operator int() { return __llvmlibc_internal_errno; }
#elif LIBC_ERRNO_MODE == LIBC_ERRNO_MODE_SHARED

namespace {
int shared_errno;
}

#elif defined(LIBC_FULL_BUILD)
// This mode is for public libc archive, hermetic, and integration tests.
// In full build mode, we provide the errno storage ourselves.
extern "C" {
LIBC_THREAD_LOCAL int __llvmlibc_errno;
int *__llvm_libc_errno() { return &shared_errno; }
}

void LIBC_NAMESPACE::Errno::operator=(int a) { __llvmlibc_errno = a; }
LIBC_NAMESPACE::Errno::operator int() { return __llvmlibc_errno; }
void Errno::operator=(int a) { shared_errno = a; }
Errno::operator int() { return shared_errno; }

#else
void LIBC_NAMESPACE::Errno::operator=(int a) { errno = a; }
LIBC_NAMESPACE::Errno::operator int() { return errno; }
#elif LIBC_ERRNO_MODE == LIBC_ERRNO_MODE_EXTERNAL

#endif // LIBC_FULL_BUILD
void Errno::operator=(int a) { *__llvm_libc_errno() = a; }
Errno::operator int() { return *__llvm_libc_errno(); }

#elif LIBC_ERRNO_MODE == LIBC_ERRNO_MODE_SYSTEM

void Errno::operator=(int a) { errno = a; }
Errno::operator int() { return errno; }

#endif

namespace LIBC_NAMESPACE {
// Define the global `libc_errno` instance.
Errno libc_errno;
} // namespace LIBC_NAMESPACE
1 change: 1 addition & 0 deletions libc/src/errno/libc_errno.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
// - Still depend on libc.src.errno.errno

namespace LIBC_NAMESPACE {

struct Errno {
void operator=(int);
operator int();
Expand Down
Loading