Don't unregister unwind tables after the JIT is done

This commit is contained in:
bjorn3 2021-04-08 18:27:26 +02:00
parent 86530f889e
commit 6fac7f089f
3 changed files with 18 additions and 43 deletions

View file

@ -72,15 +72,12 @@ impl UnwindContext {
}
#[cfg(feature = "jit")]
pub(crate) unsafe fn register_jit(
self,
jit_module: &cranelift_jit::JITModule,
) -> Option<UnwindRegistry> {
pub(crate) unsafe fn register_jit(self, jit_module: &cranelift_jit::JITModule) {
let mut eh_frame = EhFrame::from(super::emit::WriterRelocate::new(self.endian));
self.frame_table.write_eh_frame(&mut eh_frame).unwrap();
if eh_frame.0.writer.slice().is_empty() {
return None;
return;
}
let mut eh_frame = eh_frame.0.relocate_for_jit(jit_module);
@ -88,7 +85,10 @@ impl UnwindContext {
// GCC expects a terminating "empty" length, so write a 0 length at the end of the table.
eh_frame.extend(&[0, 0, 0, 0]);
let mut registrations = Vec::new();
// FIXME support unregistering unwind tables once cranelift-jit supports deallocating
// individual functions
#[allow(unused_variables)]
let (eh_frame, eh_frame_len, _) = Vec::into_raw_parts(eh_frame);
// =======================================================================
// Everything after this line up to the end of the file is loosly based on
@ -96,8 +96,8 @@ impl UnwindContext {
#[cfg(target_os = "macos")]
{
// On macOS, `__register_frame` takes a pointer to a single FDE
let start = eh_frame.as_ptr();
let end = start.add(eh_frame.len());
let start = eh_frame;
let end = start.add(eh_frame_len);
let mut current = start;
// Walk all of the entries in the frame table and register them
@ -107,7 +107,6 @@ impl UnwindContext {
// Skip over the CIE
if current != start {
__register_frame(current);
registrations.push(current as usize);
}
// Move to the next table entry (+4 because the length itself is not inclusive)
@ -117,41 +116,12 @@ impl UnwindContext {
#[cfg(not(target_os = "macos"))]
{
// On other platforms, `__register_frame` will walk the FDEs until an entry of length 0
let ptr = eh_frame.as_ptr();
__register_frame(ptr);
registrations.push(ptr as usize);
__register_frame(eh_frame);
}
Some(UnwindRegistry { _frame_table: eh_frame, registrations })
}
}
/// Represents a registry of function unwind information for System V ABI.
pub(crate) struct UnwindRegistry {
_frame_table: Vec<u8>,
registrations: Vec<usize>,
}
extern "C" {
// libunwind import
fn __register_frame(fde: *const u8);
fn __deregister_frame(fde: *const u8);
}
impl Drop for UnwindRegistry {
fn drop(&mut self) {
unsafe {
// libgcc stores the frame entries as a linked list in decreasing sort order
// based on the PC value of the registered entry.
//
// As we store the registrations in increasing order, it would be O(N^2) to
// deregister in that order.
//
// To ensure that we just pop off the first element in the list upon every
// deregistration, walk our list of registrations backwards.
for fde in self.registrations.iter().rev() {
__deregister_frame(*fde as *const _);
}
}
}
}

View file

@ -80,12 +80,17 @@ pub(crate) fn run_jit(tcx: TyCtxt<'_>, backend_config: BackendConfig) -> ! {
}
crate::allocator::codegen(tcx, &mut jit_module, &mut cx.unwind_context);
crate::main_shim::maybe_create_entry_wrapper(tcx, &mut jit_module, &mut cx.unwind_context, true);
crate::main_shim::maybe_create_entry_wrapper(
tcx,
&mut jit_module,
&mut cx.unwind_context,
true,
);
tcx.sess.abort_if_errors();
jit_module.finalize_definitions();
let _unwind_register_guard = unsafe { cx.unwind_context.register_jit(&jit_module) };
unsafe { cx.unwind_context.register_jit(&jit_module) };
println!(
"Rustc codegen cranelift will JIT run the executable, because -Cllvm-args=mode=jit was passed"
@ -147,7 +152,7 @@ extern "C" fn __clif_jit_fn(instance_ptr: *const Instance<'static>) -> *const u8
assert!(cx.global_asm.is_empty());
jit_module.finalize_definitions();
std::mem::forget(unsafe { cx.unwind_context.register_jit(&jit_module) });
unsafe { cx.unwind_context.register_jit(&jit_module) };
jit_module.get_finalized_function(func_id)
})
})

View file

@ -1,4 +1,4 @@
#![feature(rustc_private, decl_macro, never_type, hash_drain_filter)]
#![feature(rustc_private, decl_macro, never_type, hash_drain_filter, vec_into_raw_parts)]
#![warn(rust_2018_idioms)]
#![warn(unused_lifetimes)]
#![warn(unreachable_pub)]