Skip to content

arm/linux: zero-initialize stack buffers to fix Valgrind warnings#376

Open
vozvivan wants to merge 1 commit intopytorch:mainfrom
vozvivan:fix/cpuinfo-arm-init
Open

arm/linux: zero-initialize stack buffers to fix Valgrind warnings#376
vozvivan wants to merge 1 commit intopytorch:mainfrom
vozvivan:fix/cpuinfo-arm-init

Conversation

@vozvivan
Copy link
Copy Markdown

@vozvivan vozvivan commented Apr 5, 2026

Description

This PR fixes Valgrind and MemorySanitizer warnings caused by reading uninitialized stack memory.

1. Bugfix:
On modern AArch64 kernels, the "Hardware" line is often completely absent from /proc/cpuinfo. When this happens, the stack-allocated proc_cpuinfo_hardware buffer is never populated and may remains uninitialized garbage. Passing it to strnlen and strncmp in cpuinfo_arm_linux_decode_chipset_from_proc_cpuinfo_hardware triggers memory analyzer warnings. Adding = {0} ensures it safely defaults to an empty string "" and fixes the issue.

2. Follow-up hardening:
To proactively prevent the exact same issue on other platforms (or Android) where OS-specific properties might be missing, I also zero-initialized the adjacent proc_cpuinfo_revision and android_properties buffers.

Since cpuinfo_arm_linux_init runs exactly once via pthread_once, the performance overhead of zeroing these bytes is negligible.

Valgrind Trace

==24496== Conditional jump or move depends on uninitialised value(s)
==24496==    at 0x4838B0C: strnlen (vg_replace_strmem.c:468)
==24496==    by 0x1B3CCFF: cpuinfo_arm_linux_decode_chipset_from_proc_cpuinfo_hardware
==24496==    by 0x1B3E24B: cpuinfo_arm_linux_decode_chipset
==24496==    by 0x1B3AC47: cpuinfo_arm_linux_init

On modern arm64 kernels, /proc/cpuinfo may legitimately omit the
"Hardware" field. Other optional identification sources, such as
"Revision" or Android property-based identifiers, may also be absent
depending on the platform.

Default-initialize the corresponding stack buffers / structs so missing
optional identifiers are treated as empty values rather than
uninitialized memory.
@meta-cla meta-cla bot added the cla signed label Apr 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants