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 "WasmBinding.h"
28
29#if ENABLE(WEBASSEMBLY)
30
31#include "CCallHelpers.h"
32#include "JSCInlines.h"
33#include "LinkBuffer.h"
34#include "WasmCallingConvention.h"
35#include "WasmInstance.h"
36
37namespace JSC { namespace Wasm {
38
39using JIT = CCallHelpers;
40
41Expected<MacroAssemblerCodeRef<WasmEntryPtrTag>, BindingFailure> wasmToWasm(unsigned importIndex)
42{
43 // FIXME: Consider uniquify the stubs based on signature + index to see if this saves memory.
44 // https://bugs.webkit.org/show_bug.cgi?id=184157
45
46 const PinnedRegisterInfo& pinnedRegs = PinnedRegisterInfo::get();
47 JIT jit;
48
49 GPRReg scratch = wasmCallingConvention().prologueScratchGPRs[0];
50 GPRReg baseMemory = pinnedRegs.baseMemoryPointer;
51 ASSERT(baseMemory != GPRReg::InvalidGPRReg);
52 ASSERT(baseMemory != scratch);
53 ASSERT(pinnedRegs.sizeRegister != baseMemory);
54 ASSERT(pinnedRegs.sizeRegister != scratch);
55 GPRReg sizeRegAsScratch = pinnedRegs.sizeRegister;
56 ASSERT(sizeRegAsScratch != GPRReg::InvalidGPRReg);
57
58 // B3's call codegen ensures that the JSCell is a WebAssemblyFunction.
59 jit.loadWasmContextInstance(sizeRegAsScratch); // Old Instance*
60 // Get the callee's Wasm::Instance and set it as WasmContext's instance. The caller will take care of restoring its own Instance.
61 jit.loadPtr(JIT::Address(sizeRegAsScratch, Instance::offsetOfTargetInstance(importIndex)), baseMemory); // Instance*.
62 // While we're accessing that cacheline, also get the wasm entrypoint so we can tail call to it below.
63 jit.loadPtr(JIT::Address(sizeRegAsScratch, Instance::offsetOfWasmEntrypointLoadLocation(importIndex)), scratch);
64 jit.storeWasmContextInstance(baseMemory);
65
66 jit.loadPtr(JIT::Address(sizeRegAsScratch, Instance::offsetOfCachedStackLimit()), sizeRegAsScratch);
67 jit.storePtr(sizeRegAsScratch, JIT::Address(baseMemory, Instance::offsetOfCachedStackLimit()));
68
69 // FIXME the following code assumes that all Wasm::Instance have the same pinned registers. https://bugs.webkit.org/show_bug.cgi?id=162952
70 // Set up the callee's baseMemory register as well as the memory size registers.
71 {
72 GPRReg scratchOrSize = !Gigacage::isEnabled(Gigacage::Primitive) ? pinnedRegs.sizeRegister : wasmCallingConvention().prologueScratchGPRs[1];
73
74 jit.loadPtr(JIT::Address(baseMemory, Wasm::Instance::offsetOfCachedMemorySize()), pinnedRegs.sizeRegister); // Memory size.
75 jit.loadPtr(JIT::Address(baseMemory, Wasm::Instance::offsetOfCachedMemory()), baseMemory); // Wasm::Memory::TaggedArrayStoragePtr<void> (void*).
76 jit.cageConditionally(Gigacage::Primitive, baseMemory, pinnedRegs.sizeRegister, scratchOrSize);
77 }
78
79 // Tail call into the callee WebAssembly function.
80 jit.loadPtr(scratch, scratch);
81 jit.farJump(scratch, WasmEntryPtrTag);
82
83 LinkBuffer patchBuffer(jit, GLOBAL_THUNK_ID, JITCompilationCanFail);
84 if (UNLIKELY(patchBuffer.didFailToAllocate()))
85 return makeUnexpected(BindingFailure::OutOfMemory);
86
87 return FINALIZE_WASM_CODE(patchBuffer, WasmEntryPtrTag, "WebAssembly->WebAssembly import[%i]", importIndex);
88}
89
90} } // namespace JSC::Wasm
91
92#endif // ENABLE(WEBASSEMBLY)
93