forked from rust-lang/miri
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinux.rs
156 lines (138 loc) · 6.51 KB
/
linux.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
use crate::*;
use rustc_middle::mir;
impl<'mir, 'tcx: 'mir> EvalContextExt<'mir, 'tcx> for crate::MiriEvalContext<'mir, 'tcx> {}
pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx> {
fn emulate_foreign_item_by_name(
&mut self,
link_name: &str,
args: &[OpTy<'tcx, Tag>],
dest: PlaceTy<'tcx, Tag>,
_ret: mir::BasicBlock,
) -> InterpResult<'tcx, bool> {
let this = self.eval_context_mut();
match link_name {
// errno
"__errno_location" => {
let errno_place = this.machine.last_error.unwrap();
this.write_scalar(errno_place.to_ref().to_scalar()?, dest)?;
}
// File related shims (but also see "syscall" below for statx)
// These symbols have different names on Linux and macOS, which is the only reason they are not
// in the `posix` module.
"close" => {
let result = this.close(args[0])?;
this.write_scalar(Scalar::from_i32(result), dest)?;
}
"opendir" => {
let result = this.opendir(args[0])?;
this.write_scalar(result, dest)?;
}
"readdir64_r" => {
let result = this.linux_readdir64_r(args[0], args[1], args[2])?;
this.write_scalar(Scalar::from_i32(result), dest)?;
}
"ftruncate64" => {
let result = this.ftruncate64(args[0], args[1])?;
this.write_scalar(Scalar::from_i32(result), dest)?;
}
// Linux-only
"posix_fadvise" => {
let _fd = this.read_scalar(args[0])?.to_i32()?;
let _offset = this.read_scalar(args[1])?.to_machine_isize(this)?;
let _len = this.read_scalar(args[2])?.to_machine_isize(this)?;
let _advice = this.read_scalar(args[3])?.to_i32()?;
// fadvise is only informational, we can ignore it.
this.write_null(dest)?;
}
// Time related shims
"clock_gettime" => {
// This is a POSIX function but it has only been tested on linux.
let result = this.clock_gettime(args[0], args[1])?;
this.write_scalar(Scalar::from_i32(result), dest)?;
}
// Querying system information
"pthread_attr_getstack" => {
// We don't support "pthread_attr_setstack", so we just pretend all stacks have the same values here.
let _attr_place = this.deref_operand(args[0])?;
let addr_place = this.deref_operand(args[1])?;
let size_place = this.deref_operand(args[2])?;
this.write_scalar(
Scalar::from_uint(STACK_ADDR, this.pointer_size()),
addr_place.into(),
)?;
this.write_scalar(
Scalar::from_uint(STACK_SIZE, this.pointer_size()),
size_place.into(),
)?;
// Return success (`0`).
this.write_null(dest)?;
}
// Threading
"prctl" => {
assert_eq!(args.len(), 5);
let result = this.prctl(args[0], args[1], args[2], args[3], args[4])?;
this.write_scalar(Scalar::from_i32(result), dest)?;
}
// Dynamically invoked syscalls
"syscall" => {
let sys_getrandom = this
.eval_libc("SYS_getrandom")?
.to_machine_usize(this)?;
let sys_statx = this
.eval_libc("SYS_statx")?
.to_machine_usize(this)?;
match this.read_scalar(args[0])?.to_machine_usize(this)? {
// `libc::syscall(NR_GETRANDOM, buf.as_mut_ptr(), buf.len(), GRND_NONBLOCK)`
// is called if a `HashMap` is created the regular way (e.g. HashMap<K, V>).
id if id == sys_getrandom => {
// The first argument is the syscall id, so skip over it.
getrandom(this, &args[1..], dest)?;
}
// `statx` is used by `libstd` to retrieve metadata information on `linux`
// instead of using `stat`,`lstat` or `fstat` as on `macos`.
id if id == sys_statx => {
// The first argument is the syscall id, so skip over it.
let result = this.linux_statx(args[1], args[2], args[3], args[4], args[5])?;
this.write_scalar(Scalar::from_machine_isize(result.into(), this), dest)?;
}
id => throw_unsup_format!("miri does not support syscall ID {}", id),
}
}
// Miscelanneous
"getrandom" => {
getrandom(this, args, dest)?;
}
"sched_getaffinity" => {
let _pid = this.read_scalar(args[0])?.to_i32()?;
let _cpusetsize = this.read_scalar(args[1])?.to_machine_usize(this)?;
let _mask = this.deref_operand(args[2])?;
// FIXME: we just return an error; `num_cpus` then falls back to `sysconf`.
let einval = this.eval_libc("EINVAL")?;
this.set_last_error(einval)?;
this.write_scalar(Scalar::from_i32(-1), dest)?;
}
// Incomplete shims that we "stub out" just to get pre-main initialization code to work.
// These shims are enabled only when the caller is in the standard library.
"pthread_getattr_np" if this.frame().instance.to_string().starts_with("std::sys::unix::") => {
this.write_null(dest)?;
}
_ => throw_unsup_format!("can't call foreign function: {}", link_name),
};
Ok(true)
}
}
// Shims the linux `getrandom` syscall.
fn getrandom<'tcx>(
this: &mut MiriEvalContext<'_, 'tcx>,
args: &[OpTy<'tcx, Tag>],
dest: PlaceTy<'tcx, Tag>,
) -> InterpResult<'tcx> {
let ptr = this.read_scalar(args[0])?.not_undef()?;
let len = this.read_scalar(args[1])?.to_machine_usize(this)?;
// The only supported flags are GRND_RANDOM and GRND_NONBLOCK,
// neither of which have any effect on our current PRNG.
let _flags = this.read_scalar(args[2])?.to_i32()?;
this.gen_random(ptr, len)?;
this.write_scalar(Scalar::from_machine_usize(len, this), dest)?;
Ok(())
}