blob: fd4d42364af6b4c2b2d431d20afe05c651718045 [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
David Adriand8918692024-12-12 22:02:5022Rust can be used anywhere in the Chromium repository (not just `//third_party`)
23subject to [current interop capabilities][interop-rust-doc], however it is
24currently subject to a internal approval and FYI process. Googlers can view
25go/chrome-rust for details. New usages of Rust are documented at
26[`[email protected]`](https://groups.google.com/a/chromium.org/g/rust-fyi).
danakj6e25f742022-12-01 21:47:4227
28For questions or help, reach out to `[email protected]` or `#rust` on the
29[Chromium Slack](https://www.chromium.org/developers/slack/).
30
danakj6e25f742022-12-01 21:47:4231If you use VSCode, we have [additional advice below](#using-vscode).
32
danakjbb4d0c772023-10-13 13:22:2833# Adding a third-party Rust library
danakj6e25f742022-12-01 21:47:4234
danakjbb4d0c772023-10-13 13:22:2835Third-party libraries are pulled from [crates.io](https://crates.io), but
36Chromium does not use Cargo as a build system.
danakj6e25f742022-12-01 21:47:4237
38## Third-party review
39
danakj6e25f742022-12-01 21:47:4240All third-party crates need to go through third-party review. See
41[//docs/adding_to_third_party.md](adding_to_third_party.md) for instructions on
42how to have a library reviewed.
43
danakjbb4d0c772023-10-13 13:22:2844## Importing a crate from crates.io
45
danakj0ec93d12023-11-17 16:12:2346The `//third_party/rust/chromium_crates_io/Cargo.toml` file defines the set of crates
danakjbb4d0c772023-10-13 13:22:2847depended on from first-party code. Any transitive dependencies will be found
danakj0ec93d12023-11-17 16:12:2348from those listed there. The file is a [standard `Cargo.toml` file](
49https://doc.rust-lang.org/cargo/reference/manifest.html), though the crate
50itself is never built, it is only used to collect dependencies through the
51`[dependencies]` section.
danakjbb4d0c772023-10-13 13:22:2852
danakj0ec93d12023-11-17 16:12:2353To use a third-party crate "bar" version 3 from first party code:
danakj98bec162023-11-21 14:55:02541. Change directory to the root `src/` dir of Chromium.
551. Add the crate to `//third_party/rust/chromium_crates_io/Cargo.toml`:
56 * `vpython3 ./tools/crates/run_gnrt.py add foo` to add the latest version of `foo`.
57 * `vpython3 ./tools/crates/run_gnrt.py add [email protected]` to add a specific version of `foo`.
58 * Or, directly through (nightly) cargo:
59 `cargo run --release --manifest-path tools/crates/gnrt/Cargo.toml --target-dir out/gnrt add foo`
60 * Or, edit the Cargo.toml by hand, finding the version you want from [crates.io](https://crates.io).
611. Download the crate's files:
62 * `./tools/crates/run_gnrt.py vendor` to download the new crate.
63 * Or, directly through (nightly) cargo:
danakj0ec93d12023-11-17 16:12:2364 `cargo run --release --manifest-path tools/crates/gnrt/Cargo.toml --target-dir out/gnrt vendor`
danakjf509caa2023-11-23 15:29:4365 * This will also apply any patches in `//third_party/rust/chromium_crates_io/patches`
66 for the crates. If a patch can not apply, the crate's download will be cancelled and
67 an error will be printed. See [patching errors](#patching-errors) below for how to resolve
68 this.
danakj98bec162023-11-21 14:55:02691. (optional) If the crate is only to be used by tests and tooling, then
70 specify the `"test"` group in `//third_party/rust/chromium_crates_io/gnrt_config.toml`:
71 ```
72 [crate.foo]
73 group = "test"
74 ```
751. Generate the `BUILD.gn` file for the new crate:
76 * `vpython3 ./tools/crates/run_gnrt.py gen`
77 * Or, directly through (nightly) cargo:
78 `cargo run --release --manifest-path tools/crates/gnrt/Cargo.toml --target-dir out/gnrt gen`
Lukasz Anforowicz057876d2024-06-05 19:07:58791. Add the new files to git:
80 * `git add -f third_party/rust/chromium_crates_io/vendor`.
81 (The `-f` is important, as files may be skipped otherwise from a
82 `.gitignore` inside the crate.)
83 * `git add third_party/rust`
Lukasz Anforowiczf6888b7b2025-03-25 17:41:13841. Upload the CL and get a review from `//third_party/rust/OWNERS`.
danakj0ec93d12023-11-17 16:12:2385
86### Cargo features
danakjbb4d0c772023-10-13 13:22:2887
88To enable a feature "spaceships" in the crate, change the entry in
danakj0ec93d12023-11-17 16:12:2389`//third_party/rust/chromium_crates_io/Cargo.toml` to include the feature:
danakjbb4d0c772023-10-13 13:22:2890```toml
91[dependencies]
92bar = { version = "3", features = [ "spaceships" ] }
93```
94
danakjf509caa2023-11-23 15:29:4395### Patching third-party crates
danakjbb4d0c772023-10-13 13:22:2896
97You may patch a crate in tree, but save any changes made into a diff file in
danakj98bec162023-11-21 14:55:0298a `//third_party/rust/chromium_crates_io/patches/` directory for the crate.
99The diff file should be generated by `git-format-patch` each new patch numbered
100consecutively so that they can be applied in order. For example, these files
101might exist if the "foo" crate was patched with a couple of changes:
danakjbb4d0c772023-10-13 13:22:28102
103```
danakj98bec162023-11-21 14:55:02104//third_party/rust/chromium_crates_io/patches/foo/patches/0001-Edit-the-Cargo-toml.diff
105//third_party/rust/chromium_crates_io/patches/foo/patches/0002-Other-changes.diff
danakjbb4d0c772023-10-13 13:22:28106```
107
Dustin J. Mitchell3d17cd42025-02-11 20:15:00108Patches are applied with `-p6 --directory
109third_party/rust/chromium_crates_io/vendor/<crate>-<version>`, effectively
110ignoring the version numbers in the patch files.
111
Adrian Taylor138cb9f2023-11-08 18:41:54112The recommended procedure to create such patches is:
113
1141. Commit the plain new version of the crate to your local git branch
1152. Modify the crate as necessary
1163. Commit that modified version
1174. Use `git format-patch <unpatched version>` to generate the patch files
1185. Add the patch files in a new, third, commit
1196. Squash them, or rely on `git cl upload` doing so
120
danakjf509caa2023-11-23 15:29:43121#### Patching errors
122
123If `gnrt vendor` fails to apply a patch for a crate, it will cancel the download of that
124crate rather than leave it in a broken state. To recreate patches, first get a pristine
125copy of the crate by using the `--no-patches` argument:
126
1271. Download the crate without applying patches:
128 * `vpython3 ./tools/crates/run_gnrt.py vendor --no-patches=<CRATE_NAME>`
1292. Then recreate the patches as described in [Patching third-party crates](
130 #patching-third_party-crates).
131
132To verify the patches work, remove the vendored crate directory in
133`//third_party/rust/chromium_crates_io/vendor/`, named after the crate name
134and version. Then run the `vendor` action without `--no-patches` which will
135download the crate and apply the patches:
136 * `vpython3 ./tools/crates/run_gnrt.py vendor`
137
danakj98bec162023-11-21 14:55:02138## Security
danakjbb4d0c772023-10-13 13:22:28139
danakj98bec162023-11-21 14:55:02140If a shipping library needs security review (has any `unsafe`), and the review
141finds it's not satisfying the [rule of 2](../docs/security/rule-of-2.md), then
142move it to the `"sandbox"` group in `//third_party/rust/chromium_crates_io/gnrt_config.toml`
143to make it clear it can't be used in a privileged process:
144```
145[crate.foo]
146group = "sandbox"
147```
148
149If a transitive dependency moves from `"safe"` to `"sandbox"` and causes
150a dependency chain across the groups, it will break the `gnrt vendor` step.
151You will need to fix the new crate so that it's deemed safe in unsafe review,
152or move the other dependent crates out of `"safe"` as well by setting their
153group in `gnrt_config.toml`.
154
155# Updating existing third-party crates
156
Lukasz Anforowicz85528a62024-03-20 19:12:53157Third-party crates will get updated semi-automatically through the process
158described in
159[`../tools/crates/create_update_cl.md`](../tools/crates/create_update_cl.md).
160If you nevertheless need to manually update a crate to its latest minor
161version, then follow the steps below:
162
danakj98bec162023-11-21 14:55:021631. Change directory to the root `src/` dir of Chromium.
Dominik Röttschesa07a5532024-01-24 19:16:231641. Update the versions in `//third_party/rust/chromium_crates_io/Cargo.toml`.
Lukasz Anforowicz85528a62024-03-20 19:12:53165 * `vpython3 ./tools/crates/run_gnrt.py update <crate name>`
danakj98bec162023-11-21 14:55:02166 * Or, directly through (nightly) cargo:
Lukasz Anforowicz85528a62024-03-20 19:12:53167 `cargo run --release --manifest-path tools/crates/gnrt/Cargo.toml --target-dir out/gnrt update <crate name>`
danakj98bec162023-11-21 14:55:021681. Download any updated crate's files:
169 * `./tools/crates/run_gnrt.py vendor`
Dominik Röttschesa07a5532024-01-24 19:16:23170 * If you want to restrict the update to certain crates, add the crate names
171 as arguments to `vendor`, like: `./tools/crates/run_gnrt.py vendor
172 <crate-name>`
danakj98bec162023-11-21 14:55:02173 * Or, directly through (nightly) cargo:
174 `cargo run --release --manifest-path tools/crates/gnrt/Cargo.toml --target-dir out/gnrt vendor`
Lukasz Anforowicz8452bd8d2023-11-28 23:31:551751. Add the downloaded files to git:
danakj98bec162023-11-21 14:55:02176 * `git add -f third_party/rust/chromium_crates_io/vendor`
177 * The `-f` is important, as files may be skipped otherwise from a
178 `.gitignore` inside the crate.
danakj98bec162023-11-21 14:55:021791. Generate the `BUILD.gn` files
180 * `vpython3 ./tools/crates/run_gnrt.py gen`
181 * Or, directly through (nightly) cargo:
182 `cargo run --release --manifest-path tools/crates/gnrt/Cargo.toml --target-dir out/gnrt gen`
Lukasz Anforowicz8452bd8d2023-11-28 23:31:551831. Add the generated files to git:
184 * `git add -f third_party/rust`
danakjbb4d0c772023-10-13 13:22:28185
186### Directory structure for third-party crates
187
188The directory structure for a crate "foo" version 3.4.2 is:
189```
190//third_party/
191 rust/
danakj98bec162023-11-21 14:55:02192 foo/ (for the "foo" crate)
Lukasz Anforowicz8452bd8d2023-11-28 23:31:55193 v3/ (version 3.4.2 maps to the v3 epoch)
danakjbb4d0c772023-10-13 13:22:28194 BUILD.gn (generated by gnrt gen)
danakj98bec162023-11-21 14:55:02195 README.chromium (generated by gnrt vendor)
Lukasz Anforowicz8452bd8d2023-11-28 23:31:55196 chromium_crates_io/
197 vendor/
198 foo-3.4.2 (crate sources downloaded from crates.io)
danakj98bec162023-11-21 14:55:02199 patches/
Lukasz Anforowicz8452bd8d2023-11-28 23:31:55200 foo/ (patches for the "foo" crate)
danakjbb4d0c772023-10-13 13:22:28201 0001-Edit-the-Cargo-toml.diff
202 0002-Other-changes.diff
Lukasz Anforowicz8452bd8d2023-11-28 23:31:55203 Cargo.toml
204 Cargo.lock
205 gnrt_config.toml
danakjbb4d0c772023-10-13 13:22:28206```
207
danakj6e25f742022-12-01 21:47:42208## Writing a wrapper for binding generation
209
210Most Rust libraries will need a more C++-friendly API written on top of them in
danakjbb4d0c772023-10-13 13:22:28211order to generate C++ bindings to them. The wrapper library can be placed
212in `//third_party/rust/<cratename>/<epoch>/wrapper` or at another single place
213that all C++ goes through to access the library. The [CXX](https://cxx.rs) is
214used to generate bindings between C++ and Rust.
danakj6e25f742022-12-01 21:47:42215
216See
danakjbb4d0c772023-10-13 13:22:28217[`//third_party/rust/serde_json_lenient/v0_1/wrapper/`](
218https://source.chromium.org/chromium/chromium/src/+/main:third_party/rust/serde_json_lenient/v0_1/wrapper/)
219and
220[`//components/qr_code_generator`](
221https://source.chromium.org/chromium/chromium/src/+/main:components/qr_code_generator/;l=1;drc=b185db5d502d4995627e09d62c6934590031a5f2)
222for examples.
danakj6e25f742022-12-01 21:47:42223
danakjbb4d0c772023-10-13 13:22:28224Rust libraries should use the
225[`rust_static_library`](
226https://source.chromium.org/chromium/chromium/src/+/main:build/rust/rust_static_library.gni)
227GN template (not the built-in `rust_library`) to integrate properly into the
228mixed-language Chromium build and get the correct compiler options applied to
229them.
danakj6e25f742022-12-01 21:47:42230
Lukasz Anforowiczdcdb524a2025-03-24 19:26:41231See `rust-ffi.md` for information on C++/Rust FFI.
danakj6e25f742022-12-01 21:47:42232
Tatsuyuki Ishib3425ab02025-04-10 19:02:20233# Unstable features
234
235Unstable features are **unsupported** by default in Chromium. Any use of an
236unstable language or library feature should be agreed upon by the Rust toolchain
237team before enabling it.
238
239Since Chromium imports the Rust toolchain at its HEAD and builds it in a
240nightly-like configuration, it is technically possible to depend on unstable
241features. However, unstable features often change in a backwards incompatible
242way without a warning. If such incompatible changes are introduced, importing a
243new version of toolchain now requires the owner to fix forward, instead of being
244an automated process. This makes toolchain upgrades prohibitively difficult.
245
246When an exception is required, consider:
247
248- Whether the unstable feature brings significant value that is unattainable
249 in stable alternatives
250- The risk of breaking changes to the feature
251- Ways to fallback in case a backward-incompatible toolchain change is
252 introduced
253
254A list of exceptions is maintained in
255[`../tools/rust/unstable_rust_feature_usage.md`](../tools/rust/unstable_rust_feature_usage.md).
256
danakj3d037ff2024-11-07 19:31:41257# Logging
Adrian Taylor91eaa362024-02-09 14:17:03258
danakj3d037ff2024-11-07 19:31:41259Use the [log](https://docs.rs/log) crate's macros in place of base `LOG`
260macros from C++. They do the same things. The `debug!` macro maps to
261`DLOG(INFO)`, the `info!` macro maps to `LOG(INFO)`, and `warn!` and `error!`
262map to `LOG(WARNING)` and `LOG(ERROR)` respectively. The additional `trace!`
263macro maps to `DLOG(INFO)` (but there is [WIP to map it to `DVLOG(INFO)`](
264https://chromium-review.googlesource.com/c/chromium/src/+/5996820)).
265
266Note that the standard library also includes a helpful
267[`dbg!`](https://doc.rust-lang.org/std/macro.dbg.html) macro which writes
268everything about a variable to `stderr`.
269
270Logging may not yet work in component builds:
271[crbug.com/374023535](https://crbug.com/374023535).
272
273# Tracing
274
275TODO: [crbug.com/377915495](https://crbug.com/377915495).
276
277# Strings
278
279Prefer to use [`BString`](https://docs.rs/bstr/latest/bstr/struct.BString.html)
280and [`BStr`](https://docs.rs/bstr/latest/bstr/struct.BStr.html) to work with
281strings in first-party code instead of `std::String` and `str`. These types do
282not require the strings to be valid UTF-8, and avoid error handling or panic
283crashes when working with strings from C++ and/or from the web. Because the
284web is not UTF-8 encoded, many strings in Chromium are also not.
285
286In cross-language bindings, `&[u8]` can be used to represent a string until
287native support for `BStr` is available in our interop tooling. A `u8` slice
288can be converted to `BStr` or treated as a string with
289[`ByteSlice`](https://docs.rs/bstr/latest/bstr/trait.ByteSlice.html).
Adrian Taylor91eaa362024-02-09 14:17:03290
danakj6e25f742022-12-01 21:47:42291# Using VSCode
292
2931. Ensure you're using the `rust-analyzer` extension for VSCode, rather than
294 earlier forms of Rust support.
danakjbb4d0c772023-10-13 13:22:282952. Run `gn` with the `--export-rust-project` flag, such as:
296 `gn gen out/Release --export-rust-project`.
danakj6e25f742022-12-01 21:47:422973. `ln -s out/Release/rust-project.json rust-project.json`
2984. When you run VSCode, or any other IDE that uses
299 [rust-analyzer](https://rust-analyzer.github.io/) it should detect the
300 `rust-project.json` and use this to give you rich browsing, autocompletion,
301 type annotations etc. for all the Rust within the Chromium codebase.
danakjf3d7f372023-12-07 18:17:123025. Point rust-analyzer to the rust toolchain in Chromium. Otherwise you will
303 need to install Rustc in your system, and Chromium uses the nightly
304 compiler, so you would need that to match. Add the following to
305 `.vscode/settings.json` in the Chromium checkout:
306 ```
307 {
308 // The rest of the settings...
309
310 "rust-analyzer.cargo.extraEnv": {
311 "PATH": "../../third_party/rust-toolchain/bin:$PATH",
312 }
313 }
314 ```
315 This assumes you are working with an output directory like `out/Debug` which
316 has two levels; adjust the number of `..` in the path according to your own
317 setup.
Adrian Taylorc5fbb572023-11-21 14:25:42318
319# Using cargo
320
321If you are building a throwaway or experimental tool, you might like to use pure
322`cargo` tooling rather than `gn` and `ninja`. Even then, you may choose
323to restrict yourself to the toolchain and crates that are already approved for
324use in Chromium.
325
326Here's how.
327
328```
329export PATH_TO_CHROMIUM_SRC=~/chromium/src
330mkdir my-rust-tool
331cd my-rust-tool
332mkdir .cargo
333cat <<END > .cargo/config.toml
334[source.crates-io]
335replace-with = "vendored-sources"
336
337[source.vendored-sources]
338directory = "$PATH_TO_CHROMIUM_SRC/third_party/rust/chromium_crates_io/vendor"
339END
340$PATH_TO_CHROMIUM_SRC/third_party/rust-toolchain/bin/cargo init --offline
341$PATH_TO_CHROMIUM_SRC/third_party/rust-toolchain/bin/cargo run --offline
342```
343
344Most `cargo` tooling works well with this setup; one exception is `cargo add`,
345but you can still add dependencies manually to your `Cargo.toml`:
346
347```
348[dependencies]
349log = "0.4"
350```
David Adriand8918692024-12-12 22:02:50351
352[interop-rust-doc]: https://docs.google.com/document/d/1kvgaVMB_isELyDQ4nbMJYWrqrmL3UZI4tDxnyxy9RTE/edit?tab=t.0#heading=h.fpqr6hf3c3j0