]> git.lizzy.rs Git - rust.git/blob - src/tools/miri/src/shims/unix/dlsym.rs
Auto merge of #2719 - RalfJung:rustup, r=RalfJung
[rust.git] / src / tools / miri / src / shims / unix / dlsym.rs
1 use rustc_middle::mir;
2 use rustc_target::spec::abi::Abi;
3
4 use crate::*;
5 use shims::unix::android::dlsym as android;
6 use shims::unix::freebsd::dlsym as freebsd;
7 use shims::unix::linux::dlsym as linux;
8 use shims::unix::macos::dlsym as macos;
9
10 #[derive(Debug, Copy, Clone)]
11 pub enum Dlsym {
12     Android(android::Dlsym),
13     FreeBsd(freebsd::Dlsym),
14     Linux(linux::Dlsym),
15     MacOs(macos::Dlsym),
16 }
17
18 impl Dlsym {
19     // Returns an error for unsupported symbols, and None if this symbol
20     // should become a NULL pointer (pretend it does not exist).
21     pub fn from_str<'tcx>(name: &str, target_os: &str) -> InterpResult<'tcx, Option<Dlsym>> {
22         Ok(match target_os {
23             "android" => android::Dlsym::from_str(name)?.map(Dlsym::Android),
24             "freebsd" => freebsd::Dlsym::from_str(name)?.map(Dlsym::FreeBsd),
25             "linux" => linux::Dlsym::from_str(name)?.map(Dlsym::Linux),
26             "macos" => macos::Dlsym::from_str(name)?.map(Dlsym::MacOs),
27             _ => panic!("unsupported Unix OS {target_os}"),
28         })
29     }
30 }
31
32 impl<'mir, 'tcx: 'mir> EvalContextExt<'mir, 'tcx> for crate::MiriInterpCx<'mir, 'tcx> {}
33 pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
34     fn call_dlsym(
35         &mut self,
36         dlsym: Dlsym,
37         abi: Abi,
38         args: &[OpTy<'tcx, Provenance>],
39         dest: &PlaceTy<'tcx, Provenance>,
40         ret: Option<mir::BasicBlock>,
41     ) -> InterpResult<'tcx> {
42         let this = self.eval_context_mut();
43
44         this.check_abi(abi, Abi::C { unwind: false })?;
45
46         match dlsym {
47             Dlsym::Android(dlsym) =>
48                 android::EvalContextExt::call_dlsym(this, dlsym, args, dest, ret),
49             Dlsym::FreeBsd(dlsym) =>
50                 freebsd::EvalContextExt::call_dlsym(this, dlsym, args, dest, ret),
51             Dlsym::Linux(dlsym) => linux::EvalContextExt::call_dlsym(this, dlsym, args, dest, ret),
52             Dlsym::MacOs(dlsym) => macos::EvalContextExt::call_dlsym(this, dlsym, args, dest, ret),
53         }
54     }
55 }