1986f9f79SAlex Crichton# Coding guidelines 2986f9f79SAlex Crichton 3986f9f79SAlex CrichtonFor the most part, Wasmtime and Cranelift follow common Rust conventions and 4986f9f79SAlex Crichton[pull request] (PR) workflows, though we do have a few additional things to 5986f9f79SAlex Crichtonbe aware of. 6986f9f79SAlex Crichton 7986f9f79SAlex Crichton[pull request]: https://help.github.com/articles/about-pull-requests/ 8986f9f79SAlex Crichton 947d3c8deSNick Fitzgerald### `rustfmt` 10986f9f79SAlex Crichton 11986f9f79SAlex CrichtonAll PRs must be formatted according to rustfmt, and this is checked in the 12986f9f79SAlex Crichtoncontinuous integration tests. You can format code locally with: 13986f9f79SAlex Crichton 1492cfda1bSVictor Adossi```console 1592cfda1bSVictor Adossicargo fmt 16986f9f79SAlex Crichton``` 17986f9f79SAlex Crichton 18986f9f79SAlex Crichtonat the root of the repository. You can find [more information about rustfmt 19986f9f79SAlex Crichtononline](https://github.com/rust-lang/rustfmt) too, such as how to configure 20986f9f79SAlex Crichtonyour editor. 21986f9f79SAlex Crichton 22c0c3e798SAlex Crichton### Compiler Warnings and Lints 23c0c3e798SAlex Crichton 24c0c3e798SAlex CrichtonWasmtime promotes all compiler warnings to errors in CI, meaning that the `main` 25c0c3e798SAlex Crichtonbranch will never have compiler warnings for the version of Rust that's being 26c0c3e798SAlex Crichtontested on CI. Compiler warnings change over time, however, so it's not always 27c0c3e798SAlex Crichtonguaranteed that Wasmtime will build with zero warnings given an arbitrary 28c0c3e798SAlex Crichtonversion of Rust. If you encounter compiler warnings on your version of Rust 29c0c3e798SAlex Crichtonplease feel free to send a PR fixing them. 30c0c3e798SAlex Crichton 31c0c3e798SAlex CrichtonDuring local development, however, compiler warnings are simply warnings and the 32c0c3e798SAlex Crichtonbuild and tests can still succeed despite the presence of warnings. This can be 33c0c3e798SAlex Crichtonuseful because warnings are often quite prevalent in the middle of a 34c0c3e798SAlex Crichtonrefactoring, for example. By the time you make a PR, though, we'll require that 35c0c3e798SAlex Crichtonall warnings are resolved or otherwise CI will fail and the PR cannot land. 36c0c3e798SAlex Crichton 37c0c3e798SAlex CrichtonCompiler lints are controlled through the `[workspace.lints.rust]` table in the 38c0c3e798SAlex Crichton`Cargo.toml` at the root of the Wasmtime repository. A few allow-by-default 39c0c3e798SAlex Crichtonlints are enabled such as `trivial_numeric_casts`, and you're welcome to enable 40c0c3e798SAlex Crichtonmore lints as applicable. Lints can additionally be enabled on a per-crate basis 41c0c3e798SAlex Crichtonsuch as placing this in a `src/lib.rs` file: 42c0c3e798SAlex Crichton 43c0c3e798SAlex Crichton```rust 44c0c3e798SAlex Crichton#![warn(trivial_numeric_casts)] 45c0c3e798SAlex Crichton``` 46c0c3e798SAlex Crichton 47c0c3e798SAlex CrichtonUsing `warn` here will allow local development to continue while still causing 48c0c3e798SAlex CrichtonCI to promote this warning to an error. 49c0c3e798SAlex Crichton 50c0c3e798SAlex Crichton### Clippy 51c0c3e798SAlex Crichton 52c0c3e798SAlex CrichtonAll PRs are gated on `cargo clippy` passing for all workspace crates and 53c0c3e798SAlex Crichtontargets. All clippy lints, however, are allow-by-default and thus disabled. The 54c0c3e798SAlex CrichtonWasmtime project selectively enables Clippy lints on an opt-in basis. Lints can 55c0c3e798SAlex Crichtonbe controlled for the entire workspace via `[workspace.lints.clippy]`: 56c0c3e798SAlex Crichton 57c0c3e798SAlex Crichton```toml 58c0c3e798SAlex Crichton[workspace.lints.clippy] 59c0c3e798SAlex Crichton# ... 60c0c3e798SAlex Crichtonmanual_strip = 'warn' 61c0c3e798SAlex Crichton``` 62c0c3e798SAlex Crichton 63c0c3e798SAlex Crichtonor on a per-crate or module basis by using attributes: 64c0c3e798SAlex Crichton 65c0c3e798SAlex Crichton```rust 66c0c3e798SAlex Crichton#![warn(clippy::manual_strip)] 67c0c3e798SAlex Crichton``` 68c0c3e798SAlex Crichton 69c0c3e798SAlex CrichtonIn Wasmtime we've found that the default set of Clippy lints is too noisy to 70c0c3e798SAlex Crichtonproductively use other Clippy lints, hence the allow-by-default behavior. 71c0c3e798SAlex CrichtonDespite this though there are numerous useful Clippy lints which are desired for 72c0c3e798SAlex Crichtonall crates or in some cases for a single crate or module. Wasmtime encourages 73c0c3e798SAlex Crichtoncontributors to enable Clippy lints they find useful through workspace or 74c0c3e798SAlex Crichtonper-crate configuration. 75c0c3e798SAlex Crichton 76c0c3e798SAlex CrichtonLike compiler warnings in the above section all Clippy warnings are turned into 77c0c3e798SAlex Crichtonerrors in CI. This means that `cargo clippy` should always produce no warnings 78c0c3e798SAlex Crichtonon Wasmtime's `main` branch if you're using the same compiler version that CI 79c0c3e798SAlex Crichtondoes (typically current stable Rust). This means, however, that if you enable a 80c0c3e798SAlex Crichtonnew Clippy lint for the workspace you'll be required to fix the lint for all 81c0c3e798SAlex Crichtoncrates in the workspace to land the PR in CI. 82c0c3e798SAlex Crichton 83c0c3e798SAlex CrichtonClippy can be run locally with: 84c0c3e798SAlex Crichton 8592cfda1bSVictor Adossi```console 8692cfda1bSVictor Adossicargo clippy --workspace --all-targets 87c0c3e798SAlex Crichton``` 88c0c3e798SAlex Crichton 89c0c3e798SAlex CrichtonContributors are welcome to enable new lints and send PRs for this. Feel free to 90c0c3e798SAlex Crichtonreach out if you're not sure about a lint as well. 91c0c3e798SAlex Crichton 92584f6686SAlex Crichton### Minimum Supported `rustc` Version (MSRV) 93986f9f79SAlex Crichton 94a04c4930SAlex CrichtonWasmtime and Cranelift support the latest three stable releases of Rust. This 95a04c4930SAlex Crichtonmeans that if the latest version of Rust is 1.72.0 then Wasmtime supports Rust 96a04c4930SAlex Crichton1.70.0, 1.71.0, and 1.72.0. CI will test by default with 1.72.0 and there will 97a04c4930SAlex Crichtonbe one job running the full test suite on Linux x86\_64 on 1.70.0. 98986f9f79SAlex Crichton 99a04c4930SAlex CrichtonSome of the CI jobs depend on nightly Rust, for example to run rustdoc with 100a04c4930SAlex Crichtonnightly features, however these use pinned versions in CI that are updated 101a04c4930SAlex Crichtonperiodically and the general repository does not depend on nightly features. 102986f9f79SAlex Crichton 103a04c4930SAlex CrichtonUpdating Wasmtime's MSRV is done by editing the `rust-version` field in the 104a04c4930SAlex Crichtonworkspace root's `Cargo.toml` 105986f9f79SAlex Crichton 106584f6686SAlex CrichtonNote that this policy is subject to change over time (notably it might be 107584f6686SAlex Crichtonextended to include more rustc versions). Current Wasmtime users don't require a 108584f6686SAlex Crichtonlarger MSRV window to justify the maintenance needed to have a larger window. If 109584f6686SAlex Crichtonyour use case requires a larger MSRV range though please feel free to contact 110584f6686SAlex Crichtonmaintainers to raise your use case (e.g. an issue, in a Wasmtime meeting, on 111584f6686SAlex CrichtonZulip, etc). 112584f6686SAlex Crichton 1135c8bce70SAlex Crichton### Dependencies of Wasmtime 1145c8bce70SAlex Crichton 1155c8bce70SAlex CrichtonWasmtime and Cranelift have a higher threshold than default for adding 1165c8bce70SAlex Crichtondependencies to the project. All dependencies are required to be "vetted" 1175c8bce70SAlex Crichtonthrough the [`cargo vet` tool](https://mozilla.github.io/cargo-vet/). This is 1185c8bce70SAlex Crichtonchecked on CI and will run on all modifications to `Cargo.lock`. 1195c8bce70SAlex Crichton 1205c8bce70SAlex CrichtonA "vet" for Wasmtime is not a meticulous code review of a dependency for 1215c8bce70SAlex Crichtoncorrectness but rather it is a statement that the crate does not contain 1225c8bce70SAlex Crichtonmalicious code and is safe for us to run during development and (optionally) 1235c8bce70SAlex Crichtonusers to run when they run Wasmtime themselves. Wasmtime's vet entries are used 1245c8bce70SAlex Crichtonby other organizations which means that this isn't simply for our own personal 1255c8bce70SAlex Crichtonuse. Wasmtime additionally uses vet entries from other organizations as well 1265c8bce70SAlex Crichtonwhich means we don't have to vet everything ourselves. 1275c8bce70SAlex Crichton 1285c8bce70SAlex CrichtonNew vet entries are required to be made by trusted contributors to Wasmtime. 1295c8bce70SAlex CrichtonThis is all configured in the `supply-chain` folder of Wasmtime. These files 1305c8bce70SAlex Crichtongenerally aren't hand-edited though and are instead managed through the `cargo 1315c8bce70SAlex Crichtonvet` tool itself. Note that our `supply-chain/audits.toml` additionally contains 1325c8bce70SAlex Crichtonentries which indicates that authors are trusted as opposed to vets of 1335c8bce70SAlex Crichtonindividual crates. This lowers the burden of updating version of a crate from a 1345c8bce70SAlex Crichtontrusted author. 1355c8bce70SAlex Crichton 1365c8bce70SAlex CrichtonWhen put together this means that contributions to Wasmtime and Cranelift which 1375c8bce70SAlex Crichtonupdate existing dependencies or add new dependencies will not be mergeable by 1385c8bce70SAlex Crichtondefault (CI will fail). This is expected from our project's configuration and 1395c8bce70SAlex Crichtonthis situation will be handled one of a few ways: 1405c8bce70SAlex Crichton 141a6a51b71SAlex CrichtonNote that this process is not in place to prevent new dependencies or prevent 142a6a51b71SAlex Crichtonupdates, but rather it ensures that development of Wasmtime is done with a 143a6a51b71SAlex Crichtontrusted set of code that has been reviewed by trusted parties. We welcome 144a6a51b71SAlex Crichtondependency updates and new functionality, so please don't be too alarmed when 145a6a51b71SAlex Crichtoncontributing and seeing a failure of `cargo vet` on CI! 146a6a51b71SAlex Crichton 147a6a51b71SAlex Crichton### `cargo vet` for Contributors 148a6a51b71SAlex Crichton 149a6a51b71SAlex CrichtonIf you're a contributor to Wasmtime and you've landed on this documentation, 150a6a51b71SAlex Crichtonhello and thanks for your contribution! Here's some guidelines for changing the 151a6a51b71SAlex Crichtonset of dependencies in Wasmtime: 152a6a51b71SAlex Crichton 1535c8bce70SAlex Crichton* If a new dependency is being added it might be worth trying to slim down 1545c8bce70SAlex Crichton what's required or avoiding the dependency altogether. Avoiding new 1555c8bce70SAlex Crichton dependencies is best when reasonable, but it is not always reasonable to do 1565c8bce70SAlex Crichton so. This is left to the judgement of the author and reviewer. 1575c8bce70SAlex Crichton 1585c8bce70SAlex Crichton* When updating dependencies this should be done for a specific purpose relevant 1595c8bce70SAlex Crichton to the PR-at-hand. For example if the PR implements a new feature then the 1605c8bce70SAlex Crichton dependency update should be required for the new feature. Otherwise it's best 1615c8bce70SAlex Crichton to leave dependency updates to their own PRs. It's ok to update dependencies 1625c8bce70SAlex Crichton "just for the update" but we prefer to have that as separate PRs. 1635c8bce70SAlex Crichton 164a6a51b71SAlex CrichtonDependency additions or updates require action on behalf of project maintainers 165a6a51b71SAlex Crichtonso we ask that you don't run `cargo vet` yourself or update the `supply-chain` 166f900a884SAlex Crichtonfolder yourself. Instead a maintainer will review your PR and perform the `cargo 167f900a884SAlex Crichtonvet` entries themselves. Reviewers will typically make a separate pull request 168f900a884SAlex Crichtonto add `cargo vet` entries and once that lands yours will be added to the queue. 1695c8bce70SAlex Crichton 170a6a51b71SAlex Crichton### `cargo vet` for Maintainers 171a6a51b71SAlex Crichton 172a6a51b71SAlex CrichtonMaintainers of Wasmtime are required to explicitly vet and approve all 173a6a51b71SAlex Crichtondependency updates and modifications to Wasmtime. This means that when reviewing 174a6a51b71SAlex Crichtona PR you should ensure that contributors are not modifying the `supply-chain` 175a6a51b71SAlex Crichtondirectory themselves outside of commits authored by other maintainers. Otherwise 176a6a51b71SAlex Crichtonthough to add vet entries this is done through one of a few methods: 177a6a51b71SAlex Crichton 178a6a51b71SAlex Crichton* For a PR where maintainers themselves are modifying dependencies the `cargo 179a6a51b71SAlex Crichton vet` entries can be included inline with the PR itself by the author. The 180a6a51b71SAlex Crichton reviewer knows that the author of the PR is themself a maintainer. 181a6a51b71SAlex Crichton 182a6a51b71SAlex Crichton* PRs that "just update dependencies" are ok to have at any time. You can do 183a6a51b71SAlex Crichton this in preparation for a future feature or for a future contributor. This 184a6a51b71SAlex Crichton more-or-less is the same as the previous categories. 185a6a51b71SAlex Crichton 186a6a51b71SAlex Crichton* For contributors who should not add vet entries themselves maintainers should 187f900a884SAlex Crichton review the PR and add vet entries either in a separate PR or as part of the 188f900a884SAlex Crichton contributor's PR itself. As a separate PR you'll check out the branch, run 189f900a884SAlex Crichton `cargo vet`, then rebase away the contributor's commits and push your `cargo 190f900a884SAlex Crichton vet` commit alone to merge. For pushing directly to the contributor's own PR 191f900a884SAlex Crichton be sure to read the notes below. 192a6a51b71SAlex Crichton 193f900a884SAlex CrichtonNote for the last case it's important to ensure that if you push directly to a 194f900a884SAlex Crichtoncontributor's PR any future updates pushed by the contributor either contain or 195f900a884SAlex Crichtondon't overwrite your vet entries. Also verify that if the PR branch is rebased 196f900a884SAlex Crichtonor force-pushed, the details of your previously pushed vetting remain the same: 197f900a884SAlex Crichtone.g., versions were not bumped and descriptive reasons remain the same. If 198f900a884SAlex Crichtonpushing a vetting commit to a contributor's PR and also asking for more changes, 199f900a884SAlex Crichtonrequest that the contributor make the requested fixes in an additional commit 200f900a884SAlex Crichtonrather than force-pushing a rewritten history, so your existing vetting commit 201f900a884SAlex Crichtonremains untouched. These guidelines make it easier to verify no tampering has 202f900a884SAlex Crichtonoccurred. 203a6a51b71SAlex Crichton 204a6a51b71SAlex Crichton### Policy for adding `cargo vet` entries 205a6a51b71SAlex Crichton 206a6a51b71SAlex CrichtonFor maintainers this is intended to document the project's policy on adding 207a6a51b71SAlex Crichton`cargo vet` entries. The goal of this policy is to not make dependency updates 208a6a51b71SAlex Crichtonso onerous that they never happen while still achieving much of the intended 209a6a51b71SAlex Crichtonbenefit of `cargo vet` in protection against supply-chain style attacks. 210a6a51b71SAlex Crichton 211a6a51b71SAlex Crichton* For dependencies **that receive at least 10,000 downloads a day** on crates.io 212a6a51b71SAlex Crichton it's ok to add an entry to `exemptions` in `supply-chain/config.toml`. This 213a6a51b71SAlex Crichton does not require careful review or review at all of these dependencies. The 214a6a51b71SAlex Crichton assumption here is that a supply chain attack against a popular crate is 215a6a51b71SAlex Crichton statistically likely to be discovered relatively quickly. Changes to `main` in 216a6a51b71SAlex Crichton Wasmtime take at least 2 weeks to be released due to our release process, so 217a6a51b71SAlex Crichton the assumption is that popular crates that are victim of a supply chain attack 218a6a51b71SAlex Crichton would be discovered during this time. This policy additionally greatly helps 219a6a51b71SAlex Crichton when updating dependencies on popular crates that are common to see without 220a6a51b71SAlex Crichton increasing the burden too much on maintainers. 221a6a51b71SAlex Crichton 222a6a51b71SAlex Crichton* For other dependencies a manual vet is required. The `cargo vet` tool will 223a6a51b71SAlex Crichton assist in adding a vet by pointing you towards the source code, as published 224a6a51b71SAlex Crichton on crates.io, to be browsed online. Manual review should be done to ensure 225a6a51b71SAlex Crichton that "nothing nefarious" is happening. For example `unsafe` should be 226a6a51b71SAlex Crichton inspected as well as use of ambient system capabilities such as `std::fs`, 227a6a51b71SAlex Crichton `std::net`, or `std::process`, and build scripts. Note that you're not 228a6a51b71SAlex Crichton reviewing for correctness, instead only for whether a supply-chain attack 229a6a51b71SAlex Crichton appears to be present. 230a6a51b71SAlex Crichton 231a6a51b71SAlex CrichtonThis policy intends to strike a rough balance between usability and security. 232a6a51b71SAlex CrichtonIt's always recommended to add vet entries where possible, but the first bullet 233a6a51b71SAlex Crichtonabove can be used to update an `exemptions` entry or add a new entry. Note that 234a6a51b71SAlex Crichtonwhen the "popular threshold" is used **do not add a vet entry** because the 235a6a51b71SAlex Crichtoncrate is, in fact, not vetted. This is required to go through an 236a6a51b71SAlex Crichton`[[exemptions]]` entry. 237*4c8edb95SAlex Crichton 238*4c8edb95SAlex Crichton### Crate Organization 239*4c8edb95SAlex Crichton 240*4c8edb95SAlex CrichtonThe Wasmtime repository is a bit of a monorepo with lots of crates internally 241*4c8edb95SAlex Crichtonwithin it. The Wasmtime project and `wasmtime` crate also consists of a variety 242*4c8edb95SAlex Crichtonof crates intended for various purposes. As such not all crates are treated 243*4c8edb95SAlex Crichtonexactly the same and so there are some rough guidelines here about adding new 244*4c8edb95SAlex Crichtoncrates to the repository and where to place/name them: 245*4c8edb95SAlex Crichton 246*4c8edb95SAlex Crichton* Wasmtime-related crates live in `crates/foo/Cargo.toml` where the crate name 247*4c8edb95SAlex Crichton is typically `wasmtime-foo` or `wasmtime-internal-foo`. 248*4c8edb95SAlex Crichton 249*4c8edb95SAlex Crichton* Cranelift-related crates live in `cranelift/foo/Cargo.toml` where the crate is 250*4c8edb95SAlex Crichton named `cranelift-foo`. 251*4c8edb95SAlex Crichton 252*4c8edb95SAlex Crichton* Some projects such as Winch, Pulley, and Wiggle are exceptions to the above 253*4c8edb95SAlex Crichton rules and live in `winch/*`, `pulley/*` and `crates/wiggle/*`. 254*4c8edb95SAlex Crichton 255*4c8edb95SAlex Crichton* Some crates are "internal" to Wasmtime. This means that they only exist for 256*4c8edb95SAlex Crichton crate organization purposes (such as optional dependencies, or code 257*4c8edb95SAlex Crichton organization). These crates are not intended for public consumption and are 258*4c8edb95SAlex Crichton intended for exclusively being used by the `wasmtime` crate, for example, or 259*4c8edb95SAlex Crichton other public crates. These crates should be named `wasmtime-internal-foo` and 260*4c8edb95SAlex Crichton live in `crates/foo`. The `[workspace.dependencies]` directive in `Cargo.toml` 261*4c8edb95SAlex Crichton at the root of the repository should rename it to `wasmtime-foo` in 262*4c8edb95SAlex Crichton workspace-local usage, meaning that the "internal" part is only relevant on 263*4c8edb95SAlex Crichton crates.io. 264