From f45ce5041cdf176da1683ea5b416e287c49f8582 Mon Sep 17 00:00:00 2001 From: "Jeffrey C. Ollie" Date: Tue, 6 Oct 2026 12:50:48 -0500 Subject: [PATCH] Build with the Zig 0.17.0 release The toolchain is nixpkgs' `zig_0_17`, and zig-http moves to v0.3.0, the release that builds with it. build.zig.zon.nix is regenerated with `zon2nix --17`, which needed a newer zon2nix. The patched `fuzzableZig` goes: 0.17's test runner compiles a fuzz build as it is. With `use_llvm` on the fuzz tests the built-in fuzzer has coverage to steer by, so `zig build fuzz --fuzz` now works; `zig build fuzz-run` stays, for CI and for reproducing a run from its seed. Ports: `b.args` to `addPassthruArgs`, the docs server takes the emitted docs directory rather than an install path, which 0.17 does not know while configuring, the two `**` repetitions in the tests become `@splat`, and `zig fmt` renames `@intFromEnum` to `@backingInt`. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01PQaKJGMqGdkwNcphrPaNWB --- README.md | 15 ++- build.zig | 17 ++- build.zig.zon | 6 +- build.zig.zon.nix | 330 ++++++++++++++++++--------------------------- flake.lock | 14 +- flake.nix | 56 +------- package.nix | 6 +- src/Connection.zig | 4 +- src/close.zig | 38 +++--- src/frame.zig | 14 +- tests/e2e.zig | 6 +- tests/fuzz.zig | 4 +- tools/fuzz.zig | 18 +-- 13 files changed, 203 insertions(+), 325 deletions(-) diff --git a/README.md b/README.md index 8196257..abdac5d 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,8 @@ SPDX-License-Identifier: MIT # zig-websocket The WebSocket Protocol — [RFC 6455][] — for Zig servers, on top of -[zig-http][]. +[zig-http][]. It builds with Zig 0.17; for Zig 0.16, use the `zig-0.16` +branch, where the 0.1 releases live. One handler serves WebSocket over HTTP/1.1 and over HTTP/2 without knowing which it is doing: @@ -269,14 +270,18 @@ The oracle the conversation target rests on has tests of its own, because an oracle that never fails is a test that never ran. ```console +$ zig build fuzz --fuzz # Zig's fuzzer, with a web interface +$ zig build fuzz --fuzz=1M # a bounded run, then a report $ zig build fuzz-run -- --seconds 60 $ zig build fuzz-run -- --seconds 600 --target conversations ``` -Zig 0.16.0 cannot build a test executable in fuzz mode, and leaves the -fuzzer's coverage table empty once patched so that it can; `flake.nix` carries -the one-word patch with the whole story in a comment, and `tools/fuzz.zig` is -a loop written down honestly instead. +`zig build fuzz --fuzz` is Zig's own fuzzer, steered by coverage. That needs +the fuzz tests compiled by LLVM — the self-hosted backend Debug uses by +default emits no coverage instrumentation — so `build.zig` sets `use_llvm` on +them. `zig build fuzz-run` drives the same targets through the loop in +`tools/fuzz.zig` instead: seeded, bounded, and an ordinary executable, which +is what CI runs and what reproduces a run exactly. ## The virtual machine test diff --git a/build.zig b/build.zig index 72bffb8..2754dbe 100644 --- a/build.zig +++ b/build.zig @@ -58,7 +58,7 @@ pub fn build(b: *std.Build) void { const run_cmd = b.addRunArtifact(exe); run_cmd.step.dependOn(b.getInstallStep()); run_cmd.stdio = .inherit; - if (b.args) |args| run_cmd.addArgs(args); + run_cmd.addPassthruArgs(); const run_step = b.step("run", "Run the demonstration server"); run_step.dependOn(&run_cmd.step); @@ -134,15 +134,19 @@ pub fn build(b: *std.Build) void { const fuzz_tests = b.addTest(.{ .root_module = fuzz_mod, .filters = if (fuzz_filter) |f| &.{f} else &.{}, + // LLVM rather than the self-hosted backend Debug uses by default, + // which emits no coverage instrumentation: under it `--fuzz` has no + // program counters to steer by and ends with "pcs_len was zero". + .use_llvm = true, }); test_step.dependOn(&b.addRunArtifact(fuzz_tests).step); const fuzz_step = b.step("fuzz", "The fuzz targets: add --fuzz to fuzz them"); fuzz_step.dependOn(&b.addRunArtifact(fuzz_tests).step); - // The loop that drives those targets without Zig's fuzzer, which this - // toolchain cannot usefully run. `tools/fuzz.zig` in zig-http says why; - // the short version is that the coverage table comes back empty. + // The loop that drives those same targets without Zig's fuzzer: seeded + // and bounded, from an ordinary executable, which is what CI wants and + // what reproduces a run exactly. const fuzz_run = b.addExecutable(.{ .name = "zig-websocket-fuzz", .root_module = b.createModule(.{ @@ -154,7 +158,7 @@ pub fn build(b: *std.Build) void { }); const run_fuzz = b.addRunArtifact(fuzz_run); run_fuzz.stdio = .inherit; - if (b.args) |a| run_fuzz.addArgs(a); + run_fuzz.addPassthruArgs(); const fuzz_run_step = b.step("fuzz-run", "Fuzz the targets with a loop of our own"); fuzz_run_step.dependOn(&run_fuzz.step); @@ -191,8 +195,7 @@ pub fn build(b: *std.Build) void { }), }); const run_docs_server = b.addRunArtifact(docs_server); - run_docs_server.step.dependOn(&install_docs.step); - run_docs_server.addArg(b.getInstallPath(.prefix, "docs")); + run_docs_server.addDirectoryArg(library.getEmittedDocs()); run_docs_server.addArg(b.fmt("{d}", .{docs_port})); // It runs until interrupted, so its output has to reach the terminal // rather than being captured by the build runner. diff --git a/build.zig.zon b/build.zig.zon index 8ebf596..3fad7f1 100644 --- a/build.zig.zon +++ b/build.zig.zon @@ -2,11 +2,11 @@ .name = .zig_websocket, .version = "0.2.0", .fingerprint = 0xaf13c0d0b83756a5, - .minimum_zig_version = "0.16.0", + .minimum_zig_version = "0.17.0", .dependencies = .{ .http = .{ - .url = "git+https://git.jcollie.dev/jeff/zig-http.git#097f0514d8a5505bf8804ac493c8385b0260ae35", - .hash = "http-0.0.0-e28mQNVIGQALHNk9ccgdRzrIWTrE-5HAW5WNx7xFy_Ko", + .url = "git+https://git.jcollie.dev/jeff/zig-http.git#fbd67ad9293a936195b3fddc68148eeaeb69c149", + .hash = "http-0.3.0-e28mQOCUGQB9cf2VLXmlE6Yxronb7C6_k9-04Qlt7UXx", }, }, .paths = .{ diff --git a/build.zig.zon.nix b/build.zig.zon.nix index 49816c3..4f6fbd5 100644 --- a/build.zig.zon.nix +++ b/build.zig.zon.nix @@ -1,215 +1,145 @@ # generated by zon2nix (https://github.com/jcollie/zon2nix) { lib, - linkFarm, - fetchzip, - fetchurl, fetchgit, + fetchurl, + fetchzip, runCommandLocal, - zig_0_16, + zig_0_17, zstd, name ? "zig-packages", + # Accepted and ignored. Expressions from zon2nix before 0.8 built the + # packages with `linkFarm`, and some callers override it with a version that + # copies, to work around Zig mishandling symlinked package directories + # (https://codeberg.org/ziglang/zig/issues/32121). The packages are real + # directories now, so the override is not needed, and is taken here only so + # that passing it is not an error. + linkFarm ? null, }: let - unpackZigArtifact = - { - name, - artifact, - }: - runCommandLocal name - { - nativeBuildInputs = [ zig_0_16 ]; + # A git repository, checked out the way Zig reads it: every file as + # committed. An ordinary checkout applies the repository's `.gitattributes`, + # which can rewrite a file's line endings or run it through a filter, and + # the package would then no longer match its hash. Pointing git at its + # built-in empty tree for attributes turns them all off, and `core.autocrlf` + # is turned off too, since it converts line endings even without them. + # zon2nix computes the hash with exactly these settings. + fetchZigGit = + args: + fetchgit ( + args + // { + fetchSubmodules = false; + preFetch = '' + export GIT_ATTR_SOURCE=4b825dc642cb6eb9a060e54bf8d69288fbee4904 + export GIT_CONFIG_COUNT=1 GIT_CONFIG_KEY_0=core.autocrlf GIT_CONFIG_VALUE_0=false + ''; } - '' - # workaround https://codeberg.org/ziglang/zig/issues/31866 - # https://github.com/Cloudef/zig2nix/issues/54 - mkdir "$TMPDIR/src" "$TMPDIR/cache" "$TMPDIR/cache/tmp" - touch "$TMPDIR/src/build.zig" - hash="$(cd "$TMPDIR/src" && zig fetch --global-cache-dir "$TMPDIR/cache" ${artifact})" - mkdir "$out" - tar zxvf "$TMPDIR/cache/p/$hash.tar.gz" --directory "$out/" --strip-components=1 - ''; - - fetchZig = - { - name, - url, - hash, - unpack, - }: - let - artifact = - if unpack then - fetchzip { - inherit url hash; - nativeBuildInputs = [ zstd ]; - } - else - fetchurl { inherit url hash; }; - in - unpackZigArtifact { inherit name artifact; }; + ); - fetchGitZig = - { - name, - url, - hash, - }: - let - parts = lib.splitString "#" url; - url_base = builtins.elemAt parts 0; - url_without_query = builtins.elemAt (lib.splitString "?" url_base) 0; - rev_base = builtins.elemAt parts 1; - rev = - if builtins.match "^[a-fA-F0-9]{40}$" rev_base != null then rev_base else "refs/heads/${rev_base}"; - in - fetchgit { - inherit name rev hash; - url = url_without_query; - deepClone = false; - fetchSubmodules = false; + # Every package, by the hash Zig knows it by, as Nix fetches it. + packages = { + "http-0.3.0-e28mQOCUGQB9cf2VLXmlE6Yxronb7C6_k9-04Qlt7UXx" = fetchZigGit { + name = "http"; + url = "https://git.jcollie.dev/jeff/zig-http.git"; + rev = "fbd67ad9293a936195b3fddc68148eeaeb69c149"; + hash = "sha256-OJqbW1v8t3AMGLbKG+8xhJsQyH11U5hTv8zqGjbLX9M="; + }; + "zig_quic-0.2.1-4oF4ntETGwAH7eWOfQ7EQaDJc4gNOs-0Ocom1rDX1DnE" = fetchZigGit { + name = "quic"; + url = "https://git.jcollie.dev/jeff/zig-quic.git"; + rev = "b3d76fa3abd8f0d8642d6f8c734051f0532f8203"; + hash = "sha256-6AojzCp01cC0iMCtHnYr7DZMq3qMa5OxXaPYv6Xn/8s="; + }; + "tls-0.1.0-ER2e0nSlCAB0SXv5OaVhS8bCT7ySMv_8yu3aseACw0ZA" = fetchZigGit { + name = "tls"; + url = "https://git.jcollie.dev/jeff/tls.zig.git"; + rev = "8cc71ca9dbd69d68c0b874023939fa749338b39a"; + hash = "sha256-e04+YsObYCCY2DXSOIOcuyqGiCNFXv5PADgCzdjxl7A="; + }; + "uri-0.3.0-yCrwNPIaFABo5l5b1w9tWcqbFy_dLwf3EJkfI42RqekC" = fetchZigGit { + name = "uri"; + url = "https://git.jcollie.dev/jeff/zig-uri.git"; + rev = "edaf631c840967c2b9edb409caa90f7542d4046c"; + hash = "sha256-6XCjVPhuBzqrtMVnydTWoYJTchSqwrY7YPBZ5X8D6Xo="; + }; + "uucode-0.2.0-ZZjBPoGFVgCiflJNFc6Io8xzC67B6PA5bUkk4LT0kCxg" = fetchZigGit { + name = "uucode"; + url = "https://github.com/jacobsandlund/uucode"; + rev = "ea62149739404a73c202b48a33bf6dd2af4bd9b0"; + hash = "sha256-dKrRJhFThGSdv5k6pEF2adGZIKxUQssAO30XAtLOFQ8="; + }; + "z46-0.2.0-_AxhwAXTCQDzvmmh5CP6zrvAk03GpkQz_fdYpbc53DND" = fetchZigGit { + name = "z46"; + url = "https://git.jcollie.dev/jeff/z46.git"; + rev = "cde52f655fa3a099c016c042c080f8112330f682"; + hash = "sha256-RPKBTbLZ7bIJXb7g5CBytRXGPkT8Tn8dhiavVpz27Tw="; }; + }; - fetchZigArtifact = - { - name, - url, - hash, - unpack, - }: - let - parts = lib.splitString "://" url; - proto = builtins.elemAt parts 0; - path = builtins.elemAt parts 1; - fetcher = { - "git+http" = fetchGitZig { - inherit name hash; - url = "http://${path}"; - }; - "git+https" = fetchGitZig { - inherit name hash; - url = "https://${path}"; - }; - http = fetchZig { - inherit name hash unpack; - url = "http://${path}"; - }; - https = fetchZig { - inherit name hash unpack; - url = "https://${path}"; - }; - }; - in - fetcher.${proto}; - # The packages, as real directories holding symlinked files, rather than as - # a farm of symlinked directories. - # - # Zig runs a dependency's own build steps with the working directory set to - # that dependency, and points at the program to run with a path counted in - # directories up from there. Through a symlink the two disagree: Zig counts - # from `//`, four directories below the root, while the kernel - # resolves the working directory to `/nix/store/`, which is three, so - # the path lands one short of where the program is. - # - # It works anyway when the build directory is `/build`, because the sum then - # overshoots into the root and going above the root stays there. It fails - # when the build directory is under `/nix/var/nix/builds`, which is where Nix - # puts it when the sandbox is off. Real directories make the two depths - # agree, so it works either way. - # - # The files have to be real as well, which is the expensive part and cannot - # be avoided. `--symbolic-link` would leave them pointing into each - # dependency's own store path, so that the farm is a few megabytes rather - # than a second copy of every dependency -- but Zig's - # `installHeadersDirectory` walks the directory and copies only the entries - # whose kind is `.file`. Symlinked headers are skipped without a word, and - # the first thing to include one fails with `'dcimgui.h' not found`. - # - # `--link` is not the way out. A hard link into a Nix output is a file the - # builder did not create: inside the Linux sandbox the store is a separate - # mount and `link` fails with `Invalid cross-device link`, while on Darwin it - # succeeds and leaves root-owned files in the output, which Nix refuses while - # canonicalising with `invalid ownership on file`. - # - # So it is a real copy, with `--reflink=auto` to share the blocks on a - # filesystem that can. `nix store optimise` recovers the duplication after - # the fact, hard-linking identical files across the store, which is the - # store's own business to do and not a build's. - copyFarm = - farm: entries: pathDependencyPackages: - runCommandLocal farm - { - # The packages whose own manifest declares a dependency by `.path`. - # Zig 0.16.0 cannot build these through `zig build --system`: it spins - # in userspace forever, because a `.path` dependency's hash is computed - # against the system package directory during the fetch and against the - # real global cache afterwards, and in that mode the two disagree. A - # package that wants `--system` copies each of these into its build - # root and passes `--fork=`; see the README. - passthru = { inherit pathDependencyPackages; }; - } - '' - mkdir -p "$out" - cp --recursive --reflink=auto --dereference --no-preserve=mode \ - ${linkFarm farm entries}/. "$out/" - ''; + # The packages whose own manifest declares a dependency by `.path`, which + # Zig 0.16.0 cannot build through `zig build --system`. Listed here too so + # that every version's expression offers the same things; see the README. + pathDependencyPackages = [ + ]; in -copyFarm name - [ - { - name = "http-0.0.0-e28mQNVIGQALHNk9ccgdRzrIWTrE-5HAW5WNx7xFy_Ko"; - path = fetchZigArtifact { - name = "http"; - url = "git+https://git.jcollie.dev/jeff/zig-http.git#097f0514d8a5505bf8804ac493c8385b0260ae35"; - hash = "sha256-YEbnPZma2lyX+drY5410fXN6Hae4OCy1TzqpqFG2JHA="; - unpack = true; - }; - } - { - name = "zig_quic-0.0.0-4oF4npVmHAAud52Wa14Nm1OpHbSwhaWZ3LbtK6ohprAx"; - path = fetchZigArtifact { - name = "quic"; - url = "git+https://git.jcollie.dev/jeff/zig-quic.git#e0ba118dfad6ddefc1c26fc1687ed151ca1615f4"; - hash = "sha256-IH9WeNw3OvtXWgBL+fubNSe4XEOiCaOu7hjlcA1uvwU="; - unpack = true; - }; - } - { - name = "tls-0.1.0-ER2e0pV5CQCwe6a-c0KHPP-Z3sattGdSLH2upYTHCpBn"; - path = fetchZigArtifact { - name = "tls"; - url = "git+https://git.jcollie.dev/jeff/tls.zig.git?ref=quic#05587bbef303c8e9504fec7d07daeead1d597071"; - hash = "sha256-55wLBBh1A6CFBROoA7PYwJZMNYsb6VS62faF5cEP0MU="; - unpack = true; - }; - } - { - name = "uri-0.1.0-yCrwNNH2EwBMp75OiyjnW1w5FMiFdI0gWghu4QMEVWtU"; - path = fetchZigArtifact { - name = "uri"; - url = "git+https://git.jcollie.dev/jeff/zig-uri.git#c472509ef662a1528f24e283799280bebbeb2777"; - hash = "sha256-76g/TqOdvh97UfcNN2FrnrTnV0VQZ3Y5fRnfB5hoqWw="; - unpack = true; - }; - } - { - name = "uucode-0.2.0-ZZjBPh-6VADBlunHbwABTPng0DH6uJqd4CvvtjZ19tny"; - path = fetchZigArtifact { - name = "uucode"; - url = "git+https://github.com/jacobsandlund/uucode#61e54266895f833b307de81a0e3038cf1f1bebd4"; - hash = "sha256-6Riz1CowKgQr70YAcfozHETUqfOR4gYIwUE5txjX70o="; - unpack = true; - }; - } - { - name = "z46-0.1.0-_AxhwCThCQCYrGgJUgyc2Ozk5i4fwCkmimRXMxmNDPgc"; - path = fetchZigArtifact { - name = "z46"; - url = "git+https://git.jcollie.dev/jeff/z46.git#12de8fc8e77f3d857ea90d1c10fd78c3abdf05a6"; - hash = "sha256-cZR+YgslD5dmps3wRjZcfoyPniDN3X+KEfPY+5vNT1w="; - unpack = true; - }; +# One directory holding every package the way Zig unpacks it -- filtered by +# its manifest's `.paths` and checked against its hash -- which is what +# `zig build --system` wants. +# +# They have to be real directories holding real files, not links into each +# package's own store path. Zig runs a dependency's build steps from inside +# it and counts its way back out through `..`, which lands in the wrong place +# when the directory is a symlink; and `installHeadersDirectory` copies only +# what the directory walk calls a file, so symlinked headers are skipped +# without a word. Building them all in one derivation gives real files from +# the start, without a second copy of everything. +runCommandLocal name + { + nativeBuildInputs = [ zig_0_17 ]; + passthru = { inherit pathDependencyPackages; }; + } + '' + mkdir "$out" + + # Zig 0.17 runs `zig fetch` in a build runner it compiles into the global + # cache the first time, which takes a minute or more. Every package shares + # one cache, then, and it is compiled once here before they are fetched + # several at a time, rather than by each of them at once. + export ZIG_GLOBAL_CACHE_DIR="$TMPDIR/cache" + mkdir -p "$ZIG_GLOBAL_CACHE_DIR" + zig fetch --help >/dev/null + + # Without `--save`, which wants a project to save into, Zig 0.17 unpacks + # nothing where it can be found: what it keeps is `p/.tar.gz` in + # the global cache, the package filtered by its manifest's `.paths` under + # a single directory named by its hash. Unpacking that gives the + # directory `zig build --system` wants. + unpack() { + local work="$TMPDIR/work/$1" + mkdir -p "$work" + + local hash + if ! hash="$(cd "$work" && zig fetch "$2" 2>"$work/log")"; then + cat "$work/log" >&2 + return 1 + fi + if [ "$hash" != "$1" ]; then + echo "$2 is $hash to Zig, not $1" >&2 + return 1 + fi + tar -xzf "$ZIG_GLOBAL_CACHE_DIR/p/$1.tar.gz" -C "$out" + rm -f "$ZIG_GLOBAL_CACHE_DIR/p/$1.tar.gz" + rm -rf "$work" } - ] - [ - ] + export -f unpack + + # Several at a time: `zig fetch` spends most of a package on one core. + # `-r` because otherwise xargs runs `unpack` once, with no arguments, when + # there are no packages at all, and the build fails. + cores="''${NIX_BUILD_CORES:-0}" + if [ "$cores" -le 0 ]; then cores="$(nproc)"; fi + xargs -r -P "$cores" -n 2 bash -c 'unpack "$@"' unpack <<'EOF' + ${lib.concatStrings (lib.mapAttrsToList (hash: src: "${hash} ${src}\n") packages)} + EOF + '' diff --git a/flake.lock b/flake.lock index 276c742..fc94c7a 100644 --- a/flake.lock +++ b/flake.lock @@ -2,11 +2,11 @@ "nodes": { "nixpkgs": { "locked": { - "lastModified": 1789546076, - "narHash": "sha256-vWkSk5bbfTqdtMoSgD9FshACO8JCvXTFi+3cqEp0mH0=", - "rev": "b1b875982b17dabde9b4a37f3e229e74913e6db3", + "lastModified": 1791260994, + "narHash": "sha256-MFBjw66d4h06MvpW6lIl7RwarLteD0sk859CncGwb1Q=", + "rev": "151fa4e8ddfdd8dd25d945ad94ed54a13de9f6e4", "type": "tarball", - "url": "https://releases.nixos.org/nixos/unstable/nixos-26.11pre1074753.b1b875982b17/nixexprs.tar.xz" + "url": "https://releases.nixos.org/nixos/unstable/nixos-26.11pre1086391.151fa4e8ddfd/nixexprs.tar.xz" }, "original": { "type": "tarball", @@ -26,11 +26,11 @@ ] }, "locked": { - "lastModified": 1788977680, - "narHash": "sha256-3vgICcen5N2oAlQrijqJhxn8HEsnXASGPxQLJVYzX0Q=", + "lastModified": 1791261699, + "narHash": "sha256-Y1s36Ny116/8wZNgZj8dOez3qJqA/QpnCfaVs2kuNC8=", "owner": "jcollie", "repo": "zon2nix", - "rev": "7b9c43312de08e176097b8c8920f0dd1a46982be", + "rev": "5efe627e5901a73a8b707f797d08f2835ab59ac2", "type": "github" }, "original": { diff --git a/flake.nix b/flake.nix index d179a2e..501f2fb 100644 --- a/flake.nix +++ b/flake.nix @@ -36,58 +36,6 @@ forLinuxSystems = lib.genAttrs ( lib.filter (system: lib.hasSuffix "-linux" system) lib.systems.flakeExposed ); - - # The devshell's Zig, with one line of its own standard library put - # right, because without it `zig build fuzz --fuzz` cannot compile. - # - # Zig 0.16.0's `compiler/test_runner.zig` reports a failing fuzz input by - # asking `std.debug.writeStackTrace` to print what `@errorReturnTrace()` - # gave it. Those are two different types: an error return trace is a - # `builtin.StackTrace`, a ring buffer with a write index, and that - # function takes a `debug.StackTrace`, which is a plain slice and a count - # of what was skipped. It is a type error, it is on the path taken only - # under `-ffuzz`, and it stops *any* project with a fuzz test in it from - # building one. The fix is the function next door: `writeErrorReturnTrace` - # takes exactly the type in hand and is what the other three places in - # the same file use. - # - # `--replace-fail` is the whole safety of this: the day Zig ships the fix - # the pattern will not be found, the build will fail here rather than - # patch something else, and this can go. - # - # It buys the fuzzer and not its coverage. Nothing in this release - # populates the table of program counters, so a bounded run ends with - # "corrupted coverage file: pcs_len was zero" and an unbounded one - # panics in the build runner's coverage thread; neither is a finding, - # and a finding says "input saved to" above the report. The properties in - # `tests/fuzz.zig` run as ordinary tests either way, and - # `zig build fuzz-run` drives them with a loop of our own. - fuzzableZig = - pkgs: - let - # A farm of symlinks rather than a copy: the library is 217 MB, and - # exactly one file of it is being changed. - library = pkgs.runCommand "zig-0.16.0-lib-fuzz-fix" { } '' - cp -rs --no-preserve=mode ${pkgs.zig_0_16}/lib/zig $out - chmod -R u+w $out - rm $out/compiler/test_runner.zig - cp --no-preserve=mode \ - ${pkgs.zig_0_16}/lib/zig/compiler/test_runner.zig \ - $out/compiler/test_runner.zig - substituteInPlace $out/compiler/test_runner.zig \ - --replace-fail \ - 'std.debug.writeStackTrace(trace, stderr)' \ - 'std.debug.writeErrorReturnTrace(trace, stderr)' - ''; - in - pkgs.symlinkJoin { - name = "zig-0.16.0-fuzzable"; - paths = [ pkgs.zig_0_16 ]; - nativeBuildInputs = [ pkgs.makeWrapper ]; - postBuild = '' - wrapProgram $out/bin/zig --set ZIG_LIB_DIR ${library} - ''; - }; in { packages = forAllSystems ( @@ -134,7 +82,7 @@ default = pkgs.mkShell { name = "zig-websocket"; nativeBuildInputs = [ - (fuzzableZig pkgs) + pkgs.zig_0_17 pkgs.git-pages-cli pkgs.reuse @@ -148,7 +96,7 @@ nativeBuildInputs = [ pkgs.makeWrapper ]; postBuild = '' wrapProgram $out/bin/zon2nix \ - --prefix PATH : ${lib.makeBinPath [ pkgs.zig_0_16 ]} + --prefix PATH : ${lib.makeBinPath [ pkgs.zig_0_17 ]} ''; }) diff --git a/package.nix b/package.nix index 356d071..84a9f45 100644 --- a/package.nix +++ b/package.nix @@ -5,11 +5,11 @@ lib, stdenv, callPackage, - zig_0_16, + zig_0_17, }: let # Generated from build.zig.zon by zon2nix; regenerate with - # nix develop -c zon2nix --16 --nix=build.zig.zon.nix build.zig.zon + # nix develop -c zon2nix --17 --nix=build.zig.zon.nix build.zig.zon zigDeps = callPackage ./build.zig.zon.nix { }; in stdenv.mkDerivation (finalAttrs: { @@ -33,7 +33,7 @@ stdenv.mkDerivation (finalAttrs: { ]; }; - nativeBuildInputs = [ zig_0_16.hook ]; + nativeBuildInputs = [ zig_0_17.hook ]; # `--system` does not merely offer the directory, it forbids fetching # outright: a dependency missing from it is a build error naming the diff --git a/src/Connection.zig b/src/Connection.zig index ca0432b..045bc19 100644 --- a/src/Connection.zig +++ b/src/Connection.zig @@ -333,7 +333,7 @@ fn sendClose(c: *Connection, code: Code, reason: []const u8) Error!void { // `defer` that closes again has done nothing wrong. if (c.sent_code != null or c.state == .closed) return; var payload: [frame.max_control_payload_len]u8 = undefined; - std.mem.writeInt(u16, payload[0..2], @intFromEnum(code), .big); + std.mem.writeInt(u16, payload[0..2], @backingInt(code), .big); const len = @min(reason.len, payload.len - 2); @memcpy(payload[2..][0..len], reason[0..len]); @@ -369,7 +369,7 @@ fn onClose(c: *Connection, payload: []const u8) Error!?Message { if (payload.len == 1) return c.fail(.protocol_error, "close payload of one octet"); if (payload.len >= 2) { - const code: Code = @enumFromInt(std.mem.readInt(u16, payload[0..2], .big)); + const code: Code = @fromBackingInt(@intCast(std.mem.readInt(u16, payload[0..2], .big))); if (!code.isSendable()) return c.fail(.protocol_error, "reserved close code"); // §5.5.1: the reason is "UTF-8-encoded data". if (!std.unicode.utf8ValidateSlice(payload[2..])) { diff --git a/src/close.zig b/src/close.zig index a275db0..983015f 100644 --- a/src/close.zig +++ b/src/close.zig @@ -50,7 +50,7 @@ pub const Code = enum(u16) { /// codes it has actually defined are sendable, which is what stops a /// peer inventing 1016 and meaning something by it. pub fn isSendable(c: Code) bool { - const n = @intFromEnum(c); + const n = @backingInt(c); if (n >= 3000 and n <= 4999) return true; return switch (c) { .normal, @@ -73,14 +73,14 @@ pub const Code = enum(u16) { const testing = std.testing; test "the codes RFC 6455 7.4.1 defines are the ones it numbers" { - try testing.expectEqual(@as(u16, 1000), @intFromEnum(Code.normal)); - try testing.expectEqual(@as(u16, 1001), @intFromEnum(Code.going_away)); - try testing.expectEqual(@as(u16, 1002), @intFromEnum(Code.protocol_error)); - try testing.expectEqual(@as(u16, 1003), @intFromEnum(Code.unsupported_data)); - try testing.expectEqual(@as(u16, 1007), @intFromEnum(Code.invalid_payload)); - try testing.expectEqual(@as(u16, 1008), @intFromEnum(Code.policy_violation)); - try testing.expectEqual(@as(u16, 1009), @intFromEnum(Code.message_too_big)); - try testing.expectEqual(@as(u16, 1011), @intFromEnum(Code.internal_error)); + try testing.expectEqual(@as(u16, 1000), @backingInt(Code.normal)); + try testing.expectEqual(@as(u16, 1001), @backingInt(Code.going_away)); + try testing.expectEqual(@as(u16, 1002), @backingInt(Code.protocol_error)); + try testing.expectEqual(@as(u16, 1003), @backingInt(Code.unsupported_data)); + try testing.expectEqual(@as(u16, 1007), @backingInt(Code.invalid_payload)); + try testing.expectEqual(@as(u16, 1008), @backingInt(Code.policy_violation)); + try testing.expectEqual(@as(u16, 1009), @backingInt(Code.message_too_big)); + try testing.expectEqual(@as(u16, 1011), @backingInt(Code.internal_error)); } test "the three codes that exist only for an application may not be sent" { @@ -95,18 +95,18 @@ test "the three codes that exist only for an application may not be sent" { test "the reserved ranges of 7.4.2" { // Below the protocol's range. - try testing.expect(!@as(Code, @enumFromInt(0)).isSendable()); - try testing.expect(!@as(Code, @enumFromInt(999)).isSendable()); + try testing.expect(!@as(Code, @fromBackingInt(@intCast(0))).isSendable()); + try testing.expect(!@as(Code, @fromBackingInt(@intCast(999))).isSendable()); // Inside it but never defined. - try testing.expect(!@as(Code, @enumFromInt(1016)).isSendable()); - try testing.expect(!@as(Code, @enumFromInt(2999)).isSendable()); + try testing.expect(!@as(Code, @fromBackingInt(@intCast(1016))).isSendable()); + try testing.expect(!@as(Code, @fromBackingInt(@intCast(2999))).isSendable()); // Libraries and frameworks. - try testing.expect(@as(Code, @enumFromInt(3000)).isSendable()); - try testing.expect(@as(Code, @enumFromInt(3999)).isSendable()); + try testing.expect(@as(Code, @fromBackingInt(@intCast(3000))).isSendable()); + try testing.expect(@as(Code, @fromBackingInt(@intCast(3999))).isSendable()); // Private use. - try testing.expect(@as(Code, @enumFromInt(4000)).isSendable()); - try testing.expect(@as(Code, @enumFromInt(4999)).isSendable()); + try testing.expect(@as(Code, @fromBackingInt(@intCast(4000))).isSendable()); + try testing.expect(@as(Code, @fromBackingInt(@intCast(4999))).isSendable()); // And above it. - try testing.expect(!@as(Code, @enumFromInt(5000)).isSendable()); - try testing.expect(!@as(Code, @enumFromInt(65535)).isSendable()); + try testing.expect(!@as(Code, @fromBackingInt(@intCast(5000))).isSendable()); + try testing.expect(!@as(Code, @fromBackingInt(@intCast(65535))).isSendable()); } diff --git a/src/frame.zig b/src/frame.zig index 8eb0794..26316e7 100644 --- a/src/frame.zig +++ b/src/frame.zig @@ -49,7 +49,7 @@ pub const Opcode = enum(u4) { /// §5.5: "Control frames are identified by opcodes where the most /// significant bit of the opcode is 1." pub fn isControl(o: Opcode) bool { - return @intFromEnum(o) & 0x8 != 0; + return @backingInt(o) & 0x8 != 0; } /// Whether this is one of the six the protocol actually defines. @@ -102,7 +102,7 @@ pub const Header = struct { (@as(u8, @intFromBool(h.rsv1)) << 6) | (@as(u8, @intFromBool(h.rsv2)) << 5) | (@as(u8, @intFromBool(h.rsv3)) << 4) | - @intFromEnum(h.opcode); + @backingInt(h.opcode); const mask_bit: u8 = if (h.mask != null) 0x80 else 0; var at: usize = 2; @@ -190,7 +190,7 @@ pub fn parse(bytes: []const u8) ParseError!?Parsed { .rsv1 = bytes[0] & 0x40 != 0, .rsv2 = bytes[0] & 0x20 != 0, .rsv3 = bytes[0] & 0x10 != 0, - .opcode = @enumFromInt(@as(u4, @truncate(bytes[0]))), + .opcode = @fromBackingInt(@intCast(@as(u4, @truncate(bytes[0])))), .mask = mask, .payload_len = payload_len, }, @@ -370,10 +370,10 @@ test "every opcode knows whether it is a control frame" { try testing.expect(Opcode.ping.isControl()); try testing.expect(Opcode.pong.isControl()); // The reserved ranges, which an extension might one day define. - try testing.expect(!@as(Opcode, @enumFromInt(0x3)).isControl()); - try testing.expect(@as(Opcode, @enumFromInt(0xb)).isControl()); - try testing.expect(!@as(Opcode, @enumFromInt(0x3)).isDefined()); - try testing.expect(!@as(Opcode, @enumFromInt(0xf)).isDefined()); + try testing.expect(!@as(Opcode, @fromBackingInt(@intCast(0x3))).isControl()); + try testing.expect(@as(Opcode, @fromBackingInt(@intCast(0xb))).isControl()); + try testing.expect(!@as(Opcode, @fromBackingInt(@intCast(0x3))).isDefined()); + try testing.expect(!@as(Opcode, @fromBackingInt(@intCast(0xf))).isDefined()); } test "a header survives being written and read back" { diff --git a/tests/e2e.zig b/tests/e2e.zig index 14aa527..9e7a007 100644 --- a/tests/e2e.zig +++ b/tests/e2e.zig @@ -287,7 +287,7 @@ test "an empty message is a message" { } test "a message long enough to need the two-octet length" { - const payload = "x" ** 200; + const payload: []const u8 = &@as([200]u8, @splat('x')); var client: std.ArrayList(u8) = .empty; defer client.deinit(gpa); try client.appendSlice(gpa, handshake_request); @@ -396,7 +396,7 @@ fn expectCloseCode(want: ws.Code, got: []const u8) !void { std.debug.print("close frame length {d} but {d} octets follow\n", .{ got[1], got.len - 2 }); return error.WrongLength; } - const code: ws.Code = @enumFromInt(std.mem.readInt(u16, got[2..4], .big)); + const code: ws.Code = @fromBackingInt(@intCast(std.mem.readInt(u16, got[2..4], .big))); testing.expectEqual(want, code) catch |err| { std.debug.print("reason: \"{s}\"\n", .{got[4..]}); return err; @@ -447,7 +447,7 @@ test "a fragmented control frame is a protocol error" { } test "a control frame longer than 125 octets is a protocol error" { - try expectClose(.protocol_error, 0x9, "x" ** 126); + try expectClose(.protocol_error, 0x9, &@as([126]u8, @splat('x'))); } test "a continuation with nothing to continue is a protocol error" { diff --git a/tests/fuzz.zig b/tests/fuzz.zig index f8beaad..55b5a2a 100644 --- a/tests/fuzz.zig +++ b/tests/fuzz.zig @@ -159,7 +159,7 @@ fn fuzzMask(_: void, smith: *Smith) !void { /// The three codes an application may be told and a peer may not send are /// never sendable, and the ranges of §7.4.2 are what they say they are. fn closeCodeProperty(value: u16) !void { - const code: ws.Code = @enumFromInt(value); + const code: ws.Code = @fromBackingInt(@intCast(value)); const sendable = code.isSendable(); if (value < 1000 and sendable) return error.BelowTheRangeIsSendable; @@ -280,7 +280,7 @@ fn checkServerFrames(bytes: []const u8) !void { if (header.payload_len == 1) return error.ServerSentAOneOctetClose; if (header.payload_len >= 2) { if (payload.len < 2) return error.ServerWroteATruncatedFrame; - const code: ws.Code = @enumFromInt(std.mem.readInt(u16, payload[0..2], .big)); + const code: ws.Code = @fromBackingInt(@intCast(std.mem.readInt(u16, payload[0..2], .big))); if (!code.isSendable()) return error.ServerSentAReservedCloseCode; } } diff --git a/tools/fuzz.zig b/tools/fuzz.zig index 9d5b166..789d34d 100644 --- a/tools/fuzz.zig +++ b/tools/fuzz.zig @@ -3,19 +3,11 @@ //! Run the fuzz targets in `tests/fuzz.zig` against input this makes up. //! -//! Zig has a fuzzer of its own and those targets are written for it, so the -//! obvious thing to run is `zig build fuzz --fuzz`. With the devshell's -//! patched Zig that now *compiles* — `flake.nix` says what the patch is — -//! and then ends with -//! -//! ``` -//! error: step 'run test': corrupted coverage file: pcs_len was zero -//! ``` -//! -//! because nothing in 0.16.0 populates the table of program counters, however -//! the modules are built. A fuzzer with no coverage is a random number -//! generator, so this is one written down honestly: it makes an input, hands -//! it to a target, and says so when one comes back with an error. +//! `zig build fuzz --fuzz` is Zig's own fuzzer, steered by coverage. This is +//! the other way in: it makes an input, hands it to a target, and says so when +//! one comes back with an error -- for a fixed time or a fixed seed, from an +//! ordinary executable, which is what a CI job wants and what reproduces a run +//! exactly. //! //! ```console //! $ zig build fuzz-run # a minute of each -- knotmirror 0.0.1