Heap-allocate patched interface vtable so it survives in a global __cctor#251
Merged
Merged
Conversation
…ctor
A top-level binding whose declared type is an interface, initialized from an
object literal that has a method (const c: Counter = { count: 0, inc(){...} }),
crashed the JIT with 0xC0000005 on the first field/method access.
Root cause: casting the (boxed) object literal to the interface builds a
per-object vtable patched with the method's function pointer
(mlirGenCreateInterfaceVTableForObject). That vtable was a stack VariableOp
(alloca). For a local binding the alloca outlives its uses, but a global
binding's initializer lowers to a __cctor function -- the alloca dangled once
the __cctor returned, leaving the interface's vtable pointer referencing freed
stack memory.
Fix: heap-allocate the patched vtable (NewOp + StoreOp + CastOp), putting it on
the same footing as the object it describes (already NewOp-allocated). The
method-less interface path is unaffected -- it points NewInterface directly at
the shared global vtable, no per-object copy.
Regression test 00interface_global_method.ts (also exercises two independent
same-shape globals to confirm their vtables don't alias). Full suite green:
354/354 JIT + 358/358 AOT.
The cross-module (-shared) method-bearing interface case is a separate,
still-open issue in the shared-lib symbol-resolution subsystem, documented in
docs/object-literal-boxing-design.md §6.
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
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.
Summary
Fixes a JIT crash (0xC0000005) when a top-level binding whose declared type is an interface is initialized from an object literal that has a method:
Root cause
Casting the (boxed) object literal to the interface builds a per-object vtable patched with the method's function pointer (
mlirGenCreateInterfaceVTableForObject). That vtable was a stackVariableOp(alloca). For a local binding the alloca outlives its uses, but a global binding's initializer lowers to a__cctorfunction — so the alloca dangled once__cctorreturned, leaving the interface's stored vtable pointer referencing freed stack memory. The crash then happened on the first field/method access through the interface.Method-less interfaces (e.g.
Point) were always fine: they pointNewInterfacedirectly at the shared global vtable, with no per-object patched copy.Fix
Heap-allocate the patched vtable (
NewOp+StoreOp+CastOp), putting it on the same footing as the object it describes (alreadyNewOp-allocated). One localized change inMLIRGenInterfaces.cpp.Test plan
00interface_global_method.ts(registered compile + jit). Also exercises two independent same-shape globals to confirm their per-object vtables don't alias.ctest -R "^test-jit-"/"^test-compile-"-j8).Not covered (separate, still-open)
The cross-module (
-shared) method-bearing interface case still crashes (null function-pointer call in the importer's__mlir_gctors) — a distinct issue in the shared-lib DLL-load /gctors-as-methodsymbol-resolution subsystem, not the vtable-lifetime bug fixed here. Documented indocs/object-literal-boxing-design.md§6 for follow-up.🤖 Generated with Claude Code