WasmFaultSignalHandler.cpp [plain text]
#include "config.h"
#include "WasmFaultSignalHandler.h"
#if ENABLE(WEBASSEMBLY)
#include "ExecutableAllocator.h"
#include "LLIntData.h"
#include "MachineContext.h"
#include "WasmCallee.h"
#include "WasmCalleeRegistry.h"
#include "WasmCapabilities.h"
#include "WasmContextInlines.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_SIGNALING_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()));
dataLogLnIf(WasmFaultSignalHandlerInternal::verbose, "WasmLLInt memory start: ", RawPointer(untagCodePtr<void*, CFunctionPtrTag>(LLInt::wasmLLIntPCRangeStart)), " end: ", RawPointer(untagCodePtr<void*, CFunctionPtrTag>(LLInt::wasmLLIntPCRangeEnd)));
assertIsNotTagged(faultingInstruction);
if (isJITPC(faultingInstruction) || LLInt::isWasmLLIntPC(faultingInstruction)) {
bool faultedInActiveGrowableMemory = false;
{
void* faultingAddress = sigInfo.faultingAddress;
dataLogLnIf(WasmFaultSignalHandlerInternal::verbose, "checking faulting address: ", RawPointer(faultingAddress), " is in an active fast memory");
faultedInActiveGrowableMemory = Wasm::Memory::addressIsInGrowableOrFastMemory(faultingAddress);
}
if (faultedInActiveGrowableMemory) {
dataLogLnIf(WasmFaultSignalHandlerInternal::verbose, "found active fast memory for faulting address");
auto didFaultInWasm = [](void* faultingInstruction) {
if (LLInt::isWasmLLIntPC(faultingInstruction))
return true;
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");
return true;
}
}
return false;
};
if (didFaultInWasm(faultingInstruction)) {
MachineContext::argumentPointer<1>(context) = reinterpret_cast<void*>(static_cast<uintptr_t>(Wasm::Context::useFastTLS()));
MachineContext::setInstructionPointer(context, LLInt::getCodePtr<CFunctionPtrTag>(wasm_throw_from_fault_handler_trampoline));
return SignalAction::Handled;
}
}
}
return SignalAction::NotHandled;
}
#endif // ENABLE(WEBASSEMBLY_SIGNALING_MEMORY)
bool fastMemoryEnabled()
{
return fastHandlerInstalled;
}
void enableFastMemory()
{
#if ENABLE(WEBASSEMBLY_SIGNALING_MEMORY)
static std::once_flag once;
std::call_once(once, [] {
if (!Wasm::isSupported())
return;
if (!Options::useWebAssemblyFastMemory() && !Options::useSharedArrayBuffer())
return;
activateSignalHandlersFor(Signal::AccessFault);
fastHandlerInstalled = true;
});
#endif
}
void prepareFastMemory()
{
#if ENABLE(WEBASSEMBLY_SIGNALING_MEMORY)
static std::once_flag once;
std::call_once(once, [] {
if (!Wasm::isSupported())
return;
if (!Options::useWebAssemblyFastMemory() && !Options::useSharedArrayBuffer())
return;
addSignalHandler(Signal::AccessFault, [] (Signal signal, SigInfo& sigInfo, PlatformRegisters& ucontext) {
return trapHandler(signal, sigInfo, ucontext);
});
});
#endif // ENABLE(WEBASSEMBLY_SIGNALING_MEMORY)
}
} }
#endif // ENABLE(WEBASSEMBLY)