From 22aa7f98c5cfb28aee0205f56ee1a6ee781ab2ba Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Mon, 4 Jul 2022 13:46:11 -0400 Subject: [PATCH] call_function: make the unit-return-type case more convenient --- src/eval.rs | 4 ++-- src/helpers.rs | 11 +++++++++-- src/shims/panic.rs | 10 ++++------ src/shims/tls.rs | 9 +++------ src/shims/unix/thread.rs | 2 +- 5 files changed, 19 insertions(+), 17 deletions(-) diff --git a/src/eval.rs b/src/eval.rs index 1536b826ac4..d75b4f5fa6d 100644 --- a/src/eval.rs +++ b/src/eval.rs @@ -289,7 +289,7 @@ pub fn create_ecx<'mir, 'tcx: 'mir>( start_instance, Abi::Rust, &[Scalar::from_pointer(main_ptr, &ecx).into(), argc.into(), argv], - &ret_place.into(), + Some(&ret_place.into()), StackPopCleanup::Root { cleanup: true }, )?; } @@ -298,7 +298,7 @@ pub fn create_ecx<'mir, 'tcx: 'mir>( entry_instance, Abi::Rust, &[argc.into(), argv], - &ret_place.into(), + Some(&ret_place.into()), StackPopCleanup::Root { cleanup: true }, )?; } diff --git a/src/helpers.rs b/src/helpers.rs index 86823f28178..c051d44fa25 100644 --- a/src/helpers.rs +++ b/src/helpers.rs @@ -235,12 +235,15 @@ fn gen_random(&mut self, ptr: Pointer>, len: u64) -> InterpResult<'t /// Call a function: Push the stack frame and pass the arguments. /// For now, arguments must be scalars (so that the caller does not have to know the layout). + /// + /// If you do not provie a return place, a dangling zero-sized place will be created + /// for your convenience. fn call_function( &mut self, f: ty::Instance<'tcx>, caller_abi: Abi, args: &[Immediate], - dest: &PlaceTy<'tcx, Tag>, + dest: Option<&PlaceTy<'tcx, Tag>>, stack_pop: StackPopCleanup, ) -> InterpResult<'tcx> { let this = self.eval_context_mut(); @@ -256,7 +259,11 @@ fn call_function( // Push frame. let mir = this.load_mir(f.def, None)?; - this.push_stack_frame(f, mir, dest, stack_pop)?; + let dest = match dest { + Some(dest) => *dest, + None => MPlaceTy::dangling(this.layout_of(mir.return_ty())?).into(), + }; + this.push_stack_frame(f, mir, &dest, stack_pop)?; // Initialize arguments. let mut callee_args = this.frame().body.args_iter(); diff --git a/src/shims/panic.rs b/src/shims/panic.rs index 2ef0a741d52..c356dd86676 100644 --- a/src/shims/panic.rs +++ b/src/shims/panic.rs @@ -91,12 +91,11 @@ fn handle_try( // Now we make a function call, and pass `data` as first and only argument. let f_instance = this.get_ptr_fn(try_fn)?.as_instance()?; trace!("try_fn: {:?}", f_instance); - let ret_place = MPlaceTy::dangling(this.machine.layouts.unit).into(); this.call_function( f_instance, Abi::Rust, &[data.into()], - &ret_place, + None, // Directly return to caller. StackPopCleanup::Goto { ret: Some(ret), unwind: StackPopUnwind::Skip }, )?; @@ -144,12 +143,11 @@ fn handle_stack_pop_unwind( let f_instance = this.get_ptr_fn(this.scalar_to_ptr(catch_unwind.catch_fn)?)?.as_instance()?; trace!("catch_fn: {:?}", f_instance); - let ret_place = MPlaceTy::dangling(this.machine.layouts.unit).into(); this.call_function( f_instance, Abi::Rust, &[catch_unwind.data.into(), payload.into()], - &ret_place, + None, // Directly return to caller of `try`. StackPopCleanup::Goto { ret: Some(catch_unwind.ret), unwind: StackPopUnwind::Skip }, )?; @@ -175,7 +173,7 @@ fn start_panic(&mut self, msg: &str, unwind: StackPopUnwind) -> InterpResult<'tc panic, Abi::Rust, &[msg.to_ref(this)], - &MPlaceTy::dangling(this.machine.layouts.unit).into(), + None, StackPopCleanup::Goto { ret: None, unwind }, ) } @@ -204,7 +202,7 @@ fn assert_panic( panic_bounds_check, Abi::Rust, &[index.into(), len.into()], - &MPlaceTy::dangling(this.machine.layouts.unit).into(), + None, StackPopCleanup::Goto { ret: None, unwind: match unwind { diff --git a/src/shims/tls.rs b/src/shims/tls.rs index 6b4e9d4f753..5a72c872b04 100644 --- a/src/shims/tls.rs +++ b/src/shims/tls.rs @@ -253,12 +253,11 @@ fn schedule_windows_tls_dtors(&mut self) -> InterpResult<'tcx> { // The signature of this function is `unsafe extern "system" fn(h: c::LPVOID, dwReason: c::DWORD, pv: c::LPVOID)`. let reason = this.eval_path_scalar(&["std", "sys", "windows", "c", "DLL_THREAD_DETACH"])?; - let ret_place = MPlaceTy::dangling(this.machine.layouts.unit).into(); this.call_function( thread_callback, Abi::System { unwind: false }, &[Scalar::null_ptr(this).into(), reason.into(), Scalar::null_ptr(this).into()], - &ret_place, + None, StackPopCleanup::Root { cleanup: true }, )?; @@ -276,12 +275,11 @@ fn schedule_macos_tls_dtor(&mut self) -> InterpResult<'tcx, bool> { if let Some((instance, data)) = this.machine.tls.macos_thread_dtors.remove(&thread_id) { trace!("Running macos dtor {:?} on {:?} at {:?}", instance, data, thread_id); - let ret_place = MPlaceTy::dangling(this.machine.layouts.unit).into(); this.call_function( instance, Abi::C { unwind: false }, &[data.into()], - &ret_place, + None, StackPopCleanup::Root { cleanup: true }, )?; @@ -319,12 +317,11 @@ fn schedule_next_pthread_tls_dtor(&mut self) -> InterpResult<'tcx, bool> { "data can't be NULL when dtor is called!" ); - let ret_place = MPlaceTy::dangling(this.machine.layouts.unit).into(); this.call_function( instance, Abi::C { unwind: false }, &[ptr.into()], - &ret_place, + None, StackPopCleanup::Root { cleanup: true }, )?; diff --git a/src/shims/unix/thread.rs b/src/shims/unix/thread.rs index 63b9f36d6ff..8dc5f81354a 100644 --- a/src/shims/unix/thread.rs +++ b/src/shims/unix/thread.rs @@ -47,7 +47,7 @@ fn pthread_create( instance, Abi::C { unwind: false }, &[*func_arg], - &ret_place.into(), + Some(&ret_place.into()), StackPopCleanup::Root { cleanup: true }, )?;