diff --git a/lib/compiler/src/engine/artifact.rs b/lib/compiler/src/engine/artifact.rs index 2189bf80e4a..265962603fd 100644 --- a/lib/compiler/src/engine/artifact.rs +++ b/lib/compiler/src/engine/artifact.rs @@ -1,9 +1,13 @@ //! Define `Artifact`, based on `ArtifactBuild` //! to allow compiling and instantiating to be done as separate steps. -use std::sync::{ - Arc, - atomic::{AtomicUsize, Ordering::SeqCst}, +use std::{ + sync::{ + Arc, + atomic::{AtomicUsize, Ordering::SeqCst}, + }, + thread::sleep, + time::Duration, }; #[cfg(feature = "compiler")] @@ -128,6 +132,7 @@ impl Artifact { tunables: &dyn Tunables, hash_algorithm: Option, ) -> Result { + let data = Box::leak(Box::from(data)); let mut inner_engine = engine.inner_mut(); let environ = ModuleEnvironment::new(); let translation = environ.translate(data).map_err(CompileError::Wasm)?; @@ -1298,3 +1303,9 @@ impl Artifact { } } } + +impl Drop for Artifact { + fn drop(&mut self) { + eprintln!("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Dropping Artifact"); + } +} diff --git a/lib/compiler/src/engine/unwind/systemv.rs b/lib/compiler/src/engine/unwind/systemv.rs index 56e16b4f269..d4c8275b0d4 100644 --- a/lib/compiler/src/engine/unwind/systemv.rs +++ b/lib/compiler/src/engine/unwind/systemv.rs @@ -3,7 +3,11 @@ //! Module for System V ABI unwind registry. -use core::sync::atomic::{AtomicUsize, Ordering::Relaxed}; +use core::sync::atomic::{ + AtomicBool, AtomicUsize, + Ordering::{self, Relaxed}, +}; +use std::sync::Once; use crate::types::unwind::CompiledFunctionUnwindInfoReference; @@ -14,7 +18,6 @@ pub struct UnwindRegistry { #[cfg(all(target_os = "macos", target_arch = "aarch64"))] compact_unwind_mgr: compact_unwind::CompactUnwindManager, } - unsafe extern "C" { // libunwind import fn __register_frame(fde: *const u8); @@ -145,6 +148,12 @@ impl UnwindRegistry { #[allow(clippy::cast_ptr_alignment)] unsafe fn register_frames(&mut self, eh_frame: &[u8]) { + // Register atexit handler that will tell us if exit has been called. + static INIT: Once = Once::new(); + INIT.call_once(|| unsafe { + libc::atexit(atexit_handler); + }); + #[cfg(all(target_os = "macos", target_arch = "aarch64"))] { // Special call for macOS on aarch64 to register the `.eh_frame` section. @@ -243,8 +252,15 @@ impl UnwindRegistry { } } +static EXIT_CALLED: AtomicBool = AtomicBool::new(false); + +extern "C" fn atexit_handler() { + EXIT_CALLED.store(true, Ordering::SeqCst); +} + impl Drop for UnwindRegistry { fn drop(&mut self) { + eprintln!("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Dropping UnwindRegistry"); if self.published { unsafe { // libgcc stores the frame entries as a linked list in decreasing sort order @@ -264,6 +280,12 @@ impl Drop for UnwindRegistry { #[cfg(not(all(target_os = "macos", target_arch = "aarch64")))] { + // We don't want to deregister frames in UnwindRegistry::Drop as that could be called during + // program shutdown and can collide with release_registered_frames and lead to + // crashes. + if EXIT_CALLED.load(Ordering::SeqCst) { + return; + } __deregister_frame(*registration as *const _); } }