S3Client: don't double-free path on error after store takes ownership - #30011
Closed
robobun wants to merge 2 commits into
Closed
S3Client: don't double-free path on error after store takes ownership#30011robobun wants to merge 2 commits into
robobun wants to merge 2 commits into
Conversation
Blob.Store.initS3*() takes ownership of the PathLike it is given: for a .slice_with_underlying_string it calls toThreadSafe(), which for a non-atomic StringImpl creates an isolated copy and derefs the original. The caller's (shallow) copy of the PathLike is left pointing at an impl whose ref was just consumed. S3Client/S3File methods all had `errdefer path.deinit()` that would run after the blob store had already taken ownership, deref'ing the same WTFStringImpl a second time and tripping the hasAtLeastOneRef() assertion in debug builds (use-after-free in release). Make constructS3FileWithS3Credentials[AndOptions] unconditionally own `path` (releasing it if getCredentialsWithOptions throws before the store is created) and drop the errdefer from callers.
Collaborator
Author
Contributor
|
This PR may be a duplicate of:
🤖 Generated with Claude Code |
Contributor
|
Caution Review failedPull request was closed or merged during review WalkthroughRemoves automatic Changes
🚥 Pre-merge checks | ✅ 4✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. Review rate limit: 4/5 reviews remaining, refill in 12 minutes. Comment |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
What does this PR do?
Fixes a debug-assertion crash / use-after-free in
S3ClientandS3Filemethods when a JS exception is thrown after the internal S3 blob store has been created.Found by Fuzzilli (fingerprint
f824199979cdeedf).Root cause
Blob.Store.initS3*()takes ownership of thePathLikeit is given: for a.slice_with_underlying_stringit callstoThreadSafe(), which — when the underlyingWTFStringImplis not already thread-safe (not an atom) — creates an isolated copy and derefs the original. The caller's (shallow) copy of thePathLikeis left pointing at an impl whose ref was just consumed.All
S3Client/ staticS3Fileentry points haderrdefer path.deinit()that continued to fire on any error after the blob store had already taken ownership (e.g. the data argument'stoPrimitivethrowing insideBlob.writeFileInternal, or an optionstypegetter throwing). That deref'd the sameWTFStringImpla second time and trippedhasAtLeastOneRef()in debug builds.The misleading crash backtrace (
string.String.fromJSatstring.zig:543) is what Bun's panic handler printed, but the actual trap was inWTFStringImpl.derefcalled from theerrdeferduring unwind:Fix
Make
constructS3FileWithS3Credentials/constructS3FileWithS3CredentialsAndOptionsunconditionally ownpath: ifgetCredentialsWithOptionsthrows before the store is created, they now releasepaththemselves. Callers drop theirerrdefer path.deinit()(with explicit cleanup only for error paths before the constructor is reached, e.g. missingdataargument inwrite).How did you verify your code works?
Deterministic repro that crashed before and passes after:
Added
test/js/bun/s3/s3-path-error-double-free.test.tscovering instance + staticwrite/presign/exists/size/stat/unlink/filewith exceptions thrown both before and after the store is created.