arXiv:2610.30725cs.SEOct 2026

Manuscript Offprint

arXiv:2610.30725

From Natural Language to Verified C: Verification-Guided Generation with ACSL Specifications

Zian Wang, Qin Li, Weisong Sun, Haibin Cai

Zian Wang

East China Normal University

Shanghai, China

51275902168@stu.ecnu.edu.cn

Weisong Sun

Nanyang Technological University

Singapore

weisong.sun@ntu.edu.sg

Qin Li

East China Normal University

Shanghai, China

qli@sei.ecnu.edu.cn

Haibin Cai

East China Normal University

Shanghai, China

hbcai@sei.ecnu.edu.cn

Abstract—AI-assisted code generation amplifies the demand for automated formal verification. Generated programs must not only be executable but also accompanied by machine-checkable evidence of correctness. Existing approaches can synthesize functionally plausible code or plausible-looking specifications from natural-language prompts; however, they do not explicitly co-design the implementation and specification as a unified artifact optimized for verification. Consequently, automated verifiers often fail to discharge proof obligations. Typical failure modes include missing or implicit proof anchors, program structures that are hostile to automated reasoning, and inconsistencies between code-level state and specification-level requirement facts arising from the independent generation of code and specifications. Even when verification succeeds, it may only establish weak specifications that omit requirement-critical behavior. This paper introduces VeriGen-C, a formal-verification-friendly framework for the joint synthesis of C programs and ACSL specifications from natural-language requirements. Code generation and repair are constrained to expose proof-relevant state, to streamline control flow for deductive verification, and to eliminate implementation patterns that hinder automated proof search. Specification generation and refinement are similarly constrained to mitigate verification timeouts and to ensure alignment with the user requirements. Empirical evaluations on HumanEval-X, FM-Bench, and LiveCodeBench Lite demonstrate that VeriGen-C substantially improves the rate of verified and requirement-aligned solutions relative to an interface-compatible baseline, from 5.5% to 44.5%, from 44.3% to 80.0%, and from 6.5% to 58.7%, respectively.

Index Terms—Large Language Models, Formal Verification, Verified Code Generation, ACSL, Frama-C

I. INTRODUCTION

AI-assisted code generation has become pervasive in software development. Recent industrial reports document widespread developer adoption and extensive deployment of automatically generated code in production workflows [1], [2]. Within this context, assurance increasingly emerges as the principal bottleneck. For C programs, testing and manual code review remain indispensable; yet, exhaustive coverage of boundary conditions, memory accesses, arithmetic constraints, and loop-dependent behavior is rarely practical [3], [4].

Formal verification offers a mathematically rigorous approach to increase confidence in generated code by validating program behavior against explicit specifications. For C programs, the verification target is not the executable implementation alone, but a composite C/ACSL artifact. Its source code, contracts, frame conditions, and loop invariants must all be syntactically correct, semantically aligned with the requirement, and amenable to automated reasoning [5]. If either the implementation or the specification is generated incorrectly, the resulting artifact may become unverifiable, or may be verified only with respect to a specification that is too weak to capture the intended requirement. Moreover, AI-generated coding patterns can introduce redundant or proof-irrelevant structures, which in turn induce superfluous verification obligations and degrade tool performance [6].

It is therefore crucial to synthesize artifacts whose implementations and specifications are both suitable for automated verification. Example 1 illustrates this point via a simple requirement: given an array, return the first index whose value equals x, or -1 if no such index exists. The example contrasts verification-friendly and verification-unfriendly realizations.

/* Requirement: first index k with a[k] == x, or -1. / / (A) Implementation issue | WP: Fail_10_19 / /@ requires n >= 0 && (n == 0 || \valid_read(a + (0 .. n -1))); ... same first-index postconditions as below ... */ int find_first_ptr(const int *a, int n, int x) { const int *p = a, *end = a + n; while (p < end) { if (p == x) return (int)(p - a); p++; } return -1; } / (B) Missing bridge invariant | WP: Fail_21_22 / /@ ... ensures \result >= 0 => \forall all integer k; 0 <= k < n => (a[k] == x =>
result <= k); */ int find_first_heavy_spec(const int a, int n, int x) { int i = 0, res = -1; / invariants track res and 0..i, but omit: res >= 0 => all k < res have a[k] != x. */ while (i < n) { ... } return res; }

/* (C) Coupled artifact | WP: Pass_24_24 / /@ requires n >= 0 && (n == 0 || \valid_read(a + (0 .. n -1))); ensures \result == -1 => \forall all integer k; 0 <= k < n => a[k] != x; ensures \result >= 0 => a[\result] == x; ensures \result >= 0 => \forall all integer k; 0 <= k < \result => a[k] != x; */ int find_first_friendly(const int a, int n, int x) { int i = 0, res = -1; / invariants connect res with processed prefix 0..i. */ while (i < n) { if (res == -1 && a[i] == x) res = i; i++; } return res; }

Example 1. Verification-Friendly vs. Verification-Unfriendly Artifacts

In artifact (A) of Example 1, find_first_ptr conceals the searched prefix behind pointer offsets and an early return. Under Frama-C/WP [7], [8], this control and dataflow structure results in a verification-time timeout. The proof attempt must reconstruct an indexed-prefix property from pointer arithmetic p - a and from a loop exit that occurs within the search body, which provides no stable loop-level anchor characterizing the already-processed segment of the array. Artifact (B), find_first_heavy_spec, employs an index-based loop and a semantically rich first-occurrence postcondition. However, it omits the invariant res ≥ 0 ⇒ ∀k < res. a[k] ≠ x, so the verifier cannot derive the firstness postcondition ∀k. a[k] = x ⇒ res ≤ k from the available loop facts. Artifact (C), find_first_friendly, simultaneously restructures the implementation and the specification: the implementation explicitly exposes the processed-prefix state, and the invariants and postconditions express firstness in terms of this shared anchor, enabling the verifier to discharge all proof obligations. This example highlights a more general synthesis and generation challenge: implementations must expose proof-relevant state, specifications must supply the bridging properties required by the verifier, and both must be co-designed around common proof anchors. Existing requirement-formalization and specification-generation approaches [9]–[14] address important aspects of this problem, but they typically formalize natural-language requirements or synthesize specifications for a given program, rather than jointly constructing C code and ACSL specifications from the same natural-language description.

The same coupling challenge persists in the aftermath of verification failure. A non-passing weakest-precondition (WP) outcome may stem from ACSL surface form, missing proof bridges, or implementation structure. Treating these failure modes as independent regeneration targets can disrupt the alignment between program state and contract clauses. Verifier-in-the-loop systems have shown the value of proof feedback in Dafny, Verus, and broader formal-methods workflows [15]–[21]. Nevertheless, code and specifications risk losing shared proof anchors when they are generated or revised as separate artifacts. The implementation may employ compact control-flow structures, low-level pointer manipulations, or library-dependent operations, whereas the specification may articulate requirements in terms of witnesses, value ranges, or intermediate invariants that are never explicitly represented in the code. This divergence forces the verifier to reconstruct implicit relationships between program state and contract clauses. In practice, it frequently yields either undischarged proof obligations or specifications that are progressively weakened until they no longer capture the intended behavior.

This paper proposes VeriGen-C, a formal-verification-friendly framework for joint generation of C programs and ACSL specifications from natural-language requirements. VeriGen-C produces verifier-aware code and ACSL annotations as a tightly coupled artifact. It analyzes non-passing WP outcomes within a bounded repair loop and directs repair actions between specification-only refinement and conservative joint revision of both code and specification. Finally, it performs a requirement-alignment check, so reported success requires both implementation-specification conformance and non-vacuous specification content.

VeriGen-C connects verifier-guided repair, specification synthesis, and verification-aware prompting through a shared proof-anchor interface: the implementation exposes named state, the contract refers to it, and repair preserves that relation. Unlike SpecGen and AutoSpec, which start from an existing program, VeriGen-C decides implementation shape and contract together. A clause-level alignment filter after WP success ensures that verified contracts retain task-specific content.

Contributions. This work makes the following contributions.

• We propose VeriGen-C, a joint code-specification generation framework for verification-friendly C/ACSL artifacts. Unlike approaches that synthesize code and specifications as separate outputs, VeriGen-C constructs the C code and ACSL annotations around shared proof anchors. These anchors make program state, loop invariants, and function contracts support the same verification objectives.

• We introduce verification-oriented construction and refinement principles for generated C/ACSL artifacts. These principles govern both initial synthesis and repair. Implementations are shaped to expose proof-relevant state; ACSL clauses are structured to capture requirement-relevant behavior using verifier-supported forms; and refinement steps are constrained to preserve the code-specification coupling needed for automated proof.

• We empirically evaluate VeriGen-C on HumanEval-X, FM-Bench, and LiveCodeBench Lite. Compared with an interface-compatible one-shot baseline, VeriGen-C improves WP-verified, requirement-aligned success from 5.5% to 44.5%, from 44.3% to 80.0%, and from 6.5% to 58.7%, respectively, without manual post-editing.

II. BACKGROUND AND RELATED WORK

References (33)