Test atexit handlers
[openssl.git] / test / shlibloadtest.c
1 /*
2  * Copyright 2016-2018 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the OpenSSL license (the "License").  You may not use
5  * this file except in compliance with the License.  You can obtain a copy
6  * in the file LICENSE in the source distribution or at
7  * https://www.openssl.org/source/license.html
8  */
9
10 #include <stdio.h>
11 #include <string.h>
12 #include <stdlib.h>
13 #include <openssl/opensslv.h>
14 #include <openssl/ssl.h>
15 #include <openssl/ossl_typ.h>
16 #include "internal/dso_conf.h"
17
18 typedef void DSO;
19
20 typedef const SSL_METHOD * (*TLS_method_t)(void);
21 typedef SSL_CTX * (*SSL_CTX_new_t)(const SSL_METHOD *meth);
22 typedef void (*SSL_CTX_free_t)(SSL_CTX *);
23 typedef int (*OPENSSL_init_crypto_t)(uint64_t, void *);
24 typedef int (*OPENSSL_atexit_t)(void (*handler)(void));
25 typedef unsigned long (*ERR_get_error_t)(void);
26 typedef unsigned long (*OpenSSL_version_num_t)(void);
27 typedef DSO * (*DSO_dsobyaddr_t)(void (*addr)(void), int flags);
28 typedef int (*DSO_free_t)(DSO *dso);
29
30 typedef enum test_types_en {
31     CRYPTO_FIRST,
32     SSL_FIRST,
33     JUST_CRYPTO,
34     DSO_REFTEST,
35     NO_ATEXIT
36 } TEST_TYPE;
37
38 static TEST_TYPE test_type;
39 static const char *path_crypto;
40 static const char *path_ssl;
41 static const char *path_atexit;
42
43 #ifdef DSO_DLFCN
44
45 # include <dlfcn.h>
46
47 # define SHLIB_INIT NULL
48
49 typedef void *SHLIB;
50 typedef void *SHLIB_SYM;
51
52 static int shlib_load(const char *filename, SHLIB *lib)
53 {
54     int dl_flags = (RTLD_GLOBAL|RTLD_LAZY);
55 #ifdef _AIX
56     if (filename[strlen(filename) - 1] == ')')
57         dl_flags |= RTLD_MEMBER;
58 #endif
59     *lib = dlopen(filename, dl_flags);
60     return *lib == NULL ? 0 : 1;
61 }
62
63 static int shlib_sym(SHLIB lib, const char *symname, SHLIB_SYM *sym)
64 {
65     *sym = dlsym(lib, symname);
66     return *sym != NULL;
67 }
68
69 static int shlib_close(SHLIB lib)
70 {
71     return dlclose(lib) != 0 ? 0 : 1;
72 }
73 #endif
74
75 #ifdef DSO_WIN32
76
77 # include <windows.h>
78
79 # define SHLIB_INIT 0
80
81 typedef HINSTANCE SHLIB;
82 typedef void *SHLIB_SYM;
83
84 static int shlib_load(const char *filename, SHLIB *lib)
85 {
86     *lib = LoadLibraryA(filename);
87     return *lib == NULL ? 0 : 1;
88 }
89
90 static int shlib_sym(SHLIB lib, const char *symname, SHLIB_SYM *sym)
91 {
92     *sym = (SHLIB_SYM)GetProcAddress(lib, symname);
93     return *sym != NULL;
94 }
95
96 static int shlib_close(SHLIB lib)
97 {
98     return FreeLibrary(lib) == 0 ? 0 : 1;
99 }
100 #endif
101
102
103 #if defined(DSO_DLFCN) || defined(DSO_WIN32)
104
105 static void atexit_handler(void)
106 {
107     FILE *atexit_file = fopen(path_atexit, "w");
108
109     if (atexit_file == NULL)
110         return;
111
112     fprintf(atexit_file, "atexit() run\n");
113     fclose(atexit_file);
114 }
115
116 static int test_lib(void)
117 {
118     SHLIB ssllib = SHLIB_INIT;
119     SHLIB cryptolib = SHLIB_INIT;
120     SSL_CTX *ctx;
121     union {
122         void (*func)(void);
123         SHLIB_SYM sym;
124     } symbols[3];
125     TLS_method_t myTLS_method;
126     SSL_CTX_new_t mySSL_CTX_new;
127     SSL_CTX_free_t mySSL_CTX_free;
128     ERR_get_error_t myERR_get_error;
129     OpenSSL_version_num_t myOpenSSL_version_num;
130     OPENSSL_atexit_t myOPENSSL_atexit;
131     int result = 0;
132
133     switch (test_type) {
134     case JUST_CRYPTO:
135     case DSO_REFTEST:
136     case NO_ATEXIT:
137     case CRYPTO_FIRST:
138         if (!shlib_load(path_crypto, &cryptolib)) {
139             fprintf(stderr, "Failed to load libcrypto\n");
140             goto end;
141         }
142         if (test_type != CRYPTO_FIRST)
143             break;
144         /* Fall through */
145
146     case SSL_FIRST:
147         if (!shlib_load(path_ssl, &ssllib)) {
148             fprintf(stderr, "Failed to load libssl\n");
149             goto end;
150         }
151         if (test_type != SSL_FIRST)
152             break;
153         if (!shlib_load(path_crypto, &cryptolib)) {
154             fprintf(stderr, "Failed to load libcrypto\n");
155             goto end;
156         }
157         break;
158     }
159
160     if (test_type == NO_ATEXIT) {
161         OPENSSL_init_crypto_t myOPENSSL_init_crypto;
162
163         if (!shlib_sym(cryptolib, "OPENSSL_init_crypto", &symbols[0].sym)) {
164             fprintf(stderr, "Failed to load OPENSSL_init_crypto symbol\n");
165             goto end;
166         }
167         myOPENSSL_init_crypto = (OPENSSL_init_crypto_t)symbols[0].func;
168         if (!myOPENSSL_init_crypto(OPENSSL_INIT_NO_ATEXIT, NULL)) {
169             fprintf(stderr, "Failed to initialise libcrypto\n");
170             goto end;
171         }
172     }
173
174     if (test_type != JUST_CRYPTO
175             && test_type != DSO_REFTEST
176             && test_type != NO_ATEXIT) {
177         if (!shlib_sym(ssllib, "TLS_method", &symbols[0].sym)
178                 || !shlib_sym(ssllib, "SSL_CTX_new", &symbols[1].sym)
179                 || !shlib_sym(ssllib, "SSL_CTX_free", &symbols[2].sym)) {
180             fprintf(stderr, "Failed to load libssl symbols\n");
181             goto end;
182         }
183         myTLS_method = (TLS_method_t)symbols[0].func;
184         mySSL_CTX_new = (SSL_CTX_new_t)symbols[1].func;
185         mySSL_CTX_free = (SSL_CTX_free_t)symbols[2].func;
186         ctx = mySSL_CTX_new(myTLS_method());
187         if (ctx == NULL) {
188             fprintf(stderr, "Failed to create SSL_CTX\n");
189             goto end;
190         }
191         mySSL_CTX_free(ctx);
192     }
193
194     if (!shlib_sym(cryptolib, "ERR_get_error", &symbols[0].sym)
195            || !shlib_sym(cryptolib, "OpenSSL_version_num", &symbols[1].sym)
196            || !shlib_sym(cryptolib, "OPENSSL_atexit", &symbols[2].sym)) {
197         fprintf(stderr, "Failed to load libcrypto symbols\n");
198         goto end;
199     }
200     myERR_get_error = (ERR_get_error_t)symbols[0].func;
201     if (myERR_get_error() != 0) {
202         fprintf(stderr, "Unexpected ERR_get_error() response\n");
203         goto end;
204     }
205
206     myOpenSSL_version_num = (OpenSSL_version_num_t)symbols[1].func;
207     if (myOpenSSL_version_num()  != OPENSSL_VERSION_NUMBER) {
208         fprintf(stderr, "Invalid library version number\n");
209         goto end;
210     }
211
212     myOPENSSL_atexit = (OPENSSL_atexit_t)symbols[2].func;
213     if (!myOPENSSL_atexit(atexit_handler)) {
214         fprintf(stderr, "Failed to register atexit handler\n");
215         goto end;
216     }
217
218     if (test_type == DSO_REFTEST) {
219 # ifdef DSO_DLFCN
220         DSO_dsobyaddr_t myDSO_dsobyaddr;
221         DSO_free_t myDSO_free;
222
223         /*
224          * This is resembling the code used in ossl_init_base() and
225          * OPENSSL_atexit() to block unloading the library after dlclose().
226          * We are not testing this on Windows, because it is done there in a
227          * completely different way. Especially as a call to DSO_dsobyaddr()
228          * will always return an error, because DSO_pathbyaddr() is not
229          * implemented there.
230          */
231         if (!shlib_sym(cryptolib, "DSO_dsobyaddr", &symbols[0].sym)
232                 || !shlib_sym(cryptolib, "DSO_free", &symbols[1].sym)) {
233             fprintf(stderr, "Unable to load DSO symbols\n");
234             goto end;
235         }
236
237         myDSO_dsobyaddr = (DSO_dsobyaddr_t)symbols[0].func;
238         myDSO_free = (DSO_free_t)symbols[1].func;
239
240         {
241             DSO *hndl;
242             /* use known symbol from crypto module */
243             hndl = myDSO_dsobyaddr((void (*)(void))myERR_get_error, 0);
244             if (hndl == NULL) {
245                 fprintf(stderr, "DSO_dsobyaddr() failed\n");
246                 goto end;
247             }
248             myDSO_free(hndl);
249         }
250 # endif /* DSO_DLFCN */
251     }
252
253     switch (test_type) {
254     case JUST_CRYPTO:
255     case DSO_REFTEST:
256     case NO_ATEXIT:
257     case CRYPTO_FIRST:
258         if (!shlib_close(cryptolib)) {
259             fprintf(stderr, "Failed to close libcrypto\n");
260             goto end;
261         }
262         if (test_type != CRYPTO_FIRST)
263             break;
264         /* Fall through */
265
266     case SSL_FIRST:
267         if (test_type == CRYPTO_FIRST && !shlib_close(ssllib)) {
268             fprintf(stderr, "Failed to close libssl\n");
269             goto end;
270         }
271         if (test_type != SSL_FIRST)
272             break;
273
274         if (!shlib_close(cryptolib)) {
275             fprintf(stderr, "Failed to close libcrypto\n");
276             goto end;
277         }
278         break;
279     }
280
281     result = 1;
282 end:
283     return result;
284 }
285 #endif
286
287
288 /*
289  * shlibloadtest should not use the normal test framework because we don't want
290  * it to link against libcrypto (which the framework uses). The point of the
291  * test is to check dynamic loading and unloading of libcrypto/libssl.
292  */
293 int main(int argc, char *argv[])
294 {
295     const char *p;
296
297     if (argc != 5) {
298         fprintf(stderr, "Incorrect number of arguments\n");
299         return 1;
300     }
301
302     p = argv[1];
303
304     if (strcmp(p, "-crypto_first") == 0) {
305         test_type = CRYPTO_FIRST;
306     } else if (strcmp(p, "-ssl_first") == 0) {
307         test_type = SSL_FIRST;
308     } else if (strcmp(p, "-just_crypto") == 0) {
309         test_type = JUST_CRYPTO;
310     } else if (strcmp(p, "-dso_ref") == 0) {
311         test_type = DSO_REFTEST;
312     } else if (strcmp(p, "-no_atexit") == 0) {
313         test_type = NO_ATEXIT;
314     } else {
315         fprintf(stderr, "Unrecognised argument\n");
316         return 1;
317     }
318     path_crypto = argv[2];
319     path_ssl = argv[3];
320     path_atexit = argv[4];
321     if (path_crypto == NULL || path_ssl == NULL) {
322         fprintf(stderr, "Invalid libcrypto/libssl path\n");
323         return 1;
324     }
325
326 #if defined(DSO_DLFCN) || defined(DSO_WIN32)
327     if (!test_lib())
328         return 1;
329 #endif
330     return 0;
331 }