Skip to content

Commit d31d625

Browse files
authored
Merge pull request #12157 from julek-wolfssl/fenrir-concurrency-fixes
Fix threading edge cases on Apple and Zephyr, document async poll locking
2 parents d5fa1f1 + 8a96986 commit d31d625

6 files changed

Lines changed: 20 additions & 7 deletions

File tree

‎doc/dox_comments/header_files-ja/cryptocb.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
88
TPMベースの暗号コールバックの例については、wolfTPM src/tpm2_wrap.cのwolfTPM2_CryptoDevCb関数を参照してください。
99
10+
WOLFSSL_ASYNC_CRYPTとWOLF_CRYPTO_CB_ASYNC_POLLが定義されている場合、保留中の操作を完了させるために、wolfSSL_AsyncPollおよびwolfSSL_CTX_AsyncPollからWC_ALGO_TYPE_ASYNC_POLLでコールバックが呼び出されます。この呼び出しの間、イベントキューのロックは保持されています。ハンドラは、同じWOLFSSL_CTXに対してポーリングやTLS操作を行ってはならず、同じWOLF_EVENT_QUEUEを他のいかなる方法でも使用してはなりません。デッドロックが発生します。
11+
1012
\return CRYPTOCB_UNAVAILABLE ソフトウェア暗号の使用にフォールバックする場合
1113
\return 0 成功時
1214
\return 負の値 失敗時

‎doc/dox_comments/header_files/cryptocb.h‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,13 @@
2525
devIds the handler already registered stay registered and are the
2626
caller's to clean up.
2727
28+
With WOLFSSL_ASYNC_CRYPT and WOLF_CRYPTO_CB_ASYNC_POLL defined, the
29+
callback is invoked with WC_ALGO_TYPE_ASYNC_POLL from
30+
wolfSSL_AsyncPoll and wolfSSL_CTX_AsyncPoll to complete a pending
31+
operation. The event queue lock is held for this call. The handler
32+
must not poll or run TLS operations on the same WOLFSSL_CTX, or use
33+
the same WOLF_EVENT_QUEUE in any other way: that deadlocks.
34+
2835
\return CRYPTOCB_UNAVAILABLE to fallback to using software crypto
2936
\return 0 for success
3037
\return ALREADY_E if devId is already registered. A devId must be

‎doc/dox_comments/header_files/types.h‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,12 @@ int wc_strncasecmp(const char *s1, const char *s2, size_t n);
359359
\ingroup Threading
360360
\brief Creates a new thread.
361361
362+
On Zephyr the thread object and its stack are allocated with the
363+
global heap hint wolfsslThreadHeapHint and freed with it in
364+
wolfSSL_JoinThread. Set the hint before the first call to
365+
wolfSSL_NewThread. It must not be changed once it has been used for
366+
an allocation: memory would be freed to the wrong heap.
367+
362368
\return 0 on success
363369
\return negative on error
364370

‎wolfcrypt/src/wc_port.c‎

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6160,14 +6160,9 @@ char* wolfSSL_strnstr(const char* s1, const char* s2, size_t n)
61606160
if (cond == NULL)
61616161
return BAD_FUNC_ARG;
61626162

6163-
if (wc_UnLockMutex(&cond->mutex) != 0)
6164-
return BAD_MUTEX_E;
6165-
6163+
/* Never blocks, so signal with the mutex held. */
61666164
dispatch_semaphore_signal(cond->cond);
61676165

6168-
if (wc_LockMutex(&cond->mutex) != 0)
6169-
return BAD_MUTEX_E;
6170-
61716166
return 0;
61726167
}
61736168

‎wolfssl/wolfcrypt/cryptocb.h‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1237,7 +1237,8 @@ WOLFSSL_API void wc_CryptoCb_SetDeviceFindCb(CryptoDevCallbackFind cb);
12371237
#if defined(WOLFSSL_ASYNC_CRYPT) && defined(WOLF_CRYPTO_CB_ASYNC_POLL)
12381238
/* Poll completion for bulk cipher ops: on WC_PENDING_E the callback saves
12391239
* the output pointers; wolfSSL_AsyncPoll() re-enters it with
1240-
* WC_ALGO_TYPE_ASYNC_POLL to finish the job and fill the buffer. */
1240+
* WC_ALGO_TYPE_ASYNC_POLL to finish the job and fill the buffer. The event
1241+
* queue lock is held for that call, so the callback must not use the queue. */
12411242
WOLFSSL_LOCAL int wc_CryptoCb_Poll(int devId);
12421243
#endif
12431244

‎wolfssl/wolfcrypt/types.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2200,6 +2200,8 @@ WOLFSSL_API word32 CheckRunTimeSettings(void);
22002200
k_thread_stack_t* threadStack;
22012201
} THREAD_TYPE;
22022202
#define WOLFSSL_THREAD
2203+
/* Heap hint for thread allocations. Set it before the first
2204+
* wolfSSL_NewThread call and do not change it afterwards. */
22032205
extern void* wolfsslThreadHeapHint;
22042206
/* Native Zephyr condition variable (k_condvar) built on a k_mutex; no
22052207
* POSIX pthread layer required. Only reached when !SINGLE_THREADED, so

0 commit comments

Comments
 (0)