Skip to content

Commit 10a5b7f

Browse files
committed
add IPC test with proxy lib
1 parent 56fc006 commit 10a5b7f

File tree

5 files changed

+151
-8
lines changed

5 files changed

+151
-8
lines changed

CMakeLists.txt

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -381,8 +381,16 @@ if(WINDOWS)
381381
)
382382
endif()
383383
endif()
384+
384385
# set UMF_PROXY_LIB_ENABLED
385-
if(UMF_PROXY_LIB_BASED_ON_POOL STREQUAL SCALABLE)
386+
if(UMF_LINK_HWLOC_STATICALLY)
387+
message(
388+
STATUS
389+
"Disabling the proxy library, because HWLOC is set to link statically which is not supported"
390+
)
391+
elseif(UMF_DISABLE_HWLOC)
392+
message(STATUS "Disabling the proxy library, because HWLOC is disabled")
393+
elseif(UMF_PROXY_LIB_BASED_ON_POOL STREQUAL SCALABLE)
386394
if(UMF_POOL_SCALABLE_ENABLED)
387395
set(UMF_PROXY_LIB_ENABLED ON)
388396
set(PROXY_LIB_USES_SCALABLE_POOL ON)

src/CMakeLists.txt

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -278,8 +278,6 @@ install(TARGETS umf EXPORT ${PROJECT_NAME}-targets)
278278

279279
add_subdirectory(pool)
280280

281-
if(UMF_PROXY_LIB_ENABLED
282-
AND NOT UMF_LINK_HWLOC_STATICALLY
283-
AND NOT UMF_DISABLE_HWLOC)
281+
if(UMF_PROXY_LIB_ENABLED)
284282
add_subdirectory(proxy_lib)
285283
endif()

test/CMakeLists.txt

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -334,10 +334,7 @@ add_umf_test(
334334
LIBS ${UMF_UTILS_FOR_TEST})
335335

336336
# tests for the proxy library
337-
if(UMF_PROXY_LIB_ENABLED
338-
AND UMF_BUILD_SHARED_LIBRARY
339-
AND NOT UMF_DISABLE_HWLOC
340-
AND NOT UMF_LINK_HWLOC_STATICALLY)
337+
if(UMF_PROXY_LIB_ENABLED AND UMF_BUILD_SHARED_LIBRARY)
341338
add_umf_test(
342339
NAME proxy_lib_basic
343340
SRCS ${BA_SOURCES_FOR_TEST} test_proxy_lib.cpp
@@ -406,6 +403,17 @@ if(LINUX)
406403
add_umf_ipc_test(TEST ipc_os_prov_anon_fd)
407404
add_umf_ipc_test(TEST ipc_os_prov_shm)
408405

406+
if(UMF_PROXY_LIB_ENABLED AND UMF_BUILD_SHARED_LIBRARY)
407+
build_umf_test(
408+
NAME
409+
ipc_os_prov_proxy
410+
SRCS
411+
ipc_os_prov_proxy.c
412+
LIBS
413+
${UMF_UTILS_FOR_TEST})
414+
add_umf_ipc_test(TEST ipc_os_prov_proxy)
415+
endif()
416+
409417
build_umf_test(
410418
NAME
411419
ipc_devdax_prov_consumer

test/ipc_os_prov_proxy.c

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
/*
2+
* Copyright (C) 2024 Intel Corporation
3+
*
4+
* Under the Apache License v2.0 with LLVM Exceptions. See LICENSE.TXT.
5+
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
*/
7+
8+
#include <dlfcn.h>
9+
#include <fcntl.h>
10+
#include <stdio.h>
11+
#include <stdlib.h>
12+
#include <string.h>
13+
#include <sys/stat.h>
14+
#include <sys/types.h>
15+
#include <unistd.h>
16+
17+
#include <umf/ipc.h>
18+
19+
#include "utils_load_library.h"
20+
21+
umf_result_t (*pfnGetIPCHandle)(const void *ptr, umf_ipc_handle_t *umfIPCHandle,
22+
size_t *size);
23+
24+
// This is a test for a scenario where a user process is started using the
25+
// LD_PRELOAD with the UMF Proxy Lib and this process uses UMF by loading
26+
// libumf.so at runtime.
27+
// In this test, we expect that all allocations made by the process will be
28+
// handled by UMF in the Proxy Lib and added to the UMF tracker so that they
29+
// can be used later in the UMF IPC API.
30+
int main(void) {
31+
int ret = 0;
32+
33+
int fd = open("/proc/self/maps", O_RDONLY);
34+
if (fd == -1) {
35+
return -1;
36+
}
37+
38+
// read the "/proc/self/maps" file until the "libumf_proxy.so" of the maps
39+
// is found or EOF is reached.
40+
const size_t size_buf = 8192;
41+
char buf[size_buf];
42+
size_t nbytes = 1;
43+
char *found = NULL;
44+
while (nbytes > 0 && found == NULL) {
45+
memset(buf, 0, nbytes); // erase previous data
46+
nbytes = read(fd, buf, size_buf);
47+
found = strstr(buf, "libumf_proxy.so");
48+
}
49+
(void)close(fd);
50+
51+
if (found == NULL) {
52+
fprintf(
53+
stderr,
54+
"test binary not run under LD_PRELOAD with \"libumf_proxy.so\"\n");
55+
return -1;
56+
}
57+
58+
// open the UMF library and get umfGetIPCHandle() function
59+
const char *umf_lib_name = "libumf.so";
60+
void *umf_lib_handle = utils_open_library(umf_lib_name, 0);
61+
if (umf_lib_handle == NULL) {
62+
fprintf(stderr, "utils_open_library: UMF library not found (%s)\n",
63+
umf_lib_name);
64+
return -1;
65+
}
66+
67+
*(void **)&pfnGetIPCHandle =
68+
utils_get_symbol_addr(umf_lib_handle, "umfGetIPCHandle", umf_lib_name);
69+
if (pfnGetIPCHandle == NULL) {
70+
ret = -1;
71+
goto err_close_lib;
72+
}
73+
74+
// create simple allocation - it should be added to the UMF tracker if the
75+
// process was launched under UMF Proxy Lib
76+
size_t size = 2137;
77+
void *ptr = malloc(size);
78+
if (ptr == NULL) {
79+
ret = -1;
80+
goto err_close_lib;
81+
}
82+
83+
fprintf(stderr, "Allocated memory - %zu\n", size);
84+
*(int *)ptr = 0x23;
85+
86+
// get IPC handle of the allocation
87+
umf_ipc_handle_t ipc_handle = NULL;
88+
size_t ipc_handle_size = 0;
89+
umf_result_t res = pfnGetIPCHandle(ptr, &ipc_handle, &ipc_handle_size);
90+
if (res != UMF_RESULT_SUCCESS) {
91+
fprintf(stderr, "pfnGetIPCHandle() failed!\n");
92+
ret = -1;
93+
goto err_free_mem;
94+
}
95+
96+
// check if we got valid data
97+
if (ipc_handle == NULL || ipc_handle_size == 0) {
98+
fprintf(stderr, "pfnGetIPCHandle() couldn't find the handle data!\n");
99+
ret = -1;
100+
goto err_free_mem;
101+
}
102+
103+
fprintf(stderr, "Got IPCHandle for memory - %p | size - %zu\n",
104+
(void *)ipc_handle, ipc_handle_size);
105+
106+
err_free_mem:
107+
free(ptr);
108+
109+
err_close_lib:
110+
utils_close_library(umf_lib_handle);
111+
112+
return ret;
113+
}

test/ipc_os_prov_proxy.sh

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#
2+
# Copyright (C) 2024 Intel Corporation
3+
#
4+
# Under the Apache License v2.0 with LLVM Exceptions. See LICENSE.TXT.
5+
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
#
7+
8+
#!/bin/bash
9+
10+
set -e
11+
12+
UMF_LOG_VAL="level:debug;flush:debug;output:stderr;pid:yes"
13+
UMF_PROXY_VAL="page.disposition=shared-shm"
14+
LD_PRELOAD_VAL="../lib/libumf_proxy.so"
15+
16+
LD_PRELOAD=$LD_PRELOAD_VAL UMF_LOG=$UMF_LOG_VAL UMF_PROXY=$UMF_PROXY_VAL ./umf_test-ipc_os_prov_proxy

0 commit comments

Comments
 (0)