From 809149ecc8d751acd3c1595b590090cd86ada8df Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mirza=20Halil=C4=8Devi=C4=87?= <109971222+mirza-halilcevic@users.noreply.github.com> Date: Mon, 7 Nov 2022 12:20:44 +0100 Subject: [PATCH] EXSWHTEC-10 - Implement additional tests for hipIpcOpenMemHandle (#2880) - Verify that opening the same handle in different contexts on same device causes an error - Verify that opening the handle in the process that created it causes an error - Remove linux guard from test file and exclude test from windows build with cmake. - Disable Unit_hipIpcOpenMemHandle_Negative_Open_In_Creating_Process for AMD. --- .../config/config_amd_linux_common.json | 1 + .../config/config_amd_windows_common.json | 1 + tests/catch/unit/device/CMakeLists.txt | 1 + .../catch/unit/device/hipIpcOpenMemHandle.cc | 88 +++++++++++++++++++ 4 files changed, 91 insertions(+) create mode 100644 tests/catch/unit/device/hipIpcOpenMemHandle.cc diff --git a/tests/catch/hipTestMain/config/config_amd_linux_common.json b/tests/catch/hipTestMain/config/config_amd_linux_common.json index dd0f6fd6c4..b3a7503222 100644 --- a/tests/catch/hipTestMain/config/config_amd_linux_common.json +++ b/tests/catch/hipTestMain/config/config_amd_linux_common.json @@ -9,6 +9,7 @@ "Unit_hipDeviceGetCacheConfig_Positive_Threaded", "Unit_hipGetDeviceFlags_Positive_Context", "Unit_hipIpcCloseMemHandle_Negative_Close_In_Originating_Process", + "Unit_hipIpcOpenMemHandle_Negative_Open_In_Creating_Process", "Unit_hipDeviceGetPCIBusId_Negative_PartialFill" ] } diff --git a/tests/catch/hipTestMain/config/config_amd_windows_common.json b/tests/catch/hipTestMain/config/config_amd_windows_common.json index b0c106dbf3..17ae9eb145 100644 --- a/tests/catch/hipTestMain/config/config_amd_windows_common.json +++ b/tests/catch/hipTestMain/config/config_amd_windows_common.json @@ -76,6 +76,7 @@ "Unit_hipStreamValue_Wait64_Blocking_NoMask_Nor", "Unit_hipGetDeviceFlags_Positive_Context", "Unit_hipIpcCloseMemHandle_Negative_Close_In_Originating_Process", + "Unit_hipIpcOpenMemHandle_Negative_Open_In_Creating_Process", "Unit_hipDeviceGetPCIBusId_Negative_PartialFill", "Unit_hipDeviceGetSharedMemConfig_Positive_Basic", "Unit_hipDeviceGetSharedMemConfig_Positive_Threaded" diff --git a/tests/catch/unit/device/CMakeLists.txt b/tests/catch/unit/device/CMakeLists.txt index f027abbfa7..e8a738c56e 100644 --- a/tests/catch/unit/device/CMakeLists.txt +++ b/tests/catch/unit/device/CMakeLists.txt @@ -28,6 +28,7 @@ set(TEST_SRC if(UNIX) set(TEST_SRC ${TEST_SRC} + hipIpcOpenMemHandle.cc hipIpcGetMemHandle.cc hipIpcCloseMemHandle.cc ) diff --git a/tests/catch/unit/device/hipIpcOpenMemHandle.cc b/tests/catch/unit/device/hipIpcOpenMemHandle.cc new file mode 100644 index 0000000000..4edc7cd616 --- /dev/null +++ b/tests/catch/unit/device/hipIpcOpenMemHandle.cc @@ -0,0 +1,88 @@ +/* +Copyright (c) 2022 Advanced Micro Devices, Inc. All rights reserved. + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. +*/ + +#include +#include +#include +#include + +#include +#include + +TEST_CASE("Unit_hipIpcOpenMemHandle_Negative_Open_In_Creating_Process") { + hipDeviceptr_t ptr1, ptr2; + hipIpcMemHandle_t handle; + HIP_CHECK(hipMalloc(reinterpret_cast(&ptr1), 1024)); + HIP_CHECK(hipIpcGetMemHandle(&handle, reinterpret_cast(ptr1))); + HIP_CHECK_ERROR( + hipIpcOpenMemHandle(reinterpret_cast(&ptr2), handle, hipIpcMemLazyEnablePeerAccess), + hipErrorInvalidContext); + HIP_CHECK(hipFree(reinterpret_cast(ptr1))); +} + +TEST_CASE("Unit_hipIpcOpenMemHandle_Negative_Open_In_Two_Contexts_Same_Device") { + int fd[2]; + REQUIRE(pipe(fd) == 0); + + // The fork must be performed before the runtime is initialized(so before any API that implicitly + // initializes it). The pipe in conjunction with wait is then used to impose total ordering + // between parent and child process. Because total ordering is imposed regular CATCH assertions + // should be safe to use + auto pid = fork(); + REQUIRE(pid >= 0); + if (pid == 0) { // child + REQUIRE(close(fd[1]) == 0); + + hipIpcMemHandle_t handle; + REQUIRE(read(fd[0], &handle, sizeof(handle)) >= 0); + REQUIRE(close(fd[0]) == 0); + + hipDeviceptr_t ptr_child; + HIP_CHECK(hipIpcOpenMemHandle(reinterpret_cast(&ptr_child), handle, + hipIpcMemLazyEnablePeerAccess)); + + HIP_CHECK(hipInit(0)); + hipCtx_t ctx; + HIP_CHECK(hipCtxCreate(&ctx, 0, 0)); + + hipDeviceptr_t ptr_child_ctx; + HIP_CHECK_ERROR(hipIpcOpenMemHandle(reinterpret_cast(&ptr_child_ctx), handle, + hipIpcMemLazyEnablePeerAccess), + hipErrorInvalidResourceHandle); + + exit(0); + } else { // parent + REQUIRE(close(fd[0]) == 0); + + hipDeviceptr_t ptr; + hipIpcMemHandle_t handle; + HIP_CHECK(hipMalloc(reinterpret_cast(&ptr), 1024)); + HIP_CHECK(hipIpcGetMemHandle(&handle, reinterpret_cast(ptr))); + + REQUIRE(write(fd[1], &handle, sizeof(handle)) >= 0); + REQUIRE(close(fd[1]) == 0); + + REQUIRE(wait(NULL) >= 0); + + HIP_CHECK(hipFree(reinterpret_cast(ptr))); + } +} \ No newline at end of file