sos-os / kernel

The Stupid Operating System
Apache License 2.0
264 stars 16 forks source link

Weird misaligned memory allocation depending on deallocation order #8

Open hawkw opened 8 years ago

hawkw commented 8 years ago

As of 4da32e846cc0638f5a8220b06ae283269d793c90, there's a very weird bug in the heap allocator.

In buddy::test::test_alloc_and_dealloc(), when we deallocate all the 8mB blocks and then the 32mB block, the next allocation is correct. But if we deallocate the 32mB block before we've deallocated all of the 8mB blocks, the next allocation doesn't match the pointer given to us by mem.offset().

This works:

heap.deallocate(block_16_0.unwrap(), 8, 8);
heap.deallocate(block_16_3.unwrap(), 8, 8);
heap.deallocate(block_16_1.unwrap(), 8, 8);
heap.deallocate(block_16_2.unwrap(), 8, 8);

heap.deallocate(block_32_2.unwrap(), 32, 32);

let block_128_0 = heap.allocate(128, 128);
assert_eq!(Some(mem.offset(0)), block_128_0);

But this doesn't:

heap.deallocate(block_16_0.unwrap(), 8, 8);
heap.deallocate(block_16_3.unwrap(), 8, 8);

heap.deallocate(block_32_2.unwrap(), 32, 32);

heap.deallocate(block_16_1.unwrap(), 8, 8);
heap.deallocate(block_16_2.unwrap(), 8, 8);

let block_128_0 = heap.allocate(128, 128);
assert_eq!(Some(mem.offset(0)), block_128_0);
hawkw commented 8 years ago

is this fixed yet?