miri/shims/wasi/
foreign_items.rs1use rustc_abi::CanonAbi;
2use rustc_middle::ty::Ty;
3use rustc_span::Symbol;
4use rustc_target::callconv::FnAbi;
5
6use crate::shims::alloc::EvalContextExt as _;
7use crate::*;
8
9pub fn is_dyn_sym(_name: &str) -> bool {
10 false
11}
12
13impl<'tcx> EvalContextExt<'tcx> for crate::MiriInterpCx<'tcx> {}
14pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
15 fn emulate_foreign_item_inner(
16 &mut self,
17 link_name: Symbol,
18 abi: &FnAbi<'tcx, Ty<'tcx>>,
19 args: &[OpTy<'tcx>],
20 dest: &MPlaceTy<'tcx>,
21 ) -> InterpResult<'tcx, EmulateItemResult> {
22 let this = self.eval_context_mut();
23 match link_name.as_str() {
24 "posix_memalign" => {
26 let [memptr, align, size] =
27 this.check_shim_sig_lenient(abi, CanonAbi::C, link_name, args)?;
28 let result = this.posix_memalign(memptr, align, size)?;
29 this.write_scalar(result, dest)?;
30 }
31 "aligned_alloc" => {
32 let [align, size] =
33 this.check_shim_sig_lenient(abi, CanonAbi::C, link_name, args)?;
34 let res = this.aligned_alloc(align, size)?;
35 this.write_pointer(res, dest)?;
36 }
37
38 "get-stdout" => {
42 let [] =
43 this.check_shim_sig(shim_sig!(extern "C" fn() -> i32), link_name, abi, args)?;
44 this.write_scalar(Scalar::from_i32(1), dest)?; }
46 "get-stderr" => {
47 let [] =
48 this.check_shim_sig(shim_sig!(extern "C" fn() -> i32), link_name, abi, args)?;
49 this.write_scalar(Scalar::from_i32(2), dest)?; }
51 "[resource-drop]output-stream" => {
52 let [handle] =
53 this.check_shim_sig(shim_sig!(extern "C" fn(i32) -> ()), link_name, abi, args)?;
54 let handle = this.read_scalar(handle)?.to_i32()?;
55
56 if !(handle == 1 || handle == 2) {
57 throw_unsup_format!("wasm output-stream: unsupported handle");
58 }
59 }
61 "[method]output-stream.blocking-write-and-flush" => {
62 let [handle, buf, len, ret_area] = this.check_shim_sig(
63 shim_sig!(extern "C" fn(i32, *mut _, usize, *mut _) -> ()),
64 link_name,
65 abi,
66 args,
67 )?;
68 let handle = this.read_scalar(handle)?.to_i32()?;
69 let buf = this.read_pointer(buf)?;
70 let len = this.read_target_usize(len)?;
71 let ret_area = this.read_pointer(ret_area)?;
72
73 if len > 4096 {
74 throw_unsup_format!(
75 "wasm output-stream.blocking-write-and-flush: buffer too big"
76 );
77 }
78 let len = usize::try_from(len).unwrap();
79 let Some(fd) = this.machine.fds.get(handle) else {
80 throw_unsup_format!(
81 "wasm output-stream.blocking-write-and-flush: unsupported handle"
82 );
83 };
84 fd.write(
85 this.machine.communicate(),
86 buf,
87 len,
88 this,
89 callback!(
90 @capture<'tcx> {
91 len: usize,
92 ret_area: Pointer,
93 }
94 |this, result: Result<usize, IoError>| {
95 if !matches!(result, Ok(l) if l == len) {
96 throw_unsup_format!("wasm output-stream.blocking-write-and-flush: returning errors is not supported");
97 }
98 let ret = this.ptr_to_mplace(ret_area, this.machine.layouts.u8);
100 this.write_null(&ret)?;
101 interp_ok(())
102 }),
103 )?;
104 }
105
106 _ => return interp_ok(EmulateItemResult::NotSupported),
107 }
108 interp_ok(EmulateItemResult::NeedsReturn)
109 }
110}