Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix reproducible builds due to how derives were populated #455

Merged
merged 1 commit into from
Dec 11, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 52 additions & 15 deletions core/build/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
callbacks::{IntKind, ParseCallbacks},
Builder, EnumVariation,
};
use std::collections::HashSet;
use std::collections::BTreeSet;
use std::{env, path::PathBuf};

static DEFAULT_SGX_SDK_PATH: &str = "/opt/intel/sgxsdk";
Expand Down Expand Up @@ -206,43 +206,47 @@
.extend(serialize_types.into_iter().map(ToString::to_string));
self
}
}

impl ParseCallbacks for SgxParseCallbacks {
fn item_name(&self, name: &str) -> Option<String> {
normalize_item_name(name)
}

fn add_derives(&self, info: &DeriveInfo<'_>) -> Vec<String> {
let mut attributes = HashSet::new();
let name = info.name;
/// Get the derives that should be applied to the provided named type.
fn derives(&self, type_name: &str) -> Vec<String> {
let mut attributes = BTreeSet::new();

if self.default_types.iter().any(|n| *n == name) {
if self.default_types.iter().any(|n| *n == type_name) {
attributes.insert("Default");
}

// The [enum_types] method adds enums to the [copyable_types]
if self.copyable_types.iter().any(|n| *n == name) {
if self.copyable_types.iter().any(|n| *n == type_name) {
attributes.insert("Copy");
}

if !self.dynamically_sized_types.iter().any(|n| *n == name) {
if !self.dynamically_sized_types.iter().any(|n| *n == type_name) {
// For dynamically sized types we don't even derive Debug, because
// they are often times packed and packed types can't derive Debug
// without deriving Copy, however by the dynamic nature one can't
// derive Copy
attributes.insert("Debug");
if !self.enum_types.iter().any(|n| *n == name) {
if !self.enum_types.iter().any(|n| *n == type_name) {
attributes.extend(["Eq", "PartialEq", "Hash", "Clone"]);
}
};

if self.serialize_types.iter().any(|n| *n == name) {
if self.serialize_types.iter().any(|n| *n == type_name) {
attributes.extend(["Serialize", "Deserialize"]);
}

attributes.into_iter().map(String::from).collect::<Vec<_>>()
}
}

impl ParseCallbacks for SgxParseCallbacks {
fn item_name(&self, name: &str) -> Option<String> {
normalize_item_name(name)
}

Check warning on line 245 in core/build/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

core/build/src/lib.rs#L243-L245

Added lines #L243 - L245 were not covered by tests

fn add_derives(&self, info: &DeriveInfo<'_>) -> Vec<String> {
self.derives(info.name)
}

Check warning on line 249 in core/build/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

core/build/src/lib.rs#L247-L249

Added lines #L247 - L249 were not covered by tests

fn int_macro(&self, name: &str, _value: i64) -> Option<IntKind> {
const USIZE_SUFFIXES: &[&str] = &["_SIZE", "_BYTES", "_IDX", "_COUNT"];
Expand Down Expand Up @@ -328,3 +332,36 @@
_ => "",
}
}

#[cfg(test)]
mod test {
use super::*;

#[test]
fn verify_consistency_of_derive_attributes() {
// This test prevents a regression where `HashSet` was used to prevent
// duplicates in the derives. Using `HashSet` resulted in the derives
// coming back in varying order based on seed of the default hasher.
// The seed of the default hasher is randomized per process run.
// This inconsistent ordering of the derives prevented reproducible
// builds as the derived implementations would move around in the
// resultant binary.
let mut callbacks = SgxParseCallbacks::default();
callbacks = callbacks.serialize_types(["foo"]);
let derives = callbacks.derives("foo");

let expected = [
"Clone",
"Debug",
"Deserialize",
"Eq",
"Hash",
"PartialEq",
"Serialize",
]
.into_iter()
.map(String::from)
.collect::<Vec<_>>();
assert_eq!(derives, expected);
}
}
Loading