From 4b2435a791542ef8448546bea4af0cb224443a2e Mon Sep 17 00:00:00 2001 From: Robbert van der Helm Date: Sun, 26 Jun 2022 23:13:35 +0200 Subject: [PATCH] Allow allocations during most panics The panic logger is allowed to allocate now. If a panic message uses string formatting then that may still trigger `assert_no_alloc`. --- Cargo.lock | 12 +----------- Cargo.toml | 2 +- src/wrapper/util.rs | 46 ++++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 47 insertions(+), 13 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 3eb314f8..9171853d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2118,16 +2118,6 @@ dependencies = [ "value-bag", ] -[[package]] -name = "log-panics" -version = "2.0.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ae0136257df209261daa18d6c16394757c63e032e27aafd8b07788b051082bef" -dependencies = [ - "backtrace", - "log", -] - [[package]] name = "loudness_war_winner" version = "0.1.0" @@ -2387,6 +2377,7 @@ dependencies = [ "assert_no_alloc", "atomic_float", "atomic_refcell", + "backtrace", "baseview 0.1.0 (git+https://github.com/robbert-vdh/baseview.git?branch=feature/resize)", "bitflags", "cfg-if 1.0.0", @@ -2397,7 +2388,6 @@ dependencies = [ "lazy_static", "libc", "log", - "log-panics", "midi-consts", "nih_plug_derive", "parking_lot 0.12.1", diff --git a/Cargo.toml b/Cargo.toml index 7fead71c..f62534e7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -64,13 +64,13 @@ nih_plug_derive = { path = "nih_plug_derive" } atomic_float = "0.1" atomic_refcell = "0.1" +backtrace = "0.3.65" bitflags = "1.3" cfg-if = "1.0" clap-sys = "0.1" crossbeam = "0.8" lazy_static = "1.4" log = { version = "0.4", features = ["std", "release_max_level_info"] } -log-panics = { version = "2.0", features = ["with-backtrace"] } midi-consts = "0.1" parking_lot = "0.12" raw-window-handle = "0.4" diff --git a/src/wrapper/util.rs b/src/wrapper/util.rs index 998c6d83..533cf776 100644 --- a/src/wrapper/util.rs +++ b/src/wrapper/util.rs @@ -1,9 +1,12 @@ +use backtrace::Backtrace; use std::cmp; use std::fs::File; use std::marker::PhantomData; use std::os::raw::c_char; use std::sync::atomic::{AtomicBool, Ordering}; +use crate::util::permit_alloc; + /// The environment variable for controlling the logging behavior. const NIH_LOG_ENV: &str = "NIH_LOG"; @@ -123,7 +126,48 @@ pub fn setup_logger() { } } - log_panics::init(); + // This is copied from same as the `log_panics` crate, but it's wrapped in `permit_alloc()`. + // Otherwise logging panics will trigger `assert_no_alloc` as this also allocates. + std::panic::set_hook(Box::new(|info| { + permit_alloc(|| { + // All of this is directly copied from `permit_no_alloc`, except that `error!()` became + // `nih_error!()` and `Shim` has been inlined + let backtrace = Backtrace::new(); + + let thread = std::thread::current(); + let thread = thread.name().unwrap_or("unnamed"); + + let msg = match info.payload().downcast_ref::<&'static str>() { + Some(s) => *s, + None => match info.payload().downcast_ref::() { + Some(s) => &**s, + None => "Box", + }, + }; + + match info.location() { + Some(location) => { + nih_error!( + target: "panic", "thread '{}' panicked at '{}': {}:{}\n{:?}", + thread, + msg, + location.file(), + location.line(), + backtrace + ); + } + None => { + nih_error!( + target: "panic", + "thread '{}' panicked at '{}'\n{:?}", + thread, + msg, + backtrace + ) + } + } + }) + })); } /// A wrapper around the entire process function, including the plugin wrapper parts. This sets up