-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Fix use-after-free in Bun.Transpiler async transform() errors
#30263
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -503,6 +503,9 @@ pub const TransformTask = struct { | |||||||||||||||||
| this.transpiler.setAllocator(allocator); | ||||||||||||||||||
| this.transpiler.setLog(&this.log); | ||||||||||||||||||
| this.log.msgs.allocator = bun.default_allocator; | ||||||||||||||||||
| defer for (this.log.msgs.items) |*msg| { | ||||||||||||||||||
| msg.* = bun.handleOom(msg.clone(bun.default_allocator)); | ||||||||||||||||||
| }; | ||||||||||||||||||
|
Comment on lines
+506
to
+508
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done — added
Comment on lines
+506
to
+508
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟡 Minor: the Extended reasoning...What the bug isThe new defer for (this.log.msgs.items) |*msg| {
msg.* = bun.handleOom(msg.clone(bun.default_allocator));
};
The matching cleanup added in for (this.log.msgs.items) |*msg| msg.deinit(bun.default_allocator);calls // don't really know what's safe to deinit here!
pub fn deinit(_: *Location, _: std.mem.Allocator) void {}So Code path that triggers it
Why existing code doesn't prevent it
ImpactPer failing async Worth noting: the same Step-by-step proofFor one call to
Four small allocations leak per failed transform. How to fixEither locally in for (this.log.msgs.items) |*msg| {
if (msg.data.location) |*loc| {
bun.default_allocator.free(loc.file);
if (loc.line_text) |lt| bun.default_allocator.free(lt);
}
for (msg.notes) |*note| if (note.location) |*loc| {
bun.default_allocator.free(loc.file);
if (loc.line_text) |lt| bun.default_allocator.free(lt);
};
msg.deinit(bun.default_allocator);
}…or, better long-term, make |
||||||||||||||||||
|
|
||||||||||||||||||
| const jsx = if (this.tsconfig != null) | ||||||||||||||||||
| this.tsconfig.?.mergeJSX(this.transpiler.options.jsx) | ||||||||||||||||||
|
|
@@ -587,6 +590,7 @@ pub const TransformTask = struct { | |||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| pub fn deinit(this: *TransformTask) void { | ||||||||||||||||||
| for (this.log.msgs.items) |*msg| msg.deinit(bun.default_allocator); | ||||||||||||||||||
| this.log.deinit(); | ||||||||||||||||||
| this.input_code.deinitAndUnprotect(); | ||||||||||||||||||
| this.output_code.deref(); | ||||||||||||||||||
|
|
||||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| import { expect, test } from "bun:test"; | ||
|
|
||
| test("concurrent async transform() rejections do not use-after-free", async () => { | ||
| const transpiler = new Bun.Transpiler({ loader: "tsx" }); | ||
|
|
||
| const inputs = [ | ||
| "const {a, a} = b", | ||
| "class X { @invalid }", | ||
| "const x: string = ;", | ||
| "@#$%^ invalid syntax !!!", | ||
| "function (", | ||
| ]; | ||
|
|
||
| const promises: Promise<unknown>[] = []; | ||
| for (let i = 0; i < 20; i++) { | ||
| for (const input of inputs) { | ||
| promises.push(transpiler.transform(input).catch(e => e)); | ||
| } | ||
| } | ||
|
|
||
| const results = await Promise.all(promises); | ||
| expect(results).toHaveLength(20 * inputs.length); | ||
| for (const result of results) { | ||
| expect(result).toBeDefined(); | ||
| expect(typeof result).toBe("object"); | ||
| } | ||
| }); | ||
|
|
||
| test("async transform() error preserves message and notes", async () => { | ||
| const transpiler = new Bun.Transpiler({ loader: "tsx" }); | ||
|
|
||
| const errors = await Promise.all( | ||
| Array.from({ length: 8 }, () => transpiler.transform("const {a, a} = b").catch(e => e)), | ||
| ); | ||
|
|
||
| for (const err of errors) { | ||
| expect(err.message).toBe('"a" has already been declared'); | ||
| expect(err.position?.file).toBe("input.tsx"); | ||
| expect(err.position?.lineText).toBe("const {a, a} = b"); | ||
| expect(err.notes).toHaveLength(1); | ||
| expect(err.notes[0].message).toBe('"a" was originally declared here'); | ||
| expect(err.notes[0].position?.lineText).toBe("const {a, a} = b"); | ||
| } | ||
| }); |
Uh oh!
There was an error while loading. Please reload this page.