[compiler-rt] [compiler-rt][ORC][COFF] Run atexit callbacks in LIFO order (PR #214053)
via llvm-commits
llvm-commits at lists.llvm.org
Tue Aug 4 12:52:47 PDT 2026
https://github.com/Sp0tless created https://github.com/llvm/llvm-project/pull/214053
The COFF runtime stores atexit callbacks in registration order, but executes them in the same order during JITDylib deinitialization.
Run callbacks from the back of the vector so that they execute in reverse registration order, and add a regression test.
This was found while investigating #213568.
Assisted-by: OpenAI Codex
>From e373de289a195c88f446d75c86db68e593fa9e9b Mon Sep 17 00:00:00 2001
From: Sp0tless <Sp0tless at users.noreply.github.com>
Date: Wed, 5 Aug 2026 03:14:22 +0800
Subject: [PATCH] [compiler-rt][ORC][COFF] Run atexit callbacks in LIFO order
The COFF runtime stores atexit callbacks in registration order, but executes them in the same order during JITDylib deinitialization.
Run callbacks from the back of the vector so that they execute in reverse registration order, and add a regression test.
Assisted-by: OpenAI Codex
---
compiler-rt/lib/orc/coff_platform.cpp | 9 ++++---
.../TestCases/Windows/x86-64/atexit-order.c | 25 +++++++++++++++++++
2 files changed, 31 insertions(+), 3 deletions(-)
create mode 100644 compiler-rt/test/orc/TestCases/Windows/x86-64/atexit-order.c
diff --git a/compiler-rt/lib/orc/coff_platform.cpp b/compiler-rt/lib/orc/coff_platform.cpp
index 4cce20e1323f3..23f87c7d710c0 100644
--- a/compiler-rt/lib/orc/coff_platform.cpp
+++ b/compiler-rt/lib/orc/coff_platform.cpp
@@ -483,10 +483,13 @@ Error COFFPlatformRuntimeState::dlcloseDeinitialize(JITDylibState &JDS) {
JDS.Name.c_str());
});
- // Run atexits
- for (auto AtExit : JDS.AtExits)
+ // Run atexits in reverse registration order.
+ auto AtExits = std::move(JDS.AtExits);
+ while (!AtExits.empty()) {
+ auto AtExit = AtExits.back();
+ AtExits.pop_back();
AtExit();
- JDS.AtExits.clear();
+ }
// Run static terminators.
JDS.CPreTermSection.RunAllNewAndFlush();
diff --git a/compiler-rt/test/orc/TestCases/Windows/x86-64/atexit-order.c b/compiler-rt/test/orc/TestCases/Windows/x86-64/atexit-order.c
new file mode 100644
index 0000000000000..af6156fbb3895
--- /dev/null
+++ b/compiler-rt/test/orc/TestCases/Windows/x86-64/atexit-order.c
@@ -0,0 +1,25 @@
+// RUN: %clang_cl -MD -c -o %t %s
+// RUN: %llvm_jitlink %t 2>&1 | FileCheck %s
+// CHECK: Entering main
+// CHECK-NEXT: Second
+// CHECK-NEXT: First
+
+#include <stdio.h>
+#include <stdlib.h>
+
+void first(void) {
+ printf("First\n");
+ fflush(stdout);
+}
+
+void second(void) {
+ printf("Second\n");
+ fflush(stdout);
+}
+
+int main(int argc, char *argv[]) {
+ atexit(first);
+ atexit(second);
+ printf("Entering main\n");
+ return 0;
+}
More information about the llvm-commits
mailing list