brk.rs 840 B

1234567891011121314151617181920212223242526272829303132
  1. extern crate ralloc;
  2. use ralloc::sys::{inc_brk, segment_end};
  3. use std::ptr;
  4. fn main() {
  5. let alloc_before = Box::new("hello from the outside.");
  6. let ptr = unsafe { (segment_end().unwrap() as *const u8).offset(-1) };
  7. let byte_end = unsafe { ptr::read(ptr) };
  8. let abc = "abc";
  9. let mem = *inc_brk(8).unwrap() as *mut u64;
  10. unsafe {
  11. *mem = 90823;
  12. *mem = 2897309273;
  13. *mem = 293872;
  14. *mem = 0xDEADBEAFDEADBEAF;
  15. *mem = 99999;
  16. assert_eq!(*mem, 99999);
  17. }
  18. // Do some heap allocations.
  19. let bx = Box::new("yo mamma is so nice.");
  20. assert_eq!(*bx, "yo mamma is so nice.");
  21. assert_eq!(*alloc_before, "hello from the outside.");
  22. // Check that the stack frame is unaltered.
  23. assert_eq!(abc, "abc");
  24. assert_eq!(byte_end, unsafe { ptr::read(ptr) });
  25. }