A prototype WebAssembly linker using module linking.

Overview

WebAssembly Module Linker

Please note: this is an experimental project.

wasmlink is a prototype WebAssembly module linker that can link together a module and its dependencies using module linking.

When used in combination with witx-bindgen, it is capable of generating interface adapter functions in WebAssembly that enables modules to exchange interface types such as strings.

Building

To build wasmlink:

$ cargo build

Running

To run wasmlink:

$ cargo run

Testing

To run tests:

$ cargo test --all

Demo

The demo requires cargo-wasi, so install it using cargo:

$ cargo install cargo-wasi

First, build the markdown module:

$ cd demo/markdown
$ cargo wasi build
$ cp markdown.witx target/wasm32-wasi/debug/markdown.witx

This module exposes an interface consisting of a render function that takes a string (the Markdown) as an argument and returns a string (the rendered HTML).

Note: the linker currently expects either an embedded witx file in a custom section of the module or a witx file of the same name next to the input wasm module, so we copy the witx file to the target directory above.

Next, build the renderer module:

$ cd demo/renderer
$ cargo wasi build

This module will read input via stdin, pass the input as a string to the render function from the markdown module, and then print the returned HTML to stdout.

With the two modules now built, it is time to link them together so that they can be run directly with Wasmtime:

$ cargo run --release -- -i markdown=demo/markdown/target/wasm32-wasi/debug/markdown.wasm -p wasmtime -o linked.wasm demo/renderer/target/wasm32-wasi/debug/renderer.wasm

This command produces a linked module linked.wasm that we can now run directly with Wasmtime:

$ echo '# Hello\nworld' | wasmtime --enable-module-linking --enable-multi-memory linked.wasm

As the linked module uses features from both the module linking and multi-memory WebAssembly proposals, support has to be explicitly enabled in Wasmtime to enable the module to run.

If everything worked correctly, this should render the Markdown:

# Hello
world

as the following HTML:

<h1>Helloh1>
<p>worldp>
Comments
  • Update the witx-bindgen dependencies for test modules.

    Update the witx-bindgen dependencies for test modules.

    This PR updates the witx-bindgen dependencies for the test modules.

    As a result, this fixes the issue of passing f32/f64 for param-aliased types where witx-bindgen was generating code that failed to convert from &f32/&f64 to f32/f64.

    opened by peterhuene 0
  • Bug fixes and variants test.

    Bug fixes and variants test.

    This PR fixes the following bugs:

    • Returned lists are now properly freed.
    • Parameter variants with lists now properly assign to the list local conditionally rather than always.
    • Variants without lists in their cases will no longer cause unnecessary code generation.

    Additionally, the variants test case was copied from witx-codgen and added as a test case here.

    opened by peterhuene 0
  • Reimplement code generation.

    Reimplement code generation.

    This PR reimplements code generation such that it uses a custom adapter for calling from parent into adapted module.

    witx-bindgen was used before to generate code; however, the intention of witx-bindgen code generation is to convert from a language-specific ABI to the canonical ABI.

    This complicated what needed to happen for adapting a call where both sides were using the canonical ABI, especially when it came to implement support for variants.

    Instead, this uses a simpler adapter that essentially finds lists in the types being exchanged and ensures the proper marshaling of the list data.

    opened by peterhuene 0
  • Implement support for lists.

    Implement support for lists.

    Support is now implemented for lists of any currently supported types (i.e. everything but variants).

    It also fixes a bug in marshaling of canonical lists that have an element size greater than 1.

    Tests were added for lists of some basic types.

    Additionally, interface functions that don't need to be adapted are now directly aliased rather than generating an adapter function that simply called the original function.

    opened by peterhuene 0
  • Implement support for records and flags.

    Implement support for records and flags.

    This PR implements support for linking with interfaces that pass and return record types and flag types.

    New tests were added that were modeled after the tests found in the witx-bindgen repo.

    opened by peterhuene 0
  • Add integration tests.

    Add integration tests.

    This PR adds a basic integration test that will build test modules to Wasm, link them together, and then run the "main" module via the Wasmtime API.

    opened by peterhuene 0
  • Fix redundant edges in the dependency graph.

    Fix redundant edges in the dependency graph.

    This commit fixes redundant edges in the dependency graph that will occur when a module imports multiple functions from a dependency.

    Without this fix, the linked module is invalid as it will try to create multiple shim instances, one for each imported function.

    opened by peterhuene 0
  • Bug fixes and more tests.

    Bug fixes and more tests.

    • Improve error message for incorrect export type.
    • Make the order of types in the linked module stable.
    • Make the order of imports in the linked module stable.
    • Add a simple test for linking two modules without an interface.
    • Add a simple test for linking two modules with an interface.
    • Enable validation of function bodies in parsed modules.
    opened by peterhuene 0
  • Fix cycle check when building module graph.

    Fix cycle check when building module graph.

    This PR allows the graph to form diamond patterns (i.e. a module may have multiple in-edges) while still checking for a cycle by performing a topographical sort.

    opened by peterhuene 0
  • Implement more adapter code generation.

    Implement more adapter code generation.

    This PR implements more adapter code generation.

    Intrinsic types are now supported in addition to strings.

    Support for zero, one, or multiple results from interface functions was added.

    Some basic code generation unit tests were implemented.

    opened by peterhuene 0
Owner
Peter Huene
Staff Software Engineer @ Fastly
Peter Huene
A simple tool to convert a WebAssembly module to a WASI component (component model, WASI-preview2).

A simple tool to convert a WebAssembly module (wasm32-wasi, wasm32-freestanding) to a WASI component (component model, WASI-preview2). Installation In

Frank Denis 5 Feb 18, 2024
Wasmtime - Standalone JIT-style runtime for WebAssembly, using Cranelift

wasmtime A standalone runtime for WebAssembly A Bytecode Alliance project Guide | Contributing | Website | Chat Installation The Wasmtime CLI can be i

Bytecode Alliance 11.1k Jan 2, 2023
Standalone JIT-style runtime for WebAssembly, using Cranelift

wasmtime A standalone runtime for WebAssembly A Bytecode Alliance project Guide | Contributing | Website | Chat Installation The Wasmtime CLI can be i

Bytecode Alliance 11.1k Dec 31, 2022
Zaplib is an open-source library for speeding up web applications using Rust and WebAssembly.

⚡ Zaplib Zaplib is an open-source library for speeding up web applications using Rust and WebAssembly. It lets you write high-performance code in Rust

Zaplib 1.2k Jan 5, 2023
A template for kick starting a Rust and WebAssembly project using wasm-pack.

A template for kick starting a Rust and WebAssembly project using wasm-pack.

Haoxi Tan 1 Feb 14, 2022
Client for integrating private analytics in fast and reliable libraries and apps using Rust and WebAssembly

TelemetryDeck Client Client for integrating private analytics in fast and reliable libraries and apps using Rust and WebAssembly The library provides

Konstantin 2 Apr 20, 2022
Sealed boxes implementation for Rust/WebAssembly.

Sealed boxes for Rust/WebAssembly This Rust crate provides libsodium sealed boxes for WebAssembly. Usage: // Recipient: create a new key pair let reci

Frank Denis 16 Aug 28, 2022
WebAssembly on Rust is a bright future in making application runs at the Edge or on the Serverless technologies.

WebAssembly Tour WebAssembly on Rust is a bright future in making application runs at the Edge or on the Serverless technologies. We spend a lot of ti

Thang Chung 129 Dec 28, 2022
WebAssembly modules that use Azure services

This is an experimental repository containing WebAssembly modules running on top of WAGI (WebAssembly Gateway Interface, which allows you to run WebAssembly WASI binaries as HTTP handlers) and using Azure services.

null 7 Apr 18, 2022
WebAssembly Service Porter

WebAssembly Service Porter.

henrylee2cn 12 Dec 12, 2022
🚀Wasmer is a fast and secure WebAssembly runtime that enables super lightweight containers to run anywhere

Wasmer is a fast and secure WebAssembly runtime that enables super lightweight containers to run anywhere: from Desktop to the Cloud, Edge and IoT devices.

Wasmer 14.1k Jan 8, 2023
WAGI: WebAssembly Gateway Interface

Write HTTP handlers in WebAssembly with a minimal amount of work

null 724 Jan 6, 2023
A console and web-based Gomoku written in Rust and WebAssembly

?? rust-gomoku A console and web-based Gomoku written in Rust and WebAssembly Getting started with cargo & npm Install required program, run # install

namkyu1999 2 Jan 4, 2022
WebAssembly development with Trunk & Vite.js

Trunk & Vite.js Demo Trunk is a WASM web application bundler for Rust, and Vite.js is next Generation Frontend Tooling. Ok, they are together now for

Libing Chen 6 Nov 24, 2021
darkforest is a console and web-based Roguelike written in Rust and WebAssembly.

darkforest darkforest is a console and web-based Roguelike written in Rust and WebAssembly. Key Features TBA Quick Start TBA How To Contribute Contrib

Chris Ohk 5 Oct 5, 2021
WebAssembly to Lua translator, with runtime

This is a WIP (read: absolutely not ready for serious work) tool for translating WebAssembly into Lua. Support is specifically for LuaJIT, with the se

null 43 Dec 31, 2022
A Rust ESP stack trace decoder that can also runs in your browser thanks to WebAssembly

ESP Stack Trace Decoder A Rust ESP stack trace decoder that can also runs in your browser thanks to WebAssembly. It is composed of a ⌨️ Rust library,

Maxime BORGES 20 Oct 5, 2022
Simple file sharing with client-side encryption, powered by Rust and WebAssembly

Hako Simple file sharing with client-side encryption, powered by Rust and WebAssembly Not feature-packed, but basic functionalities are just working.

Jaehyeon Park 30 Nov 25, 2022
bn.js bindings for Rust & WebAssembly with primitive-types support

bn.rs bn.js bindings for Rust & WebAssembly with primitive-types support Write Rust code that uses BN use std::str::FromStr; use primitive_types::{H1

Alexey Shekhirin 23 Nov 22, 2022