Skip to content

Windows runtime: a DLL's copy leaves its crash handler installed after FreeLibrary #236

Closed

Description

@SizzleUnrlsd

Summary

With CT_BACKTRACE set, each copy of the runtime installs its crash handler for the whole process when its module initializes:

The copy in a DLL loaded after the executable thus replaces the executable's handler with its own, and nothing restores the previous one. When FreeLibrary unloads that DLL, its release (src/runtime/windows/ct_runtime_dbghelp.cpp L66-L96) closes its DbgHelp session but leaves the process's unhandled-exception filter pointing into the DLL's unmapped image. The next exception that nothing handles then calls into that address. Whatever has been mapped there since runs, or the call faults in unmapped memory, instead of a crash handler that prints a backtrace.

Found by reading the code during the review of #235 (for #225); not reproduced.

Expected

After FreeLibrary unloads a DLL whose copy installed the crash handler, an exception that nothing handles is reported either by a handler that is still loaded, such as the executable's, or by the system's default handling.

Possible directions

Possible test

A death test that sets CT_BACKTRACE for its child, as the tests of #235 do:

  1. Load ct_runtime_library, a DLL that links the runtime.
  2. Unload it.
  3. Raise an exception that nothing handles, on a thread of its own.

The executable copy's backtrace should be printed, and the process should end with the exception's code.

Activity

  1. 24 remaining items

  2. SizzleUnrlsd commented on Nov 16, 2026

    @SizzleUnrlsd
    ContributorAuthor

    Fixed by #241, merged on 2026-11-04 as 09df200: only the executable's copy of the runtime installs the crash handler. The copy in a DLL installs none and leaves the process's filter alone, so nothing is left behind when FreeLibrary unloads the DLL.

    Validation, from #241, in the four Windows jobs of LLVM versions (LLVM 19, 20, 22, 23):

    • on main, 232/238 in each job: the tests of a crash reported after the DLL has unloaded fail, with no backtrace (run 41722488438);
    • with the fix, 238/238 in each job, and the 15 jobs of LLVM versions, Build and the other checks pass (run 41759624900).

    One limit is accepted and documented in the README ("Crash handler on Windows"): a program that does not link the runtime gets no crash backtrace from an instrumented DLL. The fix is in the changelog of 0.13.0, which #269 prepares.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions