| 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 "WasmThunks.h" |
| 28 | |
| 29 | #if ENABLE(WEBASSEMBLY) |
| 30 | |
| 31 | #include "CCallHelpers.h" |
| 32 | #include "HeapCellInlines.h" |
| 33 | #include "JITExceptions.h" |
| 34 | #include "LinkBuffer.h" |
| 35 | #include "ScratchRegisterAllocator.h" |
| 36 | #include "WasmContext.h" |
| 37 | #include "WasmExceptionType.h" |
| 38 | #include "WasmInstance.h" |
| 39 | #include "WasmOMGPlan.h" |
| 40 | |
| 41 | namespace JSC { namespace Wasm { |
| 42 | |
| 43 | MacroAssemblerCodeRef<JITThunkPtrTag> throwExceptionFromWasmThunkGenerator(const AbstractLocker&) |
| 44 | { |
| 45 | CCallHelpers jit; |
| 46 | |
| 47 | // The thing that jumps here must move ExceptionType into the argumentGPR1 before jumping here. |
| 48 | // We're allowed to use temp registers here. We are not allowed to use callee saves. |
| 49 | jit.loadWasmContextInstance(GPRInfo::argumentGPR2); |
| 50 | jit.loadPtr(CCallHelpers::Address(GPRInfo::argumentGPR2, Instance::offsetOfPointerToTopEntryFrame()), GPRInfo::argumentGPR0); |
| 51 | jit.loadPtr(CCallHelpers::Address(GPRInfo::argumentGPR0), GPRInfo::argumentGPR0); |
| 52 | jit.copyCalleeSavesToEntryFrameCalleeSavesBuffer(GPRInfo::argumentGPR0); |
| 53 | jit.move(GPRInfo::callFrameRegister, GPRInfo::argumentGPR0); |
| 54 | |
| 55 | CCallHelpers::Call call = jit.call(OperationPtrTag); |
| 56 | jit.jump(GPRInfo::returnValueGPR, ExceptionHandlerPtrTag); |
| 57 | jit.breakpoint(); // We should not reach this. |
| 58 | |
| 59 | ThrowWasmException throwWasmException = Thunks::singleton().throwWasmException(); |
| 60 | RELEASE_ASSERT(throwWasmException); |
| 61 | LinkBuffer linkBuffer(jit, GLOBAL_THUNK_ID); |
| 62 | linkBuffer.link(call, FunctionPtr<OperationPtrTag>(throwWasmException)); |
| 63 | return FINALIZE_CODE(linkBuffer, JITThunkPtrTag, "Throw exception from Wasm" ); |
| 64 | } |
| 65 | |
| 66 | MacroAssemblerCodeRef<JITThunkPtrTag> throwStackOverflowFromWasmThunkGenerator(const AbstractLocker& locker) |
| 67 | { |
| 68 | CCallHelpers jit; |
| 69 | |
| 70 | int32_t stackSpace = WTF::roundUpToMultipleOf(stackAlignmentBytes(), RegisterSet::calleeSaveRegisters().numberOfSetRegisters() * sizeof(Register)); |
| 71 | ASSERT(static_cast<unsigned>(stackSpace) < Options::softReservedZoneSize()); |
| 72 | jit.addPtr(CCallHelpers::TrustedImm32(-stackSpace), GPRInfo::callFrameRegister, MacroAssembler::stackPointerRegister); |
| 73 | jit.move(CCallHelpers::TrustedImm32(static_cast<uint32_t>(ExceptionType::StackOverflow)), GPRInfo::argumentGPR1); |
| 74 | auto jumpToExceptionHandler = jit.jump(); |
| 75 | LinkBuffer linkBuffer(jit, GLOBAL_THUNK_ID); |
| 76 | linkBuffer.link(jumpToExceptionHandler, CodeLocationLabel<JITThunkPtrTag>(Thunks::singleton().stub(locker, throwExceptionFromWasmThunkGenerator).code())); |
| 77 | return FINALIZE_CODE(linkBuffer, JITThunkPtrTag, "Throw stack overflow from Wasm" ); |
| 78 | } |
| 79 | |
| 80 | MacroAssemblerCodeRef<JITThunkPtrTag> triggerOMGTierUpThunkGenerator(const AbstractLocker&) |
| 81 | { |
| 82 | // We expect that the user has already put the function index into GPRInfo::argumentGPR1 |
| 83 | CCallHelpers jit; |
| 84 | |
| 85 | jit.emitFunctionPrologue(); |
| 86 | |
| 87 | const unsigned = 0; |
| 88 | RegisterSet registersToSpill = RegisterSet::allRegisters(); |
| 89 | registersToSpill.exclude(RegisterSet::registersToNotSaveForCCall()); |
| 90 | unsigned numberOfStackBytesUsedForRegisterPreservation = ScratchRegisterAllocator::preserveRegistersToStackForCall(jit, registersToSpill, extraPaddingBytes); |
| 91 | |
| 92 | jit.loadWasmContextInstance(GPRInfo::argumentGPR0); |
| 93 | typedef void (*Run)(Instance*, uint32_t); |
| 94 | Run run = OMGPlan::runForIndex; |
| 95 | jit.move(MacroAssembler::TrustedImmPtr(tagCFunctionPtr<OperationPtrTag>(run)), GPRInfo::argumentGPR2); |
| 96 | jit.call(GPRInfo::argumentGPR2, OperationPtrTag); |
| 97 | |
| 98 | ScratchRegisterAllocator::restoreRegistersFromStackForCall(jit, registersToSpill, RegisterSet(), numberOfStackBytesUsedForRegisterPreservation, extraPaddingBytes); |
| 99 | |
| 100 | jit.emitFunctionEpilogue(); |
| 101 | jit.ret(); |
| 102 | LinkBuffer linkBuffer(jit, GLOBAL_THUNK_ID); |
| 103 | return FINALIZE_CODE(linkBuffer, JITThunkPtrTag, "Trigger OMG tier up" ); |
| 104 | } |
| 105 | |
| 106 | static Thunks* thunks; |
| 107 | void Thunks::initialize() |
| 108 | { |
| 109 | thunks = new Thunks; |
| 110 | } |
| 111 | |
| 112 | Thunks& Thunks::singleton() |
| 113 | { |
| 114 | ASSERT(thunks); |
| 115 | return *thunks; |
| 116 | } |
| 117 | |
| 118 | void Thunks::setThrowWasmException(ThrowWasmException throwWasmException) |
| 119 | { |
| 120 | auto locker = holdLock(m_lock); |
| 121 | // The thunks are unique for the entire process, therefore changing the throwing function changes it for all uses of WebAssembly. |
| 122 | RELEASE_ASSERT(!m_throwWasmException || m_throwWasmException == throwWasmException); |
| 123 | m_throwWasmException = throwWasmException; |
| 124 | } |
| 125 | |
| 126 | ThrowWasmException Thunks::throwWasmException() |
| 127 | { |
| 128 | return m_throwWasmException; |
| 129 | } |
| 130 | |
| 131 | MacroAssemblerCodeRef<JITThunkPtrTag> Thunks::stub(ThunkGenerator generator) |
| 132 | { |
| 133 | auto locker = holdLock(m_lock); |
| 134 | return stub(locker, generator); |
| 135 | } |
| 136 | |
| 137 | MacroAssemblerCodeRef<JITThunkPtrTag> Thunks::stub(const AbstractLocker& locker, ThunkGenerator generator) |
| 138 | { |
| 139 | ASSERT(!!generator); |
| 140 | { |
| 141 | auto addResult = m_stubs.add(generator, MacroAssemblerCodeRef<JITThunkPtrTag>()); |
| 142 | if (!addResult.isNewEntry) |
| 143 | return addResult.iterator->value; |
| 144 | } |
| 145 | |
| 146 | MacroAssemblerCodeRef<JITThunkPtrTag> code = generator(locker); |
| 147 | // We specifically don't use the iterator here to allow generator to recursively change m_stubs. |
| 148 | m_stubs.set(generator, code); |
| 149 | return code; |
| 150 | } |
| 151 | |
| 152 | MacroAssemblerCodeRef<JITThunkPtrTag> Thunks::existingStub(ThunkGenerator generator) |
| 153 | { |
| 154 | auto locker = holdLock(m_lock); |
| 155 | |
| 156 | auto iter = m_stubs.find(generator); |
| 157 | if (iter != m_stubs.end()) |
| 158 | return iter->value; |
| 159 | |
| 160 | return MacroAssemblerCodeRef<JITThunkPtrTag>(); |
| 161 | } |
| 162 | |
| 163 | } } // namespace JSC::Wasm |
| 164 | |
| 165 | #endif // ENABLE(WEBASSEMBLY) |
| 166 | |