Skip to content

Commit

Permalink
Merge pull request #21 from lukemartinlogan/master
Browse files Browse the repository at this point in the history
Add RealApi
  • Loading branch information
lukemartinlogan authored Jan 28, 2025
2 parents 0fca775 + 51aafae commit ae8f981
Show file tree
Hide file tree
Showing 14 changed files with 324 additions and 19 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ cmake*
build
Testing
*.env
*.env.cmake
CMakePresets.json
vcpkg_installed

Expand Down
46 changes: 36 additions & 10 deletions .vscode/CMakePresetsTempl.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// scspkg build profile m=cmake path=.env.cmake
// cp CMakePresetsTempl.json ../CMakePresets.json
{
"version": 3,
"cmakeMinimumRequired": {
Expand All @@ -11,27 +13,51 @@
"hidden": false,
"generator": "Ninja",
"binaryDir": "${sourceDir}/build",
"toolchainFile": "${sourceDir}/.vscode/scspkg.cmake",
"cacheVariables": {
"CMAKE_BUILD_TYPE": "Debug",
"CMAKE_INSTALL_PREFIX": "/home/luke/Documents/Projects/scspkg/packages/hermes_shm"
},
"environment": {
// scspkg build profile --method=vscode
"CMAKE_INSTALL_PREFIX": "/home/llogan/Documents/Projects/scspkg/packages/hermes_shm",
"HSHM_ENABLE_COMPRESS": "ON",
"HSHM_ENABLE_ENCRYPT": "ON",
"HSHM_RPC_THALLIUM": "ON",
"BUILD_HSHM_TESTS": "OFF",
"HSHM_ENABLE_OPENMP": "ON",
"HSHM_ENABLE_MPI": "ON",
"HSHM_ENABLE_ROCM": "OFF",
"BUILD_HSHM_BENCHMARKS": "OFF"
}
},
{
"name": "rocm",
"hidden": false,
"generator": "Ninja",
"binaryDir": "${sourceDir}/build",
"cacheVariables": {
"CMAKE_BUILD_TYPE": "Debug",
"CMAKE_INSTALL_PREFIX": "/home/llogan/Documents/Projects/scspkg/packages/hermes_shm",
"HSHM_ENABLE_COMPRESS": "ON",
"HSHM_ENABLE_ENCRYPT": "ON",
"HSHM_ENABLE_ROCM": "ON",
"HSHM_RPC_THALLIUM": "ON",
"BUILD_HSHM_TESTS": "ON",
"BUILD_SHARED_LIBS": "ON",
"HSHM_ENABLE_OPENMP": "ON",
"HSHM_ENABLE_MPI": "ON",
"BUILD_HSHM_BENCHMARKS": "OFF"
}
},
{
"name": "release",
"hidden": false,
"generator": "Ninja",
"binaryDir": "${sourceDir}/build",
"toolchainFile": "${sourceDir}/.vscode/scspkg.cmake",
"cacheVariables": {
"CMAKE_BUILD_TYPE": "Release",
"CMAKE_INSTALL_PREFIX": "/home/luke/Documents/Projects/scspkg/packages/hermes_shm"
},
"environment": {
// scspkg build profile --method=vscode
"CMAKE_INSTALL_PREFIX": "/home/llogan/Documents/Projects/scspkg/packages/hermes_shm",
"HSHM_ENABLE_COMPRESS": "ON",
"HSHM_ENABLE_ENCRYPT": "ON",
"HSHM_ENABLE_ROCM": "ON",
"HSHM_ENABLE_MPI": "ON",
"BUILD_HSHM_TESTS": "OFF"
}
}
]
Expand Down
2 changes: 1 addition & 1 deletion CMake/HermesShmCommonConfig.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ macro(hshm_enable_cuda CXX_STANDARD)
set(CMAKE_CUDA_ARCHITECTURES native)
endif()

set(CMAKE_CUDA_FLAGS "${CMAKE_CUDA_FLAGS} --forward-unknown-to-host-compiler -diag-suppress=177,20014,20011,200012")
set(CMAKE_CUDA_FLAGS "${CMAKE_CUDA_FLAGS} --forward-unknown-to-host-compiler -diag-suppress=177,20014,20011,20012")
enable_language(CUDA)
endmacro()

Expand Down
7 changes: 7 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@ cmake_minimum_required(VERSION 3.25)
project(hermes_shm LANGUAGES C CXX)
set(CMAKE_CXX_STANDARD 17)

# ------------------------------------------------------------------------------
# DOTENV
# ------------------------------------------------------------------------------
if(EXISTS ${CMAKE_SOURCE_DIR}/.env.cmake)
include(.env.cmake)
endif()

# ------------------------------------------------------------------------------
# Global variables
# ------------------------------------------------------------------------------
Expand Down
1 change: 1 addition & 0 deletions include/hermes_shm/memory/backend/cuda_malloc.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ class CudaMalloc : public MemoryBackend {
bool shm_deserialize(const hshm::chararr &url) {
(void)url;
HSHM_THROW_ERROR(SHMEM_NOT_SUPPORTED);
return false;
}

/** Detach the mapped memory */
Expand Down
2 changes: 1 addition & 1 deletion include/hermes_shm/memory/memory.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ union AllocatorId {
/**
* Constructor which sets major & minor
* */
HSHM_INLINE_CROSS_FUN explicit AllocatorId(uint32_t major, uint32_t minor) {
HSHM_INLINE_CROSS_FUN explicit AllocatorId(i32 major, i32 minor) {
bits_.major_ = major;
bits_.minor_ = minor;
}
Expand Down
208 changes: 208 additions & 0 deletions include/hermes_shm/util/real_api.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,208 @@
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* Distributed under BSD 3-Clause license. *
* Copyright by The HDF Group. *
* Copyright by the Illinois Institute of Technology. *
* All rights reserved. *
* *
* This file is part of Hermes. The full Hermes copyright notice, including *
* terms governing use, modification, and redistribution, is contained in *
* the COPYING file, which can be found at the top directory. If you do not *
* have access to the file, you may request a copy from [email protected]. *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

#ifndef HERMES_ADAPTER_API_H
#define HERMES_ADAPTER_API_H

#undef DEPRECATED

#include <dlfcn.h>
#include <link.h>
// #include <libelf.h>
#include <gelf.h>

#define HERMES_DECL(F) F

#define REQUIRE_API(api_name) \
if (!(api_name)) { \
HELOG(kFatal, "HERMES Adapter failed to map symbol: {}", #api_name); \
}

namespace hshm {

struct RealApiIter {
const char *symbol_name_;
const char *is_intercepted_;
const char *lib_path_;

RealApiIter(const char *symbol_name, const char *is_intercepted)
: symbol_name_(symbol_name),
is_intercepted_(is_intercepted),
lib_path_(nullptr) {}
};

struct RealApi {
void *real_lib_;
bool is_intercepted_;
bool is_loaded_ = false;

static int callback(struct dl_phdr_info *info, unsigned long size,
void *data) {
auto iter = (RealApiIter *)data;
auto lib = dlopen(info->dlpi_name, RTLD_GLOBAL | RTLD_NOW);
// auto lib = dlopen(info->dlpi_name, RTLD_NOLOAD | RTLD_NOW);
if (lib) {
auto exists = dlsym(lib, iter->symbol_name_);
void *is_intercepted = (void *)dlsym(lib, iter->is_intercepted_);
if (!is_intercepted && exists && !iter->lib_path_) {
iter->lib_path_ = info->dlpi_name;
}
}
return 0;
}

/**
* @brief Construct a new RealApi object
*
* @param symbol_name A function name or variable to look for in the shared
* object
* @param is_intercepted The name of the variable indicating this is the
* interceptor object
*/
RealApi(const char *symbol_name, const char *is_intercepted) {
RealApiIter iter(symbol_name, is_intercepted);
dl_iterate_phdr(callback, (void *)&iter);
if (iter.lib_path_) {
real_lib_ = dlopen(iter.lib_path_, RTLD_GLOBAL | RTLD_NOW);
} else {
real_lib_ = RTLD_DEFAULT;
}
void *is_intercepted_ptr = (void *)dlsym(RTLD_DEFAULT, is_intercepted);
is_intercepted_ = is_intercepted_ptr != nullptr;
/* if (is_intercepted_) {
real_lib_ = RTLD_NEXT;
} else {
real_lib_ = RTLD_DEFAULT;
}*/
}
};

// template<typename PosixT>
// struct InterceptorApi {
// PosixT *posix_;
// std::string lib_path_;
// bool is_loaded_;
// int lib_fd_ = -1;
// Elf *lib_elf_ = nullptr;

// static int callback(struct dl_phdr_info *info, size_t size, void *data) {
// auto iter = (RealApiIter*)data;
// auto lib = dlopen(info->dlpi_name, RTLD_GLOBAL | RTLD_NOW);
// // auto lib = dlopen(info->dlpi_name, RTLD_NOLOAD | RTLD_NOW);
// auto exists = dlsym(lib, iter->symbol_name_);
// void *is_intercepted =
// (void*)dlsym(lib, iter->is_intercepted_);
// if (is_intercepted && exists && !iter->lib_path_) {
// iter->lib_path_ = info->dlpi_name;
// if (iter->lib_path_ == nullptr || strlen(iter->lib_path_) == 0) {
// Dl_info dl_info;
// if (dladdr(is_intercepted, &dl_info) == 0) {
// iter->lib_path_ = "";
// } else {
// iter->lib_path_ = dl_info.dli_fname;
// }
// }
// }
// return 0;
// }

// bool IsLoaded(const char *filename) {
// // Open the ELF file
// lib_fd_ = posix_->open(filename, O_RDONLY);
// if (lib_fd_ < 0) {
// return false;
// }

// // Initialize libelf
// if (elf_version(EV_CURRENT) == EV_NONE) {
// return false;
// }

// // Open ELF descriptor
// lib_elf_ = elf_begin(lib_fd_, ELF_C_READ, NULL);
// if (!lib_elf_) {
// return false;
// }

// // Get the ELF header
// GElf_Ehdr ehdr;
// if (!gelf_getehdr(lib_elf_, &ehdr)) {
// return false;
// }

// // Scan the dynamic table
// Elf_Scn *scn = NULL;
// while ((scn = elf_nextscn(lib_elf_, scn)) != NULL) {
// GElf_Shdr shdr = {};
// if (gelf_getshdr(scn, &shdr) != &shdr) {
// return false;
// }

// if (shdr.sh_type == SHT_DYNAMIC) {
// Elf_Data *data = NULL;
// data = elf_getdata(scn, data);
// if (data == NULL) {
// return false;
// }

// size_t sh_entsize = gelf_fsize(lib_elf_, ELF_T_DYN, 1, EV_CURRENT);

// for (size_t i = 0; i < shdr.sh_size / sh_entsize; i++) {
// GElf_Dyn dyn = {};
// if (gelf_getdyn(data, i, &dyn) != &dyn) {
// return false;
// }
// const char *lib_name =
// elf_strptr(lib_elf_, shdr.sh_link, dyn.d_un.d_val);
// if (lib_name) {
// auto lib = dlopen(lib_name, RTLD_NOLOAD | RTLD_NOW);
// if (!lib) {
// return false;
// }
// }
// }
// }
// }

// // Clean up
// return true;
// }

// void CloseElf() {
// if (lib_elf_) {
// elf_end(lib_elf_);
// }
// if (lib_fd_ > 0) {
// posix_->close(lib_fd_);
// }
// }

// InterceptorApi(const char *symbol_name,
// const char *is_intercepted) : is_loaded_(false) {
// is_loaded_ = true;
// return;
// posix_ = hshm::Singleton<PosixT>::GetInstance();
// RealApiIter iter(symbol_name, is_intercepted);
// dl_iterate_phdr(callback, (void*)&iter);
// if (iter.lib_path_) {
// lib_path_ = iter.lib_path_;
// is_loaded_ = IsLoaded(iter.lib_path_);
// }
// CloseElf();
// }
// };

} // namespace hshm

#undef DEPRECATED

#endif // HERMES_ADAPTER_API_H
16 changes: 9 additions & 7 deletions test/unit/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
project(hermes_shm)



set(TEST_MAIN ${CMAKE_CURRENT_SOURCE_DIR})

include_directories(${CMAKE_CURRENT_SOURCE_DIR})
Expand All @@ -13,18 +11,22 @@ add_subdirectory(types)
add_subdirectory(thread)
add_subdirectory(singleton)

if (HSHM_ENABLE_CUDA)
if(NOT HSHM_COMPILER_MSVC)
add_subdirectory(interceptor)
endif()

if(HSHM_ENABLE_CUDA)
add_subdirectory(cuda)
endif()

if (HSHM_ENABLE_ROCM)
add_subdirectory(rocm)
if(HSHM_ENABLE_ROCM)
add_subdirectory(rocm)
endif()

if (HSHM_ENABLE_COMPRESS)
if(HSHM_ENABLE_COMPRESS)
add_subdirectory(compress)
endif()

if (HSHM_ENABLE_ENCRYPT)
if(HSHM_ENABLE_ENCRYPT)
add_subdirectory(encrypt)
endif()
25 changes: 25 additions & 0 deletions test/unit/interceptor/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
project(hermes_shm)

add_library(test_interceptor_lib_stat STATIC test_lib.cc)
add_library(test_interceptor_my_lib_stat STATIC test_my_lib.cc)
add_library(test_interceptor_lib SHARED test_lib.cc)
add_library(test_interceptor_my_lib SHARED test_my_lib.cc)

add_executable(test_interceptor test_interceptor.cc)
add_dependencies(test_interceptor test_interceptor_lib)
target_link_libraries(test_interceptor PRIVATE test_interceptor_lib)

add_executable(test_interceptor_stat test_interceptor.cc)
add_dependencies(test_interceptor_stat test_interceptor_lib_stat)
target_link_libraries(test_interceptor_stat PRIVATE test_interceptor_lib_stat)

install(TARGETS
test_interceptor
test_interceptor_lib
test_interceptor_my_lib
test_interceptor_stat
test_interceptor_lib_stat
test_interceptor_my_lib_stat
LIBRARY DESTINATION ${HSHM_INSTALL_LIB_DIR}
ARCHIVE DESTINATION ${HSHM_INSTALL_LIB_DIR}
RUNTIME DESTINATION ${HSHM_INSTALL_BIN_DIR})
5 changes: 5 additions & 0 deletions test/unit/interceptor/test_interceptor.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#include <stdio.h>

#include "test_lib.h"

int main(int argc, char **argv) { printf("testfun() = %d\n", testfun()); }
Loading

0 comments on commit ae8f981

Please sign in to comment.