1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532
|
/*
* Copyright (C) 2016-2024 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "config.h"
#include "WasmToJS.h"
#if ENABLE(WEBASSEMBLY) && ENABLE(JIT)
#include "CCallHelpers.h"
#include "JSCJSValueInlines.h"
#include "JSWebAssemblyInstance.h"
#include "LinkBuffer.h"
#include "MaxFrameExtentForSlowPathCall.h"
#include "ThunkGenerators.h"
#include "WasmCallingConvention.h"
#include "WasmContext.h"
#include "WasmExceptionType.h"
#include "WasmOperations.h"
#include "WasmTypeDefinitionInlines.h"
#include <wtf/FunctionTraits.h>
namespace JSC { namespace Wasm {
using JIT = CCallHelpers;
static void materializeImportJSCell(JIT& jit, unsigned importIndex, GPRReg result)
{
// We're calling out of the current WebAssembly.Instance. That JSWebAssemblyInstance has a list of all its import functions.
jit.loadPtr(JIT::Address(GPRInfo::wasmContextInstancePointer, JSWebAssemblyInstance::offsetOfImportFunction(importIndex)), result);
}
static Expected<MacroAssemblerCodeRef<WasmEntryPtrTag>, BindingFailure> handleBadImportTypeUse(JIT& jit, unsigned importIndex, Wasm::ExceptionType exceptionType)
{
jit.move(GPRInfo::wasmContextInstancePointer, GPRInfo::argumentGPR0);
emitThrowWasmToJSException(jit, GPRInfo::argumentGPR0, exceptionType);
LinkBuffer linkBuffer(jit, GLOBAL_THUNK_ID, LinkBuffer::Profile::WasmThunk, JITCompilationCanFail);
if (UNLIKELY(linkBuffer.didFailToAllocate()))
return makeUnexpected(BindingFailure::OutOfMemory);
return FINALIZE_WASM_CODE(linkBuffer, WasmEntryPtrTag, nullptr, "WebAssembly->JavaScript throw exception due to invalid use of restricted type in import[%i]", importIndex);
}
Expected<MacroAssemblerCodeRef<WasmEntryPtrTag>, BindingFailure> wasmToJS(TypeIndex typeIndex, unsigned importIndex)
{
// FIXME: This function doesn't properly abstract away the calling convention.
// It'd be super easy to do so: https://bugs.webkit.org/show_bug.cgi?id=169401
const auto& wasmCC = wasmCallingConvention();
const auto& jsCC = jsCallingConvention();
const TypeDefinition& typeDefinition = TypeInformation::get(typeIndex).expand();
const auto& signature = *typeDefinition.as<FunctionSignature>();
unsigned argCount = signature.argumentCount();
constexpr GPRReg importJSCellGPRReg = GPRInfo::regT0; // Callee needs to be in regT0 for slow path below.
JIT jit;
CallInformation wasmCallInfo = wasmCC.callInformationFor(typeDefinition, CallRole::Callee);
RegisterAtOffsetList savedResultRegisters = wasmCallInfo.computeResultsOffsetList();
// Note: WasmOMGIRGenerator assumes that this stub treats SP as a callee save.
// If we ever change this, we will also need to change WasmOMGIRGenerator.
// Below, we assume that the JS calling convention is always on the stack.
ASSERT_UNUSED(jsCC, !jsCC.jsrArgs.size());
ASSERT(!jsCC.fprArgs.size());
jit.emitFunctionPrologue();
GPRReg scratchGPR = GPRInfo::nonPreservedNonArgumentGPR0;
JIT_COMMENT(jit, "Store callee from ptr: ", RawPointer(&WasmToJSCallee::singleton()), " value, ", RawPointer(CalleeBits::boxNativeCallee(&WasmToJSCallee::singleton())));
jit.move(CCallHelpers::TrustedImmPtr(CalleeBits::boxNativeCallee(&WasmToJSCallee::singleton())), scratchGPR);
static_assert(CallFrameSlot::codeBlock + 1 == CallFrameSlot::callee);
if constexpr (is32Bit()) {
CCallHelpers::Address calleeSlot { GPRInfo::callFrameRegister, CallFrameSlot::callee * sizeof(Register) };
jit.storePtr(scratchGPR, calleeSlot.withOffset(PayloadOffset));
jit.move(CCallHelpers::TrustedImm32(JSValue::NativeCalleeTag), scratchGPR);
jit.store32(scratchGPR, calleeSlot.withOffset(TagOffset));
jit.storePtr(GPRInfo::wasmContextInstancePointer, CCallHelpers::addressFor(CallFrameSlot::codeBlock));
} else
jit.storePairPtr(GPRInfo::wasmContextInstancePointer, scratchGPR, GPRInfo::callFrameRegister, CCallHelpers::TrustedImm32(CallFrameSlot::codeBlock * sizeof(Register)));
// https://webassembly.github.io/spec/js-api/index.html#exported-function-exotic-objects
// If parameters or results contain v128, throw a TypeError.
// Note: the above error is thrown each time the [[Call]] method is invoked.
if (UNLIKELY(signature.argumentsOrResultsIncludeV128() || signature.argumentsOrResultsIncludeExnref()))
return handleBadImportTypeUse(jit, importIndex, ExceptionType::TypeErrorInvalidValueUse);
// Here we assume that the JS calling convention saves at least all the wasm callee saved. We therefore don't need to save and restore more registers since the wasm callee already took care of this.
#if ASSERT_ENABLED
wasmCC.calleeSaveRegisters.forEachWithWidth([&] (Reg reg, Width width) {
ASSERT(jsCC.calleeSaveRegisters.contains(reg, width));
});
#endif
// Note: We don't need to perform a stack check here since WasmOMGIRGenerator
// will do the stack check for us. Whenever it detects that it might make
// a call to this thunk, it'll make sure its stack check includes space
// for us here.
const unsigned numberOfParameters = argCount + 1; // There is a "this" argument.
const unsigned numberOfRegsForCall = CallFrame::headerSizeInRegisters + roundArgumentCountToAlignFrame(numberOfParameters);
ASSERT(!(numberOfRegsForCall % stackAlignmentRegisters()));
const unsigned numberOfBytesForCall = numberOfRegsForCall * sizeof(Register) - sizeof(CallerFrameAndPC);
const unsigned numberOfBytesForSavedResults = savedResultRegisters.sizeOfAreaInBytes();
const unsigned stackOffset = WTF::roundUpToMultipleOf<stackAlignmentBytes()>(std::max(numberOfBytesForCall, numberOfBytesForSavedResults));
jit.subPtr(MacroAssembler::TrustedImm32(stackOffset), MacroAssembler::stackPointerRegister);
JIT::Address calleeFrame = CCallHelpers::Address(MacroAssembler::stackPointerRegister, -static_cast<ptrdiff_t>(sizeof(CallerFrameAndPC)));
// FIXME make these loops which switch on signature if there are many arguments on the stack. It'll otherwise be huge for huge type definitions. https://bugs.webkit.org/show_bug.cgi?id=165547
#if USE(JSVALUE64)
constexpr JSValueRegs jsArg10 { GPRInfo::argumentGPR0 };
#elif USE(JSVALUE32_64)
constexpr JSValueRegs jsArg10 { GPRInfo::argumentGPR1, GPRInfo::argumentGPR0 };
#endif
// First go through the integer parameters, freeing up their register for use afterwards.
{
unsigned marshalledGPRs = 0;
unsigned marshalledFPRs = 0;
unsigned calleeFrameOffset = CallFrameSlot::firstArgument * static_cast<int>(sizeof(Register));
unsigned frOffset = CallFrameSlot::firstArgument * static_cast<int>(sizeof(Register));
for (unsigned argNum = 0; argNum < argCount; ++argNum) {
Type argType = signature.argumentType(argNum);
switch (argType.kind) {
case TypeKind::Void:
case TypeKind::Func:
case TypeKind::Struct:
case TypeKind::Structref:
case TypeKind::Array:
case TypeKind::Arrayref:
case TypeKind::Eqref:
case TypeKind::Anyref:
case TypeKind::Nullexn:
case TypeKind::Nullref:
case TypeKind::Nullfuncref:
case TypeKind::Nullexternref:
case TypeKind::I31ref:
case TypeKind::Rec:
case TypeKind::Sub:
case TypeKind::Subfinal:
case TypeKind::V128:
RELEASE_ASSERT_NOT_REACHED(); // Handled above.
case TypeKind::RefNull:
case TypeKind::Ref:
case TypeKind::Exn:
case TypeKind::Externref:
case TypeKind::Funcref:
case TypeKind::I32:
case TypeKind::I64: {
JSValueRegs argReg;
if (marshalledGPRs < wasmCC.jsrArgs.size())
argReg = wasmCC.jsrArgs[marshalledGPRs];
else {
// We've already spilled all arguments, these registers are available as scratch.
argReg = jsArg10;
jit.loadValue(JIT::Address(GPRInfo::callFrameRegister, frOffset), argReg);
frOffset += sizeof(Register);
}
++marshalledGPRs;
if (argType.isI32()) {
jit.zeroExtend32ToWord(argReg.payloadGPR(), argReg.payloadGPR()); // Clear non-int32 and non-tag bits.
jit.boxInt32(argReg.payloadGPR(), argReg, DoNotHaveTagRegisters);
}
jit.storeValue(argReg, calleeFrame.withOffset(calleeFrameOffset));
calleeFrameOffset += sizeof(Register);
break;
}
case TypeKind::F32:
case TypeKind::F64:
// Skipped: handled below.
if (marshalledFPRs >= wasmCC.fprArgs.size())
frOffset += sizeof(Register);
++marshalledFPRs;
calleeFrameOffset += sizeof(Register);
break;
}
}
}
{
#if USE(JSVALUE64)
// Integer registers have already been spilled, these are now available.
GPRReg doubleEncodeOffsetGPRReg = GPRInfo::argumentGPR0;
GPRReg scratch = GPRInfo::argumentGPR1;
bool hasMaterializedDoubleEncodeOffset = false;
auto materializeDoubleEncodeOffset = [&hasMaterializedDoubleEncodeOffset, &jit] (GPRReg dest) {
if (!hasMaterializedDoubleEncodeOffset) {
#if CPU(ARM64)
jit.move(JIT::TrustedImm64(JSValue::DoubleEncodeOffset), dest);
#else
jit.move(JIT::TrustedImm32(1), dest);
jit.lshift64(JIT::TrustedImm32(JSValue::DoubleEncodeOffsetBit), dest);
#endif
hasMaterializedDoubleEncodeOffset = true;
}
};
#endif
unsigned marshalledGPRs = 0;
unsigned marshalledFPRs = 0;
unsigned calleeFrameOffset = CallFrameSlot::firstArgument * static_cast<int>(sizeof(Register));
unsigned frOffset = CallFrameSlot::firstArgument * static_cast<int>(sizeof(Register));
auto marshallFPR = [&] (FPRReg fprReg) {
jit.purifyNaN(fprReg, fprReg);
#if USE(JSVALUE64)
jit.moveDoubleTo64(fprReg, scratch);
materializeDoubleEncodeOffset(doubleEncodeOffsetGPRReg);
jit.add64(doubleEncodeOffsetGPRReg, scratch);
jit.store64(scratch, calleeFrame.withOffset(calleeFrameOffset));
#else
jit.storeDouble(fprReg, calleeFrame.withOffset(calleeFrameOffset));
#endif
calleeFrameOffset += sizeof(Register);
++marshalledFPRs;
};
for (unsigned argNum = 0; argNum < argCount; ++argNum) {
Type argType = signature.argumentType(argNum);
switch (argType.kind) {
case TypeKind::Void:
case TypeKind::Func:
case TypeKind::Struct:
case TypeKind::Structref:
case TypeKind::Array:
case TypeKind::Arrayref:
case TypeKind::Eqref:
case TypeKind::Anyref:
case TypeKind::Nullexn:
case TypeKind::Nullref:
case TypeKind::Nullfuncref:
case TypeKind::Nullexternref:
case TypeKind::I31ref:
case TypeKind::Rec:
case TypeKind::Sub:
case TypeKind::Subfinal:
case TypeKind::V128:
RELEASE_ASSERT_NOT_REACHED(); // Handled above.
case TypeKind::RefNull:
case TypeKind::Ref:
case TypeKind::Exn:
case TypeKind::Externref:
case TypeKind::Funcref:
case TypeKind::I32:
case TypeKind::I64: {
// Skipped: handled above.
if (marshalledGPRs >= wasmCC.jsrArgs.size())
frOffset += sizeof(Register);
++marshalledGPRs;
calleeFrameOffset += sizeof(Register);
break;
}
case TypeKind::F32: {
FPRReg fprReg;
if (marshalledFPRs < wasmCC.fprArgs.size())
fprReg = wasmCC.fprArgs[marshalledFPRs];
else {
// We've already spilled all arguments, these registers are available as scratch.
fprReg = FPRInfo::argumentFPR0;
jit.loadFloat(JIT::Address(GPRInfo::callFrameRegister, frOffset), fprReg);
frOffset += sizeof(Register);
}
jit.convertFloatToDouble(fprReg, fprReg);
marshallFPR(fprReg);
break;
}
case TypeKind::F64: {
FPRReg fprReg;
if (marshalledFPRs < wasmCC.fprArgs.size())
fprReg = wasmCC.fprArgs[marshalledFPRs];
else {
// We've already spilled all arguments, these registers are available as scratch.
fprReg = FPRInfo::argumentFPR0;
jit.loadDouble(JIT::Address(GPRInfo::callFrameRegister, frOffset), fprReg);
frOffset += sizeof(Register);
}
marshallFPR(fprReg);
break;
}
}
}
}
CCallHelpers::JumpList exceptionChecks;
if (signature.argumentsOrResultsIncludeI64()) {
// Since all argument GPRs and FPRs are stored into stack frames, clobbering caller-save registers is OK here.
// We call functions to convert I64 to BigInt.
unsigned calleeFrameOffset = CallFrameSlot::firstArgument * static_cast<int>(sizeof(Register));
for (unsigned argNum = 0; argNum < argCount; ++argNum) {
if (signature.argumentType(argNum).isI64()) {
using Operation = decltype(operationConvertToBigInt);
constexpr JSValueRegs valueJSR = preferredArgumentJSR<Operation, 1>();
jit.loadValue(calleeFrame.withOffset(calleeFrameOffset), valueJSR);
jit.prepareWasmCallOperation(GPRInfo::wasmContextInstancePointer);
jit.setupArguments<Operation>(GPRInfo::wasmContextInstancePointer, valueJSR);
jit.callOperation<OperationPtrTag>(operationConvertToBigInt);
jit.loadPtr(CCallHelpers::Address(GPRInfo::wasmContextInstancePointer, JSWebAssemblyInstance::offsetOfVM()), GPRInfo::nonPreservedNonReturnGPR);
exceptionChecks.append(jit.branchTestPtr(CCallHelpers::NonZero, CCallHelpers::Address(GPRInfo::nonPreservedNonReturnGPR, VM::exceptionOffset())));
jit.storeValue(JSRInfo::returnValueJSR, calleeFrame.withOffset(calleeFrameOffset));
}
calleeFrameOffset += sizeof(Register);
}
}
// jsArg10 might overlap with regT0, so store 'this' argument first
jit.storeValue(jsUndefined(), calleeFrame.withOffset(CallFrameSlot::thisArgument * static_cast<int>(sizeof(Register))), jsArg10);
ASSERT(!wasmCC.calleeSaveRegisters.contains(importJSCellGPRReg, IgnoreVectors));
materializeImportJSCell(jit, importIndex, importJSCellGPRReg);
jit.storeCell(importJSCellGPRReg, calleeFrame.withOffset(CallFrameSlot::callee * static_cast<int>(sizeof(Register))));
jit.store32(JIT::TrustedImm32(numberOfParameters), calleeFrame.withOffset(CallFrameSlot::argumentCountIncludingThis * static_cast<int>(sizeof(Register)) + PayloadOffset));
// FIXME Tail call if the wasm return type is void and no registers were spilled. https://bugs.webkit.org/show_bug.cgi?id=165488
// Callee needs to be in regT0 here.
jit.move(importJSCellGPRReg, BaselineJITRegisters::Call::calleeGPR);
#if USE(JSVALUE32_64)
jit.move(CCallHelpers::TrustedImm32(JSValue::CellTag), BaselineJITRegisters::Call::calleeJSR.tagGPR());
#endif
jit.loadPtr(CCallHelpers::Address(GPRInfo::wasmContextInstancePointer, JSWebAssemblyInstance::offsetOfCallLinkInfo(importIndex)), BaselineJITRegisters::Call::callLinkInfoGPR);
CallLinkInfo::emitDataICFastPath(jit);
if (signature.returnCount() == 1) {
const auto& returnType = signature.returnType(0);
switch (returnType.kind) {
case TypeKind::I64: {
// FIXME: Optimize I64 extraction from BigInt.
// https://bugs.webkit.org/show_bug.cgi?id=220053
JSValueRegs dest = wasmCallInfo.results[0].location.jsr();
jit.prepareWasmCallOperation(GPRInfo::wasmContextInstancePointer);
jit.setupArguments<decltype(operationConvertToI64)>(GPRInfo::wasmContextInstancePointer, JSRInfo::returnValueJSR);
jit.callOperation<OperationPtrTag>(operationConvertToI64);
jit.loadPtr(CCallHelpers::Address(GPRInfo::wasmContextInstancePointer, JSWebAssemblyInstance::offsetOfVM()), GPRInfo::nonPreservedNonReturnGPR);
exceptionChecks.append(jit.branchTestPtr(CCallHelpers::NonZero, CCallHelpers::Address(GPRInfo::nonPreservedNonReturnGPR, VM::exceptionOffset())));
jit.moveValueRegs(JSRInfo::returnValueJSR, dest);
break;
}
case TypeKind::I32: {
CCallHelpers::JumpList done;
CCallHelpers::JumpList slowPath;
JSValueRegs destJSR = wasmCallInfo.results[0].location.jsr();
slowPath.append(jit.branchIfNotNumber(JSRInfo::returnValueJSR, jit.scratchRegister(), DoNotHaveTagRegisters));
slowPath.append(jit.branchIfNotInt32(JSRInfo::returnValueJSR, DoNotHaveTagRegisters));
jit.zeroExtend32ToWord(JSRInfo::returnValueJSR.payloadGPR(), destJSR.payloadGPR());
done.append(jit.jump());
slowPath.link(&jit);
jit.prepareWasmCallOperation(GPRInfo::wasmContextInstancePointer);
jit.setupArguments<decltype(operationConvertToI32)>(GPRInfo::wasmContextInstancePointer, JSRInfo::returnValueJSR);
jit.callOperation<OperationPtrTag>(operationConvertToI32);
jit.loadPtr(CCallHelpers::Address(GPRInfo::wasmContextInstancePointer, JSWebAssemblyInstance::offsetOfVM()), GPRInfo::nonPreservedNonReturnGPR);
exceptionChecks.append(jit.branchTestPtr(CCallHelpers::NonZero, CCallHelpers::Address(GPRInfo::nonPreservedNonReturnGPR, VM::exceptionOffset())));
jit.move(JSRInfo::returnValueJSR.payloadGPR(), destJSR.payloadGPR());
#if USE(JSVALUE32_64)
jit.move(CCallHelpers::TrustedImm32(0), destJSR.tagGPR());
#endif
done.link(&jit);
break;
}
case TypeKind::F32: {
FPRReg dest = wasmCallInfo.results[0].location.fpr();
CCallHelpers::JumpList done;
auto notANumber = jit.branchIfNotNumber(JSRInfo::returnValueJSR, jit.scratchRegister(), DoNotHaveTagRegisters);
auto isDouble = jit.branchIfNotInt32(JSRInfo::returnValueJSR, DoNotHaveTagRegisters);
// We're an int32
jit.convertInt32ToFloat(GPRInfo::returnValueGPR, dest);
done.append(jit.jump());
isDouble.link(&jit);
#if USE(JSVALUE64)
jit.unboxDoubleWithoutAssertions(GPRInfo::returnValueGPR, GPRInfo::returnValueGPR2, dest, DoNotHaveTagRegisters);
#else
jit.unboxDouble(JSRInfo::returnValueJSR, dest);
#endif
jit.convertDoubleToFloat(dest, dest);
done.append(jit.jump());
notANumber.link(&jit);
jit.prepareWasmCallOperation(GPRInfo::wasmContextInstancePointer);
jit.setupArguments<decltype(operationConvertToF32)>(GPRInfo::wasmContextInstancePointer, JSRInfo::returnValueJSR);
jit.callOperation<OperationPtrTag>(operationConvertToF32);
jit.loadPtr(CCallHelpers::Address(GPRInfo::wasmContextInstancePointer, JSWebAssemblyInstance::offsetOfVM()), GPRInfo::nonPreservedNonReturnGPR);
exceptionChecks.append(jit.branchTestPtr(CCallHelpers::NonZero, CCallHelpers::Address(GPRInfo::nonPreservedNonReturnGPR, VM::exceptionOffset())));
jit.moveDouble(FPRInfo::returnValueFPR , dest);
done.link(&jit);
break;
}
case TypeKind::F64: {
FPRReg dest = wasmCallInfo.results[0].location.fpr();
CCallHelpers::JumpList done;
auto notANumber = jit.branchIfNotNumber(JSRInfo::returnValueJSR, jit.scratchRegister(), DoNotHaveTagRegisters);
auto isDouble = jit.branchIfNotInt32(JSValueRegs(JSRInfo::returnValueJSR), DoNotHaveTagRegisters);
// We're an int32
jit.convertInt32ToDouble(GPRInfo::returnValueGPR, dest);
done.append(jit.jump());
isDouble.link(&jit);
#if USE(JSVALUE64)
jit.unboxDoubleWithoutAssertions(GPRInfo::returnValueGPR, GPRInfo::returnValueGPR2, dest, DoNotHaveTagRegisters);
#else
jit.unboxDouble(JSRInfo::returnValueJSR, dest);
#endif
done.append(jit.jump());
notANumber.link(&jit);
jit.prepareWasmCallOperation(GPRInfo::wasmContextInstancePointer);
jit.setupArguments<decltype(operationConvertToF64)>(GPRInfo::wasmContextInstancePointer, JSRInfo::returnValueJSR);
jit.callOperation<OperationPtrTag>(operationConvertToF64);
jit.loadPtr(CCallHelpers::Address(GPRInfo::wasmContextInstancePointer, JSWebAssemblyInstance::offsetOfVM()), GPRInfo::nonPreservedNonReturnGPR);
exceptionChecks.append(jit.branchTestPtr(CCallHelpers::NonZero, CCallHelpers::Address(GPRInfo::nonPreservedNonReturnGPR, VM::exceptionOffset())));
jit.moveDouble(FPRInfo::returnValueFPR, dest);
done.link(&jit);
break;
}
default: {
if (Wasm::isRefType(returnType)) {
if (Wasm::isExternref(returnType)) {
// Do nothing.
} else if (Wasm::isFuncref(returnType)) {
jit.prepareWasmCallOperation(GPRInfo::wasmContextInstancePointer);
jit.setupArguments<decltype(operationConvertToFuncref)>(GPRInfo::wasmContextInstancePointer, CCallHelpers::TrustedImmPtr(&typeDefinition), JSRInfo::returnValueJSR);
jit.callOperation<OperationPtrTag>(operationConvertToFuncref);
jit.loadPtr(CCallHelpers::Address(GPRInfo::wasmContextInstancePointer, JSWebAssemblyInstance::offsetOfVM()), GPRInfo::nonPreservedNonReturnGPR);
exceptionChecks.append(jit.branchTestPtr(CCallHelpers::NonZero, CCallHelpers::Address(GPRInfo::nonPreservedNonReturnGPR, VM::exceptionOffset())));
} else {
jit.prepareWasmCallOperation(GPRInfo::wasmContextInstancePointer);
jit.setupArguments<decltype(operationConvertToAnyref)>(GPRInfo::wasmContextInstancePointer, CCallHelpers::TrustedImmPtr(&typeDefinition), JSRInfo::returnValueJSR);
jit.callOperation<OperationPtrTag>(operationConvertToAnyref);
jit.loadPtr(CCallHelpers::Address(GPRInfo::wasmContextInstancePointer, JSWebAssemblyInstance::offsetOfVM()), GPRInfo::nonPreservedNonReturnGPR);
exceptionChecks.append(jit.branchTestPtr(CCallHelpers::NonZero, CCallHelpers::Address(GPRInfo::nonPreservedNonReturnGPR, VM::exceptionOffset())));
}
jit.moveValueRegs(JSRInfo::returnValueJSR, wasmCallInfo.results[0].location.jsr());
} else
// For the JavaScript embedding, imports with these types in their type definition return are a WebAssembly.Module validation error.
RELEASE_ASSERT_NOT_REACHED();
}
}
} else if (signature.returnCount() > 1) {
constexpr GPRReg savedResultsGPR = preferredArgumentGPR<decltype(operationIterateResults), 3>();
jit.move(CCallHelpers::stackPointerRegister, savedResultsGPR);
if constexpr (!!maxFrameExtentForSlowPathCall)
jit.subPtr(CCallHelpers::TrustedImm32(maxFrameExtentForSlowPathCall), CCallHelpers::stackPointerRegister);
static_assert(noOverlap(savedResultsGPR, JSRInfo::returnValueJSR));
static_assert(GPRInfo::wasmContextInstancePointer != savedResultsGPR);
jit.prepareWasmCallOperation(GPRInfo::wasmContextInstancePointer);
jit.setupArguments<decltype(operationIterateResults)>(GPRInfo::wasmContextInstancePointer, CCallHelpers::TrustedImmPtr(&typeDefinition), JSRInfo::returnValueJSR, savedResultsGPR, CCallHelpers::framePointerRegister);
jit.callOperation<OperationPtrTag>(operationIterateResults);
if constexpr (!!maxFrameExtentForSlowPathCall)
jit.addPtr(CCallHelpers::TrustedImm32(maxFrameExtentForSlowPathCall), CCallHelpers::stackPointerRegister);
jit.loadPtr(CCallHelpers::Address(GPRInfo::wasmContextInstancePointer, JSWebAssemblyInstance::offsetOfVM()), GPRInfo::nonPreservedNonReturnGPR);
exceptionChecks.append(jit.branchTestPtr(CCallHelpers::NonZero, CCallHelpers::Address(GPRInfo::nonPreservedNonReturnGPR, VM::exceptionOffset())));
for (unsigned i = 0; i < signature.returnCount(); ++i) {
ValueLocation loc = wasmCallInfo.results[i].location;
if (loc.isGPR()) {
#if USE(JSVALUE32_64)
ASSERT(savedResultRegisters.find(loc.jsr().payloadGPR())->offset() + 4 == savedResultRegisters.find(loc.jsr().tagGPR())->offset());
#endif
jit.loadValue(CCallHelpers::Address(CCallHelpers::stackPointerRegister, savedResultRegisters.find(loc.jsr().payloadGPR())->offset()), loc.jsr());
} else if (loc.isFPR())
jit.loadDouble(CCallHelpers::Address(CCallHelpers::stackPointerRegister, savedResultRegisters.find(loc.fpr())->offset()), loc.fpr());
}
}
jit.emitFunctionEpilogue();
jit.ret();
if (!exceptionChecks.empty()) {
exceptionChecks.link(&jit);
jit.loadPtr(CCallHelpers::Address(GPRInfo::wasmContextInstancePointer, JSWebAssemblyInstance::offsetOfVM()), GPRInfo::argumentGPR0);
jit.copyCalleeSavesToVMEntryFrameCalleeSavesBuffer(GPRInfo::argumentGPR0);
jit.prepareWasmCallOperation(GPRInfo::wasmContextInstancePointer);
jit.setupArguments<decltype(operationWasmUnwind)>(GPRInfo::wasmContextInstancePointer);
jit.callOperation<OperationPtrTag>(operationWasmUnwind);
jit.farJump(GPRInfo::returnValueGPR, ExceptionHandlerPtrTag);
}
LinkBuffer patchBuffer(jit, GLOBAL_THUNK_ID, LinkBuffer::Profile::WasmThunk, JITCompilationCanFail);
if (UNLIKELY(patchBuffer.didFailToAllocate()))
return makeUnexpected(BindingFailure::OutOfMemory);
return FINALIZE_WASM_CODE(patchBuffer, WasmEntryPtrTag, nullptr, "WebAssembly->JavaScript import[%i] %s", importIndex, signature.toString().ascii().data());
}
void emitThrowWasmToJSException(CCallHelpers& jit, GPRReg wasmInstance, Wasm::ExceptionType type)
{
ASSERT(wasmInstance != GPRInfo::argumentGPR2);
ASSERT(wasmInstance != InvalidGPRReg);
jit.loadPtr(CCallHelpers::Address(wasmInstance, JSWebAssemblyInstance::offsetOfVM()), GPRInfo::argumentGPR2);
jit.copyCalleeSavesToVMEntryFrameCalleeSavesBuffer(GPRInfo::argumentGPR2);
if (wasmInstance != GPRInfo::argumentGPR0)
jit.move(wasmInstance, GPRInfo::argumentGPR0);
jit.move(CCallHelpers::TrustedImm32(static_cast<int32_t>(type)), GPRInfo::argumentGPR1);
jit.prepareWasmCallOperation(GPRInfo::argumentGPR0);
jit.callOperation<OperationPtrTag>(Wasm::operationWasmToJSException);
jit.farJump(GPRInfo::returnValueGPR, ExceptionHandlerPtrTag);
jit.breakpoint(); // We should not reach this.
}
} } // namespace JSC::Wasm
#endif // ENABLE(WEBASSEMBLY) && ENABLE(JIT)
|