Skip to content

ipc4: layout_check: add build-time checks for packed bit-field layout - #1

Open
orange-dot wants to merge 1 commit into
mainfrom
fix/ipc4-bitfield-layout-build-checks
Open

orange-dot wants to merge 1 commit into
mainfrom
fix/ipc4-bitfield-layout-build-checks

Conversation

@orange-dot

@orange-dot orange-dot commented Jul 10, 2026 •

Copy link
Copy Markdown
Owner

Build-time toolchain conformance check for the IPC4 wire format, which is defined with bit-fields inside packed structs — a layout the C standard leaves implementation-defined.

Two parts:

  • src/include/ipc4/layout_check.h (included from the always-built IPC4 handler code): #error unless the compiler is GCC-compatible and the target little-endian — together these pin LSB-first bit-field allocation via the platform ABI (Xtensa is bi-endian by design, so a misconfigured toolchain is a real failure mode). Defines the canary word mirroring the IPC4 message-word idiom.
  • src/ipc/ipc4/layout_probe.c: the canary compiled with the firmware toolchain; the build extracts its byte representation from the object file with objcopy (nothing linked or executed) and compares it against checked-in golden bytes — observing the actual bit-field allocation order, which no C constant expression can express (verified: union-pun reads are rejected in any static assert form by GCC 14/16).

Runs for firmware and host library builds alike when IPC4 is enabled; a non-conforming toolchain fails the build with an expected-vs-actual byte diagnostic.

Verified locally on all 14 working platforms: 8 IPC4 platforms (tgl, tgl-h, mtl, lnl, ptl, wcl, nvl, nvl-s) build clean with canary bytes matching golden in every build tree; 6 IPC3 platforms (imx8, imx8x, imx8m, imx8m_cm7, imx8ulp, imx95) build clean with the probe correctly not built; host testbench matches as well. A corrupted golden byte fails the build loudly.

@orange-dot
orange-dot force-pushed the fix/ipc4-bitfield-layout-build-checks branch 2 times, most recently from 1cdde79 to 78f64f1 Compare July 10, 2026 15:12
@orange-dot
orange-dot marked this pull request as ready for review July 10, 2026 15:49
The IPC4 ABI headers describe the host <-> DSP wire format with
bit-fields inside packed structs. The C standard leaves bit-field
allocation order within a storage unit implementation-defined
(C99/C11 6.7.2.1), so the wire format silently relies on the
toolchain following the little-endian, LSB-first convention of the
platform ABI. A toolchain that targets a big-endian core (Xtensa is
bi-endian by design), is not GCC-compatible, or mishandles packed
bit-fields would corrupt every IPC4 message without a build error.

Add a two-part toolchain conformance check:

 - ipc4/layout_check.h, included from the always-built IPC4 handler
   code, rejects at preprocessing time any compiler that is not
   GCC-compatible or any target that is not little-endian - together
   these pin LSB-first bit-field allocation via the platform ABI (the
   same convention Linux relies on in <asm/byteorder.h>) - and defines
   a canary word mirroring the message-word bit-field idiom.

 - src/ipc/ipc4/layout_probe.c compiles that canary with the firmware
   toolchain; the build extracts its byte representation from the
   object file with objcopy (nothing is linked or executed, so cross
   and host builds are handled identically) and compares it against
   checked-in golden bytes. This observes the actual bit-field
   allocation order, which no C constant expression can express.

The check runs for firmware and host library builds alike whenever
CONFIG_IPC_MAJOR_4 code is built; a non-conforming toolchain fails
the build with an expected-vs-actual byte diagnostic.

Verified locally on all 14 working platforms: tgl, tgl-h, mtl, lnl,
ptl, wcl, nvl and nvl-s build clean with canary bytes matching the
golden file in every build tree; imx8, imx8x, imx8m, imx8m_cm7,
imx8ulp and imx95 (IPC3) build clean with the probe correctly not
built. The host testbench build passes with matching bytes as well.
A corrupted golden byte fails the tgl build with a clear diagnostic.

Signed-off-by: Bojan Janjatovic <bojan.janjatovic@mamut-studio.com>
@orange-dot
orange-dot force-pushed the fix/ipc4-bitfield-layout-build-checks branch from 78f64f1 to 1265aec Compare July 10, 2026 15:53
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.

1 participant