No-libc Linux and WebAssembly.

Silk builds for six targets, and the std lib reference documents eight profiles of them. Three profiles are ordinary hosted systems with a C library: aarch64-apple-darwin, aarch64-unknown-linux-gnu and x86_64-unknown-linux-gnu. The other five have no C library at all:

  • Linux without libc: x86_64-unknown-linux-gnu-no-libc and its aarch64 twin, which are the Linux targets with a compilation whose libc is none, not separate target names.

  • Bare WebAssembly: wasm32-unknown-unknown, which gets nothing from its host.

  • WASI: wasm32-wasip1 (a core module that imports wasi_snapshot_preview1) and wasm32-wasip2 (a WASI 0.2 component).

This guide writes one program, squares, that selects different code for each kind of target, checks it for four targets, and builds and runs two WebAssembly modules.

What changes without libc#

On a hosted target, the providers you create in main talk to the C library: StdoutWriter, StdoutLogger, OsFileSystem, OsMonotonicClock, OsRandom, the native sockets and HTTP providers. Without libc most of them are gone or refuse to compile. WASI is the exception: there the same providers call the WASI host instead. On wasm32-unknown-unknown, OsRandom and the two clocks ask the JavaScript host that runs the module. Everything that only describes work stays everywhere: services, effects, collections, text, JSON, crypto, compression, URI parsing.

Kind of moduleHostedLinux, no libcwasm32-unknown-unknownWASI
Portable logic (silk/collections/list, silk/json/value, silk/crypto/sha2, ...)yesyesyesyes
Services (silk/io/writer, silk/io/logger, silk/system/filesystem, silk/system/random, ...)yesyesyesyes
Allocator.systemAllocatorProvider()yesyesyesyes
Output, logging and files (silk/io/writer_native, silk/io/logger_native, silk/system/filesystem_native)yesempty moduleempty moduleyes
OsRandom, OsSystemClock, OsMonotonicClockyesbuild erroryes, through two host importsyes
Child processes (silk/system/child_process_native)yesempty moduleempty moduleempty module
Native sockets and HTTP (silk/net/socket_native, silk/http/client_native, ...)yesnonono; wasm32-wasip2 has its own modules
Raw kernel calls (silk/platform/linux/syscall, silk/platform/linux/mapping, silk/platform/linux/process_inputs)empty moduleyesempty moduleempty module

On WASI, OsFileSystem reaches only the directories the host preopens, and OsRandom uses the host's random_get. On wasm32-unknown-unknown, OsRandom and the clocks import functions from the host, as Random bytes and clocks from the host below shows. For everything else on the other two kinds of target, your code keeps asking for services, and you supply providers you write yourself. That is the same move you make in tests, which the Testing with providers guide covers.

Reading the per-target API pages#

The std lib API reference has one page per module. Each declaration on it ends with a Profiles: line that names the profiles where it is declared. On the silk.io.writer_native page, for example, the Writer operations of StdoutWriter list the three hosted profiles plus wasm32-wasip1 and wasm32-wasip2. A declaration missing from a profile is the SEM0014 error shown below. The overview page ends with the list of profile identities and the target each one uses.

A declaration that lists every profile can still refuse a target when you use it. OsRandom.make() is declared on all eight profiles, but on the no-libc Linux profiles it stops the build with a compileError. The provider's doc comment and its "Gotchas" section say which profiles it supports.

The manifest#

toml
[package]
name = "squares"
version = "0.1.0"
silk-version = "0.1.0"

[unstable.composition]
runtimes = [
  { name = "hosted", module = "silk/platform/native/start" },
  { name = "raw", module = "silk/platform/linux/start" },
  { name = "standalone", module = "silk/platform/wasm/start" },
  { name = "wasi", module = "silk/platform/wasi/start" },
]
defaults = ["hosted"]
components = [
  { capability = "execution-storage", bindings = [
    { operation = "create", module = "silk/concurrency/execution_storage", declaration = "silk_execution_storage_create" },
    { operation = "acquire", module = "silk/concurrency/execution_storage", declaration = "silk_execution_storage_acquire" },
    { operation = "release", module = "silk/concurrency/execution_storage", declaration = "silk_execution_storage_release" },
    { operation = "destroy", module = "silk/concurrency/execution_storage", declaration = "silk_execution_storage_destroy" },
  ] },
]

A target alone does not say how a program starts. The runtime module does that, and the std lib's catalog knows three: silk/platform/native/start for hosted targets, silk/platform/wasm/start for wasm32-unknown-unknown and silk/platform/wasi/start for both WASI targets. For Linux without libc there is no default, so you list the runtimes yourself in [unstable.composition] and select one in each compilation:

  • hosted (silk/platform/native/start) is the default, used by silk run and by any compilation that names no runtime.

  • raw (silk/platform/linux/start) enters at _start straight from the kernel. It calls pub fn main(inputs: &ProcessInputs) -> i32 and turns the result into the exit status.

  • standalone (silk/platform/wasm/start) exports main from the WebAssembly module. The host calls it with no arguments and receives an i32: an i32 result passes through, a unit result is 0, and a typed failure is 1. It gives the program no host services.

  • wasi (silk/platform/wasi/start) exports _start, the WASI command entry. main may return i32 or (), or be an effect fn with either result; it may require &mut HostInput, which reads the WASI arguments and environment. A failure exits with status 1. Other services need providers you create in main, as on a hosted target.

A root command can construct separate compilations for raw Linux, standalone WebAssembly and WASI. Use Compilation.target with the concrete target and Compilation.runtime with "raw", "standalone" or "wasi". The raw Linux compilation also sets Compilation.libc to "none". These operations take deferred strings. A --bare option can select the raw Linux compilation; a Plan command publishes its request for the editor.

The execution-storage component supports the task runner's fibers. The task program uses the hosted default while its graph selects the application runtime. Once a composition is present it replaces the catalog. Include each runtime the graph selects. A missing runtime is refused before code generation. Standard standalone and WASI builds can use the target's catalog when no custom composition is needed.

Selecting the runtime in a command#

Save this as src/tasks.silk. The shared options select the target; --bare selects raw Linux without libc. Each command constructs the same compilation, and plan publishes it without compiling the application.

silk
import silk.build.graph { Build, Compilation, Deferred, Options, BuildError }
import silk.cli.command { Command, CommandLine, Opt }
import silk.cli.task { Task, TaskSet, TaskError }
import silk.mem.allocator { Allocator }
import silk.system.filesystem { FileSystem }
import silk.system.host_input { HostInput }
import silk.text.string { String }

union Tasks {
  Build { pub options: Options, pub bare: bool },
  Run { pub options: Options, pub bare: bool },
  Plan { pub options: Options, pub bare: bool },
}

impl CommandLine for Tasks {
  static fn command() -> Command<Tasks> {
    return Command<Tasks>.make("tasks", "Build squares for a selected runtime")
      |> Command.flatten("options")
      |> Command.option("bare", Opt.about("Use the raw Linux runtime without libc"))
  }
}

fn runtimeName(options: &Options, bare: bool) -> string {
  if bare {
    return "raw"
  }
  if options.target.length() == 0 {
    return "hosted"
  }
  let target = options.target.asSlice()[0].view()
  if target == "wasm32-unknown-unknown" {
    return "standalone"
  }
  if target == "wasm32-wasip1" || target == "wasm32-wasip2" {
    return "wasi"
  }
  return "hosted"
}

effect fn setting(value: string) -> Deferred<String>
  ! BuildError
  ? &mut Allocator {
  return run Deferred<String>.value(run String.copy(value))
}

effect fn graph(options: &Options, bare: bool) -> Build
  ! BuildError
  ? &mut Allocator | &mut FileSystem | &mut HostInput {
  let package = run Compilation.fromPackage(options)
  let selected = run package.runtime(run setting(runtimeName(options, bare)))
  let compilation = if bare {
    run selected.libc(run setting("none"))
  } else {
    move selected
  }
  let build = run Build.make()
  return run build.add(move compilation)
}

impl TaskSet for Tasks {
  effect fn perform(self: Self) -> ()
    ! BuildError | TaskError
    ? &mut Allocator | &mut FileSystem | &mut HostInput {
    match move self {
      Tasks.Build { options, bare } => {
        let build = run graph(&options, bare)
        drop run build.compile()
      }
      Tasks.Run { options, bare } => {
        let build = run graph(&options, bare)
        let built = run build.compile()
        run built.launch(&[])
      }
      Tasks.Plan { options, bare } => {
        let build = run graph(&options, bare)
        drop run build.write()
      }
    }
  }
}

pub effect fn main() -> i32
  ? &mut HostInput {
  return run Task.main<Tasks>()
}

runtimeName maps the requested target to one of the runtimes in the manifest. setting wraps a string as the deferred value that the Compilation operations take. graph starts from the package's own compilation, selects the runtime, and sets libc to none only for the raw Linux build. Each operation consumes the compilation and returns the updated one.

The program#

silk
import silk.core.effect { Effect }
import silk.text.format { Format }
import silk.io.writer { Writer, WriterError }
import silk.core.compilation { Compilation, Libc }
import silk.core.target { OperatingSystem, Target }

fn sumOfSquares(limit: i32) -> i32 {
  let mut total = 0
  let mut n = 1
  while n <= limit {
    total = total + n * n
    n = n + 1
  }
  return total
}

effect fn report(limit: i32) -> ()
  ! WriterError
  ? &mut Writer {
  let total = sumOfSquares(limit)
  run Format.format("sum of squares to {limit}: {total}\n", &.{ limit: limit, total: total })
}

static if Target.operatingSystem() == OperatingSystem.Freestanding {
  pub fn main() -> i32 {
    return sumOfSquares(10)
  }
} else static if Target.operatingSystem() == OperatingSystem.Linux
  && Compilation.libc() == Libc.None {
  import silk.platform.linux.syscall { RawLinux }
  import silk.platform.linux.process_inputs { ProcessInputs }
  import silk.core.result { Result }
  import silk.collections.slice { Slice }

  struct RawStdout {}

  effect fn writeAll(self: &mut RawStdout, bytes: &[u8]) -> ()
    ! WriterError {
    let mut offset: usize = 0
    while offset < bytes.length {
      let result = RawLinux.write(1, Slice.view(bytes, offset, bytes.length - offset))
      if RawLinux.error(result) != 0 {
        fail Writer.failure()
      }
      offset = offset + result
    }
  }

  effect fn flush(self: &mut RawStdout) -> () ! WriterError {}

  impl Writer for RawStdout {
    writeAll: RawStdout.writeAll

    flush: RawStdout.flush
  }

  pub fn main(inputs: &ProcessInputs) -> i32 {
    let mut out = RawStdout {}
    let outcome = run Effect.result(report(10)) |> Effect.provideMut<Writer>(&mut out)
    return match move outcome {
      Result.Success { .. } => 0
      Result.Failure { .. } => 1
    }
  }
} else {
  import silk.io.writer_native { StdoutWriter }

  pub effect fn main() -> ()
    ! WriterError {
    let mut out = StdoutWriter.make()
    run report(10) |> Effect.provideMut<Writer>(&mut out)
  }
}

sumOfSquares and report are the portable part. report needs a Writer and does not care which one.

The rest is three versions of main, chosen by a module-level static if. Target.operatingSystem() from silk/core/target and Compilation.libc() from silk/core/compilation are static functions: the compiler knows their answers before it type-checks, so only the selected arm exists in the build. That is why each arm can import modules the others cannot use. See Static control flow and reflection in the reference.

  • On wasm32-unknown-unknown, whose operating system is OperatingSystem.Freestanding, main returns the number. The host reads it as the call's result. silk/platform/wasm/start accepts a main that returns i32 or (), or an effect fn that returns () and may fail. An effect fn main() -> i32 is refused there with SEM0225: "Entry point main has a result the selected runtime silk/platform/wasm/start cannot start; it accepts i32, (), or an Effect producing () with no requirements".

  • On Linux without libc, RawStdout implements the Writer service with RawLinux.write on descriptor 1. RawLinux.write returns the kernel's raw result; RawLinux.error turns it into zero or an error number. writeVectored, which writes several slices as one, has no faster way here, so it is left out and its default body calls writeAll for each slice. With that provider, the same report runs unchanged.

  • Everywhere else, StdoutWriter from silk/io/writer_native does the job. That includes WASI (OperatingSystem.Wasi), where it writes with fd_write. Both WASI targets also have libc = "none", which is why the second arm tests for Linux too.

silk/core/target also has arch(), abi(), endianness(), pointerBits and hasCpuFeature("..."). silk/core/compilation reports the rest of the build request, such as artifact(), safety() and threading().

Check every target#

On the host, nothing new:

shell
silk run
TEXT
sum of squares to 10: 385

silk plan writes the selected compilation request. silk check reads that request and checks the corresponding target:

shell
silk plan
TEXT
Checking squares (x86_64-unknown-linux-gnu)
  load      131 modules, 1 in package         0.02s
  check     2 modules                         0.10s
  borrow    29 bodies                         0.00s
Finished squares in 0.13s
shell
silk plan --target wasm32-unknown-unknown
silk check --target wasm32-unknown-unknown
TEXT
Checking squares (wasm32-unknown-unknown)
  load      139 modules, 1 in package         0.02s
  check     2 modules                         0.02s
  borrow    22 bodies                         0.00s
Finished squares in 0.06s
shell
silk plan --target wasm32-wasip1
silk check --target wasm32-wasip1
TEXT
Checking squares (wasm32-wasip1)
  load      139 modules, 1 in package         0.01s
  check     2 modules                         0.11s
  borrow    82 bodies                         0.00s
Finished squares in 0.14s

--target wasm32-wasip2 checks the Preview 2 surface the same way. Run these checks in CI. They are the cheapest way to keep a portable library portable.

Mistakes the checks catch#

Copy the hosted main, with its StdoutWriter import, into the Freestanding arm in place of the one there. The host and WASI builds are unaffected, but the wasm32-unknown-unknown check fails:

shell
silk plan --target wasm32-unknown-unknown
silk check --target wasm32-unknown-unknown
TEXT
Checking squares (wasm32-unknown-unknown)
  load      139 modules, 1 in package         0.06s
  check     failed: 2 modules, 1 error        0.04s
[wasm32-unknown-unknown]
src/main.silk:25:34: error[SEM0014] Module silk.io.writer_native has no member StdoutWriter
Failed squares in 0.11s

silk/io/writer_native exists on wasm32-unknown-unknown, but declares nothing there. Now try a provider that is declared everywhere but refuses a target. Put let _random = OsRandom.make() at the top of the raw Linux main, with import silk.system.random_native { OsRandom } among that arm's imports. silk plan and the build both stop:

shell
silk build --bare
TEXT
Compiling squares (x86_64-unknown-linux-gnu, dev)
  load      162 modules, 1 in package         0.00s
  check     1 module                          0.00s
  codegen   failed: 97 functions, 1 error     0.07s
[x86_64-unknown-linux-gnu]
src/main.silk:59:19: error[SEM0177] OsRandom requires a JavaScript host, WASI, Windows, Darwin or a GNU libc profile
Failed squares in 0.08s

SEM0177 means library code asked the compiler to stop. The test runs inside OsRandom.make, and the error is reported at your call to it. Without libc, write a Random provider over whatever entropy the kernel gives you, such as the getrandom system call.

Build and run the bare WebAssembly module#

shell
silk build --target wasm32-unknown-unknown
TEXT
Compiling squares (wasm32-unknown-unknown, dev)
  load      139 modules, 1 in package         0.02s
  check     1 module                          0.00s
  codegen   31 functions                      0.03s
  borrow    24 bodies                         0.00s
  link      clang                             0.15s
Finished build/wasm32-unknown-unknown/dev/squares.wasm in 0.22s

The output is a WebAssembly module, built on any host with Clang and wasm-ld. It imports nothing, since this program reads no random bytes and no clock, and exports its memory, main, and the report buffer described below, silk_report and silk_report_length. The compiler links in a small runtime of its own: an allocator over linear memory and the byte routines LLVM calls. So Allocator.systemAllocatorProvider() works here too. Freed memory is reused, and neighbouring free blocks are merged. The module grows its memory only when no free block is large enough.

Any WebAssembly host can instantiate the module and call main(). In Node, save this as run.mjs:

TEXT
import { readFile } from 'node:fs/promises'

const bytes = await readFile('build/wasm32-unknown-unknown/dev/squares.wasm')
const { instance } = await WebAssembly.instantiate(bytes, {})
console.log(instance.exports.main())
shell
node run.mjs
TEXT
385

The std lib also ships a runner, <silk>/lib/silk/stdlib/host/wasm-runner.mjs. It runs main and exits with its result as the process status, which keeps only the low 8 bits: 385 becomes 129.

shell
node <silk>/lib/silk/stdlib/host/wasm-runner.mjs build/wasm32-unknown-unknown/dev/squares.wasm
echo $?
TEXT
129

Random bytes and clocks from the host#

OsRandom, OsSystemClock and OsMonotonicClock work on wasm32-unknown-unknown too. A module that uses one of them imports a function from the import module silk:

ImportDoes
random(address, length)Fills length bytes at address with secure random bytes, as crypto.getRandomValues does
clock(monotonic)Returns milliseconds as an f64: since 1970 when monotonic is 0, since a fixed start, as performance.now() gives, when it is 1

Only the functions the program reaches are imported. Flip a coin in the Freestanding arm, so main answers 385 or -385:

silk
  import silk.system.random { Random }
  import silk.system.random_native { OsRandom }

  pub fn main() -> i32 {
    let mut random = OsRandom.make()
    let heads = run Random.nextBool() |> Effect.provideMut<Random>(&mut random)
    if heads { return sumOfSquares(10) }
    return 0 - sumOfSquares(10)
  }

The module now imports silk.random, and the run.mjs above, which passes no imports, fails with TypeError: WebAssembly.instantiate(): Import #0 module="silk": module is not an object or function. wasm-runner.mjs supplies both functions, so it runs the module as it is and exits with 129 or 127, the low 8 bits of 385 and -385. A host that instantiates the module itself passes { silk: { random, clock } }, or just the ones WebAssembly.Module.imports lists.

A JavaScript host cannot block, so a wait on the monotonic clock, such as Effect.sleep, reads the clock until the deadline. It holds the thread, and in a browser it holds the page. Browsers also round performance.now(), often to 5 or 100 microseconds.

When the module fails#

A module has no standard error, so when main fails with an unhandled error, or the program traps, the module writes the same report a native program prints into a buffer in its own memory: silk_report() returns its address and silk_report_length() its length. The runner prints it. For a trap it also prints the engine's stack, whose frames carry Silk names such as main.pick, and exits with 134. From a script, runWasm from the same file resolves to main's status and rejects a trap with a SilkError whose report property holds the report. The error type of the Effect that main returns must implement Debug, as on every other target.

A dev build has debug information. The module then ships without its DWARF: it is written beside the module as squares.wasm.debug.wasm, which Chrome's DWARF support reads, with a source map, squares.wasm.map, for Firefox and Safari. The module names both in custom sections. Debugging in the Tools section shows the reports, the runner's API and these files.

An export "C" fn in the program is exported from the module under its name, next to main, so the host can call it with numbers. Only scalar and pointer arguments cross that boundary on wasm32; C statics, variadic functions and callbacks are refused.

A wasm32 compilation may also set threading to multi. The module then imports a shared memory, plus the wasi-threads thread-spawn function when it starts threads, and silk/concurrency/thread_native runs each thread as a Worker under wasm-runner.mjs. The compilation's maxMemory setting caps the memory, for example 256 MiB; threaded modules default to 1 GiB. Joining a thread frees its stack, so a program that starts and joins threads in a loop does not use up that limit.

Build and run a WASI module#

shell
silk build --target wasm32-wasip1
TEXT
Compiling squares (wasm32-wasip1, wasi)
  load      139 modules, 1 in package         0.01s
  check     1 module                          0.00s
  codegen   218 functions                     0.18s
  borrow    148 bodies                        0.00s
  link      clang                             0.25s
Finished build/wasm32-wasip1/dev/squares.wasm in 0.49s

This is a WASI Preview 1 command, about 113 KiB. It exports memory and _start, and imports only what it uses from wasi_snapshot_preview1: here args_get, args_sizes_get, environ_get, environ_sizes_get, fd_write and proc_exit. The StdoutWriter arm of main runs, so the program prints its line itself.

silk run --target wasm32-wasip1 builds it and runs it with Wasmtime, giving the program the current directory as /. A trap or an unhandled error prints the same report as on a native target, followed for a trap by Wasmtime's trace with each frame's Silk name and source line; see Debugging. Without Wasmtime it builds and then stops with status 2:

TEXT
silk: running WASI needs wasmtime on PATH or SILK_WASMTIME

Any WASI Preview 1 host works. Node has one built in. Save this as run-wasi.mjs:

TEXT
import { readFile } from 'node:fs/promises'
import { WASI } from 'node:wasi'

const wasi = new WASI({ version: 'preview1', returnOnExit: true })
const bytes = await readFile('build/wasm32-wasip1/dev/squares.wasm')
const { instance } = await WebAssembly.instantiate(bytes, wasi.getImportObject())
process.exitCode = wasi.start(instance)
shell
node --no-warnings run-wasi.mjs
TEXT
sum of squares to 10: 385

--no-warnings hides Node's notice that WASI is experimental. To let the program read files, pass preopens, for example { '/': '.' }, to new WASI.

Preview 2 components#

wasm32-wasip2 builds the same core module and then turns it into a WASI 0.2 component. That step runs wasm-tools (on PATH, or named by SILK_WASM_TOOLS) with the Preview 1 command adapter that SILK_WASI_ADAPTER points to. Without wasm-tools, a wasm32-wasip2 build checks and compiles, then fails at the link step with status 2:

TEXT
silk: wasm32-wasip2 needs wasm-tools on PATH or SILK_WASM_TOOLS

The component targets the std lib's bundled WIT, <silk>/lib/silk/stdlib/wit/world.wit, at WASI 0.2.12. Its command world is the CLI imports plus outgoing HTTP, and its http world is wasi:http/proxy. A package can name its own WIT directory and world in its manifest:

toml
[component]
wit = "wit"
world = "my-world"

Only Preview 2 has networking. These modules exist on wasm32-wasip2 and declare nothing on Preview 1:

  • silk/net/socket_wasi (Network), silk/net/resolver_wasi (WasiResolver) and silk/net/datagram_wasi: TCP, name lookup and UDP through wasi:sockets.

  • silk/http/client_wasi: WasiHttp, an HTTP client provider over wasi:http/outgoing-handler.

  • silk/http/server_wasi: Exchange, which takes the request and response handles of a wasi:http incoming handler.

This guide checked them against wasm32-wasip2 but could not run a component, since that needs Wasmtime.

Checking raw Linux code#

Publish the raw Linux request before checking that branch of the source:

shell
silk plan --bare
silk check

The raw runtime can emit its _start entry, but the current Linux link path still includes the host startup objects. silk build --bare therefore fails with a duplicate _start symbol. Use the request to check portable raw Linux code; hosted Linux and the WebAssembly builds above can produce executable artifacts.

API reference#

  • silk/core/target: Target, Arch, OperatingSystem, Abi, Endianness, pointerBits

  • silk/core/compilation: Compilation, Libc, Artifact

  • silk/platform/linux/syscall: RawLinux (no-libc Linux only)

  • silk/platform/linux/process_inputs: ProcessInputs (no-libc Linux only)

  • silk/platform/linux/mapping: Mapping (no-libc Linux only)

  • silk/platform/linux/start, silk/platform/wasm/start, silk/platform/wasi/start, silk/platform/native/start: runtime modules, no public items

  • silk/platform/wasi/preview1, silk/platform/wasi/preview2: raw WASI imports, for writing providers

  • silk/net/socket_wasi, silk/net/resolver_wasi, silk/net/datagram_wasi, silk/http/client_wasi, silk/http/server_wasi: networking and HTTP (wasm32-wasip2 only)

  • silk/concurrency/thread_native: NativeThread (hosted targets, and wasm32 with threading = "multi")

  • <silk>/lib/silk/stdlib/host/wasm-runner.mjs: runs a wasm32 module in Node or a browser, prints its report, and exports runWasm, SilkError and LOG_LEVELS

  • silk/io/logger_console: ConsoleLogger (wasm32-unknown-unknown); silk/io/logger_wasi: WasiLogger (wasm32-wasip2)

  • silk/io/writer: Writer, WriterError; silk/io/writer_native: StdoutWriter (hosted and WASI)

  • silk/text/format: Format; silk/collections/slice: Slice

View the source of this page