Context Navigation

Rust in Tor

What & why

We are currently investigating integrating Rust as a first-class language in
Tor. We decided upon Rust due to the benefits of memory safety and the ability
to directly integrate Rust and C.
To read more about how and why this started, see our meeting notes from the
2017 meeting in Amsterdam.

Current status

We are working to get basic structures in place in order to easily build Tor with
Rust and add more Rust modules. This includes deciding how we will do
dependency management, linking Rust and C modules, and type translation across
the Rust/C FFI boundary.
We recently merged #22106 which we will use to test platform support across distributions.

Please note that we're not taking implementations of new features in Rust at this point in time.

Future steps

What we are currently working on

Understand alignment between Rust and Tor supported platforms. This is a list of which platforms we aim to support, it would be helpful to understand the intersection with Rust. (#22771)

However, we should create a doc/HACKING/RustInTor.md with basic information to get people started, as it isn't obvious to look at the above wiki pages. Also, there is still missing information, such as which dependencies are necessary if someone chooses to build using a specified local directory for Rust dependencies.

For example, we should have:

Which dependencies are needed if someone wants to build using local Rust dependencies.

In Tor, we currently have the ability to run tests for a single C module (or even a single unit test). As specified in doc/HACKING/WritingTests, running tests for the cell format module (for example) can be done via ./src/test/test cellfmt/..

Rust modules should have a similar option. Currently 'cargo test' can be run within a single Rust module, but this will not link against C modules. It would be good to be able to do this and retain the ability to test a single Rust module. Also, it would be nice to make this similar to running single C module tests, to minimize developer confusion.

If we are going to start writing more Tor things in Rust, it would be nice to understand the reproducibility of binaries created with rustc. I suspect the Tor Browser Team would also be interested in having these results, since parts of Firefox are now written in Rust, and soon (ESR 58?) it will no longer be optional to use them.

Note: this ticket is not about the reproducibility of rustc iteself. That is an extremely deep rabbit hole (trust me, I have a rustc chained back to the OCaml days). Someday we may need to explore that, but that time is not now.

My approach for this task would be probably be to create a Docker instance which builds some trivial Rust program, and then run the Docker instance on different machines and compare the hashes of the binaries (then optionally investigate the differences using whatever tools like running strings and moving up to Ida or whatever).

This enables certain things, e.g. debug_assert!(false = true) will panic. It also does stuff like bounds checks for every access and integer overflow/underflow checks every time any number is used. These are great things to do, but they are usually done in debug builds which are used in testing, not in release. This will make our code literally hundreds of times slower, so we should probably remove this.

This is possibly a good idea, because we're specifying the hash we expect. It might be a bad idea, because it specifies the registry for packages, which I'm pretty sure will mean "offline" builds would never work. In addition, it also conflicts with the dependency specification in src/rust/tor_util/Cargo.toml:

In #22830, if we do cargo update to get the dependencies, this currently looks at the Cargo.toml and gives us libc = "0.2.24" which is correct and is what we asked for with libc = "*". However, this does not satisfy the constraints in the lockfile.

In #22905, I discussed a problem that is partially the result of committing src/rust/Cargo.lock.

[Including Cargo.lock files] is possibly a good idea, because we're specifying the hash we expect. It might be a bad idea, because it specifies the registry for packages, which I'm pretty sure will mean "offline" builds would never work. In addition, it also conflicts with the dependency specification in src/rust/tor_util/Cargo.toml […]

That is, I'm pretty sure that no matter what we do on #22830, "offline" builds are going to be broken because of including this lockfile. We should figure out what the benefits of having it are, and if we can live without them.

In order to better understand if we've broken something when Rust support is enabled, we should start additionally building Tor on our Jenkins setup with Rust. See https://trac.torproject.org/projects/tor/wiki/RustInTor for the different ways to do this (one pulls the dependencies down and the other is given a local directory).

We should investigate building tor with ./configure --enable-rust on/for Windows. See the wiki page on Rust in Tor for more info. This is possibly a helpful endeavour for the Tor Browser Team as well, since the next Firefox ESR (released in March 2018 and finalised in June 2018) will not have an option to disable building with the Rust code.

Interested in helping out?

As said before, right now we are still testing platform support and
integration across the Rust/C FFI. There are a lot of places where help would
be useful.

Testing Distribution Support

(We will soon add these instructions in doc/HACKING as well)

To build Tor with Rust:

You will need to run the configure script with the --enable-rust flag to explicitly build with Rust. Additionally, you will need to specify where to fetch Rust dependencies, as we allow for either fetching dependencies from Cargo or specifying a local directory.

Identifying good candidates to move to Rust

The places in the Tor codebase that are good candidates for porting to Rust
are:

loosely coupled to other Tor submodules,

have high test coverage, and

would benefit from being implemented in a memory safe language.

Help in either identifying places such as this, or working to improve existing
areas of the C codebase by adding regression tests and simplifying
dependencies, would be really helpful.

Furthermore, as submodules in C are implemented in Rust, this is a good opportunity to refactor, add more tests, and split modules into smaller areas of responsibility.

Coding Standards

1. Rust module structure

We follow the generic structure of Rust modules. However, we require a tests/ directory for module API tests (this allows us to run these tests in the entire Tor test suite).

2. Keep FFI and library logic separate

Your Rust module should expose APIs in Rust that produce the exact same behavior as what the module FFI exposes. FFI bindings for external consumers should exist only to wrap Rust functions and translate for Tor C submodules.