Repository navigation
Fix memory lifetime bug in async array parameter handling for JS API - #8125
Nikolaj Bjorner (NikolajBjorner) merged 5 commits into
Conversation
Co-authored-by: NikolajBjorner <3085284+NikolajBjorner@users.noreply.github.com>
Co-authored-by: NikolajBjorner <3085284+NikolajBjorner@users.noreply.github.com>
Co-authored-by: NikolajBjorner <3085284+NikolajBjorner@users.noreply.github.com>
|
Kevin Gibbons (@bakkot) - FYI, copilot suggests that there is a life-time issue with the async functions and array parameters. This is the bug! The async wrapper needs to handle array parameters specially to ensure the memory stays alive until the thread completes. Let me verify this theory by checking if mk_and or mk_or are async: View src/api/js/scripts/async-fns.ts Now I've found the root cause: The async wrapper doesn't handle array parameters correctly - it lets emscripten free the temporary memory before the async thread actually uses it. |
| arrayCopyNames.push(copyName); | ||
|
|
||
| // Allocate and copy with null check | ||
| arrayCopies.push(`${ptrType} ${copyName} = (${ptrType})malloc(sizeof(${p.cType}) * ${sizeParam.name});`); |
There was a problem hiding this comment.
Nit: as a matter of style, just use a single multiline template literal here rather than pushing individual lines to this array.
|
Sounds totally plausible. |
though, the current "fix" by copilot looks fishy. Can't we just call the _flatten functionality within the closure used by the async? |
|
Copilot - could you instead use code like this to deal with the issue? |
... I want to make sure I understand your suggestion correctly. The current high-level code creates the const assumptions = _flattenArgs(exprs).map(expr => expr.ast);
const result = await asyncMutex.runExclusive(() =>
check(Z3.solver_check_assumptions(contextPtr, this.ptr, assumptions))
);Your suggestion moves it inside: const result = await asyncMutex.runExclusive(() => {
const assumptions = _flattenArgs(exprs).map(expr => expr.ast);
return check(Z3.solver_check_assumptions(contextPtr, this.ptr, assumptions));
});However, I believe the memory issue occurs at a lower level in the generated wrapper. The problem is that Moving the array creation inside Could you clarify what you're suggesting? Are you proposing:
I want to make sure I implement the right solution! |
|
Copilot OK got it |
|
Test failure says it's trying to use It's been a while since I touched this code but the explanation and fix sound right to me; if there's a test which fails before this fix and passes after, and there's no obvious leaks (I don't see any) I think it's probably good. |
|
Copilot - add the missing method to extract unsatCore to Solver to fix failing build of js bindings |
Co-authored-by: NikolajBjorner <3085284+NikolajBjorner@users.noreply.github.com>
7a35caa
into
master
…8125) * Initial plan * Fix async array parameter handling in JS API wrappers Co-authored-by: NikolajBjorner <3085284+NikolajBjorner@users.noreply.github.com> * Add test for solver.check() with assumptions Co-authored-by: NikolajBjorner <3085284+NikolajBjorner@users.noreply.github.com> * Address code review feedback: add null checks and improve readability Co-authored-by: NikolajBjorner <3085284+NikolajBjorner@users.noreply.github.com> * Add unsatCore() method to Solver class Co-authored-by: NikolajBjorner <3085284+NikolajBjorner@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: NikolajBjorner <3085284+NikolajBjorner@users.noreply.github.com>
Async functions with array parameters (e.g.,
solver.check()with assumptions) crashed due to use-after-free. Emscripten'sccallwith'array'type frees temporary memory immediately after the wrapper returns, but async wrappers spawn threads that access this memory later.Changes
TypeScript wrapper generation (
make-ts-wrapper.ts)Mod._mallocfor async array parametersnumberinstead of using ccall's automatic array handlingC++ wrapper generation (
make-cc-wrapper.ts)malloc/memcpybefore thread spawnTest coverage (
high-level.test.ts)solver.check()with multiple assumptionsAPI completion (
types.tsandhigh-level.ts)unsatCore()method to Solver class to retrieve unsat coresAstVector<Name, Bool<Name>>containing the Boolean expressions in the unsat coreAffects
Z3_solver_check_assumptionsandZ3_optimize_check.Original prompt
✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.