Audit Arithmetic audit-arithmetic

Audit for arithmetic and boundary bugs (overflow, off-by-one, drift)

Works in Claude, ChatGPT, Codex and more
Our test
—
not tested yet
GitHub stars
18k
ben-manes/caffeine
Added by
0
members
Updated
Dec 6
commit a618bb9
Content
1 file
no scripts · 3 KB
License
Apache 2.0
free to use

What our test found

Not tested yet. Our model tries every skill on a realistic request and scores the result, the most popular skills first. Until then, the safety checks on the right still apply.

View on GitHub ↗
name: audit-arithmetic
description: Audit for arithmetic and boundary bugs (overflow, off-by-one, drift)
context: fork
agent: auditor
disable-model-invocation: true

Assume there is at least one arithmetic or boundary bug in this code.

Audit for:

  • Off-by-one errors
  • Incorrect inequality direction (< vs <=, > vs >=)
  • Overflow / underflow (int, long, specifically nanoTime arithmetic)
  • Implicit narrowing or widening conversion
  • Incorrect rounding or truncation
  • Sign errors
  • Sentinel value collisions (can a valid value equal a sentinel?)
  • Counter drift (can a counter diverge from the quantity it tracks over a long-running cache?)
  • Timer wheel bucket arithmetic (modular arithmetic, wrap-around)
  • Frequency sketch counter saturation and reset arithmetic
  • Hill climber / adaptive sizing: window vs main partition adjustment; the density and probe-verdict log-ratios (ln((x+ε)/(y+ε))), the probation-capacity denominator (max(1, maximum − windowMaximum − mainProtectedMaximum)), the saturated 4·maximum sample period, and the stride/commitment rung tables (percentage calculations, rounding, overshoot, oscillation)
  • FrequencySketch table size calculation (ensureCapacity rounding to power of two, interaction with maximumSize)
  • Weight delta computation in UpdateTask (sign conventions, can intermediate sums overflow when weights are near Integer.MAX_VALUE?)

Settle each suspected issue that reduces to integer or bit arithmetic on primitives with a bit-vector solver (z3py, or Lean's bv_decide), whether you report it or dismiss it. Hand evaluation is where arithmetic claims go wrong in both directions. Work in .local/experiments/arithmetic-<date>/.

  • Model each expression at its Java width (>> is arithmetic, >>> logical, (long) of an int sign-extends, a narrowing cast keeps the low bits). Java shifts by the count's low 5 bits for an int and 6 for a long, so x >>> 32 is x, while a solver shifts every bit out once the count reaches the width: mask a variable count in the model. State the intended result at a wider width, and ask for a counterexample first.
  • Take each precondition from the code (a clamp, a cap, an array-length limit) and cite it. If the code does not fix a bound, such as how many updates reorder or how much time elapses, widen it until the verdict stops changing. A sign guard on a 32-bit weight holds for three reordered updates and fails at four.
  • Include a mutant that must fail (drop the guard, narrow the type). If the mutant passes too, the check proves nothing.
  • Replay the witness and boundary values (0, ±1, ±2^k ± 1, MIN, MAX) through both the model and the compiled class, and require identical results. A single-file java --class-path caffeine/build/classes/java/main:caffeine/build/classes/java/codeGen Replay.java reaches a package-private class by reflection. Rebuild the classes if they are older than the source. State it when a model cannot be replayed, for example because it needs a whole cache.

A proof covers the model, not the code. A claim that is not pure arithmetic, such as drift over billions of operations, needs a concrete input, a step-by-step trace, expected against actual behavior, and the operation count.

Ignore style and performance. Only report high-confidence defects that a replayed witness or such a trace supports.