Skip to content

Commit e39a652

Browse files
close the review's five gaps in the layout guard, one of which could 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
1 parent 002dd74 commit e39a652

6 files changed

Lines changed: 275 additions & 153 deletions

File tree

‎Makefile‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,10 @@ build-all:
5151
cd platformio/TimeSyncExample && $(PIO) run -e nano
5252

5353
# test-layout-negative is in the default run, not an optional extra: it takes
54-
# ~25 s and it is the only thing standing between "the layout guard passed" and
55-
# "the layout guard cannot fail", which this subsystem has produced before.
54+
# ~60 s and it is the only thing standing between "the layout guard passed" and
55+
# "the layout guard cannot fail", which this subsystem has produced before —
56+
# including once in this very change, where the colour struct's packing guard
57+
# could not fail until the sweep started compiling -DBIG_PIXELS as well.
5658
test: test-python test-native test-simavr test-layout test-layout-negative
5759

5860
test-python:
@@ -90,6 +92,7 @@ test-simavr:
9092
test-layout:
9193
@echo "=== Track 4: cross-ABI wire layout sweep ==="
9294
$(MAKE) -C $(LAYOUT_DIR)
95+
$(MAKE) -C $(LAYOUT_DIR) packed-access
9396

9497
test-layout-negative:
9598
@echo "=== Negative control: every layout assert must fail when broken ==="

‎platformio/HMTL_Test/test/test_wire_format/test_wire_format.cpp‎

Lines changed: 5 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -32,106 +32,11 @@ void tearDown() {}
3232
// Compile-time: sizes and offsets, identical on every ABI
3333
// ---------------------------------------------------------------------------
3434

35-
#define WF_SIZE(t, n) static_assert(sizeof(t) == (n), #t " changed size")
36-
#define WF_OFF(t, f, n) static_assert(offsetof(t, f) == (n), #t "." #f " moved")
37-
38-
WF_SIZE(config_hdr_v1_t, 5);
39-
WF_OFF(config_hdr_v1_t, magic, 0);
40-
WF_OFF(config_hdr_v1_t, version, 1);
41-
WF_OFF(config_hdr_v1_t, address, 2);
42-
WF_OFF(config_hdr_v1_t, num_outputs, 3);
43-
WF_OFF(config_hdr_v1_t, flags, 4);
44-
45-
// 8 B with address at 3. Unpacked this was 10 B with address at 4 on 32-bit
46-
// targets — interior padding, the worst of the drift the packing fixed.
47-
WF_SIZE(config_hdr_v2_t, 8);
48-
WF_OFF(config_hdr_v2_t, magic, 0);
49-
WF_OFF(config_hdr_v2_t, protocol_version, 1);
50-
WF_OFF(config_hdr_v2_t, hardware_version, 2);
51-
WF_OFF(config_hdr_v2_t, address, 3);
52-
WF_OFF(config_hdr_v2_t, reserved, 5);
53-
WF_OFF(config_hdr_v2_t, num_outputs, 6);
54-
WF_OFF(config_hdr_v2_t, flags, 7);
55-
56-
WF_SIZE(config_hdr_v3_t, 10);
57-
WF_OFF(config_hdr_v3_t, magic, 0);
58-
WF_OFF(config_hdr_v3_t, protocol_version, 1);
59-
WF_OFF(config_hdr_v3_t, hardware_version, 2);
60-
WF_OFF(config_hdr_v3_t, baud, 3);
61-
WF_OFF(config_hdr_v3_t, num_outputs, 4);
62-
WF_OFF(config_hdr_v3_t, flags, 5);
63-
WF_OFF(config_hdr_v3_t, device_id, 6);
64-
WF_OFF(config_hdr_v3_t, address, 8);
65-
66-
WF_SIZE(output_hdr_t, 2);
67-
WF_OFF(output_hdr_t, type, 0);
68-
WF_OFF(output_hdr_t, output, 1);
69-
70-
WF_SIZE(msg_hdr_t, 8);
71-
WF_OFF(msg_hdr_t, startcode, 0);
72-
WF_OFF(msg_hdr_t, crc, 1);
73-
WF_OFF(msg_hdr_t, version, 2);
74-
WF_OFF(msg_hdr_t, length, 3);
75-
WF_OFF(msg_hdr_t, type, 4);
76-
WF_OFF(msg_hdr_t, flags, 5);
77-
WF_OFF(msg_hdr_t, address, 6); // catches a socket_addr_t width change too
78-
79-
WF_SIZE(msg_value_t, 4);
80-
WF_OFF(msg_value_t, hdr, 0); // bitfields have no offsetof; see the runtime test
81-
82-
WF_SIZE(msg_rgb_t, 5);
83-
WF_OFF(msg_rgb_t, hdr, 0);
84-
WF_OFF(msg_rgb_t, values, 2);
85-
86-
WF_SIZE(msg_program_t, 35);
87-
WF_OFF(msg_program_t, hdr, 0);
88-
WF_OFF(msg_program_t, type, 2);
89-
WF_OFF(msg_program_t, values, 3);
90-
91-
// 15 B on every target. Unpacked this was 15 on AVR and 16 on 32-bit, which
92-
// made HMTL_MSG_POLL_MIN_LEN 23 or 24 and let a length-checking peer reject a
93-
// structurally valid poll response.
94-
WF_SIZE(msg_poll_response_t, 15);
95-
WF_OFF(msg_poll_response_t, config, 0);
96-
WF_OFF(msg_poll_response_t, object_type, 10);
97-
WF_OFF(msg_poll_response_t, recv_buffer_size, 12);
98-
WF_OFF(msg_poll_response_t, msg_version, 14);
99-
WF_OFF(msg_poll_response_t, data, 15);
100-
101-
WF_SIZE(msg_dumpconfig_response_t, 0);
102-
103-
WF_SIZE(msg_set_addr_t, 4);
104-
WF_OFF(msg_set_addr_t, device_id, 0);
105-
WF_OFF(msg_set_addr_t, address, 2);
106-
107-
WF_SIZE(msg_sensor_response_t, 0);
108-
109-
WF_SIZE(msg_sensor_data_t, 2);
110-
WF_OFF(msg_sensor_data_t, sensor_type, 0);
111-
WF_OFF(msg_sensor_data_t, data_len, 1);
112-
WF_OFF(msg_sensor_data_t, data, 2);
113-
114-
// 5 B with timestamp at 1. This one lived in TimeSync.h until it moved here with
115-
// the rest of the wire format; unpacked and with `unsigned long timestamp` it
116-
// was 8 B with timestamp at 4 on 32-bit targets, so an ESP32 and an AVR read
117-
// each other's TIMESYNC payloads three bytes out of step. Two changes were
118-
// needed, not one: packing fixes AVR-vs-32-bit, and uint32_t fixes the fact that
119-
// `unsigned long` is 8 bytes on this LP64 host — which is also why a host
120-
// -fpack-struct=1 measurement of the OLD struct (9 B) matched neither target.
121-
WF_SIZE(msg_time_sync_t, 5);
122-
WF_OFF(msg_time_sync_t, sync_phase, 0);
123-
WF_OFF(msg_time_sync_t, timestamp, 1);
124-
125-
// The frame lengths that follow from the above. These are the numbers that go
126-
// out on the wire, so they are also the numbers a peer length-checks against.
127-
static_assert(HMTL_MSG_VALUE_LEN == 12, "VALUE frame length changed");
128-
static_assert(HMTL_MSG_RGB_LEN == 13, "RGB frame length changed");
129-
static_assert(HMTL_MSG_PROGRAM_LEN == 43, "PROGRAM frame length changed");
130-
static_assert(HMTL_MSG_POLL_MIN_LEN == 23, "POLL frame length changed");
131-
static_assert(HMTL_MSG_SET_ADDR_LEN == 12, "SET_ADDR frame length changed");
132-
static_assert(HMTL_MSG_SENSOR_MIN_LEN == 8, "SENSOR frame length changed");
133-
static_assert(HMTL_MSG_DUMPCONFIG_MIN_LEN == 8, "DUMPCONFIG frame length changed");
134-
static_assert(HMTL_MSG_TIMESYNC_LEN == 13, "TIMESYNC frame length changed");
35+
// The compile-time block lives in tests/layout/wire_layout_asserts.h so the
36+
// same numbers are checked by avr-g++ and xtensa-esp32-elf-g++ too, not just
37+
// by this host-only suite. The runtime cases below are what a static_assert
38+
// cannot express.
39+
#include "../../../../tests/layout/wire_layout_asserts.h"
13540

13641
// ---------------------------------------------------------------------------
13742
// Runtime: the bytes really land where the offsets claim

‎tests/layout/Makefile‎

Lines changed: 75 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,25 +8,38 @@
88
#
99
# make — compile layout_check.cpp with every toolchain found
1010
# make negative — prove the asserts can fail (negative_control.py)
11-
# make REQUIRE_TOOLCHAINS=1 — treat a missing cross-toolchain as an error
11+
# make packed-access — -Werror=address-of-packed-member over the real .cpp
12+
# make REQUIRE_TOOLCHAINS=1 — treat any missing cross-toolchain as an error
13+
# make REQUIRE_ANY_REAL=1 — require at least one of avr-g++ / xtensa
14+
#
15+
# Two axes, not one. Each toolchain is compiled BOTH with and without
16+
# -DBIG_PIXELS, because that flag is the only configuration in which
17+
# hmtl_program_color_t ever had the interior-padding bug: with the default
18+
# PIXEL_ADDR_TYPE = uint8_t the struct is 5 bytes with range at 3 on every ABI,
19+
# packed or not, so a default-flags-only sweep leaves that guard unable to fail.
20+
# Verified by deleting the packed attribute in a scratch copy: default flags
21+
# stay green, -DBIG_PIXELS goes red with two failures.
1222
#
1323
# A missing avr-g++ or xtensa-esp32-elf-g++ is reported and skipped by default,
1424
# because this has to be runnable on a machine that has neither. It is never
1525
# silent: the run ends with a COVERAGE line naming what ran and what did not, so
16-
# a partial sweep cannot be mistaken for a full one. Set REQUIRE_TOOLCHAINS=1
17-
# where full coverage is the point — that is the switch CI should use once the
18-
# runner has both toolchains installed.
26+
# a partial sweep cannot be mistaken for a full one. REQUIRE_TOOLCHAINS=1 makes
27+
# any skip an error; REQUIRE_ANY_REAL=1 is the weaker, CI-friendly form — it
28+
# only insists that the sweep touched at least one real embedded ABI, so
29+
# "everything passed" can never mean "nothing but the host ran".
1930

2031
HMTL_ROOT := $(CURDIR)/../..
2132
LIBS := $(HMTL_ROOT)/Libraries
2233
STUBS := $(HMTL_ROOT)/platformio/HMTL_Test/stubs
34+
NATIVE_LIB := $(HMTL_ROOT)/platformio/HMTL_Test/lib/hmtl_sources
2335
PIO_PKG ?= $(HOME)/.platformio/packages
2436

2537
AVR_CXX ?= $(PIO_PKG)/toolchain-atmelavr/bin/avr-g++
2638
XTENSA_CXX ?= $(PIO_PKG)/toolchain-xtensa-esp32/bin/xtensa-esp32-elf-g++
2739
HOST_CXX ?= g++
2840

2941
REQUIRE_TOOLCHAINS ?=
42+
REQUIRE_ANY_REAL ?=
3043

3144
# -I$(LIBS)/TimeSync comes first deliberately: HMTL_Test/stubs also holds a
3245
# TimeSync.h, and that stub never declared msg_time_sync_t at all, which is how
@@ -40,18 +53,34 @@ INCLUDES := -I$(LIBS)/TimeSync -I$(LIBS)/HMTLprotocol -I$(LIBS)/HMTLMessaging \
4053
CXXFLAGS := -fsyntax-only -std=c++11 -Wall -include Arduino.h -DDEBUG_LEVEL=0 \
4154
-DDISABLE_RS485 -DDISABLE_XBEE -DDISABLE_MPR121 $(INCLUDES)
4255

43-
COVERAGE_FILE := .coverage-note
56+
PIXEL_VARIANTS := default BIG_PIXELS
57+
COVERAGE_FILE := .coverage-note
58+
59+
# The COVERAGE line is assembled by appending to a file that `host` truncates
60+
# and `summary` reads. Under -j that ordering is not guaranteed and the line
61+
# comes out empty or stale while the build still exits 0 — which defeats the one
62+
# mechanism keeping a partial sweep from reading as a full one.
63+
.NOTPARALLEL:
4464

45-
.PHONY: all host host-avr-proxy avr xtensa negative summary clean
65+
.PHONY: all host host-avr-proxy avr xtensa negative packed-access summary clean
4666

4767
all: host host-avr-proxy avr xtensa summary
4868

69+
# $(1) label, $(2) compiler + arch flags
70+
define sweep
71+
@rm -f $(COVERAGE_FILE).$(1)
72+
@for v in $(PIXEL_VARIANTS); do \
73+
case $$v in default) extra="" ;; *) extra="-D$$v" ;; esac; \
74+
echo "--- $(1) [$$v] ---"; \
75+
$(2) $(CXXFLAGS) $$extra layout_check.cpp || exit 1; \
76+
done
77+
@echo "$(1)" >> $(COVERAGE_FILE)
78+
endef
79+
4980
# The host as-is: 32-bit-style alignment with a 64-bit long.
5081
host:
51-
@echo "--- host ($(HOST_CXX)) ---"
5282
@rm -f $(COVERAGE_FILE)
53-
$(HOST_CXX) $(CXXFLAGS) layout_check.cpp
54-
@echo "host" >> $(COVERAGE_FILE)
83+
$(call sweep,host,$(HOST_CXX))
5584

5685
# -fpack-struct=1 as an AVR stand-in. Valid for these structs ONLY because none
5786
# of them contains a `long` or a pointer any more: `unsigned long` is 8 bytes on
@@ -60,14 +89,11 @@ host:
6089
# Changing timestamp to uint32_t is what made this proxy trustworthy. Do not
6190
# extend it to a struct with a long or pointer member without re-checking.
6291
host-avr-proxy:
63-
@echo "--- host, -fpack-struct=1 (AVR proxy) ---"
64-
$(HOST_CXX) $(CXXFLAGS) -fpack-struct=1 layout_check.cpp
65-
@echo "host-avr-proxy" >> $(COVERAGE_FILE)
92+
$(call sweep,host-avr-proxy,$(HOST_CXX) -fpack-struct=1)
6693

6794
avr:
6895
@if [ -x "$(AVR_CXX)" ]; then \
69-
echo "--- avr-g++ ---"; \
70-
"$(AVR_CXX)" -mmcu=atmega328p $(CXXFLAGS) layout_check.cpp && echo "avr" >> $(COVERAGE_FILE); \
96+
$(MAKE) --no-print-directory _sweep LABEL=avr CXX_CMD="$(AVR_CXX) -mmcu=atmega328p"; \
7197
elif [ -n "$(REQUIRE_TOOLCHAINS)" ]; then \
7298
echo "ERROR: avr-g++ not found at $(AVR_CXX) (REQUIRE_TOOLCHAINS is set)"; exit 1; \
7399
else \
@@ -76,24 +102,55 @@ avr:
76102

77103
xtensa:
78104
@if [ -x "$(XTENSA_CXX)" ]; then \
79-
echo "--- xtensa-esp32-elf-g++ ---"; \
80-
"$(XTENSA_CXX)" $(CXXFLAGS) layout_check.cpp && echo "xtensa" >> $(COVERAGE_FILE); \
105+
$(MAKE) --no-print-directory _sweep LABEL=xtensa CXX_CMD="$(XTENSA_CXX)"; \
81106
elif [ -n "$(REQUIRE_TOOLCHAINS)" ]; then \
82107
echo "ERROR: xtensa-esp32-elf-g++ not found at $(XTENSA_CXX) (REQUIRE_TOOLCHAINS is set)"; exit 1; \
83108
else \
84109
echo "--- xtensa-esp32-elf-g++ SKIPPED: not found at $(XTENSA_CXX) ---"; \
85110
fi
86111

112+
.PHONY: _sweep
113+
_sweep:
114+
$(call sweep,$(LABEL),$(CXX_CMD))
115+
116+
# Packing makes &packed_field a possibly-unaligned pointer, which is undefined
117+
# behaviour and a real runtime trap on xtensa. layout_check.cpp only includes
118+
# headers, so it cannot catch an existing address-of; this compiles the actual
119+
# implementation TU with the diagnostic promoted to an error.
120+
#
121+
# Scope, stated so it is not over-read: hmtl_sources.cpp pulls HMTLPrograms.cpp
122+
# and ProgramManager.cpp, which is where the newly-packed PROGRAM payloads are
123+
# read and written. TimeSync.cpp is not in that TU and is not covered here; its
124+
# four uses of msg_time_sync_t were checked by hand instead — all by value, none
125+
# taking an address. Note the include order below is the NATIVE suite's (stubs
126+
# ahead of Libraries), unlike the sweep above: hmtl_sources.cpp brings in the
127+
# stub TimeSync class, which collides with the real one if Libraries wins.
128+
packed-access:
129+
@echo "--- address-of-packed-member (host, -Werror) ---"
130+
@for v in $(PIXEL_VARIANTS); do \
131+
case $$v in default) extra="" ;; *) extra="-D$$v" ;; esac; \
132+
echo " [$$v]"; \
133+
$(HOST_CXX) -fsyntax-only -std=c++11 -Werror=address-of-packed-member \
134+
-include Arduino.h -DDEBUG_LEVEL=5 -DDISABLE_RS485 -DDISABLE_XBEE -DDISABLE_MPR121 \
135+
-I$(NATIVE_LIB) -I$(STUBS) -I$(LIBS)/HMTLMessaging -I$(LIBS)/HMTLTypes \
136+
-I$(LIBS)/HMTLprotocol $$extra $(NATIVE_LIB)/hmtl_sources.cpp || exit 1; \
137+
done
138+
87139
# Say what was actually covered. "The sweep passed" and "the sweep passed on the
88140
# only two ABIs available here" have to read differently.
89141
summary:
90-
@echo "=== layout sweep COVERAGE: $$(tr '\n' ' ' < $(COVERAGE_FILE)) ==="
142+
@echo "=== layout sweep COVERAGE: $$(tr '\n' ' ' < $(COVERAGE_FILE)) x [$(PIXEL_VARIANTS)] ==="
91143
@if ! grep -q '^avr$$' $(COVERAGE_FILE) || ! grep -q '^xtensa$$' $(COVERAGE_FILE); then \
92144
echo " (partial — a real toolchain was missing; set REQUIRE_TOOLCHAINS=1 to make that an error)"; \
93145
fi
146+
@if [ -n "$(REQUIRE_ANY_REAL)" ] && ! grep -qE '^(avr|xtensa)$$' $(COVERAGE_FILE); then \
147+
echo "ERROR: no real embedded toolchain ran — the sweep proved nothing about AVR or xtensa"; \
148+
echo " (REQUIRE_ANY_REAL is set; install avr-g++ or xtensa-esp32-elf-g++)"; \
149+
exit 1; \
150+
fi
94151

95152
negative:
96153
./negative_control.py
97154

98155
clean:
99-
rm -f $(COVERAGE_FILE)
156+
rm -f $(COVERAGE_FILE) $(COVERAGE_FILE).*

‎tests/layout/layout_check.cpp‎

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,7 @@
1717
#include "HMTLPrograms.h"
1818
#include "TimeSync.h"
1919

20-
// Frame lengths, which are what a peer length-checks against and the reason a
21-
// trailing-padding-only difference still breaks interoperation.
22-
static_assert(HMTL_MSG_VALUE_LEN == 12, "VALUE frame length changed");
23-
static_assert(HMTL_MSG_RGB_LEN == 13, "RGB frame length changed");
24-
static_assert(HMTL_MSG_PROGRAM_LEN == 43, "PROGRAM frame length changed");
25-
static_assert(HMTL_MSG_POLL_MIN_LEN == 23, "POLL frame length changed");
26-
static_assert(HMTL_MSG_SET_ADDR_LEN == 12, "SET_ADDR frame length changed");
27-
static_assert(HMTL_MSG_TIMESYNC_LEN == 13, "TIMESYNC frame length changed");
20+
// Every struct in HMTLWireFormat.h, by size and field offset. HMTL#6 packed
21+
// those and asserted them in HMTL_Test's host-only suite, so until now nothing
22+
// checked the packing under the two compilers it exists to reconcile.
23+
#include "wire_layout_asserts.h"

0 commit comments

Comments
 (0)