xref: /freebsd/crypto/openssl/crypto/threads_win.c (revision e0c4386e)
1 /*
2  * Copyright 2016-2023 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 #if defined(_WIN32)
11 # include <windows.h>
12 # if defined(_WIN32_WINNT) && _WIN32_WINNT >= 0x600
13 #  define USE_RWLOCK
14 # endif
15 #endif
16 
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 <= 1600)
26 # define NO_INTERLOCKEDOR64
27 #endif
28 
29 #include <openssl/crypto.h>
30 
31 #if defined(OPENSSL_THREADS) && !defined(CRYPTO_TDEBUG) && defined(OPENSSL_SYS_WINDOWS)
32 
33 # ifdef USE_RWLOCK
34 typedef struct {
35     SRWLOCK lock;
36     int exclusive;
37 } CRYPTO_win_rwlock;
38 # endif
39 
CRYPTO_THREAD_lock_new(void)40 CRYPTO_RWLOCK *CRYPTO_THREAD_lock_new(void)
41 {
42     CRYPTO_RWLOCK *lock;
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
51 
52     if ((lock = OPENSSL_zalloc(sizeof(CRITICAL_SECTION))) == NULL) {
53         /* Don't set error, to avoid recursion blowup. */
54         return NULL;
55     }
56 
57 #  if !defined(_WIN32_WCE)
58     /* 0x400 is the spin count value suggested in the documentation */
59     if (!InitializeCriticalSectionAndSpinCount(lock, 0x400)) {
60         OPENSSL_free(lock);
61         return NULL;
62     }
63 #  else
64     InitializeCriticalSection(lock);
65 #  endif
66 # endif
67 
68     return lock;
69 }
70 
CRYPTO_THREAD_read_lock(CRYPTO_RWLOCK * lock)71 __owur int CRYPTO_THREAD_read_lock(CRYPTO_RWLOCK *lock)
72 {
73 # ifdef USE_RWLOCK
74     CRYPTO_win_rwlock *rwlock = lock;
75 
76     AcquireSRWLockShared(&rwlock->lock);
77 # else
78     EnterCriticalSection(lock);
79 # endif
80     return 1;
81 }
82 
CRYPTO_THREAD_write_lock(CRYPTO_RWLOCK * lock)83 __owur int CRYPTO_THREAD_write_lock(CRYPTO_RWLOCK *lock)
84 {
85 # ifdef USE_RWLOCK
86     CRYPTO_win_rwlock *rwlock = lock;
87 
88     AcquireSRWLockExclusive(&rwlock->lock);
89     rwlock->exclusive = 1;
90 # else
91     EnterCriticalSection(lock);
92 # endif
93     return 1;
94 }
95 
CRYPTO_THREAD_unlock(CRYPTO_RWLOCK * lock)96 int CRYPTO_THREAD_unlock(CRYPTO_RWLOCK *lock)
97 {
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
108     LeaveCriticalSection(lock);
109 # endif
110     return 1;
111 }
112 
CRYPTO_THREAD_lock_free(CRYPTO_RWLOCK * lock)113 void CRYPTO_THREAD_lock_free(CRYPTO_RWLOCK *lock)
114 {
115     if (lock == NULL)
116         return;
117 
118 # ifndef USE_RWLOCK
119     DeleteCriticalSection(lock);
120 # endif
121     OPENSSL_free(lock);
122 
123     return;
124 }
125 
126 # define ONCE_UNINITED     0
127 # define ONCE_ININIT       1
128 # define ONCE_DONE         2
129 
130 /*
131  * We don't use InitOnceExecuteOnce because that isn't available in WinXP which
132  * we still have to support.
133  */
CRYPTO_THREAD_run_once(CRYPTO_ONCE * once,void (* init)(void))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) {
145             init();
146             *lock = ONCE_DONE;
147             return 1;
148         }
149     } while (result == ONCE_ININIT);
150 
151     return (*lock == ONCE_DONE);
152 }
153 
CRYPTO_THREAD_init_local(CRYPTO_THREAD_LOCAL * key,void (* cleanup)(void *))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 
CRYPTO_THREAD_get_local(CRYPTO_THREAD_LOCAL * key)163 void *CRYPTO_THREAD_get_local(CRYPTO_THREAD_LOCAL *key)
164 {
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;
185 }
186 
CRYPTO_THREAD_set_local(CRYPTO_THREAD_LOCAL * key,void * val)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 
CRYPTO_THREAD_cleanup_local(CRYPTO_THREAD_LOCAL * key)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 
CRYPTO_THREAD_get_current_id(void)203 CRYPTO_THREAD_ID CRYPTO_THREAD_get_current_id(void)
204 {
205     return GetCurrentThreadId();
206 }
207 
CRYPTO_THREAD_compare_id(CRYPTO_THREAD_ID a,CRYPTO_THREAD_ID b)208 int CRYPTO_THREAD_compare_id(CRYPTO_THREAD_ID a, CRYPTO_THREAD_ID b)
209 {
210     return (a == b);
211 }
212 
CRYPTO_atomic_add(int * val,int amount,int * ret,CRYPTO_RWLOCK * lock)213 int CRYPTO_atomic_add(int *val, int amount, int *ret, CRYPTO_RWLOCK *lock)
214 {
215     *ret = (int)InterlockedExchangeAdd((long volatile *)val, (long)amount) + amount;
216     return 1;
217 }
218 
CRYPTO_atomic_or(uint64_t * val,uint64_t op,uint64_t * ret,CRYPTO_RWLOCK * lock)219 int CRYPTO_atomic_or(uint64_t *val, uint64_t op, uint64_t *ret,
220                      CRYPTO_RWLOCK *lock)
221 {
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
233     *ret = (uint64_t)InterlockedOr64((LONG64 volatile *)val, (LONG64)op) | op;
234     return 1;
235 #endif
236 }
237 
CRYPTO_atomic_load(uint64_t * val,uint64_t * ret,CRYPTO_RWLOCK * lock)238 int CRYPTO_atomic_load(uint64_t *val, uint64_t *ret, CRYPTO_RWLOCK *lock)
239 {
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
249     *ret = (uint64_t)InterlockedOr64((LONG64 volatile *)val, 0);
250     return 1;
251 #endif
252 }
253 
openssl_init_fork_handlers(void)254 int openssl_init_fork_handlers(void)
255 {
256     return 0;
257 }
258 
openssl_get_fork_id(void)259 int openssl_get_fork_id(void)
260 {
261     return 0;
262 }
263 #endif
264