DEVELOPMENT EDITION · 769442725406
Toolchain Manual
On this page

C and system integration

C imports make useful libraries accessible while introducing a trust boundary. Verada proves callers against the declared contracts; it does not prove C bodies. Use small typed wrappers, explicit effects and ownership rules, and native/JIT tests of the actual implementation.

A complete scalar example

The supplied project contains a C function, a Vada import specification and a main program that prints the actual result:

veradic prove --project docs/manuals/examples/toolchain/c-integration/verada.toml
veradic run --project docs/manuals/examples/toolchain/c-integration/verada.toml
veradic build --project docs/manuals/examples/toolchain/c-integration/verada.toml -o c-demo
./c-demo

Both execution paths should print C result: 14. The import uses Interfaces.C.int, Import, Convention => C, External_Name => "twice", Global => null, and bounded Pre/Post. The matching C function has signature int twice(int value). The project's c-sources = ["support.c"] is relative to the project file.

Review the claimed postcondition against the C source. Verada may warn that an imported contract is assumed. A strict proof of the caller cannot establish that C obeys it. If the C function returned a different value, proof assumptions would still be trusted; the actual printed result test would expose the discrepancy.

ABI and representations

Use Interfaces.C types rather than assuming a Verada Integer is every C integer type. They follow LP64. Scalar in values are passed by value; by-reference formals receive addresses. C-convention array parameters receive a pointer to components, with no implicit Ada bounds/length descriptor: include an explicit length parameter and contracts connecting it to array bounds. Verada's own runtime-import convention can use pointer-plus-size pairs and is not interchangeable with Convention C.

A Verada String is not automatically a nul-terminated C string. Use Interfaces.C conversions and char_array, choose whether to append/trim nul, and respect their preconditions. Keep array storage alive throughout the foreign call. C must not retain a borrowed pointer after its owner/lifetime ends, free Verada-owned storage, or mutate data declared read-only by the contract. Do not expose compiler-specific record/class-wide descriptors as a stable C struct ABI. Use explicit scalar/pointer wrappers and verify layout where representation controls are involved.

Imports/exports can name subprograms and objects through Convention, External_Name and related supported aspects. Exports called from C require C callers to establish preconditions and respect effects and ownership. Callbacks require a compatible subprogram profile, contract and lifetime; a proof cannot police arbitrary callbacks issued by C. Use the language's callback restrictions rather than casting an untyped address into an executable pointer.

Compilation and linking

--cc PATH overrides VERADA_CC, otherwise cc is used. The value is an executable path/name, not shell flags. Project C sources are compiled with -std=c11 -O2 -fPIC, the runtime's with -std=c11 -O2, and the same compiler driver links. There are no project fields for include paths, libraries or linker flags; an executable wrapper script named by --cc can add them, but changing the wrapper does not invalidate cached C objects.

check/prove load the declarations and prove Verada callers without compiling c-sources. Build compiles and links them; run compiles and adds their objects to the JIT. Imported system symbols must be resolvable on the host that links or runs the program.

Faults, cache changes and delivery

C undefined behavior can corrupt memory, filesystem or process state. JIT handling can turn some synchronous foreign faults into B7013, but does not undo writes or validate C semantics. Native signals need native debugging. Correct Global/Depends declarations matter: lying about external state makes modular reasoning unsound.

C objects are keyed by source path/content, not all transitive headers/compiler options. Use a fresh XDG cache when changing headers, compiler wrappers or toolchain, and retest both execution paths. Inspect native dynamic dependencies before distribution; linking in a foreign library can add deployment requirements even though Verada's own runtime sources are embedded.