tobogganSe connecter

louise

Clone avec HTTPS, un jeton personnel en mot de passe, ou avec SSH, une clé ajoutée dans vos paramètres.

121 lignes · 9870 octetsBrut · Historique

ADR 0005: FFI and native dependencies

Status: Accepted — 2026-10-08, reviewed and accepted by the project owner (issue #2).

Context

Programs must be able to use existing C and Rust libraries, e.g. argon2, HTTP parsers or large prebuilt libraries, without rewriting them.

The compiler emits C (ADR 0003), so the C ABI is the natural boundary.

Decision

Declaring an extern

An @extern attribute goes on a function declared without a body:

@extern c "argon2id_hash_encoded" blocking
fn argon2_hash ::
  pw: String
  salt: Bytes
  t: Int
  m: Int
  p: Int
  -> Result<String, Int> / <ffi>

Effects: the declared row says what the call performs, and <ffi> is the conventional effect for foreign calls. An omitted row means total, as for every function (ADR 0008). Leaving it out is the binding author's explicit trust decision, used for pure built-ins such as integer arithmetic.

Types allowed at the boundary:

Louise typeC side
Int, Float, Boolunboxed
String, Bytesborrowed ptr + len for the duration of the call
owned resultsbuilt by the C side with tl_string_from / tl_bytes_alloc
Resource<t>an opaque handle with a finalizer, marked thread_safe or not (ADR 0004)

Results: a Result-shaped return is built by a thin generated wrapper.

Blocking calls: blocking sends the call to the blocking thread pool, so the scheduler is never stalled. This is the right choice for argon2 and similar CPU-heavy or IO-heavy calls.

Timing:

  • Extern calls arrive in M5, because std itself calls louise_rt this way.
  • Native dependencies (below) arrive in M7.

Native dependencies

They are declared in louise.toml:

[native.argon2]
kind = "c"                         # c | static | pkg-config | cargo
sources = ["vendor/argon2/src/*.c"]
include = ["vendor/argon2/include"]
kindHow it is built
cCompiled by the driver using the same C compiler as the generated code.
staticLinks a prebuilt .a (the "big libs as binaries" case).
pkg-configUses system libraries.
cargoBuilds a staticlib crate exporting extern "C" functions.

Rust libraries use the cargo kind. The louise-ffi helper crate provides the C-ABI string, bytes and resource types and the allocation entry points, so Rust bindings never hand-write layouts.

Consequences

  • Binding a C library takes one @extern per function plus a manifest entry; no glue code is needed.
  • Callbacks from C back into Louise are out of scope for v0. They need re-entry into the scheduler (v1: ADR 0015).
  • Header-driven binding generation (bindgen-style) is deferred (v1: ADR 0015).

Alternatives considered

  • A Rust-ABI or cxx-style bridge. It would tie the language to Rust's unstable ABI. The C ABI works for both C and Rust.
  • Dynamic loading (dlopen) only. Simpler, but it gives up static single-binary deployment.

Revision 1 — 2026-10-09: what M7 implemented (proposed)

Status: Accepted by the project owner on 2026-10-12; needs engineering review (#44). These are details decided while implementing M7.

  • Extern convention.
    • The rule: a function bound with @extern(c, "sym") has the convention of louise_rt.h: tl_value sym(tl_ctx *ctx, tl_value a, ...), with borrowed arguments and an owned result.

    • Why not generated marshalling: the table above (String as ptr + len, Result built by a generated wrapper) is replaced by a few lines of C per bound library (the binding's shim). louise_rt.h gives the shim what it needs:

      • tl_string_ptr and tl_string_len;
      • tl_string_cstr and tl_int;
      • tl_ok, tl_error, tl_some and TL_NONE;
      • tl_resource_new and tl_resource_get.

      One convention covers every type, including Result-shaped results, without per-type wrappers in the compiler. A generator for simple signatures can come later.

  • blocking:
    • Where the call runs: on a pool of four threads while the actor is parked (two pools since Revision 2).
    • Kills: a kill of that actor waits until the call returns, because the C code still reads the actor's values.
    • Restrictions: a blocking extern must not panic, perform operations or drop values.
  • Resource<t> lives in std/ffi. Its block holds the pointer and the finalizer; the finalizer runs when the last reference is dropped. A resource that is not thread_safe is pinned.
  • Packages:
    • [dependencies] name = { path = "..." } loads a package's modules as name (its src/lib.lse) and name/.... A package's own lib.lse has the same name.
    • The manifest's [native.*] entries of the whole dependency tree are built and linked.
  • kind = "cargo":
    • the driver runs cargo build --release;
    • the Rust standard library inside the resulting staticlib duplicates the copy in liblouise_rt.a, so the link uses --allow-multiple-definition;
    • runtime/louise-ffi provides the C API to Rust bindings.
  • louise test --native builds one executable for all tests and runs each test in its own process. Packages with native dependencies need it, because the interpreter cannot call C.
  • First binding: packages/argon2 vendors the reference implementation (release 20190702) and is tested against a known answer computed independently with argon2-cffi.

Revision 2 — 2026-10-23: blocking pools (issue #305)

Status: Proposed; needs engineering review (#44).

Revision 1's pool had four threads, fixed, one queue, invisible to programs. toki2's stress test (22 cores, argon2id at 19 MiB) logged in at ~44 per second with a 1.13 s median, against 65 ms for one login: four hashes at a time, whatever the cores, behind a queue that grew without bound. The cap also did good: hashing kept to four threads, so browsing beside it ran at 14,000 requests per second, and memory stayed at 4 × 19 MiB. So the pool should not simply have a thread per core; it should be sized on purpose, per kind of call, and visible.

  • Two pools, as Erlang's dirty CPU and dirty I/O schedulers:
    • @extern(c, "sym", blocking(cpu)), for calls that keep a core busy (hashing, compression). Its threads compete with the scheduler's for the same cores, and each running call holds the memory its C function uses (argon2's m_cost): small by default, half the cores, at least 1, at most 4 (Revision 1's size), so the scheduler keeps most of the machine. LOUISE_BLOCKING_CPU_THREADS sets it.
    • @extern(c, "sym", blocking(io)), for calls that mostly wait (file reads, DNS, a C library that sleeps): 16 threads by default, since a waiting thread costs no CPU. LOUISE_BLOCKING_IO_THREADS sets it.
    • Plain blocking keeps its meaning and is cpu; LOUISE_BLOCKING_THREADS, the shorthand, sets the cpu pool (the specific variable wins).
    • The runtime's own: std/file, net.resolve and tls.config are io; PBKDF2 is cpu. packages/argon2 says blocking(cpu).
  • Configuration is checked at start: 0, a negative number or anything but an integer stops the program before main, with louise: LOUISE_BLOCKING_IO_THREADS must be a positive integer (a number of threads), got "x" and status 2, rather than silently falling back to the default (LOUISE_THREADS does fall back; a pool size is a capacity decision worth refusing loudly). Each pool starts its threads on its first call, as before. There is no programmatic setting: the environment serves embedders and tests alike.
  • Statistics: std/runtime.blocking_stats() gives each pool's size, its running calls and its queued ones. A program can then refuse work early (toki2 answers 429 when too many password checks wait) instead of queueing users for seconds. There is no bounded variant (try_blocking, a queue limit per extern): the statistics give programs the same decision with their own policy, and a bounded queue would need an error path in every blocking extern's type.
  • Syntax: an attribute argument may be an identifier applied to one identifier (blocking(io); the grammar, the tree-sitter grammar and the formatter accept it). An unknown pool is E0114.
  • ABI: tl_call_blocking(ctx, pool, thunk, argc, args), with TL_BLOCKING_CPU and TL_BLOCKING_IO; TL_ABI_VERSION 9. A pool belongs to a symbol: one symbol declared on two pools runs on one of them.
  • The interpreter accepts the new syntax and runs blocking calls inline, as before; its statistics show the configured sizes with nothing running or queued.

crates/louise/tests/blocking.rs checks, natively: LOUISE_BLOCKING_THREADS=16 runs sixteen 100 ms calls in about 100 ms (400 ms by default); a full pool does not delay a call on the other; the statistics show the queue; an invalid size is refused.

Note — 2026-10-12: Rust natives and the fast runtime (issue #133)

Status: Proposed (epic #135); needs engineering review (#44).

Release builds link liblouise_rt-fast.a (ADR 0031), whose Rust standard library is built for panic = "abort". A kind = "cargo" dependency still brings its own, built for unwinding, and the link keeps the first definition of each duplicate (--allow-multiple-definition; the natives come before the runtime). The native fixture gives the same output with both runtimes, and native_dependencies_of_every_kind checks it. A panic in a Rust native was already undefined at the C boundary (extern "C" functions abort on unwinding since Rust 1.81), so panic = "abort" doesn't change what a binding may do. Building a cargo native and the runtime as one Rust program, with one std, waits for a package that needs it.