Skip to content

Fix #636: [Bug] Heap-use-after-free in chaiscript::Type_Info::bare_equal (Race Condition during eval_error formatting)#653

Closed
leftibot wants to merge 1 commit into
ChaiScript:developfrom
leftibot:fix/issue-636-bug-heap-use-after-free-in-chaiscript-ty
Closed

Fix #636: [Bug] Heap-use-after-free in chaiscript::Type_Info::bare_equal (Race Condition during eval_error formatting)#653
leftibot wants to merge 1 commit into
ChaiScript:developfrom
leftibot:fix/issue-636-bug-heap-use-after-free-in-chaiscript-ty

Conversation

@leftibot
Copy link
Copy Markdown
Contributor

Automated fix by @leftibot.

What changed

Fix #636: Prevent heap-use-after-free in async threads during engine destruction
The Dispatch_Engine member destruction order destroyed m_state (containing
type maps) before m_stack_holder (containing async futures), allowing async
threads to access freed type map memory during eval_error formatting. The fix
moves async() registration from the stdlib into ChaiScript_Basic with thread
tracking, and adds a Dispatch_Engine destructor that joins all tracked async
threads before m_state is destroyed.
Co-Authored-By: Claude Opus 4.6 (1M context) noreply@anthropic.com

Files

 CMakeLists.txt                                    |  4 ++
 include/chaiscript/chaiscript_stdlib.hpp          |  5 +--
 include/chaiscript/dispatchkit/dispatchkit.hpp    | 44 +++++++++++++++++++
 include/chaiscript/language/chaiscript_engine.hpp | 24 +++++++++++
 unittests/async_engine_lifetime_test.cpp          | 51 +++++++++++++++++++++++
 5 files changed, 125 insertions(+), 3 deletions(-)

Closes #636

Triggered by @lefticus.

…ng engine destruction

The Dispatch_Engine member destruction order destroyed m_state (containing
type maps) before m_stack_holder (containing async futures), allowing async
threads to access freed type map memory during eval_error formatting. The fix
moves async() registration from the stdlib into ChaiScript_Basic with thread
tracking, and adds a Dispatch_Engine destructor that joins all tracked async
threads before m_state is destroyed.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
leftibot added a commit to leftibot/ChaiScript that referenced this pull request Apr 11, 2026
…revent heap-use-after-free

Issues ChaiScript#632 and ChaiScript#636 (PRs ChaiScript#651 and ChaiScript#653) both stem from the same root cause: async
threads spawned via async() can outlive the Dispatch_Engine, accessing shared state
(global objects map, type maps) after it has been destroyed. The fix moves async()
registration from the stdlib module into ChaiScript_Basic, where spawned threads are
tracked via Dispatch_Engine. The engine's destructor now joins all outstanding async
threads before destroying shared data structures.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@lefticus
Copy link
Copy Markdown
Member

closing in favor of #655

@lefticus lefticus closed this Apr 11, 2026
lefticus pushed a commit that referenced this pull request Apr 11, 2026
…656)

* Fix #655: Join async threads before engine destruction to prevent heap-use-after-free

Issues #632 and #636 (PRs #651 and #653) both stem from the same root cause: async
threads spawned via async() can outlive the Dispatch_Engine, accessing shared state
(global objects map, type maps) after it has been destroyed. The fix moves async()
registration from the stdlib module into ChaiScript_Basic, where spawned threads are
tracked via Dispatch_Engine. The engine's destructor now joins all outstanding async
threads before destroying shared data structures.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>

* Address review: follow rule of 5, explicitly default move operations

Requested by @lefticus in PR #656 review.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>

---------

Co-authored-by: leftibot <leftibot@users.noreply.github.com>
Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] Heap-use-after-free in chaiscript::Type_Info::bare_equal (Race Condition during eval_error formatting)

2 participants