Skip to content

Commit d13fe01

Browse files
committed
add working shim for environ
1 parent c72af45 commit d13fe01

File tree

3 files changed

+30
-1
lines changed

3 files changed

+30
-1
lines changed

src/eval.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,8 @@ pub fn create_ecx<'mir, 'tcx: 'mir>(
7777
),
7878
);
7979
// Complete initialization.
80-
MemoryExtra::init_extern_statics(&mut ecx)?;
8180
EnvVars::init(&mut ecx, config.excluded_env_vars);
81+
MemoryExtra::init_extern_statics(&mut ecx)?;
8282

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

src/machine.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,9 @@ pub struct MemoryExtra {
8484
/// An allocation ID to report when it is being allocated
8585
/// (helps for debugging memory leaks).
8686
tracked_alloc_id: Option<AllocId>,
87+
88+
/// The `AllocId` for the `environ` static.
89+
pub(crate) environ: Option<Scalar<Tag>>,
8790
}
8891

8992
impl MemoryExtra {
@@ -99,6 +102,7 @@ impl MemoryExtra {
99102
extern_statics: FxHashMap::default(),
100103
rng: RefCell::new(rng),
101104
tracked_alloc_id,
105+
environ: None,
102106
}
103107
}
104108

@@ -118,6 +122,16 @@ impl MemoryExtra {
118122
.extern_statics
119123
.insert(Symbol::intern("__cxa_thread_atexit_impl"), place.ptr.assert_ptr().alloc_id)
120124
.unwrap_none();
125+
126+
// "environ"
127+
let layout = this.layout_of(this.tcx.types.usize)?;
128+
let place = this.allocate(layout, MiriMemoryKind::Machine.into());
129+
this.write_scalar(this.memory.extra.environ.unwrap(), place.into())?;
130+
this.memory
131+
.extra
132+
.extern_statics
133+
.insert(Symbol::intern("environ"), place.ptr.assert_ptr().alloc_id)
134+
.unwrap_none();
121135
}
122136
_ => {} // No "extern statics" supported on this platform
123137
}

src/shims/env.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use std::ffi::{OsString, OsStr};
22
use std::env;
33

44
use crate::stacked_borrows::Tag;
5+
use crate::rustc_target::abi::LayoutOf;
56
use crate::*;
67

78
use rustc_data_structures::fx::FxHashMap;
@@ -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)