blob: 80ca8370b690a4414785938670d64cca177c8818 [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);
167 EXPECT_TRUE(row_sp->GetOffset() == 0);
168 EXPECT_TRUE(row_sp->GetCFAValue().GetRegisterNumber() == k_rsp);
169 EXPECT_TRUE(row_sp->GetCFAValue().IsRegisterPlusOffset() == true);
170 EXPECT_TRUE(row_sp->GetCFAValue().GetOffset() == 8);
171
172 EXPECT_TRUE(row_sp->GetRegisterInfo(k_rip, regloc));
173 EXPECT_TRUE(regloc.IsAtCFAPlusOffset());
174 EXPECT_TRUE(regloc.GetOffset() == -8);
175
176 // 1: CFA=rsp+16 => rbp=[CFA-16] rsp=CFA+0 rip=[CFA-8]
177 row_sp = unwind_plan.GetRowForFunctionOffset(1);
178 EXPECT_TRUE(row_sp->GetOffset() == 1);
179 EXPECT_TRUE(row_sp->GetCFAValue().GetRegisterNumber() == k_rsp);
180 EXPECT_TRUE(row_sp->GetCFAValue().IsRegisterPlusOffset() == true);
181 EXPECT_TRUE(row_sp->GetCFAValue().GetOffset() == 16);
182
183 EXPECT_TRUE(row_sp->GetRegisterInfo(k_rip, regloc));
184 EXPECT_TRUE(regloc.IsAtCFAPlusOffset());
185 EXPECT_TRUE(regloc.GetOffset() == -8);
186
187 // 4: CFA=rbp+16 => rbp=[CFA-16] rsp=CFA+0 rip=[CFA-8]
188 row_sp = unwind_plan.GetRowForFunctionOffset(4);
189 EXPECT_TRUE(row_sp->GetOffset() == 4);
190 EXPECT_TRUE(row_sp->GetCFAValue().GetRegisterNumber() == k_rbp);
191 EXPECT_TRUE(row_sp->GetCFAValue().IsRegisterPlusOffset() == true);
192 EXPECT_TRUE(row_sp->GetCFAValue().GetOffset() == 16);
193
194 EXPECT_TRUE(row_sp->GetRegisterInfo(k_rip, regloc));
195 EXPECT_TRUE(regloc.IsAtCFAPlusOffset());
196 EXPECT_TRUE(regloc.GetOffset() == -8);
197
198 // 7: CFA=rsp +8 => rsp=CFA+0 rip=[CFA-8]
199 row_sp = unwind_plan.GetRowForFunctionOffset(7);
200 EXPECT_TRUE(row_sp->GetOffset() == 7);
201 EXPECT_TRUE(row_sp->GetCFAValue().GetRegisterNumber() == k_rsp);
202 EXPECT_TRUE(row_sp->GetCFAValue().IsRegisterPlusOffset() == true);
203 EXPECT_TRUE(row_sp->GetCFAValue().GetOffset() == 8);
204
205 EXPECT_TRUE(row_sp->GetRegisterInfo(k_rip, regloc));
206 EXPECT_TRUE(regloc.IsAtCFAPlusOffset());
207 EXPECT_TRUE(regloc.GetOffset() == -8);
208}
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);
243 EXPECT_TRUE(row_sp->GetOffset() == 0);
244 EXPECT_TRUE(row_sp->GetCFAValue().GetRegisterNumber() == k_esp);
245 EXPECT_TRUE(row_sp->GetCFAValue().IsRegisterPlusOffset() == true);
246 EXPECT_TRUE(row_sp->GetCFAValue().GetOffset() == 4);
247
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);
254 EXPECT_TRUE(row_sp->GetOffset() == 1);
255 EXPECT_TRUE(row_sp->GetCFAValue().GetRegisterNumber() == k_esp);
256 EXPECT_TRUE(row_sp->GetCFAValue().IsRegisterPlusOffset() == true);
257 EXPECT_TRUE(row_sp->GetCFAValue().GetOffset() == 8);
258
259 EXPECT_TRUE(row_sp->GetRegisterInfo(k_eip, regloc));
260 EXPECT_TRUE(regloc.IsAtCFAPlusOffset());
261 EXPECT_TRUE(regloc.GetOffset() == -4);
262
263 // 3: CFA=ebp +8 => ebp=[CFA-8] esp=CFA+0 eip=[CFA-4]
264 row_sp = unwind_plan.GetRowForFunctionOffset(3);
265 EXPECT_TRUE(row_sp->GetOffset() == 3);
266 EXPECT_TRUE(row_sp->GetCFAValue().GetRegisterNumber() == k_ebp);
267 EXPECT_TRUE(row_sp->GetCFAValue().IsRegisterPlusOffset() == true);
268 EXPECT_TRUE(row_sp->GetCFAValue().GetOffset() == 8);
269
270 EXPECT_TRUE(row_sp->GetRegisterInfo(k_eip, regloc));
271 EXPECT_TRUE(regloc.IsAtCFAPlusOffset());
272 EXPECT_TRUE(regloc.GetOffset() == -4);
273
274 // 6: CFA=esp +4 => esp=CFA+0 eip=[CFA-4]
275 row_sp = unwind_plan.GetRowForFunctionOffset(6);
276 EXPECT_TRUE(row_sp->GetOffset() == 6);
277 EXPECT_TRUE(row_sp->GetCFAValue().GetRegisterNumber() == k_esp);
278 EXPECT_TRUE(row_sp->GetCFAValue().IsRegisterPlusOffset() == true);
279 EXPECT_TRUE(row_sp->GetCFAValue().GetOffset() == 4);
280
281 EXPECT_TRUE(row_sp->GetRegisterInfo(k_eip, regloc));
282 EXPECT_TRUE(regloc.IsAtCFAPlusOffset());
283 EXPECT_TRUE(regloc.GetOffset() == -4);
284}
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
340 0xe8, 0x00, 0x00, 0x00, 0x00 // offset 35 -- callq whatever
341 };
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
384 EXPECT_TRUE(row_sp->GetOffset() == 17);
385 EXPECT_TRUE(row_sp->GetCFAValue().GetRegisterNumber() == k_rsp);
386 EXPECT_TRUE(row_sp->GetCFAValue().IsRegisterPlusOffset() == true);
387 EXPECT_TRUE(row_sp->GetCFAValue().GetOffset() == 14496);
388
389 EXPECT_TRUE(row_sp->GetRegisterInfo(k_rip, regloc));
390 EXPECT_TRUE(regloc.IsAtCFAPlusOffset());
391 EXPECT_TRUE(regloc.GetOffset() == -8);
392
393 EXPECT_TRUE(row_sp->GetRegisterInfo(k_rbp, regloc));
394 EXPECT_TRUE(regloc.IsAtCFAPlusOffset());
395 EXPECT_TRUE(regloc.GetOffset() == -16);
396
397 EXPECT_TRUE(row_sp->GetRegisterInfo(k_r15, regloc));
398 EXPECT_TRUE(regloc.IsAtCFAPlusOffset());
399 EXPECT_TRUE(regloc.GetOffset() == -24);
400
401 EXPECT_TRUE(row_sp->GetRegisterInfo(k_r14, regloc));
402 EXPECT_TRUE(regloc.IsAtCFAPlusOffset());
403 EXPECT_TRUE(regloc.GetOffset() == -32);
404
405 EXPECT_TRUE(row_sp->GetRegisterInfo(k_r13, regloc));
406 EXPECT_TRUE(regloc.IsAtCFAPlusOffset());
407 EXPECT_TRUE(regloc.GetOffset() == -40);
408
409 EXPECT_TRUE(row_sp->GetRegisterInfo(k_r12, regloc));
410 EXPECT_TRUE(regloc.IsAtCFAPlusOffset());
411 EXPECT_TRUE(regloc.GetOffset() == -48);
412
413 EXPECT_TRUE(row_sp->GetRegisterInfo(k_rbx, regloc));
414 EXPECT_TRUE(regloc.IsAtCFAPlusOffset());
415 EXPECT_TRUE(regloc.GetOffset() == -56);
416
417 // grab the Row for when the epiloge has finished executing:
418 // 34: CFA=rsp +8 => rsp=CFA+0 rip=[CFA-8]
419
420 row_sp = unwind_plan.GetRowForFunctionOffset(34);
421
422 EXPECT_TRUE(row_sp->GetOffset() == 34);
423 EXPECT_TRUE(row_sp->GetCFAValue().GetRegisterNumber() == k_rsp);
424 EXPECT_TRUE(row_sp->GetCFAValue().IsRegisterPlusOffset() == true);
425 EXPECT_TRUE(row_sp->GetCFAValue().GetOffset() == 8);
426
427 EXPECT_TRUE(row_sp->GetRegisterInfo(k_rip, regloc));
428 EXPECT_TRUE(regloc.IsAtCFAPlusOffset());
429 EXPECT_TRUE(regloc.GetOffset() == -8);
430
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.
434 EXPECT_TRUE(row_sp->GetRegisterInfo(k_rbp, regloc) == false);
435 EXPECT_TRUE(row_sp->GetRegisterInfo(k_r15, regloc) == false);
436 EXPECT_TRUE(row_sp->GetRegisterInfo(k_r14, regloc) == false);
437 EXPECT_TRUE(row_sp->GetRegisterInfo(k_r13, regloc) == false);
438 EXPECT_TRUE(row_sp->GetRegisterInfo(k_r12, regloc) == false);
439 EXPECT_TRUE(row_sp->GetRegisterInfo(k_rbx, regloc) == false);
440}