Skip to content

Commit 4808f31

Browse files
committed
IPC API of OS memory provider requires UMF_MEM_MAP_SHARED visibility
IPC API of OS memory provider requires the `UMF_MEM_MAP_SHARED` memory `visibility` mode (`UMF_RESULT_ERROR_INVALID_ARGUMENT` is returned otherwise). Signed-off-by: Lukasz Dorau <[email protected]>
1 parent ea53923 commit 4808f31

File tree

3 files changed

+66
-3
lines changed

3 files changed

+66
-3
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,8 @@ OS memory provider supports two types of memory mappings (set by the `visibility
143143
1) private memory mapping (`UMF_MEM_MAP_PRIVATE`)
144144
2) shared memory mapping (`UMF_MEM_MAP_SHARED` - supported on Linux only yet)
145145

146+
IPC API requires the `UMF_MEM_MAP_SHARED` memory `visibility` mode (`UMF_RESULT_ERROR_INVALID_ARGUMENT` is returned otherwise).
147+
146148
There are available two mechanisms for the shared memory mapping:
147149
1) a named shared memory object (used if the `shm_name` parameter is not NULL) or
148150
2) an anonymous file descriptor (used if the `shm_name` parameter is NULL)

src/provider/provider_os_memory.c

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1089,7 +1089,7 @@ static umf_result_t os_allocation_split(void *provider, void *ptr,
10891089
(void)totalSize;
10901090

10911091
os_memory_provider_t *os_provider = (os_memory_provider_t *)provider;
1092-
if (os_provider->fd <= 0) {
1092+
if (os_provider->fd < 0) {
10931093
return UMF_RESULT_SUCCESS;
10941094
}
10951095

@@ -1122,7 +1122,7 @@ static umf_result_t os_allocation_merge(void *provider, void *lowPtr,
11221122
(void)totalSize;
11231123

11241124
os_memory_provider_t *os_provider = (os_memory_provider_t *)provider;
1125-
if (os_provider->fd <= 0) {
1125+
if (os_provider->fd < 0) {
11261126
return UMF_RESULT_SUCCESS;
11271127
}
11281128

@@ -1152,6 +1152,11 @@ static umf_result_t os_get_ipc_handle_size(void *provider, size_t *size) {
11521152
}
11531153

11541154
os_memory_provider_t *os_provider = (os_memory_provider_t *)provider;
1155+
if (os_provider->fd < 0) {
1156+
// IPC API requires params->visibility == UMF_MEM_MAP_SHARED
1157+
LOG_ERR("memory visibility mode is not UMF_MEM_MAP_SHARED")
1158+
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
1159+
}
11551160

11561161
if (os_provider->shm_name[0]) {
11571162
// os_ipc_data_t->shm_name will be used
@@ -1171,7 +1176,9 @@ static umf_result_t os_get_ipc_handle(void *provider, const void *ptr,
11711176
}
11721177

11731178
os_memory_provider_t *os_provider = (os_memory_provider_t *)provider;
1174-
if (os_provider->fd <= 0) {
1179+
if (os_provider->fd < 0) {
1180+
// IPC API requires params->visibility == UMF_MEM_MAP_SHARED
1181+
LOG_ERR("memory visibility mode is not UMF_MEM_MAP_SHARED")
11751182
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
11761183
}
11771184

@@ -1203,6 +1210,12 @@ static umf_result_t os_put_ipc_handle(void *provider, void *providerIpcData) {
12031210
}
12041211

12051212
os_memory_provider_t *os_provider = (os_memory_provider_t *)provider;
1213+
if (os_provider->fd < 0) {
1214+
// IPC API requires params->visibility == UMF_MEM_MAP_SHARED
1215+
LOG_ERR("memory visibility mode is not UMF_MEM_MAP_SHARED")
1216+
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
1217+
}
1218+
12061219
os_ipc_data_t *os_ipc_data = (os_ipc_data_t *)providerIpcData;
12071220

12081221
if (os_ipc_data->pid != utils_getpid()) {
@@ -1229,6 +1242,12 @@ static umf_result_t os_open_ipc_handle(void *provider, void *providerIpcData,
12291242
}
12301243

12311244
os_memory_provider_t *os_provider = (os_memory_provider_t *)provider;
1245+
if (os_provider->fd < 0) {
1246+
// IPC API requires params->visibility == UMF_MEM_MAP_SHARED
1247+
LOG_ERR("memory visibility mode is not UMF_MEM_MAP_SHARED")
1248+
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
1249+
}
1250+
12321251
os_ipc_data_t *os_ipc_data = (os_ipc_data_t *)providerIpcData;
12331252
umf_result_t ret = UMF_RESULT_SUCCESS;
12341253
int fd;
@@ -1269,6 +1288,13 @@ static umf_result_t os_close_ipc_handle(void *provider, void *ptr,
12691288
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
12701289
}
12711290

1291+
os_memory_provider_t *os_provider = (os_memory_provider_t *)provider;
1292+
if (os_provider->fd < 0) {
1293+
// IPC API requires params->visibility == UMF_MEM_MAP_SHARED
1294+
LOG_ERR("memory visibility mode is not UMF_MEM_MAP_SHARED")
1295+
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
1296+
}
1297+
12721298
errno = 0;
12731299
int ret = utils_munmap(ptr, size);
12741300
// ignore error when size == 0

test/provider_os_memory.cpp

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,41 @@ TEST_P(umfProviderTest, purge_force_INVALID_POINTER) {
328328
UMF_OS_RESULT_ERROR_PURGE_FORCE_FAILED);
329329
}
330330

331+
TEST_P(umfProviderTest, get_ipc_handle_size_wrong_visibility) {
332+
size_t size;
333+
umf_result_t umf_result =
334+
umfMemoryProviderGetIPCHandleSize(provider.get(), &size);
335+
ASSERT_EQ(umf_result, UMF_RESULT_ERROR_INVALID_ARGUMENT);
336+
}
337+
338+
TEST_P(umfProviderTest, get_ipc_handle_wrong_visibility) {
339+
char providerIpcData;
340+
umf_result_t umf_result = umfMemoryProviderGetIPCHandle(
341+
provider.get(), INVALID_PTR, 1, &providerIpcData);
342+
ASSERT_EQ(umf_result, UMF_RESULT_ERROR_INVALID_ARGUMENT);
343+
}
344+
345+
TEST_P(umfProviderTest, put_ipc_handle_wrong_visibility) {
346+
char providerIpcData;
347+
umf_result_t umf_result =
348+
umfMemoryProviderPutIPCHandle(provider.get(), &providerIpcData);
349+
ASSERT_EQ(umf_result, UMF_RESULT_ERROR_INVALID_ARGUMENT);
350+
}
351+
352+
TEST_P(umfProviderTest, open_ipc_handle_wrong_visibility) {
353+
char providerIpcData;
354+
void *ptr;
355+
umf_result_t umf_result =
356+
umfMemoryProviderOpenIPCHandle(provider.get(), &providerIpcData, &ptr);
357+
ASSERT_EQ(umf_result, UMF_RESULT_ERROR_INVALID_ARGUMENT);
358+
}
359+
360+
TEST_P(umfProviderTest, close_ipc_handle_wrong_visibility) {
361+
umf_result_t umf_result =
362+
umfMemoryProviderCloseIPCHandle(provider.get(), INVALID_PTR, 1);
363+
ASSERT_EQ(umf_result, UMF_RESULT_ERROR_INVALID_ARGUMENT);
364+
}
365+
331366
GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(umfIpcTest);
332367

333368
umf_os_memory_provider_params_t osMemoryProviderParamsShared() {

0 commit comments

Comments
 (0)