Rust SDK tutorial: times10 function#

You are a 10x programmer and will soon have your annual performance review, so you are writing a function that takes any number and multiplies it by ten. If your boss tells you how productive your coworker is, you can use this tool to calculate your ten-times-greater productivity and impress him.

You write the function in Rust and at first you use print debugging. However, this performance review is very important for your career so you decide not to take any chances. You ultimately decide to thoroughly test this function using the Antithesis Rust SDK.

The rest of the document walks you through this process.

Note

Recall that the basic SDK workflow is

  1. Include the Antithesis SDK dependency in your cargo.toml file.

  2. Use SDK functions in your code.

  3. Build your code.

  4. Deploy your build.

Creating your project#

Step 0: Write your project. You begin by writing your function in Rust with printf!() debugging.

Make a directory where you would like to create your Rust project and cd to that directory.

mkdir myapp
cd myapp
cargo init --bin

Edit the file src/main.rs with the following content:

main.rs#
1  fn main() {
2      println!("Hello, world!");
3      println!("{} x 10 = {}", 3, times10(3));
4      println!("{} x 10 = {}", 8, times10(8));
5  }
6
7  fn times10(x: i32) -> i32 {
8      x * 10
9  }

Now build and run.

cargo run

and it prints

Hello, world!
3 x 10 = 30
8 x 10 = 80

Using our SDK functions#

Step 1: Include the Rust SDK and serde_json as dependencies

[package]
name = "myapp"
version = "0.1.0"
edition = "2021"

[dependencies]
antithesis_sdk = "0.1.5"
serde_json = "1.0.25"

Include the antithesis_sdk and json!() macro in your project code by adding these lines to the top of src/main.rs:

use antithesis_sdk::prelude::*;
use serde_json::json;

Step 2. Use SDK functions in your code

You wrote a function to multiply numbers by ten. What sorts of properties should it have? The output should always be even. You should also make sure you are testing a wide variety of inputs, such as both even and odd numbers.

Modify the code. The modifications are individually explained below.

src/main.rs#
 1  use antithesis_sdk::prelude::*;
 2  use serde_json::json;
 3
 4  fn main() {
 5      antithesis_init();
 6      println!("Hello, world!");
 7      for _i in 0..50 {
 8          let x = (random::get_random() % 500) as i32;
 9          println!("{} x 10 = {}", x, times10(x));
10      }
11  }
12
13  fn times10(x: i32) -> i32 {
14      assert_sometimes!(x % 2 == 1, "input is sometimes odd", &json!({"input": x}));
15      let result = x * 10;
16      assert_always!(result % 2 == 0, "result is always even", &json!({"result": result}));
17      result
18  }
  • Line 1 You imported the Antithesis Rust SDK.

  • Line 2 You imported the json!() macro from the serde_json crate.

  • Line 5 You called antithesis_init() so that assertions will work correctly.

  • You added two assertions to times10.

    • Line 16 You assert that the result is even using an Always Assertion. This is a fairly conventional assertion.

    • Line 14 You insert a Sometimes Assertion, or an assertion that something will happen at least once across the entire testing session. You assert that sometimes during testing, the function will be called with an odd argument.

      The two types of assertions complement one another: Always Assertions assert that the behavior is correct, whereas Sometimes Assertions assert that you are testing under wide enough conditions to surface any potential incorrect behavior. In this case, the output would trivially be even if you only passed it even inputs—you must ensure your properties are not being trivially satisfied!

  • Lines 7-10 You use randomness to call the function with many random values (between 0 and 500). Previously, you called the function with hardcoded values but now you will pass the function random values and test that the output is always even. This approach is more powerful but makes Sometimes Assertions necessary—now you must test that you are passing the function odd values, whereas previously the tests were hardcoded so you were certain that you were passing it odd values.

    You call the random function get_random to draw a random number and then pass this random number to times10. You use a loop to do this fifty times in a row. Every time the times10 function is called in this loop, it triggers the assertions in lines 14 & 16. In summary: You will test times10 by passing it a random integer fifty times in a row. You have asserted that all fifty outputs must be even and that at least one random input must be odd.

You will now build your project with coverage instrumentation.

Building your project#

To send code to Antithesis, you should compile with coverage instrumentation. This guide adds instrumentation as well as demonstrates using the SDK.

Step 3: Build your code

Instrumentation requires a copy of libvoidstar.so,so follow the Rust instrumentation guide to obtain a copy. Suppose it is downloaded to /usr/lib.

Change the build commandline as follows:

 1  # Assumes libvoidstar.so is in /usr/lib
 2  export LIBVOIDSTAR_PATH="/usr/lib"
 3
 4  LD_LIBRARY_PATH="${LIBVOIDSTAR_PATH}" \
 5  RUSTFLAGS=" \
 6         -Ccodegen-units=1 \
 7         -Cpasses=sancov-module \
 8         -Cllvm-args=-sanitizer-coverage-level=3 \
 9         -Cllvm-args=-sanitizer-coverage-trace-pc-guard \
10         -Clink-args=-Wl,--build-id \
11         -L${LIBVOIDSTAR_PATH} \
12         -lvoidstar \
13         " \
14  cargo build
  • Line 2 defines an environment variable that is the absolute path to libvoidstar mentioned above. Yours may be different.

  • Line 7 adds the reference to the Antithesis Instrumentation library.

  • Lines 8-10 adds instrumentation

  • Line 12 adds the search path for the Antithesis Instrumentation library.

This also requires clang version 14 or above, or else cargo build will fail when it encounters the Rust SDK.

4. Deploy your build

You are now ready to build your project. If you are building for local deployment then you would be done here, but suppose instead you intend to send your project to Antithesis for testing. Antithesis will explore your software and search for violations of the properties you have defined.

You must send us containers as described in getting started.

  • The executable should be included in your container. In this example, target/debug/myapp.

  • The instrumentation symbols () should be included in your configuration image. In this case, we’ve generated an unstripped binary, so we’ll just include myapp/myapp_executable in the configuration image, at the same path where we included the executable in the runnable container.

Note

This example is simplified compared to what the full Getting Started guide describes. Note that the function main is the workload and times10 is the software itself. There are no dependencies. The instrumentation symbols will go in the configuration image as stated.

Ultimately, you will receive a triage report that confirms the properties you defined are true: the output of the function is always even and the inputs to the function are sometimes odd. You now have a well-tested function that can multiply numbers by ten.