|
| 1 | +use std::sync::atomic::{AtomicBool, Ordering}; |
| 2 | + |
| 3 | +extern "C" { |
| 4 | + fn cpp_trampoline(func: extern "C" fn()) -> (); |
| 5 | +} |
| 6 | + |
| 7 | +#[test] |
| 8 | +fn smoke_test_cpp() { |
| 9 | + static RAN_ASSERTS: AtomicBool = AtomicBool::new(false); |
| 10 | + |
| 11 | + extern "C" fn assert_cpp_frames() { |
| 12 | + let mut physical_frames = Vec::new(); |
| 13 | + backtrace::trace(|cx| { |
| 14 | + physical_frames.push(cx.ip()); |
| 15 | + |
| 16 | + // We only want to capture this closure's frame, assert_cpp_frames, |
| 17 | + // space::templated_trampoline, and cpp_trampoline. Those are |
| 18 | + // logical frames, which might be inlined into fewer physical |
| 19 | + // frames, so we may end up with extra logical frames after |
| 20 | + // resolving these. |
| 21 | + physical_frames.len() < 5 |
| 22 | + }); |
| 23 | + |
| 24 | + let names: Vec<_> = physical_frames |
| 25 | + .into_iter() |
| 26 | + .flat_map(|ip| { |
| 27 | + let mut logical_frame_names = vec![]; |
| 28 | + |
| 29 | + backtrace::resolve(ip, |sym| { |
| 30 | + let sym_name = sym.name().expect("Should have a symbol name"); |
| 31 | + let demangled = sym_name.to_string(); |
| 32 | + logical_frame_names.push(demangled); |
| 33 | + }); |
| 34 | + |
| 35 | + assert!( |
| 36 | + !logical_frame_names.is_empty(), |
| 37 | + "Should have resolved at least one symbol for the physical frame" |
| 38 | + ); |
| 39 | + |
| 40 | + logical_frame_names |
| 41 | + }) |
| 42 | + // Skip the backtrace::trace closure and assert_cpp_frames, and then |
| 43 | + // take the two C++ frame names. |
| 44 | + .skip_while(|name| !name.contains("trampoline")) |
| 45 | + .take(2) |
| 46 | + .collect(); |
| 47 | + |
| 48 | + println!("actual names = {names:#?}"); |
| 49 | + |
| 50 | + let expected = |
| 51 | + ["void space::templated_trampoline<void (*)()>(void (*)())", "cpp_trampoline"]; |
| 52 | + println!("expected names = {expected:#?}"); |
| 53 | + |
| 54 | + assert_eq!(names.len(), expected.len()); |
| 55 | + for (actual, expected) in names.iter().zip(expected.iter()) { |
| 56 | + assert_eq!(actual, expected); |
| 57 | + } |
| 58 | + |
| 59 | + RAN_ASSERTS.store(true, Ordering::SeqCst); |
| 60 | + } |
| 61 | + |
| 62 | + assert!(!RAN_ASSERTS.load(Ordering::SeqCst)); |
| 63 | + unsafe { |
| 64 | + cpp_trampoline(assert_cpp_frames); |
| 65 | + } |
| 66 | + assert!(RAN_ASSERTS.load(Ordering::SeqCst)); |
| 67 | +} |
0 commit comments