Skip to content

Commit 96e05e8

Browse files
committed
add IPC test with proxy lib
1 parent b18cdf7 commit 96e05e8

File tree

3 files changed

+140
-0
lines changed

3 files changed

+140
-0
lines changed

test/CMakeLists.txt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -402,6 +402,17 @@ if(LINUX)
402402
add_umf_ipc_test(TEST ipc_os_prov_anon_fd)
403403
add_umf_ipc_test(TEST ipc_os_prov_shm)
404404

405+
if(UMF_PROXY_LIB_ENABLED AND UMF_BUILD_SHARED_LIBRARY)
406+
build_umf_test(
407+
NAME
408+
ipc_os_prov_proxy
409+
SRCS
410+
ipc_os_prov_proxy.c
411+
LIBS
412+
${UMF_UTILS_FOR_TEST})
413+
add_umf_ipc_test(TEST ipc_os_prov_proxy)
414+
endif()
415+
405416
build_umf_test(
406417
NAME
407418
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;tracking=enabled"
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)