From cf2cf040950af0183942aa6a11f9a38b5459d5d4 Mon Sep 17 00:00:00 2001 From: Ioannis Assiouras Date: Mon, 24 Mar 2025 19:36:14 +0000 Subject: [PATCH] SWDEV-505930 - Avoid static initialization of ModuleGuard This is to prevent calling catch2 macros from outside catch2 TEST_CASE that can lead to undefined bahavior. This change also disables hipGetProcAddress tests that are not supported on static build. --- projects/hip-tests/catch/unit/module/CMakeLists.txt | 9 +++++++-- .../catch/unit/module/hipExtModuleLaunchKernel.cc | 2 +- .../unit/module/hipModuleLaunchCooperativeKernel.cc | 3 +++ .../hipModuleLaunchCooperativeKernelMultiDevice.cc | 3 +++ .../hip-tests/catch/unit/module/hip_module_common.cc | 5 +++++ .../hip-tests/catch/unit/module/hip_module_common.hh | 2 ++ .../unit/module/hip_module_launch_kernel_common.hh | 12 +++++------- 7 files changed, 26 insertions(+), 10 deletions(-) diff --git a/projects/hip-tests/catch/unit/module/CMakeLists.txt b/projects/hip-tests/catch/unit/module/CMakeLists.txt index 3f891d97434..a1fe3d2531c 100644 --- a/projects/hip-tests/catch/unit/module/CMakeLists.txt +++ b/projects/hip-tests/catch/unit/module/CMakeLists.txt @@ -85,8 +85,13 @@ if(HIP_PLATFORM MATCHES "amd") set(TEST_SRC ${TEST_SRC} hipExtModuleLaunchKernel.cc - hipHccModuleLaunchKernel.cc - hipGetProcAddressModuleApis.cc) + hipHccModuleLaunchKernel.cc) + +if(BUILD_SHARED_LIBS) + set(TEST_SRC + ${TEST_SRC} + hipGetProcAddressModuleApis.cc) +endif() add_custom_target(empty_module.code COMMAND ${CMAKE_CXX_COMPILER} --genco ${OFFLOAD_ARCH_STR} diff --git a/projects/hip-tests/catch/unit/module/hipExtModuleLaunchKernel.cc b/projects/hip-tests/catch/unit/module/hipExtModuleLaunchKernel.cc index 5b292818601..2df5dc3ae6a 100644 --- a/projects/hip-tests/catch/unit/module/hipExtModuleLaunchKernel.cc +++ b/projects/hip-tests/catch/unit/module/hipExtModuleLaunchKernel.cc @@ -253,7 +253,7 @@ TEST_CASE("Unit_hipExtModuleLaunchKernel_UniformWorkGroup") { TEST_CASE("Unit_hipExtModuleLaunchKernel_Positive_Parameters") { ModuleLaunchKernelPositiveParameters(); - + auto mg = ModuleGuard::InitModule("launch_kernel_module.code"); SECTION("Pass only start event") { hipEvent_t start_event = nullptr; HIP_CHECK(hipEventCreate(&start_event)); diff --git a/projects/hip-tests/catch/unit/module/hipModuleLaunchCooperativeKernel.cc b/projects/hip-tests/catch/unit/module/hipModuleLaunchCooperativeKernel.cc index b86f8fa66c5..6035989fc54 100644 --- a/projects/hip-tests/catch/unit/module/hipModuleLaunchCooperativeKernel.cc +++ b/projects/hip-tests/catch/unit/module/hipModuleLaunchCooperativeKernel.cc @@ -50,6 +50,7 @@ THE SOFTWARE. * - HIP_VERSION >= 5.5 */ TEST_CASE("Unit_hipModuleLaunchCooperativeKernel_Positive_Basic") { + auto mg = ModuleGuard::InitModule("launch_kernel_module.code"); if (!DeviceAttributesSupport(0, hipDeviceAttributeCooperativeLaunch)) { HipTest::HIP_SKIP_TEST("CooperativeLaunch not supported"); return; @@ -89,6 +90,7 @@ TEST_CASE("Unit_hipModuleLaunchCooperativeKernel_Positive_Basic") { * - HIP_VERSION >= 5.5 */ TEST_CASE("Unit_hipModuleLaunchCooperativeKernel_Positive_Parameters") { + auto mg = ModuleGuard::InitModule("launch_kernel_module.code"); if (!DeviceAttributesSupport(0, hipDeviceAttributeCooperativeLaunch)) { HipTest::HIP_SKIP_TEST("CooperativeLaunch not supported"); return; @@ -124,6 +126,7 @@ TEST_CASE("Unit_hipModuleLaunchCooperativeKernel_Positive_Parameters") { * - HIP_VERSION >= 5.5 */ TEST_CASE("Unit_hipModuleLaunchCooperativeKernel_Negative_Parameters") { + auto mg = ModuleGuard::InitModule("launch_kernel_module.code"); if (!DeviceAttributesSupport(0, hipDeviceAttributeCooperativeLaunch)) { HipTest::HIP_SKIP_TEST("CooperativeLaunch not supported"); return; diff --git a/projects/hip-tests/catch/unit/module/hipModuleLaunchCooperativeKernelMultiDevice.cc b/projects/hip-tests/catch/unit/module/hipModuleLaunchCooperativeKernelMultiDevice.cc index e9f2389ce38..d9740b13cf6 100644 --- a/projects/hip-tests/catch/unit/module/hipModuleLaunchCooperativeKernelMultiDevice.cc +++ b/projects/hip-tests/catch/unit/module/hipModuleLaunchCooperativeKernelMultiDevice.cc @@ -49,6 +49,7 @@ THE SOFTWARE. * - HIP_VERSION >= 5.5 */ TEST_CASE("Unit_hipModuleLaunchCooperativeKernelMultiDevice_Positive_Basic") { + auto mg = ModuleGuard::InitModule("launch_kernel_module.code"); if (!DeviceAttributesSupport(0, hipDeviceAttributeCooperativeLaunch)) { HipTest::HIP_SKIP_TEST("CooperativeLaunch not supported"); return; @@ -106,6 +107,7 @@ TEST_CASE("Unit_hipModuleLaunchCooperativeKernelMultiDevice_Positive_Basic") { * - HIP_VERSION >= 5.5 */ TEST_CASE("Unit_hipModuleLaunchCooperativeKernelMultiDevice_Negative_Parameters") { + auto mg = ModuleGuard::InitModule("launch_kernel_module.code"); if (!DeviceAttributesSupport(0, hipDeviceAttributeCooperativeLaunch)) { HipTest::HIP_SKIP_TEST("CooperativeLaunch not supported"); return; @@ -229,6 +231,7 @@ TEST_CASE("Unit_hipModuleLaunchCooperativeKernelMultiDevice_Negative_Parameters" * - HIP_VERSION >= 5.5 */ TEST_CASE("Unit_hipModuleLaunchCooperativeKernelMultiDevice_Negative_MultiKernelSameDevice") { + auto mg = ModuleGuard::InitModule("launch_kernel_module.code"); if (!DeviceAttributesSupport(0, hipDeviceAttributeCooperativeLaunch)) { HipTest::HIP_SKIP_TEST("CooperativeLaunch not supported"); return; diff --git a/projects/hip-tests/catch/unit/module/hip_module_common.cc b/projects/hip-tests/catch/unit/module/hip_module_common.cc index 2cdb250b37d..fbd3e486e64 100644 --- a/projects/hip-tests/catch/unit/module/hip_module_common.cc +++ b/projects/hip-tests/catch/unit/module/hip_module_common.cc @@ -33,6 +33,11 @@ ModuleGuard ModuleGuard::LoadModule(const char* fname) { return ModuleGuard{module}; } +ModuleGuard ModuleGuard::InitModule(const char* fname) { + HIP_CHECK(hipFree(nullptr)); + return LoadModule(fname); +} + ModuleGuard ModuleGuard::LoadModuleDataFile(const char* fname) { const auto loaded_module = LoadModuleIntoBuffer(fname); hipModule_t module = nullptr; diff --git a/projects/hip-tests/catch/unit/module/hip_module_common.hh b/projects/hip-tests/catch/unit/module/hip_module_common.hh index 41b153b3a37..165edd2729f 100644 --- a/projects/hip-tests/catch/unit/module/hip_module_common.hh +++ b/projects/hip-tests/catch/unit/module/hip_module_common.hh @@ -34,6 +34,8 @@ class ModuleGuard { static ModuleGuard LoadModule(const char* fname); + static ModuleGuard InitModule(const char* fname); + static ModuleGuard LoadModuleDataFile(const char* fname); static ModuleGuard LoadModuleDataRTC(const char* code); diff --git a/projects/hip-tests/catch/unit/module/hip_module_launch_kernel_common.hh b/projects/hip-tests/catch/unit/module/hip_module_launch_kernel_common.hh index 32bb9ed1c8a..72424322a65 100644 --- a/projects/hip-tests/catch/unit/module/hip_module_launch_kernel_common.hh +++ b/projects/hip-tests/catch/unit/module/hip_module_launch_kernel_common.hh @@ -27,18 +27,12 @@ THE SOFTWARE. #include #include -inline ModuleGuard InitModule() { - HIP_CHECK(hipFree(nullptr)); - return ModuleGuard::LoadModule("launch_kernel_module.code"); -} - -inline ModuleGuard mg{InitModule()}; - using ExtModuleLaunchKernelSig = hipError_t(hipFunction_t, uint32_t, uint32_t, uint32_t, uint32_t, uint32_t, uint32_t, size_t, hipStream_t, void**, void**, hipEvent_t, hipEvent_t, uint32_t); template void ModuleLaunchKernelPositiveBasic() { + auto mg = ModuleGuard::InitModule("launch_kernel_module.code"); SECTION("Kernel with no arguments") { hipFunction_t f = GetKernel(mg.module(), "NOPKernel"); HIP_CHECK(func(f, 1, 1, 1, 1, 1, 1, 0, nullptr, nullptr, nullptr, nullptr, nullptr, 0u)); @@ -81,6 +75,7 @@ template void ModuleLaunchKernelPositiveParamet const auto LaunchNOPKernel = [=](unsigned int gridDimX, unsigned int gridDimY, unsigned int gridDimZ, unsigned int blockDimX, unsigned int blockDimY, unsigned int blockDimZ) { + auto mg = ModuleGuard::InitModule("launch_kernel_module.code"); hipFunction_t f = GetKernel(mg.module(), "NOPKernel"); HIP_CHECK(func(f, gridDimX, gridDimY, gridDimZ, blockDimX, blockDimY, blockDimZ, 0, nullptr, nullptr, nullptr, nullptr, nullptr, 0u)); @@ -120,6 +115,7 @@ template void ModuleLaunchKernelPositiveParamet template void ModuleLaunchKernelNegativeParameters( bool extLaunch = false) { + auto mg = ModuleGuard::InitModule("launch_kernel_module.code"); hipFunction_t f = GetKernel(mg.module(), "NOPKernel"); hipError_t expectedErrorLaunchParam = (extLaunch == true) ? hipErrorInvalidConfiguration : hipErrorInvalidValue; @@ -213,6 +209,7 @@ template void ModuleLaunchKernelNegativeParamet } SECTION("Passing kernel_args and extra simultaneously") { + auto mg = ModuleGuard::InitModule("launch_kernel_module.code"); hipFunction_t f = GetKernel(mg.module(), "Kernel42"); LinearAllocGuard result_dev(LinearAllocs::hipMalloc, sizeof(int)); int* result_ptr = result_dev.ptr(); @@ -248,6 +245,7 @@ template void ModuleLaunchKernelNegativeParamet } SECTION("Invalid extra") { + auto mg = ModuleGuard::InitModule("launch_kernel_module.code"); hipFunction_t f = GetKernel(mg.module(), "Kernel42"); void* extra[0] = {}; HIP_CHECK_ERROR(func(f, 1, 1, 1, 1, 1, 1, 0, nullptr, nullptr, extra, nullptr, nullptr, 0u),