Skip to content

Commit 666cd22

Browse files
committed
Wrap hashmap for env vars in its own type
1 parent 253af96 commit 666cd22

File tree

4 files changed

+42
-14
lines changed

4 files changed

+42
-14
lines changed

src/eval.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,17 @@ use crate::{
1313
Scalar, Tag, Pointer, FnVal,
1414
MemoryExtra, MiriMemoryKind, Evaluator, TlsEvalContextExt, HelpersEvalContextExt,
1515
};
16-
use crate::shims::env::alloc_env_value;
16+
use crate::shims::env::EnvVars;
1717

1818
/// Configuration needed to spawn a Miri instance.
1919
#[derive(Clone)]
2020
pub struct MiriConfig {
21+
/// Determine if validity checking and Stacked Borrows are enabled.
2122
pub validate: bool,
2223
/// Determines if communication with the host environment is enabled.
2324
pub communicate: bool,
2425
pub args: Vec<String>,
25-
26-
/// The seed to use when non-determinism is required (e.g. getrandom())
26+
/// The seed to use when non-determinism or randomness are required (e.g. ptr-to-int cast, `getrandom()`).
2727
pub seed: Option<u64>,
2828
}
2929

@@ -165,10 +165,7 @@ pub fn create_ecx<'mir, 'tcx: 'mir>(
165165
assert!(args.next().is_none(), "start lang item has more arguments than expected");
166166

167167
if config.communicate {
168-
for (name, value) in std::env::vars() {
169-
let value = alloc_env_value(value.as_bytes(), ecx.memory_mut(), &tcx);
170-
ecx.machine.env_vars.insert(name.into_bytes(), value);
171-
}
168+
EnvVars::init(&mut ecx, &tcx);
172169
}
173170

174171
Ok(ecx)

src/machine.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
44
use std::rc::Rc;
55
use std::borrow::Cow;
6-
use std::collections::HashMap;
76
use std::cell::RefCell;
87

98
use rand::rngs::StdRng;
@@ -15,6 +14,7 @@ use rustc::ty::{self, layout::{Size, LayoutOf}, TyCtxt};
1514
use rustc::mir;
1615

1716
use crate::*;
17+
use crate::shims::env::EnvVars;
1818

1919
// Some global facts about the emulated machine.
2020
pub const PAGE_SIZE: u64 = 4*1024; // FIXME: adjust to target architecture
@@ -79,7 +79,7 @@ impl MemoryExtra {
7979
pub struct Evaluator<'tcx> {
8080
/// Environment variables set by `setenv`.
8181
/// Miri does not expose env vars from the host to the emulated program.
82-
pub(crate) env_vars: HashMap<Vec<u8>, Pointer<Tag>>,
82+
pub(crate) env_vars: EnvVars,
8383

8484
/// Program arguments (`Option` because we can only initialize them after creating the ecx).
8585
/// These are *pointers* to argc/argv because macOS.
@@ -101,7 +101,7 @@ pub struct Evaluator<'tcx> {
101101
impl<'tcx> Evaluator<'tcx> {
102102
pub(crate) fn new(communicate: bool) -> Self {
103103
Evaluator {
104-
env_vars: HashMap::default(),
104+
env_vars: EnvVars::default(),
105105
argc: None,
106106
argv: None,
107107
cmd_line: None,

src/shims/env.rs

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,39 @@
1-
use rustc::ty::{layout::{Size, Align}, TyCtxt};
2-
use rustc_mir::interpret::Memory;
1+
use std::collections::HashMap;
32

3+
use rustc::ty::{layout::{Size, Align}, TyCtxt};
4+
use rustc_mir::interpret::{Pointer, Memory};
5+
use crate::stacked_borrows::Tag;
46
use crate::*;
57

8+
#[derive(Default)]
9+
pub struct EnvVars {
10+
map: HashMap<Vec<u8>, Pointer<Tag>>,
11+
}
12+
13+
impl EnvVars {
14+
pub(crate) fn init<'mir, 'tcx>(
15+
ecx: &mut InterpCx<'mir, 'tcx, Evaluator<'tcx>>,
16+
tcx: &TyCtxt<'tcx>,
17+
) {
18+
for (name, value) in std::env::vars() {
19+
let value = alloc_env_value(value.as_bytes(), ecx.memory_mut(), tcx);
20+
ecx.machine.env_vars.map.insert(name.into_bytes(), value);
21+
}
22+
}
23+
24+
pub(crate) fn get(&self, name: &[u8]) -> Option<&Pointer<Tag>> {
25+
self.map.get(name)
26+
}
27+
28+
pub(crate) fn unset(&mut self, name: &[u8]) -> Option<Pointer<Tag>> {
29+
self.map.remove(name)
30+
}
31+
32+
pub(crate) fn set(&mut self, name: Vec<u8>, ptr: Pointer<Tag>) -> Option<Pointer<Tag>>{
33+
self.map.insert(name, ptr)
34+
}
35+
}
36+
637
pub(crate) fn alloc_env_value<'mir, 'tcx>(
738
bytes: &[u8],
839
memory: &mut Memory<'mir, 'tcx, Evaluator<'tcx>>,

src/shims/foreign_items.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -438,7 +438,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
438438
if !this.is_null(name_ptr)? {
439439
let name = this.memory().read_c_str(name_ptr)?.to_owned();
440440
if !name.is_empty() && !name.contains(&b'=') {
441-
success = Some(this.machine.env_vars.remove(&name));
441+
success = Some(this.machine.env_vars.unset(&name));
442442
}
443443
}
444444
}
@@ -467,7 +467,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
467467
}
468468
if let Some((name, value)) = new {
469469
let value_copy = alloc_env_value(&value, this.memory_mut(), tcx);
470-
if let Some(var) = this.machine.env_vars.insert(name.to_owned(), value_copy) {
470+
if let Some(var) = this.machine.env_vars.set(name.to_owned(), value_copy) {
471471
this.memory_mut().deallocate(var, None, MiriMemoryKind::Env.into())?;
472472
}
473473
this.write_null(dest)?;

0 commit comments

Comments
 (0)