File: s2n_dynamic_load_test.c

package info (click to toggle)
aws-crt-python 0.24.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 75,932 kB
  • sloc: ansic: 418,984; python: 23,626; makefile: 6,035; sh: 4,075; ruby: 208; java: 82; perl: 73; cpp: 25; xml: 11
file content (112 lines) | stat: -rw-r--r-- 3,611 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/*
 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License").
 * You may not use this file except in compliance with the License.
 * A copy of the License is located at
 *
 *  http://aws.amazon.com/apache2.0
 *
 * or in the "license" file accompanying this file. This file is distributed
 * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
 * express or implied. See the License for the specific language governing
 * permissions and limitations under the License.
 */

#include <dlfcn.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>

static void *s2n_load_dynamic_lib(void *ctx)
{
    const char *s2n_so_path = ctx;

    void *s2n_so = dlopen(s2n_so_path, RTLD_NOW);
    if (!s2n_so) {
        printf("Error dynamically loading libs2n\n");
        printf("%s\n", dlerror());
        exit(1);
    }

    int (*s2n_init_dl)(void) = NULL;
    *(void **) (&s2n_init_dl) = dlsym(s2n_so, "s2n_init");
    if (dlerror()) {
        printf("Error dynamically loading s2n_init\n");
        exit(1);
    }

    int (*s2n_cleanup_final_dl)(void) = NULL;
    *(void **) (&s2n_cleanup_final_dl) = dlsym(s2n_so, "s2n_cleanup_final");
    if (dlerror()) {
        printf("Error dynamically loading s2n_cleanup_final\n");
        exit(1);
    }

    int (*s2n_errno_location_dl)(void) = NULL;
    *(void **) (&s2n_errno_location_dl) = dlsym(s2n_so, "s2n_errno_location");
    if (dlerror()) {
        printf("Error dynamically loading s2n_errno_location\n");
        exit(1);
    }

    const char *(*s2n_strerror_debug_dl)(int error, const char *lang) = NULL;
    *(void **) (&s2n_strerror_debug_dl) = dlsym(s2n_so, "s2n_strerror_debug");
    if (dlerror()) {
        printf("Error dynamically loading s2n_strerror_debug\n");
        exit(1);
    }

    if ((*s2n_init_dl)()) {
        int s2n_errno = (*s2n_errno_location_dl)();
        fprintf(stderr, "Error calling s2n_init: '%s'\n", (*s2n_strerror_debug_dl)(s2n_errno, "EN"));
        exit(1);
    }
    if ((*s2n_cleanup_final_dl)()) {
        int s2n_errno = (*s2n_errno_location_dl)();
        fprintf(stderr, "Error calling s2n_cleanup_final: '%s'\n", (*s2n_strerror_debug_dl)(s2n_errno, "EN"));
        exit(1);
    }

    /* TODO: https://github.com/aws/s2n-tls/issues/4827
     * This dlclose call invokes the pthread key destructor that
     * asserts that the s2n-tls library is initialized, which at this point
     * is not, due to the s2n_cleanup_final call. This is a bug.
    if (dlclose(s2n_so)) {
        printf("Error closing libs2n\n");
        printf("%s\n", dlerror());
        exit(1);
    }
    */

    return NULL;
}

int main(int argc, char *argv[])
{
    if (argc != 2) {
        printf("Usage: s2n_dynamic_load_test <path to s2n shared object>\n");
        exit(1);
    }

    /* s2n-tls library can be dynamically loaded and cleaned up safely 
     *
     * We can't use any s2n test macros because this test doesn't get linked to 
     * s2n during compile-time. This test is in a loop to make sure that we are
     * cleaning up pthread keys properly.
     */
    for (size_t i = 0; i <= PTHREAD_KEYS_MAX + 1; i++) {
        pthread_t thread_id = { 0 };
        if (pthread_create(&thread_id, NULL, &s2n_load_dynamic_lib, argv[1])) {
            printf("Error creating thread at loop index: %li\n", i);
            exit(1);
        }
        if (pthread_join(thread_id, NULL)) {
            printf("Error joining thread at loop index: %li\n", i);
            exit(1);
        }
    }

    return 0;
}