Skip to content

Fix KWS NODE_MEMREQ instance header underallocation#95

Closed
goyalpalak18 wants to merge 1 commit intoeembc:mainfrom
goyalpalak18:fix/kws-memreq-instance-size
Closed

Fix KWS NODE_MEMREQ instance header underallocation#95
goyalpalak18 wants to merge 1 commit intoeembc:mainfrom
goyalpalak18:fix/kws-memreq-instance-size

Conversation

@goyalpalak18
Copy link
Copy Markdown

While looking at the recent ABF memory patch (commit edc44cf), I noticed the exact same under-allocation bug exists in the KWS component.

The Bug

In src/ee_kws.c, NODE_MEMREQ allocates memory using a magic number: sizeof(mfcc_instance_t) + 8. The + 8 was probably meant to cover two 32-bit pointers, but it ignores the chunk_idx field entirely and breaks on 64-bit platforms (where pointers are 8 bytes). There's literally a /* TODO : justift this */ comment sitting right next to it.

Because of this, the kws_instance_t struct overflows its heap block. This eats into the 12-byte CMSIS vectorized-read safety padding, leading to undefined behavior. On 64-bit RISC-V evaluation targets, this overflow can silently corrupt heap metadata or cause sliding window drifts that completely invalidate benchmark scores.

The Fix

I mirrored the ABF fix by replacing the manual calculation with a standard sizeof(kws_instance_t). Since the struct already includes mfcc_instance_t as its first member, this naturally accounts for all pointers, chunk_idx, and any compiler-inserted alignment padding across all architectures and pointer widths.

// Before:
uint32_t size = (3 * 4) // See note above
                + sizeof(mfcc_instance_t)
                + 8; /* TODO : justift this */

// After:
uint32_t size = (3 * 4) // See note above
                + sizeof(kws_instance_t);

This restores the full CMSIS 12-byte guard padding, eliminates the undefined behavior risk, and brings KWS up to the same correctness baseline as ABF.

Signed-off-by: goyalpalak18 <goyalpalak1806@gmail.com>
@goyalpalak18
Copy link
Copy Markdown
Author

Hey @llefaucheur @joseph-yiu, could you take a look at this when you have a chance? Thanks!

@joseph-yiu
Copy link
Copy Markdown
Contributor

Hi @llefaucheur , could you confirm if you have reviewed this PR? Thanks :-)
regards,
Joseph

@llefaucheur
Copy link
Copy Markdown
Contributor

llefaucheur commented Feb 24, 2026

This "+ 8" is VERY bad indeed. Thank you Goyal for finding the bug !
Yes Joseph you can close the ticket.
Laurent

PS: audiomark served as a proof-of-concept for another project ("NanoGraph") and instead of sharing hard constants like this, the "nodes" of the graph have memory banks allocated in "manifest files". Dynamic memory allocation is still possible but not recommended for embedded devices (especially the ones with very small memory)
See the documentation here : https://github.com/llefaucheur/NanoGraph/blob/main/doc/NanoGraph_Node_Manifest.md#node-memory-allocation
and an example here https://github.com/llefaucheur/NanoGraph_Store/blob/main/arm/filter/node_manifest_filter.txt

@joseph-yiu
Copy link
Copy Markdown
Contributor

Thanks @llefaucheur for confirming. I have merged this into dev branch (dev_2026q1) and will close this PR when we merge the dev branch into main.
Thanks @goyalpalak18 for reporting the issue.
regards,
Joseph

@joseph-yiu
Copy link
Copy Markdown
Contributor

Closing this pull request as the AudioMark v1.0.4 release is completed.

@joseph-yiu joseph-yiu closed this Apr 7, 2026
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.

3 participants