wee_alloc icon indicating copy to clipboard operation
wee_alloc copied to clipboard

Impossibly large allocations fails but still allocates new memory pages

Open yvt opened this issue 4 years ago • 0 comments

Describe the Bug

Allocating usize::MAX - 8 bytes fails but allocates new memory pages every time.

Steps to Reproduce

#[test]
fn cannot_alloc_max_usize_m8() {
    let a = &wee_alloc::WeeAlloc::INIT;
    let layout = Layout::from_size_align(std::usize::MAX - 8, 1)
        .expect("should be able to create a `Layout` with size = std::usize::MAX - 8");
    for _ in 0..10000000 {
        let result = unsafe { a.alloc(layout) };
        assert!(result.is_err());
    }
}

Expected Behavior

The test should complete without causing OOM.

Actual Behavior

With debug assertions: thread 'cannot_alloc_max_usize_m8' panicked at 'attempt to add with overflow', .../.cargo/registry/src/github.com-1ecc6299db9ec823/memory_units-0.4.0/src/lib.rs:166:1

Without debug assertions: The test allocates tens of gigabytes of memory and eventually gets killed by the kernel.

yvt avatar Sep 12 '21 13:09 yvt