Skip to content

Commit 06703ba

Browse files
committed
add working shim for environ
1 parent 7cdcdec commit 06703ba

File tree

3 files changed

+29
-1
lines changed

3 files changed

+29
-1
lines changed

src/eval.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,8 @@ pub fn create_ecx<'mir, 'tcx: 'mir>(
5858
),
5959
);
6060
// Complete initialization.
61-
MemoryExtra::init_extern_statics(&mut ecx)?;
6261
EnvVars::init(&mut ecx, config.excluded_env_vars);
62+
MemoryExtra::init_extern_statics(&mut ecx)?;
6363

6464
// Setup first stack-frame
6565
let main_instance = ty::Instance::mono(tcx, main_id);

src/machine.rs

+13
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@ pub struct MemoryExtra {
8080
/// The random number generator used for resolving non-determinism.
8181
/// Needs to be queried by ptr_to_int, hence needs interior mutability.
8282
pub(crate) rng: RefCell<StdRng>,
83+
/// The `AllocId` for the `environ` static.
84+
pub(crate) environ: Option<Scalar<Tag>>,
8385
}
8486

8587
impl MemoryExtra {
@@ -94,6 +96,7 @@ impl MemoryExtra {
9496
intptrcast: Default::default(),
9597
extern_statics: HashMap::default(),
9698
rng: RefCell::new(rng),
99+
environ: None,
97100
}
98101
}
99102

@@ -113,6 +116,16 @@ impl MemoryExtra {
113116
.extern_statics
114117
.insert("__cxa_thread_atexit_impl", place.ptr.assert_ptr().alloc_id)
115118
.unwrap_none();
119+
120+
// "environ"
121+
let layout = this.layout_of(this.tcx.types.usize)?;
122+
let place = this.allocate(layout, MiriMemoryKind::Machine.into());
123+
this.write_scalar(this.memory.extra.environ.unwrap(), place.into())?;
124+
this.memory
125+
.extra
126+
.extern_statics
127+
.insert("environ", place.ptr.assert_ptr().alloc_id)
128+
.unwrap_none();
116129
}
117130
_ => {} // No "extern statics" supported on this platform
118131
}

src/shims/env.rs

+15
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use std::ffi::{OsString, OsStr};
33
use std::env;
44

55
use crate::stacked_borrows::Tag;
6+
use crate::rustc_target::abi::LayoutOf;
67
use crate::*;
78

89
use rustc::ty::layout::Size;
@@ -20,15 +21,29 @@ impl EnvVars {
2021
ecx: &mut InterpCx<'mir, 'tcx, Evaluator<'tcx>>,
2122
excluded_env_vars: Vec<String>,
2223
) {
24+
let mut vars = Vec::new();
2325
if ecx.machine.communicate {
2426
for (name, value) in env::vars() {
2527
if !excluded_env_vars.contains(&name) {
2628
let var_ptr =
2729
alloc_env_var_as_c_str(name.as_ref(), value.as_ref(), ecx);
2830
ecx.machine.env_vars.map.insert(OsString::from(name), var_ptr);
31+
vars.push(var_ptr.into());
2932
}
3033
}
3134
}
35+
// Add the trailing null pointer
36+
vars.push(Scalar::from_int(0, ecx.pointer_size()));
37+
// Make an array with all these pointers inside Miri.
38+
let tcx = ecx.tcx;
39+
let environ_layout =
40+
ecx.layout_of(tcx.mk_array(tcx.mk_imm_ptr(tcx.types.u8), vars.len() as u64)).unwrap();
41+
let environ_place = ecx.allocate(environ_layout, MiriMemoryKind::Machine.into());
42+
for (idx, var) in vars.into_iter().enumerate() {
43+
let place = ecx.mplace_field(environ_place, idx as u64).unwrap();
44+
ecx.write_scalar(var, place.into()).unwrap();
45+
}
46+
ecx.memory.extra.environ = Some(environ_place.ptr.into());
3247
}
3348
}
3449

0 commit comments

Comments
 (0)