Track memory allocation and access between C and Rust code in polyglot programs.
- Install Intel Pin and Rust
- Clone this repository into the
source/toolsfolder of your Pin installation - Run
build.shto build Baleen and follow the instructions to add it to your PATH - Copy the
rust-toolchain.tomlfile from the example program and paste it into the Rust crate you want to analyze - In your Rust project, run
cargo buildto generate the binary executable - Run
baleen <PATH TO EXECUTABLE>and watch the results in the freshly created.baleendirectory
When you run Baleen on the example program, the final report (.baleen/report.txt) will look something like this.
--- Allocation Report ---
Rust: 2704 bytes
C: 1152 bytes
Total: 3856 bytes
Name, Reads (Rust), Reads (C), Writes (Rust), Writes (C)
0, 1249, 0, 324, 0
1, 6114, 0, 1091, 0
2, 1934, 0, 0, 0
3, 2, 0, 2, 0
4, 0, 0, 2, 0
5, 0, 0, 0, 0
6, 0, 0, 0, 30
7, 0, 0, 3, 2The second portion of this file is in CSV format. In the future, a separate CSV file will be generated. For now, copy and paste it into a separate file if you'd like to open it in Excel. This table contains a list of every object allocated by your program, as well as the number of reads and writes that touch these objects.
Our program is pretty simple, so we can mostly figure out which object is which. For example, object 7 is the something object allocated in library.c because Rust writes to it three times and C writes to it twice.
You can also name objects you're interested in tracking using the baleen marker function.
use std::arch::asm;
#[unsafe(no_mangle)]
#[inline(never)]
pub extern "C" fn baleen(ptr: *const u8, size: usize, name: *const u8) {
unsafe { asm!("nop", options(nomem, nostack, preserves_flags)); }
}In the future, this function will be provided by a crate.