Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Foreign Functions

A program reaches code written in another language through an import: import "pingpong.c" as io; makes every function the file exports callable as io.f(..) from an event body or a guard. This chapter says what an import exposes, how a foreign signature becomes a Fiducia one, and where each failure is reported. Contracts covers the annotations a function may carry.

What a file exports

fic reads the imported file itself; it does not run the foreign compiler to find out what the file offers.

FileLanguageRead byExported functions
.c, .hClibclang, the real C front endevery function that is neither static nor inline
.pnkPancakea text scanner, no external toolevery export fun, with at most four word arguments and one word return

The language is the extension’s: any other extension is rejected when the project loads (import "x.rs": not a C or Pancake file), and so is a file that cannot be read. A .c import is compiled into the protection domain’s unit; a .h import is a driver whose .c is found beside it, or in src/ for a header under include/ (include/eth.h is backed by src/eth.c), and is linked; a .pnk import is compiled by CakeML into the image. Every other .c in the project, outside build/, is compiled into one archive, build/libproject.a, linked after each PD’s objects: the linker takes only the files that define something still missing, so a driver reaches the C it calls (eth.c calls util.c) without the .fi importing it, and a file nothing calls is left out. If two files define the same function, the linker takes the first it needs, without an error.

A protection domain has no C library. Its C may call memcpy, memmove, memset, memcmp and bcmp, which fic’s runtime supplies; anything else it calls, it brings. The five are C byte loops, linked into every PD, and include/gen/mem.h declares them with ACSL contracts that Frama-C has proved of those bodies, so a driver’s proof may call them. The header also declares fi_memcpy_u8(uint8_t *dst, const uint8_t *src, size_t n): the five take void *, which Frama-C’s WP follows only for char data, so a proof about uint8_t buffers copies with this one.

From a foreign signature to a Fiducia one

Each called function’s parameter and return types are lowered to Fiducia types, on AArch64 LP64 widths:

C spellingFiducia type
uint8_t, unsigned char, charuint8 (plain char is unsigned on AArch64)
uint16_t, unsigned shortuint16
uint32_t, unsigned, unsigned intuint32
uint64_t, unsigned long, unsigned long long, size_tuint64
int8_t, signed charint8
int16_t, shortint16
int32_t, intint32
int64_t, long, long long, ssize_t, ptrdiff_tint64
bool, _Boolbool
uintptr_t, intptr_t, any pointerAddress
struct S, where the .fis declares a struct Sthat struct

Every Pancake argument and return is one machine word, uint64. A void return is a function with no value. Anything else (float, an opaque typedef, a struct the .fis does not declare) has no Fiducia type, and calling such a function is an error:

error: event 'e': 'io.scale' has a type fic cannot express: parameter 1
('x') has type 'float', which has no fic type

A function that is never called is never lowered, so an unsupported signature elsewhere in the file costs nothing.

Calls

A call alias.f(args) is typed against the lowered signature: the argument count and types must match, and a result is used at its own type (an int32_t result is not an int; convert it with as). Failures:

  • Unknown function. io.nothing(1) and a static function alike are foreign function 'io.hidden' is not declared, reported where the call is.
  • Wrong arity. 'twice' takes 1 argument(s), found 2.
  • Type mismatch. expected 'int64', found 'int32'.

A struct passed or returned is the .fis struct of that name, whose layout both sides share.

When each failure is reported

An unreadable file or an unsupported extension is reported when the project loads, at the import. An unknown function, a wrong argument count or type, and a signature with no Fiducia type are reported at the call. An annotation outside the contract subset, or a call the contract cannot support, is reported by --check and --emit=xta, which read the contracts. The foreign compiler’s own errors surface only when an image is built.