diff --git a/include/umf/providers/provider_file_memory.h b/include/umf/providers/provider_file_memory.h index 5586cfe7c..b8eaa1083 100644 --- a/include/umf/providers/provider_file_memory.h +++ b/include/umf/providers/provider_file_memory.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2024-2025 Intel Corporation + * Copyright (C) 2024-2026 Intel Corporation * * Under the Apache License v2.0 with LLVM Exceptions. See LICENSE.TXT. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception @@ -47,6 +47,8 @@ umf_result_t umfFileMemoryProviderParamsSetPath( /// @param hParams handle to the parameters of the File Memory Provider. /// @param protection protection. Combination of \p umf_mem_protection_flags_t flags /// @return UMF_RESULT_SUCCESS on success or appropriate error code on failure. +/// @return UMF_RESULT_ERROR_INVALID_ARGUMENT if \p hParams is NULL, \p protection +/// is zero, or \p protection contains unsupported bits. umf_result_t umfFileMemoryProviderParamsSetProtection( umf_file_memory_provider_params_handle_t hParams, unsigned protection); diff --git a/src/provider/provider_file_memory.c b/src/provider/provider_file_memory.c index d796ee0d1..84dada6fd 100644 --- a/src/provider/provider_file_memory.c +++ b/src/provider/provider_file_memory.c @@ -1081,6 +1081,14 @@ umf_result_t umfFileMemoryProviderParamsSetProtection( return UMF_RESULT_ERROR_INVALID_ARGUMENT; } + // UMF_PROTECTION_MAX - 1 is the highest valid flag bit. + // Reject unknown bits and zero (NOTE: UMF_PROTECTION_NONE == 1) + const unsigned VALID_FLAGS_ALL = ((UMF_PROTECTION_MAX - 1) << 1) - 1; + if (protection & ~VALID_FLAGS_ALL || protection == 0) { + LOG_ERR("Incorrect memory protection flags: %u", protection); + return UMF_RESULT_ERROR_INVALID_ARGUMENT; + } + hParams->protection = protection; return UMF_RESULT_SUCCESS; diff --git a/test/provider_devdax_memory.cpp b/test/provider_devdax_memory.cpp index f3cd20eea..8aca08372 100644 --- a/test/provider_devdax_memory.cpp +++ b/test/provider_devdax_memory.cpp @@ -10,6 +10,7 @@ #endif #include +#include #include #include @@ -335,6 +336,11 @@ TEST(DevDaxProviderName, default_name_null_handle) { EXPECT_STREQ(name, "DEVDAX"); } +TEST(DevDaxProviderName, null_name) { + auto ret = umfDevDaxMemoryProviderOps()->get_name(nullptr, nullptr); + EXPECT_EQ(ret, UMF_RESULT_ERROR_INVALID_ARGUMENT); +} + TEST_P(umfProviderTest, free_size_0_ptr_not_null) { umf_result_t umf_result = umfMemoryProviderFree(provider.get(), INVALID_PTR, 0); @@ -578,3 +584,56 @@ TEST_F(test, create_wrong_size_0) { ret = umfDevDaxMemoryProviderParamsDestroy(wrong_params); ASSERT_EQ(ret, UMF_RESULT_SUCCESS); } + +TEST_F(test, create_NULL_params) { + umf_memory_provider_handle_t hProvider = nullptr; + auto ret = umfMemoryProviderCreate(umfDevDaxMemoryProviderOps(), nullptr, + &hProvider); + ASSERT_EQ(ret, UMF_RESULT_ERROR_INVALID_ARGUMENT); + ASSERT_EQ(hProvider, nullptr); +} + +TEST_F(test, params_NULL_name) { + umf_devdax_memory_provider_params_handle_t params = nullptr; + auto ret = + umfDevDaxMemoryProviderParamsCreate("/dev/dax0.0", 4096, ¶ms); + ASSERT_EQ(ret, UMF_RESULT_SUCCESS); + ASSERT_NE(params, nullptr); + + ret = umfDevDaxMemoryProviderParamsSetName(params, nullptr); + ASSERT_EQ(ret, UMF_RESULT_ERROR_INVALID_ARGUMENT); + + ret = umfDevDaxMemoryProviderParamsDestroy(params); + ASSERT_EQ(ret, UMF_RESULT_SUCCESS); + + ret = umfDevDaxMemoryProviderParamsSetName(nullptr, "test"); + ASSERT_EQ(ret, UMF_RESULT_ERROR_INVALID_ARGUMENT); +} + +TEST_F(test, params_long_name) { + auto params = create_devdax_params(); + if (!params.get()) { + GTEST_SKIP() << "devdax params unavailable"; + } + + const std::string long_name(1024, 'x'); + auto ret = + umfDevDaxMemoryProviderParamsSetName(params.get(), long_name.c_str()); + ASSERT_EQ(ret, UMF_RESULT_SUCCESS); + + umf_test::provider_unique_handle_t provider; + providerCreateExt({umfDevDaxMemoryProviderOps(), params.get()}, &provider); + ASSERT_NE(provider.get(), nullptr); + + const char *name = nullptr; + ret = umfMemoryProviderGetName(provider.get(), &name); + ASSERT_EQ(ret, UMF_RESULT_SUCCESS); + ASSERT_NE(name, nullptr); + EXPECT_EQ(strncmp(name, long_name.c_str(), 63), 0); + EXPECT_EQ(name[63], '\0'); +} + +TEST_P(umfProviderTest, get_NULL_name) { + auto ret = umfMemoryProviderGetName(provider.get(), nullptr); + ASSERT_EQ(ret, UMF_RESULT_ERROR_INVALID_ARGUMENT); +} diff --git a/test/provider_devdax_memory_not_impl.cpp b/test/provider_devdax_memory_not_impl.cpp index 8924efc4e..0e36c4a51 100644 --- a/test/provider_devdax_memory_not_impl.cpp +++ b/test/provider_devdax_memory_not_impl.cpp @@ -1,4 +1,4 @@ -// Copyright (C) 2024-2025 Intel Corporation +// Copyright (C) 2024-2026 Intel Corporation // Under the Apache License v2.0 with LLVM Exceptions. See LICENSE.TXT. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception @@ -25,6 +25,9 @@ TEST_F(test, devdax_provider_not_implemented) { umf_result = umfDevDaxMemoryProviderParamsSetProtection(nullptr, 0); EXPECT_EQ(umf_result, UMF_RESULT_ERROR_NOT_SUPPORTED); + umf_result = umfDevDaxMemoryProviderParamsSetName(nullptr, "test"); + EXPECT_EQ(umf_result, UMF_RESULT_ERROR_NOT_SUPPORTED); + const umf_memory_provider_ops_t *ops = umfDevDaxMemoryProviderOps(); EXPECT_EQ(ops, nullptr); } diff --git a/test/provider_file_memory.cpp b/test/provider_file_memory.cpp index c59223eb5..45c310ba8 100644 --- a/test/provider_file_memory.cpp +++ b/test/provider_file_memory.cpp @@ -1,7 +1,10 @@ -// Copyright (C) 2024-2025 Intel Corporation +// Copyright (C) 2024-2026 Intel Corporation // Under the Apache License v2.0 with LLVM Exceptions. See LICENSE.TXT. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +#include +#include + #include #include #include @@ -380,6 +383,11 @@ TEST(FileProviderName, default_name_null_handle) { EXPECT_STREQ(name, "FILE"); } +TEST(FileProviderName, null_name) { + auto ret = umfFileMemoryProviderOps()->get_name(nullptr, nullptr); + EXPECT_EQ(ret, UMF_RESULT_ERROR_INVALID_ARGUMENT); +} + TEST_P(FileProviderParamsDefault, free_size_0_ptr_not_null) { umf_result_t umf_result = umfMemoryProviderFree(provider.get(), INVALID_PTR, 0); @@ -624,3 +632,85 @@ TEST_P(FileProviderParamsShared, IPC_file_not_exist) { umf_result = umfMemoryProviderFree(provider.get(), ptr, size); ASSERT_EQ(umf_result, UMF_RESULT_SUCCESS); } + +TEST_F(test, create_NULL_params) { + umf_memory_provider_handle_t hProvider = nullptr; + auto ret = umfMemoryProviderCreate(umfFileMemoryProviderOps(), nullptr, + &hProvider); + ASSERT_EQ(ret, UMF_RESULT_ERROR_INVALID_ARGUMENT); + ASSERT_EQ(hProvider, nullptr); +} + +TEST_F(test, params_NULL_name) { + umf_file_memory_provider_params_handle_t params = nullptr; + umf_result_t ret = umfFileMemoryProviderParamsCreate(FILE_PATH, ¶ms); + ASSERT_EQ(ret, UMF_RESULT_SUCCESS); + ASSERT_NE(params, nullptr); + + ret = umfFileMemoryProviderParamsSetName(params, nullptr); + ASSERT_EQ(ret, UMF_RESULT_ERROR_INVALID_ARGUMENT); + + ret = umfFileMemoryProviderParamsDestroy(params); + ASSERT_EQ(ret, UMF_RESULT_SUCCESS); + + ret = umfFileMemoryProviderParamsSetName(nullptr, "test"); + ASSERT_EQ(ret, UMF_RESULT_ERROR_INVALID_ARGUMENT); +} + +TEST_F(test, params_long_name) { + auto params = get_file_params_default(FILE_PATH); + ASSERT_NE(params.get(), nullptr); + + const std::string long_name(1024, 'x'); + auto ret = + umfFileMemoryProviderParamsSetName(params.get(), long_name.c_str()); + ASSERT_EQ(ret, UMF_RESULT_SUCCESS); + + umf_test::provider_unique_handle_t provider; + providerCreateExt({umfFileMemoryProviderOps(), params.get()}, &provider); + ASSERT_NE(provider.get(), nullptr); + + const char *name = nullptr; + ret = umfMemoryProviderGetName(provider.get(), &name); + ASSERT_EQ(ret, UMF_RESULT_SUCCESS); + ASSERT_NE(name, nullptr); + EXPECT_EQ(strncmp(name, long_name.c_str(), 63), 0); + EXPECT_EQ(name[63], '\0'); +} + +TEST_P(FileProviderParamsDefault, get_NULL_name) { + auto ret = umfMemoryProviderGetName(provider.get(), nullptr); + ASSERT_EQ(ret, UMF_RESULT_ERROR_INVALID_ARGUMENT); +} + +TEST_F(test, params_protection_flag) { + umf_file_memory_provider_params_handle_t params = nullptr; + umf_result_t ret = umfFileMemoryProviderParamsCreate(FILE_PATH, ¶ms); + ASSERT_EQ(ret, UMF_RESULT_SUCCESS); + ASSERT_NE(params, nullptr); + + // test all valid combinations + for (unsigned protection = UMF_PROTECTION_NONE; + protection < (UMF_PROTECTION_MAX - 1) << 1; ++protection) { + ret = umfFileMemoryProviderParamsSetProtection(params, protection); + ASSERT_EQ(ret, UMF_RESULT_SUCCESS); + } + + ret = umfFileMemoryProviderParamsDestroy(params); + ASSERT_EQ(ret, UMF_RESULT_SUCCESS); +} + +TEST_F(test, params_invalid_protection_flag) { + auto params = get_file_params_default(FILE_PATH); + ASSERT_NE(params.get(), nullptr); + + auto ret = umfFileMemoryProviderParamsSetProtection(params.get(), 0); + EXPECT_EQ(ret, UMF_RESULT_ERROR_INVALID_ARGUMENT); + + for (unsigned bit = 4; bit < std::numeric_limits::digits; ++bit) { + unsigned invalid_protection = 1u << bit; + ret = umfFileMemoryProviderParamsSetProtection(params.get(), + invalid_protection); + EXPECT_EQ(ret, UMF_RESULT_ERROR_INVALID_ARGUMENT); + } +} diff --git a/test/provider_file_memory_not_impl.cpp b/test/provider_file_memory_not_impl.cpp index 84ae5ffdf..dd002f49d 100644 --- a/test/provider_file_memory_not_impl.cpp +++ b/test/provider_file_memory_not_impl.cpp @@ -1,4 +1,4 @@ -// Copyright (C) 2024-2025 Intel Corporation +// Copyright (C) 2024-2026 Intel Corporation // Under the Apache License v2.0 with LLVM Exceptions. See LICENSE.TXT. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception @@ -28,6 +28,9 @@ TEST_F(test, file_provider_not_implemented) { umfFileMemoryProviderParamsSetVisibility(nullptr, UMF_MEM_MAP_PRIVATE); EXPECT_EQ(umf_result, UMF_RESULT_ERROR_NOT_SUPPORTED); + umf_result = umfFileMemoryProviderParamsSetName(nullptr, "test"); + EXPECT_EQ(umf_result, UMF_RESULT_ERROR_NOT_SUPPORTED); + const umf_memory_provider_ops_t *ops = umfFileMemoryProviderOps(); EXPECT_EQ(ops, nullptr); } diff --git a/test/providers/provider_cuda.cpp b/test/providers/provider_cuda.cpp index 8a3d729eb..4ec195a19 100644 --- a/test/providers/provider_cuda.cpp +++ b/test/providers/provider_cuda.cpp @@ -1,4 +1,4 @@ -// Copyright (C) 2024-2025 Intel Corporation +// Copyright (C) 2024-2026 Intel Corporation // Under the Apache License v2.0 with LLVM Exceptions. See LICENSE.TXT. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception @@ -277,6 +277,10 @@ TEST_P(umfCUDAProviderTest, getName) { ASSERT_EQ(umf_result, UMF_RESULT_SUCCESS); ASSERT_STREQ(name, "CUDA"); + // negative case - pass NULL as a name pointer + umf_result = umfMemoryProviderGetName(provider, nullptr); + ASSERT_EQ(umf_result, UMF_RESULT_ERROR_INVALID_ARGUMENT); + umfMemoryProviderDestroy(provider); } @@ -363,6 +367,16 @@ TEST(umfCUDAProviderOps, default_name_null_handle) { EXPECT_STREQ(name, "CUDA"); } +TEST(umfCUDAProviderOps, null_name) { + auto ret = umfCUDAMemoryProviderOps()->get_name(nullptr, nullptr); + EXPECT_EQ(ret, UMF_RESULT_ERROR_INVALID_ARGUMENT); +} + +TEST_P(umfCUDAProviderTest, cudaProviderNullName) { + EXPECT_EQ(umfCUDAMemoryProviderParamsSetName(params, nullptr), + UMF_RESULT_ERROR_INVALID_ARGUMENT); +} + TEST_P(umfCUDAProviderTest, allocInvalidSize) { CUcontext expected_current_context = get_current_context(); // create CUDA provider @@ -470,6 +484,12 @@ TEST_P(umfCUDAProviderTest, cudaProviderNullParams) { res = umfCUDAMemoryProviderParamsSetMemoryType(nullptr, expected_memory_type); EXPECT_EQ(res, UMF_RESULT_ERROR_INVALID_ARGUMENT); + + res = umfCUDAMemoryProviderParamsSetAllocFlags(nullptr, 0); + EXPECT_EQ(res, UMF_RESULT_ERROR_INVALID_ARGUMENT); + + res = umfCUDAMemoryProviderParamsSetName(nullptr, "test"); + EXPECT_EQ(res, UMF_RESULT_ERROR_INVALID_ARGUMENT); } TEST_P(umfCUDAProviderTest, cudaProviderInvalidCreate) { diff --git a/test/providers/provider_cuda_not_impl.cpp b/test/providers/provider_cuda_not_impl.cpp index 9ddea2048..6d8f6abba 100644 --- a/test/providers/provider_cuda_not_impl.cpp +++ b/test/providers/provider_cuda_not_impl.cpp @@ -1,4 +1,4 @@ -// Copyright (C) 2024-2025 Intel Corporation +// Copyright (C) 2024-2026 Intel Corporation // Under the Apache License v2.0 with LLVM Exceptions. See LICENSE.TXT. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception @@ -29,6 +29,9 @@ TEST_F(test, cuda_provider_not_implemented) { result = umfCUDAMemoryProviderParamsSetAllocFlags(hParams, 0); ASSERT_EQ(result, UMF_RESULT_ERROR_NOT_SUPPORTED); + result = umfCUDAMemoryProviderParamsSetName(hParams, "test"); + ASSERT_EQ(result, UMF_RESULT_ERROR_NOT_SUPPORTED); + const umf_memory_provider_ops_t *ops = umfCUDAMemoryProviderOps(); ASSERT_EQ(ops, nullptr); } diff --git a/test/providers/provider_level_zero.cpp b/test/providers/provider_level_zero.cpp index 113348eb7..d90605aa9 100644 --- a/test/providers/provider_level_zero.cpp +++ b/test/providers/provider_level_zero.cpp @@ -445,6 +445,10 @@ TEST_P(umfLevelZeroProviderTest, getName) { ASSERT_EQ(umf_result, UMF_RESULT_SUCCESS); ASSERT_STREQ(name, "LEVEL_ZERO"); + // negative case - pass NULL as a name pointer + umf_result = umfMemoryProviderGetName(provider, nullptr); + ASSERT_EQ(umf_result, UMF_RESULT_ERROR_INVALID_ARGUMENT); + umfMemoryProviderDestroy(provider); } @@ -567,6 +571,24 @@ TEST(umfLevelZeroProviderOps, default_name_null_handle) { EXPECT_STREQ(name, "LEVEL_ZERO"); } +TEST(umfLevelZeroProviderOps, null_name) { + auto ret = umfLevelZeroMemoryProviderOps()->get_name(nullptr, nullptr); + EXPECT_EQ(ret, UMF_RESULT_ERROR_INVALID_ARGUMENT); +} + +TEST_F(test, levelZeroProviderNullName) { + umf_level_zero_memory_provider_params_handle_t params = nullptr; + auto ret = umfLevelZeroMemoryProviderParamsCreate(¶ms); + ASSERT_EQ(ret, UMF_RESULT_SUCCESS); + ASSERT_NE(params, nullptr); + + ret = umfLevelZeroMemoryProviderParamsSetName(params, nullptr); + EXPECT_EQ(ret, UMF_RESULT_ERROR_INVALID_ARGUMENT); + + ret = umfLevelZeroMemoryProviderParamsDestroy(params); + EXPECT_EQ(ret, UMF_RESULT_SUCCESS); +} + TEST_P(umfLevelZeroProviderTest, allocInvalidSize) { if (GetParam() == UMF_MEMORY_TYPE_HOST) { // For host memory, even with relaxed limits, the allocation may @@ -638,6 +660,9 @@ TEST_P(umfLevelZeroProviderTest, levelZeroProviderNullParams) { res = umfLevelZeroMemoryProviderParamsSetDeviceOrdinal(nullptr, 0); EXPECT_EQ(res, UMF_RESULT_ERROR_INVALID_ARGUMENT); + + res = umfLevelZeroMemoryProviderParamsSetName(nullptr, "test"); + EXPECT_EQ(res, UMF_RESULT_ERROR_INVALID_ARGUMENT); } TEST_P(umfLevelZeroProviderTest, setDeviceOrdinalValid) { diff --git a/test/providers/provider_level_zero_not_impl.cpp b/test/providers/provider_level_zero_not_impl.cpp index dfbd4a2b2..eea0d6ce3 100644 --- a/test/providers/provider_level_zero_not_impl.cpp +++ b/test/providers/provider_level_zero_not_impl.cpp @@ -1,4 +1,4 @@ -// Copyright (C) 2024-2025 Intel Corporation +// Copyright (C) 2024-2026 Intel Corporation // Under the Apache License v2.0 with LLVM Exceptions. See LICENSE.TXT. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception @@ -38,6 +38,9 @@ TEST_F(test, level_zero_provider_not_implemented) { result = umfLevelZeroMemoryProviderParamsSetDeviceOrdinal(hParams, 0); ASSERT_EQ(result, UMF_RESULT_ERROR_NOT_SUPPORTED); + result = umfLevelZeroMemoryProviderParamsSetName(hParams, "test"); + ASSERT_EQ(result, UMF_RESULT_ERROR_NOT_SUPPORTED); + const umf_memory_provider_ops_t *ops = umfLevelZeroMemoryProviderOps(); ASSERT_EQ(ops, nullptr); }