Skip to content

ToCString: word-align stackalloc buffers - #599

Open
miriampolzer wants to merge 1 commit into
mit-plv:masterfrom
miriampolzer:stackalloc-align
Open

miriampolzer wants to merge 1 commit into
mit-plv:masterfrom
miriampolzer:stackalloc-align

Conversation

@miriampolzer

@miriampolzer miriampolzer commented Nov 25, 2026 •

Copy link
Copy Markdown
Collaborator

Printed code accesses memory bytewise or through memcpy, but stackalloc'd buffers are often passed to external C that takes word pointers. BoringSSL passes them to fiat-crypto field arithmetic (uint32_t[8]/uint64_t[4]), where an underaligned uint8_t array is undefined behavior; UBSan caught this on 32-bit builds (https://issues.chromium.org/issues/586589239). It can also fault on strict-alignment targets.

The semantics lets stackalloc return any address, so choosing a word-aligned one is a refinement; the RISC-V compiler already does so. The prelude includes <stdalign.h> and defines BR_STACKALLOC_ALIGN as alignas(br_word_t) unless it is already defined, so users can define it empty to get plain byte arrays. Code printed with c_func but without the prelude (e.g. fiat-crypto's src/Bedrock/P256.v printing for BoringSSL) must define it; BoringSSL will carry the same #ifndef block.

Tested by printing ToCStringTest and ToCStringStackallocLoopTest and building them as C11/C17/C2x/C23 (gcc and clang, x86_64 and i386, aligned and with -DBR_STACKALLOC_ALIGN=, identical results) and as C++11/17/23, and by rebuilding fiat-crypto's src/Bedrock/P256.v, P256/Coord.v and P256/Coord32.v with this change: the printed C matches what BoringSSL ships, function for function.

@miriampolzer

miriampolzer commented Nov 25, 2026 •

Copy link
Copy Markdown
Collaborator Author

@andres-erbsen thoughts on this? I wasn't sure if we want the default to be aligned or not-aligned. Now the default is aligned, but we could also have a switch inside bedrock2 at a level we choose to emit aligned or not-aligned stackallocs. E.g. at notation level, having two different notations for aligned or non-aligned allocations, or at printing level, saying if we want byte arrays aligned or not...

Given the fact that this issue occurs between our own bedrock2 code and our own fiat generated code, I think we should fix it here and not just in boringssl. But let me know if you think this is something to be added manually afterwards.

Another question is if we want to emit the #define block explicitly, or just the BR_STACKALLOC_ALIGN in front of stackallocs, so a user will notice themselves that they need to define alignment. I've opted for printing it, just to show how we intend this to go, but open for suggestions.

@miriampolzer
miriampolzer force-pushed the stackalloc-align branch 2 times, most recently from fe7924d to 3e96018 Compare December 3, 2026 09:36
Printed code accesses memory bytewise or through memcpy, but
stackalloc'd buffers are often passed to external C that takes word
pointers (e.g. fiat-crypto field arithmetic in BoringSSL), where an
underaligned uint8_t array is undefined behavior (observed with UBSan
on 32-bit) and can fault on strict-alignment targets.

The semantics lets stackalloc return any address, so choosing a
word-aligned one is a refinement; the RISC-V compiler already does so.
The prelude defines BR_STACKALLOC_ALIGN unless it is already defined;
code printed with c_func but without the prelude must define it
(possibly empty).
@davidben

davidben commented Dec 3, 2026 •

Copy link
Copy Markdown

NB: Alignment is necessary, but not sufficient, to pass a Bedrock2 array to a C function. It's still a strict aliasing violation.

davidben pushed a commit to google/boringssl that referenced this pull request Dec 3, 2026
Bedrock2-generated P-256 point operations and their callers in
p256.cc.inc passed byte-aligned uint8_t buffers to Fiat field ops that
take br_word_t pointers, causing undefined behavior. Word-align these
buffers and verify alignment at the Bedrock-to-Fiat boundary by
prefixing generated stackallocs with BR_STACKALLOC_ALIGN (matching
mit-plv/bedrock2#599), typing the p256.cc.inc
scratch buffers as fiat_p256_felem[3], and routing pointer conversions
through an asserting br_word_ptr helper with a static alignment check.

Bug: 586589239
Change-Id: Ib60cc2f8dbe064b8b270ac686e0b50dc3249a852
Reviewed-on: https://boringssl-review.googlesource.com/c/boringssl/+/106848
Reviewed-by: Adam Langley <agl@google.com>
Commit-Queue: Miriam Polzer <mpolzer@google.com>
Presubmit-BoringSSL-Verified: boringssl-scoped@luci-project-accounts.iam.gserviceaccount.com <boringssl-scoped@luci-project-accounts.iam.gserviceaccount.com>
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