Repository navigation
Add static memory and dynamic memory - #3
Merged
Merged
Conversation
- 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.
There was a problem hiding this comment.
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-memconfigure option (default: static/no malloc) to control memory allocation strategy - Refactored context management:
wolfSPDM_Init()now does clean-slate initialization,wolfSPDM_New()is guarded behindWOLFSPDM_DYNAMIC_MEMORYdefine, andwolfSPDM_Free()handles both static and dynamic contexts - Introduced auto-generated
wolfspdm/options.hto 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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Add static memory (zero-malloc) support with compile-time toggle
and sets isDynamic flag, Free uses wc_ForceZero and conditional XFREE
to allocate context inline without heap allocation
pick up WOLFSPDM_DYNAMIC_MEMORY and WOLFSPDM_NUVOTON defines
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)