зеркало из https://github.com/mozilla/gecko-dev.git
Bug 1804361
- Update memoffset to 0.7.1. r=emilio,supply-chain-reviewers
Differential Revision: https://phabricator.services.mozilla.com/D164013
This commit is contained in:
Родитель
e664dcb83d
Коммит
ff048ff15c
|
@ -390,7 +390,7 @@ dependencies = [
|
|||
"libc",
|
||||
"libudev",
|
||||
"log",
|
||||
"memoffset 0.6.5",
|
||||
"memoffset 0.6.99",
|
||||
"nom 7.1.1",
|
||||
"nss-gk-api",
|
||||
"pkcs11-bindings",
|
||||
|
@ -1042,7 +1042,7 @@ dependencies = [
|
|||
"autocfg",
|
||||
"cfg-if 1.0.0",
|
||||
"crossbeam-utils 0.8.11",
|
||||
"memoffset 0.6.5",
|
||||
"memoffset 0.6.99",
|
||||
"once_cell",
|
||||
"scopeguard",
|
||||
]
|
||||
|
@ -3274,9 +3274,16 @@ dependencies = [
|
|||
|
||||
[[package]]
|
||||
name = "memoffset"
|
||||
version = "0.6.5"
|
||||
version = "0.6.99"
|
||||
dependencies = [
|
||||
"memoffset 0.7.1",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "memoffset"
|
||||
version = "0.7.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "5aa361d4faea93603064a027415f07bd8e1d5c88c9fbf68bf56a285428fd79ce"
|
||||
checksum = "5de893c32cde5f383baa4c04c5d6dbdd735cfd4a794b0debdb2bb1b421da5ff4"
|
||||
dependencies = [
|
||||
"autocfg",
|
||||
]
|
||||
|
|
|
@ -129,6 +129,9 @@ hashbrown = { path = "build/rust/hashbrown" }
|
|||
# Patch bindgen 0.59 to 0.63
|
||||
bindgen = { path = "build/rust/bindgen" }
|
||||
|
||||
# Patch memoffset 0.6 to 0.7
|
||||
memoffset = { path = "build/rust/memoffset" }
|
||||
|
||||
# Patch autocfg to hide rustc output. Workaround for https://github.com/cuviper/autocfg/issues/30
|
||||
autocfg = { path = "third_party/rust/autocfg" }
|
||||
|
||||
|
|
|
@ -0,0 +1,11 @@
|
|||
[package]
|
||||
name = "memoffset"
|
||||
version = "0.6.99"
|
||||
edition = "2018"
|
||||
license = "MPL-2.0"
|
||||
|
||||
[lib]
|
||||
path = "lib.rs"
|
||||
|
||||
[dependencies.memoffset]
|
||||
version = "0.7"
|
|
@ -0,0 +1,5 @@
|
|||
/* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
pub use memoffset::*;
|
|
@ -804,6 +804,11 @@ who = "Mike Hommey <mh+mozilla@glandium.org>"
|
|||
criteria = "safe-to-deploy"
|
||||
delta = "0.5.4 -> 0.5.7"
|
||||
|
||||
[[audits.memoffset]]
|
||||
who = "Gabriele Svelto <gsvelto@mozilla.com>"
|
||||
criteria = "safe-to-deploy"
|
||||
delta = "0.6.5 -> 0.7.1"
|
||||
|
||||
[[audits.metal]]
|
||||
who = "Jim Blandy <jimb@red-bean.com>"
|
||||
criteria = "safe-to-deploy"
|
||||
|
|
|
@ -1 +1 @@
|
|||
{"files":{"Cargo.toml":"2556143c764ef2315fe44ff0ec43af47ca70b260fd64aa53f57dc42760d7132d","LICENSE":"3234ac55816264ee7b6c7ee27efd61cf0a1fe775806870e3d9b4c41ea73c5cb1","README.md":"299c8957a769bac2a71f9c63064c58a8b54e613e3bf03d41a889f0b428eb4a9c","build.rs":"6d677e33a1c98d588c97ec7985d4d5c3b954683e0a73c3dc53d79db4fbb5e638","ci/miri.sh":"ad7410b0a5bd6e346f55e9d96ec0719a085a2d1ce266bddfe6fe73333a1eb8ec","src/lib.rs":"e7976d295371a3c1e0cf31b0d50210cd6b1135caba3a5111403a97ec6175c0a2","src/offset_of.rs":"e5e3062947f61418cb48220a0d17604411b5ce651b5945c06b876f26c50220b2","src/raw_field.rs":"295cc971d64e51f3d053e56c692ae6ef3bb58915298f1ec49bb695b767daff46","src/span_of.rs":"03bfb743c2dee2e5fbb9568bcbbe9f24ace3e18488a6924d26812fcb9e3c425a"},"package":"5aa361d4faea93603064a027415f07bd8e1d5c88c9fbf68bf56a285428fd79ce"}
|
||||
{"files":{"Cargo.toml":"2122b76e5dff09497c7edf7f184155e456e44209c05e4f8abb01632be7241b56","LICENSE":"3234ac55816264ee7b6c7ee27efd61cf0a1fe775806870e3d9b4c41ea73c5cb1","README.md":"7a7935d96a1a40b56afeadca391c742f7ac3a6e0f1deab1d43430553f71b6d23","build.rs":"6d677e33a1c98d588c97ec7985d4d5c3b954683e0a73c3dc53d79db4fbb5e638","src/lib.rs":"e7976d295371a3c1e0cf31b0d50210cd6b1135caba3a5111403a97ec6175c0a2","src/offset_of.rs":"ea04e76e3ab1fa192618fffb0c6a047795c275f1deaf6c6617245badaba8660c","src/raw_field.rs":"ef54087d5f507c2b639a4f61f2881eb1e41a46e22191ffd0e23b2fe9e3f17c25","src/span_of.rs":"b900faef2b852b52c37c55a172c05c9144bfff7d84dbc06e943fb0453d68adfc"},"package":"5de893c32cde5f383baa4c04c5d6dbdd735cfd4a794b0debdb2bb1b421da5ff4"}
|
|
@ -11,16 +11,23 @@
|
|||
|
||||
[package]
|
||||
name = "memoffset"
|
||||
version = "0.6.5"
|
||||
version = "0.7.1"
|
||||
authors = ["Gilad Naaman <gilad.naaman@gmail.com>"]
|
||||
description = "offset_of functionality for Rust structs."
|
||||
readme = "README.md"
|
||||
keywords = ["mem", "offset", "offset_of", "offsetof"]
|
||||
keywords = [
|
||||
"mem",
|
||||
"offset",
|
||||
"offset_of",
|
||||
"offsetof",
|
||||
]
|
||||
categories = ["no-std"]
|
||||
license = "MIT"
|
||||
repository = "https://github.com/Gilnaa/memoffset"
|
||||
|
||||
[dev-dependencies.doc-comment]
|
||||
version = "0.3"
|
||||
|
||||
[build-dependencies.autocfg]
|
||||
version = "1"
|
||||
|
||||
|
|
|
@ -16,7 +16,7 @@ Add the following dependency to your `Cargo.toml`:
|
|||
|
||||
```toml
|
||||
[dependencies]
|
||||
memoffset = "0.6"
|
||||
memoffset = "0.7"
|
||||
```
|
||||
|
||||
These versions will compile fine with rustc versions greater or equal to 1.19.
|
||||
|
@ -55,7 +55,7 @@ In order to use it, you must enable the `unstable_const` crate feature and sever
|
|||
Cargo.toml:
|
||||
```toml
|
||||
[dependencies.memoffset]
|
||||
version = "0.6"
|
||||
version = "0.7"
|
||||
features = ["unstable_const"]
|
||||
```
|
||||
|
||||
|
|
|
@ -1,14 +0,0 @@
|
|||
set -ex
|
||||
|
||||
# Install Miri.
|
||||
MIRI_NIGHTLY=nightly-$(curl -s https://rust-lang.github.io/rustup-components-history/x86_64-unknown-linux-gnu/miri)
|
||||
echo "Installing latest nightly with Miri: $MIRI_NIGHTLY"
|
||||
rustup default "$MIRI_NIGHTLY"
|
||||
rustup component add miri
|
||||
|
||||
# Run tests.
|
||||
cargo miri test
|
||||
cargo miri test --all-features
|
||||
|
||||
# Restore old state in case Travis uses this cache for other jobs.
|
||||
rustup default nightly
|
|
@ -86,6 +86,16 @@ macro_rules! _memoffset_offset_from_unsafe {
|
|||
/// assert_eq!(offset_of!(Foo, b), 4);
|
||||
/// }
|
||||
/// ```
|
||||
///
|
||||
/// ## Notes
|
||||
/// Rust's ABI is unstable, and [type layout can be changed with each
|
||||
/// compilation](https://doc.rust-lang.org/reference/type-layout.html).
|
||||
///
|
||||
/// Using `offset_of!` with a `repr(Rust)` struct will return the correct offset of the
|
||||
/// specified `field` for a particular compilation, but the exact value may change
|
||||
/// based on the compiler version, concrete struct type, time of day, or rustc's mood.
|
||||
///
|
||||
/// As a result, the value should not be retained and used between different compilations.
|
||||
#[macro_export(local_inner_macros)]
|
||||
macro_rules! offset_of {
|
||||
($parent:path, $field:tt) => {{
|
||||
|
@ -121,6 +131,39 @@ macro_rules! offset_of_tuple {
|
|||
}};
|
||||
}
|
||||
|
||||
/// Calculates the offset of the specified union member from the start of the union.
|
||||
///
|
||||
/// ## Examples
|
||||
/// ```
|
||||
/// use memoffset::offset_of_union;
|
||||
///
|
||||
/// #[repr(C, packed)]
|
||||
/// union Foo {
|
||||
/// foo32: i32,
|
||||
/// foo64: i64,
|
||||
/// }
|
||||
///
|
||||
/// fn main() {
|
||||
/// assert!(offset_of_union!(Foo, foo64) == 0);
|
||||
/// }
|
||||
/// ```
|
||||
///
|
||||
/// ## Note
|
||||
/// Due to macro_rules limitations, this macro will accept structs with a single field as well as unions.
|
||||
/// This is not a stable guarantee, and future versions of this crate might fail
|
||||
/// on any use of this macro with a struct, without a semver bump.
|
||||
#[macro_export(local_inner_macros)]
|
||||
macro_rules! offset_of_union {
|
||||
($parent:path, $field:tt) => {{
|
||||
// Get a base pointer (non-dangling if rustc supports `MaybeUninit`).
|
||||
_memoffset__let_base_ptr!(base_ptr, $parent);
|
||||
// Get field pointer.
|
||||
let field_ptr = raw_field_union!(base_ptr, $parent, $field);
|
||||
// Compute offset.
|
||||
_memoffset_offset_from_unsafe!(field_ptr, base_ptr)
|
||||
}};
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
#[test]
|
||||
|
@ -161,6 +204,21 @@ mod tests {
|
|||
assert_eq!(offset_of!(Tup, 1), 4);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn offset_union() {
|
||||
// Since we're specifying repr(C), all fields are supposed to be at offset 0
|
||||
#[repr(C)]
|
||||
union Foo {
|
||||
a: u32,
|
||||
b: [u8; 2],
|
||||
c: i64,
|
||||
}
|
||||
|
||||
assert_eq!(offset_of_union!(Foo, a), 0);
|
||||
assert_eq!(offset_of_union!(Foo, b), 0);
|
||||
assert_eq!(offset_of_union!(Foo, c), 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn path() {
|
||||
mod sub {
|
||||
|
@ -238,6 +296,22 @@ mod tests {
|
|||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_raw_field_union() {
|
||||
#[repr(C)]
|
||||
union Foo {
|
||||
a: u32,
|
||||
b: [u8; 2],
|
||||
c: i64,
|
||||
}
|
||||
|
||||
let f = Foo { a: 0 };
|
||||
let f_ptr = &f as *const _;
|
||||
assert_eq!(f_ptr as usize + 0, raw_field_union!(f_ptr, Foo, a) as usize);
|
||||
assert_eq!(f_ptr as usize + 0, raw_field_union!(f_ptr, Foo, b) as usize);
|
||||
assert_eq!(f_ptr as usize + 0, raw_field_union!(f_ptr, Foo, c) as usize);
|
||||
}
|
||||
|
||||
#[cfg(feature = "unstable_const")]
|
||||
#[test]
|
||||
fn const_offset() {
|
||||
|
|
|
@ -39,6 +39,21 @@ macro_rules! _memoffset__addr_of {
|
|||
}
|
||||
|
||||
/// Deref-coercion protection macro.
|
||||
///
|
||||
/// Prevents complilation if the specified field name is not a part of the
|
||||
/// struct definition.
|
||||
///
|
||||
/// ```compile_fail
|
||||
/// use memoffset::_memoffset__field_check;
|
||||
///
|
||||
/// struct Foo {
|
||||
/// foo: i32,
|
||||
/// }
|
||||
///
|
||||
/// type BoxedFoo = Box<Foo>;
|
||||
///
|
||||
/// _memoffset__field_check!(BoxedFoo, foo);
|
||||
/// ```
|
||||
#[cfg(allow_clippy)]
|
||||
#[macro_export]
|
||||
#[doc(hidden)]
|
||||
|
@ -64,6 +79,25 @@ macro_rules! _memoffset__field_check {
|
|||
}
|
||||
|
||||
/// Deref-coercion protection macro.
|
||||
///
|
||||
/// Prevents complilation if the specified type is not a tuple.
|
||||
///
|
||||
/// ```compile_fail
|
||||
/// use memoffset::_memoffset__field_check_tuple;
|
||||
///
|
||||
/// _memoffset__field_check_tuple!(i32, 0);
|
||||
/// ```
|
||||
#[cfg(allow_clippy)]
|
||||
#[macro_export]
|
||||
#[doc(hidden)]
|
||||
macro_rules! _memoffset__field_check_tuple {
|
||||
($type:ty, $field:tt) => {
|
||||
// Make sure the type argument is a tuple
|
||||
#[allow(clippy::unneeded_wildcard_pattern)]
|
||||
let (_, ..): $type;
|
||||
};
|
||||
}
|
||||
#[cfg(not(allow_clippy))]
|
||||
#[macro_export]
|
||||
#[doc(hidden)]
|
||||
macro_rules! _memoffset__field_check_tuple {
|
||||
|
@ -73,6 +107,53 @@ macro_rules! _memoffset__field_check_tuple {
|
|||
};
|
||||
}
|
||||
|
||||
/// Deref-coercion protection macro for unions.
|
||||
/// Unfortunately accepts single-field structs as well, which is not ideal,
|
||||
/// but ultimately pretty harmless.
|
||||
///
|
||||
/// ```compile_fail
|
||||
/// use memoffset::_memoffset__field_check_union;
|
||||
///
|
||||
/// union Foo {
|
||||
/// variant_a: i32,
|
||||
/// }
|
||||
///
|
||||
/// type BoxedFoo = Box<Foo>;
|
||||
///
|
||||
/// _memoffset__field_check_union!(BoxedFoo, variant_a);
|
||||
/// ```
|
||||
#[cfg(allow_clippy)]
|
||||
#[macro_export]
|
||||
#[doc(hidden)]
|
||||
macro_rules! _memoffset__field_check_union {
|
||||
($type:path, $field:tt) => {
|
||||
// Make sure the field actually exists. This line ensures that a
|
||||
// compile-time error is generated if $field is accessed through a
|
||||
// Deref impl.
|
||||
#[allow(clippy::unneeded_wildcard_pattern)]
|
||||
// rustc1.19 requires unsafe here for the pattern; not needed in newer versions
|
||||
#[allow(unused_unsafe)]
|
||||
unsafe {
|
||||
let $type { $field: _ };
|
||||
}
|
||||
};
|
||||
}
|
||||
#[cfg(not(allow_clippy))]
|
||||
#[macro_export]
|
||||
#[doc(hidden)]
|
||||
macro_rules! _memoffset__field_check_union {
|
||||
($type:path, $field:tt) => {
|
||||
// Make sure the field actually exists. This line ensures that a
|
||||
// compile-time error is generated if $field is accessed through a
|
||||
// Deref impl.
|
||||
// rustc1.19 requires unsafe here for the pattern; not needed in newer versions
|
||||
#[allow(unused_unsafe)]
|
||||
unsafe {
|
||||
let $type { $field: _ };
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/// Computes a const raw pointer to the given field of the given base pointer
|
||||
/// to the given parent type.
|
||||
///
|
||||
|
@ -115,3 +196,31 @@ macro_rules! raw_field_tuple {
|
|||
}
|
||||
}};
|
||||
}
|
||||
|
||||
/// Computes a const raw pointer to the given field of the given base pointer
|
||||
/// to the given parent tuple typle.
|
||||
///
|
||||
/// The `base` pointer *must not* be dangling, but it *may* point to
|
||||
/// uninitialized memory.
|
||||
///
|
||||
/// ## Note
|
||||
/// This macro is the same as `raw_field`, except for a different Deref-coercion check that
|
||||
/// supports unions.
|
||||
/// Due to macro_rules limitations, this check will accept structs with a single field as well as unions.
|
||||
/// This is not a stable guarantee, and future versions of this crate might fail
|
||||
/// on any use of this macro with a struct, without a semver bump.
|
||||
#[macro_export(local_inner_macros)]
|
||||
macro_rules! raw_field_union {
|
||||
($base:expr, $parent:path, $field:tt) => {{
|
||||
_memoffset__field_check_union!($parent, $field);
|
||||
let base = $base; // evaluate $base outside the `unsafe` block
|
||||
|
||||
// Get the field address.
|
||||
// Crucially, we know that this will not trigger a deref coercion because
|
||||
// of the field check we did above.
|
||||
#[allow(unused_unsafe)] // for when the macro is used in an unsafe block
|
||||
unsafe {
|
||||
_memoffset__addr_of!((*(base as *const $parent)).$field)
|
||||
}
|
||||
}};
|
||||
}
|
||||
|
|
|
@ -52,11 +52,18 @@ macro_rules! _memoffset__compile_error {
|
|||
/// span_of!(Struct, start ..)
|
||||
/// ```
|
||||
///
|
||||
/// *Note*:
|
||||
/// ### Note
|
||||
/// This macro uses recursion in order to resolve the range expressions, so there is a limit to
|
||||
/// the complexity of the expression.
|
||||
/// In order to raise the limit, the compiler's recursion limit should be lifted.
|
||||
///
|
||||
/// ### Safety
|
||||
/// The inter-field form mentioned above assumes that the first field is positioned before the
|
||||
/// second.
|
||||
/// This is only guarenteed for `repr(C)` structs.
|
||||
/// Usage with `repr(Rust)` structs may yield unexpected results, like downward-going ranges,
|
||||
/// spans that include unexpected fields, empty spans, or spans that include *unexpected* padding bytes.
|
||||
///
|
||||
/// ## Examples
|
||||
/// ```
|
||||
/// use memoffset::span_of;
|
||||
|
|
Загрузка…
Ссылка в новой задаче