1 //===-- RegisterContextMinidumpTest.cpp -------------------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "Plugins/Process/Utility/RegisterContextLinux_i386.h"
10 #include "Plugins/Process/Utility/RegisterContextLinux_x86_64.h"
11 #include "Plugins/Process/minidump/RegisterContextMinidump_x86_32.h"
12 #include "Plugins/Process/minidump/RegisterContextMinidump_x86_64.h"
13 #include "lldb/Utility/DataBuffer.h"
14 #include "gtest/gtest.h"
15 
16 using namespace lldb_private;
17 using namespace lldb_private::minidump;
18 
19 static uint32_t reg32(const DataBuffer &Buf, const RegisterInfo &Info) {
20   return *reinterpret_cast<const uint32_t *>(Buf.GetBytes() + Info.byte_offset);
21 }
22 
23 static uint64_t reg64(const DataBuffer &Buf, const RegisterInfo &Info) {
24   return *reinterpret_cast<const uint64_t *>(Buf.GetBytes() + Info.byte_offset);
25 }
26 
27 TEST(RegisterContextMinidump, ConvertMinidumpContext_x86_32) {
28   MinidumpContext_x86_32 Context;
29   Context.context_flags =
30       static_cast<uint32_t>(MinidumpContext_x86_32_Flags::x86_32_Flag |
31                             MinidumpContext_x86_32_Flags::Control |
32                             MinidumpContext_x86_32_Flags::Segments |
33                             MinidumpContext_x86_32_Flags::Integer);
34   Context.eax = 0x00010203;
35   Context.ebx = 0x04050607;
36   Context.ecx = 0x08090a0b;
37   Context.edx = 0x0c0d0e0f;
38   Context.edi = 0x10111213;
39   Context.esi = 0x14151617;
40   Context.ebp = 0x18191a1b;
41   Context.esp = 0x1c1d1e1f;
42   Context.eip = 0x20212223;
43   Context.eflags = 0x24252627;
44   Context.cs = 0x2829;
45   Context.fs = 0x2a2b;
46   Context.gs = 0x2c2d;
47   Context.ss = 0x2e2f;
48   Context.ds = 0x3031;
49   Context.es = 0x3233;
50   llvm::ArrayRef<uint8_t> ContextRef(reinterpret_cast<uint8_t *>(&Context),
51                                      sizeof(Context));
52 
53   ArchSpec arch("i386-pc-linux");
54   auto RegInterface = std::make_unique<RegisterContextLinux_i386>(arch);
55   lldb::DataBufferSP Buf =
56       ConvertMinidumpContext_x86_32(ContextRef, RegInterface.get());
57   ASSERT_EQ(RegInterface->GetGPRSize(), Buf->GetByteSize());
58 
59   const RegisterInfo *Info = RegInterface->GetRegisterInfo();
60   ASSERT_NE(nullptr, Info);
61 
62   EXPECT_EQ(Context.eax, reg32(*Buf, Info[lldb_eax_i386]));
63   EXPECT_EQ(Context.ebx, reg32(*Buf, Info[lldb_ebx_i386]));
64   EXPECT_EQ(Context.ecx, reg32(*Buf, Info[lldb_ecx_i386]));
65   EXPECT_EQ(Context.edx, reg32(*Buf, Info[lldb_edx_i386]));
66   EXPECT_EQ(Context.edi, reg32(*Buf, Info[lldb_edi_i386]));
67   EXPECT_EQ(Context.esi, reg32(*Buf, Info[lldb_esi_i386]));
68   EXPECT_EQ(Context.ebp, reg32(*Buf, Info[lldb_ebp_i386]));
69   EXPECT_EQ(Context.esp, reg32(*Buf, Info[lldb_esp_i386]));
70   EXPECT_EQ(Context.eip, reg32(*Buf, Info[lldb_eip_i386]));
71   EXPECT_EQ(Context.eflags, reg32(*Buf, Info[lldb_eflags_i386]));
72   EXPECT_EQ(Context.cs, reg32(*Buf, Info[lldb_cs_i386]));
73   EXPECT_EQ(Context.fs, reg32(*Buf, Info[lldb_fs_i386]));
74   EXPECT_EQ(Context.gs, reg32(*Buf, Info[lldb_gs_i386]));
75   EXPECT_EQ(Context.ss, reg32(*Buf, Info[lldb_ss_i386]));
76   EXPECT_EQ(Context.ds, reg32(*Buf, Info[lldb_ds_i386]));
77   EXPECT_EQ(Context.es, reg32(*Buf, Info[lldb_es_i386]));
78 }
79 
80 TEST(RegisterContextMinidump, ConvertMinidumpContext_x86_64) {
81   MinidumpContext_x86_64 Context;
82   Context.context_flags =
83       static_cast<uint32_t>(MinidumpContext_x86_64_Flags::x86_64_Flag |
84                             MinidumpContext_x86_64_Flags::Control |
85                             MinidumpContext_x86_64_Flags::Segments |
86                             MinidumpContext_x86_64_Flags::Integer);
87   Context.rax = 0x0001020304050607;
88   Context.rbx = 0x08090a0b0c0d0e0f;
89   Context.rcx = 0x1011121314151617;
90   Context.rdx = 0x18191a1b1c1d1e1f;
91   Context.rdi = 0x2021222324252627;
92   Context.rsi = 0x28292a2b2c2d2e2f;
93   Context.rbp = 0x3031323334353637;
94   Context.rsp = 0x38393a3b3c3d3e3f;
95   Context.r8 = 0x4041424344454647;
96   Context.r9 = 0x48494a4b4c4d4e4f;
97   Context.r10 = 0x5051525354555657;
98   Context.r11 = 0x58595a5b5c5d5e5f;
99   Context.r12 = 0x6061626364656667;
100   Context.r13 = 0x68696a6b6c6d6e6f;
101   Context.r14 = 0x7071727374757677;
102   Context.r15 = 0x78797a7b7c7d7e7f;
103   Context.rip = 0x8081828384858687;
104   Context.eflags = 0x88898a8b;
105   Context.cs = 0x8c8d;
106   Context.fs = 0x8e8f;
107   Context.gs = 0x9091;
108   Context.ss = 0x9293;
109   Context.ds = 0x9495;
110   Context.ss = 0x9697;
111   llvm::ArrayRef<uint8_t> ContextRef(reinterpret_cast<uint8_t *>(&Context),
112                                      sizeof(Context));
113 
114   ArchSpec arch("x86_64-pc-linux");
115   auto RegInterface = std::make_unique<RegisterContextLinux_x86_64>(arch);
116   lldb::DataBufferSP Buf =
117       ConvertMinidumpContext_x86_64(ContextRef, RegInterface.get());
118   ASSERT_EQ(RegInterface->GetGPRSize(), Buf->GetByteSize());
119 
120   const RegisterInfo *Info = RegInterface->GetRegisterInfo();
121   EXPECT_EQ(Context.rax, reg64(*Buf, Info[lldb_rax_x86_64]));
122   EXPECT_EQ(Context.rbx, reg64(*Buf, Info[lldb_rbx_x86_64]));
123   EXPECT_EQ(Context.rcx, reg64(*Buf, Info[lldb_rcx_x86_64]));
124   EXPECT_EQ(Context.rdx, reg64(*Buf, Info[lldb_rdx_x86_64]));
125   EXPECT_EQ(Context.rdi, reg64(*Buf, Info[lldb_rdi_x86_64]));
126   EXPECT_EQ(Context.rsi, reg64(*Buf, Info[lldb_rsi_x86_64]));
127   EXPECT_EQ(Context.rbp, reg64(*Buf, Info[lldb_rbp_x86_64]));
128   EXPECT_EQ(Context.rsp, reg64(*Buf, Info[lldb_rsp_x86_64]));
129   EXPECT_EQ(Context.r8, reg64(*Buf, Info[lldb_r8_x86_64]));
130   EXPECT_EQ(Context.r9, reg64(*Buf, Info[lldb_r9_x86_64]));
131   EXPECT_EQ(Context.r10, reg64(*Buf, Info[lldb_r10_x86_64]));
132   EXPECT_EQ(Context.r11, reg64(*Buf, Info[lldb_r11_x86_64]));
133   EXPECT_EQ(Context.r12, reg64(*Buf, Info[lldb_r12_x86_64]));
134   EXPECT_EQ(Context.r13, reg64(*Buf, Info[lldb_r13_x86_64]));
135   EXPECT_EQ(Context.r14, reg64(*Buf, Info[lldb_r14_x86_64]));
136   EXPECT_EQ(Context.r15, reg64(*Buf, Info[lldb_r15_x86_64]));
137   EXPECT_EQ(Context.rip, reg64(*Buf, Info[lldb_rip_x86_64]));
138   EXPECT_EQ(Context.eflags, reg64(*Buf, Info[lldb_rflags_x86_64]));
139   EXPECT_EQ(Context.cs, reg64(*Buf, Info[lldb_cs_x86_64]));
140   EXPECT_EQ(Context.fs, reg64(*Buf, Info[lldb_fs_x86_64]));
141   EXPECT_EQ(Context.gs, reg64(*Buf, Info[lldb_gs_x86_64]));
142   EXPECT_EQ(Context.ss, reg64(*Buf, Info[lldb_ss_x86_64]));
143   EXPECT_EQ(Context.ds, reg64(*Buf, Info[lldb_ds_x86_64]));
144   EXPECT_EQ(Context.es, reg64(*Buf, Info[lldb_es_x86_64]));
145 }
146