[PATCH] D147413: [sanitizer][win] Fix `Atexit()` on MinGW asan_dynamic runtime

Alvin Wong via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Apr 3 02:08:10 PDT 2023


alvinhochun added inline comments.


================
Comment at: compiler-rt/lib/sanitizer_common/sanitizer_win.cpp:731
+// On this environment, __asan_init is called during global constructor phase,
+// way after calling the .CRT$XID initializer.
+static int (*volatile queueOrCallAtExit)(void (*)(void)) = &queueAtexit;
----------------
mstorsjo wrote:
> So... is the root issue that C++ global constructors are executed after the `.CRT$XID` initializer in MSVC environments, but before in mingw environments? I believe they're supposed to be compatible, so if there's such a discrepancy, maybe we should also consider trying to fix it in mingw-w64 crt?
No, C++ global constructors are called after `.CRT$XID` initializer on MinGW too. This is what I got from stepping through it:

1. `RunAtexit` is called quite early.
2. The `AsanInitializer` constructor is called quite late by `__main` -> `__do_global_ctors` (`gccmain.c`). This constructor calls `AsanInitFromRtl` -> `AsanInitInternal` -> `Atexit(asan_atexit)`.

I do suspect on MSVC target there is something else other than the C++ global constructor that calls `__asan_init` early. Perhaps it may have something to do with linking with static vs dynamic CRT? (Static asan has other initializers.)


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D147413/new/

https://reviews.llvm.org/D147413



More information about the llvm-commits mailing list