1
0
Fork 0

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`.
This commit is contained in:
Robbert van der Helm 2022-06-26 23:13:35 +02:00
parent fb0c1acbed
commit 4b2435a791
3 changed files with 47 additions and 13 deletions

12
Cargo.lock generated
View file

@ -2118,16 +2118,6 @@ dependencies = [
"value-bag", "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]] [[package]]
name = "loudness_war_winner" name = "loudness_war_winner"
version = "0.1.0" version = "0.1.0"
@ -2387,6 +2377,7 @@ dependencies = [
"assert_no_alloc", "assert_no_alloc",
"atomic_float", "atomic_float",
"atomic_refcell", "atomic_refcell",
"backtrace",
"baseview 0.1.0 (git+https://github.com/robbert-vdh/baseview.git?branch=feature/resize)", "baseview 0.1.0 (git+https://github.com/robbert-vdh/baseview.git?branch=feature/resize)",
"bitflags", "bitflags",
"cfg-if 1.0.0", "cfg-if 1.0.0",
@ -2397,7 +2388,6 @@ dependencies = [
"lazy_static", "lazy_static",
"libc", "libc",
"log", "log",
"log-panics",
"midi-consts", "midi-consts",
"nih_plug_derive", "nih_plug_derive",
"parking_lot 0.12.1", "parking_lot 0.12.1",

View file

@ -64,13 +64,13 @@ nih_plug_derive = { path = "nih_plug_derive" }
atomic_float = "0.1" atomic_float = "0.1"
atomic_refcell = "0.1" atomic_refcell = "0.1"
backtrace = "0.3.65"
bitflags = "1.3" bitflags = "1.3"
cfg-if = "1.0" cfg-if = "1.0"
clap-sys = "0.1" clap-sys = "0.1"
crossbeam = "0.8" crossbeam = "0.8"
lazy_static = "1.4" lazy_static = "1.4"
log = { version = "0.4", features = ["std", "release_max_level_info"] } log = { version = "0.4", features = ["std", "release_max_level_info"] }
log-panics = { version = "2.0", features = ["with-backtrace"] }
midi-consts = "0.1" midi-consts = "0.1"
parking_lot = "0.12" parking_lot = "0.12"
raw-window-handle = "0.4" raw-window-handle = "0.4"

View file

@ -1,9 +1,12 @@
use backtrace::Backtrace;
use std::cmp; use std::cmp;
use std::fs::File; use std::fs::File;
use std::marker::PhantomData; use std::marker::PhantomData;
use std::os::raw::c_char; use std::os::raw::c_char;
use std::sync::atomic::{AtomicBool, Ordering}; use std::sync::atomic::{AtomicBool, Ordering};
use crate::util::permit_alloc;
/// The environment variable for controlling the logging behavior. /// The environment variable for controlling the logging behavior.
const NIH_LOG_ENV: &str = "NIH_LOG"; 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::<String>() {
Some(s) => &**s,
None => "Box<Any>",
},
};
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 /// A wrapper around the entire process function, including the plugin wrapper parts. This sets up