forked from WAVM/WAVM
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathException.cpp
More file actions
234 lines (219 loc) · 9.95 KB
/
Copy pathException.cpp
File metadata and controls
234 lines (219 loc) · 9.95 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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
#include "Inline/BasicTypes.h"
#include "Logging/Logging.h"
#include "Runtime.h"
#include "RuntimePrivate.h"
#include "Intrinsics.h"
namespace Runtime
{
static const TupleType* unitTupleType = TupleType::get({});
const GCPointer<ExceptionTypeInstance> Exception::accessViolationType = createExceptionTypeInstance(unitTupleType);
const GCPointer<ExceptionTypeInstance> Exception::stackOverflowType = createExceptionTypeInstance(unitTupleType);
const GCPointer<ExceptionTypeInstance> Exception::integerDivideByZeroOrIntegerOverflowType = createExceptionTypeInstance(unitTupleType);
const GCPointer<ExceptionTypeInstance> Exception::invalidFloatOperationType = createExceptionTypeInstance(unitTupleType);
const GCPointer<ExceptionTypeInstance> Exception::invokeSignatureMismatchType = createExceptionTypeInstance(unitTupleType);
const GCPointer<ExceptionTypeInstance> Exception::reachedUnreachableType = createExceptionTypeInstance(unitTupleType);
const GCPointer<ExceptionTypeInstance> Exception::indirectCallSignatureMismatchType = createExceptionTypeInstance(unitTupleType);
const GCPointer<ExceptionTypeInstance> Exception::undefinedTableElementType = createExceptionTypeInstance(unitTupleType);
const GCPointer<ExceptionTypeInstance> Exception::calledAbortType = createExceptionTypeInstance(unitTupleType);
const GCPointer<ExceptionTypeInstance> Exception::calledUnimplementedIntrinsicType = createExceptionTypeInstance(unitTupleType);
const GCPointer<ExceptionTypeInstance> Exception::outOfMemoryType = createExceptionTypeInstance(unitTupleType);
const GCPointer<ExceptionTypeInstance> Exception::invalidSegmentOffsetType = createExceptionTypeInstance(unitTupleType);
const GCPointer<ExceptionTypeInstance> Exception::misalignedAtomicMemoryAccessType = createExceptionTypeInstance(unitTupleType);
const GCPointer<ExceptionTypeInstance> Exception::invalidArgumentType = createExceptionTypeInstance(unitTupleType);
// Returns a vector of strings, each element describing a frame of the call stack.
// If the frame is a JITed function, use the JIT's information about the function
// to describe it, otherwise fallback to whatever platform-specific symbol resolution
// is available.
std::vector<std::string> describeCallStack(const Platform::CallStack& callStack)
{
std::vector<std::string> frameDescriptions;
for(auto frame : callStack.stackFrames)
{
std::string frameDescription;
if( LLVMJIT::describeInstructionPointer(frame.ip,frameDescription)
|| Platform::describeInstructionPointer(frame.ip,frameDescription))
{
frameDescriptions.push_back(frameDescription);
}
else { frameDescriptions.push_back("<unknown function>"); }
}
return frameDescriptions;
}
ExceptionTypeInstance* createExceptionTypeInstance(const IR::TupleType* parameters)
{
return new ExceptionTypeInstance(parameters);
}
std::string describeExceptionType(const ExceptionTypeInstance* type)
{
assert(type);
if(type == Exception::accessViolationType) { return "access violation"; }
else if(type == Exception::stackOverflowType) { return "stack overflow"; }
else if(type == Exception::integerDivideByZeroOrIntegerOverflowType) { return "integer divide-by-zero or overflow"; }
else if(type == Exception::invalidFloatOperationType) { return "invalid float operation"; }
else if(type == Exception::invokeSignatureMismatchType) { return "invoke signature mismatch"; }
else if(type == Exception::reachedUnreachableType) { return "reached unreachable"; }
else if(type == Exception::indirectCallSignatureMismatchType) { return "indirect call signature mismatch"; }
else if(type == Exception::undefinedTableElementType) { return "undefined table element"; }
else if(type == Exception::calledAbortType) { return "called abort"; }
else if(type == Exception::calledUnimplementedIntrinsicType) { return "called unimplemented intrinsic"; }
else if(type == Exception::outOfMemoryType) { return "out of memory"; }
else if(type == Exception::invalidSegmentOffsetType) { return "invalid segment offset"; }
else if(type == Exception::misalignedAtomicMemoryAccessType) { return "misaligned atomic memory access"; }
else if(type == Exception::invalidArgumentType) { return "invalid argument"; }
else
{
std::string result = "user exception<" + std::to_string(reinterpret_cast<Uptr>(type)) + ">(";
for(Uptr parameterIndex = 0;parameterIndex < type->parameters->elements.size();++parameterIndex)
{
if(parameterIndex != 0) { result += ','; }
result += asString(type->parameters->elements[parameterIndex]);
}
result += ')';
return result;
}
}
const IR::TupleType* getExceptionTypeParameters(const ExceptionTypeInstance* type)
{
return type->parameters;
}
std::string describeException(const Exception& exception)
{
std::string result = describeExceptionType(exception.type);
assert(exception.arguments.size() == exception.type->parameters->elements.size());
if(exception.arguments.size())
{
result += '(';
for(Uptr argumentIndex = 0;argumentIndex < exception.arguments.size();++argumentIndex)
{
if(argumentIndex != 0) { result += ','; }
result += asString(Value(exception.type->parameters->elements[argumentIndex],exception.arguments[argumentIndex]));
}
result += ')';
}
std::vector<std::string> callStackDescription = describeCallStack(exception.callStack);
result += "\nCall stack:\n";
for(auto calledFunction : callStackDescription) { result += " "; result += calledFunction.c_str(); result += '\n'; }
return result;
}
[[noreturn]] void throwException(ExceptionTypeInstance* type,std::vector<UntaggedValue>&& arguments)
{
assert(arguments.size() == type->parameters->elements.size());
ExceptionData* exceptionData = (ExceptionData*)malloc(ExceptionData::calcNumBytes(type->parameters->elements.size()));
exceptionData->typeInstance = type;
exceptionData->isUserException = 0;
memcpy(exceptionData->arguments,arguments.data(),sizeof(UntaggedValue) * arguments.size());
Platform::raisePlatformException(exceptionData);
}
DEFINE_INTRINSIC_FUNCTION(wavmIntrinsics,"throwException",void,intrinsicThrowException,
I64 exceptionTypeInstanceBits, I64 argsBits, I32 isUserException)
{
auto typeInstance = reinterpret_cast<ExceptionTypeInstance*>(Uptr(exceptionTypeInstanceBits));
auto args = reinterpret_cast<const UntaggedValue*>(Uptr(argsBits));
ExceptionData* exceptionData = (ExceptionData*)malloc(ExceptionData::calcNumBytes(typeInstance->parameters->elements.size()));
exceptionData->typeInstance = typeInstance;
exceptionData->isUserException = isUserException ? 1 : 0;
memcpy(exceptionData->arguments,args,sizeof(UntaggedValue) * typeInstance->parameters->elements.size());
Platform::raisePlatformException(exceptionData);
}
static Exception translateExceptionDataToException(
const ExceptionData* exceptionData,
const Platform::CallStack& callStack)
{
ExceptionTypeInstance* runtimeType = exceptionData->typeInstance;
std::vector<UntaggedValue> arguments(
exceptionData->arguments,
exceptionData->arguments + exceptionData->typeInstance->parameters->elements.size());
return Exception { runtimeType, std::move(arguments), callStack };
}
static bool translateSignalToRuntimeException(
const Platform::Signal& signal,
const Platform::CallStack& callStack,
Runtime::Exception& outException)
{
switch(signal.type)
{
case Platform::Signal::Type::accessViolation:
{
// If the access violation occured in a Table's reserved pages, treat it as an undefined table element runtime error.
if(isAddressOwnedByTable(reinterpret_cast<U8*>(signal.accessViolation.address)))
{
outException = Exception { Exception::undefinedTableElementType, {}, callStack };
return true;
}
// If the access violation occured in a Memory's reserved pages, treat it as an access violation runtime error.
else if(isAddressOwnedByMemory(reinterpret_cast<U8*>(signal.accessViolation.address)))
{
outException = Exception { Exception::accessViolationType, {}, callStack };
return true;
}
return false;
}
case Platform::Signal::Type::stackOverflow:
outException = Exception { Exception::stackOverflowType, {}, callStack };
return true;
case Platform::Signal::Type::intDivideByZeroOrOverflow:
outException = Exception { Exception::integerDivideByZeroOrIntegerOverflowType, {}, callStack };
return true;
case Platform::Signal::Type::unhandledException:
outException = translateExceptionDataToException(
reinterpret_cast<const ExceptionData*>(signal.unhandledException.data),
callStack);
return true;
default: Errors::unreachable();
}
}
void catchRuntimeExceptions(
const std::function<void()>& thunk,
const std::function<void(Exception&&)>& catchThunk
)
{
// Catch platform exceptions and translate them into C++ exceptions.
Result result;
Platform::catchPlatformExceptions(
[thunk,catchThunk]
{
Platform::catchSignals(
thunk,
[catchThunk](Platform::Signal signal,const Platform::CallStack& callStack) -> bool
{
Exception exception;
if(translateSignalToRuntimeException(signal,callStack,exception))
{
catchThunk(std::move(exception));
return true;
}
else { return false; }
});
},
[catchThunk](void* exceptionData,const Platform::CallStack& callStack)
{
catchThunk(translateExceptionDataToException(
reinterpret_cast<ExceptionData*>(exceptionData),
callStack));
});
}
static std::atomic<UnhandledExceptionHandler> unhandledExceptionHandler;
static bool globalSignalHandler(
Platform::Signal signal,
const Platform::CallStack& callStack)
{
Exception exception;
if(translateSignalToRuntimeException(signal,callStack,exception))
{
(unhandledExceptionHandler.load())(std::move(exception));
return true;
}
else { return false; }
}
void setUnhandledExceptionHandler(UnhandledExceptionHandler handler)
{
struct SignalHandlerRegistrar
{
SignalHandlerRegistrar()
{
Platform::setSignalHandler(globalSignalHandler);
}
} signalHandlerRegistrar;
unhandledExceptionHandler.store(handler);
}
}