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