diff --git a/.github/workflows/proxy_lib.yml b/.github/workflows/proxy_lib.yml index 678b40eff..b3250d310 100644 --- a/.github/workflows/proxy_lib.yml +++ b/.github/workflows/proxy_lib.yml @@ -58,7 +58,7 @@ jobs: - name: Run "ctest --output-on-failure" with proxy library working-directory: ${{env.BUILD_DIR}} - run: LD_PRELOAD=./lib/libumf_proxy.so ctest --output-on-failure + run: LD_PRELOAD=./lib/libumf_proxy.so ctest --output-on-failure -E "proxy_lib_memoryPool" - name: Run "./test/umf_test-memoryPool" with proxy library working-directory: ${{env.BUILD_DIR}} diff --git a/CMakeLists.txt b/CMakeLists.txt index 3ce5028c4..8c99a0d01 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -381,8 +381,16 @@ if(WINDOWS) ) endif() endif() + # set UMF_PROXY_LIB_ENABLED -if(UMF_PROXY_LIB_BASED_ON_POOL STREQUAL SCALABLE) +if(UMF_LINK_HWLOC_STATICALLY) + message( + STATUS + "Disabling the proxy library, because HWLOC is set to link statically which is not supported" + ) +elseif(UMF_DISABLE_HWLOC) + message(STATUS "Disabling the proxy library, because HWLOC is disabled") +elseif(UMF_PROXY_LIB_BASED_ON_POOL STREQUAL SCALABLE) if(UMF_POOL_SCALABLE_ENABLED) set(UMF_PROXY_LIB_ENABLED ON) set(PROXY_LIB_USES_SCALABLE_POOL ON) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 9eb883cfa..460342447 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -276,8 +276,6 @@ install(TARGETS umf EXPORT ${PROJECT_NAME}-targets) add_subdirectory(pool) -if(UMF_PROXY_LIB_ENABLED - AND NOT UMF_LINK_HWLOC_STATICALLY - AND NOT UMF_DISABLE_HWLOC) +if(UMF_PROXY_LIB_ENABLED) add_subdirectory(proxy_lib) endif() diff --git a/src/provider/provider_os_memory.c b/src/provider/provider_os_memory.c index 3b8bbbe91..96cf5b65a 100644 --- a/src/provider/provider_os_memory.c +++ b/src/provider/provider_os_memory.c @@ -1251,14 +1251,14 @@ static umf_result_t os_open_ipc_handle(void *provider, void *providerIpcData, umf_result_t ret = UMF_RESULT_SUCCESS; int fd; - if (os_provider->shm_name[0]) { - fd = utils_shm_open(os_provider->shm_name); + if (os_ipc_data->shm_name[0]) { + fd = utils_shm_open(os_ipc_data->shm_name); if (fd <= 0) { LOG_PERR("opening a shared memory file (%s) failed", - os_provider->shm_name); + os_ipc_data->shm_name); return UMF_RESULT_ERROR_UNKNOWN; } - (void)utils_shm_unlink(os_provider->shm_name); + (void)utils_shm_unlink(os_ipc_data->shm_name); } else { umf_result_t umf_result = utils_duplicate_fd(os_ipc_data->pid, os_ipc_data->fd, &fd); diff --git a/src/proxy_lib/proxy_lib.c b/src/proxy_lib/proxy_lib.c index ca8d69315..2730b9f17 100644 --- a/src/proxy_lib/proxy_lib.c +++ b/src/proxy_lib/proxy_lib.c @@ -124,8 +124,6 @@ void proxy_lib_create_common(void) { } else if (utils_env_var_has_str("UMF_PROXY", "page.disposition=shared-shm")) { - LOG_DEBUG("proxy_lib: using the MAP_SHARED visibility mode with the " - "named shared memory"); os_params.visibility = UMF_MEM_MAP_SHARED; memset(shm_name, 0, NAME_MAX); @@ -145,9 +143,8 @@ void proxy_lib_create_common(void) { exit(-1); } - umf_result = - umfPoolCreate(umfPoolManagerOps(), OS_memory_provider, NULL, - UMF_POOL_CREATE_FLAG_DISABLE_TRACKING, &Proxy_pool); + umf_result = umfPoolCreate(umfPoolManagerOps(), OS_memory_provider, NULL, 0, + &Proxy_pool); if (umf_result != UMF_RESULT_SUCCESS) { LOG_ERR("creating UMF pool manager failed"); exit(-1); diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index a10562038..f80c5f0fd 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -330,10 +330,7 @@ add_umf_test( LIBS ${UMF_UTILS_FOR_TEST}) # tests for the proxy library -if(UMF_PROXY_LIB_ENABLED - AND UMF_BUILD_SHARED_LIBRARY - AND NOT UMF_DISABLE_HWLOC - AND NOT UMF_LINK_HWLOC_STATICALLY) +if(UMF_PROXY_LIB_ENABLED AND UMF_BUILD_SHARED_LIBRARY) add_umf_test( NAME proxy_lib_basic SRCS ${BA_SOURCES_FOR_TEST} test_proxy_lib.cpp @@ -402,6 +399,17 @@ if(LINUX) add_umf_ipc_test(TEST ipc_os_prov_anon_fd) add_umf_ipc_test(TEST ipc_os_prov_shm) + if(UMF_PROXY_LIB_ENABLED AND UMF_BUILD_SHARED_LIBRARY) + build_umf_test( + NAME + ipc_os_prov_proxy + SRCS + ipc_os_prov_proxy.c + LIBS + ${UMF_UTILS_FOR_TEST}) + add_umf_ipc_test(TEST ipc_os_prov_proxy) + endif() + build_umf_test( NAME ipc_devdax_prov_consumer diff --git a/test/ipc_os_prov_proxy.c b/test/ipc_os_prov_proxy.c new file mode 100644 index 000000000..28e3d3d48 --- /dev/null +++ b/test/ipc_os_prov_proxy.c @@ -0,0 +1,113 @@ +/* + * Copyright (C) 2024 Intel Corporation + * + * Under the Apache License v2.0 with LLVM Exceptions. See LICENSE.TXT. + * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + */ + +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +#include "utils_load_library.h" + +umf_result_t (*pfnGetIPCHandle)(const void *ptr, umf_ipc_handle_t *umfIPCHandle, + size_t *size); + +// This is a test for a scenario where a user process is started using the +// LD_PRELOAD with the UMF Proxy Lib and this process uses UMF by loading +// libumf.so at runtime. +// In this test, we expect that all allocations made by the process will be +// handled by UMF in the Proxy Lib and added to the UMF tracker so that they +// can be used later in the UMF IPC API. +int main(void) { + int ret = 0; + + int fd = open("/proc/self/maps", O_RDONLY); + if (fd == -1) { + return -1; + } + + // read the "/proc/self/maps" file until the "libumf_proxy.so" of the maps + // is found or EOF is reached. + const size_t size_buf = 8192; + char buf[size_buf]; + size_t nbytes = 1; + char *found = NULL; + while (nbytes > 0 && found == NULL) { + memset(buf, 0, nbytes); // erase previous data + nbytes = read(fd, buf, size_buf); + found = strstr(buf, "libumf_proxy.so"); + } + (void)close(fd); + + if (found == NULL) { + fprintf( + stderr, + "test binary not run under LD_PRELOAD with \"libumf_proxy.so\"\n"); + return -1; + } + + // open the UMF library and get umfGetIPCHandle() function + const char *umf_lib_name = "libumf.so"; + void *umf_lib_handle = utils_open_library(umf_lib_name, 0); + if (umf_lib_handle == NULL) { + fprintf(stderr, "utils_open_library: UMF library not found (%s)\n", + umf_lib_name); + return -1; + } + + *(void **)&pfnGetIPCHandle = + utils_get_symbol_addr(umf_lib_handle, "umfGetIPCHandle", umf_lib_name); + if (pfnGetIPCHandle == NULL) { + ret = -1; + goto err_close_lib; + } + + // create simple allocation - it should be added to the UMF tracker if the + // process was launched under UMF Proxy Lib + size_t size = 2137; + void *ptr = malloc(size); + if (ptr == NULL) { + ret = -1; + goto err_close_lib; + } + + fprintf(stderr, "Allocated memory - %zu\n", size); + *(int *)ptr = 0x23; + + // get IPC handle of the allocation + umf_ipc_handle_t ipc_handle = NULL; + size_t ipc_handle_size = 0; + umf_result_t res = pfnGetIPCHandle(ptr, &ipc_handle, &ipc_handle_size); + if (res != UMF_RESULT_SUCCESS) { + fprintf(stderr, "pfnGetIPCHandle() failed!\n"); + ret = -1; + goto err_free_mem; + } + + // check if we got valid data + if (ipc_handle == NULL || ipc_handle_size == 0) { + fprintf(stderr, "pfnGetIPCHandle() couldn't find the handle data!\n"); + ret = -1; + goto err_free_mem; + } + + fprintf(stderr, "Got IPCHandle for memory - %p | size - %zu\n", + (void *)ipc_handle, ipc_handle_size); + +err_free_mem: + free(ptr); + +err_close_lib: + utils_close_library(umf_lib_handle); + + return ret; +} diff --git a/test/ipc_os_prov_proxy.sh b/test/ipc_os_prov_proxy.sh new file mode 100755 index 000000000..03fe7083f --- /dev/null +++ b/test/ipc_os_prov_proxy.sh @@ -0,0 +1,16 @@ +# +# Copyright (C) 2024 Intel Corporation +# +# Under the Apache License v2.0 with LLVM Exceptions. See LICENSE.TXT. +# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +# + +#!/bin/bash + +set -e + +UMF_LOG_VAL="level:debug;flush:debug;output:stderr;pid:yes" +UMF_PROXY_VAL="page.disposition=shared-shm" +LD_PRELOAD_VAL="../lib/libumf_proxy.so" + +LD_PRELOAD=$LD_PRELOAD_VAL UMF_LOG=$UMF_LOG_VAL UMF_PROXY=$UMF_PROXY_VAL ./umf_test-ipc_os_prov_proxy