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        import pdb; pdb.set_trace()
218        self.assertEqual(1, len(modules))
219        # LLDB makes up it own UUID as well when there is no build ID so we
220        # will check that this matches.
221        self.verify_module(modules[0], so_path, "48EB9FD7")
222
223
224    def test_facebook_hash_match(self):
225        """
226            Breakpad creates minidump files using CvRecord in each module whose
227            signature is set to PDB70 where the UUID is a hash generated by
228            breakpad of the .text section and Facebook modified this hash to
229            avoid collisions. This is only done when the executable has no ELF
230            build ID.
231
232            This test verifies that if we have a minidump with a 16 byte UUID,
233            that we are able to associate a symbol file with no ELF build ID
234            and match it up by hashing the .text section like Facebook does.
235        """
236        so_path = self.getBuildArtifact("libbreakpad.so")
237        self.yaml2obj("libbreakpad.yaml", so_path)
238        cmd = 'settings set target.exec-search-paths "%s"' % (os.path.dirname(so_path))
239        self.dbg.HandleCommand(cmd)
240        modules = self.get_minidump_modules("linux-arm-facebook-uuid-match.yaml")
241        self.assertEqual(1, len(modules))
242        # LLDB makes up it own UUID as well when there is no build ID so we
243        # will check that this matches.
244        self.verify_module(modules[0], so_path, "D9C480E8")
245
246
247    def test_relative_module_name(self):
248        old_cwd = os.getcwd()
249        self.addTearDownHook(lambda: os.chdir(old_cwd))
250        os.chdir(self.getBuildDir())
251        name = "file-with-a-name-unlikely-to-exist-in-the-current-directory.so"
252        open(name, "a").close()
253        modules = self.get_minidump_modules(
254                self.getSourcePath("relative_module_name.yaml"))
255        self.assertEqual(1, len(modules))
256        self.verify_module(modules[0], name, None)
257
258    def test_add_module_build_id_16(self):
259        """
260            Test that adding module with 16 byte UUID returns the existing
261            module or fails.
262        """
263        modules = self.get_minidump_modules("linux-arm-uuids-elf-build-id-16.yaml")
264        self.assertEqual(2, len(modules))
265
266        # Add the existing modules.
267        self.assertEqual(modules[0], self.target.AddModule(
268            "/some/local/a", "", "01020304-0506-0708-090A-0B0C0D0E0F10"))
269        self.assertEqual(modules[1], self.target.AddModule(
270            "/some/local/b", "", "0A141E28-323C-4650-5A64-6E78828C96A0"))
271
272        # Adding modules with non-existing UUID should fail.
273        self.assertFalse(
274            self.target.AddModule(
275                "a", "", "12345678-1234-1234-1234-123456789ABC").IsValid())
276        self.assertFalse(
277            self.target.AddModule(
278                "a", "", "01020304-0506-0708-090A-0B0C0D0E0F10-12345678").IsValid())
279
280    def test_add_module_build_id_20(self):
281        """
282            Test that adding module with 20 byte UUID returns the existing
283            module or fails.
284        """
285        modules = self.get_minidump_modules("linux-arm-uuids-elf-build-id-20.yaml")
286
287        # Add the existing modules.
288        self.assertEqual(modules[0], self.target.AddModule(
289            "/some/local/a", "", "01020304-0506-0708-090A-0B0C0D0E0F10-11121314"))
290        self.assertEqual(modules[1], self.target.AddModule(
291            "/some/local/b", "", "0A141E28-323C-4650-5A64-6E78828C96A0-AAB4BEC8"))
292
293        # Adding modules with non-existing UUID should fail.
294        self.assertFalse(
295            self.target.AddModule(
296                "a", "", "01020304-0506-0708-090A-0B0C0D0E0F10").IsValid())
297        self.assertFalse(
298            self.target.AddModule(
299                "a", "", "01020304-0506-0708-090A-0B0C0D0E0F10-12345678").IsValid())
300
301    def test_add_module_build_id_4(self):
302        """
303            Test that adding module with 4 byte UUID returns the existing
304            module or fails.
305        """
306        modules = self.get_minidump_modules("linux-arm-uuids-elf-build-id-4.yaml")
307
308        # Add the existing modules.
309        self.assertEqual(modules[0], self.target.AddModule(
310            "/some/local/a.so", "", "01020304"))
311        self.assertEqual(modules[1], self.target.AddModule(
312            "/some/local/b.so", "", "0A141E28"))
313
314        # Adding modules with non-existing UUID should fail.
315        self.assertFalse(
316            self.target.AddModule(
317                "a", "", "01020304-0506-0708-090A-0B0C0D0E0F10").IsValid())
318        self.assertFalse(self.target.AddModule("a", "", "01020305").IsValid())
319
320    def test_remove_placeholder_add_real_module(self):
321        """
322            Test that removing a placeholder module and adding back the real
323            module succeeds.
324        """
325        so_path = self.getBuildArtifact("libuuidmatch.so")
326        self.yaml2obj("libuuidmatch.yaml", so_path)
327        modules = self.get_minidump_modules("linux-arm-uuids-match.yaml")
328
329        uuid = "7295E17C-6668-9E05-CBB5-DEE5003865D5-5267C116";
330        self.assertEqual(1, len(modules))
331        self.verify_module(modules[0], "/target/path/libuuidmatch.so",uuid)
332
333        self.target.RemoveModule(modules[0])
334        new_module = self.target.AddModule(so_path, "", uuid)
335
336        self.verify_module(new_module, so_path, uuid)
337        self.assertEqual(new_module, self.target.modules[0])
338        self.assertEqual(1, len(self.target.modules))
339