Skip to content

chore: upgrade native toolchain to Zig 0.17.1 - #10818

Draft
GrapeBaBa wants to merge 18 commits into
unstablefrom
grapebaba/zig-0.17.1
Draft

GrapeBaBa wants to merge 18 commits into
unstablefrom
grapebaba/zig-0.17.1

Conversation

@GrapeBaBa

@GrapeBaBa GrapeBaBa commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor

Motivation

Upgrade Lodestar's Zig code and native Node.js addon to Zig 0.17.1. Migrate APIs that the Zig 0.17.0 release notes (also the notes for 0.17.1) or the 0.17.1 standard library mark deprecated or removed, rather than relying on compatibility aliases.

Description

  • Update compiler pins, build configuration, dependencies, CI, Docker images, and contributor instructions.
  • Migrate allocator formatting and printing (Allocator.print, std.mem.print), SafeAllocator, bit sets, type reflection, enum backing integers (@backingInt / @fromBackingInt, without redundant @intCast), target metadata, ceiling division, @memmove, std.Io.Dir.path, std.tar.extract, and the Run.add*Arg2 build APIs. Port the execution block hash module that landed on unstable during review.
  • Audit the release notes' silent semantic changes: @bitCast on arrays now follows logical bit order, so the two UDP casts between network-order u32 fields and [4]u8 use std.mem.toBytes / bytesToValue; @hasDecl now sees only pub declarations, and every probed type lives in another file, so results are unchanged.
  • Remove two stale benchmark declarations whose source files do not exist. A separate commit fixes cancellation after short UDP sends, and Darwin socket options now come from std.posix.
  • Fuzzing: on Linux, link the native harness archives instead of LLVM bitcode, so AFL++ does not need an LLVM version compatible with Zig's. Zig emits trace-pc-guard coverage with the LLVM backend for AFL++.
  • zapi: @chainsafe/zapi@4.5.0 passes --global-cache-dir (removed in Zig 0.17) and only accepts the old optimize names. patches/@chainsafe__zapi@4.5.0.patch passes the cache through ZIG_GLOBAL_CACHE_DIR, and the optimize mode moved into zapi.variants (-Doptimize=safe). Drop both once a zapi release includes chore: support Zig 0.17.1 zapi#96.
  • Depends on chore: support Zig 0.17.1 hashtree-z#14, chore: support Zig 0.17.1 snappy.zig#7, chore: support Zig 0.17.1 zig-yaml#2, chore: support Zig 0.17.1 zapi#96, and feat!: port zbuild to Zig 0.17 zbuild#10, pinned to their PR heads with hashes from zig fetch. Re-pin to the merged main commits before this PR merges, since squash merges replace these SHAs.
  • fix(state_transition): avoid writing through undefined optional propo… #10793 is required for 0.17.1: 0.17.1 reads an optional's null flag from its low bit, so writing through an undefined optional panics in debug and safe builds (68 test:state_transition tests crash without it). It is merged into this branch from unstable.

AI Assistance Disclosure

Hermes Agent and Claude Code assisted with the implementation, migration review, and local validation of this PR.

@codecov

codecov Bot commented Nov 27, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 59.10%. Comparing base (31011e1) to head (bcb938a).
⚠️ Report is 1 commits behind head on unstable.

Additional details and impacted files
@@             Coverage Diff              @@
##           unstable   #10818      +/-   ##
============================================
+ Coverage     59.09%   59.10%   +0.01%     
============================================
  Files           983      983              
  Lines         40451    40448       -3     
  Branches       8615     8615              
============================================
+ Hits          23903    23907       +4     
+ Misses        14099    14091       -8     
- Partials       2449     2450       +1     
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Dockerfile and Dockerfile.dev bumped ZIG_VERSION to 0.17.1 but kept the 0.16.0 tarball checksums, so sha256sum -c failed and the images could not build. Values match https://ziglang.org/download/index.json and Dockerfile.fuzz.
Zig 0.17.1 std.posix defines Darwin IP.PKTINFO, IP.DONTFRAG, IPV6.RECVPKTINFO, IPV6.PKTINFO and IPV6.DONTFRAG with the values native.zig hardcoded (26, 28, 61, 46, 62), and the same symbols on Linux, so take them from std. IP.DONTFRAG is only referenced in comptime-pruned Darwin branches; udp tests pass on macOS and x86_64-linux-gnu / aarch64-linux-musl builds compile.

The epoch cache test comment referred to -Doptimize=ReleaseFast, which Zig 0.17 spells fast.
Linking the native archive on Linux bypasses afl-cc's instrumentation pass, and root_module.fuzz alone makes Zig emit only its own inline 8-bit counters, so AFL++ saw no edges in Zig code. Enable trace-pc-guard, which AFL++'s runtime counts, and force the LLVM backend: x86_64 Debug otherwise uses the self-hosted backend, which emits no sancov at all.

Measured in ubuntu:24.04 arm64 with Ubuntu's AFL++, fuzz-ssz_basic built with -Doptimize=safe, afl-fuzz -V 15 on corpus/ssz_basic-cmin:
- before: 0 trace_pc_guard call sites, total_edges 18, edges_found 4
- after: 15356 call sites, total_edges 15374, edges_found 18

Zig 0.17.1 src/codegen/llvm.zig enables TracePCGuard only from san_cov_trace_pc_guard. On x86_64-linux Debug, an archive gets __sancov_guards and __sanitizer_cov_trace_pc_guard references only with both use_llvm and the flag set. Archives for x86_64 safe and aarch64 Debug are byte-identical with or without use_llvm.
Replace APIs that Zig 0.17.1 std marks deprecated: std.fmt.bufPrint/bufPrintSentinel -> std.mem.print/printSentinel, std.mem.indexOfScalar -> findScalar, std.mem.copyForwards -> @memmove (every site copies equal lengths; two shift in place), std.fs.path/max_path_bytes -> std.Io.Dir.path/max_path_bytes (including code emitted by the spec test writers), std.tar.pipeToFileSystem -> std.tar.extract, and Run add*Arg -> add*Arg2.

The 0.17 release notes change @bitcast on arrays to follow logical bit order independent of endianness. The two UDP casts between in_pktinfo's network-order u32 fields and [4]u8 meant memory reinterpretation, so use std.mem.toBytes / bytesToValue. Identical on the little-endian binding targets.

Audited @hasDecl, which now only sees pub declarations: every probed type lives in another file, so results are unchanged.

Comments now spell optimize modes the 0.17 way.

Verified with Zig 0.17.1 on macOS arm64: test:state_transition, udp, era, hex, cpu_count, beacon_node, tidy, fault_io (396/411 passed, 15 skipped); build-lib:bindings, benches, scripts and spec writers (92/92); build-test:udp for x86_64-linux-gnu and aarch64-linux-musl; test/fuzz configures.
Brings in #10793, which matches the cherry-picked 93e0f4a, so its diff drops out of this branch.

Resolve the epoch_cache_test.zig conflict by removing the comment saying the regression only reproduces in fast builds. Under Zig 0.17.1, reverting the #10793 fix crashes 68 test:state_transition tests in debug with "attempt to use null value", including both #10793 tests: 0.17.1 reads an optional's null flag from its low bit, so the 0xAA undefined fill reads as null in safe modes too.
@fromBackingInt coerces its operand to the enum's backing integer, and Zig only coerces implicitly when no value can be lost, so the @intcast that zig fmt's @enumFromInt upgrade inserts is a no-op wherever the operand already has that type or widens to it. Arithmetic, shift and bitwise operands are evaluated without a result type either way, so overflow and truncation behaviour is unchanged.

Removed it on 203 of 207 lines; the remaining four narrow usize to Node.Id's u32 backing. Every build-test module, build-lib:bindings and the bench, script and spec writer executables compile with Zig 0.17.1 (177/177 steps), and persistent_merkle_tree, ssz, state_transition, fork_types, ere_verifier, consensus_types and tidy tests pass (866/868, 2 skipped).
Re-pin hashtree-z (69158be), snappy.zig (23869bb), zig-yaml (2b6c488) and zapi (8315594) to the PR heads that drop redundant casts and the remaining deprecated Zig 0.17 APIs. Hashes from zig fetch of each URL.

With these pins: all Zig test modules except fixture-backed spec tests pass (1019/1020, 1 skipped), test/fuzz configures, the mainnet addon builds, and binding tests pass except suites needing unbuilt TS packages or ERA fixtures.
Re-pin zbuild to 7424bc8 (ChainSafe/zbuild#10, drops deprecated mem.indexOf*/Build.dupe and tests 0.17.0 and 0.17.1) and zapi to bc33a3d (ChainSafe/zapi#96, pinned to the same zbuild). Hashes from zig fetch with an empty global cache.

Zig test modules, bindings and executables build and pass, test/fuzz configures, and the mainnet addon builds.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: In Progress

Development

Successfully merging this pull request may close these issues.

2 participants