From a768e7110c321918df9c8be491009eaf7bce01c5 Mon Sep 17 00:00:00 2001 From: jperon Date: Tue, 10 Nov 2026 17:53:52 +0100 Subject: [PATCH 01/43] fix(ipparse): make bit53 band/bor/bxor variadic LuaJIT's bit.band/bor/bxor accept any number of arguments, but the Lua 5.3+ compatibility layer only handled two. This broke e.g. TCP flag construction (bor with seven arguments), failing 6 test_tcp cases under Lua 5.4/5.5. Fold left to right, with a direct path for the two-argument case. Co-Authored-By: Claude Opus 5.5 --- ipparse/lib/bit53.moon | 33 +++++++++++++++++++++++--- ipparse/tests/lib/test_bit_compat.moon | 31 ++++++++++++++++++++++++ ipparse/tests/run_all.moon | 1 + 3 files changed, 62 insertions(+), 3 deletions(-) create mode 100644 ipparse/tests/lib/test_bit_compat.moon diff --git a/ipparse/lib/bit53.moon b/ipparse/lib/bit53.moon index 62b32a6..1a79289 100644 --- a/ipparse/lib/bit53.moon +++ b/ipparse/lib/bit53.moon @@ -5,16 +5,43 @@ --- Bitwise operations compatibility layer (Lua 5.3 style). -- This module provides Lua 5.3-style bitwise operations using LuaJIT's native operators. +-- Like LuaJIT's `bit` library, `band`, `bor` and `bxor` are variadic: `bor(a)` +-- returns `a`, and operations fold left to right over their arguments. -- MoonScript parses `~` ambiguously, so XOR is expressed using boolean identities. -- -- @module lib.bit53 -- @return table Bitwise operations table bit = - band: (a, b) -> a & b - bor: (a, b) -> a | b + band: (...) -> + n = select "#", ... + return ... if n <= 1 + a, b = ... + return a & b if n == 2 + r = a + for i = 2, n + r &= (select i, ...) + r + bor: (...) -> + n = select "#", ... + return ... if n <= 1 + a, b = ... + return a | b if n == 2 + r = a + for i = 2, n + r |= (select i, ...) + r -- MoonScript parses `a ~ b` ambiguously; express XOR with identities. - bxor: (a, b) -> (a | b) & (~(a & b)) + bxor: (...) -> + n = select "#", ... + return ... if n <= 1 + a, b = ... + return (a | b) & (~(a & b)) if n == 2 + r = a + for i = 2, n + v = select i, ... + r = (r | v) & (~(r & v)) + r bnot: (a) -> ~a lshift: (a, n) -> a << n rshift: (a, n) -> a >> n diff --git a/ipparse/tests/lib/test_bit_compat.moon b/ipparse/tests/lib/test_bit_compat.moon new file mode 100644 index 0000000..a5b531f --- /dev/null +++ b/ipparse/tests/lib/test_bit_compat.moon @@ -0,0 +1,31 @@ +-- +-- SPDX-FileCopyrightText: (c) 2024-2026 jperon +-- SPDX-License-Identifier: MIT OR GPL-2.0-only +-- + +-- tests/lib/test_bit_compat.moon +-- Tests for the bitwise compatibility layer, notably variadic band/bor/bxor + +util = require "ipparse.lib.util" +{:test} = util +{ :band, :bor, :bxor } = require "ipparse.lib.bit_compat" + +test "bit_compat: bor with 1 argument", -> + assert bor(5) == 5, "bor(5) != 5" + +test "bit_compat: bor with 2 arguments", -> + assert bor(1, 2) == 3, "bor(1,2) != 3" + +test "bit_compat: bor with 7 arguments", -> + assert bor(1, 2, 4, 8, 16, 32, 64) == 127, "bor(1,2,4,8,16,32,64) != 127" + +test "bit_compat: bor with 7 TCP flag arguments", -> + assert bor(0, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01) == 63, "TCP flags != 63" + +test "bit_compat: band with 3 arguments", -> + assert band(0xFF, 0x3C, 0x1E) == 28, "band(0xFF,0x3C,0x1E) != 28" + +test "bit_compat: bxor with 3 arguments", -> + assert bxor(0xF0, 0x3C, 0xFF) == 51, "bxor(0xF0,0x3C,0xFF) != 51" + +util.summary "bit_compat" diff --git a/ipparse/tests/run_all.moon b/ipparse/tests/run_all.moon index 3b98cad..7d4f77e 100644 --- a/ipparse/tests/run_all.moon +++ b/ipparse/tests/run_all.moon @@ -28,6 +28,7 @@ mods = { "ipparse.tests.l7.tls.test_handshake" "ipparse.tests.test_malformed" "ipparse.tests.lib.test_hkdf" + "ipparse.tests.lib.test_bit_compat" "ipparse.tests.lib.crypto.test_ffi_wolfssl" "ipparse.tests.lib.crypto.test_ffi_mbedtls" "ipparse.tests.l4.quic.test_varint" From 0b18d66f6846ac53f7e85309055e6be23fb8d5ab Mon Sep 17 00:00:00 2001 From: jperon Date: Tue, 10 Nov 2026 17:57:18 +0100 Subject: [PATCH 02/43] fix(sha): correct SHA-256 constants, variadic XOR and HMAC double-feed - sha256_K had three wrong constants (K[17], K[18], K[54]). - The XOR helper was binary-only but Sum0/Sum1 call it with three terms, so the third term was silently dropped. - hmac() fed the message twice before finalizing the inner hash; feed it once from the returned chunker instead. Also fixes the streaming path, where the message was finalized prematurely. Co-Authored-By: Claude Opus 5.5 --- ipparse/lib/sha.lua | 8 ++-- ipparse/tests/lib/test_sha.moon | 66 +++++++++++++++++++++++++++++++++ ipparse/tests/run_all.moon | 1 + 3 files changed, 71 insertions(+), 4 deletions(-) create mode 100644 ipparse/tests/lib/test_sha.moon diff --git a/ipparse/lib/sha.lua b/ipparse/lib/sha.lua index 96494e6..007ccae 100644 --- a/ipparse/lib/sha.lua +++ b/ipparse/lib/sha.lua @@ -31,7 +31,7 @@ local AND, OR, XOR, SHL, SHR, ROL, ROR, NOT AND = bit.band or function(x, y) return x & y end OR = bit.bor or function(x, y) return x | y end -XOR = bit.bxor or function(x, y) return x ~ y end +XOR = bit.bxor or function(x, ...) local r = x; for i = 1, select('#', ...) do r = r ~ select(i, ...) end; return r end NOT = bit.bnot or function(x) return ~x end -- Lua 5.3+ shifts handle negative numbers correctly for signed int32, @@ -79,11 +79,11 @@ local sha256_H = { local sha256_K = { 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5, 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174, - 0xe49b69c1, 0xef983bda, 0xfc930062, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da, + 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da, 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967, 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85, 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070, - 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9ca4f7, 0x682e6ff3, + 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3, 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2 } @@ -236,7 +236,7 @@ local function hmac(hash_func, key, message) -- Inner hash: H(ipad || message) local inner_hash_chunker = hash_func() -- Get chunker for inner hash inner_hash_chunker(ipad) -- Feed ipad - local inner_hash_partial = inner_hash_chunker(message) -- Feed message (or return chunker) + local inner_hash_partial = inner_hash_chunker -- Chunker after ipad; message is fed once in partial() local function partial(message_part) if message_part then diff --git a/ipparse/tests/lib/test_sha.moon b/ipparse/tests/lib/test_sha.moon new file mode 100644 index 0000000..3760cc4 --- /dev/null +++ b/ipparse/tests/lib/test_sha.moon @@ -0,0 +1,66 @@ +-- +-- SPDX-FileCopyrightText: (c) 2024-2026 jperon +-- SPDX-License-Identifier: MIT OR GPL-2.0-only +-- + +-- tests/lib/test_sha.moon +-- Tests for the hand-written ipparse/lib/sha.lua (SHA-256 + HMAC-SHA256). +-- sha.lua requires Lua 5.3+ native bitwise operators; skip on LuaJIT/5.1. + +util = require "ipparse.lib.util" +{:test, :summary} = util + +ok, sha = pcall require, "ipparse.lib.sha" +unless ok and sha and sha.sha256 and sha.hmac + test "sha: unavailable on this runtime (skipped)", -> true + summary "sha" + return + +{:sha256, :hmac, :bin_to_hex} = sha + +hex = (s) -> bin_to_hex s + +-- FIPS 180-2 / NIST short message vectors. +test "sha: FIPS 180-2 'abc'", -> + assert hex(sha256 "abc") == + "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad" + +test "sha: FIPS 180-2 empty message", -> + assert hex(sha256 "") == + "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" + +test "sha: FIPS 180-2 64 x 'a'", -> + assert hex(sha256 (string.rep "a", 64)) == + "ffe054fe7ae0cb6dc65c3af9b61d5209f439851db43d0ba5997337df154668eb" + +test "sha: NIST 100 x 'a'", -> + assert hex(sha256 (string.rep "a", 100)) == + "2816597888e4a0d3a36b82b83316ab32680eb8f00f8cd3b904d681246d285a0e" + +-- 1,000,000 x 'a' is the classic FIPS vector; fall back to 1,000 x 'a' +-- if it happens to be slow on this runtime (> 2 s). +test "sha: NIST long message", -> + start = os.clock! + digest = hex sha256 string.rep "a", 1000000 + elapsed = os.clock! - start + if elapsed < 2 + assert digest == + "cdc76e5c9914fb9281a1c7e284d73e67f1809a48a497200e046d39ccc7112cd0" + else + assert hex(sha256 string.rep "a", 1000) == + "41edece42d63e8d9bf515a9ba6932e1c20cbc9f5a5d134645adb5db1b9737ea3" + +-- HMAC-SHA256 (RFC 4231). +test "hmac: RFC 4231 test case 1", -> + key = string.rep "\x0b", 20 + digest = hmac sha256, key, "Hi There" + assert #digest == 32, "hmac must return a 32-byte binary digest" + assert hex(digest) == + "b0344c61d8db38535ca8afceaf0bf12b881dc200c9833da726e9376c2e32cff7" + +test "hmac: RFC 4231 test case 2", -> + digest = hmac sha256, "Jefe", "what do ya want for nothing?" + assert hex(digest) == + "5bdcc146bf60754e6a042426089575c75a003f089d2739839dec58b964ec3843" + +summary "sha" diff --git a/ipparse/tests/run_all.moon b/ipparse/tests/run_all.moon index 7d4f77e..c1dda2b 100644 --- a/ipparse/tests/run_all.moon +++ b/ipparse/tests/run_all.moon @@ -27,6 +27,7 @@ mods = { "ipparse.tests.l7.test_dns" "ipparse.tests.l7.tls.test_handshake" "ipparse.tests.test_malformed" + "ipparse.tests.lib.test_sha" "ipparse.tests.lib.test_hkdf" "ipparse.tests.lib.test_bit_compat" "ipparse.tests.lib.crypto.test_ffi_wolfssl" From 0a6c8e6f17dcd8827fea30708ef327e4fc5038af Mon Sep 17 00:00:00 2001 From: jperon Date: Tue, 10 Nov 2026 18:02:12 +0100 Subject: [PATCH 03/43] test(lunatik): restore package state after mocking crypto Save and restore package.preload/package.loaded entries for crypto, crypto.ecb and the lunatik backend so the crypto stubs cannot leak into modules loaded later in the same test run. Co-Authored-By: Claude Opus 5.5 --- ipparse/tests/lib/crypto/test_lunatik.moon | 27 ++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/ipparse/tests/lib/crypto/test_lunatik.moon b/ipparse/tests/lib/crypto/test_lunatik.moon index 8c61fc8..84ce529 100644 --- a/ipparse/tests/lib/crypto/test_lunatik.moon +++ b/ipparse/tests/lib/crypto/test_lunatik.moon @@ -11,6 +11,21 @@ util = require "ipparse.lib.util" {:test, :summary} = util {:hex_to_bin} = require "ipparse.lib.hkdf" +-- Snapshot package state before installing the stubs below, so nothing leaks +-- into the modules loaded after this one in the same test run. +preload_before = { + crypto: package.preload.crypto + crypto_ecb: package.preload["crypto.ecb"] +} +loaded_before = { + crypto: package.loaded["crypto"] + crypto_ecb: package.loaded["crypto.ecb"] + backend: package.loaded["ipparse.lib.crypto.backend.lunatik"] +} +loaded_keys_before = {} +for k in pairs package.loaded + loaded_keys_before[k] = true + calls = { aead_new: 0 aead_close: 0 @@ -119,4 +134,16 @@ test "lunatik: AES-128-ECB encrypt uses crypto.ecb", -> assert #out == 16, "output must be 16 bytes" assert out\byte(1) == string.byte("Z"), "first byte should be transformed by stub" +-- Restore package state altered by the stubs above. +leaked = {} +for k in pairs package.loaded + leaked[#leaked + 1] = k unless loaded_keys_before[k] +for k in *leaked + package.loaded[k] = nil +package.preload.crypto = preload_before.crypto +package.preload["crypto.ecb"] = preload_before.crypto_ecb +package.loaded["crypto"] = loaded_before.crypto +package.loaded["crypto.ecb"] = loaded_before.crypto_ecb +package.loaded["ipparse.lib.crypto.backend.lunatik"] = loaded_before.backend + summary "lib.crypto.lunatik" From 53d8581dd8c07ec1834884bbd6122f9a46f24245 Mon Sep 17 00:00:00 2001 From: jperon Date: Tue, 10 Nov 2026 18:02:24 +0100 Subject: [PATCH 04/43] test(lunatik): align crypto stubs with Lunatik API The ECB stub invented a crypto.ecb module with encryptblock(); Lunatik only exposes require("crypto").skcipher (lib/luacrypto_core.c registers "skcipher") whose objects provide encrypt(iv, data) (lib/luacrypto_skcipher.c luacrypto_skcipher_encrypt). Stub skcipher and use an empty IV, matching ECB ivsize == 0 in luacrypto_checkiv. Non-auth errors: luacrypto_aead_finish() throws every failing crypt return code via lunatik_throw (lib/luacrypto_aead.c:144), and a too-short input throws -EBADMSG (line 186). The backend wraps decrypt in pcall and maps EBADMSG to a nil,error authentication failure while other failures become nil,"aead(gcm(aes)) decrypt failed: ...", consistent with the documented nil,error backend contract. Expect nil,error in the test instead of a propagated exception. Co-Authored-By: Claude Opus 5.5 --- ipparse/tests/lib/crypto/test_lunatik.moon | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/ipparse/tests/lib/crypto/test_lunatik.moon b/ipparse/tests/lib/crypto/test_lunatik.moon index 84ce529..dc26ecc 100644 --- a/ipparse/tests/lib/crypto/test_lunatik.moon +++ b/ipparse/tests/lib/crypto/test_lunatik.moon @@ -57,8 +57,9 @@ mk_ecb = -> key = nil { setkey: (k) => key = k - encryptblock: (block) => + encrypt: (iv, block) => assert key == string.rep("\x22", 16), "unexpected ECB key" + assert iv == "", "ECB expects an empty IV" assert #block == 16, "ECB block must be 16 bytes" "Z" .. block\sub(2) __close: => @@ -71,11 +72,8 @@ package.preload.crypto = -> assert name == "gcm(aes)", "unexpected AEAD algorithm: #{name}" calls.aead_new += 1 mk_aead! - } - -package.preload["crypto.ecb"] = -> - { - new: -> + skcipher: (name) -> + assert name == "ecb(aes)", "unexpected skcipher algorithm: #{name}" calls.ecb_new += 1 mk_ecb! } @@ -120,14 +118,14 @@ test "lunatik: AES-128-GCM ciphertext too short returns nil", -> assert pt == nil, "pt should be nil" assert err ~= nil, "err should be provided" -test "lunatik: AES-128-GCM decrypt raises on non-auth error", -> +test "lunatik: AES-128-GCM decrypt returns nil,error on non-auth error", -> key = string.rep "\x11", 16 nonce = string.rep "\x00", 12 - ok, err = pcall b.aes_128_gcm_decrypt, key, nonce, ("BROKEN" .. string.rep("T", 16)), "" - assert not ok, "decrypt should raise on non-auth failure" + pt, err = b.aes_128_gcm_decrypt key, nonce, ("BROKEN" .. string.rep("T", 16)), "" + assert pt == nil, "pt should be nil on non-auth failure" assert tostring(err)\find("aead(gcm(aes)) decrypt failed", 1, true), "unexpected error: #{tostring err}" -test "lunatik: AES-128-ECB encrypt uses crypto.ecb", -> +test "lunatik: AES-128-ECB encrypt uses crypto.skcipher", -> key = string.rep "\x22", 16 block = string.rep "\x33", 16 out = b.aes_128_ecb_block key, block From 8d98d009a4cfde750f064dc9c2a7636ded36ae73 Mon Sep 17 00:00:00 2001 From: jperon Date: Tue, 10 Nov 2026 17:46:59 +0100 Subject: [PATCH 05/43] test: add byte-equivalence tests for pack fallback Co-Authored-By: Claude Opus 5.5 --- ipparse/tests/lib/test_pack_compat.moon | 74 +++++++++++++++++++++++++ ipparse/tests/run_all.moon | 1 + 2 files changed, 75 insertions(+) create mode 100644 ipparse/tests/lib/test_pack_compat.moon diff --git a/ipparse/tests/lib/test_pack_compat.moon b/ipparse/tests/lib/test_pack_compat.moon new file mode 100644 index 0000000..7040da2 --- /dev/null +++ b/ipparse/tests/lib/test_pack_compat.moon @@ -0,0 +1,74 @@ +-- +-- SPDX-FileCopyrightText: (c) 2024-2026 jperon +-- SPDX-License-Identifier: MIT OR GPL-2.0-only +-- + +-- tests/lib/test_pack_compat.moon +-- Byte-for-byte equivalence tests for the LuaJIT pack fallback. + +util = require "ipparse.lib.util" +{:test} = util +pack_compat = require "ipparse.lib.pack_compat_lib" +pack = pack_compat.pack or string.pack + +from_hex = (h) -> h\gsub "..", (cc) -> string.char tonumber cc, 16 + +-- Big-endian header-like mix of 16-bit and 8-bit fields. +test "pack_compat: >H B B H H H H", -> + got = pack ">H B B H H H H", 1, 2, 3, 4, 5, 6, 7 + expected = from_hex "000102030004000500060007" + assert got == expected, "got: #{got}\nexp: #{expected}" + +-- Big-endian mix with a 4-byte integer and a 2-byte-prefixed string. +test "pack_compat: >H H I4 s2", -> + got = pack ">H H I4 s2", 1, 1, 300, "abcd" + expected = from_hex "000100010000012c000461626364" + assert got == expected, "got: #{got}\nexp: #{expected}" + +-- Little-endian 4-byte integer. +test "pack_compat: + got = pack "i2 negative", -> + got = pack ">i2", -2 + expected = from_hex "fffe" + assert got == expected, "got: #{got}\nexp: #{expected}" + +-- Fixed-width string shorter than the field is NUL-padded. +test "pack_compat: >c4 short", -> + got = pack ">c4", "ab" + expected = from_hex "61620000" + assert got == expected, "got: #{got}\nexp: #{expected}" + +-- Fixed-width string longer than the field is truncated. +test "pack_compat: >c4 long", -> + got = pack ">c4", "abcdef" + expected = from_hex "61626364" + assert got == expected, "got: #{got}\nexp: #{expected}" + +-- NUL-terminated string gets a trailing \0. +test "pack_compat: z", -> + got = pack "z", "hello" + expected = from_hex "68656c6c6f00" + assert got == expected, "got: #{got}\nexp: #{expected}" + +-- Padding emits one NUL byte. +test "pack_compat: x", -> + got = pack "x" + expected = from_hex "00" + assert got == expected, "got: #{got}\nexp: #{expected}" + +-- 3-byte big-endian unsigned integer. +test "pack_compat: >I3", -> + got = pack ">I3", 0x010203 + expected = from_hex "010203" + assert got == expected, "got: #{got}\nexp: #{expected}" + +-- uint8 keeps the low byte of out-of-range values (300 % 256 == 44). +test "pack_compat: B 300 wraps", -> + got = pack "B", 300 + expected = from_hex "2c" + assert got == expected, "got: #{got}\nexp: #{expected}" diff --git a/ipparse/tests/run_all.moon b/ipparse/tests/run_all.moon index c1dda2b..00c524a 100644 --- a/ipparse/tests/run_all.moon +++ b/ipparse/tests/run_all.moon @@ -30,6 +30,7 @@ mods = { "ipparse.tests.lib.test_sha" "ipparse.tests.lib.test_hkdf" "ipparse.tests.lib.test_bit_compat" + "ipparse.tests.lib.test_pack_compat" "ipparse.tests.lib.crypto.test_ffi_wolfssl" "ipparse.tests.lib.crypto.test_ffi_mbedtls" "ipparse.tests.l4.quic.test_varint" From c2aa0bb0553a115484182b0b7c112dccca8b16bd Mon Sep 17 00:00:00 2001 From: jperon Date: Tue, 10 Nov 2026 17:48:10 +0100 Subject: [PATCH 06/43] perf(ipparse): compile pack formats into specialized emitters Replace the per-argument switch dispatch and ffi buffers with a per-format plan of small emitter closures built once and cached. Integers up to 4 bytes are emitted with string.char arithmetic, so LuaJIT no longer abandons traces; byte output is unchanged. Co-Authored-By: Claude Opus 5.5 --- ipparse/lib/pack_compat_lib.moon | 212 +++++++++++++++++++------------ 1 file changed, 130 insertions(+), 82 deletions(-) diff --git a/ipparse/lib/pack_compat_lib.moon b/ipparse/lib/pack_compat_lib.moon index 55f87bd..9ac5b1e 100644 --- a/ipparse/lib/pack_compat_lib.moon +++ b/ipparse/lib/pack_compat_lib.moon @@ -228,99 +228,147 @@ bytes_to_float = (s, offset, le) -> -- ─── string.pack ───────────────────────────────────────────────────────────── -pack = (fmt, ...) -> - args = {...} - argi = 1 - parts = {} - ops = _fmt_cache[fmt] or parse_format_to_ops(fmt) +-- Construit un encodeur spécialisé pour un entier non signé de `sz` octets. +-- Les tailles courantes (1, 2, 3, 4) sont déroulées via string.char, sans +-- allocation de table ni appel FFI ; les tailles 5-6 utilisent une petite +-- boucle et au-delà de 6 octets on retombe sur write_uint (précision 64 bits). +make_uint = (sz, le) -> + if sz == 1 + (v) -> string.char(v % 256) + elseif sz == 2 + if le + (v) -> + u = v % 65536 + string.char(u % 256, math.floor(u / 256) % 256) + else + (v) -> + u = v % 65536 + string.char(math.floor(u / 256) % 256, u % 256) + elseif sz == 3 + mod = 16777216 + if le + (v) -> + u = v % mod + string.char(u % 256, math.floor(u / 256) % 256, math.floor(u / 65536) % 256) + else + (v) -> + u = v % mod + string.char(math.floor(u / 65536) % 256, math.floor(u / 256) % 256, u % 256) + elseif sz == 4 + mod = 4294967296 + if le + (v) -> + u = v % mod + string.char(u % 256, math.floor(u / 256) % 256, + math.floor(u / 65536) % 256, math.floor(u / 16777216) % 256) + else + (v) -> + u = v % mod + string.char(math.floor(u / 16777216) % 256, math.floor(u / 65536) % 256, + math.floor(u / 256) % 256, u % 256) + elseif sz <= 6 + mod = 2 ^ (8 * sz) + if le + (v) -> + u = v % mod + bytes = {} + for k = 1, sz + bytes[k] = u % 256 + u = math.floor(u / 256) + string.char tunpack bytes + else + (v) -> + u = v % mod + bytes = {} + for k = sz, 1, -1 + bytes[k] = u % 256 + u = math.floor(u / 256) + string.char tunpack bytes + else + (v) -> + buf = ffi.new "uint8_t[?]", sz + write_uint buf, 0, v, sz, le + ffi.string buf, sz - for j = 1, #ops - {opt, count, le} = ops[j] - switch opt +make_float = (le) -> + (v) -> string.char(tunpack(float_to_bytes(v, le))) - when "b" -- int8 - v = args[argi] - argi += 1 - v = v % 256 - parts[#parts+1] = string.char v +make_double = (le) -> + (v) -> string.char(tunpack(double_to_bytes(v, le))) - when "B" -- uint8 - v = args[argi] - argi += 1 - parts[#parts+1] = string.char(v % 256) +make_c = (sz) -> + (v) -> + if #v < sz + v .. string.rep "\0", sz - #v + else + v\sub 1, sz - when "h" -- int16 - v = args[argi] - argi += 1 - uv = v % 65536 - if le - parts[#parts+1] = string.char(uv % 256, math.floor(uv/256) % 256) - else - parts[#parts+1] = string.char(math.floor(uv/256) % 256, uv % 256) +make_s = (sz, le) -> + len_emit = make_uint sz, le + (v) -> len_emit(#v) .. v - when "H" -- uint16 - v = args[argi] - argi += 1 - uv = v % 65536 - if le - parts[#parts+1] = string.char(uv % 256, math.floor(uv/256) % 256) - else - parts[#parts+1] = string.char(math.floor(uv/256) % 256, uv % 256) - - when "i", "I" -- int/uint N octets (défaut 4) - sz = count or 4 - v = args[argi] - argi += 1 - buf = ffi.new "uint8_t[?]", sz - write_uint buf, 0, v, sz, le - parts[#parts+1] = ffi.string buf, sz - - when "l", "L", "j", "J", "T" -- 64 bits - v = args[argi] - argi += 1 - buf = ffi.new "uint8_t[8]" - write_uint buf, 0, v, 8, le - parts[#parts+1] = ffi.string buf, 8 +-- Choisit l'encodeur d'une option au moment de la compilation du format. +pack_op = (opt, count, le) -> + switch opt - when "f" -- float 32 - v = args[argi] - argi += 1 - bytes = float_to_bytes v, le - parts[#parts+1] = string.char tunpack bytes + when "b", "B" -- int8 / uint8 + { consume: true, emit: make_uint(1, le) } - when "d", "n" -- double 64 - v = args[argi] - argi += 1 - bytes = double_to_bytes v, le - parts[#parts+1] = string.char tunpack bytes + when "h", "H" -- int16 / uint16 + { consume: true, emit: make_uint(2, le) } - when "c" -- chaîne de longueur fixe - sz = count or 1 - v = args[argi] - argi += 1 - if #v < sz - parts[#parts+1] = v .. string.rep "\0", sz - #v - else - parts[#parts+1] = v\sub 1, sz - - when "s" -- chaîne préfixée par une longueur (size_t = 8 octets ici) - sz = count or 8 - v = args[argi] - argi += 1 - buf = ffi.new "uint8_t[?]", sz - write_uint buf, 0, #v, sz, le - parts[#parts+1] = ffi.string(buf, sz) .. v + when "i", "I" -- int/uint N octets (défaut 4) + { consume: true, emit: make_uint((count or 4), le) } - when "z" -- chaîne null-terminated - v = args[argi] - argi += 1 - parts[#parts+1] = v .. "\0" + when "l", "L", "j", "J", "T" -- 64 bits + { consume: true, emit: make_uint(8, le) } - when "x" -- padding (un octet nul) - parts[#parts+1] = "\0" + when "f" -- float 32 + { consume: true, emit: make_float le } - when "X" -- alignement (pas d'octet émis dans ce contexte simplifié) - nil -- no-op + when "d", "n" -- double 64 + { consume: true, emit: make_double le } + + when "c" -- chaîne de longueur fixe + { consume: true, emit: make_c(count or 1) } + + when "s" -- chaîne préfixée longueur (size_t = 8 octets par défaut) + { consume: true, emit: make_s((count or 8), le) } + + when "z" -- chaîne null-terminated + { consume: true, emit: (v) -> v .. "\0" } + + when "x" -- padding (un octet nul) + { consume: false, emit: -> "\0" } + + else -- "X" (alignement) et options inconnues : no-op + { consume: false, emit: -> "" } + +-- Cache des formats compilés en liste d'encodeurs : fmt → plan. +_pack_cache = {} + +compile_pack = (fmt) -> + plan = {} + iter = parse_format fmt + opt, count, le = iter! + while opt != nil + plan[#plan+1] = pack_op opt, count, le + opt, count, le = iter! + _pack_cache[fmt] = plan + plan + +pack = (fmt, ...) -> + plan = _pack_cache[fmt] or compile_pack(fmt) + parts = {} + argi = 1 + + for j = 1, #plan + op = plan[j] + if op.consume + parts[#parts+1] = op.emit(select(argi, ...)) + argi += 1 + else + parts[#parts+1] = op.emit! table.concat parts From 2e04346ff484ab17c5864d5716b10ec078eb4a43 Mon Sep 17 00:00:00 2001 From: jperon Date: Tue, 10 Nov 2026 17:50:03 +0100 Subject: [PATCH 07/43] test: report pack_compat summary Co-Authored-By: Claude Opus 5.5 --- ipparse/tests/lib/test_pack_compat.moon | 2 ++ 1 file changed, 2 insertions(+) diff --git a/ipparse/tests/lib/test_pack_compat.moon b/ipparse/tests/lib/test_pack_compat.moon index 7040da2..51b25a8 100644 --- a/ipparse/tests/lib/test_pack_compat.moon +++ b/ipparse/tests/lib/test_pack_compat.moon @@ -72,3 +72,5 @@ test "pack_compat: B 300 wraps", -> got = pack "B", 300 expected = from_hex "2c" assert got == expected, "got: #{got}\nexp: #{expected}" + +util.summary "pack_compat" From 7ea169d0ab0dd25d22ef2a6b423a6d7d5e9b6fc3 Mon Sep 17 00:00:00 2001 From: jperon Date: Tue, 10 Nov 2026 17:51:57 +0100 Subject: [PATCH 08/43] test: run fallback-only pack cases only on the fallback Co-Authored-By: Claude Opus 5.5 --- ipparse/tests/lib/test_pack_compat.moon | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/ipparse/tests/lib/test_pack_compat.moon b/ipparse/tests/lib/test_pack_compat.moon index 51b25a8..89358cf 100644 --- a/ipparse/tests/lib/test_pack_compat.moon +++ b/ipparse/tests/lib/test_pack_compat.moon @@ -10,6 +10,8 @@ util = require "ipparse.lib.util" {:test} = util pack_compat = require "ipparse.lib.pack_compat_lib" pack = pack_compat.pack or string.pack +-- Native string.pack rejects the two fallback-only behaviours tested below. +is_fallback = pack != string.pack from_hex = (h) -> h\gsub "..", (cc) -> string.char tonumber cc, 16 @@ -43,11 +45,12 @@ test "pack_compat: >c4 short", -> expected = from_hex "61620000" assert got == expected, "got: #{got}\nexp: #{expected}" --- Fixed-width string longer than the field is truncated. -test "pack_compat: >c4 long", -> - got = pack ">c4", "abcdef" - expected = from_hex "61626364" - assert got == expected, "got: #{got}\nexp: #{expected}" +if is_fallback + -- Fixed-width string longer than the field is truncated. + test "pack_compat: >c4 long", -> + got = pack ">c4", "abcdef" + expected = from_hex "61626364" + assert got == expected, "got: #{got}\nexp: #{expected}" -- NUL-terminated string gets a trailing \0. test "pack_compat: z", -> @@ -67,10 +70,11 @@ test "pack_compat: >I3", -> expected = from_hex "010203" assert got == expected, "got: #{got}\nexp: #{expected}" --- uint8 keeps the low byte of out-of-range values (300 % 256 == 44). -test "pack_compat: B 300 wraps", -> - got = pack "B", 300 - expected = from_hex "2c" - assert got == expected, "got: #{got}\nexp: #{expected}" +if is_fallback + -- uint8 keeps the low byte of out-of-range values (300 % 256 == 44). + test "pack_compat: B 300 wraps", -> + got = pack "B", 300 + expected = from_hex "2c" + assert got == expected, "got: #{got}\nexp: #{expected}" util.summary "pack_compat" From 89ea2fca0677625532c0ca1929076be2f180bb3e Mon Sep 17 00:00:00 2001 From: jperon Date: Tue, 10 Nov 2026 17:52:39 +0100 Subject: [PATCH 09/43] test(dns): byte-exact round-trip coverage Co-Authored-By: Claude Opus 5.5 --- ipparse/tests/l7/test_dns.moon | 65 ++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) diff --git a/ipparse/tests/l7/test_dns.moon b/ipparse/tests/l7/test_dns.moon index 0a6775c..94b642b 100644 --- a/ipparse/tests/l7/test_dns.moon +++ b/ipparse/tests/l7/test_dns.moon @@ -121,4 +121,69 @@ test "parse fails safely on truncated label", -> msg, _, err = dns.parse dns_truncated_label, 1, false assert msg == nil, "expected parse failure" assert err and err\match("truncated DNS label"), "expected truncated label error, got #{err}" + +-- Response with two A answers (compressed owner names) and an EDNS OPT record. +dns_response_2a_opt = hex2bin( + "abcd81800001000200000001" .. + "076578616d706c6503636f6d00" .. + "00010001" .. + "c00c000100010000012c00045db8d822" .. + "c00c000100010000012c00045db8d823" .. + "0000291000000000000000" +) + +-- Response with non-empty authority and additional sections. +dns_response_sections = hex2bin( + "123481800001000100010001" .. + "076578616d706c6503636f6d00" .. + "00010001" .. + "c00c000100010000003c000401020304" .. + "c00c000200010000003c0011" .. + "036e7331076578616d706c6503636f6d00" .. + "c00c000100010000003c000405060708" +) + +-- Question with several uncompressed labels. +dns_long_name_query = hex2bin( + "000101000001000000000000" .. + "03777777076578616d706c6509737562646f6d61696e0474657374056c6f63616c00" .. + "00010001" +) + +-- Same long question, answer owner name is a pointer to offset 12. +dns_long_name_answer = hex2bin( + "000181800001000100000000" .. + "03777777076578616d706c6509737562646f6d61696e0474657374056c6f63616c00" .. + "00010001" .. + "c00c000100010000003c000401020304" +) + +-- A simple query prefixed with its 2-byte TCP length (29 bytes). +dns_tcp_query = hex2bin("001d" .. "123401000001000000000000076578616d706c6503636f6d0000010001") + +test "byte-exact round-trip: two A answers + EDNS OPT", -> + assert tostring(dns.parse(dns_response_2a_opt, 1)) == dns_response_2a_opt + +test "byte-exact round-trip: simple query", -> + assert tostring(dns.parse(dns_query, 1)) == dns_query + +test "byte-exact round-trip: authority and additional sections", -> + assert tostring(dns.parse(dns_response_sections, 1)) == dns_response_sections + +test "byte-exact round-trip: TCP message with size prefix", -> + assert tostring(dns.parse(dns_tcp_query, 1, true)) == dns_tcp_query + +test "byte-exact round-trip: long uncompressed name", -> + assert tostring(dns.parse(dns_long_name_query, 1)) == dns_long_name_query + +test "long uncompressed name decodes all labels", -> + msg, _ = dns.parse dns_long_name_query, 1, false + assert msg.question.name == "www.example.subdomain.test.local", + "got '#{msg.question.name}'" + +test "compression pointer answer decodes full name", -> + msg, _ = dns.parse dns_long_name_answer, 1, false + assert msg.answers[1].name == "www.example.subdomain.test.local", + "got '#{msg.answers[1].name}'" + util.summary "l7/dns" From 7c299e8eae4f9f9a4cfacfd4d7561413e50bc132 Mon Sep 17 00:00:00 2001 From: jperon Date: Tue, 10 Nov 2026 17:54:29 +0100 Subject: [PATCH 10/43] perf(dns): single-concat pack and direct byte reads in labels MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Replace per-section comprehension closures in pack with plain for loops filling one parts table, concatenated once; the body length is accumulated so header.size stays identical. Read label length and pointer bytes with string.byte after need_bytes has already validated the offsets. Bench (200k msgs, median of 4 interleaved runs, µs/msg): LuaJIT parse 3.93 -> 2.94, pack 3.30 -> 2.96 Lua 5.4 parse 8.01 -> 7.04, pack 2.58 -> 1.87 Co-Authored-By: Claude Opus 5.5 --- ipparse/l7/dns.moon | 38 +++++++++++++++++++++++++++++--------- 1 file changed, 29 insertions(+), 9 deletions(-) diff --git a/ipparse/l7/dns.moon b/ipparse/l7/dns.moon index 47d82b6..9bf3d74 100644 --- a/ipparse/l7/dns.moon +++ b/ipparse/l7/dns.moon @@ -72,6 +72,7 @@ :bidirectional, :zero_indexed = require"ipparse.fun" pack: sp, unpack: su, :sub = require "ipparse.lib.pack_compat" :concat = table +:byte = string {:band, :bor, :bnot, :lshift, :rshift} = require"ipparse.lib.bit_compat" need_bytes = (data, off, len) -> @@ -162,13 +163,13 @@ local labels -- @treturn[opt] boolean `true` if the label is compressed, `nil` otherwise. label = (off, l7_off=1, visited=nil, depth=0) => return nil, off, nil, "invalid label offset #{off}" unless need_bytes @, off, 1 - size = su "B", @, off + size = byte @, off _off = off + 1 if size == 0 -- End of label return nil, off+1 if band(size, 0xC0) == 0xC0 -- DNS label compression pointer return nil, off, nil, "truncated DNS compression pointer" unless need_bytes @, off, 2 - pos = su "B", @, off + 1 + pos = byte @, off + 1 ptr = lshift(band(size, 0x3F), 8) + pos target_off = l7_off + ptr return nil, _off + 1, nil, "DNS compression pointer out of bounds (#{ptr})" unless target_off >= l7_off and target_off <= #@ @@ -328,13 +329,32 @@ pack = => @header.ancount = #@answers @header.nscount = #@authorities @header.arcount = #@additionals - questions = concat([pack_question q for q in *@questions]) - answers = concat([pack_rr r for r in *@answers]) - authorities = concat([pack_rr r for r in *@authorities]) - additionals = concat([pack_rr r for r in *@additionals]) - body = questions .. answers .. authorities .. additionals - @header.size = 12 + #body - pack_header(@header) .. body + parts = {} + n = 1 + body_len = 0 + for q in *@questions + s = pack_question q + n += 1 + parts[n] = s + body_len += #s + for r in *@answers + s = pack_rr r + n += 1 + parts[n] = s + body_len += #s + for r in *@authorities + s = pack_rr r + n += 1 + parts[n] = s + body_len += #s + for r in *@additionals + s = pack_rr r + n += 1 + parts[n] = s + body_len += #s + @header.size = 12 + body_len + parts[1] = pack_header @header + concat parts _mt = __tostring: pack From be01ad95c64fdaad44e045b35c724af48b8b9052 Mon Sep 17 00:00:00 2001 From: jperon Date: Tue, 10 Nov 2026 18:10:18 +0100 Subject: [PATCH 11/43] test(lunatik): run portable parse/pack suites inside the kernel Co-Authored-By: Claude Opus 5.5 --- ipparse/Makefile | 2 +- .../tests/test_lunatik_kernel_portable.moon | 55 +++++++++++++++++++ 2 files changed, 56 insertions(+), 1 deletion(-) create mode 100644 ipparse/tests/test_lunatik_kernel_portable.moon diff --git a/ipparse/Makefile b/ipparse/Makefile index 8163d15..0f17968 100644 --- a/ipparse/Makefile +++ b/ipparse/Makefile @@ -47,7 +47,7 @@ test-lunatik-run: @start=$$(dmesg | wc -l); \ echo "Running kernel Lunatik crypto tests..."; \ status=0; \ - for test in ipparse/tests/lib/crypto/test_lunatik_kernel_quic_session_e2e ipparse/tests/lib/crypto/test_lunatik_kernel ipparse/tests/lib/crypto/test_lunatik_kernel_quic_simple ipparse/tests/lib/crypto/test_lunatik_kernel_quic_comprehensive ipparse/tests/lib/crypto/test_lunatik_kernel_integration ipparse/tests/lib/crypto/test_lunatik_kernel_crypto_pipeline ipparse/tests/lib/crypto/test_lunatik_kernel_quic_google_capture; do \ + for test in ipparse/tests/lib/crypto/test_lunatik_kernel_quic_session_e2e ipparse/tests/lib/crypto/test_lunatik_kernel ipparse/tests/lib/crypto/test_lunatik_kernel_quic_simple ipparse/tests/lib/crypto/test_lunatik_kernel_quic_comprehensive ipparse/tests/lib/crypto/test_lunatik_kernel_integration ipparse/tests/lib/crypto/test_lunatik_kernel_crypto_pipeline ipparse/tests/lib/crypto/test_lunatik_kernel_quic_google_capture ipparse/tests/test_lunatik_kernel_portable; do \ echo "Running $$test..."; \ if lunatik run "$$test"; then \ : ; \ diff --git a/ipparse/tests/test_lunatik_kernel_portable.moon b/ipparse/tests/test_lunatik_kernel_portable.moon new file mode 100644 index 0000000..36f92eb --- /dev/null +++ b/ipparse/tests/test_lunatik_kernel_portable.moon @@ -0,0 +1,55 @@ +-- +-- SPDX-FileCopyrightText: (c) 2024-2026 jperon +-- SPDX-License-Identifier: MIT OR GPL-2.0-only +-- + +-- tests/test_lunatik_kernel_portable.moon +-- Runs the parsing/packing test modules that are portable to the Lunatik +-- kernel runtime: no FFI, no file/pcap access, no simulated package.preload. +-- FFI-dependent modules are still listed where their FFI use is optional and +-- guarded by pcall; they then only exercise their pure-Lua assertions. + +util = require "ipparse.lib.util" + +mods = { + "ipparse.tests.test_fun" + "ipparse.tests.test_init" + "ipparse.tests.l2.test_ethernet" + "ipparse.tests.l3.test_checksum" + "ipparse.tests.l3.test_ip4" + "ipparse.tests.l3.test_ip6" + "ipparse.tests.l3.test_ip" + "ipparse.tests.l4.test_tcp" + "ipparse.tests.l4.test_udp" + "ipparse.tests.l4.test_tcp_stream" + "ipparse.tests.l7.test_dns" + "ipparse.tests.l7.tls.test_handshake" + "ipparse.tests.test_malformed" + "ipparse.tests.lib.test_hkdf" + "ipparse.tests.lib.test_bit_compat" + "ipparse.tests.lib.test_pack_compat" + "ipparse.tests.l4.quic.test_varint" + "ipparse.tests.l4.quic.test_header" + "ipparse.tests.l4.quic.test_frames" + "ipparse.tests.l4.quic.test_keys" + "ipparse.tests.l4.quic.test_protection" + "ipparse.tests.l4.quic.test_integration" + "ipparse.tests.l7.quic.test_session" +} + +total_pass, total_all = 0, 0 +load_errors = 0 +for mod in *mods + ok, err = pcall require, mod + if ok + total_pass += util._last_pass + total_all += util._last_total + else + load_errors += 1 + print "ERROR loading #{mod}: #{err}" + +print "\n==> Kernel portable total: #{total_pass}/#{total_all}" +failed = total_pass != total_all or load_errors > 0 +if failed + print "FAIL kernel portable suite: #{load_errors} load error(s), #{total_pass}/#{total_all} passed" + error "kernel portable suite failed" From 4d00af58f45de983430996e49933ce03a4ff80f6 Mon Sep 17 00:00:00 2001 From: jperon Date: Tue, 10 Nov 2026 18:11:37 +0100 Subject: [PATCH 12/43] test(sha): make the long-message vector deterministic and kernel-portable Co-Authored-By: Claude Opus 5.5 --- ipparse/tests/lib/test_sha.moon | 13 ++----------- ipparse/tests/test_lunatik_kernel_portable.moon | 1 + 2 files changed, 3 insertions(+), 11 deletions(-) diff --git a/ipparse/tests/lib/test_sha.moon b/ipparse/tests/lib/test_sha.moon index 3760cc4..c3a1249 100644 --- a/ipparse/tests/lib/test_sha.moon +++ b/ipparse/tests/lib/test_sha.moon @@ -37,18 +37,9 @@ test "sha: NIST 100 x 'a'", -> assert hex(sha256 (string.rep "a", 100)) == "2816597888e4a0d3a36b82b83316ab32680eb8f00f8cd3b904d681246d285a0e" --- 1,000,000 x 'a' is the classic FIPS vector; fall back to 1,000 x 'a' --- if it happens to be slow on this runtime (> 2 s). test "sha: NIST long message", -> - start = os.clock! - digest = hex sha256 string.rep "a", 1000000 - elapsed = os.clock! - start - if elapsed < 2 - assert digest == - "cdc76e5c9914fb9281a1c7e284d73e67f1809a48a497200e046d39ccc7112cd0" - else - assert hex(sha256 string.rep "a", 1000) == - "41edece42d63e8d9bf515a9ba6932e1c20cbc9f5a5d134645adb5db1b9737ea3" + assert hex(sha256 string.rep "a", 1000) == + "41edece42d63e8d9bf515a9ba6932e1c20cbc9f5a5d134645adb5db1b9737ea3" -- HMAC-SHA256 (RFC 4231). test "hmac: RFC 4231 test case 1", -> diff --git a/ipparse/tests/test_lunatik_kernel_portable.moon b/ipparse/tests/test_lunatik_kernel_portable.moon index 36f92eb..6ce7aca 100644 --- a/ipparse/tests/test_lunatik_kernel_portable.moon +++ b/ipparse/tests/test_lunatik_kernel_portable.moon @@ -25,6 +25,7 @@ mods = { "ipparse.tests.l7.test_dns" "ipparse.tests.l7.tls.test_handshake" "ipparse.tests.test_malformed" + "ipparse.tests.lib.test_sha" "ipparse.tests.lib.test_hkdf" "ipparse.tests.lib.test_bit_compat" "ipparse.tests.lib.test_pack_compat" From 2afeae526f56721513fd8420d01f6fb99e027b6d Mon Sep 17 00:00:00 2001 From: jperon Date: Tue, 10 Nov 2026 18:15:18 +0100 Subject: [PATCH 13/43] test(pack_compat): cover long formats, mixed strings/padding and 64-bit bounds Co-Authored-By: Claude Opus 5.5 --- ipparse/tests/lib/test_pack_compat.moon | 45 +++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/ipparse/tests/lib/test_pack_compat.moon b/ipparse/tests/lib/test_pack_compat.moon index 89358cf..cc4f3c0 100644 --- a/ipparse/tests/lib/test_pack_compat.moon +++ b/ipparse/tests/lib/test_pack_compat.moon @@ -77,4 +77,49 @@ if is_fallback expected = from_hex "2c" assert got == expected, "got: #{got}\nexp: #{expected}" +-- More than eight arguments exercises the generic vararg path. +test "pack_compat: >B x10 more than 8 args", -> + got = pack ">B B B B B B B B B B", 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 + expected = from_hex "0102030405060708090a" + assert got == expected, "got: #{got}\nexp: #{expected}" + +-- Padding byte between integers. +test "pack_compat: >H x H", -> + got = pack ">H x H", 1, 2 + expected = from_hex "0001000002" + assert got == expected, "got: #{got}\nexp: #{expected}" + +-- String options in the middle of integer fields. +test "pack_compat: >H c2 H", -> + got = pack ">H c2 H", 1, "ab", 2 + expected = from_hex "000161620002" + assert got == expected, "got: #{got}\nexp: #{expected}" + +test "pack_compat: >H z H", -> + got = pack ">H z H", 1, "ab", 2 + expected = from_hex "00016162000002" + assert got == expected, "got: #{got}\nexp: #{expected}" + +test "pack_compat: >H s2 H", -> + got = pack ">H s2 H", 1, "ab", 2 + expected = from_hex "0001000261620002" + assert got == expected, "got: #{got}\nexp: #{expected}" + +-- Signed 32-bit negative value wraps to two's complement. +test "pack_compat: >i4 negative", -> + got = pack ">i4", -2 + expected = from_hex "fffffffe" + assert got == expected, "got: #{got}\nexp: #{expected}" + +-- 64-bit little-endian value above 2^32 keeps its high word. +test "pack_compat: + got = pack " + got = pack "" + assert got == "", "got: #{got}" + util.summary "pack_compat" From e34ca7a49a143808efa9f2335b23ddfc604dfe52 Mon Sep 17 00:00:00 2001 From: jperon Date: Tue, 10 Nov 2026 18:18:53 +0100 Subject: [PATCH 14/43] perf(ipparse): write pack bytes into a shared buffer instead of per-field strings Replace the per-field string encoders with writers that fill a module-level byte buffer, then emit one string.char per run of integers. String options (c, s, z) and floats flush the buffer first; arguments a1..a8 avoid select and vararg in the fast path. This lets LuaJIT compile pack instead of falling back to the interpreter (no FNEW/VARG aborts). LuaJIT 2.1, us per call (before -> after): pack(">H H") 0.259 -> 0.122 pack(">H B B H H H H") 0.847 -> 0.187 pack(">H H I4 s2") 0.664 -> 0.285 full DNS message pack 3.814 -> 1.904 Co-Authored-By: Claude Opus 5.5 --- ipparse/lib/pack_compat_lib.moon | 187 ++++++++++++++++++++++--------- 1 file changed, 134 insertions(+), 53 deletions(-) diff --git a/ipparse/lib/pack_compat_lib.moon b/ipparse/lib/pack_compat_lib.moon index 9ac5b1e..f249886 100644 --- a/ipparse/lib/pack_compat_lib.moon +++ b/ipparse/lib/pack_compat_lib.moon @@ -228,67 +228,121 @@ bytes_to_float = (s, offset, le) -> -- ─── string.pack ───────────────────────────────────────────────────────────── --- Construit un encodeur spécialisé pour un entier non signé de `sz` octets. --- Les tailles courantes (1, 2, 3, 4) sont déroulées via string.char, sans --- allocation de table ni appel FFI ; les tailles 5-6 utilisent une petite --- boucle et au-delà de 6 octets on retombe sur write_uint (précision 64 bits). +-- Tampon d'octets partagé au niveau du module. `pack` n'appelle aucun callback +-- utilisateur pendant la construction : il n'est donc pas réentrant, ce qui est +-- acceptable ici. Les writers écrivent dans _buf[n+1 .. n+size] et renvoient le +-- nouvel index (0-based), sans allouer de chaîne. +_buf = {} + +-- Convertit _buf[1..n] en chaîne, par morceaux de 4096 max pour rester sous la +-- limite pratique de `unpack`/`string.char`. Fonction de module (et non closure +-- locale à pack) pour que LuaJIT puisse compiler le chemin rapide : créer une +-- closure par appel (bytecode FNEW) est NYI et annule la compilation du trace. +buf_string = (buf, n) -> + if n <= 4096 + string.char tunpack buf, 1, n + else + out = {} + i = 0 + while i < n + len = math.min 4096, n - i + out[#out+1] = string.char tunpack buf, i + 1, i + len + i += len + table.concat out + +-- Ajoute `s` à `parts` (crée la liste au besoin) et renvoie la liste. +append_string = (parts, s) -> + parts = {} unless parts + parts[#parts+1] = s + parts + +-- Construit un writer d'entier non signé de `sz` octets : écrit les octets dans +-- `buf` à partir de l'index `n` et renvoie `n + sz`. +-- Les tailles 1 à 4 sont déroulées, 5-6 utilisent une petite boucle, et au-delà +-- de 6 octets on passe par uint64_t pour conserver la précision actuelle. make_uint = (sz, le) -> if sz == 1 - (v) -> string.char(v % 256) + (buf, n, v) -> + buf[n + 1] = v % 256 + n + 1 elseif sz == 2 if le - (v) -> + (buf, n, v) -> u = v % 65536 - string.char(u % 256, math.floor(u / 256) % 256) + buf[n + 1] = u % 256 + buf[n + 2] = math.floor(u / 256) % 256 + n + 2 else - (v) -> + (buf, n, v) -> u = v % 65536 - string.char(math.floor(u / 256) % 256, u % 256) + buf[n + 1] = math.floor(u / 256) % 256 + buf[n + 2] = u % 256 + n + 2 elseif sz == 3 mod = 16777216 if le - (v) -> + (buf, n, v) -> u = v % mod - string.char(u % 256, math.floor(u / 256) % 256, math.floor(u / 65536) % 256) + buf[n + 1] = u % 256 + buf[n + 2] = math.floor(u / 256) % 256 + buf[n + 3] = math.floor(u / 65536) % 256 + n + 3 else - (v) -> + (buf, n, v) -> u = v % mod - string.char(math.floor(u / 65536) % 256, math.floor(u / 256) % 256, u % 256) + buf[n + 1] = math.floor(u / 65536) % 256 + buf[n + 2] = math.floor(u / 256) % 256 + buf[n + 3] = u % 256 + n + 3 elseif sz == 4 mod = 4294967296 if le - (v) -> + (buf, n, v) -> u = v % mod - string.char(u % 256, math.floor(u / 256) % 256, - math.floor(u / 65536) % 256, math.floor(u / 16777216) % 256) + buf[n + 1] = u % 256 + buf[n + 2] = math.floor(u / 256) % 256 + buf[n + 3] = math.floor(u / 65536) % 256 + buf[n + 4] = math.floor(u / 16777216) % 256 + n + 4 else - (v) -> + (buf, n, v) -> u = v % mod - string.char(math.floor(u / 16777216) % 256, math.floor(u / 65536) % 256, - math.floor(u / 256) % 256, u % 256) + buf[n + 1] = math.floor(u / 16777216) % 256 + buf[n + 2] = math.floor(u / 65536) % 256 + buf[n + 3] = math.floor(u / 256) % 256 + buf[n + 4] = u % 256 + n + 4 elseif sz <= 6 mod = 2 ^ (8 * sz) if le - (v) -> + (buf, n, v) -> u = v % mod - bytes = {} for k = 1, sz - bytes[k] = u % 256 + buf[n + k] = u % 256 u = math.floor(u / 256) - string.char tunpack bytes + n + sz else - (v) -> + (buf, n, v) -> u = v % mod - bytes = {} for k = sz, 1, -1 - bytes[k] = u % 256 + buf[n + k] = u % 256 u = math.floor(u / 256) - string.char tunpack bytes + n + sz else - (v) -> - buf = ffi.new "uint8_t[?]", sz - write_uint buf, 0, v, sz, le - ffi.string buf, sz + if le + (buf, n, v) -> + u = ffi.cast "uint64_t", v + for k = 1, sz + buf[n + k] = tonumber(u % 256) + u = u / 256 + n + sz + else + (buf, n, v) -> + u = ffi.cast "uint64_t", v + for k = sz, 1, -1 + buf[n + k] = tonumber(u % 256) + u = u / 256 + n + sz make_float = (le) -> (v) -> string.char(tunpack(float_to_bytes(v, le))) @@ -303,25 +357,21 @@ make_c = (sz) -> else v\sub 1, sz -make_s = (sz, le) -> - len_emit = make_uint sz, le - (v) -> len_emit(#v) .. v - -- Choisit l'encodeur d'une option au moment de la compilation du format. pack_op = (opt, count, le) -> switch opt when "b", "B" -- int8 / uint8 - { consume: true, emit: make_uint(1, le) } + { consume: true, write: make_uint(1, le) } when "h", "H" -- int16 / uint16 - { consume: true, emit: make_uint(2, le) } + { consume: true, write: make_uint(2, le) } when "i", "I" -- int/uint N octets (défaut 4) - { consume: true, emit: make_uint((count or 4), le) } + { consume: true, write: make_uint((count or 4), le) } when "l", "L", "j", "J", "T" -- 64 bits - { consume: true, emit: make_uint(8, le) } + { consume: true, write: make_uint(8, le) } when "f" -- float 32 { consume: true, emit: make_float le } @@ -333,44 +383,75 @@ pack_op = (opt, count, le) -> { consume: true, emit: make_c(count or 1) } when "s" -- chaîne préfixée longueur (size_t = 8 octets par défaut) - { consume: true, emit: make_s((count or 8), le) } + { consume: true, write_len: make_uint((count or 8), le), emit: (v) -> v } when "z" -- chaîne null-terminated { consume: true, emit: (v) -> v .. "\0" } when "x" -- padding (un octet nul) - { consume: false, emit: -> "\0" } + { consume: false, write: (buf, n) -> + buf[n + 1] = 0 + n + 1 } else -- "X" (alignement) et options inconnues : no-op - { consume: false, emit: -> "" } + { consume: false, write: (buf, n) -> n } -- Cache des formats compilés en liste d'encodeurs : fmt → plan. _pack_cache = {} compile_pack = (fmt) -> plan = {} + consumed = 0 iter = parse_format fmt opt, count, le = iter! while opt != nil - plan[#plan+1] = pack_op opt, count, le + op = pack_op opt, count, le + plan[#plan+1] = op + consumed += 1 if op.consume opt, count, le = iter! + plan.count = consumed _pack_cache[fmt] = plan plan -pack = (fmt, ...) -> - plan = _pack_cache[fmt] or compile_pack(fmt) - parts = {} - argi = 1 +-- `a1`..`a8` couvrent la grande majorité des formats sans vararg ni `select` +-- dans la boucle ; au-delà de 8 valeurs consommées, on bascule sur le chemin +-- générique qui collecte le vararg une seule fois. +pack = (fmt, a1, a2, a3, a4, a5, a6, a7, a8, ...) -> + plan = _pack_cache[fmt] or compile_pack(fmt) + args = nil + if plan.count > 8 + args = {a1, a2, a3, a4, a5, a6, a7, a8, ...} + else + args = {a1, a2, a3, a4, a5, a6, a7, a8} + + n = 0 + parts = nil + argi = 1 for j = 1, #plan op = plan[j] - if op.consume - parts[#parts+1] = op.emit(select(argi, ...)) - argi += 1 + if op.write + if op.consume + n = op.write _buf, n, args[argi] + argi += 1 + else + n = op.write _buf, n else - parts[#parts+1] = op.emit! - - table.concat parts + v = args[argi] + argi += 1 + if op.write_len + n = op.write_len _buf, n, #v + if n > 0 + parts = append_string parts, buf_string(_buf, n) + n = 0 + parts = append_string parts, op.emit v + + if parts + if n > 0 + parts = append_string parts, buf_string(_buf, n) + table.concat parts + else + buf_string _buf, n -- ─── string.unpack ─────────────────────────────────────────────────────────── From b0ffad19ffd27a40723721d4e46f602f58b13fa3 Mon Sep 17 00:00:00 2001 From: jperon Date: Tue, 10 Nov 2026 18:14:54 +0100 Subject: [PATCH 15/43] fix(vmtest): propagate resolved build paths to the guest The guest re-sources lib.sh, which recomputed BUILD_DIR from VMTEST_BUILD_DIR. That variable was never exported into cmd.sh, so a cache outside the repo was silently replaced by the default path inside the read-only repo, and guest-test.sh failed on kernelrelease. Resolve BUILD_DIR and LUNATIK_SRC to absolute paths in lib.sh and export both from the host into the guest wrapper. Co-Authored-By: Claude Opus 5 --- tools/vmtest/lib.sh | 6 ++++-- tools/vmtest/run.sh | 4 ++++ 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/tools/vmtest/lib.sh b/tools/vmtest/lib.sh index 81c87a5..be1519f 100755 --- a/tools/vmtest/lib.sh +++ b/tools/vmtest/lib.sh @@ -4,14 +4,16 @@ set -euo pipefail VMTEST_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" REPO_ROOT="$(cd "$VMTEST_DIR/../.." && pwd)" -BUILD_DIR="${VMTEST_BUILD_DIR:-$VMTEST_DIR/build}" +# Absolute paths: the guest re-sources this file and must not resolve them +# relative to its own cwd (which differs from the host's). +BUILD_DIR="$(realpath -m -- "${VMTEST_BUILD_DIR:-$VMTEST_DIR/build}")" # shellcheck source=versions.env . "$VMTEST_DIR/versions.env" KERNEL_SRC="$BUILD_DIR/linux-$KERNEL_VERSION" KERNEL_IMAGE="$KERNEL_SRC/arch/x86/boot/bzImage" -LUNATIK_SRC="${LUNATIK_SRC:-$BUILD_DIR/lunatik}" +LUNATIK_SRC="$(realpath -m -- "${LUNATIK_SRC:-$BUILD_DIR/lunatik}")" log() { printf '\033[1;34m[vmtest]\033[0m %s\n' "$*" >&2; } die() { printf '\033[1;31m[vmtest]\033[0m %s\n' "$*" >&2; exit 1; } diff --git a/tools/vmtest/run.sh b/tools/vmtest/run.sh index f5c2074..38c9a71 100755 --- a/tools/vmtest/run.sh +++ b/tools/vmtest/run.sh @@ -39,6 +39,10 @@ in_vm() { { printf '#!%s\n' "$BASH_ABS" printf 'export PATH=%q\n' "$PATH" + # The guest re-sources lib.sh, which would otherwise fall back to the + # build dir inside the (read-only) repo instead of the host's cache. + printf 'export VMTEST_BUILD_DIR=%q\n' "$BUILD_DIR" + printf 'export LUNATIK_SRC=%q\n' "$LUNATIK_SRC" printf '( %s ) > %q/out 2>&1\n' "$*" "$RW_DIR" printf 'echo $? > %q/rc\n' "$RW_DIR" } > "$wrapper" From dffaaf61de90deeb0adcdb30a66922df423857e4 Mon Sep 17 00:00:00 2001 From: jperon Date: Tue, 10 Nov 2026 18:15:03 +0100 Subject: [PATCH 16/43] fix(vmtest): ignore build and .venv symlinks A trailing-slash pattern only matches directories, so a natural `tools/vmtest/build -> /shared/cache` symlink showed up as untracked. Anchor the patterns with a leading slash, which also covers symlinks. Co-Authored-By: Claude Opus 5 --- tools/vmtest/.gitignore | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/tools/vmtest/.gitignore b/tools/vmtest/.gitignore index e7f9d25..a71dd17 100644 --- a/tools/vmtest/.gitignore +++ b/tools/vmtest/.gitignore @@ -1,2 +1,5 @@ -build/ -.venv/ +# Leading slashes anchor to this directory and also match a symlink +# (e.g. `build -> ../../elsewhere/tools/vmtest/build` for a shared cache), +# which a trailing-slash pattern (`build/`) does not. +/build +/.venv From 8b8d3d466112ed10c2898bd96c92fc4f26172a06 Mon Sep 17 00:00:00 2001 From: jperon Date: Tue, 10 Nov 2026 18:15:03 +0100 Subject: [PATCH 17/43] fix(vmtest): use a per-run VM scratch directory All invocations wrote cmd.sh/out/rc/console.log under the same fixed /tmp/vmtest-rw. Two concurrent runs removed each other's directory and one failed with "VM did not run the guest command". Use mktemp -d per run (removed on success, kept on failure for the log), while keeping VMTEST_RW_DIR as an explicit opt-in override. Co-Authored-By: Claude Opus 5 --- tools/vmtest/run.sh | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/tools/vmtest/run.sh b/tools/vmtest/run.sh index 38c9a71..d6b127c 100755 --- a/tools/vmtest/run.sh +++ b/tools/vmtest/run.sh @@ -25,8 +25,17 @@ export PATH="$VMTEST_DIR/.venv/bin:$PATH" # code into a shared writable directory instead. BASH_ABS="$(command -v bash)" # Must live OUTSIDE the repository: nested inside --rodir it would be -# shadowed by the read-only mount. -RW_DIR="${VMTEST_RW_DIR:-${TMPDIR:-/tmp}/vmtest-rw}" +# shadowed by the read-only mount. Default to a fresh directory per run so +# concurrent invocations do not clobber each other's out/rc/console.log. +if [ -n "${VMTEST_RW_DIR:-}" ]; then + RW_DIR="$VMTEST_RW_DIR" + RW_DIR_OWNED=0 +else + RW_DIR="$(mktemp -d "${TMPDIR:-/tmp}/vmtest-rw.XXXXXX")" + RW_DIR_OWNED=1 + # Keep the console log around on failure (the error message points to it). + trap 'st=$?; if [ "$st" -eq 0 ] && [ "$RW_DIR_OWNED" -eq 1 ]; then rm -rf "$RW_DIR"; fi' EXIT +fi # vng bind-mounts the kernel's module dir read-only over /lib/modules; # lunatik and ipparse need to install there, so mount a writable 9p # (host-backed, reusable across boots) on top instead. From 4134386d59a2a07fb958d7b433c56030789707fd Mon Sep 17 00:00:00 2001 From: jperon Date: Tue, 10 Nov 2026 18:15:05 +0100 Subject: [PATCH 18/43] fix(vmtest): document guest paths and per-run scratch dir Co-Authored-By: Claude Opus 5 --- tools/vmtest/README.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/tools/vmtest/README.md b/tools/vmtest/README.md index db72b5a..9fda231 100644 --- a/tools/vmtest/README.md +++ b/tools/vmtest/README.md @@ -44,6 +44,14 @@ Variables utiles : du clone épinglé ; - `VMTEST_BUILD_DIR` — déplacer le cache de build. +Les deux sont résolus en chemins absolus et transmis à l'invité, qui monte le +système de fichiers de l'hôte en lecture seule : un cache situé hors du dépôt +(par exemple `tools/vmtest/build` en lien symbolique vers un cache partagé) +fonctionne tel quel. Chaque lancement utilise son propre répertoire temporaire +pour rapatrier sortie/code de retour, donc deux `run.sh` simultanés ne se +marchèvent pas (`VMTEST_RW_DIR` permet de forcer un répertoire commun, à +réserver à un usage séquentiel). + ## Fichiers | Fichier | Rôle | From a5c1d819f33df52ae8ab233b8a59f70485488f16 Mon Sep 17 00:00:00 2001 From: jperon Date: Tue, 10 Nov 2026 18:23:06 +0100 Subject: [PATCH 19/43] fix(quic): encode 8-byte varints without math.floor on native integers Use 64-bit shifts/masks when values are native integers (Lua 5.3+ and the Lunatik kernel, where math.floor and floats are unavailable); keep the high/low division split on LuaJIT doubles. Byte output is unchanged. Co-Authored-By: Claude Opus 5.5 --- ipparse/l4/quic/frames.moon | 45 ++++++++++++++++++-------- ipparse/tests/l4/quic/test_varint.moon | 11 +++++++ 2 files changed, 43 insertions(+), 13 deletions(-) diff --git a/ipparse/l4/quic/frames.moon b/ipparse/l4/quic/frames.moon index b66707b..8e5f175 100644 --- a/ipparse/l4/quic/frames.moon +++ b/ipparse/l4/quic/frames.moon @@ -32,6 +32,11 @@ pack: sp, unpack: su = require "ipparse.lib.pack_compat" :bidirectional = require"ipparse.fun" {:band, :bor, :bnot, :lshift, :rshift} = require"ipparse.lib.bit_compat" +-- True when values are native 64-bit integers (Lua 5.3+/Lunatik kernel), where +-- bitwise shifts cover the full 64-bit range and floats/math.floor may be absent. +-- False on LuaJIT, where numbers are doubles and bit ops are 32-bit. +native_int = math.floor == nil or math.type ~= nil + --- QUIC Frame Types -- Mapping of frame type codes to their names frame_types = bidirectional { @@ -126,19 +131,33 @@ encode_varint = (value) -> band(value, 0xFF) ) else - high = math.floor(value / 4294967296) - low = value % 4294967296 - -- Same issue: encode manually with top 2 bits = 11 - string.char( - bor(0xC0, band(math.floor(high / 16777216), 0x3F)) - band(math.floor(high / 65536), 0xFF) - band(math.floor(high / 256), 0xFF) - band(high, 0xFF) - band(math.floor(low / 16777216), 0xFF) - band(math.floor(low / 65536), 0xFF) - band(math.floor(low / 256), 0xFF) - band(low, 0xFF) - ) + if native_int + -- Native 64-bit integers: drive the byte split with shifts/masks only + -- (no floats, no math.floor, no division). + string.char( + bor(0xC0, band(rshift(value, 56), 0x3F)) + band(rshift(value, 48), 0xFF) + band(rshift(value, 40), 0xFF) + band(rshift(value, 32), 0xFF) + band(rshift(value, 24), 0xFF) + band(rshift(value, 16), 0xFF) + band(rshift(value, 8), 0xFF) + band(value, 0xFF) + ) + else + -- LuaJIT: doubles with 32-bit bit ops, so split high/low by division. + high = math.floor(value / 4294967296) + low = value % 4294967296 + string.char( + bor(0xC0, band(math.floor(high / 16777216), 0x3F)) + band(math.floor(high / 65536), 0xFF) + band(math.floor(high / 256), 0xFF) + band(high, 0xFF) + band(math.floor(low / 16777216), 0xFF) + band(math.floor(low / 65536), 0xFF) + band(math.floor(low / 256), 0xFF) + band(low, 0xFF) + ) --- Parses a PADDING frame -- PADDING frames consist only of the frame type (0x00) diff --git a/ipparse/tests/l4/quic/test_varint.moon b/ipparse/tests/l4/quic/test_varint.moon index 0f3133d..779db83 100644 --- a/ipparse/tests/l4/quic/test_varint.moon +++ b/ipparse/tests/l4/quic/test_varint.moon @@ -60,9 +60,20 @@ test "varint: round-trip 70000", -> test "varint: round-trip 8-byte value", -> value = 4886718345 -- 0x123456789 encoded = encode_varint value + assert encoded == "\xC0\x00\x00\x01\x23\x45\x67\x89", "8-byte encoding mismatch" v, _ = parse_varint encoded, 1 assert v == value, "round-trip 8-byte value failed: got #{v}" +-- 2^62-1 is not exactly representable as a double (LuaJIT): only run where +-- native integers exist. +if math.type + test "varint: round-trip 8-byte max value", -> + value = 4611686018427387903 -- 0x3FFFFFFFFFFFFFFF (2^62-1) + encoded = encode_varint value + assert encoded == "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF", "max 8-byte encoding mismatch" + v, _ = parse_varint encoded, 1 + assert v == value, "round-trip max 8-byte value failed: got #{v}" + test "varint: truncated 8-byte returns nil", -> v, off = parse_varint "\xC0\x00", 1 assert v == nil and off == 1, "expected nil with unchanged offset" From 333d2ae08edbf8fff318b93c9f422efa3cd52e53 Mon Sep 17 00:00:00 2001 From: jperon Date: Tue, 10 Nov 2026 18:23:40 +0100 Subject: [PATCH 20/43] test(pack_compat): load the portable entrypoint instead of LuaJIT-only module require ipparse.lib.pack_compat returns string.pack on Lua 5.3+ and the fallback on LuaJIT, so the test module also parses under the Lunatik kernel. Co-Authored-By: Claude Opus 5.5 --- ipparse/tests/lib/test_pack_compat.moon | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ipparse/tests/lib/test_pack_compat.moon b/ipparse/tests/lib/test_pack_compat.moon index cc4f3c0..52e1793 100644 --- a/ipparse/tests/lib/test_pack_compat.moon +++ b/ipparse/tests/lib/test_pack_compat.moon @@ -8,7 +8,7 @@ util = require "ipparse.lib.util" {:test} = util -pack_compat = require "ipparse.lib.pack_compat_lib" +pack_compat = require "ipparse.lib.pack_compat" pack = pack_compat.pack or string.pack -- Native string.pack rejects the two fallback-only behaviours tested below. is_fallback = pack != string.pack From 6a5ced14121bcd1e532604e4077fa1c5d77fe741 Mon Sep 17 00:00:00 2001 From: jperon Date: Tue, 10 Nov 2026 18:30:49 +0100 Subject: [PATCH 21/43] fix(tcp_stream): use a portable integer clock Read the current time through a configurable clock returning an integer in fixed units instead of os.time, which is absent in Lunatik. Defaults to os.time (1 Hz) when available, then linux.time (1 GHz) when the kernel module is loaded, and errors at creation when no clock exists. purge(max_age) now compares against max_age * clock_hz, keeping the comparison integer-only (no division, no floats). Co-Authored-By: Claude Opus 5.5 --- ipparse/l4/tcp_stream.moon | 25 +++++++++++++++++++++---- ipparse/tests/l4/test_tcp_stream.moon | 14 ++++++++++++++ 2 files changed, 35 insertions(+), 4 deletions(-) diff --git a/ipparse/l4/tcp_stream.moon b/ipparse/l4/tcp_stream.moon index 9419843..845a3d8 100644 --- a/ipparse/l4/tcp_stream.moon +++ b/ipparse/l4/tcp_stream.moon @@ -18,11 +18,27 @@ -- true (each segment with payload is immediately complete). When provided, -- feed returns nil until the predicate passes, then auto-clears the session. -- @tparam[opt] table opts Options: `max_buf` (bytes per session, default 1 MiB), --- `max_sessions` (default 1024). +-- `max_sessions` (default 1024), `clock` (zero-arg function returning an +-- integer instant in fixed units) and `clock_hz` (units per second). -- @treturn table State with :feed, :clear, :reset and :purge methods. +-- @raise Error at creation when no clock is available and none is provided. new = (check_complete=(-> true), opts={}) -> max_buf = opts.max_buf or 0x100000 max_sessions = opts.max_sessions or 1024 + clock = opts.clock + clock_hz = opts.clock_hz + if not clock + if os and os.time + clock = os.time + clock_hz = clock_hz or 1 + else + ok, linux = pcall require, "linux" + if ok and linux and linux.time + clock = linux.time + clock_hz = clock_hz or 1000000000 + else + error "tcp_stream: no clock available (pass opts.clock/opts.clock_hz)" + clock_hz = 1 if not clock_hz sessions = {} count = 0 @@ -56,7 +72,7 @@ new = (check_complete=(-> true), opts={}) -> first_seg = entry == nil if first_seg drop_oldest! if count >= max_sessions - entry = { segments: {payload}, total: #payload, :init_seq, timestamp: os.time! } + entry = { segments: {payload}, total: #payload, :init_seq, timestamp: clock! } sessions[key] = entry count += 1 else @@ -87,9 +103,10 @@ new = (check_complete=(-> true), opts={}) -> --- Purge sessions older than max_age seconds. -- @tparam[opt=300] number max_age purge: (max_age=300) -> - now = os.time! + now = clock! + threshold = max_age * clock_hz for key, entry in pairs sessions - if now - entry.timestamp > max_age + if now - entry.timestamp > threshold drop key } diff --git a/ipparse/tests/l4/test_tcp_stream.moon b/ipparse/tests/l4/test_tcp_stream.moon index a0df3a0..bc36fd5 100644 --- a/ipparse/tests/l4/test_tcp_stream.moon +++ b/ipparse/tests/l4/test_tcp_stream.moon @@ -49,4 +49,18 @@ test "tcp_stream: reset drops everything", -> buf = s.feed "k", "123456", 0x10, 2 assert buf == "123456", "buffer should only contain post-reset data, got #{buf}" +test "tcp_stream: purge drops sessions older than max_age", -> + now = 0 + s = tcp_stream.new ((buf) -> #buf >= 6), clock: -> now, clock_hz: 1000 + s.feed "old", "abc", 0x10, 1 + now = 5000 -- 5 s later + s.feed "new", "abc", 0x10, 1 + s.purge 3 -- max_age = 3 s => 3000 ticks + -- The recent session survived: appending completes its buffer. + buf = s.feed "new", "def", 0x10, 2 + assert buf == "abcdef", "recent session should survive purge, got #{buf}" + -- The old session was dropped: a new segment starts from scratch. + buf = s.feed "old", "def", 0x10, 2 + assert buf == nil, "old session should have been purged, got #{buf}" + util.summary "l4/tcp_stream" From 6c9508ee5e197f7bf841a05d86a26606d72b5841 Mon Sep 17 00:00:00 2001 From: jperon Date: Tue, 10 Nov 2026 18:31:25 +0100 Subject: [PATCH 22/43] test(varint): run 2^62-1 round-trip whenever native integers exist Mirror frames.moon's native_int check (math.floor == nil or math.type ~= nil) so the maximum-value case also runs in the Lunatik kernel, where math.type is absent but native 64-bit integers are available. Co-Authored-By: Claude Opus 5.5 --- ipparse/tests/l4/quic/test_varint.moon | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/ipparse/tests/l4/quic/test_varint.moon b/ipparse/tests/l4/quic/test_varint.moon index 779db83..289c814 100644 --- a/ipparse/tests/l4/quic/test_varint.moon +++ b/ipparse/tests/l4/quic/test_varint.moon @@ -65,8 +65,9 @@ test "varint: round-trip 8-byte value", -> assert v == value, "round-trip 8-byte value failed: got #{v}" -- 2^62-1 is not exactly representable as a double (LuaJIT): only run where --- native integers exist. -if math.type +-- native integers exist. Same condition as frames.moon's native_int: in the +-- kernel math.type is absent but native 64-bit integers do exist. +if math.floor == nil or math.type ~= nil test "varint: round-trip 8-byte max value", -> value = 4611686018427387903 -- 0x3FFFFFFFFFFFFFFF (2^62-1) encoded = encode_varint value From d292002d2707f9c098afdb5e8cb471735cc70efe Mon Sep 17 00:00:00 2001 From: jperon Date: Tue, 10 Nov 2026 18:34:18 +0100 Subject: [PATCH 23/43] perf(pack_compat): avoid tail-call returns in pack MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit LuaJIT aborted the traces exiting pack's loop with "NYI: return to lower frame" because `return table.concat(parts)` / `return buf_string(...)` are tail calls, whose return target frame is not recordable. Assigning the result to a local and returning it removes every abort for b4.lua. LuaJIT 2.1, bench b4.lua (median of 5, µs): msg pack 1.76 (was 1.79-3.39, noisy) rr pack 0.28 sp >H H I4 s2 0.24 sp >H B B H H H H 0.19 sp >H H 0.12 -jv: no abort/NYI/blacklist on pack_compat_lib.lua (was 3 kinds). Co-Authored-By: Claude Opus 5.5 --- ipparse/lib/pack_compat_lib.moon | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/ipparse/lib/pack_compat_lib.moon b/ipparse/lib/pack_compat_lib.moon index f249886..d3187f6 100644 --- a/ipparse/lib/pack_compat_lib.moon +++ b/ipparse/lib/pack_compat_lib.moon @@ -446,12 +446,17 @@ pack = (fmt, a1, a2, a3, a4, a5, a6, a7, a8, ...) -> n = 0 parts = append_string parts, op.emit v + -- Assign before returning: `return table.concat parts` would be a tail call, + -- whose return to the caller frame LuaJIT cannot record (NYI: return to + -- lower frame), aborting the trace that exits the loop above. + result = nil if parts if n > 0 parts = append_string parts, buf_string(_buf, n) - table.concat parts + result = table.concat parts else - buf_string _buf, n + result = buf_string _buf, n + result -- ─── string.unpack ─────────────────────────────────────────────────────────── From e4908ee1f9be2f3db151fc5619a2b9b09e9ce410 Mon Sep 17 00:00:00 2001 From: jperon Date: Tue, 10 Nov 2026 18:35:24 +0100 Subject: [PATCH 24/43] perf(pack_compat): single trailing string fast path MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When every option but the last only writes integer bytes and the last one emits a string (s/c/z/f/d as the final field), skip the parts list and table.concat entirely: write the length prefix (if any), flush the byte buffer, and concatenate once with the emitted string. This covers pack_rr's ">H H I4 s2", EDNS's ">Hs2" and the zero/lib string cases. LuaJIT 2.1, bench b4.lua (median of 5, µs): msg pack 1.41 (was 1.76) rr pack 0.20 (was 0.28) sp >H H I4 s2 0.17 (was 0.24) sp >H B B H H H H 0.18 (was 0.19) sp >H H 0.12 (was 0.12) -jv: still no abort/NYI/blacklist on pack_compat_lib.lua. Tests: LuaJIT 296/296, Lua 5.4 309/309. Co-Authored-By: Claude Opus 5.5 --- ipparse/lib/pack_compat_lib.moon | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/ipparse/lib/pack_compat_lib.moon b/ipparse/lib/pack_compat_lib.moon index d3187f6..78cc289 100644 --- a/ipparse/lib/pack_compat_lib.moon +++ b/ipparse/lib/pack_compat_lib.moon @@ -410,6 +410,22 @@ compile_pack = (fmt) -> consumed += 1 if op.consume opt, count, le = iter! plan.count = consumed + + -- Chemin rapide « une seule chaîne en dernière position » (cas de `s2` dans + -- pack_rr, `Hs2` de l'EDNS, ou une option `z`/`c`/`f`/`d` finale) : toutes + -- les options précédentes n'écrivent que des octets entiers, la dernière + -- produit une chaîne. On évite alors la table `parts` et `table.concat`. + tail = nil + if #plan > 0 + last = plan[#plan] + if last.consume and last.emit and not last.write + ok = true + for k = 1, #plan - 1 + prev = plan[k] + ok = false unless prev.write and not prev.emit + tail = last if ok + plan.tail = tail + _pack_cache[fmt] = plan plan @@ -428,7 +444,9 @@ pack = (fmt, a1, a2, a3, a4, a5, a6, a7, a8, ...) -> parts = nil argi = 1 - for j = 1, #plan + last = plan.tail + limit = last and (#plan - 1) or #plan + for j = 1, limit op = plan[j] if op.write if op.consume @@ -450,7 +468,12 @@ pack = (fmt, a1, a2, a3, a4, a5, a6, a7, a8, ...) -> -- whose return to the caller frame LuaJIT cannot record (NYI: return to -- lower frame), aborting the trace that exits the loop above. result = nil - if parts + if last + v = args[argi] + if last.write_len + n = last.write_len _buf, n, #v + result = buf_string(_buf, n) .. last.emit v + elseif parts if n > 0 parts = append_string parts, buf_string(_buf, n) result = table.concat parts From 5b2cfbd10827a91bbbee14e44373d1d12cace09d Mon Sep 17 00:00:00 2001 From: jperon Date: Tue, 10 Nov 2026 18:36:11 +0100 Subject: [PATCH 25/43] perf(pack_compat): reuse a module-level argument array MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit pack allocated a fresh 8-slot table on every call, which dominated small formats. Reuse a module-level _args array instead (like _buf, pack never yields or calls user callbacks, so it is not reentrant). Formats consuming more than 8 values keep the one-off vararg table. LuaJIT 2.1, bench b4.lua (median of 5, µs): msg pack 1.19 (was 1.41) rr pack 0.18 (was 0.20) sp >H H I4 s2 0.137 (was 0.17) sp >H B B H H H H 0.146 (was 0.18) sp >H H 0.088 (was 0.12) -jv: still no abort/NYI/blacklist on pack_compat_lib.lua. Tests: LuaJIT 296/296, Lua 5.4 309/309. Co-Authored-By: Claude Opus 5.5 --- ipparse/lib/pack_compat_lib.moon | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/ipparse/lib/pack_compat_lib.moon b/ipparse/lib/pack_compat_lib.moon index 78cc289..497a3a1 100644 --- a/ipparse/lib/pack_compat_lib.moon +++ b/ipparse/lib/pack_compat_lib.moon @@ -234,6 +234,12 @@ bytes_to_float = (s, offset, le) -> -- nouvel index (0-based), sans allouer de chaîne. _buf = {} +-- Tableau d'arguments réutilisé par `pack` : allouer une table de 8 entrées à +-- chaque appel dominait le coût des petits formats (`>H H`, préfixe d'en-tête). +-- Comme `_buf`, ce tableau n'est pas réentrant, mais `pack` ne cède jamais la +-- main : aucun callback utilisateur n'est appelé pendant la construction. +_args = {} + -- Convertit _buf[1..n] en chaîne, par morceaux de 4096 max pour rester sous la -- limite pratique de `unpack`/`string.char`. Fonction de module (et non closure -- locale à pack) pour que LuaJIT puisse compiler le chemin rapide : créer une @@ -438,7 +444,15 @@ pack = (fmt, a1, a2, a3, a4, a5, a6, a7, a8, ...) -> if plan.count > 8 args = {a1, a2, a3, a4, a5, a6, a7, a8, ...} else - args = {a1, a2, a3, a4, a5, a6, a7, a8} + args = _args + args[1] = a1 + args[2] = a2 + args[3] = a3 + args[4] = a4 + args[5] = a5 + args[6] = a6 + args[7] = a7 + args[8] = a8 n = 0 parts = nil From 04385ba7daa6a9a01079d992a5a7ea51c6666e25 Mon Sep 17 00:00:00 2001 From: jperon Date: Tue, 10 Nov 2026 18:53:11 +0100 Subject: [PATCH 26/43] fix(crypto): recognize all Lunatik EBADMSG error-name forms lunatik_pusherrname emits "EBADMSG" on kernels >= 6.7, and on older kernels "%pE" of ERR_PTR(-err): "-EBADMSG" with CONFIG_SYMBOLIC_ERRNAME, or the signed decimal "-74" otherwise. Match these exact whole strings (after stripping Lua's optional "chunk:line: " prefix), so lookalike errnos such as -174 are not mistaken for a tag mismatch. decrypt always raises via lunatik_throw on failure and returns only the plaintext on success, so drop the bogus plaintext == "EBADMSG" check. Co-Authored-By: Claude Opus 5.5 --- ipparse/lib/crypto/backend/lunatik.moon | 30 ++++++++--- ipparse/tests/lib/crypto/test_lunatik.moon | 60 +++++++++++++++++++++- 2 files changed, 82 insertions(+), 8 deletions(-) diff --git a/ipparse/lib/crypto/backend/lunatik.moon b/ipparse/lib/crypto/backend/lunatik.moon index d7ad7ce..072ba43 100644 --- a/ipparse/lib/crypto/backend/lunatik.moon +++ b/ipparse/lib/crypto/backend/lunatik.moon @@ -27,6 +27,25 @@ close_tfm = (tfm) -> return unless tfm and tfm.__close pcall tfm.__close, tfm +-- EBADMSG error names produced by Lunatik's lunatik_pusherrname +-- (lunatik_aux.c, kernel >= 6.7): errname(abs(err)) == "EBADMSG". +-- On older kernels it formats "%pE" of ERR_PTR(-err): with +-- CONFIG_SYMBOLIC_ERRNAME this is errname(-err) == "-EBADMSG" +-- (lib/errname.c), otherwise the signed decimal "-74" (EBADMSG == 74, +-- include/uapi/asm-generic/errno.h), see lib/vsprintf.c err_ptr(). +ebadmsg_names = { EBADMSG: true, ["-EBADMSG"]: true, ["-74"]: true } + +--- Checks whether an error value denotes EBADMSG. +-- Compares the whole string so digits embedded in other errnos (e.g. -174) +-- are not matched. Lua's error() prepends "chunk:line: "; Lunatik's +-- lunatik_throw() calls lua_error() directly, without such a prefix. +-- @tparam string err Error value +-- @treturn boolean true if the error is EBADMSG +is_ebadmsg = (err) -> + s = tostring err + bare = (s\match "^.*:%d+: (.*)$") or s + ebadmsg_names[bare] == true + cached_gcm_enc_tfm = nil cached_gcm_enc_key = nil cached_gcm_dec_tfm = nil @@ -80,16 +99,13 @@ aes_128_gcm_decrypt = (key, nonce, ciphertext_with_tag, aad = "") -> return nil, "ciphertext_with_tag too short (need at least 16-byte tag)" c = get_gcm_tfm "decrypt", key -- Wrap decrypt call to catch EBADMSG as a handled error, not an exception - ok, pt_or_err, err = pcall -> c\decrypt nonce, ciphertext_with_tag, aad + ok, pt_or_err = pcall -> c\decrypt nonce, ciphertext_with_tag, aad unless ok - -- Decrypt failed with Lua error - if (tostring pt_or_err)\match "EBADMSG" + -- Decrypt raises on failure (luacrypto_aead_finish -> lunatik_throw) + if is_ebadmsg pt_or_err return nil, "AES-128-GCM authentication failed (tag mismatch)" return nil, "aead(gcm(aes)) decrypt failed: #{tostring pt_or_err}" - -- ok=true, pt_or_err is plaintext, err is error code - if pt_or_err == "EBADMSG" - return nil, "AES-128-GCM authentication failed (tag mismatch)" - -- pt_or_err is plaintext (may be empty string, which is valid) + -- Success always returns the plaintext (may be empty string, which is valid). pt_or_err, nil --- Encrypts single AES-128-ECB block. diff --git a/ipparse/tests/lib/crypto/test_lunatik.moon b/ipparse/tests/lib/crypto/test_lunatik.moon index dc26ecc..4c5eb31 100644 --- a/ipparse/tests/lib/crypto/test_lunatik.moon +++ b/ipparse/tests/lib/crypto/test_lunatik.moon @@ -44,10 +44,23 @@ mk_aead = -> assert #nonce == 12, "nonce length should be 12" "ct:" .. plaintext .. "|aad:" .. aad .. "|tag:" .. string.rep("T", 16) decrypt: (nonce, ciphertext_with_tag, aad="") => - if ciphertext_with_tag\sub(1, 8) == "AUTHFAIL" + prefix = ciphertext_with_tag\sub 1, 8 + if prefix == "AUTHFAIL" error "EBADMSG" + if prefix == "AUTHMIN1" + error "-EBADMSG" + if prefix == "AUTHNUM1" + error "-74" + if prefix == "AUTHNUM3" + error "-174" + if prefix == "AUTHNUM4" + error "-740" + if prefix == "NOTAUTH2" + error "-22" if ciphertext_with_tag\sub(1, 6) == "BROKEN" error "EINVAL" + if prefix == "PLAINBAD" + return "EBADMSG" "pt:" .. ciphertext_with_tag .. "|aad:" .. aad __close: => calls.aead_close += 1 @@ -111,6 +124,51 @@ test "lunatik: AES-128-GCM decrypt maps EBADMSG to nil,error", -> assert pt == nil, "pt should be nil on auth failure" assert err == "AES-128-GCM authentication failed (tag mismatch)", "unexpected error: #{tostring err}" +test "lunatik: AES-128-GCM decrypt maps -EBADMSG (pre-6.7 symbolic) to nil,error", -> + key = string.rep "\x11", 16 + nonce = string.rep "\x00", 12 + pt, err = b.aes_128_gcm_decrypt key, nonce, ("AUTHMIN1" .. string.rep("T", 16)), "" + assert pt == nil, "pt should be nil on auth failure" + assert err == "AES-128-GCM authentication failed (tag mismatch)", "unexpected error: #{tostring err}" + +test "lunatik: AES-128-GCM decrypt maps -74 (pre-6.7 numeric) to nil,error", -> + key = string.rep "\x11", 16 + nonce = string.rep "\x00", 12 + pt, err = b.aes_128_gcm_decrypt key, nonce, ("AUTHNUM1" .. string.rep("T", 16)), "" + assert pt == nil, "pt should be nil on auth failure" + assert err == "AES-128-GCM authentication failed (tag mismatch)", "unexpected error: #{tostring err}" + +test "lunatik: AES-128-GCM decrypt does not treat -174 as EBADMSG", -> + key = string.rep "\x11", 16 + nonce = string.rep "\x00", 12 + pt, err = b.aes_128_gcm_decrypt key, nonce, ("AUTHNUM3" .. string.rep("T", 16)), "" + assert pt == nil, "pt should be nil on failure" + assert err ~= "AES-128-GCM authentication failed (tag mismatch)", "unexpected auth error: #{tostring err}" + assert tostring(err)\find("aead(gcm(aes)) decrypt failed", 1, true), "unexpected error: #{tostring err}" + +test "lunatik: AES-128-GCM decrypt does not treat -740 as EBADMSG", -> + key = string.rep "\x11", 16 + nonce = string.rep "\x00", 12 + pt, err = b.aes_128_gcm_decrypt key, nonce, ("AUTHNUM4" .. string.rep("T", 16)), "" + assert pt == nil, "pt should be nil on failure" + assert err ~= "AES-128-GCM authentication failed (tag mismatch)", "unexpected auth error: #{tostring err}" + assert tostring(err)\find("aead(gcm(aes)) decrypt failed", 1, true), "unexpected error: #{tostring err}" + +test "lunatik: AES-128-GCM decrypt maps -22 (numeric EINVAL) to nil,error", -> + key = string.rep "\x11", 16 + nonce = string.rep "\x00", 12 + pt, err = b.aes_128_gcm_decrypt key, nonce, ("NOTAUTH2" .. string.rep("T", 16)), "" + assert pt == nil, "pt should be nil on failure" + assert err ~= "AES-128-GCM authentication failed (tag mismatch)", "unexpected auth error: #{tostring err}" + assert tostring(err)\find("aead(gcm(aes)) decrypt failed", 1, true), "unexpected error: #{tostring err}" + +test "lunatik: AES-128-GCM decrypt returns plaintext equal to EBADMSG verbatim", -> + key = string.rep "\x11", 16 + nonce = string.rep "\x00", 12 + pt, err = b.aes_128_gcm_decrypt key, nonce, ("PLAINBAD" .. string.rep("T", 16)), "" + assert err == nil, "unexpected decrypt error: #{tostring err}" + assert pt == "EBADMSG", "plaintext should be returned verbatim, got: #{tostring pt}" + test "lunatik: AES-128-GCM ciphertext too short returns nil", -> key = string.rep "\x11", 16 nonce = string.rep "\x00", 12 From c49f527a6b2736dab85c6e6ac959e3bcb7c1cf71 Mon Sep 17 00:00:00 2001 From: jperon Date: Tue, 10 Nov 2026 19:49:37 +0100 Subject: [PATCH 27/43] fix(tcp_stream): reject clock_hz without an explicit clock Co-Authored-By: Claude Opus 5.5 --- ipparse/l4/tcp_stream.moon | 12 ++++++++---- ipparse/tests/l4/test_tcp_stream.moon | 5 +++++ 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/ipparse/l4/tcp_stream.moon b/ipparse/l4/tcp_stream.moon index 845a3d8..f55af6b 100644 --- a/ipparse/l4/tcp_stream.moon +++ b/ipparse/l4/tcp_stream.moon @@ -19,7 +19,8 @@ -- feed returns nil until the predicate passes, then auto-clears the session. -- @tparam[opt] table opts Options: `max_buf` (bytes per session, default 1 MiB), -- `max_sessions` (default 1024), `clock` (zero-arg function returning an --- integer instant in fixed units) and `clock_hz` (units per second). +-- integer instant in fixed units) and `clock_hz` (units per second, default +-- 1 when `clock` is given; `clock_hz` without `clock` is an error). -- @treturn table State with :feed, :clear, :reset and :purge methods. -- @raise Error at creation when no clock is available and none is provided. new = (check_complete=(-> true), opts={}) -> @@ -27,15 +28,18 @@ new = (check_complete=(-> true), opts={}) -> max_sessions = opts.max_sessions or 1024 clock = opts.clock clock_hz = opts.clock_hz - if not clock + if clock + clock_hz = clock_hz or 1 + else + error "tcp_stream: opts.clock_hz requires opts.clock" if clock_hz if os and os.time clock = os.time - clock_hz = clock_hz or 1 + clock_hz = 1 else ok, linux = pcall require, "linux" if ok and linux and linux.time clock = linux.time - clock_hz = clock_hz or 1000000000 + clock_hz = 1000000000 else error "tcp_stream: no clock available (pass opts.clock/opts.clock_hz)" clock_hz = 1 if not clock_hz diff --git a/ipparse/tests/l4/test_tcp_stream.moon b/ipparse/tests/l4/test_tcp_stream.moon index bc36fd5..caddeaf 100644 --- a/ipparse/tests/l4/test_tcp_stream.moon +++ b/ipparse/tests/l4/test_tcp_stream.moon @@ -63,4 +63,9 @@ test "tcp_stream: purge drops sessions older than max_age", -> buf = s.feed "old", "def", 0x10, 2 assert buf == nil, "old session should have been purged, got #{buf}" +test "tcp_stream: clock_hz without clock raises", -> + ok, err = pcall tcp_stream.new, nil, clock_hz: 1000 + assert not ok, "expected an error when clock_hz is given without clock" + assert tostring(err)\find("clock_hz"), "error should mention clock_hz, got #{err}" + util.summary "l4/tcp_stream" From 9bedb8fa60a539fda058682a88285a7e19a23f62 Mon Sep 17 00:00:00 2001 From: jperon Date: Tue, 10 Nov 2026 19:52:49 +0100 Subject: [PATCH 28/43] fix(bit_compat): add pure-Lua 32-bit bitwise fallback for Lua 5.1 Co-Authored-By: Claude Opus 5.5 --- ipparse/lib/bit_compat.moon | 3 + ipparse/lib/bit_pure.moon | 94 ++++++++++++++++++++++++++ ipparse/tests/lib/test_bit_compat.moon | 79 ++++++++++++++++++++++ 3 files changed, 176 insertions(+) create mode 100644 ipparse/lib/bit_pure.moon diff --git a/ipparse/lib/bit_compat.moon b/ipparse/lib/bit_compat.moon index 375b34e..ed52829 100644 --- a/ipparse/lib/bit_compat.moon +++ b/ipparse/lib/bit_compat.moon @@ -41,4 +41,7 @@ return normalize(bit) if ok and bit -- Use native operators for Lua 5.3+ (through helper module) ok, bit = pcall require, "ipparse.lib.bit53" return normalize(bit) if ok and bit +-- Pure-Lua arithmetic fallback (Lua 5.1 without LuaJIT's `bit`) +ok, bit = pcall require, "ipparse.lib.bit_pure" +return normalize(bit) if ok and bit error "no bitwise compatibility backend available" diff --git a/ipparse/lib/bit_pure.moon b/ipparse/lib/bit_pure.moon new file mode 100644 index 0000000..0bc61eb --- /dev/null +++ b/ipparse/lib/bit_pure.moon @@ -0,0 +1,94 @@ +-- +-- SPDX-FileCopyrightText: (c) 2024-2026 jperon +-- SPDX-License-Identifier: MIT OR GPL-2.0-only +-- + +--- Pure-Lua bitwise fallback (32-bit, LuaJIT `bit` semantics). +-- Arithmetic only: no FFI, no `bit`/`bit32`, no Lua 5.3 operators, so it +-- loads under Lua 5.1. Results are signed 32-bit values, exactly like +-- LuaJIT's `bit` library (`bor(0x80000000, 0) == -2147483648`). ipparse's +-- real paths rely on that signedness: fields are masked afterwards, and the +-- QUIC VarInt encoder deliberately avoids the negative int32 produced by +-- `bor(0x80000000, value)`. +-- +-- Like `bit` (and `bit53`), `band`, `bor` and `bxor` are variadic and fold +-- left to right; shift counts are masked to 5 bits. +-- +-- @module lib.bit_pure +-- @return table Bitwise operations table + +TWO32 = 0x100000000 +MASK32 = 0xFFFFFFFF +SIGN32 = 0x80000000 + +pow2 = (n) -> + p = 1 + p *= 2 for _ = 1, n + p + +--- Normalizes a number to an unsigned 32-bit value. +to_u32 = (n) -> + math.floor(n % TWO32) + +--- Reinterprets an unsigned 32-bit value as a signed 32-bit value. +to_s32 = (u) -> + return u - TWO32 if u >= SIGN32 + u + +and2 = (a, b) -> + result, weight = 0, 1 + for _ = 1, 32 + result += weight if a % 2 == 1 and b % 2 == 1 + a = math.floor(a / 2) + b = math.floor(b / 2) + weight *= 2 + result + +or2 = (a, b) -> + result, weight = 0, 1 + for _ = 1, 32 + result += weight if a % 2 == 1 or b % 2 == 1 + a = math.floor(a / 2) + b = math.floor(b / 2) + weight *= 2 + result + +xor2 = (a, b) -> + result, weight = 0, 1 + for _ = 1, 32 + result += weight if (a % 2) != (b % 2) + a = math.floor(a / 2) + b = math.floor(b / 2) + weight *= 2 + result + +fold = (op, ...) -> + n = select("#", ...) + r = to_u32((...)) + for i = 2, n + r = op(r, to_u32((select(i, ...)))) + to_s32(r) + +bit = + band: (...) -> fold(and2, ...) + bor: (...) -> fold(or2, ...) + bxor: (...) -> fold(xor2, ...) + bnot: (a) -> to_s32(MASK32 - to_u32(a)) + lshift: (a, n) -> + n %= 32 + u = to_u32(a) + return to_s32(u) if n == 0 + -- Keep the product below 2^32 so it stays exactly representable. + to_s32((u % pow2(32 - n)) * pow2(n)) + rshift: (a, n) -> + n %= 32 + u = to_u32(a) + return to_s32(u) if n == 0 + to_s32(math.floor(u / pow2(n))) + arshift: (a, n) -> + n %= 32 + s = to_s32(to_u32(a)) + return s if n == 0 + to_s32(math.floor(s / pow2(n))) + +return bit diff --git a/ipparse/tests/lib/test_bit_compat.moon b/ipparse/tests/lib/test_bit_compat.moon index a5b531f..2dd45cb 100644 --- a/ipparse/tests/lib/test_bit_compat.moon +++ b/ipparse/tests/lib/test_bit_compat.moon @@ -9,6 +9,9 @@ util = require "ipparse.lib.util" {:test} = util { :band, :bor, :bxor } = require "ipparse.lib.bit_compat" +-- Test the pure-Lua fallback directly on every runtime, so it is exercised +-- even when the host has LuaJIT's `bit` or Lua 5.2+'s `bit32`. +pure = require "ipparse.lib.bit_pure" test "bit_compat: bor with 1 argument", -> assert bor(5) == 5, "bor(5) != 5" @@ -28,4 +31,80 @@ test "bit_compat: band with 3 arguments", -> test "bit_compat: bxor with 3 arguments", -> assert bxor(0xF0, 0x3C, 0xFF) == 51, "bxor(0xF0,0x3C,0xFF) != 51" +-- Expected values below are hardcoded from LuaJIT's `bit` library, which +-- returns SIGNED 32-bit results (e.g. bit.bor(0x80000000, 0) == -2147483648). +-- ipparse relies on that signedness in the QUIC VarInt encoder, which avoids +-- the negative int32 produced by `bor(0x80000000, value)`. + +test "bit_pure: band with 1 argument", -> + assert pure.band(0xFF) == 255, "band(0xFF) != 255" + +test "bit_pure: band with 2 arguments", -> + assert pure.band(-1, 0xFF) == 255, "band(-1,0xFF) != 255" + assert pure.band(0xFFFFFFFF, 0xFFFF) == 65535, "band(0xFFFFFFFF,0xFFFF) != 65535" + assert pure.band(0x80000000, 0x80000000) == -2147483648, "band of bit 31 != -2147483648" + +test "bit_pure: band with 3 arguments", -> + assert pure.band(0xFF, 0x3C, 0x1E) == 28, "band(0xFF,0x3C,0x1E) != 28" + +test "bit_pure: bor with 1 argument", -> + assert pure.bor(5) == 5, "bor(5) != 5" + assert pure.bor(0x80000000) == -2147483648, "bor(0x80000000) != -2147483648" + +test "bit_pure: bor with 2 arguments", -> + assert pure.bor(1, 2) == 3, "bor(1,2) != 3" + assert pure.bor(0x80000000, 0) == -2147483648, "bor(0x80000000,0) != -2147483648" + assert pure.bor(0x80000000, 0xFF) == -2147483393, "bor(0x80000000,0xFF) != -2147483393" + +test "bit_pure: bor with 3 arguments", -> + assert pure.bor(1, 2, 4) == 7, "bor(1,2,4) != 7" + +test "bit_pure: bor with 7 arguments", -> + assert pure.bor(1, 2, 4, 8, 16, 32, 64) == 127, "bor(1,2,4,8,16,32,64) != 127" + +test "bit_pure: bxor with 1 argument", -> + assert pure.bxor(5) == 5, "bxor(5) != 5" + +test "bit_pure: bxor with 2 arguments", -> + assert pure.bxor(0x80000000, 0xFFFFFFFF) == 2147483647, "bxor(0x80000000,0xFFFFFFFF) != 2147483647" + assert pure.bxor(0x80000000, 0x80000000) == 0, "bxor(0x80000000,0x80000000) != 0" + assert pure.bxor(-1, 0xFF) == -256, "bxor(-1,0xFF) != -256" + +test "bit_pure: bxor with 3 arguments", -> + assert pure.bxor(0xF0, 0x3C, 0xFF) == 51, "bxor(0xF0,0x3C,0xFF) != 51" + +test "bit_pure: bnot", -> + assert pure.bnot(0) == -1, "bnot(0) != -1" + assert pure.bnot(0xFFFFFFFF) == 0, "bnot(0xFFFFFFFF) != 0" + assert pure.bnot(-1) == 0, "bnot(-1) != 0" + +test "bit_pure: lshift by 0, 1 and 31 bits", -> + assert pure.lshift(3, 0) == 3, "lshift(3,0) != 3" + assert pure.lshift(1, 1) == 2, "lshift(1,1) != 2" + assert pure.lshift(1, 31) == -2147483648, "lshift(1,31) != -2147483648" + +test "bit_pure: lshift masks the shift count to 5 bits", -> + assert pure.lshift(1, 32) == 1, "lshift(1,32) != 1" + assert pure.lshift(1, 33) == 2, "lshift(1,33) != 2" + +test "bit_pure: rshift by 0, 1 and 31 bits", -> + assert pure.rshift(3, 0) == 3, "rshift(3,0) != 3" + assert pure.rshift(0x80000000, 1) == 1073741824, "rshift(0x80000000,1) != 1073741824" + assert pure.rshift(0x80000000, 31) == 1, "rshift(0x80000000,31) != 1" + assert pure.rshift(-1, 1) == 2147483647, "rshift(-1,1) != 2147483647" + assert pure.rshift(0x80000000, 32) == -2147483648, "rshift(0x80000000,32) != -2147483648" + +test "bit_pure: arshift sign-extends", -> + assert pure.arshift(3, 0) == 3, "arshift(3,0) != 3" + assert pure.arshift(0x80000000, 1) == -1073741824, "arshift(0x80000000,1) != -1073741824" + assert pure.arshift(-1, 1) == -1, "arshift(-1,1) != -1" + assert pure.arshift(0x40000000, 1) == 536870912, "arshift(0x40000000,1) != 536870912" + +test "bit_pure: IPv6 version/traffic class/flow label round-trip", -> + vtf = pure.bor(pure.lshift(6, 28), pure.lshift(0x12, 20), 0x34567) + assert vtf == 1629701479, "vtf != 1629701479" + assert pure.rshift(vtf, 28) == 6, "version != 6" + assert pure.band(pure.rshift(vtf, 20), 0xFF) == 18, "traffic class != 18" + assert pure.band(vtf, 0xFFFFF) == 214375, "flow label != 214375" + util.summary "bit_compat" From d6df7db47b6c89ccf6a12b17bf83fdb8ab18f12b Mon Sep 17 00:00:00 2001 From: jperon Date: Tue, 10 Nov 2026 19:56:49 +0100 Subject: [PATCH 29/43] fix(pack_compat_lib): make FFI optional with pure-Lua fallback Lua 5.1 (PUC) has no ffi module, so requiring it failed before any pack operation could run: ./ipparse/lib/pack_compat_lib.lua:5: module 'ffi' not found Load FFI with pcall and keep the LuaJIT fast paths unchanged. Without FFI: - native endianness defaults to little-endian (no reliable probe); - integers up to 8 bytes use double arithmetic (2^53 precision), with signed l/j/i8 decoded by byte-wise two's complement so small negatives such as -2 stay exact; - f/d/n use a pure-Lua IEEE 754 codec handling zero, -0, infinities, NaN, normals, subnormals and round-to-nearest-even. Also fix the FFI float/double byte extraction, which used a table comprehension that produced an empty table (pack of a double returned an empty string), and decode i8 through int64_t instead of the lossy uint64 path. Co-Authored-By: Claude Opus 5.5 --- ipparse/lib/pack_compat_lib.moon | 299 +++++++++++++++++++----- ipparse/tests/lib/test_pack_compat.moon | 84 +++++++ 2 files changed, 327 insertions(+), 56 deletions(-) diff --git a/ipparse/lib/pack_compat_lib.moon b/ipparse/lib/pack_compat_lib.moon index 497a3a1..91674c9 100644 --- a/ipparse/lib/pack_compat_lib.moon +++ b/ipparse/lib/pack_compat_lib.moon @@ -22,19 +22,29 @@ return string if string.unpack tunpack = unpack or table.unpack -ffi = require "ffi" - -ffi.cdef [[ - void *memcpy(void *dest, const void *src, size_t n); - void *memset(void *s, int c, size_t n); -]] +-- FFI est facultatif : LuaJIT le fournit, Lua 5.1 (PUC) non. Les chemins +-- rapides FFI ci-dessous restent inchangés pour LuaJIT ; les équivalents en +-- Lua pur ne servent que lorsque `require "ffi"` échoue. +ok, ffi = pcall require, "ffi" +HAS_FFI = ok +unless HAS_FFI + ffi = nil + +if HAS_FFI + ffi.cdef [[ + void *memcpy(void *dest, const void *src, size_t n); + void *memset(void *s, int c, size_t n); + ]] -- ─── Détection de l'endianness native ─────────────────────────────────────── -NATIVE_LE = do +-- Sans FFI, aucune sonde fiable n'est disponible : on suppose le +-- petit-boutiste (x86/ARM), qui couvre les runtimes sans FFI visés. +NATIVE_LE = true +if HAS_FFI probe = ffi.new "uint16_t[1]", 0x0102 pb = ffi.cast "uint8_t *", probe - pb[0] == 0x02 -- little-endian si l'octet de poids faible est en premier + NATIVE_LE = pb[0] == 0x02 -- little-endian si l'octet de poids faible est en premier -- ─── Utilitaires ──────────────────────────────────────────────────────────── @@ -50,14 +60,26 @@ read_uint = (s, offset, size, le) -> if size == 4 a, b, c, d = string.byte s, offset + 1, offset + 4 return le and (d * 16777216 + c * 65536 + b * 256 + a) or (a * 16777216 + b * 65536 + c * 256 + d) - val = ffi.cast "uint64_t", 0 - if le - for i = size - 1, 0, -1 - val = val * 256 + string.byte(s, offset + i + 1) + if HAS_FFI + val = ffi.cast "uint64_t", 0 + if le + for i = size - 1, 0, -1 + val = val * 256 + string.byte(s, offset + i + 1) + else + for i = 0, size - 1 + val = val * 256 + string.byte(s, offset + i + 1) + val else - for i = 0, size - 1 - val = val * 256 + string.byte(s, offset + i + 1) - val + -- Arithmétique en double : exacte jusqu'à 2^53, comme `tonumber` sur un + -- uint64_t FFI pour les valeurs plus grandes. + val = 0 + if le + for i = size - 1, 0, -1 + val = val * 256 + string.byte(s, offset + i + 1) + else + for i = 0, size - 1 + val = val * 256 + string.byte(s, offset + i + 1) + val -- Écrit un entier non signé vers un buffer ffi uint8_t* (offset 0-based) write_uint = (buf, offset, val, size, le) -> @@ -73,7 +95,7 @@ write_uint = (buf, offset, val, size, le) -> -- Conversion unsigned → signed (complément à deux) to_signed = (val, size) -> - uval = tonumber(ffi.cast "uint64_t", val) + uval = if HAS_FFI then tonumber(ffi.cast "uint64_t", val) else val bits = size * 8 limit = 2 ^ (bits - 1) if uval >= limit @@ -81,6 +103,26 @@ to_signed = (val, size) -> else uval +-- Lit un entier signé en complément à deux (Lua pur). Pour un octet de poids +-- fort à 1, la magnitude est calculée par inversion + 1 octet par octet, ce +-- qui garde exacts les petits négatifs même au-delà de 2^53 (ex. -2 = ff..fe). +read_int = (s, offset, size, le) -> + u = read_uint s, offset, size, le + limit = 2 ^ (size * 8 - 1) + return u if u < limit + mag = 0 + carry = 1 + for i = 0, size - 1 + idx = if le then offset + i + 1 else offset + size - i + b = 255 - string.byte(s, idx) + carry + if b >= 256 + b -= 256 + carry = 1 + else + carry = 0 + mag += b * 2 ^ (8 * i) + -mag + -- ─── Parseur de format ─────────────────────────────────────────────────────── -- Renvoie un itérateur sur {option, count} depuis une chaîne de format. @@ -185,46 +227,165 @@ parse_format_to_ops = (fmt) -> _fmt_cache[fmt] = ops ops --- ─── Encodage IEEE 754 double ──────────────────────────────────────────────── - -double_to_bytes = (n, le) -> - buf = ffi.new "double[1]", n - b = ffi.cast "uint8_t *", buf - bytes = {b[i] for i = 0, 7} - if not le - bytes = {bytes[8], bytes[7], bytes[6], bytes[5], - bytes[4], bytes[3], bytes[2], bytes[1]} - bytes +-- ─── Encodage IEEE 754 ────────────────────────────────────────────────────── -bytes_to_double = (s, offset, le) -> - buf = ffi.new "uint8_t[8]" - if le - for i = 0, 7 - buf[i] = string.byte s, offset + i + 1 +-- Arrondi au plus proche, pair en cas d'égalité (round-to-nearest-even). +round_even = (x) -> + f = math.floor x + d = x - f + if d > 0.5 or (d == 0.5 and f % 2 == 1) + f + 1 + else + f + +-- Encode un flottant binaire (expbits/fracbits/bias : 11/52/1023 pour un +-- double, 8/23/127 pour un float). Produit les octets de poids fort d'abord, +-- puis les inverse si `le`. Gère zéro, -0, infinis, NaN, normaux et dénormaux. +ieee_encode = (n, expbits, fracbits, bias, le) -> + maxexp = 2 ^ expbits - 1 + sign = 0 + exp = 0 + frac = 0 + if n ~= n -- NaN (n'importe quel NaN convient) + exp = maxexp + frac = 2 ^ (fracbits - 1) + elseif n == math.huge or n == -math.huge + sign = 1 if n < 0 + exp = maxexp + frac = 0 + elseif n == 0 + sign = 1 if 1 / n < 0 + exp = 0 + frac = 0 + else + sign = 1 if n < 0 + a = math.abs n + m, e = math.frexp a + E = e - 1 + bias + if E <= 0 + -- dénormal : fraction = a / 2^(1-bias-fracbits) + frac = round_even math.ldexp m, e + fracbits + bias - 1 + if frac >= 2 ^ fracbits + exp = 1 + frac -= 2 ^ fracbits + else + exp = 0 + else + frac = round_even math.ldexp (m * 2 - 1), fracbits + if frac >= 2 ^ fracbits + frac -= 2 ^ fracbits + E += 1 + if E >= maxexp + exp = maxexp + frac = 0 + else + exp = E + + bytes = {} + if fracbits == 52 + bytes[1] = sign * 128 + math.floor exp / 16 + bytes[2] = (exp % 16) * 16 + math.floor frac / 2 ^ 48 + bytes[3] = math.floor(frac / 2 ^ 40) % 256 + bytes[4] = math.floor(frac / 2 ^ 32) % 256 + bytes[5] = math.floor(frac / 2 ^ 24) % 256 + bytes[6] = math.floor(frac / 2 ^ 16) % 256 + bytes[7] = math.floor(frac / 2 ^ 8) % 256 + bytes[8] = frac % 256 else - for i = 0, 7 - buf[i] = string.byte s, offset + 7 - i + 1 - d = ffi.cast "double *", buf - d[0] - -float_to_bytes = (n, le) -> - buf = ffi.new "float[1]", n - b = ffi.cast "uint8_t *", buf - bytes = {b[i] for i = 0, 3} - if not le - bytes = {bytes[4], bytes[3], bytes[2], bytes[1]} + bytes[1] = sign * 128 + math.floor exp / 2 + bytes[2] = (exp % 2) * 128 + math.floor frac / 2 ^ 16 + bytes[3] = math.floor(frac / 2 ^ 8) % 256 + bytes[4] = frac % 256 + if le + out = {} + for i = 1, #bytes + out[i] = bytes[#bytes + 1 - i] + bytes = out bytes -bytes_to_float = (s, offset, le) -> - buf = ffi.new "uint8_t[4]" +-- Décode un flottant binaire. `bytes[1]` est l'octet de poids fort. +ieee_decode = (s, offset, expbits, fracbits, bias, le) -> + nbytes = (expbits + fracbits + 1) / 8 + error "data string too short" if offset + nbytes > #s + bytes = {} if le - for i = 0, 3 - buf[i] = string.byte s, offset + i + 1 + for i = 1, nbytes + bytes[i] = string.byte s, offset + nbytes - i + 1 + else + for i = 1, nbytes + bytes[i] = string.byte s, offset + i + b0 = bytes[1] + sign = b0 >= 128 and -1 or 1 + exp = 0 + frac = 0 + maxexp = 2 ^ expbits - 1 + denorm_shift = 1 - bias - fracbits + if fracbits == 52 + exp = (b0 % 128) * 16 + math.floor bytes[2] / 16 + frac = (bytes[2] % 16) * 2 ^ 48 + bytes[3] * 2 ^ 40 + bytes[4] * 2 ^ 32 + bytes[5] * 2 ^ 24 + bytes[6] * 2 ^ 16 + bytes[7] * 2 ^ 8 + bytes[8] + else + exp = (b0 % 128) * 2 + math.floor bytes[2] / 128 + frac = (bytes[2] % 128) * 2 ^ 16 + bytes[3] * 2 ^ 8 + bytes[4] + if exp == maxexp + if frac == 0 + sign * math.huge + else + 0 / 0 -- NaN + elseif exp == 0 + sign * math.ldexp frac, denorm_shift else - for i = 0, 3 - buf[i] = string.byte s, offset + 3 - i + 1 - f = ffi.cast "float *", buf - tonumber f[0] + sign * math.ldexp (1 + frac / 2 ^ fracbits), exp - bias + +-- Déclarés ici pour rester visibles hors des blocs `if HAS_FFI` ci-dessous. +double_to_bytes = nil +bytes_to_double = nil +float_to_bytes = nil +bytes_to_float = nil + +if HAS_FFI + double_to_bytes = (n, le) -> + buf = ffi.new "double[1]", n + b = ffi.cast "uint8_t *", buf + bytes = [b[i] for i = 0, 7] + if not le + bytes = {bytes[8], bytes[7], bytes[6], bytes[5], + bytes[4], bytes[3], bytes[2], bytes[1]} + bytes + + bytes_to_double = (s, offset, le) -> + buf = ffi.new "uint8_t[8]" + if le + for i = 0, 7 + buf[i] = string.byte s, offset + i + 1 + else + for i = 0, 7 + buf[i] = string.byte s, offset + 7 - i + 1 + d = ffi.cast "double *", buf + d[0] + + float_to_bytes = (n, le) -> + buf = ffi.new "float[1]", n + b = ffi.cast "uint8_t *", buf + bytes = [b[i] for i = 0, 3] + if not le + bytes = {bytes[4], bytes[3], bytes[2], bytes[1]} + bytes + + bytes_to_float = (s, offset, le) -> + buf = ffi.new "uint8_t[4]" + if le + for i = 0, 3 + buf[i] = string.byte s, offset + i + 1 + else + for i = 0, 3 + buf[i] = string.byte s, offset + 3 - i + 1 + f = ffi.cast "float *", buf + tonumber f[0] +else + double_to_bytes = (n, le) -> ieee_encode n, 11, 52, 1023, le + bytes_to_double = (s, offset, le) -> ieee_decode s, offset, 11, 52, 1023, le + float_to_bytes = (n, le) -> ieee_encode n, 8, 23, 127, le + bytes_to_float = (s, offset, le) -> ieee_decode s, offset, 8, 23, 127, le -- ─── string.pack ───────────────────────────────────────────────────────────── @@ -334,6 +495,23 @@ make_uint = (sz, le) -> buf[n + k] = u % 256 u = math.floor(u / 256) n + sz + elseif not HAS_FFI + -- 7-8 octets : calcul octet par octet pour rester exact sur les petits + -- négatifs (2^64 - 2 n'est pas représentable en double). + if le + (buf, n, v) -> + p = 1 + for k = 1, sz + buf[n + k] = math.floor(v / p) % 256 + p *= 256 + n + sz + else + (buf, n, v) -> + p = 256 ^ (sz - 1) + for k = 1, sz + buf[n + k] = math.floor(v / p) % 256 + p /= 256 + n + sz else if le (buf, n, v) -> @@ -533,9 +711,14 @@ unpack = (fmt, s, pos) -> when "i" -- intN signé sz = count or 4 - uv = read_uint s, pos, sz, le n += 1 - _results[n] = to_signed uv, sz + if not HAS_FFI + _results[n] = read_int s, pos, sz, le + elseif sz == 8 + _results[n] = tonumber(ffi.cast "int64_t", read_uint(s, pos, 8, le)) + else + uv = read_uint s, pos, sz, le + _results[n] = to_signed uv, sz pos += sz when "I" -- uintN non signé @@ -545,9 +728,11 @@ unpack = (fmt, s, pos) -> pos += sz when "l" -- int64 signé - uv = read_uint s, pos, 8, le n += 1 - _results[n] = tonumber ffi.cast "int64_t", uv + if HAS_FFI + _results[n] = tonumber(ffi.cast "int64_t", read_uint(s, pos, 8, le)) + else + _results[n] = read_int s, pos, 8, le pos += 8 when "L", "J", "T" -- uint64 @@ -556,9 +741,11 @@ unpack = (fmt, s, pos) -> pos += 8 when "j" -- lua_Integer (int64 signé) - uv = read_uint s, pos, 8, le n += 1 - _results[n] = tonumber ffi.cast "int64_t", uv + if HAS_FFI + _results[n] = tonumber(ffi.cast "int64_t", read_uint(s, pos, 8, le)) + else + _results[n] = read_int s, pos, 8, le pos += 8 when "f" -- float 32 diff --git a/ipparse/tests/lib/test_pack_compat.moon b/ipparse/tests/lib/test_pack_compat.moon index 52e1793..2c215f2 100644 --- a/ipparse/tests/lib/test_pack_compat.moon +++ b/ipparse/tests/lib/test_pack_compat.moon @@ -122,4 +122,88 @@ test "pack_compat: empty format", -> got = pack "" assert got == "", "got: #{got}" +-- ─── Floats, doubles and 64-bit signed values ──────────────────────────────── + +unpack = pack_compat.unpack or string.unpack + +-- 1.5 is exactly 0x3ff8000000000000; little-endian byte order. +test "pack_compat: + got = pack "d -2.5 bytes", -> + got = pack ">d", -2.5 + expected = from_hex "c004000000000000" + assert got == expected, "got: #{got}\nexp: #{expected}" + +-- 1.5 is exactly 0x3fc00000 as a 32-bit float; little-endian byte order. +test "pack_compat: + got = pack "i8 negative bytes", -> + got = pack ">i8", -2 + expected = from_hex "fffffffffffffffe" + assert got == expected, "got: #{got}\nexp: #{expected}" + +test "pack_compat: + got = pack " + got = pack "=I4", 0x01020304 + expected = from_hex "04030201" + assert got == expected, "got: #{got}\nexp: #{expected}" + +test "pack_compat: + v, pos = unpack "d round-trip", -> + v, pos = unpack ">d", pack(">d", 123.456) + assert v == 123.456, "got: #{v}" + assert pos == 9, "pos: #{pos}" + +test "pack_compat: + v, pos = unpack "i8 negative round-trip", -> + v, pos = unpack ">i8", pack(">i8", -2) + assert v == -2, "got: #{v}" + assert pos == 9, "pos: #{pos}" + +-- IEEE 754 special values: infinities, signed zero, subnormal and NaN. +test "pack_compat: + assert unpack(" + got = pack " + v = 2 ^ -1074 + got = pack " + nan = math.huge - math.huge + v = unpack " Date: Tue, 10 Nov 2026 20:01:27 +0100 Subject: [PATCH 30/43] test: use Lua 5.1-compatible decimal string escapes Lua 5.1 does not support hexadecimal escapes in string literals, so test data such as "\x01hello\xff" was read literally as x01helloxff. This produced failures across most modules under Lua 5.1, for example: FAIL init: hex2bin converts hex to bytes FAIL l2/ethernet: mac2s converts binary to string expected 'aa:bb:cc:dd:ee:ff', got '78:61:61:78:62:62' FAIL l3/ip4: ip42s converts 4 bytes to dotted decimal expected '192.168.1.1', got '120.99.48.120' FAIL quic/varint: parse 1-byte value 0 ... got 14384 Rewrite every \xNN escape in the test sources as the equivalent \DDD decimal escape, which is valid in Lua 5.1 through 5.5 and LuaJIT. Co-Authored-By: Claude Opus 5.5 --- ipparse/tests/l2/test_ethernet.moon | 36 ++++++------- ipparse/tests/l3/test_checksum.moon | 10 ++-- ipparse/tests/l3/test_ip4.moon | 10 ++-- ipparse/tests/l3/test_ip6.moon | 4 +- ipparse/tests/l4/quic/test_frames.moon | 50 +++++++++--------- ipparse/tests/l4/quic/test_header.moon | 6 +-- ipparse/tests/l4/quic/test_integration.moon | 16 +++--- ipparse/tests/l4/quic/test_protection.moon | 12 ++--- ipparse/tests/l4/quic/test_varint.moon | 26 +++++----- ipparse/tests/l7/quic/test_sni.moon | 8 +-- ipparse/tests/l7/test_dns.moon | 2 +- ipparse/tests/l7/tls/test_handshake.moon | 6 +-- .../tests/lib/crypto/test_ffi_mbedtls.moon | 38 +++++++------- .../tests/lib/crypto/test_ffi_wolfssl.moon | 28 +++++----- ipparse/tests/lib/crypto/test_lunatik.moon | 52 +++++++++---------- .../tests/lib/crypto/test_lunatik_kernel.moon | 12 ++--- ipparse/tests/lib/test_sha.moon | 2 +- ipparse/tests/test_init.moon | 6 +-- ipparse/tests/test_malformed.moon | 16 +++--- 19 files changed, 170 insertions(+), 170 deletions(-) diff --git a/ipparse/tests/l2/test_ethernet.moon b/ipparse/tests/l2/test_ethernet.moon index 4a70698..4eeb559 100644 --- a/ipparse/tests/l2/test_ethernet.moon +++ b/ipparse/tests/l2/test_ethernet.moon @@ -9,27 +9,27 @@ eth = require"ipparse.l2.ethernet" sp = require("ipparse.lib.pack_compat").pack -- Build a 14-byte Ethernet header: dst=aa:bb:cc:dd:ee:ff, src=00:11:22:33:44:55, proto=0x0800 -eth_raw = sp("c6c6>H", "\xaa\xbb\xcc\xdd\xee\xff", "\x00\x11\x22\x33\x44\x55", 0x0800) +eth_raw = sp("c6c6>H", "\170\187\204\221\238\255", "\000\017\034\051\068\085", 0x0800) test "mac2s converts binary to string", -> - result = eth.mac2s "\xaa\xbb\xcc\xdd\xee\xff" + result = eth.mac2s "\170\187\204\221\238\255" assert result == "aa:bb:cc:dd:ee:ff", "expected 'aa:bb:cc:dd:ee:ff', got '#{result}'" test "s2mac converts string to binary", -> result = eth.s2mac "aa:bb:cc:dd:ee:ff" - assert result == "\xaa\xbb\xcc\xdd\xee\xff", "s2mac failed" + assert result == "\170\187\204\221\238\255", "s2mac failed" test "mac2s/s2mac round-trip", -> - original = "\x12\x34\x56\x78\x9a\xbc" + original = "\018\052\086\120\154\188" assert eth.s2mac(eth.mac2s(original)) == original, "mac2s/s2mac round-trip failed" test "parse extracts dst MAC", -> frame, next_off = eth.parse eth_raw, 1 - assert frame.dst == "\xaa\xbb\xcc\xdd\xee\xff", "dst MAC mismatch" + assert frame.dst == "\170\187\204\221\238\255", "dst MAC mismatch" test "parse extracts src MAC", -> frame, next_off = eth.parse eth_raw, 1 - assert frame.src == "\x00\x11\x22\x33\x44\x55", "src MAC mismatch" + assert frame.src == "\000\017\034\051\068\085", "src MAC mismatch" test "parse extracts protocol", -> frame, next_off = eth.parse eth_raw, 1 @@ -54,18 +54,18 @@ test "proto IP6 == 0x86DD", -> test "new + tostring round-trip", -> frame = eth.new { - dst: "\xaa\xbb\xcc\xdd\xee\xff" - src: "\x00\x11\x22\x33\x44\x55" + dst: "\170\187\204\221\238\255" + src: "\000\017\034\051\068\085" protocol: 0x0800 } raw = tostring frame parsed, _ = eth.parse raw, 1 - assert parsed.dst == "\xaa\xbb\xcc\xdd\xee\xff", "round-trip dst mismatch" - assert parsed.src == "\x00\x11\x22\x33\x44\x55", "round-trip src mismatch" + assert parsed.dst == "\170\187\204\221\238\255", "round-trip dst mismatch" + assert parsed.src == "\000\017\034\051\068\085", "round-trip src mismatch" assert parsed.protocol == 0x0800, "round-trip protocol mismatch" -- Build an 18-byte 802.1Q-tagged frame: same MACs, VLAN 6, inner proto=0x0800 -eth_vlan_raw = sp("c6c6>HHH", "\xaa\xbb\xcc\xdd\xee\xff", "\x00\x11\x22\x33\x44\x55", 0x8100, 6, 0x0800) +eth_vlan_raw = sp("c6c6>HHH", "\170\187\204\221\238\255", "\000\017\034\051\068\085", 0x8100, 6, 0x0800) test "parse detects 802.1Q tag and extracts vlan", -> frame, next_off = eth.parse eth_vlan_raw, 1 @@ -86,8 +86,8 @@ test "parse untagged frame: vlan is nil", -> test "new with vlan: tostring produces 802.1Q frame", -> frame = eth.new { - dst: "\xaa\xbb\xcc\xdd\xee\xff" - src: "\x00\x11\x22\x33\x44\x55" + dst: "\170\187\204\221\238\255" + src: "\000\017\034\051\068\085" protocol: 0x0800 vlan: 6 } @@ -95,8 +95,8 @@ test "new with vlan: tostring produces 802.1Q frame", -> test "new with vlan=0: tostring produces plain frame (no tag)", -> frame = eth.new { - dst: "\xaa\xbb\xcc\xdd\xee\xff" - src: "\x00\x11\x22\x33\x44\x55" + dst: "\170\187\204\221\238\255" + src: "\000\017\034\051\068\085" protocol: 0x0800 vlan: 0 } @@ -104,14 +104,14 @@ test "new with vlan=0: tostring produces plain frame (no tag)", -> test "new + tostring round-trip with vlan", -> frame = eth.new { - dst: "\xaa\xbb\xcc\xdd\xee\xff" - src: "\x00\x11\x22\x33\x44\x55" + dst: "\170\187\204\221\238\255" + src: "\000\017\034\051\068\085" protocol: 0x0800 vlan: 42 } parsed, _ = eth.parse tostring(frame), 1 assert parsed.vlan == 42, "round-trip vlan mismatch: got #{parsed.vlan}" assert parsed.protocol == 0x0800, "round-trip protocol mismatch" - assert parsed.dst == "\xaa\xbb\xcc\xdd\xee\xff", "round-trip dst mismatch" + assert parsed.dst == "\170\187\204\221\238\255", "round-trip dst mismatch" util.summary "l2/ethernet" diff --git a/ipparse/tests/l3/test_checksum.moon b/ipparse/tests/l3/test_checksum.moon index 912e381..3d45f36 100644 --- a/ipparse/tests/l3/test_checksum.moon +++ b/ipparse/tests/l3/test_checksum.moon @@ -10,11 +10,11 @@ pack: sp = require "ipparse.lib.pack_compat" ip4 = require"ipparse.l3.ip4" test "checksum of 0xffff is 0", -> - result = checksum "\xff\xff" + result = checksum "\255\255" assert result == 0, "checksum(0xffff) should be 0, got #{result}" test "checksum of 0x0000 is 0xffff", -> - result = checksum "\x00\x00" + result = checksum "\000\000" assert result == 0xffff, "checksum(0x0000) should be 0xffff, got #{result}" test "checksum of packed header is 0", -> @@ -32,9 +32,9 @@ test "checksum of packed header is 0", -> assert result == 0, "checksum of valid IP header should be 0, got #{result}" test "checksum odd-length pads with zero", -> - -- Odd-length input should be padded; "\xff" = "\xff\x00" padded → checksum = 0xff00 → bnot = 0x00ff - result = checksum "\xff" - assert result == 0x00ff, "checksum(\\xff) should be 0x00ff, got #{result}" + -- Odd-length input should be padded; "\255" = "\255\000" padded → checksum = 0xff00 → bnot = 0x00ff + result = checksum "\255" + assert result == 0x00ff, "checksum(\\255) should be 0x00ff, got #{result}" test "checksum6 matches IPv6 pseudo-header formula", -> src = string.char 0x20, 0x01, 0x0d, 0xb8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 diff --git a/ipparse/tests/l3/test_ip4.moon b/ipparse/tests/l3/test_ip4.moon index 39ccd9a..87f6821 100644 --- a/ipparse/tests/l3/test_ip4.moon +++ b/ipparse/tests/l3/test_ip4.moon @@ -10,26 +10,26 @@ ip4 = require"ipparse.l3.ip4" has_data, fragmented_ip4 = pcall require, "ipparse.l3.fragmented_ip4" test "ip42s converts 4 bytes to dotted decimal", -> - result = ip4.ip42s "\xc0\xa8\x01\x01" + result = ip4.ip42s "\192\168\001\001" assert result == "192.168.1.1", "expected '192.168.1.1', got '#{result}'" test "s2ip4 converts dotted decimal to 4 bytes", -> result = ip4.s2ip4 "192.168.1.1" - assert result == "\xc0\xa8\x01\x01", "s2ip4 failed" + assert result == "\192\168\001\001", "s2ip4 failed" test "ip42s/s2ip4 round-trip", -> - original = "\x0a\x00\x00\x01" + original = "\010\000\000\001" assert ip4.s2ip4(ip4.ip42s(original)) == original, "ip42s/s2ip4 round-trip failed" test "net42s converts 5 bytes to CIDR notation", -> -- mask=24, then 192.168.1.0 - bin = "\x18\xc0\xa8\x01\x00" + bin = "\024\192\168\001\000" result = ip4.net42s bin assert result == "192.168.1.0/24", "expected '192.168.1.0/24', got '#{result}'" test "s2net4 converts CIDR to 5 bytes", -> result = ip4.s2net4 "192.168.1.0/24" - assert result == "\x18\xc0\xa8\x01\x00", "s2net4 failed" + assert result == "\024\192\168\001\000", "s2net4 failed" test "net42s/s2net4 round-trip", -> original = "10.0.0.0/8" diff --git a/ipparse/tests/l3/test_ip6.moon b/ipparse/tests/l3/test_ip6.moon index 608adf6..de09cc6 100644 --- a/ipparse/tests/l3/test_ip6.moon +++ b/ipparse/tests/l3/test_ip6.moon @@ -14,7 +14,7 @@ test "ip62s compresses zero runs per RFC 5952", -> test "s2ip6 converts address to 16 bytes", -> result = ip6.s2ip6 "0:0:0:0:0:0:0:1" - expected = "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01" + expected = "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\001" assert result == expected, "s2ip6(0:0:0:0:0:0:0:1) mismatch" test "s2ip6 full address", -> @@ -22,7 +22,7 @@ test "s2ip6 full address", -> assert #result == 16, "s2ip6 should return 16 bytes, got #{#result}" test "ip62s/s2ip6 round-trip preserves bytes", -> - original = "\x20\x01\x0d\xb8\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01" + original = "\032\001\013\184\000\000\000\000\000\000\000\000\000\000\000\001" s = ip6.ip62s original assert s == "2001:db8::1", "expected '2001:db8::1', got '#{s}'" converted = ip6.s2ip6 s diff --git a/ipparse/tests/l4/quic/test_frames.moon b/ipparse/tests/l4/quic/test_frames.moon index c0f7aa9..dd30bb0 100644 --- a/ipparse/tests/l4/quic/test_frames.moon +++ b/ipparse/tests/l4/quic/test_frames.moon @@ -12,17 +12,17 @@ util = require "ipparse.lib.util" -- PADDING frame (0x00) test "frame: PADDING parsed", -> - f, off = parse_frame "\x00", 1 + f, off = parse_frame "\000", 1 assert f.type == 0x00 and f.name == "PADDING", "expected PADDING" -- PING frame (0x01) test "frame: PING parsed", -> - f, off = parse_frame "\x01", 1 + f, off = parse_frame "\001", 1 assert f.type == 0x01 and f.name == "PING", "expected PING" -- ACK frame (0x02) — minimal: largest_acked=0, delay=0, count=0, first=0 test "frame: ACK minimal parsed", -> - data = "\x02\x00\x00\x00\x00" -- type + 4 VarInts of value 0 + data = "\002\000\000\000\000" -- type + 4 VarInts of value 0 f, _ = parse_frame data, 1 assert f.type == 0x02 and f.name == "ACK" assert f.largest_acked == 0 @@ -31,7 +31,7 @@ test "frame: ACK minimal parsed", -> test "frame: CRYPTO frame parsed", -> payload = "hello" -- type(0x06) + offset(0) + length(5) + data - data = "\x06" .. encode_varint(0) .. encode_varint(#payload) .. payload + data = "\006" .. encode_varint(0) .. encode_varint(#payload) .. payload f, _ = parse_frame data, 1 assert f.type == 0x06 and f.name == "CRYPTO" assert f.data == payload, "crypto data mismatch" @@ -41,7 +41,7 @@ test "frame: CRYPTO frame parsed", -> test "frame: STREAM frame parsed", -> payload = "world" -- 0x0E: STREAM with offset(0x04) and length(0x02) bits set - data = "\x0E" .. encode_varint(1) .. encode_varint(0) .. encode_varint(#payload) .. payload + data = "\014" .. encode_varint(1) .. encode_varint(0) .. encode_varint(#payload) .. payload f, _ = parse_frame data, 1 assert f.type == 0x0E and f.name == "STREAM" assert f.id == 1 @@ -49,51 +49,51 @@ test "frame: STREAM frame parsed", -> -- RESET_STREAM (0x04) test "frame: RESET_STREAM parsed", -> - data = "\x04" .. encode_varint(3) .. encode_varint(0) .. encode_varint(100) + data = "\004" .. encode_varint(3) .. encode_varint(0) .. encode_varint(100) f, _ = parse_frame data, 1 assert f.type == 0x04 and f.name == "RESET_STREAM" assert f.stream_id == 3 and f.final_size == 100 -- STOP_SENDING (0x05) test "frame: STOP_SENDING parsed", -> - data = "\x05" .. encode_varint(7) .. encode_varint(42) + data = "\005" .. encode_varint(7) .. encode_varint(42) f, _ = parse_frame data, 1 assert f.type == 0x05 and f.name == "STOP_SENDING" assert f.stream_id == 7 and f.app_error_code == 42 -- MAX_DATA (0x10) test "frame: MAX_DATA parsed", -> - data = "\x10" .. encode_varint(1000) + data = "\016" .. encode_varint(1000) f, _ = parse_frame data, 1 assert f.type == 0x10 and f.name == "MAX_DATA" assert f.maximum_data == 1000 -- MAX_STREAM_DATA (0x11) test "frame: MAX_STREAM_DATA parsed", -> - data = "\x11" .. encode_varint(2) .. encode_varint(2000) + data = "\017" .. encode_varint(2) .. encode_varint(2000) f, _ = parse_frame data, 1 assert f.type == 0x11 and f.name == "MAX_STREAM_DATA" assert f.stream_id == 2 and f.maximum_stream_data == 2000 -- MAX_STREAMS bidi (0x12) test "frame: MAX_STREAMS_BIDI parsed", -> - data = "\x12" .. encode_varint(10) + data = "\018" .. encode_varint(10) f, _ = parse_frame data, 1 assert f.type == 0x12 and f.name == "MAX_STREAMS_BIDI" assert f.maximum_streams == 10 -- DATA_BLOCKED (0x14) test "frame: DATA_BLOCKED parsed", -> - data = "\x14" .. encode_varint(500) + data = "\020" .. encode_varint(500) f, _ = parse_frame data, 1 assert f.type == 0x14 and f.name == "DATA_BLOCKED" assert f.maximum_data == 500 -- NEW_CONNECTION_ID (0x18) test "frame: NEW_CONNECTION_ID parsed", -> - cid = "\xAA\xBB\xCC\xDD\xEE\xFF\x11\x22" - rst = string.rep("\x00", 16) -- 16-byte stateless reset token - data = "\x18" .. encode_varint(1) .. encode_varint(0) .. "\x08" .. cid .. rst + cid = "\170\187\204\221\238\255\017\034" + rst = string.rep("\000", 16) -- 16-byte stateless reset token + data = "\024" .. encode_varint(1) .. encode_varint(0) .. "\008" .. cid .. rst f, _ = parse_frame data, 1 assert f.type == 0x18 and f.name == "NEW_CONNECTION_ID" assert f.sequence_number == 1 @@ -101,51 +101,51 @@ test "frame: NEW_CONNECTION_ID parsed", -> -- RETIRE_CONNECTION_ID (0x19) test "frame: RETIRE_CONNECTION_ID parsed", -> - data = "\x19" .. encode_varint(5) + data = "\025" .. encode_varint(5) f, _ = parse_frame data, 1 assert f.type == 0x19 and f.name == "RETIRE_CONNECTION_ID" assert f.sequence_number == 5 -- PATH_CHALLENGE (0x1a) — 8 bytes of data test "frame: PATH_CHALLENGE parsed", -> - data = "\x1a" .. string.rep("\xAB", 8) + data = "\026" .. string.rep("\171", 8) f, _ = parse_frame data, 1 assert f.type == 0x1a and f.name == "PATH_CHALLENGE" assert #f.data == 8 -- PATH_RESPONSE (0x1b) test "frame: PATH_RESPONSE parsed", -> - data = "\x1b" .. string.rep("\xCD", 8) + data = "\027" .. string.rep("\205", 8) f, _ = parse_frame data, 1 assert f.type == 0x1b and f.name == "PATH_RESPONSE" -- CONNECTION_CLOSE (0x1c) test "frame: CONNECTION_CLOSE parsed", -> reason = "test error" - data = "\x1c" .. encode_varint(0) .. encode_varint(0) .. encode_varint(#reason) .. reason + data = "\028" .. encode_varint(0) .. encode_varint(0) .. encode_varint(#reason) .. reason f, _ = parse_frame data, 1 assert f.type == 0x1c and f.name == "CONNECTION_CLOSE" assert f.reason_phrase == reason -- HANDSHAKE_DONE (0x1e) — no fields test "frame: HANDSHAKE_DONE parsed", -> - f, _ = parse_frame "\x1e", 1 + f, _ = parse_frame "\030", 1 assert f.type == 0x1e and f.name == "HANDSHAKE_DONE" test "frame: CRYPTO truncated payload returns nil", -> - data = "\x06\x00\x05\xaa" -- offset=0, len=5, only 1 byte payload + data = "\006\000\005\170" -- offset=0, len=5, only 1 byte payload f, _, err = parse_frame data, 1 assert f == nil, "expected parse failure" assert err and err\match("CRYPTO"), "expected CRYPTO error, got #{err}" test "frame: PATH_CHALLENGE truncated returns nil", -> - f, _, err = parse_frame "\x1a\xaa", 1 + f, _, err = parse_frame "\026\170", 1 assert f == nil, "expected parse failure" assert err and err\match("PATH_CHALLENGE"), "expected PATH_CHALLENGE error, got #{err}" -- iter_frames: two frames test "frame: iter_frames over two frames", -> - data = "\x01\x01" -- PING, PING + data = "\001\001" -- PING, PING count = 0 for f in iter_frames data count += 1 @@ -153,12 +153,12 @@ test "frame: iter_frames over two frames", -> -- validate_frames test "frame: validate_frames succeeds on valid data", -> - data = "\x01\x01" + data = "\001\001" ok, msg = validate_frames data assert ok == true, "expected valid, got: #{msg}" test "frame: validate_frames fails on malformed data", -> - data = "\x06\x00\x05\xaa" + data = "\006\000\005\170" ok, msg = validate_frames data assert ok == false, "expected invalid data" assert msg and msg\match("CRYPTO"), "expected CRYPTO validation error, got #{msg}" @@ -169,7 +169,7 @@ test "frame: UNKNOWN frame does not get stuck", -> -- Actually the remaining frames we haven't mapped: -- Let's use a reserved/unknown type like 0x20 -- encode_varint(0x20) = 0x20 (< 64, 1-byte) - data = "\x20\x01" -- unknown type, then PING + data = "\032\001" -- unknown type, then PING f1, off1 = parse_frame data, 1 assert f1.name == "UNKNOWN", "expected UNKNOWN, got #{f1.name}" f2, off2 = parse_frame data, off1 diff --git a/ipparse/tests/l4/quic/test_header.moon b/ipparse/tests/l4/quic/test_header.moon index 9428c5b..6debbe2 100644 --- a/ipparse/tests/l4/quic/test_header.moon +++ b/ipparse/tests/l4/quic/test_header.moon @@ -20,7 +20,7 @@ v1 = require "ipparse.l4.quic.v1" -- token_length: 0 (VarInt 1-byte) -- length: 1 (VarInt 1-byte, represents pn(1) + empty payload) -- packet number: 0x00 (1 byte, unprotected for test) -dcid = "\x01\x02\x03\x04\x05\x06\x07\x08" +dcid = "\001\002\003\004\005\006\007\008" build_initial_pkt = -> sp(">B I4 s1 s1 B B B", 0xC0, 1, dcid, "", 0, 1, 0) @@ -69,7 +69,7 @@ test "header: pn_off points past fixed fields", -> -- Short header: HEADER_FORM=0, FIXED_BIT=1 test "header: parse short header", -> - pkt = "\x40" .. dcid -- byte1=0x40 (short), then dst_connection_id + pkt = "\064" .. dcid -- byte1=0x40 (short), then dst_connection_id h, _ = quic.parse pkt, 1, dcid assert h.long_header == nil or h.long_header == false, "expected short header" @@ -97,7 +97,7 @@ test "header: split_datagrams splits coalesced Initial packets", -> assert datagrams[2].data == p2, "second datagram mismatch" test "header: split_datagrams rejects short header payload", -> - short_pkt = "\x40" .. dcid + short_pkt = "\064" .. dcid datagrams, err = quic.split_datagrams short_pkt, 1, dcid assert datagrams == nil, "expected split failure on short header" assert err and err\find("short header"), "expected short-header error, got #{err}" diff --git a/ipparse/tests/l4/quic/test_integration.moon b/ipparse/tests/l4/quic/test_integration.moon index 3cfbc97..ba5e935 100644 --- a/ipparse/tests/l4/quic/test_integration.moon +++ b/ipparse/tests/l4/quic/test_integration.moon @@ -77,16 +77,16 @@ ip_total = 20 + udp_len -- IPv4 header: placeholder checksum ip_no_csum = sp(">BBHHHBBH", 0x45, 0, ip_total, 0x1234, 0x4000, 64, 17, 0) .. - "\xc0\xa8\x01\x01" .. -- src 192.168.1.1 - "\x01\x01\x01\x01" -- dst 1.1.1.1 + "\192\168\001\001" .. -- src 192.168.1.1 + "\001\001\001\001" -- dst 1.1.1.1 ip_csum = checksum ip_no_csum ip_hdr = sp(">BBHHHBBH", 0x45, 0, ip_total, 0x1234, 0x4000, 64, 17, ip_csum) .. - "\xc0\xa8\x01\x01" .. "\x01\x01\x01\x01" + "\192\168\001\001" .. "\001\001\001\001" -- Ethernet header -eth_hdr = "\xaa\xbb\xcc\xdd\xee\xff" .. -- dst mac - "\x11\x22\x33\x44\x55\x66" .. -- src mac - "\x08\x00" -- ethertype IPv4 +eth_hdr = "\170\187\204\221\238\255" .. -- dst mac + "\017\034\051\068\085\102" .. -- src mac + "\008\000" -- ethertype IPv4 -- UDP header udp_hdr = sp ">HHHH", 1234, 443, udp_len, 0 @@ -110,10 +110,10 @@ pn_off_quic = q.pn_off - off3 + 1 -- 1-based offset within the QUIC packet -- ── Layer 2 ────────────────────────────────────────────────────────────────── test "integration: L2 dst MAC = aa:bb:cc:dd:ee:ff", -> - assert e.dst == "\xaa\xbb\xcc\xdd\xee\xff", "dst MAC mismatch" + assert e.dst == "\170\187\204\221\238\255", "dst MAC mismatch" test "integration: L2 src MAC = 11:22:33:44:55:66", -> - assert e.src == "\x11\x22\x33\x44\x55\x66", "src MAC mismatch" + assert e.src == "\017\034\051\068\085\102", "src MAC mismatch" test "integration: L2 EtherType = 0x0800 (IPv4)", -> assert e.protocol == 0x0800, "expected 0x0800, got #{string.format('0x%04x', e.protocol)}" diff --git a/ipparse/tests/l4/quic/test_protection.moon b/ipparse/tests/l4/quic/test_protection.moon index 9723e46..7a8ca9a 100644 --- a/ipparse/tests/l4/quic/test_protection.moon +++ b/ipparse/tests/l4/quic/test_protection.moon @@ -57,13 +57,13 @@ test "protection: recover_packet_number lower half wraps back", -> test "protection: sample_from_packet 16 bytes at correct position", -> -- pkt = 40 zeros, enc_off = 20 -- sample starts at 20+4=24, length 16 → bytes 24..39 - pkt = (string.rep "\x00", 20) .. (string.rep "\xFF", 20) + pkt = (string.rep "\000", 20) .. (string.rep "\255", 20) sample = prot.sample_from_packet pkt, 20 assert #sample == 16, "sample must be 16 bytes" -- bytes 24..39 of the packet: all 0xFF (we have 20 zeros then 20 FFs) -- wait: pkt[1..20]=0x00, pkt[21..40]=0xFF -- sample = pkt[24..39] = all 0xFF - assert sample == string.rep("\xFF", 16), "sample bytes mismatch" + assert sample == string.rep("\255", 16), "sample bytes mismatch" -- Backend-dependent tests (skip if libcrypto not available) ok_backend, backend = pcall require, "ipparse.lib.crypto.backend.ffi_openssl" @@ -71,8 +71,8 @@ ok_backend, backend = pcall require, "ipparse.lib.crypto.backend.ffi_openssl" if ok_backend -- AES-128-GCM round-trip test "protection: AES-128-GCM encrypt/decrypt round-trip", -> - key = string.rep "\x01", 16 - iv = string.rep "\x02", 12 + key = string.rep "\001", 16 + iv = string.rep "\002", 12 plain = "Hello, QUIC!" aad = "header" ct = backend.aes_128_gcm_encrypt key, iv, plain, aad @@ -80,8 +80,8 @@ if ok_backend assert pt == plain, "round-trip failed: #{err}" test "protection: AES-128-GCM decrypt fails on bad tag", -> - key = string.rep "\x01", 16 - iv = string.rep "\x02", 12 + key = string.rep "\001", 16 + iv = string.rep "\002", 12 ct = backend.aes_128_gcm_encrypt key, iv, "plaintext", "" -- corrupt last byte (auth tag) bad_ct = ct\sub(1, #ct - 1) .. string.char((string.byte(ct, #ct) + 1) % 256) diff --git a/ipparse/tests/l4/quic/test_varint.moon b/ipparse/tests/l4/quic/test_varint.moon index 289c814..f187663 100644 --- a/ipparse/tests/l4/quic/test_varint.moon +++ b/ipparse/tests/l4/quic/test_varint.moon @@ -12,39 +12,39 @@ util = require "ipparse.lib.util" -- parse_varint: 1-byte (00xxxxxx) test "varint: parse 1-byte value 0", -> - v, off = parse_varint "\x00", 1 + v, off = parse_varint "\000", 1 assert v == 0 and off == 2, "expected 0, got #{v}" test "varint: parse 1-byte value 63", -> - v, off = parse_varint "\x3F", 1 + v, off = parse_varint "\063", 1 assert v == 63 and off == 2, "expected 63, got #{v}" -- parse_varint: 2-byte (01xxxxxx) test "varint: parse 2-byte value 64", -> - v, off = parse_varint "\x40\x40", 1 + v, off = parse_varint "\064\064", 1 assert v == 64 and off == 3, "expected 64, got #{v}" test "varint: parse 2-byte value 16383", -> - v, off = parse_varint "\x7F\xFF", 1 + v, off = parse_varint "\127\255", 1 assert v == 16383 and off == 3, "expected 16383, got #{v}" -- parse_varint: 4-byte (10xxxxxx) test "varint: parse 4-byte value 16384", -> - v, off = parse_varint "\x80\x00\x40\x00", 1 + v, off = parse_varint "\128\000\064\000", 1 assert v == 16384 and off == 5, "expected 16384, got #{v}" -- encode_varint test "varint: encode 0", -> - assert encode_varint(0) == "\x00" + assert encode_varint(0) == "\000" test "varint: encode 63", -> - assert encode_varint(63) == "\x3F" + assert encode_varint(63) == "\063" test "varint: encode 64", -> - assert encode_varint(64) == "\x40\x40" + assert encode_varint(64) == "\064\064" test "varint: encode 16383", -> - assert encode_varint(16383) == "\x7F\xFF" + assert encode_varint(16383) == "\127\255" -- round-trip test "varint: round-trip 500", -> @@ -60,7 +60,7 @@ test "varint: round-trip 70000", -> test "varint: round-trip 8-byte value", -> value = 4886718345 -- 0x123456789 encoded = encode_varint value - assert encoded == "\xC0\x00\x00\x01\x23\x45\x67\x89", "8-byte encoding mismatch" + assert encoded == "\192\000\000\001\035\069\103\137", "8-byte encoding mismatch" v, _ = parse_varint encoded, 1 assert v == value, "round-trip 8-byte value failed: got #{v}" @@ -71,17 +71,17 @@ if math.floor == nil or math.type ~= nil test "varint: round-trip 8-byte max value", -> value = 4611686018427387903 -- 0x3FFFFFFFFFFFFFFF (2^62-1) encoded = encode_varint value - assert encoded == "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF", "max 8-byte encoding mismatch" + assert encoded == "\255\255\255\255\255\255\255\255", "max 8-byte encoding mismatch" v, _ = parse_varint encoded, 1 assert v == value, "round-trip max 8-byte value failed: got #{v}" test "varint: truncated 8-byte returns nil", -> - v, off = parse_varint "\xC0\x00", 1 + v, off = parse_varint "\192\000", 1 assert v == nil and off == 1, "expected nil with unchanged offset" -- offset arithmetic test "varint: parse at non-1 offset", -> - data = "\xFF" .. "\x3F" -- junk byte, then value 63 + data = "\255" .. "\063" -- junk byte, then value 63 v, off = parse_varint data, 2 assert v == 63 and off == 3 diff --git a/ipparse/tests/l7/quic/test_sni.moon b/ipparse/tests/l7/quic/test_sni.moon index 22e0ae4..2b9f91e 100644 --- a/ipparse/tests/l7/quic/test_sni.moon +++ b/ipparse/tests/l7/quic/test_sni.moon @@ -26,9 +26,9 @@ make_hs = (hs_type, body) -> -- Build a minimal ClientHello body ready to be wrapped in make_hs. -- extensions_bin: raw concatenated extension bytes (already packed). make_ch_body = (extensions_bin) -> - client_random = string.rep "\x00", 32 - ciphers = "\x13\x01" -- TLS_AES_128_GCM_SHA256 - compressions = "\x00" + client_random = string.rep "\000", 32 + ciphers = "\019\001" -- TLS_AES_128_GCM_SHA256 + compressions = "\000" sp ">H c32 s1 s2 s1 s2", 0x0303, client_random, "", ciphers, compressions, extensions_bin -- Build a server_name extension (ext type = 0x0000). @@ -65,7 +65,7 @@ test "reassemble_crypto: two frames out of order", -> test "reassemble_crypto: non-CRYPTO frames are ignored", -> frames = { - {name: "PADDING", offset: 0, data: "\x00\x00"} + {name: "PADDING", offset: 0, data: "\000\000"} {name: "CRYPTO", offset: 0, data: "data"} {name: "ACK", offset: 0, data: "ack"} } diff --git a/ipparse/tests/l7/test_dns.moon b/ipparse/tests/l7/test_dns.moon index 94b642b..53c36f7 100644 --- a/ipparse/tests/l7/test_dns.moon +++ b/ipparse/tests/l7/test_dns.moon @@ -98,7 +98,7 @@ test "answer ttl is 3600", -> test "answer rdata is 93.184.216.34", -> msg, _ = dns.parse dns_response, 1, false - assert msg.answers[1].rdata == "\x5d\xb8\xd8\x22", "rdata mismatch" + assert msg.answers[1].rdata == "\093\184\216\034", "rdata mismatch" test "types bidirectional: types[1] == 'A'", -> assert dns.types[1] == "A", "types[1] should be 'A', got '#{dns.types[1]}'" diff --git a/ipparse/tests/l7/tls/test_handshake.moon b/ipparse/tests/l7/tls/test_handshake.moon index ca0b015..9a8a1b4 100644 --- a/ipparse/tests/l7/tls/test_handshake.moon +++ b/ipparse/tests/l7/tls/test_handshake.moon @@ -22,7 +22,7 @@ test "extension: parse round-trips pack", -> assert tostring(e) == raw, "pack should round-trip" test "extension: truncated header returns nil", -> - e, off = ext.parse "\x00", 1 + e, off = ext.parse "\000", 1 assert e == nil and off == 1, "should return nil, input offset" test "extension: truncated data returns nil", -> @@ -40,11 +40,11 @@ test "server_name: parses single host_name", -> assert not s.incomplete, "should be complete" test "server_name: truncated list returns nil with error", -> - s, off, err = sn.parse "\x00", 1 + s, off, err = sn.parse "\000", 1 assert s == nil and err, "should return nil, off, err" test "server_name: truncated entry flagged incomplete", -> - bad = sp(">H", 20) .. "\x00\x00\x10ab" -- announces 16-byte name, has 2 + bad = sp(">H", 20) .. "\000\000\016ab" -- announces 16-byte name, has 2 s = sn.parse bad, 1 assert s and s.incomplete, "should be flagged incomplete" diff --git a/ipparse/tests/lib/crypto/test_ffi_mbedtls.moon b/ipparse/tests/lib/crypto/test_ffi_mbedtls.moon index 27b8f49..c4691a6 100644 --- a/ipparse/tests/lib/crypto/test_ffi_mbedtls.moon +++ b/ipparse/tests/lib/crypto/test_ffi_mbedtls.moon @@ -23,23 +23,23 @@ unless ok -- AES-128-GCM: NIST vector (zero key, zero nonce, empty plaintext). -- AES-128-GCM(key=0^16, nonce=0^12, pt="", aad="") → tag=58e2fccefa7e3061367f1d57a4e7455a test "ffi_mbedtls: GCM encrypt empty plaintext (NIST)", -> - key = string.rep "\x00", 16 - nonce = string.rep "\x00", 12 + key = string.rep "\000", 16 + nonce = string.rep "\000", 12 ct = b.aes_128_gcm_encrypt key, nonce, "", "" assert #ct == 16, "ciphertext should be 16-byte tag only" assert bin2hex(ct) == "58e2fccefa7e3061367f1d57a4e7455a", "tag mismatch: #{bin2hex ct}" test "ffi_mbedtls: GCM decrypt empty plaintext (NIST)", -> - key = string.rep "\x00", 16 - nonce = string.rep "\x00", 12 + key = string.rep "\000", 16 + nonce = string.rep "\000", 12 tag = hex_to_bin "58e2fccefa7e3061367f1d57a4e7455a" pt, err = b.aes_128_gcm_decrypt key, nonce, tag, "" assert pt == "", "plaintext should be empty, got: #{tostring err}" test "ffi_mbedtls: GCM encrypt/decrypt round-trip", -> - key = string.rep "\x42", 16 - nonce = string.rep "\x00", 12 + key = string.rep "\066", 16 + nonce = string.rep "\000", 12 plain = "hello, mbedtls!!" -- 16 bytes ct = b.aes_128_gcm_encrypt key, nonce, plain, "aad-data" pt, err = b.aes_128_gcm_decrypt key, nonce, ct, "aad-data" @@ -48,9 +48,9 @@ test "ffi_mbedtls: GCM encrypt/decrypt round-trip", -> -- NIST GCM vector: key=0^16, nonce=0^12, pt=0^16, aad="" -- ct=0388dace60b6a392f328c2b971b2fe78, tag=ab6e47d42cec13bdf53a67b21257bddf test "ffi_mbedtls: GCM encrypt 16-byte plaintext (NIST)", -> - key = string.rep "\x00", 16 - nonce = string.rep "\x00", 12 - pt = string.rep "\x00", 16 + key = string.rep "\000", 16 + nonce = string.rep "\000", 12 + pt = string.rep "\000", 16 ct_and_tag = b.aes_128_gcm_encrypt key, nonce, pt, "" assert #ct_and_tag == 32, "expected 16 ct + 16 tag" assert bin2hex(ct_and_tag\sub 1, 16) == "0388dace60b6a392f328c2b971b2fe78", @@ -59,23 +59,23 @@ test "ffi_mbedtls: GCM encrypt 16-byte plaintext (NIST)", -> "tag mismatch: #{bin2hex ct_and_tag\sub 17, 32}" test "ffi_mbedtls: GCM decrypt fails on bad tag", -> - key = string.rep "\x00", 16 - nonce = string.rep "\x00", 12 - tag = string.rep "\xff", 16 + key = string.rep "\000", 16 + nonce = string.rep "\000", 12 + tag = string.rep "\255", 16 pt, err = b.aes_128_gcm_decrypt key, nonce, tag, "" assert pt == nil, "should return nil on bad tag" assert err ~= nil, "should return error message" test "ffi_mbedtls: GCM decrypt fails on tampered AAD", -> - key = string.rep "\x42", 16 - nonce = string.rep "\x00", 12 + key = string.rep "\066", 16 + nonce = string.rep "\000", 12 ct = b.aes_128_gcm_encrypt key, nonce, "test data...!!!!", "correct-aad" pt, err = b.aes_128_gcm_decrypt key, nonce, ct, "wrong-aad" assert pt == nil, "should fail with wrong AAD" test "ffi_mbedtls: GCM ciphertext too short returns nil", -> - key = string.rep "\x00", 16 - nonce = string.rep "\x00", 12 + key = string.rep "\000", 16 + nonce = string.rep "\000", 12 pt, err = b.aes_128_gcm_decrypt key, nonce, "tooshort", "" assert pt == nil, "should return nil" assert err ~= nil, "should return error" @@ -91,8 +91,8 @@ test "ffi_mbedtls: ECB RFC 9001 §A.2 header-protection mask", -> "first 5 bytes mismatch: #{bin2hex(mask)\sub 1, 10}" test "ffi_mbedtls: ECB known NIST vector (key=0^16, block=0^16)", -> - key = string.rep "\x00", 16 - block = string.rep "\x00", 16 + key = string.rep "\000", 16 + block = string.rep "\000", 16 out = b.aes_128_ecb_block key, block -- AES-128-ECB(0^16, 0^16) = 66e94bd4ef8a2c3b884cfa59ca342b2e assert bin2hex(out) == "66e94bd4ef8a2c3b884cfa59ca342b2e", @@ -110,7 +110,7 @@ test "ffi_mbedtls: construct_nonce pn=1 XORs last byte", -> assert nonce\byte(12) == 0x5d, "last byte 0x5c XOR 0x01 = 0x5d" test "ffi_mbedtls: construct_nonce pn=0x0102 XORs last two bytes", -> - iv = string.rep "\x00", 12 + iv = string.rep "\000", 12 nonce = b.construct_nonce iv, 0x0102 assert nonce\byte(11) == 0x01, "byte 11 should be 0x01" assert nonce\byte(12) == 0x02, "byte 12 should be 0x02" diff --git a/ipparse/tests/lib/crypto/test_ffi_wolfssl.moon b/ipparse/tests/lib/crypto/test_ffi_wolfssl.moon index 8088ab9..2ffd291 100644 --- a/ipparse/tests/lib/crypto/test_ffi_wolfssl.moon +++ b/ipparse/tests/lib/crypto/test_ffi_wolfssl.moon @@ -23,39 +23,39 @@ unless ok -- AES-128-GCM: RFC 8452 / NIST vectors (zero key, zero nonce, empty plaintext). -- AES-128-GCM(key=0^16, nonce=0^12, pt="", aad="") → tag = 58e2fccefa7e3061367f1d57a4e7455a test "ffi_wolfssl: GCM encrypt empty plaintext (NIST)", -> - key = string.rep "\x00", 16 - nonce = string.rep "\x00", 12 + key = string.rep "\000", 16 + nonce = string.rep "\000", 12 ct = b.aes_128_gcm_encrypt key, nonce, "", "" assert #ct == 16, "ciphertext should be 16-byte tag only" assert bin2hex(ct) == "58e2fccefa7e3061367f1d57a4e7455a", "tag mismatch: #{bin2hex ct}" test "ffi_wolfssl: GCM decrypt empty plaintext (NIST)", -> - key = string.rep "\x00", 16 - nonce = string.rep "\x00", 12 + key = string.rep "\000", 16 + nonce = string.rep "\000", 12 tag = hex_to_bin "58e2fccefa7e3061367f1d57a4e7455a" pt, err = b.aes_128_gcm_decrypt key, nonce, tag, "" assert pt == "", "plaintext should be empty, got: #{tostring err}" test "ffi_wolfssl: GCM encrypt/decrypt round-trip", -> - key = string.rep "\x42", 16 - nonce = string.rep "\x00", 12 + key = string.rep "\066", 16 + nonce = string.rep "\000", 12 plain = "hello, wolfssl!!" -- 16 bytes ct = b.aes_128_gcm_encrypt key, nonce, plain, "aad-data" pt, err = b.aes_128_gcm_decrypt key, nonce, ct, "aad-data" assert pt == plain, "round-trip failed: #{tostring err}" test "ffi_wolfssl: GCM decrypt fails on bad tag", -> - key = string.rep "\x00", 16 - nonce = string.rep "\x00", 12 - tag = string.rep "\xff", 16 + key = string.rep "\000", 16 + nonce = string.rep "\000", 12 + tag = string.rep "\255", 16 pt, err = b.aes_128_gcm_decrypt key, nonce, tag, "" assert pt == nil, "should return nil on bad tag" assert err ~= nil, "should return error message" test "ffi_wolfssl: GCM decrypt fails on tampered AAD", -> - key = string.rep "\x42", 16 - nonce = string.rep "\x00", 12 + key = string.rep "\066", 16 + nonce = string.rep "\000", 12 ct = b.aes_128_gcm_encrypt key, nonce, "test data...!!!!", "correct-aad" pt, err = b.aes_128_gcm_decrypt key, nonce, ct, "wrong-aad" assert pt == nil, "should fail with wrong AAD" @@ -71,8 +71,8 @@ test "ffi_wolfssl: ECB RFC 9001 §A.2 header-protection mask", -> "first 5 bytes mismatch: #{bin2hex(mask)\sub 1, 10}" test "ffi_wolfssl: ECB known NIST vector (key=0^16, block=0^16)", -> - key = string.rep "\x00", 16 - block = string.rep "\x00", 16 + key = string.rep "\000", 16 + block = string.rep "\000", 16 out = b.aes_128_ecb_block key, block -- AES-128-ECB(0^16, 0^16) = 66e94bd4ef8a2c3b884cfa59ca342b2e assert bin2hex(out) == "66e94bd4ef8a2c3b884cfa59ca342b2e", @@ -90,7 +90,7 @@ test "ffi_wolfssl: construct_nonce pn=1 XORs last byte", -> assert nonce\byte(12) == 0x5d, "last byte 0x5c XOR 0x01 = 0x5d" test "ffi_wolfssl: construct_nonce pn=0x0102 XORs last two bytes", -> - iv = string.rep "\x00", 12 + iv = string.rep "\000", 12 nonce = b.construct_nonce iv, 0x0102 assert nonce\byte(11) == 0x01, "byte 11 should be 0x01" assert nonce\byte(12) == 0x02, "byte 12 should be 0x02" diff --git a/ipparse/tests/lib/crypto/test_lunatik.moon b/ipparse/tests/lib/crypto/test_lunatik.moon index 4c5eb31..4bba8de 100644 --- a/ipparse/tests/lib/crypto/test_lunatik.moon +++ b/ipparse/tests/lib/crypto/test_lunatik.moon @@ -39,7 +39,7 @@ mk_aead = -> setkey: (k) => key = k setauthsize: (n) => authsize = n encrypt: (nonce, plaintext, aad="") => - assert key == string.rep("\x11", 16), "unexpected test key" + assert key == string.rep("\017", 16), "unexpected test key" assert authsize == 16, "authsize must be 16" assert #nonce == 12, "nonce length should be 12" "ct:" .. plaintext .. "|aad:" .. aad .. "|tag:" .. string.rep("T", 16) @@ -71,7 +71,7 @@ mk_ecb = -> { setkey: (k) => key = k encrypt: (iv, block) => - assert key == string.rep("\x22", 16), "unexpected ECB key" + assert key == string.rep("\034", 16), "unexpected ECB key" assert iv == "", "ECB expects an empty IV" assert #block == 16, "ECB block must be 16 bytes" "Z" .. block\sub(2) @@ -105,87 +105,87 @@ test "lunatik: construct_nonce pn=1 XORs last byte", -> assert nonce\byte(12) == 0x5d, "last byte 0x5c XOR 0x01 = 0x5d" test "lunatik: AES-128-GCM encrypt uses crypto.aead", -> - key = string.rep "\x11", 16 - nonce = string.rep "\x00", 12 + key = string.rep "\017", 16 + nonce = string.rep "\000", 12 out = b.aes_128_gcm_encrypt key, nonce, "hello", "aad" assert out == ("ct:hello|aad:aad|tag:" .. string.rep("T", 16)), "unexpected ciphertext" test "lunatik: AES-128-GCM decrypt returns plaintext", -> - key = string.rep "\x11", 16 - nonce = string.rep "\x00", 12 + key = string.rep "\017", 16 + nonce = string.rep "\000", 12 pt, err = b.aes_128_gcm_decrypt key, nonce, ("cipher" .. string.rep("T", 16)), "aad" assert err == nil, "unexpected decrypt error: #{tostring err}" assert pt == ("pt:cipher" .. string.rep("T", 16) .. "|aad:aad"), "unexpected plaintext" test "lunatik: AES-128-GCM decrypt maps EBADMSG to nil,error", -> - key = string.rep "\x11", 16 - nonce = string.rep "\x00", 12 + key = string.rep "\017", 16 + nonce = string.rep "\000", 12 pt, err = b.aes_128_gcm_decrypt key, nonce, ("AUTHFAIL" .. string.rep("T", 16)), "" assert pt == nil, "pt should be nil on auth failure" assert err == "AES-128-GCM authentication failed (tag mismatch)", "unexpected error: #{tostring err}" test "lunatik: AES-128-GCM decrypt maps -EBADMSG (pre-6.7 symbolic) to nil,error", -> - key = string.rep "\x11", 16 - nonce = string.rep "\x00", 12 + key = string.rep "\017", 16 + nonce = string.rep "\000", 12 pt, err = b.aes_128_gcm_decrypt key, nonce, ("AUTHMIN1" .. string.rep("T", 16)), "" assert pt == nil, "pt should be nil on auth failure" assert err == "AES-128-GCM authentication failed (tag mismatch)", "unexpected error: #{tostring err}" test "lunatik: AES-128-GCM decrypt maps -74 (pre-6.7 numeric) to nil,error", -> - key = string.rep "\x11", 16 - nonce = string.rep "\x00", 12 + key = string.rep "\017", 16 + nonce = string.rep "\000", 12 pt, err = b.aes_128_gcm_decrypt key, nonce, ("AUTHNUM1" .. string.rep("T", 16)), "" assert pt == nil, "pt should be nil on auth failure" assert err == "AES-128-GCM authentication failed (tag mismatch)", "unexpected error: #{tostring err}" test "lunatik: AES-128-GCM decrypt does not treat -174 as EBADMSG", -> - key = string.rep "\x11", 16 - nonce = string.rep "\x00", 12 + key = string.rep "\017", 16 + nonce = string.rep "\000", 12 pt, err = b.aes_128_gcm_decrypt key, nonce, ("AUTHNUM3" .. string.rep("T", 16)), "" assert pt == nil, "pt should be nil on failure" assert err ~= "AES-128-GCM authentication failed (tag mismatch)", "unexpected auth error: #{tostring err}" assert tostring(err)\find("aead(gcm(aes)) decrypt failed", 1, true), "unexpected error: #{tostring err}" test "lunatik: AES-128-GCM decrypt does not treat -740 as EBADMSG", -> - key = string.rep "\x11", 16 - nonce = string.rep "\x00", 12 + key = string.rep "\017", 16 + nonce = string.rep "\000", 12 pt, err = b.aes_128_gcm_decrypt key, nonce, ("AUTHNUM4" .. string.rep("T", 16)), "" assert pt == nil, "pt should be nil on failure" assert err ~= "AES-128-GCM authentication failed (tag mismatch)", "unexpected auth error: #{tostring err}" assert tostring(err)\find("aead(gcm(aes)) decrypt failed", 1, true), "unexpected error: #{tostring err}" test "lunatik: AES-128-GCM decrypt maps -22 (numeric EINVAL) to nil,error", -> - key = string.rep "\x11", 16 - nonce = string.rep "\x00", 12 + key = string.rep "\017", 16 + nonce = string.rep "\000", 12 pt, err = b.aes_128_gcm_decrypt key, nonce, ("NOTAUTH2" .. string.rep("T", 16)), "" assert pt == nil, "pt should be nil on failure" assert err ~= "AES-128-GCM authentication failed (tag mismatch)", "unexpected auth error: #{tostring err}" assert tostring(err)\find("aead(gcm(aes)) decrypt failed", 1, true), "unexpected error: #{tostring err}" test "lunatik: AES-128-GCM decrypt returns plaintext equal to EBADMSG verbatim", -> - key = string.rep "\x11", 16 - nonce = string.rep "\x00", 12 + key = string.rep "\017", 16 + nonce = string.rep "\000", 12 pt, err = b.aes_128_gcm_decrypt key, nonce, ("PLAINBAD" .. string.rep("T", 16)), "" assert err == nil, "unexpected decrypt error: #{tostring err}" assert pt == "EBADMSG", "plaintext should be returned verbatim, got: #{tostring pt}" test "lunatik: AES-128-GCM ciphertext too short returns nil", -> - key = string.rep "\x11", 16 - nonce = string.rep "\x00", 12 + key = string.rep "\017", 16 + nonce = string.rep "\000", 12 pt, err = b.aes_128_gcm_decrypt key, nonce, "short", "" assert pt == nil, "pt should be nil" assert err ~= nil, "err should be provided" test "lunatik: AES-128-GCM decrypt returns nil,error on non-auth error", -> - key = string.rep "\x11", 16 - nonce = string.rep "\x00", 12 + key = string.rep "\017", 16 + nonce = string.rep "\000", 12 pt, err = b.aes_128_gcm_decrypt key, nonce, ("BROKEN" .. string.rep("T", 16)), "" assert pt == nil, "pt should be nil on non-auth failure" assert tostring(err)\find("aead(gcm(aes)) decrypt failed", 1, true), "unexpected error: #{tostring err}" test "lunatik: AES-128-ECB encrypt uses crypto.skcipher", -> - key = string.rep "\x22", 16 - block = string.rep "\x33", 16 + key = string.rep "\034", 16 + block = string.rep "\051", 16 out = b.aes_128_ecb_block key, block assert #out == 16, "output must be 16 bytes" assert out\byte(1) == string.byte("Z"), "first byte should be transformed by stub" diff --git a/ipparse/tests/lib/crypto/test_lunatik_kernel.moon b/ipparse/tests/lib/crypto/test_lunatik_kernel.moon index cf30ee6..4378493 100644 --- a/ipparse/tests/lib/crypto/test_lunatik_kernel.moon +++ b/ipparse/tests/lib/crypto/test_lunatik_kernel.moon @@ -35,24 +35,24 @@ test "lunatik-kernel: construct_nonce pn=2 (RFC 9001 A.3)", -> assert bin2hex(nonce) == "fa044b2f42a3fd3b46fb255e", "nonce mismatch: #{bin2hex nonce}" test "lunatik-kernel: GCM encrypt empty plaintext (NIST)", -> - key = "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" - nonce = "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" + key = "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000" + nonce = "\000\000\000\000\000\000\000\000\000\000\000\000" ct = backend.aes_128_gcm_encrypt key, nonce, "", "" assert #ct == 16, "ciphertext should be 16-byte tag only" assert bin2hex(ct) == "58e2fccefa7e3061367f1d57a4e7455a", "tag mismatch: #{bin2hex ct}" test "lunatik-kernel: GCM decrypt empty plaintext (NIST)", -> - key = "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" - nonce = "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" + key = "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000" + nonce = "\000\000\000\000\000\000\000\000\000\000\000\000" tag = hex_to_bin "58e2fccefa7e3061367f1d57a4e7455a" pt, err = backend.aes_128_gcm_decrypt key, nonce, tag, "" assert err == nil, "unexpected decrypt error: #{tostring err}" assert pt == "", "plaintext should be empty" test "lunatik-kernel: GCM decrypt fails on bad tag", -> - key = "\x42\x42\x42\x42\x42\x42\x42\x42\x42\x42\x42\x42\x42\x42\x42\x42" - nonce = "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" + key = "\066\066\066\066\066\066\066\066\066\066\066\066\066\066\066\066" + nonce = "\000\000\000\000\000\000\000\000\000\000\000\000" ct = backend.aes_128_gcm_encrypt key, nonce, "test data...!!!!", "aad" bad_ct = ct\sub(1, #ct - 1) .. string.char((string.byte(ct, #ct) + 1) % 256) pt, err = backend.aes_128_gcm_decrypt key, nonce, bad_ct, "aad" diff --git a/ipparse/tests/lib/test_sha.moon b/ipparse/tests/lib/test_sha.moon index c3a1249..d1ef7d7 100644 --- a/ipparse/tests/lib/test_sha.moon +++ b/ipparse/tests/lib/test_sha.moon @@ -43,7 +43,7 @@ test "sha: NIST long message", -> -- HMAC-SHA256 (RFC 4231). test "hmac: RFC 4231 test case 1", -> - key = string.rep "\x0b", 20 + key = string.rep "\011", 20 digest = hmac sha256, key, "Hi There" assert #digest == 32, "hmac must return a 32-byte binary digest" assert hex(digest) == diff --git a/ipparse/tests/test_init.moon b/ipparse/tests/test_init.moon index 8a1d7c5..27c6ded 100644 --- a/ipparse/tests/test_init.moon +++ b/ipparse/tests/test_init.moon @@ -12,14 +12,14 @@ util = require"ipparse.lib.util" test "hex2bin converts hex to bytes", -> result = hex2bin "ff00" - assert result == "\xff\x00", "hex2bin failed" + assert result == "\255\000", "hex2bin failed" test "hex2bin single byte", -> result = hex2bin "ab" - assert result == "\xab", "hex2bin single byte failed" + assert result == "\171", "hex2bin single byte failed" test "filterascii replaces non-printable with dot", -> - result = filterascii "\x01hello\xff" + result = filterascii "\001hello\255" assert result == ".hello.", "expected '.hello.', got '#{result}'" test "filterascii keeps printable chars", -> diff --git a/ipparse/tests/test_malformed.moon b/ipparse/tests/test_malformed.moon index 1c78b61..86c5873 100644 --- a/ipparse/tests/test_malformed.moon +++ b/ipparse/tests/test_malformed.moon @@ -18,12 +18,12 @@ quic = require"ipparse.l4.quic" frames = require"ipparse.l4.quic.frames" test "ip6: truncated header returns nil", -> - res, off = ip6.parse "\x60\x00\x00\x00", 1 + res, off = ip6.parse "\096\000\000\000", 1 assert res == nil and off == 1, "truncated IPv6 header should return nil" test "ip: get_version out of bounds returns nil", -> assert ip.get_version("", 1) == nil, "empty buffer" - assert ip.get_version("\x45", 5) == nil, "offset past end" + assert ip.get_version("\069", 5) == nil, "offset past end" test "ip: parse empty buffer returns nil, err", -> res, err = ip.parse "", 1 @@ -35,25 +35,25 @@ test "quic: parse empty buffer returns nil", -> test "quic: truncated long header returns nil", -> -- Long-header flag set, then nothing else. - q, off = quic.parse "\xc0\x00", 1 + q, off = quic.parse "\192\000", 1 assert q == nil, "truncated long header should return nil" test "quic: ACK frame with huge range count is rejected", -> -- type=0x02, largest=0, delay=0, range_count=0x3FFFFFFF (varint 4 bytes), first_range=0 - raw = "\x02\x00\x00" .. "\xbf\xff\xff\xff" .. "\x00" + raw = "\002\000\000" .. "\191\255\255\255" .. "\000" frame, _, err = frames.parse_frame raw, 1 assert frame == nil and err and err\match("too large"), "expected range-count rejection, got #{err}" test "quic: validate_frames detects truncated CRYPTO frame", -> -- CRYPTO frame: type=0x06, offset=0, length=100, but no data - raw = "\x06\x00\x40\x64" + raw = "\006\000\064\100" ok, err = frames.validate_frames raw assert ok == false and err, "truncated CRYPTO should fail validation" test "dns: circular compression pointer is rejected", -> -- Minimal DNS header (12 bytes) + a name that is a pointer to itself. hdr = sp ">HHHHHH", 0x1234, 0x0100, 1, 0, 0, 0 - name = "\xc0\x0c" -- pointer to offset 12 = itself + name = "\192\012" -- pointer to offset 12 = itself qd = name .. sp ">HH", 1, 1 msg = hdr .. qd res, _, err = dns.parse msg, 1 @@ -65,11 +65,11 @@ test "dns: circular compression pointer is rejected", -> test "dns: truncated label returns error", -> -- Label announces 10 bytes, provides 2. - lbls, _, err = dns.labels "\x0aab", 1, 1 + lbls, _, err = dns.labels "\010ab", 1, 1 assert lbls == nil and err, "truncated label should return an error" test "pack_compat: unpack on short data raises a clear error", -> - ok, err = pcall su, ">I4", "\x01\x02", 1 + ok, err = pcall su, ">I4", "\001\002", 1 assert not ok and tostring(err)\match("too short"), "expected 'data string too short', got #{err}" util.summary "malformed" From 3c7a21cce225a085cc19aa6fbfcbf42ae1f41e5d Mon Sep 17 00:00:00 2001 From: jperon Date: Tue, 10 Nov 2026 20:10:28 +0100 Subject: [PATCH 31/43] vmtest: pin Lunatik to upstream v5.1 tag and commit Track the official v5.1 release instead of a rewriting fork branch, and verify the tag still resolves to the expected commit so a moved tag fails loudly. An absent LUNATIK_SRC is cloned even when explicitly named; a stale harness-managed cache is re-cloned, while a user-provided checkout is only reported. Build code generation with the Lua 5.4 toolchain already used by CI (the module itself embeds Lua 5.5). Also key the kernel CI cache on KERNEL_VERSION and kernel.fragment only, so changing the Lunatik pin does not trigger a needless kernel rebuild. Co-Authored-By: Claude Opus 5.5 --- .github/workflows/test.yml | 8 +++++++- tools/vmtest/README.md | 2 +- tools/vmtest/build-lunatik.sh | 36 ++++++++++++++++++++++++++++++++++- tools/vmtest/lib.sh | 3 +++ tools/vmtest/versions.env | 16 +++++++++++----- 5 files changed, 57 insertions(+), 8 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 730e80e..58fd2af 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -44,11 +44,17 @@ jobs: sudo udevadm control --reload-rules sudo udevadm trigger --name-match=kvm || true + - name: Read pinned kernel version + id: pinned + run: | + . tools/vmtest/versions.env + echo "kernel=$KERNEL_VERSION" >> "$GITHUB_OUTPUT" + - name: Restore kernel build cache uses: actions/cache@v5 with: path: tools/vmtest/build/linux-* - key: vmtest-kernel-${{ hashFiles('tools/vmtest/versions.env', 'tools/vmtest/kernel.fragment') }} + key: vmtest-kernel-${{ steps.pinned.outputs.kernel }}-${{ hashFiles('tools/vmtest/kernel.fragment') }} - name: Restore ccache uses: actions/cache@v5 diff --git a/tools/vmtest/README.md b/tools/vmtest/README.md index 9fda231..afc1156 100644 --- a/tools/vmtest/README.md +++ b/tools/vmtest/README.md @@ -56,7 +56,7 @@ réserver à un usage séquentiel). | Fichier | Rôle | |---|---| -| `versions.env` | Versions épinglées (noyau, dépôt/branche lunatik) | +| `versions.env` | Versions épinglées (noyau, dépôt/étiquette/commit lunatik) | | `kernel.fragment` | Options de config noyau requises par Lunatik | | `flake.nix` | Toolchain reproductible (gcc, qemu, luajit, moonscript…) | | `ensure-vng.sh` | Installe virtme-ng dans `.venv/` (absent de nixpkgs) | diff --git a/tools/vmtest/build-lunatik.sh b/tools/vmtest/build-lunatik.sh index 0a28a6c..ce8a4e0 100755 --- a/tools/vmtest/build-lunatik.sh +++ b/tools/vmtest/build-lunatik.sh @@ -6,17 +6,51 @@ [ -e "$KERNEL_IMAGE" ] || die "kernel not built yet — run kernel.sh first" -if [ ! -d "$LUNATIK_SRC" ]; then +# LUNATIK_REF is a tag by default; verify it still points at LUNATIK_COMMIT. +# LUNATIK_COMMIT is empty when testing an unpinned reference (master, or an +# arbitrary ref supplied by the CI workflow). +clone_pinned() { log "Cloning $LUNATIK_REPO@$LUNATIK_REF…" + rm -rf "$LUNATIK_SRC" git clone --depth 1 --branch "$LUNATIK_REF" --recurse-submodules \ "$LUNATIK_REPO" "$LUNATIK_SRC" + if [ -n "${LUNATIK_COMMIT:-}" ]; then + head="$(git -C "$LUNATIK_SRC" rev-parse HEAD)" + if [ "$head" != "$LUNATIK_COMMIT" ]; then + die "$LUNATIK_REF resolved to $head, expected pinned commit $LUNATIK_COMMIT (was the tag moved?)" + fi + fi +} + +lunatik_head() { + git -C "$LUNATIK_SRC" rev-parse HEAD 2>/dev/null || echo unknown +} + +if [ ! -d "$LUNATIK_SRC" ]; then + # An absent directory is cloned even when LUNATIK_SRC was named explicitly. + clone_pinned +elif [ -z "$LUNATIK_SRC_USER" ]; then + # Harness-managed cache: never trust a stale checkout silently. + if [ -n "${LUNATIK_COMMIT:-}" ] && [ "$(lunatik_head)" != "$LUNATIK_COMMIT" ]; then + log "Cached Lunatik checkout is at $(lunatik_head), expected $LUNATIK_COMMIT — re-cloning…" + clone_pinned + fi +else + head="$(lunatik_head)" + if [ -n "${LUNATIK_COMMIT:-}" ] && [ "$head" != "$LUNATIK_COMMIT" ]; then + log "WARNING: user-provided LUNATIK_SRC $LUNATIK_SRC is at $head, not the pinned $LUNATIK_COMMIT — using it as-is" + fi fi KREL="$(make -s -C "$KERNEL_SRC" kernelrelease)" log "Building lunatik against $KREL ($KERNEL_SRC)…" # cd rather than make -C: lunatik's Makefile passes M=$(PWD) to Kbuild, # and PWD is the caller's directory unless we actually chdir. +# Upstream v5.1 defaults LUA to lua5.5 (bundled kernel Lua). LUA here only +# drives host-side code generation and the CLI, so use the 5.4 toolchain +# already required by CI; the kernel module still embeds Lua 5.5. (cd "$LUNATIK_SRC" && make \ + LUA="${LUNATIK_LUA:-lua5.4}" \ KERNEL_RELEASE="$KREL" \ MODULES_BUILD_PATH="$KERNEL_SRC" \ -j"$(nproc)") diff --git a/tools/vmtest/lib.sh b/tools/vmtest/lib.sh index be1519f..e10bd40 100755 --- a/tools/vmtest/lib.sh +++ b/tools/vmtest/lib.sh @@ -13,6 +13,9 @@ BUILD_DIR="$(realpath -m -- "${VMTEST_BUILD_DIR:-$VMTEST_DIR/build}")" KERNEL_SRC="$BUILD_DIR/linux-$KERNEL_VERSION" KERNEL_IMAGE="$KERNEL_SRC/arch/x86/boot/bzImage" +# Remember whether LUNATIK_SRC came from the environment (user-provided +# checkout, never altered) or is the harness-managed cache under BUILD_DIR. +LUNATIK_SRC_USER="${LUNATIK_SRC:-}" LUNATIK_SRC="$(realpath -m -- "${LUNATIK_SRC:-$BUILD_DIR/lunatik}")" log() { printf '\033[1;34m[vmtest]\033[0m %s\n' "$*" >&2; } diff --git a/tools/vmtest/versions.env b/tools/vmtest/versions.env index d338d88..419c432 100644 --- a/tools/vmtest/versions.env +++ b/tools/vmtest/versions.env @@ -1,10 +1,16 @@ # Pinned versions for the VM test environment. # Single source of truth — sourced by the scripts and hashed for CI caching. +# Values are defaults: an environment variable with the same name wins, so a +# job can override e.g. LUNATIK_REF to test a new Lunatik reference. # Linux LTS kernel the tests run against. -KERNEL_VERSION=6.18.35 +: "${KERNEL_VERSION:=6.18.35}" -# Lunatik source: pinned on jperon's fork until the fix_crypto_enomem PR -# is merged into luainkernel/lunatik master. -LUNATIK_REPO=https://github.com/jperon/lunatik -LUNATIK_REF=fix_crypto_enomem +# Lunatik source: upstream release tag, with the exact commit it must resolve +# to so a moved tag is detected. The fix_crypto_enomem branch of the fork is +# abandoned: its crypto preallocation fix (commit 95318191) is in v5.1. +: "${LUNATIK_REPO:=https://github.com/luainkernel/lunatik}" +: "${LUNATIK_REF:=v5.1}" +# `=` (not `:=`) so an explicitly empty value stays empty when testing an +# unpinned reference. +: "${LUNATIK_COMMIT=e9b48135f45ca8a950015a3ee7280d54368d09c4}" From ad582ec2fb98f0d07881c7fad581afc5cd155b68 Mon Sep 17 00:00:00 2001 From: jperon Date: Tue, 10 Nov 2026 20:10:34 +0100 Subject: [PATCH 32/43] ci: upload the VM console and output logs on failure Run the VM suite with a fixed VMTEST_RW_DIR and upload the guest's console.log and out from it, instead of the useless host dmesg. Co-Authored-By: Claude Opus 5.5 --- .github/workflows/test.yml | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 58fd2af..edb7a7e 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -64,16 +64,16 @@ jobs: restore-keys: vmtest-ccache- - name: Run full suite in VM + env: + VMTEST_RW_DIR: ${{ runner.temp }}/vmtest-rw run: make vm-test - - name: Collect dmesg on failure - if: failure() - run: dmesg > vm-dmesg.log 2>/dev/null || true - - name: Upload logs on failure if: failure() uses: actions/upload-artifact@v7 with: name: vm-logs - path: vm-dmesg.log + path: | + ${{ runner.temp }}/vmtest-rw/console.log + ${{ runner.temp }}/vmtest-rw/out if-no-files-found: ignore From f228ca9aa26b405e7c4a1ef3b2592ce3c0e67b12 Mon Sep 17 00:00:00 2001 From: jperon Date: Tue, 10 Nov 2026 20:10:41 +0100 Subject: [PATCH 33/43] ci: run the userspace suite under LuaJIT, Lua 5.1, 5.4 and 5.5 Install the interpreters through Nix (Lua 5.5 is not in apt on ubuntu-latest), compile MoonScript once, then reuse the generated Lua files. Lua 5.1 is marked continue-on-error until its support lands. The Makefile gains LUA ?= luajit, used by test-nobuild and example, keeping the default behaviour unchanged. Co-Authored-By: Claude Opus 5.5 --- .github/workflows/test.yml | 26 ++++++++++++++++++++------ ipparse/Makefile | 5 +++-- 2 files changed, 23 insertions(+), 8 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index edb7a7e..5ca6c13 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -12,15 +12,29 @@ on: jobs: userspace: runs-on: ubuntu-latest + strategy: + fail-fast: false + matrix: + lua: [luajit, lua5.1, lua5.4, lua5.5] + include: + - lua: lua5.1 + experimental: true steps: - uses: actions/checkout@v6 - - name: Install LuaJIT + MoonScript - run: | - sudo apt-get update - sudo apt-get install -y luajit luarocks - sudo luarocks install moonscript + - uses: cachix/install-nix-action@v31 + - name: Compile MoonScript sources + run: nix-shell -p luajitPackages.moonscript --run "make -C ipparse all" - name: Run test suite - run: make -C ipparse test + # TODO: remove once Lua 5.1 support lands + continue-on-error: ${{ matrix.experimental == true }} + run: | + case "${{ matrix.lua }}" in + luajit) pkg=luajit; bin=luajit ;; + lua5.1) pkg=lua5_1; bin=lua ;; + lua5.4) pkg=lua5_4; bin=lua ;; + lua5.5) pkg=lua5_5; bin=lua ;; + esac + nix-shell -p "$pkg" --run "make -C ipparse test-nobuild LUA=$bin" kernel: runs-on: ubuntu-latest diff --git a/ipparse/Makefile b/ipparse/Makefile index 0f17968..bbe24f2 100644 --- a/ipparse/Makefile +++ b/ipparse/Makefile @@ -3,6 +3,7 @@ NAME = ipparse LUA_MODULE_DIR = /lib/modules/lua +LUA ?= luajit all: @@ -24,10 +25,10 @@ uninstall: test: all test-nobuild test-nobuild: - cd .. && LUA_PATH="./?.lua;./?/init.lua" luajit ipparse/tests/run_all.lua + cd .. && LUA_PATH="./?.lua;./?/init.lua" $(LUA) ipparse/tests/run_all.lua example: all - cd .. && LUA_PATH="./?.lua;./?/init.lua" luajit ipparse/examples/parse_real_quic.lua + cd .. && LUA_PATH="./?.lua;./?/init.lua" $(LUA) ipparse/examples/parse_real_quic.lua test-lunatik: all @$(MAKE) install From 35d4657d5e64511d30a33cc231461209186155e6 Mon Sep 17 00:00:00 2001 From: jperon Date: Tue, 10 Nov 2026 20:10:56 +0100 Subject: [PATCH 34/43] ci: add a non-blocking Lunatik master/ref test job Add kernel-lunatik-head, which runs the same VM suite against luainkernel/lunatik at a workflow_dispatch-selected ref (default master), with its own LUNATIK_SRC and artefact. LUNATIK_REF/LUNATIK_COMMIT are overridable from the environment so versions.env only sets defaults. Document the upgrade procedure at the top of the workflow. Provide libbpf for master's eBPF loader, and make the guest install use the writable 5.4 Lua module paths instead of the read-only Nix store. Co-Authored-By: Claude Opus 5.5 --- .github/workflows/test.yml | 79 +++++++++++++++++++++++++++++++++++++- tools/vmtest/flake.nix | 3 ++ tools/vmtest/guest-test.sh | 12 ++++-- 3 files changed, 90 insertions(+), 4 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 5ca6c13..ece9aac 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -1,13 +1,28 @@ # Unified test pipeline: -# - userspace: fast LuaJIT run of the ipparse suite (no VM) +# - userspace: fast Lua suite (LuaJIT, 5.1, 5.4, 5.5) of the ipparse tests +# (no VM) # - kernel: full suite (userspace + lunatik kernel modules) inside a # virtme-ng microVM on the pinned LTS kernel (tools/vmtest/) +# - kernel-lunatik-head: same as kernel, but against a Lunatik ref supplied +# via workflow_dispatch (default: upstream master); non-blocking. +# +# Lunatik upgrade procedure: +# 1. run kernel-lunatik-head with lunatik_ref=; +# 2. once green, update LUNATIK_REF and LUNATIK_COMMIT in +# tools/vmtest/versions.env to that tag and its commit. name: test on: push: branches: [master] pull_request: + workflow_dispatch: + inputs: + lunatik_ref: + description: Lunatik ref to test in kernel-lunatik-head + required: false + default: master + type: string jobs: userspace: @@ -91,3 +106,65 @@ jobs: ${{ runner.temp }}/vmtest-rw/console.log ${{ runner.temp }}/vmtest-rw/out if-no-files-found: ignore + + kernel-lunatik-head: + runs-on: ubuntu-latest + continue-on-error: true + env: + LUNATIK_REF: ${{ github.event.inputs.lunatik_ref || 'master' }} + LUNATIK_COMMIT: '' + steps: + - uses: actions/checkout@v6 + + - name: Install dependencies + run: | + sudo apt-get update + sudo apt-get install -y \ + build-essential flex bison bc libelf-dev libssl-dev dwarves \ + ccache cpio zstd qemu-system-x86 busybox-static \ + python3-venv rustc cargo \ + luajit luarocks lua5.4 git curl kmod \ + libbpf-dev pkg-config + sudo luarocks install moonscript + + - name: Enable KVM on the runner + run: | + echo 'KERNEL=="kvm", GROUP="kvm", MODE="0666"' \ + | sudo tee /etc/udev/rules.d/99-kvm.rules + sudo udevadm control --reload-rules + sudo udevadm trigger --name-match=kvm || true + + - name: Read pinned kernel version + id: pinned + run: | + . tools/vmtest/versions.env + echo "kernel=$KERNEL_VERSION" >> "$GITHUB_OUTPUT" + + - name: Restore kernel build cache + uses: actions/cache@v5 + with: + path: tools/vmtest/build/linux-* + key: vmtest-kernel-${{ steps.pinned.outputs.kernel }}-${{ hashFiles('tools/vmtest/kernel.fragment') }} + + - name: Restore ccache + uses: actions/cache@v5 + with: + path: ~/.cache/ccache + key: vmtest-ccache-${{ hashFiles('tools/vmtest/versions.env') }} + restore-keys: vmtest-ccache- + + - name: Run full suite in VM + env: + LUNATIK_SRC: ${{ runner.temp }}/lunatik-head + VMTEST_RW_DIR: ${{ runner.temp }}/vmtest-rw-head + run: make vm-test + + - name: Upload logs on failure + if: failure() + uses: actions/upload-artifact@v7 + with: + name: vm-logs-lunatik-head + path: | + ${{ runner.temp }}/vmtest-rw-head/console.log + ${{ runner.temp }}/vmtest-rw-head/out + if-no-files-found: ignore diff --git a/tools/vmtest/flake.nix b/tools/vmtest/flake.nix index c9b4096..061dbca 100644 --- a/tools/vmtest/flake.nix +++ b/tools/vmtest/flake.nix @@ -43,6 +43,9 @@ git curl kmod + # Lunatik master builds its eBPF loader against libbpf. + libbpf + pkg-config ]; shellHook = '' # Nix binaries carry their rpath; an inherited LD_LIBRARY_PATH only diff --git a/tools/vmtest/guest-test.sh b/tools/vmtest/guest-test.sh index 1c08a2a..a3309be 100755 --- a/tools/vmtest/guest-test.sh +++ b/tools/vmtest/guest-test.sh @@ -8,17 +8,22 @@ KREL="$(cat "$BUILD_DIR/kernelrelease")" [ "$(uname -r)" = "$KREL" ] || die "VM kernel $(uname -r) != expected $KREL" log "Installing lunatik modules + scripts into the overlay…" -# LUA_PATH (make variable): where the lunatik.config symlink goes. The -# default derived from lua5.4's package.path points into the read-only -# /nix/store; use the writable overlay instead. +# LUA_PATH/LUA_CPATH (make variables): where lunatik installs its modules and +# eBPF loader. Their defaults are derived from package.path/package.cpath, +# which point into the read-only /nix/store; use the writable overlay instead. +# Upstream master derives both from $(LUA) (default lua5.5), hence passing +# LUA explicitly as well. GUEST_LUA_DIR=/usr/local/share/lua/5.4 +GUEST_LUA_CPATH=/usr/local/lib/lua/5.4 # cd rather than make -C: lunatik's Makefile uses $(PWD) # INSTALL: /lib/modules is a 9p mount mapped to the host user; chown to # root is impossible there (and irrelevant for tests). (cd "$LUNATIK_SRC" && make install \ + LUA=lua5.4 \ KERNEL_RELEASE="$KREL" \ MODULES_BUILD_PATH="$KERNEL_SRC" \ LUA_PATH="$GUEST_LUA_DIR" \ + LUA_CPATH="$GUEST_LUA_CPATH" \ INSTALL="install" \ >/dev/null) depmod "$KREL" @@ -29,6 +34,7 @@ mkdir -p /usr/local/bin cat > /usr/local/bin/lunatik < Date: Tue, 10 Nov 2026 20:17:01 +0100 Subject: [PATCH 35/43] fix(crypto): handle Lunatik v5.1 nil,error AEAD decrypt convention lib/luacrypto_aead.c in v5.1 replaces lunatik_throw(L, -EBADMSG) with lunatik_pushfail(L, -EBADMSG), so a bad tag yields nil, "EBADMSG" instead of raising. aes_128_gcm_decrypt only inspected the pcall status slot and lost the error, returning nil, nil. Check both slots: the pcall error (old API) and, when the plaintext is nil, the second return value (v5.1+). Other failures still raise, as luacrypto_aead_finish and luacrypto_skcipher_crypt keep using lunatik_throw; encrypt and ECB therefore need no change. Co-Authored-By: Claude Opus 5.5 --- ipparse/lib/crypto/backend/lunatik.moon | 32 +++++++++--- ipparse/tests/lib/crypto/test_lunatik.moon | 59 ++++++++++++++++++++++ 2 files changed, 83 insertions(+), 8 deletions(-) diff --git a/ipparse/lib/crypto/backend/lunatik.moon b/ipparse/lib/crypto/backend/lunatik.moon index 072ba43..05a40ab 100644 --- a/ipparse/lib/crypto/backend/lunatik.moon +++ b/ipparse/lib/crypto/backend/lunatik.moon @@ -46,6 +46,13 @@ is_ebadmsg = (err) -> bare = (s\match "^.*:%d+: (.*)$") or s ebadmsg_names[bare] == true +--- Normalizes an EBADMSG from either Lunatik convention to the public error. +-- @tparam string err Error value (name, "-NAME" or signed decimal) +-- @treturn string|nil Authentication error message, or nil if not EBADMSG +ebadmsg_error = (err) -> + return unless is_ebadmsg err + "AES-128-GCM authentication failed (tag mismatch)" + cached_gcm_enc_tfm = nil cached_gcm_enc_key = nil cached_gcm_dec_tfm = nil @@ -98,15 +105,24 @@ aes_128_gcm_decrypt = (key, nonce, ciphertext_with_tag, aad = "") -> if #ciphertext_with_tag < 16 return nil, "ciphertext_with_tag too short (need at least 16-byte tag)" c = get_gcm_tfm "decrypt", key - -- Wrap decrypt call to catch EBADMSG as a handled error, not an exception - ok, pt_or_err = pcall -> c\decrypt nonce, ciphertext_with_tag, aad + -- Two Lunatik conventions must be handled: + -- * old API (fork fix_crypto_enomem): decrypt raises (lunatik_throw) on + -- bad tag, so the error lands in the pcall status slot; + -- * upstream v5.1+ (lib/luacrypto_aead.c: luacrypto_aead_decrypt calls + -- lunatik_pushfail(L, -EBADMSG)): decrypt returns nil, "EBADMSG" and + -- nothing is raised. Only non-EBADMSG failures still raise. + ok, pt, err = pcall -> c\decrypt nonce, ciphertext_with_tag, aad unless ok - -- Decrypt raises on failure (luacrypto_aead_finish -> lunatik_throw) - if is_ebadmsg pt_or_err - return nil, "AES-128-GCM authentication failed (tag mismatch)" - return nil, "aead(gcm(aes)) decrypt failed: #{tostring pt_or_err}" - -- Success always returns the plaintext (may be empty string, which is valid). - pt_or_err, nil + msg = ebadmsg_error pt + return nil, msg if msg + return nil, "aead(gcm(aes)) decrypt failed: #{tostring pt}" + -- Success returns the plaintext (possibly ""); only inspect the second + -- value when the plaintext is nil (a plaintext equal to "EBADMSG" is kept). + if pt == nil + msg = ebadmsg_error err + return nil, msg if msg + return nil, "aead(gcm(aes)) decrypt failed: #{tostring err}" + pt, nil --- Encrypts single AES-128-ECB block. -- Uses a cached skcipher transform to avoid frequent allocations under load. diff --git a/ipparse/tests/lib/crypto/test_lunatik.moon b/ipparse/tests/lib/crypto/test_lunatik.moon index 4bba8de..c5c5ce9 100644 --- a/ipparse/tests/lib/crypto/test_lunatik.moon +++ b/ipparse/tests/lib/crypto/test_lunatik.moon @@ -61,6 +61,20 @@ mk_aead = -> error "EINVAL" if prefix == "PLAINBAD" return "EBADMSG" + -- Upstream Lunatik v5.1 convention (luacrypto_aead_decrypt -> + -- lunatik_pushfail): return nil, instead of raising. + if prefix == "NEWFAIL1" + return nil, "EBADMSG" + if prefix == "NEWFAIL2" + return nil, "-EBADMSG" + if prefix == "NEWFAIL3" + return nil, "-74" + if prefix == "NEWFAIL4" + return nil, "unknown" + if prefix == "NEWERR01" + return nil, "EINVAL" + if prefix == "NEWERR02" + return nil, "-174" "pt:" .. ciphertext_with_tag .. "|aad:" .. aad __close: => calls.aead_close += 1 @@ -138,6 +152,51 @@ test "lunatik: AES-128-GCM decrypt maps -74 (pre-6.7 numeric) to nil,error", -> assert pt == nil, "pt should be nil on auth failure" assert err == "AES-128-GCM authentication failed (tag mismatch)", "unexpected error: #{tostring err}" +test "lunatik: AES-128-GCM decrypt maps v5.1 nil,EBADMSG to nil,error", -> + key = string.rep "\017", 16 + nonce = string.rep "\000", 12 + pt, err = b.aes_128_gcm_decrypt key, nonce, ("NEWFAIL1" .. string.rep("T", 16)), "" + assert pt == nil, "pt should be nil on auth failure" + assert err == "AES-128-GCM authentication failed (tag mismatch)", "unexpected error: #{tostring err}" + +test "lunatik: AES-128-GCM decrypt maps v5.1 nil,-EBADMSG to nil,error", -> + key = string.rep "\017", 16 + nonce = string.rep "\000", 12 + pt, err = b.aes_128_gcm_decrypt key, nonce, ("NEWFAIL2" .. string.rep("T", 16)), "" + assert pt == nil, "pt should be nil on auth failure" + assert err == "AES-128-GCM authentication failed (tag mismatch)", "unexpected error: #{tostring err}" + +test "lunatik: AES-128-GCM decrypt maps v5.1 nil,-74 to nil,error", -> + key = string.rep "\017", 16 + nonce = string.rep "\000", 12 + pt, err = b.aes_128_gcm_decrypt key, nonce, ("NEWFAIL3" .. string.rep("T", 16)), "" + assert pt == nil, "pt should be nil on auth failure" + assert err == "AES-128-GCM authentication failed (tag mismatch)", "unexpected error: #{tostring err}" + +test "lunatik: AES-128-GCM decrypt maps v5.1 nil,unknown to decrypt failed", -> + key = string.rep "\017", 16 + nonce = string.rep "\000", 12 + pt, err = b.aes_128_gcm_decrypt key, nonce, ("NEWFAIL4" .. string.rep("T", 16)), "" + assert pt == nil, "pt should be nil on failure" + assert err ~= "AES-128-GCM authentication failed (tag mismatch)", "unexpected auth error: #{tostring err}" + assert tostring(err)\find("aead(gcm(aes)) decrypt failed", 1, true), "unexpected error: #{tostring err}" + +test "lunatik: AES-128-GCM decrypt maps v5.1 nil,EINVAL to decrypt failed", -> + key = string.rep "\017", 16 + nonce = string.rep "\000", 12 + pt, err = b.aes_128_gcm_decrypt key, nonce, ("NEWERR01" .. string.rep("T", 16)), "" + assert pt == nil, "pt should be nil on failure" + assert err ~= "AES-128-GCM authentication failed (tag mismatch)", "unexpected auth error: #{tostring err}" + assert tostring(err)\find("aead(gcm(aes)) decrypt failed", 1, true), "unexpected error: #{tostring err}" + +test "lunatik: AES-128-GCM decrypt does not treat v5.1 nil,-174 as EBADMSG", -> + key = string.rep "\017", 16 + nonce = string.rep "\000", 12 + pt, err = b.aes_128_gcm_decrypt key, nonce, ("NEWERR02" .. string.rep("T", 16)), "" + assert pt == nil, "pt should be nil on failure" + assert err ~= "AES-128-GCM authentication failed (tag mismatch)", "unexpected auth error: #{tostring err}" + assert tostring(err)\find("aead(gcm(aes)) decrypt failed", 1, true), "unexpected error: #{tostring err}" + test "lunatik: AES-128-GCM decrypt does not treat -174 as EBADMSG", -> key = string.rep "\017", 16 nonce = string.rep "\000", 12 From ec0dcc0c3eb72d56134987c9ac1be17146d720e8 Mon Sep 17 00:00:00 2001 From: jperon Date: Tue, 10 Nov 2026 20:29:12 +0100 Subject: [PATCH 36/43] test(bit_compat): skip bit_pure cases where math.floor is absent bit_pure is only the last-resort fallback of bit_compat (Lua 5.1 without `bit`) and relies on math.floor, which the Lua 5.5 kernel runtime does not provide. Guard the bit_pure cases behind math.floor so the kernel run keeps only the bit_compat cases instead of failing 16 assertions. Co-Authored-By: Claude Opus 5.5 --- ipparse/tests/lib/test_bit_compat.moon | 40 +++++++++++++++----------- 1 file changed, 23 insertions(+), 17 deletions(-) diff --git a/ipparse/tests/lib/test_bit_compat.moon b/ipparse/tests/lib/test_bit_compat.moon index 2dd45cb..e29331f 100644 --- a/ipparse/tests/lib/test_bit_compat.moon +++ b/ipparse/tests/lib/test_bit_compat.moon @@ -9,10 +9,16 @@ util = require "ipparse.lib.util" {:test} = util { :band, :bor, :bxor } = require "ipparse.lib.bit_compat" --- Test the pure-Lua fallback directly on every runtime, so it is exercised +-- Test the pure-Lua fallback directly wherever it can run, so it is exercised -- even when the host has LuaJIT's `bit` or Lua 5.2+'s `bit32`. +-- It is only the last-resort fallback of `bit_compat` (Lua 5.1 without `bit`) +-- and relies on `math.floor`, which the Lua 5.5 kernel runtime lacks: the +-- `bit_pure` cases below therefore only run where `math.floor` exists. pure = require "ipparse.lib.bit_pure" +test_pure = (name, fn) -> + test name, fn if math.floor + test "bit_compat: bor with 1 argument", -> assert bor(5) == 5, "bor(5) != 5" @@ -36,71 +42,71 @@ test "bit_compat: bxor with 3 arguments", -> -- ipparse relies on that signedness in the QUIC VarInt encoder, which avoids -- the negative int32 produced by `bor(0x80000000, value)`. -test "bit_pure: band with 1 argument", -> +test_pure "bit_pure: band with 1 argument", -> assert pure.band(0xFF) == 255, "band(0xFF) != 255" -test "bit_pure: band with 2 arguments", -> +test_pure "bit_pure: band with 2 arguments", -> assert pure.band(-1, 0xFF) == 255, "band(-1,0xFF) != 255" assert pure.band(0xFFFFFFFF, 0xFFFF) == 65535, "band(0xFFFFFFFF,0xFFFF) != 65535" assert pure.band(0x80000000, 0x80000000) == -2147483648, "band of bit 31 != -2147483648" -test "bit_pure: band with 3 arguments", -> +test_pure "bit_pure: band with 3 arguments", -> assert pure.band(0xFF, 0x3C, 0x1E) == 28, "band(0xFF,0x3C,0x1E) != 28" -test "bit_pure: bor with 1 argument", -> +test_pure "bit_pure: bor with 1 argument", -> assert pure.bor(5) == 5, "bor(5) != 5" assert pure.bor(0x80000000) == -2147483648, "bor(0x80000000) != -2147483648" -test "bit_pure: bor with 2 arguments", -> +test_pure "bit_pure: bor with 2 arguments", -> assert pure.bor(1, 2) == 3, "bor(1,2) != 3" assert pure.bor(0x80000000, 0) == -2147483648, "bor(0x80000000,0) != -2147483648" assert pure.bor(0x80000000, 0xFF) == -2147483393, "bor(0x80000000,0xFF) != -2147483393" -test "bit_pure: bor with 3 arguments", -> +test_pure "bit_pure: bor with 3 arguments", -> assert pure.bor(1, 2, 4) == 7, "bor(1,2,4) != 7" -test "bit_pure: bor with 7 arguments", -> +test_pure "bit_pure: bor with 7 arguments", -> assert pure.bor(1, 2, 4, 8, 16, 32, 64) == 127, "bor(1,2,4,8,16,32,64) != 127" -test "bit_pure: bxor with 1 argument", -> +test_pure "bit_pure: bxor with 1 argument", -> assert pure.bxor(5) == 5, "bxor(5) != 5" -test "bit_pure: bxor with 2 arguments", -> +test_pure "bit_pure: bxor with 2 arguments", -> assert pure.bxor(0x80000000, 0xFFFFFFFF) == 2147483647, "bxor(0x80000000,0xFFFFFFFF) != 2147483647" assert pure.bxor(0x80000000, 0x80000000) == 0, "bxor(0x80000000,0x80000000) != 0" assert pure.bxor(-1, 0xFF) == -256, "bxor(-1,0xFF) != -256" -test "bit_pure: bxor with 3 arguments", -> +test_pure "bit_pure: bxor with 3 arguments", -> assert pure.bxor(0xF0, 0x3C, 0xFF) == 51, "bxor(0xF0,0x3C,0xFF) != 51" -test "bit_pure: bnot", -> +test_pure "bit_pure: bnot", -> assert pure.bnot(0) == -1, "bnot(0) != -1" assert pure.bnot(0xFFFFFFFF) == 0, "bnot(0xFFFFFFFF) != 0" assert pure.bnot(-1) == 0, "bnot(-1) != 0" -test "bit_pure: lshift by 0, 1 and 31 bits", -> +test_pure "bit_pure: lshift by 0, 1 and 31 bits", -> assert pure.lshift(3, 0) == 3, "lshift(3,0) != 3" assert pure.lshift(1, 1) == 2, "lshift(1,1) != 2" assert pure.lshift(1, 31) == -2147483648, "lshift(1,31) != -2147483648" -test "bit_pure: lshift masks the shift count to 5 bits", -> +test_pure "bit_pure: lshift masks the shift count to 5 bits", -> assert pure.lshift(1, 32) == 1, "lshift(1,32) != 1" assert pure.lshift(1, 33) == 2, "lshift(1,33) != 2" -test "bit_pure: rshift by 0, 1 and 31 bits", -> +test_pure "bit_pure: rshift by 0, 1 and 31 bits", -> assert pure.rshift(3, 0) == 3, "rshift(3,0) != 3" assert pure.rshift(0x80000000, 1) == 1073741824, "rshift(0x80000000,1) != 1073741824" assert pure.rshift(0x80000000, 31) == 1, "rshift(0x80000000,31) != 1" assert pure.rshift(-1, 1) == 2147483647, "rshift(-1,1) != 2147483647" assert pure.rshift(0x80000000, 32) == -2147483648, "rshift(0x80000000,32) != -2147483648" -test "bit_pure: arshift sign-extends", -> +test_pure "bit_pure: arshift sign-extends", -> assert pure.arshift(3, 0) == 3, "arshift(3,0) != 3" assert pure.arshift(0x80000000, 1) == -1073741824, "arshift(0x80000000,1) != -1073741824" assert pure.arshift(-1, 1) == -1, "arshift(-1,1) != -1" assert pure.arshift(0x40000000, 1) == 536870912, "arshift(0x40000000,1) != 536870912" -test "bit_pure: IPv6 version/traffic class/flow label round-trip", -> +test_pure "bit_pure: IPv6 version/traffic class/flow label round-trip", -> vtf = pure.bor(pure.lshift(6, 28), pure.lshift(0x12, 20), 0x34567) assert vtf == 1629701479, "vtf != 1629701479" assert pure.rshift(vtf, 28) == 6, "version != 6" From a374571ef0bfb1b633d848a85c360a484f58cbbf Mon Sep 17 00:00:00 2001 From: jperon Date: Tue, 10 Nov 2026 20:29:20 +0100 Subject: [PATCH 37/43] test(pack_compat): split floating-point cases into their own module The Lua 5.5 kernel runtime has no floats: a single float literal makes the whole chunk fail to compile ("malformed number near '.5'"), which also dropped the integer cases of the module. Move every float/double/IEEE test into ipparse/tests/lib/test_pack_compat_float.moon, registered in run_all but deliberately absent from test_lunatik_kernel_portable. The remaining module has no float literal, no `^` and no `/` in code, so it loads and runs in the kernel. The native kernel string.pack is correct for negative >i8: the pinned Lunatik v5.1 lua/ sources built with -D_KERNEL (Lua 5.5.1, 64-bit lua_Integer, SZINT=8) return fffffffffffffffe for pack(">i8", -2) and round-trip -2; both cases PASS in the VM now that the module compiles. Co-Authored-By: Claude Opus 5.5 --- ipparse/tests/lib/test_pack_compat.moon | 65 +--------------- ipparse/tests/lib/test_pack_compat_float.moon | 78 +++++++++++++++++++ ipparse/tests/run_all.moon | 1 + 3 files changed, 83 insertions(+), 61 deletions(-) create mode 100644 ipparse/tests/lib/test_pack_compat_float.moon diff --git a/ipparse/tests/lib/test_pack_compat.moon b/ipparse/tests/lib/test_pack_compat.moon index 2c215f2..5db927c 100644 --- a/ipparse/tests/lib/test_pack_compat.moon +++ b/ipparse/tests/lib/test_pack_compat.moon @@ -111,8 +111,8 @@ test "pack_compat: >i4 negative", -> expected = from_hex "fffffffe" assert got == expected, "got: #{got}\nexp: #{expected}" --- 64-bit little-endian value above 2^32 keeps its high word. -test "pack_compat: +-- 64-bit little-endian value above the 32-bit range keeps its high word. +test "pack_compat: got = pack " got = pack "" assert got == "", "got: #{got}" --- ─── Floats, doubles and 64-bit signed values ──────────────────────────────── +-- ─── 64-bit signed and unsigned values ────────────────────────────────────── unpack = pack_compat.unpack or string.unpack --- 1.5 is exactly 0x3ff8000000000000; little-endian byte order. -test "pack_compat: - got = pack "d -2.5 bytes", -> - got = pack ">d", -2.5 - expected = from_hex "c004000000000000" - assert got == expected, "got: #{got}\nexp: #{expected}" - --- 1.5 is exactly 0x3fc00000 as a 32-bit float; little-endian byte order. -test "pack_compat: - got = pack "i8 negative bytes", -> got = pack ">i8", -2 expected = from_hex "fffffffffffffffe" assert got == expected, "got: #{got}\nexp: #{expected}" -test "pack_compat: +test "pack_compat: got = pack " expected = from_hex "04030201" assert got == expected, "got: #{got}\nexp: #{expected}" -test "pack_compat: - v, pos = unpack "d round-trip", -> - v, pos = unpack ">d", pack(">d", 123.456) - assert v == 123.456, "got: #{v}" - assert pos == 9, "pos: #{pos}" - -test "pack_compat: - v, pos = unpack "i8 negative round-trip", -> v, pos = unpack ">i8", pack(">i8", -2) assert v == -2, "got: #{v}" assert pos == 9, "pos: #{pos}" --- IEEE 754 special values: infinities, signed zero, subnormal and NaN. -test "pack_compat: - assert unpack(" - got = pack " - v = 2 ^ -1074 - got = pack " - nan = math.huge - math.huge - v = unpack " +-- SPDX-License-Identifier: MIT OR GPL-2.0-only +-- + +-- tests/lib/test_pack_compat_float.moon +-- Floating-point tests for the pack/unpack compatibility layer. +-- Kept in a separate module because the Lua 5.5 kernel runtime has no floats: +-- a single float literal would make the whole chunk fail to compile +-- ("malformed number"), which would also drop the integer tests. This module is +-- therefore NOT listed in test_lunatik_kernel_portable.moon. + +util = require "ipparse.lib.util" +{:test} = util +pack_compat = require "ipparse.lib.pack_compat" +pack = pack_compat.pack or string.pack +unpack = pack_compat.unpack or string.unpack + +from_hex = (h) -> h\gsub "..", (cc) -> string.char tonumber cc, 16 + +-- 1.5 is exactly 0x3ff8000000000000; little-endian byte order. +test "pack_compat_float: + got = pack "d -2.5 bytes", -> + got = pack ">d", -2.5 + expected = from_hex "c004000000000000" + assert got == expected, "got: #{got}\nexp: #{expected}" + +-- 1.5 is exactly 0x3fc00000 as a 32-bit float; little-endian byte order. +test "pack_compat_float: + got = pack " + v, pos = unpack "d round-trip", -> + v, pos = unpack ">d", pack(">d", 123.456) + assert v == 123.456, "got: #{v}" + assert pos == 9, "pos: #{pos}" + +test "pack_compat_float: + v, pos = unpack " + assert unpack(" + got = pack " + v = 2 ^ -1074 + got = pack " + nan = math.huge - math.huge + v = unpack " Date: Tue, 10 Nov 2026 21:02:02 +0100 Subject: [PATCH 38/43] fix(fun): flatten protected so ipparse.fun loads under the C-stack guard Lunatik v5.1 computes LUAI_MAXCCALLS from the kernel stack actually consumed, and the Lua 5.5 parser is recursive in C. Loading ipparse.fun through a second require level (e.g. script -> ipparse.l7.dns -> fun) overflowed the 16 KiB kernel stack because `protected` nested two inline function expressions inside the xpcall call. Define the error handler and the returned function as block-level locals and build the per-call closure as a statement inside it. Parser C recursion drops enough that ipparse.fun now loads up to four require levels deep (measured: depth 4 OK, depth 5 overflows), covering real usage (script -> ipparse.l7.quic.session -> ipparse.l4.quic -> fun, or script -> ipparse.l7.dns -> fun). API and semantics are unchanged: same 26 arguments/return values, same xpcall error handling, same optional `op` callback. Co-Authored-By: Claude Opus 5.5 --- ipparse/fun.moon | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/ipparse/fun.moon b/ipparse/fun.moon index 9b4af7e..61506cc 100644 --- a/ipparse/fun.moon +++ b/ipparse/fun.moon @@ -269,6 +269,15 @@ _ = nil -- on the hot path (per-packet), and a vararg implementation would allocate a -- table and call `unpack` on every invocation. The trade-off is a hard limit -- of 26 arguments and 26 return values. +-- +-- Implementation note: the error handler and the returned function are defined +-- as flat locals rather than inline in the `xpcall` call. The Lua parser is +-- recursive in C, and Lunatik runs on a 16 KiB kernel stack with a C-call +-- guard derived from the stack actually consumed: inline function expressions +-- nested two deep here pushed `require "ipparse.fun"` over that guard when the +-- module was loaded one `require` level deeper than the top level. Defining +-- them at block level reduces the parser's C recursion; the API and semantics +-- are unchanged. -- @tparam function fn -- @tparam[opt] function op -- @treturn function A new, protected function (max 26 args/returns). @@ -278,15 +287,16 @@ _ = nil -- -- @treturn ...any|nil Results from `fn` or `op` (see main description for conditions). protected = (fn, op) -> leak_debug = _ and _.leak_debug - (a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z) -> - ok, a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z = xpcall ( - -> fn a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z - ), (err) -> - print err - print debug.traceback! if leak_debug - op! if op + onerror = (err) -> + print err + print debug.traceback! if leak_debug + op! if op + run = (a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z) -> + call = -> fn a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z + ok, a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z = xpcall call, onerror if ok a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z + run --- The main export table for the `fun` module. From 7bb2b0846b1063486f80a5a56366f3706ec84481 Mon Sep 17 00:00:00 2001 From: jperon Date: Tue, 10 Nov 2026 21:02:02 +0100 Subject: [PATCH 39/43] test(lunatik): preload ipparse.fun at first level in portable suite MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Most modules exercised by the portable kernel suite require ipparse.fun two or three `require` levels down, inside a pcall(require …) chain that already consumes kernel stack. Load it once at the shallowest C-stack point before the loop so the suite no longer depends on that chain depth and stays clear of Lunatik's stack-derived C-call guard. Co-Authored-By: Claude Opus 5.5 --- ipparse/tests/test_lunatik_kernel_portable.moon | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/ipparse/tests/test_lunatik_kernel_portable.moon b/ipparse/tests/test_lunatik_kernel_portable.moon index 6ce7aca..4ebf957 100644 --- a/ipparse/tests/test_lunatik_kernel_portable.moon +++ b/ipparse/tests/test_lunatik_kernel_portable.moon @@ -11,6 +11,14 @@ util = require "ipparse.lib.util" +-- Preload ipparse.fun at the first level: almost every module below requires +-- it (often two or three `require` levels down), and Lunatik's 16 KiB kernel +-- stack makes loading it from inside a pcall(require …) chain tight. Loading +-- it once here, where the C stack is shallowest, keeps the suite independent +-- of the chain depth. Real usage requires ipparse.fun at depth 3 at most +-- (e.g. script → ipparse.l7.quic.session → ipparse.l4.quic → fun). +require "ipparse.fun" + mods = { "ipparse.tests.test_fun" "ipparse.tests.test_init" From 25799f5d8665f060b7db98de1b7363588bc61546 Mon Sep 17 00:00:00 2001 From: jperon Date: Tue, 10 Nov 2026 21:04:35 +0100 Subject: [PATCH 40/43] fix(pack_compat): avoid UB casting negative doubles to uint64 The double -> uint64_t conversion is undefined for negative values, and older LuaJIT releases saturate it to 0x8000000000000000, corrupting 64-bit signed writes. Route negatives through int64_t first, then reinterpret the bits as unsigned, keeping the direct cast for non-negative values (so values >= 2^63 are unaffected). Co-Authored-By: Claude Opus 5.5 --- ipparse/lib/pack_compat_lib.moon | 16 +++++++++++++--- ipparse/tests/lib/test_pack_compat.moon | 12 ++++++++++++ 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/ipparse/lib/pack_compat_lib.moon b/ipparse/lib/pack_compat_lib.moon index 91674c9..d2b2a8f 100644 --- a/ipparse/lib/pack_compat_lib.moon +++ b/ipparse/lib/pack_compat_lib.moon @@ -81,9 +81,19 @@ read_uint = (s, offset, size, le) -> val = val * 256 + string.byte(s, offset + i + 1) val +-- Convertit un nombre en uint64_t sans conversion indéfinie pour un négatif : +-- la conversion double → int64_t est définie dans la plage représentable, puis +-- on réinterprète les mêmes bits en non signé. Certains LuaJIT saturaient la +-- conversion directe double négatif → uint64_t à 0x8000000000000000. +cast_uint = (val) -> + if val < 0 + ffi.cast "uint64_t", ffi.cast("int64_t", val) + else + ffi.cast "uint64_t", val + -- Écrit un entier non signé vers un buffer ffi uint8_t* (offset 0-based) write_uint = (buf, offset, val, size, le) -> - v = ffi.cast "uint64_t", val + v = cast_uint val if le for i = 0, size - 1 buf[offset + i] = tonumber(v % 256) @@ -515,14 +525,14 @@ make_uint = (sz, le) -> else if le (buf, n, v) -> - u = ffi.cast "uint64_t", v + u = cast_uint v for k = 1, sz buf[n + k] = tonumber(u % 256) u = u / 256 n + sz else (buf, n, v) -> - u = ffi.cast "uint64_t", v + u = cast_uint v for k = sz, 1, -1 buf[n + k] = tonumber(u % 256) u = u / 256 diff --git a/ipparse/tests/lib/test_pack_compat.moon b/ipparse/tests/lib/test_pack_compat.moon index 5db927c..720038e 100644 --- a/ipparse/tests/lib/test_pack_compat.moon +++ b/ipparse/tests/lib/test_pack_compat.moon @@ -132,6 +132,18 @@ test "pack_compat: >i8 negative bytes", -> expected = from_hex "fffffffffffffffe" assert got == expected, "got: #{got}\nexp: #{expected}" +-- -1 as a signed 64-bit value is all ones in two's complement. +test "pack_compat: + got = pack "i8 negative 2^53 bytes", -> + got = pack ">i8", -9007199254740992 + expected = from_hex "ffe0000000000000" + assert got == expected, "got: #{got}\nexp: #{expected}" + test "pack_compat: got = pack " Date: Tue, 10 Nov 2026 21:09:02 +0100 Subject: [PATCH 41/43] fix(fun): revalidate bidirectional reverse-lookup cache The reverse-lookup cache kept returning a stale key after the forward entry was modified or deleted. Validate the cached key with rawget(self, key) == val before reuse; rawget avoids re-entering the __index metamethod, which would recurse on an absent key. Co-Authored-By: Claude Opus 5.5 --- ipparse/fun.moon | 6 ++++-- ipparse/tests/test_fun.moon | 42 +++++++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 2 deletions(-) diff --git a/ipparse/fun.moon b/ipparse/fun.moon index 61506cc..9e5f99f 100644 --- a/ipparse/fun.moon +++ b/ipparse/fun.moon @@ -63,8 +63,10 @@ memoN = => bidirmt_gen = -> _mem = setmetatable {}, __mode: "kv" (val) => - if _k = _mem[val] - return _k + _k = _mem[val] + -- rawget (not @[_k]) validates without re-entering __index, which would + -- recurse on an absent key. + return _k if _k and rawget(@, _k) == val for k, v in pairs @ if v == val _mem[val] = k diff --git a/ipparse/tests/test_fun.moon b/ipparse/tests/test_fun.moon index d5ee470..0fa8b56 100644 --- a/ipparse/tests/test_fun.moon +++ b/ipparse/tests/test_fun.moon @@ -91,4 +91,46 @@ test "opairs returns sorted keys", -> test "zero_indexed copies t[1] to t[0]", -> t = zero_indexed {"x", "y", "z"} assert t[0] == "x", "t[0] should be 'x', got '#{t[0]}'" + +test "bidirectional invalidates stale cache on key modification", -> + t = bidirectional {a: 1} + assert t[1] == "a", "t[1] should be 'a', got '#{t[1]}'" + t.a = 2 + assert t[1] == nil, "t[1] should be nil after t.a changed, got '#{t[1]}'" + assert t[2] == "a", "t[2] should be 'a' after change, got '#{t[2]}'" + +test "bidirectional drops cache for deleted key", -> + t = bidirectional {a: 1} + assert t[1] == "a", "t[1] should be 'a', got '#{t[1]}'" + t.a = nil + assert t[1] == nil, "t[1] should be nil after key deletion, got '#{t[1]}'" + +test "bidirectional cache cycle after deletion", -> + t = bidirectional {x: "y", y: "x"} + assert t["y"] == "x", "t['y'] should be 'x', got '#{t['y']}'" + assert t["x"] == "y", "t['x'] should be 'y', got '#{t['x']}'" + t.x = nil + t.y = nil + assert t["y"] == nil, "t['y'] should be nil after deletion, got '#{t['y']}'" + +test "bidirectional non-regression forward and reverse lookup", -> + t = bidirectional {a: 1, b: 2} + assert t.a == 1, "t.a should be 1" + assert t.b == 2, "t.b should be 2" + assert t[1] == "a", "t[1] should be 'a'" + assert t[2] == "b", "t[2] should be 'b'" + +test "bidirectional reverse sees key added later", -> + t = bidirectional {a: 1} + t.b = 2 + assert t[2] == "b", "t[2] should be 'b' after adding t.b, got '#{t[2]}'" + +test "bidirectional pairs only sees real entries", -> + t = bidirectional {a: 1, b: 2} + t[1] + t[2] + n = 0 + for k, v in pairs t + n += 1 + assert n == 2, "pairs should see 2 entries, got #{n}" util.summary "fun" From fb8ff2850eac8f4fcdfe039c1f7a87afd939e263 Mon Sep 17 00:00:00 2001 From: jperon Date: Tue, 10 Nov 2026 21:14:19 +0100 Subject: [PATCH 42/43] ci: make the Lua 5.1 userspace job blocking Co-Authored-By: Claude Opus 5.5 --- .github/workflows/test.yml | 5 ----- 1 file changed, 5 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index ece9aac..9f196e9 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -31,17 +31,12 @@ jobs: fail-fast: false matrix: lua: [luajit, lua5.1, lua5.4, lua5.5] - include: - - lua: lua5.1 - experimental: true steps: - uses: actions/checkout@v6 - uses: cachix/install-nix-action@v31 - name: Compile MoonScript sources run: nix-shell -p luajitPackages.moonscript --run "make -C ipparse all" - name: Run test suite - # TODO: remove once Lua 5.1 support lands - continue-on-error: ${{ matrix.experimental == true }} run: | case "${{ matrix.lua }}" in luajit) pkg=luajit; bin=luajit ;; From b3fa8b911d8aa4ebeb1db5edf2fac9c54ee89820 Mon Sep 17 00:00:00 2001 From: jperon Date: Tue, 10 Nov 2026 21:32:38 +0100 Subject: [PATCH 43/43] ci: give nix-shell a nixpkgs channel in the userspace job Co-Authored-By: Claude --- .github/workflows/test.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 9f196e9..98d4ff6 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -34,6 +34,8 @@ jobs: steps: - uses: actions/checkout@v6 - uses: cachix/install-nix-action@v31 + with: + nix_path: nixpkgs=channel:nixos-unstable - name: Compile MoonScript sources run: nix-shell -p luajitPackages.moonscript --run "make -C ipparse all" - name: Run test suite