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 | |
37 | namespace JSC { namespace Wasm { |
38 | |
39 | using JIT = CCallHelpers; |
40 | |
41 | Expected<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 = wasmCallingConventionAir().prologueScratch(0); |
50 | GPRReg baseMemory = pinnedRegs.baseMemoryPointer; |
51 | ASSERT(baseMemory != scratch); |
52 | ASSERT(pinnedRegs.sizeRegister != baseMemory); |
53 | ASSERT(pinnedRegs.sizeRegister != scratch); |
54 | GPRReg sizeRegAsScratch = pinnedRegs.sizeRegister; |
55 | |
56 | // B3's call codegen ensures that the JSCell is a WebAssemblyFunction. |
57 | jit.loadWasmContextInstance(sizeRegAsScratch); // Old Instance* |
58 | // Get the callee's Wasm::Instance and set it as WasmContext's instance. The caller will take care of restoring its own Instance. |
59 | jit.loadPtr(JIT::Address(sizeRegAsScratch, Instance::offsetOfTargetInstance(importIndex)), baseMemory); // Instance*. |
60 | // While we're accessing that cacheline, also get the wasm entrypoint so we can tail call to it below. |
61 | jit.loadPtr(JIT::Address(sizeRegAsScratch, Instance::offsetOfWasmEntrypointLoadLocation(importIndex)), scratch); |
62 | jit.storeWasmContextInstance(baseMemory); |
63 | |
64 | jit.loadPtr(JIT::Address(sizeRegAsScratch, Instance::offsetOfCachedStackLimit()), sizeRegAsScratch); |
65 | jit.storePtr(sizeRegAsScratch, JIT::Address(baseMemory, Instance::offsetOfCachedStackLimit())); |
66 | |
67 | // FIXME the following code assumes that all Wasm::Instance have the same pinned registers. https://bugs.webkit.org/show_bug.cgi?id=162952 |
68 | // Set up the callee's baseMemory register as well as the memory size registers. |
69 | { |
70 | GPRReg scratchOrSize = !Gigacage::isEnabled(Gigacage::Primitive) ? pinnedRegs.sizeRegister : wasmCallingConventionAir().prologueScratch(1); |
71 | |
72 | jit.loadPtr(JIT::Address(baseMemory, Wasm::Instance::offsetOfCachedMemorySize()), pinnedRegs.sizeRegister); // Memory size. |
73 | jit.loadPtr(JIT::Address(baseMemory, Wasm::Instance::offsetOfCachedMemory()), baseMemory); // Wasm::Memory::TaggedArrayStoragePtr<void> (void*). |
74 | jit.cageConditionally(Gigacage::Primitive, baseMemory, pinnedRegs.sizeRegister, scratchOrSize); |
75 | } |
76 | |
77 | // Tail call into the callee WebAssembly function. |
78 | jit.loadPtr(scratch, scratch); |
79 | jit.jump(scratch, WasmEntryPtrTag); |
80 | |
81 | LinkBuffer patchBuffer(jit, GLOBAL_THUNK_ID, JITCompilationCanFail); |
82 | if (UNLIKELY(patchBuffer.didFailToAllocate())) |
83 | return makeUnexpected(BindingFailure::OutOfMemory); |
84 | |
85 | return FINALIZE_CODE(patchBuffer, WasmEntryPtrTag, "WebAssembly->WebAssembly import[%i]" , importIndex); |
86 | } |
87 | |
88 | } } // namespace JSC::Wasm |
89 | |
90 | #endif // ENABLE(WEBASSEMBLY) |
91 | |