Debian Codegraph @

crt/s2n/codebuild/bin/s2n_dynamic_load_test.c

raw symbols SCIP
1 /*
2 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License").
5 * You may not use this file except in compliance with the License.
6 * A copy of the License is located at
7 *
8 * http://aws.amazon.com/apache2.0
9 *
10 * or in the "license" file accompanying this file. This file is distributed
11 * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12 * express or implied. See the License for the specific language governing
13 * permissions and limitations under the License.
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 /* TODO: https://github.com/aws/s2n-tls/issues/4827
73 * This dlclose call invokes the pthread key destructor that
74 * asserts that the s2n-tls library is initialized, which at this point
75 * is not, due to the s2n_cleanup_final call. This is a bug.
76 if (dlclose(s2n_so)) {
77 printf("Error closing libs2n\n");
78 printf("%s\n", dlerror());
79 exit(1);
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 /* s2n-tls library can be dynamically loaded and cleaned up safely
94 *
95 * We can't use any s2n test macros because this test doesn't get linked to
96 * s2n during compile-time. This test is in a loop to make sure that we are
97 * cleaning up pthread keys properly.
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 }