From acd443db9940fe0afb7fad7e14e1ae6cb039d0a2 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. --- src/core/common/hsa_table_interface.cpp | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/opensrc/hsa-runtime/core/common/hsa_table_interface.cpp b/opensrc/hsa-runtime/core/common/hsa_table_interface.cpp index 3802ae9b..593ccb38 100644 --- a/src/core/common/hsa_table_interface.cpp +++ b/src/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(); } -- 2.40.1