blob: 823488b39153e49ffd11d6c53ebc67fb67eb9130 [file] [log] [blame]
Jason Molenda74b8fbc2016-09-29 01:00:16 +00001//===-- Testx86AssemblyInspectionEngine.cpp ---------------------------*- C++
2//-*-===//
3
4//
5// The LLVM Compiler Infrastructure
6//
7// This file is distributed under the University of Illinois Open Source
8// License. See LICENSE.TXT for details.
9//
10//===----------------------------------------------------------------------===//
11
12#include "gtest/gtest.h"
13
14#include <vector>
15
16#include "Plugins/UnwindAssembly/x86/x86AssemblyInspectionEngine.h"
17#include "lldb/Core/Address.h"
18#include "lldb/Core/AddressRange.h"
19#include "lldb/Core/ArchSpec.h"
20#include "lldb/Symbol/UnwindPlan.h"
21
22#include "llvm/Support/TargetSelect.h"
23
24using namespace lldb;
25using namespace lldb_private;
26
27class Testx86AssemblyInspectionEngine : public testing::Test {
28public:
29 // static void SetUpTestCase() { }
30
31 // static void TearDownTestCase() { }
32
33 // virtual void SetUp() override { }
34
35 // virtual void TearDown() override { }
36
37protected:
38};
39
40// only defining the register names / numbers that the unwinder is actually
41// using today
42
43// names should match the constants below. These will be the eRegisterKindLLDB
44// register numbers.
45
Jason Molenda415f7322016-09-29 04:01:43 +000046const char *x86_64_reg_names[] = {"rax", "rbx", "rcx", "rdx", "rsp", "rbp",
47 "rsi", "rdi", "r8", "r9", "r10", "r11",
48 "r12", "r13", "r14", "r15", "rip"};
Jason Molenda74b8fbc2016-09-29 01:00:16 +000049
50enum x86_64_regs {
51 k_rax = 0,
Jason Molenda415f7322016-09-29 04:01:43 +000052 k_rbx = 1,
53 k_rcx = 2,
54 k_rdx = 3,
55 k_rsp = 4,
56 k_rbp = 5,
57 k_rsi = 6,
58 k_rdi = 7,
59 k_r8 = 8,
60 k_r9 = 9,
61 k_r10 = 10,
62 k_r11 = 11,
63 k_r12 = 12,
64 k_r13 = 13,
65 k_r14 = 14,
66 k_r15 = 15,
67 k_rip = 16
Jason Molenda74b8fbc2016-09-29 01:00:16 +000068};
69
70// names should match the constants below. These will be the eRegisterKindLLDB
71// register numbers.
72
73const char *i386_reg_names[] = {"eax", "ecx", "edx", "ebx", "esp",
74 "ebp", "esi", "edi", "eip"};
75
76enum i386_regs {
77 k_eax = 0,
78 k_ecx = 1,
79 k_edx = 2,
80 k_ebx = 3,
81 k_esp = 4,
82 k_ebp = 5,
83 k_esi = 6,
84 k_edi = 7,
85 k_eip = 8
86};
87
88std::unique_ptr<x86AssemblyInspectionEngine> Getx86_64Inspector() {
89
90 ArchSpec arch("x86_64-apple-macosx", nullptr);
91 llvm::InitializeAllTargets();
92 llvm::InitializeAllAsmPrinters();
93 llvm::InitializeAllTargetMCs();
94 llvm::InitializeAllDisassemblers();
95 std::unique_ptr<x86AssemblyInspectionEngine> engine(
96 new x86AssemblyInspectionEngine(arch));
97
98 std::vector<x86AssemblyInspectionEngine::lldb_reg_info> lldb_regnums;
99 int i = 0;
100 for (const auto &name : x86_64_reg_names) {
101 x86AssemblyInspectionEngine::lldb_reg_info ri;
102 ri.name = name;
103 ri.lldb_regnum = i++;
104 lldb_regnums.push_back(ri);
105 }
106
107 engine->Initialize(lldb_regnums);
108 return engine;
109}
110
111std::unique_ptr<x86AssemblyInspectionEngine> Geti386Inspector() {
112
113 ArchSpec arch("i386-apple-macosx", nullptr);
114 llvm::InitializeAllTargets();
115 llvm::InitializeAllAsmPrinters();
116 llvm::InitializeAllTargetMCs();
117 llvm::InitializeAllDisassemblers();
118 std::unique_ptr<x86AssemblyInspectionEngine> engine(
119 new x86AssemblyInspectionEngine(arch));
120
121 std::vector<x86AssemblyInspectionEngine::lldb_reg_info> lldb_regnums;
122 int i = 0;
123 for (const auto &name : i386_reg_names) {
124 x86AssemblyInspectionEngine::lldb_reg_info ri;
125 ri.name = name;
126 ri.lldb_regnum = i++;
127 lldb_regnums.push_back(ri);
128 }
129
130 engine->Initialize(lldb_regnums);
131 return engine;
132}
133
134TEST_F(Testx86AssemblyInspectionEngine, TestSimple64bitFrameFunction) {
135 std::unique_ptr<x86AssemblyInspectionEngine> engine = Getx86_64Inspector();
136
Jason Molenda415f7322016-09-29 04:01:43 +0000137 // 'int main() { }' compiled for x86_64-apple-macosx with clang
Jason Molenda74b8fbc2016-09-29 01:00:16 +0000138 uint8_t data[] = {
139 0x55, // offset 0 -- pushq %rbp
140 0x48, 0x89, 0xe5, // offset 1 -- movq %rsp, %rbp
141 0x31, 0xc0, // offset 4 -- xorl %eax, %eax
142 0x5d, // offset 6 -- popq %rbp
143 0xc3 // offset 7 -- retq
144 };
145
146 AddressRange sample_range(0x1000, sizeof(data));
147
148 UnwindPlan unwind_plan(eRegisterKindLLDB);
149 EXPECT_TRUE(engine->GetNonCallSiteUnwindPlanFromAssembly(
150 data, sizeof(data), sample_range, unwind_plan));
151
152 // Expect four unwind rows:
153 // 0: CFA=rsp +8 => rsp=CFA+0 rip=[CFA-8]
154 // 1: CFA=rsp+16 => rbp=[CFA-16] rsp=CFA+0 rip=[CFA-8]
155 // 4: CFA=rbp+16 => rbp=[CFA-16] rsp=CFA+0 rip=[CFA-8]
156 // 7: CFA=rsp +8 => rsp=CFA+0 rip=[CFA-8]
157
158 EXPECT_TRUE(unwind_plan.GetInitialCFARegister() == k_rsp);
159 EXPECT_TRUE(unwind_plan.GetUnwindPlanValidAtAllInstructions() ==
160 eLazyBoolYes);
161 EXPECT_TRUE(unwind_plan.GetSourcedFromCompiler() == eLazyBoolNo);
162
163 UnwindPlan::Row::RegisterLocation regloc;
164
165 // 0: CFA=rsp +8 => rsp=CFA+0 rip=[CFA-8]
166 UnwindPlan::RowSP row_sp = unwind_plan.GetRowForFunctionOffset(0);
Jason Molendaf96c13d2016-09-29 23:57:33 +0000167 EXPECT_EQ(0, row_sp->GetOffset());
Jason Molenda74b8fbc2016-09-29 01:00:16 +0000168 EXPECT_TRUE(row_sp->GetCFAValue().GetRegisterNumber() == k_rsp);
169 EXPECT_TRUE(row_sp->GetCFAValue().IsRegisterPlusOffset() == true);
Jason Molendaf96c13d2016-09-29 23:57:33 +0000170 EXPECT_EQ(8, row_sp->GetCFAValue().GetOffset());
Jason Molenda74b8fbc2016-09-29 01:00:16 +0000171
172 EXPECT_TRUE(row_sp->GetRegisterInfo(k_rip, regloc));
173 EXPECT_TRUE(regloc.IsAtCFAPlusOffset());
Jason Molendaf96c13d2016-09-29 23:57:33 +0000174 EXPECT_EQ(-8, regloc.GetOffset());
Jason Molenda74b8fbc2016-09-29 01:00:16 +0000175
176 // 1: CFA=rsp+16 => rbp=[CFA-16] rsp=CFA+0 rip=[CFA-8]
177 row_sp = unwind_plan.GetRowForFunctionOffset(1);
Jason Molendaf96c13d2016-09-29 23:57:33 +0000178 EXPECT_EQ(1, row_sp->GetOffset());
Jason Molenda74b8fbc2016-09-29 01:00:16 +0000179 EXPECT_TRUE(row_sp->GetCFAValue().GetRegisterNumber() == k_rsp);
180 EXPECT_TRUE(row_sp->GetCFAValue().IsRegisterPlusOffset() == true);
Jason Molendaf96c13d2016-09-29 23:57:33 +0000181 EXPECT_EQ(16, row_sp->GetCFAValue().GetOffset());
Jason Molenda74b8fbc2016-09-29 01:00:16 +0000182
183 EXPECT_TRUE(row_sp->GetRegisterInfo(k_rip, regloc));
184 EXPECT_TRUE(regloc.IsAtCFAPlusOffset());
Jason Molendaf96c13d2016-09-29 23:57:33 +0000185 EXPECT_EQ(-8, regloc.GetOffset());
Jason Molenda74b8fbc2016-09-29 01:00:16 +0000186
187 // 4: CFA=rbp+16 => rbp=[CFA-16] rsp=CFA+0 rip=[CFA-8]
188 row_sp = unwind_plan.GetRowForFunctionOffset(4);
Jason Molendaf96c13d2016-09-29 23:57:33 +0000189 EXPECT_EQ(4, row_sp->GetOffset());
Jason Molenda74b8fbc2016-09-29 01:00:16 +0000190 EXPECT_TRUE(row_sp->GetCFAValue().GetRegisterNumber() == k_rbp);
191 EXPECT_TRUE(row_sp->GetCFAValue().IsRegisterPlusOffset() == true);
Jason Molendaf96c13d2016-09-29 23:57:33 +0000192 EXPECT_EQ(16, row_sp->GetCFAValue().GetOffset());
Jason Molenda74b8fbc2016-09-29 01:00:16 +0000193
194 EXPECT_TRUE(row_sp->GetRegisterInfo(k_rip, regloc));
195 EXPECT_TRUE(regloc.IsAtCFAPlusOffset());
Jason Molendaf96c13d2016-09-29 23:57:33 +0000196 EXPECT_EQ(-8, regloc.GetOffset());
Jason Molenda74b8fbc2016-09-29 01:00:16 +0000197
198 // 7: CFA=rsp +8 => rsp=CFA+0 rip=[CFA-8]
199 row_sp = unwind_plan.GetRowForFunctionOffset(7);
Jason Molendaf96c13d2016-09-29 23:57:33 +0000200 EXPECT_EQ(7, row_sp->GetOffset());
Jason Molenda74b8fbc2016-09-29 01:00:16 +0000201 EXPECT_TRUE(row_sp->GetCFAValue().GetRegisterNumber() == k_rsp);
202 EXPECT_TRUE(row_sp->GetCFAValue().IsRegisterPlusOffset() == true);
Jason Molendaf96c13d2016-09-29 23:57:33 +0000203 EXPECT_EQ(8, row_sp->GetCFAValue().GetOffset());
Jason Molenda74b8fbc2016-09-29 01:00:16 +0000204
205 EXPECT_TRUE(row_sp->GetRegisterInfo(k_rip, regloc));
206 EXPECT_TRUE(regloc.IsAtCFAPlusOffset());
Jason Molendaf96c13d2016-09-29 23:57:33 +0000207 EXPECT_EQ(-8, regloc.GetOffset());
Jason Molenda74b8fbc2016-09-29 01:00:16 +0000208}
209
210TEST_F(Testx86AssemblyInspectionEngine, TestSimple32bitFrameFunction) {
211 std::unique_ptr<x86AssemblyInspectionEngine> engine = Geti386Inspector();
212
Jason Molenda415f7322016-09-29 04:01:43 +0000213 // 'int main() { }' compiled for i386-apple-macosx with clang
Jason Molenda74b8fbc2016-09-29 01:00:16 +0000214 uint8_t data[] = {
215 0x55, // offset 0 -- pushl %ebp
216 0x89, 0xe5, // offset 1 -- movl %esp, %ebp
217 0x31, 0xc0, // offset 3 -- xorl %eax, %eax
218 0x5d, // offset 5 -- popl %ebp
219 0xc3 // offset 6 -- retl
220 };
221
222 AddressRange sample_range(0x1000, sizeof(data));
223
224 UnwindPlan unwind_plan(eRegisterKindLLDB);
225 EXPECT_TRUE(engine->GetNonCallSiteUnwindPlanFromAssembly(
226 data, sizeof(data), sample_range, unwind_plan));
227
228 // Expect four unwind rows:
229 // 0: CFA=esp +4 => esp=CFA+0 eip=[CFA-4]
230 // 1: CFA=esp +8 => ebp=[CFA-8] esp=CFA+0 eip=[CFA-4]
231 // 3: CFA=ebp +8 => ebp=[CFA-8] esp=CFA+0 eip=[CFA-4]
232 // 6: CFA=esp +4 => esp=CFA+0 eip=[CFA-4]
233
234 EXPECT_TRUE(unwind_plan.GetInitialCFARegister() == k_esp);
235 EXPECT_TRUE(unwind_plan.GetUnwindPlanValidAtAllInstructions() ==
236 eLazyBoolYes);
237 EXPECT_TRUE(unwind_plan.GetSourcedFromCompiler() == eLazyBoolNo);
238
239 UnwindPlan::Row::RegisterLocation regloc;
240
241 // offset 0 -- pushl %ebp
242 UnwindPlan::RowSP row_sp = unwind_plan.GetRowForFunctionOffset(0);
Jason Molendaf96c13d2016-09-29 23:57:33 +0000243 EXPECT_EQ(0, row_sp->GetOffset());
Jason Molenda74b8fbc2016-09-29 01:00:16 +0000244 EXPECT_TRUE(row_sp->GetCFAValue().GetRegisterNumber() == k_esp);
245 EXPECT_TRUE(row_sp->GetCFAValue().IsRegisterPlusOffset() == true);
Jason Molendaf96c13d2016-09-29 23:57:33 +0000246 EXPECT_EQ(4, row_sp->GetCFAValue().GetOffset());
Jason Molenda74b8fbc2016-09-29 01:00:16 +0000247
248 EXPECT_TRUE(row_sp->GetRegisterInfo(k_eip, regloc));
249 EXPECT_TRUE(regloc.IsAtCFAPlusOffset());
250 EXPECT_TRUE(regloc.GetOffset() == -4);
251
252 // 1: CFA=esp +8 => ebp=[CFA-8] esp=CFA+0 eip=[CFA-4]
253 row_sp = unwind_plan.GetRowForFunctionOffset(1);
Jason Molendaf96c13d2016-09-29 23:57:33 +0000254 EXPECT_EQ(1, row_sp->GetOffset());
Jason Molenda74b8fbc2016-09-29 01:00:16 +0000255 EXPECT_TRUE(row_sp->GetCFAValue().GetRegisterNumber() == k_esp);
256 EXPECT_TRUE(row_sp->GetCFAValue().IsRegisterPlusOffset() == true);
Jason Molendaf96c13d2016-09-29 23:57:33 +0000257 EXPECT_EQ(8, row_sp->GetCFAValue().GetOffset());
Jason Molenda74b8fbc2016-09-29 01:00:16 +0000258
259 EXPECT_TRUE(row_sp->GetRegisterInfo(k_eip, regloc));
260 EXPECT_TRUE(regloc.IsAtCFAPlusOffset());
Jason Molendaf96c13d2016-09-29 23:57:33 +0000261 EXPECT_EQ(-4, regloc.GetOffset());
Jason Molenda74b8fbc2016-09-29 01:00:16 +0000262
263 // 3: CFA=ebp +8 => ebp=[CFA-8] esp=CFA+0 eip=[CFA-4]
264 row_sp = unwind_plan.GetRowForFunctionOffset(3);
Jason Molendaf96c13d2016-09-29 23:57:33 +0000265 EXPECT_EQ(3, row_sp->GetOffset());
Jason Molenda74b8fbc2016-09-29 01:00:16 +0000266 EXPECT_TRUE(row_sp->GetCFAValue().GetRegisterNumber() == k_ebp);
267 EXPECT_TRUE(row_sp->GetCFAValue().IsRegisterPlusOffset() == true);
Jason Molendaf96c13d2016-09-29 23:57:33 +0000268 EXPECT_EQ(8, row_sp->GetCFAValue().GetOffset());
Jason Molenda74b8fbc2016-09-29 01:00:16 +0000269
270 EXPECT_TRUE(row_sp->GetRegisterInfo(k_eip, regloc));
271 EXPECT_TRUE(regloc.IsAtCFAPlusOffset());
Jason Molendaf96c13d2016-09-29 23:57:33 +0000272 EXPECT_EQ(-4, regloc.GetOffset());
Jason Molenda74b8fbc2016-09-29 01:00:16 +0000273
274 // 6: CFA=esp +4 => esp=CFA+0 eip=[CFA-4]
275 row_sp = unwind_plan.GetRowForFunctionOffset(6);
Jason Molendaf96c13d2016-09-29 23:57:33 +0000276 EXPECT_EQ(6, row_sp->GetOffset());
Jason Molenda74b8fbc2016-09-29 01:00:16 +0000277 EXPECT_TRUE(row_sp->GetCFAValue().GetRegisterNumber() == k_esp);
278 EXPECT_TRUE(row_sp->GetCFAValue().IsRegisterPlusOffset() == true);
Jason Molendaf96c13d2016-09-29 23:57:33 +0000279 EXPECT_EQ(4, row_sp->GetCFAValue().GetOffset());
Jason Molenda74b8fbc2016-09-29 01:00:16 +0000280
281 EXPECT_TRUE(row_sp->GetRegisterInfo(k_eip, regloc));
282 EXPECT_TRUE(regloc.IsAtCFAPlusOffset());
Jason Molendaf96c13d2016-09-29 23:57:33 +0000283 EXPECT_EQ(-4, regloc.GetOffset());
Jason Molenda74b8fbc2016-09-29 01:00:16 +0000284}
Jason Molenda415f7322016-09-29 04:01:43 +0000285
286TEST_F(Testx86AssemblyInspectionEngine, Test64bitFramelessBigStackFrame) {
287 std::unique_ptr<x86AssemblyInspectionEngine> engine = Getx86_64Inspector();
288
289 // this source file:
290 //
291 // #include <stdio.h>
292 // int main (int argc, char **argv)
293 // {
294 //
295 // const int arrsize = 60;
296 // int buf[arrsize * arrsize];
297 // int accum = argc;
298 // for (int i = 0; i < arrsize; i++)
299 // for (int j = 0; j < arrsize; j++)
300 // {
301 // if (i > 0 && j > 0)
302 // {
303 // int n = buf[(i-1) * (j-1)] * 2;
304 // int m = buf[(i-1) * (j-1)] / 2;
305 // int j = buf[(i-1) * (j-1)] + 2;
306 // int k = buf[(i-1) * (j-1)] - 2;
307 // printf ("%d ", n + m + j + k);
308 // buf[(i-1) * (j-1)] += n - m + j - k;
309 // }
310 // buf[i*j] = accum++;
311 // }
312 //
313 // return buf[(arrsize * arrsize) - 2] + printf ("%d\n", buf[(arrsize *
314 // arrsize) - 3]);
315 // }
316 //
317 // compiled 'clang -fomit-frame-pointer -Os' for x86_64-apple-macosx
318
319 uint8_t data[] = {
320 0x55, // offset 0 -- pushq %rbp
321 0x41, 0x57, // offset 1 -- pushq %r15
322 0x41, 0x56, // offset 3 -- pushq %r14
323 0x41, 0x55, // offset 5 -- pushq %r13
324 0x41, 0x54, // offset 7 -- pushq %r12
325 0x53, // offset 9 -- pushq %rbx
326 0x48, 0x81, 0xec, 0x68, 0x38, 0x00,
327 0x00, // offset 10 -- subq $0x3868, %rsp
328
329 // ....
330
331 0x48, 0x81, 0xc4, 0x68, 0x38, 0x00,
332 0x00, // offset 17 -- addq $0x3868, %rsp
333 0x5b, // offset 24 -- popq %rbx
334 0x41, 0x5c, // offset 25 -- popq %r12
335 0x41, 0x5d, // offset 27 -- popq %r13
336 0x41, 0x5e, // offset 29 -- popq %r14
337 0x41, 0x5f, // offset 31 -- popq %r15
338 0x5d, // offset 33 -- popq %rbp
339 0xc3, // offset 34 -- retq
Jason Molendaf96c13d2016-09-29 23:57:33 +0000340 0xe8, 0x12, 0x34, 0x56, 0x78 // offset 35 -- callq whatever
Jason Molenda415f7322016-09-29 04:01:43 +0000341 };
342
343 AddressRange sample_range(0x1000, sizeof(data));
344
345 UnwindPlan unwind_plan(eRegisterKindLLDB);
346 EXPECT_TRUE(engine->GetNonCallSiteUnwindPlanFromAssembly(
347 data, sizeof(data), sample_range, unwind_plan));
348
349 // Unwind rules should look like
350 // 0: CFA=rsp +8 => rsp=CFA+0 rip=[CFA-8]
351 // 1: CFA=rsp+16 => rbp=[CFA-16] rsp=CFA+0 rip=[CFA-8]
352 // 3: CFA=rsp+24 => rbp=[CFA-16] rsp=CFA+0 r15=[CFA-24] rip=[CFA-8]
353 // 5: CFA=rsp+32 => rbp=[CFA-16] rsp=CFA+0 r14=[CFA-32] r15=[CFA-24]
354 // rip=[CFA-8
355 // 7: CFA=rsp+40 => rbp=[CFA-16] rsp=CFA+0 r13=[CFA-40] r14=[CFA-32]
356 // r15=[CFA-24] rip=[CFA-8]
357 // 9: CFA=rsp+48 => rbp=[CFA-16] rsp=CFA+0 r12=[CFA-48] r13=[CFA-40]
358 // r14=[CFA-32] r15=[CFA-24] rip=[CFA-8]
359 // 10: CFA=rsp+56 => rbx=[CFA-56] rbp=[CFA-16] rsp=CFA+0 r12=[CFA-48]
360 // r13=[CFA-40] r14=[CFA-32] r15=[CFA-24] rip=[CFA-8]
361 // 17: CFA=rsp+14496 => rbx=[CFA-56] rbp=[CFA-16] rsp=CFA+0 r12=[CFA-48]
362 // r13=[CFA-40] r14=[CFA-32] r15=[CFA-24] rip=[CFA-8]
363
364 // 24: CFA=rsp+56 => rbx=[CFA-56] rbp=[CFA-16] rsp=CFA+0 r12=[CFA-48]
365 // r13=[CFA-40] r14=[CFA-32] r15=[CFA-24] rip=[CFA-8]
366 // 25: CFA=rsp+48 => rbp=[CFA-16] rsp=CFA+0 r12=[CFA-48] r13=[CFA-40]
367 // r14=[CFA-32] r15=[CFA-24] rip=[CFA-8]
368 // 27: CFA=rsp+40 => rbp=[CFA-16] rsp=CFA+0 r13=[CFA-40] r14=[CFA-32]
369 // r15=[CFA-24] rip=[CFA-8]
370 // 29: CFA=rsp+32 => rbp=[CFA-16] rsp=CFA+0 r14=[CFA-32] r15=[CFA-24]
371 // rip=[CFA-8]
372 // 31: CFA=rsp+24 => rbp=[CFA-16] rsp=CFA+0 r15=[CFA-24] rip=[CFA-8]
373 // 33: CFA=rsp+16 => rbp=[CFA-16] rsp=CFA+0 rip=[CFA-8]
374 // 34: CFA=rsp +8 => rsp=CFA+0 rip=[CFA-8]
375
376 UnwindPlan::Row::RegisterLocation regloc;
377
378 // grab the Row for when the prologue has finished executing:
379 // 17: CFA=rsp+14496 => rbx=[CFA-56] rbp=[CFA-16] rsp=CFA+0 r12=[CFA-48]
380 // r13=[CFA-40] r14=[CFA-32] r15=[CFA-24] rip=[CFA-8]
381
382 UnwindPlan::RowSP row_sp = unwind_plan.GetRowForFunctionOffset(17);
383
Jason Molendaf96c13d2016-09-29 23:57:33 +0000384 EXPECT_EQ(17, row_sp->GetOffset());
Jason Molenda415f7322016-09-29 04:01:43 +0000385 EXPECT_TRUE(row_sp->GetCFAValue().GetRegisterNumber() == k_rsp);
386 EXPECT_TRUE(row_sp->GetCFAValue().IsRegisterPlusOffset() == true);
Jason Molendaf96c13d2016-09-29 23:57:33 +0000387 EXPECT_EQ(14496, row_sp->GetCFAValue().GetOffset());
Jason Molenda415f7322016-09-29 04:01:43 +0000388
389 EXPECT_TRUE(row_sp->GetRegisterInfo(k_rip, regloc));
390 EXPECT_TRUE(regloc.IsAtCFAPlusOffset());
Jason Molendaf96c13d2016-09-29 23:57:33 +0000391 EXPECT_EQ(-8, regloc.GetOffset());
Jason Molenda415f7322016-09-29 04:01:43 +0000392
393 EXPECT_TRUE(row_sp->GetRegisterInfo(k_rbp, regloc));
394 EXPECT_TRUE(regloc.IsAtCFAPlusOffset());
Jason Molendaf96c13d2016-09-29 23:57:33 +0000395 EXPECT_EQ(-16, regloc.GetOffset());
Jason Molenda415f7322016-09-29 04:01:43 +0000396
397 EXPECT_TRUE(row_sp->GetRegisterInfo(k_r15, regloc));
398 EXPECT_TRUE(regloc.IsAtCFAPlusOffset());
Jason Molendaf96c13d2016-09-29 23:57:33 +0000399 EXPECT_EQ(-24, regloc.GetOffset());
Jason Molenda415f7322016-09-29 04:01:43 +0000400
401 EXPECT_TRUE(row_sp->GetRegisterInfo(k_r14, regloc));
402 EXPECT_TRUE(regloc.IsAtCFAPlusOffset());
Jason Molendaf96c13d2016-09-29 23:57:33 +0000403 EXPECT_EQ(-32, regloc.GetOffset());
Jason Molenda415f7322016-09-29 04:01:43 +0000404
405 EXPECT_TRUE(row_sp->GetRegisterInfo(k_r13, regloc));
406 EXPECT_TRUE(regloc.IsAtCFAPlusOffset());
Jason Molendaf96c13d2016-09-29 23:57:33 +0000407 EXPECT_EQ(-40, regloc.GetOffset());
Jason Molenda415f7322016-09-29 04:01:43 +0000408
409 EXPECT_TRUE(row_sp->GetRegisterInfo(k_r12, regloc));
410 EXPECT_TRUE(regloc.IsAtCFAPlusOffset());
Jason Molendaf96c13d2016-09-29 23:57:33 +0000411 EXPECT_EQ(-48, regloc.GetOffset());
Jason Molenda415f7322016-09-29 04:01:43 +0000412
413 EXPECT_TRUE(row_sp->GetRegisterInfo(k_rbx, regloc));
414 EXPECT_TRUE(regloc.IsAtCFAPlusOffset());
Jason Molendaf96c13d2016-09-29 23:57:33 +0000415 EXPECT_EQ(-56, regloc.GetOffset());
Jason Molenda415f7322016-09-29 04:01:43 +0000416
Jason Molendaf96c13d2016-09-29 23:57:33 +0000417 // grab the Row for when the epilogue has finished executing:
Jason Molenda415f7322016-09-29 04:01:43 +0000418 // 34: CFA=rsp +8 => rsp=CFA+0 rip=[CFA-8]
419
420 row_sp = unwind_plan.GetRowForFunctionOffset(34);
421
Jason Molendaf96c13d2016-09-29 23:57:33 +0000422 EXPECT_EQ(34, row_sp->GetOffset());
Jason Molenda415f7322016-09-29 04:01:43 +0000423 EXPECT_TRUE(row_sp->GetCFAValue().GetRegisterNumber() == k_rsp);
424 EXPECT_TRUE(row_sp->GetCFAValue().IsRegisterPlusOffset() == true);
Jason Molendaf96c13d2016-09-29 23:57:33 +0000425 EXPECT_EQ(8, row_sp->GetCFAValue().GetOffset());
Jason Molenda415f7322016-09-29 04:01:43 +0000426
427 EXPECT_TRUE(row_sp->GetRegisterInfo(k_rip, regloc));
428 EXPECT_TRUE(regloc.IsAtCFAPlusOffset());
Jason Molendaf96c13d2016-09-29 23:57:33 +0000429 EXPECT_EQ(-8, regloc.GetOffset());
Jason Molenda415f7322016-09-29 04:01:43 +0000430
431 // these could be set to IsSame and be valid -- meaning that the
432 // register value is the same as the caller's -- but I'd rather
433 // they not be mentioned at all.
Jason Molenda7b10b1d2016-09-30 00:41:15 +0000434
435 EXPECT_FALSE(row_sp->GetRegisterInfo(k_rax, regloc));
Jason Molendaf96c13d2016-09-29 23:57:33 +0000436 EXPECT_FALSE(row_sp->GetRegisterInfo(k_rbx, regloc));
Jason Molenda7b10b1d2016-09-30 00:41:15 +0000437 EXPECT_FALSE(row_sp->GetRegisterInfo(k_rcx, regloc));
438 EXPECT_FALSE(row_sp->GetRegisterInfo(k_rdx, regloc));
439 EXPECT_FALSE(row_sp->GetRegisterInfo(k_rbp, regloc));
440 EXPECT_FALSE(row_sp->GetRegisterInfo(k_rsi, regloc));
441 EXPECT_FALSE(row_sp->GetRegisterInfo(k_rdi, regloc));
442 EXPECT_FALSE(row_sp->GetRegisterInfo(k_r8, regloc));
443 EXPECT_FALSE(row_sp->GetRegisterInfo(k_r9, regloc));
444 EXPECT_FALSE(row_sp->GetRegisterInfo(k_r10, regloc));
445 EXPECT_FALSE(row_sp->GetRegisterInfo(k_r11, regloc));
446 EXPECT_FALSE(row_sp->GetRegisterInfo(k_r12, regloc));
447 EXPECT_FALSE(row_sp->GetRegisterInfo(k_r13, regloc));
448 EXPECT_FALSE(row_sp->GetRegisterInfo(k_r14, regloc));
449 EXPECT_FALSE(row_sp->GetRegisterInfo(k_r15, regloc));
450
Jason Molenda415f7322016-09-29 04:01:43 +0000451}
Jason Molendaf96c13d2016-09-29 23:57:33 +0000452
Jason Molenda7b10b1d2016-09-30 00:41:15 +0000453
454TEST_F(Testx86AssemblyInspectionEngine, Test64bitFramelessSmallStackFrame) {
455 std::unique_ptr<x86AssemblyInspectionEngine> engine = Getx86_64Inspector();
456
457 // this source file:
458 // #include <stdio.h>
459 // int main () {
460 // puts ("HI");
461 // }
462 //
463 // compiled 'clang -fomit-frame-pointer' for x86_64-apple-macosx
464
465 uint8_t data[] = {
466 0x50,
467 // offset 0 -- pushq %rax
468
469 0x48, 0x8d, 0x3d, 0x32, 0x00, 0x00, 0x00,
470 // offset 1 -- leaq 0x32(%rip), %rdi ; "HI"
471
472 0xe8, 0x0b, 0x00, 0x00, 0x00,
473 // offset 8 -- callq 0x100000f58 ; puts
474
475 0x31, 0xc9,
476 // offset 13 -- xorl %ecx, %ecx
477
478 0x89, 0x44, 0x24, 0x04,
479 // offset 15 -- movl %eax, 0x4(%rsp)
480
481 0x89, 0xc8,
482 // offset 19 -- movl %ecx, %eax
483
484 0x59,
485 // offset 21 -- popq %rcx
486
487 0xc3
488 // offset 22 -- retq
489 };
490
491 AddressRange sample_range(0x1000, sizeof(data));
492
493 UnwindPlan unwind_plan(eRegisterKindLLDB);
494 EXPECT_TRUE(engine->GetNonCallSiteUnwindPlanFromAssembly(
495 data, sizeof(data), sample_range, unwind_plan));
496
497 // Unwind rules should look like
498 // 0: CFA=rsp +8 => rsp=CFA+0 rip=[CFA-8]
499 // 1: CFA=rsp+16 => rsp=CFA+0 rip=[CFA-8]
500 // 22: CFA=rsp +8 => rsp=CFA+0 rip=[CFA-8]
501
502 UnwindPlan::Row::RegisterLocation regloc;
503
504 // grab the Row for when the prologue has finished executing:
505 // 1: CFA=rsp+16 => rsp=CFA+0 rip=[CFA-8]
506
507 UnwindPlan::RowSP row_sp = unwind_plan.GetRowForFunctionOffset(13);
508
509 EXPECT_EQ(1, row_sp->GetOffset());
510 EXPECT_TRUE(row_sp->GetCFAValue().GetRegisterNumber() == k_rsp);
511 EXPECT_TRUE(row_sp->GetCFAValue().IsRegisterPlusOffset() == true);
512 EXPECT_EQ(16, row_sp->GetCFAValue().GetOffset());
513
514 EXPECT_TRUE(row_sp->GetRegisterInfo(k_rip, regloc));
515 EXPECT_TRUE(regloc.IsAtCFAPlusOffset());
516 EXPECT_EQ(-8, regloc.GetOffset());
517
518 // none of these were spilled
519
520 EXPECT_FALSE(row_sp->GetRegisterInfo(k_rax, regloc));
521 EXPECT_FALSE(row_sp->GetRegisterInfo(k_rbx, regloc));
522 EXPECT_FALSE(row_sp->GetRegisterInfo(k_rcx, regloc));
523 EXPECT_FALSE(row_sp->GetRegisterInfo(k_rdx, regloc));
524 EXPECT_FALSE(row_sp->GetRegisterInfo(k_rbp, regloc));
525 EXPECT_FALSE(row_sp->GetRegisterInfo(k_rsi, regloc));
526 EXPECT_FALSE(row_sp->GetRegisterInfo(k_rdi, regloc));
527 EXPECT_FALSE(row_sp->GetRegisterInfo(k_r8, regloc));
528 EXPECT_FALSE(row_sp->GetRegisterInfo(k_r9, regloc));
529 EXPECT_FALSE(row_sp->GetRegisterInfo(k_r10, regloc));
530 EXPECT_FALSE(row_sp->GetRegisterInfo(k_r11, regloc));
531 EXPECT_FALSE(row_sp->GetRegisterInfo(k_r12, regloc));
532 EXPECT_FALSE(row_sp->GetRegisterInfo(k_r13, regloc));
533 EXPECT_FALSE(row_sp->GetRegisterInfo(k_r14, regloc));
534 EXPECT_FALSE(row_sp->GetRegisterInfo(k_r15, regloc));
535
536 // grab the Row for when the epilogue has finished executing:
537 // 22: CFA=rsp +8 => rsp=CFA+0 rip=[CFA-8]
538
539 row_sp = unwind_plan.GetRowForFunctionOffset(22);
540
541 EXPECT_EQ(22, row_sp->GetOffset());
542 EXPECT_TRUE(row_sp->GetCFAValue().GetRegisterNumber() == k_rsp);
543 EXPECT_TRUE(row_sp->GetCFAValue().IsRegisterPlusOffset() == true);
544 EXPECT_EQ(8, row_sp->GetCFAValue().GetOffset());
545
546 EXPECT_TRUE(row_sp->GetRegisterInfo(k_rip, regloc));
547 EXPECT_TRUE(regloc.IsAtCFAPlusOffset());
548 EXPECT_EQ(-8, regloc.GetOffset());
549}
550
551
552TEST_F(Testx86AssemblyInspectionEngine, Test32bitFramelessSmallStackFrame) {
553 std::unique_ptr<x86AssemblyInspectionEngine> engine = Geti386Inspector();
554
555 // this source file:
556 // #include <stdio.h>
557 // int main () {
558 // puts ("HI");
559 // }
560 //
561 // compiled 'clang -arch i386 -fomit-frame-pointer' for i386-apple-macosx
562
563 uint8_t data[] = {
564 0x83, 0xec, 0x0c,
565 // offset 0 -- subl $0xc, %esp
566
567 0xe8, 0x00, 0x00, 0x00, 0x00,
568 // offset 3 -- calll 0 {call the next instruction, to put the pc on
569 // the stack}
570
571 0x58,
572 // offset 8 -- popl %eax {pop the saved pc value off stack, into eax}
573
574 0x8d, 0x80, 0x3a, 0x00, 0x00, 0x00,
575 // offset 9 -- leal 0x3a(%eax),%eax
576
577 0x89, 0x04, 0x24,
578 // offset 15 -- movl %eax, (%esp)
579
580 0xe8, 0x0d, 0x00, 0x00, 0x00,
581 // offset 18 -- calll 0x1f94 (puts)
582
583 0x31, 0xc9,
584 // offset 23 -- xorl %ecx, %ecx
585
586 0x89, 0x44, 0x24, 0x08,
587 // offset 25 -- movl %eax, 0x8(%esp)
588
589 0x89, 0xc8,
590 // offset 29 -- movl %ecx, %eax
591
592 0x83, 0xc4, 0x0c,
593 // offset 31 -- addl $0xc, %esp
594
595 0xc3
596 // offset 34 -- retl
597 };
598
599 AddressRange sample_range(0x1000, sizeof(data));
600
601 UnwindPlan unwind_plan(eRegisterKindLLDB);
602 EXPECT_TRUE(engine->GetNonCallSiteUnwindPlanFromAssembly(
603 data, sizeof(data), sample_range, unwind_plan));
604
605 // Unwind rules should look like
606 // row[0]: 0: CFA=esp +4 => esp=CFA+0 eip=[CFA-4]
607 // row[1]: 3: CFA=esp+16 => esp=CFA+0 eip=[CFA-4]
608 // row[2]: 8: CFA=esp+20 => esp=CFA+0 eip=[CFA-4]
609 // row[3]: 9: CFA=esp+16 => esp=CFA+0 eip=[CFA-4]
610 // row[4]: 34: CFA=esp +4 => esp=CFA+0 eip=[CFA-4]
611
612 UnwindPlan::Row::RegisterLocation regloc;
613
614 // Check unwind state before we set up the picbase register
615 // 3: CFA=esp+16 => esp=CFA+0 eip=[CFA-4]
616
617 UnwindPlan::RowSP row_sp = unwind_plan.GetRowForFunctionOffset(3);
618
619 EXPECT_EQ(3, row_sp->GetOffset());
620 EXPECT_TRUE(row_sp->GetCFAValue().GetRegisterNumber() == k_rsp);
621 EXPECT_TRUE(row_sp->GetCFAValue().IsRegisterPlusOffset() == true);
622 EXPECT_EQ(16, row_sp->GetCFAValue().GetOffset());
623
624 // Check unwind state after we call the next instruction
625 // 8: CFA=esp+20 => esp=CFA+0 eip=[CFA-4]
626
627 row_sp = unwind_plan.GetRowForFunctionOffset(8);
628 EXPECT_EQ(8, row_sp->GetOffset());
629 EXPECT_TRUE(row_sp->GetCFAValue().GetRegisterNumber() == k_rsp);
630 EXPECT_TRUE(row_sp->GetCFAValue().IsRegisterPlusOffset() == true);
631 EXPECT_EQ(20, row_sp->GetCFAValue().GetOffset());
632
633 // Check unwind state after we pop the pic base value off the stack
634 // row[3]: 9: CFA=esp+16 => esp=CFA+0 eip=[CFA-4]
635
636 row_sp = unwind_plan.GetRowForFunctionOffset(9);
637 EXPECT_EQ(9, row_sp->GetOffset());
638 EXPECT_TRUE(row_sp->GetCFAValue().GetRegisterNumber() == k_rsp);
639 EXPECT_TRUE(row_sp->GetCFAValue().IsRegisterPlusOffset() == true);
640 EXPECT_EQ(16, row_sp->GetCFAValue().GetOffset());
641
642 // Check that no unexpected registers were saved
643
644 EXPECT_FALSE(row_sp->GetRegisterInfo(k_eax, regloc));
645 EXPECT_FALSE(row_sp->GetRegisterInfo(k_ebx, regloc));
646 EXPECT_FALSE(row_sp->GetRegisterInfo(k_ecx, regloc));
647 EXPECT_FALSE(row_sp->GetRegisterInfo(k_edx, regloc));
648 EXPECT_FALSE(row_sp->GetRegisterInfo(k_esi, regloc));
649 EXPECT_FALSE(row_sp->GetRegisterInfo(k_edi, regloc));
650 EXPECT_FALSE(row_sp->GetRegisterInfo(k_ebp, regloc));
651
652 // verify that we get back to the original unwind state before the ret
653 // 34: CFA=esp +4 => esp=CFA+0 eip=[CFA-4]
654
655 row_sp = unwind_plan.GetRowForFunctionOffset(34);
656 EXPECT_EQ(34, row_sp->GetOffset());
657 EXPECT_TRUE(row_sp->GetCFAValue().GetRegisterNumber() == k_rsp);
658 EXPECT_TRUE(row_sp->GetCFAValue().IsRegisterPlusOffset() == true);
659 EXPECT_EQ(4, row_sp->GetCFAValue().GetOffset());
660}