]> git.lizzy.rs Git - rust.git/blob - src/shims/foreign_items/posix/macos.rs
improve docs
[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
45             // Time related shims
46             "gettimeofday" => {
47                 let result = this.gettimeofday(args[0], args[1])?;
48                 this.write_scalar(Scalar::from_int(result, dest.layout.size), dest)?;
49             }
50
51             // Other shims
52             "pthread_attr_get_np" => {
53                 this.write_null(dest)?;
54             }
55
56             "pthread_get_stackaddr_np" => {
57                 let stack_addr = Scalar::from_uint(STACK_ADDR, dest.layout.size);
58                 this.write_scalar(stack_addr, dest)?;
59             }
60
61             "pthread_get_stacksize_np" => {
62                 let stack_size = Scalar::from_uint(STACK_SIZE, dest.layout.size);
63                 this.write_scalar(stack_size, dest)?;
64             }
65
66             "_tlv_atexit" => {
67                 // FIXME: register the destructor.
68             }
69
70             "_NSGetArgc" => {
71                 this.write_scalar(this.machine.argc.expect("machine must be initialized"), dest)?;
72             }
73
74             "_NSGetArgv" => {
75                 this.write_scalar(this.machine.argv.expect("machine must be initialized"), dest)?;
76             }
77
78             "SecRandomCopyBytes" => {
79                 let len = this.read_scalar(args[1])?.to_machine_usize(this)?;
80                 let ptr = this.read_scalar(args[2])?.not_undef()?;
81                 this.gen_random(ptr, len as usize)?;
82                 this.write_null(dest)?;
83             }
84
85             _ => throw_unsup_format!("can't call foreign function: {}", link_name),
86         };
87
88         Ok(true)
89     }
90 }
91