Skip to content

Commit

Permalink
style: Fix LengthPercentage on big-endian machines.
Browse files Browse the repository at this point in the history
Always store the pointer in little-endian order so that the tag trick works.

Differential Revision: https://phabricator.services.mozilla.com/D61386
  • Loading branch information
emilio committed Feb 12, 2020
1 parent 071ce6f commit d5b49f1
Showing 1 changed file with 40 additions and 19 deletions.
59 changes: 40 additions & 19 deletions components/style/values/computed/length_percentage.rs
Expand Up @@ -42,15 +42,15 @@ use style_traits::{CssWriter, ToCss};
#[derive(Clone, Copy)]
#[repr(C)]
pub struct LengthVariant {
tag: u32,
tag: u8,
length: Length,
}

#[doc(hidden)]
#[derive(Clone, Copy)]
#[repr(C)]
pub struct PercentageVariant {
tag: u32,
tag: u8,
percentage: Percentage,
}

Expand All @@ -61,7 +61,7 @@ pub struct PercentageVariant {
#[repr(C)]
#[cfg(target_pointer_width = "32")]
pub struct CalcVariant {
tag: u32,
tag: u8,
ptr: *mut CalcLengthPercentage,
}

Expand All @@ -70,7 +70,7 @@ pub struct CalcVariant {
#[repr(C)]
#[cfg(target_pointer_width = "64")]
pub struct CalcVariant {
ptr: *mut CalcLengthPercentage,
ptr: usize, // In little-endian byte order
}

// `CalcLengthPercentage` is `Send + Sync` as asserted below.
Expand All @@ -81,7 +81,7 @@ unsafe impl Sync for CalcVariant {}
#[derive(Clone, Copy)]
#[repr(C)]
pub struct TagVariant {
tag: u32,
tag: u8,
}

/// A `<length-percentage>` value. This can be either a `<length>`, a
Expand Down Expand Up @@ -114,17 +114,17 @@ pub union LengthPercentageUnion {

impl LengthPercentageUnion {
#[doc(hidden)] // Need to be public so that cbindgen generates it.
pub const TAG_CALC: u32 = 0;
pub const TAG_CALC: u8 = 0;
#[doc(hidden)]
pub const TAG_LENGTH: u32 = 1;
pub const TAG_LENGTH: u8 = 1;
#[doc(hidden)]
pub const TAG_PERCENTAGE: u32 = 2;
pub const TAG_PERCENTAGE: u8 = 2;
#[doc(hidden)]
pub const TAG_MASK: u32 = 0b11;
pub const TAG_MASK: u8 = 0b11;
}

#[derive(Clone, Copy, Debug, PartialEq)]
#[repr(u32)]
#[repr(u8)]
enum Tag {
Calc = LengthPercentageUnion::TAG_CALC,
Length = LengthPercentageUnion::TAG_LENGTH,
Expand All @@ -147,7 +147,7 @@ unsafe fn static_assert() {
impl Drop for LengthPercentage {
fn drop(&mut self) {
if self.tag() == Tag::Calc {
let _ = unsafe { Box::from_raw(self.0.calc.ptr) };
let _ = unsafe { Box::from_raw(self.calc_ptr()) };
}
}
}
Expand Down Expand Up @@ -236,13 +236,22 @@ impl LengthPercentage {
/// checking.
fn new_calc_unchecked(calc: Box<CalcLengthPercentage>) -> Self {
let ptr = Box::into_raw(calc);
let calc = Self(LengthPercentageUnion {
calc: CalcVariant {
#[cfg(target_pointer_width = "32")]
tag: LengthPercentageUnion::TAG_CALC,
ptr,
},
});

#[cfg(target_pointer_width = "32")]
let calc = CalcVariant {
tag: LengthPercentageUnion::TAG_CALC,
ptr,
};

#[cfg(target_pointer_width = "64")]
let calc = CalcVariant {
#[cfg(target_endian = "little")]
ptr: ptr as usize,
#[cfg(target_endian = "big")]
ptr: (ptr as usize).swap_bytes(),
};

let calc = Self(LengthPercentageUnion { calc });
debug_assert_eq!(calc.tag(), Tag::Calc);
calc
}
Expand All @@ -261,13 +270,25 @@ impl LengthPercentage {
fn unpack<'a>(&'a self) -> Unpacked<'a> {
unsafe {
match self.tag() {
Tag::Calc => Unpacked::Calc(&*self.0.calc.ptr),
Tag::Calc => Unpacked::Calc(&*self.calc_ptr()),
Tag::Length => Unpacked::Length(self.0.length.length),
Tag::Percentage => Unpacked::Percentage(self.0.percentage.percentage),
}
}
}

#[inline]
unsafe fn calc_ptr(&self) -> *mut CalcLengthPercentage {
#[cfg(not(all(target_endian = "big", target_pointer_width = "64")))]
{
self.0.calc.ptr as *mut _
}
#[cfg(all(target_endian = "big", target_pointer_width = "64"))]
{
self.0.calc.ptr.swap_bytes() as *mut _
}
}

#[inline]
fn to_serializable(&self) -> Serializable {
match self.unpack() {
Expand Down

0 comments on commit d5b49f1

Please sign in to comment.