Skip to main content

Module foreign

Module foreign 

Source
Expand description

Foreign declarations: the functions a program can call through an import "file" as alias;, and the readers that produce them from the imported source. No toolchain is embedded beyond what a reader itself needs (libclang for C; the Pancake reader and this module’s own types need nothing).

fic reads the imported source itself and records each exported function as an AST node (rustc’s ast::ForeignMod/ForeignItemKind::Fn, but derived rather than hand-written). One ForeignModule per imported file is loaded into ForeignModules, beside the AST, numbered by its ModuleId, with each program’s imported files (links). rast resolves an alias.f to the export and keeps each called function once, by FfnId: its name and signature, lowered to fic’s types (ty::foreign, one lowering per language), in Env.ffis, and where it is, in the store’s ffis. The backends read the files, the called functions and the links from the store.

Signatures are kept in a surface form (ForeignType): the type as the foreign language spells it, reduced to what any C-ABI boundary can express. Lowering to fic types (uint8_t → a fic scalar, a pointer → Address, struct Slot → a fic struct) is ty::foreign’s, which rast runs on each called function, not this module’s.

Which files can be imported, what counts as exported, and how each is read:

filelanguagereaderexported functions
.h/.cCc: libclang, the real C front endnon-static, non-inline prototypes/definitions
.pnkPancakepnk: a text scanner, no external tool run; its /@ … @/ contracts parsed by the pancake2viper crate (linked in) and lifted in pnk::annotationexport fun (≤ 4 word arguments, one word return), with its /@ … @/ annotations

Every language has a reader, so every module is complete: a call resolves to one of its functions or is an error (rast).

Calling convention, layout, symbol location and any runtime set-up (Pancake’s heap initialisation) are the backend’s concern, not this module’s. This module keeps what the reader found — a ForeignContract beside each signature; pnk::annotation and c::acsl give it meaning.

Modules§

c
C: the reader (libclang), and ACSL contracts read through Frama-C.
calls
Call sites of imported functions in a .fi program, with how each call’s value is used — the information both the C backend (prototypes) and the contract checks need.
contract
The contract IR: an imported function’s annotations, whichever language they were written in (Viper for .pnk, ACSL for .c). A Term over parameters, constants and retval, a FnContract per function, and a ContractTable keyed by the foreign function. The readers produce it; xta_backend::contract renders it.
pnk
Pancake: the reader (pnk.rs, a scanner running no external tool) and the lifting of its /@ … @/ annotations through the pancake2viper crate (annotation.rs). pancake2viper proves the contracts against the code; the model assumes them (see demo/pingpongpan/README.md).

Structs§

Annotation
One verifier annotation of a foreign declaration, as written.
ForeignContract
The verifier side of a ForeignFn: its contract as written, and why it is impure. Lifted into the neutral IR at extraction.
ForeignFn
A foreign function as the imported file declares it.
ForeignFnRef
Where a foreign function is: its file, and its position among that file’s exports.
ForeignModule
One imported file and the functions it exports. Shared by every program that imports the same file: it is read and parsed once, into ForeignModules, which keeps it.
ForeignModules
The foreign files a project has loaded, one ForeignModule per distinct file — the loaded-modules store, not part of the AST.
ForeignParam
One parameter of a ForeignFn. The name is informational — C prototypes may omit it (int f(int)).
Link
A file a program imports, as its first import of it writes it.

Enums§

ForeignKind
What an imported file is, decided from its extension in one place (the front end, as it binds a program’s imports): its language, and for C whether it is a header (a separately compiled driver, its .c linked beside the unit) or a source (inlined into the unit).
ForeignLang
The language an imported file is written in — decided by its extension.
ForeignType
A type at a C-ABI boundary, as spelled in the foreign language and reduced to what fic can reason about. Qualifiers (const, volatile) are dropped; they do not change the call.

Constants§

INCLUDE_DIR
The include directory, relative to the project.
MARKER
The first line of every file fic writes.

Traits§

Header
One foreign language’s data-layer header.
Lift
One foreign language’s contract lifting.
Reader
One foreign language’s reader.

Functions§

export
Write each header rawast needs, from its data layer tenv (typed by tast::build_data). Returns <project>/include.
extract
The contract of every foreign function a program calls, each file lifted once, and the call-site checks.
load
Load every import of rawast’s programs, one libclang for all its C files, then record each program’s files (ForeignModules::bind). A C file is read with the include path the image build compiles it with: the project directory dir, then include, then extra.
parse
Read one imported file with its language’s Reader.