Skip to content

Instantly share code, notes, and snippets.

@Shnatsel
Last active August 25, 2018 03:16
Show Gist options
  • Save Shnatsel/0c024a51b64c6e0b6c6e66f991904816 to your computer and use it in GitHub Desktop.
Save Shnatsel/0c024a51b64c6e0b6c6e66f991904816 to your computer and use it in GitHub Desktop.
trivial testcase to notice uninitialized memory use in Rust
use std::vec;
// Use the system allocator so we can substitute it with a custom one via LD_PRELOAD
use std::alloc::System;
#[global_allocator]
static GLOBAL: System = System;
/// Returns the sum of all values in a vector of uninitialized memory
fn sum_uninitialized() -> u64 {
let mut my_vec: Vec<u64> = Vec::with_capacity(2048);
unsafe {my_vec.set_len(2048);}
let mut sum = 0;
for element in my_vec {
sum += element;
}
sum
}
// The program will panic if the use of uninitialized memory is noticed.
fn main() {
let first_run_result = sum_uninitialized();
let second_run_result = sum_uninitialized();
println!("Sum is {}", first_run_result);
assert!(first_run_result == second_run_result);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment