aboutsummaryrefslogtreecommitdiff
path: root/vendor/addr2line/tests/correctness.rs
blob: 73ee462f8c18d3cdcb709b50bb9e9eee6e0a20de (plain) (blame)
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
use addr2line::Context;
use fallible_iterator::FallibleIterator;
use findshlibs::{IterationControl, SharedLibrary, TargetSharedLibrary};
use object::Object;
use std::borrow::Cow;
use std::fs::File;
use std::sync::Arc;

fn find_debuginfo() -> memmap2::Mmap {
    let path = std::env::current_exe().unwrap();
    let file = File::open(&path).unwrap();
    let map = unsafe { memmap2::Mmap::map(&file).unwrap() };
    let file = &object::File::parse(&*map).unwrap();
    if let Ok(uuid) = file.mach_uuid() {
        for candidate in path.parent().unwrap().read_dir().unwrap() {
            let path = candidate.unwrap().path();
            if !path.to_str().unwrap().ends_with(".dSYM") {
                continue;
            }
            for candidate in path.join("Contents/Resources/DWARF").read_dir().unwrap() {
                let path = candidate.unwrap().path();
                let file = File::open(&path).unwrap();
                let map = unsafe { memmap2::Mmap::map(&file).unwrap() };
                let file = &object::File::parse(&*map).unwrap();
                if file.mach_uuid().unwrap() == uuid {
                    return map;
                }
            }
        }
    }

    return map;
}

#[test]
fn correctness() {
    let map = find_debuginfo();
    let file = &object::File::parse(&*map).unwrap();
    let module_base = file.relative_address_base();

    let endian = if file.is_little_endian() {
        gimli::RunTimeEndian::Little
    } else {
        gimli::RunTimeEndian::Big
    };

    fn load_section<'data: 'file, 'file, O, Endian>(
        id: gimli::SectionId,
        file: &'file O,
        endian: Endian,
    ) -> Result<gimli::EndianArcSlice<Endian>, gimli::Error>
    where
        O: object::Object<'data, 'file>,
        Endian: gimli::Endianity,
    {
        use object::ObjectSection;

        let data = file
            .section_by_name(id.name())
            .and_then(|section| section.uncompressed_data().ok())
            .unwrap_or(Cow::Borrowed(&[]));
        Ok(gimli::EndianArcSlice::new(Arc::from(&*data), endian))
    }

    let dwarf = gimli::Dwarf::load(|id| load_section(id, file, endian)).unwrap();
    let ctx = Context::from_dwarf(dwarf).unwrap();
    let mut split_dwarf_loader = addr2line::builtin_split_dwarf_loader::SplitDwarfLoader::new(
        |data, endian| gimli::EndianArcSlice::new(Arc::from(&*data), endian),
        None,
    );

    let mut bias = None;
    TargetSharedLibrary::each(|lib| {
        bias = Some((lib.virtual_memory_bias().0 as u64).wrapping_sub(module_base));
        IterationControl::Break
    });

    #[allow(unused_mut)]
    let mut test = |sym: u64, expected_prefix: &str| {
        let ip = sym.wrapping_sub(bias.unwrap());

        let frames = ctx.find_frames(ip);
        let frames = split_dwarf_loader.run(frames).unwrap();
        let frame = frames.last().unwrap().unwrap();
        let name = frame.function.as_ref().unwrap().demangle().unwrap();
        // Old rust versions generate DWARF with wrong linkage name,
        // so only check the start.
        if !name.starts_with(expected_prefix) {
            panic!("incorrect name '{}', expected {:?}", name, expected_prefix);
        }
    };

    test(test_function as u64, "correctness::test_function");
    test(
        small::test_function as u64,
        "correctness::small::test_function",
    );
    test(auxiliary::foo as u64, "auxiliary::foo");
}

mod small {
    pub fn test_function() {
        println!("y");
    }
}

fn test_function() {
    println!("x");
}

#[test]
fn zero_function() {
    let map = find_debuginfo();
    let file = &object::File::parse(&*map).unwrap();
    let ctx = Context::new(file).unwrap();
    for probe in 0..10 {
        assert!(
            ctx.find_frames(probe)
                .skip_all_loads()
                .unwrap()
                .count()
                .unwrap()
                < 10
        );
    }
}