]> git.lizzy.rs Git - rust.git/blob - src/shims/foreign_items/posix/macos.rs
274248e8b54f7489f3bc14ccd4b69ff92a2a4f04
[rust.git] / src / shims / foreign_items / posix / macos.rs
1 use crate::*;
2 use rustc::mir;
3
4 impl<'mir, 'tcx> EvalContextExt<'mir, 'tcx> for crate::MiriEvalContext<'mir, 'tcx> {}
5 pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx> {
6     fn emulate_foreign_item_by_name(
7         &mut self,
8         link_name: &str,
9         args: &[OpTy<'tcx, Tag>],
10         dest: PlaceTy<'tcx, Tag>,
11         _ret: mir::BasicBlock,
12     ) -> InterpResult<'tcx, bool> {
13         let this = self.eval_context_mut();
14
15         match link_name {
16             "__error" => {
17                 let errno_place = this.machine.last_error.unwrap();
18                 this.write_scalar(errno_place.to_ref().to_scalar()?, dest)?;
19             }
20
21             // File related shims
22              "open" => {
23                 let result = this.open(args[0], args[1])?;
24                 this.write_scalar(Scalar::from_int(result, dest.layout.size), dest)?;
25             }
26
27             "close$NOCANCEL" => {
28                 let result = this.close(args[0])?;
29                 this.write_scalar(Scalar::from_int(result, dest.layout.size), dest)?;
30             }
31
32             "stat$INODE64" => {
33                 let result = this.stat(args[0], args[1])?;
34                 this.write_scalar(Scalar::from_int(result, dest.layout.size), dest)?;
35             }
36
37             "lstat$INODE64" => {
38                 let result = this.lstat(args[0], args[1])?;
39                 this.write_scalar(Scalar::from_int(result, dest.layout.size), dest)?;
40             }
41
42             "fstat$INODE64" => {
43                 let result = this.fstat(args[0], args[1])?;
44                 this.write_scalar(Scalar::from_int(result, dest.layout.size), dest)?;
45             }
46
47             "lseek" => {
48                 let result = this.lseek64(args[0], args[1], args[2])?;
49                 this.write_scalar(Scalar::from_int(result, dest.layout.size), dest)?;
50             }
51
52             // Time related shims
53             "gettimeofday" => {
54                 let result = this.gettimeofday(args[0], args[1])?;
55                 this.write_scalar(Scalar::from_int(result, dest.layout.size), dest)?;
56             }
57
58             // macOS API stubs.
59             "pthread_attr_get_np" => {
60                 this.write_null(dest)?;
61             }
62
63             "pthread_get_stackaddr_np" => {
64                 let stack_addr = Scalar::from_uint(STACK_ADDR, dest.layout.size);
65                 this.write_scalar(stack_addr, dest)?;
66             }
67
68             "pthread_get_stacksize_np" => {
69                 let stack_size = Scalar::from_uint(STACK_SIZE, dest.layout.size);
70                 this.write_scalar(stack_size, dest)?;
71             }
72
73             "_tlv_atexit" => {
74                 // FIXME: register the destructor.
75             }
76
77             "_NSGetArgc" => {
78                 this.write_scalar(this.machine.argc.expect("machine must be initialized"), dest)?;
79             }
80
81             "_NSGetArgv" => {
82                 this.write_scalar(this.machine.argv.expect("machine must be initialized"), dest)?;
83             }
84
85             "SecRandomCopyBytes" => {
86                 let len = this.read_scalar(args[1])?.to_machine_usize(this)?;
87                 let ptr = this.read_scalar(args[2])?.not_undef()?;
88                 this.gen_random(ptr, len as usize)?;
89                 this.write_null(dest)?;
90             }
91
92             "syscall" => {
93                 let sys_getrandom = this
94                     .eval_path_scalar(&["libc", "SYS_getrandom"])?
95                     .expect("Failed to get libc::SYS_getrandom")
96                     .to_machine_usize(this)?;
97
98                 match this.read_scalar(args[0])?.to_machine_usize(this)? {
99                     // `libc::syscall(NR_GETRANDOM, buf.as_mut_ptr(), buf.len(), GRND_NONBLOCK)`
100                     // is called if a `HashMap` is created the regular way (e.g. HashMap<K, V>).
101                     id if id == sys_getrandom => {
102                         // The first argument is the syscall id,
103                         // so skip over it.
104                         super::getrandom(this, &args[1..], dest)?;
105                     }
106                     id => throw_unsup_format!("miri does not support syscall ID {}", id),
107                 }
108             }
109
110             _ => throw_unsup_format!("can't call foreign function: {}", link_name),
111         };
112
113         Ok(true)
114     }
115 }
116