github.com/AR1011/wazero@v1.0.5/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  ### Host access
    76  
    77  First, a Wasm module can require the import of functions at instantiation phrase.
    78  Such import requirements are included in the original Wasm binary. For example,
    79  
    80  ```wat
    81  (module (import "env" "foo" (func)))
    82  ```
    83  
    84  this WebAssembly module requires importing the exported function named `foo` from the instantiated module named `env`.
    85  An imported functions can be called by the importing modules, and this is how a Wasm module interacts with the outside of
    86  its own sandbox.
    87  
    88  In wazero, the imported modules can be Host Modules which consist of Go functions. Therefore,
    89  the importing modules can invoke Go functions defined by the embedding Go programs.
    90  The notable example of this imported host module is wazero's [`wasi_snapshot_preview1`][wasi] module which provides
    91  the system calls to wasm modules because the Wasm specification itself doesn't define system calls. This way, Wasm modules
    92  are granted the ability to do, for example, file system access, etc.
    93  
    94  Here's the diagram of how a Wasm module accesses Go functions:
    95  
    96  ```goat
    97                                                          func add(foo, bar int32) int32 {
    98                                                              return foo + bar
    99                                                          }         |
   100                                                                    |
   101                                                                    | implements
   102                                  host module                       v
   103  +---------+                +------------------+          +-----------------+
   104  | Runtime | -------------> | (module: myhost) | -------> | (function: add) |
   105  +---------+  ^             +------------------+  export  +-----------------+
   106      \       /                                                       /
   107       \instantiate                                                  /
   108        \   /                                                       /
   109         \ v                                                       /
   110          \                                                       /
   111           \                                                     / imported
   112            \ (import "myhost" "add" (func))                    /
   113             \                                                 /
   114              \                                   +-----------/------+
   115               \                                  |          v       |
   116                \                                 |   (myhost.add)   |
   117                 v                                |        ^         |
   118                  +--------------------+          |        | call    |
   119                  | (module: need_add) |--------->| (export:use_add) <----- Exported
   120                  +--------------------+          |                  |
   121                                                  +------------------+
   122                                              functions in need_add's sandbox
   123  ```
   124  
   125  In this example diagram, we instantiated a host module named `myhost` which consists of a Go function `add`, and it exports
   126  the Go function under the name `add`. Then, we instantiate the Wasm module which requires importing function whose module is `mymodule`
   127  and name is `add`. This case, the import target module instance and function already exists, and therefore the resulting sandbox contains
   128  the imported function in its sandbox. Finally, the importing module exports the function named `use_add` which in turns calls the imported function,
   129  therefore, we can freely access the imported Go function from the importing Wasm module.
   130  
   131  Here's [the working example in wazero repository][age-calculator], so please check it out for more details.
   132  
   133  ### Engine
   134  
   135  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
   136  intermediate data structure, caches the compiled information, and performs function calls of Wasm functions.
   137  Notably, the interpreter and compiler in wazero's [Runtime configuration][RuntimeConfig] refer to the type of engine tied to [Runtime][Runtime].
   138  
   139  #### Compiler
   140  
   141  In wazero, a compiler is a runtime configured to compile modules to platform-specific machine code ahead of time (AOT)
   142  during the creation of [CompiledModule][CompiledModule]. This means your WebAssembly functions execute
   143  natively at runtime of the embedding Go program. Compiler is faster than Interpreter, often by order of
   144  magnitude (10x) or more, and therefore enabled by default whenever available.
   145  
   146  #### Interpreter
   147  
   148  Interpreter is a naive interpreter-based implementation of Wasm virtual machine.
   149  Its implementation doesn't have any platform (GOARCH, GOOS) specific code,
   150  therefore interpreter can be used for any compilation target available for Go (such as riscv64).
   151  
   152  ## How do function calls work?
   153  
   154  WebAssembly runtimes let you call functions defined in wasm. How this works in
   155  wazero is different depending on your `RuntimeConfig`.
   156  
   157  - `RuntimeConfigCompiler` compiles machine code from your wasm, and jumps to
   158    that when invoking a function.
   159  - `RuntimeConfigInterpreter` does not generate code. It interprets wasm and
   160    executes go statements that correspond to WebAssembly instructions.
   161  
   162  How the compiler works precisely is a large topic. If you are interested in
   163  digging deeper, please look at [the dedicated documentation]({{< relref "/how_do_compiler_functions_work.md" >}})
   164  on this topic!
   165  
   166  ## Rationales behind wazero
   167  
   168  Please refer to [RATIONALE][rationale] for the notable rationales behind wazero's implementations.
   169  
   170  [Module]: https://pkg.go.dev/github.com/AR1011/wazero@v1.0.0-rc.1/api#Module
   171  [Runtime]: https://pkg.go.dev/github.com/AR1011/wazero#Runtime
   172  [RuntimeConfig]: https://pkg.go.dev/github.com/AR1011/wazero#RuntimeConfig
   173  [CompiledModule]: https://pkg.go.dev/github.com/AR1011/wazero#CompiledModule
   174  [godoc]: https://pkg.go.dev/github.com/AR1011/wazero
   175  [rationale]: https://github.com/AR1011/wazero/blob/main/RATIONALE.md
   176  [wasi]: https://github.com/AR1011/wazero/tree/main/imports/wasi_snapshot_preview1/example
   177  [age-calculator]: https://github.com/AR1011/wazero/blob/v1.0.0-rc.1/examples/import-go/age-calculator.go