Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion include/umf/providers/provider_file_memory.h
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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);

Expand Down
8 changes: 8 additions & 0 deletions src/provider/provider_file_memory.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
59 changes: 59 additions & 0 deletions test/provider_devdax_memory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#endif

#include <cerrno>
#include <string>

#include <umf/experimental/ctl.h>
#include <umf/memory_provider.h>
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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, &params);
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);
}
5 changes: 4 additions & 1 deletion test/provider_devdax_memory_not_impl.cpp
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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);
}
92 changes: 91 additions & 1 deletion test/provider_file_memory.cpp
Original file line number Diff line number Diff line change
@@ -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 <limits>
#include <string>

#include <umf/experimental/ctl.h>
#include <umf/memory_provider.h>
#include <umf/providers/provider_file_memory.h>
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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, &params);
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, &params);
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);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test can never fail. umfFileMemoryProviderParamsSetProtection() only checks
that hParams is not NULL. It accepts any value. The devdax provider checks the
flags and has a params_invalid_protection_flag test, but the file provider does
not check them. Please add the same check to the file provider and add a
params_invalid_protection_flag test. Or remove this test.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

check + new test added

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<unsigned>::digits; ++bit) {
unsigned invalid_protection = 1u << bit;
ret = umfFileMemoryProviderParamsSetProtection(params.get(),
invalid_protection);
EXPECT_EQ(ret, UMF_RESULT_ERROR_INVALID_ARGUMENT);
}
}
5 changes: 4 additions & 1 deletion test/provider_file_memory_not_impl.cpp
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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);
}
22 changes: 21 additions & 1 deletion test/providers/provider_cuda.cpp
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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);
Comment thread
lukaszstolarczuk marked this conversation as resolved.
ASSERT_EQ(umf_result, UMF_RESULT_ERROR_INVALID_ARGUMENT);

umfMemoryProviderDestroy(provider);
}

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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");
Comment thread
lukaszstolarczuk marked this conversation as resolved.
EXPECT_EQ(res, UMF_RESULT_ERROR_INVALID_ARGUMENT);
}

TEST_P(umfCUDAProviderTest, cudaProviderInvalidCreate) {
Expand Down
5 changes: 4 additions & 1 deletion test/providers/provider_cuda_not_impl.cpp
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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);
}
25 changes: 25 additions & 0 deletions test/providers/provider_level_zero.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down Expand Up @@ -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(&params);
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
Expand Down Expand Up @@ -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) {
Expand Down
5 changes: 4 additions & 1 deletion test/providers/provider_level_zero_not_impl.cpp
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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);
}
Loading