From daaaf8cad6c57cf2aeab9ebd2c6b3e82487199d1 Mon Sep 17 00:00:00 2001 From: JC-000 <3798556+JC-000@users.noreply.github.com> Date: Thu, 15 Oct 2026 13:38:04 -0500 Subject: [PATCH 1/4] make: sort source wildcards so the PRG is locale-independent (#301) make 3.81 returns $(wildcard) in locale collation order and the lists are the ld65 object order, so ip65-onchip, uci-onchip and uci-comb each had two PRG hashes. $(sort) is byte order. New hashes differ from v0.5.1. Co-Authored-By: Claude Opus 5.5 (1M context) --- CLAUDE.md | 4 +++- Makefile | 10 ++++---- tools/test_build_flags_stamp.py | 42 +++++++++++++++++++++++++++++++++ 3 files changed, 51 insertions(+), 5 deletions(-) diff --git a/CLAUDE.md b/CLAUDE.md index 3542ddd..b677bef 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -95,7 +95,9 @@ though the name in it is not. Still true, and still how you prove a build: `.o` hashes are never evidence — ca65 stamps build time into every object; ld65 does not propagate it, so -the PRG is deterministic and **its sha256 is the check**. One consequence +the PRG is deterministic and **its sha256 is the check**. The PRG is also +locale-independent (#301: every source wildcard is `$(sort)`ed, because make 3.81 +returns them in collation order), pinned by `tools/test_build_flags_stamp.py`. One consequence of the stamp: `CA65FLAGS` carries two `$(abspath ...)` `.incbin` roots, so moving a checkout forces exactly one rebuild. diff --git a/Makefile b/Makefile index dffa86c..50b7aab 100644 --- a/Makefile +++ b/Makefile @@ -267,8 +267,10 @@ endif # Paths come from LINK_OUTPUTS' variables below (lazy =, so they may follow). LD65FLAGS = -C $(CFG) -Ln $(LABELS) -m $(LINK_MAP) --dbgfile $(LINK_DBG) -# Source inventory. -TOP_SRCS := $(wildcard src/*.s) +# Source inventory. Every wildcard here is wrapped in $(sort): make 3.81 returns +# wildcard hits in locale-collation order, and the list is the link order, so +# an unsorted one made the PRG depend on LANG (#301). $(sort) is byte order. +TOP_SRCS := $(sort $(wildcard src/*.s)) # Crypto sources: wildcard-discovered. In-tree src/crypto/x25519.s + # src/crypto/fe25519.s are used under both backends; the Phase C.1 # libs/x25519/ overlay integration was rolled back after it broke the @@ -279,11 +281,11 @@ TOP_SRCS := $(wildcard src/*.s) # (build/lib/nistcurves-p256.a). The now-unused files were physically # deleted in Phase G. ecdsa_verify.s stays — rewritten as a thin # dispatcher that packs the BE struct + calls ecdsa_verify_256. -CRYPTO_SRCS_ALL := $(wildcard src/crypto/*.s) +CRYPTO_SRCS_ALL := $(sort $(wildcard src/crypto/*.s)) # Shared crypto infrastructure introduced in Phase C.0: canonical ZP map, # overlay swap dispatcher, init orchestrator, shared sqtab stub. Always # linked; sibling-lib integration (Phase C.3) hangs off these. -CRYPTO_SHARED_SRCS := $(wildcard src/crypto/shared/*.s) +CRYPTO_SHARED_SRCS := $(sort $(wildcard src/crypto/shared/*.s)) IP65_SRCS := src/net/ip65/ip65_blob.s src/net/ip65/net.s src/net/ip65/net_banner.s src/net/ip65/exports.s src/net/ip65/net_manifest.s UCI_SRCS := src/net/uci/net.s src/net/uci/uci_cmd.s src/net/uci/net_manifest.s diff --git a/tools/test_build_flags_stamp.py b/tools/test_build_flags_stamp.py index f578c78..e641e6c 100644 --- a/tools/test_build_flags_stamp.py +++ b/tools/test_build_flags_stamp.py @@ -1059,6 +1059,48 @@ def test_stamp_records_the_whole_command_line(): ) +def test_prg_is_independent_of_the_build_locale(): + """The PRG must not depend on LANG/LC_ALL (#301). + + GNU make 3.81 returns `$(wildcard)` hits in the locale's collation order + (en_US ignores the underscore: tls_record_io, tls_record, tls13; C is + byte order: tls13, tls_record, tls_record_io), and the wildcard lists are + the ld65 object order. Unsorted, v0.5.1's three wildcard-built products + each had two PRG hashes. `$(sort)` is byte order, so the link order -- and + the bytes -- are now locale-free. + + The probe first proves the two locales really do order `src/*.s` + differently under the make in use (4.x re-sorts wildcard results, so it + may not); without that, equal hashes would prove nothing about the fix. + """ + _require_toolchain() + with Farm() as farm: + probe = farm.path("probe.mk") + probe.write_text("all:\n\t@echo $(wildcard src/*.s)\n") + orders = {} + for loc in ("C", "en_US.UTF-8"): + proc = subprocess.run( + ["make", "-f", str(probe)], cwd=farm.dir, text=True, + env=dict(os.environ, LC_ALL=loc), + stdout=subprocess.PIPE, stderr=subprocess.STDOUT) + assert proc.returncode == 0, proc.stdout + orders[loc] = proc.stdout.split() + assert sorted(orders["C"]) == sorted(orders["en_US.UTF-8"]) + if orders["C"] == orders["en_US.UTF-8"]: + require(False, + "this make/locale pair does not reorder $(wildcard); " + "the cross-locale comparison would be vacuous", + executed=0, total=1, certifies=CERTIFIES, + opt_out_env="C64_ALLOW_SKIP") + shas = {} + for loc in ("C", "en_US.UTF-8"): + with Farm() as farm: + farm.make(*UCI, env={"LC_ALL": loc}) + shas[loc] = farm.sha() + assert shas["C"] == shas["en_US.UTF-8"], ( + f"PRG depends on the build locale: {shas}") + + def main() -> int: print("=== build/flags.stamp ===") tests = [(name, fn) for name, fn in sorted(globals().items()) From 6097c4aabd4cb1477322672cbac7f37686be70c0 Mon Sep 17 00:00:00 2001 From: JC-000 <3798556+JC-000@users.noreply.github.com> Date: Thu, 15 Oct 2026 14:16:03 -0500 Subject: [PATCH 2/4] make: put the object list in flags.stamp; pin cs_CZ and the stale-order relink (#301) Co-Authored-By: Claude Opus 5.5 (1M context) --- CLAUDE.md | 8 ++- Makefile | 1 + tools/test_build_flags_stamp.py | 114 ++++++++++++++++++++++++-------- 3 files changed, 91 insertions(+), 32 deletions(-) diff --git a/CLAUDE.md b/CLAUDE.md index b677bef..123987c 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -96,9 +96,11 @@ though the name in it is not. Still true, and still how you prove a build: `.o` hashes are never evidence — ca65 stamps build time into every object; ld65 does not propagate it, so the PRG is deterministic and **its sha256 is the check**. The PRG is also -locale-independent (#301: every source wildcard is `$(sort)`ed, because make 3.81 -returns them in collation order), pinned by `tools/test_build_flags_stamp.py`. One consequence -of the stamp: `CA65FLAGS` carries two `$(abspath ...)` `.incbin` roots, so +locale-independent (#301): make 3.81 returns `$(wildcard)` in collation +order, so `src/*.s` and `src/crypto/*.s` are `$(sort)`ed; the test pins those +two (C/en_US/cs_CZ). `src/crypto/shared/*.s`'s sort is defensive, unpinned. +`flags.stamp` carries the object list, so a changed link order relinks. +One consequence of the stamp: `CA65FLAGS` carries two `$(abspath ...)` `.incbin` roots, so moving a checkout forces exactly one rebuild. Also: `$(PRG)`'s recipe `rm -f`s the target first, because ld65 writes diff --git a/Makefile b/Makefile index 50b7aab..69a011c 100644 --- a/Makefile +++ b/Makefile @@ -763,6 +763,7 @@ FLAGS_STAMP_BODY := printf '%s\n' \ 'CA65FLAGS=$(CA65FLAGS)' \ 'LD65FLAGS=$(LD65FLAGS)' \ 'CFG_SHA256=$(CFG_SHA256)' \ + 'OBJECTS=$(ALL_OBJS)' \ 'SIBLING_LIB_ARCHIVES=$(SIBLING_LIB_ARCHIVES)' \ 'X25519_SEG_FE25519=$(X25519_SEG_FE25519)' \ 'X25519_SEG_LADDER=$(X25519_SEG_LADDER)' \ diff --git a/tools/test_build_flags_stamp.py b/tools/test_build_flags_stamp.py index e641e6c..1f79935 100644 --- a/tools/test_build_flags_stamp.py +++ b/tools/test_build_flags_stamp.py @@ -71,7 +71,8 @@ from pathlib import Path sys.path.insert(0, str(Path(__file__).resolve().parent)) -from _skip_policy import EXIT_CANNOT_RUN, cannot_run, require # noqa: E402 +from _skip_policy import ( # noqa: E402 + EXIT_CANNOT_RUN, VoluntarySkip, cannot_run, require) REPO = Path(__file__).resolve().parent.parent @@ -1059,48 +1060,100 @@ def test_stamp_records_the_whole_command_line(): ) +LOCALES = ("C", "en_US.UTF-8", "cs_CZ.UTF-8") + +CERTIFIES_LOCALE = ("that the PRG's bytes do not depend on the build locale " + "(#301), and that a stale link order is rebuilt") + + +def _wildcard_orders(farm, pattern): + """`$(wildcard pattern)` as the make in use returns it, per locale.""" + probe = farm.path("probe.mk") + probe.write_text(f"all:\n\t@echo $(wildcard {pattern})\n") + orders = {} + for loc in LOCALES: + proc = subprocess.run( + ["make", "-f", str(probe)], cwd=farm.dir, text=True, + env=dict(os.environ, LC_ALL=loc), + stdout=subprocess.PIPE, stderr=subprocess.STDOUT) + assert proc.returncode == 0, proc.stdout + orders[loc] = proc.stdout.split() + return orders + + def test_prg_is_independent_of_the_build_locale(): """The PRG must not depend on LANG/LC_ALL (#301). GNU make 3.81 returns `$(wildcard)` hits in the locale's collation order - (en_US ignores the underscore: tls_record_io, tls_record, tls13; C is - byte order: tls13, tls_record, tls_record_io), and the wildcard lists are - the ld65 object order. Unsorted, v0.5.1's three wildcard-built products - each had two PRG hashes. `$(sort)` is byte order, so the link order -- and - the bytes -- are now locale-free. - - The probe first proves the two locales really do order `src/*.s` - differently under the make in use (4.x re-sorts wildcard results, so it - may not); without that, equal hashes would prove nothing about the fix. + and the wildcard lists are the ld65 object order. Unsorted, each of the + three wildcard-built products had three PRG hashes (C, en_US, cs_CZ). + `$(sort)` is byte order, so link order -- and the bytes -- are locale-free. + + Each sorted list is pinned only if some locale pair really reorders it, + so the probe checks that per list and refuses a vacuous comparison: + * src/*.s en_US ignores the underscore (tls_record_io before + tls13); C does not. + * src/crypto/*.s cs_CZ sorts "ch" after "h" (chacha20 after hmac_drbg); + C and en_US AGREE here, so two locales are not enough. + * src/crypto/shared/*.s no locale tried reorders it (crypto_init, + mul_tables), so its `$(sort)` is defensive and NOT + pinned by this test. """ _require_toolchain() with Farm() as farm: - probe = farm.path("probe.mk") - probe.write_text("all:\n\t@echo $(wildcard src/*.s)\n") - orders = {} - for loc in ("C", "en_US.UTF-8"): - proc = subprocess.run( - ["make", "-f", str(probe)], cwd=farm.dir, text=True, - env=dict(os.environ, LC_ALL=loc), - stdout=subprocess.PIPE, stderr=subprocess.STDOUT) - assert proc.returncode == 0, proc.stdout - orders[loc] = proc.stdout.split() - assert sorted(orders["C"]) == sorted(orders["en_US.UTF-8"]) - if orders["C"] == orders["en_US.UTF-8"]: - require(False, - "this make/locale pair does not reorder $(wildcard); " - "the cross-locale comparison would be vacuous", - executed=0, total=1, certifies=CERTIFIES, - opt_out_env="C64_ALLOW_SKIP") + for pattern in ("src/*.s", "src/crypto/*.s"): + orders = _wildcard_orders(farm, pattern) + assert len({tuple(sorted(o)) for o in orders.values()}) == 1, ( + f"{pattern}: locales disagree on the file SET: {orders}") + if len({tuple(o) for o in orders.values()}) == 1: + require(False, + f"no locale in {LOCALES} reorders $(wildcard " + f"{pattern}) under this make; the cross-locale " + "comparison would be vacuous for that list", + executed=0, total=1, certifies=CERTIFIES_LOCALE, + opt_out_env="C64_ALLOW_SKIP") shas = {} - for loc in ("C", "en_US.UTF-8"): + for loc in LOCALES: with Farm() as farm: farm.make(*UCI, env={"LC_ALL": loc}) shas[loc] = farm.sha() - assert shas["C"] == shas["en_US.UTF-8"], ( + assert len(set(shas.values())) == 1, ( f"PRG depends on the build locale: {shas}") +def test_stale_link_order_is_rebuilt_to_the_clean_hash(): + """A tree linked in another object order must relink, not stay stale (#301). + + The object list is a link input that was in neither flags.stamp nor any + prerequisite, so a build/ left by an unsorted Makefile answered "Nothing + to be done" and kept the old PRG. The stale tree here is built by a + private copy of the Makefile with the TOP_SRCS `$(sort)` removed, under a + locale that reorders it, then rebuilt with the real Makefile. + """ + _require_toolchain() + loc = {"LC_ALL": "en_US.UTF-8"} + with Farm() as clean: + clean.make(*UCI, env=loc) + oracle = clean.sha() + with Farm() as farm: + real = REPO / "Makefile" + text = real.read_text() + old = "TOP_SRCS := $(sort $(wildcard src/*.s))" + assert old in text, "TOP_SRCS line moved; re-aim this test" + os.unlink(farm.path("Makefile")) + farm.path("Makefile").write_text( + text.replace(old, "TOP_SRCS := $(wildcard src/*.s)")) + farm.make(*UCI, env=loc) + stale = farm.sha() + os.unlink(farm.path("Makefile")) + os.symlink(real, farm.path("Makefile")) + farm.make(*UCI, env=loc) + assert stale != oracle, "the stale tree was not actually stale" + assert farm.sha() == oracle, ( + "a changed object order did not invalidate the tree: PRG " + f"{farm.sha()} != clean build {oracle}") + + def main() -> int: print("=== build/flags.stamp ===") tests = [(name, fn) for name, fn in sorted(globals().items()) @@ -1126,6 +1179,9 @@ def main() -> int: for name, fn in tests: try: fn() + except VoluntarySkip as exc: # named before Exception + print(f" SKIP {name}\n {exc}") + continue except Exception as exc: # noqa: BLE001 - a crash is a fail failed += 1 print(f" FAIL {name}\n {type(exc).__name__}: {exc}") From e3db5774b893e4bd32fa4ff92f52f9d9a5c57196 Mon Sep 17 00:00:00 2001 From: JC-000 <3798556+JC-000@users.noreply.github.com> Date: Thu, 15 Oct 2026 14:16:15 -0500 Subject: [PATCH 3/4] CLAUDE.md: rewrap the #301 sentence Co-Authored-By: Claude Opus 5.5 (1M context) --- CLAUDE.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/CLAUDE.md b/CLAUDE.md index 123987c..8d7ff00 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -100,8 +100,8 @@ locale-independent (#301): make 3.81 returns `$(wildcard)` in collation order, so `src/*.s` and `src/crypto/*.s` are `$(sort)`ed; the test pins those two (C/en_US/cs_CZ). `src/crypto/shared/*.s`'s sort is defensive, unpinned. `flags.stamp` carries the object list, so a changed link order relinks. -One consequence of the stamp: `CA65FLAGS` carries two `$(abspath ...)` `.incbin` roots, so -moving a checkout forces exactly one rebuild. +One consequence of the stamp: `CA65FLAGS` carries two +`$(abspath ...)` `.incbin` roots, so moving a checkout forces exactly one rebuild. Also: `$(PRG)`'s recipe `rm -f`s the target first, because ld65 writes nothing on a memory-area overflow and every rig loads the PRG by path — an From bfa0ed7b1ec41cbfb054a769753b45366bbd5363 Mon Sep 17 00:00:00 2001 From: JC-000 <3798556+JC-000@users.noreply.github.com> Date: Thu, 15 Oct 2026 14:24:11 -0500 Subject: [PATCH 4/4] test/Makefile: per-test certifies text, fail-closed stale-order precondition, wording nits (#301) Co-Authored-By: Claude Opus 5.5 (1M context) --- CLAUDE.md | 3 ++- Makefile | 2 +- tools/test_build_flags_stamp.py | 19 +++++++++++++++---- 3 files changed, 18 insertions(+), 6 deletions(-) diff --git a/CLAUDE.md b/CLAUDE.md index 8d7ff00..a35a189 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -101,7 +101,8 @@ order, so `src/*.s` and `src/crypto/*.s` are `$(sort)`ed; the test pins those two (C/en_US/cs_CZ). `src/crypto/shared/*.s`'s sort is defensive, unpinned. `flags.stamp` carries the object list, so a changed link order relinks. One consequence of the stamp: `CA65FLAGS` carries two -`$(abspath ...)` `.incbin` roots, so moving a checkout forces exactly one rebuild. +`$(abspath ...)` `.incbin` roots, so moving a checkout forces exactly one +rebuild. Also: `$(PRG)`'s recipe `rm -f`s the target first, because ld65 writes nothing on a memory-area overflow and every rig loads the PRG by path — an diff --git a/Makefile b/Makefile index 69a011c..6a1c561 100644 --- a/Makefile +++ b/Makefile @@ -783,7 +783,7 @@ _ := $(shell mkdir -p build; \ else # -n / -q / -t: compare and report, mutate nothing. See MAKE_DRY_RUN above. ifneq ($(shell $(FLAGS_STAMP_BODY) 2>/dev/null | cmp -s - $(FLAGS_STAMP) >/dev/null 2>&1 || echo differs),) -$(warning DRY RUN (-$(MAKE_OPT_LETTERS)): build flags differ from $(FLAGS_STAMP).) +$(warning DRY RUN (-$(MAKE_OPT_LETTERS)): build inputs (flags or object list) differ from $(FLAGS_STAMP).) $(warning A real build would rewrite the stamp and delete every object and $(LINK_OUTPUTS)) $(warning at parse time. NOTHING was deleted; the tree is untouched.) endif diff --git a/tools/test_build_flags_stamp.py b/tools/test_build_flags_stamp.py index 1f79935..3f1e2c8 100644 --- a/tools/test_build_flags_stamp.py +++ b/tools/test_build_flags_stamp.py @@ -1062,8 +1062,9 @@ def test_stamp_records_the_whole_command_line(): LOCALES = ("C", "en_US.UTF-8", "cs_CZ.UTF-8") -CERTIFIES_LOCALE = ("that the PRG's bytes do not depend on the build locale " - "(#301), and that a stale link order is rebuilt") +CERTIFIES_LOCALE = "whether the PRG's bytes depend on the build locale (#301)" +CERTIFIES_STALE_ORDER = ("whether a tree linked in another object order is " + "rebuilt (#301)") def _wildcard_orders(farm, pattern): @@ -1148,7 +1149,13 @@ def test_stale_link_order_is_rebuilt_to_the_clean_hash(): os.unlink(farm.path("Makefile")) os.symlink(real, farm.path("Makefile")) farm.make(*UCI, env=loc) - assert stale != oracle, "the stale tree was not actually stale" + if stale == oracle: + require(False, + "this make/locale does not reorder $(wildcard src/*.s); " + "the stale tree is not actually stale, so a relink to the " + "clean hash would prove nothing", + executed=0, total=1, certifies=CERTIFIES_STALE_ORDER, + opt_out_env="C64_ALLOW_SKIP") assert farm.sha() == oracle, ( "a changed object order did not invalidate the tree: PRG " f"{farm.sha()} != clean build {oracle}") @@ -1176,10 +1183,12 @@ def main() -> int: failed = 0 ran = 0 + skipped = 0 for name, fn in tests: try: fn() except VoluntarySkip as exc: # named before Exception + skipped += 1 print(f" SKIP {name}\n {exc}") continue except Exception as exc: # noqa: BLE001 - a crash is a fail @@ -1199,7 +1208,9 @@ def main() -> int: ) print(f"\n{'FAILED' if failed else 'PASSED'}: {failed} failure(s) " - f"({ran}/{len(tests)} executed)") + f"({ran}/{len(tests)} executed" + + (f", {skipped} skipped via C64_ALLOW_SKIP" if skipped else "") + + ")") return 1 if failed else 0