Skip to content

Commit a696d86

Browse files
committed
add test for IPC handle size overflow
1 parent f365d3c commit a696d86

1 file changed

Lines changed: 81 additions & 1 deletion

File tree

test/ipcAPI.cpp

Lines changed: 81 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
1-
// Copyright (C) 2023-2025 Intel Corporation
1+
// Copyright (C) 2023-2026 Intel Corporation
22
// Under the Apache License v2.0 with LLVM Exceptions. See LICENSE.TXT.
33
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
44
// This file contains tests for UMF pool API
55

66
#include "ipcFixtures.hpp"
77

8+
#include "ipc_internal.h"
89
#include "provider.hpp"
910

1011
#include <umf/pools/pool_disjoint.h>
1112

13+
#include <array>
14+
#include <limits>
1215
#include <shared_mutex>
1316
#include <unordered_map>
1417

@@ -122,6 +125,83 @@ provider_mock_ipc::allocations_map_type provider_mock_ipc::allocations;
122125
static umf_memory_provider_ops_t IPC_MOCK_PROVIDER_OPS =
123126
umf_test::providerMakeCOps<provider_mock_ipc, void>();
124127

128+
struct provider_mock_ipc_huge_handle : public provider_mock_ipc {
129+
umf_result_t get_name(const char **name) noexcept {
130+
*name = "mock_ipc_huge_handle";
131+
return UMF_RESULT_SUCCESS;
132+
}
133+
134+
umf_result_t ext_get_ipc_handle_size(size_t *size) noexcept {
135+
*size = std::numeric_limits<size_t>::max();
136+
return UMF_RESULT_SUCCESS;
137+
}
138+
139+
umf_result_t ext_get_ipc_handle(const void *ptr, size_t size,
140+
void *providerIpcData) noexcept {
141+
(void)ptr;
142+
(void)size;
143+
(void)providerIpcData;
144+
ADD_FAILURE() << "IPC handle callback should not be called";
145+
return UMF_RESULT_ERROR_UNKNOWN;
146+
}
147+
};
148+
149+
static umf_memory_provider_ops_t IPC_HUGE_HANDLE_PROVIDER_OPS =
150+
umf_test::providerMakeCOps<provider_mock_ipc_huge_handle, void>();
151+
152+
static umf_test::pool_unique_handle_t createHugeHandlePool() {
153+
umf_memory_provider_handle_t hProvider = nullptr;
154+
auto ret = umfMemoryProviderCreate(&IPC_HUGE_HANDLE_PROVIDER_OPS, nullptr,
155+
&hProvider);
156+
EXPECT_EQ(ret, UMF_RESULT_SUCCESS);
157+
158+
umf_memory_pool_handle_t hPool = nullptr;
159+
ret = umfPoolCreate(umfProxyPoolOps(), hProvider, nullptr,
160+
UMF_POOL_CREATE_FLAG_OWN_PROVIDER, &hPool);
161+
if (ret != UMF_RESULT_SUCCESS) {
162+
umfMemoryProviderDestroy(hProvider);
163+
}
164+
EXPECT_EQ(ret, UMF_RESULT_SUCCESS);
165+
166+
return umf_test::pool_unique_handle_t(hPool, &umfPoolDestroy);
167+
}
168+
169+
struct ipcOverflowTest : umf_test::test {};
170+
171+
TEST_F(ipcOverflowTest, ipcHandleSizeOverflow) {
172+
auto pool = createHugeHandlePool();
173+
ASSERT_NE(pool, nullptr);
174+
175+
size_t ipcHandleSize = 0;
176+
auto ret = umfPoolGetIPCHandleSize(pool.get(), &ipcHandleSize);
177+
EXPECT_EQ(ret, UMF_RESULT_ERROR_INVALID_ARGUMENT);
178+
EXPECT_EQ(ipcHandleSize, 0u);
179+
}
180+
181+
TEST_F(ipcOverflowTest, trackingIpcCacheValueSizeOverflow) {
182+
auto pool = createHugeHandlePool();
183+
ASSERT_NE(pool, nullptr);
184+
185+
void *ptr = umfPoolMalloc(pool.get(), 4096);
186+
ASSERT_NE(ptr, nullptr);
187+
188+
umf_ipc_handler_handle_t hIPCHandler = nullptr;
189+
auto ret = umfPoolGetIPCHandler(pool.get(), &hIPCHandler);
190+
ASSERT_EQ(ret, UMF_RESULT_SUCCESS);
191+
ASSERT_NE(hIPCHandler, nullptr);
192+
193+
alignas(umf_ipc_data_t) std::array<char, sizeof(umf_ipc_data_t) + 64>
194+
ipcDataBuffer{};
195+
auto *ipcData = reinterpret_cast<umf_ipc_data_t *>(ipcDataBuffer.data());
196+
197+
ret = umfMemoryProviderGetIPCHandle(
198+
reinterpret_cast<umf_memory_provider_handle_t>(hIPCHandler), ptr, 4096,
199+
ipcData->providerIpcData);
200+
EXPECT_EQ(ret, UMF_RESULT_ERROR_INVALID_ARGUMENT);
201+
202+
umfPoolFree(pool.get(), ptr);
203+
}
204+
125205
HostMemoryAccessor hostMemoryAccessor;
126206

127207
INSTANTIATE_TEST_SUITE_P(umfIpcTestSuite, umfIpcTest,

0 commit comments

Comments
 (0)