Skip to content

Commit 8392a0c

Browse files
committed
only expose environ on linux
1 parent 18a71ef commit 8392a0c

File tree

5 files changed

+26
-25
lines changed

5 files changed

+26
-25
lines changed

src/eval.rs

+1-1
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

+10-16
Original file line numberDiff line numberDiff line change
@@ -112,29 +112,23 @@ impl<'tcx> MemoryExtra<'tcx> {
112112
) -> InterpResult<'tcx> {
113113
let target_os = this.tcx.sess.target.target.target_os.as_str();
114114
match target_os {
115-
"linux" | "macos" => {
116-
if target_os == "linux" {
117-
// "__cxa_thread_atexit_impl"
118-
// This should be all-zero, pointer-sized.
119-
let layout = this.layout_of(this.tcx.types.usize)?;
120-
let place = this.allocate(layout, MiriMemoryKind::Machine.into());
121-
this.write_scalar(Scalar::from_machine_usize(0, &*this.tcx), place.into())?;
122-
this.memory
123-
.extra
124-
.extern_statics
125-
.insert(Symbol::intern("__cxa_thread_atexit_impl"), place.ptr.assert_ptr().alloc_id)
126-
.unwrap_none();
127-
}
128-
// "environ"
115+
"linux" => {
116+
// "__cxa_thread_atexit_impl"
117+
// This should be all-zero, pointer-sized.
129118
let layout = this.layout_of(this.tcx.types.usize)?;
130119
let place = this.allocate(layout, MiriMemoryKind::Machine.into());
131120
this.write_scalar(Scalar::from_machine_usize(0, &*this.tcx), place.into())?;
132121
this.memory
133122
.extra
134123
.extern_statics
135-
.insert(Symbol::intern("environ"), place.ptr.assert_ptr().alloc_id)
124+
.insert(Symbol::intern("__cxa_thread_atexit_impl"), place.ptr.assert_ptr().alloc_id)
125+
.unwrap_none();
126+
// "environ"
127+
this.memory
128+
.extra
129+
.extern_statics
130+
.insert(Symbol::intern("environ"), this.memory.extra.environ.unwrap().ptr.assert_ptr().alloc_id)
136131
.unwrap_none();
137-
this.memory.extra.environ = Some(place);
138132
}
139133
_ => {} // No "extern statics" supported on this platform
140134
}

src/shims/env.rs

+7-2
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ impl EnvVars {
3030
}
3131
}
3232
}
33+
// Initialize the `environ` static
34+
let layout = ecx.layout_of(ecx.tcx.types.usize)?;
35+
let place = ecx.allocate(layout, MiriMemoryKind::Machine.into());
36+
ecx.write_scalar(Scalar::from_machine_usize(0, &*ecx.tcx), place.into())?;
37+
ecx.memory.extra.environ = Some(place);
3338
ecx.update_environ()
3439
}
3540
}
@@ -156,12 +161,12 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
156161
}
157162

158163
/// Updates the `environ` static. It should not be called before
159-
/// `MemoryExtra::init_extern_statics`.
164+
/// `EnvVars::init`.
160165
fn update_environ(&mut self) -> InterpResult<'tcx> {
161166
let this = self.eval_context_mut();
162167
// Deallocate the old environ value.
163168
let old_vars_ptr = this.read_scalar(this.memory.extra.environ.unwrap().into())?.not_undef()?;
164-
// The pointer itself can be null because `MemoryExtra::init_extern_statics` only
169+
// The pointer itself can be null because `EnvVars::init` only
165170
// initializes the place for the static but not the static itself.
166171
if !this.is_null(old_vars_ptr)? {
167172
this.memory.deallocate(this.force_ptr(old_vars_ptr)?, None, MiriMemoryKind::Machine.into())?;

src/shims/foreign_items/posix/macos.rs

+6-4
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
4141
let result = this.macos_fstat(args[0], args[1])?;
4242
this.write_scalar(Scalar::from_int(result, dest.layout.size), dest)?;
4343
}
44-
// Environment related shims
45-
"_NSGetEnviron" => {
46-
this.write_scalar(this.memory.extra.environ.unwrap().ptr, dest)?;
47-
}
44+
4845
// The only reason this is not in the `posix` module is because the `linux` item has a
4946
// different name.
5047
"opendir$INODE64" => {
@@ -59,6 +56,11 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
5956
this.write_scalar(Scalar::from_int(result, dest.layout.size), dest)?;
6057
}
6158

59+
// Environment related shims
60+
"_NSGetEnviron" => {
61+
this.write_scalar(this.memory.extra.environ.unwrap().ptr, dest)?;
62+
}
63+
6264
// Time related shims
6365
"gettimeofday" => {
6466
let result = this.gettimeofday(args[0], args[1])?;

tests/compile-fail/environ-gets-deallocated.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
#[cfg(target_os="linux")]
44
fn get_environ() -> *const *const u8 {
55
extern "C" {
6-
static environ: *const *const u8;
6+
static mut environ: *const *const u8;
77
}
8-
environ
8+
unsafe { environ }
99
}
1010

1111
#[cfg(target_os="macos")]

0 commit comments

Comments
 (0)