Skip to content

Commit aee8f17

Browse files
committed
Delegate writing to emulate_foreign_item
1 parent 41f8cfa commit aee8f17

File tree

2 files changed

+29
-27
lines changed

2 files changed

+29
-27
lines changed

src/shims/env.rs

Lines changed: 15 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -52,34 +52,28 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
5252
fn getenv(
5353
&mut self,
5454
name_op: OpTy<'tcx, Tag>,
55-
dest: PlaceTy<'tcx, Tag>
56-
) -> InterpResult<'tcx> {
55+
) -> InterpResult<'tcx, Scalar<Tag>> {
5756
let this = self.eval_context_mut();
5857

59-
let result = {
60-
let name_ptr = this.read_scalar(name_op)?.not_undef()?;
61-
let name = this.memory().read_c_str(name_ptr)?;
62-
match this.machine.env_vars.map.get(name) {
63-
Some(&var) => Scalar::Ptr(var),
64-
None => Scalar::ptr_null(&*this.tcx),
65-
}
66-
};
67-
this.write_scalar(result, dest)?;
68-
Ok(())
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+
})
6964
}
7065

7166
fn setenv(
7267
&mut self,
7368
name_op: OpTy<'tcx, Tag>,
7469
value_op: OpTy<'tcx, Tag>,
75-
dest: PlaceTy<'tcx, Tag>
76-
) -> InterpResult<'tcx> {
70+
) -> InterpResult<'tcx, i32> {
7771
let this = self.eval_context_mut();
7872

79-
let mut new = None;
8073
let name_ptr = this.read_scalar(name_op)?.not_undef()?;
8174
let value_ptr = this.read_scalar(value_op)?.not_undef()?;
8275
let value = this.memory().read_c_str(value_ptr)?;
76+
let mut new = None;
8377
if !this.is_null(name_ptr)? {
8478
let name = this.memory().read_c_str(name_ptr)?;
8579
if !name.is_empty() && !name.contains(&b'=') {
@@ -91,22 +85,20 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
9185
if let Some(var) = this.machine.env_vars.map.insert(name.to_owned(), value_copy) {
9286
this.memory_mut().deallocate(var, None, MiriMemoryKind::Env.into())?;
9387
}
94-
this.write_null(dest)?;
88+
Ok(0)
9589
} else {
96-
this.write_scalar(Scalar::from_int(-1, dest.layout.size), dest)?;
90+
Ok(-1)
9791
}
98-
Ok(())
9992
}
10093

10194
fn unsetenv(
10295
&mut self,
10396
name_op: OpTy<'tcx, Tag>,
104-
dest: PlaceTy<'tcx, Tag>
105-
) -> InterpResult<'tcx> {
97+
) -> InterpResult<'tcx, i32> {
10698
let this = self.eval_context_mut();
10799

108-
let mut success = None;
109100
let name_ptr = this.read_scalar(name_op)?.not_undef()?;
101+
let mut success = None;
110102
if !this.is_null(name_ptr)? {
111103
let name = this.memory().read_c_str(name_ptr)?.to_owned();
112104
if !name.is_empty() && !name.contains(&b'=') {
@@ -117,10 +109,9 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
117109
if let Some(var) = old {
118110
this.memory_mut().deallocate(var, None, MiriMemoryKind::Env.into())?;
119111
}
120-
this.write_null(dest)?;
112+
Ok(0)
121113
} else {
122-
this.write_scalar(Scalar::from_int(-1, dest.layout.size), dest)?;
114+
Ok(-1)
123115
}
124-
Ok(())
125116
}
126117
}

src/shims/foreign_items.rs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -421,9 +421,20 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
421421
}
422422
}
423423

424-
"getenv" => this.getenv(args[0], dest)?,
425-
"unsetenv" => this.unsetenv(args[0], dest)?,
426-
"setenv" => this.setenv(args[0], args[1], dest)?,
424+
"getenv" => {
425+
let result = this.getenv(args[0])?;
426+
this.write_scalar(result, dest)?;
427+
}
428+
429+
"unsetenv" => {
430+
let result = this.unsetenv(args[0])?;
431+
this.write_scalar(Scalar::from_int(result, dest.layout.size), dest)?;
432+
}
433+
434+
"setenv" => {
435+
let result = this.setenv(args[0], args[1])?;
436+
this.write_scalar(Scalar::from_int(result, dest.layout.size), dest)?;
437+
}
427438

428439
"write" => {
429440
let fd = this.read_scalar(args[0])?.to_i32()?;

0 commit comments

Comments
 (0)