Skip to content

Add static memory and dynamic memory - #3

Merged
aidangarske merged 3 commits into
masterfrom
static-memory
Feb 13, 2026
Merged

aidangarske merged 3 commits into
masterfrom
static-memory

Conversation

@aidangarske

Copy link
Copy Markdown
Member

Description

Add static memory (zero-malloc) support with compile-time toggle

  • Add --enable-dynamic-mem configure option; default is static (no malloc)
  • Guard wolfSPDM_New() behind WOLFSPDM_DYNAMIC_MEMORY define
  • Refactor Init/New/Free: Init does XMEMSET clean slate, New calls Init
    and sets isDynamic flag, Free uses wc_ForceZero and conditional XFREE
  • Add wolfSPDM_InitStatic() and WOLFSPDM_CTX_STATIC_SIZE for consumers
    to allocate context inline without heap allocation
  • Generate wolfspdm/options.h from config.h so consumers (e.g. wolfTPM)
    pick up WOLFSPDM_DYNAMIC_MEMORY and WOLFSPDM_NUVOTON defines
  • Add options.h to .gitignore (auto-generated build artifact)
  • Update unit_test.c and test_spdm.c for both static and dynamic paths

Tested across all 4 configurations:
dynamic-mem + spdm-emu: PASS --emu
static + spdm-emu: PASS --emu
dynamic-mem + Nuvoton: PASS (5/5)
static + Nuvoton: PASS (5/5)

  • Update CI tests for new --enable-dynamic-mem

   - Add --enable-dynamic-mem configure option; default is static (no malloc)
   - Guard wolfSPDM_New() behind WOLFSPDM_DYNAMIC_MEMORY define
   - Refactor Init/New/Free: Init does XMEMSET clean slate, New calls Init
     and sets isDynamic flag, Free uses wc_ForceZero and conditional XFREE
   - Add wolfSPDM_InitStatic() and WOLFSPDM_CTX_STATIC_SIZE for consumers
     to allocate context inline without heap allocation
   - Generate wolfspdm/options.h from config.h so consumers (e.g. wolfTPM)
     pick up WOLFSPDM_DYNAMIC_MEMORY and WOLFSPDM_NUVOTON defines
   - Add options.h to .gitignore (auto-generated build artifact)
   - Update unit_test.c and test_spdm.c for both static and dynamic paths

   Tested across all 4 configurations:
     dynamic-mem + spdm-emu:  PASS
     static + spdm-emu:       PASS
     dynamic-mem + Nuvoton:   PASS (5/5)
     static + Nuvoton:        PASS (5/5)
  - Add dynamic-mem matrix to build-test.yml for comprehensive testing.
  - Update memory-check.yml to include dynamic-mem matrix for valgrind tests.
  - Ensure all relevant CI configurations are tested with both static and dynamic memory allocation.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

This PR adds compile-time toggleable static memory (zero-malloc) support to wolfSPDM, making dynamic memory allocation opt-in rather than default. The primary goal is to enable zero-malloc operation for embedded systems while maintaining backward compatibility for users who prefer dynamic allocation.

Changes:

  • Added --enable-dynamic-mem configure option (default: static/no malloc) to control memory allocation strategy
  • Refactored context management: wolfSPDM_Init() now does clean-slate initialization, wolfSPDM_New() is guarded behind WOLFSPDM_DYNAMIC_MEMORY define, and wolfSPDM_Free() handles both static and dynamic contexts
  • Introduced auto-generated wolfspdm/options.h to propagate build configuration to downstream consumers (e.g., wolfTPM)
  • Optimized stack usage by reducing oversized local buffers and eliminating redundant copies in encryption path
  • Updated CI workflows to test all combinations of build configurations (2 OS × 2 debug × 2 nuvoton × 2 dynamic-mem = 16 test configurations)

Reviewed changes

Copilot reviewed 13 out of 14 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
wolfspdm/spdm.h Added options.h include, updated API documentation for static/dynamic usage patterns, added WOLFSPDM_CTX_STATIC_SIZE constant, conditionally compiled wolfSPDM_New()
src/spdm_context.c Refactored Init/New/Free functions: Init does XMEMSET clean slate, New calls Init and sets isDynamic flag, Free uses wc_ForceZero and conditional XFREE based on isDynamic
src/spdm_internal.h Added isDynamic flag to WOLFSPDM_CTX, included memory.h for wc_ForceZero
src/spdm_session.c Reduced local buffer sizes from oversized allocations to more precise sizes (e.g., VERSION rxBuf: 64→32, CAPABILITIES: 32→24)
src/spdm_secured.c Removed redundant ciphertext buffer, encrypt directly into output buffer to save ~4KB stack space, reduced buffer overhead from +64 to +48 bytes
src/spdm_crypto.c Reduced derSig buffer from 128 to 104 bytes (P-384 DER signature max size)
test/unit_test.c Updated tests to use stack-allocated contexts for static path, conditionally compiled test_context_new_free() behind WOLFSPDM_DYNAMIC_MEMORY
test/test_spdm.c Changed to use static buffer (g_ctxBuf) with wolfSPDM_InitStatic() instead of wolfSPDM_New()
configure.ac Added --enable-dynamic-mem option with default=no
config.h.in Added WOLFSPDM_DYNAMIC_MEMORY configuration define
Makefile.am Added build rule to generate wolfspdm/options.h from config.h, added options.h to installed headers and CLEANFILES
.gitignore Added wolfspdm/options.h to ignore list
.github/workflows/build-test.yml Added dynamic-mem to test matrix (16 total configurations)
.github/workflows/memory-check.yml Added dynamic-mem to Valgrind test matrix

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

Comment thread test/test_spdm.c
Comment thread wolfspdm/spdm.h Outdated
Comment thread src/spdm_context.c
Comment thread wolfspdm/spdm.h Outdated
@aidangarske
aidangarske merged commit 8651a7b into master Feb 13, 2026
25 checks passed
@aidangarske
aidangarske deleted the static-memory branch February 17, 2026 19:17
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