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