Explicit panic instead of silent memory corruption #1377
Merged
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.
Due to the automatic entry and exit behavior of Isolate upon creation and drop, it is crucial to ensure that v8::OwnedIsolate instances are dropped in the reverse order of their creation. Dropping them in the incorrect order can result in the corruption of the thread-local stack managed by v8, leading to memory corruption and potential segfaults.
This introduces a check to verify the
this == Isolate::GetCurrent()
requirement before invoking the exit function. If the requirement is not met, a clean panic is triggered to provide explicit error handling instead of allowing silent memory corruption.The following program demonstrates how a segfault can be currently triggered using only the safe Rust API:
An alternative approach would be to strictly disallow the creation of multiple v8::OwnedIsolate instances on the same thread to ensure immediate failure. However, some tests (and probably some applications) currently rely on this behavior.
Another option is to remove the automatic enter/exit functionality and instead introduce a safe API that explicitly requires entering and exiting the Isolate. However, implementing this change would result in a significant break in the API.