From 4095af59ec94cd16794a9b51cdc7ce7880195a17 Mon Sep 17 00:00:00 2001 From: Kai Law Date: Mon, 30 Dec 2024 16:02:57 +0800 Subject: [PATCH] [compiler-rt] Fix race condition when allocating object For following program compiled with `-femulated-tls`, there is chance the first allocated object is different from other allocation. ``` thread_local int id = 0; std::mutex print_mutex; int main() { // std::cout << "thread " << 0 << ": " << &id << std::endl; std::vector group; for (int i = 1; i < 8; ++i) { group.emplace_back([i] { std::unique_lock _(print_mutex); std::cout << "thread " << i << ": " << &id << std::endl; }); } for (auto &t : group) t.join(); return 0; } Output: thread 1: 0x7f57040010a8 thread 2: 0x7f56fc000c98 thread 3: 0x7f56fc000c98 thread 5: 0x7f56fc000c98 thread 4: 0x7f56fc000c98 thread 6: 0x7f56fc000c98 thread 7: 0x7f56fc000c98 ``` --- compiler-rt/lib/builtins/emutls.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/compiler-rt/lib/builtins/emutls.c b/compiler-rt/lib/builtins/emutls.c index 390ffb25f6cf0..ca18da2fffe6f 100644 --- a/compiler-rt/lib/builtins/emutls.c +++ b/compiler-rt/lib/builtins/emutls.c @@ -392,8 +392,12 @@ __attribute__((visibility("default"), weak)) void *__emutls_get_address(__emutls_control *control) { uintptr_t index = emutls_get_index(control); emutls_address_array *array = emutls_get_address_array(index--); - if (array->data[index] == NULL) - array->data[index] = emutls_allocate_object(control); + if (array->data[index] == NULL) { + emutls_lock(); + if (array->data[index] == NULL) + array->data[index] = emutls_allocate_object(control); + emutls_unlock(); + } return array->data[index]; }