| 1 | /* |
| 2 | * Copyright (C) 2017-2018 Apple Inc. All rights reserved. |
| 3 | * |
| 4 | * Redistribution and use in source and binary forms, with or without |
| 5 | * modification, are permitted provided that the following conditions |
| 6 | * are met: |
| 7 | * 1. Redistributions of source code must retain the above copyright |
| 8 | * notice, this list of conditions and the following disclaimer. |
| 9 | * 2. Redistributions in binary form must reproduce the above copyright |
| 10 | * notice, this list of conditions and the following disclaimer in the |
| 11 | * documentation and/or other materials provided with the distribution. |
| 12 | * |
| 13 | * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY |
| 14 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 15 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 16 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR |
| 17 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 18 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 19 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 20 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
| 21 | * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 22 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 23 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 24 | */ |
| 25 | |
| 26 | #include "config.h" |
| 27 | #include "WasmFaultSignalHandler.h" |
| 28 | |
| 29 | #if ENABLE(WEBASSEMBLY) |
| 30 | |
| 31 | #include "ExecutableAllocator.h" |
| 32 | #include "MachineContext.h" |
| 33 | #include "WasmCapabilities.h" |
| 34 | #include "WasmExceptionType.h" |
| 35 | #include "WasmMemory.h" |
| 36 | #include "WasmThunks.h" |
| 37 | |
| 38 | #include <wtf/HashSet.h> |
| 39 | #include <wtf/Lock.h> |
| 40 | #include <wtf/NeverDestroyed.h> |
| 41 | #include <wtf/threads/Signals.h> |
| 42 | |
| 43 | namespace JSC { namespace Wasm { |
| 44 | |
| 45 | namespace { |
| 46 | namespace WasmFaultSignalHandlerInternal { |
| 47 | static const bool verbose = false; |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | static Lock codeLocationsLock; |
| 52 | static LazyNeverDestroyed<HashSet<std::tuple<void*, void*>>> codeLocations; // (start, end) |
| 53 | |
| 54 | static bool fastHandlerInstalled { false }; |
| 55 | |
| 56 | #if ENABLE(WEBASSEMBLY_FAST_MEMORY) |
| 57 | |
| 58 | static SignalAction trapHandler(Signal, SigInfo& sigInfo, PlatformRegisters& context) |
| 59 | { |
| 60 | auto instructionPointer = MachineContext::instructionPointer(context); |
| 61 | if (!instructionPointer) |
| 62 | return SignalAction::NotHandled; |
| 63 | void* faultingInstruction = instructionPointer->untaggedExecutableAddress(); |
| 64 | dataLogLnIf(WasmFaultSignalHandlerInternal::verbose, "starting handler for fault at: " , RawPointer(faultingInstruction)); |
| 65 | |
| 66 | dataLogLnIf(WasmFaultSignalHandlerInternal::verbose, "JIT memory start: " , RawPointer(startOfFixedExecutableMemoryPool()), " end: " , RawPointer(endOfFixedExecutableMemoryPool())); |
| 67 | // First we need to make sure we are in JIT code before we can aquire any locks. Otherwise, |
| 68 | // we might have crashed in code that is already holding one of the locks we want to aquire. |
| 69 | assertIsNotTagged(faultingInstruction); |
| 70 | if (isJITPC(faultingInstruction)) { |
| 71 | bool faultedInActiveFastMemory = false; |
| 72 | { |
| 73 | void* faultingAddress = sigInfo.faultingAddress; |
| 74 | dataLogLnIf(WasmFaultSignalHandlerInternal::verbose, "checking faulting address: " , RawPointer(faultingAddress), " is in an active fast memory" ); |
| 75 | faultedInActiveFastMemory = Wasm::Memory::addressIsInActiveFastMemory(faultingAddress); |
| 76 | } |
| 77 | if (faultedInActiveFastMemory) { |
| 78 | dataLogLnIf(WasmFaultSignalHandlerInternal::verbose, "found active fast memory for faulting address" ); |
| 79 | LockHolder locker(codeLocationsLock); |
| 80 | for (auto range : codeLocations.get()) { |
| 81 | void* start; |
| 82 | void* end; |
| 83 | std::tie(start, end) = range; |
| 84 | dataLogLnIf(WasmFaultSignalHandlerInternal::verbose, "function start: " , RawPointer(start), " end: " , RawPointer(end)); |
| 85 | if (start <= faultingInstruction && faultingInstruction < end) { |
| 86 | dataLogLnIf(WasmFaultSignalHandlerInternal::verbose, "found match" ); |
| 87 | MacroAssemblerCodeRef<JITThunkPtrTag> exceptionStub = Thunks::singleton().existingStub(throwExceptionFromWasmThunkGenerator); |
| 88 | // If for whatever reason we don't have a stub then we should just treat this like a regular crash. |
| 89 | if (!exceptionStub) |
| 90 | break; |
| 91 | dataLogLnIf(WasmFaultSignalHandlerInternal::verbose, "found stub: " , RawPointer(exceptionStub.code().executableAddress())); |
| 92 | MachineContext::argumentPointer<1>(context) = reinterpret_cast<void*>(ExceptionType::OutOfBoundsMemoryAccess); |
| 93 | MachineContext::setInstructionPointer(context, exceptionStub.code().retagged<CFunctionPtrTag>()); |
| 94 | return SignalAction::Handled; |
| 95 | } |
| 96 | } |
| 97 | } |
| 98 | } |
| 99 | return SignalAction::NotHandled; |
| 100 | } |
| 101 | |
| 102 | #endif // ENABLE(WEBASSEMBLY_FAST_MEMORY) |
| 103 | |
| 104 | void registerCode(void* start, void* end) |
| 105 | { |
| 106 | if (!fastMemoryEnabled()) |
| 107 | return; |
| 108 | LockHolder locker(codeLocationsLock); |
| 109 | codeLocations->add(std::make_tuple(start, end)); |
| 110 | } |
| 111 | |
| 112 | void unregisterCode(void* start, void* end) |
| 113 | { |
| 114 | if (!fastMemoryEnabled()) |
| 115 | return; |
| 116 | LockHolder locker(codeLocationsLock); |
| 117 | codeLocations->remove(std::make_tuple(start, end)); |
| 118 | } |
| 119 | |
| 120 | bool fastMemoryEnabled() |
| 121 | { |
| 122 | return fastHandlerInstalled; |
| 123 | } |
| 124 | |
| 125 | void enableFastMemory() |
| 126 | { |
| 127 | #if ENABLE(WEBASSEMBLY_FAST_MEMORY) |
| 128 | static std::once_flag once; |
| 129 | std::call_once(once, [] { |
| 130 | if (!Wasm::isSupported()) |
| 131 | return; |
| 132 | |
| 133 | if (!Options::useWebAssemblyFastMemory()) |
| 134 | return; |
| 135 | |
| 136 | installSignalHandler(Signal::BadAccess, [] (Signal signal, SigInfo& sigInfo, PlatformRegisters& ucontext) { |
| 137 | return trapHandler(signal, sigInfo, ucontext); |
| 138 | }); |
| 139 | |
| 140 | codeLocations.construct(); |
| 141 | fastHandlerInstalled = true; |
| 142 | }); |
| 143 | #endif // ENABLE(WEBASSEMBLY_FAST_MEMORY) |
| 144 | } |
| 145 | |
| 146 | } } // namespace JSC::Wasm |
| 147 | |
| 148 | #endif // ENABLE(WEBASSEMBLY) |
| 149 | |
| 150 | |