6 use rustc::ty::layout::{Align, LayoutOf, Size};
8 impl<'mir, 'tcx> EvalContextExt<'mir, 'tcx> for crate::MiriEvalContext<'mir, 'tcx> {}
9 pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx> {
10 fn emulate_foreign_item_by_name(
13 args: &[OpTy<'tcx, Tag>],
14 dest: PlaceTy<'tcx, Tag>,
16 ) -> InterpResult<'tcx, bool> {
17 let this = self.eval_context_mut();
18 let tcx = &{ this.tcx.tcx };
21 // Environment related shims
23 let result = this.getenv(args[0])?;
24 this.write_scalar(result, dest)?;
28 let result = this.unsetenv(args[0])?;
29 this.write_scalar(Scalar::from_int(result, dest.layout.size), dest)?;
33 let result = this.setenv(args[0], args[1])?;
34 this.write_scalar(Scalar::from_int(result, dest.layout.size), dest)?;
38 let result = this.getcwd(args[0], args[1])?;
39 this.write_scalar(result, dest)?;
43 let result = this.chdir(args[0])?;
44 this.write_scalar(Scalar::from_int(result, dest.layout.size), dest)?;
48 "open" | "open64" => {
49 let result = this.open(args[0], args[1])?;
50 this.write_scalar(Scalar::from_int(result, dest.layout.size), dest)?;
54 let result = this.fcntl(args[0], args[1], args.get(2).cloned())?;
55 this.write_scalar(Scalar::from_int(result, dest.layout.size), dest)?;
59 let result = this.read(args[0], args[1], args[2])?;
60 this.write_scalar(Scalar::from_int(result, dest.layout.size), dest)?;
64 let fd = this.read_scalar(args[0])?.to_i32()?;
65 let buf = this.read_scalar(args[1])?.not_undef()?;
66 let n = this.read_scalar(args[2])?.to_machine_usize(tcx)?;
67 trace!("Called write({:?}, {:?}, {:?})", fd, buf, n);
68 let result = if fd == 1 || fd == 2 {
70 use std::io::{self, Write};
72 let buf_cont = this.memory.read_bytes(buf, Size::from_bytes(n))?;
73 // We need to flush to make sure this actually appears on the screen
74 let res = if fd == 1 {
75 // Stdout is buffered, flush to make sure it appears on the screen.
76 // This is the write() syscall of the interpreted program, we want it
77 // to correspond to a write() syscall on the host -- there is no good
78 // in adding extra buffering here.
79 let res = io::stdout().write(buf_cont);
80 io::stdout().flush().unwrap();
83 // No need to flush, stderr is not buffered.
84 io::stderr().write(buf_cont)
91 this.write(args[0], args[1], args[2])?
93 // Now, `result` is the value we return back to the program.
94 this.write_scalar(Scalar::from_int(result, dest.layout.size), dest)?;
98 let result = this.unlink(args[0])?;
99 this.write_scalar(Scalar::from_int(result, dest.layout.size), dest)?;
103 let result = this.symlink(args[0], args[1])?;
104 this.write_scalar(Scalar::from_int(result, dest.layout.size), dest)?;
108 let result = this.rename(args[0], args[1])?;
109 this.write_scalar(Scalar::from_int(result, dest.layout.size), dest)?;
113 let result = this.mkdir(args[0], args[1])?;
114 this.write_scalar(Scalar::from_int(result, dest.layout.size), dest)?;
118 let result = this.rmdir(args[0])?;
119 this.write_scalar(Scalar::from_int(result, dest.layout.size), dest)?;
122 "lseek" | "lseek64" => {
123 let result = this.lseek64(args[0], args[1], args[2])?;
124 this.write_scalar(Scalar::from_int(result, dest.layout.size), dest)?;
128 "posix_memalign" => {
129 let ret = this.deref_operand(args[0])?;
130 let align = this.read_scalar(args[1])?.to_machine_usize(this)?;
131 let size = this.read_scalar(args[2])?.to_machine_usize(this)?;
132 // Align must be power of 2, and also at least ptr-sized (POSIX rules).
133 if !align.is_power_of_two() {
134 throw_unsup!(HeapAllocNonPowerOfTwoAlignment(align));
136 if align < this.pointer_size().bytes() {
138 "posix_memalign: alignment must be at least the size of a pointer, but is {}",
144 this.write_null(ret.into())?;
146 let ptr = this.memory.allocate(
147 Size::from_bytes(size),
148 Align::from_bytes(align).unwrap(),
149 MiriMemoryKind::C.into(),
151 this.write_scalar(ptr, ret.into())?;
153 this.write_null(dest)?;
157 let _handle = this.read_scalar(args[0])?;
158 let symbol = this.read_scalar(args[1])?.not_undef()?;
159 let symbol_name = this.memory.read_c_str(symbol)?;
160 let err = format!("bad c unicode symbol: {:?}", symbol_name);
161 let symbol_name = ::std::str::from_utf8(symbol_name).unwrap_or(&err);
162 if let Some(dlsym) = Dlsym::from_str(symbol_name)? {
163 let ptr = this.memory.create_fn_alloc(FnVal::Other(dlsym));
164 this.write_scalar(Scalar::from(ptr), dest)?;
166 this.write_null(dest)?;
170 // Hook pthread calls that go to the thread-local storage memory subsystem.
171 "pthread_key_create" => {
172 let key_place = this.deref_operand(args[0])?;
174 // Extract the function type out of the signature (that seems easier than constructing it ourselves).
175 let dtor = match this.test_null(this.read_scalar(args[1])?.not_undef()?)? {
176 Some(dtor_ptr) => Some(this.memory.get_fn(dtor_ptr)?.as_instance()?),
180 // Figure out how large a pthread TLS key actually is.
181 // This is `libc::pthread_key_t`.
182 let key_type = args[0].layout.ty
184 .ok_or_else(|| err_ub_format!(
185 "wrong signature used for `pthread_key_create`: first argument must be a raw pointer."
188 let key_layout = this.layout_of(key_type)?;
190 // Create key and write it into the memory where `key_ptr` wants it.
191 let key = this.machine.tls.create_tls_key(dtor) as u128;
192 if key_layout.size.bits() < 128 && key >= (1u128 << key_layout.size.bits() as u128)
194 throw_unsup!(OutOfTls);
197 this.write_scalar(Scalar::from_uint(key, key_layout.size), key_place.into())?;
199 // Return success (`0`).
200 this.write_null(dest)?;
202 "pthread_key_delete" => {
203 let key = this.read_scalar(args[0])?.to_bits(args[0].layout.size)?;
204 this.machine.tls.delete_tls_key(key)?;
205 // Return success (0)
206 this.write_null(dest)?;
208 "pthread_getspecific" => {
209 let key = this.read_scalar(args[0])?.to_bits(args[0].layout.size)?;
210 let ptr = this.machine.tls.load_tls(key, tcx)?;
211 this.write_scalar(ptr, dest)?;
213 "pthread_setspecific" => {
214 let key = this.read_scalar(args[0])?.to_bits(args[0].layout.size)?;
215 let new_ptr = this.read_scalar(args[1])?.not_undef()?;
216 this.machine.tls.store_tls(key, this.test_null(new_ptr)?)?;
218 // Return success (`0`).
219 this.write_null(dest)?;
222 // Stack size/address stuff.
223 | "pthread_attr_init"
224 | "pthread_attr_destroy"
226 | "pthread_attr_setstacksize" => {
227 this.write_null(dest)?;
229 "pthread_attr_getstack" => {
230 let addr_place = this.deref_operand(args[1])?;
231 let size_place = this.deref_operand(args[2])?;
234 Scalar::from_uint(STACK_ADDR, addr_place.layout.size),
238 Scalar::from_uint(STACK_SIZE, size_place.layout.size),
242 // Return success (`0`).
243 this.write_null(dest)?;
246 // We don't support threading.
247 "pthread_create" => {
248 throw_unsup_format!("Miri does not support threading");
251 // Stub out calls for condvar, mutex and rwlock, to just return `0`.
252 | "pthread_mutexattr_init"
253 | "pthread_mutexattr_settype"
254 | "pthread_mutex_init"
255 | "pthread_mutexattr_destroy"
256 | "pthread_mutex_lock"
257 | "pthread_mutex_unlock"
258 | "pthread_mutex_destroy"
259 | "pthread_rwlock_rdlock"
260 | "pthread_rwlock_unlock"
261 | "pthread_rwlock_wrlock"
262 | "pthread_rwlock_destroy"
263 | "pthread_condattr_init"
264 | "pthread_condattr_setclock"
265 | "pthread_cond_init"
266 | "pthread_condattr_destroy"
267 | "pthread_cond_destroy"
269 this.write_null(dest)?;
272 // We don't support fork so we don't have to do anything for atfork.
273 "pthread_atfork" => {
274 this.write_null(dest)?;
277 // Some things needed for `sys::thread` initialization to go through.
282 this.write_scalar(Scalar::from_int(0, dest.layout.size), dest)?;
286 let name = this.read_scalar(args[0])?.to_i32()?;
288 trace!("sysconf() called with name {}", name);
289 // TODO: Cache the sysconf integers via Miri's global cache.
291 (&["libc", "_SC_PAGESIZE"], Scalar::from_int(PAGE_SIZE, dest.layout.size)),
292 (&["libc", "_SC_GETPW_R_SIZE_MAX"], Scalar::from_int(-1, dest.layout.size)),
294 &["libc", "_SC_NPROCESSORS_ONLN"],
295 Scalar::from_int(NUM_CPUS, dest.layout.size),
298 let mut result = None;
299 for &(path, path_value) in paths {
300 if let Some(val) = this.eval_path_scalar(path)? {
301 let val = val.to_i32()?;
303 result = Some(path_value);
308 if let Some(result) = result {
309 this.write_scalar(result, dest)?;
311 throw_unsup_format!("Unimplemented sysconf name: {}", name)
316 this.write_null(dest)?;
320 // fadvise is only informational, we can ignore it.
321 this.write_null(dest)?;
325 // This is a horrible hack, but since the guard page mechanism calls mmap and expects a particular return value, we just give it that value.
326 let addr = this.read_scalar(args[0])?.not_undef()?;
327 this.write_scalar(addr, dest)?;
331 this.write_null(dest)?;
335 match this.tcx.sess.target.target.target_os.as_str() {
336 "linux" => return linux::EvalContextExt::emulate_foreign_item_by_name(this, link_name, args, dest, ret),
337 "macos" => return macos::EvalContextExt::emulate_foreign_item_by_name(this, link_name, args, dest, ret),