Skip to content

Commit 221558d

Browse files
avanhatttedinski
authored andcommitted
Add cargo help to README (rust-lang#431)
1 parent f8f529e commit 221558d

File tree

1 file changed

+64
-1
lines changed

1 file changed

+64
-1
lines changed

README.md

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ If you encounter issues when using RMC we encourage you to
4747

4848
## Running RMC
4949
RMC currently supports command-line invocation on single files.
50-
We are actively working to integrate RMC into `cargo`.
50+
We are actively working to integrate RMC into `cargo` (see [experimental Cargo integration](#experimental-cargo-integration)).
5151
Until then, the easiest way to use RMC is as follows
5252

5353

@@ -116,6 +116,69 @@ For example, consider the `CopyIntrinsics` regression test:
116116
This generates a file `main.c` which contains a "C" like formatting of the CBMC IR.
117117
1. You can also view the raw CBMC internal representation using the `--keep-temps` option.
118118

119+
### Experimental Cargo integration
120+
121+
We are actively working to improve RMC's integration with Rust's Cargo package and build system. Currently, you can build projects with Cargo via a multi-step process.
122+
123+
For example, we will describe using RMC as a backend to build the [`rand-core` crate](https://crates.io/crates/rand_core). These instructions have been tested on Ubuntu Linux with the `x86_64-unknown-linux-gnu` target.
124+
125+
1. Build RMC
126+
```
127+
./x.py build -i --stage 1 library/std
128+
```
129+
130+
2. Clone `rand` and navigate to the `rand-core` directory:
131+
```
132+
git clone [email protected]:rust-random/rand.git
133+
cd rand/rand-core
134+
```
135+
3. Next, we need to add an entry-point for CBMC to the crate's source. For now, we will just pick an existing unit test. Open `src/le.rs` and find the `test_read` function at the bottom of the file. Add the following attribute to keep the function name unmangled, so we can later pass it to CBMC.
136+
137+
```rust
138+
// #[test] <- Remove/comment out this
139+
#[no_mangle] // <- Add this
140+
fn test_read() {
141+
let bytes = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16];
142+
143+
let mut buf = [0u32; 4];
144+
read_u32_into(&bytes, &mut buf);
145+
assert_eq!(buf[0], 0x04030201);
146+
assert_eq!(buf[3], 0x100F0E0D);
147+
// ...
148+
}
149+
```
150+
151+
4. Now, we can run Cargo and specify that RMC should be the backend. We also pass a location for the build artifacts (`rand-core-demo`) and a target (`x86_64-unknown-linux-gnu`).
152+
```
153+
CARGO_TARGET_DIR=rand-core-demo RUST_BACKTRACE=1 RUSTFLAGS="-Z codegen-backend=gotoc --cfg=rmc" RUSTC=rmc-rustc cargo build --target x86_64-unknown-linux-gnu
154+
```
155+
156+
5. Now, navigate to the output directory for the given target.
157+
```
158+
cd rand-core-demo/x86_64-unknown-linux-gnu/debug/deps/
159+
```
160+
161+
6. The output of Cargo with RMC is a series of JSON files that define CMBC symbols. We now can use CBMC commands to convert and link these files:
162+
```
163+
symtab2gb rand_core-*.json --out a.out // Convert from JSON to Gotoc
164+
goto-cc --function test_read a.out -o a.out // Add the entry point we previously selected
165+
goto-instrument --drop-unused-functions a.out a.out // Remove unused functions
166+
cbmc a.out // Run CBMC
167+
```
168+
169+
You should then see verification succeed:
170+
```
171+
** 0 of 43 failed (1 iterations)
172+
VERIFICATION SUCCESSFUL
173+
```
174+
175+
To sanity-check that verification is working, try changing ` assert_eq!(buf[0], 0x04030201);` to a different value and rerun these commands.
176+
177+
For crates with multiple JSON files in the `deps` folder, we suggest running the first command in this step with [`parallel`](https://www.gnu.org/software/parallel/):
178+
```
179+
ls *.json | parallel -j 16 symtab2gb {} --out {.}.out // Convert from JSON to Gotoc
180+
```
181+
119182
## Security
120183
See [SECURITY](https://github.com/model-checking/rmc/security/policy) for more information.
121184

0 commit comments

Comments
 (0)