Skip to content

Commit 413d7bd

Browse files
author
Niam Shah
committed
Fixed 4 bugs – two where sizes too small for allocator or indexes too big were being accepted and another where many allocate functions were not releasing lock if they ran out of memory mid-function
1 parent 5ad406f commit 413d7bd

1 file changed

Lines changed: 16 additions & 6 deletions

File tree

common/src/buddy.rs

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,13 @@ impl AllocatorInner {
413413
size: 1 << size_log2,
414414
});
415415
}
416+
// Check if index is within valid range for this level
417+
if index >= self.size_of_level_bits(size_log2) {
418+
return Err(AllocationError::InvalidReservation {
419+
index,
420+
size: 1 << size_log2,
421+
});
422+
}
416423
self.with_level(base, size_log2, |level: &mut AllocatorLevel<'_>| {
417424
if level.reserve(index) {
418425
Ok(index)
@@ -553,12 +560,15 @@ impl BuddyAllocatorImpl {
553560

554561
// prevent physical zero page from being allocated
555562
assert_eq!(temp.to_offset(temp.reserve_raw(0, 4096)), 0);
556-
// reserve kernel pages
563+
// reserve kernel pages (only if within range)
557564
let mut pages = alloc::vec![];
558565
for i in 0..8 {
559-
let p = temp.reserve_raw(0x100000 * (i + 1), 0x100000);
560-
assert!(!p.is_null());
561-
pages.push(p);
566+
let addr = 0x100000 * (i + 1);
567+
if addr + 0x100000 <= size {
568+
let p = temp.reserve_raw(addr, 0x100000);
569+
assert!(!p.is_null());
570+
pages.push(p);
571+
}
562572
}
563573

564574
let new_inner = AllocatorInner::new_in(slice, &temp);
@@ -681,6 +691,7 @@ impl BuddyAllocatorImpl {
681691
for (i, item) in ptrs.iter_mut().enumerate() {
682692
let result = self.allocate_raw_unchecked(size);
683693
if result.is_null() {
694+
self.inner.unlock();
684695
return Some(i);
685696
}
686697
*item = result;
@@ -696,6 +707,7 @@ impl BuddyAllocatorImpl {
696707
for (i, item) in ptrs.iter_mut().enumerate() {
697708
let result = self.allocate_raw_unchecked(size);
698709
if result.is_null() {
710+
self.inner.unlock();
699711
return i;
700712
}
701713
*item = result;
@@ -1584,7 +1596,6 @@ mod tests {
15841596
}
15851597

15861598
#[test]
1587-
#[ignore]
15881599
// Testing allocating more pointers than space available, making sure bulk alloc stops cleanly when space out,
15891600
// and partial success allowed, reported successes are valid. Currently hanging
15901601
fn test_allocate_many_partial_success() {
@@ -1930,7 +1941,6 @@ mod tests {
19301941
}
19311942

19321943
#[test]
1933-
#[ignore]
19341944
// Testing allocate_many_raw where partial failure does not poison the lock
19351945
// Currently hanging because partial failures is not working, test after that is fixed
19361946
fn allocate_many_partial_failure_does_not_poison_lock() {

0 commit comments

Comments
 (0)