]> git.lizzy.rs Git - rust.git/blob - src/shims/dlsym.rs
Auto merge of #808 - RalfJung:extra-fn, r=RalfJung
[rust.git] / src / shims / dlsym.rs
1 use rustc::mir;
2
3 use crate::*;
4
5 #[derive(Debug, Copy, Clone)]
6 pub enum Dlsym {
7     GetEntropy,
8 }
9
10 impl Dlsym {
11     // Returns an error for unsupported symbols, and None if this symbol
12     // should become a NULL pointer (pretend it does not exist).
13     pub fn from_str(name: &str) -> InterpResult<'static, Option<Dlsym>> {
14         use self::Dlsym::*;
15         Ok(match name {
16             "getentropy" => Some(GetEntropy),
17             "__pthread_get_minstack" => None,
18             _ =>
19                 return err!(Unimplemented(format!(
20                     "Unsupported dlsym: {}", name
21                 ))),
22         })
23     }
24 }
25
26 impl<'mir, 'tcx> EvalContextExt<'mir, 'tcx> for crate::MiriEvalContext<'mir, 'tcx> {}
27 pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx> {
28     fn call_dlsym(
29         &mut self,
30         dlsym: Dlsym,
31         args: &[OpTy<'tcx, Tag>],
32         dest: Option<PlaceTy<'tcx, Tag>>,
33         ret: Option<mir::BasicBlock>,
34     ) -> InterpResult<'tcx> {
35         use self::Dlsym::*;
36
37         let this = self.eval_context_mut();
38
39         let dest = dest.expect("we don't support any diverging dlsym");
40         let ret = ret.expect("dest is `Some` but ret is `None`");
41
42         match dlsym {
43             GetEntropy => {
44                 let ptr = this.read_scalar(args[0])?.not_undef()?;
45                 let len = this.read_scalar(args[1])?.to_usize(this)?;
46                 this.gen_random(len as usize, ptr)?;
47                 this.write_null(dest)?;
48             }
49         }
50
51         this.goto_block(Some(ret))?;
52         this.dump_place(*dest);
53         Ok(())
54     }
55 }