dotnet-csverify
0.5.5
See the version list below for details.
dotnet tool install --global dotnet-csverify --version 0.5.5
dotnet new tool-manifest
dotnet tool install --local dotnet-csverify --version 0.5.5
#tool dotnet:?package=dotnet-csverify&version=0.5.5
nuke :add-package dotnet-csverify --version 0.5.5
CsVerify
An applied type system for C#, written entirely in C#.
CsVerify brings the ideas of ATS (Hongwei Xi's Applied Type System) to C#. In the imperative style of Xanadu, ATS's C-like sibling, it gives you:
- static indices,
- dependent refinements,
- loop invariants,
- termination metrics,
- proof functions,
- linear views of memory.
It adds no syntax. There is no preprocessor, desugaring step or compiler fork. Every file you write is ordinary C#: it compiles with the normal compiler and gets full IDE support. The extensions are:
- a small specification library (
CsVerify.Spec). Its statements are erased from normal builds. - Roslyn analyzers that check linearity and ghost hygiene as you type.
- a verifier that translates each method and its specifications to Boogie, which proves them with Z3.
- an optional proof-erasure pass (
dotnet csverify erase, or-p:CsVerifyErase=true). It builds the program with every proof removed and checks that what is left compiles. This is not a language layer: the sources stay plain C#, and a normal build of them is the reference meaning, which erasure must and does preserve.
CsVerify is the proving counterpart of Tarp. Tarp measures, by running your tests, which conditions and decisions were exercised (MC/DC). CsVerify proves statically that index bounds, contracts, invariants and termination hold on every path. Both work on the same plain C# sources.
public static int BinarySearch(int[] a, int key)
{
Spec.Requires(Spec.Forall(0, a.Length, i => Spec.Forall(i, a.Length, j => a[i] <= a[j])));
Spec.Ensures<int>(r => -1 <= r && r < a.Length);
Spec.Ensures<int>(r => r < 0 || a[r] == key);
Spec.Ensures<int>(r => r >= 0 || Spec.Forall(0, a.Length, i => a[i] != key));
int lo = 0, hi = a.Length;
while (lo < hi)
{
Spec.Invariant(0 <= lo && lo <= hi && hi <= a.Length);
Spec.Invariant(Spec.Forall(0, lo, i => a[i] < key));
Spec.Invariant(Spec.Forall(hi, a.Length, i => a[i] > key));
Spec.Decreases(hi - lo);
int mid = lo + (hi - lo) / 2; // every a[...] below is proved in bounds
if (a[mid] < key) lo = mid + 1;
else if (a[mid] > key) hi = mid;
else return mid;
}
return -1;
}
What it does
- Proves your C# correct, with no new syntax. Preconditions, postconditions, loop invariants, termination
measures, lemmas and ghost values are calls to
Spec.*that a normal build compiles away. The verifier turns them into Boogie procedures, which Boogie proves with Z3, method by method. - Rules out run-time errors. Array and string indexes are in bounds, nothing null is dereferenced (C#'s nullable
annotations become contracts,
null!included), nothing divides by zero, and integer arithmetic does not overflow silently (it is bounded, as at run time). - Strings and bits, exactly. A string is its characters:
s[i],foreach, concatenation andSubstringcan be stated and proved, up tos.Substring(0, n) + s.Substring(n) == s. Shifts and bitwise operators are exact, with C#'s own semantics, so a packed word can be proved to come apart again. - Linear views of memory and objects, as in ATS. Pointers with
PointsToviews, array views that split and join, owned objects, predicates over linked structures, fractional permissions. Roslyn analyzers check that linear values are used exactly once, in the IDE as you type. - Locks. What a lock guards, by
[GuardedBy], is used only under it, so it can't race. Even in code with no specifications, a field written under a lock, or under aSemaphoreSlimof count 1, and used elsewhere without it is reported. Lock invariants hold whenever a lock is free. Algorithms can also be modeled and proved over every interleaving: a ticket lock's mutual exclusion and progress, and dining philosophers without deadlock. See Async and concurrency. - Objects too. Data types with closed hierarchies, object invariants, heap fields with inferred frames, dynamic
frames (
Spec.Modifies, for fields and arrays, andSpec.Reads), virtual members with contracts, exceptions, floating point as IEEE 754. - Proofs erased, and checked to be.
-p:CsVerifyErase=truebuilds the program with every proof removed, and checks that what runs never depended on one.-p:CsVerifyChecked=truekeeps the specifications that can run as run-time checks instead (the checked build). - Static proof, then dynamic coverage. With Tarp: verify, erase, and run the tests under Tarp's MC/DC instrumentation, on the program that runs.
- Used on real code. CsLemon, SQLite's parser generator ported to C#, verifies completely (and the proof found a bug in the C original); the samples port proofs from ATS, Dafny and ACSL by Example, and prove UTF-8, Base64 and Knuth-Morris-Pratt search correct.
- Finds what linters can't, and leaves them what it doesn't look for. In a benchmark against C# linters and InferSharp,
CsVerify reports all 14 bugs hidden behind an alias, a helper or a value that is only sometimes bad, where the
other tools together report 4. They report six bugs it doesn't: code that runs fine but isn't what was meant, such
as
d == double.NaN. So it complements them rather than replacing them.
What it does not prove, and why, is listed in Limitations; what "verified" claims, what it rests on, and why each part keeps it, in Soundness.
Using it in your project
You need the .NET 10 SDK and Z3 on PATH (brew install z3, apt install z3,
winget install z3), or CSVERIFY_Z3=/path/to/z3.
Reference the CsVerify package (the Spec library, the analyzers, and the MSBuild targets) and install the
verifier, the dotnet-csverify tool:
dotnet add package CsVerify
dotnet new tool-manifest # once per repository
dotnet tool install dotnet-csverify
Write specifications (see Writing specifications), then verify:
dotnet build -p:CsVerifyEnabled=true # verifies each project after it builds; failures are build errors
dotnet csverify src/MyProject # or from the command line (files, directories, .csproj)
src/MyProject/Orders.cs(12,24): error CSV1003: index in 'a[i]' might be out of range
csverify: 4 of 5 methods verified, 42/43 obligations proved
Test projects are not verified (set CsVerifyExclude to leave out others). A normal build is unaffected: the
specifications compile away. To build the program with the proofs removed, and test it with Tarp's MC/DC coverage:
dotnet build -p:CsVerifyErase=true
dotnet tool install dotnet-tarp
dotnet tarp test -p:CsVerifyErase=true
Running the verifier has the options and diagnostics; CI shows it in GitHub
Actions and Azure DevOps, with Tarp. Until the packages are on nuget.org, take them from a
GitHub release (--add-source <directory>), or build them
(below).
Building from source
dotnet build # everything
dotnet test # the tests (they need Z3)
dotnet run --project src/CsVerify.Verifier -- -v samples/Good
bash samples/e2e.sh # the packages end to end: verify, erase and test with Tarp a copy of samples/Shop
bash corpus/report.sh # the library report on the corpus: what real code's failures come from, ranked
bash bench/linters.sh # the linter benchmark: which bugs CsVerify, C# linters and InferSharp each report
src/CsVerify.Spec Spec, Ghost<T>, attributes, Ptr<T>/PointsTo<T>/Mem (net10.0, package "CsVerify")
src/CsVerify.Analyzers LinearityAnalyzer, GhostAnalyzer, PurityAnalyzer (netstandard2.0)
src/CsVerify.Verifier the verifier: contracts, CFG to Boogie (Boogie/), static checks, erasure, MSBuild targets
(net10.0, tool "dotnet-csverify")
tests/CsVerify.Tests xUnit: analyzer rules, erasure, verifier proved/refuted per feature, samples
samples/Good, samples/Buggy, samples/Shop
docs the documentation
lemon/CsLemon cslemon: Lemon ported to C# and verified
lemon/CsLemon.Runtime the verified LR interpreter and validators, the run time of its C# parsers
lemon/tests differential tests against C Lemon and its parsers, grammars
corpus C code ported without specifications (jsmn, picohttpparser): what the library report measures
bench/Linters the linter benchmark: bugs linters look for, direct, hidden and harmless (README.md)
dotnet pack src/CsVerify.Spec and dotnet pack src/CsVerify.Verifier make the two packages. CI and releases are
in CI.
Samples
samples/Goodverifies completely, with no analyzer diagnostics: arrays and searches with full functional specifications, lemmas by induction, ghost indices, data types and their proofs, mutable and owned objects, dynamic frames, views of memory and arrays, exceptions, floating point, null references, strings and bits (UTF-8, Base64, Knuth-Morris-Pratt, number conversions, escaping). Many are ports: from ATS (the INT2PROGINATS book, libatsc, ATS-playground, rtrace), Dafny's tutorial, test suite and standard library, ACSL by Example, and dafny-lemon.samples/Buggyhas a planted bug in each method, which the verifier or the analyzers must report: off-by-one indexes, wrong invariants, leaks and double frees, unsound proofs.samples/Shopis a small solution using CsVerify from its package, as you would:samples/e2e.shverifies it, erases it and runs its tests through Tarp.lemon/is CsLemon (below).
Samples describes each one.
CsLemon
lemon/ holds a larger example: the LEMON LALR(1) parser generator (SQLite's), ported from C to C# and verified
completely (no unexpected exceptions or silent overflow, non-null fields, invariants of its data structures; proving
one found an out-of-bounds write in C Lemon). Its output is byte-identical to C Lemon's on SQLite's grammar and the
others tested. It comes with an LR(1) interpreter proved sound, and validators for safety and completeness after
Jourdan, Pottier and Leroy's Validating LR(1) Parsers, and can generate C# parsers that run the verified
interpreter. See lemon/README.md.
Documentation
- Writing specifications: ATS concepts in C#, the specification library, the rules.
- How it works: the analyzers, and how the verifier turns code into verification conditions.
- The Boogie engine: C# lowered to Boogie, Dafny's and Spec#'s intermediate language, construct by construct.
- Data types and objects: data types and invariants, mutable and owned objects, predicates, dynamic frames, object-oriented code.
- Integers, exceptions, async, floating point and null.
- Contracts for code without sources:
[ContractsFor]stubs and[Model]functions for the BCL and other assemblies, and the BCL contracts CsVerify ships. - Proof erasure.
- Running the verifier: the command line, MSBuild, diagnostics.
- CI: GitHub Actions, Azure DevOps, with Tarp; building and releasing CsVerify.
- Samples.
- Limitations.
- Soundness: the claim, the trusted base, and the published result behind each mechanism.
- Entity Framework Core: queries as the database runs them; the change tracker, transactions and one operation at a time, checked.
- CsVerify and Coyote: the concurrent samples, checked builds of them, run under Coyote's scheduler; every wait checked to end; what each tool finds.
Licence
MIT: see LICENSE. The ported samples are rewritten, not copied, from the sources named under Samples. Those that carry a licence are MIT or MIT-style (Dafny, ACSL by Example, the INT2PROGINATS code, the ATS-CodeBook), and the rest are the author's own ATS repositories.
References
-
- Xi, Applied Type System (extended abstract), TYPES 2003.
-
- Xi, Applied Type System: An Approach to Practical Programming with Theorem-Proving, arXiv:1703.08683.
-
- Zhu, H. Xi, Safe Programming with Pointers through Stateful Views, PADL 2005.
-
- Xi, Imperative Programming with Dependent Types (Xanadu), LICS 2000.
-
- Xi, F. Pfenning, Eliminating Array Bound Checking Through Dependent Types, PLDI 1998.
-
- Xi, Introduction to Programming in ATS, https://ats-lang.github.io (INT2PROGINATS).
-
-
-
- Leino et al., Dafny tutorial and test suite, https://github.com/dafny-lang/dafny.
-
-
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net10.0 is compatible. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
This package has no dependencies.
| Version | Downloads | Last Updated |
|---|---|---|
| 0.5.23 | 0 | 11/23/2026 |
| 0.5.22 | 0 | 11/23/2026 |
| 0.5.21 | 57 | 11/22/2026 |
| 0.5.20 | 56 | 11/22/2026 |
| 0.5.19 | 63 | 11/22/2026 |
| 0.5.18 | 61 | 11/21/2026 |
| 0.5.17 | 55 | 11/21/2026 |
| 0.5.16 | 55 | 11/21/2026 |
| 0.5.15 | 60 | 11/21/2026 |
| 0.5.14 | 55 | 11/21/2026 |
| 0.5.13 | 61 | 11/20/2026 |
| 0.5.12 | 60 | 11/20/2026 |
| 0.5.11 | 85 | 11/19/2026 |
| 0.5.10 | 96 | 11/19/2026 |
| 0.5.9 | 75 | 11/19/2026 |
| 0.5.8 | 94 | 11/18/2026 |
| 0.5.7 | 74 | 11/18/2026 |
| 0.5.6 | 80 | 11/18/2026 |
| 0.5.5 | 96 | 11/18/2026 |
| 0.5.4 | 96 | 11/17/2026 |