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

Implement Eq, Clone and Hash for MemoryData and Evaluator #387

Merged
merged 2 commits into from
Jul 13, 2018
Merged
Show file tree
Hide file tree
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
41 changes: 38 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,13 @@ use rustc::ty::subst::Subst;
use rustc::hir::def_id::DefId;
use rustc::mir;

use rustc_data_structures::fx::FxHasher;

use syntax::ast::Mutability;
use syntax::codemap::Span;

use std::collections::{HashMap, BTreeMap};
use std::hash::{Hash, Hasher};

pub use rustc::mir::interpret::*;
pub use rustc_mir::interpret::*;
Expand Down Expand Up @@ -296,7 +299,7 @@ pub fn eval_main<'a, 'tcx: 'a>(
}
}

#[derive(Default)]
#[derive(Clone, Default, PartialEq, Eq)]
pub struct Evaluator<'tcx> {
/// Environment variables set by `setenv`
/// Miri does not expose env vars from the host to the emulated program
Expand All @@ -306,15 +309,34 @@ pub struct Evaluator<'tcx> {
pub(crate) suspended: HashMap<DynamicLifetime, Vec<ValidationQuery<'tcx>>>,
}

impl<'tcx> Hash for Evaluator<'tcx> {
fn hash<H: Hasher>(&self, state: &mut H) {
let Evaluator {
env_vars,
suspended: _,
} = self;

env_vars.iter()
.map(|(env, ptr)| {
let mut h = FxHasher::default();
env.hash(&mut h);
ptr.hash(&mut h);
h.finish()
})
.fold(0u64, |acc, hash| acc.wrapping_add(hash))
.hash(state);
}
}

pub type TlsKey = u128;

#[derive(Copy, Clone, Debug)]
#[derive(Copy, Clone, Debug, Hash, PartialEq, Eq)]
pub struct TlsEntry<'tcx> {
data: Scalar, // Will eventually become a map from thread IDs to `Scalar`s, if we ever support more than one thread.
dtor: Option<ty::Instance<'tcx>>,
}

#[derive(Default)]
#[derive(Clone, Default, PartialEq, Eq)]
pub struct MemoryData<'tcx> {
/// The Key to use for the next thread-local allocation.
next_thread_local: TlsKey,
Expand All @@ -331,6 +353,19 @@ pub struct MemoryData<'tcx> {
statics: HashMap<GlobalId<'tcx>, AllocId>,
}

impl<'tcx> Hash for MemoryData<'tcx> {
fn hash<H: Hasher>(&self, state: &mut H) {
let MemoryData {
next_thread_local: _,
thread_local,
locks: _,
statics: _,
} = self;

thread_local.hash(state);
}
}

impl<'mir, 'tcx: 'mir> Machine<'mir, 'tcx> for Evaluator<'tcx> {
type MemoryData = MemoryData<'tcx>;
type MemoryKinds = memory::MemoryKind;
Expand Down
2 changes: 1 addition & 1 deletion src/locks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use rustc::ty::layout::Size;
////////////////////////////////////////////////////////////////////////////////

/// Information about a lock that is currently held.
#[derive(Clone, Debug)]
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct LockInfo<'tcx> {
/// Stores for which lifetimes (of the original write lock) we got
/// which suspensions.
Expand Down
2 changes: 1 addition & 1 deletion src/range_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use std::collections::BTreeMap;
use std::ops;

#[derive(Clone, Debug)]
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct RangeMap<T> {
map: BTreeMap<Range, T>,
}
Expand Down