You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
To create an Alloc struct, Alloc::new() is called with a byte slice. Then, to allocate, Alloc::alloc is called. This seems to align to make an aligned allocation, but does not take into account that the start of the byte slice may be at any address.
For example, imagine a fresh Alloc struct:
Alloc{len:1021,pos:0,start:3// The *address* 3}
Now imagine alloc::<u64>() is called, the important line here is:
let new_pos = round_up(self.pos, align);
This will set new_pos to 0, since round_up(0, 8) is 0.
The line that actually calculates the final address is:
This will calculate the address of the allocation to be 3.add(0) = 3, and will hand out an &mut MaybeUninit<T> with the reference itself having the value 3. This is obviously not aligned, and therefore not sound.
This will start out by calculating the address that is free by adding self.pos to the base pointer (self.start).
It then contininues to round that up to whatever the alignment of T requires.
Lastly it substracts the base pointer from this value, effectively having calculated the offset such that base.add(offset) is aligned for T
The text was updated successfully, but these errors were encountered:
The alloc implementation is unsound.
To create an
Alloc
struct,Alloc::new()
is called with a byte slice. Then, to allocate,Alloc::alloc
is called. This seems to align to make an aligned allocation, but does not take into account that the start of the byte slice may be at any address.For example, imagine a fresh Alloc struct:
Now imagine
alloc::<u64>()
is called, the important line here is:This will set
new_pos
to 0, sinceround_up(0, 8)
is 0.The line that actually calculates the final address is:
This will calculate the address of the allocation to be
3.add(0) = 3
, and will hand out an&mut MaybeUninit<T>
with the reference itself having the value3
. This is obviously not aligned, and therefore not sound.To fix this, the following can be used:
This will start out by calculating the address that is free by adding
self.pos
to the base pointer (self.start
).It then contininues to round that up to whatever the alignment of T requires.
Lastly it substracts the base pointer from this value, effectively having calculated the offset such that
base.add(offset)
is aligned forT
The text was updated successfully, but these errors were encountered: