Repository navigation
Expand file tree
/
Copy pathSignals.cpp
More file actions
174 lines (164 loc) · 6.4 KB
/
Copy pathSignals.cpp
File metadata and controls
174 lines (164 loc) · 6.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#include "prx/libc/include/General.hpp"
#include "prx/libc/include/general/VabiMacros.hpp"
#include <atomic>
#include <csignal>
#include <cstddef>
#include <cstdint>
#include <mutex>
#include <stdexcept>
extern "C" int* APS5_VABI __error_nid_postfix();
extern "C" int APS5_VABI getpid_nid_postfix(void);
struct GuestSignalSet {
std::uint32_t bits[4];
};
struct GuestSigaction {
std::uintptr_t handler;
int flags;
GuestSignalSet mask;
};
static_assert(sizeof(GuestSigaction) == 32 && offsetof(GuestSigaction, flags) == 8 && offsetof(GuestSigaction, mask) == 12);
namespace {
using GuestHandler = void (APS5_VABI *)(int);
constexpr int MaxSignal = 128;
constexpr int GuestSigkill = 9;
constexpr int GuestSigstop = 17;
constexpr int SaRestart = 0x2;
constexpr int SaSiginfo = 0x40;
std::atomic<GuestHandler> handlers[MaxSignal + 1]{};
static_assert(std::atomic<GuestHandler>::is_always_lock_free);
std::atomic<std::uint32_t> blockedMask{0};
static_assert(std::atomic<std::uint32_t>::is_always_lock_free);
std::mutex registration;
GuestSigaction dispositions[MaxSignal + 1]{};
int NativeSignal(int guest) {
switch (guest) {
case 2: return SIGINT;
case 4: return SIGILL;
case 6: return SIGABRT;
case 8: return SIGFPE;
case 11: return SIGSEGV;
case 15: return SIGTERM;
default: return 0;
}
}
void Dispatch(int native) {
int guest = 0;
for (int candidate : {2, 4, 6, 8, 11, 15})
if (NativeSignal(candidate) == native) { guest = candidate; break; }
if (!guest) return;
#ifdef _WIN32
// Preserve the guest's persistent registration across CRT delivery.
std::signal(native, Dispatch);
#endif
if ((blockedMask.load() & (1u << (guest - 1))) != 0) return;
const auto callback = handlers[guest].load();
if (reinterpret_cast<std::uintptr_t>(callback) > 1) callback(guest);
}
bool Delivered(const GuestSigaction& action) {
return action.handler > 1 && (action.flags & SaSiginfo) == 0;
}
bool Install(int guest, const GuestSigaction& action) {
const int native = NativeSignal(guest);
if (!native) return true;
const auto previous = handlers[guest].exchange(Delivered(action) ? reinterpret_cast<GuestHandler>(action.handler) : nullptr);
auto hostHandler = action.handler == 1 ? SIG_IGN : Delivered(action) ? Dispatch : SIG_DFL;
if (std::signal(native, hostHandler) == SIG_ERR) {
handlers[guest].store(previous);
return false;
}
return true;
}
}
struct GuestStack {
void* sp;
std::size_t size;
int flags;
};
static_assert(sizeof(GuestStack) == 24 && offsetof(GuestStack, flags) == 16);
namespace {
constexpr int SsDisable = 4;
constexpr std::size_t MinSignalStackSize = 2048;
thread_local GuestStack alternateStack{nullptr, 0, SsDisable};
}
extern "C" {
GuestHandler APS5_VABI signal_nid_postfix(int guest, GuestHandler handler) {
const auto invalid = reinterpret_cast<GuestHandler>(static_cast<std::uintptr_t>(-1));
const int native = NativeSignal(guest);
if (!native || handler == invalid) { *__error_nid_postfix() = 22; return invalid; }
std::lock_guard lock(registration);
const GuestSigaction action{reinterpret_cast<std::uintptr_t>(handler), SaRestart, {}};
if (!Install(guest, action)) { *__error_nid_postfix() = 22; return invalid; }
const auto previous = reinterpret_cast<GuestHandler>(dispositions[guest].handler);
dispositions[guest] = action;
return previous;
}
int APS5_VABI sigaction_nid_postfix(int guest, const GuestSigaction* action, GuestSigaction* previous) {
if (guest < 1 || guest > MaxSignal) { *__error_nid_postfix() = 22; return -1; }
if (action && (guest == GuestSigkill || guest == GuestSigstop) && action->handler != 0) { *__error_nid_postfix() = 22; return -1; }
std::lock_guard lock(registration);
const GuestSigaction old = dispositions[guest];
if (action) {
if (!Install(guest, *action)) { *__error_nid_postfix() = 22; return -1; }
dispositions[guest] = *action;
}
if (previous) *previous = old;
return 0;
}
int APS5_VABI raise_nid_postfix(int guest) {
const int native = NativeSignal(guest);
if (!native) { *__error_nid_postfix() = 22; return -1; }
{
std::lock_guard lock(registration);
if (dispositions[guest].handler > 1 && !Delivered(dispositions[guest])) throw std::runtime_error("raise: SA_SIGINFO handlers are not delivered");
}
const int result = std::raise(native);
if (result) *__error_nid_postfix() = 22;
return result ? -1 : 0;
}
int APS5_VABI kill_nid_postfix(int pid, int guest) {
if (guest < 0 || guest > MaxSignal) { *__error_nid_postfix() = 22; return -1; }
const int self = getpid_nid_postfix();
if (pid != self && pid != 0 && pid != -self) { *__error_nid_postfix() = 3; return -1; }
return guest == 0 ? 0 : raise_nid_postfix(guest);
}
int APS5_VABI sigaltstack_nid_postfix(const GuestStack* stack, GuestStack* previous) {
GuestStack replacement = alternateStack;
if (stack != nullptr) {
if ((stack->flags & ~SsDisable) != 0) { *__error_nid_postfix() = 22; return -1; }
if (stack->flags & SsDisable) replacement.flags = SsDisable;
else if (stack->size < MinSignalStackSize) { *__error_nid_postfix() = 12; return -1; }
else replacement = {stack->sp, stack->size, 0};
}
if (previous != nullptr) *previous = alternateStack;
alternateStack = replacement;
return 0;
}
int APS5_VABI _sigprocmask_nid_postfix(int how, const GuestSignalSet* set, GuestSignalSet* previousSet) {
std::lock_guard lock(registration);
if (previousSet != nullptr) {
previousSet->bits[0] = blockedMask.load();
previousSet->bits[1] = 0;
previousSet->bits[2] = 0;
previousSet->bits[3] = 0;
}
if (set != nullptr) {
switch (how) {
case 1: blockedMask.fetch_or(set->bits[0]); break;
case 2: blockedMask.fetch_and(~set->bits[0]); break;
case 3: blockedMask.store(set->bits[0]); break;
default: throw std::invalid_argument("_sigprocmask: invalid how");
}
}
return 0;
}
int APS5_VABI sigprocmask_nid_postfix(int how, const void* set, void* previousSet) {
return _sigprocmask_nid_postfix(how, static_cast<const GuestSignalSet*>(set),
static_cast<GuestSignalSet*>(previousSet));
}
}
extern "C" {
int APS5_VABI _is_signal_return_nid_postfix(std::uint64_t programCounter) {
(void)programCounter;
return 0;
}
}