| 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 2016 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/WasmCode.h" |
| 20 |
|
| 21 |
#include "mozilla/Atomics.h" |
| 22 |
#include "mozilla/BinarySearch.h" |
| 23 |
#include "mozilla/EnumeratedRange.h" |
| 24 |
#include "mozilla/Sprintf.h" |
| 25 |
|
| 26 |
#include <algorithm> |
| 27 |
|
| 28 |
#include "jsnum.h" |
| 29 |
|
| 30 |
#include "jit/Disassemble.h" |
| 31 |
#include "jit/ExecutableAllocator.h" |
| 32 |
#include "jit/FlushICache.h" // for FlushExecutionContextForAllThreads |
| 33 |
#include "jit/MacroAssembler.h" |
| 34 |
#include "jit/PerfSpewer.h" |
| 35 |
#include "util/Poison.h" |
| 36 |
#include "vm/HelperThreadState.h" // PartialTier2CompileTask |
| 37 |
#ifdef MOZ_VTUNE |
| 38 |
# include "vtune/VTuneWrapper.h" |
| 39 |
#endif |
| 40 |
#include "wasm/WasmModule.h" |
| 41 |
#include "wasm/WasmProcess.h" |
| 42 |
#include "wasm/WasmSerialize.h" |
| 43 |
#include "wasm/WasmStubs.h" |
| 44 |
#include "wasm/WasmUtility.h" |
| 45 |
|
| 46 |
using namespace js; |
| 47 |
using namespace js::jit; |
| 48 |
using namespace js::wasm; |
| 49 |
using mozilla::Atomic; |
| 50 |
using mozilla::BinarySearch; |
| 51 |
using mozilla::BinarySearchIf; |
| 52 |
using mozilla::DebugOnly; |
| 53 |
using mozilla::MakeEnumeratedRange; |
| 54 |
using mozilla::MallocSizeOf; |
| 55 |
using mozilla::Maybe; |
| 56 |
|
| 57 |
size_t LinkData::SymbolicLinkArray::sizeOfExcludingThis( |
| 58 |
MallocSizeOf mallocSizeOf) const { |
| 59 |
size_t size = 0; |
| 60 |
for (const Uint32Vector& offsets : *this) { |
| 61 |
size += offsets.sizeOfExcludingThis(mallocSizeOf); |
| 62 |
} |
| 63 |
return size; |
| 64 |
} |
| 65 |
|
| 66 |
static uint32_t RoundupExecutableCodePageSize(uint32_t codeLength) { |
| 67 |
static_assert(MaxCodeBytesPerProcess <= INT32_MAX, "rounding won't overflow"); |
| 68 |
// AllocateExecutableMemory() requires a multiple of ExecutableCodePageSize. |
| 69 |
return RoundUp(codeLength, ExecutableCodePageSize); |
| 70 |
} |
| 71 |
|
| 72 |
UniqueCodeBytes wasm::AllocateCodeBytes( |
| 73 |
Maybe<AutoMarkJitCodeWritableForThread>& writable, uint32_t codeLength, |
| 74 |
bool allowLastDitchGC) { |
| 75 |
if (codeLength > MaxCodeBytesPerProcess) { |
| 76 |
return nullptr; |
| 77 |
} |
| 78 |
|
| 79 |
MOZ_RELEASE_ASSERT(codeLength == RoundupExecutableCodePageSize(codeLength)); |
| 80 |
void* p = AllocateExecutableMemory(codeLength, ProtectionSetting::Writable, |
| 81 |
MemCheckKind::MakeUndefined); |
| 82 |
|
| 83 |
// If the allocation failed and the embedding gives us a last-ditch attempt |
| 84 |
// to purge all memory (which, in gecko, does a purging GC/CC/GC), do that |
| 85 |
// then retry the allocation. |
| 86 |
if (!p && allowLastDitchGC) { |
| 87 |
if (OnLargeAllocationFailure) { |
| 88 |
OnLargeAllocationFailure(); |
| 89 |
p = AllocateExecutableMemory(codeLength, ProtectionSetting::Writable, |
| 90 |
MemCheckKind::MakeUndefined); |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
if (!p) { |
| 95 |
return nullptr; |
| 96 |
} |
| 97 |
|
| 98 |
// Construct AutoMarkJitCodeWritableForThread after allocating memory, to |
| 99 |
// ensure it's not nested (OnLargeAllocationFailure can trigger GC). |
| 100 |
writable.emplace(); |
| 101 |
|
| 102 |
// We account for the bytes allocated in WasmModuleObject::create, where we |
| 103 |
// have the necessary JSContext. |
| 104 |
return UniqueCodeBytes((uint8_t*)p, FreeCode(codeLength)); |
| 105 |
} |
| 106 |
|
| 107 |
void FreeCode::operator()(uint8_t* bytes) { |
| 108 |
MOZ_ASSERT(codeLength); |
| 109 |
MOZ_ASSERT(codeLength == RoundupExecutableCodePageSize(codeLength)); |
| 110 |
|
| 111 |
#ifdef MOZ_VTUNE |
| 112 |
vtune::UnmarkBytes(bytes, codeLength); |
| 113 |
#endif |
| 114 |
DeallocateExecutableMemory(bytes, codeLength); |
| 115 |
} |
| 116 |
|
| 117 |
bool wasm::StaticallyLink(jit::AutoMarkJitCodeWritableForThread& writable, |
| 118 |
uint8_t* base, const LinkData& linkData, |
| 119 |
const Code* maybeCode) { |
| 120 |
if (!EnsureBuiltinThunksInitialized(writable)) { |
| 121 |
return false; |
| 122 |
} |
| 123 |
|
| 124 |
for (LinkData::InternalLink link : linkData.internalLinks) { |
| 125 |
CodeLabel label; |
| 126 |
label.patchAt()->bind(link.patchAtOffset); |
| 127 |
label.target()->bind(link.targetOffset); |
| 128 |
#ifdef JS_CODELABEL_LINKMODE |
| 129 |
label.setLinkMode(static_cast<CodeLabel::LinkMode>(link.mode)); |
| 130 |
#endif |
| 131 |
Assembler::Bind(base, label); |
| 132 |
} |
| 133 |
|
| 134 |
for (CallFarJump far : linkData.callFarJumps) { |
| 135 |
MOZ_ASSERT(maybeCode && maybeCode->mode() == CompileMode::LazyTiering); |
| 136 |
const CodeBlock& bestBlock = maybeCode->funcCodeBlock(far.targetFuncIndex); |
| 137 |
uint32_t stubRangeIndex = bestBlock.funcToCodeRange[far.targetFuncIndex]; |
| 138 |
const CodeRange& stubRange = bestBlock.codeRanges[stubRangeIndex]; |
| 139 |
uint8_t* stubBase = bestBlock.base(); |
| 140 |
MacroAssembler::patchFarJump(base + far.jumpOffset, |
| 141 |
stubBase + stubRange.funcUncheckedCallEntry()); |
| 142 |
} |
| 143 |
|
| 144 |
for (auto imm : MakeEnumeratedRange(SymbolicAddress::Limit)) { |
| 145 |
const Uint32Vector& offsets = linkData.symbolicLinks[imm]; |
| 146 |
if (offsets.empty()) { |
| 147 |
continue; |
| 148 |
} |
| 149 |
|
| 150 |
void* target = SymbolicAddressTarget(imm); |
| 151 |
for (uint32_t offset : offsets) { |
| 152 |
uint8_t* patchAt = base + offset; |
| 153 |
Assembler::PatchDataWithValueCheck(CodeLocationLabel(patchAt), |
| 154 |
PatchedImmPtr(target), |
| 155 |
PatchedImmPtr((void*)-1)); |
| 156 |
} |
| 157 |
} |
| 158 |
|
| 159 |
return true; |
| 160 |
} |
| 161 |
|
| 162 |
void wasm::StaticallyUnlink(uint8_t* base, const LinkData& linkData) { |
| 163 |
for (LinkData::InternalLink link : linkData.internalLinks) { |
| 164 |
CodeLabel label; |
| 165 |
label.patchAt()->bind(link.patchAtOffset); |
| 166 |
label.target()->bind(-size_t(base)); // to reset immediate to null |
| 167 |
#ifdef JS_CODELABEL_LINKMODE |
| 168 |
label.setLinkMode(static_cast<CodeLabel::LinkMode>(link.mode)); |
| 169 |
#endif |
| 170 |
Assembler::Bind(base, label); |
| 171 |
} |
| 172 |
|
| 173 |
for (auto imm : MakeEnumeratedRange(SymbolicAddress::Limit)) { |
| 174 |
const Uint32Vector& offsets = linkData.symbolicLinks[imm]; |
| 175 |
if (offsets.empty()) { |
| 176 |
continue; |
| 177 |
} |
| 178 |
|
| 179 |
void* target = SymbolicAddressTarget(imm); |
| 180 |
for (uint32_t offset : offsets) { |
| 181 |
uint8_t* patchAt = base + offset; |
| 182 |
Assembler::PatchDataWithValueCheck(CodeLocationLabel(patchAt), |
| 183 |
PatchedImmPtr((void*)-1), |
| 184 |
PatchedImmPtr(target)); |
| 185 |
} |
| 186 |
} |
| 187 |
} |
| 188 |
|
| 189 |
CodeSource::CodeSource(jit::MacroAssembler& masm, const LinkData* linkData, |
| 190 |
const Code* code) |
| 191 |
: masm_(&masm), |
| 192 |
bytes_(nullptr), |
| 193 |
length_(masm.bytesNeeded()), |
| 194 |
linkData_(linkData), |
| 195 |
code_(code) {} |
| 196 |
|
| 197 |
CodeSource::CodeSource(const uint8_t* bytes, uint32_t length, |
| 198 |
const LinkData& linkData, const Code* code) |
| 199 |
: masm_(nullptr), |
| 200 |
bytes_(bytes), |
| 201 |
length_(length), |
| 202 |
linkData_(&linkData), |
| 203 |
code_(code) {} |
| 204 |
|
| 205 |
bool CodeSource::copyAndLink(jit::AutoMarkJitCodeWritableForThread& writable, |
| 206 |
uint8_t* codeStart) const { |
| 207 |
// Copy the machine code over |
| 208 |
if (masm_) { |
| 209 |
masm_->executableCopy(codeStart); |
| 210 |
} else { |
| 211 |
memcpy(codeStart, bytes_, length_); |
| 212 |
} |
| 213 |
|
| 214 |
// Use link data if we have it, or else fall back to basic linking using the |
| 215 |
// MacroAssembler. |
| 216 |
if (linkData_) { |
| 217 |
return StaticallyLink(writable, codeStart, *linkData_, code_); |
| 218 |
} |
| 219 |
|
| 220 |
// We must always have link data if we're coming from raw bytes. |
| 221 |
MOZ_ASSERT(masm_); |
| 222 |
// If we didn't provide link data, then we shouldn't have provided the code |
| 223 |
// object. |
| 224 |
MOZ_ASSERT(!code_); |
| 225 |
PatchDebugSymbolicAccesses(codeStart, *masm_); |
| 226 |
for (const CodeLabel& label : masm_->codeLabels()) { |
| 227 |
Assembler::Bind(codeStart, label); |
| 228 |
} |
| 229 |
return true; |
| 230 |
} |
| 231 |
|
| 232 |
size_t CodeSegment::AllocationAlignment() { |
| 233 |
// If we are write-protecting code, all new code allocations must be rounded |
| 234 |
// to the system page size. |
| 235 |
if (JitOptions.writeProtectCode) { |
| 236 |
return gc::SystemPageSize(); |
| 237 |
} |
| 238 |
|
| 239 |
// Otherwise we can just use the standard JIT code alignment. |
| 240 |
return jit::CodeAlignment; |
| 241 |
} |
| 242 |
|
| 243 |
size_t CodeSegment::AlignAllocationBytes(uintptr_t bytes) { |
| 244 |
return AlignBytes(bytes, AllocationAlignment()); |
| 245 |
} |
| 246 |
|
| 247 |
bool CodeSegment::IsAligned(uintptr_t bytes) { |
| 248 |
return bytes == AlignAllocationBytes(bytes); |
| 249 |
} |
| 250 |
|
| 251 |
bool CodeSegment::hasSpace(size_t bytes) const { |
| 252 |
MOZ_ASSERT(CodeSegment::IsAligned(bytes)); |
| 253 |
return bytes <= capacityBytes() && lengthBytes_ <= capacityBytes() - bytes; |
| 254 |
} |
| 255 |
|
| 256 |
void CodeSegment::claimSpace(size_t bytes, uint8_t** claimedBase) { |
| 257 |
MOZ_RELEASE_ASSERT(hasSpace(bytes)); |
| 258 |
*claimedBase = base() + lengthBytes_; |
| 259 |
lengthBytes_ += bytes; |
| 260 |
} |
| 261 |
|
| 262 |
/* static */ |
| 263 |
SharedCodeSegment CodeSegment::create( |
| 264 |
mozilla::Maybe<jit::AutoMarkJitCodeWritableForThread>& writable, |
| 265 |
size_t capacityBytes, bool allowLastDitchGC) { |
| 266 |
MOZ_RELEASE_ASSERT(capacityBytes == |
| 267 |
RoundupExecutableCodePageSize(capacityBytes)); |
| 268 |
|
| 269 |
UniqueCodeBytes codeBytes; |
| 270 |
if (capacityBytes != 0) { |
| 271 |
codeBytes = AllocateCodeBytes(writable, capacityBytes, allowLastDitchGC); |
| 272 |
if (!codeBytes) { |
| 273 |
return nullptr; |
| 274 |
} |
| 275 |
} |
| 276 |
|
| 277 |
return js_new<CodeSegment>(std::move(codeBytes), /*lengthBytes=*/0, |
| 278 |
capacityBytes); |
| 279 |
} |
| 280 |
|
| 281 |
// When allocating a single stub to a page, we should not always place the stub |
| 282 |
// at the beginning of the page as the stubs will tend to thrash the icache by |
| 283 |
// creating conflicts (everything ends up in the same cache set). Instead, |
| 284 |
// locate stubs at different line offsets up to 3/4 the system page size (the |
| 285 |
// code allocation quantum). |
| 286 |
// |
| 287 |
// This may be called on background threads, hence the atomic. |
| 288 |
static uint32_t RandomPaddingForCodeLength(uint32_t codeLength) { |
| 289 |
// The counter serves only to spread the code out, it has no other meaning and |
| 290 |
// can wrap around. |
| 291 |
static mozilla::Atomic<uint32_t, mozilla::MemoryOrdering::ReleaseAcquire> |
| 292 |
counter(0); |
| 293 |
// We assume that the icache line size is 64 bytes, which is close to |
| 294 |
// universally true. |
| 295 |
const size_t cacheLineSize = 64; |
| 296 |
const size_t systemPageSize = gc::SystemPageSize(); |
| 297 |
|
| 298 |
// If we're not write-protecting code, then we do not need to add any padding |
| 299 |
if (!JitOptions.writeProtectCode) { |
| 300 |
return 0; |
| 301 |
} |
| 302 |
|
| 303 |
// Don't add more than 3/4 of a page of padding |
| 304 |
size_t maxPadBytes = ((systemPageSize * 3) / 4); |
| 305 |
size_t maxPadLines = maxPadBytes / cacheLineSize; |
| 306 |
|
| 307 |
// If code length is close to a page boundary, avoid pushing it to a new page |
| 308 |
size_t remainingBytesInPage = |
| 309 |
AlignBytes(codeLength, systemPageSize) - codeLength; |
| 310 |
size_t remainingLinesInPage = remainingBytesInPage / cacheLineSize; |
| 311 |
|
| 312 |
// Limit padding to the smallest of the above |
| 313 |
size_t padLinesAvailable = std::min(maxPadLines, remainingLinesInPage); |
| 314 |
|
| 315 |
// Don't add any padding if none is available |
| 316 |
if (padLinesAvailable == 0) { |
| 317 |
return 0; |
| 318 |
} |
| 319 |
|
| 320 |
uint32_t random = counter++; |
| 321 |
uint32_t padding = (random % padLinesAvailable) * cacheLineSize; |
| 322 |
// "adding on the padding area doesn't change the total number of pages |
| 323 |
// required" |
| 324 |
MOZ_ASSERT(AlignBytes(codeLength + padding, systemPageSize) == |
| 325 |
AlignBytes(codeLength, systemPageSize)); |
| 326 |
return padding; |
| 327 |
} |
| 328 |
|
| 329 |
/* static */ |
| 330 |
SharedCodeSegment CodeSegment::allocate(const CodeSource& codeSource, |
| 331 |
SharedCodeSegmentVector* segmentPool, |
| 332 |
bool allowLastDitchGC, |
| 333 |
uint8_t** codeStart, |
| 334 |
uint32_t* allocationLength) { |
| 335 |
mozilla::Maybe<AutoMarkJitCodeWritableForThread> writable; |
| 336 |
uint32_t codeLength = codeSource.lengthBytes(); |
| 337 |
uint32_t paddingLength = RandomPaddingForCodeLength(codeLength); |
| 338 |
*allocationLength = |
| 339 |
CodeSegment::AlignAllocationBytes(paddingLength + codeLength); |
| 340 |
|
| 341 |
// If we have a pool of segments, try to find one that has enough space. We |
| 342 |
// just check the last segment in the pool for simplicity. |
| 343 |
SharedCodeSegment segment; |
| 344 |
if (segmentPool && !segmentPool->empty() && |
| 345 |
segmentPool->back()->hasSpace(*allocationLength)) { |
| 346 |
segment = segmentPool->back(); |
| 347 |
} else { |
| 348 |
uint32_t newSegmentCapacity = |
| 349 |
RoundupExecutableCodePageSize(*allocationLength); |
| 350 |
segment = |
| 351 |
CodeSegment::create(writable, newSegmentCapacity, allowLastDitchGC); |
| 352 |
if (!segment) { |
| 353 |
return nullptr; |
| 354 |
} |
| 355 |
if (segmentPool && !segmentPool->append(segment)) { |
| 356 |
return nullptr; |
| 357 |
} |
| 358 |
} |
| 359 |
|
| 360 |
// Claim space in the segment we found or created |
| 361 |
uint8_t* allocationStart = nullptr; |
| 362 |
segment->claimSpace(*allocationLength, &allocationStart); |
| 363 |
*codeStart = allocationStart + paddingLength; |
| 364 |
|
| 365 |
// Check our constraints |
| 366 |
MOZ_ASSERT(CodeSegment::IsAligned(uintptr_t(segment->base()))); |
| 367 |
MOZ_ASSERT(CodeSegment::IsAligned(allocationStart - segment->base())); |
| 368 |
MOZ_ASSERT(CodeSegment::IsAligned(uintptr_t(allocationStart))); |
| 369 |
MOZ_ASSERT(*codeStart >= allocationStart); |
| 370 |
MOZ_ASSERT(codeLength <= *allocationLength); |
| 371 |
MOZ_ASSERT_IF(JitOptions.writeProtectCode, |
| 372 |
uintptr_t(allocationStart) % gc::SystemPageSize() == 0 && |
| 373 |
*allocationLength % gc::SystemPageSize() == 0); |
| 374 |
MOZ_ASSERT(uintptr_t(*codeStart) % jit::CodeAlignment == 0); |
| 375 |
|
| 376 |
if (!writable) { |
| 377 |
writable.emplace(); |
| 378 |
} |
| 379 |
if (!codeSource.copyAndLink(*writable, *codeStart)) { |
| 380 |
return nullptr; |
| 381 |
} |
| 382 |
|
| 383 |
// Clear the padding between the end of the code and the end of the |
| 384 |
// allocation. |
| 385 |
uint8_t* allocationEnd = allocationStart + *allocationLength; |
| 386 |
uint8_t* codeEnd = *codeStart + codeLength; |
| 387 |
MOZ_ASSERT(codeEnd <= allocationEnd); |
| 388 |
size_t paddingAfterCode = allocationEnd - codeEnd; |
| 389 |
// The swept code pattern is guaranteed to crash if it is ever executed. |
| 390 |
memset(codeEnd, JS_SWEPT_CODE_PATTERN, paddingAfterCode); |
| 391 |
|
| 392 |
// Optimized compilation finishes on a background thread, so we must make sure |
| 393 |
// to flush the icaches of all the executing threads. |
| 394 |
// Reprotect the whole region to avoid having separate RW and RX mappings. |
| 395 |
if (*allocationLength != 0 && |
| 396 |
!ExecutableAllocator::makeExecutableAndFlushICache(allocationStart, |
| 397 |
*allocationLength)) { |
| 398 |
return nullptr; |
| 399 |
} |
| 400 |
|
| 401 |
return segment; |
| 402 |
} |
| 403 |
|
| 404 |
void CodeSegment::addSizeOfMisc(MallocSizeOf mallocSizeOf, size_t* code, |
| 405 |
size_t* data) const { |
| 406 |
*code += capacityBytes(); |
| 407 |
*data += mallocSizeOf(this); |
| 408 |
} |
| 409 |
|
| 410 |
size_t CacheableChars::sizeOfExcludingThis(MallocSizeOf mallocSizeOf) const { |
| 411 |
return mallocSizeOf(get()); |
| 412 |
} |
| 413 |
|
| 414 |
static constexpr unsigned LAZY_STUB_LIFO_DEFAULT_CHUNK_SIZE = 8 * 1024; |
| 415 |
|
| 416 |
bool Code::createManyLazyEntryStubs(const WriteGuard& guard, |
| 417 |
const Uint32Vector& funcExportIndices, |
| 418 |
const CodeBlock& tierCodeBlock, |
| 419 |
size_t* stubBlockIndex) const { |
| 420 |
MOZ_ASSERT(funcExportIndices.length()); |
| 421 |
|
| 422 |
LifoAlloc lifo(LAZY_STUB_LIFO_DEFAULT_CHUNK_SIZE, js::MallocArena); |
| 423 |
TempAllocator alloc(&lifo); |
| 424 |
JitContext jitContext; |
| 425 |
WasmMacroAssembler masm(alloc); |
| 426 |
|
| 427 |
const FuncExportVector& funcExports = tierCodeBlock.funcExports; |
| 428 |
uint8_t* codeBase = tierCodeBlock.base(); |
| 429 |
|
| 430 |
CodeRangeVector codeRanges; |
| 431 |
DebugOnly<uint32_t> numExpectedRanges = 0; |
| 432 |
for (uint32_t funcExportIndex : funcExportIndices) { |
| 433 |
const FuncExport& fe = funcExports[funcExportIndex]; |
| 434 |
const FuncType& funcType = codeMeta_->getFuncType(fe.funcIndex()); |
| 435 |
// Exports that don't support a jit entry get only the interp entry. |
| 436 |
numExpectedRanges += (funcType.canHaveJitEntry() ? 2 : 1); |
| 437 |
void* calleePtr = |
| 438 |
codeBase + tierCodeBlock.codeRange(fe).funcUncheckedCallEntry(); |
| 439 |
Maybe<ImmPtr> callee; |
| 440 |
callee.emplace(calleePtr, ImmPtr::NoCheckToken()); |
| 441 |
if (!GenerateEntryStubs(masm, funcExportIndex, fe, funcType, callee, |
| 442 |
/* asmjs */ false, &codeRanges)) { |
| 443 |
return false; |
| 444 |
} |
| 445 |
} |
| 446 |
MOZ_ASSERT(codeRanges.length() == numExpectedRanges, |
| 447 |
"incorrect number of entries per function"); |
| 448 |
|
| 449 |
masm.finish(); |
| 450 |
|
| 451 |
MOZ_ASSERT(masm.inliningContext().empty()); |
| 452 |
MOZ_ASSERT(masm.callSites().empty()); |
| 453 |
MOZ_ASSERT(masm.callSiteTargets().empty()); |
| 454 |
MOZ_ASSERT(masm.trapSites().empty()); |
| 455 |
MOZ_ASSERT(masm.tryNotes().empty()); |
| 456 |
MOZ_ASSERT(masm.codeRangeUnwindInfos().empty()); |
| 457 |
|
| 458 |
if (masm.oom()) { |
| 459 |
return false; |
| 460 |
} |
| 461 |
|
| 462 |
UniqueCodeBlock stubCodeBlock = |
| 463 |
MakeUnique<CodeBlock>(CodeBlockKind::LazyStubs); |
| 464 |
if (!stubCodeBlock) { |
| 465 |
return false; |
| 466 |
} |
| 467 |
|
| 468 |
// Allocate space in a code segment we can use |
| 469 |
uint32_t codeLength = masm.bytesNeeded(); |
| 470 |
uint8_t* codeStart; |
| 471 |
uint32_t allocationLength; |
| 472 |
CodeSource codeSource(masm, nullptr, nullptr); |
| 473 |
stubCodeBlock->segment = CodeSegment::allocate( |
| 474 |
codeSource, &guard->lazyStubSegments, |
| 475 |
/* allowLastDitchGC = */ true, &codeStart, &allocationLength); |
| 476 |
if (!stubCodeBlock->segment) { |
| 477 |
return false; |
| 478 |
} |
| 479 |
|
| 480 |
stubCodeBlock->codeBase = codeStart; |
| 481 |
stubCodeBlock->codeLength = codeLength; |
| 482 |
stubCodeBlock->codeRanges = std::move(codeRanges); |
| 483 |
|
| 484 |
*stubBlockIndex = guard->blocks.length(); |
| 485 |
|
| 486 |
uint32_t codeRangeIndex = 0; |
| 487 |
for (uint32_t funcExportIndex : funcExportIndices) { |
| 488 |
const FuncExport& fe = funcExports[funcExportIndex]; |
| 489 |
const FuncType& funcType = codeMeta_->getFuncType(fe.funcIndex()); |
| 490 |
|
| 491 |
LazyFuncExport lazyExport(fe.funcIndex(), *stubBlockIndex, codeRangeIndex, |
| 492 |
tierCodeBlock.kind); |
| 493 |
|
| 494 |
codeRangeIndex += 1; |
| 495 |
|
| 496 |
if (funcType.canHaveJitEntry()) { |
| 497 |
codeRangeIndex += 1; |
| 498 |
} |
| 499 |
|
| 500 |
size_t exportIndex; |
| 501 |
const uint32_t targetFunctionIndex = fe.funcIndex(); |
| 502 |
|
| 503 |
if (BinarySearchIf( |
| 504 |
guard->lazyExports, 0, guard->lazyExports.length(), |
| 505 |
[targetFunctionIndex](const LazyFuncExport& funcExport) { |
| 506 |
return targetFunctionIndex - funcExport.funcIndex; |
| 507 |
}, |
| 508 |
&exportIndex)) { |
| 509 |
DebugOnly<CodeBlockKind> oldKind = |
| 510 |
guard->lazyExports[exportIndex].funcKind; |
| 511 |
MOZ_ASSERT(oldKind == CodeBlockKind::SharedStubs || |
| 512 |
oldKind == CodeBlockKind::BaselineTier); |
| 513 |
guard->lazyExports[exportIndex] = std::move(lazyExport); |
| 514 |
} else if (!guard->lazyExports.insert( |
| 515 |
guard->lazyExports.begin() + exportIndex, |
| 516 |
std::move(lazyExport))) { |
| 517 |
return false; |
| 518 |
} |
| 519 |
} |
| 520 |
|
| 521 |
stubCodeBlock->sendToProfiler(*codeMeta_, *codeTailMeta_, codeMetaForAsmJS_, |
| 522 |
FuncIonPerfSpewerSpan(), |
| 523 |
FuncBaselinePerfSpewerSpan()); |
| 524 |
return addCodeBlock(guard, std::move(stubCodeBlock), nullptr); |
| 525 |
} |
| 526 |
|
| 527 |
bool Code::createOneLazyEntryStub(const WriteGuard& guard, |
| 528 |
uint32_t funcExportIndex, |
| 529 |
const CodeBlock& tierCodeBlock, |
| 530 |
void** interpEntry) const { |
| 531 |
Uint32Vector funcExportIndexes; |
| 532 |
if (!funcExportIndexes.append(funcExportIndex)) { |
| 533 |
return false; |
| 534 |
} |
| 535 |
|
| 536 |
size_t stubBlockIndex; |
| 537 |
if (!createManyLazyEntryStubs(guard, funcExportIndexes, tierCodeBlock, |
| 538 |
&stubBlockIndex)) { |
| 539 |
return false; |
| 540 |
} |
| 541 |
|
| 542 |
const CodeBlock& block = *guard->blocks[stubBlockIndex]; |
| 543 |
const CodeRangeVector& codeRanges = block.codeRanges; |
| 544 |
|
| 545 |
const FuncExport& fe = tierCodeBlock.funcExports[funcExportIndex]; |
| 546 |
const FuncType& funcType = codeMeta_->getFuncType(fe.funcIndex()); |
| 547 |
|
| 548 |
// We created one or two stubs, depending on the function type. |
| 549 |
uint32_t funcEntryRanges = funcType.canHaveJitEntry() ? 2 : 1; |
| 550 |
MOZ_ASSERT(codeRanges.length() >= funcEntryRanges); |
| 551 |
|
| 552 |
// The first created range is the interp entry |
| 553 |
const CodeRange& interpRange = |
| 554 |
codeRanges[codeRanges.length() - funcEntryRanges]; |
| 555 |
MOZ_ASSERT(interpRange.isInterpEntry()); |
| 556 |
*interpEntry = block.base() + interpRange.begin(); |
| 557 |
|
| 558 |
// The second created range is the jit entry |
| 559 |
if (funcType.canHaveJitEntry()) { |
| 560 |
const CodeRange& jitRange = |
| 561 |
codeRanges[codeRanges.length() - funcEntryRanges + 1]; |
| 562 |
MOZ_ASSERT(jitRange.isJitEntry()); |
| 563 |
jumpTables_.setJitEntry(jitRange.funcIndex(), |
| 564 |
block.base() + jitRange.begin()); |
| 565 |
} |
| 566 |
return true; |
| 567 |
} |
| 568 |
|
| 569 |
bool Code::getOrCreateInterpEntry(uint32_t funcIndex, |
| 570 |
const FuncExport** funcExport, |
| 571 |
void** interpEntry) const { |
| 572 |
size_t funcExportIndex; |
| 573 |
const CodeBlock& codeBlock = funcCodeBlock(funcIndex); |
| 574 |
*funcExport = &codeBlock.lookupFuncExport(funcIndex, &funcExportIndex); |
| 575 |
|
| 576 |
const FuncExport& fe = **funcExport; |
| 577 |
if (fe.hasEagerStubs()) { |
| 578 |
*interpEntry = codeBlock.base() + fe.eagerInterpEntryOffset(); |
| 579 |
return true; |
| 580 |
} |
| 581 |
|
| 582 |
MOZ_ASSERT(!codeMetaForAsmJS_, "only wasm can lazily export functions"); |
| 583 |
|
| 584 |
auto guard = data_.writeLock(); |
| 585 |
*interpEntry = lookupLazyInterpEntry(guard, funcIndex); |
| 586 |
if (*interpEntry) { |
| 587 |
return true; |
| 588 |
} |
| 589 |
|
| 590 |
return createOneLazyEntryStub(guard, funcExportIndex, codeBlock, interpEntry); |
| 591 |
} |
| 592 |
|
| 593 |
bool Code::createTier2LazyEntryStubs(const WriteGuard& guard, |
| 594 |
const CodeBlock& tier2Code, |
| 595 |
Maybe<size_t>* outStubBlockIndex) const { |
| 596 |
if (!guard->lazyExports.length()) { |
| 597 |
return true; |
| 598 |
} |
| 599 |
|
| 600 |
Uint32Vector funcExportIndices; |
| 601 |
if (!funcExportIndices.reserve(guard->lazyExports.length())) { |
| 602 |
return false; |
| 603 |
} |
| 604 |
|
| 605 |
for (size_t i = 0; i < tier2Code.funcExports.length(); i++) { |
| 606 |
const FuncExport& fe = tier2Code.funcExports[i]; |
| 607 |
const LazyFuncExport* lfe = lookupLazyFuncExport(guard, fe.funcIndex()); |
| 608 |
if (lfe) { |
| 609 |
MOZ_ASSERT(lfe->funcKind == CodeBlockKind::BaselineTier); |
| 610 |
funcExportIndices.infallibleAppend(i); |
| 611 |
} |
| 612 |
} |
| 613 |
|
| 614 |
if (funcExportIndices.length() == 0) { |
| 615 |
return true; |
| 616 |
} |
| 617 |
|
| 618 |
size_t stubBlockIndex; |
| 619 |
if (!createManyLazyEntryStubs(guard, funcExportIndices, tier2Code, |
| 620 |
&stubBlockIndex)) { |
| 621 |
return false; |
| 622 |
} |
| 623 |
|
| 624 |
outStubBlockIndex->emplace(stubBlockIndex); |
| 625 |
return true; |
| 626 |
} |
| 627 |
|
| 628 |
class Module::PartialTier2CompileTaskImpl : public PartialTier2CompileTask { |
| 629 |
const SharedCode code_; |
| 630 |
uint32_t funcIndex_; |
| 631 |
Atomic<bool> cancelled_; |
| 632 |
|
| 633 |
public: |
| 634 |
PartialTier2CompileTaskImpl(const Code& code, uint32_t funcIndex) |
| 635 |
: code_(&code), funcIndex_(funcIndex), cancelled_(false) {} |
| 636 |
|
| 637 |
void cancel() override { cancelled_ = true; } |
| 638 |
|
| 639 |
void runHelperThreadTask(AutoLockHelperThreadState& locked) override { |
| 640 |
if (!cancelled_) { |
| 641 |
AutoUnlockHelperThreadState unlock(locked); |
| 642 |
|
| 643 |
// In the case `!success && !cancelled_`, compilation has failed |
| 644 |
// and this function will be stuck in state TierUpState::Requested |
| 645 |
// forever. |
| 646 |
UniqueChars error; |
| 647 |
UniqueCharsVector warnings; |
| 648 |
bool success = CompilePartialTier2(*code_, funcIndex_, &error, &warnings, |
| 649 |
&cancelled_); |
| 650 |
ReportTier2ResultsOffThread( |
| 651 |
cancelled_, success, mozilla::Some(funcIndex_), |
| 652 |
code_->codeMeta().scriptedCaller(), error, warnings); |
| 653 |
} |
| 654 |
|
| 655 |
// The task is finished, release it. |
| 656 |
js_delete(this); |
| 657 |
} |
| 658 |
|
| 659 |
ThreadType threadType() override { |
| 660 |
return ThreadType::THREAD_TYPE_WASM_COMPILE_PARTIAL_TIER2; |
| 661 |
} |
| 662 |
}; |
| 663 |
|
| 664 |
bool Code::requestTierUp(uint32_t funcIndex) const { |
| 665 |
// Note: this runs on the requesting (wasm-running) thread, not on a |
| 666 |
// compilation-helper thread. |
| 667 |
MOZ_ASSERT(mode_ == CompileMode::LazyTiering); |
| 668 |
FuncState& state = funcStates_[funcIndex - codeMeta_->numFuncImports]; |
| 669 |
if (!state.tierUpState.compareExchange(TierUpState::NotRequested, |
| 670 |
TierUpState::Requested)) { |
| 671 |
return true; |
| 672 |
} |
| 673 |
|
| 674 |
auto task = |
| 675 |
js::MakeUnique<Module::PartialTier2CompileTaskImpl>(*this, funcIndex); |
| 676 |
if (!task) { |
| 677 |
// Effect is (I think), if we OOM here, the request is ignored. |
| 678 |
// See bug 1911060. |
| 679 |
return false; |
| 680 |
} |
| 681 |
|
| 682 |
StartOffThreadWasmPartialTier2Compile(std::move(task)); |
| 683 |
return true; |
| 684 |
} |
| 685 |
|
| 686 |
bool Code::finishTier2(UniqueCodeBlock tier2CodeBlock, |
| 687 |
UniqueLinkData tier2LinkData, |
| 688 |
const CompileAndLinkStats& tier2Stats) const { |
| 689 |
MOZ_RELEASE_ASSERT(mode_ == CompileMode::EagerTiering || |
| 690 |
mode_ == CompileMode::LazyTiering); |
| 691 |
MOZ_RELEASE_ASSERT(hasCompleteTier2_ == false && |
| 692 |
tier2CodeBlock->tier() == Tier::Optimized); |
| 693 |
// Acquire the write guard before we start mutating anything. We hold this |
| 694 |
// for the minimum amount of time necessary. |
| 695 |
CodeBlock* tier2CodePointer; |
| 696 |
{ |
| 697 |
auto guard = data_.writeLock(); |
| 698 |
|
| 699 |
// Record the tier2 stats. |
| 700 |
guard->tier2Stats.merge(tier2Stats); |
| 701 |
|
| 702 |
// Borrow the tier2 pointer before moving it into the block vector. This |
| 703 |
// ensures we maintain the invariant that completeTier2_ is never read if |
| 704 |
// hasCompleteTier2_ is false. |
| 705 |
tier2CodePointer = tier2CodeBlock.get(); |
| 706 |
|
| 707 |
// Publish this code to the process wide map. |
| 708 |
if (!addCodeBlock(guard, std::move(tier2CodeBlock), |
| 709 |
std::move(tier2LinkData))) { |
| 710 |
return false; |
| 711 |
} |
| 712 |
|
| 713 |
// Before we can make tier-2 live, we need to compile tier2 versions of any |
| 714 |
// extant tier1 lazy stubs (otherwise, tiering would break the assumption |
| 715 |
// that any extant exported wasm function has had a lazy entry stub already |
| 716 |
// compiled for it). |
| 717 |
// |
| 718 |
// Also see doc block for stubs in WasmJS.cpp. |
| 719 |
Maybe<size_t> stub2Index; |
| 720 |
if (!createTier2LazyEntryStubs(guard, *tier2CodePointer, &stub2Index)) { |
| 721 |
return false; |
| 722 |
} |
| 723 |
|
| 724 |
// Initializing the code above will have flushed the icache for all cores. |
| 725 |
// However, there could still be stale data in the execution pipeline of |
| 726 |
// other cores on some platforms. Force an execution context flush on all |
| 727 |
// threads to fix this before we commit the code. |
| 728 |
// |
| 729 |
// This is safe due to the check in `PlatformCanTier` in WasmCompile.cpp |
| 730 |
jit::FlushExecutionContextForAllThreads(); |
| 731 |
|
| 732 |
// Now that we can't fail or otherwise abort tier2, make it live. |
| 733 |
if (mode_ == CompileMode::EagerTiering) { |
| 734 |
completeTier2_ = tier2CodePointer; |
| 735 |
hasCompleteTier2_ = true; |
| 736 |
|
| 737 |
// We don't need to update funcStates, because we're doing eager tiering |
| 738 |
MOZ_ASSERT(!funcStates_.get()); |
| 739 |
} else { |
| 740 |
for (const CodeRange& cr : tier2CodePointer->codeRanges) { |
| 741 |
if (!cr.isFunction()) { |
| 742 |
continue; |
| 743 |
} |
| 744 |
FuncState& state = |
| 745 |
funcStates_.get()[cr.funcIndex() - codeMeta_->numFuncImports]; |
| 746 |
state.bestTier = tier2CodePointer; |
| 747 |
state.tierUpState = TierUpState::Finished; |
| 748 |
} |
| 749 |
} |
| 750 |
|
| 751 |
// Update jump vectors with pointers to tier-2 lazy entry stubs, if any. |
| 752 |
if (stub2Index) { |
| 753 |
const CodeBlock& block = *guard->blocks[*stub2Index]; |
| 754 |
for (const CodeRange& cr : block.codeRanges) { |
| 755 |
if (!cr.isJitEntry()) { |
| 756 |
continue; |
| 757 |
} |
| 758 |
jumpTables_.setJitEntry(cr.funcIndex(), block.base() + cr.begin()); |
| 759 |
} |
| 760 |
} |
| 761 |
} |
| 762 |
|
| 763 |
// And we update the jump vectors with pointers to tier-2 functions and eager |
| 764 |
// stubs. Callers will continue to invoke tier-1 code until, suddenly, they |
| 765 |
// will invoke tier-2 code. This is benign. |
| 766 |
uint8_t* base = tier2CodePointer->base(); |
| 767 |
for (const CodeRange& cr : tier2CodePointer->codeRanges) { |
| 768 |
// These are racy writes that we just want to be visible, atomically, |
| 769 |
// eventually. All hardware we care about will do this right. But |
| 770 |
// we depend on the compiler not splitting the stores hidden inside the |
| 771 |
// set*Entry functions. |
| 772 |
if (cr.isFunction()) { |
| 773 |
jumpTables_.setTieringEntry(cr.funcIndex(), base + cr.funcTierEntry()); |
| 774 |
} else if (cr.isJitEntry()) { |
| 775 |
jumpTables_.setJitEntry(cr.funcIndex(), base + cr.begin()); |
| 776 |
} |
| 777 |
} |
| 778 |
return true; |
| 779 |
} |
| 780 |
|
| 781 |
bool Code::addCodeBlock(const WriteGuard& guard, UniqueCodeBlock block, |
| 782 |
UniqueLinkData maybeLinkData) const { |
| 783 |
// Don't bother saving the link data if the block won't be serialized |
| 784 |
if (maybeLinkData && !block->isSerializable()) { |
| 785 |
maybeLinkData = nullptr; |
| 786 |
} |
| 787 |
|
| 788 |
CodeBlock* blockPtr = block.get(); |
| 789 |
size_t codeBlockIndex = guard->blocks.length(); |
| 790 |
return guard->blocks.append(std::move(block)) && |
| 791 |
guard->blocksLinkData.append(std::move(maybeLinkData)) && |
| 792 |
blockMap_.insert(blockPtr) && |
| 793 |
blockPtr->initialize(*this, codeBlockIndex); |
| 794 |
} |
| 795 |
|
| 796 |
SharedCodeSegment Code::createFuncCodeSegmentFromPool( |
| 797 |
jit::MacroAssembler& masm, const LinkData& linkData, bool allowLastDitchGC, |
| 798 |
uint8_t** codeStartOut, uint32_t* codeLengthOut) const { |
| 799 |
uint32_t codeLength = masm.bytesNeeded(); |
| 800 |
|
| 801 |
// Allocate the code segment |
| 802 |
uint8_t* codeStart; |
| 803 |
uint32_t allocationLength; |
| 804 |
SharedCodeSegment segment; |
| 805 |
{ |
| 806 |
auto guard = data_.writeLock(); |
| 807 |
CodeSource codeSource(masm, &linkData, this); |
| 808 |
segment = |
| 809 |
CodeSegment::allocate(codeSource, &guard->lazyFuncSegments, |
| 810 |
allowLastDitchGC, &codeStart, &allocationLength); |
| 811 |
if (!segment) { |
| 812 |
return nullptr; |
| 813 |
} |
| 814 |
|
| 815 |
// This function is always used with tier-2 |
| 816 |
guard->tier2Stats.codeBytesMapped += allocationLength; |
| 817 |
guard->tier2Stats.codeBytesUsed += codeLength; |
| 818 |
} |
| 819 |
|
| 820 |
*codeStartOut = codeStart; |
| 821 |
*codeLengthOut = codeLength; |
| 822 |
return segment; |
| 823 |
} |
| 824 |
|
| 825 |
const LazyFuncExport* Code::lookupLazyFuncExport(const WriteGuard& guard, |
| 826 |
uint32_t funcIndex) const { |
| 827 |
size_t match; |
| 828 |
if (!BinarySearchIf( |
| 829 |
guard->lazyExports, 0, guard->lazyExports.length(), |
| 830 |
[funcIndex](const LazyFuncExport& funcExport) { |
| 831 |
return funcIndex - funcExport.funcIndex; |
| 832 |
}, |
| 833 |
&match)) { |
| 834 |
return nullptr; |
| 835 |
} |
| 836 |
return &guard->lazyExports[match]; |
| 837 |
} |
| 838 |
|
| 839 |
void* Code::lookupLazyInterpEntry(const WriteGuard& guard, |
| 840 |
uint32_t funcIndex) const { |
| 841 |
const LazyFuncExport* fe = lookupLazyFuncExport(guard, funcIndex); |
| 842 |
if (!fe) { |
| 843 |
return nullptr; |
| 844 |
} |
| 845 |
const CodeBlock& block = *guard->blocks[fe->lazyStubBlockIndex]; |
| 846 |
return block.base() + block.codeRanges[fe->funcCodeRangeIndex].begin(); |
| 847 |
} |
| 848 |
|
| 849 |
CodeBlock::~CodeBlock() { |
| 850 |
if (unregisterOnDestroy_) { |
| 851 |
UnregisterCodeBlock(this); |
| 852 |
} |
| 853 |
} |
| 854 |
|
| 855 |
bool CodeBlock::initialize(const Code& code, size_t codeBlockIndex) { |
| 856 |
MOZ_ASSERT(!initialized()); |
| 857 |
this->code = &code; |
| 858 |
this->codeBlockIndex = codeBlockIndex; |
| 859 |
segment->setCode(code); |
| 860 |
|
| 861 |
// In the case of tiering, RegisterCodeBlock() immediately makes this code |
| 862 |
// block live to access from other threads executing the containing |
| 863 |
// module. So only call once the CodeBlock is fully initialized. |
| 864 |
if (!RegisterCodeBlock(this)) { |
| 865 |
return false; |
| 866 |
} |
| 867 |
|
| 868 |
// This bool is only used by the destructor which cannot be called racily |
| 869 |
// and so it is not a problem to mutate it after RegisterCodeBlock(). |
| 870 |
MOZ_ASSERT(!unregisterOnDestroy_); |
| 871 |
unregisterOnDestroy_ = true; |
| 872 |
|
| 873 |
MOZ_ASSERT(initialized()); |
| 874 |
return true; |
| 875 |
} |
| 876 |
|
| 877 |
static JS::UniqueChars DescribeCodeRangeForProfiler( |
| 878 |
const wasm::CodeMetadata& codeMeta, |
| 879 |
const wasm::CodeTailMetadata& codeTailMeta, |
| 880 |
const CodeMetadataForAsmJS* codeMetaForAsmJS, const CodeRange& codeRange, |
| 881 |
CodeBlockKind codeBlockKind) { |
| 882 |
uint32_t funcIndex = codeRange.funcIndex(); |
| 883 |
UTF8Bytes name; |
| 884 |
bool ok; |
| 885 |
if (codeMetaForAsmJS) { |
| 886 |
ok = codeMetaForAsmJS->getFuncNameForAsmJS(funcIndex, &name); |
| 887 |
} else { |
| 888 |
ok = codeMeta.getFuncNameForWasm(NameContext::Standalone, funcIndex, |
| 889 |
codeTailMeta.nameSectionPayload.get(), |
| 890 |
&name); |
| 891 |
} |
| 892 |
if (!ok) { |
| 893 |
return nullptr; |
| 894 |
} |
| 895 |
if (!name.append('\0')) { |
| 896 |
return nullptr; |
| 897 |
} |
| 898 |
|
| 899 |
const char* category = ""; |
| 900 |
const char* filename = codeMeta.scriptedCaller().filename.get(); |
| 901 |
const char* suffix = ""; |
| 902 |
if (codeRange.isFunction()) { |
| 903 |
category = "Wasm"; |
| 904 |
if (codeBlockKind == CodeBlockKind::BaselineTier) { |
| 905 |
suffix = " [baseline]"; |
| 906 |
} else if (codeBlockKind == CodeBlockKind::OptimizedTier) { |
| 907 |
suffix = " [optimized]"; |
| 908 |
} |
| 909 |
} else if (codeRange.isInterpEntry()) { |
| 910 |
category = "WasmTrampoline"; |
| 911 |
suffix = " slow entry"; |
| 912 |
} else if (codeRange.isJitEntry()) { |
| 913 |
category = "WasmTrampoline"; |
| 914 |
suffix = " fast entry"; |
| 915 |
} else if (codeRange.isImportInterpExit()) { |
| 916 |
category = "WasmTrampoline"; |
| 917 |
suffix = " slow exit"; |
| 918 |
} else if (codeRange.isImportJitExit()) { |
| 919 |
category = "WasmTrampoline"; |
| 920 |
suffix = " fast exit"; |
| 921 |
} |
| 922 |
|
| 923 |
return JS_smprintf("%s: %s: Function %s%s (WASM:%u)", category, filename, |
| 924 |
name.begin(), suffix, funcIndex); |
| 925 |
} |
| 926 |
|
| 927 |
void CodeBlock::sendToProfiler( |
| 928 |
const CodeMetadata& codeMeta, const CodeTailMetadata& codeTailMeta, |
| 929 |
const CodeMetadataForAsmJS* codeMetaForAsmJS, |
| 930 |
FuncIonPerfSpewerSpan ionSpewers, |
| 931 |
FuncBaselinePerfSpewerSpan baselineSpewers) const { |
| 932 |
bool enabled = false; |
| 933 |
enabled |= PerfEnabled(); |
| 934 |
#ifdef MOZ_VTUNE |
| 935 |
enabled |= vtune::IsProfilingActive(); |
| 936 |
#endif |
| 937 |
if (!enabled) { |
| 938 |
return; |
| 939 |
} |
| 940 |
|
| 941 |
// We only ever have ion or baseline spewers, and they correspond with our |
| 942 |
// code block kind. |
| 943 |
MOZ_ASSERT(ionSpewers.empty() || baselineSpewers.empty()); |
| 944 |
MOZ_ASSERT_IF(kind == CodeBlockKind::BaselineTier, ionSpewers.empty()); |
| 945 |
MOZ_ASSERT_IF(kind == CodeBlockKind::OptimizedTier, baselineSpewers.empty()); |
| 946 |
bool hasSpewers = !ionSpewers.empty() || !baselineSpewers.empty(); |
| 947 |
|
| 948 |
// Save the collected Ion perf spewers with their IR/source information. |
| 949 |
for (FuncIonPerfSpewer& funcIonSpewer : ionSpewers) { |
| 950 |
const CodeRange& codeRange = this->codeRange(funcIonSpewer.funcIndex); |
| 951 |
UniqueChars desc = DescribeCodeRangeForProfiler( |
| 952 |
codeMeta, codeTailMeta, codeMetaForAsmJS, codeRange, kind); |
| 953 |
if (!desc) { |
| 954 |
return; |
| 955 |
} |
| 956 |
uintptr_t start = uintptr_t(base() + codeRange.begin()); |
| 957 |
uintptr_t size = codeRange.end() - codeRange.begin(); |
| 958 |
funcIonSpewer.spewer.saveWasmProfile(start, size, desc); |
| 959 |
} |
| 960 |
|
| 961 |
// Save the collected baseline perf spewers with their IR/source information. |
| 962 |
for (FuncBaselinePerfSpewer& funcBaselineSpewer : baselineSpewers) { |
| 963 |
const CodeRange& codeRange = this->codeRange(funcBaselineSpewer.funcIndex); |
| 964 |
UniqueChars desc = DescribeCodeRangeForProfiler( |
| 965 |
codeMeta, codeTailMeta, codeMetaForAsmJS, codeRange, kind); |
| 966 |
if (!desc) { |
| 967 |
return; |
| 968 |
} |
| 969 |
uintptr_t start = uintptr_t(base() + codeRange.begin()); |
| 970 |
uintptr_t size = codeRange.end() - codeRange.begin(); |
| 971 |
funcBaselineSpewer.spewer.saveProfile(start, size, desc); |
| 972 |
} |
| 973 |
|
| 974 |
// Save the rest of the code ranges. |
| 975 |
for (const CodeRange& codeRange : codeRanges) { |
| 976 |
if (!codeRange.hasFuncIndex()) { |
| 977 |
continue; |
| 978 |
} |
| 979 |
|
| 980 |
// Skip functions when they have corresponding spewers, as they will have |
| 981 |
// already handled the function. |
| 982 |
if (codeRange.isFunction() && hasSpewers) { |
| 983 |
continue; |
| 984 |
} |
| 985 |
|
| 986 |
UniqueChars desc = DescribeCodeRangeForProfiler( |
| 987 |
codeMeta, codeTailMeta, codeMetaForAsmJS, codeRange, kind); |
| 988 |
if (!desc) { |
| 989 |
return; |
| 990 |
} |
| 991 |
|
| 992 |
uintptr_t start = uintptr_t(base() + codeRange.begin()); |
| 993 |
uintptr_t size = codeRange.end() - codeRange.begin(); |
| 994 |
|
| 995 |
#ifdef MOZ_VTUNE |
| 996 |
if (vtune::IsProfilingActive()) { |
| 997 |
vtune::MarkWasm(vtune::GenerateUniqueMethodID(), desc.get(), (void*)start, |
| 998 |
size); |
| 999 |
} |
| 1000 |
#endif |
| 1001 |
|
| 1002 |
if (PerfEnabled()) { |
| 1003 |
CollectPerfSpewerWasmMap(start, size, std::move(desc)); |
| 1004 |
} |
| 1005 |
} |
| 1006 |
} |
| 1007 |
|
| 1008 |
void CodeBlock::addSizeOfMisc(MallocSizeOf mallocSizeOf, size_t* code, |
| 1009 |
size_t* data) const { |
| 1010 |
segment->addSizeOfMisc(mallocSizeOf, code, data); |
| 1011 |
*data += funcToCodeRange.sizeOfExcludingThis(mallocSizeOf) + |
| 1012 |
codeRanges.sizeOfExcludingThis(mallocSizeOf) + |
| 1013 |
inliningContext.sizeOfExcludingThis(mallocSizeOf) + |
| 1014 |
callSites.sizeOfExcludingThis(mallocSizeOf) + |
| 1015 |
tryNotes.sizeOfExcludingThis(mallocSizeOf) + |
| 1016 |
codeRangeUnwindInfos.sizeOfExcludingThis(mallocSizeOf) + |
| 1017 |
trapSites.sizeOfExcludingThis(mallocSizeOf) + |
| 1018 |
stackMaps.sizeOfExcludingThis(mallocSizeOf) + |
| 1019 |
funcExports.sizeOfExcludingThis(mallocSizeOf); |
| 1020 |
; |
| 1021 |
} |
| 1022 |
|
| 1023 |
const CodeRange* CodeBlock::lookupRange(const void* pc) const { |
| 1024 |
CodeRange::OffsetInCode target((uint8_t*)pc - base()); |
| 1025 |
return LookupInSorted(codeRanges, target); |
| 1026 |
} |
| 1027 |
|
| 1028 |
bool CodeBlock::lookupCallSite(void* pc, CallSite* callSite) const { |
| 1029 |
uint32_t target = ((uint8_t*)pc) - base(); |
| 1030 |
return callSites.lookup(target, inliningContext, callSite); |
| 1031 |
} |
| 1032 |
|
| 1033 |
const StackMap* CodeBlock::lookupStackMap(uint8_t* pc) const { |
| 1034 |
// We need to subtract the offset from the beginning of the codeblock. |
| 1035 |
uint32_t offsetInCodeBlock = pc - base(); |
| 1036 |
return stackMaps.lookup(offsetInCodeBlock); |
| 1037 |
} |
| 1038 |
|
| 1039 |
const wasm::TryNote* CodeBlock::lookupTryNote(const void* pc) const { |
| 1040 |
size_t target = (uint8_t*)pc - base(); |
| 1041 |
|
| 1042 |
// We find the first hit (there may be multiple) to obtain the innermost |
| 1043 |
// handler, which is why we cannot binary search here. |
| 1044 |
for (const auto& tryNote : tryNotes) { |
| 1045 |
if (tryNote.offsetWithinTryBody(target)) { |
| 1046 |
return &tryNote; |
| 1047 |
} |
| 1048 |
} |
| 1049 |
|
| 1050 |
return nullptr; |
| 1051 |
} |
| 1052 |
|
| 1053 |
bool CodeBlock::lookupTrap(void* pc, Trap* kindOut, TrapSite* trapOut) const { |
| 1054 |
MOZ_ASSERT(containsCodePC(pc)); |
| 1055 |
uint32_t target = ((uint8_t*)pc) - base(); |
| 1056 |
return trapSites.lookup(target, inliningContext, kindOut, trapOut); |
| 1057 |
} |
| 1058 |
|
| 1059 |
struct UnwindInfoPCOffset { |
| 1060 |
const CodeRangeUnwindInfoVector& info; |
| 1061 |
explicit UnwindInfoPCOffset(const CodeRangeUnwindInfoVector& info) |
| 1062 |
: info(info) {} |
| 1063 |
uint32_t operator[](size_t index) const { return info[index].offset(); } |
| 1064 |
}; |
| 1065 |
|
| 1066 |
const CodeRangeUnwindInfo* CodeBlock::lookupUnwindInfo(void* pc) const { |
| 1067 |
uint32_t target = ((uint8_t*)pc) - base(); |
| 1068 |
size_t match; |
| 1069 |
const CodeRangeUnwindInfo* info = nullptr; |
| 1070 |
if (BinarySearch(UnwindInfoPCOffset(codeRangeUnwindInfos), 0, |
| 1071 |
codeRangeUnwindInfos.length(), target, &match)) { |
| 1072 |
info = &codeRangeUnwindInfos[match]; |
| 1073 |
} else { |
| 1074 |
// Exact match is not found, using insertion point to get the previous |
| 1075 |
// info entry; skip if info is outside of codeRangeUnwindInfos. |
| 1076 |
if (match == 0) return nullptr; |
| 1077 |
if (match == codeRangeUnwindInfos.length()) { |
| 1078 |
MOZ_ASSERT( |
| 1079 |
codeRangeUnwindInfos[codeRangeUnwindInfos.length() - 1].unwindHow() == |
| 1080 |
CodeRangeUnwindInfo::Normal); |
| 1081 |
return nullptr; |
| 1082 |
} |
| 1083 |
info = &codeRangeUnwindInfos[match - 1]; |
| 1084 |
} |
| 1085 |
return info->unwindHow() == CodeRangeUnwindInfo::Normal ? nullptr : info; |
| 1086 |
} |
| 1087 |
|
| 1088 |
struct ProjectFuncIndex { |
| 1089 |
const FuncExportVector& funcExports; |
| 1090 |
explicit ProjectFuncIndex(const FuncExportVector& funcExports) |
| 1091 |
: funcExports(funcExports) {} |
| 1092 |
uint32_t operator[](size_t index) const { |
| 1093 |
return funcExports[index].funcIndex(); |
| 1094 |
} |
| 1095 |
}; |
| 1096 |
|
| 1097 |
FuncExport& CodeBlock::lookupFuncExport( |
| 1098 |
uint32_t funcIndex, size_t* funcExportIndex /* = nullptr */) { |
| 1099 |
size_t match; |
| 1100 |
if (!BinarySearch(ProjectFuncIndex(funcExports), 0, funcExports.length(), |
| 1101 |
funcIndex, &match)) { |
| 1102 |
MOZ_CRASH("missing function export"); |
| 1103 |
} |
| 1104 |
if (funcExportIndex) { |
| 1105 |
*funcExportIndex = match; |
| 1106 |
} |
| 1107 |
return funcExports[match]; |
| 1108 |
} |
| 1109 |
|
| 1110 |
const FuncExport& CodeBlock::lookupFuncExport(uint32_t funcIndex, |
| 1111 |
size_t* funcExportIndex) const { |
| 1112 |
return const_cast<CodeBlock*>(this)->lookupFuncExport(funcIndex, |
| 1113 |
funcExportIndex); |
| 1114 |
} |
| 1115 |
|
| 1116 |
bool JumpTables::initialize(CompileMode mode, const CodeMetadata& codeMeta, |
| 1117 |
const CodeBlock& sharedStubs, |
| 1118 |
const CodeBlock& tier1) { |
| 1119 |
static_assert(JSScript::offsetOfJitCodeRaw() == 0, |
| 1120 |
"wasm fast jit entry is at (void*) jit[funcIndex]"); |
| 1121 |
|
| 1122 |
mode_ = mode; |
| 1123 |
numFuncs_ = codeMeta.numFuncs(); |
| 1124 |
|
| 1125 |
if (mode_ != CompileMode::Once) { |
| 1126 |
tiering_ = TablePointer(js_pod_calloc<void*>(numFuncs_)); |
| 1127 |
if (!tiering_) { |
| 1128 |
return false; |
| 1129 |
} |
| 1130 |
} |
| 1131 |
|
| 1132 |
// The number of jit entries is overestimated, but it is simpler when |
| 1133 |
// filling/looking up the jit entries and safe (worst case we'll crash |
| 1134 |
// because of a null deref when trying to call the jit entry of an |
| 1135 |
// unexported function). |
| 1136 |
jit_ = TablePointer(js_pod_calloc<void*>(numFuncs_)); |
| 1137 |
if (!jit_) { |
| 1138 |
return false; |
| 1139 |
} |
| 1140 |
|
| 1141 |
uint8_t* codeBase = sharedStubs.base(); |
| 1142 |
for (const CodeRange& cr : sharedStubs.codeRanges) { |
| 1143 |
if (cr.isFunction()) { |
| 1144 |
setTieringEntry(cr.funcIndex(), codeBase + cr.funcTierEntry()); |
| 1145 |
} else if (cr.isJitEntry()) { |
| 1146 |
setJitEntry(cr.funcIndex(), codeBase + cr.begin()); |
| 1147 |
} |
| 1148 |
} |
| 1149 |
|
| 1150 |
codeBase = tier1.base(); |
| 1151 |
for (const CodeRange& cr : tier1.codeRanges) { |
| 1152 |
if (cr.isFunction()) { |
| 1153 |
setTieringEntry(cr.funcIndex(), codeBase + cr.funcTierEntry()); |
| 1154 |
} else if (cr.isJitEntry()) { |
| 1155 |
setJitEntry(cr.funcIndex(), codeBase + cr.begin()); |
| 1156 |
} |
| 1157 |
} |
| 1158 |
return true; |
| 1159 |
} |
| 1160 |
|
| 1161 |
Code::Code(CompileMode mode, const CodeMetadata& codeMeta, |
| 1162 |
const CodeTailMetadata& codeTailMeta, |
| 1163 |
const CodeMetadataForAsmJS* codeMetaForAsmJS) |
| 1164 |
: mode_(mode), |
| 1165 |
data_(mutexid::WasmCodeProtected), |
| 1166 |
codeMeta_(&codeMeta), |
| 1167 |
codeTailMeta_(&codeTailMeta), |
| 1168 |
codeMetaForAsmJS_(codeMetaForAsmJS), |
| 1169 |
completeTier1_(nullptr), |
| 1170 |
completeTier2_(nullptr), |
| 1171 |
profilingLabels_(mutexid::WasmCodeProfilingLabels, |
| 1172 |
CacheableCharsVector()), |
| 1173 |
trapCode_(nullptr), |
| 1174 |
debugStubOffset_(0), |
| 1175 |
requestTierUpStubOffset_(0), |
| 1176 |
updateCallRefMetricsStubOffset_(0) {} |
| 1177 |
|
| 1178 |
Code::~Code() { printStats(); } |
| 1179 |
|
| 1180 |
void Code::printStats() const { |
| 1181 |
#ifdef JS_JITSPEW |
| 1182 |
auto guard = data_.readLock(); |
| 1183 |
|
| 1184 |
JS_LOG(wasmPerf, Info, "CM=..%06lx Code::~Code <<<<", |
| 1185 |
0xFFFFFF & (unsigned long)uintptr_t(codeMeta_.get())); |
| 1186 |
|
| 1187 |
// Module information |
| 1188 |
JS_LOG(wasmPerf, Info, " %7zu functions in module", codeMeta_->numFuncs()); |
| 1189 |
JS_LOG(wasmPerf, Info, " %7zu bytecode bytes in module", |
| 1190 |
codeMeta_->codeSectionSize()); |
| 1191 |
uint32_t numCallRefs = codeTailMeta_->numCallRefMetrics == UINT32_MAX |
| 1192 |
? 0 |
| 1193 |
: codeTailMeta_->numCallRefMetrics; |
| 1194 |
JS_LOG(wasmPerf, Info, " %7u call_refs in module", numCallRefs); |
| 1195 |
|
| 1196 |
// Tier information |
| 1197 |
JS_LOG(wasmPerf, Info, " ------ Tier 1 ------"); |
| 1198 |
guard->tier1Stats.print(); |
| 1199 |
if (mode() != CompileMode::Once) { |
| 1200 |
JS_LOG(wasmPerf, Info, " ------ Tier 2 ------"); |
| 1201 |
guard->tier2Stats.print(); |
| 1202 |
} |
| 1203 |
|
| 1204 |
JS_LOG(wasmPerf, Info, ">>>>"); |
| 1205 |
#endif |
| 1206 |
} |
| 1207 |
|
| 1208 |
bool Code::initialize(FuncImportVector&& funcImports, |
| 1209 |
UniqueCodeBlock sharedStubs, |
| 1210 |
UniqueLinkData sharedStubsLinkData, |
| 1211 |
UniqueCodeBlock tier1CodeBlock, |
| 1212 |
UniqueLinkData tier1LinkData, |
| 1213 |
const CompileAndLinkStats& tier1Stats) { |
| 1214 |
funcImports_ = std::move(funcImports); |
| 1215 |
|
| 1216 |
auto guard = data_.writeLock(); |
| 1217 |
|
| 1218 |
MOZ_ASSERT(guard->tier1Stats.empty()); |
| 1219 |
guard->tier1Stats = tier1Stats; |
| 1220 |
|
| 1221 |
sharedStubs_ = sharedStubs.get(); |
| 1222 |
completeTier1_ = tier1CodeBlock.get(); |
| 1223 |
trapCode_ = sharedStubs_->base() + sharedStubsLinkData->trapOffset; |
| 1224 |
if (!jumpTables_.initialize(mode_, *codeMeta_, *sharedStubs_, |
| 1225 |
*completeTier1_) || |
| 1226 |
!addCodeBlock(guard, std::move(sharedStubs), |
| 1227 |
std::move(sharedStubsLinkData)) || |
| 1228 |
!addCodeBlock(guard, std::move(tier1CodeBlock), |
| 1229 |
std::move(tier1LinkData))) { |
| 1230 |
return false; |
| 1231 |
} |
| 1232 |
|
| 1233 |
if (mode_ == CompileMode::LazyTiering) { |
| 1234 |
uint32_t numFuncDefs = codeMeta_->numFuncs() - codeMeta_->numFuncImports; |
| 1235 |
funcStates_ = FuncStatesPointer(js_pod_calloc<FuncState>(numFuncDefs)); |
| 1236 |
if (!funcStates_) { |
| 1237 |
return false; |
| 1238 |
} |
| 1239 |
for (uint32_t funcDefIndex = 0; funcDefIndex < numFuncDefs; |
| 1240 |
funcDefIndex++) { |
| 1241 |
funcStates_.get()[funcDefIndex].bestTier = completeTier1_; |
| 1242 |
funcStates_.get()[funcDefIndex].tierUpState = TierUpState::NotRequested; |
| 1243 |
} |
| 1244 |
} |
| 1245 |
|
| 1246 |
return true; |
| 1247 |
} |
| 1248 |
|
| 1249 |
Tiers Code::completeTiers() const { |
| 1250 |
if (hasCompleteTier2_) { |
| 1251 |
return Tiers(completeTier1_->tier(), completeTier2_->tier()); |
| 1252 |
} |
| 1253 |
return Tiers(completeTier1_->tier()); |
| 1254 |
} |
| 1255 |
|
| 1256 |
bool Code::hasCompleteTier(Tier t) const { |
| 1257 |
if (hasCompleteTier2_ && completeTier2_->tier() == t) { |
| 1258 |
return true; |
| 1259 |
} |
| 1260 |
return completeTier1_->tier() == t; |
| 1261 |
} |
| 1262 |
|
| 1263 |
Tier Code::stableCompleteTier() const { return completeTier1_->tier(); } |
| 1264 |
|
| 1265 |
Tier Code::bestCompleteTier() const { |
| 1266 |
if (hasCompleteTier2_) { |
| 1267 |
return completeTier2_->tier(); |
| 1268 |
} |
| 1269 |
return completeTier1_->tier(); |
| 1270 |
} |
| 1271 |
|
| 1272 |
const CodeBlock& Code::completeTierCodeBlock(Tier tier) const { |
| 1273 |
switch (tier) { |
| 1274 |
case Tier::Baseline: |
| 1275 |
if (completeTier1_->tier() == Tier::Baseline) { |
| 1276 |
MOZ_ASSERT(completeTier1_->initialized()); |
| 1277 |
return *completeTier1_; |
| 1278 |
} |
| 1279 |
MOZ_CRASH("No code segment at this tier"); |
| 1280 |
case Tier::Optimized: |
| 1281 |
if (completeTier1_->tier() == Tier::Optimized) { |
| 1282 |
MOZ_ASSERT(completeTier1_->initialized()); |
| 1283 |
return *completeTier1_; |
| 1284 |
} |
| 1285 |
// It is incorrect to ask for the optimized tier without there being such |
| 1286 |
// a tier and the tier having been committed. The guard here could |
| 1287 |
// instead be `if (hasCompleteTier2_) ... ` but codeBlock(t) should not be |
| 1288 |
// called in contexts where that test is necessary. |
| 1289 |
MOZ_RELEASE_ASSERT(hasCompleteTier2_); |
| 1290 |
MOZ_ASSERT(completeTier2_->initialized()); |
| 1291 |
return *completeTier2_; |
| 1292 |
} |
| 1293 |
MOZ_CRASH(); |
| 1294 |
} |
| 1295 |
|
| 1296 |
const LinkData* Code::codeBlockLinkData(const CodeBlock& block) const { |
| 1297 |
auto guard = data_.readLock(); |
| 1298 |
MOZ_ASSERT(block.initialized() && block.code == this); |
| 1299 |
return guard->blocksLinkData[block.codeBlockIndex].get(); |
| 1300 |
} |
| 1301 |
|
| 1302 |
void Code::clearLinkData() const { |
| 1303 |
auto guard = data_.writeLock(); |
| 1304 |
for (UniqueLinkData& linkData : guard->blocksLinkData) { |
| 1305 |
linkData = nullptr; |
| 1306 |
} |
| 1307 |
} |
| 1308 |
|
| 1309 |
// When enabled, generate profiling labels for every name in funcNames_ that is |
| 1310 |
// the name of some Function CodeRange. This involves malloc() so do it now |
| 1311 |
// since, once we start sampling, we'll be in a signal-handing context where we |
| 1312 |
// cannot malloc. |
| 1313 |
void Code::ensureProfilingLabels(bool profilingEnabled) const { |
| 1314 |
auto labels = profilingLabels_.lock(); |
| 1315 |
|
| 1316 |
if (!profilingEnabled) { |
| 1317 |
labels->clear(); |
| 1318 |
return; |
| 1319 |
} |
| 1320 |
|
| 1321 |
if (!labels->empty()) { |
| 1322 |
return; |
| 1323 |
} |
| 1324 |
|
| 1325 |
// Any tier will do, we only need tier-invariant data that are incidentally |
| 1326 |
// stored with the code ranges. |
| 1327 |
const CodeBlock& sharedStubsCodeBlock = sharedStubs(); |
| 1328 |
const CodeBlock& tier1CodeBlock = completeTierCodeBlock(stableCompleteTier()); |
| 1329 |
|
| 1330 |
// Ignore any OOM failures, nothing we can do about it |
| 1331 |
(void)appendProfilingLabels(labels, sharedStubsCodeBlock); |
| 1332 |
(void)appendProfilingLabels(labels, tier1CodeBlock); |
| 1333 |
} |
| 1334 |
|
| 1335 |
bool Code::appendProfilingLabels( |
| 1336 |
const ExclusiveData<CacheableCharsVector>::Guard& labels, |
| 1337 |
const CodeBlock& codeBlock) const { |
| 1338 |
for (const CodeRange& codeRange : codeBlock.codeRanges) { |
| 1339 |
if (!codeRange.isFunction()) { |
| 1340 |
continue; |
| 1341 |
} |
| 1342 |
|
| 1343 |
Int32ToCStringBuf cbuf; |
| 1344 |
size_t bytecodeStrLen; |
| 1345 |
const char* bytecodeStr = Uint32ToCString( |
| 1346 |
&cbuf, codeTailMeta().funcBytecodeOffset(codeRange.funcIndex()), |
| 1347 |
&bytecodeStrLen); |
| 1348 |
MOZ_ASSERT(bytecodeStr); |
| 1349 |
|
| 1350 |
UTF8Bytes name; |
| 1351 |
bool ok; |
| 1352 |
if (codeMetaForAsmJS()) { |
| 1353 |
ok = |
| 1354 |
codeMetaForAsmJS()->getFuncNameForAsmJS(codeRange.funcIndex(), &name); |
| 1355 |
} else { |
| 1356 |
ok = codeMeta().getFuncNameForWasm( |
| 1357 |
NameContext::Standalone, codeRange.funcIndex(), |
| 1358 |
codeTailMeta().nameSectionPayload.get(), &name); |
| 1359 |
} |
| 1360 |
if (!ok || !name.append(" (", 2)) { |
| 1361 |
return false; |
| 1362 |
} |
| 1363 |
|
| 1364 |
if (const char* filename = codeMeta().scriptedCaller().filename.get()) { |
| 1365 |
if (!name.append(filename, strlen(filename))) { |
| 1366 |
return false; |
| 1367 |
} |
| 1368 |
} else { |
| 1369 |
if (!name.append('?')) { |
| 1370 |
return false; |
| 1371 |
} |
| 1372 |
} |
| 1373 |
|
| 1374 |
if (!name.append(':') || !name.append(bytecodeStr, bytecodeStrLen) || |
| 1375 |
!name.append(")\0", 2)) { |
| 1376 |
return false; |
| 1377 |
} |
| 1378 |
|
| 1379 |
UniqueChars label(name.extractOrCopyRawBuffer()); |
| 1380 |
if (!label) { |
| 1381 |
return false; |
| 1382 |
} |
| 1383 |
|
| 1384 |
if (codeRange.funcIndex() >= labels->length()) { |
| 1385 |
if (!labels->resize(codeRange.funcIndex() + 1)) { |
| 1386 |
return false; |
| 1387 |
} |
| 1388 |
} |
| 1389 |
|
| 1390 |
((CacheableCharsVector&)labels)[codeRange.funcIndex()] = std::move(label); |
| 1391 |
} |
| 1392 |
return true; |
| 1393 |
} |
| 1394 |
|
| 1395 |
const char* Code::profilingLabel(uint32_t funcIndex) const { |
| 1396 |
auto labels = profilingLabels_.lock(); |
| 1397 |
|
| 1398 |
if (funcIndex >= labels->length() || |
| 1399 |
!((CacheableCharsVector&)labels)[funcIndex]) { |
| 1400 |
return "?"; |
| 1401 |
} |
| 1402 |
return ((CacheableCharsVector&)labels)[funcIndex].get(); |
| 1403 |
} |
| 1404 |
|
| 1405 |
void Code::addSizeOfMiscIfNotSeen( |
| 1406 |
MallocSizeOf mallocSizeOf, CodeMetadata::SeenSet* seenCodeMeta, |
| 1407 |
CodeMetadataForAsmJS::SeenSet* seenCodeMetaForAsmJS, |
| 1408 |
Code::SeenSet* seenCode, size_t* code, size_t* data) const { |
| 1409 |
auto p = seenCode->lookupForAdd(this); |
| 1410 |
if (p) { |
| 1411 |
return; |
| 1412 |
} |
| 1413 |
bool ok = seenCode->add(p, this); |
| 1414 |
(void)ok; // oh well |
| 1415 |
|
| 1416 |
auto guard = data_.readLock(); |
| 1417 |
*data += |
| 1418 |
mallocSizeOf(this) + guard->blocks.sizeOfExcludingThis(mallocSizeOf) + |
| 1419 |
guard->blocksLinkData.sizeOfExcludingThis(mallocSizeOf) + |
| 1420 |
guard->lazyExports.sizeOfExcludingThis(mallocSizeOf) + |
| 1421 |
(codeMetaForAsmJS() ? codeMetaForAsmJS()->sizeOfIncludingThisIfNotSeen( |
| 1422 |
mallocSizeOf, seenCodeMetaForAsmJS) |
| 1423 |
: 0) + |
| 1424 |
funcImports_.sizeOfExcludingThis(mallocSizeOf) + |
| 1425 |
profilingLabels_.lock()->sizeOfExcludingThis(mallocSizeOf) + |
| 1426 |
jumpTables_.sizeOfMiscExcludingThis(); |
| 1427 |
for (const SharedCodeSegment& stub : guard->lazyStubSegments) { |
| 1428 |
stub->addSizeOfMisc(mallocSizeOf, code, data); |
| 1429 |
} |
| 1430 |
|
| 1431 |
sharedStubs().addSizeOfMisc(mallocSizeOf, code, data); |
| 1432 |
for (auto t : completeTiers()) { |
| 1433 |
completeTierCodeBlock(t).addSizeOfMisc(mallocSizeOf, code, data); |
| 1434 |
} |
| 1435 |
} |
| 1436 |
|
| 1437 |
void CodeBlock::disassemble(JSContext* cx, int kindSelection, |
| 1438 |
PrintCallback printString) const { |
| 1439 |
for (const CodeRange& range : codeRanges) { |
| 1440 |
if (kindSelection & (1 << range.kind())) { |
| 1441 |
MOZ_ASSERT(range.begin() < segment->lengthBytes()); |
| 1442 |
MOZ_ASSERT(range.end() < segment->lengthBytes()); |
| 1443 |
|
| 1444 |
const char* kind; |
| 1445 |
char kindbuf[128]; |
| 1446 |
switch (range.kind()) { |
| 1447 |
case CodeRange::Function: |
| 1448 |
kind = "Function"; |
| 1449 |
break; |
| 1450 |
case CodeRange::InterpEntry: |
| 1451 |
kind = "InterpEntry"; |
| 1452 |
break; |
| 1453 |
case CodeRange::JitEntry: |
| 1454 |
kind = "JitEntry"; |
| 1455 |
break; |
| 1456 |
case CodeRange::ImportInterpExit: |
| 1457 |
kind = "ImportInterpExit"; |
| 1458 |
break; |
| 1459 |
case CodeRange::ImportJitExit: |
| 1460 |
kind = "ImportJitExit"; |
| 1461 |
break; |
| 1462 |
default: |
| 1463 |
SprintfLiteral(kindbuf, "CodeRange::Kind(%d)", range.kind()); |
| 1464 |
kind = kindbuf; |
| 1465 |
break; |
| 1466 |
} |
| 1467 |
const char* separator = |
| 1468 |
"\n--------------------------------------------------\n"; |
| 1469 |
// The buffer is quite large in order to accomodate mangled C++ names; |
| 1470 |
// lengths over 3500 have been observed in the wild. |
| 1471 |
char buf[4096]; |
| 1472 |
if (range.hasFuncIndex()) { |
| 1473 |
const char* funcName = "(unknown)"; |
| 1474 |
UTF8Bytes namebuf; |
| 1475 |
bool ok; |
| 1476 |
if (code->codeMetaForAsmJS()) { |
| 1477 |
ok = code->codeMetaForAsmJS()->getFuncNameForAsmJS(range.funcIndex(), |
| 1478 |
&namebuf); |
| 1479 |
} else { |
| 1480 |
ok = code->codeMeta().getFuncNameForWasm( |
| 1481 |
NameContext::Standalone, range.funcIndex(), |
| 1482 |
code->codeTailMeta().nameSectionPayload.get(), &namebuf); |
| 1483 |
} |
| 1484 |
if (ok && namebuf.append('\0')) { |
| 1485 |
funcName = namebuf.begin(); |
| 1486 |
} |
| 1487 |
SprintfLiteral(buf, "%sKind = %s, index = %d, name = %s:\n", separator, |
| 1488 |
kind, range.funcIndex(), funcName); |
| 1489 |
} else { |
| 1490 |
SprintfLiteral(buf, "%sKind = %s\n", separator, kind); |
| 1491 |
} |
| 1492 |
printString(buf); |
| 1493 |
|
| 1494 |
uint8_t* theCode = base() + range.begin(); |
| 1495 |
jit::Disassemble(theCode, range.end() - range.begin(), printString); |
| 1496 |
} |
| 1497 |
} |
| 1498 |
} |
| 1499 |
|
| 1500 |
void Code::disassemble(JSContext* cx, Tier tier, int kindSelection, |
| 1501 |
PrintCallback printString) const { |
| 1502 |
this->sharedStubs().disassemble(cx, kindSelection, printString); |
| 1503 |
this->completeTierCodeBlock(tier).disassemble(cx, kindSelection, printString); |
| 1504 |
} |
| 1505 |
|
| 1506 |
// Return a map with names and associated statistics |
| 1507 |
MetadataAnalysisHashMap Code::metadataAnalysis(JSContext* cx) const { |
| 1508 |
MetadataAnalysisHashMap hashmap; |
| 1509 |
if (!hashmap.reserve(14)) { |
| 1510 |
return hashmap; |
| 1511 |
} |
| 1512 |
|
| 1513 |
for (auto t : completeTiers()) { |
| 1514 |
const CodeBlock& codeBlock = completeTierCodeBlock(t); |
| 1515 |
size_t length = codeBlock.funcToCodeRange.numEntries(); |
| 1516 |
length += codeBlock.codeRanges.length(); |
| 1517 |
length += codeBlock.callSites.length(); |
| 1518 |
length += codeBlock.trapSites.sumOfLengths(); |
| 1519 |
length += codeBlock.funcExports.length(); |
| 1520 |
length += codeBlock.stackMaps.length(); |
| 1521 |
length += codeBlock.tryNotes.length(); |
| 1522 |
|
| 1523 |
hashmap.putNewInfallible("metadata length", length); |
| 1524 |
|
| 1525 |
// Iterate over the Code Ranges and accumulate all pieces of code. |
| 1526 |
size_t code_size = 0; |
| 1527 |
for (const CodeRange& codeRange : codeBlock.codeRanges) { |
| 1528 |
if (!codeRange.isFunction()) { |
| 1529 |
continue; |
| 1530 |
} |
| 1531 |
code_size += codeRange.end() - codeRange.begin(); |
| 1532 |
} |
| 1533 |
|
| 1534 |
hashmap.putNewInfallible("stackmaps number", codeBlock.stackMaps.length()); |
| 1535 |
hashmap.putNewInfallible("trapSites number", |
| 1536 |
codeBlock.trapSites.sumOfLengths()); |
| 1537 |
hashmap.putNewInfallible("codeRange size in bytes", code_size); |
| 1538 |
hashmap.putNewInfallible("code segment capacity", |
| 1539 |
codeBlock.segment->capacityBytes()); |
| 1540 |
|
| 1541 |
auto mallocSizeOf = cx->runtime()->debuggerMallocSizeOf; |
| 1542 |
|
| 1543 |
hashmap.putNewInfallible( |
| 1544 |
"funcToCodeRange size", |
| 1545 |
codeBlock.funcToCodeRange.sizeOfExcludingThis(mallocSizeOf)); |
| 1546 |
hashmap.putNewInfallible( |
| 1547 |
"codeRanges size", |
| 1548 |
codeBlock.codeRanges.sizeOfExcludingThis(mallocSizeOf)); |
| 1549 |
hashmap.putNewInfallible( |
| 1550 |
"callSites size", |
| 1551 |
codeBlock.callSites.sizeOfExcludingThis(mallocSizeOf)); |
| 1552 |
hashmap.putNewInfallible( |
| 1553 |
"tryNotes size", codeBlock.tryNotes.sizeOfExcludingThis(mallocSizeOf)); |
| 1554 |
hashmap.putNewInfallible( |
| 1555 |
"trapSites size", |
| 1556 |
codeBlock.trapSites.sizeOfExcludingThis(mallocSizeOf)); |
| 1557 |
hashmap.putNewInfallible( |
| 1558 |
"stackMaps size", |
| 1559 |
codeBlock.stackMaps.sizeOfExcludingThis(mallocSizeOf)); |
| 1560 |
hashmap.putNewInfallible( |
| 1561 |
"funcExports size", |
| 1562 |
codeBlock.funcExports.sizeOfExcludingThis(mallocSizeOf)); |
| 1563 |
} |
| 1564 |
|
| 1565 |
return hashmap; |
| 1566 |
} |
| 1567 |
|
| 1568 |
void wasm::PatchDebugSymbolicAccesses(uint8_t* codeBase, MacroAssembler& masm) { |
| 1569 |
#ifdef WASM_CODEGEN_DEBUG |
| 1570 |
for (auto& access : masm.symbolicAccesses()) { |
| 1571 |
switch (access.target) { |
| 1572 |
case SymbolicAddress::PrintI32: |
| 1573 |
case SymbolicAddress::PrintPtr: |
| 1574 |
case SymbolicAddress::PrintF32: |
| 1575 |
case SymbolicAddress::PrintF64: |
| 1576 |
case SymbolicAddress::PrintText: |
| 1577 |
break; |
| 1578 |
default: |
| 1579 |
MOZ_CRASH("unexpected symbol in PatchDebugSymbolicAccesses"); |
| 1580 |
} |
| 1581 |
ABIFunctionType abiType; |
| 1582 |
void* target = AddressOf(access.target, &abiType); |
| 1583 |
uint8_t* patchAt = codeBase + access.patchAt.offset(); |
| 1584 |
Assembler::PatchDataWithValueCheck(CodeLocationLabel(patchAt), |
| 1585 |
PatchedImmPtr(target), |
| 1586 |
PatchedImmPtr((void*)-1)); |
| 1587 |
} |
| 1588 |
#else |
| 1589 |
MOZ_ASSERT(masm.symbolicAccesses().empty()); |
| 1590 |
#endif |
| 1591 |
} |