1 | /*
|
---|
2 | * Copyright 1995-2021 The OpenSSL Project Authors. All Rights Reserved.
|
---|
3 | *
|
---|
4 | * Licensed under the Apache License 2.0 (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 | #ifndef OSSL_INTERNAL_THREAD_ONCE_H
|
---|
11 | # define OSSL_INTERNAL_THREAD_ONCE_H
|
---|
12 | # pragma once
|
---|
13 |
|
---|
14 | # include <openssl/crypto.h>
|
---|
15 |
|
---|
16 | /*
|
---|
17 | * Initialisation of global data should never happen via "RUN_ONCE" inside the
|
---|
18 | * FIPS module. Global data should instead always be associated with a specific
|
---|
19 | * OSSL_LIB_CTX object. In this way data will get cleaned up correctly when the
|
---|
20 | * module gets unloaded.
|
---|
21 | */
|
---|
22 | # if !defined(FIPS_MODULE) || defined(ALLOW_RUN_ONCE_IN_FIPS)
|
---|
23 | # ifndef VBOX
|
---|
24 | /*
|
---|
25 | * DEFINE_RUN_ONCE: Define an initialiser function that should be run exactly
|
---|
26 | * once. It takes no arguments and returns an int result (1 for success or
|
---|
27 | * 0 for failure). Typical usage might be:
|
---|
28 | *
|
---|
29 | * DEFINE_RUN_ONCE(myinitfunc)
|
---|
30 | * {
|
---|
31 | * do_some_initialisation();
|
---|
32 | * if (init_is_successful())
|
---|
33 | * return 1;
|
---|
34 | *
|
---|
35 | * return 0;
|
---|
36 | * }
|
---|
37 | */
|
---|
38 | # define DEFINE_RUN_ONCE(init) \
|
---|
39 | static int init(void); \
|
---|
40 | int init##_ossl_ret_ = 0; \
|
---|
41 | void init##_ossl_(void) \
|
---|
42 | { \
|
---|
43 | init##_ossl_ret_ = init(); \
|
---|
44 | } \
|
---|
45 | static int init(void)
|
---|
46 |
|
---|
47 | /*
|
---|
48 | * DECLARE_RUN_ONCE: Declare an initialiser function that should be run exactly
|
---|
49 | * once that has been defined in another file via DEFINE_RUN_ONCE().
|
---|
50 | */
|
---|
51 | # define DECLARE_RUN_ONCE(init) \
|
---|
52 | extern int init##_ossl_ret_; \
|
---|
53 | void init##_ossl_(void);
|
---|
54 |
|
---|
55 | /*
|
---|
56 | * DEFINE_RUN_ONCE_STATIC: Define an initialiser function that should be run
|
---|
57 | * exactly once. This function will be declared as static within the file. It
|
---|
58 | * takes no arguments and returns an int result (1 for success or 0 for
|
---|
59 | * failure). Typical usage might be:
|
---|
60 | *
|
---|
61 | * DEFINE_RUN_ONCE_STATIC(myinitfunc)
|
---|
62 | * {
|
---|
63 | * do_some_initialisation();
|
---|
64 | * if (init_is_successful())
|
---|
65 | * return 1;
|
---|
66 | *
|
---|
67 | * return 0;
|
---|
68 | * }
|
---|
69 | */
|
---|
70 | # define DEFINE_RUN_ONCE_STATIC(init) \
|
---|
71 | static int init(void); \
|
---|
72 | static int init##_ossl_ret_ = 0; \
|
---|
73 | static void init##_ossl_(void) \
|
---|
74 | { \
|
---|
75 | init##_ossl_ret_ = init(); \
|
---|
76 | } \
|
---|
77 | static int init(void)
|
---|
78 |
|
---|
79 | /*
|
---|
80 | * DEFINE_RUN_ONCE_STATIC_ALT: Define an alternative initialiser function. This
|
---|
81 | * function will be declared as static within the file. It takes no arguments
|
---|
82 | * and returns an int result (1 for success or 0 for failure). An alternative
|
---|
83 | * initialiser function is expected to be associated with a primary initialiser
|
---|
84 | * function defined via DEFINE_ONCE_STATIC where both functions use the same
|
---|
85 | * CRYPTO_ONCE object to synchronise. Where an alternative initialiser function
|
---|
86 | * is used only one of the primary or the alternative initialiser function will
|
---|
87 | * ever be called - and that function will be called exactly once. Definition
|
---|
88 | * of an alternative initialiser function MUST occur AFTER the definition of the
|
---|
89 | * primary initialiser function.
|
---|
90 | *
|
---|
91 | * Typical usage might be:
|
---|
92 | *
|
---|
93 | * DEFINE_RUN_ONCE_STATIC(myinitfunc)
|
---|
94 | * {
|
---|
95 | * do_some_initialisation();
|
---|
96 | * if (init_is_successful())
|
---|
97 | * return 1;
|
---|
98 | *
|
---|
99 | * return 0;
|
---|
100 | * }
|
---|
101 | *
|
---|
102 | * DEFINE_RUN_ONCE_STATIC_ALT(myaltinitfunc, myinitfunc)
|
---|
103 | * {
|
---|
104 | * do_some_alternative_initialisation();
|
---|
105 | * if (init_is_successful())
|
---|
106 | * return 1;
|
---|
107 | *
|
---|
108 | * return 0;
|
---|
109 | * }
|
---|
110 | */
|
---|
111 | # define DEFINE_RUN_ONCE_STATIC_ALT(initalt, init) \
|
---|
112 | static int initalt(void); \
|
---|
113 | static void initalt##_ossl_(void) \
|
---|
114 | { \
|
---|
115 | init##_ossl_ret_ = initalt(); \
|
---|
116 | } \
|
---|
117 | static int initalt(void)
|
---|
118 |
|
---|
119 | #else /* VBOX */
|
---|
120 |
|
---|
121 | /*
|
---|
122 | * PFNRTONCE returns an IPRT status code but the OpenSSL once functions
|
---|
123 | * return 1 for success and 0 for failure. We need to translate between
|
---|
124 | * these errors back (here) and force (in RUN_ONCE()).
|
---|
125 | */
|
---|
126 | # define DEFINE_RUN_ONCE(init) \
|
---|
127 | static int init(void); \
|
---|
128 | int init##_ossl_ret_ = 0; \
|
---|
129 | DECLCALLBACK(int) init##_ossl_(void *pvUser) \
|
---|
130 | { \
|
---|
131 | init##_ossl_ret_ = init(); \
|
---|
132 | return init##_ossl_ret_ ? VINF_SUCCESS : VERR_INTERNAL_ERROR; \
|
---|
133 | } \
|
---|
134 | static int init(void)
|
---|
135 |
|
---|
136 | # define DECLARE_RUN_ONCE(init) \
|
---|
137 | extern int init##_ossl_ret_; \
|
---|
138 | extern DECLCALLBACK(int) init##_ossl_(void *pvUser);
|
---|
139 |
|
---|
140 | # undef DEFINE_RUN_ONCE_STATIC_ALT /* currently unused */
|
---|
141 |
|
---|
142 | # define DEFINE_RUN_ONCE_STATIC(init) \
|
---|
143 | static int init(void); \
|
---|
144 | static int init##_ossl_ret_ = 0; \
|
---|
145 | static DECLCALLBACK(int) init##_ossl_(void *pvUser) \
|
---|
146 | { \
|
---|
147 | init##_ossl_ret_ = init(); \
|
---|
148 | return init##_ossl_ret_ ? VINF_SUCCESS : VERR_INTERNAL_ERROR; \
|
---|
149 | } \
|
---|
150 | static int init(void)
|
---|
151 |
|
---|
152 | # define DEFINE_RUN_ONCE_STATIC_ALT(initalt, init) \
|
---|
153 | static int initalt(void); \
|
---|
154 | static DECLCALLBACK(int) initalt##_ossl_(void *pvUser) \
|
---|
155 | { \
|
---|
156 | init##_ossl_ret_ = initalt(); \
|
---|
157 | return init##_ossl_ret_ ? VINF_SUCCESS : VERR_INTERNAL_ERROR; \
|
---|
158 | } \
|
---|
159 | static int initalt(void)
|
---|
160 |
|
---|
161 | #endif /* VBOX */
|
---|
162 |
|
---|
163 | /*
|
---|
164 | * RUN_ONCE - use CRYPTO_THREAD_run_once, and check if the init succeeded
|
---|
165 | * @once: pointer to static object of type CRYPTO_ONCE
|
---|
166 | * @init: function name that was previously given to DEFINE_RUN_ONCE,
|
---|
167 | * DEFINE_RUN_ONCE_STATIC or DECLARE_RUN_ONCE. This function
|
---|
168 | * must return 1 for success or 0 for failure.
|
---|
169 | *
|
---|
170 | * The return value is 1 on success (*) or 0 in case of error.
|
---|
171 | *
|
---|
172 | * (*) by convention, since the init function must return 1 on success.
|
---|
173 | */
|
---|
174 | # ifndef VBOX
|
---|
175 | # define RUN_ONCE(once, init) \
|
---|
176 | (CRYPTO_THREAD_run_once(once, init##_ossl_) ? init##_ossl_ret_ : 0)
|
---|
177 |
|
---|
178 | /*
|
---|
179 | * RUN_ONCE_ALT - use CRYPTO_THREAD_run_once, to run an alternative initialiser
|
---|
180 | * function and check if that initialisation succeeded
|
---|
181 | * @once: pointer to static object of type CRYPTO_ONCE
|
---|
182 | * @initalt: alternative initialiser function name that was previously given to
|
---|
183 | * DEFINE_RUN_ONCE_STATIC_ALT. This function must return 1 for
|
---|
184 | * success or 0 for failure.
|
---|
185 | * @init: primary initialiser function name that was previously given to
|
---|
186 | * DEFINE_RUN_ONCE_STATIC. This function must return 1 for success or
|
---|
187 | * 0 for failure.
|
---|
188 | *
|
---|
189 | * The return value is 1 on success (*) or 0 in case of error.
|
---|
190 | *
|
---|
191 | * (*) by convention, since the init function must return 1 on success.
|
---|
192 | */
|
---|
193 | # define RUN_ONCE_ALT(once, initalt, init) \
|
---|
194 | (CRYPTO_THREAD_run_once(once, initalt##_ossl_) ? init##_ossl_ret_ : 0)
|
---|
195 | # else
|
---|
196 | # define RUN_ONCE(once, init) \
|
---|
197 | (RT_SUCCESS_NP(RTOnce(once, init##_ossl_, NULL)) ? init##_ossl_ret_ : 0)
|
---|
198 |
|
---|
199 | # define RUN_ONCE_ALT(once, initalt, init) \
|
---|
200 | (RT_SUCCESS_NP(RTOnce(once, initalt##_ossl_, NULL)) ? init##_ossl_ret_ : 0)
|
---|
201 | # endif /* VBOX */
|
---|
202 |
|
---|
203 | # endif /* FIPS_MODULE */
|
---|
204 | #endif /* OSSL_INTERNAL_THREAD_ONCE_H */
|
---|
205 |
|
---|