WasmFaultSignalHandler.cpp [plain text]
#include "config.h"
#include "WasmFaultSignalHandler.h"
#if ENABLE(WEBASSEMBLY)
#include "ExecutableAllocator.h"
#include "MachineContext.h"
#include "WasmExceptionType.h"
#include "WasmMemory.h"
#include "WasmThunks.h"
#include <wtf/HashSet.h>
#include <wtf/Lock.h>
#include <wtf/NeverDestroyed.h>
#include <wtf/threads/Signals.h>
namespace JSC { namespace Wasm {
namespace {
namespace WasmFaultSignalHandlerInternal {
static const bool verbose = false;
}
}
static StaticLock codeLocationsLock;
static LazyNeverDestroyed<HashSet<std::tuple<void*, void*>>> codeLocations;
#if ENABLE(WEBASSEMBLY_FAST_MEMORY)
static bool fastHandlerInstalled { false };
static SignalAction trapHandler(Signal, SigInfo& sigInfo, PlatformRegisters& context)
{
void* faultingInstruction = MachineContext::instructionPointer(context);
dataLogLnIf(WasmFaultSignalHandlerInternal::verbose, "starting handler for fault at: ", RawPointer(faultingInstruction));
dataLogLnIf(WasmFaultSignalHandlerInternal::verbose, "JIT memory start: ", RawPointer(reinterpret_cast<void*>(startOfFixedExecutableMemoryPool)), " end: ", RawPointer(reinterpret_cast<void*>(endOfFixedExecutableMemoryPool)));
if (isJITPC(faultingInstruction)) {
bool faultedInActiveFastMemory = false;
{
void* faultingAddress = sigInfo.faultingAddress;
dataLogLnIf(WasmFaultSignalHandlerInternal::verbose, "checking faulting address: ", RawPointer(faultingAddress), " is in an active fast memory");
faultedInActiveFastMemory = Wasm::Memory::addressIsInActiveFastMemory(faultingAddress);
}
if (faultedInActiveFastMemory) {
dataLogLnIf(WasmFaultSignalHandlerInternal::verbose, "found active fast memory for faulting address");
LockHolder locker(codeLocationsLock);
for (auto range : codeLocations.get()) {
void* start;
void* end;
std::tie(start, end) = range;
dataLogLnIf(WasmFaultSignalHandlerInternal::verbose, "function start: ", RawPointer(start), " end: ", RawPointer(end));
if (start <= faultingInstruction && faultingInstruction < end) {
dataLogLnIf(WasmFaultSignalHandlerInternal::verbose, "found match");
MacroAssemblerCodeRef exceptionStub = Thunks::singleton().existingStub(throwExceptionFromWasmThunkGenerator);
if (!exceptionStub)
break;
dataLogLnIf(WasmFaultSignalHandlerInternal::verbose, "found stub: ", RawPointer(exceptionStub.code().executableAddress()));
MachineContext::argumentPointer<1>(context) = reinterpret_cast<void*>(ExceptionType::OutOfBoundsMemoryAccess);
MachineContext::instructionPointer(context) = exceptionStub.code().executableAddress();
return SignalAction::Handled;
}
}
}
}
return SignalAction::NotHandled;
}
#endif // ENABLE(WEBASSEMBLY_FAST_MEMORY)
void registerCode(void* start, void* end)
{
if (!fastMemoryEnabled())
return;
LockHolder locker(codeLocationsLock);
codeLocations->add(std::make_tuple(start, end));
}
void unregisterCode(void* start, void* end)
{
if (!fastMemoryEnabled())
return;
LockHolder locker(codeLocationsLock);
codeLocations->remove(std::make_tuple(start, end));
}
bool fastMemoryEnabled()
{
return fastHandlerInstalled;
}
void enableFastMemory()
{
static std::once_flag once;
std::call_once(once, [] {
if (!Options::useWebAssemblyFastMemory())
return;
#if ENABLE(WEBASSEMBLY_FAST_MEMORY)
installSignalHandler(Signal::BadAccess, [] (Signal signal, SigInfo& sigInfo, PlatformRegisters& ucontext) {
return trapHandler(signal, sigInfo, ucontext);
});
codeLocations.construct();
fastHandlerInstalled = true;
#endif // ENABLE(WEBASSEMBLY_FAST_MEMORY)
});
}
} }
#endif // ENABLE(WEBASSEMBLY)