diff --git a/test/conformance/testing/include/uur/fixtures.h b/test/conformance/testing/include/uur/fixtures.h index dde1eb7277..65a91d6df7 100644 --- a/test/conformance/testing/include/uur/fixtures.h +++ b/test/conformance/testing/include/uur/fixtures.h @@ -636,12 +636,33 @@ template struct urUSMPoolTestWithParam : urContextTestWithParam { ur_usm_pool_handle_t pool; }; -struct urPhysicalMemTest : urContextTest { +struct urVirtualMemGranularityTest : urContextTest { void SetUp() override { UUR_RETURN_ON_FATAL_FAILURE(urContextTest::SetUp()); ASSERT_SUCCESS(urVirtualMemGranularityGetInfo( context, device, UR_VIRTUAL_MEM_GRANULARITY_INFO_MINIMUM, sizeof(granularity), &granularity, nullptr)); + } + size_t granularity; +}; + +template +struct urVirtualMemGranularityTestWithParam : urContextTestWithParam { + void SetUp() override { + UUR_RETURN_ON_FATAL_FAILURE(urContextTestWithParam::SetUp()); + ASSERT_SUCCESS(urVirtualMemGranularityGetInfo( + this->context, this->device, + UR_VIRTUAL_MEM_GRANULARITY_INFO_MINIMUM, sizeof(granularity), + &granularity, nullptr)); + ASSERT_NE(granularity, 0); + } + + size_t granularity = 0; +}; + +struct urPhysicalMemTest : urVirtualMemGranularityTest { + void SetUp() override { + UUR_RETURN_ON_FATAL_FAILURE(urVirtualMemGranularityTest::SetUp()); size = granularity * 256; ur_physical_mem_properties_t props{ UR_STRUCTURE_TYPE_PHYSICAL_MEM_PROPERTIES, @@ -657,14 +678,110 @@ struct urPhysicalMemTest : urContextTest { if (physical_mem) { EXPECT_SUCCESS(urPhysicalMemRelease(physical_mem)); } - UUR_RETURN_ON_FATAL_FAILURE(urContextTest::TearDown()); + UUR_RETURN_ON_FATAL_FAILURE(urVirtualMemGranularityTest::TearDown()); + } + + size_t size = 0; + ur_physical_mem_handle_t physical_mem = nullptr; +}; + +template +struct urPhysicalMemTestWithParam : urVirtualMemGranularityTestWithParam { + void SetUp() override { + UUR_RETURN_ON_FATAL_FAILURE( + urVirtualMemGranularityTestWithParam::SetUp()); + size = this->granularity * 256; + ur_physical_mem_properties_t props{ + UR_STRUCTURE_TYPE_PHYSICAL_MEM_PROPERTIES, + nullptr, + 0 /*flags*/, + }; + ASSERT_SUCCESS(urPhysicalMemCreate(this->context, this->device, size, + &props, &physical_mem)); + ASSERT_NE(physical_mem, nullptr); + } + + void TearDown() override { + if (physical_mem) { + EXPECT_SUCCESS(urPhysicalMemRelease(physical_mem)); + } + UUR_RETURN_ON_FATAL_FAILURE( + urVirtualMemGranularityTestWithParam::TearDown()); } - size_t granularity = 0; size_t size = 0; ur_physical_mem_handle_t physical_mem = nullptr; }; +struct urVirtualMemTest : urPhysicalMemTest { + void SetUp() override { + UUR_RETURN_ON_FATAL_FAILURE(urPhysicalMemTest::SetUp()); + ASSERT_SUCCESS( + urVirtualMemReserve(context, nullptr, size, &virtual_ptr)); + ASSERT_NE(virtual_ptr, nullptr); + } + + void TearDown() override { + if (virtual_ptr) { + EXPECT_SUCCESS(urVirtualMemFree(context, virtual_ptr, size)); + } + UUR_RETURN_ON_FATAL_FAILURE(urPhysicalMemTest::TearDown()); + } + + void *virtual_ptr = nullptr; +}; + +template +struct urVirtualMemTestWithParam : urPhysicalMemTestWithParam { + void SetUp() override { + UUR_RETURN_ON_FATAL_FAILURE(urPhysicalMemTestWithParam::SetUp()); + ASSERT_SUCCESS(urVirtualMemReserve(this->context, nullptr, this->size, + &virtual_ptr)); + } + + void TearDown() override { + if (virtual_ptr) { + EXPECT_SUCCESS( + urVirtualMemFree(this->context, virtual_ptr, this->size)); + } + UUR_RETURN_ON_FATAL_FAILURE(urPhysicalMemTestWithParam::TearDown()); + } + + void *virtual_ptr = nullptr; +}; + +struct urVirtualMemMappedTest : urVirtualMemTest { + + void SetUp() override { + UUR_RETURN_ON_FATAL_FAILURE(urVirtualMemTest::SetUp()); + ASSERT_SUCCESS(urVirtualMemMap(context, virtual_ptr, size, physical_mem, + 0, + UR_VIRTUAL_MEM_ACCESS_FLAG_READ_WRITE)); + } + + void TearDown() override { + EXPECT_SUCCESS(urVirtualMemUnmap(context, virtual_ptr, size)); + UUR_RETURN_ON_FATAL_FAILURE(urVirtualMemTest::TearDown()); + } +}; + +template +struct urVirtualMemMappedTestWithParam : urVirtualMemTestWithParam { + + void SetUp() override { + UUR_RETURN_ON_FATAL_FAILURE(urVirtualMemTestWithParam::SetUp()); + ASSERT_SUCCESS(urVirtualMemMap(this->context, this->virtual_ptr, + this->size, this->physical_mem, 0, + UR_VIRTUAL_MEM_ACCESS_FLAG_READ_WRITE)); + } + + void TearDown() override { + EXPECT_SUCCESS( + urVirtualMemUnmap(this->context, this->virtual_ptr, this->size)); + UUR_RETURN_ON_FATAL_FAILURE(urVirtualMemTestWithParam::TearDown()); + } +}; + template struct urUSMDeviceAllocTestWithParam : urQueueTestWithParam { diff --git a/test/conformance/testing/include/uur/utils.h b/test/conformance/testing/include/uur/utils.h index 75e1c51b9c..027e270f25 100644 --- a/test/conformance/testing/include/uur/utils.h +++ b/test/conformance/testing/include/uur/utils.h @@ -14,6 +14,10 @@ namespace uur { +inline size_t RoundUpToNearestFactor(size_t num, size_t factor) { + return ((num + factor - 1) / factor) * factor; +} + /// @brief Make a string a valid identifier for gtest. /// @param str The string to sanitize. inline std::string GTestSanitizeString(const std::string &str) { diff --git a/test/conformance/virtual_memory/urPhysicalMemCreate.cpp b/test/conformance/virtual_memory/urPhysicalMemCreate.cpp index 39059ae0c1..078b0e68db 100644 --- a/test/conformance/virtual_memory/urPhysicalMemCreate.cpp +++ b/test/conformance/virtual_memory/urPhysicalMemCreate.cpp @@ -4,28 +4,21 @@ // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception #include -using urPhysicalMemCreateTestParams = size_t; struct urPhysicalMemCreateTest - : uur::urContextTestWithParam { + : uur::urVirtualMemGranularityTestWithParam { void SetUp() override { - UUR_RETURN_ON_FATAL_FAILURE(uur::urContextTestWithParam< - urPhysicalMemCreateTestParams>::SetUp()); - ASSERT_SUCCESS(urVirtualMemGranularityGetInfo( - context, device, UR_VIRTUAL_MEM_GRANULARITY_INFO_MINIMUM, - sizeof(granularity), &granularity, nullptr)); - ASSERT_NE(granularity, 0); + UUR_RETURN_ON_FATAL_FAILURE( + uur::urVirtualMemGranularityTestWithParam::SetUp()); size = getParam() * granularity; } - size_t granularity; size_t size; }; -UUR_TEST_SUITE_P( - urPhysicalMemCreateTest, - ::testing::Values(1, 2, 3, 7, 12, 44, 1024, 4000, 12345), - uur::deviceTestWithParamPrinter); +UUR_TEST_SUITE_P(urPhysicalMemCreateTest, + ::testing::Values(1, 2, 3, 7, 12, 44, 1024, 4000, 12345), + uur::deviceTestWithParamPrinter); TEST_P(urPhysicalMemCreateTest, Success) { ur_physical_mem_handle_t physical_mem = nullptr; diff --git a/test/conformance/virtual_memory/urVirtualMemFree.cpp b/test/conformance/virtual_memory/urVirtualMemFree.cpp index 281aa8badb..6cb8795547 100644 --- a/test/conformance/virtual_memory/urVirtualMemFree.cpp +++ b/test/conformance/virtual_memory/urVirtualMemFree.cpp @@ -2,3 +2,22 @@ // Part of the Unified-Runtime Project, under the Apache License v2.0 with LLVM Exceptions. // See LICENSE.TXT // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +#include + +using urVirtualMemFreeTest = uur::urVirtualMemTest; +UUR_INSTANTIATE_DEVICE_TEST_SUITE_P(urVirtualMemFreeTest); + +TEST_P(urVirtualMemFreeTest, Success) { + ASSERT_SUCCESS(urVirtualMemFree(context, virtual_ptr, size)); + virtual_ptr = nullptr; // set to nullptr to prevent double-free +} + +TEST_P(urVirtualMemFreeTest, InvalidNullHandleContext) { + ASSERT_EQ_RESULT(urVirtualMemFree(nullptr, virtual_ptr, size), + UR_RESULT_ERROR_INVALID_NULL_HANDLE); +} + +TEST_P(urVirtualMemFreeTest, InvalidNullPointerStart) { + ASSERT_EQ_RESULT(urVirtualMemFree(context, nullptr, size), + UR_RESULT_ERROR_INVALID_NULL_HANDLE); +} diff --git a/test/conformance/virtual_memory/urVirtualMemGetInfo.cpp b/test/conformance/virtual_memory/urVirtualMemGetInfo.cpp index 281aa8badb..7d5c3c04ff 100644 --- a/test/conformance/virtual_memory/urVirtualMemGetInfo.cpp +++ b/test/conformance/virtual_memory/urVirtualMemGetInfo.cpp @@ -2,3 +2,62 @@ // Part of the Unified-Runtime Project, under the Apache License v2.0 with LLVM Exceptions. // See LICENSE.TXT // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +#include + +using urVirtualMemGetInfoTestWithParam = + uur::urVirtualMemMappedTestWithParam; +UUR_TEST_SUITE_P(urVirtualMemGetInfoTestWithParam, + ::testing::Values(UR_VIRTUAL_MEM_INFO_ACCESS_MODE), + uur::deviceTestWithParamPrinter); + +TEST_P(urVirtualMemGetInfoTestWithParam, Success) { + size_t info_size = 0; + ur_virtual_mem_info_t info = getParam(); + ASSERT_SUCCESS(urVirtualMemGetInfo(context, virtual_ptr, size, info, 0, + nullptr, &info_size)); + ASSERT_NE(info_size, 0); + + std::vector data(info_size); + ASSERT_SUCCESS(urVirtualMemGetInfo(context, virtual_ptr, size, info, + data.size(), data.data(), nullptr)); + + switch (info) { + case UR_VIRTUAL_MEM_INFO_ACCESS_MODE: { + ASSERT_EQ(sizeof(ur_virtual_mem_access_flags_t), data.size()); + ur_virtual_mem_access_flags_t flags = + *reinterpret_cast(data.data()); + ASSERT_TRUE(flags & UR_VIRTUAL_MEM_ACCESS_FLAG_READ_WRITE); + } break; + + default: + FAIL() << "Unhandled ur_virtual_mem_info_t enumeration: " << info; + break; + } +} + +using urVirtualMemGetInfoTest = uur::urVirtualMemMappedTest; +UUR_INSTANTIATE_DEVICE_TEST_SUITE_P(urVirtualMemGetInfoTest); + +TEST_P(urVirtualMemGetInfoTest, InvalidNullHandleContext) { + ur_virtual_mem_access_flags_t flags = 0; + ASSERT_EQ_RESULT(urVirtualMemGetInfo(nullptr, virtual_ptr, size, + UR_VIRTUAL_MEM_INFO_ACCESS_MODE, + sizeof(flags), &flags, nullptr), + UR_RESULT_ERROR_INVALID_NULL_HANDLE); +} + +TEST_P(urVirtualMemGetInfoTest, InvalidNullPointerStart) { + ur_virtual_mem_access_flags_t flags = 0; + ASSERT_EQ_RESULT(urVirtualMemGetInfo(context, nullptr, size, + UR_VIRTUAL_MEM_INFO_ACCESS_MODE, + sizeof(flags), &flags, nullptr), + UR_RESULT_ERROR_INVALID_NULL_HANDLE); +} + +TEST_P(urVirtualMemGetInfoTest, InvalidEnumerationInfo) { + size_t info_size = 0; + ASSERT_EQ_RESULT(urVirtualMemGetInfo(context, virtual_ptr, size, + UR_VIRTUAL_MEM_INFO_FORCE_UINT32, 0, + nullptr, &info_size), + UR_RESULT_ERROR_INVALID_NULL_HANDLE); +} diff --git a/test/conformance/virtual_memory/urVirtualMemMap.cpp b/test/conformance/virtual_memory/urVirtualMemMap.cpp index 281aa8badb..bed65e2018 100644 --- a/test/conformance/virtual_memory/urVirtualMemMap.cpp +++ b/test/conformance/virtual_memory/urVirtualMemMap.cpp @@ -2,3 +2,38 @@ // Part of the Unified-Runtime Project, under the Apache License v2.0 with LLVM Exceptions. // See LICENSE.TXT // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +#include + +using urVirtualMemMapTest = uur::urVirtualMemTest; +UUR_INSTANTIATE_DEVICE_TEST_SUITE_P(urVirtualMemMapTest); + +TEST_P(urVirtualMemMapTest, Success) { + ASSERT_SUCCESS(urVirtualMemMap(context, virtual_ptr, size, physical_mem, 0, + UR_VIRTUAL_MEM_ACCESS_FLAG_READ_WRITE)); + EXPECT_SUCCESS(urVirtualMemUnmap(context, virtual_ptr, size)); +} + +TEST_P(urVirtualMemMapTest, InvalidNullHandleContext) { + ASSERT_EQ_RESULT(urVirtualMemMap(nullptr, virtual_ptr, size, physical_mem, + 0, UR_VIRTUAL_MEM_ACCESS_FLAG_READ_WRITE), + UR_RESULT_ERROR_INVALID_NULL_HANDLE); +} + +TEST_P(urVirtualMemMapTest, InvalidNullHandlePhysicalMem) { + ASSERT_EQ_RESULT(urVirtualMemMap(context, virtual_ptr, size, nullptr, 0, + UR_VIRTUAL_MEM_ACCESS_FLAG_READ_WRITE), + UR_RESULT_ERROR_INVALID_NULL_HANDLE); +} + +TEST_P(urVirtualMemMapTest, InvalidNullPointerStart) { + ASSERT_EQ_RESULT(urVirtualMemMap(context, nullptr, size, physical_mem, 0, + UR_VIRTUAL_MEM_ACCESS_FLAG_READ_WRITE), + UR_RESULT_ERROR_INVALID_NULL_POINTER); +} + +TEST_P(urVirtualMemMapTest, InvalidEnumerationFlags) { + ASSERT_EQ_RESULT(urVirtualMemMap(context, virtual_ptr, size, physical_mem, + 0, + UR_VIRTUAL_MEM_ACCESS_FLAG_FORCE_UINT32), + UR_RESULT_ERROR_INVALID_ENUMERATION); +} diff --git a/test/conformance/virtual_memory/urVirtualMemReserve.cpp b/test/conformance/virtual_memory/urVirtualMemReserve.cpp index 281aa8badb..7d870dfb05 100644 --- a/test/conformance/virtual_memory/urVirtualMemReserve.cpp +++ b/test/conformance/virtual_memory/urVirtualMemReserve.cpp @@ -2,3 +2,61 @@ // Part of the Unified-Runtime Project, under the Apache License v2.0 with LLVM Exceptions. // See LICENSE.TXT // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +#include + +using urVirtualMemReserveTestWithParam = + uur::urVirtualMemGranularityTestWithParam; +UUR_TEST_SUITE_P(urVirtualMemReserveTestWithParam, + ::testing::Values(2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, + 2048, 5000, 100000), + uur::deviceTestWithParamPrinter); + +TEST_P(urVirtualMemReserveTestWithParam, SuccessNoStartPointer) { + // round up to nearest granularity + size_t virtual_mem_size = + uur::RoundUpToNearestFactor(getParam(), granularity); + void *virtual_mem_start = nullptr; + ASSERT_SUCCESS(urVirtualMemReserve(context, nullptr, virtual_mem_size, + &virtual_mem_start)); + ASSERT_NE(virtual_mem_start, nullptr); + + EXPECT_SUCCESS( + urVirtualMemFree(context, virtual_mem_start, virtual_mem_size)); +} + +TEST_P(urVirtualMemReserveTestWithParam, SuccessWithStartPointer) { + // roundup to nearest granularity + size_t page_size = uur::RoundUpToNearestFactor(getParam(), granularity); + void *origin_ptr = nullptr; + ASSERT_SUCCESS( + urVirtualMemReserve(context, nullptr, page_size, &origin_ptr)); + ASSERT_NE(origin_ptr, nullptr); + + // try to reserve at the end of origin_ptr + void *virtual_mem_ptr = nullptr; + void *pStart = (uint8_t *)origin_ptr + page_size; + ASSERT_SUCCESS( + urVirtualMemReserve(context, pStart, page_size, &virtual_mem_ptr)); + ASSERT_NE(virtual_mem_ptr, nullptr); + + // both pointers have to be freed + EXPECT_SUCCESS(urVirtualMemFree(context, origin_ptr, page_size)); + EXPECT_SUCCESS(urVirtualMemFree(context, virtual_mem_ptr, page_size)); +} + +using urVirtualMemReserveTest = uur::urVirtualMemGranularityTest; +UUR_INSTANTIATE_DEVICE_TEST_SUITE_P(urVirtualMemReserveTest); + +TEST_P(urVirtualMemReserveTest, InvalidNullHandleContext) { + size_t page_size = uur::RoundUpToNearestFactor(1024, granularity); + void *virtual_ptr = nullptr; + ASSERT_EQ_RESULT( + urVirtualMemReserve(nullptr, nullptr, page_size, &virtual_ptr), + UR_RESULT_ERROR_INVALID_NULL_HANDLE); +} + +TEST_P(urVirtualMemReserveTest, InvalidNullPointer) { + size_t page_size = uur::RoundUpToNearestFactor(1024, granularity); + ASSERT_EQ_RESULT(urVirtualMemReserve(context, nullptr, page_size, nullptr), + UR_RESULT_ERROR_INVALID_NULL_POINTER); +} diff --git a/test/conformance/virtual_memory/urVirtualMemSetAccess.cpp b/test/conformance/virtual_memory/urVirtualMemSetAccess.cpp index 281aa8badb..5ba2904473 100644 --- a/test/conformance/virtual_memory/urVirtualMemSetAccess.cpp +++ b/test/conformance/virtual_memory/urVirtualMemSetAccess.cpp @@ -2,3 +2,32 @@ // Part of the Unified-Runtime Project, under the Apache License v2.0 with LLVM Exceptions. // See LICENSE.TXT // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +#include + +using urVirtualMemSetAccessTest = uur::urVirtualMemMappedTest; +UUR_INSTANTIATE_DEVICE_TEST_SUITE_P(urVirtualMemSetAccessTest); + +TEST_P(urVirtualMemSetAccessTest, Success) { + ASSERT_SUCCESS(urVirtualMemSetAccess(context, virtual_ptr, size, + UR_VIRTUAL_MEM_ACCESS_FLAG_READ_ONLY)); + + ur_virtual_mem_access_flags_t flags = 0; + ASSERT_SUCCESS(urVirtualMemGetInfo(context, virtual_ptr, size, + UR_VIRTUAL_MEM_INFO_ACCESS_MODE, + sizeof(flags), &flags, nullptr)); + ASSERT_TRUE(flags & UR_VIRTUAL_MEM_ACCESS_FLAG_READ_ONLY); +} + +TEST_P(urVirtualMemSetAccessTest, InvalidNullHandleContext) { + ASSERT_EQ_RESULT( + urVirtualMemSetAccess(nullptr, virtual_ptr, size, + UR_VIRTUAL_MEM_ACCESS_FLAG_READ_ONLY), + UR_RESULT_ERROR_INVALID_NULL_HANDLE); +} + +TEST_P(urVirtualMemSetAccessTest, InvalidNullPointerStart) { + ASSERT_EQ_RESULT( + urVirtualMemSetAccess(context, nullptr, size, + UR_VIRTUAL_MEM_ACCESS_FLAG_READ_ONLY), + UR_RESULT_ERROR_INVALID_NULL_HANDLE); +} diff --git a/test/conformance/virtual_memory/urVirtualMemUnmap.cpp b/test/conformance/virtual_memory/urVirtualMemUnmap.cpp index 281aa8badb..154dac3d69 100644 --- a/test/conformance/virtual_memory/urVirtualMemUnmap.cpp +++ b/test/conformance/virtual_memory/urVirtualMemUnmap.cpp @@ -2,3 +2,23 @@ // Part of the Unified-Runtime Project, under the Apache License v2.0 with LLVM Exceptions. // See LICENSE.TXT // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +#include + +using urVirtualMemUnmapTest = uur::urVirtualMemTest; +UUR_INSTANTIATE_DEVICE_TEST_SUITE_P(urVirtualMemUnmapTest); + +TEST_P(urVirtualMemUnmapTest, Success) { + ASSERT_SUCCESS(urVirtualMemMap(context, virtual_ptr, size, physical_mem, 0, + UR_VIRTUAL_MEM_ACCESS_FLAG_READ_WRITE)); + ASSERT_SUCCESS(urVirtualMemUnmap(context, virtual_ptr, size)); +} + +TEST_P(urVirtualMemUnmapTest, InvalidNullHandleContext) { + ASSERT_EQ_RESULT(urVirtualMemUnmap(nullptr, virtual_ptr, size), + UR_RESULT_ERROR_INVALID_NULL_HANDLE); +} + +TEST_P(urVirtualMemUnmapTest, InvalidNullPointerStart) { + ASSERT_EQ_RESULT(urVirtualMemUnmap(context, nullptr, size), + UR_RESULT_ERROR_INVALID_NULL_HANDLE); +}