Skip to content

Commit 868da2a

Browse files
committed
Auto merge of #907 - christianpoveda:env-vars-shim, r=RalfJung
Move env shims to its own module r? @RalfJung
2 parents 1f504ea + aee8f17 commit 868da2a

File tree

4 files changed

+76
-63
lines changed

4 files changed

+76
-63
lines changed

src/eval.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ pub fn create_ecx<'mir, 'tcx: 'mir>(
3939
Evaluator::new(config.communicate),
4040
MemoryExtra::new(StdRng::seed_from_u64(config.seed.unwrap_or(0)), config.validate),
4141
);
42-
4342
// Complete initialization.
4443
EnvVars::init(&mut ecx, config.communicate);
4544

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ pub use crate::shims::foreign_items::EvalContextExt as ForeignItemsEvalContextEx
3333
pub use crate::shims::intrinsics::EvalContextExt as IntrinsicsEvalContextExt;
3434
pub use crate::shims::tls::{EvalContextExt as TlsEvalContextExt, TlsData};
3535
pub use crate::shims::dlsym::{Dlsym, EvalContextExt as DlsymEvalContextExt};
36-
pub use crate::shims::env::EnvVars;
36+
pub use crate::shims::env::{EnvVars, EvalContextExt as EnvEvalContextExt};
3737
pub use crate::operator::EvalContextExt as OperatorEvalContextExt;
3838
pub use crate::range_map::RangeMap;
3939
pub use crate::helpers::{EvalContextExt as HelpersEvalContextExt};

src/shims/env.rs

Lines changed: 70 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,21 +22,9 @@ impl EnvVars {
2222
}
2323
}
2424
}
25-
26-
pub(crate) fn get(&self, name: &[u8]) -> Option<&Pointer<Tag>> {
27-
self.map.get(name)
28-
}
29-
30-
pub(crate) fn unset(&mut self, name: &[u8]) -> Option<Pointer<Tag>> {
31-
self.map.remove(name)
32-
}
33-
34-
pub(crate) fn set(&mut self, name: Vec<u8>, ptr: Pointer<Tag>) -> Option<Pointer<Tag>>{
35-
self.map.insert(name, ptr)
36-
}
3725
}
3826

39-
pub(crate) fn alloc_env_value<'mir, 'tcx>(
27+
fn alloc_env_value<'mir, 'tcx>(
4028
bytes: &[u8],
4129
memory: &mut Memory<'mir, 'tcx, Evaluator<'tcx>>,
4230
) -> Pointer<Tag> {
@@ -58,3 +46,72 @@ pub(crate) fn alloc_env_value<'mir, 'tcx>(
5846
alloc.write_bytes(&tcx, trailing_zero_ptr, &[0]).unwrap();
5947
ptr
6048
}
49+
50+
impl<'mir, 'tcx> EvalContextExt<'mir, 'tcx> for crate::MiriEvalContext<'mir, 'tcx> {}
51+
pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx> {
52+
fn getenv(
53+
&mut self,
54+
name_op: OpTy<'tcx, Tag>,
55+
) -> InterpResult<'tcx, Scalar<Tag>> {
56+
let this = self.eval_context_mut();
57+
58+
let name_ptr = this.read_scalar(name_op)?.not_undef()?;
59+
let name = this.memory().read_c_str(name_ptr)?;
60+
Ok(match this.machine.env_vars.map.get(name) {
61+
Some(&var) => Scalar::Ptr(var),
62+
None => Scalar::ptr_null(&*this.tcx),
63+
})
64+
}
65+
66+
fn setenv(
67+
&mut self,
68+
name_op: OpTy<'tcx, Tag>,
69+
value_op: OpTy<'tcx, Tag>,
70+
) -> InterpResult<'tcx, i32> {
71+
let this = self.eval_context_mut();
72+
73+
let name_ptr = this.read_scalar(name_op)?.not_undef()?;
74+
let value_ptr = this.read_scalar(value_op)?.not_undef()?;
75+
let value = this.memory().read_c_str(value_ptr)?;
76+
let mut new = None;
77+
if !this.is_null(name_ptr)? {
78+
let name = this.memory().read_c_str(name_ptr)?;
79+
if !name.is_empty() && !name.contains(&b'=') {
80+
new = Some((name.to_owned(), value.to_owned()));
81+
}
82+
}
83+
if let Some((name, value)) = new {
84+
let value_copy = alloc_env_value(&value, this.memory_mut());
85+
if let Some(var) = this.machine.env_vars.map.insert(name.to_owned(), value_copy) {
86+
this.memory_mut().deallocate(var, None, MiriMemoryKind::Env.into())?;
87+
}
88+
Ok(0)
89+
} else {
90+
Ok(-1)
91+
}
92+
}
93+
94+
fn unsetenv(
95+
&mut self,
96+
name_op: OpTy<'tcx, Tag>,
97+
) -> InterpResult<'tcx, i32> {
98+
let this = self.eval_context_mut();
99+
100+
let name_ptr = this.read_scalar(name_op)?.not_undef()?;
101+
let mut success = None;
102+
if !this.is_null(name_ptr)? {
103+
let name = this.memory().read_c_str(name_ptr)?.to_owned();
104+
if !name.is_empty() && !name.contains(&b'=') {
105+
success = Some(this.machine.env_vars.map.remove(&name));
106+
}
107+
}
108+
if let Some(old) = success {
109+
if let Some(var) = old {
110+
this.memory_mut().deallocate(var, None, MiriMemoryKind::Env.into())?;
111+
}
112+
Ok(0)
113+
} else {
114+
Ok(-1)
115+
}
116+
}
117+
}

src/shims/foreign_items.rs

Lines changed: 5 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ use syntax::attr;
88
use syntax::symbol::sym;
99

1010
use crate::*;
11-
use crate::shims::env::alloc_env_value;
1211

1312
impl<'mir, 'tcx> EvalContextExt<'mir, 'tcx> for crate::MiriEvalContext<'mir, 'tcx> {}
1413
pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx> {
@@ -423,60 +422,18 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
423422
}
424423

425424
"getenv" => {
426-
let result = {
427-
let name_ptr = this.read_scalar(args[0])?.not_undef()?;
428-
let name = this.memory().read_c_str(name_ptr)?;
429-
match this.machine.env_vars.get(name) {
430-
Some(&var) => Scalar::Ptr(var),
431-
None => Scalar::ptr_null(&*this.tcx),
432-
}
433-
};
425+
let result = this.getenv(args[0])?;
434426
this.write_scalar(result, dest)?;
435427
}
436428

437429
"unsetenv" => {
438-
let mut success = None;
439-
{
440-
let name_ptr = this.read_scalar(args[0])?.not_undef()?;
441-
if !this.is_null(name_ptr)? {
442-
let name = this.memory().read_c_str(name_ptr)?.to_owned();
443-
if !name.is_empty() && !name.contains(&b'=') {
444-
success = Some(this.machine.env_vars.unset(&name));
445-
}
446-
}
447-
}
448-
if let Some(old) = success {
449-
if let Some(var) = old {
450-
this.memory_mut().deallocate(var, None, MiriMemoryKind::Env.into())?;
451-
}
452-
this.write_null(dest)?;
453-
} else {
454-
this.write_scalar(Scalar::from_int(-1, dest.layout.size), dest)?;
455-
}
430+
let result = this.unsetenv(args[0])?;
431+
this.write_scalar(Scalar::from_int(result, dest.layout.size), dest)?;
456432
}
457433

458434
"setenv" => {
459-
let mut new = None;
460-
{
461-
let name_ptr = this.read_scalar(args[0])?.not_undef()?;
462-
let value_ptr = this.read_scalar(args[1])?.not_undef()?;
463-
let value = this.memory().read_c_str(value_ptr)?;
464-
if !this.is_null(name_ptr)? {
465-
let name = this.memory().read_c_str(name_ptr)?;
466-
if !name.is_empty() && !name.contains(&b'=') {
467-
new = Some((name.to_owned(), value.to_owned()));
468-
}
469-
}
470-
}
471-
if let Some((name, value)) = new {
472-
let value_copy = alloc_env_value(&value, this.memory_mut());
473-
if let Some(var) = this.machine.env_vars.set(name.to_owned(), value_copy) {
474-
this.memory_mut().deallocate(var, None, MiriMemoryKind::Env.into())?;
475-
}
476-
this.write_null(dest)?;
477-
} else {
478-
this.write_scalar(Scalar::from_int(-1, dest.layout.size), dest)?;
479-
}
435+
let result = this.setenv(args[0], args[1])?;
436+
this.write_scalar(Scalar::from_int(result, dest.layout.size), dest)?;
480437
}
481438

482439
"write" => {

0 commit comments

Comments
 (0)