Skip to content

Conversation

@bigbrett
Copy link
Owner

@bigbrett bigbrett commented Jan 21, 2026

TL;DR: Makes wolfHSM server thread safe!

This pull request implements a generic framework for thread-safe access to shared server resources in wolfHSM, specifically targeting the NVM (non-volatile memory) and global key cache subsystems.

Note that an instance of a server context itself cannot be shared across threads without serialization by the caller. This PR just ensures that if multiple server contexts share an NVM or global keystore, that accesses to shared resources are properly serialized such that requests from multiple clients can be processed concurrently in their own threads.

Changes:

  • Introduces lock abstraction layer (wh_lock.{c,h}) with callback-based design for platform independence
  • Example POSIX lock implementation using pthread_mutex
  • Refactor NVM and keystore layer internals to use lock abstraction such that global keystore and NVM can be shared by multiple server contexts in a thread-safe manner
  • Thread safe functionality enabled with the WOLFHSM_CFG_THREADSAFE build option. When this option is NOT defined, all lock abstraction operations compile to no-ops, with zero overhead
  • Adds "thread safe stress test" to test suite that attempts the flesh out data races via a large number of contention cases, meant to be run under ThreadSanitizer

Gaps/future work:

  • Serializing access to global crypto state, specifically hardware crypto for ports. A bit of a tricky problem since offload is provided at the port level, and there isn't a good way for wolfHSM to know which algos will be accelerated and which wont

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This pull request implements thread-safe access to shared server resources in wolfHSM, specifically targeting the NVM (non-volatile memory) and global key cache subsystems. The implementation introduces a platform-agnostic lock abstraction layer with a POSIX pthread_mutex implementation, along with comprehensive stress testing infrastructure.

Changes:

  • Introduces lock abstraction layer (wh_lock.h/c) with callback-based design for platform independence
  • Adds POSIX lock implementation using pthread_mutex
  • Implements thread-safe wrappers for all NVM and keystore operations with proper lock acquisition/release
  • Adds "unlocked" variants of functions for atomic multi-step operations
  • Includes comprehensive stress tests with 18+ contention phases and TSAN (Thread Sanitizer) integration

Reviewed changes

Copilot reviewed 16 out of 16 changed files in this pull request and generated no comments.

Show a summary per file
File Description
wolfhsm/wh_settings.h Documents WOLFHSM_CFG_THREADSAFE configuration option
wolfhsm/wh_nvm.h Adds lock member to NVM context and declares unlocked function variants
wolfhsm/wh_lock.h Defines platform-agnostic lock abstraction API
src/wh_lock.c Implements lock abstraction with callback-based design
src/wh_nvm.c Adds locking to all NVM operations and implements unlocked variants
src/wh_server_keystore.c Adds locking to all keystore operations with atomic multi-step support
port/posix/posix_lock.h Defines POSIX-specific lock structures and callbacks
port/posix/posix_lock.c Implements pthread_mutex-based locking
test/wh_test_lock.h Header for lock unit tests
test/wh_test_lock.c Basic lock functionality tests
test/wh_test_threadsafe_stress.h Header for stress tests
test/wh_test_threadsafe_stress.c Comprehensive multi-threaded stress tests
test/wh_test_crypto.c Initializes NVM config to avoid uninitialized memory
test/wh_test.c Integrates lock and stress tests into test suite
test/tsan.supp Thread Sanitizer suppressions for known false positives
test/Makefile Adds TSAN support and stress test mode

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

…ety,

serializing access to shared global resources like NVM and global keycache
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants