fix(module): prevent double-free when initializing native module exports#689
fix(module): prevent double-free when initializing native module exports#689
Conversation
QuickJS JS_SetModuleExport takes ownership of the JSValue passed in. We were passing Go-owned values directly, which could later be freed again on the Go side, leading to intermittent ref_count assertion failures during JS_FreeRuntime (issue #688). After handing an export value to JS_SetModuleExport, invalidate the Go Value so subsequent Free() becomes a no-op. Also release the duplicated module private value and propagate export init failures. Add a regression stress test that runs the minimal repro scenario 50 times.
Summary of ChangesHello @buke, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request addresses a critical memory management bug where native module exports could lead to double-free errors and runtime crashes in QuickJS. The changes ensure proper ownership transfer of JavaScript values between Go and QuickJS, preventing resource mismanagement, and include a robust regression test to safeguard against future recurrences of this issue. Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #689 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 11 11
Lines 2343 2349 +6
=========================================
+ Hits 2343 2349 +6
Continue to review full report in Codecov by Sentry.
🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
Code Review
This pull request correctly addresses a double-free vulnerability by invalidating the Go-side Value after its ownership is transferred to QuickJS's JS_SetModuleExport. The changes also improve error propagation and add a valuable regression stress test to prevent this issue from recurring. My review includes one critical suggestion to prevent a resource leak on the error path within the module initialization proxy.
Add an error-branch test that forces a mismatch between declared module exports and init-time exports, making JS_SetModuleExport return < 0 and exercising the previously uncovered rc<0 path. Also free the JSValue returned by JS_GetModulePrivateValue in getContextAndModuleBuilder, and make module init failures throw a JS exception with a clear error message.
QuickJS JS_SetModuleExport takes ownership of the JSValue passed in. We were passing Go-owned values directly, which could later be freed again on the Go side, leading to intermittent ref_count assertion failures during JS_FreeRuntime (issue #688).
After handing an export value to JS_SetModuleExport, invalidate the Go Value so subsequent Free() becomes a no-op. Also release the duplicated module private value and propagate export init failures.
Add a regression stress test that runs the minimal repro scenario 50 times.