> ## Documentation Index
> Fetch the complete documentation index at: https://docs.harmonic.fun/llms.txt
> Use this file to discover all available pages before exploring further.

# C verification

> Verify C code with Aristotle: memory safety, functional correctness, external libraries, and current limitations.

Aristotle formally verifies properties of programs written in C. It can verify whole programs,
libraries, or individual functions. The default deliverable is **memory safety**: a machine-checked
proof that a function executes without undefined behavior (out-of-bounds reads and writes, null or
dangling pointer dereferences, uninitialized reads, signed integer overflow, and so on) whenever its
callers meet a stated set of preconditions. On request, Aristotle also proves **functional correctness**:
that a function computes what you say it should. You state the properties of interest in English, either
in the prompt or in a specification file uploaded with your sources.

After analysis, Aristotle returns a summary listing each property you asked for and its status,
followed by the **verification boundary**: which of your functions are verified, which parts of the
codebase the verification does not reach, and everything the result assumes, such as preconditions on
function inputs or the assumed behavior of an external dependency. If a memory-safety
property is found to be false, you will receive a small standalone C program that fixes the failing
inputs and calls your original implementation, so you can reproduce the violation locally with a sanitizer.

## Under the hood

Aristotle compiles the provided C code **deterministically** into our proprietary Lean formal model
of C execution (onward: the embedding). The proofs Aristotle writes use compositional reasoning based on
separation logic.

Sources are verified **exactly as submitted**. Aristotle never edits your C to make it easier to analyze:
it does not rewrite a function, replace it with a simpler one, or stub out a dependency.

## Supported language and platform features

Aristotle supports integers up to 128 bits, pointers, arrays, structures, unions, dynamic allocation and
deallocation, loops and recursion. Multiple C files can be combined with checked linking. Support also
includes variadic functions, bit-fields, restricted variable-length arrays and selected GCC inline assembly.

**Floating point.** Support covers binary32 `float`, binary64 `double` and x87 extended-precision
`long double`, including arithmetic and conversions.

**Libraries and input and output.** Contracts cover parts of the standard C and POSIX libraries, including
allocation, strings and file I/O. Caller proofs assume these contracts; they do not verify the library or
operating-system implementations. Proof rules also cover buffered file and directory I/O.

**Concurrency.** Support covers thread creation and joining, mutexes, and sequentially consistent atomics.

**Memory leaks.** A proof can show that a program frees all the heap memory it allocates by the time `main`
returns. This covers executions in which `main` and every thread return normally, not those that end through
`exit` or `abort`. You need to explicitly ask for this feature to be enabled in your prompt, as it is not
enabled by default.

## Undefined behavior that Aristotle detects

A successful Aristotle proof rules out:

* accessing memory outside the allocation that a pointer comes from (its provenance), or after the lifetime
  of that allocation has ended, such as after it is freed;
* null dereferences, invalid frees and writes to read-only storage;
* integer division by zero, invalid shift counts, and signed arithmetic overflow (in `-fwrapv` builds,
  addition, subtraction, negation and multiplication wrap instead);
* conflicting accesses to an object that the C sequencing rules leave unordered, such as `i = i++`, in any
  of the evaluation orders that C permits;
* incompatible typed accesses under the effective-type (strict-aliasing) rules, which are checked by default
  and not in `-fno-strict-aliasing` builds;
* reading a local variable before its first assignment, if its address is never taken;
* using an uninitialized pointer for memory access, arithmetic or a call argument, and reading an
  uninitialized `_Bool`.

The embedding does not detect every form of C undefined behavior. It does not enforce `restrict` promises.
Other reads of uninitialized integer or floating-point values, for example from heap memory, an array or a
structure field, give an arbitrary value of the type, and a proof must hold for every such value.

## Installation

Upload your C files in the [web app](https://aristotle.harmonic.fun), or have your AI assistant send them
through the [MCP server](https://docs.harmonic.fun/installation.md#set-up-the-mcp). To submit from the command line, see
[MCP & CLI](https://docs.harmonic.fun/installation.md#use-the-cli). C verification needs no extra setup.

## Example workflow

This example uses a fixed-capacity ring buffer, split across a header `ring_buffer.h`:

```c wrap theme={null}
#ifndef RING_BUFFER_H
#define RING_BUFFER_H

#include <stddef.h>
#include <stdint.h>

#define RB_CAPACITY 8

typedef struct {
    uint32_t data[RB_CAPACITY];
    size_t   head;   /* index of the oldest element   */
    size_t   count;  /* number of elements stored      */
} ring_buffer;

void rb_init(ring_buffer *rb);
int  rb_push(ring_buffer *rb, uint32_t value);
int  rb_pop(ring_buffer *rb, uint32_t *out);
size_t rb_size(const ring_buffer *rb);

#endif
```

and an implementation `ring_buffer.c`:

```c wrap theme={null}
#include "ring_buffer.h"

void rb_init(ring_buffer *rb) {
    rb->head = 0;
    rb->count = 0;
    for (size_t i = 0; i < RB_CAPACITY; i++) {
        rb->data[i] = 0;
    }
}

/* Returns 1 on success, 0 if the buffer is full. */
int rb_push(ring_buffer *rb, uint32_t value) {
    if (rb->count == RB_CAPACITY) {
        return 0;
    }
    size_t tail = (rb->head + rb->count) % RB_CAPACITY;
    rb->data[tail] = value;
    rb->count++;
    return 1;
}

/* Returns 1 on success, 0 if the buffer is empty. */
int rb_pop(ring_buffer *rb, uint32_t *out) {
    if (rb->count == 0) {
        return 0;
    }
    *out = rb->data[rb->head];
    rb->head = (rb->head + 1) % RB_CAPACITY;
    rb->count--;
    return 1;
}

size_t rb_size(const ring_buffer *rb) {
    return rb->count;
}
```

To specify the intended behavior, create a file `ringbuf_spec.md` alongside the sources:

```markdown wrap theme={null}
# Formally Verify the Ring Buffer

Verify that every function in `ring_buffer.c` is memory safe. Callers guarantee that
`rb` points to a valid, writable `ring_buffer` that was initialized with `rb_init`,
and that `out` in `rb_pop` points to writable storage for one `uint32_t` that does
not overlap any part of `*rb`.

Additionally verify the following functional properties:

- After `rb_init(rb)`, `rb_size(rb)` returns 0.
- `rb_push` returns 0 and leaves the buffer unchanged when `rb_size(rb) ==
  RB_CAPACITY`; otherwise it stores `value`, returns 1, and `rb_size` increases by
  exactly one.
- `rb_pop` returns 0 and leaves the buffer unchanged when `rb_size(rb) == 0`;
  otherwise it writes the oldest stored value to `*out`, returns 1, and `rb_size`
  decreases by exactly one.
- Values are returned by `rb_pop` in the same order they were stored by `rb_push`
  (FIFO).
- `rb_size(rb)` never exceeds `RB_CAPACITY`.
```

Place the three files in a directory on their own and submit that directory. The CLI uploads the
directory, except what an ignore rule matches and a few kinds of file it never sends, such as files named
like credentials. In a git repository, git's ignore rules apply; elsewhere, or in a folder git ignores, only
the directory's top-level `.gitignore` does. Add `--include-ignored` to upload what the ignore rules match too,
such as headers a build generates. A link to a file goes up only when the file it points to does too, and a link
to a folder never does. Include every header the sources need and nothing unrelated:

```shell wrap theme={null}
aristotle submit "Perform the formal verification of ring_buffer.c as specified in ringbuf_spec.md" --project-dir ./ringbuf --wait --destination ringbuf_result.tar.gz
```

Note: mention any relevant compiler flags (`-D`, `-I`, `-fno-strict-aliasing`, `-ffp-contract=off`, `-fwrapv`, etc.) or include your build script.
Also include header files, API docs, and relevant conceptual docs for your code's non-open-source dependencies.
Also include source code and/or Linux/x86-64 binaries for all transitive non-open-source dependencies, if you want Aristotle to check whether a bug
in your code can be triggered in your setup.

`aristotle submit` leaves out files whose names end in `.o` or `.so`, even with `--include-ignored`. Other
binaries, such as static libraries (`.a`), executables, and versioned shared libraries like `libz.so.1`, follow
the ignore rules like any other file, so pass `--include-ignored` if an ignore rule matches them. To list what
would go up without sending anything, run the command with `--dry-run` in place of `--wait` and `--destination`.

Aristotle detects C verification from the `.c` files in the upload. The command creates a project
which you can monitor at [Projects](https://aristotle.harmonic.fun/projects).
Without `--wait` the command returns immediately after creating the project; use `aristotle list`,
`aristotle show <project-id>` and `aristotle download <project-id>` to follow up later.

Before it begins proving, Aristotle reads your sources, builds the call graph from the functions you
named, and may ask a small batch of clarifying questions on the project page: which functions are the
verification targets, what each entry point may assume about its arguments where the code does not
check (non-null, buffer sizes, NUL termination, value ranges). Questions are phrased entirely in terms
of your own function and variable names. Aristotle may ask them while it
works, whether or not you pass `--wait`, and waits up to 15 minutes for each answer. With `--wait` at a
terminal, the CLI also shows each question as it arrives and sends your answers. Without it, the command
prints `aristotle questions <project-id>`, which lists the questions and lets you answer them. If a
question gets no answer in time, Aristotle carries on without it: it verifies what doesn't need a
decision only you can make, leaves the rest unverified, and names the open question in its summary.
For a batch run nobody will answer, pass `--no-questions`: Aristotle then asks nothing, and treats
each open decision the same way.

For a run where all the properties are proved you will see a summary like this:

```markdown wrap theme={null}
## Formal Verification Complete

All 4 functions in `ring_buffer.c` are proved memory safe, and every requested
functional property is proved, under the caller obligations listed below. Each
result has a complete machine-checked proof. The results concern the sources as
submitted.

### Memory safety (4 functions)
- **`rb_init`** — memory safe provided `rb` points to a writable `ring_buffer`.
- **`rb_push`** — memory safe provided `rb` points to a writable `ring_buffer` with
  `count <= RB_CAPACITY` and `head < RB_CAPACITY`.
- **`rb_pop`** — memory safe provided `rb` satisfies the same invariant and `out`
  points to writable storage for one `uint32_t` that does not overlap `*rb`.
- **`rb_size`** — memory safe provided `rb` points to a readable `ring_buffer`.

### Functional correctness
- `rb_init` establishes `count == 0` and `head == 0`; `rb_size` then returns 0.
- `rb_push` returns 0 and modifies nothing when `count == RB_CAPACITY`; otherwise
  it writes `value` at index `(head + count) % RB_CAPACITY`, increments `count`,
  and returns 1.
- `rb_pop` returns 0 and modifies nothing when `count == 0`; otherwise it writes
  `data[head]` to `*out`, advances `head` modulo `RB_CAPACITY`, decrements `count`,
  and returns 1.
- FIFO order: for any sequence of pushes and pops starting from `rb_init`, the
  values returned by successful pops are the successfully pushed values in push
  order.
- `count` never exceeds `RB_CAPACITY` in any state reachable from `rb_init`.

### Caller obligations
- The invariant `count <= RB_CAPACITY && head < RB_CAPACITY` is established by
  `rb_init` and preserved by every function; callers must not modify the struct
  fields directly.
- All postconditions are stated conditionally on the function returning.

### External assumptions
None. `ring_buffer.c` calls no external functions.
```

For a verification task where a bug was found—suppose `rb_push` had instead computed the write
index as `size_t tail = rb->head + rb->count;`—you will get a message like this:

```markdown wrap theme={null}
## Memory-Safety Violation Found in `rb_push`

**`rb_push` is not memory safe** under the stated caller obligations. The write index
is computed as `size_t tail = rb->head + rb->count;` with no reduction modulo
`RB_CAPACITY`, so whenever `head + count >= RB_CAPACITY` the store `rb->data[tail] =
value` writes past the end of the `data` array, into `head` and `count` and beyond.

**Failing case:** call `rb_init`, push the values 1 through 8, pop once (so `head ==
1` and `count == 7`), then push the value 9. The store targets `rb->data[8]`, one
element past the end of `data[RB_CAPACITY]`. On the tested layout this overwrites the
low bytes of `rb->head`.

**Reproducer:** `sim/rb_push_counterexample.c` fixes these inputs and calls the
unmodified implementation. Build and run instructions are in the file's comments.
Compiled and run with `-fsanitize=undefined`, the run reports `runtime error: index 8
out of bounds for type 'uint32_t [8]'` on the store in `rb_push`, and the reproducer
prints `head == 9` afterwards, an index that is itself out of range for `data`.
AddressSanitizer alone does not flag this write, because it stays inside the
`ring_buffer` object.

### Other requested properties
- `rb_init`, `rb_pop`, `rb_size` — proved memory safe under the obligations stated in
  their entries in the Properties table.
- `rb_init` establishes `rb_size(rb) == 0` — proved.
- `rb_pop` returns 0 and modifies nothing when the buffer is empty — proved.
- Remaining functional properties of `rb_push` and `rb_pop`, the FIFO property, and
  the bound `rb_size(rb) <= RB_CAPACITY` — not verified; each depends on the
  `rb_push` behaviour shown to be broken above.

The results concern the sources as submitted. No source file was modified.
```

Extract the result archive and use the reproducer path listed in your summary. For the ring buffer
example, run the following commands from the archive root:

```shell wrap theme={null}
cc -fsanitize=undefined -g -I. sim/rb_push_counterexample.c ring_buffer.c -o rb_counterexample
./rb_counterexample
```

A memory-safety violation is reported as a **counterexample**, a concrete failing execution, rather
than as a formal proof that the property is false.

## Specifying what to verify

Properties may be given in the prompt directly or in any text or Markdown file included in the
project directory and referenced from the prompt. Write them in terms of your own functions,
parameters and struct fields. Useful things to state explicitly:

* **Which functions are the targets.** By default Aristotle verifies the functions you name and every
  helper they reach in your sources, and reports each of them separately.
* **Caller preconditions** the code does not check itself: which pointers are non-null, how large the
  buffers behind them are, which strings are NUL-terminated, which integer arguments are in range,
  and which buffers must not overlap. If you leave these open, Aristotle will ask, or will state the
  minimal obligations it needed and report them as caller obligations.
* **What "correct" means** for functional properties. Memory safety needs no specification beyond
  the preconditions; functional correctness does.

Multi-file programs are supported. Put all `.c` and `.h` files under the project directory. Functions
called across files within your sources are verified against a single shared specification so that the
results compose.

## External libraries

Your code will call functions you did not write. Aristotle treats them as follows:

* **The C standard library (glibc).** Contracts for the standard headers (`string.h`, `stdio.h`,
  `stdlib.h`, `ctype.h`, `errno.h`, `unistd.h`, `time.h` and many more) are built in and used
  automatically. Caller obligations they impose, such as a destination buffer being large enough for
  `sprintf` output, are proved at each call site or reported as obligations on your callers. A few
  contracts hold only under an assumption about the client program (for example that it never
  `fclose`s a standard stream); Aristotle reports such an assumption when your code depends on it.

* **Other libraries.** By default, Aristotle verifies the supplied code needed for your properties,
  including supplied library implementations. It may offer to extend verification to additional
  libraries and continues with the default scope while awaiting your answer.

  For libraries outside the verification scope, Aristotle reuses applicable contracts or derives
  contracts from their source code. When source is unavailable, it uses documentation and how your
  code calls the library. Establishing these contracts does not require your approval. The summary
  states the assumed behavior and every result that depends on it; the library implementations remain
  unverified. Obligations imposed on your code must still be proved or stated as caller preconditions.

* **Callbacks** your code passes to a library (for example a `qsort` comparator) remain part of the
  verification scope.

## Public Lean specifications

When a result includes public Lean specifications, the archive stores the contracts and supporting
definitions under `Spec/`, alongside your submitted sources, the summary, and any counterexample
reproducers. The project's Properties table lists each property's status and shows a Code snippet
when a public Lean specification is available.

The archive also includes the generated function interfaces at `<module>/Public.lean` and seven public
support files under `C2lean/`. These include `C2lean/Public/Integer.lean`, which defines the platform's
integer bounds used in specifications. Assumptions about the C library appear as an abstract
`LibCSpecs S` premise, or `LibCIOSpecs S` for tracked I/O. The property's prose explains the library
guarantees and restrictions on which it relies.

To build the public specifications locally, install Lean's `elan` toolchain manager and follow the
instructions in the downloaded `Spec/BUILD.md`. Keep the specifications, generated interfaces, and
support files together at their original paths. The instructions specify the Lean toolchain, pinned
public dependencies, Lake configuration, and commands for a fresh project. This build checks that
the published specifications compile; it does not rerun the proofs about your C implementation.

## Current assumptions and limitations

### Assumptions

Aristotle assumes that:

* preprocessing and the translation into the embedding are faithful;
* the compiler has no bugs that affect the program, and does not optimize it based on undefined behavior that the embedding does not detect;
* glibc 2.35 and the Linux kernel behave as their assumed contracts state;
* floating-point operations round to nearest, ties to even, as in IEEE 754, and the program does not change the rounding mode (for example with `fesetround`) or depend on floating-point exception flags (for example through `fetestexcept`);
* the compiler does not fuse separate floating-point operations, such as a multiplication and an addition, into one (compile with `-ffp-contract=off`);
* the compiler does not assume that a loop without side effects terminates;
* the Lean proof checker is correct, and so is native evaluation where a proof uses it.

Aristotle uses a formal model of C on Linux/x86-64 (more specifically, gcc on Ubuntu 22.04) that follows the platform as documented, plus some known platform bugs.

### Functions Aristotle cannot handle yet

If a function your properties reach cannot be verified yet, the summary lists it by name under
"Functions we cannot handle yet", and the properties that depend on it are not verified. Verification
continues for the other properties. Functions your properties never reach do not affect the run.

### Compiler and platform

Aristotle verifies programs as compiled by GCC 11.4 for Linux on x86-64.

### Termination

Aristotle does not support reasoning about termination: functional postconditions describe what holds **if** a function returns.

### Concurrency and I/O

The proof rules for buffered file and directory I/O are not yet formally proved sound with respect to the embedding. Aristotle does not support weak-memory concurrency or device I/O through `volatile`.

### Liveness and complexity

Aristotle does not support reasoning about deadlock freedom, program complexity or constant-time execution.

### Unsupported constructs

Aristotle does not support complex or decimal arithmetic, binary128 types (`_Float128`, `__float128`), or vector operations. It supports `setjmp` and `longjmp` only where the C standard allows a call of `setjmp`, and thread-local objects only in programs that run a single thread.

### Integer-to-pointer casts

Pointers reconstructed from integer addresses support some equality comparisons, but do not regain permission to access an allocation. Unsupported uses include memory access, nonzero offsets, ordering or subtraction against allocation pointers, `free`/`realloc`, mutex operations and indirect calls, even after a pointer-to-integer-to-pointer round trip.

### Bugs are reported as counterexamples, not as proofs of incorrectness

Aristotle can prove a C property, and it can exhibit a concrete failing execution, but it does not
produce a formal proof that a C property is *false*. A property that Aristotle could neither prove
nor refute with a confirmed counterexample is reported as unverified, with the reason. A failed proof
attempt on its own is never reported as a bug.

### Prose specifications

When Aristotle cannot generate a public Lean specification for a property, it provides a prose
specification detailing the proved statement, counterexample, or unverified outcome.

### Proof source is not returned

If Aristotle claims it has verified a function, this is always based on a machine-checked formal proof
of the property for a mechanically (not agentically) produced formal embedding of the function against
a formal semantics of C on Linux/x86-64. The internal C semantics, proof source, correspondence
certificates, and built-in libc contract formulas are not included in the result archive.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.