TestMiniDumpUUID.py 19.7 KB
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
"""
Test basics of Minidump debugging.
"""

from six import iteritems


import lldb
import os
from lldbsuite.test.decorators import *
from lldbsuite.test.lldbtest import *
from lldbsuite.test import lldbutil


class MiniDumpUUIDTestCase(TestBase):

    mydir = TestBase.compute_mydir(__file__)

    NO_DEBUG_INFO_TESTCASE = True

    def verify_module(self, module, verify_path, verify_uuid):
        # Compare the filename and the directory separately. We are avoiding
        # SBFileSpec.fullpath because it causes a slash/backslash confusion
        # on Windows.  Similarly, we compare the directories using normcase
        # because they may contain a Linux-style relative path from the
        # minidump appended to a Windows-style root path from the host.
        self.assertEqual(
            os.path.basename(verify_path), module.GetFileSpec().basename)
        self.assertEqual(
            os.path.normcase(os.path.dirname(verify_path)),
            os.path.normcase(module.GetFileSpec().dirname or ""))
        self.assertEqual(verify_uuid, module.GetUUIDString())

    def get_minidump_modules(self, yaml_file, exe = None):
        minidump_path = self.getBuildArtifact(os.path.basename(yaml_file) + ".dmp")
        self.yaml2obj(yaml_file, minidump_path)
        self.target = self.dbg.CreateTarget(exe)
        self.process = self.target.LoadCore(minidump_path)
        return self.target.modules

    def test_zero_uuid_modules(self):
        """
            Test multiple modules having a MINIDUMP_MODULE.CvRecord that is valid,
            but contains a PDB70 value whose age is zero and whose UUID values are
            all zero. Prior to a fix all such modules would be duplicated to the
            first one since the UUIDs claimed to be valid and all zeroes. Now we
            ensure that the UUID is not valid for each module and that we have
            each of the modules in the target after loading the core
        """
        modules = self.get_minidump_modules("linux-arm-zero-uuids.yaml")
        self.assertEqual(2, len(modules))
        self.verify_module(modules[0], "/file/does/not/exist/a", None)
        self.verify_module(modules[1], "/file/does/not/exist/b", None)

    def test_uuid_modules_no_age(self):
        """
            Test multiple modules having a MINIDUMP_MODULE.CvRecord that is valid,
            and contains a PDB70 value whose age is zero and whose UUID values are
            valid. Ensure we decode the UUID and don't include the age field in the UUID.
        """
        modules = self.get_minidump_modules("linux-arm-uuids-no-age.yaml")
        modules = self.target.modules
        self.assertEqual(2, len(modules))
        self.verify_module(modules[0], "/tmp/a", "01020304-0506-0708-090A-0B0C0D0E0F10")
        self.verify_module(modules[1], "/tmp/b", "0A141E28-323C-4650-5A64-6E78828C96A0")

    def test_uuid_modules_no_age_apple(self):
        """
            Test multiple modules having a MINIDUMP_MODULE.CvRecord that is valid,
            and contains a PDB70 value whose age is zero and whose UUID values are
            valid. Ensure we decode the UUID and don't include the age field in the UUID.
            Also ensure that the first uint32_t is byte swapped, along with the next
            two uint16_t values. Breakpad incorrectly byte swaps these values when it
            saves Darwin minidump files.
        """
        modules = self.get_minidump_modules("macos-arm-uuids-no-age.yaml")
        modules = self.target.modules
        self.assertEqual(2, len(modules))
        self.verify_module(modules[0], "/tmp/a", "04030201-0605-0807-090A-0B0C0D0E0F10")
        self.verify_module(modules[1], "/tmp/b", "281E140A-3C32-5046-5A64-6E78828C96A0")

    def test_uuid_modules_with_age(self):
        """
            Test multiple modules having a MINIDUMP_MODULE.CvRecord that is valid,
            and contains a PDB70 value whose age is valid and whose UUID values are
            valid. Ensure we decode the UUID and include the age field in the UUID.
        """
        modules = self.get_minidump_modules("linux-arm-uuids-with-age.yaml")
        self.assertEqual(2, len(modules))
        self.verify_module(modules[0], "/tmp/a", "01020304-0506-0708-090A-0B0C0D0E0F10-10101010")
        self.verify_module(modules[1], "/tmp/b", "0A141E28-323C-4650-5A64-6E78828C96A0-20202020")

    def test_uuid_modules_elf_build_id_16(self):
        """
            Test multiple modules having a MINIDUMP_MODULE.CvRecord that is valid,
            and contains a ELF build ID whose value is valid and is 16 bytes long.
        """
        modules = self.get_minidump_modules("linux-arm-uuids-elf-build-id-16.yaml")
        self.assertEqual(2, len(modules))
        self.verify_module(modules[0], "/tmp/a", "01020304-0506-0708-090A-0B0C0D0E0F10")
        self.verify_module(modules[1], "/tmp/b", "0A141E28-323C-4650-5A64-6E78828C96A0")

    def test_uuid_modules_elf_build_id_20(self):
        """
            Test multiple modules having a MINIDUMP_MODULE.CvRecord that is valid,
            and contains a ELF build ID whose value is valid and is 20 bytes long.
        """
        modules = self.get_minidump_modules("linux-arm-uuids-elf-build-id-20.yaml")
        self.assertEqual(2, len(modules))
        self.verify_module(modules[0], "/tmp/a", "01020304-0506-0708-090A-0B0C0D0E0F10-11121314")
        self.verify_module(modules[1], "/tmp/b", "0A141E28-323C-4650-5A64-6E78828C96A0-AAB4BEC8")

    def test_uuid_modules_elf_build_id_zero(self):
        """
            Test multiple modules having a MINIDUMP_MODULE.CvRecord that is valid,
            and contains a ELF build ID whose value is all zero.
        """
        modules = self.get_minidump_modules("linux-arm-uuids-elf-build-id-zero.yaml")
        self.assertEqual(2, len(modules))
        self.verify_module(modules[0], "/not/exist/a", None)
        self.verify_module(modules[1], "/not/exist/b", None)

    def test_uuid_modules_elf_build_id_same(self):
        """
            Test multiple modules having a MINIDUMP_MODULE.CvRecord that is
            valid, and contains a ELF build ID whose value is the same. There
            is an assert in the PlaceholderObjectFile that was firing when we
            encountered this which was crashing the process that was checking
            if PlaceholderObjectFile.m_base was the same as the address this
            fake module was being loaded at. We need to ensure we don't crash
            in such cases and that we add both modules even though they have
            the same UUID.
        """
        modules = self.get_minidump_modules("linux-arm-same-uuids.yaml")
        self.assertEqual(2, len(modules))
        self.verify_module(modules[0], "/file/does/not/exist/a",
                           '11223344-1122-3344-1122-334411223344-11223344')
        self.verify_module(modules[1], "/file/does/not/exist/b",
                           '11223344-1122-3344-1122-334411223344-11223344')

    def test_partial_uuid_match(self):
        """
            Breakpad has been known to create minidump files using CvRecord in each
            module whose signature is set to PDB70 where the UUID only contains the
            first 16 bytes of a 20 byte ELF build ID. Code was added to
            ProcessMinidump.cpp to deal with this and allows partial UUID matching.

            This test verifies that if we have a minidump with a 16 byte UUID, that
            we are able to associate a symbol file with a 20 byte UUID only if the
            first 16 bytes match. In this case we will see the path from the file
            we found in the test directory and the 20 byte UUID from the actual
            file, not the 16 byte shortened UUID from the minidump.
        """
        so_path = self.getBuildArtifact("libuuidmatch.so")
        self.yaml2obj("libuuidmatch.yaml", so_path)
        cmd = 'settings set target.exec-search-paths "%s"' % (os.path.dirname(so_path))
        self.dbg.HandleCommand(cmd)
        modules = self.get_minidump_modules("linux-arm-partial-uuids-match.yaml")
        self.assertEqual(1, len(modules))
        self.verify_module(modules[0], so_path,
                           "7295E17C-6668-9E05-CBB5-DEE5003865D5-5267C116")

    def test_partial_uuid_mismatch(self):
        """
            Breakpad has been known to create minidump files using CvRecord in each
            module whose signature is set to PDB70 where the UUID only contains the
            first 16 bytes of a 20 byte ELF build ID. Code was added to
            ProcessMinidump.cpp to deal with this and allows partial UUID matching.

            This test verifies that if we have a minidump with a 16 byte UUID, that
            we are not able to associate a symbol file with a 20 byte UUID only if
            any of the first 16 bytes do not match. In this case we will see the UUID
            from the minidump file and the path from the minidump file.
        """
        so_path = self.getBuildArtifact("libuuidmismatch.so")
        self.yaml2obj("libuuidmismatch.yaml", so_path)
        cmd = 'settings set target.exec-search-paths "%s"' % (os.path.dirname(so_path))
        self.dbg.HandleCommand(cmd)
        modules = self.get_minidump_modules("linux-arm-partial-uuids-mismatch.yaml")
        self.assertEqual(1, len(modules))
        self.verify_module(modules[0],
                           "/invalid/path/on/current/system/libuuidmismatch.so",
                           "7295E17C-6668-9E05-CBB5-DEE5003865D5")

    def test_breakpad_hash_match(self):
        """
            Breakpad creates minidump files using CvRecord in each module whose
            signature is set to PDB70 where the UUID is a hash generated by
            breakpad of the .text section. This is only done when the
            executable has no ELF build ID.

            This test verifies that if we have a minidump with a 16 byte UUID,
            that we are able to associate a symbol file with no ELF build ID
            and match it up by hashing the .text section.
        """
        so_path = self.getBuildArtifact("libbreakpad.so")
        self.yaml2obj("libbreakpad.yaml", so_path)
        cmd = 'settings set target.exec-search-paths "%s"' % (os.path.dirname(so_path))
        self.dbg.HandleCommand(cmd)
        modules = self.get_minidump_modules("linux-arm-breakpad-uuid-match.yaml")
        self.assertEqual(1, len(modules))
        # LLDB makes up it own UUID as well when there is no build ID so we
        # will check that this matches.
        self.verify_module(modules[0], so_path, "D9C480E8")

    def test_breakpad_hash_match_sysroot(self):
        """
            Check that we can match the breakpad .text section hash when the
            module is located under a user-provided sysroot.
        """
        sysroot_path = os.path.join(self.getBuildDir(), "mock_sysroot")
        # Create the directory under the sysroot where the minidump reports
        # the module.
        so_dir = os.path.join(sysroot_path, "invalid", "path", "on", "current", "system")
        so_path = os.path.join(so_dir, "libbreakpad.so")
        lldbutil.mkdir_p(so_dir)
        self.yaml2obj("libbreakpad.yaml", so_path)
        self.runCmd("platform select remote-linux --sysroot '%s'" % sysroot_path)
        modules = self.get_minidump_modules("linux-arm-breakpad-uuid-match.yaml")
        self.assertEqual(1, len(modules))
        # LLDB makes up its own UUID as well when there is no build ID so we
        # will check that this matches.
        self.verify_module(modules[0], so_path, "D9C480E8")

    def test_breakpad_hash_match_sysroot_decoy(self):
        """
            Check that we can match the breakpad .text section hash when there is
            a module with the right name but wrong contents under a user-provided
            sysroot, and the right module is at the given search path..
        """
        sysroot_path = os.path.join(self.getBuildDir(), "mock_sysroot")
        # Create the directory under the sysroot where the minidump reports
        # the module.
        decoy_dir = os.path.join(sysroot_path, "invalid", "path", "on", "current", "system")
        decoy_path = os.path.join(decoy_dir, "libbreakpad.so")
        lldbutil.mkdir_p(decoy_dir)
        self.yaml2obj("libbreakpad-decoy.yaml", decoy_path)
        self.runCmd("platform select remote-linux --sysroot '%s'" % sysroot_path)
        so_dir = os.path.join(self.getBuildDir(), "searchpath_dir")
        so_path = os.path.join(so_dir, "libbreakpad.so")
        lldbutil.mkdir_p(so_dir)
        self.yaml2obj("libbreakpad.yaml", so_path)
        self.runCmd('settings set target.exec-search-paths "%s"' % so_dir)
        modules = self.get_minidump_modules("linux-arm-breakpad-uuid-match.yaml")
        self.assertEqual(1, len(modules))
        # LLDB makes up its own UUID as well when there is no build ID so we
        # will check that this matches.
        self.verify_module(modules[0], so_path, "D9C480E8")

    def test_breakpad_overflow_hash_match(self):
        """
            This is a similar to test_breakpad_hash_match, but it verifies that
            if the .text section does not end on a 16 byte boundary, then it
            will overflow into the next section's data by up to 15 bytes. This
            verifies that we are able to match what breakpad does as it will do
            this.
        """
        so_path = self.getBuildArtifact("libbreakpad.so")
        self.yaml2obj("libbreakpad-overflow.yaml", so_path)
        cmd = 'settings set target.exec-search-paths "%s"' % (os.path.dirname(so_path))
        self.dbg.HandleCommand(cmd)
        modules = self.get_minidump_modules("linux-arm-breakpad-uuid-match.yaml")
        self.assertEqual(1, len(modules))
        # LLDB makes up it own UUID as well when there is no build ID so we
        # will check that this matches.
        self.verify_module(modules[0], so_path, "48EB9FD7")

    def test_breakpad_hash_match_exe_outside_sysroot(self):
        """
            Check that we can match the breakpad .text section hash when the
            module is specified as the exe during launch, and a syroot is
            provided, which does not contain the exe.
        """
        sysroot_path = os.path.join(self.getBuildDir(), "mock_sysroot")
        lldbutil.mkdir_p(sysroot_path)
        so_dir = os.path.join(self.getBuildDir(), "binary")
        so_path = os.path.join(so_dir, "libbreakpad.so")
        lldbutil.mkdir_p(so_dir)
        self.yaml2obj("libbreakpad.yaml", so_path)
        self.runCmd("platform select remote-linux --sysroot '%s'" % sysroot_path)
        modules = self.get_minidump_modules("linux-arm-breakpad-uuid-match.yaml", so_path)
        self.assertEqual(1, len(modules))
        # LLDB makes up its own UUID as well when there is no build ID so we
        # will check that this matches.
        self.verify_module(modules[0], so_path, "D9C480E8")

    def test_facebook_hash_match(self):
        """
            Breakpad creates minidump files using CvRecord in each module whose
            signature is set to PDB70 where the UUID is a hash generated by
            breakpad of the .text section and Facebook modified this hash to
            avoid collisions. This is only done when the executable has no ELF
            build ID.

            This test verifies that if we have a minidump with a 16 byte UUID,
            that we are able to associate a symbol file with no ELF build ID
            and match it up by hashing the .text section like Facebook does.
        """
        so_path = self.getBuildArtifact("libbreakpad.so")
        self.yaml2obj("libbreakpad.yaml", so_path)
        cmd = 'settings set target.exec-search-paths "%s"' % (os.path.dirname(so_path))
        self.dbg.HandleCommand(cmd)
        modules = self.get_minidump_modules("linux-arm-facebook-uuid-match.yaml")
        self.assertEqual(1, len(modules))
        # LLDB makes up it own UUID as well when there is no build ID so we
        # will check that this matches.
        self.verify_module(modules[0], so_path, "D9C480E8")


    def test_relative_module_name(self):
        old_cwd = os.getcwd()
        self.addTearDownHook(lambda: os.chdir(old_cwd))
        os.chdir(self.getBuildDir())
        name = "file-with-a-name-unlikely-to-exist-in-the-current-directory.so"
        open(name, "a").close()
        modules = self.get_minidump_modules(
                self.getSourcePath("relative_module_name.yaml"))
        self.assertEqual(1, len(modules))
        self.verify_module(modules[0], name, None)

    def test_add_module_build_id_16(self):
        """
            Test that adding module with 16 byte UUID returns the existing
            module or fails.
        """
        modules = self.get_minidump_modules("linux-arm-uuids-elf-build-id-16.yaml")
        self.assertEqual(2, len(modules))

        # Add the existing modules.
        self.assertEqual(modules[0], self.target.AddModule(
            "/some/local/a", "", "01020304-0506-0708-090A-0B0C0D0E0F10"))
        self.assertEqual(modules[1], self.target.AddModule(
            "/some/local/b", "", "0A141E28-323C-4650-5A64-6E78828C96A0"))

        # Adding modules with non-existing UUID should fail.
        self.assertFalse(
            self.target.AddModule(
                "a", "", "12345678-1234-1234-1234-123456789ABC").IsValid())
        self.assertFalse(
            self.target.AddModule(
                "a", "", "01020304-0506-0708-090A-0B0C0D0E0F10-12345678").IsValid())

    def test_add_module_build_id_20(self):
        """
            Test that adding module with 20 byte UUID returns the existing
            module or fails.
        """
        modules = self.get_minidump_modules("linux-arm-uuids-elf-build-id-20.yaml")

        # Add the existing modules.
        self.assertEqual(modules[0], self.target.AddModule(
            "/some/local/a", "", "01020304-0506-0708-090A-0B0C0D0E0F10-11121314"))
        self.assertEqual(modules[1], self.target.AddModule(
            "/some/local/b", "", "0A141E28-323C-4650-5A64-6E78828C96A0-AAB4BEC8"))

        # Adding modules with non-existing UUID should fail.
        self.assertFalse(
            self.target.AddModule(
                "a", "", "01020304-0506-0708-090A-0B0C0D0E0F10").IsValid())
        self.assertFalse(
            self.target.AddModule(
                "a", "", "01020304-0506-0708-090A-0B0C0D0E0F10-12345678").IsValid())

    def test_add_module_build_id_4(self):
        """
            Test that adding module with 4 byte UUID returns the existing
            module or fails.
        """
        modules = self.get_minidump_modules("linux-arm-uuids-elf-build-id-4.yaml")

        # Add the existing modules.
        self.assertEqual(modules[0], self.target.AddModule(
            "/some/local/a.so", "", "01020304"))
        self.assertEqual(modules[1], self.target.AddModule(
            "/some/local/b.so", "", "0A141E28"))

        # Adding modules with non-existing UUID should fail.
        self.assertFalse(
            self.target.AddModule(
                "a", "", "01020304-0506-0708-090A-0B0C0D0E0F10").IsValid())
        self.assertFalse(self.target.AddModule("a", "", "01020305").IsValid())

    @skipIfReproducer # Modules are not orphaned and it finds the module with the same UUID from test_partial_uuid_match.
    def test_remove_placeholder_add_real_module(self):
        """
            Test that removing a placeholder module and adding back the real
            module succeeds.
        """
        so_path = self.getBuildArtifact("libuuidmatch.so")
        self.yaml2obj("libuuidmatch.yaml", so_path)
        modules = self.get_minidump_modules("linux-arm-uuids-match.yaml")

        uuid = "7295E17C-6668-9E05-CBB5-DEE5003865D5-5267C116";
        self.assertEqual(1, len(modules))
        self.verify_module(modules[0], "/target/path/libuuidmatch.so",uuid)

        self.target.RemoveModule(modules[0])
        new_module = self.target.AddModule(so_path, "", uuid)

        self.verify_module(new_module, so_path, uuid)
        self.assertEqual(new_module, self.target.modules[0])
        self.assertEqual(1, len(self.target.modules))