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