From 19ac34f2a026bdf5b294e202287b36c22074561b Mon Sep 17 00:00:00 2001 From: Mateusz Tabaka <tab.debugteam@gmail.com> Date: Tue, 5 Jan 2021 22:48:41 +0100 Subject: [PATCH] Extend dlfcn tests --- tests/Makefile | 11 ++++ tests/dlfcn.c | 102 +++++++++++++++++++++++++++++++----- tests/expected/dlfcn.stdout | 5 ++ tests/sharedlib.c | 10 ++++ 4 files changed, 114 insertions(+), 14 deletions(-) create mode 100644 tests/sharedlib.c diff --git a/tests/Makefile b/tests/Makefile index 240d8818..9574fe69 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -233,12 +233,23 @@ DYNAMIC_FLAGS+=\ -lc DEPS=../sysroot +else +DYNAMIC_FLAGS+=\ + -Wl,-rpath=\$$ORIGIN endif bins_static/%: %.c $(DEPS) mkdir -p "$$(dirname "$@")" $(CC) "$<" -o "$@" $(FLAGS) $(STATIC_FLAGS) +bins_dynamic/%.so: %.c $(DEPS) + mkdir -p "$$(dirname "$@")" + $(CC) "$<" -o "$@" -shared -fpic $(FLAGS) $(DYNAMIC_FLAGS) + +bins_dynamic/dlfcn: dlfcn.c bins_dynamic/sharedlib.so $(DEPS) + mkdir -p "$$(dirname "$@")" + $(CC) "$<" -o "$@" $(FLAGS) $(DYNAMIC_FLAGS) + bins_dynamic/%: %.c $(DEPS) mkdir -p "$$(dirname "$@")" $(CC) "$<" -o "$@" $(FLAGS) $(DYNAMIC_FLAGS) diff --git a/tests/dlfcn.c b/tests/dlfcn.c index 16729e1c..47e2f69b 100644 --- a/tests/dlfcn.c +++ b/tests/dlfcn.c @@ -5,24 +5,98 @@ int add(int a, int b) { - return a + b; + return a + b; } +void test_dlopen_null() +{ + void* handle = dlopen(NULL, RTLD_LAZY); + if (!handle) { + printf("dlopen(NULL) failed\n"); + exit(1); + } + int (*f)(int, int) = dlsym(handle, "add"); + if (!f) { + printf("dlsym(handle, add) failed\n"); + exit(2); + } + int a = 22; + int b = 33; + printf("add(%d, %d) = %d\n", a, b, f(a, b)); + dlclose(handle); +} + +void test_dlopen_libc() +{ + void* handle = dlopen("libc.so.6", RTLD_LAZY); + if (!handle) { + printf("dlopen(libc.so.6) failed\n"); + exit(1); + } + int (*f)(const char*) = dlsym(handle, "puts"); + if (!f) { + printf("dlsym(handle, puts) failed\n"); + exit(2); + } + f("puts from dlopened libc"); + dlclose(handle); +} + + +void test_dlsym_function() +{ + void* handle = dlopen("sharedlib.so", RTLD_LAZY); + if (!handle) { + printf("dlopen(sharedlib.so) failed\n"); + exit(1); + } + void (*f)() = dlsym(handle, "print"); + if (!f) { + printf("dlsym(handle, print) failed\n"); + exit(2); + } + f(); + dlclose(handle); +} + +void test_dlsym_global_var() +{ + void* handle = dlopen("sharedlib.so", RTLD_LAZY); + if (!handle) { + printf("dlopen(sharedlib.so) failed\n"); + exit(1); + } + int* global_var = dlsym(handle, "global_var"); + if (!global_var) { + printf("dlsym(handle, global_var) failed\n"); + exit(2); + } + printf("main: global_var == %d\n", *global_var); + dlclose(handle); +} + +void test_dlsym_tls_var() +{ + void* handle = dlopen("sharedlib.so", RTLD_LAZY); + if (!handle) { + printf("dlopen(sharedlib.so) failed\n"); + exit(1); + } + int* tls_var = dlsym(handle, "tls_var"); + if (!tls_var) { + printf("dlsym(handle, tls_var) failed\n"); + exit(2); + } + printf("main: tls_var == %d\n", *tls_var); + dlclose(handle); +} int main() { - void* handle = dlopen(NULL, RTLD_LAZY); - if (!handle) { - printf("dlopen(NULL) failed\n"); - exit(1); - } - int (*f)(int, int) = dlsym(handle, "add"); - if (!f) { - printf("dlsym(handle, add) failed\n"); - exit(2); - } - int a = 22; - int b = 33; - printf("add(%d, %d) = %d\n", a, b, f(a, b)); + test_dlopen_null(); + test_dlopen_libc(); + test_dlsym_function(); + test_dlsym_global_var(); + test_dlsym_tls_var(); } diff --git a/tests/expected/dlfcn.stdout b/tests/expected/dlfcn.stdout index 36ae1185..5b51826b 100644 --- a/tests/expected/dlfcn.stdout +++ b/tests/expected/dlfcn.stdout @@ -1 +1,6 @@ add(22, 33) = 55 +puts from dlopened libc +sharedlib: global_var == 42 +sharedlib: tls_var == 21 +main: global_var == 42 +main: tls_var == 21 diff --git a/tests/sharedlib.c b/tests/sharedlib.c new file mode 100644 index 00000000..fe2dbf82 --- /dev/null +++ b/tests/sharedlib.c @@ -0,0 +1,10 @@ +#include <stdio.h> + +int global_var = 42; +_Thread_local int tls_var = 21; + +void print() +{ + fprintf(stdout, "sharedlib: global_var == %d\n", global_var); + fprintf(stdout, "sharedlib: tls_var == %d\n", tls_var); +} -- GitLab