| 1 | /* |
| 2 | * Copyright (C) 2016-2019 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 "WebAssemblyFunction.h" |
| 28 | |
| 29 | #if ENABLE(WEBASSEMBLY) |
| 30 | |
| 31 | #include "B3Compilation.h" |
| 32 | #include "FrameTracers.h" |
| 33 | #include "JSCInlines.h" |
| 34 | #include "JSFunctionInlines.h" |
| 35 | #include "JSObject.h" |
| 36 | #include "JSToWasm.h" |
| 37 | #include "JSWebAssemblyHelpers.h" |
| 38 | #include "JSWebAssemblyInstance.h" |
| 39 | #include "JSWebAssemblyMemory.h" |
| 40 | #include "JSWebAssemblyRuntimeError.h" |
| 41 | #include "LLIntThunks.h" |
| 42 | #include "LinkBuffer.h" |
| 43 | #include "ProtoCallFrameInlines.h" |
| 44 | #include "VM.h" |
| 45 | #include "WasmCallee.h" |
| 46 | #include "WasmCallingConvention.h" |
| 47 | #include "WasmContextInlines.h" |
| 48 | #include "WasmFormat.h" |
| 49 | #include "WasmMemory.h" |
| 50 | #include "WasmMemoryInformation.h" |
| 51 | #include "WasmModuleInformation.h" |
| 52 | #include "WasmSignatureInlines.h" |
| 53 | #include <wtf/FastTLS.h> |
| 54 | #include <wtf/StackPointer.h> |
| 55 | #include <wtf/SystemTracing.h> |
| 56 | |
| 57 | namespace JSC { |
| 58 | |
| 59 | const ClassInfo WebAssemblyFunction::s_info = { "WebAssemblyFunction" , &Base::s_info, nullptr, nullptr, CREATE_METHOD_TABLE(WebAssemblyFunction) }; |
| 60 | |
| 61 | static EncodedJSValue JSC_HOST_CALL callWebAssemblyFunction(JSGlobalObject* globalObject, CallFrame* callFrame) |
| 62 | { |
| 63 | VM& vm = globalObject->vm(); |
| 64 | auto scope = DECLARE_THROW_SCOPE(vm); |
| 65 | WebAssemblyFunction* wasmFunction = jsCast<WebAssemblyFunction*>(callFrame->jsCallee()); |
| 66 | Wasm::SignatureIndex signatureIndex = wasmFunction->signatureIndex(); |
| 67 | const Wasm::Signature& signature = Wasm::SignatureInformation::get(signatureIndex); |
| 68 | |
| 69 | // Make sure that the memory we think we are going to run with matches the one we expect. |
| 70 | ASSERT(wasmFunction->instance()->instance().codeBlock()->isSafeToRun(wasmFunction->instance()->memory()->memory().mode())); |
| 71 | |
| 72 | Optional<TraceScope> traceScope; |
| 73 | if (Options::useTracePoints()) |
| 74 | traceScope.emplace(WebAssemblyExecuteStart, WebAssemblyExecuteEnd); |
| 75 | |
| 76 | Vector<JSValue, MarkedArgumentBuffer::inlineCapacity> boxedArgs; |
| 77 | JSWebAssemblyInstance* instance = wasmFunction->instance(); |
| 78 | Wasm::Instance* wasmInstance = &instance->instance(); |
| 79 | |
| 80 | for (unsigned argIndex = 0; argIndex < signature.argumentCount(); ++argIndex) { |
| 81 | JSValue arg = callFrame->argument(argIndex); |
| 82 | switch (signature.argument(argIndex)) { |
| 83 | case Wasm::I32: |
| 84 | arg = JSValue::decode(arg.toInt32(globalObject)); |
| 85 | break; |
| 86 | case Wasm::Funcref: { |
| 87 | if (!isWebAssemblyHostFunction(vm, arg) && !arg.isNull()) |
| 88 | return JSValue::encode(throwException(globalObject, scope, createJSWebAssemblyRuntimeError(globalObject, vm, "Funcref must be an exported wasm function" ))); |
| 89 | break; |
| 90 | } |
| 91 | case Wasm::Anyref: |
| 92 | break; |
| 93 | case Wasm::I64: |
| 94 | arg = JSValue(); |
| 95 | break; |
| 96 | case Wasm::F32: |
| 97 | arg = JSValue::decode(bitwise_cast<uint32_t>(arg.toFloat(globalObject))); |
| 98 | break; |
| 99 | case Wasm::F64: |
| 100 | arg = JSValue::decode(bitwise_cast<uint64_t>(arg.toNumber(globalObject))); |
| 101 | break; |
| 102 | case Wasm::Void: |
| 103 | case Wasm::Func: |
| 104 | RELEASE_ASSERT_NOT_REACHED(); |
| 105 | } |
| 106 | RETURN_IF_EXCEPTION(scope, encodedJSValue()); |
| 107 | boxedArgs.append(arg); |
| 108 | } |
| 109 | |
| 110 | // When we don't use fast TLS to store the context, the JS |
| 111 | // entry wrapper expects a JSWebAssemblyInstance as the |this| value argument. |
| 112 | JSValue context = Wasm::Context::useFastTLS() ? JSValue() : instance; |
| 113 | JSValue* args = boxedArgs.data(); |
| 114 | int argCount = boxedArgs.size() + 1; |
| 115 | |
| 116 | // Note: we specifically use the WebAssemblyFunction as the callee to begin with in the ProtoCallFrame. |
| 117 | // The reason for this is that calling into the llint may stack overflow, and the stack overflow |
| 118 | // handler might read the global object from the callee. |
| 119 | ProtoCallFrame protoCallFrame; |
| 120 | protoCallFrame.init(nullptr, globalObject, wasmFunction, context, argCount, args); |
| 121 | |
| 122 | // FIXME Do away with this entire function, and only use the entrypoint generated by B3. https://bugs.webkit.org/show_bug.cgi?id=166486 |
| 123 | Wasm::Instance* prevWasmInstance = vm.wasmContext.load(); |
| 124 | { |
| 125 | // We do the stack check here for the wrapper function because we don't |
| 126 | // want to emit a stack check inside every wrapper function. |
| 127 | const intptr_t sp = bitwise_cast<intptr_t>(currentStackPointer()); |
| 128 | const intptr_t frameSize = (boxedArgs.size() + CallFrame::headerSizeInRegisters) * sizeof(Register); |
| 129 | const intptr_t stackSpaceUsed = 2 * frameSize; // We're making two calls. One to the wrapper, and one to the actual wasm code. |
| 130 | if (UNLIKELY((sp < stackSpaceUsed) || ((sp - stackSpaceUsed) < bitwise_cast<intptr_t>(vm.softStackLimit())))) |
| 131 | return JSValue::encode(throwException(globalObject, scope, createStackOverflowError(globalObject))); |
| 132 | } |
| 133 | vm.wasmContext.store(wasmInstance, vm.softStackLimit()); |
| 134 | ASSERT(wasmFunction->instance()); |
| 135 | ASSERT(&wasmFunction->instance()->instance() == vm.wasmContext.load()); |
| 136 | EncodedJSValue rawResult = vmEntryToWasm(wasmFunction->jsEntrypoint(MustCheckArity).executableAddress(), &vm, &protoCallFrame); |
| 137 | // We need to make sure this is in a register or on the stack since it's stored in Vector<JSValue>. |
| 138 | // This probably isn't strictly necessary, since the WebAssemblyFunction* should keep the instance |
| 139 | // alive. But it's good hygiene. |
| 140 | instance->use(); |
| 141 | if (prevWasmInstance != wasmInstance) { |
| 142 | // This is just for some extra safety instead of leaving a cached |
| 143 | // value in there. If we ever forget to set the value to be a real |
| 144 | // bounds, this will force every stack overflow check to immediately |
| 145 | // fire. The stack limit never changes while executing except when |
| 146 | // WebAssembly is used through the JSC API: API users can ask the code |
| 147 | // to migrate threads. |
| 148 | wasmInstance->setCachedStackLimit(bitwise_cast<void*>(std::numeric_limits<uintptr_t>::max())); |
| 149 | } |
| 150 | vm.wasmContext.store(prevWasmInstance, vm.softStackLimit()); |
| 151 | RETURN_IF_EXCEPTION(scope, { }); |
| 152 | |
| 153 | return rawResult; |
| 154 | } |
| 155 | |
| 156 | bool WebAssemblyFunction::useTagRegisters() const |
| 157 | { |
| 158 | const auto& signature = Wasm::SignatureInformation::get(signatureIndex()); |
| 159 | return signature.argumentCount() || !signature.returnsVoid(); |
| 160 | } |
| 161 | |
| 162 | RegisterSet WebAssemblyFunction::calleeSaves() const |
| 163 | { |
| 164 | return Wasm::PinnedRegisterInfo::get().toSave(instance()->memoryMode()); |
| 165 | } |
| 166 | |
| 167 | RegisterAtOffsetList WebAssemblyFunction::usedCalleeSaveRegisters() const |
| 168 | { |
| 169 | return RegisterAtOffsetList { calleeSaves(), RegisterAtOffsetList::OffsetBaseType::FramePointerBased }; |
| 170 | } |
| 171 | |
| 172 | ptrdiff_t WebAssemblyFunction::previousInstanceOffset() const |
| 173 | { |
| 174 | ptrdiff_t result = calleeSaves().numberOfSetRegisters() * sizeof(CPURegister); |
| 175 | result = -result - sizeof(CPURegister); |
| 176 | #if !ASSERT_DISABLED |
| 177 | ptrdiff_t minOffset = 1; |
| 178 | for (const RegisterAtOffset& regAtOffset : usedCalleeSaveRegisters()) { |
| 179 | ptrdiff_t offset = regAtOffset.offset(); |
| 180 | ASSERT(offset < 0); |
| 181 | minOffset = std::min(offset, minOffset); |
| 182 | } |
| 183 | ASSERT(minOffset - static_cast<ptrdiff_t>(sizeof(CPURegister)) == result); |
| 184 | #endif |
| 185 | return result; |
| 186 | } |
| 187 | |
| 188 | Wasm::Instance* WebAssemblyFunction::previousInstance(CallFrame* callFrame) |
| 189 | { |
| 190 | ASSERT(callFrame->callee().rawPtr() == m_jsToWasmICCallee.get()); |
| 191 | auto* result = *bitwise_cast<Wasm::Instance**>(bitwise_cast<char*>(callFrame) + previousInstanceOffset()); |
| 192 | return result; |
| 193 | } |
| 194 | |
| 195 | MacroAssemblerCodePtr<JSEntryPtrTag> WebAssemblyFunction::jsCallEntrypointSlow() |
| 196 | { |
| 197 | VM& vm = this->vm(); |
| 198 | CCallHelpers jit; |
| 199 | |
| 200 | const auto& signature = Wasm::SignatureInformation::get(signatureIndex()); |
| 201 | const auto& pinnedRegs = Wasm::PinnedRegisterInfo::get(); |
| 202 | RegisterAtOffsetList registersToSpill = usedCalleeSaveRegisters(); |
| 203 | |
| 204 | auto& moduleInformation = instance()->instance().module().moduleInformation(); |
| 205 | |
| 206 | const Wasm::WasmCallingConvention& wasmCC = Wasm::wasmCallingConvention(); |
| 207 | Wasm::CallInformation wasmCallInfo = wasmCC.callInformationFor(signature); |
| 208 | Wasm::CallInformation jsCallInfo = Wasm::jsCallingConvention().callInformationFor(signature, Wasm::CallRole::Callee); |
| 209 | RegisterAtOffsetList savedResultRegisters = wasmCallInfo.computeResultsOffsetList(); |
| 210 | |
| 211 | unsigned totalFrameSize = registersToSpill.size() * sizeof(CPURegister); |
| 212 | totalFrameSize += sizeof(CPURegister); // Slot for the VM's previous wasm instance. |
| 213 | totalFrameSize += wasmCallInfo.headerAndArgumentStackSizeInBytes; |
| 214 | totalFrameSize += savedResultRegisters.size() * sizeof(CPURegister); |
| 215 | |
| 216 | if (wasmCallInfo.argumentsIncludeI64 || wasmCallInfo.resultsIncludeI64) |
| 217 | return nullptr; |
| 218 | |
| 219 | totalFrameSize = WTF::roundUpToMultipleOf(stackAlignmentBytes(), totalFrameSize); |
| 220 | |
| 221 | jit.emitFunctionPrologue(); |
| 222 | jit.subPtr(MacroAssembler::TrustedImm32(totalFrameSize), MacroAssembler::stackPointerRegister); |
| 223 | jit.store64(CCallHelpers::TrustedImm64(0), CCallHelpers::addressFor(CallFrameSlot::codeBlock)); |
| 224 | |
| 225 | for (const RegisterAtOffset& regAtOffset : registersToSpill) { |
| 226 | GPRReg reg = regAtOffset.reg().gpr(); |
| 227 | ptrdiff_t offset = regAtOffset.offset(); |
| 228 | jit.storePtr(reg, CCallHelpers::Address(GPRInfo::callFrameRegister, offset)); |
| 229 | } |
| 230 | |
| 231 | GPRReg scratchGPR = Wasm::wasmCallingConvention().prologueScratchGPRs[1]; |
| 232 | bool stackLimitGPRIsClobbered = false; |
| 233 | GPRReg stackLimitGPR = Wasm::wasmCallingConvention().prologueScratchGPRs[0]; |
| 234 | jit.loadPtr(vm.addressOfSoftStackLimit(), stackLimitGPR); |
| 235 | |
| 236 | CCallHelpers::JumpList slowPath; |
| 237 | slowPath.append(jit.branchPtr(CCallHelpers::Above, MacroAssembler::stackPointerRegister, GPRInfo::callFrameRegister)); |
| 238 | slowPath.append(jit.branchPtr(CCallHelpers::Below, MacroAssembler::stackPointerRegister, stackLimitGPR)); |
| 239 | |
| 240 | // Ensure: |
| 241 | // argCountPlusThis - 1 >= signature.argumentCount() |
| 242 | // argCountPlusThis >= signature.argumentCount() + 1 |
| 243 | // FIXME: We should handle mismatched arity |
| 244 | // https://bugs.webkit.org/show_bug.cgi?id=196564 |
| 245 | slowPath.append(jit.branch32(CCallHelpers::Below, |
| 246 | CCallHelpers::payloadFor(CallFrameSlot::argumentCount), CCallHelpers::TrustedImm32(signature.argumentCount() + 1))); |
| 247 | |
| 248 | if (useTagRegisters()) |
| 249 | jit.emitMaterializeTagCheckRegisters(); |
| 250 | |
| 251 | // Loop backwards so we can use the first floating point argument as a scratch. |
| 252 | FPRReg scratchFPR = Wasm::wasmCallingConvention().fprArgs[0].fpr(); |
| 253 | for (unsigned i = signature.argumentCount(); i--;) { |
| 254 | CCallHelpers::Address calleeFrame = CCallHelpers::Address(MacroAssembler::stackPointerRegister, 0); |
| 255 | CCallHelpers::Address jsParam(GPRInfo::callFrameRegister, jsCallInfo.params[i].offsetFromFP()); |
| 256 | bool isStack = wasmCallInfo.params[i].isStackArgument(); |
| 257 | |
| 258 | auto type = signature.argument(i); |
| 259 | switch (type) { |
| 260 | case Wasm::I32: { |
| 261 | jit.load64(jsParam, scratchGPR); |
| 262 | slowPath.append(jit.branchIfNotInt32(scratchGPR)); |
| 263 | if (isStack) |
| 264 | jit.store32(scratchGPR, calleeFrame.withOffset(wasmCallInfo.params[i].offsetFromSP())); |
| 265 | else |
| 266 | jit.zeroExtend32ToPtr(scratchGPR, wasmCallInfo.params[i].gpr()); |
| 267 | break; |
| 268 | } |
| 269 | case Wasm::Funcref: { |
| 270 | // Ensure we have a WASM exported function. |
| 271 | jit.load64(jsParam, scratchGPR); |
| 272 | auto isNull = jit.branchIfNull(scratchGPR); |
| 273 | slowPath.append(jit.branchIfNotCell(scratchGPR)); |
| 274 | |
| 275 | stackLimitGPRIsClobbered = true; |
| 276 | jit.emitLoadStructure(vm, scratchGPR, scratchGPR, stackLimitGPR); |
| 277 | jit.loadPtr(CCallHelpers::Address(scratchGPR, Structure::classInfoOffset()), scratchGPR); |
| 278 | |
| 279 | static_assert(std::is_final<WebAssemblyFunction>::value, "We do not check for subtypes below" ); |
| 280 | static_assert(std::is_final<WebAssemblyWrapperFunction>::value, "We do not check for subtypes below" ); |
| 281 | |
| 282 | auto isWasmFunction = jit.branchPtr(CCallHelpers::Equal, scratchGPR, CCallHelpers::TrustedImmPtr(WebAssemblyFunction::info())); |
| 283 | slowPath.append(jit.branchPtr(CCallHelpers::NotEqual, scratchGPR, CCallHelpers::TrustedImmPtr(WebAssemblyWrapperFunction::info()))); |
| 284 | |
| 285 | isWasmFunction.link(&jit); |
| 286 | isNull.link(&jit); |
| 287 | FALLTHROUGH; |
| 288 | } |
| 289 | case Wasm::Anyref: { |
| 290 | if (isStack) { |
| 291 | jit.load64(jsParam, scratchGPR); |
| 292 | jit.store64(scratchGPR, calleeFrame.withOffset(wasmCallInfo.params[i].offsetFromSP())); |
| 293 | } else |
| 294 | jit.load64(jsParam, wasmCallInfo.params[i].gpr()); |
| 295 | break; |
| 296 | } |
| 297 | case Wasm::F32: |
| 298 | case Wasm::F64: { |
| 299 | if (!isStack) |
| 300 | scratchFPR = wasmCallInfo.params[i].fpr(); |
| 301 | auto moveToDestination = [&] () { |
| 302 | if (isStack) { |
| 303 | if (signature.argument(i) == Wasm::F32) |
| 304 | jit.storeFloat(scratchFPR, calleeFrame.withOffset(wasmCallInfo.params[i].offsetFromSP())); |
| 305 | else |
| 306 | jit.storeDouble(scratchFPR, calleeFrame.withOffset(wasmCallInfo.params[i].offsetFromSP())); |
| 307 | } |
| 308 | }; |
| 309 | |
| 310 | jit.load64(jsParam, scratchGPR); |
| 311 | slowPath.append(jit.branchIfNotNumber(scratchGPR)); |
| 312 | auto isInt32 = jit.branchIfInt32(scratchGPR); |
| 313 | |
| 314 | jit.unboxDouble(scratchGPR, scratchGPR, scratchFPR); |
| 315 | if (signature.argument(i) == Wasm::F32) |
| 316 | jit.convertDoubleToFloat(scratchFPR, scratchFPR); |
| 317 | moveToDestination(); |
| 318 | auto done = jit.jump(); |
| 319 | |
| 320 | isInt32.link(&jit); |
| 321 | if (signature.argument(i) == Wasm::F32) { |
| 322 | jit.convertInt32ToFloat(scratchGPR, scratchFPR); |
| 323 | moveToDestination(); |
| 324 | } else { |
| 325 | jit.convertInt32ToDouble(scratchGPR, scratchFPR); |
| 326 | moveToDestination(); |
| 327 | } |
| 328 | done.link(&jit); |
| 329 | |
| 330 | break; |
| 331 | } |
| 332 | default: |
| 333 | RELEASE_ASSERT_NOT_REACHED(); |
| 334 | } |
| 335 | } |
| 336 | |
| 337 | // At this point, we're committed to doing a fast call. |
| 338 | |
| 339 | if (Wasm::Context::useFastTLS()) |
| 340 | jit.loadWasmContextInstance(scratchGPR); |
| 341 | else |
| 342 | jit.loadPtr(vm.wasmContext.pointerToInstance(), scratchGPR); |
| 343 | ptrdiff_t previousInstanceOffset = this->previousInstanceOffset(); |
| 344 | jit.storePtr(scratchGPR, CCallHelpers::Address(GPRInfo::callFrameRegister, previousInstanceOffset)); |
| 345 | |
| 346 | jit.move(CCallHelpers::TrustedImmPtr(&instance()->instance()), scratchGPR); |
| 347 | if (Wasm::Context::useFastTLS()) |
| 348 | jit.storeWasmContextInstance(scratchGPR); |
| 349 | else { |
| 350 | jit.move(scratchGPR, pinnedRegs.wasmContextInstancePointer); |
| 351 | jit.storePtr(scratchGPR, vm.wasmContext.pointerToInstance()); |
| 352 | } |
| 353 | if (stackLimitGPRIsClobbered) |
| 354 | jit.loadPtr(vm.addressOfSoftStackLimit(), stackLimitGPR); |
| 355 | jit.storePtr(stackLimitGPR, CCallHelpers::Address(scratchGPR, Wasm::Instance::offsetOfCachedStackLimit())); |
| 356 | |
| 357 | if (!!moduleInformation.memory) { |
| 358 | GPRReg baseMemory = pinnedRegs.baseMemoryPointer; |
| 359 | GPRReg scratchOrSize = stackLimitGPR; |
| 360 | auto mode = instance()->memoryMode(); |
| 361 | |
| 362 | if (isARM64E()) { |
| 363 | if (mode != Wasm::MemoryMode::Signaling) |
| 364 | scratchOrSize = pinnedRegs.sizeRegister; |
| 365 | jit.loadPtr(CCallHelpers::Address(scratchGPR, Wasm::Instance::offsetOfCachedMemorySize()), scratchOrSize); |
| 366 | } else { |
| 367 | if (mode != Wasm::MemoryMode::Signaling) |
| 368 | jit.loadPtr(CCallHelpers::Address(scratchGPR, Wasm::Instance::offsetOfCachedMemorySize()), pinnedRegs.sizeRegister); |
| 369 | } |
| 370 | |
| 371 | jit.loadPtr(CCallHelpers::Address(scratchGPR, Wasm::Instance::offsetOfCachedMemory()), baseMemory); |
| 372 | jit.cageConditionally(Gigacage::Primitive, baseMemory, scratchOrSize, scratchOrSize); |
| 373 | } |
| 374 | |
| 375 | // We use this callee to indicate how to unwind past these types of frames: |
| 376 | // 1. We need to know where to get callee saves. |
| 377 | // 2. We need to know to restore the previous wasm context. |
| 378 | if (!m_jsToWasmICCallee) |
| 379 | m_jsToWasmICCallee.set(vm, this, JSToWasmICCallee::create(vm, globalObject(), this)); |
| 380 | jit.storePtr(CCallHelpers::TrustedImmPtr(m_jsToWasmICCallee.get()), CCallHelpers::addressFor(CallFrameSlot::callee)); |
| 381 | |
| 382 | { |
| 383 | // FIXME: Currently we just do an indirect jump. But we should teach the Module |
| 384 | // how to repatch us: |
| 385 | // https://bugs.webkit.org/show_bug.cgi?id=196570 |
| 386 | jit.loadPtr(entrypointLoadLocation(), scratchGPR); |
| 387 | jit.call(scratchGPR, WasmEntryPtrTag); |
| 388 | } |
| 389 | |
| 390 | marshallJSResult(jit, signature, wasmCallInfo, savedResultRegisters); |
| 391 | |
| 392 | ASSERT(!RegisterSet::runtimeTagRegisters().contains(GPRInfo::nonPreservedNonReturnGPR)); |
| 393 | jit.loadPtr(CCallHelpers::Address(GPRInfo::callFrameRegister, previousInstanceOffset), GPRInfo::nonPreservedNonReturnGPR); |
| 394 | if (Wasm::Context::useFastTLS()) |
| 395 | jit.storeWasmContextInstance(GPRInfo::nonPreservedNonReturnGPR); |
| 396 | else |
| 397 | jit.storePtr(GPRInfo::nonPreservedNonReturnGPR, vm.wasmContext.pointerToInstance()); |
| 398 | |
| 399 | auto emitRestoreCalleeSaves = [&] { |
| 400 | for (const RegisterAtOffset& regAtOffset : registersToSpill) { |
| 401 | GPRReg reg = regAtOffset.reg().gpr(); |
| 402 | ASSERT(reg != GPRInfo::returnValueGPR); |
| 403 | ptrdiff_t offset = regAtOffset.offset(); |
| 404 | jit.loadPtr(CCallHelpers::Address(GPRInfo::callFrameRegister, offset), reg); |
| 405 | } |
| 406 | }; |
| 407 | |
| 408 | emitRestoreCalleeSaves(); |
| 409 | |
| 410 | jit.emitFunctionEpilogue(); |
| 411 | jit.ret(); |
| 412 | |
| 413 | slowPath.link(&jit); |
| 414 | emitRestoreCalleeSaves(); |
| 415 | jit.move(CCallHelpers::TrustedImmPtr(this), GPRInfo::regT0); |
| 416 | jit.emitFunctionEpilogue(); |
| 417 | #if CPU(ARM64E) |
| 418 | jit.untagReturnAddress(); |
| 419 | #endif |
| 420 | auto jumpToHostCallThunk = jit.jump(); |
| 421 | |
| 422 | LinkBuffer linkBuffer(jit, nullptr, JITCompilationCanFail); |
| 423 | if (UNLIKELY(linkBuffer.didFailToAllocate())) |
| 424 | return nullptr; |
| 425 | |
| 426 | linkBuffer.link(jumpToHostCallThunk, CodeLocationLabel<JSEntryPtrTag>(executable()->entrypointFor(CodeForCall, MustCheckArity).executableAddress())); |
| 427 | m_jsCallEntrypoint = FINALIZE_WASM_CODE(linkBuffer, WasmEntryPtrTag, "JS->Wasm IC" ); |
| 428 | return m_jsCallEntrypoint.code(); |
| 429 | } |
| 430 | |
| 431 | WebAssemblyFunction* WebAssemblyFunction::create(VM& vm, JSGlobalObject* globalObject, Structure* structure, unsigned length, const String& name, JSWebAssemblyInstance* instance, Wasm::Callee& jsEntrypoint, Wasm::WasmToWasmImportableFunction::LoadLocation wasmToWasmEntrypointLoadLocation, Wasm::SignatureIndex signatureIndex) |
| 432 | { |
| 433 | NativeExecutable* executable = vm.getHostFunction(callWebAssemblyFunction, NoIntrinsic, callHostFunctionAsConstructor, nullptr, name); |
| 434 | WebAssemblyFunction* function = new (NotNull, allocateCell<WebAssemblyFunction>(vm.heap)) WebAssemblyFunction(vm, globalObject, structure, jsEntrypoint, wasmToWasmEntrypointLoadLocation, signatureIndex); |
| 435 | function->finishCreation(vm, executable, length, name, instance); |
| 436 | return function; |
| 437 | } |
| 438 | |
| 439 | Structure* WebAssemblyFunction::createStructure(VM& vm, JSGlobalObject* globalObject, JSValue prototype) |
| 440 | { |
| 441 | ASSERT(globalObject); |
| 442 | return Structure::create(vm, globalObject, prototype, TypeInfo(JSFunctionType, StructureFlags), info()); |
| 443 | } |
| 444 | |
| 445 | WebAssemblyFunction::WebAssemblyFunction(VM& vm, JSGlobalObject* globalObject, Structure* structure, Wasm::Callee& jsEntrypoint, Wasm::WasmToWasmImportableFunction::LoadLocation wasmToWasmEntrypointLoadLocation, Wasm::SignatureIndex signatureIndex) |
| 446 | : Base { vm, globalObject, structure } |
| 447 | , m_jsEntrypoint { jsEntrypoint.entrypoint() } |
| 448 | , m_importableFunction { signatureIndex, wasmToWasmEntrypointLoadLocation } |
| 449 | { } |
| 450 | |
| 451 | void WebAssemblyFunction::visitChildren(JSCell* cell, SlotVisitor& visitor) |
| 452 | { |
| 453 | WebAssemblyFunction* thisObject = jsCast<WebAssemblyFunction*>(cell); |
| 454 | ASSERT_GC_OBJECT_INHERITS(thisObject, info()); |
| 455 | |
| 456 | Base::visitChildren(thisObject, visitor); |
| 457 | visitor.append(thisObject->m_jsToWasmICCallee); |
| 458 | } |
| 459 | |
| 460 | void WebAssemblyFunction::destroy(JSCell* cell) |
| 461 | { |
| 462 | static_cast<WebAssemblyFunction*>(cell)->WebAssemblyFunction::~WebAssemblyFunction(); |
| 463 | } |
| 464 | |
| 465 | } // namespace JSC |
| 466 | |
| 467 | #endif // ENABLE(WEBASSEMBLY) |
| 468 | |