Skip to content

Commit

Permalink
Merge pull request #2165 from ljedrz/feat/custom_catch_hook
Browse files Browse the repository at this point in the history
Use a custom panic hook for caught halting
  • Loading branch information
howardwu authored Nov 15, 2023
2 parents f06c915 + 00aaa56 commit 0608677
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 6 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions synthesizer/process/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,11 @@ package = "snarkvm-synthesizer-snark"
path = "../../synthesizer/snark"
version = "=0.16.9"

[dependencies.utilities]
package = "snarkvm-utilities"
path = "../../utilities"
version = "=0.16.8"

[dependencies.aleo-std]
version = "0.1.18"
default-features = false
Expand Down
12 changes: 6 additions & 6 deletions synthesizer/process/src/finalize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use super::*;
use console::program::{Future, Register};
use synthesizer_program::{Await, FinalizeRegistersState, Operand};
use utilities::handle_halting;

impl<N: Network> Process<N> {
/// Finalizes the deployment and fee.
Expand Down Expand Up @@ -224,7 +225,7 @@ fn finalize_transition<N: Network, P: FinalizeStorage<N>>(
// Finalize the command.
match &command {
Command::BranchEq(branch_eq) => {
let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
let result = handle_halting!(panic::AssertUnwindSafe(|| {
branch_to(counter, branch_eq, finalize, stack, &registers)
}));
match result {
Expand All @@ -238,7 +239,7 @@ fn finalize_transition<N: Network, P: FinalizeStorage<N>>(
}
}
Command::BranchNeq(branch_neq) => {
let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
let result = handle_halting!(panic::AssertUnwindSafe(|| {
branch_to(counter, branch_neq, finalize, stack, &registers)
}));
match result {
Expand Down Expand Up @@ -276,7 +277,7 @@ fn finalize_transition<N: Network, P: FinalizeStorage<N>>(
None => bail!("Transition ID '{transition_id}' not found in call graph"),
};

let callee_state = match std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
let callee_state = match handle_halting!(panic::AssertUnwindSafe(|| {
// Set up the finalize state for the await.
setup_await(state, await_, stack, &registers, child_transition_id)
})) {
Expand Down Expand Up @@ -306,9 +307,8 @@ fn finalize_transition<N: Network, P: FinalizeStorage<N>>(
break;
}
_ => {
let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
command.finalize(stack, store, &mut registers)
}));
let result =
handle_halting!(panic::AssertUnwindSafe(|| { command.finalize(stack, store, &mut registers) }));
match result {
// If the evaluation succeeds with an operation, add it to the list.
Ok(Ok(Some(finalize_operation))) => finalize_operations.push(finalize_operation),
Expand Down
29 changes: 29 additions & 0 deletions utilities/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,32 @@ impl Error for crate::String {}

#[cfg(not(feature = "std"))]
impl Error for crate::io::Error {}

/// This purpose of this macro is to catch the instances of halting
/// without producing logs looking like unexpected panics. It prints
/// to stderr using the format: "Halted at <location>: <halt message>".
#[macro_export]
macro_rules! handle_halting {
($e:expr) => {{
use std::panic;

// Set a custom hook before calling catch_unwind to
// indicate that the panic was expected and handled.
panic::set_hook(Box::new(|e| {
let msg = e.to_string();
let msg = msg.split_ascii_whitespace().skip_while(|&word| word != "panicked").collect::<Vec<&str>>();
let mut msg = msg.join(" ");
msg = msg.replacen("panicked", "Halted", 1);
eprintln!("{msg}");
}));

// Perform the operation that may panic.
let result = panic::catch_unwind($e);

// Restore the standard panic hook.
let _ = panic::take_hook();

// Return the result, allowing regular error-handling.
result
}};
}

0 comments on commit 0608677

Please sign in to comment.