deniz.in

Markets

Weather

Loading weather

· via dev.to (home feed)

Formal verification finds nim-libp2p slice bug that 16 months of tests missed

A one-line fix merged into nim-libp2p in November 2026 closes a slice-bounds bug that survived 16 months of passing CI, after an SMT-based verification run produced a counterexample no test had caught.

Formal verification finds nim-libp2p slice bug that 16 months of tests missed

In November 2026, the nim-libp2p project merged a pull request that changes one line in libp2p/utils/bytesview.nim and adds eleven lines of tests. The merge closes issue #3364, a bug report whose counterexample came not from a fuzzer or a regression suite but from Axiom, an SMT-backed verification engine built by the authors of a detailed write-up on dev.to. According to that post, the defect had been present for sixteen months while every continuous-integration run stayed green.

The bug

nim-libp2p is the Nim implementation of the libp2p peer-to-peer networking stack. BytesView is its non-owning view over a byte sequence, used when a protocol parses a frame or slices a field without copying the underlying buffer. The toOpenArray(b, e) template is supposed to return the unconsumed bytes from relative index b through e, both bounds inclusive, matching the convention of Nim's system.toOpenArray. Instead, it computed its end index as rpos + e - b rather than rpos + e, so every call with a non-zero start returned a view exactly b bytes shorter than requested. At rpos = 0, a slice from b through e had length e - 2b + 1 where the inclusive convention requires e - b + 1.

BytesView shipped in July 2025 with a test that encoded the truncated result. Because the expected value came from the same mistaken arithmetic as the code, the suite validated the buggy behaviour, and additional tests built the same way would have passed just as happily, as the dev.to post notes.

Nothing failed inside the library because both in-tree call sites, in noise.nim, pass a literal zero start — the one case where the wrong and right formulas coincide. External callers fared worse: a non-zero start produced a silently shortened view, a valid one-element slice (b = e > 0) came back empty, and once 2b > e the computed length turned negative, raising a RangeDefect when materialised into a sequence. After the fix, an out-of-range end that previously truncated silently now raises an IndexDefect.

How verification found what tests could not

Axiom takes a property stated independently of the implementation — here, that a view-relative inclusive slice from b through e has length e - b + 1 — and has a solver search a bounded domain for an input that breaks it. The search returned the smallest counterexample: with b = 1 and e = 4, the implementation yields three bytes where the law demands four. That is a concrete input a maintainer can check by hand, not an example someone chose in advance.

The campaign classified the outcome as DISPROVED_REPRODUCIBLE: the failure appeared identically on two CI legs, five integrity guards covering input, determinism, vacuity, footprint and environment all passed, and both legs produced identical canonical digests.

The write-up is candid about the limits. The length law was inferred and adopted by the verifying run rather than inherited from a documented upstream guarantee; the underlying argument is simply that the wrapper should follow the convention of the Nim primitive it wraps. Axiom also verifies a model — an intermediate representation capturing the index and length arithmetic — within a bounded scope, not every property of the program.

The fix

PR #3430, written by richard-ramos, removes the offending subtraction so the end bound becomes rpos + e. The eleven added test lines pin the corrected behaviour. The post describes the division of labour — a machine finds the violation, a human writes the patch — as the normal dynamic of an upstream contribution.

Why it matters

This episode is a clean demonstration of where formal verification earns its keep in core infrastructure. Tests only check inputs someone thought to try, and when expected values are derived from the same faulty reasoning as the implementation, more testing entrenches a bug rather than exposing it. A property stated independently of the code and checked mechanically escaped that trap after sixteen months of green builds. In a networking library whose byte-view primitives feed protocol decoders, a silent short read is exactly the kind of defect that surfaces far from its cause. Downstream users should take note as well: any caller of BytesView.toOpenArray with a non-zero start has been receiving short data, and after upgrading, ends that once truncated silently will raise defects. The caveats — bounded scope, an adopted rather than inherited requirement — are real, but they do not blunt the central point: mechanical search over a stated law found what a year and a half of testing structurally could not.

  • #nim
  • #libp2p
  • #formal-verification
  • #bug-fix
  • #open-source

Related posts