docs/heap/roots.md
Roots are a set of well-known objects that are always reachable by the garbage collector. They serve as the entry points for heap traversal during GC and provide fast access to frequently used objects and constants.
V8 categorizes roots into several lists, defined primarily in src/roots/roots.h using X-macros:
ReadOnlySpace.
undefined, null, true, false, the_hole_value.
the_hole_value, hash_table_hole_value, uninitialized_value) as sentinels in different places. Their bodies are often deliberately unmapped or placed in protected memory so that attempting to read them (for example, as part of a type confusion exploit) will cause a crash, preventing security vulnerabilities.ByteArrayMap, FixedArrayMap, SymbolMap, etc.EmptyFixedArray, EmptyPropertyArray, etc.ArraySpeciesProtector).RootsTable: This class (defined in src/roots/roots.h) encapsulates the array of all roots. It provides accessors to get handles to specific roots.ReadOnlyRoots: A lighter-weight class that provides access only to the read-only roots. It can be used even when the full Isolate is not available (e.g., on background threads using LocalIsolate) because read-only roots are shared or at least stable.When v8_enable_static_roots = true is enabled (supported under pointer compression, since then read-only pointers are cage-relative), V8 uses a fixed layout for the read-only heap to achieve compile-time constant addresses for core objects.
mksnapshot tool deterministically creates a bit-identical read-only heap.libv8), V8 needs to know them at compile time.
mksnapshot (or specialized generator tools) is run to generate the addresses and write them to configuration-specific headers (e.g., src/roots/static-roots-intl-wasm.h), which are included by src/roots/static-roots.h based on build flags.StaticReadOnlyRoot::kUndefinedValue).tools/dev/gen-static-roots.py script.undefined from the roots table, V8 can directly compare the compressed pointer to a constant (e.g., checking if it equals StaticReadOnlyRoot::kUndefinedValue).