Skip to content

Pack the PROGRAM and TIMESYNC payloads, and build the cross-ABI check that was missing - #7

Merged
aphelps-claude-bot merged 2 commits into
mainfrom
pack-programs-timesync-wire-structs
Aug 14, 2026
Merged

aphelps-claude-bot merged 2 commits into
mainfrom
pack-programs-timesync-wire-structs

Conversation

@aphelps-claude-bot

Copy link
Copy Markdown
Collaborator

Finishes what HMTL#6 started. That PR packed the structs in HMTLWireFormat.h and deliberately left the PROGRAM payloads in HMTLPrograms.h and msg_time_sync_t in TimeSync.h out to stay reviewable. They have the same defect: an ATMega328 and an ESP32 disagree about their layout, so a blob written by one misparses when read by the other.

Two severities, worth keeping distinct

A field moves — silent misparse. Measured with the real toolchains, not inferred:

struct AVR ESP32 what moves
hmtl_program_blink_t 10 12 off_period at 5 vs 6
msg_time_sync_t 5 8 timestamp at 1 vs 4
hmtl_program_color_t (-DBIG_PIXELS) 7 8 range at 3 vs 4

An ESP32 reading an AVR blink blob takes off_period from AVR bytes 6-7 — the high byte of off_period plus off_value[0]. For TIMESYNC the direction matters: an ESP32 reads an AVR peer's timestamp three bytes late and off the end of the payload; AVR→ESP32 is the mirror, three bytes early.

Size only — still breaks interoperation, because sizeof feeds the HMTL_MSG_*_LEN family a peer length-checks against: timed_change 10/12, fade 11/12, sparkle 13/14, circular 9/10. sequence (32) and brightness (1) never diverged; packed and asserted anyway so the invariant is uniform.

msg_time_sync_t needed two changes, not one

Packing fixes AVR-vs-32-bit. unsigned long → uint32_t fixes the fact that the struct's size otherwise follows the ABI's long — 4 bytes on both real targets but 8 on any LP64 host. All consumers are in TimeSync.cpp, all by value, none take its address, no printf formats involved.

It also moves into HMTLWireFormat.h, filling the placeholder comment HMTL#6 left for it: it is wire format, every module handles MSG_TYPE_TIMESYNC, and HMTL_Command_CLI can originate one to any address on the bus.

Packing is layout-neutral on AVR — verified, not asserted

Under avr-g++ -mmcu=atmega328p, every size and offset above is byte-for-byte identical packed and unpacked. The deployed fleet sees no change; the 32-bit side moves to match the AVR layout that is already on the wire. python/hmtl/HMTLprotocol.py independently encodes the packed forms (MSG_PROGRAM_BLINK_FMT = '<HBBBHBBB', timed_change 10 B), so a second implementation already treats the AVR layout as the truth.

The harder half: nothing was going to run these asserts

The plan for this work assumed the AVR side came free from make test-simavr. It does not. HMTL_Module's platformio.ini resolves its libraries from a machine-local Arduino directory, not from this repo — verified rather than assumed: a deliberately impossible static_assert in Libraries/HMTLMessaging/HMTLPrograms.h leaves make test-simavr green. Nothing in any Makefile invoked xtensa-esp32-elf-g++ at all, and HMTL_Test substitutes a stub TimeSync.h that never declared msg_time_sync_t.

So this PR adds tests/layout/:

  • layout_check.cpp includes the real headers and has no test body — every assertion is a static_assert beside the struct it constrains, so compiling is the test.
  • The Makefile sweeps host, host -fpack-struct=1, avr-g++ and xtensa-esp32-elf-g++, ends with a COVERAGE: line naming anything it skipped, and takes REQUIRE_TOOLCHAINS=1 to turn a skip into an error. A partial sweep must not read as a full one.
  • negative_control.py perturbs all 46 asserted numbers under all four ABIs and requires every one of the 184 compiles to fail. It masks digits inside comments and string literals first — the naive version reported five phantom "cannot fail" results by bumping a number in a trailing comment.

Both are in make test, and the negative control runs by default (~25 s): it is the only thing between "the guard passed" and "the guard cannot fail", which this subsystem has produced before.

The -fpack-struct=1 proxy is trustworthy here only because of the uint32_t change — with unsigned long the host reported 9 bytes, matching neither real target. Do not extend it to a struct with a long or pointer member without re-checking.

Two stub fixes fall out

  • stubs/PixelUtil.h hard-coded PIXEL_ADDR_TYPE as uint16_t, which is the -DBIG_PIXELS width; the default is uint8_t. pixel_range_t goes on the wire inside hmtl_program_color_t, so the stub silently doubled a wire field's width. It now mirrors the real header. (This stub is what previously misled a reader into recording the wrong widths as confirmed.)
  • stubs/PixelUtil.h and stubs/Debug.h now use headers avr-libc actually ships, so avr-g++ can compile the stub set.

Open question, decided conservatively

hmtl_program_color_t's wire width follows a build flag, not the target: pixel_range_t is two PIXEL_ADDR_TYPE, uint8_t by default and uint16_t under -DBIG_PIXELS, and real envs differ (lightbringer_*, rfm_328_10_2, esp32_routed set it; nano, uno, esp32_breadboard do not). Two AVR modules built with different flags already disagree about this struct, and packing does not change that.

Rather than pick a fixed wire width here — which would change what half the fleet sends, a different kind of PR from "packing is layout-neutral" — this asserts the invariant packing does guarantee, as an expression: sizeof == 3 + 2 * sizeof(PIXEL_ADDR_TYPE) with range at offset 3. That holds under every flag combination and under the stub. Pinning the wire layout is left as a follow-up, since it is a fleet-compatibility decision.

Expect five new warnings

Both toolchains emit, under -Wall, ignoring packed attribute because of unpacked non-POD field 'CRGB ...' — once each for fade ×2, sparkle, color, circular. GCC declines to pack the CRGB field; the structs still come out packed only because CRGB is sizeof 3 / alignof 1, a FastLED property nothing checked. That is now pinned by static_assert beside the layouts, and the header says not to delete the attribute to silence the warning. There is no -Werror here, so it is noise rather than a break.

state_* structs are deliberately left unpacked: runtime state, never on the wire. HMTLPrograms.cpp only ever memcpys the embedded msg member.

Testing

  • make test-native — 54/54 pass; the in-header asserts now also run on the host via hmtl_sources.cpp.
  • make test-layout — clean under all four ABIs.
  • make test-layout-negative — 184/184 perturbations break the build.
  • make test-simavr — SUCCESS (and, as above, demonstrably not checking these headers).
  • Super-repo make test STRICT=1 and pio run -e ampworks — both green; the bridge imports only the wire header.

Pre-existing and untouched: make test-python fails on main too (13/13, "Emulator did not start" — the hmtl package is not installed; make install-dev is its prerequisite). Not caused by this change and not fixed here.

🤖 Generated with Claude Code

https://claude.ai/code/session_013HqzdRdTRnreSvevaQGQSJ

…missing

HMTL#6 packed the structs in HMTLWireFormat.h and deliberately left the PROGRAM
payloads in HMTLPrograms.h and msg_time_sync_t in TimeSync.h for a follow-up.
They have the same defect: an ATMega328 and an ESP32 disagree about their
layout, so a blob written by one misparses when read by the other.

Two structs moved a field, which is a silent misparse rather than a length
mismatch. hmtl_program_blink_t was 10 B on AVR with off_period at 5 and 12 B on
32-bit with it at 6. msg_time_sync_t was 5 B with timestamp at 1 on AVR and 8 B
with it at 4 on 32-bit, so an ESP32 read an AVR peer's timestamp three bytes
late and off the end of the payload. hmtl_program_color_t joins them under
-DBIG_PIXELS. Four more differed in size only, which still matters because
sizeof feeds the length a peer checks.

msg_time_sync_t needed two changes, not one. Packing fixes AVR-vs-32-bit;
`unsigned long` -> uint32_t fixes the fact that its size otherwise follows the
ABI's long. It also moves to HMTLWireFormat.h, into the placeholder HMTL#6 left
for it, because it is wire format and every module handles MSG_TYPE_TIMESYNC.

Packing is layout-neutral on AVR, verified rather than asserted: under avr-g++
every size and offset is byte-identical packed and unpacked, so the deployed
fleet sees no change. python/hmtl/HMTLprotocol.py already encodes the packed
layouts, so an independent implementation agrees they are the wire truth.

The harder half of this change is that nothing was going to run the new asserts.
The plan assumed the AVR side came free from `make test-simavr`, and it does
not: HMTL_Module's platformio.ini resolves libraries from a machine-local
Arduino directory, not from this repo. Verified rather than assumed — a
deliberately impossible static_assert in HMTLPrograms.h leaves test-simavr
green. Nothing invoked xtensa-esp32-elf-g++ at all, and HMTL_Test substitutes a
stub TimeSync.h that never declared msg_time_sync_t.

So tests/layout/ compiles the real headers with host, host -fpack-struct=1,
avr-g++ and xtensa-esp32-elf-g++, prints a COVERAGE line naming any toolchain it
skipped, and takes REQUIRE_TOOLCHAINS=1 to make a skip an error.
negative_control.py perturbs all 46 asserted numbers across all four ABIs and
requires every one of the 184 compiles to fail; both targets are in `make test`.

The -fpack-struct=1 proxy is only trustworthy here because of the uint32_t
change: with `unsigned long` the host reported 9 bytes, matching neither target.

Two stub fixes fall out. stubs/PixelUtil.h hard-coded PIXEL_ADDR_TYPE as
uint16_t, which is the -DBIG_PIXELS width rather than the default uint8_t — it
now mirrors the real header, since pixel_range_t goes on the wire inside
hmtl_program_color_t. stubs/Debug.h and stubs/PixelUtil.h use headers avr-libc
ships, so avr-g++ can compile them.

hmtl_program_color_t is asserted as an expression rather than a literal, because
its wire width follows -DBIG_PIXELS rather than the target and real envs differ.
Packing fixes AVR-vs-ESP32 for each flag setting; flag-vs-flag is a
fleet-compatibility decision, left for a follow-up.

Expect five new -Wall warnings in module builds: GCC declines to pack the CRGB
members and says so. The structs still come out packed because CRGB is 3 bytes
at alignment 1, which nothing else checked, so that is now pinned by
static_assert next to the layouts.

Co-Authored-By: Claude Opus 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_013HqzdRdTRnreSvevaQGQSJ
…not fail

Self-review on WLED_dev#33 found the exact defect this task exists to prevent,
in the guard the task added.

hmtl_program_color_t's packing assert could not fail. -DBIG_PIXELS is the only
configuration where that struct ever had the interior-padding bug: at the
default PIXEL_ADDR_TYPE width it is 5 bytes with range at 3 on every ABI,
packed or not. The sweep only ever compiled default flags. Demonstrated by
deleting the attribute in a scratch copy - default stayed green, -DBIG_PIXELS
went red with two failures - and the sweep now runs both settings on every
toolchain, so removing the attribute is caught. The same demonstration now
passes.

HMTLWireFormat.h had no asserts to guard. HMTL#6 put its per-struct numbers in
test_wire_format.cpp, which the native suite alone compiles, so the structs
packed to make an AVR and an ESP32 agree were still unchecked under either.
Those numbers move to tests/layout/wire_layout_asserts.h, included by both
layout_check.cpp and test_wire_format.cpp: one copy, now swept by all four
ABIs, and reachable by the negative control.

The negative control also perturbs its own directory's asserts, treats a
perturbation as caught if any pixel variant goes red (some asserts are only
reachable under one), and masks /* */ comments as well as // and strings - the
same false-result bug its docstring already warned about, re-armed.

make -j could silently empty the COVERAGE line: host truncates the file, four
targets append, summary reads, with no ordering between them, and the run still
exits 0. That line is the only thing keeping a partial sweep from reading as a
full one, so the directory is .NOTPARALLEL.

Nothing compiled a TU that dereferences a packed member, so the plan's
-Waddress-of-packed-member item was ticked without being done: packing makes
&packed_field a possibly-unaligned pointer and a real trap on xtensa. The new
packed-access target compiles hmtl_sources.cpp - which pulls HMTLPrograms.cpp -
with the diagnostic as an error, verified by injecting an address-of and
watching it go red. TimeSync.cpp is not in that TU and is not covered; its four
uses were checked by hand instead, all by value.

Coverage goes from 184 perturbations to 460, and the run from ~25 s to ~60 s.

Co-Authored-By: Claude Opus 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_013HqzdRdTRnreSvevaQGQSJ

@aphelps-reviewer-bot aphelps-reviewer-bot left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Self-review of the packing change plus the tests/layout/ harness it adds. I verified the measurements rather than reading them, because the whole claim of this PR is that a guard exists and can fail.

Re-ran at this tip (e39a652):

=== layout sweep COVERAGE: host host-avr-proxy avr xtensa x [default BIG_PIXELS] ===
make packed-access   clean, both pixel widths
make negative        460 perturbations across 4 toolchains x 2 pixel-width variants:
                     460 broke the build as required, 0 did not

The guard can fail — checked directly, not inferred. I removed __attribute__((__packed__)) from hmtl_program_color_t and re-ran the sweep:

--- host [default] ---        clean
--- host [BIG_PIXELS] ---     error: static assertion failed: hmtl_program_color_t changed size
                              error: static assertion failed: hmtl_program_color_t.range moved

Green at the default width, red under -DBIG_PIXELS with exactly two failures. That is the right signature: PIXEL_ADDR_TYPE = uint8_t makes the struct 5 bytes with range at 3 on every ABI packed or not, so a default-only sweep could not have caught it. The two-axis PIXEL_VARIANTS is what makes this guard real, and it is the single most important line in the Makefile.

One finding, and it is about where this harness runs, not what it checks.

The sweep's value in CI depends on a real embedded toolchain being present, and in the super-repo it currently is not guaranteed to be. aphelps/WLED_dev#33 wires make test-hmtl in and makes STRICT=1 imply REQUIRE_ANY_REAL, but ci.yml runs make test STRICT=1 before the step that installs the espressif32 platform, and setup.sh --no-verify explicitly skips the smoke build that fetches the toolchain (setup.sh:113/:117). So xtensa is present there only via the ~/.platformio cache restore. On a cache miss this harness reports "no real embedded toolchain ran" and CI goes red for an environment reason. I have raised it on #33, where the fix belongs; noting it here because it determines whether the four-ABI coverage this PR builds is actually exercised upstream or only on a warm runner. The pending "install avr-gcc on CI" task resolves it properly.

Checked and fine, recorded so it is not re-litigated:

  • The warning: ignoring packed attribute because of unpacked non-POD field 'CRGB …' on fade x2, sparkle, color and circular is real — GCC genuinely does not pack those fields. The layouts hold only because CRGB is sizeof 3 / alignof 1, which is a FastLED property nothing else checks. This header documents that at the top and pins it with static_assert(sizeof(CRGB) == 3) / alignof(CRGB) == 1. That is the correct handling: the attribute is not what makes those structs safe, the asserts are, and the header says so.
  • msg_time_sync_t has exactly one definition on this branch (HMTLWireFormat.h:346). The old unpacked unsigned long version is gone from TimeSync.h, replaced by a comment explaining the move and the guard — no divergent second copy left behind, which was the failure mode worth checking after a struct relocation.
  • Asserting in the real header rather than only in test_wire_format/ is load-bearing, and the comment gives the actual reason: that suite substitutes stubs/TimeSync.h, which never declared the struct at all. That is precisely how this one stayed unpacked while the rest of the wire format was fixed.
  • wire_layout_asserts.h is now in negative_control.py's GUARDED, so the harness negative-controls its own file — the perturbation count went from 184 to 460, consistent with the added axis and the added file.
  • unsigned long to uint32_t on timestamp is a no-op for real targets (4 bytes on both AVR and xtensa) and is what makes the -fpack-struct=1 host proxy trustworthy, since that proxy is only valid for structs free of long and pointers. The Makefile comment states that constraint explicitly.

@aphelps-claude-bot
aphelps-claude-bot merged commit 64cc71d into main Aug 14, 2026
@aphelps-claude-bot
aphelps-claude-bot deleted the pack-programs-timesync-wire-structs branch August 14, 2026 19:09
aphelps-claude-bot added a commit to aphelps/WLED_dev that referenced this pull request Aug 14, 2026
…nter (#33)

* wire the HMTL cross-ABI layout sweep into make test, and bump the pointer

aphelps/HMTL#7 packs the PROGRAM payloads and msg_time_sync_t so an ATMega328
and an ESP32 stop disagreeing about them, and adds HMTL/tests/layout/ — the
thing that actually compiles those headers with avr-g++ and
xtensa-esp32-elf-g++, because nothing did before.

This repo has never invoked HMTL's own `make test`, so without a target here the
new guard would not run in CI: exactly the failure mode the guard exists to
prevent. test-hmtl delegates to HMTL/tests/layout/ and then to its negative
control, which perturbs all 46 asserted numbers across all four ABIs and
requires every one of the 184 compiles to fail.

Coverage is reported rather than assumed. The sweep prints a COVERAGE line
naming any toolchain it skipped, so a host-only run cannot read as a full one,
and REQUIRE_TOOLCHAINS=1 turns a skip into an error — pass it once a runner has
avr-g++ and xtensa-esp32-elf-g++ installed.

Verified end to end: breaking one assert in HMTL/Libraries/TimeSync/TimeSync.h
turns `make test-hmtl` red here, not just inside the submodule.

Co-Authored-By: Claude Opus 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_013HqzdRdTRnreSvevaQGQSJ

* make STRICT imply "at least one real embedded ABI ran"

Self-review caught that `make test STRICT=1` on a runner with neither cross
toolchain would compile the host pair, report success, and have cross-checked no
embedded ABI at all - the same "a skipped suite reports green" failure the STRICT
machinery in this Makefile already exists to prevent, reintroduced by the target
I added to enforce it.

STRICT now passes REQUIRE_ANY_REAL to the sweep. That is deliberately weaker than
REQUIRE_TOOLCHAINS: it demands not a particular compiler but that at least one of
avr-g++ / xtensa ran, so it holds today (the espressif32 platform install brings
xtensa to the runner) while still failing loudly if the real coverage ever drops
to zero. Verified both directions: passes as-is, exits 2 with PIO_PKG pointed at
nothing.

Also runs the sweep's new packed-access check, and bumps the pointer for
aphelps/HMTL@e39a652, which fixes a colour-struct guard that could not fail.

Co-Authored-By: Claude Opus 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_013HqzdRdTRnreSvevaQGQSJ

* repoint HMTL at the squash-merged wire-format packing commit

HMTL#7 landed on main as 64cc71d. The pointer this branch carried (e39a652)
was the pre-squash branch head, which no ref reaches now that the branch is
deleted — a fresh `git submodule update --init` would fail to fetch it.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_013HqzdRdTRnreSvevaQGQSJ

---------

Co-authored-by: aphelps-claude-bot <[email protected]>
Co-authored-by: Claude Opus 5 <[email protected]>
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