After using Lean 4 and differential testing to examine Clojure 1.12.7’s Java core, I found 17 bugs with no matching reports in Clojure’s JIRA or ask.clojure.org. The findings include a `reduce` call on `vector-of` that never finishes, a hash collision node that can hide a key, and a Java functional-interface call whose result changes depending on whether a type hint is present.
The investigation was prompted by Koukyosyumei’s article, “著名なRust製OSSをLean 4で形式検証してみたら、めちゃめちゃバグが見つかった話” (in Japanese; the title means “I formally verified well-known Rust OSS with Lean 4 and found a lot of bugs”). Its author ported well-known Rust projects into a form that could be proved in Lean 4 and found bugs in the original code when proofs failed. I wanted to see whether the same approach could find problems in Clojure, whose persistent data structures have been used since around 1.0.
I used Clojure 1.12.7, released on 2026-11-18, and JDK 21. Source links point to the `clojure-1.12.7` tag. For small functions involving integer and bit arithmetic, I modeled the code in Lean and searched for counterexamples. I used differential testing for the larger collections, comparing them with Java’s `java.util` collections, and compared Java interop calls made with and without a type hint.
The `lean-verify` procedure models one function as a Lean definition, then checks a property comparing it with a simpler specification computed at a wider bit width. The `bv_decide` tactic searches for counterexamples. Modeling Java `long` and `int` as `BitVec 64` and `BitVec 32` captures the wraparound of addition and multiplication, as well as division rounding toward zero and the sign of `%`.
When Lean found a counterexample, I converted it into a Clojure input and reproduced it in the original code. I then modeled the fix in Lean and proved the same property. Because Lean can handle only small functions that reduce to integer and bit arithmetic, I tested the larger persistent data structures separately.
For maps, each test applied the same random operations to `PersistentHashMap` and `java.util.HashMap`, then compared the results. The HAMT tests mixed partial collisions at each 5-bit level with identical hashes: 1,500 operations for each of 2,000 seeds. For vectors, I compared against `ArrayList` around the tree-level boundaries 32, 1056, 32800, and 1048608, using `conj`, `pop`, `assoc`, and their transient versions.
I searched Clojure’s JIRA through its REST API, and searched ask.clojure.org both on the web and through the site itself. I found no matching report for any of the 17 bugs.
The most striking issue is in `vector-of`, a vector backed by primitive arrays and implemented separately from `PersistentVector` in `gvec.clj`. Calling `reduce` after `drop` never returns when the vector has 65505 or more elements. With 65504 elements, the example returns `2145354256`; at 65505, it loops indefinitely. The equivalent operation on a regular vector of 65505 elements returns `2145419760`.
The problem is at line 79 of `gvec.clj`, where the sequence’s `reduce` calculates the start of the next 32-element chunk:
`(recur result (bit-and 0xffe0 (+ aidx 32)))`
The mask `0xffe0` clears the low five bits to round down to a multiple of 32, but it also clears bit 16 and every higher bit. At 65505 elements, the last chunk starts at index 65504. The next chunk should start at 65536, but the mask changes that to 0, sending `reduce` back to the beginning. The mask has been present since the 2010 commit that added `internal-reduce`.
In Lean, I checked the property that, for a position `a` that is a multiple of 32, the next chunk begins at `a + 32`. The implementation using `0xffe0` fails with the counterexample `a = 1073741792`; evaluating `stepImpl 65504` also returns 0. Replacing the mask with `(~~~0x1f)` makes the proof succeed under the assumptions that `a` is a multiple of 32 and below `0x7fffffe0`.
Another bug affects `dissoc` on a HAMT collision node. A Clojure hash map splits a key’s hash into 5-bit pieces to navigate the tree. Keys with identical 32-bit hashes share a `HashCollisionNode`, which finds a key using `=`. When `HashCollisionNode.without` removes a key, it should give the new node its own hash, `this.hash`, but instead passes the `hash` parameter—the hash of the key being removed.
If the key passed to `dissoc` is `=` to a stored key but has a different hash, the node records the wrong hash. A Clojure vector and a Java `ArrayList` with the same elements are `=`, but hash differently; the mismatch is tracked in CLJ-1372. In the example, two `ArrayList` keys both hash to 1003, while `[0 42]` hashes to 1867155179 and is `=` to the first list.
After dissociating `[0 42]` and associating the first `ArrayList` again, the map prints with both keys and has a count of 2, but `(get m3 b)` returns `nil`. The node records 1867155179 after the removal. The next `assoc` then treats the key with hash 1003 as non-colliding and splits the keys into separate branches. A lookup for `b` follows the 1003 branch and never reaches it. In this example, the collision node is at the tree’s first level, so the key passed to `dissoc` must share the low five bits of 1003; 1867155179 does.
`disj` on a hash set has the same bug, but the corresponding transient `dissoc!` path does not. I changed the line in `PersistentHashMap.java` from the 1.12.7 release to use `this.hash` and compiled it. With the patched class ahead of the jar on the classpath, the example returned `:B` for `(get m3 b)`.
A third finding is that `.removeIf` can behave differently depending on whether the list’s type is known. Since Clojure 1.12, a Clojure function can be passed directly where a Java method expects a functional interface. CLJ-2864 removed logic that interpreted the function’s return value as a Clojure truthy value.
When the compiler knows the type, it follows the new behavior. Without a type hint, the call goes through reflection and uses `Reflector.coerceAdapterReturn`, which still applies `RT.booleanCast` to a boolean return value. As a result, `.removeIf` with the set `#{1 2}` throws `ClassCastException` when the list type is known, but returns `[3]` when the list is wrapped in `identity` and reflection is used. The set, used as a function, returns the element itself: the typed adapter cannot turn the `Long` into a `Boolean` and throws, while the reflective call reads the return value as true and removes the elements.
Other findings include integer overflow in `StringSeq.drop`, which can produce a negative index, and a negative-index bug in `SubVector.assocN`. For example, dropping `Integer/MAX_VALUE` from `(next (seq "abc"))` throws `StringIndexOutOfBoundsException` with index `-2147483648`, while dropping the same amount from `(seq "abc")` returns `()`. Lean found the counterexample `i = 2147483646` and `n = 2147483647`. I changed the check to compare `n` with the remaining length, `s.length() - i`; Lean proved the revised check matches the naive specification for every 32-bit input under the assumptions `0 < i < s.length()` and `0 < n`.
For `SubVector.assocN`, associating at index `-1` normally throws `IndexOutOfBoundsException`, but doing so on `(subvec [0 1 2 3 4] 2 4)` changes the underlying vector to `[0 :x 2 3 4]` while the returned subvector still looks like `[2 3]`. CLJ-962 fixed the same bug in `nth`, but `assoc` still has it.
The 12 other bugs have smaller impacts, and I reproduced each on Clojure 1.12.7. They include `reduce-kv` throwing on `vector-of`; functional-interface calls with three or more arguments truncating an `int` return value when the type is known; unexpected metadata from `next` on sequences of strings, primitive arrays, or infinite repeats; and numeric conversion inconsistencies involving `NaN`, infinity, and values outside primitive ranges.
The remaining findings concern `char` wrapping a `BigInt` at 2^64; `float` rejecting `##Inf` but accepting `##NaN`; boxed and primitive doubles using different range checks when converted to `byte` and similar types; `get` truncating fractional indices on strings and Java arrays; `byte-array` and `short-array` rejecting an initial value; `.indexOf` and `.contains` disagreeing on a sequence of a primitive array; and `.lastIndexOf` on a list using `equals` instead of `=`.
The tests also found areas where the implementations matched. `PersistentHashMap` and `PersistentHashSet` matched `java.util.HashMap` over 3 million operations mixing persistent and transient updates. The tests covered maps remaining unchanged before an update, `ArrayNode` splits and merges, and collision nodes down to depth 7. `PersistentTreeMap` matched `java.util.TreeMap` and retained red-black tree invariants, while vectors matched `ArrayList` around every tree boundary tested.
For numeric code, Lean proved at 64 bits that the check `Numbers` uses to choose the fast path for addition and subtraction agrees with the overflow check. `bv_decide` timed out at 16 bits for multiplication, so I checked 8 and 10 bits instead. I also proved up to 32 bits that `clojure.core/mod` matches the floored remainder. Every property proved in Lean holds for the model shown here; I checked that each model matches the Java code by comparing it with the source and the reproduced behavior.
