12017-03-02 Keith Miller <keith_miller@apple.com>
2
3 WASM should support faster loads.
4 https://bugs.webkit.org/show_bug.cgi?id=162693
5
6 Reviewed by Saam Barati.
7
8 This patch adds support for WebAssembly using a 32-bit address
9 space for memory (along with some extra space for offset
10 overflow). With a 32-bit address space (we call them
11 Signaling/fast memories), we reserve the virtual address space for
12 2^32 + offset bytes of memory and only mark the usable section as
13 read/write. If wasm code would read/write out of bounds we use a
14 custom signal handler to catch the SIGBUS. The signal handler then
15 checks if the faulting instruction is wasm code and tells the
16 thread to resume executing from the wasm exception
17 handler. Otherwise, the signal handler crashes the process, as
18 usual.
19
20 All of the allocations of these memories are managed by the
21 Wasm::Memory class. In order to avoid TLB churn in the OS we cache
22 old Signaling memories that are no longer in use. Since getting
23 the wrong memory can cause recompiles, we try to reserve a memory
24 for modules that do not import a memory. If a module does import a
25 memory, we try to guess the type of memory we are going to get
26 based on the last one allocated.
27
28 This patch also changes how the wasm JS-api manages objects. Since
29 we can compile different versions of code, this patch adds a new
30 JSWebAssemblyCodeBlock class that holds all the information
31 specific to running a module in a particular bounds checking
32 mode. Additionally, the Wasm::Memory object is now a reference
33 counted class that is shared between the JSWebAssemblyMemory
34 object and the ArrayBuffer that also views it.
35
36 * JavaScriptCore.xcodeproj/project.pbxproj:
37 * jit/JITThunks.cpp:
38 (JSC::JITThunks::existingCTIStub):
39 * jit/JITThunks.h:
40 * jsc.cpp:
41 (jscmain):
42 * runtime/Options.h:
43 * runtime/VM.cpp:
44 (JSC::VM::VM):
45 * runtime/VM.h:
46 * wasm/JSWebAssemblyCodeBlock.h: Copied from Source/JavaScriptCore/wasm/js/JSWebAssemblyModule.h.
47 (JSC::JSWebAssemblyCodeBlock::create):
48 (JSC::JSWebAssemblyCodeBlock::createStructure):
49 (JSC::JSWebAssemblyCodeBlock::functionImportCount):
50 (JSC::JSWebAssemblyCodeBlock::mode):
51 (JSC::JSWebAssemblyCodeBlock::module):
52 (JSC::JSWebAssemblyCodeBlock::jsEntrypointCalleeFromFunctionIndexSpace):
53 (JSC::JSWebAssemblyCodeBlock::wasmEntrypointCalleeFromFunctionIndexSpace):
54 (JSC::JSWebAssemblyCodeBlock::setJSEntrypointCallee):
55 (JSC::JSWebAssemblyCodeBlock::setWasmEntrypointCallee):
56 (JSC::JSWebAssemblyCodeBlock::callees):
57 (JSC::JSWebAssemblyCodeBlock::offsetOfCallees):
58 (JSC::JSWebAssemblyCodeBlock::allocationSize):
59 * wasm/WasmB3IRGenerator.cpp:
60 (JSC::Wasm::B3IRGenerator::B3IRGenerator):
61 (JSC::Wasm::getMemoryBaseAndSize):
62 (JSC::Wasm::B3IRGenerator::emitCheckAndPreparePointer):
63 (JSC::Wasm::B3IRGenerator::emitLoadOp):
64 (JSC::Wasm::B3IRGenerator::emitStoreOp):
65 * wasm/WasmCallingConvention.h:
66 * wasm/WasmFaultSignalHandler.cpp: Added.
67 (JSC::Wasm::trapHandler):
68 (JSC::Wasm::registerCode):
69 (JSC::Wasm::unregisterCode):
70 (JSC::Wasm::fastMemoryEnabled):
71 (JSC::Wasm::enableFastMemory):
72 * wasm/WasmFaultSignalHandler.h: Copied from Source/JavaScriptCore/wasm/js/JSWebAssemblyCallee.cpp.
73 * wasm/WasmFormat.h:
74 (JSC::Wasm::ModuleInformation::importFunctionCount):
75 (JSC::Wasm::ModuleInformation::hasMemory): Deleted.
76 * wasm/WasmMemory.cpp:
77 (JSC::Wasm::mmapBytes):
78 (JSC::Wasm::Memory::lastAllocatedMode):
79 (JSC::Wasm::availableFastMemories):
80 (JSC::Wasm::tryGetFastMemory):
81 (JSC::Wasm::releaseFastMemory):
82 (JSC::Wasm::Memory::Memory):
83 (JSC::Wasm::Memory::createImpl):
84 (JSC::Wasm::Memory::create):
85 (JSC::Wasm::Memory::~Memory):
86 (JSC::Wasm::Memory::grow):
87 (JSC::Wasm::Memory::dump):
88 (JSC::Wasm::Memory::makeString):
89 * wasm/WasmMemory.h:
90 (JSC::Wasm::Memory::operator bool):
91 (JSC::Wasm::Memory::size):
92 (JSC::Wasm::Memory::check):
93 (JSC::Wasm::Memory::Memory): Deleted.
94 (JSC::Wasm::Memory::offsetOfMemory): Deleted.
95 (JSC::Wasm::Memory::offsetOfSize): Deleted.
96 * wasm/WasmMemoryInformation.cpp:
97 (JSC::Wasm::MemoryInformation::MemoryInformation):
98 * wasm/WasmMemoryInformation.h:
99 (JSC::Wasm::MemoryInformation::hasReservedMemory):
100 (JSC::Wasm::MemoryInformation::takeReservedMemory):
101 (JSC::Wasm::MemoryInformation::mode):
102 * wasm/WasmModuleParser.cpp:
103 * wasm/WasmModuleParser.h:
104 (JSC::Wasm::ModuleParser::ModuleParser):
105 * wasm/WasmPlan.cpp:
106 (JSC::Wasm::Plan::parseAndValidateModule):
107 (JSC::Wasm::Plan::run):
108 * wasm/WasmPlan.h:
109 (JSC::Wasm::Plan::mode):
110 * wasm/js/JSWebAssemblyCallee.cpp:
111 (JSC::JSWebAssemblyCallee::finishCreation):
112 (JSC::JSWebAssemblyCallee::destroy):
113 * wasm/js/JSWebAssemblyCodeBlock.cpp: Added.
114 (JSC::JSWebAssemblyCodeBlock::JSWebAssemblyCodeBlock):
115 (JSC::JSWebAssemblyCodeBlock::destroy):
116 (JSC::JSWebAssemblyCodeBlock::isSafeToRun):
117 (JSC::JSWebAssemblyCodeBlock::visitChildren):
118 (JSC::JSWebAssemblyCodeBlock::UnconditionalFinalizer::finalizeUnconditionally):
119 * wasm/js/JSWebAssemblyInstance.cpp:
120 (JSC::JSWebAssemblyInstance::setMemory):
121 (JSC::JSWebAssemblyInstance::finishCreation):
122 (JSC::JSWebAssemblyInstance::visitChildren):
123 * wasm/js/JSWebAssemblyInstance.h:
124 (JSC::JSWebAssemblyInstance::module):
125 (JSC::JSWebAssemblyInstance::codeBlock):
126 (JSC::JSWebAssemblyInstance::memoryMode):
127 (JSC::JSWebAssemblyInstance::setMemory): Deleted.
128 * wasm/js/JSWebAssemblyMemory.cpp:
129 (JSC::JSWebAssemblyMemory::create):
130 (JSC::JSWebAssemblyMemory::JSWebAssemblyMemory):
131 (JSC::JSWebAssemblyMemory::buffer):
132 (JSC::JSWebAssemblyMemory::grow):
133 (JSC::JSWebAssemblyMemory::destroy):
134 * wasm/js/JSWebAssemblyMemory.h:
135 (JSC::JSWebAssemblyMemory::memory):
136 (JSC::JSWebAssemblyMemory::offsetOfMemory):
137 (JSC::JSWebAssemblyMemory::offsetOfSize):
138 * wasm/js/JSWebAssemblyModule.cpp:
139 (JSC::JSWebAssemblyModule::buildCodeBlock):
140 (JSC::JSWebAssemblyModule::create):
141 (JSC::JSWebAssemblyModule::JSWebAssemblyModule):
142 (JSC::JSWebAssemblyModule::codeBlock):
143 (JSC::JSWebAssemblyModule::finishCreation):
144 (JSC::JSWebAssemblyModule::visitChildren):
145 (JSC::JSWebAssemblyModule::UnconditionalFinalizer::finalizeUnconditionally): Deleted.
146 * wasm/js/JSWebAssemblyModule.h:
147 (JSC::JSWebAssemblyModule::takeReservedMemory):
148 (JSC::JSWebAssemblyModule::signatureIndexFromFunctionIndexSpace):
149 (JSC::JSWebAssemblyModule::codeBlock):
150 (JSC::JSWebAssemblyModule::functionImportCount): Deleted.
151 (JSC::JSWebAssemblyModule::jsEntrypointCalleeFromFunctionIndexSpace): Deleted.
152 (JSC::JSWebAssemblyModule::wasmEntrypointCalleeFromFunctionIndexSpace): Deleted.
153 (JSC::JSWebAssemblyModule::setJSEntrypointCallee): Deleted.
154 (JSC::JSWebAssemblyModule::setWasmEntrypointCallee): Deleted.
155 (JSC::JSWebAssemblyModule::callees): Deleted.
156 (JSC::JSWebAssemblyModule::offsetOfCallees): Deleted.
157 (JSC::JSWebAssemblyModule::allocationSize): Deleted.
158 * wasm/js/WebAssemblyFunction.cpp:
159 (JSC::callWebAssemblyFunction):
160 * wasm/js/WebAssemblyInstanceConstructor.cpp:
161 (JSC::constructJSWebAssemblyInstance):
162 * wasm/js/WebAssemblyMemoryConstructor.cpp:
163 (JSC::constructJSWebAssemblyMemory):
164 * wasm/js/WebAssemblyModuleConstructor.cpp:
165 (JSC::WebAssemblyModuleConstructor::createModule):
166 * wasm/js/WebAssemblyModuleRecord.cpp:
167 (JSC::WebAssemblyModuleRecord::link):
168 (JSC::WebAssemblyModuleRecord::evaluate):
169