File: vec_deque_alloc_error.rs

package info (click to toggle)
rustc 1.86.0%2Bdfsg1-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental, sid
  • size: 913,560 kB
  • sloc: xml: 158,127; python: 35,921; javascript: 19,689; sh: 19,600; cpp: 18,906; ansic: 13,124; asm: 4,376; makefile: 708; perl: 29; lisp: 29; ruby: 19; sql: 11
file content (48 lines) | stat: -rw-r--r-- 1,820 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#![feature(alloc_error_hook, allocator_api)]

use std::alloc::{AllocError, Allocator, Layout, System, set_alloc_error_hook};
use std::collections::VecDeque;
use std::panic::{AssertUnwindSafe, catch_unwind};
use std::ptr::NonNull;

#[test]
#[cfg_attr(not(panic = "unwind"), ignore = "test requires unwinding support")]
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]);
}