Skip to content

Commit 4f793dd

Browse files
authored
Use u8 for alignment value (#1399)
The current `fill_alignment_gap` may write beyond the 'alignment gap'. This PR fixes this issue by using a `u8` value as the alignment value, and write the u8 into the alignment gap.
1 parent a9074a3 commit 4f793dd

File tree

2 files changed

+7
-18
lines changed

2 files changed

+7
-18
lines changed

src/util/alloc/allocator.rs

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -126,22 +126,11 @@ pub fn align_allocation_inner<VM: VMBinding>(
126126
}
127127

128128
/// Fill the specified region with the alignment value.
129-
pub fn fill_alignment_gap<VM: VMBinding>(immut_start: Address, end: Address) {
130-
let mut start = immut_start;
131-
132-
if VM::MAX_ALIGNMENT - VM::MIN_ALIGNMENT == BYTES_IN_INT {
133-
// At most a single hole
134-
if end - start != 0 {
135-
unsafe {
136-
start.store(VM::ALIGNMENT_VALUE);
137-
}
138-
}
139-
} else {
140-
while start < end {
141-
unsafe {
142-
start.store(VM::ALIGNMENT_VALUE);
143-
}
144-
start += BYTES_IN_INT;
129+
pub fn fill_alignment_gap<VM: VMBinding>(start: Address, end: Address) {
130+
if VM::ALIGNMENT_VALUE != 0 {
131+
let start_ptr = start.to_mut_ptr::<u8>();
132+
unsafe {
133+
std::ptr::write_bytes(start_ptr, VM::ALIGNMENT_VALUE, end - start);
145134
}
146135
}
147136
}

src/vm/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,8 @@ where
6363
/// The type of heap memory slice in this VM.
6464
type VMMemorySlice: slot::MemorySlice<SlotType = Self::VMSlot>;
6565

66-
/// A value to fill in alignment gaps. This value can be used for debugging.
67-
const ALIGNMENT_VALUE: usize = 0xdead_beef;
66+
/// A value to fill in alignment gaps. This value can be used for debugging. Set this value to 0 to skip filling alignment gaps.
67+
const ALIGNMENT_VALUE: u8 = 0xab;
6868
/// Allowed minimal alignment in bytes.
6969
const MIN_ALIGNMENT: usize = 1 << DEFAULT_LOG_MIN_ALIGNMENT;
7070
/// Allowed maximum alignment in bytes.

0 commit comments

Comments
 (0)