Skip to content

[fix] Fix the extern early return in HXTAllocation - #1419

Open
dimensionscape wants to merge 1 commit into
HaxeFoundation:masterfrom
dimensionscape:fix/telemetry-alloc-unlock
Open

dimensionscape wants to merge 1 commit into
HaxeFoundation:masterfrom
dimensionscape:fix/telemetry-alloc-unlock

Conversation

@dimensionscape

Copy link
Copy Markdown
Contributor

HXTAllocation skips allocations made inside a CFFI call (an extern stack frame), but before returning it calls alloc_mutex.unlock() on a mutex it hasn’t locked yet; the lock is taken a few lines later. Unlocking a mutex the thread doesn’t own is undefined behaviour. On Windows it leaves alloc_mutex held, so the next thread that tracks an allocation or stashes telemetry blocks forever. The bug dates back to the original telemetry merge (2015); #1298 only changed Unlock() to unlock().

The same check also calls getCurrentStackFrame(), which is mStackFrames.back() and undefined when no frame is on the stack.

This PR drops the stray unlock and checks the stack depth first. Builds with HXCPP_PROFILE_EXTERNS are unaffected.

Test: test/telemetry/TestAllocations.hx allocates inside an extern frame on one thread (the same frame cpp.Lib.load primitives push), then stashes on another. Without the fix the telemetry tests deadlock on Windows; with it they pass in release and debug.

HXTAllocation skips allocations made inside a CFFI call, but before
returning it unlocked alloc_mutex, which it has not locked yet (the lock
is taken a few lines further down). Unlocking a mutex the thread does
not own is undefined behaviour. On Windows it leaves the mutex held, and
the next thread that tracks an allocation or stashes blocks forever.

The same check also read getCurrentStackFrame(), which is
mStackFrames.back() and undefined while no frame is on the stack.

Drop the stray unlock and check the stack depth first. The new
telemetry test allocates inside an extern frame on one thread, then
stashes on another; without the fix it deadlocks on Windows.
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.

1 participant