github.com/wasilibs/wazerox@v0.0.0-20240124024944-4923be63ab5f/site/content/docs/_index.md (about)

     1  +++
     2  title = "Docs"
     3  layout = "single"
     4  +++
     5  
     6  ## Overview
     7  
     8  WebAssembly is a way to safely run code compiled in other languages.
     9  Runtimes execute WebAssembly Modules (Wasm), which are most often binaries with a `.wasm` extension.
    10  Most WebAssembly modules import functions from the host, to perform tasks that are otherwise disallowed by their sandbox.
    11  The most commonly imported functions are called WASI, which allow access to system resources such as the console or files.
    12  
    13  wazero is a WebAssembly runtime, written completely in Go. It has no platform dependencies, so can be used in any environment supported by Go.
    14  
    15  ## API
    16  
    17  Being a Go library, which we document wazero's API via [godoc][godoc].
    18  
    19  ## Terminology
    20  
    21  Wazero has consistent terminology used inside the codebase which may be new to you, or different than another WebAssembly runtime.
    22  This section covers the most commonly used vocabulary. Terms rarely used may also be defined inline in individual sections.
    23  
    24  * Host - Host is a WebAssembly concept that refers to the process that embeds a WebAssembly runtime. In wazero, the host is a program written in Go.
    25  * Binary - Binary is a WebAssembly module, compiled from source such as C, Rust or Tinygo. This is also called Wasm or a guest, and usually is a file with a `.wasm` extension. This is the code wazero runs.
    26  * Sandbox - Sandbox is a term that describes isolation. For example, a WebAssembly module, defined below, is isolated from the host memory and memory of other modules. This means it cannot corrupt the calling process or cause it to crash.
    27  * [Module][Module] - Module an instance of a Binary, which usually exports functions that can be invoked by the embedder. It can also import functions from the host to perform tasks not defined in the WebAssembly Core Specification, such as I/O.
    28  * Host Module - Host Module is a wazero concept that represents a collection of exported functions that give functionality not provided in the WebAssembly Core Specification, such as I/O. These exported functions are defined as normal Go functions (including closures). For example, WASI is often used to describe a host module named "wasi_snapshot_preview1".
    29  * Exported Function - An Exported Function is a function addressable by name. Guests can import functions from a host module, and export them so that Go applications can call them.
    30  * [Runtime][Runtime] - Runtime is the top-level component in wazero that compiles binaries, configures host functions, and runs guests in sandboxes. How it behaves is determined by its engine: interpreter or compiler.
    31  * Compile - In wazero, compile means prepares a binary, or a host module to be instantiated. This is implemented differently based on whether a runtime is a compiler or an interpreter.
    32  * [Compiled Module][CompiledModule] - a prepared and ready to be instantiated object created vi Compilation phrase. This can be used in instantiation multiple times to create multiple and isolated sandbox from a single Wasm binary.
    33  * Instantiate - In wazero, instantiate means allocating a [Compiled Module][CompiledModule] and associating it with a unique name, resulting in a [Module][Module]. This includes running any start functions. The result of instantiation is a module whose exported functions can be called.
    34  
    35  ## Architecture
    36  
    37  This section covers the library architecture wazero uses to implements the WebAssembly Core specification and WASI.
    38  Features unique to Go or wazero are discussed where architecture affecting.
    39  
    40  ### Components
    41  
    42  At a high level, wazero exposes a [Runtime][Runtime], which can compile the binary into [Compiled Module][CompiledModule],
    43  and instantiate it as a sandboxed [Module][Module].
    44  These sandboxed modules are isolated from each other (modulo imports) and the embedding Go program. In a sandbox,
    45  there are 4 types of objects: memory, global, table, and function. Functions might be exported by name, and they can be executed by
    46  the embedding Go programs. During the execution of a function, the objects in the sandbox will be accessed, for example,
    47  a Wasm function can read and write from the memory object in the sandbox. The same goes for globals and tables.
    48  
    49  Here's a diagram showing the relationship between these components.
    50  
    51  ```goat
    52                                               |           Access during execution
    53                                               |        +--------+-------+-----------+
    54                                               |        |        |       |       +---|
    55                                               |        |        |       |       |   |
    56                                               |        v        v       v       v   |
    57                                               |     (Memory, Globals, Table, Functions)
    58             Wasm Binary                       |                      |              ^
    59                 |                             |                      |              |
    60  +----------+   v   +--------------------+    |    1 : N    +------------------+    |
    61  | Runtime  | ----> |  Compiled  Module  |----|-----------> |      Module      |    |
    62  +----------+       +--------------------+    | Instantiate +------------------+    |
    63                                               |                      |              |
    64                                               |                      | 1 : N        |
    65                                               |                      v              |
    66                                               |             +-------------------+   |
    67                                               |             | Exported Function |---+
    68                                               |             +-------------------+
    69                                               |
    70                                               |
    71                             compile time      |      runtime
    72                                               |
    73  ```
    74  
    75  
    76  ### Host access
    77  
    78  First, a Wasm module can require the import of functions at instantiation phrase.
    79  Such import requirements are included in the original Wasm binary. For example,
    80  
    81  ```wat
    82  (module (import "env" "foo" (func)))
    83  ```
    84  
    85  this WebAssembly module requires importing the exported function named `foo` from the instantiated module named `env`.
    86  An imported functions can be called by the importing modules, and this is how a Wasm module interacts with the outside of
    87  its own sandbox.
    88  
    89  In wazero, the imported modules can be Host Modules which consist of Go functions. Therefore,
    90  the importing modules can invoke Go functions defined by the embedding Go programs.
    91  The notable example of this imported host module is wazero's [`wasi_snapshot_preview1`][wasi] module which provides
    92  the system calls to wasm modules because the Wasm specification itself doesn't define system calls. This way, Wasm modules
    93  are granted the ability to do, for example, file system access, etc.
    94  
    95  Here's the diagram of how a Wasm module accesses Go functions:
    96  
    97  ```goat
    98                                                          func add(foo, bar int32) int32 {
    99                                                              return foo + bar
   100                                                          }         |
   101                                                                    |
   102                                                                    | implements
   103                                  host module                       v
   104  +---------+                +------------------+          +-----------------+
   105  | Runtime | -------------> | (module: myhost) | -------> | (function: add) |
   106  +---------+  ^             +------------------+  export  +-----------------+
   107      \       /                                                       /
   108       \instantiate                                                  /
   109        \   /                                                       /
   110         \ v                                                       /
   111          \                                                       /
   112           \                                                     / imported
   113            \ (import "myhost" "add" (func))                    /
   114             \                                                 /
   115              \                                   +-----------/------+
   116               \                                  |          v       |
   117                \                                 |   (myhost.add)   |
   118                 v                                |        ^         |
   119                  +--------------------+          |        | call    |
   120                  | (module: need_add) |--------->| (export:use_add) <----- Exported
   121                  +--------------------+          |                  |
   122                                                  +------------------+
   123                                              functions in need_add's sandbox
   124  ```
   125  
   126  In this example diagram,  we instantiated a host module named `myhost` which consists of a Go function `add`, and it exports
   127  the Go function under the name `add`. Then, we instantiate the Wasm module which requires importing function whose module is `mymodule`
   128  and name is `add`. This case, the import target module instance and function already exists, and therefore the resulting sandbox contains
   129  the imported function in its sandbox. Finally, the importing module exports the function named `use_add` which in turns calls the imported function,
   130  therefore, we can freely access the imported Go function from the importing Wasm module.
   131  
   132  Here's [the working example in wazero repository][age-calculator], so please check it out for more details.
   133  
   134  
   135  ### Engine
   136  
   137  There's a concept called "engine" in wazero's codebase. It is in charge of how wazero compiles the raw Wasm binary, transforms it into
   138  intermediate data structure, caches the compiled information, and performs function calls of Wasm functions.
   139  Notably, the interpreter and compiler in wazero's [Runtime configuration][RuntimeConfig] refer to the type of engine tied to [Runtime][Runtime].
   140  
   141  #### Compiler
   142  
   143  In wazero, a compiler is a runtime configured to compile modules to platform-specific machine code ahead of time (AOT)
   144  during the creation of [CompiledModule][CompiledModule]. This means your WebAssembly functions execute
   145  natively at runtime of the embedding Go program. Compiler is faster than Interpreter, often by order of
   146  magnitude (10x) or more, and therefore enabled by default whenever available.
   147  
   148  #### Interpreter
   149  
   150  Interpreter is a naive interpreter-based implementation of Wasm virtual machine.
   151  Its implementation doesn't have any platform (GOARCH, GOOS) specific code,
   152  therefore interpreter can be used for any compilation target available for Go (such as riscv64).
   153  
   154  ## How do function calls work?
   155  
   156  WebAssembly runtimes let you call functions defined in wasm. How this works in
   157  wazero is different depending on your `RuntimeConfig`.
   158  
   159  * `RuntimeConfigCompiler` compiles machine code from your wasm, and jumps to
   160    that when invoking a function.
   161  * `RuntimeConfigInterpreter` does not generate code. It interprets wasm and
   162    executes go statements that correspond to WebAssembly instructions.
   163  
   164  How the compiler works precisely is a large topic. If you are interested in
   165  digging deeper, please look at [the dedicated documentation]({{< relref "/how_do_compiler_functions_work.md" >}})
   166  on this topic!
   167  
   168  ## Rationales behind wazero
   169  
   170  Please refer to [RATIONALE][rationale] for the notable rationales behind wazero's implementations.
   171  
   172  [Module]: https://pkg.go.dev/github.com/tetratelabs/wazero@v1.0.0-rc.1/api#Module
   173  [Runtime]: https://pkg.go.dev/github.com/tetratelabs/wazero#Runtime
   174  [RuntimeConfig]: https://pkg.go.dev/github.com/tetratelabs/wazero#RuntimeConfig
   175  [CompiledModule]: https://pkg.go.dev/github.com/tetratelabs/wazero#CompiledModule
   176  [godoc]: https://pkg.go.dev/github.com/tetratelabs/wazero
   177  [rationale]: https://github.com/tetratelabs/wazero/blob/main/RATIONALE.md
   178  [wasi]: https://github.com/tetratelabs/wazero/tree/main/imports/wasi_snapshot_preview1/example
   179  [age-calculator]: https://github.com/tetratelabs/wazero/blob/v1.0.0-rc.1/examples/import-go/age-calculator.go