fix(neon): Tag Root
with an instance id and verify before using
#847
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Overview
Root
are Neon's mechanism for holding persistent references to values on the JavaScript heap. They are bothSend
andSync
to allow holding references across Rust threads. However, the current implementation has a soundness hole.Resolves #843
Problem
When using Node worker threads, there may be multiple JavaScript runtimes present in the process. As shown in #843, it is possible to use a
Root
with aContext
from the wrong runtime and cause a crash/undefined behavior.Solution
This PR resolves the issue by panicking when attempting to dereference from the wrong module instance. In the future, we may want to add
try_
variants to theRoot
methods to allow error handling without apanic
. However, in most cases, this will be a logic error that applications cannot recover.Approach
On Node-API 6+, we use instance data to store a unique identifier initialized from a global counter. Each
Root
stores the identifier for comparing later when being used.On previous versions, the
ThreadId
is used. This may not be as robust and is sensitive to V8 implementation details.