Skip to content

Commit 3726081

Browse files
committed
Add helper function to fetch libc constants
1 parent 79b1f91 commit 3726081

File tree

2 files changed

+14
-35
lines changed

2 files changed

+14
-35
lines changed

src/shims/foreign_items.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -949,6 +949,14 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
949949
}
950950
return Ok(None);
951951
}
952+
953+
fn eval_libc_i32(&mut self, name: &str) -> InterpResult<'tcx, i32> {
954+
self
955+
.eval_context_mut()
956+
.eval_path_scalar(&["libc", name])?
957+
.ok_or_else(|| err_unsup_format!("Path libc::{} cannot be resolved.", name).into())
958+
.and_then(|scalar| scalar.to_i32())
959+
}
952960
}
953961

954962
// Shims the linux 'getrandom()' syscall.

src/shims/io.rs

Lines changed: 6 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
2727

2828
let flag = this.read_scalar(flag_op)?.to_i32()?;
2929

30-
if flag
31-
!= this
32-
.eval_path_scalar(&["libc", "O_RDONLY"])?
33-
.unwrap()
34-
.to_i32()?
35-
&& flag
36-
!= this
37-
.eval_path_scalar(&["libc", "O_CLOEXEC"])?
38-
.unwrap()
39-
.to_i32()?
40-
{
30+
if flag != this.eval_libc_i32("O_RDONLY")? && flag != this.eval_libc_i32("O_CLOEXEC")? {
4131
throw_unsup_format!("Unsupported flag {:#x}", flag);
4232
}
4333

@@ -78,28 +68,15 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
7868
let fd = this.read_scalar(fd_op)?.to_i32()?;
7969
let cmd = this.read_scalar(cmd_op)?.to_i32()?;
8070

81-
if cmd
82-
== this
83-
.eval_path_scalar(&["libc", "F_SETFD"])?
84-
.unwrap()
85-
.to_i32()?
86-
{
71+
if cmd == this.eval_libc_i32("F_SETFD")? {
8772
let flag = this.read_scalar(arg_op.unwrap())?.to_i32()?;
8873
this.machine.file_handler.flags.insert(fd, flag);
8974
Ok(0)
90-
} else if cmd
91-
== this
92-
.eval_path_scalar(&["libc", "F_GETFD"])?
93-
.unwrap()
94-
.to_i32()?
95-
{
75+
} else if cmd == this.eval_libc_i32("F_GETFD")? {
9676
if let Some(flag) = this.machine.file_handler.flags.get(&fd) {
9777
Ok(*flag)
9878
} else {
99-
this.machine.last_error = this
100-
.eval_path_scalar(&["libc", "EBADF"])?
101-
.unwrap()
102-
.to_u32()?;
79+
this.machine.last_error = this.eval_libc_i32("EBADF")? as u32;
10380
Ok(-1)
10481
}
10582
} else {
@@ -125,10 +102,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
125102
}
126103
}
127104
} else {
128-
this.machine.last_error = this
129-
.eval_path_scalar(&["libc", "EBADF"])?
130-
.unwrap()
131-
.to_u32()?;
105+
this.machine.last_error = this.eval_libc_i32("EBADF")? as u32;
132106
Ok(-1)
133107
}
134108
}
@@ -169,10 +143,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
169143
}
170144
}
171145
} else {
172-
this.machine.last_error = this
173-
.eval_path_scalar(&["libc", "EBADF"])?
174-
.unwrap()
175-
.to_u32()?;
146+
this.machine.last_error = this.eval_libc_i32("EBADF")? as u32;
176147
Ok(-1)
177148
}
178149
}

0 commit comments

Comments
 (0)