Skip to content

Commit

Permalink
Merge pull request #154 from LLNL/develop
Browse files Browse the repository at this point in the history
Release v0.0.7
  • Loading branch information
hariharan-devarajan authored Jun 17, 2024
2 parents 81401b9 + 7436bb8 commit 9cc0088
Show file tree
Hide file tree
Showing 19 changed files with 312 additions and 36 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,7 @@
build
tags
**/tags
*.core
.cache
.idea
.vscode
11 changes: 11 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ set(LIBTOOL_INTERFACE 2)
set(LIBTOOL_REVISION 3)
set(LIBTOOL_AGE 2)

set(GOTCHA_VERSION "(1, 0, 7)")


set(DEFAULT_SYMBOL_VISIBILITY hidden)

if(GOTCHA_ENABLE_TESTS)
Expand All @@ -22,6 +25,7 @@ if(GOTCHA_ENABLE_TESTS)
endif()
endif()
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/include)
include_directories(${CMAKE_BINARY_DIR}/include)
add_subdirectory(include)
add_subdirectory(src)
if(GOTCHA_ENABLE_TESTS)
Expand Down Expand Up @@ -50,3 +54,10 @@ configure_package_config_file(
install(
FILES "${CMAKE_CURRENT_BINARY_DIR}/gotcha-config.cmake" "${CMAKE_CURRENT_BINARY_DIR}/gotcha-config-version.cmake"
DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/gotcha")

# Write the configure file
configure_file("${CMAKE_SOURCE_DIR}/cmake/gotcha_config.h.in"
"${CMAKE_CURRENT_BINARY_DIR}/include/gotcha/gotcha_config.h" @ONLY)
install(
FILES "${CMAKE_CURRENT_BINARY_DIR}/include/gotcha/gotcha_config.h"
DESTINATION "${gotcha_INSTALL_INCLUDE_DIR}/gotcha/gotcha_config.h")
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
GOTCHA v1.0.6
GOTCHA v1.0.7
============
[![GOTCHA Build and Test](https://github.com/LLNL/GOTCHA/actions/workflows/build-and-test.yaml/badge.svg)](https://github.com/LLNL/GOTCHA/actions/workflows/build-and-test.yaml)
[![Coverage Status](https://coveralls.io/repos/github/LLNL/GOTCHA/badge.svg?branch=develop)](https://coveralls.io/github/LLNL/GOTCHA?branch=develop)
Expand Down
10 changes: 10 additions & 0 deletions cmake/gotcha_config.h.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#ifndef GOTCHA_CONFIG_H
#define GOTCHA_CONFIG_H

#define GOTCHA_GET_VERSION(MAJOR, MINOR, PATCH) (MAJOR * 100000 + MINOR * 100 + PATCH)
#define GOTCHA_VERSION (GOTCHA_GET_VERSION @GOTCHA_VERSION@)
#define GOTCHA_VERSION_MAJOR (GOTCHA_VERSION / 100000)
#define GOTCHA_VERSION_MINOR ((GOTCHA_VERSION / 100) % 1000)
#define GOTCHA_VERSION_PATCH (GOTCHA_VERSION % 100)

#endif /* GOTCHA_CONFIG_H */
25 changes: 25 additions & 0 deletions docs/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -178,5 +178,30 @@ The default filter of gotcha selects all libraries loaded. This function set the
.. explicit external hyperlink targets
---------------------------
Using Gotcha Version Macros
---------------------------

The source version of GOTCHA is defined by the `GOTCHA_VERSION` macro which uses the XYYYZZ format.
**Available since version 1.0.7.**
Here, X signifies the major version, Y is the minor version, and Z is the patch.
Additionally, we define `GOTCHA_VERSION_MAJOR`, `GOTCHA_VERSION_MINOR`, and `GOTCHA_VERSION_PATCH` macros for convienience.
The codes can use the macros like


.. code-block:: c
#if GOTCHA_VERSION > 100006 // this will check of version greater than 1.0.6
#endif
#if GOTCHA_VERSION_MAJOR > 1 // this will check of version greater than 2.0.0
#endif
#if GOTCHA_VERSION > GOTCHA_GET_VERSION(1,0,6) // this will check of version greater than 1.0.6
#endif
.. _`gnu constructor`: https://gcc.gnu.org/onlinedocs/gcc-4.7.2/gcc/Function-Attributes.html
.. _symbol: https://refspecs.linuxfoundation.org/LSB_3.0.0/LSB-PDA/LSB-PDA.junk/symversion.html
2 changes: 1 addition & 1 deletion docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
# The short X.Y version
version = u'1.0'
# The full version, including alpha/beta/rc tags
release = u'1.0.6'
release = u'1.0.7'


# -- General configuration ---------------------------------------------------
Expand Down
4 changes: 2 additions & 2 deletions include/gotcha/gotcha.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
#ifndef GOTCHA_H
#define GOTCHA_H

#include <gotcha/gotcha_config.h>
#include <gotcha/gotcha_types.h>
#include <link.h>

#include "gotcha/gotcha_types.h"

#if defined(__cplusplus)
extern "C" {
#endif
Expand Down
2 changes: 1 addition & 1 deletion src/gotcha.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ static void setBindingAddressPointer(struct gotcha_binding_t *in, void *value) {
writeAddress(target, value);
}

static void **getInternalBindingAddressPointer(struct internal_binding_t **in) {
void **getInternalBindingAddressPointer(struct internal_binding_t **in) {
return (void **)&((*in)->wrappee_pointer);
}

Expand Down
14 changes: 12 additions & 2 deletions src/gotcha_dl.c
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ static int per_binding(hash_key_t key, hash_data_t data,
binding->user_binding->name,
binding->associated_binding_table->tool->tool_name);

if (!binding->user_binding->name) return 0;
while (binding->next_binding) {
binding = binding->next_binding; // GCOVR_EXCL_START
debug_printf(3,
Expand Down Expand Up @@ -157,7 +158,16 @@ static void *dlsym_wrapper(void *handle, const char *symbol_name) {
debug_printf(1, "User called dlsym(%p, %s)\n", handle, symbol_name);
int result = lookup_hashtable(&function_hash_table, (hash_key_t)symbol_name,
(hash_data_t *)&binding);
if (result != -1) return binding->user_binding->wrapper_pointer;
void *val = orig_dlsym(handle, symbol_name);
void **wrappee_ptr = getInternalBindingAddressPointer(
(struct internal_binding_t **)binding->user_binding->function_handle);
if (result != -1 && (val == NULL || *wrappee_ptr == val)) {
// if the wrapper is found and the wrappee is the function requested.
// This is needed in cases where we wrap a function F1 from library A and
// we dynamically load function F1 from library B. As name is same, we need
// to make sure the wrappee are the same as well
return binding->user_binding->wrapper_pointer;
}
if (handle == RTLD_NEXT) {
struct link_map *lib = gotchas_dlsym_rtld_next_lookup(
symbol_name, __builtin_return_address(0));
Expand All @@ -168,7 +178,7 @@ static void *dlsym_wrapper(void *handle, const char *symbol_name) {
}
return NULL;
} else {
return orig_dlsym(handle, symbol_name);
return val;
}
}

Expand Down
1 change: 1 addition & 0 deletions src/gotcha_dl.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ extern void update_all_library_gots(hash_table_t *bindings);
extern long lookup_exported_symbol(const char *name, const struct link_map *lib,
void **symbol);
extern int prepare_symbol(struct internal_binding_t *binding);
extern void **getInternalBindingAddressPointer(struct internal_binding_t **in);

extern gotcha_wrappee_handle_t orig_dlopen_handle;
extern gotcha_wrappee_handle_t orig_dlsym_handle;
Expand Down
3 changes: 2 additions & 1 deletion test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ add_subdirectory(filter)
add_subdirectory(wrap_main)
#add_subdirectory(multi_agent_dlopen)
add_subdirectory(symver)
add_subdirectory(function_ptr)
add_subdirectory(function_ptr)
add_subdirectory(dispatcher)
8 changes: 8 additions & 0 deletions test/dispatcher/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
add_library(impl SHARED libimpl.c)
add_library(dispatcher SHARED libdispatcher.c)
target_link_libraries(dispatcher -ldl -lpthread)
add_executable(test_dispatcher main.c)
target_link_libraries(test_dispatcher gotcha dispatcher)
gotcha_add_test(dispatcher_test test_dispatcher)
set_property(TEST dispatcher_test APPEND PROPERTY ENVIRONMENT "GOTCHA_DEBUG=3")
set_property(TEST dispatcher_test APPEND PROPERTY ENVIRONMENT "LD_LIBRARY_PATH=${CMAKE_CURRENT_BINARY_DIR}")
67 changes: 67 additions & 0 deletions test/dispatcher/libdispatcher.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
This file is part of GOTCHA. For copyright information see the COPYRIGHT
file in the top level directory, or at
https://github.com/LLNL/gotcha/blob/master/COPYRIGHT
This program is free software; you can redistribute it and/or modify it under
the terms of the GNU Lesser General Public License (as published by the Free
Software Foundation) version 2.1 dated February 1999. This program is
distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
without even the IMPLIED WARRANTY OF MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the terms and conditions of the GNU Lesser General Public License
for more details. You should have received a copy of the GNU Lesser General
Public License along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/

#define _GNU_SOURCE
#include <assert.h>
#include <dlfcn.h>
#include <pthread.h>
#include <stdio.h>

int foo(void);
int bar(void);

static void* impl_lib;
static int (*impl_foo)(void);
static int (*impl_bar)(void);

static pthread_once_t init_once = PTHREAD_ONCE_INIT;
void dispatch_init(void) {
fprintf(stderr, "Ed dispatch_init()\n");

impl_lib = dlopen("libimpl.so", RTLD_NOW);
assert(impl_lib);
impl_foo = dlsym(impl_lib, "foo");
assert(impl_foo);
impl_bar = dlsym(impl_lib, "bar");
assert(impl_bar);

int ret = impl_bar();

fprintf(stderr, "Ld dispatch_init() = %d\n", ret);
}

int foo(void) {
fprintf(stderr, "Ed foo()\n");

pthread_once(&init_once, dispatch_init);

int ret = impl_bar() + impl_foo();

fprintf(stderr, "Ld foo()\n");

return ret;
}

int bar(void) {
fprintf(stderr, "Ed bar()\n");

pthread_once(&init_once, dispatch_init);

int ret = impl_bar();

fprintf(stderr, "Ld bar()\n");

return ret;
}
31 changes: 31 additions & 0 deletions test/dispatcher/libimpl.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
This file is part of GOTCHA. For copyright information see the COPYRIGHT
file in the top level directory, or at
https://github.com/LLNL/gotcha/blob/master/COPYRIGHT
This program is free software; you can redistribute it and/or modify it under
the terms of the GNU Lesser General Public License (as published by the Free
Software Foundation) version 2.1 dated February 1999. This program is
distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
without even the IMPLIED WARRANTY OF MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the terms and conditions of the GNU Lesser General Public License
for more details. You should have received a copy of the GNU Lesser General
Public License along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/

#define _GNU_SOURCE
#include <stdio.h>

int foo(void) {
fprintf(stderr, "Ei foo()\n");
fprintf(stderr, "Li foo()\n");

return 42;
}

int bar(void) {
fprintf(stderr, "Ei bar()\n");
fprintf(stderr, "Li bar()\n");

return 23;
}
58 changes: 58 additions & 0 deletions test/dispatcher/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
This file is part of GOTCHA. For copyright information see the COPYRIGHT
file in the top level directory, or at
https://github.com/LLNL/gotcha/blob/master/COPYRIGHT
This program is free software; you can redistribute it and/or modify it under
the terms of the GNU Lesser General Public License (as published by the Free
Software Foundation) version 2.1 dated February 1999. This program is
distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
without even the IMPLIED WARRANTY OF MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the terms and conditions of the GNU Lesser General Public License
for more details. You should have received a copy of the GNU Lesser General
Public License along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/

#define _GNU_SOURCE
#include <gotcha/gotcha.h>
#include <stdio.h>

int foo(void);
int bar(void);

static gotcha_wrappee_handle_t handle_foo;
static gotcha_wrappee_handle_t handle_bar;

static int do_foo(void) {
fprintf(stderr, "Ew foo()\n");

typeof(&do_foo) orig_foo = gotcha_get_wrappee(handle_foo);
int ret = orig_foo();

fprintf(stderr, "Lw foo() = %d\n", ret);

return ret;
}

static int do_bar(void) {
fprintf(stderr, "Ew bar()\n");

typeof(&do_bar) orig_bar = gotcha_get_wrappee(handle_bar);
int ret = orig_bar();

fprintf(stderr, "Lw bar() = %d\n", ret);

return ret;
}

static struct gotcha_binding_t bindings[] = {
{"foo", do_foo, &handle_foo},
{"bar", do_bar, &handle_bar},
};

int main(int ac, char *av[]) {
gotcha_wrap(bindings, 2, "test");
printf("%d\n", foo());

return 0;
}
1 change: 0 additions & 1 deletion test/dlopen/num.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ Public License along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/

extern void mark_had_error();
extern int return_five();

int return_four() {
Expand Down
10 changes: 5 additions & 5 deletions test/dlopen/num2.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ Public License along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/

extern void mark_had_error();
extern int return_five();
int return_four() { return 6; }

int return_four() { return 4; }

int test_return_five() { return return_five(); }
int return_six() {
/* Intentional bug, gotcha wrapping will correct this to return 6 */
return 7;
}
Loading

0 comments on commit 9cc0088

Please sign in to comment.