moire-trace-capture 1.0.1

Frame-pointer backtrace capture for moire runtime instrumentation
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
use moire_trace_types::{
    BacktraceId, BacktraceRecord, InvariantError, ModuleId, ModulePath, RuntimeBase,
};
use std::error::Error;
use std::fmt;
use std::num::NonZeroUsize;
use std::sync::Once;

#[derive(Debug, Clone, Copy)]
pub struct CaptureOptions {
    pub max_frames: NonZeroUsize,
    pub skip_frames: usize,
}

impl Default for CaptureOptions {
    fn default() -> Self {
        Self {
            max_frames: NonZeroUsize::new(256)
                .expect("invariant violated: default max_frames must be non-zero"),
            skip_frames: 0,
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CapturedModule {
    pub id: ModuleId,
    pub path: ModulePath,
    pub runtime_base: RuntimeBase,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CapturedBacktrace {
    pub backtrace: BacktraceRecord,
    pub modules: Vec<CapturedModule>,
}

#[derive(Debug)]
pub enum CaptureError {
    UnsupportedPlatform {
        target_os: &'static str,
    },
    EmptyBacktrace,
    MissingModuleInfo {
        ip: u64,
    },
    MissingModulePath {
        ip: u64,
    },
    ZeroModuleBase {
        ip: u64,
    },
    IpBeforeModuleBase {
        ip: u64,
        module_base: RuntimeBase,
    },
    InvariantViolation {
        context: &'static str,
        source: InvariantError,
    },
}

impl CaptureError {
    fn invariant(context: &'static str, source: InvariantError) -> Self {
        Self::InvariantViolation { context, source }
    }
}

impl fmt::Display for CaptureError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::UnsupportedPlatform { target_os } => {
                write!(
                    f,
                    "unsupported platform for trace capture backend: {target_os}; only Unix targets are implemented"
                )
            }
            Self::EmptyBacktrace => write!(
                f,
                "invariant violated: captured backtrace must be non-empty"
            ),
            Self::MissingModuleInfo { ip } => {
                write!(
                    f,
                    "invariant violated: dladdr returned no module info for ip=0x{ip:x}"
                )
            }
            Self::MissingModulePath { ip } => {
                write!(
                    f,
                    "invariant violated: module path is required for ip=0x{ip:x}"
                )
            }
            Self::ZeroModuleBase { ip } => {
                write!(
                    f,
                    "invariant violated: module base must be non-zero for ip=0x{ip:x}"
                )
            }
            Self::IpBeforeModuleBase { ip, module_base } => {
                write!(
                    f,
                    "invariant violated: instruction pointer 0x{ip:x} is below module base 0x{:x}",
                    module_base.get()
                )
            }
            Self::InvariantViolation { context, source } => {
                write!(f, "invariant violated in {context}: {source}")
            }
        }
    }
}

impl Error for CaptureError {
    fn source(&self) -> Option<&(dyn Error + 'static)> {
        match self {
            Self::InvariantViolation { source, .. } => Some(source),
            _ => None,
        }
    }
}

static FRAME_POINTER_VALIDATION_ONCE: Once = Once::new();

// r[impl process.frame-pointer-validation]
pub fn validate_frame_pointers_or_panic() {
    FRAME_POINTER_VALIDATION_ONCE.call_once(|| {
        if let Err(reason) = platform::validate_frame_pointers_impl() {
            panic!(
                "frame-pointer validation failed: {reason}. \
recompile with -C force-frame-pointers=yes"
            );
        }
    });
}

// r[impl process.backtrace-capture]
pub fn capture_current(
    backtrace_id: BacktraceId,
    options: CaptureOptions,
) -> Result<CapturedBacktrace, CaptureError> {
    platform::capture_current_impl(backtrace_id, options)
}

#[cfg(unix)]
mod platform {
    use super::{CaptureError, CaptureOptions, CapturedBacktrace, CapturedModule};
    use moire_trace_types::{
        BacktraceId, BacktraceRecord, FrameKey, ModuleId, ModulePath, RelPc, RuntimeBase,
    };
    use std::collections::BTreeMap;
    use std::ffi::{CStr, c_void};
    use std::sync::{Mutex as StdMutex, OnceLock};

    pub fn validate_frame_pointers_impl() -> Result<(), String> {
        #[inline(never)]
        fn layer0() -> Result<(), String> {
            layer1()
        }
        #[inline(never)]
        fn layer1() -> Result<(), String> {
            layer2()
        }
        #[inline(never)]
        fn layer2() -> Result<(), String> {
            layer3()
        }
        #[inline(never)]
        fn layer3() -> Result<(), String> {
            layer4()
        }
        #[inline(never)]
        fn layer4() -> Result<(), String> {
            validate_frame_pointer_chain(6)
        }

        layer0()
    }

    fn validate_frame_pointer_chain(min_depth: usize) -> Result<(), String> {
        let mut frame_ptr = read_frame_pointer()?;
        if frame_ptr == 0 {
            return Err("current frame pointer is null".to_string());
        }

        let mut prev_frame_ptr = 0usize;
        let mut depth = 0usize;
        const MAX_FRAMES: usize = 4096;

        for _ in 0..MAX_FRAMES {
            if frame_ptr == 0 {
                break;
            }

            if frame_ptr % std::mem::align_of::<usize>() != 0 {
                return Err(format!("misaligned frame pointer 0x{frame_ptr:x}"));
            }

            if prev_frame_ptr != 0 && frame_ptr <= prev_frame_ptr {
                return Err(format!(
                    "frame pointer did not increase: current=0x{frame_ptr:x}, previous=0x{prev_frame_ptr:x}"
                ));
            }

            let next_frame_ptr = unsafe { *(frame_ptr as *const usize) };
            depth += 1;
            if next_frame_ptr == 0 {
                break;
            }

            prev_frame_ptr = frame_ptr;
            frame_ptr = next_frame_ptr;
        }

        if depth < min_depth {
            return Err(format!(
                "frame pointer chain too shallow: got {depth}, need at least {min_depth}"
            ));
        }

        Ok(())
    }

    #[cfg(target_arch = "x86_64")]
    fn read_frame_pointer() -> Result<usize, String> {
        let frame_ptr: usize;
        unsafe {
            core::arch::asm!(
                "mov {}, rbp",
                out(reg) frame_ptr,
                options(nomem, nostack, preserves_flags)
            );
        }
        Ok(frame_ptr)
    }

    #[cfg(target_arch = "aarch64")]
    fn read_frame_pointer() -> Result<usize, String> {
        let frame_ptr: usize;
        unsafe {
            core::arch::asm!(
                "mov {}, x29",
                out(reg) frame_ptr,
                options(nomem, nostack, preserves_flags)
            );
        }
        Ok(frame_ptr)
    }

    #[cfg(not(any(target_arch = "x86_64", target_arch = "aarch64")))]
    fn read_frame_pointer() -> Result<usize, String> {
        Err(format!(
            "unsupported architecture for frame pointer validation: {}",
            std::env::consts::ARCH
        ))
    }

    // r[impl process.backtrace-capture.impl]
    pub fn capture_current_impl(
        backtrace_id: BacktraceId,
        options: CaptureOptions,
    ) -> Result<CapturedBacktrace, CaptureError> {
        let raw_ips = collect_raw_ips(options)?;

        if raw_ips.is_empty() {
            return Err(CaptureError::EmptyBacktrace);
        }

        let mut modules_by_key: BTreeMap<(RuntimeBase, String), ModuleId> = BTreeMap::new();
        let mut modules = Vec::new();
        let mut frames = Vec::with_capacity(raw_ips.len());
        for ip in raw_ips {
            let module = module_info_for_ip(ip)?;

            let key = (module.runtime_base, module.path.clone());
            let module_id = if let Some(module_id) = modules_by_key.get(&key).copied() {
                module_id
            } else {
                let module_id =
                    ModuleId::next().map_err(|err| CaptureError::invariant("module_id", err))?;
                let module_path = ModulePath::new(module.path)
                    .map_err(|err| CaptureError::invariant("module_path", err))?;

                modules.push(CapturedModule {
                    id: module_id,
                    path: module_path,
                    runtime_base: module.runtime_base,
                });

                modules_by_key.insert(key, module_id);
                module_id
            };

            if ip < module.runtime_base.get() {
                return Err(CaptureError::IpBeforeModuleBase {
                    ip,
                    module_base: module.runtime_base,
                });
            }
            let rel_pc = RelPc::new(ip - module.runtime_base.get())
                .map_err(|err| CaptureError::invariant("rel_pc", err))?;

            frames.push(FrameKey { module_id, rel_pc });
        }

        let backtrace = BacktraceRecord::new(backtrace_id, frames)
            .map_err(|err| CaptureError::invariant("backtrace_record", err))?;

        Ok(CapturedBacktrace { backtrace, modules })
    }

    fn collect_raw_ips(options: CaptureOptions) -> Result<Vec<u64>, CaptureError> {
        let mut raw_ips = Vec::new();
        let mut skip_remaining = options.skip_frames;
        let mut frame_ptr =
            read_frame_pointer().map_err(|_| CaptureError::UnsupportedPlatform {
                target_os: std::env::consts::OS,
            })?;

        while frame_ptr != 0 && raw_ips.len() < options.max_frames.get() {
            if frame_ptr % std::mem::align_of::<usize>() != 0 {
                break;
            }

            let next_frame_ptr = unsafe { *(frame_ptr as *const usize) };
            let return_ip = unsafe { *((frame_ptr as *const usize).add(1)) };

            if return_ip != 0 {
                if skip_remaining > 0 {
                    skip_remaining -= 1;
                } else {
                    raw_ips.push(return_ip as u64);
                }
            }

            if next_frame_ptr == 0 || next_frame_ptr <= frame_ptr {
                break;
            }

            frame_ptr = next_frame_ptr;
        }

        Ok(raw_ips)
    }

    #[derive(Debug, Clone)]
    struct RawModuleInfo {
        runtime_base: RuntimeBase,
        path: String,
    }

    fn module_info_cache() -> &'static StdMutex<BTreeMap<u64, RawModuleInfo>> {
        static CACHE: OnceLock<StdMutex<BTreeMap<u64, RawModuleInfo>>> = OnceLock::new();
        CACHE.get_or_init(|| StdMutex::new(BTreeMap::new()))
    }

    fn module_info_for_ip(ip: u64) -> Result<RawModuleInfo, CaptureError> {
        let cached = {
            let Ok(cache) = module_info_cache().lock() else {
                panic!("module info cache mutex poisoned; cannot continue");
            };
            cache.get(&ip).cloned()
        };
        if let Some(info) = cached {
            return Ok(info);
        }

        let resolved = resolve_module_info_for_ip(ip)?;

        let Ok(mut cache) = module_info_cache().lock() else {
            panic!("module info cache mutex poisoned; cannot continue");
        };
        cache.insert(ip, resolved.clone());
        Ok(resolved)
    }

    fn resolve_module_info_for_ip(ip: u64) -> Result<RawModuleInfo, CaptureError> {
        let mut info = std::mem::MaybeUninit::<libc::Dl_info>::zeroed();
        let ok = unsafe { libc::dladdr(ip as usize as *const c_void, info.as_mut_ptr()) };
        if ok == 0 {
            return Err(CaptureError::MissingModuleInfo { ip });
        }

        let info = unsafe { info.assume_init() };
        if info.dli_fbase.is_null() {
            return Err(CaptureError::ZeroModuleBase { ip });
        }

        let runtime_base = RuntimeBase::new(info.dli_fbase as usize as u64)
            .map_err(|err| CaptureError::invariant("runtime_base", err))?;

        if info.dli_fname.is_null() {
            return Err(CaptureError::MissingModulePath { ip });
        }

        let path = unsafe { CStr::from_ptr(info.dli_fname) }
            .to_string_lossy()
            .into_owned();
        if path.is_empty() {
            return Err(CaptureError::MissingModulePath { ip });
        }

        Ok(RawModuleInfo { runtime_base, path })
    }
}

#[cfg(not(unix))]
mod platform {
    use super::{CaptureError, CaptureOptions, CapturedBacktrace};
    use moire_trace_types::BacktraceId;

    pub fn validate_frame_pointers_impl() -> Result<(), String> {
        Err(format!(
            "unsupported platform for trace capture backend: {}",
            std::env::consts::OS
        ))
    }

    pub fn capture_current_impl(
        _backtrace_id: BacktraceId,
        _options: CaptureOptions,
    ) -> Result<CapturedBacktrace, CaptureError> {
        Err(CaptureError::UnsupportedPlatform {
            target_os: std::env::consts::OS,
        })
    }
}