Repository navigation
Pack the PROGRAM and TIMESYNC payloads, and build the cross-ABI check that was missing - #7
Conversation
…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
left a comment
There was a problem hiding this comment.
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 becauseCRGBissizeof 3 / alignof 1, which is a FastLED property nothing else checks. This header documents that at the top and pins it withstatic_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_thas exactly one definition on this branch (HMTLWireFormat.h:346). The old unpackedunsigned longversion is gone fromTimeSync.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 substitutesstubs/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.his now innegative_control.py'sGUARDED, 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 longtouint32_tontimestampis a no-op for real targets (4 bytes on both AVR and xtensa) and is what makes the-fpack-struct=1host proxy trustworthy, since that proxy is only valid for structs free oflongand pointers. The Makefile comment states that constraint explicitly.
…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]>
Finishes what HMTL#6 started. That PR packed the structs in
HMTLWireFormat.hand deliberately left the PROGRAM payloads inHMTLPrograms.handmsg_time_sync_tinTimeSync.hout 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:
hmtl_program_blink_toff_periodat 5 vs 6msg_time_sync_ttimestampat 1 vs 4hmtl_program_color_t(-DBIG_PIXELS)rangeat 3 vs 4An ESP32 reading an AVR blink blob takes
off_periodfrom AVR bytes 6-7 — the high byte ofoff_periodplusoff_value[0]. For TIMESYNC the direction matters: an ESP32 reads an AVR peer'stimestampthree bytes late and off the end of the payload; AVR→ESP32 is the mirror, three bytes early.Size only — still breaks interoperation, because
sizeoffeeds theHMTL_MSG_*_LENfamily a peer length-checks against:timed_change10/12,fade11/12,sparkle13/14,circular9/10.sequence(32) andbrightness(1) never diverged; packed and asserted anyway so the invariant is uniform.msg_time_sync_tneeded two changes, not onePacking fixes AVR-vs-32-bit.
unsigned long→uint32_tfixes the fact that the struct's size otherwise follows the ABI'slong— 4 bytes on both real targets but 8 on any LP64 host. All consumers are inTimeSync.cpp, all by value, none take its address, noprintfformats involved.It also moves into
HMTLWireFormat.h, filling the placeholder comment HMTL#6 left for it: it is wire format, every module handlesMSG_TYPE_TIMESYNC, andHMTL_Command_CLIcan 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.pyindependently 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'splatformio.iniresolves its libraries from a machine-local Arduino directory, not from this repo — verified rather than assumed: a deliberately impossiblestatic_assertinLibraries/HMTLMessaging/HMTLPrograms.hleavesmake test-simavrgreen. Nothing in any Makefile invokedxtensa-esp32-elf-g++at all, andHMTL_Testsubstitutes a stubTimeSync.hthat never declaredmsg_time_sync_t.So this PR adds
tests/layout/:layout_check.cppincludes the real headers and has no test body — every assertion is astatic_assertbeside the struct it constrains, so compiling is the test.-fpack-struct=1,avr-g++andxtensa-esp32-elf-g++, ends with aCOVERAGE:line naming anything it skipped, and takesREQUIRE_TOOLCHAINS=1to turn a skip into an error. A partial sweep must not read as a full one.negative_control.pyperturbs 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=1proxy is trustworthy here only because of theuint32_tchange — withunsigned longthe host reported 9 bytes, matching neither real target. Do not extend it to a struct with alongor pointer member without re-checking.Two stub fixes fall out
stubs/PixelUtil.hhard-codedPIXEL_ADDR_TYPEasuint16_t, which is the-DBIG_PIXELSwidth; the default isuint8_t.pixel_range_tgoes on the wire insidehmtl_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.handstubs/Debug.hnow use headers avr-libc actually ships, soavr-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_tis twoPIXEL_ADDR_TYPE,uint8_tby default anduint16_tunder-DBIG_PIXELS, and real envs differ (lightbringer_*,rfm_328_10_2,esp32_routedset it;nano,uno,esp32_breadboarddo 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)withrangeat 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 forfade×2,sparkle,color,circular. GCC declines to pack theCRGBfield; the structs still come out packed only becauseCRGBissizeof 3 / alignof 1, a FastLED property nothing checked. That is now pinned bystatic_assertbeside the layouts, and the header says not to delete the attribute to silence the warning. There is no-Werrorhere, so it is noise rather than a break.state_*structs are deliberately left unpacked: runtime state, never on the wire.HMTLPrograms.cpponly evermemcpys the embeddedmsgmember.Testing
make test-native— 54/54 pass; the in-header asserts now also run on the host viahmtl_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).make test STRICT=1andpio run -e ampworks— both green; the bridge imports only the wire header.Pre-existing and untouched:
make test-pythonfails onmaintoo (13/13, "Emulator did not start" — thehmtlpackage is not installed;make install-devis its prerequisite). Not caused by this change and not fixed here.🤖 Generated with Claude Code
https://claude.ai/code/session_013HqzdRdTRnreSvevaQGQSJ