Skip to content

Commit 430298c

Browse files
committed
a bit of refactoring and tweak the aligned-allocation tests
1 parent 5ea21ca commit 430298c

File tree

4 files changed

+61
-62
lines changed

4 files changed

+61
-62
lines changed

src/tools/miri/src/shims/alloc.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -175,10 +175,13 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
175175

176176
fn aligned_alloc(
177177
&mut self,
178-
align: u64,
179-
size: u64,
178+
align: &OpTy<'tcx, Provenance>,
179+
size: &OpTy<'tcx, Provenance>,
180180
) -> InterpResult<'tcx, Pointer<Option<Provenance>>> {
181181
let this = self.eval_context_mut();
182+
let align = this.read_target_usize(align)?;
183+
let size = this.read_target_usize(size)?;
184+
182185
// Alignment must be a power of 2, and "supported by the implementation".
183186
// We decide that "supported by the implementation" means that the
184187
// size must be a multiple of the alignment. (This restriction seems common

src/tools/miri/src/shims/unix/foreign_items.rs

-3
Original file line numberDiff line numberDiff line change
@@ -300,9 +300,6 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
300300
// (MSVC explicitly does not support this.)
301301
let [align, size] =
302302
this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?;
303-
let align = this.read_target_usize(align)?;
304-
let size = this.read_target_usize(size)?;
305-
306303
let res = this.aligned_alloc(align, size)?;
307304
this.write_pointer(res, dest)?;
308305
}

src/tools/miri/src/shims/wasi/foreign_items.rs

-3
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,6 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
2929
"aligned_alloc" => {
3030
let [align, size] =
3131
this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?;
32-
let align = this.read_target_usize(align)?;
33-
let size = this.read_target_usize(size)?;
34-
3532
let res = this.aligned_alloc(align, size)?;
3633
this.write_pointer(res, dest)?;
3734
}

src/tools/miri/tests/pass-dep/libc/libc-mem.rs

+56-54
Original file line numberDiff line numberDiff line change
@@ -148,53 +148,55 @@ fn test_calloc() {
148148

149149
#[cfg(not(target_os = "windows"))]
150150
fn test_memalign() {
151-
// A normal allocation.
152-
unsafe {
153-
let mut ptr: *mut libc::c_void = ptr::null_mut();
154-
let align = 8;
155-
let size = 64;
156-
assert_eq!(libc::posix_memalign(&mut ptr, align, size), 0);
157-
assert!(!ptr.is_null());
158-
assert!(ptr.is_aligned_to(align));
159-
ptr.cast::<u8>().write_bytes(1, size);
160-
libc::free(ptr);
161-
}
151+
for _ in 0..16 {
152+
// A normal allocation.
153+
unsafe {
154+
let mut ptr: *mut libc::c_void = ptr::null_mut();
155+
let align = 8;
156+
let size = 64;
157+
assert_eq!(libc::posix_memalign(&mut ptr, align, size), 0);
158+
assert!(!ptr.is_null());
159+
assert!(ptr.is_aligned_to(align));
160+
ptr.cast::<u8>().write_bytes(1, size);
161+
libc::free(ptr);
162+
}
162163

163-
// Align > size.
164-
unsafe {
165-
let mut ptr: *mut libc::c_void = ptr::null_mut();
166-
let align = 64;
167-
let size = 8;
168-
assert_eq!(libc::posix_memalign(&mut ptr, align, size), 0);
169-
assert!(!ptr.is_null());
170-
assert!(ptr.is_aligned_to(align));
171-
ptr.cast::<u8>().write_bytes(1, size);
172-
libc::free(ptr);
173-
}
164+
// Align > size.
165+
unsafe {
166+
let mut ptr: *mut libc::c_void = ptr::null_mut();
167+
let align = 64;
168+
let size = 8;
169+
assert_eq!(libc::posix_memalign(&mut ptr, align, size), 0);
170+
assert!(!ptr.is_null());
171+
assert!(ptr.is_aligned_to(align));
172+
ptr.cast::<u8>().write_bytes(1, size);
173+
libc::free(ptr);
174+
}
174175

175-
// Size not multiple of align
176-
unsafe {
177-
let mut ptr: *mut libc::c_void = ptr::null_mut();
178-
let align = 16;
179-
let size = 31;
180-
assert_eq!(libc::posix_memalign(&mut ptr, align, size), 0);
181-
assert!(!ptr.is_null());
182-
assert!(ptr.is_aligned_to(align));
183-
ptr.cast::<u8>().write_bytes(1, size);
184-
libc::free(ptr);
185-
}
176+
// Size not multiple of align
177+
unsafe {
178+
let mut ptr: *mut libc::c_void = ptr::null_mut();
179+
let align = 16;
180+
let size = 31;
181+
assert_eq!(libc::posix_memalign(&mut ptr, align, size), 0);
182+
assert!(!ptr.is_null());
183+
assert!(ptr.is_aligned_to(align));
184+
ptr.cast::<u8>().write_bytes(1, size);
185+
libc::free(ptr);
186+
}
186187

187-
// Size == 0
188-
unsafe {
189-
let mut ptr: *mut libc::c_void = ptr::null_mut();
190-
let align = 64;
191-
let size = 0;
192-
assert_eq!(libc::posix_memalign(&mut ptr, align, size), 0);
193-
// Non-null pointer is returned if size == 0.
194-
// (This is not a guarantee, it just reflects our current behavior.)
195-
assert!(!ptr.is_null());
196-
assert!(ptr.is_aligned_to(align));
197-
libc::free(ptr);
188+
// Size == 0
189+
unsafe {
190+
let mut ptr: *mut libc::c_void = ptr::null_mut();
191+
let align = 64;
192+
let size = 0;
193+
assert_eq!(libc::posix_memalign(&mut ptr, align, size), 0);
194+
// Non-null pointer is returned if size == 0.
195+
// (This is not a guarantee, it just reflects our current behavior.)
196+
assert!(!ptr.is_null());
197+
assert!(ptr.is_aligned_to(align));
198+
libc::free(ptr);
199+
}
198200
}
199201

200202
// Non-power of 2 align
@@ -260,20 +262,20 @@ fn test_aligned_alloc() {
260262
assert_eq!(p, ptr::null_mut());
261263
}
262264

263-
// alignment lesser than a word but still a successful allocation
264-
unsafe {
265-
let p = aligned_alloc(1, 4);
266-
assert!(!p.is_null());
267-
assert!(p.is_aligned_to(4));
268-
libc::free(p);
269-
}
270-
271265
// repeated tests on correct alignment/size
272266
for _ in 0..16 {
267+
// alignment 1, size 4 should succeed and actually must align to 4 (because C says so...)
268+
unsafe {
269+
let p = aligned_alloc(1, 4);
270+
assert!(!p.is_null());
271+
assert!(p.is_aligned_to(4));
272+
libc::free(p);
273+
}
274+
273275
unsafe {
274-
let p = aligned_alloc(16, 16);
276+
let p = aligned_alloc(64, 64);
275277
assert!(!p.is_null());
276-
assert!(p.is_aligned_to(16));
278+
assert!(p.is_aligned_to(64));
277279
libc::free(p);
278280
}
279281
}

0 commit comments

Comments
 (0)