Skip to content

libclang.dll crashes process on exit when unloaded #154361

@mrexodia

Description

@mrexodia

If you unload the latest libclang.dll from https://github.com/llvm/llvm-project/releases/download/llvmorg-21.1.0-rc3/LLVM-21.1.0-rc3-win64.exe (also happens for 20.x) the process crashes on exit. Minimal reproduction:

#include <Windows.h> int main() { auto libclang = LoadLibraryW(L"libclang.dll"); FreeLibrary(libclang); } 

Here is a screenshot of a crash on Windows Sandbox (Windows 10 Pro 22H2 19045.6216), nothing installed (notably no MSVC runtime at all):

Image

The crash is a DEP violation trying to execute a nonexistent region. This happens because the atexit function passed to FlsAlloc ceases to exist when you unload the DLL. Cross referencing this function on module load shows the following code:

Image

You are expected to call FlsFree for all the slots during DllMain with the DLL_PROCESS_DETACH reason. Searching the LLVM codebase for FlsAlloc shows two occurrences:

Looking at the disassembly around the FlsAlloc I see OpenProcessToken, which matches rpmalloc. There is an FlsFree in there, but it does not appear to be called correctly on DLL_PROCESS_DETACH.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions