rustrip 0.1.1

Make stripped Rust binaries readable again: recover slice-string boundaries, demangle symbols, and reconstruct panic source locations from release/stripped ELF/PE binaries.
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
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
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
//! Detect `core::panic::Location` structures in read-only data and emit
//! source `file:line:col` annotations.
//!
//! When Rust compiles a `panic!`, `unwrap()`, `expect()`, or bounds-check
//! failure, the compiler embeds a `core::panic::Location` record adjacent
//! to the panic message / formatting machinery:
//!
//! ```text
//! struct Location {
//!     file_ptr: usize,   // pointer to "../src/foo.rs"
//!     file_len: usize,
//!     line: u32,
//!     col: u32,
//! }
//! ```
//!
//! Validation: file slice must be valid UTF-8, end in `.rs`, and line/col
//! must be plausible (line < `Limits::max_line`, col < `Limits::max_col`).
//! The combination is tight enough that false positives are rare even on
//! noisy binaries.

use crate::analyzers::{Analyzer, Annotation, AnnotationKind, Limits};
use crate::binary::Binary;

/// Analyzer that recovers `core::panic::Location` structures from read-only data.
#[derive(Debug, Clone)]
pub struct PanicsAnalyzer {
    pub limits: Limits,
}

impl Default for PanicsAnalyzer {
    fn default() -> Self {
        Self::new()
    }
}

impl PanicsAnalyzer {
    /// Create with default limits.
    #[must_use]
    pub fn new() -> Self {
        Self {
            limits: Limits::default(),
        }
    }

    /// Create with custom limits.
    #[must_use]
    pub const fn with_limits(limits: Limits) -> Self {
        Self { limits }
    }
}

impl Analyzer for PanicsAnalyzer {
    fn name(&self) -> &'static str {
        "panics"
    }

    fn analyze(&self, bin: &Binary) -> Vec<Annotation> {
        let mut out = Vec::new();
        if bin.is_64 {
            // 64-bit layout: ptr, ptr, u32, u32 -> 24 bytes total.
            scan_locations::<8>(bin, &self.limits, &mut out);
        } else {
            // 32-bit layout: ptr32, ptr32, u32, u32 -> 16 bytes total.
            scan_locations::<4>(bin, &self.limits, &mut out);
        }
        out
    }
}

// We pass `&mut Vec<Annotation>` instead of `&mut [Annotation]` because
// we call `out.push(...)` inside the loop, which requires the owned
// vector. clippy flags this as `ptr_arg` / `needless_pass_by_ref_mut`;
// allow with rationale.
//
// `file_len as usize` truncations in the inner loop are tolerated
// because `file_len` is bounded to `1..=4096` after the validation
// step above — fitting comfortably in `usize` on any supported target.
#[allow(
    clippy::ptr_arg,
    clippy::needless_pass_by_ref_mut,
    clippy::cast_possible_truncation
)]
fn scan_locations<const WS: usize>(bin: &Binary, limits: &Limits, out: &mut Vec<Annotation>) {
    let entry_size = WS * 2 + 4 + 4;
    for sec in bin.sections() {
        if !is_panic_container(&sec.name, sec.size) {
            continue;
        }
        let data = sec.data.as_slice();
        if data.len() < entry_size {
            continue;
        }
        let mut off = 0usize;
        while off + entry_size <= data.len() {
            // Wraparound-protected vaddr arithmetic: a malicious binary
            // could give a section vaddr + offset that overflows u64. Stop
            // the walk in that case rather than wrap around and silently
            // walk past the original intent.
            let Some(lookup) = sec.vaddr.checked_add(off as u64) else {
                break;
            };
            let Some(file_ptr) = bin.read_ptr(lookup) else {
                off += WS;
                continue;
            };
            let Some(file_len_vaddr) = lookup.checked_add(WS as u64) else {
                off += WS;
                continue;
            };
            let Some(file_len) = bin.read_ptr(file_len_vaddr) else {
                off += WS;
                continue;
            };
            if !(1..=4096).contains(&file_len) {
                off += WS;
                continue;
            }
            if !bin.vaddr_in_string_section(file_ptr, file_len) {
                off += WS;
                continue;
            }
            let Some(line_off) = lookup.checked_add((WS as u64) * 2) else {
                off += WS;
                continue;
            };
            let Some(col_off) = line_off.checked_add(4) else {
                off += WS;
                continue;
            };
            let Some(line) = bin.read_u32(line_off) else {
                off += WS;
                continue;
            };
            let Some(col) = bin.read_u32(col_off) else {
                off += WS;
                continue;
            };
            if line == 0 || line > limits.max_line || col == 0 || col > limits.max_col {
                off += WS;
                continue;
            }
            // file_len is bounded to `1..=4096` above — fits in usize
            // on all supported targets. The fn-level `#[allow]`
            // suppresses the warning here too.
            let Some(bytes) = bin.read_at_vaddr(file_ptr, file_len as usize) else {
                off += WS;
                continue;
            };
            let Ok(file) = std::str::from_utf8(bytes) else {
                off += WS;
                continue;
            };
            // Rust source files are always lowercase-`.rs`. We deliberately
            // do NOT case-fold to "case-insensitive" because Rust toolchain
            // output never produces `Foo.RS`-style names.
            #[allow(clippy::case_sensitive_file_extension_comparisons)]
            if !file.ends_with(".rs") {
                off += WS;
                continue;
            }
            out.push(Annotation {
                vaddr: lookup,
                kind: AnnotationKind::PanicSite,
                label: format!("{file}:{line}:{col}"),
                comment: None,
            });
            // Skip past the entire record (fixed-size header + variable
            // filename slice). The next record starts at
            //   `off + entry_size + file_len`
            // and may sit at any byte boundary; the WS=8 walker catches
            // it on the next iteration. `file_len` is bounded to
            // `1..=4096` above so the `as usize` cannot truncate on a
            // supported target.
            off += entry_size + file_len as usize;
        }
    }
}

fn is_panic_container(name: &str, size: u64) -> bool {
    // Locations live in any read-only data section. We rely on the section
    // index already being a "string section" — a panic Site struct points
    // into one of those sections, so we only need to scan the same set.
    // For efficiency, we additionally require non-trivial size.
    if size < 16 {
        return false;
    }
    matches!(name, ".rodata" | ".rdata")
        || name.starts_with(".rodata.")
        || name.starts_with(".rdata.")
        || name.starts_with(".data.rel.ro.")
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::binary::{Binary, BinaryFormat as Bf, Section};

    #[test]
    fn constants_distinct() {
        let lim = Limits::default();
        assert!(lim.max_line >= 100_000);
        assert!(lim.max_col >= 100);
    }

    #[test]
    fn analyzer_name_returns_panics() {
        assert_eq!(PanicsAnalyzer::new().name(), "panics");
    }

    #[test]
    fn is_panic_container_rejects_size_below_16() {
        assert!(!is_panic_container(".rodata", 15));
    }

    #[test]
    fn is_panic_container_accepts_size_exactly_16() {
        assert!(is_panic_container(".rodata", 16));
    }

    #[test]
    fn is_panic_container_rejects_non_rodata() {
        assert!(!is_panic_container(".text", 256));
        assert!(!is_panic_container(".data", 256));
        assert!(!is_panic_container(".bss", 256));
    }

    #[test]
    fn is_panic_container_accepts_all_rodata_variants() {
        assert!(is_panic_container(".rodata", 64));
        assert!(is_panic_container(".rdata", 64));
        assert!(is_panic_container(".rodata.cst4", 64));
        assert!(is_panic_container(".rdata.0", 64));
        assert!(is_panic_container(".data.rel.ro.local", 64));
    }

    fn loc64(file_ptr: u64, file_len: u64, line: u32, col: u32) -> Vec<u8> {
        let mut b = Vec::with_capacity(24);
        b.extend_from_slice(&file_ptr.to_le_bytes());
        b.extend_from_slice(&file_len.to_le_bytes());
        b.extend_from_slice(&line.to_le_bytes());
        b.extend_from_slice(&col.to_le_bytes());
        b
    }

    fn panic_bin(rodata_data: Vec<u8>, string_vaddr: u64, string_data: Vec<u8>) -> Binary {
        Binary::from_test_parts(
            Bf::Elf,
            true,
            true,
            vec![
                Section {
                    name: ".rodata".into(),
                    vaddr: 0x1000,
                    size: rodata_data.len() as u64,
                    data: rodata_data,
                },
                Section {
                    name: ".rodata.str".into(),
                    vaddr: string_vaddr,
                    size: string_data.len() as u64,
                    data: string_data,
                },
            ],
            vec![],
            vec![0, 1],
        )
    }

    #[test]
    fn scan_finds_single_panic_at_exact_entry_size() {
        let sv = 0x4000u64;
        let f = b"src/foo.rs";
        let rd = loc64(sv, f.len() as u64, 42, 9);
        assert_eq!(rd.len(), 24);
        let bin = panic_bin(rd, sv, f.to_vec());
        let anns = PanicsAnalyzer::new().analyze(&bin);
        assert_eq!(anns.len(), 1);
        assert_eq!(anns[0].label, "src/foo.rs:42:9");
    }

    #[test]
    fn scan_finds_two_panics() {
        let sv = 0x4000u64;
        let f1 = b"src/foo.rs";
        let f2 = b"src/bar.rs";
        let mut rd = loc64(sv, f1.len() as u64, 42, 9);
        rd.resize(34, 0);
        rd.extend_from_slice(&loc64(sv + f1.len() as u64, f2.len() as u64, 7, 1));
        let mut sd = Vec::new();
        sd.extend_from_slice(f1);
        sd.extend_from_slice(f2);
        let bin = panic_bin(rd, sv, sd);
        let anns = PanicsAnalyzer::new().analyze(&bin);
        assert_eq!(anns.len(), 2);
    }

    #[test]
    fn scan_skips_invalid_file_ptr_finds_valid() {
        let sv = 0x4000u64;
        let f = b"src/bar.rs";
        let mut rd = loc64(0xDEAD_BEEF, 10, 42, 9);
        rd.extend_from_slice(&loc64(sv, f.len() as u64, 7, 1));
        let bin = panic_bin(rd, sv, f.to_vec());
        let anns = PanicsAnalyzer::new().analyze(&bin);
        assert!(anns.iter().any(|a| a.label == "src/bar.rs:7:1"));
    }

    #[test]
    fn scan_skips_non_rs_filename_finds_valid() {
        let sv = 0x4000u64;
        let bad = b"src/foo.txt";
        let good = b"src/bar.rs";
        let mut rd = loc64(sv, bad.len() as u64, 42, 9);
        rd.extend_from_slice(&loc64(sv + bad.len() as u64, good.len() as u64, 7, 1));
        let mut sd = Vec::new();
        sd.extend_from_slice(bad);
        sd.extend_from_slice(good);
        let bin = panic_bin(rd, sv, sd);
        let anns = PanicsAnalyzer::new().analyze(&bin);
        assert!(anns.iter().any(|a| a.label == "src/bar.rs:7:1"));
    }

    #[test]
    fn scan_skips_invalid_utf8_finds_valid() {
        let sv = 0x4000u64;
        let bad: Vec<u8> = vec![0xFF, 0xFE, b'.', b'r', b's'];
        let good = b"src/bar.rs";
        let mut rd = loc64(sv, bad.len() as u64, 42, 9);
        rd.extend_from_slice(&loc64(sv + bad.len() as u64, good.len() as u64, 7, 1));
        let mut sd = Vec::new();
        sd.extend_from_slice(&bad);
        sd.extend_from_slice(good);
        let bin = panic_bin(rd, sv, sd);
        let anns = PanicsAnalyzer::new().analyze(&bin);
        assert!(anns.iter().any(|a| a.label == "src/bar.rs:7:1"));
    }

    #[test]
    fn scan_rejects_line_zero() {
        let sv = 0x4000u64;
        let f = b"src/foo.rs";
        let rd = loc64(sv, f.len() as u64, 0, 9);
        let bin = panic_bin(rd, sv, f.to_vec());
        assert!(PanicsAnalyzer::new().analyze(&bin).is_empty());
    }

    #[test]
    fn scan_rejects_col_zero() {
        let sv = 0x4000u64;
        let f = b"src/foo.rs";
        let rd = loc64(sv, f.len() as u64, 42, 0);
        let bin = panic_bin(rd, sv, f.to_vec());
        assert!(PanicsAnalyzer::new().analyze(&bin).is_empty());
    }

    #[test]
    fn scan_accepts_line_at_max_boundary() {
        let lim = Limits {
            max_line: 100,
            max_col: 10_000,
            ..Limits::default()
        };
        let sv = 0x4000u64;
        let f = b"src/foo.rs";
        let rd = loc64(sv, f.len() as u64, 100, 9);
        let bin = panic_bin(rd, sv, f.to_vec());
        assert_eq!(PanicsAnalyzer::with_limits(lim).analyze(&bin).len(), 1);
    }

    #[test]
    fn scan_rejects_line_above_max() {
        let lim = Limits {
            max_line: 100,
            max_col: 10_000,
            ..Limits::default()
        };
        let sv = 0x4000u64;
        let f = b"src/foo.rs";
        let rd = loc64(sv, f.len() as u64, 101, 9);
        let bin = panic_bin(rd, sv, f.to_vec());
        assert!(PanicsAnalyzer::with_limits(lim).analyze(&bin).is_empty());
    }

    #[test]
    fn scan_accepts_col_at_max_boundary() {
        let lim = Limits {
            max_line: 1_000_000,
            max_col: 100,
            ..Limits::default()
        };
        let sv = 0x4000u64;
        let f = b"src/foo.rs";
        let rd = loc64(sv, f.len() as u64, 42, 100);
        let bin = panic_bin(rd, sv, f.to_vec());
        assert_eq!(PanicsAnalyzer::with_limits(lim).analyze(&bin).len(), 1);
    }

    #[test]
    fn scan_rejects_col_above_max() {
        let lim = Limits {
            max_line: 1_000_000,
            max_col: 100,
            ..Limits::default()
        };
        let sv = 0x4000u64;
        let f = b"src/foo.rs";
        let rd = loc64(sv, f.len() as u64, 42, 101);
        let bin = panic_bin(rd, sv, f.to_vec());
        assert!(PanicsAnalyzer::with_limits(lim).analyze(&bin).is_empty());
    }

    #[test]
    fn scan_rejects_file_len_zero() {
        let sv = 0x4000u64;
        let rd = loc64(sv, 0, 42, 9);
        let bin = panic_bin(rd, sv, b"src/foo.rs".to_vec());
        assert!(PanicsAnalyzer::new().analyze(&bin).is_empty());
    }

    #[test]
    fn scan_rejects_file_len_above_4096() {
        let sv = 0x4000u64;
        let rd = loc64(sv, 4097, 42, 9);
        let bin = panic_bin(rd, sv, b"src/foo.rs".to_vec());
        assert!(PanicsAnalyzer::new().analyze(&bin).is_empty());
    }

    #[test]
    fn scan_rejects_section_smaller_than_entry_size() {
        let sv = 0x4000u64;
        let rd = vec![0u8; 23];
        let bin = panic_bin(rd, sv, vec![]);
        assert!(PanicsAnalyzer::new().analyze(&bin).is_empty());
    }

    #[test]
    fn scan_accepts_section_exactly_entry_size() {
        let sv = 0x4000u64;
        let f = b"src/foo.rs";
        let rd = loc64(sv, f.len() as u64, 42, 9);
        assert_eq!(rd.len(), 24);
        let bin = panic_bin(rd, sv, f.to_vec());
        assert_eq!(PanicsAnalyzer::new().analyze(&bin).len(), 1);
    }

    // ── Kill entry_size mutations (line 82: + → -) ───────────────────
    // entry_size = WS*2 + 4 + 4 = 24 for WS=8.
    // If + → -: entry_size = WS*2 - 4 - 4 = 8. The while condition
    // becomes off+8 <= data.len(), allowing more iterations but reading
    // only 8 bytes as a "record" → garbage → no valid annotations.
    // A test with exactly 24 bytes (one valid record) and 32 bytes
    // (one valid + 8 padding) kills this: with correct entry_size=24,
    // the 32-byte section allows off=0 only (0+24<=32, 8+24=32<=32).
    // Wait, 8+24=32<=32 → yes, off=8 also passes. But at off=8 the
    // 24 bytes starting at offset 8 are padding+garbage → no valid
    // annotation. With mutated entry_size=8, off=0,8,16,24 all pass,
    // and the scan reads 8-byte "records" → garbage → 0 annotations.
    // Both produce 0 extra annotations → NOT killed.
    //
    // Better approach: two valid records at offsets 0 and 24. With
    // correct entry_size=24, the while condition allows off=0 (0+24<=48)
    // and off=24 (24+24<=48). Both records found → 2 annotations.
    // With mutated entry_size=8, off=0,8,16,24,32,40 all pass. At
    // off=0, the scan reads 8 bytes as file_ptr (valid), then tries
    // to read file_len at off+8=8 (succeeds), etc. The 8-byte "record"
    // is actually the first 8 bytes of the 24-byte record → file_ptr
    // is valid but file_len/line/col are from wrong offsets → likely
    // rejected → 0 annotations → test expects 2 → FAIL → CAUGHT.

    #[test]
    fn scan_finds_two_records_at_exact_entry_size_boundaries() {
        let sv = 0x4000u64;
        let f1 = b"src/a.rs"; // 8 bytes
        let f2 = b"src/b.rs"; // 8 bytes
                              // Record 1 at offset 0: after finding it, scan jumps to
                              //   off += entry_size + file_len = 24 + 8 = 32
                              // Record 2 must be at offset 32 to be found.
        let mut rd = loc64(sv, f1.len() as u64, 1, 1); // 24 bytes
        rd.resize(32, 0); // pad 8 bytes
        rd.extend_from_slice(&loc64(sv + f1.len() as u64, f2.len() as u64, 2, 2));
        // Total: 32 + 24 = 56 bytes
        let mut sd = Vec::new();
        sd.extend_from_slice(f1);
        sd.extend_from_slice(f2);
        let bin = panic_bin(rd, sv, sd);
        let anns = PanicsAnalyzer::new().analyze(&bin);
        assert_eq!(
            anns.len(),
            2,
            "two records must both be found; got {anns:?}"
        );
    }

    // ── Kill off += WS mutations via manual Binary construction ──────
    // These lines are "structurally unreachable" with from_test_parts
    // because the while condition guarantees all reads succeed. We
    // bypass this by manually constructing a Binary where sec_by_vaddr
    // is carefully crafted to make specific reads fail.

    fn manual_bin(
        rodata_vaddr: u64,
        rodata_size: u64,
        rodata_data: Vec<u8>,
        sec_by_vaddr: Vec<(u64, u64, usize)>,
        string_sections: Vec<usize>,
        string_data: Vec<u8>,
    ) -> Binary {
        let sv = 0x4000u64;
        Binary {
            path: None,
            format: Bf::Elf,
            arch: "x86_64".into(),
            is_64: true,
            little_endian: true,
            bytes: vec![],
            sections: vec![
                Section {
                    name: ".rodata".into(),
                    vaddr: rodata_vaddr,
                    size: rodata_size,
                    data: rodata_data,
                },
                Section {
                    name: ".rodata.str".into(),
                    vaddr: sv,
                    size: string_data.len() as u64,
                    data: string_data,
                },
            ],
            symbols: vec![],
            sec_by_vaddr,
            string_sections,
        }
    }

    // Line 101: read_ptr(lookup) returns None → off += WS
    // rodata NOT in sec_by_vaddr → read_ptr always fails
    #[test]
    fn scan_read_ptr_fails_does_not_panic() {
        let bin = manual_bin(
            0x1000,
            48,
            vec![0; 48],
            vec![(0x4000, 0x400B, 1)], // only string section
            vec![1],
            b"src/foo.rs".to_vec(),
        );
        assert!(PanicsAnalyzer::new().analyze(&bin).is_empty());
    }

    // Line 105: checked_add(WS) for file_len_vaddr overflows
    #[test]
    fn scan_checked_add_file_len_vaddr_overflows_does_not_panic() {
        let v = u64::MAX - 7;
        let bin = manual_bin(
            v,
            24,
            vec![0xFF; 24],
            vec![(v, u64::MAX, 0), (0x4000, 0x400B, 1)],
            vec![1],
            b"src/foo.rs".to_vec(),
        );
        assert!(PanicsAnalyzer::new().analyze(&bin).is_empty());
    }

    // Line 109: read_ptr(file_len_vaddr) returns None
    // sec_by_vaddr maps only 8 bytes of rodata → read_ptr at off+8 fails
    #[test]
    fn scan_read_ptr_file_len_fails_does_not_panic() {
        let bin = manual_bin(
            0x1000,
            48,
            vec![0; 48],
            vec![(0x1000, 0x1008, 0), (0x4000, 0x400B, 1)], // only 8 bytes
            vec![1],
            b"src/foo.rs".to_vec(),
        );
        assert!(PanicsAnalyzer::new().analyze(&bin).is_empty());
    }

    // Line 121: checked_add(WS*2) for line_off overflows
    #[test]
    fn scan_checked_add_line_off_overflows_does_not_panic() {
        // Need read_ptr(lookup) and read_ptr(lookup+8) to succeed,
        // but checked_add(16) to overflow.
        // v + 8 must not overflow, v + 16 must overflow.
        // v = u64::MAX - 15: v+8 = u64::MAX - 7 (ok), v+16 = u64::MAX + 1 (overflow!)
        let v = u64::MAX - 15;
        let mut data = vec![0u8; 24];
        // Make read_ptr(v) succeed: file_ptr = 0x4000 (string section)
        data[0..8].copy_from_slice(&0x4000u64.to_le_bytes());
        // Make read_ptr(v+8) succeed: file_len = 10
        data[8..16].copy_from_slice(&10u64.to_le_bytes());
        let bin = manual_bin(
            v,
            24,
            data,
            vec![(v, u64::MAX, 0), (0x4000, 0x400B, 1)],
            vec![1],
            b"src/foo.rs".to_vec(),
        );
        assert!(PanicsAnalyzer::new().analyze(&bin).is_empty());
    }

    // Line 125: checked_add(4) for col_off overflows
    #[test]
    fn scan_checked_add_col_off_overflows_does_not_panic() {
        // Need line_off to succeed but line_off+4 to overflow.
        // line_off = lookup + 16. Need lookup+16 ok, lookup+20 overflow.
        // v + 20 > u64::MAX → v > u64::MAX - 20.
        // v + 16 <= u64::MAX → v <= u64::MAX - 16.
        // So u64::MAX - 20 < v <= u64::MAX - 16. Pick v = u64::MAX - 19.
        // line_off = v + 16 = u64::MAX - 3. col_off = u64::MAX - 3 + 4 = u64::MAX + 1 → overflow!
        let v = u64::MAX - 19;
        let mut data = vec![0u8; 24];
        data[0..8].copy_from_slice(&0x4000u64.to_le_bytes()); // file_ptr
        data[8..16].copy_from_slice(&10u64.to_le_bytes()); // file_len
        data[16..20].copy_from_slice(&1u32.to_le_bytes()); // line (at line_off)
        let bin = manual_bin(
            v,
            24,
            data,
            vec![(v, u64::MAX, 0), (0x4000, 0x400B, 1)],
            vec![1],
            b"src/foo.rs".to_vec(),
        );
        assert!(PanicsAnalyzer::new().analyze(&bin).is_empty());
    }

    // Line 129: read_u32(line_off) returns None
    // sec_by_vaddr maps only 16 bytes → read_u32 at off+16 fails
    #[test]
    fn scan_read_u32_line_fails_does_not_panic() {
        let mut data = vec![0u8; 24];
        data[0..8].copy_from_slice(&0x4000u64.to_le_bytes()); // file_ptr
        data[8..16].copy_from_slice(&10u64.to_le_bytes()); // file_len
        let bin = manual_bin(
            0x1000,
            24,
            data,
            vec![(0x1000, 0x1010, 0), (0x4000, 0x400B, 1)], // only 16 bytes
            vec![1],
            b"src/foo.rs".to_vec(),
        );
        assert!(PanicsAnalyzer::new().analyze(&bin).is_empty());
    }

    // Line 133: read_u32(col_off) returns None
    // sec_by_vaddr maps only 20 bytes → read_u32 at off+20 fails
    #[test]
    fn scan_read_u32_col_fails_does_not_panic() {
        let mut data = vec![0u8; 24];
        data[0..8].copy_from_slice(&0x4000u64.to_le_bytes()); // file_ptr
        data[8..16].copy_from_slice(&10u64.to_le_bytes()); // file_len
        data[16..20].copy_from_slice(&1u32.to_le_bytes()); // line
        let bin = manual_bin(
            0x1000,
            24,
            data,
            vec![(0x1000, 0x1014, 0), (0x4000, 0x400B, 1)], // only 20 bytes
            vec![1],
            b"src/foo.rs".to_vec(),
        );
        assert!(PanicsAnalyzer::new().analyze(&bin).is_empty());
    }

    // Line 144: read_at_vaddr(file_ptr, file_len) returns None
    // file_ptr points to string section but file_ptr+file_len extends beyond
    #[test]
    fn scan_read_at_vaddr_extends_beyond_does_not_panic() {
        let sv = 0x4000u64;
        // String section has 5 bytes, but file_len claims 10
        let rd = loc64(sv, 10, 42, 9); // file_len = 10
        let bin = panic_bin(rd, sv, b"abcde".to_vec()); // only 5 bytes
        assert!(PanicsAnalyzer::new().analyze(&bin).is_empty());
    }

    // Line 144: read_at_vaddr fails, then valid record found
    #[test]
    fn scan_read_at_vaddr_fails_then_finds_valid() {
        let sv = 0x4000u64;
        // Record 1 at off=0: file_ptr=sv, file_len=100 (extends beyond 5-byte string section)
        // Record 2 at off=24: file_ptr=sv, file_len=10 (fits in 11-byte string section)
        let mut rd = loc64(sv, 100, 42, 9); // invalid: file_len=100 > string_data.len()=11
        rd.extend_from_slice(&loc64(sv, 10, 7, 1)); // valid
        let sd = b"src/bar.rs\0".to_vec(); // 11 bytes
        let bin = panic_bin(rd, sv, sd);
        let anns = PanicsAnalyzer::new().analyze(&bin);
        assert!(
            anns.iter().any(|a| a.label == "src/bar.rs:7:1"),
            "valid record after read_at_vaddr failure must be found"
        );
    }
}