KC Sivaramakrishnan Building Functional Systems

The Unreasonable Effectiveness of Vibeproving for Bug Finding

We all wish our software did not have bugs. All programming languages make it easier to write buggy code than correct code, but C and C++ are especially good at the former. Alas, the world runs on C, and much of that C is multi-threaded, which is where the fun(!) begins. I have contributed to this dumpster fire by adding more multi-threaded C to the OCaml runtime to support shared-memory parallel programming.

I spent a good part of my youth debugging multicore OCaml crashes with GDB and rr, and I still look back on that fondly. But I’d rather find these bugs before someone else has to debug them in production. This talk is about using coding agents for bug finding, assisted by formal proofs vibecoded by agents. I was surprised by how good they were, especially with formal verification keeping them honest.

This is the written version of the talk I gave at IndiCS 2026 (Frontiers in Verification and Analysis for Concurrency) at Infosys Mysore on 1 December 2026. The slides are here. I’ve added notes below each slide, roughly following what I said.

Title slide: The Unreasonable Effectiveness of Vibeproving for Bug Finding. Auditing the OCaml 5 runtime system.

For the last month or so, I have been pointing coding agents at the OCaml 5 runtime system and asking them to find bugs. This talk is a report on how that went.

This talk: 124 issues filed in three weeks, 83 public and 41 private; 67 concurrency bugs; many with a reliable reproducer.

The agents did two kinds of things. They audited the C code of the runtime directly, and they built a model of the garbage collector (first in Veil, then in plain Lean) which we used to prove things about the collector.

Between 5 and 20 November, we filed 124 issues. 83 of them are public issues on ocaml/ocaml, and 41 went through private channels. 67 of the 124 involve concurrency in some form: multiple domains or threads, or asynchronous callbacks such as signal handlers. Many of the reports include a reliable reproducer, which is unusual for bugs that occur this rarely. Although we used the model to prove properties of the collector, none of the 124 bugs was first found by a proof.

OCaml 5: a functional-first language; OCaml 5 added shared-memory parallelism and effect handlers, with a rewritten runtime of about 60,000 lines of C and assembly.

OCaml is a functional-first language, first released in 1996. Jane Street alone has more than 100 million lines of OCaml. OCaml 5, released in December 2022, added shared-memory parallelism and effect handlers to the language (the Multicore OCaml project that led to it started in 2014).

To get there, we rewrote the runtime. It is about 60,000 lines of C and assembly now, most of which is the garbage collector and the machinery for managing domains. Every OCaml program depends on this code, and a bug in it shows up as a crash or as silent memory corruption.

Three levels of concurrency: domains run in parallel; systhreads within a domain; fibers within a thread. A suspended fiber still holds live values.

OCaml 5 has concurrency at three levels. Domains are the unit of parallelism, and you’d typically run one per core. Each domain may have several systhreads, which are OS threads, but only one of them runs OCaml code at a time (the one holding the domain lock), and systhreads don’t migrate between domains. Within a thread, there may be many fibers. Each fiber has its own stack, and an effect handler can suspend and resume it; schedulers such as Eio run lots of fibers on a single thread.

So at any point, each domain runs one fiber of one thread. The suspended fibers aren’t running, but their stacks still hold live values, and the collector has to find them.

OCaml 5's heap: domain-local minor heaps and one shared major heap. Pointers from the major heap to a minor heap are recorded in the writer's remembered set.

Each domain has its own minor heap, and all the domains share a single major heap. Pointers can go between any two of these heaps. If a domain writes a pointer to a minor heap object into a major heap object, the location is recorded in that domain’s remembered set.

OCaml's garbage collector: each domain allocates into its own minor heap arena; when an arena fills, all domains stop and collect their minor heaps in parallel; survivors are promoted.

The collector is generational. Domains allocate into their own minor heap arena (a pointer bump, no locks). When any one arena fills up, all the domains stop and collect their minor heaps in parallel, and the survivors get promoted to the shared major heap.

One major cycle: sweep slices, a stop-the-world minor GC that marks roots, interleaved mark and sweep slices, short stop-the-world phase changes, and a stop-the-world section that ends the cycle.

The major heap is collected incrementally and concurrently, in a cycle. Each domain sweeps the previous cycle’s garbage in small slices, in between running its program. Marking begins inside a stop-the-world minor collection, at which point every minor heap is empty, and the roots get marked. From then on, every domain interleaves mark slices and sweep slices, so marking for this cycle overlaps with sweeping for the previous one.

Phase changes (the variable is caml_gc_phase) happen in short stop-the-world sections: first to final marking, then to the ephemeron sweep. Another stop-the-world section ends the cycle, and whatever wasn’t marked becomes the garbage that the next cycle sweeps.

Marking runs while the program runs: the program stores a pointer to C into A, which is already scanned, then removes the other path to C; caml_modify's deletion barrier darkens the overwritten value.

Marking is the textbook algorithm: start from the roots, follow the pointers, and mark whatever you reach. The difference is that it runs in small slices on every domain, while the program keeps running and changing the heap underneath it.

Consider the example on the slide. The collector has already scanned A. Now the program stores a pointer to C into A, and then removes the only other path to C (through B). If nothing else happened, C would never get marked, and it would be freed while still reachable. OCaml uses a deletion barrier to prevent this: caml_modify darkens the value that it overwrites, so C gets marked when the program overwrites B’s field. Every caml_modify pays for this barrier.

When is marking over? A shared counter num_domains_to_mark; a domain with an empty mark stack decrements it, but a caml_modify can darken a block and count the domain back in.

How do the domains agree that marking is over? No domain can see all the marking work, so there’s a shared counter, num_domains_to_mark. When a domain’s mark stack becomes empty, it sets its marking_done flag and decrements the counter. But the program on that domain is still running, and a caml_modify might darken a block, which gives the domain more marking work. In that case, the domain clears marking_done and increments the counter again. Once the counter hits zero, all the domains stop and check one more time. There are several such counters guarding the cycle.

The set of domains keeps changing: a domain can be spawned or exit mid-cycle, and a stop-the-world section must stop every current member.

So far I’ve assumed a fixed set of domains. If a domain is spawned during a cycle, the collector has to count it in at the right point. If a domain exits during a cycle, the other domains take over its unfinished work. A stop-the-world section has to stop all the current domains even as the set of domains changes.

Meet the awkward squad: finalisers, weak references and ephemerons; domains, systhreads and fibers that come and go; asynchronous callbacks; relaxed memory underneath.

None of this is hidden from the program, and there’s more. Each of the features below is simple in isolation, but the bugs are in how they interleave. OCaml has two kinds of finalisers, weak references and ephemerons, all of which interact with the collector. Domains and systhreads come and go in the middle of a cycle, and fibers get moved or dropped. Signal handlers, memprof callbacks, finalisers and asynchronous exceptions run at poll points, and any of them can allocate, raise or trigger a collection. So a poll point is an interleaving point even when there’s only one thread. And all of this runs on relaxed memory.

Why I care: debugging runtime concurrency bugs while building Multicore OCaml, tens of person-years across the team; a verified sequential GC in F*.

When we were building Multicore OCaml, I spent a large chunk of my time chasing concurrency bugs in the runtime with gdb and rr. My estimate is that, across the team, it added up to tens of person-years. These bugs are rare, depend on timing and are hard to reproduce. Quite often, “fixed” really meant “we haven’t seen it again”.

We had previously built a mechanically verified GC for OCaml in F* and extracted it to C (Journal of Automated Reasoning, 2025). That collector is sequential and targets OCaml 4.14. For the concurrency bugs we had spent so much time debugging, I wanted to see whether an agent could have found some of them for us.

Attempt 1: agents read the runtime's C and report suspects, each reproduced before filing. DeepSeek v4.1 Flash: 20 public, 10 private; Kimi K3.1 Pro: 2 public, 3 private. Only 5 of the 22 public ones are concurrency bugs.

I first asked the agents to audit the C code. They read the runtime and report suspects, and before we file anything, each suspect has to be reproduced on the real runtime.

The first filings, between 5 and 12 November, came from DeepSeek v4.1 Flash (20 public, 10 private) and Kimi K3.1 Pro (2 public, 3 private). These are real bugs, but most of them are sequential: integer overflows, missing checks, mistakes on failure paths. Only 5 of the 22 public issues were concurrency bugs.

In my experience, Claude and Codex would often decline to do this kind of low-level bug hunting, or come back with very little. DeepSeek v4.1 Flash did not.

Attempt 2: model the runtime as a distributed system. The agent writes a model from the C, assertions become safety properties, the model is checked and proved, and suspects are reproduced.

For the concurrency bugs, we treated the runtime as a distributed system: the domains are processes, the counters and barriers are protocols, and the stop-the-world sections are rounds. So we get an agent to write a model of it from the C. The assertions that are already in the runtime become safety properties, which we model check and then prove. Anything suspicious gets reproduced on the real runtime, and it either becomes a bug report or a fix to the model.

We vibecode the model. The question is how faithful it is to the C.

Veil: a foundational alternative to TLA+, in Lean.

We started with Veil, a framework for transition systems in Lean. You can think of it as TLA+, but in Lean. Veil lets you model check a system and prove its safety and liveness properties in the same place, with proofs by tactic and an SMT solver (cvc5) helping out.

One heisenbug, ocaml/ocaml#15304: a domain sweeping a weak array reads a dead key, another domain stores a live value into the slot, and the sweep writes none over it. The control writes only if the slot still holds the key.

Here’s one of the bugs that came out of the Veil model, ocaml/ocaml#15304. Domain A allocated a weak array and is responsible for cleaning it, while domain B’s program uses it. One of the slots holds a key k that is dead (unmarked).

Domain A, while sweeping, reads the slot and sees the dead key (this is caml_ephe_clean in weak.c). Before A gets to clear it, domain B stores a live value v into the same slot using Weak.set. Then A writes “none” into the slot, without checking whether the slot still holds k, and B’s store is lost. Weak.get now returns None for a value that the program is still holding on to. The fix is to make the write conditional: only clear the slot if it still holds k. With that, the store survives.

We found this by splitting caml_ephe_clean in the model into the steps that the C actually takes. Once we did that, the model checker found the 10-step interleaving, and the proof failed at the same write.

Does it matter? A post: 'i just used TLA+ to formally verify my app and fix 28 bugs that would never have triggered anyway'.

A bug found in a model may never happen in a real run. This post appeared the day before the talk.

Making it reproducible: without a hook, not reproduced; with a test-only hook, 50 of 50 stores lost; with the conditional write, 0 of 50; later without a hook, 3 lost on trunk and 55 on 5.5.0, with two controls at 0.

The window for #15304 is a few instructions wide. The model gives the interleaving in 10 steps (the sweep reads, the program stores, the sweep writes), but running the real program as is didn’t reproduce the bug.

So we added a test-only hook that holds the sweep between the read and the write. With the hook, 50 out of 50 stores were lost. With the same hook and the write made conditional, none of the 50 were lost.

Later, we managed without any hook. With two domains doing 15 million stores per run, we lost 3 stores on trunk and 55 on OCaml 5.5.0. Two control runs lost none: one used keys that the sweep never rewrites, and in the other, the storing domain was the one that allocated the array.

The Veil model grew fast: actions in the model from 11 to 16 November.

Between 11 and 16 November, the Veil model grew from a few dozen actions to well over a hundred. The agents first added the domain lifecycle, then ephemerons and the program, followed by finalisers and shutdown. They added the deletion barrier, split the ephemeron scan and combined 16 fragments into a single model. They then added the minor heap and the participants in stop-the-world sections, split the weak operations and added support for joining a cycle.

Veil found six bugs (#15288, #15293, #15299, #15304, #15305, #15308), then stopped scaling: elaboration grew faster than linearly, and large bounded runs did not finish.

The Veil model found these bugs while we were building it:

  • #15288: ephemeron data left unmarked after a key store
  • #15293: a lost ephemeron round at the end of marking
  • #15299: a finaliser called on a reachable value
  • #15304: Weak.set lost when the collector rewrites the slot
  • #15305: ephemeron data of a dead key returned
  • #15308: the major GC never completes a cycle after a failed Domain.spawn

And then it stopped scaling. Elaboration time grew faster than linearly in the number of fields, actions and clauses; a larger view of the model did not finish elaborating in 19 minutes. Large bounded runs did not finish either, with 6 workers and 20 GB of memory. Even a one-action edit took 97 seconds to check.

Long check times are more of a problem for agents than for people. An agent works well when it can make a guess, get feedback from a check, and try again; what matters is the number of these feedback loops. With each check taking minutes, the agent spends most of its time waiting and gets through too few loops. So we switched to writing the state machine directly in Lean, and split the proof up so that a change to one action only rebuilds that action’s file.

The model is a state machine: sorts, a world fixed for a run, a state of 24 records and 223 fields; step applies an action's effect if its guard holds; Reachable is the least set of states closed under step.

The Lean model is a state machine. The sorts are finite types for domains, blocks, key indices, minor-heap slots, threads and stacks. The theorems hold for every finite choice, so they are about any number of domains and blocks, even though any one run has a fixed number. The world holds the things that don’t change during a run, such as which blocks are ephemerons. The state has 24 records and 223 fields, named after the runtime variables where there is one (the phase, the mark bits, marking_done and so on).

Each action has a guard and an effect, and step applies the effect if the guard holds (otherwise it returns none). Reachable is the least set of states that contains the initial states and is closed under step. Actions fire one at a time, each sees the whole state, and any enabled action may fire next. There is no fairness or pacing, and memory is sequentially consistent.

An action is a guard and an effect: caml_modify storing v into the field of p darkens the old value o, then writes v.

Here’s what an action looks like, for caml_modify storing v into a field of p. The guard says that the collector is marking or sweeping, that both p and v are allocated and held (marked, or in some domain’s local roots), and that p.f currently points to o. The effect darkens o (that’s the deletion barrier) and then sets p.f to v. Darkening o also pushes it on the writer’s mark stack, clears the writer’s marking_done and counts the writer back in.

Each of the 256 actions cites the lines of C that it models (here, memory.c). Wherever other code can run in the middle of a C operation, we split the operation into several actions. In the target model, this store is actually two actions, just as in the C: read and darken the old value, then store the new one.

What we proved about the collector: memory, marking, ephemerons, finalisers and the minor GC, in every reachable state at the target configuration.

What did we prove? The theorems talk about every reachable state of the model at the target configuration, which is trunk with the fixes we proposed. For the collector itself:

  • Memory: nothing reachable in the major heap is freed (the roots being globals, locals and values waiting for a finaliser).
  • Marking: when marking ends, every reachable block is marked, and it stays marked through the sweep.
  • Ephemerons: if all the keys are marked, the data is marked (#15288); the sweep loses no store (#15304), and it never returns dead or freed data.
  • Finalisers: values with a pending finaliser are allocated, and a value registered during marking isn’t queued to run in the same cycle.
  • Minor GC: a young value that’s still held is promoted rather than dropped when it leaves the minor heap, and finalisers never read a freed minor heap.
What we proved about the runtime around the collector: stop-the-world sections, threads and signals.

The same theorem also covers the runtime around the collector:

  • Stop-the-world: there’s at most one leader, every active domain takes part while a section runs, and a failed Domain.spawn leaves no count behind (#15308).
  • Threads: the masterlock has at most one owner; the main thread holds the domain lock while it runs, and the backup thread holds it only while handling an interrupt; a domain that is stopping has no running thread.
  • Signals: while a signal is pending, every domain that hasn’t terminated is either flagged to poll or already acting on it, and an exception from a handler is only ever raised into an OCaml frame.

These are all safety properties about the present state. None of them says that a signal is eventually delivered, or that a cycle ever completes.

How: one inductive invariant. reachable_inv: every reachable state satisfies Inv. 256 actions, 177 clauses, 45,312 proof obligations, all closed.

All of these follow from a single theorem, reachable_inv. For every configuration that satisfies Config.Safe, every state reachable in the full model satisfies the invariant Inv, for any number of domains, blocks and threads, after any number of steps, taken in any order. (Config.Safe has 66 conjuncts over the model’s 117 switches, and the target configuration satisfies it.)

The invariant has 177 clauses. Only a handful of them are things you would care about as a user; the rest are there to make the induction go through. With 256 actions, that gives 45,312 proof obligations, and all of them are closed. Lean’s kernel checks the proof, and there is no sorry, no added axiom and no external solver.

The proof: 39,524 frame obligations, 4,229 automatic, 1,559 bespoke.

There’s one obligation per action and clause, saying that the action preserves the clause. 39,524 of these are frame obligations, where the action writes none of the fields that the clause reads, so a single lemma per clause takes care of them (we generate these from each action’s write set). Another 4,229 are automatic: unfold the clause, the guard and the effect, and automation closes the goal. The remaining 1,559 need a bespoke lemma, written by an agent, that uses other clauses of the invariant.

This works because most clauses read only a few fields and most actions write only a few, so most pairs end up being frames. It also means that editing one action only rebuilds that action’s file.

Size and effort: 376,976 lines of Lean in 7,291 files, at commit 0c9e6450a; 20 days and 1,297 commits.

The Lean development took 20 days (9 to 28 November) and 1,297 commits. It’s about 377,000 lines of Lean in 7,291 files, though the model itself (actions, state and core definitions) is only about 26,000 lines. Most of the rest is proof: 127,434 lines of generated proof assembly, 89,844 lines of proof leaves produced by tools, and 54,832 lines of bespoke proofs that the agents wrote.

Known bugs are reachable states: each known bug has a switch; with it off, a state breaking a clause is reachable; with it on, the same steps are refused.

If the invariant still held with every known bug present, the theorem might be vacuous. We check this using a switch for each known bug in the model. Turning the switch off introduces the bug, and Lean checks a run that reaches a state violating one of the clauses of the invariant. Turning the switch on restores the fix, and the model then refuses the same sequence of steps.

For #15288, turning off the switch set_darkens gives a 7-step run that breaks a clause, and a 10-step run that reaches a use-after-free. With the switch on, the same run is refused at step

  1. These runs go through the same step function that the theorem is about.
Where the bugs came from: 28 from design reading, 14 from clauses used as a checklist, 5 from splitting operations, 2 from model counterexamples, 1 from weakened guards, 0 from the proof.

We counted the new runtime defects found while modelling, by the stage at which we found them. There were 50 in all:

  • 28 from design reading, i.e. working out what the model has to say, including for new layers
  • 14 from using the clauses of the invariant as a checklist against the C
  • 5 from splitting operations where the C can interleave
  • 2 from model counterexamples
  • 1 from deliberately weakening a guard
  • 0 from the 45,312 proof obligations, and 0 from trace replay, bounded search or independent review

Working out what to model required us to read the C, including parts of the runtime that we have not yet proved anything about. Of the 28 defects found during design reading, 18 are in compaction, fork and memprof, all still outside the proved model.

Proofs keep the model honest: actions in the model, Veil then Lean; 131 actions on 16 November, 256 by 28 November.

Although none of the bugs was found first by a proof, the proofs constrained how the model grew. Every time an agent added an action to the Lean model, it had to prove that the action preserves all 177 clauses. The model grew from 131 actions on 16 November to 256 on 28 November, by which time all 45,312 preservation obligations were closed.

When the proof failed: two extra assumptions, a clause about global roots that is false in the runtime too, and a weak tricolour invariant that is not preserved. On a branch, 316 more failed proof steps, none an OCaml defect.

Whenever a proof failed, we found a discrepancy between the model and the C. For example, at one point we needed two additional assumptions to get the proof through. Our model allowed root marking while minor heaps still held blocks, and it retained remembered entries that had no source field. The runtime waits until every minor heap is empty before marking roots, and does not retain such entries. Once we corrected the model, the proof went through without either assumption.

A clause saying that global roots are always marked could not be proved, because it’s false in the runtime too (caml_register_global_root does not darken). A root registered during marking is safe because of the snapshot-at-the-beginning (SATB) argument, so we dropped the clause.

Similarly, a weak tricolour invariant could not be preserved. Two domains can race on a slot, and the second caml_modify overwrites a value that nobody darkened. So the proof moved to the runtime’s own argument, which is SATB.

On a branch that adds allocation actions, a further 316 proof steps fail, each with a counterexample. We have not found an OCaml defect among them.

Why trust a vibecoded model? C citations and split operations, controls, replay of recorded runs, and independent review by a different AI system.

The proof checks the agent-written model against its invariant. To check the model against the C, we read the code and replay recorded runs. Each action cites the lines of C that it models, and an audit splits operations wherever another domain could interleave. For each known bug, we check that a violation is reachable with the fix switch off and that the model refuses the same run with the fix on.

Replaying runs of an instrumented runtime against the model found one defect in the model and no runtime bug. A different AI system independently reviews every stage; on 27 November alone it made 213 corrections.

What is modelled: minor and major GC, ephemerons and weak references, finalisers, stop-the-world sections, domains, systhreads, signals, fibers, remembered references. Not established: relaxed memory, refinement, liveness, compaction and memprof.

The model covers the minor and major collectors, ephemerons and weak references, finalisers, stop-the-world sections, creating and terminating domains, systhreads and the domain lock, signals and poll points, fiber reclamation, and remembered references.

The model assumes sequential consistency, whereas the runtime runs on relaxed memory, and we have no theorem relating the model to the C. We have not proved that a cycle completes, which matters for #15308 since it is a hang. The proved model does not yet include compaction or memprof, although memprof is modelled on a branch.

Takeaways: modelling makes agents read code closely; direct audits found mostly sequential bugs; the proof made us go back to the C; a bug report and its fix need a reliable reproducer with a control; a model failure is an executable run.

To summarise what I took away from this:

  1. Modelling makes agents read code closely. Of the 50 defects found while modelling, 47 came from that reading, 3 from model runs, and none from proofs.
  2. Direct audits found real bugs, but mostly sequential ones (5 of the 22 public issues were concurrency bugs).
  3. The proof made us go back to the C. Our model allowed root marking while minor heaps still held blocks, whereas the runtime waits until they are empty.
  4. To substantiate a bug report and its fix, we need a reliable reproducer with a control.
  5. A model failure is a complete, executable run. For #15304, it gave us the interleaving that the reproducer forces.
The same approach on hardware: BOOM, an open-source out-of-order RISC-V core. Can a machine-mode D-cache secret reach a user-mode request address speculatively?

We’ve also tried the same approach on hardware. BOOM, the Berkeley Out-of-Order Machine, is an open-source out-of-order RISC-V core. We wanted to know whether, during speculative execution, a machine-mode secret in the data cache can reach the address of a user-mode memory request.

The model is an executable Lean semantics for the subset of Verilog that BOOM’s netlist uses, which is about a million lines. We run it on slices of the data-cache logic, and its traces agree with Icarus Verilog on the runs we recorded. Codex wrote most of the model, and Claude a part of it. For the attack search we used DeepSeek v4.1 Flash, since Codex and Claude stopped on their safeguards.

BOOM, the same story: a new instance of a Meltdown-style transient, reproduced on Verilator, with a control; the spec's reset premise makes the property hold vacuously.

The agents found a new instance of a Meltdown-style transient: a load that physical memory protection denies still writes the secret to a register, and the instructions that depend on it leak the secret through bus addresses. It was reproduced on Verilator with two programs that differ only in the secret. The root cause is the same as in BOOM #715 and IntroSpectre, but this variant had not been reported before. In the control, an integer chain replaces the floating-point chain, and the dependent request is killed before it issues.

The specification had a problem of its own. Under its reset premise, the refill overwrites the secret byte in both copies, so the property as written holds vacuously.

Open problems: understandability, faithfulness, liveness, scalability and other systems. Worth the effort, or correct by construction?

Several problems remain open:

  • Understandability: when we read the model closely, we still find assumptions that are too strong.
  • Faithfulness: does the model admit the interleavings that matter in the C, on relaxed memory?
  • Liveness: every cycle completes and every finaliser runs (#15308 is a hang).
  • Scalability: each addition costs more than the last. Are there useful abstractions above actions and clauses?
  • Other systems: would this work on the Linux kernel?

And finally, is all of this worth the effort, compared to building the collector correct by construction, as we did with the verified GC?


Creative Commons License kcsrk dot info by KC Sivaramakrishnan is licensed under a Creative Commons Attribution 4.0 International License.