| 1 |
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- |
| 2 |
* vim: set ts=8 sts=2 et sw=2 tw=80: |
| 3 |
* |
| 4 |
* Copyright 2017 Mozilla Foundation |
| 5 |
* |
| 6 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 |
* you may not use this file except in compliance with the License. |
| 8 |
* You may obtain a copy of the License at |
| 9 |
* |
| 10 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 11 |
* |
| 12 |
* Unless required by applicable law or agreed to in writing, software |
| 13 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 14 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 |
* See the License for the specific language governing permissions and |
| 16 |
* limitations under the License. |
| 17 |
*/ |
| 18 |
|
| 19 |
#include "wasm/WasmProcess.h" |
| 20 |
|
| 21 |
#include "mozilla/Attributes.h" |
| 22 |
|
| 23 |
#include "gc/Memory.h" |
| 24 |
#include "threading/ExclusiveData.h" |
| 25 |
#include "vm/MutexIDs.h" |
| 26 |
#include "vm/Runtime.h" |
| 27 |
#include "wasm/WasmBuiltinModule.h" |
| 28 |
#include "wasm/WasmBuiltins.h" |
| 29 |
#include "wasm/WasmCode.h" |
| 30 |
#include "wasm/WasmInstance.h" |
| 31 |
#include "wasm/WasmModuleTypes.h" |
| 32 |
#include "wasm/WasmStaticTypeDefs.h" |
| 33 |
|
| 34 |
using namespace js; |
| 35 |
using namespace wasm; |
| 36 |
|
| 37 |
mozilla::Atomic<bool> wasm::CodeExists(false); |
| 38 |
|
| 39 |
// Per-process map from values of program-counter (pc) to CodeBlocks. |
| 40 |
// |
| 41 |
// Whenever a new CodeBlock is ready to use, it has to be registered so that |
| 42 |
// we can have fast lookups from pc to CodeBlocks in numerous places. Since |
| 43 |
// wasm compilation may be tiered, and the second tier doesn't have access to |
| 44 |
// any JSContext/JS::Compartment/etc lying around, we have to use a process-wide |
| 45 |
// map instead. |
| 46 |
|
| 47 |
// This field is only atomic to handle buggy scenarios where we crash during |
| 48 |
// startup or shutdown and thus racily perform wasm::LookupCodeBlock() from |
| 49 |
// the crashing thread. |
| 50 |
|
| 51 |
static mozilla::Atomic<ThreadSafeCodeBlockMap*> sThreadSafeCodeBlockMap( |
| 52 |
nullptr); |
| 53 |
|
| 54 |
bool wasm::RegisterCodeBlock(const CodeBlock* cs) { |
| 55 |
if (cs->length() == 0) { |
| 56 |
return true; |
| 57 |
} |
| 58 |
|
| 59 |
// This function cannot race with startup/shutdown. |
| 60 |
ThreadSafeCodeBlockMap* map = sThreadSafeCodeBlockMap; |
| 61 |
MOZ_RELEASE_ASSERT(map); |
| 62 |
bool result = map->insert(cs); |
| 63 |
if (result) { |
| 64 |
CodeExists = true; |
| 65 |
} |
| 66 |
return result; |
| 67 |
} |
| 68 |
|
| 69 |
void wasm::UnregisterCodeBlock(const CodeBlock* cs) { |
| 70 |
if (cs->length() == 0) { |
| 71 |
return; |
| 72 |
} |
| 73 |
|
| 74 |
// This function cannot race with startup/shutdown. |
| 75 |
ThreadSafeCodeBlockMap* map = sThreadSafeCodeBlockMap; |
| 76 |
MOZ_RELEASE_ASSERT(map); |
| 77 |
size_t newCount = map->remove(cs); |
| 78 |
if (newCount == 0) { |
| 79 |
CodeExists = false; |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
const CodeBlock* wasm::LookupCodeBlock( |
| 84 |
const void* pc, const CodeRange** codeRange /*= nullptr */) { |
| 85 |
ThreadSafeCodeBlockMap* map = sThreadSafeCodeBlockMap; |
| 86 |
if (!map) { |
| 87 |
return nullptr; |
| 88 |
} |
| 89 |
|
| 90 |
return map->lookup(pc, codeRange); |
| 91 |
} |
| 92 |
|
| 93 |
const Code* wasm::LookupCode(const void* pc, |
| 94 |
const CodeRange** codeRange /* = nullptr */) { |
| 95 |
const CodeBlock* found = LookupCodeBlock(pc, codeRange); |
| 96 |
MOZ_ASSERT_IF(!found && codeRange, !*codeRange); |
| 97 |
return found ? found->code : nullptr; |
| 98 |
} |
| 99 |
|
| 100 |
bool wasm::InCompiledCode(void* pc) { |
| 101 |
if (LookupCodeBlock(pc)) { |
| 102 |
return true; |
| 103 |
} |
| 104 |
|
| 105 |
const CodeRange* codeRange; |
| 106 |
const uint8_t* codeBase; |
| 107 |
return LookupBuiltinThunk(pc, &codeRange, &codeBase); |
| 108 |
} |
| 109 |
|
| 110 |
#ifdef WASM_SUPPORTS_HUGE_MEMORY |
| 111 |
/* |
| 112 |
* Some 64 bit systems greatly limit the range of available virtual memory. We |
| 113 |
* require about 6GiB for each wasm huge memory, which can exhaust the address |
| 114 |
* spaces of these systems quickly. In order to avoid this, we only enable huge |
| 115 |
* memory if we observe a large enough address space. |
| 116 |
* |
| 117 |
* This number is conservatively chosen to continue using huge memory on our |
| 118 |
* smallest address space system, Android on ARM64 (39 bits), along with a bit |
| 119 |
* for error in detecting the address space limit. |
| 120 |
*/ |
| 121 |
static const size_t MinAddressBitsForHugeMemory = 38; |
| 122 |
|
| 123 |
/* |
| 124 |
* In addition to the above, some systems impose an independent limit on the |
| 125 |
* amount of virtual memory that may be used. |
| 126 |
*/ |
| 127 |
static const size_t MinVirtualMemoryLimitForHugeMemory = |
| 128 |
size_t(1) << MinAddressBitsForHugeMemory; |
| 129 |
#endif |
| 130 |
|
| 131 |
static bool sHugeMemoryEnabled32 = false; |
| 132 |
|
| 133 |
bool wasm::IsHugeMemoryEnabled(wasm::AddressType t) { |
| 134 |
if (t == AddressType::I64) { |
| 135 |
// No support for huge memory with 64-bit memories |
| 136 |
return false; |
| 137 |
} |
| 138 |
return sHugeMemoryEnabled32; |
| 139 |
} |
| 140 |
|
| 141 |
void ConfigureHugeMemory() { |
| 142 |
#ifdef WASM_SUPPORTS_HUGE_MEMORY |
| 143 |
MOZ_ASSERT(!sHugeMemoryEnabled32); |
| 144 |
|
| 145 |
if (JS::Prefs::wasm_disable_huge_memory()) { |
| 146 |
return; |
| 147 |
} |
| 148 |
|
| 149 |
if (gc::SystemAddressBits() < MinAddressBitsForHugeMemory) { |
| 150 |
return; |
| 151 |
} |
| 152 |
|
| 153 |
if (gc::VirtualMemoryLimit() != size_t(-1) && |
| 154 |
gc::VirtualMemoryLimit() < MinVirtualMemoryLimitForHugeMemory) { |
| 155 |
return; |
| 156 |
} |
| 157 |
|
| 158 |
sHugeMemoryEnabled32 = true; |
| 159 |
#endif |
| 160 |
} |
| 161 |
|
| 162 |
const TagType* wasm::sWrappedJSValueTagType = nullptr; |
| 163 |
|
| 164 |
static bool InitTagForJSValue() { |
| 165 |
MutableTagType type = js_new<TagType>(); |
| 166 |
if (!type || !type->initialize(StaticTypeDefs::jsTag)) { |
| 167 |
return false; |
| 168 |
} |
| 169 |
MOZ_ASSERT(WrappedJSValueTagType_ValueOffset == type->argOffsets()[0]); |
| 170 |
|
| 171 |
type.forget(&sWrappedJSValueTagType); |
| 172 |
|
| 173 |
return true; |
| 174 |
} |
| 175 |
|
| 176 |
bool wasm::Init() { |
| 177 |
MOZ_RELEASE_ASSERT(!sThreadSafeCodeBlockMap); |
| 178 |
|
| 179 |
// Assert invariants that should universally hold true, but cannot be checked |
| 180 |
// at compile time. |
| 181 |
uintptr_t pageSize = gc::SystemPageSize(); |
| 182 |
MOZ_RELEASE_ASSERT(wasm::NullPtrGuardSize <= pageSize); |
| 183 |
MOZ_RELEASE_ASSERT(intptr_t(nullptr) == AnyRef::NullRefValue); |
| 184 |
|
| 185 |
ConfigureHugeMemory(); |
| 186 |
|
| 187 |
AutoEnterOOMUnsafeRegion oomUnsafe; |
| 188 |
ThreadSafeCodeBlockMap* map = js_new<ThreadSafeCodeBlockMap>(); |
| 189 |
if (!map) { |
| 190 |
oomUnsafe.crash("js::wasm::Init"); |
| 191 |
} |
| 192 |
|
| 193 |
if (!StaticTypeDefs::init()) { |
| 194 |
oomUnsafe.crash("js::wasm::Init"); |
| 195 |
} |
| 196 |
|
| 197 |
// This uses StaticTypeDefs |
| 198 |
if (!BuiltinModuleFuncs::init()) { |
| 199 |
oomUnsafe.crash("js::wasm::Init"); |
| 200 |
} |
| 201 |
|
| 202 |
sThreadSafeCodeBlockMap = map; |
| 203 |
|
| 204 |
if (!InitTagForJSValue()) { |
| 205 |
oomUnsafe.crash("js::wasm::Init"); |
| 206 |
} |
| 207 |
|
| 208 |
return true; |
| 209 |
} |
| 210 |
|
| 211 |
void wasm::ShutDown() { |
| 212 |
// If there are live runtimes then we are already pretty much leaking the |
| 213 |
// world, so to avoid spurious assertions (which are valid and valuable when |
| 214 |
// there are not live JSRuntimes), don't bother releasing anything here. |
| 215 |
if (JSRuntime::hasLiveRuntimes()) { |
| 216 |
return; |
| 217 |
} |
| 218 |
|
| 219 |
BuiltinModuleFuncs::destroy(); |
| 220 |
StaticTypeDefs::destroy(); |
| 221 |
PurgeCanonicalTypes(); |
| 222 |
|
| 223 |
if (sWrappedJSValueTagType) { |
| 224 |
sWrappedJSValueTagType->Release(); |
| 225 |
sWrappedJSValueTagType = nullptr; |
| 226 |
} |
| 227 |
|
| 228 |
// After signalling shutdown by clearing sThreadSafeCodeBlockMap, wait for |
| 229 |
// concurrent wasm::LookupCodeBlock()s to finish. |
| 230 |
ThreadSafeCodeBlockMap* map = sThreadSafeCodeBlockMap; |
| 231 |
MOZ_RELEASE_ASSERT(map); |
| 232 |
sThreadSafeCodeBlockMap = nullptr; |
| 233 |
while (map->numActiveLookups() > 0) { |
| 234 |
} |
| 235 |
|
| 236 |
ReleaseBuiltinThunks(); |
| 237 |
js_delete(map); |
| 238 |
} |