WasmFaultSignalHandler.cpp [plain text]
#include "config.h"
#include "WasmFaultSignalHandler.h"
#if ENABLE(WEBASSEMBLY)
#include "ExecutableAllocator.h"
#include "MachineContext.h"
#include "WasmCallee.h"
#include "WasmCalleeRegistry.h"
#include "WasmCapabilities.h"
#include "WasmExceptionType.h"
#include "WasmMemory.h"
#include "WasmThunks.h"
#include <wtf/HashSet.h>
#include <wtf/Lock.h>
#include <wtf/threads/Signals.h>
namespace JSC { namespace Wasm {
namespace {
namespace WasmFaultSignalHandlerInternal {
static constexpr bool verbose = false;
}
}
static bool fastHandlerInstalled { false };
#if ENABLE(WEBASSEMBLY_FAST_MEMORY)
static SignalAction trapHandler(Signal, SigInfo& sigInfo, PlatformRegisters& context)
{
auto instructionPointer = MachineContext::instructionPointer(context);
if (!instructionPointer)
return SignalAction::NotHandled;
void* faultingInstruction = instructionPointer->untaggedExecutableAddress();
dataLogLnIf(WasmFaultSignalHandlerInternal::verbose, "starting handler for fault at: ", RawPointer(faultingInstruction));
dataLogLnIf(WasmFaultSignalHandlerInternal::verbose, "JIT memory start: ", RawPointer(startOfFixedExecutableMemoryPool()), " end: ", RawPointer(endOfFixedExecutableMemoryPool()));
assertIsNotTagged(faultingInstruction);
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");
auto& calleeRegistry = CalleeRegistry::singleton();
auto locker = holdLock(calleeRegistry.getLock());
for (auto* callee : calleeRegistry.allCallees(locker)) {
auto [start, end] = callee->range();
dataLogLnIf(WasmFaultSignalHandlerInternal::verbose, "function start: ", RawPointer(start), " end: ", RawPointer(end));
if (start <= faultingInstruction && faultingInstruction < end) {
dataLogLnIf(WasmFaultSignalHandlerInternal::verbose, "found match");
MacroAssemblerCodeRef<JITThunkPtrTag> 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::setInstructionPointer(context, exceptionStub.code().retagged<CFunctionPtrTag>());
return SignalAction::Handled;
}
}
}
}
return SignalAction::NotHandled;
}
#endif // ENABLE(WEBASSEMBLY_FAST_MEMORY)
bool fastMemoryEnabled()
{
return fastHandlerInstalled;
}
void enableFastMemory()
{
#if ENABLE(WEBASSEMBLY_FAST_MEMORY)
static std::once_flag once;
std::call_once(once, [] {
if (!Wasm::isSupported())
return;
if (!Options::useWebAssemblyFastMemory())
return;
activateSignalHandlersFor(Signal::AccessFault);
fastHandlerInstalled = true;
});
#endif
}
void prepareFastMemory()
{
#if ENABLE(WEBASSEMBLY_FAST_MEMORY)
static std::once_flag once;
std::call_once(once, [] {
if (!Wasm::isSupported())
return;
if (!Options::useWebAssemblyFastMemory())
return;
addSignalHandler(Signal::AccessFault, [] (Signal signal, SigInfo& sigInfo, PlatformRegisters& ucontext) {
return trapHandler(signal, sigInfo, ucontext);
});
});
#endif // ENABLE(WEBASSEMBLY_FAST_MEMORY)
}
} }
#endif // ENABLE(WEBASSEMBLY)