Calling a native library

A native:NAME block binds one function out of a shared library to a name the program can call, behind the ffi grant. It is the one way a midad program reaches code that is not midad.

The block

```native:hypot
lib: libm.so.6
symbol: hypot
sig: (float, float) -> float

`lib` is the library opened, `symbol` the function looked up in it, and `sig`
the signature the call site is checked against. `hypot` is then an ordinary
function in the program:

````midad run hypot.md
---
midad: "0.1"
module: hyp
permissions:
  ffi: ["libm.so.6"]
---

```native:hypot
lib: libm.so.6
symbol: hypot
sig: (float, float) -> float
print(str(hypot(3.0, 4.0)));
```output
5
```

The types in `sig` are the language's: `number`, `float`, `string`, `boolean`,
`list`, `map`, `any`, `void`. A `float` is a C double and a `number` is a C
int64, so the signature has to say which one the symbol really takes.

## The grant

`ffi` names the exact library, not a directory and not a wildcard:

````midad check:E0301
---
midad: "0.1"
module: nogrant
---

```native:hypot
lib: libm.so.6
symbol: hypot
sig: (float, float) -> float
```

```exec
print(str(hypot(3.0, 4.0)));
```

The message names the grant to add. "*" is not a shortcut here: a program that can name any library can call any code on the machine.

The signature is checked at the call site

sig is not documentation. A call that does not match it is a checker error, not undefined behaviour at run time:

check-1.md
---
midad: "0.1"
module: badargs
permissions:
  ffi: ["libm.so.6"]
---

```native:hypot
lib: libm.so.6
symbol: hypot
sig: (float, float) -> float
```

```exec
print(str(hypot("3", 4.0)));
```
midad check
× E0230: type mismatch: expected float, found string

The argument count is checked the same way.

A library that is not there

The library is opened when the call runs, so a missing one is a runtime error that names the file, not a failed build:

error-2.md
---
midad: "0.1"
module: missing
permissions:
  ffi: ["libnope.so.9"]
---

```native:nope
lib: libnope.so.9
symbol: nope
sig: (float) -> float
```

```exec
print(str(nope(0.0)));
```
midad run (exit 1)
error-2.md:15:11
  × R0001: cannot load `libnope.so.9`: libnope.so.9: cannot open shared object
  │ file: No such file or directory
    ╭─[error-2.md:15:11]
 14 │ ```exec
 15 │ print(str(nope(0.0)));
    ·           ────┬────
    ·               ╰── cannot load `libnope.so.9`: libnope.so.9: cannot open shared object file: No such file or directory
 16 │ ```
    ╰────

A native build refuses a bridge

In 0.2.1 a program that calls an ffi bridge runs in the interpreter only. Both native backends refuse it by name rather than compile a call they cannot make:

c backend failed: unsupported in native subset: the C backend does not cover
this yet: builtin `hypot` is not supported there

native build failed: unsupported in native subset: call to non-native function
hypot in native subset

That is the same rule the rest of the native subset follows: it refuses what it cannot do rather than produce a binary that behaves differently.

What it is not

ffi is not a sandbox. Once a library is granted, its code runs with the process's full rights: it can open files, reach the network and read memory that the grants do not cover. The grant decides which libraries a program may name, not what the library may do once it is called. That is stated with the rest of the model on Permissions and the frontmatter.

On this page