| 1 |
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 |
// SPDX-License-Identifier: Apache-2.0 OR ISC |
| 3 |
|
| 4 |
// This test verifies that AWS-LC's shared library can safely be |
| 5 |
// loaded/unloaded using `dlopen`/`dlclose`. |
| 6 |
// |
| 7 |
// Problem: |
| 8 |
// When a thread terminates, its thread-local data is destructed via a call to |
| 9 |
// our internal thread_local_destructor function. However, when our shared |
| 10 |
// library is unloaded (via `dlclose`) prior to the thread's termination, it may |
| 11 |
// result in a segmentation fault due to the destructor function no longer being |
| 12 |
// available. |
| 13 |
// |
| 14 |
// Building: |
| 15 |
// This binary should not be linked to `libcrypto` when built. Doing so would |
| 16 |
// result in the `dlclose` being no-op and invalidating the test. |
| 17 |
// |
| 18 |
// The path to the shared library must be passed as a compiler macro |
| 19 |
// `-DLIBCRYPTO_PATH=<<path to libcrypto.so>>` when built. |
| 20 |
|
| 21 |
#include <stdio.h> |
| 22 |
|
| 23 |
#ifdef LIBCRYPTO_PATH |
| 24 |
|
| 25 |
#include <stdint.h> |
| 26 |
#include <stdlib.h> |
| 27 |
#include <dlfcn.h> |
| 28 |
#include <pthread.h> |
| 29 |
|
| 30 |
typedef void (*fp_lc_clear_error_t)(void); |
| 31 |
typedef int (*fp_lc_tl_func_t)(void); |
| 32 |
typedef int (*fp_rand_bytes_t)(uint8_t *buf, size_t len); |
| 33 |
|
| 34 |
#define BUFFER_SIZE 16 |
| 35 |
#define TEST_ITERS 10 |
| 36 |
#define THREAD_COUNT 10 |
| 37 |
|
| 38 |
static void *cycle_thread_local_setup(void *lc_so) { |
| 39 |
uint8_t buffer[BUFFER_SIZE]; |
| 40 |
fp_lc_clear_error_t lc_clear_error = dlsym(lc_so, "ERR_clear_error"); |
| 41 |
fp_rand_bytes_t lc_rand_bytes = dlsym(lc_so, "RAND_bytes"); |
| 42 |
fp_lc_tl_func_t lc_tl_clear = dlsym(lc_so, "AWSLC_thread_local_clear"); |
| 43 |
|
| 44 |
for (int i = 0; i < TEST_ITERS; i++) { |
| 45 |
(*lc_clear_error)(); |
| 46 |
if (1 != (*lc_rand_bytes)(buffer, BUFFER_SIZE)) { |
| 47 |
fprintf(stderr, "Call to RAND_bytes failed."); |
| 48 |
exit(1); |
| 49 |
} |
| 50 |
if (1 != (*lc_tl_clear)()) { |
| 51 |
fprintf(stderr, "Call to AWSLC_thread_local_clear failed."); |
| 52 |
exit(1); |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
return NULL; |
| 57 |
} |
| 58 |
|
| 59 |
static void *load_unload(void *ctx) { |
| 60 |
const char* path = ctx; |
| 61 |
void *lc_so = dlopen(path, RTLD_NOW); |
| 62 |
fp_lc_tl_func_t lc_tl_shutdown = dlsym(lc_so, "AWSLC_thread_local_shutdown"); |
| 63 |
|
| 64 |
pthread_t thread_id[THREAD_COUNT]; |
| 65 |
for (int i = 0; i < THREAD_COUNT; i++) { |
| 66 |
if (pthread_create(&thread_id[i], NULL, cycle_thread_local_setup, lc_so)) { |
| 67 |
fprintf(stderr, "Call to pthread_create in load_unload failed."); |
| 68 |
exit(1); |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
// Also cycle on the current thread |
| 73 |
cycle_thread_local_setup(lc_so); |
| 74 |
|
| 75 |
for (int i = 0; i < THREAD_COUNT; i++) { |
| 76 |
if (pthread_join(thread_id[i], NULL)) { |
| 77 |
fprintf(stderr, "Call to pthread_join in load_unload failed."); |
| 78 |
exit(1); |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
if (1 != (*lc_tl_shutdown)()) { |
| 83 |
fprintf(stderr, "Call to AWSLC_thread_local_shutdown failed."); |
| 84 |
exit(1); |
| 85 |
} |
| 86 |
dlclose(lc_so); |
| 87 |
|
| 88 |
return NULL; |
| 89 |
} |
| 90 |
|
| 91 |
|
| 92 |
#define xstr(s) str(s) |
| 93 |
#define str(s) #s |
| 94 |
#define DYNAMIC_LIBRARY_PATH xstr(LIBCRYPTO_PATH) |
| 95 |
|
| 96 |
|
| 97 |
int main(int argc, char *argv[]) { |
| 98 |
pthread_t thread_id; |
| 99 |
if (pthread_create(&thread_id, NULL, load_unload, (void*)DYNAMIC_LIBRARY_PATH)) { |
| 100 |
fprintf(stderr, "Call to pthread_create in main failed."); |
| 101 |
exit(1); |
| 102 |
} |
| 103 |
|
| 104 |
if (pthread_join(thread_id, NULL)) { |
| 105 |
fprintf(stderr, "Call to pthread_join in main failed."); |
| 106 |
exit(1); |
| 107 |
} |
| 108 |
|
| 109 |
fprintf(stdout, "PASS\n"); |
| 110 |
fflush(stdout); |
| 111 |
return 0; |
| 112 |
} |
| 113 |
#else |
| 114 |
|
| 115 |
int main(int argc, char **argv) { |
| 116 |
fprintf(stdout, "PASS\n"); // NOLINT(clang-analyzer-security.insecureAPI.DeprecatedOrUnsafeBufferHandling) |
| 117 |
fflush(stdout); |
| 118 |
return 0; |
| 119 |
} |
| 120 |
|
| 121 |
#endif // LIBCRYPTO_PATH |