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! debug_fatal_no_invariant {
120    ($msg:literal $(, $arg:expr)*) => {{
121        loop {
122            #[cfg(msim)]
123            {
124                if let Some(cb) = $crate::logging::intercept_debug_fatal::get_callback() {
125                    tracing::error!($msg $(, $arg)*);
126                    let msg = format!($msg $(, $arg)*);
127                    if msg.contains(&cb.pattern) {
128                        (cb.callback)();
129                    }
130                    break;
131                }
132            }
133
134            if !$crate::in_antithesis() && $crate::logging::crash_on_debug() {
135                $crate::fatal!($msg $(, $arg)*);
136            } else {
137                tracing::error!($msg $(, $arg)*);
138                if $crate::in_antithesis() {
139                    let full_msg = format!($msg $(, $arg)*);
140                    let json = $crate::logging::json!({ "message": full_msg });
141                    $crate::logging::assert_unreachable_antithesis!($msg, &json);
142                }
143            }
144            break;
145        }
146    }};
147}
148
149#[macro_export]
150macro_rules! assert_reachable {
151    () => {
152        $crate::logging::assert_reachable!("");
153    };
154    ($message:literal) => {{
155        // calling in to antithesis sdk breaks determinisim in simtests (on linux only)
156        if !cfg!(msim) {
157            $crate::logging::assert_reachable_antithesis!($message);
158        } else {
159            $crate::assert_reachable_simtest!($message);
160        }
161    }};
162}
163
164#[macro_export]
165macro_rules! assert_sometimes {
166    ($expr:expr, $message:literal) => {{
167        // calling in to antithesis sdk breaks determinisim in simtests (on linux only)
168        if !cfg!(msim) {
169            $crate::logging::assert_sometimes_antithesis!($expr, $message);
170        } else {
171            $crate::assert_sometimes_simtest!($expr, $message);
172        }
173    }};
174}
175
176mod tests {
177    #[test]
178    #[should_panic]
179    fn test_fatal() {
180        fatal!("This is a fatal error");
181    }
182
183    #[test]
184    #[should_panic]
185    fn test_debug_fatal() {
186        if cfg!(debug_assertions) {
187            debug_fatal!("This is a debug fatal error");
188        } else {
189            // pass in release mode as well
190            fatal!("This is a fatal error");
191        }
192    }
193
194    #[cfg(not(debug_assertions))]
195    #[test]
196    fn test_debug_fatal_release_mode() {
197        debug_fatal!("This is a debug fatal error");
198    }
199
200    #[test]
201    fn test_assert_sometimes_side_effects() {
202        let mut x = 0;
203
204        let mut inc = || {
205            x += 1;
206            true
207        };
208
209        assert_sometimes!(inc(), "");
210        assert_eq!(x, 1);
211    }
212}