From 4a7c3a2607fbb9e955b055070f4161b14ba2eaad Mon Sep 17 00:00:00 2001 From: Philipp Knechtges Date: Tue, 24 Nov 2020 21:28:25 +0100 Subject: [PATCH] fix link-time ordering condition This fixes a segfault error in cases where the linking order of compilation unit varies. Reason behind the segfault is that one global variable in one compilation unit depends on another global variable in another compilation unit, but there is no guarantee that this other compilation unit is initialized first. The fix forces a reinitialization at the first invocation of the library. Change-Id: I1428592c6898bca13a330c4588941de260ff0370 [ROCm/ROCR-Runtime commit: d220e16000df4502aa3102dbcc99346fd8e16187] --- .../hsa-runtime/core/common/hsa_table_interface.cpp | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/projects/rocr-runtime/runtime/hsa-runtime/core/common/hsa_table_interface.cpp b/projects/rocr-runtime/runtime/hsa-runtime/core/common/hsa_table_interface.cpp index 3802ae9b49..593ccb389e 100644 --- a/projects/rocr-runtime/runtime/hsa-runtime/core/common/hsa_table_interface.cpp +++ b/projects/rocr-runtime/runtime/hsa-runtime/core/common/hsa_table_interface.cpp @@ -58,7 +58,15 @@ const HsaApiTable* hsa_table_interface_get_table() { } // Pass through stub functions -hsa_status_t HSA_API hsa_init() { return coreApiTable->hsa_init_fn(); } +hsa_status_t HSA_API hsa_init() { + // We initialize the api tables here once more since the code above is prone to a + // link-time ordering condition: This compilation unit here may get its global + // variables initialized earlier than the global objects in other compilation units. + // In particular Init::Init may get called earlier than that the underlying hsa_api_table_ + // object in hsa_api_trace.cpp has been initialized. + rocr::core::LoadInitialHsaApiTable(); + return coreApiTable->hsa_init_fn(); +} hsa_status_t HSA_API hsa_shut_down() { return coreApiTable->hsa_shut_down_fn(); }