blob: f917fced9c94c6044814549dc9bd25466c625f86 [file] [log] [blame] [view]
danakj6e25f742022-12-01 21:47:421# Rust in Chromium
2
3[TOC]
4
5# Why?
6
danakjbb4d0c772023-10-13 13:22:287Handling untrustworthy data in non-trivial ways is a major source of security
8bugs, and it's therefore against Chromium's security policies
9[to do it in the Browser or Gpu process](../docs/security/rule-of-2.md) unless
10you are working in a memory-safe language.
danakj6e25f742022-12-01 21:47:4211
12Rust provides a cross-platform memory-safe language so that all platforms can
13handle untrustworthy data directly from a privileged process, without the
14performance overheads and complexity of a utility process.
15
danakj6e25f742022-12-01 21:47:4216# Status
17
danakjbb4d0c772023-10-13 13:22:2818The Rust toolchain is enabled for and supports all platforms and development
19environments that are supported by the Chromium project. The first milestone
20to include full production-ready support was M119.
danakj6e25f742022-12-01 21:47:4221
danakjbb4d0c772023-10-13 13:22:2822Rust is approved by Chrome ATLs for production use in
23[certain third-party scenarios](../docs/adding_to_third_party.md#Rust).
danakj6e25f742022-12-01 21:47:4224
25For questions or help, reach out to `[email protected]` or `#rust` on the
26[Chromium Slack](https://www.chromium.org/developers/slack/).
27
danakj6e25f742022-12-01 21:47:4228If you use VSCode, we have [additional advice below](#using-vscode).
29
danakjbb4d0c772023-10-13 13:22:2830# Adding a third-party Rust library
danakj6e25f742022-12-01 21:47:4231
danakjbb4d0c772023-10-13 13:22:2832Third-party libraries are pulled from [crates.io](https://crates.io), but
33Chromium does not use Cargo as a build system.
danakj6e25f742022-12-01 21:47:4234
35## Third-party review
36
danakj6e25f742022-12-01 21:47:4237All third-party crates need to go through third-party review. See
38[//docs/adding_to_third_party.md](adding_to_third_party.md) for instructions on
39how to have a library reviewed.
40
danakjbb4d0c772023-10-13 13:22:2841## Importing a crate from crates.io
42
danakj0ec93d12023-11-17 16:12:2343The `//third_party/rust/chromium_crates_io/Cargo.toml` file defines the set of crates
danakjbb4d0c772023-10-13 13:22:2844depended on from first-party code. Any transitive dependencies will be found
danakj0ec93d12023-11-17 16:12:2345from those listed there. The file is a [standard `Cargo.toml` file](
46https://doc.rust-lang.org/cargo/reference/manifest.html), though the crate
47itself is never built, it is only used to collect dependencies through the
48`[dependencies]` section.
danakjbb4d0c772023-10-13 13:22:2849
danakj0ec93d12023-11-17 16:12:2350To use a third-party crate "bar" version 3 from first party code:
danakj98bec162023-11-21 14:55:02511. Change directory to the root `src/` dir of Chromium.
521. Add the crate to `//third_party/rust/chromium_crates_io/Cargo.toml`:
53 * `vpython3 ./tools/crates/run_gnrt.py add foo` to add the latest version of `foo`.
54 * `vpython3 ./tools/crates/run_gnrt.py add [email protected]` to add a specific version of `foo`.
55 * Or, directly through (nightly) cargo:
56 `cargo run --release --manifest-path tools/crates/gnrt/Cargo.toml --target-dir out/gnrt add foo`
57 * Or, edit the Cargo.toml by hand, finding the version you want from [crates.io](https://crates.io).
581. Download the crate's files:
59 * `./tools/crates/run_gnrt.py vendor` to download the new crate.
60 * Or, directly through (nightly) cargo:
danakj0ec93d12023-11-17 16:12:2361 `cargo run --release --manifest-path tools/crates/gnrt/Cargo.toml --target-dir out/gnrt vendor`
danakjf509caa2023-11-23 15:29:4362 * This will also apply any patches in `//third_party/rust/chromium_crates_io/patches`
63 for the crates. If a patch can not apply, the crate's download will be cancelled and
64 an error will be printed. See [patching errors](#patching-errors) below for how to resolve
65 this.
danakj98bec162023-11-21 14:55:02661. (optional) If the crate is only to be used by tests and tooling, then
67 specify the `"test"` group in `//third_party/rust/chromium_crates_io/gnrt_config.toml`:
68 ```
69 [crate.foo]
70 group = "test"
71 ```
721. Generate the `BUILD.gn` file for the new crate:
73 * `vpython3 ./tools/crates/run_gnrt.py gen`
74 * Or, directly through (nightly) cargo:
75 `cargo run --release --manifest-path tools/crates/gnrt/Cargo.toml --target-dir out/gnrt gen`
Lukasz Anforowicz652bf4a2024-03-20 16:48:29761. Verify if all new dependencies are already audited by running `cargo vet`
Lukasz Anforowiczdf3d5af2024-03-20 18:21:1677 See [`rust-unsafe.md#cargo-vet-policy`](rust-unsafe.md#cargo-vet-policy) for
Lukasz Anforowicz652bf4a2024-03-20 16:48:2978 more details. This boils down to:
Lukasz Anforowicz11c34872024-01-25 17:15:4679 * `./tools/crates/run_cargo_vet.py check`
danakjaed1daf2024-01-05 17:44:3280 * If `check` fails, then there are missing audits, which need to be added to
81 `//third_party/rust/chromium_crates_io/supply-chain/audits.toml`.
Lukasz Anforowicz057876d2024-06-05 19:07:58821. Add the new files to git:
83 * `git add -f third_party/rust/chromium_crates_io/vendor`.
84 (The `-f` is important, as files may be skipped otherwise from a
85 `.gitignore` inside the crate.)
86 * `git add third_party/rust`
danakjaed1daf2024-01-05 17:44:32871. Upload the CL. If there is any `unsafe` usage then Security experts will need to
Lukasz Anforowicz652bf4a2024-03-20 16:48:2988 audit the "ub-risk" level. See
Lukasz Anforowiczdf3d5af2024-03-20 18:21:1689 [`rust-unsafe.md#code-review-policy`](rust-unsafe.md#code-review-policy) for
Lukasz Anforowicz652bf4a2024-03-20 16:48:2990 more details.
danakj0ec93d12023-11-17 16:12:2391
92### Cargo features
danakjbb4d0c772023-10-13 13:22:2893
94To enable a feature "spaceships" in the crate, change the entry in
danakj0ec93d12023-11-17 16:12:2395`//third_party/rust/chromium_crates_io/Cargo.toml` to include the feature:
danakjbb4d0c772023-10-13 13:22:2896```toml
97[dependencies]
98bar = { version = "3", features = [ "spaceships" ] }
99```
100
danakjf509caa2023-11-23 15:29:43101### Patching third-party crates
danakjbb4d0c772023-10-13 13:22:28102
103You may patch a crate in tree, but save any changes made into a diff file in
danakj98bec162023-11-21 14:55:02104a `//third_party/rust/chromium_crates_io/patches/` directory for the crate.
105The diff file should be generated by `git-format-patch` each new patch numbered
106consecutively so that they can be applied in order. For example, these files
107might exist if the "foo" crate was patched with a couple of changes:
danakjbb4d0c772023-10-13 13:22:28108
109```
danakj98bec162023-11-21 14:55:02110//third_party/rust/chromium_crates_io/patches/foo/patches/0001-Edit-the-Cargo-toml.diff
111//third_party/rust/chromium_crates_io/patches/foo/patches/0002-Other-changes.diff
danakjbb4d0c772023-10-13 13:22:28112```
113
Adrian Taylor138cb9f2023-11-08 18:41:54114The recommended procedure to create such patches is:
115
1161. Commit the plain new version of the crate to your local git branch
1172. Modify the crate as necessary
1183. Commit that modified version
1194. Use `git format-patch <unpatched version>` to generate the patch files
1205. Add the patch files in a new, third, commit
1216. Squash them, or rely on `git cl upload` doing so
122
danakjf509caa2023-11-23 15:29:43123#### Patching errors
124
125If `gnrt vendor` fails to apply a patch for a crate, it will cancel the download of that
126crate rather than leave it in a broken state. To recreate patches, first get a pristine
127copy of the crate by using the `--no-patches` argument:
128
1291. Download the crate without applying patches:
130 * `vpython3 ./tools/crates/run_gnrt.py vendor --no-patches=<CRATE_NAME>`
1312. Then recreate the patches as described in [Patching third-party crates](
132 #patching-third_party-crates).
133
134To verify the patches work, remove the vendored crate directory in
135`//third_party/rust/chromium_crates_io/vendor/`, named after the crate name
136and version. Then run the `vendor` action without `--no-patches` which will
137download the crate and apply the patches:
138 * `vpython3 ./tools/crates/run_gnrt.py vendor`
139
danakj98bec162023-11-21 14:55:02140## Security
danakjbb4d0c772023-10-13 13:22:28141
danakj98bec162023-11-21 14:55:02142If a shipping library needs security review (has any `unsafe`), and the review
143finds it's not satisfying the [rule of 2](../docs/security/rule-of-2.md), then
144move it to the `"sandbox"` group in `//third_party/rust/chromium_crates_io/gnrt_config.toml`
145to make it clear it can't be used in a privileged process:
146```
147[crate.foo]
148group = "sandbox"
149```
150
151If a transitive dependency moves from `"safe"` to `"sandbox"` and causes
152a dependency chain across the groups, it will break the `gnrt vendor` step.
153You will need to fix the new crate so that it's deemed safe in unsafe review,
154or move the other dependent crates out of `"safe"` as well by setting their
155group in `gnrt_config.toml`.
156
157# Updating existing third-party crates
158
Lukasz Anforowicz85528a62024-03-20 19:12:53159Third-party crates will get updated semi-automatically through the process
160described in
161[`../tools/crates/create_update_cl.md`](../tools/crates/create_update_cl.md).
162If you nevertheless need to manually update a crate to its latest minor
163version, then follow the steps below:
164
danakj98bec162023-11-21 14:55:021651. Change directory to the root `src/` dir of Chromium.
Dominik Röttschesa07a5532024-01-24 19:16:231661. Update the versions in `//third_party/rust/chromium_crates_io/Cargo.toml`.
Lukasz Anforowicz85528a62024-03-20 19:12:53167 * `vpython3 ./tools/crates/run_gnrt.py update <crate name>`
danakj98bec162023-11-21 14:55:02168 * Or, directly through (nightly) cargo:
Lukasz Anforowicz85528a62024-03-20 19:12:53169 `cargo run --release --manifest-path tools/crates/gnrt/Cargo.toml --target-dir out/gnrt update <crate name>`
danakj98bec162023-11-21 14:55:021701. Download any updated crate's files:
171 * `./tools/crates/run_gnrt.py vendor`
Dominik Röttschesa07a5532024-01-24 19:16:23172 * If you want to restrict the update to certain crates, add the crate names
173 as arguments to `vendor`, like: `./tools/crates/run_gnrt.py vendor
174 <crate-name>`
danakj98bec162023-11-21 14:55:02175 * Or, directly through (nightly) cargo:
176 `cargo run --release --manifest-path tools/crates/gnrt/Cargo.toml --target-dir out/gnrt vendor`
Lukasz Anforowicz8452bd8d2023-11-28 23:31:551771. Add the downloaded files to git:
danakj98bec162023-11-21 14:55:02178 * `git add -f third_party/rust/chromium_crates_io/vendor`
179 * The `-f` is important, as files may be skipped otherwise from a
180 `.gitignore` inside the crate.
danakj98bec162023-11-21 14:55:021811. Generate the `BUILD.gn` files
182 * `vpython3 ./tools/crates/run_gnrt.py gen`
183 * Or, directly through (nightly) cargo:
184 `cargo run --release --manifest-path tools/crates/gnrt/Cargo.toml --target-dir out/gnrt gen`
Lukasz Anforowicz8452bd8d2023-11-28 23:31:551851. Add the generated files to git:
186 * `git add -f third_party/rust`
danakjbb4d0c772023-10-13 13:22:28187
188### Directory structure for third-party crates
189
190The directory structure for a crate "foo" version 3.4.2 is:
191```
192//third_party/
193 rust/
danakj98bec162023-11-21 14:55:02194 foo/ (for the "foo" crate)
Lukasz Anforowicz8452bd8d2023-11-28 23:31:55195 v3/ (version 3.4.2 maps to the v3 epoch)
danakjbb4d0c772023-10-13 13:22:28196 BUILD.gn (generated by gnrt gen)
danakj98bec162023-11-21 14:55:02197 README.chromium (generated by gnrt vendor)
Lukasz Anforowicz8452bd8d2023-11-28 23:31:55198 chromium_crates_io/
199 vendor/
200 foo-3.4.2 (crate sources downloaded from crates.io)
danakj98bec162023-11-21 14:55:02201 patches/
Lukasz Anforowicz8452bd8d2023-11-28 23:31:55202 foo/ (patches for the "foo" crate)
danakjbb4d0c772023-10-13 13:22:28203 0001-Edit-the-Cargo-toml.diff
204 0002-Other-changes.diff
Lukasz Anforowicz8452bd8d2023-11-28 23:31:55205 Cargo.toml
206 Cargo.lock
207 gnrt_config.toml
danakjbb4d0c772023-10-13 13:22:28208```
209
danakj6e25f742022-12-01 21:47:42210## Writing a wrapper for binding generation
211
212Most Rust libraries will need a more C++-friendly API written on top of them in
danakjbb4d0c772023-10-13 13:22:28213order to generate C++ bindings to them. The wrapper library can be placed
214in `//third_party/rust/<cratename>/<epoch>/wrapper` or at another single place
215that all C++ goes through to access the library. The [CXX](https://cxx.rs) is
216used to generate bindings between C++ and Rust.
danakj6e25f742022-12-01 21:47:42217
218See
danakjbb4d0c772023-10-13 13:22:28219[`//third_party/rust/serde_json_lenient/v0_1/wrapper/`](
220https://source.chromium.org/chromium/chromium/src/+/main:third_party/rust/serde_json_lenient/v0_1/wrapper/)
221and
222[`//components/qr_code_generator`](
223https://source.chromium.org/chromium/chromium/src/+/main:components/qr_code_generator/;l=1;drc=b185db5d502d4995627e09d62c6934590031a5f2)
224for examples.
danakj6e25f742022-12-01 21:47:42225
danakjbb4d0c772023-10-13 13:22:28226Rust libraries should use the
227[`rust_static_library`](
228https://source.chromium.org/chromium/chromium/src/+/main:build/rust/rust_static_library.gni)
229GN template (not the built-in `rust_library`) to integrate properly into the
230mixed-language Chromium build and get the correct compiler options applied to
231them.
danakj6e25f742022-12-01 21:47:42232
danakjbb4d0c772023-10-13 13:22:28233The [CXX](https://cxx.rs) tool is used for generating C++ bindings to Rust
234code. Since it requires explicit declarations in Rust, an wrapper shim around a
235pure Rust library is needed. Add these Rust shims that contain the CXX
236`bridge` macro to the `cxx_bindings` GN variable in the `rust_static_library`
237to have CXX generate a C++ header for that file. To include the C++ header
238file, rooted in the `gen` output directory, use
danakj6e25f742022-12-01 21:47:42239```
danakjbb4d0c772023-10-13 13:22:28240#include "the/path/to/the/rust/file.rs.h"
danakj6e25f742022-12-01 21:47:42241```
242
danakj3d037ff2024-11-07 19:31:41243# Logging
Adrian Taylor91eaa362024-02-09 14:17:03244
danakj3d037ff2024-11-07 19:31:41245Use the [log](https://docs.rs/log) crate's macros in place of base `LOG`
246macros from C++. They do the same things. The `debug!` macro maps to
247`DLOG(INFO)`, the `info!` macro maps to `LOG(INFO)`, and `warn!` and `error!`
248map to `LOG(WARNING)` and `LOG(ERROR)` respectively. The additional `trace!`
249macro maps to `DLOG(INFO)` (but there is [WIP to map it to `DVLOG(INFO)`](
250https://chromium-review.googlesource.com/c/chromium/src/+/5996820)).
251
252Note that the standard library also includes a helpful
253[`dbg!`](https://doc.rust-lang.org/std/macro.dbg.html) macro which writes
254everything about a variable to `stderr`.
255
256Logging may not yet work in component builds:
257[crbug.com/374023535](https://crbug.com/374023535).
258
259# Tracing
260
261TODO: [crbug.com/377915495](https://crbug.com/377915495).
262
263# Strings
264
265Prefer to use [`BString`](https://docs.rs/bstr/latest/bstr/struct.BString.html)
266and [`BStr`](https://docs.rs/bstr/latest/bstr/struct.BStr.html) to work with
267strings in first-party code instead of `std::String` and `str`. These types do
268not require the strings to be valid UTF-8, and avoid error handling or panic
269crashes when working with strings from C++ and/or from the web. Because the
270web is not UTF-8 encoded, many strings in Chromium are also not.
271
272In cross-language bindings, `&[u8]` can be used to represent a string until
273native support for `BStr` is available in our interop tooling. A `u8` slice
274can be converted to `BStr` or treated as a string with
275[`ByteSlice`](https://docs.rs/bstr/latest/bstr/trait.ByteSlice.html).
Adrian Taylor91eaa362024-02-09 14:17:03276
danakj6e25f742022-12-01 21:47:42277# Using VSCode
278
2791. Ensure you're using the `rust-analyzer` extension for VSCode, rather than
280 earlier forms of Rust support.
danakjbb4d0c772023-10-13 13:22:282812. Run `gn` with the `--export-rust-project` flag, such as:
282 `gn gen out/Release --export-rust-project`.
danakj6e25f742022-12-01 21:47:422833. `ln -s out/Release/rust-project.json rust-project.json`
2844. When you run VSCode, or any other IDE that uses
285 [rust-analyzer](https://rust-analyzer.github.io/) it should detect the
286 `rust-project.json` and use this to give you rich browsing, autocompletion,
287 type annotations etc. for all the Rust within the Chromium codebase.
danakjf3d7f372023-12-07 18:17:122885. Point rust-analyzer to the rust toolchain in Chromium. Otherwise you will
289 need to install Rustc in your system, and Chromium uses the nightly
290 compiler, so you would need that to match. Add the following to
291 `.vscode/settings.json` in the Chromium checkout:
292 ```
293 {
294 // The rest of the settings...
295
296 "rust-analyzer.cargo.extraEnv": {
297 "PATH": "../../third_party/rust-toolchain/bin:$PATH",
298 }
299 }
300 ```
301 This assumes you are working with an output directory like `out/Debug` which
302 has two levels; adjust the number of `..` in the path according to your own
303 setup.
Adrian Taylorc5fbb572023-11-21 14:25:42304
305# Using cargo
306
307If you are building a throwaway or experimental tool, you might like to use pure
308`cargo` tooling rather than `gn` and `ninja`. Even then, you may choose
309to restrict yourself to the toolchain and crates that are already approved for
310use in Chromium.
311
312Here's how.
313
314```
315export PATH_TO_CHROMIUM_SRC=~/chromium/src
316mkdir my-rust-tool
317cd my-rust-tool
318mkdir .cargo
319cat <<END > .cargo/config.toml
320[source.crates-io]
321replace-with = "vendored-sources"
322
323[source.vendored-sources]
324directory = "$PATH_TO_CHROMIUM_SRC/third_party/rust/chromium_crates_io/vendor"
325END
326$PATH_TO_CHROMIUM_SRC/third_party/rust-toolchain/bin/cargo init --offline
327$PATH_TO_CHROMIUM_SRC/third_party/rust-toolchain/bin/cargo run --offline
328```
329
330Most `cargo` tooling works well with this setup; one exception is `cargo add`,
331but you can still add dependencies manually to your `Cargo.toml`:
332
333```
334[dependencies]
335log = "0.4"
336```