aprender-profile 0.32.0

Pure Rust system call tracer with source-aware correlation for Rust binaries
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
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
//! DWARF debug info parsing for source correlation
//!
//! Sprint 5-6: Map instruction pointers to source <file:line> using DWARF .`debug_line`
//!
//! Uses addr2line crate for robust DWARF parsing

use anyhow::{Context, Result};
use object::{Object, ObjectSection};
use std::fs::File;
use std::path::Path;

/// Source location information
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SourceLocation {
    /// Source file path
    pub file: String,
    /// Line number
    pub line: u32,
    /// Column number (if available)
    pub column: Option<u32>,
    /// Function name (if available)
    pub function: Option<String>,
}

/// DWARF debug info context for a binary
///
/// Sprint 5-6: Full implementation using addr2line crate
pub struct DwarfContext {
    /// addr2line context for DWARF lookups
    context: addr2line::Context<gimli::EndianRcSlice<gimli::RunTimeEndian>>,
}

impl std::fmt::Debug for DwarfContext {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("DwarfContext").field("context", &"<addr2line context>").finish()
    }
}

impl DwarfContext {
    /// Load DWARF debug info from an ELF binary
    ///
    /// Sprint 5-6: Full implementation using addr2line + object crates
    ///
    /// # Allows
    /// `unsafe_code` — `Mmap::map` requires unsafe for memory-mapped I/O.
    #[allow(unsafe_code)]
    pub fn load(binary_path: &Path) -> Result<Self> {
        // Verify binary exists
        if !binary_path.exists() {
            anyhow::bail!("Binary does not exist: {}", binary_path.display());
        }

        // Open and parse ELF binary
        let file = File::open(binary_path)
            .with_context(|| format!("Failed to open binary: {}", binary_path.display()))?;

        // SAFETY: File is valid and open; memory mapping is read-only and file stays open for mmap lifetime
        let mmap = unsafe { memmap2::Mmap::map(&file) }.context("Failed to memory-map binary")?;

        let object = object::File::parse(&*mmap).context("Failed to parse ELF binary")?;

        // Load DWARF sections from object file
        let endian = if object.is_little_endian() {
            gimli::RunTimeEndian::Little
        } else {
            gimli::RunTimeEndian::Big
        };

        // Helper to load a DWARF section
        let load_section =
            |id: gimli::SectionId| -> Result<gimli::EndianRcSlice<gimli::RunTimeEndian>> {
                let data = object
                    .section_by_name(id.name())
                    .and_then(|section| section.uncompressed_data().ok())
                    .unwrap_or(std::borrow::Cow::Borrowed(&[]));
                // Convert Cow<[u8]> to Rc<[u8]> by converting to owned Vec first
                let bytes: std::rc::Rc<[u8]> = std::rc::Rc::from(data.into_owned());
                Ok(gimli::EndianRcSlice::new(bytes, endian))
            };

        // Load all DWARF sections
        let dwarf = gimli::Dwarf::load(&load_section)
            .context("Failed to load DWARF sections - binary may not have debug symbols. Compile with -g flag.")?;

        // Create addr2line context from DWARF
        let context =
            addr2line::Context::from_dwarf(dwarf).context("Failed to create DWARF context")?;

        Ok(Self { context })
    }

    /// Look up source location for an instruction pointer
    ///
    /// Sprint 5-6: Full implementation with DWARF .`debug_line` parsing
    /// Returns the first valid source location found in DWARF
    pub fn lookup(&self, ip: u64) -> Result<Option<SourceLocation>> {
        // Try multiple IP offsets to find user code
        // At syscall-entry-stop, IP might be in libc, so we try backing up
        for offset in [0, 1, 2, 4, 8, 16] {
            let adjusted_ip = ip.saturating_sub(offset);

            // Look up location in DWARF data
            let location = match self.context.find_location(adjusted_ip) {
                Ok(Some(loc)) => loc,
                Ok(None) => continue,
                Err(_) => continue,
            };

            // Extract file and line
            let file = match location.file {
                Some(f) => {
                    // Filter out libc/std paths - we want user code
                    if f.contains("/rustc/") || f.contains("library/") {
                        continue;
                    }
                    f
                }
                None => continue,
            };

            let line = location.line.unwrap_or(0);
            if line == 0 {
                continue; // Invalid line number
            }

            let column = location.column;

            // Try to find function name using find_frames
            let mut function_name = None;
            let frames_result = self.context.find_frames(adjusted_ip);

            // addr2line returns LookupResult which needs to be handled with load()
            if let Ok(mut frames_iter) = frames_result.skip_all_loads() {
                if let Ok(Some(frame)) = frames_iter.next() {
                    if let Some(func) = frame.function {
                        // Get raw name as Cow<str>
                        let raw_name = func.raw_name().ok();
                        if let Some(name) = raw_name {
                            function_name = Some(name.to_string());
                        }
                    }
                }
            }

            return Ok(Some(SourceLocation {
                file: file.to_string(),
                line,
                column,
                function: function_name,
            }));
        }

        // No valid location found
        Ok(None)
    }
}

// Compile-time thread-safety verification (Sprint 59)
static_assertions::assert_impl_all!(SourceLocation: Send, Sync);

#[cfg(test)]
mod tests {
    use super::*;
    use std::fs;
    use std::process::Command;
    use tempfile::TempDir;

    fn compile_test_binary() -> (TempDir, std::path::PathBuf) {
        let temp_dir = TempDir::new().expect("test");
        let src_file = temp_dir.path().join("test.rs");
        let bin_file = temp_dir.path().join("test_bin");

        fs::write(&src_file, "fn main() { println!(\"test\"); }").expect("test");

        Command::new("rustc")
            .arg(&src_file)
            .arg("-o")
            .arg(&bin_file)
            .arg("-g")
            .status()
            .expect("test");

        (temp_dir, bin_file)
    }

    #[test]
    fn test_dwarf_context_loads() {
        let (_temp_dir, bin_file) = compile_test_binary();
        let result = DwarfContext::load(&bin_file);
        assert!(result.is_ok(), "Should load DWARF context: {:?}", result.err());
    }

    #[test]
    fn test_dwarf_lookup_returns_option() {
        let (_temp_dir, bin_file) = compile_test_binary();
        let ctx = DwarfContext::load(&bin_file).expect("test");
        let result = ctx.lookup(0x1000);
        assert!(result.is_ok(), "Lookup should not crash: {:?}", result.err());
    }

    #[test]
    fn test_dwarf_load_nonexistent_file() {
        // Test error handling: nonexistent file
        let result = DwarfContext::load(std::path::Path::new("/nonexistent/binary"));
        assert!(result.is_err(), "Should fail for nonexistent file");
        let err = result.unwrap_err().to_string();
        assert!(err.contains("does not exist"), "Error should mention file doesn't exist: {}", err);
    }

    #[test]
    fn test_dwarf_load_invalid_binary() {
        // Test error handling: invalid ELF file
        let temp_dir = TempDir::new().expect("test");
        let invalid_file = temp_dir.path().join("invalid.bin");
        fs::write(&invalid_file, b"not a valid ELF file").expect("test");

        let result = DwarfContext::load(&invalid_file);
        assert!(result.is_err(), "Should fail for invalid ELF");
    }

    #[test]
    fn test_dwarf_load_no_debug_symbols() {
        // Test error handling: binary without debug symbols
        let temp_dir = TempDir::new().expect("test");
        let src_file = temp_dir.path().join("test.rs");
        let bin_file = temp_dir.path().join("test_bin_stripped");

        fs::write(&src_file, "fn main() {}").expect("test");

        // Compile without -g (no debug symbols)
        Command::new("rustc")
            .arg(&src_file)
            .arg("-o")
            .arg(&bin_file)
            .arg("-C")
            .arg("strip=symbols")
            .status()
            .expect("test");

        let result = DwarfContext::load(&bin_file);
        // May succeed but with no useful DWARF data, or may fail
        // Either is acceptable
        let _ = result;
    }

    #[test]
    fn test_dwarf_lookup_with_valid_address() {
        // Test lookup with a potentially valid address
        let (_temp_dir, bin_file) = compile_test_binary();
        let ctx = DwarfContext::load(&bin_file).expect("test");

        // Try multiple addresses to increase coverage
        for offset in [0, 1, 2, 4, 8, 16] {
            let _ = ctx.lookup(0x1000 + offset);
        }
    }

    #[test]
    fn test_dwarf_lookup_zero_address() {
        let (_temp_dir, bin_file) = compile_test_binary();
        let ctx = DwarfContext::load(&bin_file).expect("test");
        let result = ctx.lookup(0);
        assert!(result.is_ok(), "Lookup of zero address should not crash");
    }

    #[test]
    fn test_dwarf_lookup_high_address() {
        let (_temp_dir, bin_file) = compile_test_binary();
        let ctx = DwarfContext::load(&bin_file).expect("test");
        let result = ctx.lookup(0xFFFFFFFFFFFF);
        assert!(result.is_ok(), "Lookup of high address should not crash");
    }

    #[test]
    fn test_source_location_clone() {
        // Test SourceLocation Clone and PartialEq
        let loc1 = SourceLocation {
            file: "test.rs".to_string(),
            line: 42,
            column: Some(10),
            function: Some("main".to_string()),
        };
        let loc2 = loc1.clone();
        assert_eq!(loc1, loc2, "Cloned SourceLocation should be equal");
    }

    #[test]
    fn test_source_location_debug() {
        // Test SourceLocation Debug impl
        let loc = SourceLocation {
            file: "test.rs".to_string(),
            line: 42,
            column: Some(10),
            function: Some("main".to_string()),
        };
        let debug_str = format!("{:?}", loc);
        assert!(debug_str.contains("test.rs"), "Debug should contain file: {}", debug_str);
        assert!(debug_str.contains("42"), "Debug should contain line: {}", debug_str);
    }

    #[test]
    fn test_source_location_no_column() {
        let loc =
            SourceLocation { file: "lib.rs".to_string(), line: 100, column: None, function: None };
        assert_eq!(loc.file, "lib.rs");
        assert_eq!(loc.line, 100);
        assert_eq!(loc.column, None);
        assert_eq!(loc.function, None);
    }

    #[test]
    fn test_source_location_equality() {
        let loc1 = SourceLocation {
            file: "main.rs".to_string(),
            line: 1,
            column: Some(5),
            function: Some("foo".to_string()),
        };
        let loc2 = SourceLocation {
            file: "main.rs".to_string(),
            line: 1,
            column: Some(5),
            function: Some("foo".to_string()),
        };
        let loc3 = SourceLocation {
            file: "main.rs".to_string(),
            line: 2,
            column: Some(5),
            function: Some("foo".to_string()),
        };
        assert_eq!(loc1, loc2);
        assert_ne!(loc1, loc3);
    }

    #[test]
    fn test_dwarf_context_debug() {
        let (_temp_dir, bin_file) = compile_test_binary();
        let ctx = DwarfContext::load(&bin_file).expect("test");
        let debug_str = format!("{:?}", ctx);
        assert!(debug_str.contains("DwarfContext"));
        assert!(debug_str.contains("addr2line context"));
    }

    #[test]
    fn test_dwarf_lookup_multiple_addresses() {
        let (_temp_dir, bin_file) = compile_test_binary();
        let ctx = DwarfContext::load(&bin_file).expect("test");

        // Test a range of addresses
        for addr in [0x1000, 0x2000, 0x3000, 0x4000, 0x5000] {
            let result = ctx.lookup(addr);
            assert!(result.is_ok(), "Lookup should not crash for addr {:#x}", addr);
        }
    }

    #[test]
    fn test_dwarf_lookup_negative_offset() {
        let (_temp_dir, bin_file) = compile_test_binary();
        let ctx = DwarfContext::load(&bin_file).expect("test");

        // Test lookup with addresses that might underflow with offset
        let result = ctx.lookup(0);
        assert!(result.is_ok());

        let result = ctx.lookup(1);
        assert!(result.is_ok());
    }

    #[test]
    fn test_dwarf_load_real_binary() {
        // Test loading DWARF from a real binary (this executable)
        let exe_path = std::env::current_exe().expect("Failed to get current exe");

        // Try to load DWARF - may succeed or fail depending on if we have debug info
        let result = DwarfContext::load(&exe_path);

        // Either way, it shouldn't panic
        match result {
            Ok(ctx) => {
                // If we loaded it, try a lookup
                let lookup_result = ctx.lookup(0x1000);
                assert!(lookup_result.is_ok());
            }
            Err(e) => {
                // If it failed, error should be reasonable
                let err_msg = e.to_string();
                assert!(!err_msg.is_empty(), "Error message should not be empty");
            }
        }
    }

    #[test]
    fn test_source_location_with_column() {
        let loc = SourceLocation {
            file: "test.rs".to_string(),
            line: 10,
            column: Some(20),
            function: Some("test_fn".to_string()),
        };
        assert_eq!(loc.column, Some(20));
        assert_eq!(loc.function, Some("test_fn".to_string()));
    }

    #[test]
    fn test_dwarf_lookup_extensive_coverage() {
        // Compile a more complex test binary with actual code
        let temp_dir = TempDir::new().expect("test");
        let src_file = temp_dir.path().join("complex.rs");
        let bin_file = temp_dir.path().join("complex_bin");

        // Write a program with multiple functions
        fs::write(
            &src_file,
            r#"
fn helper(x: i32) -> i32 {
    x + 1
}

fn main() {
    let a = helper(5);
    let b = helper(10);
    println!("Result: {} {}", a, b);
}
"#,
        )
        .expect("test");

        Command::new("rustc")
            .arg(&src_file)
            .arg("-o")
            .arg(&bin_file)
            .arg("-g")
            .status()
            .expect("test");

        let ctx = DwarfContext::load(&bin_file).expect("test");

        // Try many different addresses to maximize coverage of lookup logic
        for addr in 0x1000..0x1100 {
            let _ = ctx.lookup(addr);
        }

        // Also test edge cases (avoid u64::MAX to prevent addr2line overflow)
        let _ = ctx.lookup(0x7FFF_FFFF_FFFF_FFF0);
        let _ = ctx.lookup(0x7FFF_FFFF_FFFF_FFFF);
        let _ = ctx.lookup(0);
        let _ = ctx.lookup(1);
        let _ = ctx.lookup(2);
        let _ = ctx.lookup(4);
        let _ = ctx.lookup(8);
        let _ = ctx.lookup(16);
    }

    #[test]
    fn test_source_location_all_variants() {
        // Test all combinations of Option fields
        let loc1 = SourceLocation {
            file: "a.rs".to_string(),
            line: 1,
            column: Some(1),
            function: Some("f".to_string()),
        };
        let loc2 =
            SourceLocation { file: "b.rs".to_string(), line: 2, column: Some(2), function: None };
        let loc3 = SourceLocation {
            file: "c.rs".to_string(),
            line: 3,
            column: None,
            function: Some("g".to_string()),
        };
        let loc4 =
            SourceLocation { file: "d.rs".to_string(), line: 4, column: None, function: None };

        assert_eq!(loc1.file, "a.rs");
        assert_eq!(loc2.file, "b.rs");
        assert_eq!(loc3.file, "c.rs");
        assert_eq!(loc4.file, "d.rs");

        // Test inequality
        assert_ne!(loc1, loc2);
        assert_ne!(loc2, loc3);
        assert_ne!(loc3, loc4);
    }
}