Skip to content

rustup for AllocRef changes #1296

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion rust-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
b793f403bdfbcc0ff3e15ed8177a81d79ba4a29b
127a11a344eb59b5aea1464e98257c262dcba967
11 changes: 3 additions & 8 deletions tests/compile-fail/deallocate-bad-alignment.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
#![feature(allocator_api)]

extern crate alloc;

use alloc::alloc::Global;
use std::alloc::{AllocRef, Layout};
use std::alloc::{alloc, dealloc, realloc, Layout};

// error-pattern: allocation has size 1 and alignment 1, but gave size 1 and alignment 2

fn main() {
unsafe {
let x = Global.alloc(Layout::from_size_align_unchecked(1, 1)).unwrap().0;
Global.dealloc(x, Layout::from_size_align_unchecked(1, 2));
let x = alloc(Layout::from_size_align_unchecked(1, 1));
dealloc(x, Layout::from_size_align_unchecked(1, 2));
}
}
11 changes: 3 additions & 8 deletions tests/compile-fail/deallocate-bad-size.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
#![feature(allocator_api)]

extern crate alloc;

use alloc::alloc::Global;
use std::alloc::{AllocRef, Layout};
use std::alloc::{alloc, dealloc, realloc, Layout};

// error-pattern: allocation has size 1 and alignment 1, but gave size 2 and alignment 1

fn main() {
unsafe {
let x = Global.alloc(Layout::from_size_align_unchecked(1, 1)).unwrap().0;
Global.dealloc(x, Layout::from_size_align_unchecked(2, 1));
let x = alloc(Layout::from_size_align_unchecked(1, 1));
dealloc(x, Layout::from_size_align_unchecked(2, 1));
}
}
13 changes: 4 additions & 9 deletions tests/compile-fail/deallocate-twice.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
#![feature(allocator_api)]

extern crate alloc;

use alloc::alloc::Global;
use std::alloc::{AllocRef, Layout};
use std::alloc::{alloc, dealloc, realloc, Layout};

// error-pattern: dereferenced after this allocation got freed

fn main() {
unsafe {
let x = Global.alloc(Layout::from_size_align_unchecked(1, 1)).unwrap().0;
Global.dealloc(x, Layout::from_size_align_unchecked(1, 1));
Global.dealloc(x, Layout::from_size_align_unchecked(1, 1));
let x = alloc(Layout::from_size_align_unchecked(1, 1));
dealloc(x, Layout::from_size_align_unchecked(1, 1));
dealloc(x, Layout::from_size_align_unchecked(1, 1));
}
}
11 changes: 3 additions & 8 deletions tests/compile-fail/reallocate-bad-size.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
#![feature(allocator_api)]

extern crate alloc;

use alloc::alloc::Global;
use std::alloc::{AllocRef, Layout};
use std::alloc::{alloc, dealloc, realloc, Layout};

// error-pattern: allocation has size 1 and alignment 1, but gave size 2 and alignment 1

fn main() {
unsafe {
let x = Global.alloc(Layout::from_size_align_unchecked(1, 1)).unwrap().0;
Global.realloc(x, Layout::from_size_align_unchecked(2, 1), 1).unwrap();
let x = alloc(Layout::from_size_align_unchecked(1, 1));
realloc(x, Layout::from_size_align_unchecked(2, 1), 1);
}
}
13 changes: 4 additions & 9 deletions tests/compile-fail/reallocate-change-alloc.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
#![feature(allocator_api)]

extern crate alloc;

use alloc::alloc::Global;
use std::alloc::{AllocRef, Layout};
use std::alloc::{alloc, dealloc, realloc, Layout};

fn main() {
unsafe {
let x = Global.alloc(Layout::from_size_align_unchecked(1, 1)).unwrap().0;
Global.realloc(x, Layout::from_size_align_unchecked(1, 1), 1).unwrap();
let _z = *(x.as_ptr() as *mut u8); //~ ERROR dereferenced after this allocation got freed
let x = alloc(Layout::from_size_align_unchecked(1, 1));
realloc(x, Layout::from_size_align_unchecked(1, 1), 1);
let _z = *x; //~ ERROR dereferenced after this allocation got freed
}
}
13 changes: 4 additions & 9 deletions tests/compile-fail/reallocate-dangling.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
#![feature(allocator_api)]

extern crate alloc;

use alloc::alloc::Global;
use std::alloc::{AllocRef, Layout};
use std::alloc::{alloc, dealloc, realloc, Layout};

// error-pattern: dereferenced after this allocation got freed

fn main() {
unsafe {
let x = Global.alloc(Layout::from_size_align_unchecked(1, 1)).unwrap().0;
Global.dealloc(x, Layout::from_size_align_unchecked(1, 1));
Global.realloc(x, Layout::from_size_align_unchecked(1, 1), 1).unwrap();
let x = alloc(Layout::from_size_align_unchecked(1, 1));
dealloc(x, Layout::from_size_align_unchecked(1, 1));
realloc(x, Layout::from_size_align_unchecked(1, 1), 1);
}
}
17 changes: 9 additions & 8 deletions tests/run-pass/heap_allocator.rs
Original file line number Diff line number Diff line change
@@ -1,36 +1,37 @@
#![feature(allocator_api)]

use std::ptr::NonNull;
use std::alloc::{Global, AllocRef, Layout, System};
use std::alloc::{Global, AllocRef, Layout, System, AllocInit, ReallocPlacement};
use std::slice;

fn check_alloc<T: AllocRef>(mut allocator: T) { unsafe {
for &align in &[4, 8, 16, 32] {
let layout = Layout::from_size_align(20, align).unwrap();

for _ in 0..32 {
let a = allocator.alloc(layout).unwrap().0;
let a = allocator.alloc(layout, AllocInit::Uninitialized).unwrap().ptr;
assert_eq!(a.as_ptr() as usize % align, 0, "pointer is incorrectly aligned");
allocator.dealloc(a, layout);
}

let p1 = allocator.alloc_zeroed(layout).unwrap().0;
let p1 = allocator.alloc(layout, AllocInit::Zeroed).unwrap().ptr;
assert_eq!(p1.as_ptr() as usize % align, 0, "pointer is incorrectly aligned");

let p2 = allocator.realloc(p1, layout, 40).unwrap().0;
// old size < new size
let p2 = allocator.grow(p1, layout, 40, ReallocPlacement::MayMove, AllocInit::Uninitialized).unwrap().ptr;
let layout = Layout::from_size_align(40, align).unwrap();
assert_eq!(p2.as_ptr() as usize % align, 0, "pointer is incorrectly aligned");
let slice = slice::from_raw_parts(p2.as_ptr(), 20);
assert_eq!(&slice, &[0_u8; 20]);

// old size == new size
let p3 = allocator.realloc(p2, layout, 40).unwrap().0;
let p3 = allocator.grow(p2, layout, 40, ReallocPlacement::MayMove, AllocInit::Uninitialized).unwrap().ptr;
assert_eq!(p3.as_ptr() as usize % align, 0, "pointer is incorrectly aligned");
let slice = slice::from_raw_parts(p3.as_ptr(), 20);
assert_eq!(&slice, &[0_u8; 20]);

// old size > new size
let p4 = allocator.realloc(p3, layout, 10).unwrap().0;
let p4 = allocator.shrink(p3, layout, 10, ReallocPlacement::MayMove).unwrap().ptr;
let layout = Layout::from_size_align(10, align).unwrap();
assert_eq!(p4.as_ptr() as usize % align, 0, "pointer is incorrectly aligned");
let slice = slice::from_raw_parts(p4.as_ptr(), 10);
Expand All @@ -46,7 +47,7 @@ fn check_align_requests<T: AllocRef>(mut allocator: T) {
let iterations = 32;
unsafe {
let pointers: Vec<_> = (0..iterations).map(|_| {
allocator.alloc(Layout::from_size_align(size, align).unwrap()).unwrap().0
allocator.alloc(Layout::from_size_align(size, align).unwrap(), AllocInit::Uninitialized).unwrap().ptr
}).collect();
for &ptr in &pointers {
assert_eq!((ptr.as_ptr() as usize) % align, 0,
Expand All @@ -67,7 +68,7 @@ fn global_to_box() {
let l = Layout::new::<T>();
// allocate manually with global allocator, then turn into Box and free there
unsafe {
let ptr = Global.alloc(l).unwrap().0.as_ptr() as *mut T;
let ptr = Global.alloc(l, AllocInit::Uninitialized).unwrap().ptr.as_ptr() as *mut T;
let b = Box::from_raw(ptr);
drop(b);
}
Expand Down