running-process-probe 4.8.0

Sidecar / file-hook tier for running-process (#539 follow-up #551)
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
//! Windows capture backend: `SuspendThread` → `GetThreadContext` → bounded
//! stack copy → `ResumeThread`.
//!
//! The ordering here is load-bearing. See the module docs in
//! [`super`] for why nothing but a register read and a `memcpy` may happen
//! while a thread is suspended.

#![allow(unsafe_code)] // Thread enumeration, suspension, and context reads are FFI-only.

use std::io;
use std::time::Instant;

use winapi::shared::minwindef::FALSE;
use winapi::um::handleapi::{CloseHandle, INVALID_HANDLE_VALUE};
use winapi::um::memoryapi::VirtualQuery;
use winapi::um::processthreadsapi::{
    GetCurrentProcessId, GetCurrentThreadId, GetThreadContext, OpenThread, ResumeThread,
    SuspendThread,
};
use winapi::um::tlhelp32::{
    CreateToolhelp32Snapshot, Thread32First, Thread32Next, TH32CS_SNAPTHREAD, THREADENTRY32,
};
use winapi::um::winnt::{
    CONTEXT, MEMORY_BASIC_INFORMATION, MEM_COMMIT, THREAD_GET_CONTEXT,
    THREAD_QUERY_LIMITED_INFORMATION, THREAD_SUSPEND_RESUME,
};

/// Register set to request. x86_64 exposes a `CONTEXT_FULL` alias; aarch64
/// does not, so compose control+integer, which is what the stack walk needs.
#[cfg(target_arch = "x86_64")]
const WANTED_CONTEXT: u32 = winapi::um::winnt::CONTEXT_FULL;
#[cfg(target_arch = "aarch64")]
const WANTED_CONTEXT: u32 = winapi::um::winnt::CONTEXT_CONTROL | winapi::um::winnt::CONTEXT_INTEGER;

/// `CONTEXT` with the alignment `GetThreadContext` requires.
///
/// winapi's `CONTEXT` carries no alignment attribute (its aarch64 definition
/// even says `// FIXME align 16`), but the API requires 16-byte alignment on
/// both architectures and fails with `ERROR_NOACCESS` otherwise. A bare local
/// is aligned only by accident of stack layout — which is exactly the kind of
/// bug that appears when unrelated code shifts the frame, so pin it here.
#[repr(align(16))]
struct AlignedContext(CONTEXT);

/// Registers the unwinder needs, named per architecture.
struct CapturedRegs {
    sp: u64,
    ip: u64,
    fp: u64,
    /// Link register. `Some` only on aarch64.
    lr: Option<u64>,
}

/// Pull the unwind-relevant registers out of a `CONTEXT`.
///
/// The whole architecture difference in this file lives here.
#[cfg(target_arch = "x86_64")]
fn captured_regs(context: &CONTEXT) -> CapturedRegs {
    CapturedRegs {
        sp: context.Rsp,
        ip: context.Rip,
        fp: context.Rbp,
        // x86_64 has no link register; the return address is on the stack.
        lr: None,
    }
}

/// # Safety
///
/// Reads the aarch64 `CONTEXT` union, which is initialized by
/// `GetThreadContext` before this is called.
#[cfg(target_arch = "aarch64")]
fn captured_regs(context: &CONTEXT) -> CapturedRegs {
    // Fp/Lr live in the X-register union rather than as named top-level
    // fields the way Rbp does on x86_64.
    let s = unsafe { context.u.s() };
    CapturedRegs {
        sp: context.Sp,
        ip: context.Pc,
        fp: s.Fp,
        // Load-bearing: a leaf frame's return address is in LR, not on the
        // stack, so dropping it loses the innermost frame.
        lr: Some(s.Lr),
    }
}

use super::{CaptureKind, Snapshot, SnapshotConfig, SnapshotError, SnapshotStats, ThreadSample};

/// `SuspendThread` returns this on failure.
const SUSPEND_FAILED: u32 = u32::MAX;

/// Enumerate sibling thread ids via a toolhelp snapshot.
///
/// Excludes the calling thread — suspending yourself never returns.
fn sibling_thread_ids() -> io::Result<Vec<u32>> {
    let pid = unsafe { GetCurrentProcessId() };
    let self_tid = unsafe { GetCurrentThreadId() };

    let snap = unsafe { CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, 0) };
    if snap == INVALID_HANDLE_VALUE {
        return Err(io::Error::last_os_error());
    }

    let mut ids = Vec::new();
    let mut entry: THREADENTRY32 = unsafe { std::mem::zeroed() };
    entry.dwSize = std::mem::size_of::<THREADENTRY32>() as u32;

    let mut ok = unsafe { Thread32First(snap, &mut entry) };
    while ok != FALSE {
        if entry.th32OwnerProcessID == pid && entry.th32ThreadID != self_tid {
            ids.push(entry.th32ThreadID);
        }
        entry.dwSize = std::mem::size_of::<THREADENTRY32>() as u32;
        ok = unsafe { Thread32Next(snap, &mut entry) };
    }

    unsafe { CloseHandle(snap) };
    Ok(ids)
}

/// Highest address we may read from, given a stack pointer.
///
/// The stack region's committed extent bounds the copy. Without this a
/// fixed-size read past the top of the stack would touch unmapped pages and
/// fault — while holding a thread suspended.
fn committed_stack_top(sp: u64) -> Option<u64> {
    let mut info: MEMORY_BASIC_INFORMATION = unsafe { std::mem::zeroed() };
    let queried = unsafe {
        VirtualQuery(
            sp as *const _,
            &mut info,
            std::mem::size_of::<MEMORY_BASIC_INFORMATION>(),
        )
    };
    if queried == 0 || info.State != MEM_COMMIT {
        return None;
    }
    Some(info.BaseAddress as u64 + info.RegionSize as u64)
}

/// Capture one thread. Returns `None` if it could not be captured, which the
/// caller counts as a drop rather than failing the whole snapshot — a thread
/// exiting mid-capture is normal.
///
/// `scratch` is preallocated by the caller so the suspend window contains no
/// allocation.
fn capture_thread(tid: u32, max_stack_bytes: usize, scratch: &mut [u8]) -> Option<ThreadSample> {
    let handle = unsafe {
        OpenThread(
            THREAD_SUSPEND_RESUME | THREAD_GET_CONTEXT | THREAD_QUERY_LIMITED_INFORMATION,
            FALSE,
            tid,
        )
    };
    if handle.is_null() {
        return None;
    }

    // ---- suspend window opens ------------------------------------------
    // Only a register read and a bounded memcpy may happen from here until
    // ResumeThread. No allocation, no locks, no logging.
    let suspended = unsafe { SuspendThread(handle) };
    if suspended == SUSPEND_FAILED {
        unsafe { CloseHandle(handle) };
        return None;
    }

    let mut aligned: AlignedContext = unsafe { std::mem::zeroed() };
    aligned.0.ContextFlags = WANTED_CONTEXT;
    let got_context = unsafe { GetThreadContext(handle, &mut aligned.0) };

    let mut copied = 0usize;
    let regs = if got_context != FALSE {
        let regs = captured_regs(&aligned.0);
        let sp = regs.sp;
        if let Some(top) = committed_stack_top(sp) {
            let available = top.saturating_sub(sp) as usize;
            let want = available.min(max_stack_bytes).min(scratch.len());
            if want > 0 {
                unsafe {
                    std::ptr::copy_nonoverlapping(sp as *const u8, scratch.as_mut_ptr(), want);
                }
                copied = want;
            }
        }
        regs
    } else {
        CapturedRegs {
            sp: 0,
            ip: 0,
            fp: 0,
            lr: None,
        }
    };

    unsafe { ResumeThread(handle) };
    // ---- suspend window closes -----------------------------------------

    unsafe { CloseHandle(handle) };

    if got_context == FALSE {
        return None;
    }

    // Allocation happens here, after the thread is running again.
    let stack_bytes = scratch[..copied].to_vec();
    let truncated = copied == max_stack_bytes;

    Some(ThreadSample {
        os_tid: u64::from(tid),
        stack_pointer: regs.sp,
        instruction_pointer: regs.ip,
        frame_pointer: regs.fp,
        link_register: regs.lr,
        stack_bytes,
        truncated,
        kind: CaptureKind::RawContext,
        frames: Vec::new(),
    })
}

/// Capture every sibling thread.
pub fn capture(config: &SnapshotConfig) -> Result<Snapshot, SnapshotError> {
    let tids = sibling_thread_ids()?;

    // Preallocated once, reused per thread, so no allocator call occurs inside
    // any suspend window.
    let mut scratch = vec![0u8; config.max_stack_bytes];

    let mut threads = Vec::with_capacity(tids.len());
    let mut dropped = 0u32;
    let started = Instant::now();

    for tid in &tids {
        match capture_thread(*tid, config.max_stack_bytes, &mut scratch) {
            Some(sample) => threads.push(sample),
            None => dropped += 1,
        }
    }

    let pause_nanos = started.elapsed().as_nanos().min(u128::from(u64::MAX)) as u64;

    Ok(Snapshot {
        stats: SnapshotStats {
            threads_total: tids.len() as u32,
            threads_captured: threads.len() as u32,
            threads_dropped: dropped,
            pause_nanos,
        },
        threads,
        frames_resolved: false,
    })
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::sync::atomic::{AtomicBool, AtomicU64, Ordering};
    use std::sync::{Arc, Mutex};
    use std::time::Duration;

    /// The probe thread must never appear in its own enumeration.
    #[test]
    fn enumeration_excludes_the_calling_thread() {
        let self_tid = unsafe { GetCurrentThreadId() };
        let ids = sibling_thread_ids().expect("enumerate");
        assert!(
            !ids.contains(&self_tid),
            "suspending the calling thread would deadlock immediately"
        );
    }

    /// All spawned threads must show up in the snapshot.
    #[test]
    fn snapshot_sees_every_spawned_thread() {
        const N: usize = 4;
        let stop = Arc::new(AtomicBool::new(false));
        let running = Arc::new(AtomicU64::new(0));

        let handles: Vec<_> = (0..N)
            .map(|_| {
                let stop = Arc::clone(&stop);
                let running = Arc::clone(&running);
                std::thread::spawn(move || {
                    running.fetch_add(1, Ordering::SeqCst);
                    while !stop.load(Ordering::Relaxed) {
                        std::thread::sleep(Duration::from_millis(5));
                    }
                })
            })
            .collect();

        while running.load(Ordering::SeqCst) < N as u64 {
            std::thread::sleep(Duration::from_millis(5));
        }

        let snap = capture(&SnapshotConfig::default()).expect("capture");
        assert!(
            snap.stats.threads_captured >= N as u32,
            "expected at least the {N} spawned threads, got {:?}",
            snap.stats
        );

        stop.store(true, Ordering::Relaxed);
        for h in handles {
            h.join().unwrap();
        }
    }

    /// Every thread must be running again after the capture.
    ///
    /// A missed `ResumeThread` leaves the process wedged, and the symptom
    /// would otherwise appear far from the cause.
    #[test]
    fn threads_make_progress_after_capture() {
        let stop = Arc::new(AtomicBool::new(false));
        let ticks = Arc::new(AtomicU64::new(0));

        let worker = {
            let stop = Arc::clone(&stop);
            let ticks = Arc::clone(&ticks);
            std::thread::spawn(move || {
                while !stop.load(Ordering::Relaxed) {
                    ticks.fetch_add(1, Ordering::Relaxed);
                    std::thread::sleep(Duration::from_millis(1));
                }
            })
        };

        while ticks.load(Ordering::Relaxed) == 0 {
            std::thread::sleep(Duration::from_millis(1));
        }

        capture(&SnapshotConfig::default()).expect("capture");

        let before = ticks.load(Ordering::Relaxed);
        let deadline = Instant::now() + Duration::from_secs(5);
        let mut progressed = false;
        while Instant::now() < deadline {
            if ticks.load(Ordering::Relaxed) > before {
                progressed = true;
                break;
            }
            std::thread::sleep(Duration::from_millis(10));
        }

        stop.store(true, Ordering::Relaxed);
        worker.join().unwrap();
        assert!(
            progressed,
            "a thread stopped ticking after capture — it was not resumed"
        );
    }

    /// The capture must not need any lock the application holds.
    ///
    /// A thread is suspended while holding a mutex that the capturing thread
    /// never touches; the snapshot must still complete. If the capture path
    /// allocated or logged inside the suspend window, this is the shape of
    /// test that catches the resulting deadlock.
    #[test]
    fn capture_completes_while_a_thread_holds_an_app_lock() {
        let lock = Arc::new(Mutex::new(0u64));
        let stop = Arc::new(AtomicBool::new(false));
        let holding = Arc::new(AtomicBool::new(false));

        let hog = {
            let lock = Arc::clone(&lock);
            let stop = Arc::clone(&stop);
            let holding = Arc::clone(&holding);
            std::thread::spawn(move || {
                let _guard = lock.lock().unwrap();
                holding.store(true, Ordering::SeqCst);
                while !stop.load(Ordering::Relaxed) {
                    std::thread::sleep(Duration::from_millis(5));
                }
            })
        };

        while !holding.load(Ordering::SeqCst) {
            std::thread::sleep(Duration::from_millis(5));
        }

        let snap = capture(&SnapshotConfig::default()).expect("capture must not block");
        assert!(snap.stats.threads_captured > 0);

        stop.store(true, Ordering::Relaxed);
        hog.join().unwrap();
    }

    /// The copy honors its bound.
    #[test]
    fn stack_copy_respects_the_configured_cap() {
        let cfg = SnapshotConfig {
            max_stack_bytes: 4096,
        };
        let stop = Arc::new(AtomicBool::new(false));
        let worker = {
            let stop = Arc::clone(&stop);
            std::thread::spawn(move || {
                while !stop.load(Ordering::Relaxed) {
                    std::thread::sleep(Duration::from_millis(5));
                }
            })
        };
        std::thread::sleep(Duration::from_millis(20));

        let snap = capture(&cfg).expect("capture");
        for sample in &snap.threads {
            assert!(
                sample.stack_bytes.len() <= cfg.max_stack_bytes,
                "copied {} bytes, cap is {}",
                sample.stack_bytes.len(),
                cfg.max_stack_bytes
            );
        }

        stop.store(true, Ordering::Relaxed);
        worker.join().unwrap();
    }

    /// Captures are raw until unwinding lands.
    #[test]
    fn samples_are_marked_raw_and_unresolved() {
        let snap = capture(&SnapshotConfig::default()).expect("capture");
        assert!(!snap.frames_resolved);
        for sample in &snap.threads {
            assert_eq!(sample.kind, CaptureKind::RawContext);
        }
    }
}