From 033eae5ad73ef69648f7b794040de1cb2c4317a1 Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Thu, 2 Apr 2020 11:45:17 +0200 Subject: [PATCH 1/2] rustup for AllocRef changes --- rust-version | 2 +- tests/run-pass/heap_allocator.rs | 17 +++++++++-------- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/rust-version b/rust-version index 6ed19772cad..2cb1f848a68 100644 --- a/rust-version +++ b/rust-version @@ -1 +1 @@ -b793f403bdfbcc0ff3e15ed8177a81d79ba4a29b +127a11a344eb59b5aea1464e98257c262dcba967 diff --git a/tests/run-pass/heap_allocator.rs b/tests/run-pass/heap_allocator.rs index 8077be405f4..c2fcfea58cd 100644 --- a/tests/run-pass/heap_allocator.rs +++ b/tests/run-pass/heap_allocator.rs @@ -1,7 +1,7 @@ #![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(mut allocator: T) { unsafe { @@ -9,28 +9,29 @@ fn check_alloc(mut allocator: T) { unsafe { 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); @@ -46,7 +47,7 @@ fn check_align_requests(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, @@ -67,7 +68,7 @@ fn global_to_box() { let l = Layout::new::(); // 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); } From cd132f563ea5385c8dfed2b9c9032ea3e8dc3edc Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Thu, 2 Apr 2020 13:33:59 +0200 Subject: [PATCH 2/2] port compile-fail allocator tests to stable API --- tests/compile-fail/deallocate-bad-alignment.rs | 11 +++-------- tests/compile-fail/deallocate-bad-size.rs | 11 +++-------- tests/compile-fail/deallocate-twice.rs | 13 ++++--------- tests/compile-fail/reallocate-bad-size.rs | 11 +++-------- tests/compile-fail/reallocate-change-alloc.rs | 13 ++++--------- tests/compile-fail/reallocate-dangling.rs | 13 ++++--------- 6 files changed, 21 insertions(+), 51 deletions(-) diff --git a/tests/compile-fail/deallocate-bad-alignment.rs b/tests/compile-fail/deallocate-bad-alignment.rs index f2cd87bac62..9b5ee9a934b 100644 --- a/tests/compile-fail/deallocate-bad-alignment.rs +++ b/tests/compile-fail/deallocate-bad-alignment.rs @@ -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)); } } diff --git a/tests/compile-fail/deallocate-bad-size.rs b/tests/compile-fail/deallocate-bad-size.rs index 498a662518e..39a0d48c8b1 100644 --- a/tests/compile-fail/deallocate-bad-size.rs +++ b/tests/compile-fail/deallocate-bad-size.rs @@ -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)); } } diff --git a/tests/compile-fail/deallocate-twice.rs b/tests/compile-fail/deallocate-twice.rs index a851d75b9d5..3c5e8e96360 100644 --- a/tests/compile-fail/deallocate-twice.rs +++ b/tests/compile-fail/deallocate-twice.rs @@ -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)); } } diff --git a/tests/compile-fail/reallocate-bad-size.rs b/tests/compile-fail/reallocate-bad-size.rs index a62c1adae7e..bbdef4421b6 100644 --- a/tests/compile-fail/reallocate-bad-size.rs +++ b/tests/compile-fail/reallocate-bad-size.rs @@ -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); } } diff --git a/tests/compile-fail/reallocate-change-alloc.rs b/tests/compile-fail/reallocate-change-alloc.rs index 0d4b60e0a33..8130d72dee5 100644 --- a/tests/compile-fail/reallocate-change-alloc.rs +++ b/tests/compile-fail/reallocate-change-alloc.rs @@ -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 } } diff --git a/tests/compile-fail/reallocate-dangling.rs b/tests/compile-fail/reallocate-dangling.rs index 9661d7e9667..702ddc0724a 100644 --- a/tests/compile-fail/reallocate-dangling.rs +++ b/tests/compile-fail/reallocate-dangling.rs @@ -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); } }