Skip to content

Commit c0c3327

Browse files
committed
Check for overflow in DroplessArena and return aligned pointer
* Check for overflow when calculating the slice start & end position. * Align the pointer obtained from the allocator, ensuring that it satisfies user requested alignment (the allocator is only asked for layout compatible with u8 slice). * Remove an incorrect assertion from DroplessArena::align.
1 parent 4fb54ed commit c0c3327

File tree

1 file changed

+33
-20
lines changed

1 file changed

+33
-20
lines changed

src/librustc_arena/lib.rs

+33-20
Original file line numberDiff line numberDiff line change
@@ -333,13 +333,6 @@ impl Default for DroplessArena {
333333
}
334334

335335
impl DroplessArena {
336-
#[inline]
337-
fn align(&self, align: usize) {
338-
let final_address = ((self.ptr.get() as usize) + align - 1) & !(align - 1);
339-
self.ptr.set(final_address as *mut u8);
340-
assert!(self.ptr <= self.end);
341-
}
342-
343336
#[inline(never)]
344337
#[cold]
345338
fn grow(&self, additional: usize) {
@@ -370,22 +363,42 @@ impl DroplessArena {
370363
}
371364
}
372365

366+
/// Allocates a byte slice with specified size and alignment from the
367+
/// current memory chunk. Returns `None` if there is no free space left to
368+
/// satisfy the request.
373369
#[inline]
374-
pub fn alloc_raw(&self, bytes: usize, align: usize) -> &mut [u8] {
375-
unsafe {
376-
assert!(bytes != 0);
377-
378-
self.align(align);
370+
fn alloc_raw_without_grow(&self, bytes: usize, align: usize) -> Option<&mut [u8]> {
371+
let ptr = self.ptr.get() as usize;
372+
let end = self.end.get() as usize;
373+
// The allocation request fits into the current chunk iff:
374+
//
375+
// let aligned = align_to(ptr, align);
376+
// ptr <= aligned && aligned + bytes <= end
377+
//
378+
// Except that we work with fixed width integers and need to be careful
379+
// about potential overflow in the calcuation. If the overflow does
380+
// happen, then we definitely don't have enough free and need to grow
381+
// the arena.
382+
let aligned = ptr.checked_add(align - 1)? & !(align - 1);
383+
let new_ptr = aligned.checked_add(bytes)?;
384+
if new_ptr <= end {
385+
self.ptr.set(new_ptr as *mut u8);
386+
unsafe { Some(slice::from_raw_parts_mut(aligned as *mut u8, bytes)) }
387+
} else {
388+
None
389+
}
390+
}
379391

380-
let future_end = intrinsics::arith_offset(self.ptr.get(), bytes as isize);
381-
if (future_end as *mut u8) > self.end.get() {
382-
self.grow(bytes);
392+
#[inline]
393+
pub fn alloc_raw(&self, bytes: usize, align: usize) -> &mut [u8] {
394+
assert!(bytes != 0);
395+
loop {
396+
if let Some(a) = self.alloc_raw_without_grow(bytes, align) {
397+
break a;
383398
}
384-
385-
let ptr = self.ptr.get();
386-
// Set the pointer past ourselves
387-
self.ptr.set(intrinsics::arith_offset(self.ptr.get(), bytes as isize) as *mut u8);
388-
slice::from_raw_parts_mut(ptr, bytes)
399+
// No free space left. Allocate a new chunk to satisfy the request.
400+
// On failure the grow will panic or abort.
401+
self.grow(bytes);
389402
}
390403
}
391404

0 commit comments

Comments
 (0)