Skip to content

Commit b8329da

Browse files
authored
Merge pull request rust-lang#311 from RalfJung/memory
Memory API refactoring
2 parents 44a360d + 55eaf5e commit b8329da

File tree

19 files changed

+466
-547
lines changed

19 files changed

+466
-547
lines changed

Cargo.lock

Lines changed: 20 additions & 20 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

miri/bin/miri.rs

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,11 @@ use rustc::ty::TyCtxt;
1919
use syntax::ast::{self, MetaItemKind, NestedMetaItemKind};
2020
use std::path::PathBuf;
2121

22-
struct MiriCompilerCalls(RustcDefaultCalls);
22+
struct MiriCompilerCalls {
23+
default: RustcDefaultCalls,
24+
/// whether we are building for the host
25+
host_target: bool,
26+
}
2327

2428
impl<'a> CompilerCalls<'a> for MiriCompilerCalls {
2529
fn early_callback(
@@ -30,7 +34,7 @@ impl<'a> CompilerCalls<'a> for MiriCompilerCalls {
3034
descriptions: &rustc_errors::registry::Registry,
3135
output: ErrorOutputType,
3236
) -> Compilation {
33-
self.0.early_callback(
37+
self.default.early_callback(
3438
matches,
3539
sopts,
3640
cfg,
@@ -47,7 +51,7 @@ impl<'a> CompilerCalls<'a> for MiriCompilerCalls {
4751
ofile: &Option<PathBuf>,
4852
descriptions: &rustc_errors::registry::Registry,
4953
) -> Option<(Input, Option<PathBuf>)> {
50-
self.0.no_input(
54+
self.default.no_input(
5155
matches,
5256
sopts,
5357
cfg,
@@ -64,17 +68,17 @@ impl<'a> CompilerCalls<'a> for MiriCompilerCalls {
6468
odir: &Option<PathBuf>,
6569
ofile: &Option<PathBuf>,
6670
) -> Compilation {
67-
self.0.late_callback(matches, sess, input, odir, ofile)
71+
self.default.late_callback(matches, sess, input, odir, ofile)
6872
}
6973
fn build_controller(
7074
&mut self,
7175
sess: &Session,
7276
matches: &getopts::Matches,
7377
) -> CompileController<'a> {
74-
let mut control = self.0.build_controller(sess, matches);
78+
let mut control = self.default.build_controller(sess, matches);
7579
control.after_hir_lowering.callback = Box::new(after_hir_lowering);
7680
control.after_analysis.callback = Box::new(after_analysis);
77-
if std::env::var("MIRI_HOST_TARGET") != Ok("yes".to_owned()) {
81+
if !self.host_target {
7882
// only fully compile targets on the host
7983
control.after_analysis.stop = Compilation::Stop;
8084
}
@@ -254,6 +258,16 @@ fn main() {
254258

255259
// for auxilary builds in unit tests
256260
args.push("-Zalways-encode-mir".to_owned());
261+
let mut host_target = false;
262+
args.retain(|arg| if arg == "--miri_host_target" {
263+
host_target = true;
264+
false // remove the flag, rustc doesn't know it
265+
} else {
266+
true
267+
});
257268

258-
rustc_driver::run_compiler(&args, &mut MiriCompilerCalls(RustcDefaultCalls), None, None);
269+
rustc_driver::run_compiler(&args, &mut MiriCompilerCalls {
270+
default: RustcDefaultCalls,
271+
host_target,
272+
}, None, None);
259273
}

miri/fn_call.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -421,11 +421,11 @@ impl<'a, 'tcx> EvalContextExt<'tcx> for EvalContext<'a, 'tcx, super::Evaluator>
421421
if key_size.bits() < 128 && key >= (1u128 << key_size.bits() as u128) {
422422
return err!(OutOfTls);
423423
}
424-
// TODO: Does this need checking for alignment?
425-
self.memory.write_uint(
424+
self.memory.write_primval(
426425
key_ptr.to_ptr()?,
427-
key,
426+
PrimVal::Bytes(key),
428427
key_size.bytes(),
428+
false,
429429
)?;
430430

431431
// Return success (0)

miri/intrinsic.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use rustc::ty::layout::Layout;
44
use rustc::ty::{self, Ty};
55

66
use rustc_miri::interpret::{EvalResult, Lvalue, LvalueExtra, PrimVal, PrimValKind, Value, Pointer,
7-
HasMemory, EvalContext, PtrAndAlign, ValTy};
7+
HasMemory, AccessKind, EvalContext, PtrAndAlign, ValTy};
88

99
use helpers::EvalContextExt as HelperEvalContextExt;
1010

@@ -624,7 +624,7 @@ impl<'a, 'tcx> EvalContextExt<'tcx> for EvalContext<'a, 'tcx, super::Evaluator>
624624
if count > 0 {
625625
// HashMap relies on write_bytes on a NULL ptr with count == 0 to work
626626
// TODO: Should we, at least, validate the alignment? (Also see the copy intrinsic)
627-
self.memory.check_align(ptr, ty_align)?;
627+
self.memory.check_align(ptr, ty_align, Some(AccessKind::Write))?;
628628
self.memory.write_repeat(ptr, val_byte, size * count)?;
629629
}
630630
}

0 commit comments

Comments
 (0)