-
Notifications
You must be signed in to change notification settings - Fork 5.1k
sys: make SystemError own its strings via OwnedString #35335
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🔴
#[derive(Clone)]onSystemErrorrelies onOwnedString::clone(), but that impl (src/bun_core/string/mod.rs:1259) isSelf(self.0.clone())wherebun_core::Stringis#[derive(Copy)]— a bitwise copy with no WTF refcount bump (its own doc comment is wrong; contrastOwnedStringCell::clone, which uses.dupe_ref()). Both the original and the clone then runOwnedString::drop → String::deref()on the sameWTFStringImpl, decrementing twice for one ref → double-free/UAF. This PR replaces the previously-correct.dupe()(which didptr::read+ref_()) with.clone()atBody.rs:630(reachable viaResponse#clone()on an errored body whoseSystemErrorcarriescreate_format/clone_utf8-backed strings fromFetchTasklet/read_file.rs) andsocket_body.rs:1207. Fix: makeOwnedString::clone()useself.0.dupe_ref()to match its doc andOwnedStringCell.Extended reasoning...
What the bug is
The PR description states: "
jsc::SystemErrornow#[derive(Clone)](OwnedString::clone bumps the refcount)". That premise is false.OwnedString::clone()atsrc/bun_core/string/mod.rs:1258-1260is:where
self.0isbun_core::String, which at line 63-65 is:For a
Copytype, the derivedClone::clone()is a trivial bitwise copy — no refcount bump. The doc comment onOwnedString::cloneis simply wrong. The correct refcount-bumping copy isString::dupe_ref()(line 519:self.ref_(); *self), which is exactly what the siblingOwnedStringCell::clone()at line 2487 uses. MeanwhileOwnedString::drop()(line 1239) callsself.0.deref().So cloning an
OwnedStringproduces two owners of the sameWTFStringImplpointer with only one ref between them; when both drop,deref()runs twice → the second decrement frees (or corrupts) an already-freedWTF::StringImpl.The specific code path this PR breaks
Before this PR,
jsc::SystemErrorhad a hand-written.dupe():which was correct: bitwise copy + one
ref_()per field, so both the original and the copy each held a balanced +1 that their eventual consumers released.This PR deletes
.dupe()/.ref_()and replaces the two call sites with.clone():src/runtime/webcore/Body.rs:630—ValueError::SystemError(e.clone())insideValueError::dupe(), which is called fromBody::Valuecloning (e.g.Response#clone()when the body is inValue::Error(ValueError::SystemError(..))state). TheSystemErrors stored here carry heapWTFStringImpls:FetchTaskletbuildsmessage/path/hostnameviacreate_format/clone_utf8, andread_file.rssetspath: BunString::clone_utf8(...).into().src/runtime/socket/socket_body.rs:1207—PendingSystemError(Some(err.clone())). Currently benign only by accident: every field there isBunString::static_(...)(Tag::StaticZigString, whosederef()is a no-op), but nothing enforces that.Why existing code doesn't prevent it
The comment right above
Body.rs:630says ".clone()on BunString/SystemError already bumps the refcount" — that comment (and the identical claim in the PR description) is exactly the mistaken assumption. The type system has no way to catch this:OwnedStringimplementsClone, so#[derive(Clone)]onSystemErrorcompiles cleanly; the refcount imbalance only manifests at runtime. The new LSan test in this PR checks for leaks on shell error paths, not double-frees on theBody/socket clone paths, so it doesn't cover this.Step-by-step proof
fetch()fails DNS;FetchTaskletbuilds ajsc::SystemErrorwithmessage = BunString::create_format(...)andhostname = BunString::clone_utf8(host)— bothTag::WTFStringImplwith refcount 1 — and stores it asBody::Value::Error(ValueError::SystemError(err))on theResponse.response.clone().Body::Value::clone→ValueError::dupe→ValueError::SystemError(e.clone())(Body.rs:630).e.clone()runs the derived<SystemError as Clone>::clone, which callsOwnedString::clone()on each field →Self(self.0.clone())→ bitwise copy of theString.hostname'sWTFStringImplstill has refcount 1, but is now referenced by twoOwnedStrings.Responseis dropped (or its body error is consumed viato_error_instance, whoseselfthen drops).SystemError::drop→OwnedString::drop→String::deref()onhostname: refcount 1 → 0,WTFStringImplfreed.Responseis dropped. Same path runsderef()on the same freedWTFStringImpl→ use-after-free / double-free.Before this PR, step 3 went through
.dupe()→ref_(), so refcount was 2 after the clone and both drops balanced.Impact
Memory-safety regression (double-free / UAF of
WTF::StringImpl) on a user-reachable path (Response#clone()on any fetch/file-read that errored). Per REVIEW.md's memory-safety section ("reference counts provably balanced on every terminal path — success, error, cancellation, finalize"), this is squarely in the most-blocked category.Fix
Fix the root cause: change
OwnedString::clone()toSelf(self.0.dupe_ref()), matching its own doc comment andOwnedStringCell::clone. That makes#[derive(Clone)]on bothSystemErrorstructs correct with no further changes. (Alternatively, hand-implementClone for jsc::SystemErrorwith per-field.dupe_ref()— but theOwnedStringfix is the right layer, since anything else that starts cloning anOwnedStringwill hit the same trap.)