Skip to main content
Back

SJTU's LeetCode-C-Spec Benchmark Lets AI Write and Verify Its Own Code Proofs

A new neuro-symbolic pipeline turns natural language problem descriptions into machine-checkable C function contracts, filtering plausible-but-wrong outputs via counterexample proofs.

Pro

Paper, Full Access
  • New neuro-symbolic pipeline generates memory-aware formal specifications for C functions from natural language and signatures.
  • Uses in-context learning plus iterative refinement guided by QCP separation-logic prover diagnostics.
  • Validates candidates by attempting to prove the negation of the spec with concrete counterexamples.
  • Introduces LeetCode-C-Spec-200, a benchmark harder than prior C verification datasets.
  • Compiler feedback fixes syntax; symbolic refutation filters false positives LLM-as-judge accepts.
  • Deliberately avoids loop invariant synthesis, focusing only on function contracts.

Formal verification has always had an uncomfortable dependency: before you can prove a C function correct, someone has to hand-write a precise mathematical specification of what it does to memory. That someone is usually a domain expert, and the bottleneck has only gotten worse as LLMs pump out low-level systems code that nobody has actually verified. A new paper from Shanghai Jiao Tong University proposes a way to let the models write those specifications too, then use symbolic provers to catch it when they lie.

The work, titled Neuro-Symbolic Generation and Validation of Memory-Aware Formal Function Specifications, deliberately sidesteps the hardest part of traditional verification. The authors focus exclusively on function specification generation, deliberately avoiding the synthesis of complex loop invariants that are central to traditional verification pipelines. Function contracts describe what a routine promises about its inputs and outputs, including how it touches the heap. Loop invariants are a separate, historically harder synthesis problem. Splitting them apart makes the tractable half tractable.

Why memory specs are the hard currency

The target verifier is QCP, a separation-logic based C verifier developed at SJTU. Separation logic is the flavor of program logic that reasons about disjoint heap regions using a separating conjunction operator, letting you state things like "this pointer owns this list, that pointer owns that tree, and they do not overlap." For C function calls, QCP users are required to provide function specifications to describe C function behavior, and during symbolic execution QCP checks whether the functions' preconditions are satisfied and the calling commands' postcondition accordingly. QCP also allows users to manually write Rocq proof code to fill proof gaps about user-defined predicates that cannot be verified automatically by an SMT solver.

In practice this means a spec for something like linked-list reversal reads like Require sll(p, l) and Ensure sll(__return, rev(l)), with the separating conjunction ensuring memory regions do not alias. Writing that fluently in an assertion language is exactly the skill LLMs stumble over.

Generate, compile, refute

The pipeline is an agentic loop built around two very different kinds of feedback. It first produces candidate specifications via in-context learning, and then iteratively refines them using compiler diagnostics from symbolic provers and the verification toolchain. Steps roughly break down as:

  • Prompt an LLM with the natural-language problem, the C signature, and few-shot examples of valid separation-logic annotations.
  • Feed the candidate spec into the QCP toolchain and collect syntactic or type errors from the compiler and prover.
  • Re-prompt the model with those diagnostics until the spec parses and type-checks.
  • Run a semantic validation step that tries to disprove the specification using concrete inputs.

That last step is the interesting one. The authors validate candidate specifications by constructing a proof for the negation of the specification with concrete examples, enabling machine-checked rejection of plausible-but-incorrect specifications. If the prover can build a counterexample proof against the spec, the spec is wrong, full stop. There is no room for an LLM judge to be charitable.

The prompt itself is engineered against the failure modes the authors observed. Examples are drawn from three sources: QCP's standard test suite including operations such as list reversal and binary search tree insertion, several synthetic problems inspired by algorithmic patterns common in competitive programming but not directly from LeetCode, and one representative LeetCode problem that is excluded from the evaluation benchmark to avoid data leakage. They also incorporate common error patterns that summarize frequent syntactic mistakes made by LLMs when generating separation logic annotations.

A tougher benchmark than usual

To measure any of this, the paper introduces LeetCode-C-Spec. It is a benchmark of 200 LeetCode problems for formal C specification generation, featuring more complex algorithms than prior C verification benchmarks. Prior C verification datasets tend to be dominated by numerical loops and toy pointer manipulations. LeetCode-style problems bring in linked lists, trees, hash tables, and variable-length arrays with real algorithmic content.

The difficulty split is roughly what you would expect from a LeetCode mining exercise:

DifficultyShare of benchmark
Easy31.0%
Medium48.5%
Hard20.5%

The authors chose LeetCode problems because they provide standardized data structures, input-output formats, and explicit test cases, with substantially higher algorithmic complexity than existing C verification benchmarks.

What actually works, and what does not

Two claims fall out of the experiments. First, the proposed agentic workflow significantly improves the syntactic validity of LLM-generated function specifications. Compiler-driven refinement is the workhorse here: a syntactically broken spec is a fast, cheap thing to detect, and re-prompting with the exact prover error message closes most of the gap.

Second, and more important, the refutation step catches semantic bugs that pure LLM-as-judge setups wave through. An LLM asked to grade whether a spec matches an English description will happily approve one that is subtly wrong on edge cases involving null pointers, empty lists, or aliased regions. A theorem prover constructing a concrete counterexample will not. This is the real breakthrough of the paper beyond the headline: swapping subjective neural evaluation for objective symbolic refutation on the exact class of bugs where LLMs are known to be overconfident.

Where this fits and where it does not

The intended use case is clear. If you are running a verification pipeline over C code, whether hand-written or generated by a coding agent, the specification-writing step is currently a manual choke point. A tool that produces machine-checked contracts from a docstring and a signature is directly usable as an assistant that a human engineer reviews rather than authors from scratch.

The limitations are equally clear:

  • It punts on loop invariants, which are still required for many full-proof workflows.
  • It is tied to QCP and separation logic. Frama-C / ACSL users would need a port.
  • Refutation catches wrong specs but does not prove a passing spec is complete. A spec can be too weak and still survive.
  • Complex user-defined predicates that need Rocq proof gaps filled in are still out of scope.

The broader trend it fits into is the shift from LLM-only code assistants toward orchestrated loops where a neural generator proposes and a symbolic engine disposes. Similar patterns are showing up in specification synthesis, RTL-to-spec translation, and autoformalization for theorem proving. The lesson is the same across all of them: neural fluency plus symbolic ground truth beats either one alone, and the useful research question is where exactly to draw the seam between them.