|
| 1 | +//@ignore-target-windows: No libc on Windows |
| 2 | + |
| 3 | +#![feature(rustc_private)] |
| 4 | +#![feature(pointer_is_aligned)] |
| 5 | +#![feature(strict_provenance)] |
| 6 | + |
| 7 | +use core::ptr; |
| 8 | + |
| 9 | +fn main() { |
| 10 | + // A normal allocation. |
| 11 | + unsafe { |
| 12 | + let mut ptr: *mut libc::c_void = ptr::null_mut(); |
| 13 | + let align = 8; |
| 14 | + let size = 64; |
| 15 | + assert_eq!(libc::posix_memalign(&mut ptr, align, size), 0); |
| 16 | + assert!(!ptr.is_null()); |
| 17 | + assert!(ptr.is_aligned_to(align)); |
| 18 | + ptr.cast::<u8>().write_bytes(1, size); |
| 19 | + libc::free(ptr); |
| 20 | + } |
| 21 | + |
| 22 | + // Align > size. |
| 23 | + unsafe { |
| 24 | + let mut ptr: *mut libc::c_void = ptr::null_mut(); |
| 25 | + let align = 64; |
| 26 | + let size = 8; |
| 27 | + assert_eq!(libc::posix_memalign(&mut ptr, align, size), 0); |
| 28 | + assert!(!ptr.is_null()); |
| 29 | + assert!(ptr.is_aligned_to(align)); |
| 30 | + ptr.cast::<u8>().write_bytes(1, size); |
| 31 | + libc::free(ptr); |
| 32 | + } |
| 33 | + |
| 34 | + // Size not multiple of align |
| 35 | + unsafe { |
| 36 | + let mut ptr: *mut libc::c_void = ptr::null_mut(); |
| 37 | + let align = 16; |
| 38 | + let size = 31; |
| 39 | + assert_eq!(libc::posix_memalign(&mut ptr, align, size), 0); |
| 40 | + assert!(!ptr.is_null()); |
| 41 | + assert!(ptr.is_aligned_to(align)); |
| 42 | + ptr.cast::<u8>().write_bytes(1, size); |
| 43 | + libc::free(ptr); |
| 44 | + } |
| 45 | + |
| 46 | + // Size == 0 |
| 47 | + unsafe { |
| 48 | + let mut ptr: *mut libc::c_void = ptr::null_mut(); |
| 49 | + let align = 64; |
| 50 | + let size = 0; |
| 51 | + assert_eq!(libc::posix_memalign(&mut ptr, align, size), 0); |
| 52 | + // We are not required to return null if size == 0, but we currently do. |
| 53 | + // It's fine to remove this assert if we start returning non-null pointers. |
| 54 | + assert!(ptr.is_null()); |
| 55 | + assert!(ptr.is_aligned_to(align)); |
| 56 | + // Regardless of what we return, it must be `free`able. |
| 57 | + libc::free(ptr); |
| 58 | + } |
| 59 | + |
| 60 | + // Non-power of 2 align |
| 61 | + unsafe { |
| 62 | + let mut ptr: *mut libc::c_void = ptr::invalid_mut(0x1234567); |
| 63 | + let align = 15; |
| 64 | + let size = 8; |
| 65 | + assert_eq!(libc::posix_memalign(&mut ptr, align, size), libc::EINVAL); |
| 66 | + // The pointer is not modified on failure, posix_memalign(3) says: |
| 67 | + // > On Linux (and other systems), posix_memalign() does not modify memptr on failure. |
| 68 | + // > A requirement standardizing this behavior was added in POSIX.1-2008 TC2. |
| 69 | + assert_eq!(ptr.addr(), 0x1234567); |
| 70 | + } |
| 71 | + |
| 72 | + // Too small align (smaller than ptr) |
| 73 | + unsafe { |
| 74 | + let mut ptr: *mut libc::c_void = ptr::invalid_mut(0x1234567); |
| 75 | + let align = std::mem::size_of::<usize>() / 2; |
| 76 | + let size = 8; |
| 77 | + assert_eq!(libc::posix_memalign(&mut ptr, align, size), libc::EINVAL); |
| 78 | + // The pointer is not modified on failure, posix_memalign(3) says: |
| 79 | + // > On Linux (and other systems), posix_memalign() does not modify memptr on failure. |
| 80 | + // > A requirement standardizing this behavior was added in POSIX.1-2008 TC2. |
| 81 | + assert_eq!(ptr.addr(), 0x1234567); |
| 82 | + } |
| 83 | +} |
0 commit comments