| 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 |
* This Source Code Form is subject to the terms of the Mozilla Public |
| 4 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 5 |
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 6 |
|
| 7 |
#include "jit/JitcodeMap.h" |
| 8 |
|
| 9 |
#include "mozilla/ArrayUtils.h" |
| 10 |
#include "mozilla/MathAlgorithms.h" |
| 11 |
#include "mozilla/Maybe.h" |
| 12 |
#include "mozilla/ScopeExit.h" |
| 13 |
|
| 14 |
#include "gc/Marking.h" |
| 15 |
#include "gc/Zone.h" |
| 16 |
#include "jit/BaselineJIT.h" |
| 17 |
#include "jit/InlineScriptTree.h" |
| 18 |
#include "jit/JitRuntime.h" |
| 19 |
#include "jit/JitSpewer.h" |
| 20 |
#include "js/Vector.h" |
| 21 |
#include "vm/BytecodeLocation.h" // for BytecodeLocation |
| 22 |
#include "vm/GeckoProfiler.h" |
| 23 |
|
| 24 |
#include "vm/GeckoProfiler-inl.h" |
| 25 |
#include "vm/JSScript-inl.h" |
| 26 |
|
| 27 |
using mozilla::Maybe; |
| 28 |
|
| 29 |
namespace js { |
| 30 |
namespace jit { |
| 31 |
|
| 32 |
static inline JitcodeRegionEntry RegionAtAddr(const IonEntry& entry, void* ptr, |
| 33 |
uint32_t* ptrOffset) { |
| 34 |
MOZ_ASSERT(entry.containsPointer(ptr)); |
| 35 |
*ptrOffset = reinterpret_cast<uint8_t*>(ptr) - |
| 36 |
reinterpret_cast<uint8_t*>(entry.nativeStartAddr()); |
| 37 |
|
| 38 |
uint32_t regionIdx = entry.regionTable()->findRegionEntry(*ptrOffset); |
| 39 |
MOZ_ASSERT(regionIdx < entry.regionTable()->numRegions()); |
| 40 |
|
| 41 |
return entry.regionTable()->regionEntry(regionIdx); |
| 42 |
} |
| 43 |
|
| 44 |
void* IonEntry::canonicalNativeAddrFor(void* ptr) const { |
| 45 |
uint32_t ptrOffset; |
| 46 |
JitcodeRegionEntry region = RegionAtAddr(*this, ptr, &ptrOffset); |
| 47 |
return (void*)(((uint8_t*)nativeStartAddr()) + region.nativeOffset()); |
| 48 |
} |
| 49 |
|
| 50 |
bool IonEntry::callStackAtAddr(void* ptr, BytecodeLocationVector& results, |
| 51 |
uint32_t* depth) const { |
| 52 |
uint32_t ptrOffset; |
| 53 |
JitcodeRegionEntry region = RegionAtAddr(*this, ptr, &ptrOffset); |
| 54 |
*depth = region.scriptDepth(); |
| 55 |
|
| 56 |
JitcodeRegionEntry::ScriptPcIterator locationIter = region.scriptPcIterator(); |
| 57 |
MOZ_ASSERT(locationIter.hasMore()); |
| 58 |
bool first = true; |
| 59 |
while (locationIter.hasMore()) { |
| 60 |
uint32_t scriptIdx, pcOffset; |
| 61 |
locationIter.readNext(&scriptIdx, &pcOffset); |
| 62 |
// For the first entry pushed (innermost frame), the pcOffset is obtained |
| 63 |
// from the delta-run encodings. |
| 64 |
if (first) { |
| 65 |
pcOffset = region.findPcOffset(ptrOffset, pcOffset); |
| 66 |
first = false; |
| 67 |
} |
| 68 |
JSScript* script = getScript(scriptIdx); |
| 69 |
jsbytecode* pc = script->offsetToPC(pcOffset); |
| 70 |
if (!results.append(BytecodeLocation(script, pc))) { |
| 71 |
return false; |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
return true; |
| 76 |
} |
| 77 |
|
| 78 |
uint32_t IonEntry::callStackAtAddr(void* ptr, const char** results, |
| 79 |
uint32_t maxResults) const { |
| 80 |
MOZ_ASSERT(maxResults >= 1); |
| 81 |
|
| 82 |
uint32_t ptrOffset; |
| 83 |
JitcodeRegionEntry region = RegionAtAddr(*this, ptr, &ptrOffset); |
| 84 |
|
| 85 |
JitcodeRegionEntry::ScriptPcIterator locationIter = region.scriptPcIterator(); |
| 86 |
MOZ_ASSERT(locationIter.hasMore()); |
| 87 |
uint32_t count = 0; |
| 88 |
while (locationIter.hasMore()) { |
| 89 |
uint32_t scriptIdx, pcOffset; |
| 90 |
|
| 91 |
locationIter.readNext(&scriptIdx, &pcOffset); |
| 92 |
MOZ_ASSERT(getStr(scriptIdx)); |
| 93 |
|
| 94 |
results[count++] = getStr(scriptIdx); |
| 95 |
if (count >= maxResults) { |
| 96 |
break; |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
return count; |
| 101 |
} |
| 102 |
|
| 103 |
uint64_t IonEntry::lookupRealmID(void* ptr) const { |
| 104 |
uint32_t ptrOffset; |
| 105 |
JitcodeRegionEntry region = RegionAtAddr(*this, ptr, &ptrOffset); |
| 106 |
JitcodeRegionEntry::ScriptPcIterator locationIter = region.scriptPcIterator(); |
| 107 |
MOZ_ASSERT(locationIter.hasMore()); |
| 108 |
uint32_t scriptIdx, pcOffset; |
| 109 |
locationIter.readNext(&scriptIdx, &pcOffset); |
| 110 |
|
| 111 |
JSScript* script = getScript(scriptIdx); |
| 112 |
return script->realm()->creationOptions().profilerRealmID(); |
| 113 |
} |
| 114 |
|
| 115 |
IonEntry::~IonEntry() { |
| 116 |
// The region table is stored at the tail of the compacted data, |
| 117 |
// which means the start of the region table is a pointer to |
| 118 |
// the _middle_ of the memory space allocated for it. |
| 119 |
// |
| 120 |
// When freeing it, obtain the payload start pointer first. |
| 121 |
MOZ_ASSERT(regionTable_); |
| 122 |
js_free((void*)(regionTable_->payloadStart())); |
| 123 |
regionTable_ = nullptr; |
| 124 |
} |
| 125 |
|
| 126 |
static IonEntry& IonEntryForIonIC(JSRuntime* rt, const IonICEntry* icEntry) { |
| 127 |
// The table must have an IonEntry for the IC's rejoin address. |
| 128 |
auto* table = rt->jitRuntime()->getJitcodeGlobalTable(); |
| 129 |
auto* entry = table->lookup(icEntry->rejoinAddr()); |
| 130 |
MOZ_ASSERT(entry); |
| 131 |
MOZ_RELEASE_ASSERT(entry->isIon()); |
| 132 |
return entry->asIon(); |
| 133 |
} |
| 134 |
|
| 135 |
void* IonICEntry::canonicalNativeAddrFor(void* ptr) const { return ptr; } |
| 136 |
|
| 137 |
bool IonICEntry::callStackAtAddr(JSRuntime* rt, void* ptr, |
| 138 |
BytecodeLocationVector& results, |
| 139 |
uint32_t* depth) const { |
| 140 |
const IonEntry& entry = IonEntryForIonIC(rt, this); |
| 141 |
return entry.callStackAtAddr(rejoinAddr(), results, depth); |
| 142 |
} |
| 143 |
|
| 144 |
uint32_t IonICEntry::callStackAtAddr(JSRuntime* rt, void* ptr, |
| 145 |
const char** results, |
| 146 |
uint32_t maxResults) const { |
| 147 |
const IonEntry& entry = IonEntryForIonIC(rt, this); |
| 148 |
return entry.callStackAtAddr(rejoinAddr(), results, maxResults); |
| 149 |
} |
| 150 |
|
| 151 |
uint64_t IonICEntry::lookupRealmID(JSRuntime* rt, void* ptr) const { |
| 152 |
const IonEntry& entry = IonEntryForIonIC(rt, this); |
| 153 |
return entry.lookupRealmID(rejoinAddr()); |
| 154 |
} |
| 155 |
|
| 156 |
void* BaselineEntry::canonicalNativeAddrFor(void* ptr) const { |
| 157 |
// TODO: We can't yet normalize Baseline addresses until we unify |
| 158 |
// BaselineScript's PCMappingEntries with JitcodeGlobalTable. |
| 159 |
return ptr; |
| 160 |
} |
| 161 |
|
| 162 |
bool BaselineEntry::callStackAtAddr(void* ptr, BytecodeLocationVector& results, |
| 163 |
uint32_t* depth) const { |
| 164 |
MOZ_ASSERT(containsPointer(ptr)); |
| 165 |
MOZ_ASSERT(script_->hasBaselineScript()); |
| 166 |
|
| 167 |
uint8_t* addr = reinterpret_cast<uint8_t*>(ptr); |
| 168 |
jsbytecode* pc = |
| 169 |
script_->baselineScript()->approximatePcForNativeAddress(script_, addr); |
| 170 |
if (!results.append(BytecodeLocation(script_, pc))) { |
| 171 |
return false; |
| 172 |
} |
| 173 |
|
| 174 |
*depth = 1; |
| 175 |
|
| 176 |
return true; |
| 177 |
} |
| 178 |
|
| 179 |
uint32_t BaselineEntry::callStackAtAddr(void* ptr, const char** results, |
| 180 |
uint32_t maxResults) const { |
| 181 |
MOZ_ASSERT(containsPointer(ptr)); |
| 182 |
MOZ_ASSERT(maxResults >= 1); |
| 183 |
|
| 184 |
results[0] = str(); |
| 185 |
return 1; |
| 186 |
} |
| 187 |
|
| 188 |
uint64_t BaselineEntry::lookupRealmID() const { |
| 189 |
return script_->realm()->creationOptions().profilerRealmID(); |
| 190 |
} |
| 191 |
|
| 192 |
void* BaselineInterpreterEntry::canonicalNativeAddrFor(void* ptr) const { |
| 193 |
return ptr; |
| 194 |
} |
| 195 |
|
| 196 |
bool BaselineInterpreterEntry::callStackAtAddr(void* ptr, |
| 197 |
BytecodeLocationVector& results, |
| 198 |
uint32_t* depth) const { |
| 199 |
MOZ_CRASH("shouldn't be called for BaselineInterpreter entries"); |
| 200 |
} |
| 201 |
|
| 202 |
uint32_t BaselineInterpreterEntry::callStackAtAddr(void* ptr, |
| 203 |
const char** results, |
| 204 |
uint32_t maxResults) const { |
| 205 |
MOZ_CRASH("shouldn't be called for BaselineInterpreter entries"); |
| 206 |
} |
| 207 |
|
| 208 |
uint64_t BaselineInterpreterEntry::lookupRealmID() const { |
| 209 |
MOZ_CRASH("shouldn't be called for BaselineInterpreter entries"); |
| 210 |
} |
| 211 |
|
| 212 |
void* SelfHostedSharedEntry::canonicalNativeAddrFor(void* ptr) const { |
| 213 |
// TODO: We can't yet normalize Baseline addresses until we unify |
| 214 |
// BaselineScript's PCMappingEntries with JitcodeGlobalTable. |
| 215 |
return ptr; |
| 216 |
} |
| 217 |
|
| 218 |
bool SelfHostedSharedEntry::callStackAtAddr(void* ptr, |
| 219 |
BytecodeLocationVector& results, |
| 220 |
uint32_t* depth) const { |
| 221 |
JitSpew(JitSpew_Profiling, |
| 222 |
"Unexpected call - without a script, what can we do here?"); |
| 223 |
return true; |
| 224 |
} |
| 225 |
|
| 226 |
uint32_t SelfHostedSharedEntry::callStackAtAddr(void* ptr, const char** results, |
| 227 |
uint32_t maxResults) const { |
| 228 |
MOZ_ASSERT(containsPointer(ptr)); |
| 229 |
MOZ_ASSERT(maxResults >= 1); |
| 230 |
|
| 231 |
results[0] = str(); |
| 232 |
return 1; |
| 233 |
} |
| 234 |
|
| 235 |
uint64_t SelfHostedSharedEntry::lookupRealmID() const { return 0; } |
| 236 |
|
| 237 |
const JitcodeGlobalEntry* JitcodeGlobalTable::lookupForSampler( |
| 238 |
void* ptr, JSRuntime* rt, uint64_t samplePosInBuffer) { |
| 239 |
JitcodeGlobalEntry* entry = lookupInternal(ptr); |
| 240 |
if (!entry) { |
| 241 |
return nullptr; |
| 242 |
} |
| 243 |
|
| 244 |
entry->setSamplePositionInBuffer(samplePosInBuffer); |
| 245 |
|
| 246 |
// IonIC entries must keep their corresponding Ion entries alive. |
| 247 |
if (entry->isIonIC()) { |
| 248 |
IonEntry& ionEntry = IonEntryForIonIC(rt, &entry->asIonIC()); |
| 249 |
ionEntry.setSamplePositionInBuffer(samplePosInBuffer); |
| 250 |
} |
| 251 |
|
| 252 |
// JitcodeGlobalEntries are marked at the end of the mark phase. A read |
| 253 |
// barrier is not needed. Any JS frames sampled during the sweep phase of |
| 254 |
// the GC must be on stack, and on-stack frames must already be marked at |
| 255 |
// the beginning of the sweep phase. It's not possible to assert this here |
| 256 |
// as we may be off main thread when called from the gecko profiler. |
| 257 |
|
| 258 |
return entry; |
| 259 |
} |
| 260 |
|
| 261 |
JitcodeGlobalEntry* JitcodeGlobalTable::lookupInternal(void* ptr) { |
| 262 |
// Search for an entry containing the one-byte range starting at |ptr|. |
| 263 |
JitCodeRange range(ptr, static_cast<uint8_t*>(ptr) + 1); |
| 264 |
|
| 265 |
if (JitCodeRange** entry = tree_.maybeLookup(&range)) { |
| 266 |
MOZ_ASSERT((*entry)->containsPointer(ptr)); |
| 267 |
return static_cast<JitcodeGlobalEntry*>(*entry); |
| 268 |
} |
| 269 |
|
| 270 |
return nullptr; |
| 271 |
} |
| 272 |
|
| 273 |
bool JitcodeGlobalTable::addEntry(UniqueJitcodeGlobalEntry entry) { |
| 274 |
MOZ_ASSERT(entry->isIon() || entry->isIonIC() || entry->isBaseline() || |
| 275 |
entry->isBaselineInterpreter() || entry->isDummy() || |
| 276 |
entry->isSelfHostedShared()); |
| 277 |
|
| 278 |
// Assert the new entry does not have a code range that's equal to (or |
| 279 |
// contained in) one of the existing entries, because that would confuse the |
| 280 |
// AVL tree. |
| 281 |
MOZ_ASSERT(!tree_.maybeLookup(entry.get())); |
| 282 |
|
| 283 |
// Suppress profiler sampling while data structures are being mutated. |
| 284 |
AutoSuppressProfilerSampling suppressSampling(TlsContext.get()); |
| 285 |
|
| 286 |
if (!entries_.append(std::move(entry))) { |
| 287 |
return false; |
| 288 |
} |
| 289 |
if (!tree_.insert(entries_.back().get())) { |
| 290 |
entries_.popBack(); |
| 291 |
return false; |
| 292 |
} |
| 293 |
|
| 294 |
return true; |
| 295 |
} |
| 296 |
|
| 297 |
void JitcodeGlobalTable::setAllEntriesAsExpired() { |
| 298 |
AutoSuppressProfilerSampling suppressSampling(TlsContext.get()); |
| 299 |
for (EntryVector::Range r(entries_.all()); !r.empty(); r.popFront()) { |
| 300 |
auto& entry = r.front(); |
| 301 |
entry->setAsExpired(); |
| 302 |
} |
| 303 |
} |
| 304 |
|
| 305 |
bool JitcodeGlobalTable::markIteratively(GCMarker* marker) { |
| 306 |
// JitcodeGlobalTable must keep entries that are in the sampler buffer |
| 307 |
// alive. This conditionality is akin to holding the entries weakly. |
| 308 |
// |
| 309 |
// If this table were marked at the beginning of the mark phase, then |
| 310 |
// sampling would require a read barrier for sampling in between |
| 311 |
// incremental GC slices. However, invoking read barriers from the sampler |
| 312 |
// is wildly unsafe. The sampler may run at any time, including during GC |
| 313 |
// itself. |
| 314 |
// |
| 315 |
// Instead, JitcodeGlobalTable is marked at the beginning of the sweep |
| 316 |
// phase, along with weak references. The key assumption is the |
| 317 |
// following. At the beginning of the sweep phase, any JS frames that the |
| 318 |
// sampler may put in its buffer that are not already there at the |
| 319 |
// beginning of the mark phase must have already been marked, as either 1) |
| 320 |
// the frame was on-stack at the beginning of the sweep phase, or 2) the |
| 321 |
// frame was pushed between incremental sweep slices. Frames of case 1) |
| 322 |
// are already marked. Frames of case 2) must have been reachable to have |
| 323 |
// been newly pushed, and thus are already marked. |
| 324 |
// |
| 325 |
// The approach above obviates the need for read barriers. The assumption |
| 326 |
// above is checked in JitcodeGlobalTable::lookupForSampler. |
| 327 |
|
| 328 |
MOZ_ASSERT(!JS::RuntimeHeapIsMinorCollecting()); |
| 329 |
|
| 330 |
AutoSuppressProfilerSampling suppressSampling(TlsContext.get()); |
| 331 |
|
| 332 |
// If the profiler is off, rangeStart will be Nothing() and all entries are |
| 333 |
// considered to be expired. |
| 334 |
Maybe<uint64_t> rangeStart = |
| 335 |
marker->runtime()->profilerSampleBufferRangeStart(); |
| 336 |
|
| 337 |
bool markedAny = false; |
| 338 |
for (EntryVector::Range r(entries_.all()); !r.empty(); r.popFront()) { |
| 339 |
auto& entry = r.front(); |
| 340 |
|
| 341 |
// If an entry is not sampled, reset its buffer position to the invalid |
| 342 |
// position, and conditionally mark the rest of the entry if its |
| 343 |
// JitCode is not already marked. This conditional marking ensures |
| 344 |
// that so long as the JitCode *may* be sampled, we keep any |
| 345 |
// information that may be handed out to the sampler, like tracked |
| 346 |
// types used by optimizations and scripts used for pc to line number |
| 347 |
// mapping, alive as well. |
| 348 |
if (!rangeStart || !entry->isSampled(*rangeStart)) { |
| 349 |
entry->setAsExpired(); |
| 350 |
if (!entry->isJitcodeMarkedFromAnyThread(marker->runtime())) { |
| 351 |
continue; |
| 352 |
} |
| 353 |
} |
| 354 |
|
| 355 |
// The table is runtime-wide. Not all zones may be participating in |
| 356 |
// the GC. |
| 357 |
if (!entry->zone()->isCollecting() || entry->zone()->isGCFinished()) { |
| 358 |
continue; |
| 359 |
} |
| 360 |
|
| 361 |
markedAny |= entry->trace(marker->tracer()); |
| 362 |
} |
| 363 |
|
| 364 |
return markedAny; |
| 365 |
} |
| 366 |
|
| 367 |
void JitcodeGlobalTable::traceWeak(JSRuntime* rt, JSTracer* trc) { |
| 368 |
AutoSuppressProfilerSampling suppressSampling(rt->mainContextFromOwnThread()); |
| 369 |
|
| 370 |
entries_.eraseIf([&](auto& entry) { |
| 371 |
if (!entry->zone()->isCollecting() || entry->zone()->isGCFinished()) { |
| 372 |
return false; |
| 373 |
} |
| 374 |
|
| 375 |
if (TraceManuallyBarrieredWeakEdge( |
| 376 |
trc, entry->jitcodePtr(), |
| 377 |
"JitcodeGlobalTable::JitcodeGlobalEntry::jitcode_")) { |
| 378 |
entry->traceWeak(trc); |
| 379 |
return false; |
| 380 |
} |
| 381 |
|
| 382 |
// We have to remove the entry. |
| 383 |
#ifdef DEBUG |
| 384 |
Maybe<uint64_t> rangeStart = rt->profilerSampleBufferRangeStart(); |
| 385 |
MOZ_ASSERT_IF(rangeStart, !entry->isSampled(*rangeStart)); |
| 386 |
#endif |
| 387 |
tree_.remove(entry.get()); |
| 388 |
return true; |
| 389 |
}); |
| 390 |
|
| 391 |
MOZ_ASSERT(tree_.empty() == entries_.empty()); |
| 392 |
} |
| 393 |
|
| 394 |
bool JitcodeGlobalEntry::traceJitcode(JSTracer* trc) { |
| 395 |
if (!IsMarkedUnbarriered(trc->runtime(), jitcode_)) { |
| 396 |
TraceManuallyBarrieredEdge(trc, &jitcode_, |
| 397 |
"jitcodglobaltable-baseentry-jitcode"); |
| 398 |
return true; |
| 399 |
} |
| 400 |
return false; |
| 401 |
} |
| 402 |
|
| 403 |
bool JitcodeGlobalEntry::isJitcodeMarkedFromAnyThread(JSRuntime* rt) { |
| 404 |
return IsMarkedUnbarriered(rt, jitcode_); |
| 405 |
} |
| 406 |
|
| 407 |
bool BaselineEntry::trace(JSTracer* trc) { |
| 408 |
if (!IsMarkedUnbarriered(trc->runtime(), script_)) { |
| 409 |
TraceManuallyBarrieredEdge(trc, &script_, |
| 410 |
"jitcodeglobaltable-baselineentry-script"); |
| 411 |
return true; |
| 412 |
} |
| 413 |
return false; |
| 414 |
} |
| 415 |
|
| 416 |
void BaselineEntry::traceWeak(JSTracer* trc) { |
| 417 |
MOZ_ALWAYS_TRUE( |
| 418 |
TraceManuallyBarrieredWeakEdge(trc, &script_, "BaselineEntry::script_")); |
| 419 |
} |
| 420 |
|
| 421 |
bool IonEntry::trace(JSTracer* trc) { |
| 422 |
bool tracedAny = false; |
| 423 |
|
| 424 |
JSRuntime* rt = trc->runtime(); |
| 425 |
for (auto& pair : scriptList_) { |
| 426 |
if (!IsMarkedUnbarriered(rt, pair.script)) { |
| 427 |
TraceManuallyBarrieredEdge(trc, &pair.script, |
| 428 |
"jitcodeglobaltable-ionentry-script"); |
| 429 |
tracedAny = true; |
| 430 |
} |
| 431 |
} |
| 432 |
|
| 433 |
return tracedAny; |
| 434 |
} |
| 435 |
|
| 436 |
void IonEntry::traceWeak(JSTracer* trc) { |
| 437 |
for (auto& pair : scriptList_) { |
| 438 |
JSScript** scriptp = &pair.script; |
| 439 |
MOZ_ALWAYS_TRUE( |
| 440 |
TraceManuallyBarrieredWeakEdge(trc, scriptp, "IonEntry script")); |
| 441 |
} |
| 442 |
} |
| 443 |
|
| 444 |
bool IonICEntry::trace(JSTracer* trc) { |
| 445 |
IonEntry& entry = IonEntryForIonIC(trc->runtime(), this); |
| 446 |
return entry.trace(trc); |
| 447 |
} |
| 448 |
|
| 449 |
void IonICEntry::traceWeak(JSTracer* trc) { |
| 450 |
IonEntry& entry = IonEntryForIonIC(trc->runtime(), this); |
| 451 |
entry.traceWeak(trc); |
| 452 |
} |
| 453 |
|
| 454 |
[[nodiscard]] bool JitcodeGlobalEntry::callStackAtAddr( |
| 455 |
JSRuntime* rt, void* ptr, BytecodeLocationVector& results, |
| 456 |
uint32_t* depth) const { |
| 457 |
switch (kind()) { |
| 458 |
case Kind::Ion: |
| 459 |
return asIon().callStackAtAddr(ptr, results, depth); |
| 460 |
case Kind::IonIC: |
| 461 |
return asIonIC().callStackAtAddr(rt, ptr, results, depth); |
| 462 |
case Kind::Baseline: |
| 463 |
return asBaseline().callStackAtAddr(ptr, results, depth); |
| 464 |
case Kind::BaselineInterpreter: |
| 465 |
return asBaselineInterpreter().callStackAtAddr(ptr, results, depth); |
| 466 |
case Kind::Dummy: |
| 467 |
return asDummy().callStackAtAddr(rt, ptr, results, depth); |
| 468 |
case Kind::SelfHostedShared: |
| 469 |
return asSelfHostedShared().callStackAtAddr(ptr, results, depth); |
| 470 |
} |
| 471 |
MOZ_CRASH("Invalid kind"); |
| 472 |
} |
| 473 |
|
| 474 |
uint32_t JitcodeGlobalEntry::callStackAtAddr(JSRuntime* rt, void* ptr, |
| 475 |
const char** results, |
| 476 |
uint32_t maxResults) const { |
| 477 |
switch (kind()) { |
| 478 |
case Kind::Ion: |
| 479 |
return asIon().callStackAtAddr(ptr, results, maxResults); |
| 480 |
case Kind::IonIC: |
| 481 |
return asIonIC().callStackAtAddr(rt, ptr, results, maxResults); |
| 482 |
case Kind::Baseline: |
| 483 |
return asBaseline().callStackAtAddr(ptr, results, maxResults); |
| 484 |
case Kind::BaselineInterpreter: |
| 485 |
return asBaselineInterpreter().callStackAtAddr(ptr, results, maxResults); |
| 486 |
case Kind::Dummy: |
| 487 |
return asDummy().callStackAtAddr(rt, ptr, results, maxResults); |
| 488 |
case Kind::SelfHostedShared: |
| 489 |
return asSelfHostedShared().callStackAtAddr(ptr, results, maxResults); |
| 490 |
} |
| 491 |
MOZ_CRASH("Invalid kind"); |
| 492 |
} |
| 493 |
|
| 494 |
uint64_t JitcodeGlobalEntry::lookupRealmID(JSRuntime* rt, void* ptr) const { |
| 495 |
switch (kind()) { |
| 496 |
case Kind::Ion: |
| 497 |
return asIon().lookupRealmID(ptr); |
| 498 |
case Kind::IonIC: |
| 499 |
return asIonIC().lookupRealmID(rt, ptr); |
| 500 |
case Kind::Baseline: |
| 501 |
return asBaseline().lookupRealmID(); |
| 502 |
case Kind::Dummy: |
| 503 |
return asDummy().lookupRealmID(); |
| 504 |
case Kind::SelfHostedShared: |
| 505 |
return asSelfHostedShared().lookupRealmID(); |
| 506 |
case Kind::BaselineInterpreter: |
| 507 |
break; |
| 508 |
} |
| 509 |
MOZ_CRASH("Invalid kind"); |
| 510 |
} |
| 511 |
|
| 512 |
bool JitcodeGlobalEntry::trace(JSTracer* trc) { |
| 513 |
bool tracedAny = traceJitcode(trc); |
| 514 |
switch (kind()) { |
| 515 |
case Kind::Ion: |
| 516 |
tracedAny |= asIon().trace(trc); |
| 517 |
break; |
| 518 |
case Kind::IonIC: |
| 519 |
tracedAny |= asIonIC().trace(trc); |
| 520 |
break; |
| 521 |
case Kind::Baseline: |
| 522 |
tracedAny |= asBaseline().trace(trc); |
| 523 |
break; |
| 524 |
case Kind::BaselineInterpreter: |
| 525 |
case Kind::Dummy: |
| 526 |
case Kind::SelfHostedShared: |
| 527 |
break; |
| 528 |
} |
| 529 |
return tracedAny; |
| 530 |
} |
| 531 |
|
| 532 |
void JitcodeGlobalEntry::traceWeak(JSTracer* trc) { |
| 533 |
switch (kind()) { |
| 534 |
case Kind::Ion: |
| 535 |
asIon().traceWeak(trc); |
| 536 |
break; |
| 537 |
case Kind::IonIC: |
| 538 |
asIonIC().traceWeak(trc); |
| 539 |
break; |
| 540 |
case Kind::Baseline: |
| 541 |
asBaseline().traceWeak(trc); |
| 542 |
break; |
| 543 |
case Kind::BaselineInterpreter: |
| 544 |
case Kind::Dummy: |
| 545 |
case Kind::SelfHostedShared: |
| 546 |
break; |
| 547 |
} |
| 548 |
} |
| 549 |
|
| 550 |
void* JitcodeGlobalEntry::canonicalNativeAddrFor(JSRuntime* rt, |
| 551 |
void* ptr) const { |
| 552 |
switch (kind()) { |
| 553 |
case Kind::Ion: |
| 554 |
return asIon().canonicalNativeAddrFor(ptr); |
| 555 |
case Kind::IonIC: |
| 556 |
return asIonIC().canonicalNativeAddrFor(ptr); |
| 557 |
case Kind::Baseline: |
| 558 |
return asBaseline().canonicalNativeAddrFor(ptr); |
| 559 |
case Kind::Dummy: |
| 560 |
return asDummy().canonicalNativeAddrFor(rt, ptr); |
| 561 |
case Kind::SelfHostedShared: |
| 562 |
return asSelfHostedShared().canonicalNativeAddrFor(ptr); |
| 563 |
case Kind::BaselineInterpreter: |
| 564 |
break; |
| 565 |
} |
| 566 |
MOZ_CRASH("Invalid kind"); |
| 567 |
} |
| 568 |
|
| 569 |
// static |
| 570 |
void JitcodeGlobalEntry::DestroyPolicy::operator()(JitcodeGlobalEntry* entry) { |
| 571 |
switch (entry->kind()) { |
| 572 |
case JitcodeGlobalEntry::Kind::Ion: |
| 573 |
js_delete(&entry->asIon()); |
| 574 |
break; |
| 575 |
case JitcodeGlobalEntry::Kind::IonIC: |
| 576 |
js_delete(&entry->asIonIC()); |
| 577 |
break; |
| 578 |
case JitcodeGlobalEntry::Kind::Baseline: |
| 579 |
js_delete(&entry->asBaseline()); |
| 580 |
break; |
| 581 |
case JitcodeGlobalEntry::Kind::BaselineInterpreter: |
| 582 |
js_delete(&entry->asBaselineInterpreter()); |
| 583 |
break; |
| 584 |
case JitcodeGlobalEntry::Kind::Dummy: |
| 585 |
js_delete(&entry->asDummy()); |
| 586 |
break; |
| 587 |
case JitcodeGlobalEntry::Kind::SelfHostedShared: |
| 588 |
js_delete(&entry->asSelfHostedShared()); |
| 589 |
break; |
| 590 |
} |
| 591 |
} |
| 592 |
|
| 593 |
/* static */ |
| 594 |
void JitcodeRegionEntry::WriteHead(CompactBufferWriter& writer, |
| 595 |
uint32_t nativeOffset, uint8_t scriptDepth) { |
| 596 |
writer.writeUnsigned(nativeOffset); |
| 597 |
writer.writeByte(scriptDepth); |
| 598 |
} |
| 599 |
|
| 600 |
/* static */ |
| 601 |
void JitcodeRegionEntry::ReadHead(CompactBufferReader& reader, |
| 602 |
uint32_t* nativeOffset, |
| 603 |
uint8_t* scriptDepth) { |
| 604 |
*nativeOffset = reader.readUnsigned(); |
| 605 |
*scriptDepth = reader.readByte(); |
| 606 |
} |
| 607 |
|
| 608 |
/* static */ |
| 609 |
void JitcodeRegionEntry::WriteScriptPc(CompactBufferWriter& writer, |
| 610 |
uint32_t scriptIdx, uint32_t pcOffset) { |
| 611 |
writer.writeUnsigned(scriptIdx); |
| 612 |
writer.writeUnsigned(pcOffset); |
| 613 |
} |
| 614 |
|
| 615 |
/* static */ |
| 616 |
void JitcodeRegionEntry::ReadScriptPc(CompactBufferReader& reader, |
| 617 |
uint32_t* scriptIdx, uint32_t* pcOffset) { |
| 618 |
*scriptIdx = reader.readUnsigned(); |
| 619 |
*pcOffset = reader.readUnsigned(); |
| 620 |
} |
| 621 |
|
| 622 |
/* static */ |
| 623 |
void JitcodeRegionEntry::WriteDelta(CompactBufferWriter& writer, |
| 624 |
uint32_t nativeDelta, int32_t pcDelta) { |
| 625 |
if (pcDelta >= 0) { |
| 626 |
// 1 and 2-byte formats possible. |
| 627 |
|
| 628 |
// NNNN-BBB0 |
| 629 |
if (pcDelta <= ENC1_PC_DELTA_MAX && nativeDelta <= ENC1_NATIVE_DELTA_MAX) { |
| 630 |
uint8_t encVal = ENC1_MASK_VAL | (pcDelta << ENC1_PC_DELTA_SHIFT) | |
| 631 |
(nativeDelta << ENC1_NATIVE_DELTA_SHIFT); |
| 632 |
writer.writeByte(encVal); |
| 633 |
return; |
| 634 |
} |
| 635 |
|
| 636 |
// NNNN-NNNN BBBB-BB01 |
| 637 |
if (pcDelta <= ENC2_PC_DELTA_MAX && nativeDelta <= ENC2_NATIVE_DELTA_MAX) { |
| 638 |
uint16_t encVal = ENC2_MASK_VAL | (pcDelta << ENC2_PC_DELTA_SHIFT) | |
| 639 |
(nativeDelta << ENC2_NATIVE_DELTA_SHIFT); |
| 640 |
writer.writeByte(encVal & 0xff); |
| 641 |
writer.writeByte((encVal >> 8) & 0xff); |
| 642 |
return; |
| 643 |
} |
| 644 |
} |
| 645 |
|
| 646 |
// NNNN-NNNN NNNB-BBBB BBBB-B011 |
| 647 |
if (pcDelta >= ENC3_PC_DELTA_MIN && pcDelta <= ENC3_PC_DELTA_MAX && |
| 648 |
nativeDelta <= ENC3_NATIVE_DELTA_MAX) { |
| 649 |
uint32_t encVal = |
| 650 |
ENC3_MASK_VAL | |
| 651 |
((uint32_t(pcDelta) << ENC3_PC_DELTA_SHIFT) & ENC3_PC_DELTA_MASK) | |
| 652 |
(nativeDelta << ENC3_NATIVE_DELTA_SHIFT); |
| 653 |
writer.writeByte(encVal & 0xff); |
| 654 |
writer.writeByte((encVal >> 8) & 0xff); |
| 655 |
writer.writeByte((encVal >> 16) & 0xff); |
| 656 |
return; |
| 657 |
} |
| 658 |
|
| 659 |
// NNNN-NNNN NNNN-NNNN BBBB-BBBB BBBB-B111 |
| 660 |
if (pcDelta >= ENC4_PC_DELTA_MIN && pcDelta <= ENC4_PC_DELTA_MAX && |
| 661 |
nativeDelta <= ENC4_NATIVE_DELTA_MAX) { |
| 662 |
uint32_t encVal = |
| 663 |
ENC4_MASK_VAL | |
| 664 |
((uint32_t(pcDelta) << ENC4_PC_DELTA_SHIFT) & ENC4_PC_DELTA_MASK) | |
| 665 |
(nativeDelta << ENC4_NATIVE_DELTA_SHIFT); |
| 666 |
writer.writeByte(encVal & 0xff); |
| 667 |
writer.writeByte((encVal >> 8) & 0xff); |
| 668 |
writer.writeByte((encVal >> 16) & 0xff); |
| 669 |
writer.writeByte((encVal >> 24) & 0xff); |
| 670 |
return; |
| 671 |
} |
| 672 |
|
| 673 |
// Should never get here. |
| 674 |
MOZ_CRASH("pcDelta/nativeDelta values are too large to encode."); |
| 675 |
} |
| 676 |
|
| 677 |
/* static */ |
| 678 |
void JitcodeRegionEntry::ReadDelta(CompactBufferReader& reader, |
| 679 |
uint32_t* nativeDelta, int32_t* pcDelta) { |
| 680 |
// NB: |
| 681 |
// It's possible to get nativeDeltas with value 0 in two cases: |
| 682 |
// |
| 683 |
// 1. The last region's run. This is because the region table's start |
| 684 |
// must be 4-byte aligned, and we must insert padding bytes to align the |
| 685 |
// payload section before emitting the table. |
| 686 |
// |
| 687 |
// 2. A zero-offset nativeDelta with a negative pcDelta. |
| 688 |
// |
| 689 |
// So if nativeDelta is zero, then pcDelta must be <= 0. |
| 690 |
|
| 691 |
// NNNN-BBB0 |
| 692 |
const uint32_t firstByte = reader.readByte(); |
| 693 |
if ((firstByte & ENC1_MASK) == ENC1_MASK_VAL) { |
| 694 |
uint32_t encVal = firstByte; |
| 695 |
*nativeDelta = encVal >> ENC1_NATIVE_DELTA_SHIFT; |
| 696 |
*pcDelta = (encVal & ENC1_PC_DELTA_MASK) >> ENC1_PC_DELTA_SHIFT; |
| 697 |
MOZ_ASSERT_IF(*nativeDelta == 0, *pcDelta <= 0); |
| 698 |
return; |
| 699 |
} |
| 700 |
|
| 701 |
// NNNN-NNNN BBBB-BB01 |
| 702 |
const uint32_t secondByte = reader.readByte(); |
| 703 |
if ((firstByte & ENC2_MASK) == ENC2_MASK_VAL) { |
| 704 |
uint32_t encVal = firstByte | secondByte << 8; |
| 705 |
*nativeDelta = encVal >> ENC2_NATIVE_DELTA_SHIFT; |
| 706 |
*pcDelta = (encVal & ENC2_PC_DELTA_MASK) >> ENC2_PC_DELTA_SHIFT; |
| 707 |
MOZ_ASSERT(*pcDelta != 0); |
| 708 |
MOZ_ASSERT_IF(*nativeDelta == 0, *pcDelta <= 0); |
| 709 |
return; |
| 710 |
} |
| 711 |
|
| 712 |
// NNNN-NNNN NNNB-BBBB BBBB-B011 |
| 713 |
const uint32_t thirdByte = reader.readByte(); |
| 714 |
if ((firstByte & ENC3_MASK) == ENC3_MASK_VAL) { |
| 715 |
uint32_t encVal = firstByte | secondByte << 8 | thirdByte << 16; |
| 716 |
*nativeDelta = encVal >> ENC3_NATIVE_DELTA_SHIFT; |
| 717 |
|
| 718 |
uint32_t pcDeltaU = (encVal & ENC3_PC_DELTA_MASK) >> ENC3_PC_DELTA_SHIFT; |
| 719 |
// Fix sign if necessary. |
| 720 |
if (pcDeltaU > static_cast<uint32_t>(ENC3_PC_DELTA_MAX)) { |
| 721 |
pcDeltaU |= ~ENC3_PC_DELTA_MAX; |
| 722 |
} |
| 723 |
*pcDelta = pcDeltaU; |
| 724 |
MOZ_ASSERT(*pcDelta != 0); |
| 725 |
MOZ_ASSERT_IF(*nativeDelta == 0, *pcDelta <= 0); |
| 726 |
return; |
| 727 |
} |
| 728 |
|
| 729 |
// NNNN-NNNN NNNN-NNNN BBBB-BBBB BBBB-B111 |
| 730 |
MOZ_ASSERT((firstByte & ENC4_MASK) == ENC4_MASK_VAL); |
| 731 |
const uint32_t fourthByte = reader.readByte(); |
| 732 |
uint32_t encVal = |
| 733 |
firstByte | secondByte << 8 | thirdByte << 16 | fourthByte << 24; |
| 734 |
*nativeDelta = encVal >> ENC4_NATIVE_DELTA_SHIFT; |
| 735 |
|
| 736 |
uint32_t pcDeltaU = (encVal & ENC4_PC_DELTA_MASK) >> ENC4_PC_DELTA_SHIFT; |
| 737 |
// fix sign if necessary |
| 738 |
if (pcDeltaU > static_cast<uint32_t>(ENC4_PC_DELTA_MAX)) { |
| 739 |
pcDeltaU |= ~ENC4_PC_DELTA_MAX; |
| 740 |
} |
| 741 |
*pcDelta = pcDeltaU; |
| 742 |
|
| 743 |
MOZ_ASSERT(*pcDelta != 0); |
| 744 |
MOZ_ASSERT_IF(*nativeDelta == 0, *pcDelta <= 0); |
| 745 |
} |
| 746 |
|
| 747 |
/* static */ |
| 748 |
uint32_t JitcodeRegionEntry::ExpectedRunLength(const NativeToBytecode* entry, |
| 749 |
const NativeToBytecode* end) { |
| 750 |
MOZ_ASSERT(entry < end); |
| 751 |
|
| 752 |
// We always use the first entry, so runLength starts at 1 |
| 753 |
uint32_t runLength = 1; |
| 754 |
|
| 755 |
uint32_t curNativeOffset = entry->nativeOffset.offset(); |
| 756 |
uint32_t curBytecodeOffset = entry->tree->script()->pcToOffset(entry->pc); |
| 757 |
|
| 758 |
for (auto nextEntry = entry + 1; nextEntry != end; nextEntry += 1) { |
| 759 |
// If the next run moves to a different inline site, stop the run. |
| 760 |
if (nextEntry->tree != entry->tree) { |
| 761 |
break; |
| 762 |
} |
| 763 |
|
| 764 |
uint32_t nextNativeOffset = nextEntry->nativeOffset.offset(); |
| 765 |
uint32_t nextBytecodeOffset = |
| 766 |
nextEntry->tree->script()->pcToOffset(nextEntry->pc); |
| 767 |
MOZ_ASSERT(nextNativeOffset >= curNativeOffset); |
| 768 |
|
| 769 |
uint32_t nativeDelta = nextNativeOffset - curNativeOffset; |
| 770 |
int32_t bytecodeDelta = |
| 771 |
int32_t(nextBytecodeOffset) - int32_t(curBytecodeOffset); |
| 772 |
|
| 773 |
// If deltas are too large (very unlikely), stop the run. |
| 774 |
if (!IsDeltaEncodeable(nativeDelta, bytecodeDelta)) { |
| 775 |
break; |
| 776 |
} |
| 777 |
|
| 778 |
runLength++; |
| 779 |
|
| 780 |
// If the run has grown to its maximum length, stop the run. |
| 781 |
if (runLength == MAX_RUN_LENGTH) { |
| 782 |
break; |
| 783 |
} |
| 784 |
|
| 785 |
curNativeOffset = nextNativeOffset; |
| 786 |
curBytecodeOffset = nextBytecodeOffset; |
| 787 |
} |
| 788 |
|
| 789 |
return runLength; |
| 790 |
} |
| 791 |
|
| 792 |
struct JitcodeMapBufferWriteSpewer { |
| 793 |
#ifdef JS_JITSPEW |
| 794 |
CompactBufferWriter* writer; |
| 795 |
uint32_t startPos; |
| 796 |
|
| 797 |
static const uint32_t DumpMaxBytes = 50; |
| 798 |
|
| 799 |
explicit JitcodeMapBufferWriteSpewer(CompactBufferWriter& w) |
| 800 |
: writer(&w), startPos(writer->length()) {} |
| 801 |
|
| 802 |
void spewAndAdvance(const char* name) { |
| 803 |
if (writer->oom()) { |
| 804 |
return; |
| 805 |
} |
| 806 |
|
| 807 |
uint32_t curPos = writer->length(); |
| 808 |
const uint8_t* start = writer->buffer() + startPos; |
| 809 |
const uint8_t* end = writer->buffer() + curPos; |
| 810 |
const char* MAP = "0123456789ABCDEF"; |
| 811 |
uint32_t bytes = end - start; |
| 812 |
|
| 813 |
char buffer[DumpMaxBytes * 3]; |
| 814 |
for (uint32_t i = 0; i < bytes; i++) { |
| 815 |
buffer[i * 3] = MAP[(start[i] >> 4) & 0xf]; |
| 816 |
buffer[i * 3 + 1] = MAP[(start[i] >> 0) & 0xf]; |
| 817 |
buffer[i * 3 + 2] = ' '; |
| 818 |
} |
| 819 |
if (bytes >= DumpMaxBytes) { |
| 820 |
buffer[DumpMaxBytes * 3 - 1] = '\0'; |
| 821 |
} else { |
| 822 |
buffer[bytes * 3 - 1] = '\0'; |
| 823 |
} |
| 824 |
|
| 825 |
JitSpew(JitSpew_Profiling, "%s@%d[%d bytes] - %s", name, int(startPos), |
| 826 |
int(bytes), buffer); |
| 827 |
|
| 828 |
// Move to the end of the current buffer. |
| 829 |
startPos = writer->length(); |
| 830 |
} |
| 831 |
#else // !JS_JITSPEW |
| 832 |
explicit JitcodeMapBufferWriteSpewer(CompactBufferWriter& w) {} |
| 833 |
void spewAndAdvance(const char* name) {} |
| 834 |
#endif // JS_JITSPEW |
| 835 |
}; |
| 836 |
|
| 837 |
// Write a run, starting at the given NativeToBytecode entry, into the given |
| 838 |
// buffer writer. |
| 839 |
/* static */ |
| 840 |
bool JitcodeRegionEntry::WriteRun(CompactBufferWriter& writer, |
| 841 |
const IonEntry::ScriptList& scriptList, |
| 842 |
uint32_t runLength, |
| 843 |
const NativeToBytecode* entry) { |
| 844 |
MOZ_ASSERT(runLength > 0); |
| 845 |
MOZ_ASSERT(runLength <= MAX_RUN_LENGTH); |
| 846 |
|
| 847 |
// Calculate script depth. |
| 848 |
MOZ_ASSERT(entry->tree->depth() <= 0xff); |
| 849 |
uint8_t scriptDepth = entry->tree->depth(); |
| 850 |
uint32_t regionNativeOffset = entry->nativeOffset.offset(); |
| 851 |
|
| 852 |
JitcodeMapBufferWriteSpewer spewer(writer); |
| 853 |
|
| 854 |
// Write the head info. |
| 855 |
JitSpew(JitSpew_Profiling, " Head Info: nativeOffset=%d scriptDepth=%d", |
| 856 |
int(regionNativeOffset), int(scriptDepth)); |
| 857 |
WriteHead(writer, regionNativeOffset, scriptDepth); |
| 858 |
spewer.spewAndAdvance(" "); |
| 859 |
|
| 860 |
// Write each script/pc pair. |
| 861 |
{ |
| 862 |
InlineScriptTree* curTree = entry->tree; |
| 863 |
jsbytecode* curPc = entry->pc; |
| 864 |
for (uint8_t i = 0; i < scriptDepth; i++) { |
| 865 |
// Find the index of the script within the list. |
| 866 |
// NB: scriptList is guaranteed to contain curTree->script() |
| 867 |
uint32_t scriptIdx = 0; |
| 868 |
for (; scriptIdx < scriptList.length(); scriptIdx++) { |
| 869 |
if (scriptList[scriptIdx].script == curTree->script()) { |
| 870 |
break; |
| 871 |
} |
| 872 |
} |
| 873 |
MOZ_ASSERT(scriptIdx < scriptList.length()); |
| 874 |
|
| 875 |
uint32_t pcOffset = curTree->script()->pcToOffset(curPc); |
| 876 |
|
| 877 |
JitSpew(JitSpew_Profiling, " Script/PC %d: scriptIdx=%d pcOffset=%d", |
| 878 |
int(i), int(scriptIdx), int(pcOffset)); |
| 879 |
WriteScriptPc(writer, scriptIdx, pcOffset); |
| 880 |
spewer.spewAndAdvance(" "); |
| 881 |
|
| 882 |
MOZ_ASSERT_IF(i < scriptDepth - 1, curTree->hasCaller()); |
| 883 |
curPc = curTree->callerPc(); |
| 884 |
curTree = curTree->caller(); |
| 885 |
} |
| 886 |
} |
| 887 |
|
| 888 |
// Start writing runs. |
| 889 |
uint32_t curNativeOffset = entry->nativeOffset.offset(); |
| 890 |
uint32_t curBytecodeOffset = entry->tree->script()->pcToOffset(entry->pc); |
| 891 |
|
| 892 |
JitSpew(JitSpew_Profiling, |
| 893 |
" Writing Delta Run from nativeOffset=%d bytecodeOffset=%d", |
| 894 |
int(curNativeOffset), int(curBytecodeOffset)); |
| 895 |
|
| 896 |
// Skip first entry because it is implicit in the header. Start at subsequent |
| 897 |
// entry. |
| 898 |
for (uint32_t i = 1; i < runLength; i++) { |
| 899 |
MOZ_ASSERT(entry[i].tree == entry->tree); |
| 900 |
|
| 901 |
uint32_t nextNativeOffset = entry[i].nativeOffset.offset(); |
| 902 |
uint32_t nextBytecodeOffset = |
| 903 |
entry[i].tree->script()->pcToOffset(entry[i].pc); |
| 904 |
MOZ_ASSERT(nextNativeOffset >= curNativeOffset); |
| 905 |
|
| 906 |
uint32_t nativeDelta = nextNativeOffset - curNativeOffset; |
| 907 |
int32_t bytecodeDelta = |
| 908 |
int32_t(nextBytecodeOffset) - int32_t(curBytecodeOffset); |
| 909 |
MOZ_ASSERT(IsDeltaEncodeable(nativeDelta, bytecodeDelta)); |
| 910 |
|
| 911 |
JitSpew(JitSpew_Profiling, |
| 912 |
" RunEntry native: %d-%d [%d] bytecode: %d-%d [%d]", |
| 913 |
int(curNativeOffset), int(nextNativeOffset), int(nativeDelta), |
| 914 |
int(curBytecodeOffset), int(nextBytecodeOffset), |
| 915 |
int(bytecodeDelta)); |
| 916 |
WriteDelta(writer, nativeDelta, bytecodeDelta); |
| 917 |
|
| 918 |
// Spew the bytecode in these ranges. |
| 919 |
if (curBytecodeOffset < nextBytecodeOffset) { |
| 920 |
JitSpewStart(JitSpew_Profiling, " OPS: "); |
| 921 |
uint32_t curBc = curBytecodeOffset; |
| 922 |
while (curBc < nextBytecodeOffset) { |
| 923 |
jsbytecode* pc = entry[i].tree->script()->offsetToPC(curBc); |
| 924 |
#ifdef JS_JITSPEW |
| 925 |
JSOp op = JSOp(*pc); |
| 926 |
JitSpewCont(JitSpew_Profiling, "%s ", CodeName(op)); |
| 927 |
#endif |
| 928 |
curBc += GetBytecodeLength(pc); |
| 929 |
} |
| 930 |
JitSpewFin(JitSpew_Profiling); |
| 931 |
} |
| 932 |
spewer.spewAndAdvance(" "); |
| 933 |
|
| 934 |
curNativeOffset = nextNativeOffset; |
| 935 |
curBytecodeOffset = nextBytecodeOffset; |
| 936 |
} |
| 937 |
|
| 938 |
if (writer.oom()) { |
| 939 |
return false; |
| 940 |
} |
| 941 |
|
| 942 |
return true; |
| 943 |
} |
| 944 |
|
| 945 |
void JitcodeRegionEntry::unpack() { |
| 946 |
CompactBufferReader reader(data_, end_); |
| 947 |
ReadHead(reader, &nativeOffset_, &scriptDepth_); |
| 948 |
MOZ_ASSERT(scriptDepth_ > 0); |
| 949 |
|
| 950 |
scriptPcStack_ = reader.currentPosition(); |
| 951 |
// Skip past script/pc stack |
| 952 |
for (unsigned i = 0; i < scriptDepth_; i++) { |
| 953 |
uint32_t scriptIdx, pcOffset; |
| 954 |
ReadScriptPc(reader, &scriptIdx, &pcOffset); |
| 955 |
} |
| 956 |
|
| 957 |
deltaRun_ = reader.currentPosition(); |
| 958 |
} |
| 959 |
|
| 960 |
uint32_t JitcodeRegionEntry::findPcOffset(uint32_t queryNativeOffset, |
| 961 |
uint32_t startPcOffset) const { |
| 962 |
DeltaIterator iter = deltaIterator(); |
| 963 |
uint32_t curNativeOffset = nativeOffset(); |
| 964 |
uint32_t curPcOffset = startPcOffset; |
| 965 |
while (iter.hasMore()) { |
| 966 |
uint32_t nativeDelta; |
| 967 |
int32_t pcDelta; |
| 968 |
iter.readNext(&nativeDelta, &pcDelta); |
| 969 |
|
| 970 |
// The start address of the next delta-run entry is counted towards |
| 971 |
// the current delta-run entry, because return addresses should |
| 972 |
// associate with the bytecode op prior (the call) not the op after. |
| 973 |
if (queryNativeOffset <= curNativeOffset + nativeDelta) { |
| 974 |
break; |
| 975 |
} |
| 976 |
curNativeOffset += nativeDelta; |
| 977 |
curPcOffset += pcDelta; |
| 978 |
} |
| 979 |
return curPcOffset; |
| 980 |
} |
| 981 |
|
| 982 |
uint32_t JitcodeIonTable::findRegionEntry(uint32_t nativeOffset) const { |
| 983 |
static const uint32_t LINEAR_SEARCH_THRESHOLD = 8; |
| 984 |
uint32_t regions = numRegions(); |
| 985 |
MOZ_ASSERT(regions > 0); |
| 986 |
|
| 987 |
// For small region lists, just search linearly. |
| 988 |
if (regions <= LINEAR_SEARCH_THRESHOLD) { |
| 989 |
JitcodeRegionEntry previousEntry = regionEntry(0); |
| 990 |
for (uint32_t i = 1; i < regions; i++) { |
| 991 |
JitcodeRegionEntry nextEntry = regionEntry(i); |
| 992 |
MOZ_ASSERT(nextEntry.nativeOffset() >= previousEntry.nativeOffset()); |
| 993 |
|
| 994 |
// See note in binary-search code below about why we use '<=' here |
| 995 |
// instead of '<'. Short explanation: regions are closed at their |
| 996 |
// ending addresses, and open at their starting addresses. |
| 997 |
if (nativeOffset <= nextEntry.nativeOffset()) { |
| 998 |
return i - 1; |
| 999 |
} |
| 1000 |
|
| 1001 |
previousEntry = nextEntry; |
| 1002 |
} |
| 1003 |
// If nothing found, assume it falls within last region. |
| 1004 |
return regions - 1; |
| 1005 |
} |
| 1006 |
|
| 1007 |
// For larger ones, binary search the region table. |
| 1008 |
uint32_t idx = 0; |
| 1009 |
uint32_t count = regions; |
| 1010 |
while (count > 1) { |
| 1011 |
uint32_t step = count / 2; |
| 1012 |
uint32_t mid = idx + step; |
| 1013 |
JitcodeRegionEntry midEntry = regionEntry(mid); |
| 1014 |
|
| 1015 |
// A region memory range is closed at its ending address, not starting |
| 1016 |
// address. This is because the return address for calls must associate |
| 1017 |
// with the call's bytecode PC, not the PC of the bytecode operator after |
| 1018 |
// the call. |
| 1019 |
// |
| 1020 |
// So a query is < an entry if the query nativeOffset is <= the start |
| 1021 |
// address of the entry, and a query is >= an entry if the query |
| 1022 |
// nativeOffset is > the start address of an entry. |
| 1023 |
if (nativeOffset <= midEntry.nativeOffset()) { |
| 1024 |
// Target entry is below midEntry. |
| 1025 |
count = step; |
| 1026 |
} else { // if (nativeOffset > midEntry.nativeOffset()) |
| 1027 |
// Target entry is at midEntry or above. |
| 1028 |
idx = mid; |
| 1029 |
count -= step; |
| 1030 |
} |
| 1031 |
} |
| 1032 |
return idx; |
| 1033 |
} |
| 1034 |
|
| 1035 |
/* static */ |
| 1036 |
bool JitcodeIonTable::WriteIonTable(CompactBufferWriter& writer, |
| 1037 |
const IonEntry::ScriptList& scriptList, |
| 1038 |
const NativeToBytecode* start, |
| 1039 |
const NativeToBytecode* end, |
| 1040 |
uint32_t* tableOffsetOut, |
| 1041 |
uint32_t* numRegionsOut) { |
| 1042 |
MOZ_ASSERT(tableOffsetOut != nullptr); |
| 1043 |
MOZ_ASSERT(numRegionsOut != nullptr); |
| 1044 |
MOZ_ASSERT(writer.length() == 0); |
| 1045 |
MOZ_ASSERT(scriptList.length() > 0); |
| 1046 |
|
| 1047 |
JitSpew(JitSpew_Profiling, |
| 1048 |
"Writing native to bytecode map for %s:%u:%u (%zu entries)", |
| 1049 |
scriptList[0].script->filename(), scriptList[0].script->lineno(), |
| 1050 |
scriptList[0].script->column().oneOriginValue(), |
| 1051 |
mozilla::PointerRangeSize(start, end)); |
| 1052 |
|
| 1053 |
JitSpew(JitSpew_Profiling, " ScriptList of size %u", |
| 1054 |
unsigned(scriptList.length())); |
| 1055 |
for (uint32_t i = 0; i < scriptList.length(); i++) { |
| 1056 |
JitSpew(JitSpew_Profiling, " Script %u - %s:%u:%u", i, |
| 1057 |
scriptList[i].script->filename(), scriptList[i].script->lineno(), |
| 1058 |
scriptList[i].script->column().oneOriginValue()); |
| 1059 |
} |
| 1060 |
|
| 1061 |
// Write out runs first. Keep a vector tracking the positive offsets from |
| 1062 |
// payload start to the run. |
| 1063 |
const NativeToBytecode* curEntry = start; |
| 1064 |
js::Vector<uint32_t, 32, SystemAllocPolicy> runOffsets; |
| 1065 |
|
| 1066 |
while (curEntry != end) { |
| 1067 |
// Calculate the length of the next run. |
| 1068 |
uint32_t runLength = JitcodeRegionEntry::ExpectedRunLength(curEntry, end); |
| 1069 |
MOZ_ASSERT(runLength > 0); |
| 1070 |
MOZ_ASSERT(runLength <= uintptr_t(end - curEntry)); |
| 1071 |
JitSpew(JitSpew_Profiling, " Run at entry %d, length %d, buffer offset %d", |
| 1072 |
int(curEntry - start), int(runLength), int(writer.length())); |
| 1073 |
|
| 1074 |
// Store the offset of the run. |
| 1075 |
if (!runOffsets.append(writer.length())) { |
| 1076 |
return false; |
| 1077 |
} |
| 1078 |
|
| 1079 |
// Encode the run. |
| 1080 |
if (!JitcodeRegionEntry::WriteRun(writer, scriptList, runLength, |
| 1081 |
curEntry)) { |
| 1082 |
return false; |
| 1083 |
} |
| 1084 |
|
| 1085 |
curEntry += runLength; |
| 1086 |
} |
| 1087 |
|
| 1088 |
// Done encoding regions. About to start table. Ensure we are aligned to 4 |
| 1089 |
// bytes since table is composed of uint32_t values. |
| 1090 |
uint32_t padding = sizeof(uint32_t) - (writer.length() % sizeof(uint32_t)); |
| 1091 |
if (padding == sizeof(uint32_t)) { |
| 1092 |
padding = 0; |
| 1093 |
} |
| 1094 |
JitSpew(JitSpew_Profiling, " Padding %d bytes after run @%d", int(padding), |
| 1095 |
int(writer.length())); |
| 1096 |
for (uint32_t i = 0; i < padding; i++) { |
| 1097 |
writer.writeByte(0); |
| 1098 |
} |
| 1099 |
|
| 1100 |
// Now at start of table. |
| 1101 |
uint32_t tableOffset = writer.length(); |
| 1102 |
|
| 1103 |
// The table being written at this point will be accessed directly via |
| 1104 |
// uint32_t pointers, so all writes below use native endianness. |
| 1105 |
|
| 1106 |
// Write out numRegions |
| 1107 |
JitSpew(JitSpew_Profiling, " Writing numRuns=%d", int(runOffsets.length())); |
| 1108 |
writer.writeNativeEndianUint32_t(runOffsets.length()); |
| 1109 |
|
| 1110 |
// Write out region offset table. The offsets in |runOffsets| are currently |
| 1111 |
// forward offsets from the beginning of the buffer. We convert them to |
| 1112 |
// backwards offsets from the start of the table before writing them into |
| 1113 |
// their table entries. |
| 1114 |
for (uint32_t i = 0; i < runOffsets.length(); i++) { |
| 1115 |
JitSpew(JitSpew_Profiling, " Run %d offset=%d backOffset=%d @%d", int(i), |
| 1116 |
int(runOffsets[i]), int(tableOffset - runOffsets[i]), |
| 1117 |
int(writer.length())); |
| 1118 |
writer.writeNativeEndianUint32_t(tableOffset - runOffsets[i]); |
| 1119 |
} |
| 1120 |
|
| 1121 |
if (writer.oom()) { |
| 1122 |
return false; |
| 1123 |
} |
| 1124 |
|
| 1125 |
*tableOffsetOut = tableOffset; |
| 1126 |
*numRegionsOut = runOffsets.length(); |
| 1127 |
return true; |
| 1128 |
} |
| 1129 |
|
| 1130 |
} // namespace jit |
| 1131 |
} // namespace js |
| 1132 |
|
| 1133 |
JS::ProfiledFrameHandle::ProfiledFrameHandle(JSRuntime* rt, |
| 1134 |
js::jit::JitcodeGlobalEntry& entry, |
| 1135 |
void* addr, const char* label, |
| 1136 |
uint32_t depth) |
| 1137 |
: rt_(rt), |
| 1138 |
entry_(entry), |
| 1139 |
addr_(addr), |
| 1140 |
canonicalAddr_(nullptr), |
| 1141 |
label_(label), |
| 1142 |
depth_(depth) { |
| 1143 |
if (!canonicalAddr_) { |
| 1144 |
canonicalAddr_ = entry_.canonicalNativeAddrFor(rt_, addr_); |
| 1145 |
} |
| 1146 |
} |
| 1147 |
|
| 1148 |
JS_PUBLIC_API JS::ProfilingFrameIterator::FrameKind |
| 1149 |
JS::ProfiledFrameHandle::frameKind() const { |
| 1150 |
if (entry_.isBaselineInterpreter()) { |
| 1151 |
return JS::ProfilingFrameIterator::Frame_BaselineInterpreter; |
| 1152 |
} |
| 1153 |
if (entry_.isBaseline()) { |
| 1154 |
return JS::ProfilingFrameIterator::Frame_Baseline; |
| 1155 |
} |
| 1156 |
if (entry_.isSelfHostedShared()) { |
| 1157 |
return JS::ProfilingFrameIterator::Frame_Baseline; |
| 1158 |
} |
| 1159 |
return JS::ProfilingFrameIterator::Frame_Ion; |
| 1160 |
} |
| 1161 |
|
| 1162 |
JS_PUBLIC_API uint64_t JS::ProfiledFrameHandle::realmID() const { |
| 1163 |
return entry_.lookupRealmID(rt_, addr_); |
| 1164 |
} |
| 1165 |
|
| 1166 |
JS_PUBLIC_API JS::ProfiledFrameRange JS::GetProfiledFrames(JSContext* cx, |
| 1167 |
void* addr) { |
| 1168 |
JSRuntime* rt = cx->runtime(); |
| 1169 |
js::jit::JitcodeGlobalTable* table = |
| 1170 |
rt->jitRuntime()->getJitcodeGlobalTable(); |
| 1171 |
js::jit::JitcodeGlobalEntry* entry = table->lookup(addr); |
| 1172 |
|
| 1173 |
ProfiledFrameRange result(rt, addr, entry); |
| 1174 |
|
| 1175 |
if (entry) { |
| 1176 |
result.depth_ = entry->callStackAtAddr(rt, addr, result.labels_, |
| 1177 |
std::size(result.labels_)); |
| 1178 |
} |
| 1179 |
return result; |
| 1180 |
} |
| 1181 |
|
| 1182 |
JS::ProfiledFrameHandle JS::ProfiledFrameRange::Iter::operator*() const { |
| 1183 |
// The iterator iterates in high depth to low depth order. index_ goes up, |
| 1184 |
// and the depth we need to pass to ProfiledFrameHandle goes down. |
| 1185 |
uint32_t depth = range_.depth_ - 1 - index_; |
| 1186 |
return ProfiledFrameHandle(range_.rt_, *range_.entry_, range_.addr_, |
| 1187 |
range_.labels_[depth], depth); |
| 1188 |
} |