| 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 |
#ifndef jit_JitcodeMap_h |
| 8 |
#define jit_JitcodeMap_h |
| 9 |
|
| 10 |
#include "mozilla/Assertions.h" // MOZ_ASSERT, MOZ_ASSERT_IF, MOZ_CRASH |
| 11 |
|
| 12 |
#include <stddef.h> // size_t |
| 13 |
#include <stdint.h> // uint8_t, uint32_t, uint64_t |
| 14 |
|
| 15 |
#include "ds/AvlTree.h" // AvlTree |
| 16 |
#include "jit/CompactBuffer.h" // CompactBufferReader, CompactBufferWriter |
| 17 |
#include "jit/shared/Assembler-shared.h" // CodeOffset |
| 18 |
#include "js/AllocPolicy.h" // SystemAllocPolicy |
| 19 |
#include "js/TypeDecls.h" // jsbytecode |
| 20 |
#include "js/Vector.h" // Vector |
| 21 |
#include "vm/BytecodeLocation.h" // BytecodeLocation |
| 22 |
|
| 23 |
class JSScript; |
| 24 |
class JSTracer; |
| 25 |
struct JSRuntime; |
| 26 |
|
| 27 |
namespace JS { |
| 28 |
class Zone; |
| 29 |
} // namespace JS |
| 30 |
|
| 31 |
namespace js { |
| 32 |
|
| 33 |
class GCMarker; |
| 34 |
|
| 35 |
namespace jit { |
| 36 |
|
| 37 |
class InlineScriptTree; |
| 38 |
|
| 39 |
/* |
| 40 |
* The jitcode map implements tables to allow mapping from addresses in jitcode |
| 41 |
* to the list of (JSScript*, jsbytecode*) pairs that are implicitly active in |
| 42 |
* the frame at that point in the native code. |
| 43 |
* |
| 44 |
* To represent this information efficiently, a multi-level table is used. |
| 45 |
* |
| 46 |
* At the top level, a global AVL-tree of JitcodeGlobalEntry describing the |
| 47 |
* mapping for each individual JitCode generated by compiles. The entries are |
| 48 |
* ordered by their nativeStartAddr. |
| 49 |
* |
| 50 |
* Every entry in the table is of fixed size, but there are different entry |
| 51 |
* types, distinguished by the kind field. |
| 52 |
*/ |
| 53 |
|
| 54 |
class JitcodeGlobalTable; |
| 55 |
class JitcodeIonTable; |
| 56 |
class JitcodeRegionEntry; |
| 57 |
|
| 58 |
struct NativeToBytecode { |
| 59 |
CodeOffset nativeOffset; |
| 60 |
InlineScriptTree* tree; |
| 61 |
jsbytecode* pc; |
| 62 |
}; |
| 63 |
|
| 64 |
// Describes range [start, end) of JIT-generated code. |
| 65 |
class JitCodeRange { |
| 66 |
protected: |
| 67 |
void* const nativeStartAddr_; |
| 68 |
void* const nativeEndAddr_; |
| 69 |
|
| 70 |
public: |
| 71 |
JitCodeRange(void* start, void* end) |
| 72 |
: nativeStartAddr_(start), nativeEndAddr_(end) { |
| 73 |
MOZ_ASSERT(start < end); |
| 74 |
} |
| 75 |
|
| 76 |
// Comparator used by the AvlTree. |
| 77 |
static int compare(const JitCodeRange* r1, const JitCodeRange* r2) { |
| 78 |
// JitCodeRange includes 'start' but excludes 'end'. |
| 79 |
if (r1->nativeEndAddr_ <= r2->nativeStartAddr_) { |
| 80 |
return -1; |
| 81 |
} |
| 82 |
if (r1->nativeStartAddr_ >= r2->nativeEndAddr_) { |
| 83 |
return 1; |
| 84 |
} |
| 85 |
return 0; |
| 86 |
} |
| 87 |
|
| 88 |
void* nativeStartAddr() const { return nativeStartAddr_; } |
| 89 |
void* nativeEndAddr() const { return nativeEndAddr_; } |
| 90 |
|
| 91 |
bool containsPointer(void* ptr) const { |
| 92 |
return nativeStartAddr() <= ptr && ptr < nativeEndAddr(); |
| 93 |
} |
| 94 |
}; |
| 95 |
|
| 96 |
using BytecodeLocationVector = Vector<BytecodeLocation, 0, SystemAllocPolicy>; |
| 97 |
|
| 98 |
class IonEntry; |
| 99 |
class IonICEntry; |
| 100 |
class BaselineEntry; |
| 101 |
class BaselineInterpreterEntry; |
| 102 |
class DummyEntry; |
| 103 |
class SelfHostedSharedEntry; |
| 104 |
|
| 105 |
// Base class for all entries. |
| 106 |
class JitcodeGlobalEntry : public JitCodeRange { |
| 107 |
protected: |
| 108 |
JitCode* jitcode_; |
| 109 |
// If this entry is referenced from the profiler buffer, this is the |
| 110 |
// position where the most recent sample that references it starts. |
| 111 |
// Otherwise set to kNoSampleInBuffer. |
| 112 |
static const uint64_t kNoSampleInBuffer = UINT64_MAX; |
| 113 |
uint64_t samplePositionInBuffer_ = kNoSampleInBuffer; |
| 114 |
|
| 115 |
public: |
| 116 |
enum class Kind : uint8_t { |
| 117 |
Ion, |
| 118 |
IonIC, |
| 119 |
Baseline, |
| 120 |
BaselineInterpreter, |
| 121 |
Dummy, |
| 122 |
SelfHostedShared, |
| 123 |
}; |
| 124 |
|
| 125 |
protected: |
| 126 |
Kind kind_; |
| 127 |
|
| 128 |
JitcodeGlobalEntry(Kind kind, JitCode* code, void* nativeStartAddr, |
| 129 |
void* nativeEndAddr) |
| 130 |
: JitCodeRange(nativeStartAddr, nativeEndAddr), |
| 131 |
jitcode_(code), |
| 132 |
kind_(kind) { |
| 133 |
MOZ_ASSERT(code); |
| 134 |
MOZ_ASSERT(nativeStartAddr); |
| 135 |
MOZ_ASSERT(nativeEndAddr); |
| 136 |
} |
| 137 |
|
| 138 |
// Protected destructor to ensure this is called through DestroyPolicy. |
| 139 |
~JitcodeGlobalEntry() = default; |
| 140 |
|
| 141 |
JitcodeGlobalEntry(const JitcodeGlobalEntry& other) = delete; |
| 142 |
void operator=(const JitcodeGlobalEntry& other) = delete; |
| 143 |
|
| 144 |
public: |
| 145 |
struct DestroyPolicy { |
| 146 |
void operator()(JitcodeGlobalEntry* entry); |
| 147 |
}; |
| 148 |
|
| 149 |
void setSamplePositionInBuffer(uint64_t bufferWritePos) { |
| 150 |
samplePositionInBuffer_ = bufferWritePos; |
| 151 |
} |
| 152 |
void setAsExpired() { samplePositionInBuffer_ = kNoSampleInBuffer; } |
| 153 |
bool isSampled(uint64_t bufferRangeStart) { |
| 154 |
if (samplePositionInBuffer_ == kNoSampleInBuffer) { |
| 155 |
return false; |
| 156 |
} |
| 157 |
return bufferRangeStart <= samplePositionInBuffer_; |
| 158 |
} |
| 159 |
|
| 160 |
Kind kind() const { return kind_; } |
| 161 |
bool isIon() const { return kind() == Kind::Ion; } |
| 162 |
bool isIonIC() const { return kind() == Kind::IonIC; } |
| 163 |
bool isBaseline() const { return kind() == Kind::Baseline; } |
| 164 |
bool isBaselineInterpreter() const { |
| 165 |
return kind() == Kind::BaselineInterpreter; |
| 166 |
} |
| 167 |
bool isDummy() const { return kind() == Kind::Dummy; } |
| 168 |
bool isSelfHostedShared() const { return kind() == Kind::SelfHostedShared; } |
| 169 |
|
| 170 |
inline const IonEntry& asIon() const; |
| 171 |
inline const IonICEntry& asIonIC() const; |
| 172 |
inline const BaselineEntry& asBaseline() const; |
| 173 |
inline const BaselineInterpreterEntry& asBaselineInterpreter() const; |
| 174 |
inline const DummyEntry& asDummy() const; |
| 175 |
inline const SelfHostedSharedEntry& asSelfHostedShared() const; |
| 176 |
|
| 177 |
inline IonEntry& asIon(); |
| 178 |
inline IonICEntry& asIonIC(); |
| 179 |
inline BaselineEntry& asBaseline(); |
| 180 |
inline BaselineInterpreterEntry& asBaselineInterpreter(); |
| 181 |
inline DummyEntry& asDummy(); |
| 182 |
inline SelfHostedSharedEntry& asSelfHostedShared(); |
| 183 |
|
| 184 |
JitCode* jitcode() const { return jitcode_; } |
| 185 |
JitCode** jitcodePtr() { return &jitcode_; } |
| 186 |
Zone* zone() const { return jitcode()->zone(); } |
| 187 |
|
| 188 |
bool traceJitcode(JSTracer* trc); |
| 189 |
bool isJitcodeMarkedFromAnyThread(JSRuntime* rt); |
| 190 |
|
| 191 |
bool trace(JSTracer* trc); |
| 192 |
void traceWeak(JSTracer* trc); |
| 193 |
uint64_t lookupRealmID(JSRuntime* rt, void* ptr) const; |
| 194 |
void* canonicalNativeAddrFor(JSRuntime* rt, void* ptr) const; |
| 195 |
|
| 196 |
// Read the inline call stack at a given point in the native code and append |
| 197 |
// into the given vector. Innermost (script,pc) pair will be appended first, |
| 198 |
// and outermost appended last. |
| 199 |
// |
| 200 |
// Returns false on memory failure. |
| 201 |
[[nodiscard]] bool callStackAtAddr(JSRuntime* rt, void* ptr, |
| 202 |
BytecodeLocationVector& results, |
| 203 |
uint32_t* depth) const; |
| 204 |
uint32_t callStackAtAddr(JSRuntime* rt, void* ptr, const char** results, |
| 205 |
uint32_t maxResults) const; |
| 206 |
}; |
| 207 |
|
| 208 |
using UniqueJitcodeGlobalEntry = |
| 209 |
UniquePtr<JitcodeGlobalEntry, JitcodeGlobalEntry::DestroyPolicy>; |
| 210 |
|
| 211 |
template <typename T, typename... Args> |
| 212 |
inline UniqueJitcodeGlobalEntry MakeJitcodeGlobalEntry(JSContext* cx, |
| 213 |
Args&&... args) { |
| 214 |
UniqueJitcodeGlobalEntry res(js_new<T>(std::forward<Args>(args)...)); |
| 215 |
if (!res) { |
| 216 |
ReportOutOfMemory(cx); |
| 217 |
} |
| 218 |
return res; |
| 219 |
} |
| 220 |
|
| 221 |
class IonEntry : public JitcodeGlobalEntry { |
| 222 |
public: |
| 223 |
struct ScriptNamePair { |
| 224 |
JSScript* script; |
| 225 |
UniqueChars str; |
| 226 |
ScriptNamePair(JSScript* script, UniqueChars str) |
| 227 |
: script(script), str(std::move(str)) {} |
| 228 |
}; |
| 229 |
using ScriptList = Vector<ScriptNamePair, 2, SystemAllocPolicy>; |
| 230 |
|
| 231 |
private: |
| 232 |
ScriptList scriptList_; |
| 233 |
|
| 234 |
// regionTable_ points to the start of the region table within the |
| 235 |
// packed map for compile represented by this entry. Since the |
| 236 |
// region table occurs at the tail of the memory region, this pointer |
| 237 |
// points somewhere inside the region memory space, and not to the start |
| 238 |
// of the memory space. |
| 239 |
const JitcodeIonTable* regionTable_; |
| 240 |
|
| 241 |
public: |
| 242 |
IonEntry(JitCode* code, void* nativeStartAddr, void* nativeEndAddr, |
| 243 |
ScriptList&& scriptList, JitcodeIonTable* regionTable) |
| 244 |
: JitcodeGlobalEntry(Kind::Ion, code, nativeStartAddr, nativeEndAddr), |
| 245 |
scriptList_(std::move(scriptList)), |
| 246 |
regionTable_(regionTable) { |
| 247 |
MOZ_ASSERT(regionTable); |
| 248 |
} |
| 249 |
|
| 250 |
~IonEntry(); |
| 251 |
|
| 252 |
ScriptList& scriptList() { return scriptList_; } |
| 253 |
|
| 254 |
size_t numScripts() const { return scriptList_.length(); } |
| 255 |
|
| 256 |
JSScript* getScript(unsigned idx) const { |
| 257 |
MOZ_ASSERT(idx < numScripts()); |
| 258 |
return scriptList_[idx].script; |
| 259 |
} |
| 260 |
|
| 261 |
const char* getStr(unsigned idx) const { |
| 262 |
MOZ_ASSERT(idx < numScripts()); |
| 263 |
return scriptList_[idx].str.get(); |
| 264 |
} |
| 265 |
|
| 266 |
const JitcodeIonTable* regionTable() const { return regionTable_; } |
| 267 |
|
| 268 |
void* canonicalNativeAddrFor(void* ptr) const; |
| 269 |
|
| 270 |
[[nodiscard]] bool callStackAtAddr(void* ptr, BytecodeLocationVector& results, |
| 271 |
uint32_t* depth) const; |
| 272 |
|
| 273 |
uint32_t callStackAtAddr(void* ptr, const char** results, |
| 274 |
uint32_t maxResults) const; |
| 275 |
|
| 276 |
uint64_t lookupRealmID(void* ptr) const; |
| 277 |
|
| 278 |
bool trace(JSTracer* trc); |
| 279 |
void traceWeak(JSTracer* trc); |
| 280 |
}; |
| 281 |
|
| 282 |
class IonICEntry : public JitcodeGlobalEntry { |
| 283 |
// Address for this IC in the IonScript code. Most operations on IonICEntry |
| 284 |
// use this to forward to the IonEntry. |
| 285 |
void* rejoinAddr_; |
| 286 |
|
| 287 |
public: |
| 288 |
IonICEntry(JitCode* code, void* nativeStartAddr, void* nativeEndAddr, |
| 289 |
void* rejoinAddr) |
| 290 |
: JitcodeGlobalEntry(Kind::IonIC, code, nativeStartAddr, nativeEndAddr), |
| 291 |
rejoinAddr_(rejoinAddr) { |
| 292 |
MOZ_ASSERT(rejoinAddr_); |
| 293 |
} |
| 294 |
|
| 295 |
void* rejoinAddr() const { return rejoinAddr_; } |
| 296 |
|
| 297 |
void* canonicalNativeAddrFor(void* ptr) const; |
| 298 |
|
| 299 |
[[nodiscard]] bool callStackAtAddr(JSRuntime* rt, void* ptr, |
| 300 |
BytecodeLocationVector& results, |
| 301 |
uint32_t* depth) const; |
| 302 |
|
| 303 |
uint32_t callStackAtAddr(JSRuntime* rt, void* ptr, const char** results, |
| 304 |
uint32_t maxResults) const; |
| 305 |
|
| 306 |
uint64_t lookupRealmID(JSRuntime* rt, void* ptr) const; |
| 307 |
|
| 308 |
bool trace(JSTracer* trc); |
| 309 |
void traceWeak(JSTracer* trc); |
| 310 |
}; |
| 311 |
|
| 312 |
class BaselineEntry : public JitcodeGlobalEntry { |
| 313 |
JSScript* script_; |
| 314 |
UniqueChars str_; |
| 315 |
|
| 316 |
public: |
| 317 |
BaselineEntry(JitCode* code, void* nativeStartAddr, void* nativeEndAddr, |
| 318 |
JSScript* script, UniqueChars str) |
| 319 |
: JitcodeGlobalEntry(Kind::Baseline, code, nativeStartAddr, |
| 320 |
nativeEndAddr), |
| 321 |
script_(script), |
| 322 |
str_(std::move(str)) { |
| 323 |
MOZ_ASSERT(script_); |
| 324 |
MOZ_ASSERT(str_); |
| 325 |
} |
| 326 |
|
| 327 |
JSScript* script() const { return script_; } |
| 328 |
|
| 329 |
const char* str() const { return str_.get(); } |
| 330 |
|
| 331 |
void* canonicalNativeAddrFor(void* ptr) const; |
| 332 |
|
| 333 |
[[nodiscard]] bool callStackAtAddr(void* ptr, BytecodeLocationVector& results, |
| 334 |
uint32_t* depth) const; |
| 335 |
|
| 336 |
uint32_t callStackAtAddr(void* ptr, const char** results, |
| 337 |
uint32_t maxResults) const; |
| 338 |
|
| 339 |
uint64_t lookupRealmID() const; |
| 340 |
|
| 341 |
bool trace(JSTracer* trc); |
| 342 |
void traceWeak(JSTracer* trc); |
| 343 |
}; |
| 344 |
|
| 345 |
class SelfHostedSharedEntry : public JitcodeGlobalEntry { |
| 346 |
UniqueChars str_; |
| 347 |
|
| 348 |
public: |
| 349 |
SelfHostedSharedEntry(JitCode* code, void* nativeStartAddr, |
| 350 |
void* nativeEndAddr, UniqueChars str) |
| 351 |
: JitcodeGlobalEntry(Kind::SelfHostedShared, code, nativeStartAddr, |
| 352 |
nativeEndAddr), |
| 353 |
str_(std::move(str)) { |
| 354 |
MOZ_ASSERT(str_); |
| 355 |
} |
| 356 |
|
| 357 |
const char* str() const { return str_.get(); } |
| 358 |
|
| 359 |
void* canonicalNativeAddrFor(void* ptr) const; |
| 360 |
|
| 361 |
[[nodiscard]] bool callStackAtAddr(void* ptr, BytecodeLocationVector& results, |
| 362 |
uint32_t* depth) const; |
| 363 |
|
| 364 |
uint32_t callStackAtAddr(void* ptr, const char** results, |
| 365 |
uint32_t maxResults) const; |
| 366 |
|
| 367 |
uint64_t lookupRealmID() const; |
| 368 |
}; |
| 369 |
|
| 370 |
class BaselineInterpreterEntry : public JitcodeGlobalEntry { |
| 371 |
public: |
| 372 |
BaselineInterpreterEntry(JitCode* code, void* nativeStartAddr, |
| 373 |
void* nativeEndAddr) |
| 374 |
: JitcodeGlobalEntry(Kind::BaselineInterpreter, code, nativeStartAddr, |
| 375 |
nativeEndAddr) {} |
| 376 |
|
| 377 |
void* canonicalNativeAddrFor(void* ptr) const; |
| 378 |
|
| 379 |
[[nodiscard]] bool callStackAtAddr(void* ptr, BytecodeLocationVector& results, |
| 380 |
uint32_t* depth) const; |
| 381 |
|
| 382 |
uint32_t callStackAtAddr(void* ptr, const char** results, |
| 383 |
uint32_t maxResults) const; |
| 384 |
|
| 385 |
uint64_t lookupRealmID() const; |
| 386 |
}; |
| 387 |
|
| 388 |
// Dummy entries are created for jitcode generated when profiling is not |
| 389 |
// turned on, so that they have representation in the global table if they are |
| 390 |
// on the stack when profiling is enabled. |
| 391 |
class DummyEntry : public JitcodeGlobalEntry { |
| 392 |
public: |
| 393 |
DummyEntry(JitCode* code, void* nativeStartAddr, void* nativeEndAddr) |
| 394 |
: JitcodeGlobalEntry(Kind::Dummy, code, nativeStartAddr, nativeEndAddr) {} |
| 395 |
|
| 396 |
void* canonicalNativeAddrFor(JSRuntime* rt, void* ptr) const { |
| 397 |
return nullptr; |
| 398 |
} |
| 399 |
|
| 400 |
[[nodiscard]] bool callStackAtAddr(JSRuntime* rt, void* ptr, |
| 401 |
BytecodeLocationVector& results, |
| 402 |
uint32_t* depth) const { |
| 403 |
return true; |
| 404 |
} |
| 405 |
|
| 406 |
uint32_t callStackAtAddr(JSRuntime* rt, void* ptr, const char** results, |
| 407 |
uint32_t maxResults) const { |
| 408 |
return 0; |
| 409 |
} |
| 410 |
|
| 411 |
uint64_t lookupRealmID() const { return 0; } |
| 412 |
}; |
| 413 |
|
| 414 |
inline const IonEntry& JitcodeGlobalEntry::asIon() const { |
| 415 |
MOZ_ASSERT(isIon()); |
| 416 |
return *static_cast<const IonEntry*>(this); |
| 417 |
} |
| 418 |
|
| 419 |
inline const IonICEntry& JitcodeGlobalEntry::asIonIC() const { |
| 420 |
MOZ_ASSERT(isIonIC()); |
| 421 |
return *static_cast<const IonICEntry*>(this); |
| 422 |
} |
| 423 |
|
| 424 |
inline const BaselineEntry& JitcodeGlobalEntry::asBaseline() const { |
| 425 |
MOZ_ASSERT(isBaseline()); |
| 426 |
return *static_cast<const BaselineEntry*>(this); |
| 427 |
} |
| 428 |
|
| 429 |
inline const BaselineInterpreterEntry& |
| 430 |
JitcodeGlobalEntry::asBaselineInterpreter() const { |
| 431 |
MOZ_ASSERT(isBaselineInterpreter()); |
| 432 |
return *static_cast<const BaselineInterpreterEntry*>(this); |
| 433 |
} |
| 434 |
|
| 435 |
inline const DummyEntry& JitcodeGlobalEntry::asDummy() const { |
| 436 |
MOZ_ASSERT(isDummy()); |
| 437 |
return *static_cast<const DummyEntry*>(this); |
| 438 |
} |
| 439 |
|
| 440 |
inline const SelfHostedSharedEntry& JitcodeGlobalEntry::asSelfHostedShared() |
| 441 |
const { |
| 442 |
MOZ_ASSERT(isSelfHostedShared()); |
| 443 |
return *static_cast<const SelfHostedSharedEntry*>(this); |
| 444 |
} |
| 445 |
|
| 446 |
inline IonEntry& JitcodeGlobalEntry::asIon() { |
| 447 |
MOZ_ASSERT(isIon()); |
| 448 |
return *static_cast<IonEntry*>(this); |
| 449 |
} |
| 450 |
|
| 451 |
inline IonICEntry& JitcodeGlobalEntry::asIonIC() { |
| 452 |
MOZ_ASSERT(isIonIC()); |
| 453 |
return *static_cast<IonICEntry*>(this); |
| 454 |
} |
| 455 |
|
| 456 |
inline BaselineEntry& JitcodeGlobalEntry::asBaseline() { |
| 457 |
MOZ_ASSERT(isBaseline()); |
| 458 |
return *static_cast<BaselineEntry*>(this); |
| 459 |
} |
| 460 |
|
| 461 |
inline BaselineInterpreterEntry& JitcodeGlobalEntry::asBaselineInterpreter() { |
| 462 |
MOZ_ASSERT(isBaselineInterpreter()); |
| 463 |
return *static_cast<BaselineInterpreterEntry*>(this); |
| 464 |
} |
| 465 |
|
| 466 |
inline DummyEntry& JitcodeGlobalEntry::asDummy() { |
| 467 |
MOZ_ASSERT(isDummy()); |
| 468 |
return *static_cast<DummyEntry*>(this); |
| 469 |
} |
| 470 |
|
| 471 |
inline SelfHostedSharedEntry& JitcodeGlobalEntry::asSelfHostedShared() { |
| 472 |
MOZ_ASSERT(isSelfHostedShared()); |
| 473 |
return *static_cast<SelfHostedSharedEntry*>(this); |
| 474 |
} |
| 475 |
|
| 476 |
// Global table of JitcodeGlobalEntry entries. |
| 477 |
class JitcodeGlobalTable { |
| 478 |
private: |
| 479 |
// Vector containing (and owning) all entries. This is unsorted and used for |
| 480 |
// iterating over all entries, because the AvlTree currently doesn't support |
| 481 |
// modifications while iterating. |
| 482 |
using EntryVector = Vector<UniqueJitcodeGlobalEntry, 0, SystemAllocPolicy>; |
| 483 |
EntryVector entries_; |
| 484 |
|
| 485 |
// AVL tree containing all entries in the Vector above. This is used to |
| 486 |
// efficiently look up the entry corresponding to a native code address. |
| 487 |
using EntryTree = AvlTree<JitCodeRange*, JitCodeRange>; |
| 488 |
static const size_t LIFO_CHUNK_SIZE = 16 * 1024; |
| 489 |
LifoAlloc alloc_; |
| 490 |
EntryTree tree_; |
| 491 |
|
| 492 |
public: |
| 493 |
JitcodeGlobalTable() |
| 494 |
: alloc_(LIFO_CHUNK_SIZE, js::BackgroundMallocArena), tree_(&alloc_) {} |
| 495 |
|
| 496 |
bool empty() const { |
| 497 |
MOZ_ASSERT(entries_.empty() == tree_.empty()); |
| 498 |
return entries_.empty(); |
| 499 |
} |
| 500 |
|
| 501 |
JitcodeGlobalEntry* lookup(void* ptr) { return lookupInternal(ptr); } |
| 502 |
|
| 503 |
const JitcodeGlobalEntry* lookupForSampler(void* ptr, JSRuntime* rt, |
| 504 |
uint64_t samplePosInBuffer); |
| 505 |
|
| 506 |
[[nodiscard]] bool addEntry(UniqueJitcodeGlobalEntry entry); |
| 507 |
|
| 508 |
void setAllEntriesAsExpired(); |
| 509 |
[[nodiscard]] bool markIteratively(GCMarker* marker); |
| 510 |
void traceWeak(JSRuntime* rt, JSTracer* trc); |
| 511 |
|
| 512 |
private: |
| 513 |
JitcodeGlobalEntry* lookupInternal(void* ptr); |
| 514 |
}; |
| 515 |
|
| 516 |
// clang-format off |
| 517 |
/* |
| 518 |
* Container class for main jitcode table. |
| 519 |
* The Region table's memory is structured as follows: |
| 520 |
* |
| 521 |
* +------------------------------------------------+ | |
| 522 |
* | Region 1 Run | | |
| 523 |
* |------------------------------------------------| | |
| 524 |
* | Region 2 Run | | |
| 525 |
* | | | |
| 526 |
* | | | |
| 527 |
* |------------------------------------------------| | |
| 528 |
* | Region 3 Run | | |
| 529 |
* | | | |
| 530 |
* |------------------------------------------------| |-- Payload |
| 531 |
* | | | |
| 532 |
* | ... | | |
| 533 |
* | | | |
| 534 |
* |------------------------------------------------| | |
| 535 |
* | Region M Run | | |
| 536 |
* | | | |
| 537 |
* +================================================+ <- RegionTable pointer points here |
| 538 |
* | uint23_t numRegions = M | | |
| 539 |
* +------------------------------------------------+ | |
| 540 |
* | Region 1 | | |
| 541 |
* | uint32_t entryOffset = size(Payload) | | |
| 542 |
* +------------------------------------------------+ | |
| 543 |
* | | |-- Table |
| 544 |
* | ... | | |
| 545 |
* | | | |
| 546 |
* +------------------------------------------------+ | |
| 547 |
* | Region M | | |
| 548 |
* | uint32_t entryOffset | | |
| 549 |
* +------------------------------------------------+ | |
| 550 |
* |
| 551 |
* The region table is composed of two sections: a tail section that contains a table of |
| 552 |
* fixed-size entries containing offsets into the the head section, and a head section that |
| 553 |
* holds a sequence of variable-sized runs. The table in the tail section serves to |
| 554 |
* locate the variable-length encoded structures in the head section. |
| 555 |
* |
| 556 |
* The entryOffsets in the table indicate the bytes offset to subtract from the regionTable |
| 557 |
* pointer to arrive at the encoded region in the payload. |
| 558 |
* |
| 559 |
* |
| 560 |
* Variable-length entries in payload |
| 561 |
* ---------------------------------- |
| 562 |
* The entryOffsets in the region table's fixed-sized entries refer to a location within the |
| 563 |
* variable-length payload section. This location contains a compactly encoded "run" of |
| 564 |
* mappings. |
| 565 |
* |
| 566 |
* Each run starts by describing the offset within the native code it starts at, and the |
| 567 |
* sequence of (JSScript*, jsbytecode*) pairs active at that site. Following that, there |
| 568 |
* are a number of variable-length entries encoding (nativeOffsetDelta, bytecodeOffsetDelta) |
| 569 |
* pairs for the run. |
| 570 |
* |
| 571 |
* VarUint32 nativeOffset; |
| 572 |
* - The offset from nativeStartAddr in the global table entry at which |
| 573 |
* the jitcode for this region starts. |
| 574 |
* |
| 575 |
* Uint8_t scriptDepth; |
| 576 |
* - The depth of inlined scripts for this region. |
| 577 |
* |
| 578 |
* List<VarUint32> inlineScriptPcStack; |
| 579 |
* - We encode (2 * scriptDepth) VarUint32s here. Each pair of uint32s are taken |
| 580 |
* as an index into the scriptList in the global table entry, and a pcOffset |
| 581 |
* respectively. |
| 582 |
* |
| 583 |
* List<NativeAndBytecodeDelta> deltaRun; |
| 584 |
* - The rest of the entry is a deltaRun that stores a series of variable-length |
| 585 |
* encoded NativeAndBytecodeDelta datums. |
| 586 |
*/ |
| 587 |
// clang-format on |
| 588 |
class JitcodeRegionEntry { |
| 589 |
private: |
| 590 |
static const unsigned MAX_RUN_LENGTH = 100; |
| 591 |
|
| 592 |
public: |
| 593 |
static void WriteHead(CompactBufferWriter& writer, uint32_t nativeOffset, |
| 594 |
uint8_t scriptDepth); |
| 595 |
static void ReadHead(CompactBufferReader& reader, uint32_t* nativeOffset, |
| 596 |
uint8_t* scriptDepth); |
| 597 |
|
| 598 |
static void WriteScriptPc(CompactBufferWriter& writer, uint32_t scriptIdx, |
| 599 |
uint32_t pcOffset); |
| 600 |
static void ReadScriptPc(CompactBufferReader& reader, uint32_t* scriptIdx, |
| 601 |
uint32_t* pcOffset); |
| 602 |
|
| 603 |
static void WriteDelta(CompactBufferWriter& writer, uint32_t nativeDelta, |
| 604 |
int32_t pcDelta); |
| 605 |
static void ReadDelta(CompactBufferReader& reader, uint32_t* nativeDelta, |
| 606 |
int32_t* pcDelta); |
| 607 |
|
| 608 |
// Given a pointer into an array of NativeToBytecode (and a pointer to the end |
| 609 |
// of the array), compute the number of entries that would be consume by |
| 610 |
// outputting a run starting at this one. |
| 611 |
static uint32_t ExpectedRunLength(const NativeToBytecode* entry, |
| 612 |
const NativeToBytecode* end); |
| 613 |
|
| 614 |
// Write a run, starting at the given NativeToBytecode entry, into the given |
| 615 |
// buffer writer. |
| 616 |
[[nodiscard]] static bool WriteRun(CompactBufferWriter& writer, |
| 617 |
const IonEntry::ScriptList& scriptList, |
| 618 |
uint32_t runLength, |
| 619 |
const NativeToBytecode* entry); |
| 620 |
|
| 621 |
// Delta Run entry formats are encoded little-endian: |
| 622 |
// |
| 623 |
// byte 0 |
| 624 |
// NNNN-BBB0 |
| 625 |
// Single byte format. nativeDelta in [0, 15], pcDelta in [0, 7] |
| 626 |
// |
| 627 |
static const uint32_t ENC1_MASK = 0x1; |
| 628 |
static const uint32_t ENC1_MASK_VAL = 0x0; |
| 629 |
|
| 630 |
static const uint32_t ENC1_NATIVE_DELTA_MAX = 0xf; |
| 631 |
static const unsigned ENC1_NATIVE_DELTA_SHIFT = 4; |
| 632 |
|
| 633 |
static const uint32_t ENC1_PC_DELTA_MASK = 0x0e; |
| 634 |
static const int32_t ENC1_PC_DELTA_MAX = 0x7; |
| 635 |
static const unsigned ENC1_PC_DELTA_SHIFT = 1; |
| 636 |
|
| 637 |
// byte 1 byte 0 |
| 638 |
// NNNN-NNNN BBBB-BB01 |
| 639 |
// Two-byte format. nativeDelta in [0, 255], pcDelta in [0, 63] |
| 640 |
// |
| 641 |
static const uint32_t ENC2_MASK = 0x3; |
| 642 |
static const uint32_t ENC2_MASK_VAL = 0x1; |
| 643 |
|
| 644 |
static const uint32_t ENC2_NATIVE_DELTA_MAX = 0xff; |
| 645 |
static const unsigned ENC2_NATIVE_DELTA_SHIFT = 8; |
| 646 |
|
| 647 |
static const uint32_t ENC2_PC_DELTA_MASK = 0x00fc; |
| 648 |
static const int32_t ENC2_PC_DELTA_MAX = 0x3f; |
| 649 |
static const unsigned ENC2_PC_DELTA_SHIFT = 2; |
| 650 |
|
| 651 |
// byte 2 byte 1 byte 0 |
| 652 |
// NNNN-NNNN NNNB-BBBB BBBB-B011 |
| 653 |
// Three-byte format. nativeDelta in [0, 2047], pcDelta in [-512, 511] |
| 654 |
// |
| 655 |
static const uint32_t ENC3_MASK = 0x7; |
| 656 |
static const uint32_t ENC3_MASK_VAL = 0x3; |
| 657 |
|
| 658 |
static const uint32_t ENC3_NATIVE_DELTA_MAX = 0x7ff; |
| 659 |
static const unsigned ENC3_NATIVE_DELTA_SHIFT = 13; |
| 660 |
|
| 661 |
static const uint32_t ENC3_PC_DELTA_MASK = 0x001ff8; |
| 662 |
static const int32_t ENC3_PC_DELTA_MAX = 0x1ff; |
| 663 |
static const int32_t ENC3_PC_DELTA_MIN = -ENC3_PC_DELTA_MAX - 1; |
| 664 |
static const unsigned ENC3_PC_DELTA_SHIFT = 3; |
| 665 |
|
| 666 |
// byte 3 byte 2 byte 1 byte 0 |
| 667 |
// NNNN-NNNN NNNN-NNNN BBBB-BBBB BBBB-B111 |
| 668 |
// Three-byte format. nativeDelta in [0, 65535], |
| 669 |
// pcDelta in [-4096, 4095] |
| 670 |
static const uint32_t ENC4_MASK = 0x7; |
| 671 |
static const uint32_t ENC4_MASK_VAL = 0x7; |
| 672 |
|
| 673 |
static const uint32_t ENC4_NATIVE_DELTA_MAX = 0xffff; |
| 674 |
static const unsigned ENC4_NATIVE_DELTA_SHIFT = 16; |
| 675 |
|
| 676 |
static const uint32_t ENC4_PC_DELTA_MASK = 0x0000fff8; |
| 677 |
static const int32_t ENC4_PC_DELTA_MAX = 0xfff; |
| 678 |
static const int32_t ENC4_PC_DELTA_MIN = -ENC4_PC_DELTA_MAX - 1; |
| 679 |
static const unsigned ENC4_PC_DELTA_SHIFT = 3; |
| 680 |
|
| 681 |
static bool IsDeltaEncodeable(uint32_t nativeDelta, int32_t pcDelta) { |
| 682 |
return (nativeDelta <= ENC4_NATIVE_DELTA_MAX) && |
| 683 |
(pcDelta >= ENC4_PC_DELTA_MIN) && (pcDelta <= ENC4_PC_DELTA_MAX); |
| 684 |
} |
| 685 |
|
| 686 |
private: |
| 687 |
const uint8_t* data_; |
| 688 |
const uint8_t* end_; |
| 689 |
|
| 690 |
// Unpacked state from jitcode entry. |
| 691 |
uint32_t nativeOffset_; |
| 692 |
uint8_t scriptDepth_; |
| 693 |
const uint8_t* scriptPcStack_; |
| 694 |
const uint8_t* deltaRun_; |
| 695 |
|
| 696 |
void unpack(); |
| 697 |
|
| 698 |
public: |
| 699 |
JitcodeRegionEntry(const uint8_t* data, const uint8_t* end) |
| 700 |
: data_(data), |
| 701 |
end_(end), |
| 702 |
nativeOffset_(0), |
| 703 |
scriptDepth_(0), |
| 704 |
scriptPcStack_(nullptr), |
| 705 |
deltaRun_(nullptr) { |
| 706 |
MOZ_ASSERT(data_ < end_); |
| 707 |
unpack(); |
| 708 |
MOZ_ASSERT(scriptPcStack_ < end_); |
| 709 |
MOZ_ASSERT(deltaRun_ <= end_); |
| 710 |
} |
| 711 |
|
| 712 |
uint32_t nativeOffset() const { return nativeOffset_; } |
| 713 |
uint32_t scriptDepth() const { return scriptDepth_; } |
| 714 |
|
| 715 |
class ScriptPcIterator { |
| 716 |
private: |
| 717 |
const uint8_t* start_; |
| 718 |
const uint8_t* end_; |
| 719 |
#ifdef DEBUG |
| 720 |
uint32_t count_; |
| 721 |
#endif |
| 722 |
uint32_t idx_; |
| 723 |
const uint8_t* cur_; |
| 724 |
|
| 725 |
public: |
| 726 |
ScriptPcIterator(const uint8_t* start, const uint8_t* end, uint32_t count) |
| 727 |
: start_(start), |
| 728 |
end_(end), |
| 729 |
#ifdef DEBUG |
| 730 |
count_(count), |
| 731 |
#endif |
| 732 |
idx_(0), |
| 733 |
cur_(start_) { |
| 734 |
} |
| 735 |
|
| 736 |
bool hasMore() const { |
| 737 |
MOZ_ASSERT((idx_ == count_) == (cur_ == end_)); |
| 738 |
MOZ_ASSERT((idx_ < count_) == (cur_ < end_)); |
| 739 |
return cur_ < end_; |
| 740 |
} |
| 741 |
|
| 742 |
void readNext(uint32_t* scriptIdxOut, uint32_t* pcOffsetOut) { |
| 743 |
MOZ_ASSERT(scriptIdxOut); |
| 744 |
MOZ_ASSERT(pcOffsetOut); |
| 745 |
MOZ_ASSERT(hasMore()); |
| 746 |
|
| 747 |
CompactBufferReader reader(cur_, end_); |
| 748 |
ReadScriptPc(reader, scriptIdxOut, pcOffsetOut); |
| 749 |
|
| 750 |
cur_ = reader.currentPosition(); |
| 751 |
MOZ_ASSERT(cur_ <= end_); |
| 752 |
|
| 753 |
idx_++; |
| 754 |
MOZ_ASSERT_IF(idx_ == count_, cur_ == end_); |
| 755 |
} |
| 756 |
|
| 757 |
void reset() { |
| 758 |
idx_ = 0; |
| 759 |
cur_ = start_; |
| 760 |
} |
| 761 |
}; |
| 762 |
|
| 763 |
ScriptPcIterator scriptPcIterator() const { |
| 764 |
// End of script+pc sequence is the start of the delta run. |
| 765 |
return ScriptPcIterator(scriptPcStack_, deltaRun_, scriptDepth_); |
| 766 |
} |
| 767 |
|
| 768 |
class DeltaIterator { |
| 769 |
private: |
| 770 |
const uint8_t* start_; |
| 771 |
const uint8_t* end_; |
| 772 |
const uint8_t* cur_; |
| 773 |
|
| 774 |
public: |
| 775 |
DeltaIterator(const uint8_t* start, const uint8_t* end) |
| 776 |
: start_(start), end_(end), cur_(start) {} |
| 777 |
|
| 778 |
bool hasMore() const { |
| 779 |
MOZ_ASSERT(cur_ <= end_); |
| 780 |
return cur_ < end_; |
| 781 |
} |
| 782 |
|
| 783 |
void readNext(uint32_t* nativeDeltaOut, int32_t* pcDeltaOut) { |
| 784 |
MOZ_ASSERT(nativeDeltaOut != nullptr); |
| 785 |
MOZ_ASSERT(pcDeltaOut != nullptr); |
| 786 |
|
| 787 |
MOZ_ASSERT(hasMore()); |
| 788 |
|
| 789 |
CompactBufferReader reader(cur_, end_); |
| 790 |
ReadDelta(reader, nativeDeltaOut, pcDeltaOut); |
| 791 |
|
| 792 |
cur_ = reader.currentPosition(); |
| 793 |
MOZ_ASSERT(cur_ <= end_); |
| 794 |
} |
| 795 |
|
| 796 |
void reset() { cur_ = start_; } |
| 797 |
}; |
| 798 |
DeltaIterator deltaIterator() const { return DeltaIterator(deltaRun_, end_); } |
| 799 |
|
| 800 |
uint32_t findPcOffset(uint32_t queryNativeOffset, |
| 801 |
uint32_t startPcOffset) const; |
| 802 |
}; |
| 803 |
|
| 804 |
class JitcodeIonTable { |
| 805 |
private: |
| 806 |
/* Variable length payload section "below" here. */ |
| 807 |
uint32_t numRegions_; |
| 808 |
uint32_t regionOffsets_[1]; |
| 809 |
|
| 810 |
const uint8_t* payloadEnd() const { |
| 811 |
return reinterpret_cast<const uint8_t*>(this); |
| 812 |
} |
| 813 |
|
| 814 |
public: |
| 815 |
JitcodeIonTable() = delete; |
| 816 |
|
| 817 |
uint32_t numRegions() const { return numRegions_; } |
| 818 |
|
| 819 |
uint32_t regionOffset(uint32_t regionIndex) const { |
| 820 |
MOZ_ASSERT(regionIndex < numRegions()); |
| 821 |
return regionOffsets_[regionIndex]; |
| 822 |
} |
| 823 |
|
| 824 |
JitcodeRegionEntry regionEntry(uint32_t regionIndex) const { |
| 825 |
const uint8_t* regionStart = payloadEnd() - regionOffset(regionIndex); |
| 826 |
const uint8_t* regionEnd = payloadEnd(); |
| 827 |
if (regionIndex < numRegions_ - 1) { |
| 828 |
regionEnd -= regionOffset(regionIndex + 1); |
| 829 |
} |
| 830 |
return JitcodeRegionEntry(regionStart, regionEnd); |
| 831 |
} |
| 832 |
|
| 833 |
uint32_t findRegionEntry(uint32_t offset) const; |
| 834 |
|
| 835 |
const uint8_t* payloadStart() const { |
| 836 |
// The beginning of the payload the beginning of the first region are the |
| 837 |
// same. |
| 838 |
return payloadEnd() - regionOffset(0); |
| 839 |
} |
| 840 |
|
| 841 |
[[nodiscard]] static bool WriteIonTable( |
| 842 |
CompactBufferWriter& writer, const IonEntry::ScriptList& scriptList, |
| 843 |
const NativeToBytecode* start, const NativeToBytecode* end, |
| 844 |
uint32_t* tableOffsetOut, uint32_t* numRegionsOut); |
| 845 |
}; |
| 846 |
|
| 847 |
} // namespace jit |
| 848 |
} // namespace js |
| 849 |
|
| 850 |
#endif /* jit_JitcodeMap_h */ |