| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
#include <dlfcn.h> |
| 17 |
#include <pthread.h> |
| 18 |
#include <stdio.h> |
| 19 |
#include <stdlib.h> |
| 20 |
#include <limits.h> |
| 21 |
|
| 22 |
static void *s2n_load_dynamic_lib(void *ctx) |
| 23 |
{ |
| 24 |
const char *s2n_so_path = ctx; |
| 25 |
|
| 26 |
void *s2n_so = dlopen(s2n_so_path, RTLD_NOW); |
| 27 |
if (!s2n_so) { |
| 28 |
printf("Error dynamically loading libs2n\n"); |
| 29 |
printf("%s\n", dlerror()); |
| 30 |
exit(1); |
| 31 |
} |
| 32 |
|
| 33 |
int (*s2n_init_dl)(void) = NULL; |
| 34 |
*(void **) (&s2n_init_dl) = dlsym(s2n_so, "s2n_init"); |
| 35 |
if (dlerror()) { |
| 36 |
printf("Error dynamically loading s2n_init\n"); |
| 37 |
exit(1); |
| 38 |
} |
| 39 |
|
| 40 |
int (*s2n_cleanup_final_dl)(void) = NULL; |
| 41 |
*(void **) (&s2n_cleanup_final_dl) = dlsym(s2n_so, "s2n_cleanup_final"); |
| 42 |
if (dlerror()) { |
| 43 |
printf("Error dynamically loading s2n_cleanup_final\n"); |
| 44 |
exit(1); |
| 45 |
} |
| 46 |
|
| 47 |
int (*s2n_errno_location_dl)(void) = NULL; |
| 48 |
*(void **) (&s2n_errno_location_dl) = dlsym(s2n_so, "s2n_errno_location"); |
| 49 |
if (dlerror()) { |
| 50 |
printf("Error dynamically loading s2n_errno_location\n"); |
| 51 |
exit(1); |
| 52 |
} |
| 53 |
|
| 54 |
const char *(*s2n_strerror_debug_dl)(int error, const char *lang) = NULL; |
| 55 |
*(void **) (&s2n_strerror_debug_dl) = dlsym(s2n_so, "s2n_strerror_debug"); |
| 56 |
if (dlerror()) { |
| 57 |
printf("Error dynamically loading s2n_strerror_debug\n"); |
| 58 |
exit(1); |
| 59 |
} |
| 60 |
|
| 61 |
if ((*s2n_init_dl)()) { |
| 62 |
int s2n_errno = (*s2n_errno_location_dl)(); |
| 63 |
fprintf(stderr, "Error calling s2n_init: '%s'\n", (*s2n_strerror_debug_dl)(s2n_errno, "EN")); |
| 64 |
exit(1); |
| 65 |
} |
| 66 |
if ((*s2n_cleanup_final_dl)()) { |
| 67 |
int s2n_errno = (*s2n_errno_location_dl)(); |
| 68 |
fprintf(stderr, "Error calling s2n_cleanup_final: '%s'\n", (*s2n_strerror_debug_dl)(s2n_errno, "EN")); |
| 69 |
exit(1); |
| 70 |
} |
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
return NULL; |
| 84 |
} |
| 85 |
|
| 86 |
int main(int argc, char *argv[]) |
| 87 |
{ |
| 88 |
if (argc != 2) { |
| 89 |
printf("Usage: s2n_dynamic_load_test <path to s2n shared object>\n"); |
| 90 |
exit(1); |
| 91 |
} |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
for (size_t i = 0; i <= PTHREAD_KEYS_MAX + 1; i++) { |
| 100 |
pthread_t thread_id = { 0 }; |
| 101 |
if (pthread_create(&thread_id, NULL, &s2n_load_dynamic_lib, argv[1])) { |
| 102 |
printf("Error creating thread at loop index: %li\n", i); |
| 103 |
exit(1); |
| 104 |
} |
| 105 |
if (pthread_join(thread_id, NULL)) { |
| 106 |
printf("Error joining thread at loop index: %li\n", i); |
| 107 |
exit(1); |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
return 0; |
| 112 |
} |