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 | |
| 12 | Rust provides a cross-platform memory-safe language so that all platforms can |
| 13 | handle untrustworthy data directly from a privileged process, without the |
| 14 | performance overheads and complexity of a utility process. |
| 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 | |
danakj | bb4d0c77 | 2023-10-13 13:22:28 | [diff] [blame^] | 22 | Rust is approved by Chrome ATLs for production use in |
| 23 | [certain third-party scenarios](../docs/adding_to_third_party.md#Rust). |
danakj | 6e25f74 | 2022-12-01 21:47:42 | [diff] [blame] | 24 | |
| 25 | For questions or help, reach out to `[email protected]` or `#rust` on the |
| 26 | [Chromium Slack](https://www.chromium.org/developers/slack/). |
| 27 | |
danakj | 6e25f74 | 2022-12-01 21:47:42 | [diff] [blame] | 28 | If you use VSCode, we have [additional advice below](#using-vscode). |
| 29 | |
danakj | bb4d0c77 | 2023-10-13 13:22:28 | [diff] [blame^] | 30 | # Adding a third-party Rust library |
danakj | 6e25f74 | 2022-12-01 21:47:42 | [diff] [blame] | 31 | |
danakj | bb4d0c77 | 2023-10-13 13:22:28 | [diff] [blame^] | 32 | Third-party libraries are pulled from [crates.io](https://crates.io), but |
| 33 | Chromium does not use Cargo as a build system. |
danakj | 6e25f74 | 2022-12-01 21:47:42 | [diff] [blame] | 34 | |
| 35 | ## Third-party review |
| 36 | |
danakj | 6e25f74 | 2022-12-01 21:47:42 | [diff] [blame] | 37 | All 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 |
| 39 | how to have a library reviewed. |
| 40 | |
danakj | bb4d0c77 | 2023-10-13 13:22:28 | [diff] [blame^] | 41 | ## Importing a crate from crates.io |
| 42 | |
| 43 | The `//third_party/rust/third_party.toml` crate defines the set of crates |
| 44 | depended on from first-party code. Any transitive dependencies will be found |
| 45 | from those listed there. The file is a subset of a |
| 46 | [standard `Cargo.toml` file](https://doc.rust-lang.org/cargo/reference/manifest.html), |
| 47 | but only listing the `[dependencies]` section. |
| 48 | |
| 49 | To use a third-party crate "bar" version 3 from first party code, add the |
| 50 | following to `//third_party/rust/third_party.toml` in `[dependencies]`: |
| 51 | ```toml |
| 52 | [dependencies] |
| 53 | bar = "3" |
| 54 | ``` |
| 55 | |
| 56 | To enable a feature "spaceships" in the crate, change the entry in |
| 57 | `//third_party/rust/third_party.toml` to include the feature: |
| 58 | ```toml |
| 59 | [dependencies] |
| 60 | bar = { version = "3", features = [ "spaceships" ] } |
| 61 | ``` |
| 62 | |
| 63 | ### Generating `BUILD.gn` files for third-party crates |
| 64 | |
| 65 | To generate `BUILD.gn` files for all third-party crates, and find missing |
| 66 | transitive dependencies to download, use the `gnrt` tool: |
| 67 | |
| 68 | 1. Change directory to the root `src/` dir of Chromium. |
| 69 | 1. Run `vpython3 ./tools/crates/run_gnrt.py gen` to build and run gnrt with the `gen` action. |
| 70 | |
| 71 | Or, to directly build and run gnrt with the system Rust toolchain: |
| 72 | |
| 73 | 1. Change directory to the root `src/` dir of Chromium. |
| 74 | 1. Build and run `gnrt gen`: |
| 75 | `cargo run --release --manifest-path tools/crates/gnrt/Cargo.toml --target-dir out/gnrt gen`. |
| 76 | |
| 77 | This will generate a `BUILD.gn` file for each third-party crate. The `BUILD.gn` |
| 78 | file changes will be visible in `git status` and can be added with `git add`. |
| 79 | |
| 80 | ### Downloading missing third-party crates |
| 81 | |
| 82 | To download crate "foo", at version 4.2.3: |
| 83 | 1. Change directory to the root src/ dir of Chromium. |
| 84 | 1. Run `gnrt` with the `download` action. e.g. |
| 85 | `vpython3 ./tools/crates/run_gnrt.py download --security-critical=yes --shipped=yes foo 4.2.3` |
| 86 | |
| 87 | This will download the crate and unpack it into |
| 88 | `//third_party/rust/foo/v4/crate`. The entire `v4` directory, which includes the |
| 89 | `crate` subdirectory as well as a generated `README.chromium` file, should be |
| 90 | added to the repository with `git add third_party/rust/foo/v4`. |
| 91 | |
| 92 | Once all the crates are downloaded and `gnrt gen` completes, a CL can be |
| 93 | uploaded to go through third-party review. |
| 94 | |
| 95 | ### Patching third-party crates. |
| 96 | |
| 97 | You may patch a crate in tree, but save any changes made into a diff file in |
| 98 | a `patches/` directory for the crate. The diff file should be generated by |
| 99 | `git-format-patch` each new patch numbered consecutively so that they can be |
| 100 | applied in order. For example, these files might exist if the "foo" crate was |
| 101 | patched with a couple of changes: |
| 102 | |
| 103 | ``` |
| 104 | //third_party/rust/foo/v4/patches/0001-Edit-the-Cargo-toml.diff |
| 105 | //third_party/rust/foo/v4/patches/0002-Other-changes.diff |
| 106 | ``` |
| 107 | |
| 108 | ### Updating existing third-party crates |
| 109 | |
| 110 | To update a crate "foo" to the latest version you must just re-import it at this |
| 111 | time. To update from version "1.2.0" to "1.3.2": |
| 112 | 1. Remove the `//third_party/rust/foo/v1/crate` directory, which contains the |
| 113 | upstream code. |
| 114 | 1. Re-download the crate at the new version with `out/gnrt/release/gnrt download |
| 115 | foo 1.3.2`. |
| 116 | 1. If there are any, re-apply local patches with |
| 117 | `for i in $(find third_party/rust/foo/v1/patches/*); do patch -p1 < $i; done` |
| 118 | 1. Run `vpython3 ./tools/crates/run_gnrt.py gen` to re-generate all third-party |
| 119 | `BUILD.gn` files. |
| 120 | 1. Build `all_rust` to verify things are working. |
| 121 | |
| 122 | ### Directory structure for third-party crates |
| 123 | |
| 124 | The directory structure for a crate "foo" version 3.4.2 is: |
| 125 | ``` |
| 126 | //third_party/ |
| 127 | rust/ |
| 128 | foo/ |
| 129 | v3/ |
| 130 | BUILD.gn (generated by gnrt gen) |
| 131 | README.chromium (generated by gnrt download) |
| 132 | crate/ |
| 133 | Cargo.toml |
| 134 | src/ |
| 135 | ...etc... |
| 136 | patches/ |
| 137 | 0001-Edit-the-Cargo-toml.diff |
| 138 | 0002-Other-changes.diff |
| 139 | ``` |
| 140 | |
danakj | 6e25f74 | 2022-12-01 21:47:42 | [diff] [blame] | 141 | ## Writing a wrapper for binding generation |
| 142 | |
| 143 | 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^] | 144 | order to generate C++ bindings to them. The wrapper library can be placed |
| 145 | in `//third_party/rust/<cratename>/<epoch>/wrapper` or at another single place |
| 146 | that all C++ goes through to access the library. The [CXX](https://cxx.rs) is |
| 147 | used to generate bindings between C++ and Rust. |
danakj | 6e25f74 | 2022-12-01 21:47:42 | [diff] [blame] | 148 | |
| 149 | See |
danakj | bb4d0c77 | 2023-10-13 13:22:28 | [diff] [blame^] | 150 | [`//third_party/rust/serde_json_lenient/v0_1/wrapper/`]( |
| 151 | https://source.chromium.org/chromium/chromium/src/+/main:third_party/rust/serde_json_lenient/v0_1/wrapper/) |
| 152 | and |
| 153 | [`//components/qr_code_generator`]( |
| 154 | https://source.chromium.org/chromium/chromium/src/+/main:components/qr_code_generator/;l=1;drc=b185db5d502d4995627e09d62c6934590031a5f2) |
| 155 | for examples. |
danakj | 6e25f74 | 2022-12-01 21:47:42 | [diff] [blame] | 156 | |
danakj | bb4d0c77 | 2023-10-13 13:22:28 | [diff] [blame^] | 157 | Rust libraries should use the |
| 158 | [`rust_static_library`]( |
| 159 | https://source.chromium.org/chromium/chromium/src/+/main:build/rust/rust_static_library.gni) |
| 160 | GN template (not the built-in `rust_library`) to integrate properly into the |
| 161 | mixed-language Chromium build and get the correct compiler options applied to |
| 162 | them. |
danakj | 6e25f74 | 2022-12-01 21:47:42 | [diff] [blame] | 163 | |
danakj | bb4d0c77 | 2023-10-13 13:22:28 | [diff] [blame^] | 164 | The [CXX](https://cxx.rs) tool is used for generating C++ bindings to Rust |
| 165 | code. Since it requires explicit declarations in Rust, an wrapper shim around a |
| 166 | pure Rust library is needed. Add these Rust shims that contain the CXX |
| 167 | `bridge` macro to the `cxx_bindings` GN variable in the `rust_static_library` |
| 168 | to have CXX generate a C++ header for that file. To include the C++ header |
| 169 | file, rooted in the `gen` output directory, use |
danakj | 6e25f74 | 2022-12-01 21:47:42 | [diff] [blame] | 170 | ``` |
danakj | bb4d0c77 | 2023-10-13 13:22:28 | [diff] [blame^] | 171 | #include "the/path/to/the/rust/file.rs.h" |
danakj | 6e25f74 | 2022-12-01 21:47:42 | [diff] [blame] | 172 | ``` |
| 173 | |
danakj | 6e25f74 | 2022-12-01 21:47:42 | [diff] [blame] | 174 | # Using VSCode |
| 175 | |
| 176 | 1. Ensure you're using the `rust-analyzer` extension for VSCode, rather than |
| 177 | earlier forms of Rust support. |
danakj | bb4d0c77 | 2023-10-13 13:22:28 | [diff] [blame^] | 178 | 2. Run `gn` with the `--export-rust-project` flag, such as: |
| 179 | `gn gen out/Release --export-rust-project`. |
danakj | 6e25f74 | 2022-12-01 21:47:42 | [diff] [blame] | 180 | 3. `ln -s out/Release/rust-project.json rust-project.json` |
| 181 | 4. When you run VSCode, or any other IDE that uses |
| 182 | [rust-analyzer](https://rust-analyzer.github.io/) it should detect the |
| 183 | `rust-project.json` and use this to give you rich browsing, autocompletion, |
| 184 | type annotations etc. for all the Rust within the Chromium codebase. |