danakj | 6e25f74 | 2022-12-01 21:47:42 | [diff] [blame] | 1 | # Rust in Chromium |
| 2 | |
| 3 | [TOC] |
| 4 | |
| 5 | # Why? |
| 6 | |
danakj | bb4d0c77 | 2023-10-13 13:22:28 | [diff] [blame] | 7 | Handling untrustworthy data in non-trivial ways is a major source of security |
| 8 | bugs, 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 |
| 10 | you are working in a memory-safe language. |
danakj | 6e25f74 | 2022-12-01 21:47:42 | [diff] [blame] | 11 | |
Lukasz Anforowicz | c8ebad8 | 2025-05-22 20:51:41 | [diff] [blame] | 12 | Rust provides a cross-platform, memory-safe language so that all platforms can |
danakj | 6e25f74 | 2022-12-01 21:47:42 | [diff] [blame] | 13 | handle untrustworthy data directly from a privileged process, without the |
Lukasz Anforowicz | c8ebad8 | 2025-05-22 20:51:41 | [diff] [blame] | 14 | performance overhead and complexity of a utility process. |
danakj | 6e25f74 | 2022-12-01 21:47:42 | [diff] [blame] | 15 | |
danakj | 6e25f74 | 2022-12-01 21:47:42 | [diff] [blame] | 16 | # Status |
| 17 | |
danakj | bb4d0c77 | 2023-10-13 13:22:28 | [diff] [blame] | 18 | The Rust toolchain is enabled for and supports all platforms and development |
| 19 | environments that are supported by the Chromium project. The first milestone |
| 20 | to include full production-ready support was M119. |
danakj | 6e25f74 | 2022-12-01 21:47:42 | [diff] [blame] | 21 | |
David Adrian | d891869 | 2024-12-12 22:02:50 | [diff] [blame] | 22 | Rust can be used anywhere in the Chromium repository (not just `//third_party`) |
| 23 | subject to [current interop capabilities][interop-rust-doc], however it is |
| 24 | currently subject to a internal approval and FYI process. Googlers can view |
| 25 | go/chrome-rust for details. New usages of Rust are documented at |
| 26 | [`[email protected]`](https://groups.google.com/a/chromium.org/g/rust-fyi). |
danakj | 6e25f74 | 2022-12-01 21:47:42 | [diff] [blame] | 27 | |
Lukasz Anforowicz | c8ebad8 | 2025-05-22 20:51:41 | [diff] [blame] | 28 | For questions or help, reach out to |
| 29 | [`[email protected]`](https://groups.google.com/a/chromium.org/g/rust-dev), |
| 30 | or [`#rust` channel](https://chromium.slack.com/archives/C01T3EWCJ9Z) |
| 31 | on the [Chromium Slack](https://www.chromium.org/developers/slack/), |
| 32 | or (Google-internal, sorry) |
| 33 | [Chrome Rust chatroom](https://chat.google.com/room/AAAAk1UCFGg?cls=7). |
danakj | 6e25f74 | 2022-12-01 21:47:42 | [diff] [blame] | 34 | |
danakj | 6e25f74 | 2022-12-01 21:47:42 | [diff] [blame] | 35 | If you use VSCode, we have [additional advice below](#using-vscode). |
| 36 | |
danakj | bb4d0c77 | 2023-10-13 13:22:28 | [diff] [blame] | 37 | # Adding a third-party Rust library |
danakj | 6e25f74 | 2022-12-01 21:47:42 | [diff] [blame] | 38 | |
danakj | bb4d0c77 | 2023-10-13 13:22:28 | [diff] [blame] | 39 | Third-party libraries are pulled from [crates.io](https://crates.io), but |
| 40 | Chromium does not use Cargo as a build system. |
danakj | 6e25f74 | 2022-12-01 21:47:42 | [diff] [blame] | 41 | |
| 42 | ## Third-party review |
| 43 | |
Lukasz Anforowicz | c8ebad8 | 2025-05-22 20:51:41 | [diff] [blame] | 44 | All third-party libraries (not just Rust) need to go through third-party review. |
| 45 | See [//docs/adding_to_third_party.md](adding_to_third_party.md) for instructions. |
danakj | 6e25f74 | 2022-12-01 21:47:42 | [diff] [blame] | 46 | |
danakj | bb4d0c77 | 2023-10-13 13:22:28 | [diff] [blame] | 47 | ## Importing a crate from crates.io |
| 48 | |
Lukasz Anforowicz | c8ebad8 | 2025-05-22 20:51:41 | [diff] [blame] | 49 | Third-party crates (from [crates.io](https://crates.io)) |
| 50 | that Chromium depends on are described by two files: |
danakj | bb4d0c77 | 2023-10-13 13:22:28 | [diff] [blame] | 51 | |
Lukasz Anforowicz | c8ebad8 | 2025-05-22 20:51:41 | [diff] [blame] | 52 | * `//third_party/rust/chromium_crates_io/Cargo.toml`. |
| 53 | This file defines the set of crates |
| 54 | **directly** depended on from first-party code (from Chromium first-party |
| 55 | code, but also from Pdfium, V8, etc.). Their transitive dependencies don't |
| 56 | need to be listed, because they will be automatically identified and covered |
| 57 | by tools like `gnrt`. The file is a [standard `Cargo.toml` file]( |
| 58 | https://doc.rust-lang.org/cargo/reference/manifest.html), even though the crate |
| 59 | itself is never built - it is only used to enable/disable crate features, |
| 60 | specify crate versions, etc. |
| 61 | * `//third_party/rust/chromium_crates_io/gnrt_config.toml`. |
| 62 | This file defines Chromium-specific, `cargo`-agnostic metadata like: |
| 63 | - Configuring certain aspects of Chromium build (e.g. `allow_unsafe`, |
| 64 | `allow_unstable_features`, `extra_src_roots`, `group = "test"`, etc.) |
| 65 | - Specifying licensing information when it can't be automatically inferred |
| 66 | (e.g. pointing out `license_files` with non-standard filenames). |
| 67 | |
| 68 | To import a third-party crate follow the steps below: |
| 69 | |
danakj | 98bec16 | 2023-11-21 14:55:02 | [diff] [blame] | 70 | 1. Change directory to the root `src/` dir of Chromium. |
| 71 | 1. Add the crate to `//third_party/rust/chromium_crates_io/Cargo.toml`: |
| 72 | * `vpython3 ./tools/crates/run_gnrt.py add foo` to add the latest version of `foo`. |
| 73 | * `vpython3 ./tools/crates/run_gnrt.py add [email protected]` to add a specific version of `foo`. |
Lukasz Anforowicz | c8ebad8 | 2025-05-22 20:51:41 | [diff] [blame] | 74 | * Or, edit `//third_party/rust/chromium_crates_io/Cargo.toml` by hand, |
| 75 | finding the version you want from [crates.io](https://crates.io). |
danakj | 98bec16 | 2023-11-21 14:55:02 | [diff] [blame] | 76 | 1. Download the crate's files: |
| 77 | * `./tools/crates/run_gnrt.py vendor` to download the new crate. |
Lukasz Anforowicz | c8ebad8 | 2025-05-22 20:51:41 | [diff] [blame] | 78 | * This will also apply any patches in `//third_party/rust/chromium_crates_io/patches`. |
| 79 | See `//third_party/rust/chromium_crates_io/patches/README.md` for more details. |
danakj | 98bec16 | 2023-11-21 14:55:02 | [diff] [blame] | 80 | 1. (optional) If the crate is only to be used by tests and tooling, then |
| 81 | specify the `"test"` group in `//third_party/rust/chromium_crates_io/gnrt_config.toml`: |
| 82 | ``` |
| 83 | [crate.foo] |
| 84 | group = "test" |
| 85 | ``` |
| 86 | 1. Generate the `BUILD.gn` file for the new crate: |
| 87 | * `vpython3 ./tools/crates/run_gnrt.py gen` |
Lukasz Anforowicz | b7a722f | 2025-05-30 00:13:31 | [diff] [blame] | 88 | 1. Add `//third_party/rust/crate_name/OWNERS` |
Lukasz Anforowicz | 057876d | 2024-06-05 19:07:58 | [diff] [blame] | 89 | 1. Add the new files to git: |
| 90 | * `git add -f third_party/rust/chromium_crates_io/vendor`. |
| 91 | (The `-f` is important, as files may be skipped otherwise from a |
| 92 | `.gitignore` inside the crate.) |
| 93 | * `git add third_party/rust` |
Lukasz Anforowicz | c8ebad8 | 2025-05-22 20:51:41 | [diff] [blame] | 94 | 1. Upload the CL and get a review from `//third_party/rust/OWNERS` |
| 95 | (check |
| 96 | [`third_party/rust/OWNERS-review-checklist.md`](../third_party/rust/OWNERS-review-checklist.md) |
| 97 | to see what to expect). |
danakj | 0ec93d1 | 2023-11-17 16:12:23 | [diff] [blame] | 98 | |
Lukasz Anforowicz | fbf3e76 | 2025-05-21 17:50:41 | [diff] [blame] | 99 | Note that at this point the new crate is still not seen by `gn` nor `ninja`, |
| 100 | and is not covered by CQ. To make the new crate part of the build, |
| 101 | you need to add a `deps` edge between an existing build target |
| 102 | and the newly added `//third_party/rust/some_crate/v123:lib` target. |
| 103 | This will allow `autoninja -C out/Default third_party/rust/some_crate/v123:lib` |
| 104 | to work. Additionally, this will help CQ to prevent regressions when updating |
| 105 | `rustc` or enabling new Rust warnings. |
| 106 | |
danakj | 98bec16 | 2023-11-21 14:55:02 | [diff] [blame] | 107 | ## Security |
danakj | bb4d0c77 | 2023-10-13 13:22:28 | [diff] [blame] | 108 | |
danakj | 98bec16 | 2023-11-21 14:55:02 | [diff] [blame] | 109 | If a shipping library needs security review (has any `unsafe`), and the review |
| 110 | finds it's not satisfying the [rule of 2](../docs/security/rule-of-2.md), then |
| 111 | move it to the `"sandbox"` group in `//third_party/rust/chromium_crates_io/gnrt_config.toml` |
| 112 | to make it clear it can't be used in a privileged process: |
| 113 | ``` |
| 114 | [crate.foo] |
| 115 | group = "sandbox" |
| 116 | ``` |
| 117 | |
| 118 | If a transitive dependency moves from `"safe"` to `"sandbox"` and causes |
| 119 | a dependency chain across the groups, it will break the `gnrt vendor` step. |
| 120 | You will need to fix the new crate so that it's deemed safe in unsafe review, |
| 121 | or move the other dependent crates out of `"safe"` as well by setting their |
| 122 | group in `gnrt_config.toml`. |
| 123 | |
| 124 | # Updating existing third-party crates |
| 125 | |
Lukasz Anforowicz | 85528a6 | 2024-03-20 19:12:53 | [diff] [blame] | 126 | Third-party crates will get updated semi-automatically through the process |
| 127 | described in |
| 128 | [`../tools/crates/create_update_cl.md`](../tools/crates/create_update_cl.md). |
Lukasz Anforowicz | c8ebad8 | 2025-05-22 20:51:41 | [diff] [blame] | 129 | If you nevertheless need to manually update a crate to its latest minor or major |
| 130 | version, then follow the steps below. To facilitate easier review, we recommend |
| 131 | uploading separate patchsets for 1) manual changes, and 2) tool-driven, |
| 132 | automated changes. |
Lukasz Anforowicz | 85528a6 | 2024-03-20 19:12:53 | [diff] [blame] | 133 | |
danakj | 98bec16 | 2023-11-21 14:55:02 | [diff] [blame] | 134 | 1. Change directory to the root `src/` dir of Chromium. |
Dominik Röttsches | a07a553 | 2024-01-24 19:16:23 | [diff] [blame] | 135 | 1. Update the versions in `//third_party/rust/chromium_crates_io/Cargo.toml`. |
Lukasz Anforowicz | c8ebad8 | 2025-05-22 20:51:41 | [diff] [blame] | 136 | * `vpython3 ./tools/crates/run_gnrt.py update <crate name>`. |
| 137 | * Under the hood this invokes `cargo update` and accepts the same |
| 138 | [command line parameters](https://doc.rust-lang.org/cargo/commands/cargo-update.html#update-options). |
| 139 | In particular, you may need to specify `--breaking` when working on |
| 140 | major version updates. |
danakj | 98bec16 | 2023-11-21 14:55:02 | [diff] [blame] | 141 | 1. Download any updated crate's files: |
| 142 | * `./tools/crates/run_gnrt.py vendor` |
Lukasz Anforowicz | 8452bd8d | 2023-11-28 23:31:55 | [diff] [blame] | 143 | 1. Add the downloaded files to git: |
danakj | 98bec16 | 2023-11-21 14:55:02 | [diff] [blame] | 144 | * `git add -f third_party/rust/chromium_crates_io/vendor` |
| 145 | * The `-f` is important, as files may be skipped otherwise from a |
| 146 | `.gitignore` inside the crate. |
danakj | 98bec16 | 2023-11-21 14:55:02 | [diff] [blame] | 147 | 1. Generate the `BUILD.gn` files |
| 148 | * `vpython3 ./tools/crates/run_gnrt.py gen` |
| 149 | * Or, directly through (nightly) cargo: |
| 150 | `cargo run --release --manifest-path tools/crates/gnrt/Cargo.toml --target-dir out/gnrt gen` |
Lukasz Anforowicz | 8452bd8d | 2023-11-28 23:31:55 | [diff] [blame] | 151 | 1. Add the generated files to git: |
Lukasz Anforowicz | c8ebad8 | 2025-05-22 20:51:41 | [diff] [blame] | 152 | * `git add third_party/rust` |
danakj | bb4d0c77 | 2023-10-13 13:22:28 | [diff] [blame] | 153 | |
| 154 | ### Directory structure for third-party crates |
| 155 | |
| 156 | The directory structure for a crate "foo" version 3.4.2 is: |
Lukasz Anforowicz | c8ebad8 | 2025-05-22 20:51:41 | [diff] [blame] | 157 | |
danakj | bb4d0c77 | 2023-10-13 13:22:28 | [diff] [blame] | 158 | ``` |
| 159 | //third_party/ |
| 160 | rust/ |
danakj | 98bec16 | 2023-11-21 14:55:02 | [diff] [blame] | 161 | foo/ (for the "foo" crate) |
Lukasz Anforowicz | 8452bd8d | 2023-11-28 23:31:55 | [diff] [blame] | 162 | v3/ (version 3.4.2 maps to the v3 epoch) |
danakj | bb4d0c77 | 2023-10-13 13:22:28 | [diff] [blame] | 163 | BUILD.gn (generated by gnrt gen) |
danakj | 98bec16 | 2023-11-21 14:55:02 | [diff] [blame] | 164 | README.chromium (generated by gnrt vendor) |
Lukasz Anforowicz | 8452bd8d | 2023-11-28 23:31:55 | [diff] [blame] | 165 | chromium_crates_io/ |
| 166 | vendor/ |
Lukasz Anforowicz | c8ebad8 | 2025-05-22 20:51:41 | [diff] [blame] | 167 | foo-v3 (crate sources downloaded from crates.io) |
danakj | 98bec16 | 2023-11-21 14:55:02 | [diff] [blame] | 168 | patches/ |
Lukasz Anforowicz | 8452bd8d | 2023-11-28 23:31:55 | [diff] [blame] | 169 | foo/ (patches for the "foo" crate) |
Lukasz Anforowicz | b2b4b123 | 2025-04-25 16:22:33 | [diff] [blame] | 170 | 0001-Some-changes.diff |
danakj | bb4d0c77 | 2023-10-13 13:22:28 | [diff] [blame] | 171 | 0002-Other-changes.diff |
Lukasz Anforowicz | 8452bd8d | 2023-11-28 23:31:55 | [diff] [blame] | 172 | Cargo.toml |
| 173 | Cargo.lock |
| 174 | gnrt_config.toml |
danakj | bb4d0c77 | 2023-10-13 13:22:28 | [diff] [blame] | 175 | ``` |
| 176 | |
danakj | 6e25f74 | 2022-12-01 21:47:42 | [diff] [blame] | 177 | ## Writing a wrapper for binding generation |
| 178 | |
| 179 | Most Rust libraries will need a more C++-friendly API written on top of them in |
danakj | bb4d0c77 | 2023-10-13 13:22:28 | [diff] [blame] | 180 | order to generate C++ bindings to them. The wrapper library can be placed |
| 181 | in `//third_party/rust/<cratename>/<epoch>/wrapper` or at another single place |
| 182 | that all C++ goes through to access the library. The [CXX](https://cxx.rs) is |
| 183 | used to generate bindings between C++ and Rust. |
danakj | 6e25f74 | 2022-12-01 21:47:42 | [diff] [blame] | 184 | |
| 185 | See |
danakj | bb4d0c77 | 2023-10-13 13:22:28 | [diff] [blame] | 186 | [`//third_party/rust/serde_json_lenient/v0_1/wrapper/`]( |
| 187 | https://source.chromium.org/chromium/chromium/src/+/main:third_party/rust/serde_json_lenient/v0_1/wrapper/) |
| 188 | and |
| 189 | [`//components/qr_code_generator`]( |
| 190 | https://source.chromium.org/chromium/chromium/src/+/main:components/qr_code_generator/;l=1;drc=b185db5d502d4995627e09d62c6934590031a5f2) |
| 191 | for examples. |
danakj | 6e25f74 | 2022-12-01 21:47:42 | [diff] [blame] | 192 | |
danakj | bb4d0c77 | 2023-10-13 13:22:28 | [diff] [blame] | 193 | Rust libraries should use the |
| 194 | [`rust_static_library`]( |
| 195 | https://source.chromium.org/chromium/chromium/src/+/main:build/rust/rust_static_library.gni) |
| 196 | GN template (not the built-in `rust_library`) to integrate properly into the |
| 197 | mixed-language Chromium build and get the correct compiler options applied to |
| 198 | them. |
danakj | 6e25f74 | 2022-12-01 21:47:42 | [diff] [blame] | 199 | |
Lukasz Anforowicz | dcdb524a | 2025-03-24 19:26:41 | [diff] [blame] | 200 | See `rust-ffi.md` for information on C++/Rust FFI. |
danakj | 6e25f74 | 2022-12-01 21:47:42 | [diff] [blame] | 201 | |
Tatsuyuki Ishi | b3425ab0 | 2025-04-10 19:02:20 | [diff] [blame] | 202 | # Unstable features |
| 203 | |
| 204 | Unstable features are **unsupported** by default in Chromium. Any use of an |
| 205 | unstable language or library feature should be agreed upon by the Rust toolchain |
Lukasz Anforowicz | c8ebad8 | 2025-05-22 20:51:41 | [diff] [blame] | 206 | team before enabling it. See |
| 207 | [`tools/rust/unstable_rust_feature_usage.md`](../tools/rust/unstable_rust_feature_usage.md) |
| 208 | for more details. |
Tatsuyuki Ishi | b3425ab0 | 2025-04-10 19:02:20 | [diff] [blame] | 209 | |
danakj | 3d037ff | 2024-11-07 19:31:41 | [diff] [blame] | 210 | # Logging |
Adrian Taylor | 91eaa36 | 2024-02-09 14:17:03 | [diff] [blame] | 211 | |
danakj | 3d037ff | 2024-11-07 19:31:41 | [diff] [blame] | 212 | Use the [log](https://docs.rs/log) crate's macros in place of base `LOG` |
| 213 | macros from C++. They do the same things. The `debug!` macro maps to |
| 214 | `DLOG(INFO)`, the `info!` macro maps to `LOG(INFO)`, and `warn!` and `error!` |
| 215 | map to `LOG(WARNING)` and `LOG(ERROR)` respectively. The additional `trace!` |
| 216 | macro maps to `DLOG(INFO)` (but there is [WIP to map it to `DVLOG(INFO)`]( |
| 217 | https://chromium-review.googlesource.com/c/chromium/src/+/5996820)). |
| 218 | |
| 219 | Note that the standard library also includes a helpful |
| 220 | [`dbg!`](https://doc.rust-lang.org/std/macro.dbg.html) macro which writes |
| 221 | everything about a variable to `stderr`. |
| 222 | |
| 223 | Logging may not yet work in component builds: |
| 224 | [crbug.com/374023535](https://crbug.com/374023535). |
| 225 | |
| 226 | # Tracing |
| 227 | |
| 228 | TODO: [crbug.com/377915495](https://crbug.com/377915495). |
| 229 | |
danakj | 6e25f74 | 2022-12-01 21:47:42 | [diff] [blame] | 230 | # Using VSCode |
| 231 | |
| 232 | 1. Ensure you're using the `rust-analyzer` extension for VSCode, rather than |
| 233 | earlier forms of Rust support. |
danakj | bb4d0c77 | 2023-10-13 13:22:28 | [diff] [blame] | 234 | 2. Run `gn` with the `--export-rust-project` flag, such as: |
| 235 | `gn gen out/Release --export-rust-project`. |
danakj | 6e25f74 | 2022-12-01 21:47:42 | [diff] [blame] | 236 | 3. `ln -s out/Release/rust-project.json rust-project.json` |
| 237 | 4. When you run VSCode, or any other IDE that uses |
| 238 | [rust-analyzer](https://rust-analyzer.github.io/) it should detect the |
| 239 | `rust-project.json` and use this to give you rich browsing, autocompletion, |
| 240 | type annotations etc. for all the Rust within the Chromium codebase. |
danakj | f3d7f37 | 2023-12-07 18:17:12 | [diff] [blame] | 241 | 5. Point rust-analyzer to the rust toolchain in Chromium. Otherwise you will |
| 242 | need to install Rustc in your system, and Chromium uses the nightly |
| 243 | compiler, so you would need that to match. Add the following to |
| 244 | `.vscode/settings.json` in the Chromium checkout: |
| 245 | ``` |
| 246 | { |
| 247 | // The rest of the settings... |
| 248 | |
| 249 | "rust-analyzer.cargo.extraEnv": { |
| 250 | "PATH": "../../third_party/rust-toolchain/bin:$PATH", |
| 251 | } |
| 252 | } |
| 253 | ``` |
| 254 | This assumes you are working with an output directory like `out/Debug` which |
| 255 | has two levels; adjust the number of `..` in the path according to your own |
| 256 | setup. |
Adrian Taylor | c5fbb57 | 2023-11-21 14:25:42 | [diff] [blame] | 257 | |
| 258 | # Using cargo |
| 259 | |
| 260 | If you are building a throwaway or experimental tool, you might like to use pure |
| 261 | `cargo` tooling rather than `gn` and `ninja`. Even then, you may choose |
| 262 | to restrict yourself to the toolchain and crates that are already approved for |
Lukasz Anforowicz | c8ebad8 | 2025-05-22 20:51:41 | [diff] [blame] | 263 | use in Chromium, by |
Adrian Taylor | c5fbb57 | 2023-11-21 14:25:42 | [diff] [blame] | 264 | |
Lukasz Anforowicz | c8ebad8 | 2025-05-22 20:51:41 | [diff] [blame] | 265 | * Using `tools/crates/run_cargo.py` (which will use |
| 266 | Chromium's `//third_party/rust-toolchain/bin/cargo`) |
| 267 | * Configuring `.cargo/config.toml` to ask to use the crates vendored |
| 268 | into Chromium's `//third_party/rust/chromium_crates_io`. |
Adrian Taylor | c5fbb57 | 2023-11-21 14:25:42 | [diff] [blame] | 269 | |
Lukasz Anforowicz | c8ebad8 | 2025-05-22 20:51:41 | [diff] [blame] | 270 | An example of how this can work can be found in |
| 271 | https://crrev.com/c/6320795/5. |
David Adrian | d891869 | 2024-12-12 22:02:50 | [diff] [blame] | 272 | |
| 273 | [interop-rust-doc]: https://docs.google.com/document/d/1kvgaVMB_isELyDQ4nbMJYWrqrmL3UZI4tDxnyxy9RTE/edit?tab=t.0#heading=h.fpqr6hf3c3j0 |