about summary refs log tree commit diff
path: root/library/alloc/tests/vec_deque_alloc_error.rs
diff options
context:
space:
mode:
authorMarkus Everling <markuseverling@gmail.com>2024-05-07 19:43:54 +0000
committerMarkus Everling <markuseverling@gmail.com>2024-05-07 19:43:54 +0000
commit5cb53bc34d462a59629e5430cf2e29fe6820c550 (patch)
tree6acd5833caae5836f7abc6631669f091d2a03db0 /library/alloc/tests/vec_deque_alloc_error.rs
parentffe8510e3d989f35163ac5406af82e1c9dd8f769 (diff)
downloadrust-5cb53bc34d462a59629e5430cf2e29fe6820c550.tar.gz
rust-5cb53bc34d462a59629e5430cf2e29fe6820c550.zip
Move `test_shrink_to_unwind` to its own file.
This way, no other test can be tripped up by `test_shrink_to_unwind` changing the alloc error hook.
Diffstat (limited to 'library/alloc/tests/vec_deque_alloc_error.rs')
-rw-r--r--library/alloc/tests/vec_deque_alloc_error.rs49
1 files changed, 49 insertions, 0 deletions
diff --git a/library/alloc/tests/vec_deque_alloc_error.rs b/library/alloc/tests/vec_deque_alloc_error.rs
new file mode 100644
index 00000000000..c11f4556da9
--- /dev/null
+++ b/library/alloc/tests/vec_deque_alloc_error.rs
@@ -0,0 +1,49 @@
+#![feature(alloc_error_hook, allocator_api)]
+
+use std::{
+    alloc::{set_alloc_error_hook, AllocError, Allocator, Layout, System},
+    collections::VecDeque,
+    panic::{catch_unwind, AssertUnwindSafe},
+    ptr::NonNull,
+};
+
+#[test]
+fn test_shrink_to_unwind() {
+    // This tests that `shrink_to` leaves the deque in a consistent state when
+    // the call to `RawVec::shrink_to_fit` unwinds. The code is adapted from #123369
+    // but changed to hopefully not have any UB even if the test fails.
+
+    struct BadAlloc;
+
+    unsafe impl Allocator for BadAlloc {
+        fn allocate(&self, l: Layout) -> Result<NonNull<[u8]>, AllocError> {
+            // We allocate zeroed here so that the whole buffer of the deque
+            // is always initialized. That way, even if the deque is left in
+            // an inconsistent state, no uninitialized memory should be accessed.
+            System.allocate_zeroed(l)
+        }
+
+        unsafe fn deallocate(&self, ptr: NonNull<u8>, layout: Layout) {
+            unsafe { System.deallocate(ptr, layout) }
+        }
+
+        unsafe fn shrink(
+            &self,
+            _ptr: NonNull<u8>,
+            _old_layout: Layout,
+            _new_layout: Layout,
+        ) -> Result<NonNull<[u8]>, AllocError> {
+            Err(AllocError)
+        }
+    }
+
+    set_alloc_error_hook(|_| panic!("alloc error"));
+
+    let mut v = VecDeque::with_capacity_in(15, BadAlloc);
+    v.push_back(1);
+    v.push_front(2);
+    // This should unwind because it calls `BadAlloc::shrink` and then `handle_alloc_error` which unwinds.
+    assert!(catch_unwind(AssertUnwindSafe(|| v.shrink_to_fit())).is_err());
+    // This should only pass if the deque is left in a consistent state.
+    assert_eq!(v, [2, 1]);
+}