Skip to content

net.mbedtls: enable MBEDTLS_THREADING_C on Windows and macOS (currently Linux/BSD only) #27436

Description

@quaesitor-scientiam

Summary

In V's vendored mbedtls, MBEDTLS_THREADING_C (mbedtls's internal locking) is enabled
only on Linux/FreeBSD/OpenBSD and #undef'd on Windows and macOS. This means
mbedtls does not mutex-protect its shared state (the ctr_drbg RNG, RSA key blinding,
and the library's global mutexes) on Windows/macOS, so any design that shares an mbedtls
object across threads is unsafe there.

Where it comes from

thirdparty/mbedtls/mbedtls.patch (V's local patch over mbedtls 3.6.x) contains:

#if ( defined(__linux__) || defined(__FreeBSD__) ) || defined (__OpenBSD__)
#define MBEDTLS_THREADING_PTHREAD
#define MBEDTLS_THREADING_C
#else
#undef MBEDTLS_THREADING_PTHREAD
#undef MBEDTLS_THREADING_C
#endif

mbedtls offers two locking backends: MBEDTLS_THREADING_PTHREAD (built-in, needs
pthread.h) and MBEDTLS_THREADING_ALT (the embedder supplies the mutex callbacks via
mbedtls_threading_set_alt() and defines mbedtls_threading_mutex_t in a
threading_alt.h). The patch enables the pthread backend only where pthreads are present
and disables threading otherwise. Windows (MSVC) has no pthread.h and no THREADING_ALT
shim was written; macOS has pthreads but is simply not listed in the gate (apparent
oversight).

Why it matters

Today it is harmless: V never shares mutable mbedtls state across threads — client
SSLConns each own their config + RNG, and the TLS server handshakes run serially on the
accept thread. But it blocks any future concurrency that shares an mbedtls config/RNG
across threads, in particular parallelizing TLS server handshakes across worker threads
(the deferred item #3 from #27433). On Windows/macOS that would be a data race on the
shared ctr_drbg and RSA key state.

Proposed fix

  • macOS: add || defined(__APPLE__) to the gate — macOS has pthreads, so
    MBEDTLS_THREADING_PTHREAD works as-is.
  • Windows: implement MBEDTLS_THREADING_ALT over Win32 CRITICAL_SECTION:
    1. threading_alt.h defining mbedtls_threading_mutex_t (a CRITICAL_SECTION + valid
      flag);
    2. four C callbacks wrapping InitializeCriticalSection/EnterCriticalSection/
      LeaveCriticalSection/DeleteCriticalSection;
    3. define MBEDTLS_THREADING_C + MBEDTLS_THREADING_ALT on _WIN32;
    4. call mbedtls_threading_set_alt(...) once at startup (V module init()), before any
      TLS use;
    5. rebuild the mbedtls objects.
  • Fold the change into mbedtls.patch so it survives the next mbedtls upgrade.

Caveats

  • Enables (does not require) cross-thread sharing; small per-lock overhead once on.
  • set_alt must run before mbedtls touches any global mutex (module-init() handles this).
  • Touches thirdparty/ and the prebuilt mbedtls objects (rebuild needed).

Plan

Implementing on a branch; macOS (pthread) + Windows (THREADING_ALT). macOS will be tested
on Apple Silicon (M-series). Prerequisite for safe parallel TLS handshakes (#27433 item #3).

Note

You can use the 👍 reaction to increase the issue's priority for developers.

Please note that only the 👍 reaction to the issue itself counts as a vote.
Other reactions and those to comments will not be taken into account.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions