Skip to content

Commit

Permalink
chore: Document how to use address sanitizer (#152)
Browse files Browse the repository at this point in the history
`docs/sanitizers.md`:
- The steps are documented instead of automated because it makes it
  easier for readers to adapt the steps if they do no longer work, or
  never worked, for their needs. One downside of this is that it
  becomes more difficult to test that the instructions are correct, but
  for now this seems like a good choice. In the future it would be
  interesting to leverage the cargo runner option to partially automate
  locating, uploading and running the executable.
  • Loading branch information
apljungquist authored Dec 16, 2024
1 parent eb103bd commit 40f7792
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions docs/sanitizers.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
Sanitizers are particularly helpful when working with unsafe code which.
Rust has unstable support for compiling programs with a sanitizer[^1], the documentation was insufficient for me to get started.
This is a brief guide on how to build and run a program with AddressSanitizer enabled.
Note that the instructions are specific to an aarch64 device, but adapting them for other architectures should be easy.

Ensure the app is installed so we can patch it:
```shell
cargo-acap-sdk install -- -p $AXIS_PACKAGE
```

Ensure the asan library is in place so that we can load it:
```shell
scp /opt/axis/acapsdk/sysroots/aarch64/usr/lib/libasan.so.8.0.0 $AXIS_DEVICE_USER@$AXIS_DEVICE_IP:/tmp/libasan.so
```

Compile the tests:
```shell
CARGO_TARGET_AARCH64_UNKNOWN_LINUX_GNU_RUSTFLAGS="-C link-arg=--sysroot=/opt/axis/acapsdk/sysroots/aarch64 -C link-arg=-lasan -Zsanitizer=address -Z external-clangrt" \
cargo +nightly build \
--package $AXIS_PACKAGE \
--target aarch64-unknown-linux-gnu \
--tests
```

Find the executable:
```shell
find target/ -executable -name $AXIS_PACKAGE-\* -type f
```

Upload the executable:
```shell
scp <EXECUTABLE> $AXIS_DEVICE_USER@$AXIS_DEVICE_IP:/usr/local/packages/$AXIS_PACKAGE/$AXIS_PACKAGE
```

Run the executable:
```shell
ssh $AXIS_DEVICE_USER@$AXIS_DEVICE_IP LD_PRELOAD=/tmp/libasan.so /usr/local/packages/$AXIS_PACKAGE/$AXIS_PACKAGE
```

[^1]: https://doc.rust-lang.org/beta/unstable-book/compiler-flags/sanitizer.html#sanitizer

0 comments on commit 40f7792

Please sign in to comment.