|
| 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 | +} |
0 commit comments