mysten_common/
logging.rs

1// Copyright (c) Mysten Labs, Inc.
2// SPDX-License-Identifier: Apache-2.0
3
4use crate::in_test_configuration;
5use once_cell::sync::Lazy;
6
7#[macro_export]
8macro_rules! fatal {
9    ($msg:literal $(, $arg:expr)*) => {{
10        if $crate::in_antithesis() {
11            let full_msg = format!($msg $(, $arg)*);
12            let json = $crate::logging::json!({ "message": full_msg });
13            $crate::logging::assert_unreachable_antithesis!($msg, &json);
14        }
15        tracing::error!(fatal = true, $msg $(, $arg)*);
16        panic!($msg $(, $arg)*);
17    }};
18}
19
20pub use antithesis_sdk::assert_reachable as assert_reachable_antithesis;
21pub use antithesis_sdk::assert_sometimes as assert_sometimes_antithesis;
22pub use antithesis_sdk::assert_unreachable as assert_unreachable_antithesis;
23
24pub use serde_json::json;
25
26#[inline(always)]
27pub fn crash_on_debug() -> bool {
28    static CRASH_ON_DEBUG: Lazy<bool> = Lazy::new(|| {
29        in_test_configuration() || std::env::var("SUI_ENABLE_DEBUG_ASSERTIONS").is_ok()
30    });
31
32    *CRASH_ON_DEBUG
33}
34
35#[cfg(msim)]
36pub mod intercept_debug_fatal {
37    use std::sync::{Arc, Mutex};
38
39    #[derive(Clone)]
40    pub struct DebugFatalCallback {
41        pub pattern: String,
42        pub callback: Arc<dyn Fn() + Send + Sync>,
43    }
44
45    thread_local! {
46        static INTERCEPT_DEBUG_FATAL: Mutex<Option<DebugFatalCallback>> = Mutex::new(None);
47    }
48
49    pub fn register_callback(message: &str, f: impl Fn() + Send + Sync + 'static) {
50        INTERCEPT_DEBUG_FATAL.with(|m| {
51            *m.lock().unwrap() = Some(DebugFatalCallback {
52                pattern: message.to_string(),
53                callback: Arc::new(f),
54            });
55        });
56    }
57
58    pub fn get_callback() -> Option<DebugFatalCallback> {
59        INTERCEPT_DEBUG_FATAL.with(|m| m.lock().unwrap().clone())
60    }
61}
62
63#[macro_export]
64macro_rules! register_debug_fatal_handler {
65    ($message:literal, $f:expr) => {
66        #[cfg(msim)]
67        $crate::logging::intercept_debug_fatal::register_callback($message, $f);
68
69        #[cfg(not(msim))]
70        {
71            // silence unused variable warnings from the body of the callback
72            let _ = $f;
73        }
74    };
75}
76
77#[macro_export]
78macro_rules! debug_fatal {
79    //($msg:literal $(, $arg:expr)* $(,)?)
80    ($msg:literal $(, $arg:expr)*) => {{
81        loop {
82            #[cfg(msim)]
83            {
84                if let Some(cb) = $crate::logging::intercept_debug_fatal::get_callback() {
85                    tracing::error!($msg $(, $arg)*);
86                    let msg = format!($msg $(, $arg)*);
87                    if msg.contains(&cb.pattern) {
88                        (cb.callback)();
89                    }
90                    break;
91                }
92            }
93
94            // In antithesis, rather than crashing, we will use the assert_unreachable_antithesis
95            // macro to catch the signal that something has gone wrong.
96            if !$crate::in_antithesis() && $crate::logging::crash_on_debug() {
97                $crate::fatal!($msg $(, $arg)*);
98            } else {
99                let stacktrace = std::backtrace::Backtrace::capture();
100                tracing::error!(debug_fatal = true, stacktrace = ?stacktrace, $msg $(, $arg)*);
101                let location = concat!(file!(), ':', line!());
102                if let Some(metrics) = mysten_metrics::get_metrics() {
103                    metrics.system_invariant_violations.with_label_values(&[location]).inc();
104                }
105                if $crate::in_antithesis() {
106                    // antithesis requires a literal for first argument. pass the formatted argument
107                    // as a string.
108                    let full_msg = format!($msg $(, $arg)*);
109                    let json = $crate::logging::json!({ "message": full_msg });
110                    $crate::logging::assert_unreachable_antithesis!($msg, &json);
111                }
112            }
113            break;
114        }
115    }};
116}
117
118#[macro_export]
119macro_rules! assert_reachable {
120    () => {
121        $crate::logging::assert_reachable!("");
122    };
123    ($message:literal) => {{
124        // calling in to antithesis sdk breaks determinisim in simtests (on linux only)
125        if !cfg!(msim) {
126            $crate::logging::assert_reachable_antithesis!($message);
127        } else {
128            $crate::assert_reachable_simtest!($message);
129        }
130    }};
131}
132
133#[macro_export]
134macro_rules! assert_sometimes {
135    ($expr:expr, $message:literal) => {{
136        // calling in to antithesis sdk breaks determinisim in simtests (on linux only)
137        if !cfg!(msim) {
138            $crate::logging::assert_sometimes_antithesis!($expr, $message);
139        } else {
140            $crate::assert_sometimes_simtest!($expr, $message);
141        }
142    }};
143}
144
145mod tests {
146    #[test]
147    #[should_panic]
148    fn test_fatal() {
149        fatal!("This is a fatal error");
150    }
151
152    #[test]
153    #[should_panic]
154    fn test_debug_fatal() {
155        if cfg!(debug_assertions) {
156            debug_fatal!("This is a debug fatal error");
157        } else {
158            // pass in release mode as well
159            fatal!("This is a fatal error");
160        }
161    }
162
163    #[cfg(not(debug_assertions))]
164    #[test]
165    fn test_debug_fatal_release_mode() {
166        debug_fatal!("This is a debug fatal error");
167    }
168
169    #[test]
170    fn test_assert_sometimes_side_effects() {
171        let mut x = 0;
172
173        let mut inc = || {
174            x += 1;
175            true
176        };
177
178        assert_sometimes!(inc(), "");
179        assert_eq!(x, 1);
180    }
181}