MinidumpParserTest.cpp 34.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 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847
//===-- MinidumpTypesTest.cpp ---------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "Plugins/Process/minidump/MinidumpParser.h"
#include "Plugins/Process/minidump/MinidumpTypes.h"
#include "Plugins/Process/minidump/RegisterContextMinidump_x86_32.h"
#include "Plugins/Process/minidump/RegisterContextMinidump_x86_64.h"
#include "TestingSupport/SubsystemRAII.h"
#include "TestingSupport/TestUtilities.h"
#include "lldb/Host/FileSystem.h"
#include "lldb/Target/MemoryRegionInfo.h"
#include "lldb/Utility/ArchSpec.h"
#include "lldb/Utility/DataBufferHeap.h"
#include "lldb/Utility/DataExtractor.h"
#include "lldb/Utility/FileSpec.h"
#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/Optional.h"
#include "llvm/ObjectYAML/yaml2obj.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/Path.h"
#include "llvm/Support/YAMLTraits.h"
#include "llvm/Testing/Support/Error.h"
#include "gtest/gtest.h"

// C includes

// C++ includes
#include <memory>

using namespace lldb_private;
using namespace minidump;

class MinidumpParserTest : public testing::Test {
public:
  SubsystemRAII<FileSystem> subsystems;

  void SetUpData(const char *minidump_filename) {
    std::string filename = GetInputFilePath(minidump_filename);
    auto BufferPtr = FileSystem::Instance().CreateDataBuffer(filename, -1, 0);
    ASSERT_NE(BufferPtr, nullptr);
    llvm::Expected<MinidumpParser> expected_parser =
        MinidumpParser::Create(BufferPtr);
    ASSERT_THAT_EXPECTED(expected_parser, llvm::Succeeded());
    parser = std::move(*expected_parser);
    ASSERT_GT(parser->GetData().size(), 0UL);
  }

  llvm::Error SetUpFromYaml(llvm::StringRef yaml) {
    std::string data;
    llvm::raw_string_ostream os(data);
    llvm::yaml::Input YIn(yaml);
    if (!llvm::yaml::convertYAML(YIn, os, [](const llvm::Twine &Msg) {}))
      return llvm::createStringError(llvm::inconvertibleErrorCode(),
                                     "convertYAML() failed");

    os.flush();
    auto data_buffer_sp =
        std::make_shared<DataBufferHeap>(data.data(), data.size());
    auto expected_parser = MinidumpParser::Create(std::move(data_buffer_sp));
    if (!expected_parser)
      return expected_parser.takeError();
    parser = std::move(*expected_parser);
    return llvm::Error::success();
  }

  llvm::Optional<MinidumpParser> parser;
};

TEST_F(MinidumpParserTest, InvalidMinidump) {
  std::string duplicate_streams;
  llvm::raw_string_ostream os(duplicate_streams);
  llvm::yaml::Input YIn(R"(
--- !minidump
Streams:
  - Type:            LinuxAuxv
    Content:         DEADBEEFBAADF00D
  - Type:            LinuxAuxv
    Content:         DEADBEEFBAADF00D
  )");

  ASSERT_TRUE(llvm::yaml::convertYAML(YIn, os, [](const llvm::Twine &Msg){}));
  os.flush();
  auto data_buffer_sp = std::make_shared<DataBufferHeap>(
      duplicate_streams.data(), duplicate_streams.size());
  ASSERT_THAT_EXPECTED(MinidumpParser::Create(data_buffer_sp), llvm::Failed());
}

TEST_F(MinidumpParserTest, GetThreadsAndGetThreadContext) {
  ASSERT_THAT_ERROR(SetUpFromYaml(R"(
--- !minidump
Streams:
  - Type:            ThreadList
    Threads:
      - Thread Id:       0x00003E81
        Stack:
          Start of Memory Range: 0x00007FFCEB34A000
          Content:         C84D04BCE97F00
        Context:         00000000000000
...
)"),
                    llvm::Succeeded());
  llvm::ArrayRef<minidump::Thread> thread_list;

  thread_list = parser->GetThreads();
  ASSERT_EQ(1UL, thread_list.size());

  const minidump::Thread &thread = thread_list[0];

  EXPECT_EQ(0x3e81u, thread.ThreadId);

  llvm::ArrayRef<uint8_t> context = parser->GetThreadContext(thread);
  EXPECT_EQ(7u, context.size());
}

TEST_F(MinidumpParserTest, GetArchitecture) {
  ASSERT_THAT_ERROR(SetUpFromYaml(R"(
--- !minidump
Streams:
  - Type:            SystemInfo
    Processor Arch:  AMD64
    Processor Level: 6
    Processor Revision: 16130
    Number of Processors: 1
    Platform ID:     Linux
    CPU:
      Vendor ID:       GenuineIntel
      Version Info:    0x00000000
      Feature Info:    0x00000000
...
)"),
                    llvm::Succeeded());
  ASSERT_EQ(llvm::Triple::ArchType::x86_64,
            parser->GetArchitecture().GetMachine());
  ASSERT_EQ(llvm::Triple::OSType::Linux,
            parser->GetArchitecture().GetTriple().getOS());
}

TEST_F(MinidumpParserTest, GetMiscInfo_no_stream) {
  // Test that GetMiscInfo returns nullptr when the minidump does not contain
  // this stream.
  ASSERT_THAT_ERROR(SetUpFromYaml(R"(
--- !minidump
Streams:
...
)"),
                    llvm::Succeeded());
  EXPECT_EQ(nullptr, parser->GetMiscInfo());
}

TEST_F(MinidumpParserTest, GetLinuxProcStatus) {
  ASSERT_THAT_ERROR(SetUpFromYaml(R"(
--- !minidump
Streams:
  - Type:            SystemInfo
    Processor Arch:  AMD64
    Processor Level: 6
    Processor Revision: 16130
    Number of Processors: 1
    Platform ID:     Linux
    CSD Version:     'Linux 3.13.0-91-generic'
    CPU:
      Vendor ID:       GenuineIntel
      Version Info:    0x00000000
      Feature Info:    0x00000000
  - Type:            LinuxProcStatus
    Text:             |
      Name:	a.out
      State:	t (tracing stop)
      Tgid:	16001
      Ngid:	0
      Pid:	16001
      PPid:	13243
      TracerPid:	16002
      Uid:	404696	404696	404696	404696
      Gid:	5762	5762	5762	5762
...
)"),
                    llvm::Succeeded());
  llvm::Optional<LinuxProcStatus> proc_status = parser->GetLinuxProcStatus();
  ASSERT_TRUE(proc_status.hasValue());
  lldb::pid_t pid = proc_status->GetPid();
  ASSERT_EQ(16001UL, pid);
}

TEST_F(MinidumpParserTest, GetPid) {
  ASSERT_THAT_ERROR(SetUpFromYaml(R"(
--- !minidump
Streams:
  - Type:            SystemInfo
    Processor Arch:  AMD64
    Processor Level: 6
    Processor Revision: 16130
    Number of Processors: 1
    Platform ID:     Linux
    CSD Version:     'Linux 3.13.0-91-generic'
    CPU:
      Vendor ID:       GenuineIntel
      Version Info:    0x00000000
      Feature Info:    0x00000000
  - Type:            LinuxProcStatus
    Text:             |
      Name:	a.out
      State:	t (tracing stop)
      Tgid:	16001
      Ngid:	0
      Pid:	16001
      PPid:	13243
      TracerPid:	16002
      Uid:	404696	404696	404696	404696
      Gid:	5762	5762	5762	5762
...
)"),
                    llvm::Succeeded());
  llvm::Optional<lldb::pid_t> pid = parser->GetPid();
  ASSERT_TRUE(pid.hasValue());
  ASSERT_EQ(16001UL, pid.getValue());
}

TEST_F(MinidumpParserTest, GetFilteredModuleList) {
  ASSERT_THAT_ERROR(SetUpFromYaml(R"(
--- !minidump
Streams:
  - Type:            ModuleList
    Modules:
      - Base of Image:   0x0000000000400000
        Size of Image:   0x00001000
        Module Name:     '/tmp/test/linux-x86_64_not_crashed'
        CodeView Record: 4C4570426CCF3F60FFA7CC4B86AE8FF44DB2576A68983611
      - Base of Image:   0x0000000000600000
        Size of Image:   0x00002000
        Module Name:     '/tmp/test/linux-x86_64_not_crashed'
        CodeView Record: 4C4570426CCF3F60FFA7CC4B86AE8FF44DB2576A68983611
...
)"),
                    llvm::Succeeded());
  llvm::ArrayRef<minidump::Module> modules = parser->GetModuleList();
  std::vector<const minidump::Module *> filtered_modules =
      parser->GetFilteredModuleList();
  EXPECT_EQ(2u, modules.size());
  ASSERT_EQ(1u, filtered_modules.size());
  const minidump::Module &M = *filtered_modules[0];
  EXPECT_THAT_EXPECTED(parser->GetMinidumpFile().getString(M.ModuleNameRVA),
                       llvm::HasValue("/tmp/test/linux-x86_64_not_crashed"));
}

TEST_F(MinidumpParserTest, GetExceptionStream) {
  SetUpData("linux-x86_64.dmp");
  const llvm::minidump::ExceptionStream *exception_stream =
      parser->GetExceptionStream();
  ASSERT_NE(nullptr, exception_stream);
  ASSERT_EQ(11UL, exception_stream->ExceptionRecord.ExceptionCode);
}

void check_mem_range_exists(MinidumpParser &parser, const uint64_t range_start,
                            const uint64_t range_size) {
  llvm::Optional<minidump::Range> range = parser.FindMemoryRange(range_start);
  ASSERT_TRUE(range.hasValue()) << "There is no range containing this address";
  EXPECT_EQ(range_start, range->start);
  EXPECT_EQ(range_start + range_size, range->start + range->range_ref.size());
}

TEST_F(MinidumpParserTest, FindMemoryRange) {
  ASSERT_THAT_ERROR(SetUpFromYaml(R"(
--- !minidump
Streams:
  - Type:            MemoryList
    Memory Ranges:
      - Start of Memory Range: 0x00007FFCEB34A000
        Content:         C84D04BCE9
      - Start of Memory Range: 0x0000000000401D46
        Content:         5421
...
)"),
                    llvm::Succeeded());
  EXPECT_EQ(llvm::None, parser->FindMemoryRange(0x00));
  EXPECT_EQ(llvm::None, parser->FindMemoryRange(0x2a));
  EXPECT_EQ((minidump::Range{0x401d46, llvm::ArrayRef<uint8_t>{0x54, 0x21}}),
            parser->FindMemoryRange(0x401d46));
  EXPECT_EQ(llvm::None, parser->FindMemoryRange(0x401d46 + 2));

  EXPECT_EQ(
      (minidump::Range{0x7ffceb34a000,
                       llvm::ArrayRef<uint8_t>{0xc8, 0x4d, 0x04, 0xbc, 0xe9}}),
      parser->FindMemoryRange(0x7ffceb34a000 + 2));
  EXPECT_EQ(llvm::None, parser->FindMemoryRange(0x7ffceb34a000 + 5));
}

TEST_F(MinidumpParserTest, GetMemory) {
  ASSERT_THAT_ERROR(SetUpFromYaml(R"(
--- !minidump
Streams:
  - Type:            MemoryList
    Memory Ranges:
      - Start of Memory Range: 0x00007FFCEB34A000
        Content:         C84D04BCE9
      - Start of Memory Range: 0x0000000000401D46
        Content:         5421
...
)"),
                    llvm::Succeeded());

  EXPECT_EQ((llvm::ArrayRef<uint8_t>{0x54}), parser->GetMemory(0x401d46, 1));
  EXPECT_EQ((llvm::ArrayRef<uint8_t>{0x54, 0x21}),
            parser->GetMemory(0x401d46, 4));

  EXPECT_EQ((llvm::ArrayRef<uint8_t>{0xc8, 0x4d, 0x04, 0xbc, 0xe9}),
            parser->GetMemory(0x7ffceb34a000, 5));
  EXPECT_EQ((llvm::ArrayRef<uint8_t>{0xc8, 0x4d, 0x04}),
            parser->GetMemory(0x7ffceb34a000, 3));

  EXPECT_EQ(llvm::ArrayRef<uint8_t>(), parser->GetMemory(0x500000, 512));
}

TEST_F(MinidumpParserTest, FindMemoryRangeWithFullMemoryMinidump) {
  SetUpData("fizzbuzz_wow64.dmp");

  // There are a lot of ranges in the file, just testing with some of them
  EXPECT_FALSE(parser->FindMemoryRange(0x00).hasValue());
  EXPECT_FALSE(parser->FindMemoryRange(0x2a).hasValue());
  check_mem_range_exists(*parser, 0x10000, 65536); // first range
  check_mem_range_exists(*parser, 0x40000, 4096);
  EXPECT_FALSE(parser->FindMemoryRange(0x40000 + 4096).hasValue());
  check_mem_range_exists(*parser, 0x77c12000, 8192);
  check_mem_range_exists(*parser, 0x7ffe0000, 4096); // last range
  EXPECT_FALSE(parser->FindMemoryRange(0x7ffe0000 + 4096).hasValue());
}

constexpr auto yes = MemoryRegionInfo::eYes;
constexpr auto no = MemoryRegionInfo::eNo;
constexpr auto unknown = MemoryRegionInfo::eDontKnow;

TEST_F(MinidumpParserTest, GetMemoryRegionInfo) {
  ASSERT_THAT_ERROR(SetUpFromYaml(R"(
--- !minidump
Streams:
  - Type:            MemoryInfoList
    Memory Ranges:
      - Base Address:    0x0000000000000000
        Allocation Protect: [  ]
        Region Size:     0x0000000000010000
        State:           [ MEM_FREE ]
        Protect:         [ PAGE_NO_ACCESS ]
        Type:            [  ]
      - Base Address:    0x0000000000010000
        Allocation Protect: [ PAGE_READ_WRITE ]
        Region Size:     0x0000000000021000
        State:           [ MEM_COMMIT ]
        Type:            [ MEM_MAPPED ]
      - Base Address:    0x0000000000040000
        Allocation Protect: [ PAGE_EXECUTE_WRITE_COPY ]
        Region Size:     0x0000000000001000
        State:           [ MEM_COMMIT ]
        Protect:         [ PAGE_READ_ONLY ]
        Type:            [ MEM_IMAGE ]
      - Base Address:    0x000000007FFE0000
        Allocation Protect: [ PAGE_READ_ONLY ]
        Region Size:     0x0000000000001000
        State:           [ MEM_COMMIT ]
        Type:            [ MEM_PRIVATE ]
      - Base Address:    0x000000007FFE1000
        Allocation Base: 0x000000007FFE0000
        Allocation Protect: [ PAGE_READ_ONLY ]
        Region Size:     0x000000000000F000
        State:           [ MEM_RESERVE ]
        Protect:         [ PAGE_NO_ACCESS ]
        Type:            [ MEM_PRIVATE ]
...
)"),
                    llvm::Succeeded());

  EXPECT_THAT(
      parser->BuildMemoryRegions(),
      testing::Pair(testing::ElementsAre(
                        MemoryRegionInfo({0x0, 0x10000}, no, no, no, no,
                                         ConstString(), unknown, 0),
                        MemoryRegionInfo({0x10000, 0x21000}, yes, yes, no, yes,
                                         ConstString(), unknown, 0),
                        MemoryRegionInfo({0x40000, 0x1000}, yes, no, no, yes,
                                         ConstString(), unknown, 0),
                        MemoryRegionInfo({0x7ffe0000, 0x1000}, yes, no, no, yes,
                                         ConstString(), unknown, 0),
                        MemoryRegionInfo({0x7ffe1000, 0xf000}, no, no, no, yes,
                                         ConstString(), unknown, 0)),
                    true));
}

TEST_F(MinidumpParserTest, GetMemoryRegionInfoFromMemoryList) {
  ASSERT_THAT_ERROR(SetUpFromYaml(R"(
--- !minidump
Streams:
  - Type:            MemoryList
    Memory Ranges:
      - Start of Memory Range: 0x0000000000001000
        Content:         '31313131313131313131313131313131'
      - Start of Memory Range: 0x0000000000002000
        Content:         '3333333333333333333333333333333333333333333333333333333333333333'
...
)"),
                    llvm::Succeeded());

  // Test we can get memory regions from the MINIDUMP_MEMORY_LIST stream when
  // we don't have a MemoryInfoListStream.

  EXPECT_THAT(
      parser->BuildMemoryRegions(),
      testing::Pair(testing::ElementsAre(
                        MemoryRegionInfo({0x1000, 0x10}, yes, unknown, unknown,
                                         yes, ConstString(), unknown, 0),
                        MemoryRegionInfo({0x2000, 0x20}, yes, unknown, unknown,
                                         yes, ConstString(), unknown, 0)),
                    false));
}

TEST_F(MinidumpParserTest, GetMemoryRegionInfoFromMemory64List) {
  SetUpData("regions-memlist64.dmp");

  // Test we can get memory regions from the MINIDUMP_MEMORY64_LIST stream when
  // we don't have a MemoryInfoListStream.
  EXPECT_THAT(
      parser->BuildMemoryRegions(),
      testing::Pair(testing::ElementsAre(
                        MemoryRegionInfo({0x1000, 0x10}, yes, unknown, unknown,
                                         yes, ConstString(), unknown, 0),
                        MemoryRegionInfo({0x2000, 0x20}, yes, unknown, unknown,
                                         yes, ConstString(), unknown, 0)),
                    false));
}

TEST_F(MinidumpParserTest, GetMemoryRegionInfoLinuxMaps) {
  ASSERT_THAT_ERROR(SetUpFromYaml(R"(
--- !minidump
Streams:
  - Type:            LinuxMaps
    Text:             |
      400d9000-400db000 r-xp 00000000 b3:04 227        /system/bin/app_process
      400db000-400dc000 r--p 00001000 b3:04 227        /system/bin/app_process
      400dc000-400dd000 rw-p 00000000 00:00 0
      400ec000-400ed000 r--p 00000000 00:00 0
      400ee000-400ef000 rw-p 00010000 b3:04 300        /system/bin/linker
      400fc000-400fd000 rwxp 00001000 b3:04 1096       /system/lib/liblog.so

...
)"),
                    llvm::Succeeded());
  // Test we can get memory regions from the linux /proc/<pid>/maps stream when
  // we don't have a MemoryInfoListStream.
  ConstString app_process("/system/bin/app_process");
  ConstString linker("/system/bin/linker");
  ConstString liblog("/system/lib/liblog.so");
  EXPECT_THAT(
      parser->BuildMemoryRegions(),
      testing::Pair(testing::ElementsAre(
                        MemoryRegionInfo({0x400d9000, 0x2000}, yes, no, yes,
                                         yes, app_process, unknown, 0),
                        MemoryRegionInfo({0x400db000, 0x1000}, yes, no, no, yes,
                                         app_process, unknown, 0),
                        MemoryRegionInfo({0x400dc000, 0x1000}, yes, yes, no,
                                         yes, ConstString(), unknown, 0),
                        MemoryRegionInfo({0x400ec000, 0x1000}, yes, no, no, yes,
                                         ConstString(), unknown, 0),
                        MemoryRegionInfo({0x400ee000, 0x1000}, yes, yes, no,
                                         yes, linker, unknown, 0),
                        MemoryRegionInfo({0x400fc000, 0x1000}, yes, yes, yes,
                                         yes, liblog, unknown, 0)),
                    true));
}

// Windows Minidump tests
TEST_F(MinidumpParserTest, GetArchitectureWindows) {
  ASSERT_THAT_ERROR(SetUpFromYaml(R"(
--- !minidump
Streams:
  - Type:            SystemInfo
    Processor Arch:  X86
    Processor Level: 6
    Processor Revision: 15876
    Number of Processors: 32
    Product type:    1
    Major Version:   6
    Minor Version:   1
    Build Number:    7601
    Platform ID:     Win32NT
    CSD Version:     Service Pack 1
    Suite Mask:      0x0100
    CPU:
      Vendor ID:       GenuineIntel
      Version Info:    0x000306E4
      Feature Info:    0xBFEBFBFF
      AMD Extended Features: 0x771EEC80
...
)"),
                    llvm::Succeeded());
  ASSERT_EQ(llvm::Triple::ArchType::x86,
            parser->GetArchitecture().GetMachine());
  ASSERT_EQ(llvm::Triple::OSType::Win32,
            parser->GetArchitecture().GetTriple().getOS());
}

TEST_F(MinidumpParserTest, GetLinuxProcStatus_no_stream) {
  // Test that GetLinuxProcStatus returns nullptr when the minidump does not
  // contain this stream.
  ASSERT_THAT_ERROR(SetUpFromYaml(R"(
--- !minidump
Streams:
...
)"),
                    llvm::Succeeded());
  EXPECT_EQ(llvm::None, parser->GetLinuxProcStatus());
}

TEST_F(MinidumpParserTest, GetMiscInfoWindows) {
  SetUpData("fizzbuzz_no_heap.dmp");
  const MinidumpMiscInfo *misc_info = parser->GetMiscInfo();
  ASSERT_NE(nullptr, misc_info);
  llvm::Optional<lldb::pid_t> pid = misc_info->GetPid();
  ASSERT_TRUE(pid.hasValue());
  ASSERT_EQ(4440UL, pid.getValue());
}

TEST_F(MinidumpParserTest, GetPidWindows) {
  SetUpData("fizzbuzz_no_heap.dmp");
  llvm::Optional<lldb::pid_t> pid = parser->GetPid();
  ASSERT_TRUE(pid.hasValue());
  ASSERT_EQ(4440UL, pid.getValue());
}

// wow64
TEST_F(MinidumpParserTest, GetPidWow64) {
  SetUpData("fizzbuzz_wow64.dmp");
  llvm::Optional<lldb::pid_t> pid = parser->GetPid();
  ASSERT_TRUE(pid.hasValue());
  ASSERT_EQ(7836UL, pid.getValue());
}

// Register tests
#define REG_VAL32(x) *(reinterpret_cast<uint32_t *>(x))
#define REG_VAL64(x) *(reinterpret_cast<uint64_t *>(x))

TEST_F(MinidumpParserTest, GetThreadContext_x86_32) {
  ASSERT_THAT_ERROR(SetUpFromYaml(R"(
--- !minidump
Streams:
  - Type:            ThreadList
    Threads:
      - Thread Id:       0x00026804
        Stack:
          Start of Memory Range: 0x00000000FF9DD000
          Content:         68D39DFF
        Context:         0F0001000000000000000000000000000000000000000000000000007F03FFFF0000FFFFFFFFFFFF09DC62F72300000088E36CF72B00FFFF00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000063000000000000002B0000002B000000A88204085CD59DFF008077F7A3D49DFF01000000000000003CD59DFFA082040823000000820201002CD59DFF2B0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
)"),
                    llvm::Succeeded());

  llvm::ArrayRef<minidump::Thread> thread_list = parser->GetThreads();
  const minidump::Thread &thread = thread_list[0];
  llvm::ArrayRef<uint8_t> registers(parser->GetThreadContext(thread));
  const MinidumpContext_x86_32 *context;
  EXPECT_TRUE(consumeObject(registers, context).Success());

  EXPECT_EQ(MinidumpContext_x86_32_Flags(uint32_t(context->context_flags)),
            MinidumpContext_x86_32_Flags::x86_32_Flag |
                MinidumpContext_x86_32_Flags::Full |
                MinidumpContext_x86_32_Flags::FloatingPoint);

  EXPECT_EQ(0x00000000u, context->eax);
  EXPECT_EQ(0xf7778000u, context->ebx);
  EXPECT_EQ(0x00000001u, context->ecx);
  EXPECT_EQ(0xff9dd4a3u, context->edx);
  EXPECT_EQ(0x080482a8u, context->edi);
  EXPECT_EQ(0xff9dd55cu, context->esi);
  EXPECT_EQ(0xff9dd53cu, context->ebp);
  EXPECT_EQ(0xff9dd52cu, context->esp);
  EXPECT_EQ(0x080482a0u, context->eip);
  EXPECT_EQ(0x00010282u, context->eflags);
  EXPECT_EQ(0x0023u, context->cs);
  EXPECT_EQ(0x0000u, context->fs);
  EXPECT_EQ(0x0063u, context->gs);
  EXPECT_EQ(0x002bu, context->ss);
  EXPECT_EQ(0x002bu, context->ds);
  EXPECT_EQ(0x002bu, context->es);
}

TEST_F(MinidumpParserTest, GetThreadContext_x86_64) {
  ASSERT_THAT_ERROR(SetUpFromYaml(R"(
--- !minidump
Streams:
  - Type:            ThreadList
    Threads:
      - Thread Id:       0x00003E81
        Stack:
          Start of Memory Range: 0x00007FFCEB34A000
          Content:         C84D04BCE97F00
        Context:         0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000B0010000000000033000000000000000000000006020100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000010A234EBFC7F000010A234EBFC7F00000000000000000000F09C34EBFC7F0000C0A91ABCE97F00000000000000000000A0163FBCE97F00004602000000000000921C40000000000030A434EBFC7F000000000000000000000000000000000000C61D4000000000007F0300000000000000000000000000000000000000000000801F0000FFFF0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000FFFF00FFFFFFFFFFFFFF00FFFFFFFF25252525252525252525252525252525000000000000000000000000000000000000000000000000000000000000000000FFFF00FFFFFFFFFFFFFF00FFFFFFFF0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000FF00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
...
)"),
                    llvm::Succeeded());
  llvm::ArrayRef<minidump::Thread> thread_list = parser->GetThreads();
  const minidump::Thread &thread = thread_list[0];
  llvm::ArrayRef<uint8_t> registers(parser->GetThreadContext(thread));
  const MinidumpContext_x86_64 *context;
  EXPECT_TRUE(consumeObject(registers, context).Success());

  EXPECT_EQ(MinidumpContext_x86_64_Flags(uint32_t(context->context_flags)),
            MinidumpContext_x86_64_Flags::x86_64_Flag |
                MinidumpContext_x86_64_Flags::Control |
                MinidumpContext_x86_64_Flags::FloatingPoint |
                MinidumpContext_x86_64_Flags::Integer);
  EXPECT_EQ(0x0000000000000000u, context->rax);
  EXPECT_EQ(0x0000000000000000u, context->rbx);
  EXPECT_EQ(0x0000000000000010u, context->rcx);
  EXPECT_EQ(0x0000000000000000u, context->rdx);
  EXPECT_EQ(0x00007ffceb349cf0u, context->rdi);
  EXPECT_EQ(0x0000000000000000u, context->rsi);
  EXPECT_EQ(0x00007ffceb34a210u, context->rbp);
  EXPECT_EQ(0x00007ffceb34a210u, context->rsp);
  EXPECT_EQ(0x00007fe9bc1aa9c0u, context->r8);
  EXPECT_EQ(0x0000000000000000u, context->r9);
  EXPECT_EQ(0x00007fe9bc3f16a0u, context->r10);
  EXPECT_EQ(0x0000000000000246u, context->r11);
  EXPECT_EQ(0x0000000000401c92u, context->r12);
  EXPECT_EQ(0x00007ffceb34a430u, context->r13);
  EXPECT_EQ(0x0000000000000000u, context->r14);
  EXPECT_EQ(0x0000000000000000u, context->r15);
  EXPECT_EQ(0x0000000000401dc6u, context->rip);
  EXPECT_EQ(0x00010206u, context->eflags);
  EXPECT_EQ(0x0033u, context->cs);
  EXPECT_EQ(0x0000u, context->ss);
}

TEST_F(MinidumpParserTest, GetThreadContext_x86_32_wow64) {
  SetUpData("fizzbuzz_wow64.dmp");
  llvm::ArrayRef<minidump::Thread> thread_list = parser->GetThreads();
  const minidump::Thread &thread = thread_list[0];
  llvm::ArrayRef<uint8_t> registers(parser->GetThreadContextWow64(thread));
  const MinidumpContext_x86_32 *context;
  EXPECT_TRUE(consumeObject(registers, context).Success());

  EXPECT_EQ(MinidumpContext_x86_32_Flags(uint32_t(context->context_flags)),
            MinidumpContext_x86_32_Flags::x86_32_Flag |
                MinidumpContext_x86_32_Flags::Full |
                MinidumpContext_x86_32_Flags::FloatingPoint |
                MinidumpContext_x86_32_Flags::ExtendedRegisters);

  EXPECT_EQ(0x00000000u, context->eax);
  EXPECT_EQ(0x0037f608u, context->ebx);
  EXPECT_EQ(0x00e61578u, context->ecx);
  EXPECT_EQ(0x00000008u, context->edx);
  EXPECT_EQ(0x00000000u, context->edi);
  EXPECT_EQ(0x00000002u, context->esi);
  EXPECT_EQ(0x0037f654u, context->ebp);
  EXPECT_EQ(0x0037f5b8u, context->esp);
  EXPECT_EQ(0x77ce01fdu, context->eip);
  EXPECT_EQ(0x00000246u, context->eflags);
  EXPECT_EQ(0x0023u, context->cs);
  EXPECT_EQ(0x0053u, context->fs);
  EXPECT_EQ(0x002bu, context->gs);
  EXPECT_EQ(0x002bu, context->ss);
  EXPECT_EQ(0x002bu, context->ds);
  EXPECT_EQ(0x002bu, context->es);
}

TEST_F(MinidumpParserTest, MinidumpDuplicateModuleMinAddress) {
  ASSERT_THAT_ERROR(SetUpFromYaml(R"(
--- !minidump
Streams:
  - Type:            ModuleList
    Modules:
      - Base of Image:   0x0000000000002000
        Size of Image:   0x00001000
        Module Name:     '/tmp/a'
        CodeView Record: ''
      - Base of Image:   0x0000000000001000
        Size of Image:   0x00001000
        Module Name:     '/tmp/a'
        CodeView Record: ''
...
)"),
                    llvm::Succeeded());
  // If we have a module mentioned twice in the module list, the filtered
  // module list should contain the instance with the lowest BaseOfImage.
  std::vector<const minidump::Module *> filtered_modules =
      parser->GetFilteredModuleList();
  ASSERT_EQ(1u, filtered_modules.size());
  EXPECT_EQ(0x0000000000001000u, filtered_modules[0]->BaseOfImage);
}

TEST_F(MinidumpParserTest, MinidumpDuplicateModuleMappedFirst) {
  ASSERT_THAT_ERROR(SetUpFromYaml(R"(
--- !minidump
Streams:
  - Type:            ModuleList
    Modules:
      - Base of Image:   0x400d0000
        Size of Image:   0x00002000
        Module Name:     '/usr/lib/libc.so'
        CodeView Record: ''
      - Base of Image:   0x400d3000
        Size of Image:   0x00001000
        Module Name:     '/usr/lib/libc.so'
        CodeView Record: ''
  - Type:            LinuxMaps
    Text:             |
      400d0000-400d2000 r--p 00000000 b3:04 227        /usr/lib/libc.so
      400d2000-400d3000 rw-p 00000000 00:00 0
      400d3000-400d4000 r-xp 00010000 b3:04 227        /usr/lib/libc.so
      400d4000-400d5000 rwxp 00001000 b3:04 227        /usr/lib/libc.so
...
)"),
                    llvm::Succeeded());
  // If we have a module mentioned twice in the module list, and we have full
  // linux maps for all of the memory regions, make sure we pick the one that
  // has a consecutive region with a matching path that has executable
  // permissions. If clients open an object file with mmap, breakpad can create
  // multiple mappings for a library errnoneously and the lowest address isn't
  // always the right address. In this case we check the consective memory
  // regions whose path matches starting at the base of image address and make
  // sure one of the regions is executable and prefer that one.
  //
  // This test will make sure that if the executable is second in the module
  // list, that it will become the selected module in the filtered list.
  std::vector<const minidump::Module *> filtered_modules =
      parser->GetFilteredModuleList();
  ASSERT_EQ(1u, filtered_modules.size());
  EXPECT_EQ(0x400d3000u, filtered_modules[0]->BaseOfImage);
}

TEST_F(MinidumpParserTest, MinidumpDuplicateModuleMappedSecond) {
  ASSERT_THAT_ERROR(SetUpFromYaml(R"(
--- !minidump
Streams:
  - Type:            ModuleList
    Modules:
      - Base of Image:   0x400d0000
        Size of Image:   0x00002000
        Module Name:     '/usr/lib/libc.so'
        CodeView Record: ''
      - Base of Image:   0x400d3000
        Size of Image:   0x00001000
        Module Name:     '/usr/lib/libc.so'
        CodeView Record: ''
  - Type:            LinuxMaps
    Text:             |
      400d0000-400d1000 r-xp 00010000 b3:04 227        /usr/lib/libc.so
      400d1000-400d2000 rwxp 00001000 b3:04 227        /usr/lib/libc.so
      400d2000-400d3000 rw-p 00000000 00:00 0
      400d3000-400d5000 r--p 00000000 b3:04 227        /usr/lib/libc.so
...
)"),
                    llvm::Succeeded());
  // If we have a module mentioned twice in the module list, and we have full
  // linux maps for all of the memory regions, make sure we pick the one that
  // has a consecutive region with a matching path that has executable
  // permissions. If clients open an object file with mmap, breakpad can create
  // multiple mappings for a library errnoneously and the lowest address isn't
  // always the right address. In this case we check the consective memory
  // regions whose path matches starting at the base of image address and make
  // sure one of the regions is executable and prefer that one.
  //
  // This test will make sure that if the executable is first in the module
  // list, that it will remain the correctly selected module in the filtered
  // list.
  std::vector<const minidump::Module *> filtered_modules =
      parser->GetFilteredModuleList();
  ASSERT_EQ(1u, filtered_modules.size());
  EXPECT_EQ(0x400d0000u, filtered_modules[0]->BaseOfImage);
}

TEST_F(MinidumpParserTest, MinidumpDuplicateModuleSeparateCode) {
  ASSERT_THAT_ERROR(SetUpFromYaml(R"(
--- !minidump
Streams:
  - Type:            ModuleList
    Modules:
      - Base of Image:   0x400d0000
        Size of Image:   0x00002000
        Module Name:     '/usr/lib/libc.so'
        CodeView Record: ''
      - Base of Image:   0x400d5000
        Size of Image:   0x00001000
        Module Name:     '/usr/lib/libc.so'
        CodeView Record: ''
  - Type:            LinuxMaps
    Text:             |
      400d0000-400d3000 r--p 00000000 b3:04 227        /usr/lib/libc.so
      400d3000-400d5000 rw-p 00000000 00:00 0
      400d5000-400d6000 r--p 00000000 b3:04 227        /usr/lib/libc.so
      400d6000-400d7000 r-xp 00010000 b3:04 227        /usr/lib/libc.so
      400d7000-400d8000 rwxp 00001000 b3:04 227        /usr/lib/libc.so
...
)"),
                    llvm::Succeeded());
  // If we have a module mentioned twice in the module list, and we have full
  // linux maps for all of the memory regions, make sure we pick the one that
  // has a consecutive region with a matching path that has executable
  // permissions. If clients open an object file with mmap, breakpad can create
  // multiple mappings for a library errnoneously and the lowest address isn't
  // always the right address. In this case we check the consective memory
  // regions whose path matches starting at the base of image address and make
  // sure one of the regions is executable and prefer that one.
  //
  // This test will make sure if binaries are compiled with "-z separate-code",
  // where the first region for a binary won't be marked as executable, that
  // it gets selected by detecting the second consecutive mapping at 0x400d7000
  // when asked about the a module mamed "/usr/lib/libc.so" at 0x400d5000.
  std::vector<const minidump::Module *> filtered_modules =
      parser->GetFilteredModuleList();
  ASSERT_EQ(1u, filtered_modules.size());
  EXPECT_EQ(0x400d5000u, filtered_modules[0]->BaseOfImage);
}

TEST_F(MinidumpParserTest, MinidumpModuleOrder) {
  ASSERT_THAT_ERROR(SetUpFromYaml(R"(
--- !minidump
Streams:
  - Type:            ModuleList
    Modules:
      - Base of Image:   0x0000000000002000
        Size of Image:   0x00001000
        Module Name:     '/tmp/a'
        CodeView Record: ''
      - Base of Image:   0x0000000000001000
        Size of Image:   0x00001000
        Module Name:     '/tmp/b'
        CodeView Record: ''
...
)"),
                    llvm::Succeeded());
  // Test module filtering does not affect the overall module order.  Previous
  // versions of the MinidumpParser::GetFilteredModuleList() function would sort
  // all images by address and modify the order of the modules.
  std::vector<const minidump::Module *> filtered_modules =
      parser->GetFilteredModuleList();
  ASSERT_EQ(2u, filtered_modules.size());
  EXPECT_EQ(0x0000000000002000u, filtered_modules[0]->BaseOfImage);
  EXPECT_THAT_EXPECTED(
      parser->GetMinidumpFile().getString(filtered_modules[0]->ModuleNameRVA),
      llvm::HasValue("/tmp/a"));
  EXPECT_EQ(0x0000000000001000u, filtered_modules[1]->BaseOfImage);
  EXPECT_THAT_EXPECTED(
      parser->GetMinidumpFile().getString(filtered_modules[1]->ModuleNameRVA),
      llvm::HasValue("/tmp/b"));
}