rcore-os / rcore-os/buddy_system_allocator
Reject unsupported `ORDER` values explicitly instead of panicking later with arithmetic overflow
Nobody has claimed this yet.
- Dominant language
- Rust
- Stars
- 145
- Forks
- 34
- PR merge metrics
- No merged PRs in 30d
Description
The crate currently has implicit constraints on the const generic ORDER, but those constraints are not checked explicitly.
What I can reproduce today:
FrameAllocator::<0>panics later due toORDER - 1Heap::<0>also panics later due toORDER - 1FrameAllocator::<{ usize::BITS as usize + 1 }>panics later due to shift overflow
Relevant code:
src/frame.rs:58->1 << (ORDER - 1)src/lib.rs:106->if order > ORDER - 1
Reproduction
use buddy_system_allocator::{FrameAllocator, Heap};
#[test]
#[should_panic]
fn frame_allocator_order_zero_panics_late_with_underflow() {
let mut frame = FrameAllocator::<0>::new();
frame.add_frame(0, 1);
}
#[test]
#[should_panic]
fn heap_order_zero_panics_late_with_underflow() {
let mut heap = Heap::<0>::new();
let mut space = [0usize; 1];
unsafe {
heap.add_to_heap(
space.as_mut_ptr() as usize,
space.as_mut_ptr().add(1) as usize,
);
}
}
#[test]
#[should_panic]
fn frame_allocator_order_above_usize_bits_panics_with_shift_overflow() {
let mut frame = FrameAllocator::<{ usize::BITS as usize + 1 }>::new();
frame.add_frame(0, 1);
}
Why this matters
Today, invalid ORDER values are accepted at construction time and only fail later during normal allocator operations.
That makes the failure mode harder to understand:
- the panic happens far away from the actual configuration mistake
- the panic message reports arithmetic overflow instead of an invalid type parameter
- users have to reverse-engineer the real precondition from implementation details
Suggested fix
At minimum, reject unsupported ORDER values explicitly in constructors:
FrameAllocator::new()should rejectORDER == 0Heap::new()should rejectORDER == 0FrameAllocator::new()should also rejectORDER > usize::BITS as usize
If maintainers prefer a uniform crate-wide rule, the same upper bound could also be applied to Heap.
A runtime assert! in new() is probably the smallest change, though a const assertion would also work if a compile-time failure is preferred.
If it is confirmed to be a bug, I can submit a PR to help fix it.
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the constructor paths for FrameAllocator and Heap, then inspect src/frame.rs:58 and src/lib.rs:106 to understand the unchecked ORDER assumptions. Run the supplied reproduction tests and add coverage for invalid ORDER values; done means unsupported values are rejected at construction instead of failing later with arithmetic overflow.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- operating-systems
- Issue type
- Bug
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Quiet
- Clarity
- Clearly specified
- Newbie friendliness
- 74/100