blob: 39ba63302e42164e85eec81b48e7bf9f65241114 [file] [edit]
// Copyright 2026 The Bazel Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//! Child binary spawned by `panic_handler_test` to exercise fatal Rust panics in an isolated
//! process.
//!
//! Usage: `panic_handler_test_helper <crash_dir> <mode> [mode-args...]`
//!
//! `<crash_dir>` is the directory where the handler writes `exit_code_to_use_on_abrupt_exit` and
//! `failure_detail.rawproto`.
//!
//! Supported modes:
//! - `panic`: installs the handler and triggers a panic with `"deliberate panic"`.
//! - `long-message <string> <count> [prefix]`: installs the handler and panics with an optional
//! UTF-8 `prefix` followed by `<string>` repeated `<count>` times.
//! - `non-string`: installs the handler and calls `std::panic::panic_any(42)`.
//! - `install-twice`: calls `PanicHandler::install` twice to trigger the double-install panic.
//! - `stderr-locked`: holds `std::io::stderr().lock()` on the main thread while a worker thread
//! panics.
//! - `concurrent-panics`: spawns 8 threads that synchronize on a barrier and panic simultaneously.
//! - `missing-exit-dir`: configures `abrupt_exit_file` inside a nonexistent subdirectory under
//! `<crash_dir>` and panics.
//! - `missing-failure-detail-dir`: configures `failure_detail_file` inside a nonexistent
//! subdirectory under `<crash_dir>` and panics.
use panic_handler::PanicHandler;
use std::env;
use std::io;
use std::path::PathBuf;
use std::sync::Barrier;
use std::thread;
fn main() {
let mut args = env::args_os().skip(1);
let crash_dir = PathBuf::from(args.next().expect("crash_dir arg"));
let mode = args.next().expect("mode arg").into_string().expect("UTF-8 mode arg");
let make_handler = || PanicHandler {
abrupt_exit_file: crash_dir.join("exit_code_to_use_on_abrupt_exit"),
failure_detail_file: crash_dir.join("failure_detail.rawproto"),
};
match mode.as_str() {
"panic" => {
make_handler().install();
panic!("deliberate panic");
}
"long-message" => {
let repeated =
args.next().expect("string arg").into_string().expect("UTF-8 string arg");
let count: usize = args
.next()
.expect("count arg")
.into_string()
.expect("UTF-8 count arg")
.parse()
.expect("usize count arg");
let prefix =
args.next().map(|s| s.into_string().expect("UTF-8 prefix")).unwrap_or_default();
make_handler().install();
let long_payload = format!("{prefix}{}", repeated.repeat(count));
panic!("{long_payload}");
}
"non-string" => {
make_handler().install();
std::panic::panic_any(42);
}
"install-twice" => {
make_handler().install();
make_handler().install();
}
"stderr-locked" => {
make_handler().install();
let barrier = Barrier::new(2);
// Hold `std::io::stderr()`'s lock while waiting on the panicking thread. No second
// thread ever panics, so the winner must write both crash files and the stderr banner
// via the raw stderr handle without blocking on `std::io::stderr()`'s lock.
let _stderr_lock = io::stderr().lock();
thread::scope(|scope| {
scope.spawn(|| {
barrier.wait();
panic!("panic while stderr is locked");
});
barrier.wait();
});
}
"concurrent-panics" => {
make_handler().install();
const THREAD_COUNT: usize = 8;
let barrier = &Barrier::new(THREAD_COUNT);
thread::scope(|scope| {
for index in 0..THREAD_COUNT {
scope.spawn(move || {
barrier.wait();
panic!("concurrent panic {index}");
});
}
});
}
"missing-exit-dir" => {
PanicHandler {
abrupt_exit_file: crash_dir.join("nonexistent/exit_code_to_use_on_abrupt_exit"),
failure_detail_file: crash_dir.join("failure_detail.rawproto"),
}
.install();
panic!("panic with unwritable abrupt exit file");
}
"missing-failure-detail-dir" => {
PanicHandler {
abrupt_exit_file: crash_dir.join("exit_code_to_use_on_abrupt_exit"),
failure_detail_file: crash_dir.join("nonexistent/failure_detail.rawproto"),
}
.install();
panic!("panic with unwritable failure detail file");
}
other => panic!("unknown mode: {other}"),
}
}