Skip to content

Commit

Permalink
adding 0 size test
Browse files Browse the repository at this point in the history
  • Loading branch information
devnexen committed May 14, 2024
1 parent 8708834 commit a0914de
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 4 deletions.
10 changes: 6 additions & 4 deletions src/shims/unix/foreign_items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
// (MSVC explicitly does not support this.)
let [align, size] =
this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?;
let mut align = this.read_target_usize(align)?;
let align = this.read_target_usize(align)?;
let size = this.read_target_usize(size)?;

// The size must be a power of the alignment.
Expand All @@ -334,12 +334,14 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
let _ = this.set_last_error(this.eval_libc("EINVAL"));
this.write_null(dest)?;
} else {
if align < this.pointer_size().bytes() {
align = this.pointer_size().bytes();
let mut align = Align::from_bytes(align).unwrap();
let min_align = this.malloc_align(size);
if align < min_align {
align = min_align;
}
let ptr = this.allocate_ptr(
Size::from_bytes(size),
Align::from_bytes(align).unwrap(),
align,
MiriMemoryKind::C.into(),
)?;
this.write_pointer(ptr, dest)?;
Expand Down
14 changes: 14 additions & 0 deletions tests/fail-dep/libc/aligned_alloc.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//@ignore-target-windows: Windows does not support the standard C11 aligned_alloc.

fn main() {
// libc doesn't have this function (https://github.com/rust-lang/libc/issues/3689),
// so we declare it ourselves.
extern "C" {
fn aligned_alloc(alignment: libc::size_t, size: libc::size_t) -> *mut libc::c_void;
}

unsafe {
aligned_alloc(2, 0); //~ERROR: memory leaked
}
}

15 changes: 15 additions & 0 deletions tests/fail-dep/libc/aligned_alloc.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
error: memory leaked: ALLOC (C heap, size: 0, align: 2), allocated here:
--> $DIR/aligned_alloc.rs:LL:CC
|
LL | aligned_alloc(2, 0);
| ^^^^^^^^^^^^^^^^^^^
|
= note: BACKTRACE:
= note: inside `main` at $DIR/aligned_alloc.rs:LL:CC

note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace

note: the evaluated program leaked memory, pass `-Zmiri-ignore-leaks` to disable this check

error: aborting due to 1 previous error

0 comments on commit a0914de

Please sign in to comment.