]> git.lizzy.rs Git - rust.git/blob - src/shims/dlsym.rs
disentangle macos and linux dlsyms
[rust.git] / src / shims / dlsym.rs
1 use rustc_middle::mir;
2
3 use crate::*;
4 use helpers::check_arg_count;
5
6 #[derive(Debug, Copy, Clone)]
7 pub enum Dlsym {
8     GetEntropy,
9 }
10
11 impl Dlsym {
12     // Returns an error for unsupported symbols, and None if this symbol
13     // should become a NULL pointer (pretend it does not exist).
14     pub fn from_str(name: &[u8], target_os: &str) -> InterpResult<'static, Option<Dlsym>> {
15         use self::Dlsym::*;
16         let name = String::from_utf8_lossy(name);
17         Ok(match target_os {
18             "linux" => match &*name {
19                 "__pthread_get_minstack" => None,
20                 _ => throw_unsup_format!("unsupported Linux dlsym: {}", name),
21             }
22             "macos" => match &*name {
23                 "getentropy" => Some(GetEntropy),
24                 _ => throw_unsup_format!("unsupported macOS dlsym: {}", name),
25             }
26             "windows" => match &*name {
27                 "SetThreadStackGuarantee" => None,
28                 "AcquireSRWLockExclusive" => None,
29                 _ => throw_unsup_format!("unsupported Windows dlsym: {}", name),
30             }
31             os => bug!("dlsym not implemented for target_os {}", os),
32         })
33     }
34 }
35
36 impl<'mir, 'tcx: 'mir> EvalContextExt<'mir, 'tcx> for crate::MiriEvalContext<'mir, 'tcx> {}
37 pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx> {
38     fn call_dlsym(
39         &mut self,
40         dlsym: Dlsym,
41         args: &[OpTy<'tcx, Tag>],
42         ret: Option<(PlaceTy<'tcx, Tag>, mir::BasicBlock)>,
43     ) -> InterpResult<'tcx> {
44         use self::Dlsym::*;
45
46         let this = self.eval_context_mut();
47         let (dest, ret) = ret.expect("we don't support any diverging dlsym");
48
49         match dlsym {
50             GetEntropy => {
51                 let &[ptr, len] = check_arg_count(args)?;
52                 let ptr = this.read_scalar(ptr)?.not_undef()?;
53                 let len = this.read_scalar(len)?.to_machine_usize(this)?;
54                 this.gen_random(ptr, len)?;
55                 this.write_null(dest)?;
56             }
57         }
58
59         this.dump_place(*dest);
60         this.go_to_block(ret);
61         Ok(())
62     }
63 }