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
15@skipIfReproducer # Modules are not orphaned and it finds the module with the same UUID from test_partial_uuid_match.
16class MiniDumpUUIDTestCase(TestBase):
17
18    mydir = TestBase.compute_mydir(__file__)
19
20    NO_DEBUG_INFO_TESTCASE = True
21
22    def verify_module(self, module, verify_path, verify_uuid):
23        # Compare the filename and the directory separately. We are avoiding
24        # SBFileSpec.fullpath because it causes a slash/backslash confusion
25        # on Windows.
26        self.assertEqual(
27            os.path.basename(verify_path), module.GetFileSpec().basename)
28        self.assertEqual(
29            os.path.dirname(verify_path), module.GetFileSpec().dirname or "")
30        self.assertEqual(verify_uuid, module.GetUUIDString())
31
32    def get_minidump_modules(self, yaml_file):
33        minidump_path = self.getBuildArtifact(os.path.basename(yaml_file) + ".dmp")
34        self.yaml2obj(yaml_file, minidump_path)
35        self.target = self.dbg.CreateTarget(None)
36        self.process = self.target.LoadCore(minidump_path)
37        return self.target.modules
38
39    def test_zero_uuid_modules(self):
40        """
41            Test multiple modules having a MINIDUMP_MODULE.CvRecord that is valid,
42            but contains a PDB70 value whose age is zero and whose UUID values are
43            all zero. Prior to a fix all such modules would be duplicated to the
44            first one since the UUIDs claimed to be valid and all zeroes. Now we
45            ensure that the UUID is not valid for each module and that we have
46            each of the modules in the target after loading the core
47        """
48        modules = self.get_minidump_modules("linux-arm-zero-uuids.yaml")
49        self.assertEqual(2, len(modules))
50        self.verify_module(modules[0], "/file/does/not/exist/a", None)
51        self.verify_module(modules[1], "/file/does/not/exist/b", None)
52
53    def test_uuid_modules_no_age(self):
54        """
55            Test multiple modules having a MINIDUMP_MODULE.CvRecord that is valid,
56            and contains a PDB70 value whose age is zero and whose UUID values are
57            valid. Ensure we decode the UUID and don't include the age field in the UUID.
58        """
59        modules = self.get_minidump_modules("linux-arm-uuids-no-age.yaml")
60        modules = self.target.modules
61        self.assertEqual(2, len(modules))
62        self.verify_module(modules[0], "/tmp/a", "01020304-0506-0708-090A-0B0C0D0E0F10")
63        self.verify_module(modules[1], "/tmp/b", "0A141E28-323C-4650-5A64-6E78828C96A0")
64
65    def test_uuid_modules_no_age_apple(self):
66        """
67            Test multiple modules having a MINIDUMP_MODULE.CvRecord that is valid,
68            and contains a PDB70 value whose age is zero and whose UUID values are
69            valid. Ensure we decode the UUID and don't include the age field in the UUID.
70            Also ensure that the first uint32_t is byte swapped, along with the next
71            two uint16_t values. Breakpad incorrectly byte swaps these values when it
72            saves Darwin minidump files.
73        """
74        modules = self.get_minidump_modules("macos-arm-uuids-no-age.yaml")
75        modules = self.target.modules
76        self.assertEqual(2, len(modules))
77        self.verify_module(modules[0], "/tmp/a", "04030201-0605-0807-090A-0B0C0D0E0F10")
78        self.verify_module(modules[1], "/tmp/b", "281E140A-3C32-5046-5A64-6E78828C96A0")
79
80    def test_uuid_modules_with_age(self):
81        """
82            Test multiple modules having a MINIDUMP_MODULE.CvRecord that is valid,
83            and contains a PDB70 value whose age is valid and whose UUID values are
84            valid. Ensure we decode the UUID and include the age field in the UUID.
85        """
86        modules = self.get_minidump_modules("linux-arm-uuids-with-age.yaml")
87        self.assertEqual(2, len(modules))
88        self.verify_module(modules[0], "/tmp/a", "01020304-0506-0708-090A-0B0C0D0E0F10-10101010")
89        self.verify_module(modules[1], "/tmp/b", "0A141E28-323C-4650-5A64-6E78828C96A0-20202020")
90
91    def test_uuid_modules_elf_build_id_16(self):
92        """
93            Test multiple modules having a MINIDUMP_MODULE.CvRecord that is valid,
94            and contains a ELF build ID whose value is valid and is 16 bytes long.
95        """
96        modules = self.get_minidump_modules("linux-arm-uuids-elf-build-id-16.yaml")
97        self.assertEqual(2, len(modules))
98        self.verify_module(modules[0], "/tmp/a", "01020304-0506-0708-090A-0B0C0D0E0F10")
99        self.verify_module(modules[1], "/tmp/b", "0A141E28-323C-4650-5A64-6E78828C96A0")
100
101    def test_uuid_modules_elf_build_id_20(self):
102        """
103            Test multiple modules having a MINIDUMP_MODULE.CvRecord that is valid,
104            and contains a ELF build ID whose value is valid and is 20 bytes long.
105        """
106        modules = self.get_minidump_modules("linux-arm-uuids-elf-build-id-20.yaml")
107        self.assertEqual(2, len(modules))
108        self.verify_module(modules[0], "/tmp/a", "01020304-0506-0708-090A-0B0C0D0E0F10-11121314")
109        self.verify_module(modules[1], "/tmp/b", "0A141E28-323C-4650-5A64-6E78828C96A0-AAB4BEC8")
110
111    def test_uuid_modules_elf_build_id_zero(self):
112        """
113            Test multiple modules having a MINIDUMP_MODULE.CvRecord that is valid,
114            and contains a ELF build ID whose value is all zero.
115        """
116        modules = self.get_minidump_modules("linux-arm-uuids-elf-build-id-zero.yaml")
117        self.assertEqual(2, len(modules))
118        self.verify_module(modules[0], "/not/exist/a", None)
119        self.verify_module(modules[1], "/not/exist/b", None)
120
121    def test_uuid_modules_elf_build_id_same(self):
122        """
123            Test multiple modules having a MINIDUMP_MODULE.CvRecord that is
124            valid, and contains a ELF build ID whose value is the same. There
125            is an assert in the PlaceholderObjectFile that was firing when we
126            encountered this which was crashing the process that was checking
127            if PlaceholderObjectFile.m_base was the same as the address this
128            fake module was being loaded at. We need to ensure we don't crash
129            in such cases and that we add both modules even though they have
130            the same UUID.
131        """
132        modules = self.get_minidump_modules("linux-arm-same-uuids.yaml")
133        self.assertEqual(2, len(modules))
134        self.verify_module(modules[0], "/file/does/not/exist/a",
135                           '11223344-1122-3344-1122-334411223344-11223344')
136        self.verify_module(modules[1], "/file/does/not/exist/b",
137                           '11223344-1122-3344-1122-334411223344-11223344')
138
139    def test_partial_uuid_match(self):
140        """
141            Breakpad has been known to create minidump files using CvRecord in each
142            module whose signature is set to PDB70 where the UUID only contains the
143            first 16 bytes of a 20 byte ELF build ID. Code was added to
144            ProcessMinidump.cpp to deal with this and allows partial UUID matching.
145
146            This test verifies that if we have a minidump with a 16 byte UUID, that
147            we are able to associate a symbol file with a 20 byte UUID only if the
148            first 16 bytes match. In this case we will see the path from the file
149            we found in the test directory and the 20 byte UUID from the actual
150            file, not the 16 byte shortened UUID from the minidump.
151        """
152        so_path = self.getBuildArtifact("libuuidmatch.so")
153        self.yaml2obj("libuuidmatch.yaml", so_path)
154        cmd = 'settings set target.exec-search-paths "%s"' % (os.path.dirname(so_path))
155        self.dbg.HandleCommand(cmd)
156        modules = self.get_minidump_modules("linux-arm-partial-uuids-match.yaml")
157        self.assertEqual(1, len(modules))
158        self.verify_module(modules[0], so_path,
159                           "7295E17C-6668-9E05-CBB5-DEE5003865D5-5267C116")
160
161    def test_partial_uuid_mismatch(self):
162        """
163            Breakpad has been known to create minidump files using CvRecord in each
164            module whose signature is set to PDB70 where the UUID only contains the
165            first 16 bytes of a 20 byte ELF build ID. Code was added to
166            ProcessMinidump.cpp to deal with this and allows partial UUID matching.
167
168            This test verifies that if we have a minidump with a 16 byte UUID, that
169            we are not able to associate a symbol file with a 20 byte UUID only if
170            any of the first 16 bytes do not match. In this case we will see the UUID
171            from the minidump file and the path from the minidump file.
172        """
173        so_path = self.getBuildArtifact("libuuidmismatch.so")
174        self.yaml2obj("libuuidmismatch.yaml", so_path)
175        cmd = 'settings set target.exec-search-paths "%s"' % (os.path.dirname(so_path))
176        self.dbg.HandleCommand(cmd)
177        modules = self.get_minidump_modules("linux-arm-partial-uuids-mismatch.yaml")
178        self.assertEqual(1, len(modules))
179        self.verify_module(modules[0],
180                           "/invalid/path/on/current/system/libuuidmismatch.so",
181                           "7295E17C-6668-9E05-CBB5-DEE5003865D5")
182
183    def test_breakpad_hash_match(self):
184        """
185            Breakpad creates minidump files using CvRecord in each module whose
186            signature is set to PDB70 where the UUID is a hash generated by
187            breakpad of the .text section. This is only done when the
188            executable has no ELF build ID.
189
190            This test verifies that if we have a minidump with a 16 byte UUID,
191            that we are able to associate a symbol file with no ELF build ID
192            and match it up by hashing the .text section.
193        """
194        so_path = self.getBuildArtifact("libbreakpad.so")
195        self.yaml2obj("libbreakpad.yaml", so_path)
196        cmd = 'settings set target.exec-search-paths "%s"' % (os.path.dirname(so_path))
197        self.dbg.HandleCommand(cmd)
198        modules = self.get_minidump_modules("linux-arm-breakpad-uuid-match.yaml")
199        self.assertEqual(1, len(modules))
200        # LLDB makes up it own UUID as well when there is no build ID so we
201        # will check that this matches.
202        self.verify_module(modules[0], so_path, "D9C480E8")
203
204    def test_breakpad_overflow_hash_match(self):
205        """
206            This is a similar to test_breakpad_hash_match, but it verifies that
207            if the .text section does not end on a 16 byte boundary, then it
208            will overflow into the next section's data by up to 15 bytes. This
209            verifies that we are able to match what breakpad does as it will do
210            this.
211        """
212        so_path = self.getBuildArtifact("libbreakpad.so")
213        self.yaml2obj("libbreakpad-overflow.yaml", so_path)
214        cmd = 'settings set target.exec-search-paths "%s"' % (os.path.dirname(so_path))
215        self.dbg.HandleCommand(cmd)
216        modules = self.get_minidump_modules("linux-arm-breakpad-uuid-match.yaml")
217        self.assertEqual(1, len(modules))
218        # LLDB makes up it own UUID as well when there is no build ID so we
219        # will check that this matches.
220        self.verify_module(modules[0], so_path, "48EB9FD7")
221
222
223    def test_facebook_hash_match(self):
224        """
225            Breakpad creates minidump files using CvRecord in each module whose
226            signature is set to PDB70 where the UUID is a hash generated by
227            breakpad of the .text section and Facebook modified this hash to
228            avoid collisions. This is only done when the executable has no ELF
229            build ID.
230
231            This test verifies that if we have a minidump with a 16 byte UUID,
232            that we are able to associate a symbol file with no ELF build ID
233            and match it up by hashing the .text section like Facebook does.
234        """
235        so_path = self.getBuildArtifact("libbreakpad.so")
236        self.yaml2obj("libbreakpad.yaml", so_path)
237        cmd = 'settings set target.exec-search-paths "%s"' % (os.path.dirname(so_path))
238        self.dbg.HandleCommand(cmd)
239        modules = self.get_minidump_modules("linux-arm-facebook-uuid-match.yaml")
240        self.assertEqual(1, len(modules))
241        # LLDB makes up it own UUID as well when there is no build ID so we
242        # will check that this matches.
243        self.verify_module(modules[0], so_path, "D9C480E8")
244
245
246    def test_relative_module_name(self):
247        old_cwd = os.getcwd()
248        self.addTearDownHook(lambda: os.chdir(old_cwd))
249        os.chdir(self.getBuildDir())
250        name = "file-with-a-name-unlikely-to-exist-in-the-current-directory.so"
251        open(name, "a").close()
252        modules = self.get_minidump_modules(
253                self.getSourcePath("relative_module_name.yaml"))
254        self.assertEqual(1, len(modules))
255        self.verify_module(modules[0], name, None)
256
257    def test_add_module_build_id_16(self):
258        """
259            Test that adding module with 16 byte UUID returns the existing
260            module or fails.
261        """
262        modules = self.get_minidump_modules("linux-arm-uuids-elf-build-id-16.yaml")
263        self.assertEqual(2, len(modules))
264
265        # Add the existing modules.
266        self.assertEqual(modules[0], self.target.AddModule(
267            "/some/local/a", "", "01020304-0506-0708-090A-0B0C0D0E0F10"))
268        self.assertEqual(modules[1], self.target.AddModule(
269            "/some/local/b", "", "0A141E28-323C-4650-5A64-6E78828C96A0"))
270
271        # Adding modules with non-existing UUID should fail.
272        self.assertFalse(
273            self.target.AddModule(
274                "a", "", "12345678-1234-1234-1234-123456789ABC").IsValid())
275        self.assertFalse(
276            self.target.AddModule(
277                "a", "", "01020304-0506-0708-090A-0B0C0D0E0F10-12345678").IsValid())
278
279    def test_add_module_build_id_20(self):
280        """
281            Test that adding module with 20 byte UUID returns the existing
282            module or fails.
283        """
284        modules = self.get_minidump_modules("linux-arm-uuids-elf-build-id-20.yaml")
285
286        # Add the existing modules.
287        self.assertEqual(modules[0], self.target.AddModule(
288            "/some/local/a", "", "01020304-0506-0708-090A-0B0C0D0E0F10-11121314"))
289        self.assertEqual(modules[1], self.target.AddModule(
290            "/some/local/b", "", "0A141E28-323C-4650-5A64-6E78828C96A0-AAB4BEC8"))
291
292        # Adding modules with non-existing UUID should fail.
293        self.assertFalse(
294            self.target.AddModule(
295                "a", "", "01020304-0506-0708-090A-0B0C0D0E0F10").IsValid())
296        self.assertFalse(
297            self.target.AddModule(
298                "a", "", "01020304-0506-0708-090A-0B0C0D0E0F10-12345678").IsValid())
299
300    def test_add_module_build_id_4(self):
301        """
302            Test that adding module with 4 byte UUID returns the existing
303            module or fails.
304        """
305        modules = self.get_minidump_modules("linux-arm-uuids-elf-build-id-4.yaml")
306
307        # Add the existing modules.
308        self.assertEqual(modules[0], self.target.AddModule(
309            "/some/local/a.so", "", "01020304"))
310        self.assertEqual(modules[1], self.target.AddModule(
311            "/some/local/b.so", "", "0A141E28"))
312
313        # Adding modules with non-existing UUID should fail.
314        self.assertFalse(
315            self.target.AddModule(
316                "a", "", "01020304-0506-0708-090A-0B0C0D0E0F10").IsValid())
317        self.assertFalse(self.target.AddModule("a", "", "01020305").IsValid())
318
319    def test_remove_placeholder_add_real_module(self):
320        """
321            Test that removing a placeholder module and adding back the real
322            module succeeds.
323        """
324        so_path = self.getBuildArtifact("libuuidmatch.so")
325        self.yaml2obj("libuuidmatch.yaml", so_path)
326        modules = self.get_minidump_modules("linux-arm-uuids-match.yaml")
327
328        uuid = "7295E17C-6668-9E05-CBB5-DEE5003865D5-5267C116";
329        self.assertEqual(1, len(modules))
330        self.verify_module(modules[0], "/target/path/libuuidmatch.so",uuid)
331
332        self.target.RemoveModule(modules[0])
333        new_module = self.target.AddModule(so_path, "", uuid)
334
335        self.verify_module(new_module, so_path, uuid)
336        self.assertEqual(new_module, self.target.modules[0])
337        self.assertEqual(1, len(self.target.modules))
338