1"""
2Test basics of Minidump debugging.
3"""
4
5from six import iteritems
6
7
8import lldb
9import os
10from lldbsuite.test.decorators import *
11from lldbsuite.test.lldbtest import *
12from lldbsuite.test import lldbutil
13
14
15class MiniDumpUUIDTestCase(TestBase):
16
17    mydir = TestBase.compute_mydir(__file__)
18
19    NO_DEBUG_INFO_TESTCASE = True
20
21    def verify_module(self, module, verify_path, verify_uuid):
22        uuid = module.GetUUIDString()
23        self.assertEqual(verify_path, module.GetFileSpec().fullpath)
24        self.assertEqual(verify_uuid, uuid)
25
26    def get_minidump_modules(self, yaml_file):
27        minidump_path = self.getBuildArtifact(os.path.basename(yaml_file) + ".dmp")
28        self.yaml2obj(yaml_file, minidump_path)
29        self.target = self.dbg.CreateTarget(None)
30        self.process = self.target.LoadCore(minidump_path)
31        return self.target.modules
32
33    def test_zero_uuid_modules(self):
34        """
35            Test multiple modules having a MINIDUMP_MODULE.CvRecord that is valid,
36            but contains a PDB70 value whose age is zero and whose UUID values are
37            all zero. Prior to a fix all such modules would be duplicated to the
38            first one since the UUIDs claimed to be valid and all zeroes. Now we
39            ensure that the UUID is not valid for each module and that we have
40            each of the modules in the target after loading the core
41        """
42        modules = self.get_minidump_modules("linux-arm-zero-uuids.yaml")
43        self.assertEqual(2, len(modules))
44        self.verify_module(modules[0], "/file/does/not/exist/a", None)
45        self.verify_module(modules[1], "/file/does/not/exist/b", None)
46
47    def test_uuid_modules_no_age(self):
48        """
49            Test multiple modules having a MINIDUMP_MODULE.CvRecord that is valid,
50            and contains a PDB70 value whose age is zero and whose UUID values are
51            valid. Ensure we decode the UUID and don't include the age field in the UUID.
52        """
53        modules = self.get_minidump_modules("linux-arm-uuids-no-age.yaml")
54        modules = self.target.modules
55        self.assertEqual(2, len(modules))
56        self.verify_module(modules[0], "/tmp/a", "01020304-0506-0708-090A-0B0C0D0E0F10")
57        self.verify_module(modules[1], "/tmp/b", "0A141E28-323C-4650-5A64-6E78828C96A0")
58
59    def test_uuid_modules_no_age_apple(self):
60        """
61            Test multiple modules having a MINIDUMP_MODULE.CvRecord that is valid,
62            and contains a PDB70 value whose age is zero and whose UUID values are
63            valid. Ensure we decode the UUID and don't include the age field in the UUID.
64            Also ensure that the first uint32_t is byte swapped, along with the next
65            two uint16_t values. Breakpad incorrectly byte swaps these values when it
66            saves Darwin minidump files.
67        """
68        modules = self.get_minidump_modules("macos-arm-uuids-no-age.yaml")
69        modules = self.target.modules
70        self.assertEqual(2, len(modules))
71        self.verify_module(modules[0], "/tmp/a", "04030201-0605-0807-090A-0B0C0D0E0F10")
72        self.verify_module(modules[1], "/tmp/b", "281E140A-3C32-5046-5A64-6E78828C96A0")
73
74    def test_uuid_modules_with_age(self):
75        """
76            Test multiple modules having a MINIDUMP_MODULE.CvRecord that is valid,
77            and contains a PDB70 value whose age is valid and whose UUID values are
78            valid. Ensure we decode the UUID and include the age field in the UUID.
79        """
80        modules = self.get_minidump_modules("linux-arm-uuids-with-age.yaml")
81        self.assertEqual(2, len(modules))
82        self.verify_module(modules[0], "/tmp/a", "01020304-0506-0708-090A-0B0C0D0E0F10-10101010")
83        self.verify_module(modules[1], "/tmp/b", "0A141E28-323C-4650-5A64-6E78828C96A0-20202020")
84
85    def test_uuid_modules_elf_build_id_16(self):
86        """
87            Test multiple modules having a MINIDUMP_MODULE.CvRecord that is valid,
88            and contains a ELF build ID whose value is valid and is 16 bytes long.
89        """
90        modules = self.get_minidump_modules("linux-arm-uuids-elf-build-id-16.yaml")
91        self.assertEqual(2, len(modules))
92        self.verify_module(modules[0], "/tmp/a", "01020304-0506-0708-090A-0B0C0D0E0F10")
93        self.verify_module(modules[1], "/tmp/b", "0A141E28-323C-4650-5A64-6E78828C96A0")
94
95    def test_uuid_modules_elf_build_id_20(self):
96        """
97            Test multiple modules having a MINIDUMP_MODULE.CvRecord that is valid,
98            and contains a ELF build ID whose value is valid and is 20 bytes long.
99        """
100        modules = self.get_minidump_modules("linux-arm-uuids-elf-build-id-20.yaml")
101        self.assertEqual(2, len(modules))
102        self.verify_module(modules[0], "/tmp/a", "01020304-0506-0708-090A-0B0C0D0E0F10-11121314")
103        self.verify_module(modules[1], "/tmp/b", "0A141E28-323C-4650-5A64-6E78828C96A0-AAB4BEC8")
104
105    def test_uuid_modules_elf_build_id_zero(self):
106        """
107            Test multiple modules having a MINIDUMP_MODULE.CvRecord that is valid,
108            and contains a ELF build ID whose value is all zero.
109        """
110        modules = self.get_minidump_modules("linux-arm-uuids-elf-build-id-zero.yaml")
111        self.assertEqual(2, len(modules))
112        self.verify_module(modules[0], "/not/exist/a", None)
113        self.verify_module(modules[1], "/not/exist/b", None)
114
115    def test_uuid_modules_elf_build_id_same(self):
116        """
117            Test multiple modules having a MINIDUMP_MODULE.CvRecord that is
118            valid, and contains a ELF build ID whose value is the same. There
119            is an assert in the PlaceholderObjectFile that was firing when we
120            encountered this which was crashing the process that was checking
121            if PlaceholderObjectFile.m_base was the same as the address this
122            fake module was being loaded at. We need to ensure we don't crash
123            in such cases and that we add both modules even though they have
124            the same UUID.
125        """
126        modules = self.get_minidump_modules("linux-arm-same-uuids.yaml")
127        self.assertEqual(2, len(modules))
128        self.verify_module(modules[0], "/file/does/not/exist/a",
129                           '11223344-1122-3344-1122-334411223344-11223344')
130        self.verify_module(modules[1], "/file/does/not/exist/b",
131                           '11223344-1122-3344-1122-334411223344-11223344')
132
133    @expectedFailureAll(oslist=["windows"])
134    def test_partial_uuid_match(self):
135        """
136            Breakpad has been known to create minidump files using CvRecord in each
137            module whose signature is set to PDB70 where the UUID only contains the
138            first 16 bytes of a 20 byte ELF build ID. Code was added to
139            ProcessMinidump.cpp to deal with this and allows partial UUID matching.
140
141            This test verifies that if we have a minidump with a 16 byte UUID, that
142            we are able to associate a symbol file with a 20 byte UUID only if the
143            first 16 bytes match. In this case we will see the path from the file
144            we found in the test directory and the 20 byte UUID from the actual
145            file, not the 16 byte shortened UUID from the minidump.
146        """
147        so_path = self.getBuildArtifact("libuuidmatch.so")
148        self.yaml2obj("libuuidmatch.yaml", so_path)
149        cmd = 'settings set target.exec-search-paths "%s"' % (os.path.dirname(so_path))
150        self.dbg.HandleCommand(cmd)
151        modules = self.get_minidump_modules("linux-arm-partial-uuids-match.yaml")
152        self.assertEqual(1, len(modules))
153        self.verify_module(modules[0], so_path,
154                           "7295E17C-6668-9E05-CBB5-DEE5003865D5-5267C116")
155
156    def test_partial_uuid_mismatch(self):
157        """
158            Breakpad has been known to create minidump files using CvRecord in each
159            module whose signature is set to PDB70 where the UUID only contains the
160            first 16 bytes of a 20 byte ELF build ID. Code was added to
161            ProcessMinidump.cpp to deal with this and allows partial UUID matching.
162
163            This test verifies that if we have a minidump with a 16 byte UUID, that
164            we are not able to associate a symbol file with a 20 byte UUID only if
165            any of the first 16 bytes do not match. In this case we will see the UUID
166            from the minidump file and the path from the minidump file.
167        """
168        so_path = self.getBuildArtifact("libuuidmismatch.so")
169        self.yaml2obj("libuuidmismatch.yaml", so_path)
170        cmd = 'settings set target.exec-search-paths "%s"' % (os.path.dirname(so_path))
171        self.dbg.HandleCommand(cmd)
172        modules = self.get_minidump_modules("linux-arm-partial-uuids-mismatch.yaml")
173        self.assertEqual(1, len(modules))
174        self.verify_module(modules[0],
175                           "/invalid/path/on/current/system/libuuidmismatch.so",
176                           "7295E17C-6668-9E05-CBB5-DEE5003865D5")
177
178    def test_relative_module_name(self):
179        old_cwd = os.getcwd()
180        self.addTearDownHook(lambda: os.chdir(old_cwd))
181        os.chdir(self.getBuildDir())
182        name = "file-with-a-name-unlikely-to-exist-in-the-current-directory.so"
183        open(name, "a").close()
184        modules = self.get_minidump_modules(
185                self.getSourcePath("relative_module_name.yaml"))
186        self.assertEqual(1, len(modules))
187        self.verify_module(modules[0], name, None)
188