COMMIT_MESSAGE

 1crashIfWebAssemblyCantFastMemory
 2

Source/JavaScriptCore/ChangeLog

 12017-03-28 JF Bastien <jfbastien@apple.com>
 2
 3 WebAssembly: option to crash if no fast memory is available
 4 https://bugs.webkit.org/show_bug.cgi?id=170219
 5
 6 Reviewed by NOBODY (OOPS!).
 7
 8 * runtime/Options.h:
 9 * wasm/WasmMemory.cpp:
 10 (JSC::Wasm::webAssemblyCouldntGetFastMemory):
 11 (JSC::Wasm::tryGetFastMemory):
 12
1132017-03-28 Keith Miller <keith_miller@apple.com>
214
315 WebAssembly: Make WebAssembly.instantiate/compile truly asynchronous

Source/JavaScriptCore/runtime/Options.h

@@typedef const char* optionString;
434434 \
435435 v(bool, simulateWebAssemblyLowMemory, false, Normal, "If true, the Memory object won't mmap the full 'maximum' range and instead will allocate the minimum required amount.") \
436436 v(bool, useWebAssemblyFastMemory, true, Normal, "If true, we will try to use a 32-bit address space with a signal handler to bounds check wasm memory.") \
 437 v(bool, crashIfWebAssemblyCantFastMemory, false, Normal, "If true, we will crash if we can't obtain fast memory for wasm.") \
437438 v(bool, useWebAssemblyFastTLS, true, Normal, "If true, we will try to use fast thread-local storage if available on the current platform.")
438439
439440

Source/JavaScriptCore/wasm/WasmMemory.cpp

@@namespace {
4242const bool verbose = false;
4343}
4444
 45static NEVER_INLINE NO_RETURN_DUE_TO_CRASH void webAssemblyCouldntGetFastMemory()
 46{
 47 CRASH();
 48}
 49
4550inline bool mmapBytes(size_t bytes, void*& memory)
4651{
4752 dataLogIf(verbose, "Attempting to mmap ", bytes, " bytes: ");

@@const HashSet<void*>& viewActiveFastMemories(const AbstractLocker& locker)
98103
99104inline bool tryGetFastMemory(VM& vm, void*& memory, size_t& mappedCapacity, MemoryMode& mode)
100105{
101  // We might GC here so we should be holding the API lock.
102  // FIXME: We should be able to syncronously trigger the GC from another thread.
103  ASSERT(vm.currentThreadIsHoldingAPILock());
104  if (!fastMemoryEnabled())
105  return false;
106 
107  // We need to be sure we have a stub prior to running code.
108  if (!vm.getCTIStub(throwExceptionFromWasmThunkGenerator).size())
109  return false;
110 
111106 auto dequeFastMemory = [&] () -> bool {
112107 // FIXME: We should eventually return these to the OS if we go some number of GCs
113108 // without using them.

@@inline bool tryGetFastMemory(VM& vm, void*& memory, size_t& mappedCapacity, Memo
123118 return false;
124119 };
125120
 121 // We might GC here so we should be holding the API lock.
 122 // FIXME: We should be able to syncronously trigger the GC from another thread.
 123 ASSERT(vm.currentThreadIsHoldingAPILock());
 124 if (!fastMemoryEnabled())
 125 goto fail;
 126
 127 // We need to be sure we have a stub prior to running code.
 128 if (!vm.getCTIStub(throwExceptionFromWasmThunkGenerator).size())
 129 goto fail;
 130
126131 ASSERT(allocatedFastMemories <= maxFastMemories);
127132 if (dequeFastMemory())
128133 return true;

@@inline bool tryGetFastMemory(VM& vm, void*& memory, size_t& mappedCapacity, Memo
131136 if (allocatedFastMemories == maxFastMemories) {
132137 // There is a reasonable chance that another module has died but has not been collected yet. Don't lose hope yet!
133138 vm.heap.collectAllGarbage();
134  return dequeFastMemory();
 139 if (dequeFastMemory())
 140 return true;
 141 goto fail;
135142 }
136143
137144 if (mmapBytes(fastMemoryMappedBytes, memory)) {

@@inline bool tryGetFastMemory(VM& vm, void*& memory, size_t& mappedCapacity, Memo
142149 auto result = activeFastMemories(locker).add(memory);
143150 ASSERT_UNUSED(result, result.isNewEntry);
144151 }
145  return memory;
 152
 153 if (memory)
 154 return true;
 155 goto fail;
 156
 157fail:
 158 if (Options::crashIfWebAssemblyCantFastMemory())
 159 webAssemblyCouldntGetFastMemory();
 160
 161 return false;
146162}
147163
148164inline void releaseFastMemory(void*& memory, size_t writableSize, size_t mappedCapacity, MemoryMode mode)