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