|
| 1 | +#[macro_use] |
| 2 | +mod shared; |
| 3 | + |
| 4 | +use runner_shared::artifacts::MemtrackEventKind; |
| 5 | +use std::collections::HashSet; |
| 6 | +use std::path::{Path, PathBuf}; |
| 7 | +use std::process::Command; |
| 8 | +use tempfile::TempDir; |
| 9 | + |
| 10 | +fn compile_shared(source: &str, name: &str, dir: &Path) -> PathBuf { |
| 11 | + let src = dir.join(format!("{name}.c")); |
| 12 | + std::fs::write(&src, source).expect("write source"); |
| 13 | + let out = dir.join(format!("{name}.so")); |
| 14 | + let ok = Command::new("gcc") |
| 15 | + .args(["-shared", "-fPIC", "-o"]) |
| 16 | + .arg(&out) |
| 17 | + .arg(&src) |
| 18 | + .status() |
| 19 | + .expect("run gcc") |
| 20 | + .success(); |
| 21 | + assert!(ok, "failed to compile {name}.so"); |
| 22 | + out |
| 23 | +} |
| 24 | + |
| 25 | +fn compile_exe(source: &str, name: &str, dir: &Path, libs: &[&str]) -> PathBuf { |
| 26 | + let src = dir.join(format!("{name}.c")); |
| 27 | + std::fs::write(&src, source).expect("write source"); |
| 28 | + let out = dir.join(name); |
| 29 | + let ok = Command::new("gcc") |
| 30 | + .arg("-o") |
| 31 | + .arg(&out) |
| 32 | + .arg(&src) |
| 33 | + .args(libs) |
| 34 | + .status() |
| 35 | + .expect("run gcc") |
| 36 | + .success(); |
| 37 | + assert!(ok, "failed to compile {name}"); |
| 38 | + out |
| 39 | +} |
| 40 | + |
| 41 | +/// dlopen -> fentry -> SIGSTOP -> resolve -> classify -> attach -> SIGCONT -> |
| 42 | +/// first allocation captured. The dlopen'd allocator's 100 allocations must all |
| 43 | +/// be captured, proving the attach completes before the child's first alloc. |
| 44 | +#[test_with::env(GITHUB_ACTIONS)] |
| 45 | +#[test_log::test] |
| 46 | +fn test_dlopen_allocator() -> Result<(), Box<dyn std::error::Error>> { |
| 47 | + let dir = TempDir::new()?; |
| 48 | + let lib = compile_shared( |
| 49 | + include_str!("../testdata/dlopen/fake_mimalloc.c"), |
| 50 | + "libfake_mimalloc", |
| 51 | + dir.path(), |
| 52 | + ); |
| 53 | + let exe = compile_exe( |
| 54 | + include_str!("../testdata/dlopen/dlopen_alloc.c"), |
| 55 | + "dlopen_alloc", |
| 56 | + dir.path(), |
| 57 | + &["-ldl"], |
| 58 | + ); |
| 59 | + |
| 60 | + let mut cmd = Command::new(&exe); |
| 61 | + cmd.arg(&lib); |
| 62 | + let (events, thread_handle) = shared::track_command(cmd)?; |
| 63 | + |
| 64 | + let malloc_addrs: HashSet<u64> = events |
| 65 | + .iter() |
| 66 | + .filter_map(|e| match e.kind { |
| 67 | + MemtrackEventKind::Malloc { size: 4242 } => Some(e.addr), |
| 68 | + _ => None, |
| 69 | + }) |
| 70 | + .collect(); |
| 71 | + let malloc_count = events |
| 72 | + .iter() |
| 73 | + .filter(|e| matches!(e.kind, MemtrackEventKind::Malloc { size: 4242 })) |
| 74 | + .count(); |
| 75 | + let free_count = events |
| 76 | + .iter() |
| 77 | + .filter(|e| matches!(e.kind, MemtrackEventKind::Free) && malloc_addrs.contains(&e.addr)) |
| 78 | + .count(); |
| 79 | + |
| 80 | + assert_eq!(malloc_count, 100, "expected 100 mi_malloc(4242) events"); |
| 81 | + assert_eq!( |
| 82 | + free_count, 100, |
| 83 | + "expected 100 mi_free events for tracked addrs" |
| 84 | + ); |
| 85 | + |
| 86 | + thread_handle.join().unwrap(); |
| 87 | + Ok(()) |
| 88 | +} |
| 89 | + |
| 90 | +/// Two threads dlopen distinct allocator libs concurrently. Both libs' 100 |
| 91 | +/// allocations each must be captured, exercising concurrent stop-the-world. |
| 92 | +#[test_with::env(GITHUB_ACTIONS)] |
| 93 | +#[test_log::test] |
| 94 | +fn test_thread_dlopen() -> Result<(), Box<dyn std::error::Error>> { |
| 95 | + let dir = TempDir::new()?; |
| 96 | + let mimalloc = compile_shared( |
| 97 | + include_str!("../testdata/dlopen/fake_mimalloc.c"), |
| 98 | + "libfake_mimalloc", |
| 99 | + dir.path(), |
| 100 | + ); |
| 101 | + let jemalloc = compile_shared( |
| 102 | + include_str!("../testdata/dlopen/fake_jemalloc.c"), |
| 103 | + "libfake_jemalloc", |
| 104 | + dir.path(), |
| 105 | + ); |
| 106 | + let exe = compile_exe( |
| 107 | + include_str!("../testdata/dlopen/thread_dlopen.c"), |
| 108 | + "thread_dlopen", |
| 109 | + dir.path(), |
| 110 | + &["-ldl", "-lpthread"], |
| 111 | + ); |
| 112 | + |
| 113 | + let mut cmd = Command::new(&exe); |
| 114 | + cmd.arg(&mimalloc).arg(&jemalloc); |
| 115 | + let (events, thread_handle) = shared::track_command(cmd)?; |
| 116 | + |
| 117 | + let m4242 = events |
| 118 | + .iter() |
| 119 | + .filter(|e| matches!(e.kind, MemtrackEventKind::Malloc { size: 4242 })) |
| 120 | + .count(); |
| 121 | + let m4243 = events |
| 122 | + .iter() |
| 123 | + .filter(|e| matches!(e.kind, MemtrackEventKind::Malloc { size: 4243 })) |
| 124 | + .count(); |
| 125 | + |
| 126 | + assert_eq!(m4242, 100, "expected 100 mi_malloc(4242) events"); |
| 127 | + assert_eq!(m4243, 100, "expected 100 je_malloc(4243) events"); |
| 128 | + |
| 129 | + thread_handle.join().unwrap(); |
| 130 | + Ok(()) |
| 131 | +} |
0 commit comments