20#include "../microfmt.hpp"
21#include <reloco/tls_provider.hpp>
52using fallible_tls = reloco::tls_provider<fault_recovery_context *, fallible_space_tls_tag>;
67 auto result = fallible_tls::get();
70 using value_type = std::decay_t<
decltype(*result)>;
71 if constexpr (std::is_same_v<value_type, std::reference_wrapper<fault_recovery_context *>>) {
85 if (ctx && ctx->active) {
86 siglongjmp(ctx->env, sig);
90 struct sigaction sa{};
91 sa.sa_handler = SIG_DFL;
92 sigemptyset(&sa.sa_mask);
93 sigaction(sig, &sa,
nullptr);
98 static bool initialized = []() {
99 struct sigaction sa{};
101 sa.sa_flags = SA_SIGINFO;
102 sigemptyset(&sa.sa_mask);
103 sigaction(SIGSEGV, &sa,
nullptr);
104 sigaction(SIGBUS, &sa,
nullptr);
118 static bool read_bytes(uintptr_t addr,
void *dest,
size_t size)
noexcept {
134 if (sigsetjmp(fault_ctx.env, 1) != 0) {
139 RELOCO_BEGIN_UNSAFE_BUFFER_USAGE;
140 std::memcpy(dest,
reinterpret_cast<const void *
>(addr), size);
141 RELOCO_END_UNSAFE_BUFFER_USAGE;
147 static bool write_bytes(uintptr_t addr,
const void *src,
size_t size)
noexcept {
162 if (sigsetjmp(fault_ctx.env, 1) != 0) {
167 RELOCO_BEGIN_UNSAFE_BUFFER_USAGE;
168 std::memcpy(
reinterpret_cast<void *
>(addr), src, size);
169 RELOCO_END_UNSAFE_BUFFER_USAGE;
175 static bool read_string(uintptr_t addr,
char *dest,
size_t max_len,
size_t &out_len,
bool &null_term)
noexcept {
188 if (sigsetjmp(fault_ctx.env, 1) != 0) {
193 RELOCO_BEGIN_UNSAFE_BUFFER_USAGE;
194 const char *src =
reinterpret_cast<const char *
>(addr);
196 while (i < max_len) {
198 if (dest[i] ==
'\0') {
206 RELOCO_END_UNSAFE_BUFFER_USAGE;
Type-erased remote address-space access and remote string/object views.
#define MICROFMT_API_CLASS
Definition compat.hpp:181
fault_recovery_context * fallible_tls_get() noexcept
Unwraps fallible_tls::get()'s result to a plain pointer.
Definition fallible_address_space.hpp:66
bool install_fallible_handlers() noexcept
Definition fallible_address_space.hpp:97
reloco::tls_provider< fault_recovery_context *, fallible_space_tls_tag > fallible_tls
Definition fallible_address_space.hpp:52
void fallible_signal_handler(int sig, siginfo_t *, void *) noexcept
Definition fallible_address_space.hpp:83
bool fallible_tls_set(fault_recovery_context *ctx) noexcept
Sets the calling thread's recovery-context slot; returns false if the underlying tls_provider::set() ...
Definition fallible_address_space.hpp:81
void context_type
Definition fallible_address_space.hpp:116
static bool write_bytes(uintptr_t addr, const void *src, size_t size) noexcept
Definition fallible_address_space.hpp:147
static bool read_bytes(uintptr_t addr, void *dest, size_t size) noexcept
Definition fallible_address_space.hpp:118
static bool read_string(uintptr_t addr, char *dest, size_t max_len, size_t &out_len, bool &null_term) noexcept
Definition fallible_address_space.hpp:175
Definition address_space.hpp:37
Unique tag to isolate the fallible address space's TLS storage slot.
Definition fallible_address_space.hpp:49
Context holding the jump buffer and active state for signal-safe memory fault recovery.
Definition fallible_address_space.hpp:41
bool active
Definition fallible_address_space.hpp:43
sigjmp_buf env
Definition fallible_address_space.hpp:42
Tag identifying the fallible local address space.
Definition fallible_address_space.hpp:34