]>
Commit | Line | Data |
---|---|---|
aa6bb135 | 1 | /* |
8020d79b | 2 | * Copyright 2016-2021 The OpenSSL Project Authors. All Rights Reserved. |
71a04cfc | 3 | * |
0e9725bc | 4 | * Licensed under the Apache License 2.0 (the "License"). You may not use |
aa6bb135 RS |
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 | |
71a04cfc AG |
8 | */ |
9 | ||
f1f5ee17 AP |
10 | #if defined(_WIN32) |
11 | # include <windows.h> | |
f70863d9 | 12 | # if defined(_WIN32_WINNT) && _WIN32_WINNT >= 0x600 |
f70863d9 VD |
13 | # define USE_RWLOCK |
14 | # endif | |
f1f5ee17 AP |
15 | #endif |
16 | ||
2d46a44f DN |
17 | /* |
18 | * VC++ 2008 or earlier x86 compilers do not have an inline implementation | |
19 | * of InterlockedOr64 for 32bit and will fail to run on Windows XP 32bit. | |
20 | * https://docs.microsoft.com/en-us/cpp/intrinsics/interlockedor-intrinsic-functions#requirements | |
21 | * To work around this problem, we implement a manual locking mechanism for | |
22 | * only VC++ 2008 or earlier x86 compilers. | |
23 | */ | |
24 | ||
25 | #if (defined(_MSC_VER) && defined(_M_IX86) && _MSC_VER <= 1500) | |
26 | # define NO_INTERLOCKEDOR64 | |
27 | #endif | |
28 | ||
71a04cfc | 29 | #include <openssl/crypto.h> |
71a04cfc AG |
30 | |
31 | #if defined(OPENSSL_THREADS) && !defined(CRYPTO_TDEBUG) && defined(OPENSSL_SYS_WINDOWS) | |
32 | ||
f70863d9 VD |
33 | # ifdef USE_RWLOCK |
34 | typedef struct { | |
35 | SRWLOCK lock; | |
36 | int exclusive; | |
37 | } CRYPTO_win_rwlock; | |
38 | # endif | |
39 | ||
71a04cfc AG |
40 | CRYPTO_RWLOCK *CRYPTO_THREAD_lock_new(void) |
41 | { | |
7de2b9c4 | 42 | CRYPTO_RWLOCK *lock; |
f70863d9 VD |
43 | # ifdef USE_RWLOCK |
44 | CRYPTO_win_rwlock *rwlock; | |
45 | ||
46 | if ((lock = OPENSSL_zalloc(sizeof(CRYPTO_win_rwlock))) == NULL) | |
47 | return NULL; | |
48 | rwlock = lock; | |
49 | InitializeSRWLock(&rwlock->lock); | |
50 | # else | |
7de2b9c4 RS |
51 | |
52 | if ((lock = OPENSSL_zalloc(sizeof(CRITICAL_SECTION))) == NULL) { | |
53 | /* Don't set error, to avoid recursion blowup. */ | |
71a04cfc | 54 | return NULL; |
7de2b9c4 | 55 | } |
71a04cfc | 56 | |
f70863d9 | 57 | # if !defined(_WIN32_WCE) |
71a04cfc | 58 | /* 0x400 is the spin count value suggested in the documentation */ |
0b2fc928 F |
59 | if (!InitializeCriticalSectionAndSpinCount(lock, 0x400)) { |
60 | OPENSSL_free(lock); | |
71a04cfc | 61 | return NULL; |
0b2fc928 | 62 | } |
f70863d9 | 63 | # else |
09305a7d | 64 | InitializeCriticalSection(lock); |
f70863d9 | 65 | # endif |
7f0a8dc7 | 66 | # endif |
71a04cfc AG |
67 | |
68 | return lock; | |
69 | } | |
70 | ||
cd3f8c1b | 71 | __owur int CRYPTO_THREAD_read_lock(CRYPTO_RWLOCK *lock) |
71a04cfc | 72 | { |
f70863d9 VD |
73 | # ifdef USE_RWLOCK |
74 | CRYPTO_win_rwlock *rwlock = lock; | |
75 | ||
76 | AcquireSRWLockShared(&rwlock->lock); | |
77 | # else | |
71a04cfc | 78 | EnterCriticalSection(lock); |
f70863d9 | 79 | # endif |
71a04cfc AG |
80 | return 1; |
81 | } | |
82 | ||
cd3f8c1b | 83 | __owur int CRYPTO_THREAD_write_lock(CRYPTO_RWLOCK *lock) |
71a04cfc | 84 | { |
f70863d9 VD |
85 | # ifdef USE_RWLOCK |
86 | CRYPTO_win_rwlock *rwlock = lock; | |
87 | ||
88 | AcquireSRWLockExclusive(&rwlock->lock); | |
89 | rwlock->exclusive = 1; | |
90 | # else | |
71a04cfc | 91 | EnterCriticalSection(lock); |
f70863d9 | 92 | # endif |
71a04cfc AG |
93 | return 1; |
94 | } | |
95 | ||
96 | int CRYPTO_THREAD_unlock(CRYPTO_RWLOCK *lock) | |
97 | { | |
f70863d9 VD |
98 | # ifdef USE_RWLOCK |
99 | CRYPTO_win_rwlock *rwlock = lock; | |
100 | ||
101 | if (rwlock->exclusive) { | |
102 | rwlock->exclusive = 0; | |
103 | ReleaseSRWLockExclusive(&rwlock->lock); | |
104 | } else { | |
105 | ReleaseSRWLockShared(&rwlock->lock); | |
106 | } | |
107 | # else | |
71a04cfc | 108 | LeaveCriticalSection(lock); |
f70863d9 | 109 | # endif |
71a04cfc AG |
110 | return 1; |
111 | } | |
112 | ||
113 | void CRYPTO_THREAD_lock_free(CRYPTO_RWLOCK *lock) | |
114 | { | |
115 | if (lock == NULL) | |
116 | return; | |
117 | ||
f70863d9 | 118 | # ifndef USE_RWLOCK |
71a04cfc | 119 | DeleteCriticalSection(lock); |
f70863d9 | 120 | # endif |
71a04cfc AG |
121 | OPENSSL_free(lock); |
122 | ||
123 | return; | |
124 | } | |
125 | ||
d5e742de MC |
126 | # define ONCE_UNINITED 0 |
127 | # define ONCE_ININIT 1 | |
128 | # define ONCE_DONE 2 | |
71a04cfc | 129 | |
fcb318c6 MC |
130 | /* |
131 | * We don't use InitOnceExecuteOnce because that isn't available in WinXP which | |
132 | * we still have to support. | |
133 | */ | |
71a04cfc AG |
134 | int CRYPTO_THREAD_run_once(CRYPTO_ONCE *once, void (*init)(void)) |
135 | { | |
136 | LONG volatile *lock = (LONG *)once; | |
137 | LONG result; | |
138 | ||
139 | if (*lock == ONCE_DONE) | |
140 | return 1; | |
141 | ||
142 | do { | |
143 | result = InterlockedCompareExchange(lock, ONCE_ININIT, ONCE_UNINITED); | |
144 | if (result == ONCE_UNINITED) { | |
349d1cfd | 145 | init(); |
1fda5bc4 | 146 | *lock = ONCE_DONE; |
71a04cfc AG |
147 | return 1; |
148 | } | |
149 | } while (result == ONCE_ININIT); | |
150 | ||
151 | return (*lock == ONCE_DONE); | |
152 | } | |
153 | ||
71a04cfc AG |
154 | int CRYPTO_THREAD_init_local(CRYPTO_THREAD_LOCAL *key, void (*cleanup)(void *)) |
155 | { | |
156 | *key = TlsAlloc(); | |
157 | if (*key == TLS_OUT_OF_INDEXES) | |
158 | return 0; | |
159 | ||
160 | return 1; | |
161 | } | |
162 | ||
163 | void *CRYPTO_THREAD_get_local(CRYPTO_THREAD_LOCAL *key) | |
164 | { | |
2de108df DB |
165 | DWORD last_error; |
166 | void *ret; | |
167 | ||
168 | /* | |
169 | * TlsGetValue clears the last error even on success, so that callers may | |
170 | * distinguish it successfully returning NULL or failing. It is documented | |
171 | * to never fail if the argument is a valid index from TlsAlloc, so we do | |
172 | * not need to handle this. | |
173 | * | |
174 | * However, this error-mangling behavior interferes with the caller's use of | |
175 | * GetLastError. In particular SSL_get_error queries the error queue to | |
176 | * determine whether the caller should look at the OS's errors. To avoid | |
177 | * destroying state, save and restore the Windows error. | |
178 | * | |
179 | * https://msdn.microsoft.com/en-us/library/windows/desktop/ms686812(v=vs.85).aspx | |
180 | */ | |
181 | last_error = GetLastError(); | |
182 | ret = TlsGetValue(*key); | |
183 | SetLastError(last_error); | |
184 | return ret; | |
71a04cfc AG |
185 | } |
186 | ||
187 | int CRYPTO_THREAD_set_local(CRYPTO_THREAD_LOCAL *key, void *val) | |
188 | { | |
189 | if (TlsSetValue(*key, val) == 0) | |
190 | return 0; | |
191 | ||
192 | return 1; | |
193 | } | |
194 | ||
195 | int CRYPTO_THREAD_cleanup_local(CRYPTO_THREAD_LOCAL *key) | |
196 | { | |
197 | if (TlsFree(*key) == 0) | |
198 | return 0; | |
199 | ||
200 | return 1; | |
201 | } | |
202 | ||
203 | CRYPTO_THREAD_ID CRYPTO_THREAD_get_current_id(void) | |
204 | { | |
205 | return GetCurrentThreadId(); | |
206 | } | |
207 | ||
208 | int CRYPTO_THREAD_compare_id(CRYPTO_THREAD_ID a, CRYPTO_THREAD_ID b) | |
209 | { | |
210 | return (a == b); | |
211 | } | |
212 | ||
213 | int CRYPTO_atomic_add(int *val, int amount, int *ret, CRYPTO_RWLOCK *lock) | |
214 | { | |
7da7b27e | 215 | *ret = (int)InterlockedExchangeAdd((long volatile *)val, (long)amount) + amount; |
71a04cfc AG |
216 | return 1; |
217 | } | |
218 | ||
d5e742de MC |
219 | int CRYPTO_atomic_or(uint64_t *val, uint64_t op, uint64_t *ret, |
220 | CRYPTO_RWLOCK *lock) | |
221 | { | |
2d46a44f DN |
222 | #if (defined(NO_INTERLOCKEDOR64)) |
223 | if (lock == NULL || !CRYPTO_THREAD_write_lock(lock)) | |
224 | return 0; | |
225 | *val |= op; | |
226 | *ret = *val; | |
227 | ||
228 | if (!CRYPTO_THREAD_unlock(lock)) | |
229 | return 0; | |
230 | ||
231 | return 1; | |
232 | #else | |
d5e742de MC |
233 | *ret = (uint64_t)InterlockedOr64((LONG64 volatile *)val, (LONG64)op) | op; |
234 | return 1; | |
2d46a44f | 235 | #endif |
d5e742de MC |
236 | } |
237 | ||
238 | int CRYPTO_atomic_load(uint64_t *val, uint64_t *ret, CRYPTO_RWLOCK *lock) | |
239 | { | |
2d46a44f DN |
240 | #if (defined(NO_INTERLOCKEDOR64)) |
241 | if (lock == NULL || !CRYPTO_THREAD_read_lock(lock)) | |
242 | return 0; | |
243 | *ret = *val; | |
244 | if (!CRYPTO_THREAD_unlock(lock)) | |
245 | return 0; | |
246 | ||
247 | return 1; | |
248 | #else | |
d5e742de MC |
249 | *ret = (uint64_t)InterlockedOr64((LONG64 volatile *)val, 0); |
250 | return 1; | |
2d46a44f | 251 | #endif |
d5e742de MC |
252 | } |
253 | ||
2915fe19 RS |
254 | int openssl_init_fork_handlers(void) |
255 | { | |
256 | return 0; | |
257 | } | |
258 | ||
84952925 DMSP |
259 | int openssl_get_fork_id(void) |
260 | { | |
261 | return 0; | |
262 | } | |
71a04cfc | 263 | #endif |