mirror of
https://github.com/servo/servo.git
synced 2025-08-03 12:40:06 +01:00
Auto merge of #17380 - MortimerGoro:over_alignment, r=bholley
Fix over-alignment assert crash on Android. <!-- Please describe your changes on the following line: --> See https://github.com/servo/servo/issues/17320 --- <!-- Thank you for contributing to Servo! Please replace each `[ ]` by `[X]` when the step is complete, and replace `__` with appropriate data: --> - [x] `./mach build -d` does not report any errors - [x] `./mach test-tidy` does not report any errors - [x] These changes fix #17320 (github issue number if applicable). <!-- Either: --> - [x] There are tests for these changes OR - [ ] These changes do not require tests because _____ <!-- Also, please make sure that "Allow edits from maintainers" checkbox is checked, so that we can help you if you get stuck somewhere along the way.--> <!-- Pull requests that do not address these steps are welcome, but they will require additional verification as part of the review process. --> <!-- Reviewable:start --> --- This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/servo/17380) <!-- Reviewable:end -->
This commit is contained in:
commit
f56d192276
1 changed files with 18 additions and 6 deletions
|
@ -527,12 +527,16 @@ impl<H, T> Arc<HeaderSlice<H, [T]>> {
|
|||
//
|
||||
// To avoid alignment issues, we allocate words rather than bytes,
|
||||
// rounding up to the nearest word size.
|
||||
assert!(mem::align_of::<T>() <= mem::align_of::<usize>(),
|
||||
"We don't handle over-aligned types");
|
||||
let words_to_allocate = divide_rounding_up(size, size_of::<usize>());
|
||||
let mut vec = Vec::<usize>::with_capacity(words_to_allocate);
|
||||
vec.set_len(words_to_allocate);
|
||||
let buffer = Box::into_raw(vec.into_boxed_slice()) as *mut usize as *mut u8;
|
||||
let buffer = if mem::align_of::<T>() <= mem::align_of::<usize>() {
|
||||
Self::allocate_buffer::<usize>(size)
|
||||
} else if mem::align_of::<T>() <= mem::align_of::<u64>() {
|
||||
// On 32-bit platforms <T> may have 8 byte alignment while usize has 4 byte aligment.
|
||||
// Use u64 to avoid over-alignment.
|
||||
// This branch will compile away in optimized builds.
|
||||
Self::allocate_buffer::<u64>(size)
|
||||
} else {
|
||||
panic!("Over-aligned type not handled");
|
||||
};
|
||||
|
||||
// Synthesize the fat pointer. We do this by claiming we have a direct
|
||||
// pointer to a [T], and then changing the type of the borrow. The key
|
||||
|
@ -564,6 +568,14 @@ impl<H, T> Arc<HeaderSlice<H, [T]>> {
|
|||
assert_eq!(size_of::<Self>(), size_of::<usize>() * 2, "The Arc will be fat");
|
||||
Arc { p: NonZeroPtrMut::new(ptr) }
|
||||
}
|
||||
|
||||
#[inline]
|
||||
unsafe fn allocate_buffer<W>(size: usize) -> *mut u8 {
|
||||
let words_to_allocate = divide_rounding_up(size, mem::size_of::<W>());
|
||||
let mut vec = Vec::<W>::with_capacity(words_to_allocate);
|
||||
vec.set_len(words_to_allocate);
|
||||
Box::into_raw(vec.into_boxed_slice()) as *mut W as *mut u8
|
||||
}
|
||||
}
|
||||
|
||||
/// Header data with an inline length. Consumers that use HeaderWithLength as the
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue