Skip to content

Commit

Permalink
Adding test case for dispatcher.
Browse files Browse the repository at this point in the history
  • Loading branch information
hariharan-devarajan committed Apr 22, 2024
1 parent c09a9ef commit 1d510f2
Show file tree
Hide file tree
Showing 5 changed files with 180 additions and 1 deletion.
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}")
69 changes: 69 additions & 0 deletions test/dispatcher/libdispatcher.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
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 <dlfcn.h>
#include <math.h>
#include <pthread.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.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);
impl_foo = dlsym(impl_lib, "foo");
impl_bar = dlsym(impl_lib, "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;
}
37 changes: 37 additions & 0 deletions test/dispatcher/libimpl.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
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 <math.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.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;
}
64 changes: 64 additions & 0 deletions test/dispatcher/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
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 <math.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

int foo(void);
int bar(void);
#include <gotcha/gotcha.h>

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;
}

0 comments on commit 1d510f2

Please sign in to comment.