Skip to content

node_mksnapshot.cc: code cache generated based on temporary RO heap layout #47636

@schuay

Description

@schuay

Version

No response

Platform

No response

Subsystem

No response

What steps will reproduce the bug?

No response

How often does it reproduce? Is there a required condition?

No response

What is the expected behavior? Why is that the expected behavior?

No response

What do you see instead?

fbe1478 moved code cache generation into node_mksnapshot, but I suspect this process is (and has always been) unsupported by V8. It has just worked somewhat by accident til now.

A hidden requirement of V8's code serializer is that it runs inside an Isolate that has been deserialized from the final snapshot, i.e. the same snapshot that is used in the node binary itself. Why? Because SerializeReadOnlyObjectReference encodes the RO object ref as {chunk index, offset}, but RO space layout can and does change between RO space serialization- and deserialization-time (and thus the serialized encoded RO ref may be invalid).

I'm hitting this locally (based on a V8 change I'm currently working on), note how the offset of the "load" string changes:
in node_mksnapshot: 0x0a78926543d9 <String[4]: #load> offset 82904
in node 0x32bf83c14789 <String[4]: #load> offset 83848.

This results in the code-deserializer creating a pointer pointing at a random location inside RO space.

We could fix this in several ways,

  1. change the snapshot-generation process in Node s.t. code serialization happens after heap serialization,
  2. disable node_use_node_code_cache
  3. (long-term vision) change V8 to keep RO space layout fixed, or
  4. (pragmatic hack) have Node pass a parameter to ScriptCompiler::CompileFunction to enable slow-but-safe RO object serialization (although I hesitate to expose this quirk through the API)

@joyeecheung wdyt?

Additional information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions