Bug 1500362 - Make GkAtoms opaque to avoid lld-link.exe errors r=emilio

Depends on D15078

Differential Revision: https://phabricator.services.mozilla.com/D15800

--HG--
extra : moz-landing-system : lando
This commit is contained in:
Cameron McCormack 2019-01-07 09:50:25 +00:00
parent 81b00f453b
commit b05708ce89
3 changed files with 36 additions and 3 deletions

View File

@ -167,6 +167,7 @@ whitelist-vars = [
"nsContentUtils_.*",
"GECKO_IS_NIGHTLY",
"mozilla::detail::gGkAtoms",
"mozilla::detail::kGkAtomsArrayOffset",
]
whitelist-types = [
"RawGecko.*",
@ -377,6 +378,7 @@ opaque-types = [
"mozilla::dom::.*Callback", # Pulls in ErrorResult and other things that
# don't align properly on Linux 32-bit
"mozilla::SchedulerGroup", # Non-standard-layout packing of field into superclass
"mozilla::detail::GkAtoms", # https://bugzilla.mozilla.org/show_bug.cgi?id=1517685
]
# All cbindgen-types are in mod "structs::root::mozilla".

View File

@ -15,7 +15,9 @@ use crate::gecko_bindings::bindings::Gecko_Atomize;
use crate::gecko_bindings::bindings::Gecko_Atomize16;
use crate::gecko_bindings::bindings::Gecko_ReleaseAtom;
use crate::gecko_bindings::structs::{nsAtom, nsDynamicAtom, nsStaticAtom};
use crate::gecko_bindings::structs::root::mozilla::detail::GkAtoms_Atoms_AtomsCount;
use crate::gecko_bindings::structs::root::mozilla::detail::gGkAtoms;
use crate::gecko_bindings::structs::root::mozilla::detail::kGkAtomsArrayOffset;
use nsstring::{nsAString, nsStr};
use precomputed_hash::PrecomputedHash;
use std::borrow::{Borrow, Cow};
@ -57,11 +59,32 @@ pub struct Atom(usize);
/// where `'a` is the lifetime of something that holds a strong reference to that atom.
pub struct WeakAtom(nsAtom);
/// The number of static atoms we have.
const STATIC_ATOM_COUNT: usize = GkAtoms_Atoms_AtomsCount as usize;
/// Returns the Gecko static atom array.
///
/// We have this rather than use rust-bindgen to generate
/// mozilla::detail::gGkAtoms and then just reference gGkAtoms.mAtoms, so we
/// avoid a problem with lld-link.exe on Windows.
///
/// https://bugzilla.mozilla.org/show_bug.cgi?id=1517685
#[inline]
fn static_atoms() -> &'static [nsStaticAtom; STATIC_ATOM_COUNT] {
unsafe {
let addr = &gGkAtoms as *const _ as usize + kGkAtomsArrayOffset as usize;
&*(addr as *const _)
}
}
/// Returns whether the specified address points to one of the nsStaticAtom
/// objects in the Gecko static atom array.
#[inline]
fn valid_static_atom_addr(addr: usize) -> bool {
unsafe {
let start = gGkAtoms.mAtoms.get_unchecked(0) as *const _;
let end = gGkAtoms.mAtoms.get_unchecked(gGkAtoms.mAtoms.len()) as *const _;
let atoms = static_atoms();
let start = atoms.get_unchecked(0) as *const _;
let end = atoms.get_unchecked(STATIC_ATOM_COUNT) as *const _;
let in_range = addr >= start as usize && addr < end as usize;
let aligned = addr % mem::align_of::<nsStaticAtom>() == 0;
in_range && aligned
@ -341,7 +364,7 @@ impl Atom {
/// checking in release builds.
#[inline]
pub unsafe fn from_index(index: u16) -> Self {
let ptr = gGkAtoms.mAtoms.get_unchecked(index as usize) as *const _;
let ptr = static_atoms().get_unchecked(index as usize) as *const _;
let handle = make_static_handle(ptr);
let atom = Atom(handle);
debug_assert!(valid_static_atom_addr(ptr as usize));

View File

@ -109,6 +109,14 @@ struct GkAtoms {
const nsStaticAtom mAtoms[static_cast<size_t>(Atoms::AtomsCount)];
};
// The offset from the start of the GkAtoms object to the start of the
// nsStaticAtom array inside it. This is used in Rust to avoid problems
// with lld-link.exe on Windows when rust-bindgen generates a non-opaque
// version of GkAtoms.
//
// https://bugzilla.mozilla.org/show_bug.cgi?id=1517685
const ptrdiff_t kGkAtomsArrayOffset = offsetof(GkAtoms, mAtoms);
// The GkAtoms instance is `extern const` so it can be defined in a .cpp file.
//
// XXX: The NS_EXTERNAL_VIS is necessary to work around an apparent GCC bug: