]> git.lizzy.rs Git - rust.git/blob - src/shims/foreign_items/posix/macos.rs
add `_NSGetEnviron` foreign function for macos
[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
23             // The only reason this is not in the `posix` module is because the `linux` item has a
24             // different name.
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.macos_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.macos_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.macos_fstat(args[0], args[1])?;
42                 this.write_scalar(Scalar::from_int(result, dest.layout.size), dest)?;
43             }
44             // Environment related shims
45             "_NSGetEnviron" => {
46                 this.write_scalar(this.memory.extra.environ.unwrap().ptr, dest)?;
47             }
48             // The only reason this is not in the `posix` module is because the `linux` item has a
49             // different name.
50             "opendir$INODE64" => {
51                 let result = this.opendir(args[0])?;
52                 this.write_scalar(result, dest)?;
53             }
54
55             // The `linux` module has a parallel foreign item, `readdir64_r`, which uses a
56             // different struct layout.
57             "readdir_r$INODE64" => {
58                 let result = this.macos_readdir_r(args[0], args[1], args[2])?;
59                 this.write_scalar(Scalar::from_int(result, dest.layout.size), dest)?;
60             }
61
62             // Time related shims
63             "gettimeofday" => {
64                 let result = this.gettimeofday(args[0], args[1])?;
65                 this.write_scalar(Scalar::from_int(result, dest.layout.size), dest)?;
66             }
67
68             // Other shims
69             "pthread_attr_get_np" => {
70                 this.write_null(dest)?;
71             }
72
73             "pthread_get_stackaddr_np" => {
74                 let stack_addr = Scalar::from_uint(STACK_ADDR, dest.layout.size);
75                 this.write_scalar(stack_addr, dest)?;
76             }
77
78             "pthread_get_stacksize_np" => {
79                 let stack_size = Scalar::from_uint(STACK_SIZE, dest.layout.size);
80                 this.write_scalar(stack_size, dest)?;
81             }
82
83             "_tlv_atexit" => {
84                 // FIXME: register the destructor.
85             }
86
87             "_NSGetArgc" => {
88                 this.write_scalar(this.machine.argc.expect("machine must be initialized"), dest)?;
89             }
90
91             "_NSGetArgv" => {
92                 this.write_scalar(this.machine.argv.expect("machine must be initialized"), dest)?;
93             }
94
95             "SecRandomCopyBytes" => {
96                 let len = this.read_scalar(args[1])?.to_machine_usize(this)?;
97                 let ptr = this.read_scalar(args[2])?.not_undef()?;
98                 this.gen_random(ptr, len as usize)?;
99                 this.write_null(dest)?;
100             }
101
102             _ => throw_unsup_format!("can't call foreign function: {}", link_name),
103         };
104
105         Ok(true)
106     }
107 }
108