Skip to content

Latest commit

 

History

History
105 lines (68 loc) · 4.7 KB

bindings-generation.md

File metadata and controls

105 lines (68 loc) · 4.7 KB

Bindings Generation (ffier)

libkrun uses ffier to generate its C ABI surface and Rust clients directly from Rust source annotations.


Architecture

libkrun exposes two distinct FFI libraries:

  1. libkrun (krun): VMM builder, device management, payload loading, execution control.
  2. libkrun_init (krun_init): Guest init configuration builder, OCI conversion, and overlay injection.

Building with --features ffi generates machine-readable JSON schemas in target/ffier-<lib>.json. The schemas are checked in and used by ffier-gen-c-header to generate C headers (include/) and by ffier-gen-rust-client (invoked by dependent crate build.rs scripts) to generate dynamic Rust clients in Cargo's OUT_DIR.

1. The libkrun Bridge

  • Declaration: src/libkrun/src/api/mod.rs registers exported types via ffier::library_definition!("krun", ...) and src/libkrun/src/lib.rs invokes ffier::generate_bridge!.
  • Schema: bindings/libkrun-via-libc/ffier-krun.json.
  • C Header: include/libkrun.h.
  • Rust Client: bindings/libkrun-via-libc/build.rs reads the schema and generates the Rust client into Cargo's OUT_DIR.

2. The init-blob Bridge

  • Declaration: init/init-blob/src/lib.rs registers types via ffier::library_definition!("krun_init", ...) and invokes ffier::generate_bridge!.
  • Schema: bindings/init-blob-via-cdylib/ffier-krun_init.json.
  • C Header: include/libkrun_init.h.
  • Rust Client: bindings/init-blob-via-cdylib/build.rs generates the Rust client into OUT_DIR.

Rust Integration & Linking Modes

Rust applications can consume libkrun and krun-init-blob in two ways:

  1. Direct Static Linking (In-Process Rust Crates):

    • Depend directly on the krun crate.
    • Depend on krun-init-blob with its default krun feature for compile-time type integration with krun::FsOverlay and krun::Payload.
    • Zero FFI overhead and full compiler type safety.
  2. Shared-Library Clients:

    • libkrun-via-libc uses ffier-dynlookup for runtime symbol lookup or ffier-strong for linking against libkrun.so. Consumers must select a mode; neither is enabled by default.
    • krun-init-blob-via-cdylib uses the same features for libkrun_init.so. Select the libkrun-via-libc mode separately for its foreign handle types.

The FFI Build Toggle

  • make defaults to FFI=1, building libkrun with --features ffi and compiling the C ABI export bridge into libkrun.so / libkrun.dylib.
  • make FFI=0 omits the libkrun C ABI bridge. make test runs the dynamically linked tests by default; make test FFI=0 runs the statically linked tests.

The ffi Feature Flag

Binding generation and C ABI export are gated behind the ffi feature:

  • libkrun: Building with --features ffi (the default for make) compiles the C ABI bridge symbols into libkrun.so / libkrun.dylib and generates target/ffier-krun.json.
  • krun-init-blob: Building with --features ffi compiles libkrun_init.so and generates target/ffier-krun_init.json.

Prerequisites

Install the ffier-gen-c-header binary:

cargo install --git https://github.com/mtjhrc/ffier.git --tag v0.3.0 --locked ffier-gen-c-header

C Header Feature Guards

make gen-libkrun-bindings generates one libkrun.h for all supported hosts. Platform predicates become C preprocessor guards automatically, so each consumer sees declarations for its target platform.

TEE and Nitro change which API exists, so generation uses --guard-features tee,aws-nitro. When compiling a consumer, define KRUN_CFG_FEATURE_TEE and KRUN_CFG_FEATURE_AWS_NITRO only for features enabled in its library build; leave them undefined otherwise. These macros select header declarations, not the library's build features.

Other optional features, such as net, blk, gpu, input and vhost-user, are assumed enabled during generation. The header can therefore declare functions absent from a narrower build. Override GEN_HEADER_FFIER_FLAGS with --disable-features to omit those declarations.


Binding Regeneration & Verification

# Regenerate all bindings
make gen-libkrun-bindings
make gen-init-blob-bindings

# Verify working tree has no uncommitted binding or schema diffs
git diff --exit-code bindings/ include/

Source of Truth

The Rust annotations and ffier::library_definition! calls in src/libkrun/src/api/ and init/init-blob/src/ are the sole source of truth.

Never hand-edit anything generated by make gen-libkrun-bindings / make gen-init-blob-bindings: the schemas in bindings/, include/libkrun.h, include/libkrun_init.h, or any code generated in OUT_DIR by build.rs.