[llvm] [BOLT] Delay indirect call pointer setup (PR #204229)
Fabian Parzefall via llvm-commits
llvm-commits at lists.llvm.org
Tue Jun 16 11:58:42 PDT 2026
https://github.com/pzfl created https://github.com/llvm/llvm-project/pull/204229
There is a race in the instrumentation runtime during setup. The setup initializes the function pointers for indirect call instrumentation before the indirect call counters array. If the application spawns a background thread through a constructor (as does jemalloc), the background thread has a chance to derefence that uninitialized array pointer. Defer initialization of these function pointers to prevent this race.
>From 1dedd1d0e3cc8ce3306b6ced99df51d29fa86399 Mon Sep 17 00:00:00 2001
From: Fabian Parzefall <parzefall at meta.com>
Date: Tue, 16 Jun 2026 11:19:40 -0700
Subject: [PATCH] [BOLT] Delay indirect call pointer setup
There is a race in the instrumentation runtime during setup. The setup
initializes the function pointers for indirect call instrumentation
before the indirect call counters array. If the application spawns
a background thread through a constructor (as does jemalloc), the
background thread has a chance to derefence that uninitialized array
pointer. Defer initialization of these function pointers to prevent
this race.
---
bolt/runtime/instr.cpp | 8 ++++++--
1 file changed, 6 insertions(+), 2 deletions(-)
diff --git a/bolt/runtime/instr.cpp b/bolt/runtime/instr.cpp
index 3470e391224ba..1e5bddee5793e 100644
--- a/bolt/runtime/instr.cpp
+++ b/bolt/runtime/instr.cpp
@@ -1659,8 +1659,6 @@ extern "C" void __bolt_instr_indirect_tailcall();
/// Initialization code
extern "C" void __attribute((force_align_arg_pointer)) __bolt_instr_setup() {
- __bolt_ind_call_counter_func_pointer = __bolt_instr_indirect_call;
- __bolt_ind_tailcall_counter_func_pointer = __bolt_instr_indirect_tailcall;
TextBaseAddress = getTextBaseAddress();
const uint64_t CountersStart =
@@ -1695,6 +1693,12 @@ extern "C" void __attribute((force_align_arg_pointer)) __bolt_instr_setup() {
GlobalIndCallCounters =
new (*GlobalAlloc, 0) IndirectCallHashTable[__bolt_instr_num_ind_calls];
+ // Set these up after initializing indirect call counters. Otherwise,
+ // background threads spawned through global constructors might try to access
+ // uninitialized counters.
+ __bolt_ind_call_counter_func_pointer = __bolt_instr_indirect_call;
+ __bolt_ind_tailcall_counter_func_pointer = __bolt_instr_indirect_tailcall;
+
if (__bolt_instr_sleep_time != 0) {
// Separate instrumented process to the own process group
if (__bolt_instr_wait_forks)
More information about the llvm-commits
mailing list