| 1 |
<div align="center"> |
| 2 |
<h1><code>wasm-tools</code></h1> |
| 3 |
|
| 4 |
<strong>A <a href="https://bytecodealliance.org/">Bytecode Alliance</a> project</strong> |
| 5 |
|
| 6 |
<p> |
| 7 |
<strong>CLI and Rust libraries for low-level manipulation of WebAssembly modules</strong> |
| 8 |
</p> |
| 9 |
</div> |
| 10 |
|
| 11 |
# Installation |
| 12 |
|
| 13 |
[Precompiled artifacts built on CI][artifacts] are available for download for |
| 14 |
each release. |
| 15 |
|
| 16 |
[artifacts]: https://github.com/bytecodealliance/wasm-tools/releases |
| 17 |
|
| 18 |
To build from source first [install Rust for your |
| 19 |
platform](https://www.rust-lang.org/tools/install) and then use the included |
| 20 |
Cargo package manager to install: |
| 21 |
|
| 22 |
``` |
| 23 |
$ cargo install --locked wasm-tools |
| 24 |
``` |
| 25 |
|
| 26 |
Alternatively if you use [`cargo |
| 27 |
binstall`](https://github.com/cargo-bins/cargo-binstall) then that can be used |
| 28 |
to install [the precompiled artifacts][artifacts] instead: |
| 29 |
|
| 30 |
``` |
| 31 |
$ cargo binstall wasm-tools |
| 32 |
``` |
| 33 |
|
| 34 |
Installation can be confirmed with: |
| 35 |
|
| 36 |
``` |
| 37 |
$ wasm-tools --version |
| 38 |
``` |
| 39 |
|
| 40 |
Subcommands can be explored with: |
| 41 |
|
| 42 |
``` |
| 43 |
$ wasm-tools help |
| 44 |
``` |
| 45 |
|
| 46 |
# Examples |
| 47 |
|
| 48 |
Basic validation/printing/parsing: |
| 49 |
|
| 50 |
```sh |
| 51 |
# Validate a WebAssembly file |
| 52 |
$ wasm-tools validate foo.wasm |
| 53 |
|
| 54 |
# Validate a WebAssembly module in the text format, automatically converting to |
| 55 |
# binary. |
| 56 |
$ wasm-tools validate foo.wat |
| 57 |
|
| 58 |
# Validate a WebAssembly file enabling an off-by-default feature |
| 59 |
$ wasm-tools validate foo.wasm --features=exception-handling |
| 60 |
|
| 61 |
# Validate a WebAssembly file with a default-enabled feature disabled |
| 62 |
$ wasm-tools validate foo.wasm --features=-simd |
| 63 |
|
| 64 |
# Print the text format of a module to stdout |
| 65 |
$ wasm-tools print foo.wasm |
| 66 |
|
| 67 |
# Convert a binary module to text |
| 68 |
$ wasm-tools print foo.wasm -o foo.wat |
| 69 |
|
| 70 |
# Convert a text module to binary |
| 71 |
$ wasm-tools parse foo.wat -o foo.wasm |
| 72 |
``` |
| 73 |
|
| 74 |
Simple mutation as well as piping commands together: |
| 75 |
|
| 76 |
```sh |
| 77 |
# Mutate a WebAssembly module and print its text representation to stdout |
| 78 |
$ wasm-tools mutate foo.wasm -t |
| 79 |
|
| 80 |
# Mutate a WebAssembly module with a non-default seed and validate that the |
| 81 |
# output is a valid module. |
| 82 |
$ wasm-tools mutate foo.wasm --seed 192 | wasm-tools validate |
| 83 |
|
| 84 |
# Demangle Rust/C++ symbol names in the `name` section, strip all other custom |
| 85 |
# sections, and then print out what binary sections remain. |
| 86 |
$ wasm-tools demangle foo.wasm | wasm-tools strip | wasm-tools objdump |
| 87 |
``` |
| 88 |
|
| 89 |
Working with components: |
| 90 |
|
| 91 |
```sh |
| 92 |
# Print the WIT interface of a component |
| 93 |
$ wasm-tools component wit component.wasm |
| 94 |
|
| 95 |
# Convert WIT text files to a binary-encoded WIT package, printing the result to |
| 96 |
# stdout |
| 97 |
$ wasm-tools component wit ./wit -t |
| 98 |
|
| 99 |
# Convert a WIT document to JSON |
| 100 |
$ wasm-tools component wit ./wit --json |
| 101 |
|
| 102 |
# Round trip WIT through the binary-encoded format to stdout. |
| 103 |
$ wasm-tools component wit ./wit --wasm | wasm-tools component wit |
| 104 |
|
| 105 |
# Convert a core WebAssembly binary into a component. Note that this requires |
| 106 |
# WIT metadata having previously been embedded in the core wasm module. |
| 107 |
$ wasm-tools component new my-core.wasm -o my-component.wasm |
| 108 |
|
| 109 |
# Convert a core WebAssembly binary which uses WASI to a component. |
| 110 |
$ wasm-tools component new my-core.wasm -o my-component.wasm --adapt wasi_snapshot_preview1.reactor.wasm |
| 111 |
``` |
| 112 |
|
| 113 |
### CLI Conventions |
| 114 |
|
| 115 |
There are a few conventions that all CLI commands adhere to: |
| 116 |
|
| 117 |
* All subcommands print "short help" with `-h` and "long help" with `--help`. |
| 118 |
* Input is by default read from stdin if no file input is specified (when |
| 119 |
applicable). |
| 120 |
* Output is by default sent to stdout if a `-o` or `--output` flag is not |
| 121 |
provided. Binary WebAssembly is not printed to a tty by default, however. |
| 122 |
* Commands which output WebAssembly binaries all support a `-t` or `--wat` flag |
| 123 |
to generate the WebAssembly text format instead. |
| 124 |
* A `-v` or `--verbose` flag can be passed to enable log messages throughout the |
| 125 |
tooling. Verbosity can be turned up by passing the flag multiple times such as |
| 126 |
`-vvv`. |
| 127 |
* Color in error messages and console output is enabled by default for TTY based |
| 128 |
outputs and can be configured with a `--color` argument. |
| 129 |
|
| 130 |
# Tools included |
| 131 |
|
| 132 |
The `wasm-tools` binary internally contains a number of subcommands for working |
| 133 |
with wasm modules and component. Many subcommands also come with Rust crates |
| 134 |
that can be use programmatically as well: |
| 135 |
|
| 136 |
| CLI | Rust Crate | Playground | Description | |
| 137 |
|------|------|--------|------------| |
| 138 |
| `wasm-tools validate` | [wasmparser] | | Validate a WebAssembly file | |
| 139 |
| `wasm-tools parse` | [wat] and [wast] | [parse](https://bytecodealliance.github.io/wasm-tools/parse) | Translate the WebAssembly text format to binary | |
| 140 |
| `wasm-tools print` | [wasmprinter] | [print](https://bytecodealliance.github.io/wasm-tools/print) | Translate the WebAssembly binary format to text | |
| 141 |
| `wasm-tools smith` | [wasm-smith] | | Generate a valid WebAssembly module from an input seed | |
| 142 |
| `wasm-tools mutate` | [wasm-mutate] | | Mutate an input wasm file into a new valid wasm file | |
| 143 |
| `wasm-tools shrink` | [wasm-shrink] | | Shrink a wasm file while preserving a predicate | |
| 144 |
| `wasm-tools dump` | | | Print debugging information about the binary format | |
| 145 |
| `wasm-tools objdump` | | | Print debugging information about section headers | |
| 146 |
| `wasm-tools strip` | | | Remove custom sections from a WebAssembly file | |
| 147 |
| `wasm-tools demangle` | | | Demangle Rust and C++ symbol names in the `name` section | |
| 148 |
| `wasm-tools compose` | [wasm-compose] | | Compose wasm components together (*deprecated*) | |
| 149 |
| `wasm-tools component new` | [wit-component] | | Create a component from a core wasm binary | |
| 150 |
| `wasm-tools component wit` | | | Extract a `*.wit` interface from a component | |
| 151 |
| `wasm-tools component embed` | | | Embed a `component-type` custom section in a core wasm binary | |
| 152 |
| `wasm-tools component unbundle` | | | Extract core wasm modules from a component | |
| 153 |
| `wasm-tools metadata show` | [wasm-metadata] | | Show name and producer metadata in a component or module | |
| 154 |
| `wasm-tools metadata add` | | | Add name or producer metadata to a component or module | |
| 155 |
| `wasm-tools addr2line` | | | Translate wasm offsets to filename/line numbers with DWARF | |
| 156 |
| `wasm-tools completion` | | | Generate shell completion scripts for `wasm-tools` | |
| 157 |
| `wasm-tools json-from-wast` | | | Convert a `*.wast` file into JSON commands | |
| 158 |
| `wasm-tools wast` | | | Validate the structure of a `*.wast` file | |
| 159 |
|
| 160 |
[wasmparser]: https://crates.io/crates/wasmparser |
| 161 |
[wat]: https://crates.io/crates/wat |
| 162 |
[wast]: https://crates.io/crates/wast |
| 163 |
[wasmprinter]: https://crates.io/crates/wasmprinter |
| 164 |
[wasm-smith]: https://crates.io/crates/wasm-smith |
| 165 |
[wasm-mutate]: https://crates.io/crates/wasm-mutate |
| 166 |
[wasm-shrink]: https://crates.io/crates/wasm-shrink |
| 167 |
[wit-component]: https://crates.io/crates/wit-component |
| 168 |
[wasm-compose]: https://crates.io/crates/wasm-compose |
| 169 |
[wasm-metadata]: https://crates.io/crates/wasm-metadata |
| 170 |
|
| 171 |
The `wasm-tools` CLI contains useful tools for debugging WebAssembly modules and |
| 172 |
components. The various subcommands all have `--help` explainer texts to |
| 173 |
describe more about their functionality as well. |
| 174 |
|
| 175 |
# WebAssembly proposals |
| 176 |
|
| 177 |
This repository strives to implement any standardized proposal to WebAssembly. |
| 178 |
All proposals that are [Stage 4+](https://github.com/WebAssembly/proposals) are |
| 179 |
enabled by default in validation. Support in the text format is always enabled |
| 180 |
at all times for all proposals. |
| 181 |
|
| 182 |
Currently implemented proposals in this repository that are stage 4+ are: |
| 183 |
|
| 184 |
* [x] [annotations](https://github.com/WebAssembly/annotations) |
| 185 |
* [x] [branch-hinting](https://github.com/WebAssembly/branch-hinting) |
| 186 |
* [x] [bulk-memory](https://github.com/WebAssembly/bulk-memory-operations) |
| 187 |
* [x] [component-model](https://github.com/WebAssembly/component-model) (note |
| 188 |
this is an exception, it's not stage4+ but is on-by-default) |
| 189 |
* [x] [exception-handling](https://github.com/WebAssembly/exception-handling) |
| 190 |
* [x] [extended-const](https://github.com/WebAssembly/extended-const) |
| 191 |
* [x] [extended-name-section](https://github.com/WebAssembly/extended-name-section) |
| 192 |
(note this is not phase 4 but `wast` does not have the concept of features) |
| 193 |
* [x] [function-references](https://github.com/WebAssembly/function-references) |
| 194 |
* [x] [gc](https://github.com/WebAssembly/gc) |
| 195 |
* [x] [memory64](https://github.com/WebAssembly/memory64) |
| 196 |
* [x] [multi-memory](https://github.com/WebAssembly/multi-memory) |
| 197 |
* [x] [multi-value](https://github.com/WebAssembly/multi-value) |
| 198 |
* [x] [mutable-global](https://github.com/WebAssembly/mutable-global) |
| 199 |
* [x] [reference-types](https://github.com/WebAssembly/reference-types) |
| 200 |
* [x] [relaxed-simd](https://github.com/WebAssembly/relaxed-simd) |
| 201 |
* [x] [saturating-float-to-int](https://github.com/WebAssembly/nontrapping-float-to-int-conversions) |
| 202 |
* [x] [sign-extension-ops](https://github.com/WebAssembly/sign-extension-ops) |
| 203 |
* [x] [simd](https://github.com/WebAssembly/simd) |
| 204 |
* [x] [tail-call](https://github.com/WebAssembly/tail-call) |
| 205 |
* [x] [threads](https://github.com/WebAssembly/threads) |
| 206 |
* [x] [wat-numeric-values](https://github.com/WebAssembly/wat-numeric-values) |
| 207 |
(note this is not phase 4 but `wast` does not have the concept of features) |
| 208 |
|
| 209 |
These proposals are implemented but have not yet reached stage 4. This means |
| 210 |
they are disabled by default in validation and their implementation may change |
| 211 |
over time as the proposal evolves. Additionally the proposal may also have |
| 212 |
changed since these proposals were implemented, so there may be a mismatch too. |
| 213 |
|
| 214 |
* [x] [custom-page-sizes](https://github.com/WebAssembly/custom-page-sizes) |
| 215 |
* [x] [memory-control](https://github.com/WebAssembly/memory-control) |
| 216 |
* [x] [shared-everything-threads](https://github.com/WebAssembly/shared-everything-threads) |
| 217 |
* [x] [stack-switching](https://github.com/WebAssembly/stack-switching) |
| 218 |
* [x] [wide-arithmetic](https://github.com/WebAssembly/wide-arithmetic) |
| 219 |
|
| 220 |
# Libraries |
| 221 |
|
| 222 |
As mentioned above many of the tools of the `wasm-tools` CLI have libraries |
| 223 |
implemented in this repository as well. These libraries are: |
| 224 |
|
| 225 |
* [**`wasmparser`**](crates/wasmparser) - a library to parse WebAssembly binaries |
| 226 |
* [**`wat`**](crates/wat) - a library to parse the WebAssembly text format |
| 227 |
* [**`wast`**](crates/wast) - like `wat`, except provides an AST |
| 228 |
* [**`wasmprinter`**](crates/wasmprinter) - prints WebAssembly binaries in their |
| 229 |
string form |
| 230 |
* [**`wasm-mutate`**](crates/wasm-mutate) - a WebAssembly test case mutator |
| 231 |
* [**`wasm-shrink`**](crates/wasm-shrink) - a WebAssembly test case shrinker |
| 232 |
* [**`wasm-smith`**](crates/wasm-smith) - a WebAssembly test case generator |
| 233 |
* [**`wasm-encoder`**](crates/wasm-encoder) - a crate to generate a binary |
| 234 |
WebAssembly module |
| 235 |
* [**`wit-parser`**](crates/wit-parser) - a crate to parse and manage `*.wit` |
| 236 |
files and interfaces. |
| 237 |
* [**`wit-encoder`**](crates/wit-encoder) - a crate to generate `*.wit` files and |
| 238 |
interfaces. |
| 239 |
* [**`wit-component`**](crates/wit-component) - a crate to create components |
| 240 |
from core wasm modules. |
| 241 |
* [**`wit-smith`**](crates/wit-smith) - a `*.wit` test case generator. |
| 242 |
* [**`wasm-metadata`**](crates/wasm-metadata) - a crate to read and manipulate |
| 243 |
WebAssembly metadata |
| 244 |
|
| 245 |
It's recommended to use the libraries directly rather than the CLI tooling when |
| 246 |
embedding into a separate project. |
| 247 |
|
| 248 |
# C/C++ bindings |
| 249 |
|
| 250 |
Using the `CMakeLists.txt` in `crates/c-api`, `wasm-tools` can be used from the |
| 251 |
[`wasm-tools.h` header](crates/c-api/include/wasm-tools.h). Note that these |
| 252 |
bindings do not comprehensively cover all the functionality of this repository |
| 253 |
at this time, but please feel free to contribute more if you find functions |
| 254 |
useful! |
| 255 |
|
| 256 |
# Versioning and Releases |
| 257 |
|
| 258 |
This repository has both a CLI and a suite of crates that is published to |
| 259 |
crates.io (Rust's package manager). The versioning scheme used by this |
| 260 |
repository looks like: |
| 261 |
|
| 262 |
* `wasm-tools` - the CLI follows the versioning pattern of `1.X.Y`. Frequently |
| 263 |
`Y` is 0 and `X` is bumped as part of a release for this repository. |
| 264 |
* `wat` - this Rust crate is versioned at `1.X.Y` as well and matches the |
| 265 |
`wasm-tools` version. |
| 266 |
* `wast` - this Rust crate is versioned as `X.0.Y`. The `X` here matches the `X` |
| 267 |
in `1.X.Y` of `wasm-tools`. |
| 268 |
* All other crates - all other crates in this repository are versioned at |
| 269 |
`0.X.Y` where `X` matches the `1.X.Y` of `wasm-tools`. |
| 270 |
|
| 271 |
Note that the `Y` of all the versions above will also match for any release of |
| 272 |
this repository. This versioning scheme is intended to reflect the stable nature |
| 273 |
of the CLI and the `wat` crate in terms of API stability. Other crates, however, |
| 274 |
all receive a major version bump that are not automatically considered API |
| 275 |
compatible on all releases. This reflects how WebAssembly itself is an evolving |
| 276 |
standard which is not an unchanging foundation. All of the crates in this |
| 277 |
repository are suitable for "production use" but be aware that API stability is |
| 278 |
not guaranteed over time. If you have difficulty upgrading versions please feel |
| 279 |
free to file an issue and we can help out. |
| 280 |
|
| 281 |
Also, this repository does not currently have a strict release cadence. Releases |
| 282 |
are done on an as-needed basis. If you'd like a release done please feel free to |
| 283 |
reach out on [Zulip], file an issue, leave a comment on a PR, or otherwise |
| 284 |
contact a maintainer. |
| 285 |
|
| 286 |
[Zulip]: https://bytecodealliance.zulipchat.com/ |
| 287 |
|
| 288 |
For maintainers, the release process looks like: |
| 289 |
|
| 290 |
* Go to [this link](https://github.com/bytecodealliance/wasm-tools/actions/workflows/release-process.yml) |
| 291 |
* Click on "Run workflow" in the UI. |
| 292 |
* Use the default `bump` argument and hit "Run workflow" |
| 293 |
* Wait for a PR to be created by CI. You can watch the "Actions" tab for if |
| 294 |
things go wrong. |
| 295 |
* When the PR opens, close it then reopen it. Don't ask questions. |
| 296 |
* Review the PR, approve it, then queue it for merge. |
| 297 |
|
| 298 |
That should be it, but be sure to keep an eye on CI in case anything goes wrong. |
| 299 |
|
| 300 |
## Patch Release Process |
| 301 |
|
| 302 |
Patch releases aren't too frequent so the process is a bit more involved at this |
| 303 |
time. The basic idea is the same though as a normal release: |
| 304 |
|
| 305 |
* Determine that you're going to make a patch release for the release of |
| 306 |
`wasm-tools` version `1.N.0`. |
| 307 |
* If `N` is the latest version on `main`, or basically if a major release hasn't |
| 308 |
happened since `N`, then land changes on `main` first. Then go to [this |
| 309 |
link](https://github.com/bytecodealliance/wasm-tools/actions/workflows/release-process.yml) |
| 310 |
and run the workflow against the `main` branch with `bump-patch` as an |
| 311 |
argument. Double-check all changes from the `v1.N.0` tag to ensure that no |
| 312 |
breaking changes in any crate have landed. |
| 313 |
* If `N` is not the latest version on `main`, then create a new branch in the |
| 314 |
repository named `release-N` starting from the `v1.N.0` tag. Next land changes |
| 315 |
as PRs against this branch, and note that CI may not have been run in awhile |
| 316 |
so it may need some fixes. Then run the same workflow as above against the |
| 317 |
`release-N` branch with the `bump-patch` argument. Again double-check that no |
| 318 |
breaking changes have been made. |
| 319 |
|
| 320 |
Basically a `release-N` may need to be manually created if the `main` branch has |
| 321 |
moved beyond `N`. After that though it should work basically the same where a |
| 322 |
workflow is run and a PR is merged and that's all that's necessary. |
| 323 |
|
| 324 |
# Contributing |
| 325 |
|
| 326 |
See [CONTRIBUTING.md](./CONTRIBUTING.md) for more information about contributing |
| 327 |
to this repository. |
| 328 |
|
| 329 |
# License |
| 330 |
|
| 331 |
This project is triple licenced under the Apache 2/ Apache 2 with LLVM exceptions/ MIT licences. The reasoning for this is: |
| 332 |
- Apache 2/ MIT is common in the rust ecosystem. |
| 333 |
- Apache 2/ MIT is used in the rust compiler, and some of this code may be migrated there. |
| 334 |
- Some of this code may be used in compiler output, and the Apache 2 with LLVM exceptions licence is useful for this. |
| 335 |
|
| 336 |
For more details see |
| 337 |
- [Apache 2 Licence](LICENSE-APACHE) |
| 338 |
- [Apache 2 Licence with LLVM exceptions](LICENSE-Apache-2.0_WITH_LLVM-exception) |
| 339 |
- [MIT Licence](LICENSE-MIT) |
| 340 |
|
| 341 |
### Contribution |
| 342 |
|
| 343 |
Unless you explicitly state otherwise, any contribution intentionally submitted |
| 344 |
for inclusion in this project by you, as defined in the Apache 2/ Apache 2 with LLVM exceptions/ MIT licenses, |
| 345 |
shall be licensed as above, without any additional terms or conditions. |