PageRenderTime 71ms CodeModel.GetById 2ms app.highlight 62ms RepoModel.GetById 1ms app.codeStats 0ms

/thirdparty/breakpad/processor/stackwalker_arm_unittest.cc

http://github.com/tomahawk-player/tomahawk
C++ | 764 lines | 545 code | 94 blank | 125 comment | 10 complexity | c25d3f7e4b22e4b6f363d56bf066c4ce MD5 | raw file
  1// Copyright (c) 2010, Google Inc.
  2// All rights reserved.
  3//
  4// Redistribution and use in source and binary forms, with or without
  5// modification, are permitted provided that the following conditions are
  6// met:
  7//
  8//     * Redistributions of source code must retain the above copyright
  9// notice, this list of conditions and the following disclaimer.
 10//     * Redistributions in binary form must reproduce the above
 11// copyright notice, this list of conditions and the following disclaimer
 12// in the documentation and/or other materials provided with the
 13// distribution.
 14//     * Neither the name of Google Inc. nor the names of its
 15// contributors may be used to endorse or promote products derived from
 16// this software without specific prior written permission.
 17//
 18// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 19// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 20// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 21// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 22// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 23// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 24// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 25// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 26// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 27// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 28// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 29
 30// Original author: Jim Blandy <jimb@mozilla.com> <jimb@red-bean.com>
 31
 32// stackwalker_arm_unittest.cc: Unit tests for StackwalkerARM class.
 33
 34#include <string>
 35#include <string.h>
 36#include <vector>
 37
 38#include "breakpad_googletest_includes.h"
 39#include "common/test_assembler.h"
 40#include "google_breakpad/common/minidump_format.h"
 41#include "google_breakpad/processor/basic_source_line_resolver.h"
 42#include "google_breakpad/processor/call_stack.h"
 43#include "google_breakpad/processor/source_line_resolver_interface.h"
 44#include "google_breakpad/processor/stack_frame_cpu.h"
 45#include "processor/stackwalker_unittest_utils.h"
 46#include "processor/stackwalker_arm.h"
 47#include "processor/windows_frame_info.h"
 48
 49using google_breakpad::BasicSourceLineResolver;
 50using google_breakpad::CallStack;
 51using google_breakpad::StackFrame;
 52using google_breakpad::StackFrameARM;
 53using google_breakpad::StackwalkerARM;
 54using google_breakpad::SystemInfo;
 55using google_breakpad::WindowsFrameInfo;
 56using google_breakpad::test_assembler::kLittleEndian;
 57using google_breakpad::test_assembler::Label;
 58using google_breakpad::test_assembler::Section;
 59using std::string;
 60using std::vector;
 61using testing::_;
 62using testing::Return;
 63using testing::SetArgumentPointee;
 64using testing::Test;
 65
 66class StackwalkerARMFixture {
 67 public:
 68  StackwalkerARMFixture()
 69    : stack_section(kLittleEndian),
 70      // Give the two modules reasonable standard locations and names
 71      // for tests to play with.
 72      module1(0x40000000, 0x10000, "module1", "version1"),
 73      module2(0x50000000, 0x10000, "module2", "version2") {
 74    // Identify the system as a Linux system.
 75    system_info.os = "Linux";
 76    system_info.os_short = "linux";
 77    system_info.os_version = "Lugubrious Labrador";
 78    system_info.cpu = "arm";
 79    system_info.cpu_info = "";
 80
 81    // Put distinctive values in the raw CPU context.
 82    BrandContext(&raw_context);
 83
 84    // Create some modules with some stock debugging information.
 85    modules.Add(&module1);
 86    modules.Add(&module2);
 87
 88    // By default, none of the modules have symbol info; call
 89    // SetModuleSymbols to override this.
 90    EXPECT_CALL(supplier, GetCStringSymbolData(_, _, _, _))
 91      .WillRepeatedly(Return(MockSymbolSupplier::NOT_FOUND));
 92  }
 93
 94  // Set the Breakpad symbol information that supplier should return for
 95  // MODULE to INFO.
 96  void SetModuleSymbols(MockCodeModule *module, const string &info) {
 97    unsigned int buffer_size = info.size() + 1;
 98    char *buffer = reinterpret_cast<char*>(operator new(buffer_size));
 99    strcpy(buffer, info.c_str());
100    EXPECT_CALL(supplier, GetCStringSymbolData(module, &system_info, _, _))
101      .WillRepeatedly(DoAll(SetArgumentPointee<3>(buffer),
102                            Return(MockSymbolSupplier::FOUND)));
103  }
104
105  // Populate stack_region with the contents of stack_section. Use
106  // stack_section.start() as the region's starting address.
107  void RegionFromSection() {
108    string contents;
109    ASSERT_TRUE(stack_section.GetContents(&contents));
110    stack_region.Init(stack_section.start().Value(), contents);
111  }
112
113  // Fill RAW_CONTEXT with pseudo-random data, for round-trip checking.
114  void BrandContext(MDRawContextARM *raw_context) {
115    u_int8_t x = 173;
116    for (size_t i = 0; i < sizeof(*raw_context); i++)
117      reinterpret_cast<u_int8_t *>(raw_context)[i] = (x += 17);
118  }
119
120  SystemInfo system_info;
121  MDRawContextARM raw_context;
122  Section stack_section;
123  MockMemoryRegion stack_region;
124  MockCodeModule module1;
125  MockCodeModule module2;
126  MockCodeModules modules;
127  MockSymbolSupplier supplier;
128  BasicSourceLineResolver resolver;
129  CallStack call_stack;
130  const vector<StackFrame *> *frames;
131};
132
133class SanityCheck: public StackwalkerARMFixture, public Test { };
134
135TEST_F(SanityCheck, NoResolver) {
136  // Since we have no call frame information, and all unwinding
137  // requires call frame information, the stack walk will end after
138  // the first frame.
139  StackwalkerARM walker(&system_info, &raw_context, -1, &stack_region, &modules,
140                        NULL, NULL);
141  // This should succeed even without a resolver or supplier.
142  ASSERT_TRUE(walker.Walk(&call_stack));
143  frames = call_stack.frames();
144  ASSERT_EQ(1U, frames->size());
145  StackFrameARM *frame = static_cast<StackFrameARM *>(frames->at(0));
146  // Check that the values from the original raw context made it
147  // through to the context in the stack frame.
148  EXPECT_EQ(0, memcmp(&raw_context, &frame->context, sizeof(raw_context)));
149}
150
151class GetContextFrame: public StackwalkerARMFixture, public Test { };
152
153TEST_F(GetContextFrame, Simple) {
154  // Since we have no call frame information, and all unwinding
155  // requires call frame information, the stack walk will end after
156  // the first frame.
157  StackwalkerARM walker(&system_info, &raw_context, -1, &stack_region, &modules,
158                        &supplier, &resolver);
159  ASSERT_TRUE(walker.Walk(&call_stack));
160  frames = call_stack.frames();
161  ASSERT_EQ(1U, frames->size());
162  StackFrameARM *frame = static_cast<StackFrameARM *>(frames->at(0));
163  // Check that the values from the original raw context made it
164  // through to the context in the stack frame.
165  EXPECT_EQ(0, memcmp(&raw_context, &frame->context, sizeof(raw_context)));
166}
167
168class GetCallerFrame: public StackwalkerARMFixture, public Test { };
169
170TEST_F(GetCallerFrame, ScanWithoutSymbols) {
171  // When the stack walker resorts to scanning the stack,
172  // only addresses located within loaded modules are
173  // considered valid return addresses.
174  // Force scanning through three frames to ensure that the
175  // stack pointer is set properly in scan-recovered frames.
176  stack_section.start() = 0x80000000;
177  u_int32_t return_address1 = 0x50000100;
178  u_int32_t return_address2 = 0x50000900;
179  Label frame1_sp, frame2_sp;
180  stack_section
181    // frame 0
182    .Append(16, 0)                      // space
183
184    .D32(0x40090000)                    // junk that's not
185    .D32(0x60000000)                    // a return address
186
187    .D32(return_address1)               // actual return address
188    // frame 1
189    .Mark(&frame1_sp)
190    .Append(16, 0)                      // space
191
192    .D32(0xF0000000)                    // more junk
193    .D32(0x0000000D)
194
195    .D32(return_address2)               // actual return address
196    // frame 2
197    .Mark(&frame2_sp)
198    .Append(32, 0);                     // end of stack
199  RegionFromSection();
200
201  raw_context.iregs[MD_CONTEXT_ARM_REG_PC] = 0x40005510;
202  raw_context.iregs[MD_CONTEXT_ARM_REG_SP] = stack_section.start().Value();
203
204  StackwalkerARM walker(&system_info, &raw_context, -1, &stack_region, &modules,
205                        &supplier, &resolver);
206  ASSERT_TRUE(walker.Walk(&call_stack));
207  frames = call_stack.frames();
208  ASSERT_EQ(3U, frames->size());
209
210  StackFrameARM *frame0 = static_cast<StackFrameARM *>(frames->at(0));
211  EXPECT_EQ(StackFrame::FRAME_TRUST_CONTEXT, frame0->trust);
212  ASSERT_EQ(StackFrameARM::CONTEXT_VALID_ALL, frame0->context_validity);
213  EXPECT_EQ(0, memcmp(&raw_context, &frame0->context, sizeof(raw_context)));
214
215  StackFrameARM *frame1 = static_cast<StackFrameARM *>(frames->at(1));
216  EXPECT_EQ(StackFrame::FRAME_TRUST_SCAN, frame1->trust);
217  ASSERT_EQ((StackFrameARM::CONTEXT_VALID_PC |
218             StackFrameARM::CONTEXT_VALID_SP),
219            frame1->context_validity);
220  EXPECT_EQ(return_address1, frame1->context.iregs[MD_CONTEXT_ARM_REG_PC]);
221  EXPECT_EQ(frame1_sp.Value(), frame1->context.iregs[MD_CONTEXT_ARM_REG_SP]);
222
223  StackFrameARM *frame2 = static_cast<StackFrameARM *>(frames->at(2));
224  EXPECT_EQ(StackFrame::FRAME_TRUST_SCAN, frame2->trust);
225  ASSERT_EQ((StackFrameARM::CONTEXT_VALID_PC |
226             StackFrameARM::CONTEXT_VALID_SP),
227            frame2->context_validity);
228  EXPECT_EQ(return_address2, frame2->context.iregs[MD_CONTEXT_ARM_REG_PC]);
229  EXPECT_EQ(frame2_sp.Value(), frame2->context.iregs[MD_CONTEXT_ARM_REG_SP]);
230}
231
232TEST_F(GetCallerFrame, ScanWithFunctionSymbols) {
233  // During stack scanning, if a potential return address
234  // is located within a loaded module that has symbols,
235  // it is only considered a valid return address if it
236  // lies within a function's bounds.
237  stack_section.start() = 0x80000000;
238  u_int32_t return_address = 0x50000200;
239  Label frame1_sp;
240
241  stack_section
242    // frame 0
243    .Append(16, 0)                      // space
244
245    .D32(0x40090000)                    // junk that's not
246    .D32(0x60000000)                    // a return address
247
248    .D32(0x40001000)                    // a couple of plausible addresses
249    .D32(0x5000F000)                    // that are not within functions
250
251    .D32(return_address)                // actual return address
252    // frame 1
253    .Mark(&frame1_sp)
254    .Append(32, 0);                     // end of stack
255  RegionFromSection();
256
257  raw_context.iregs[MD_CONTEXT_ARM_REG_PC] = 0x40000200;
258  raw_context.iregs[MD_CONTEXT_ARM_REG_SP] = stack_section.start().Value();
259
260  SetModuleSymbols(&module1,
261                   // The youngest frame's function.
262                   "FUNC 100 400 10 monotreme\n");
263  SetModuleSymbols(&module2,
264                   // The calling frame's function.
265                   "FUNC 100 400 10 marsupial\n");
266
267  StackwalkerARM walker(&system_info, &raw_context, -1, &stack_region, &modules,
268                        &supplier, &resolver);
269  ASSERT_TRUE(walker.Walk(&call_stack));
270  frames = call_stack.frames();
271  ASSERT_EQ(2U, frames->size());
272
273  StackFrameARM *frame0 = static_cast<StackFrameARM *>(frames->at(0));
274  EXPECT_EQ(StackFrame::FRAME_TRUST_CONTEXT, frame0->trust);
275  ASSERT_EQ(StackFrameARM::CONTEXT_VALID_ALL, frame0->context_validity);
276  EXPECT_EQ(0, memcmp(&raw_context, &frame0->context, sizeof(raw_context)));
277  EXPECT_EQ("monotreme", frame0->function_name);
278  EXPECT_EQ(0x40000100, frame0->function_base);
279
280  StackFrameARM *frame1 = static_cast<StackFrameARM *>(frames->at(1));
281  EXPECT_EQ(StackFrame::FRAME_TRUST_SCAN, frame1->trust);
282  ASSERT_EQ((StackFrameARM::CONTEXT_VALID_PC |
283             StackFrameARM::CONTEXT_VALID_SP),
284            frame1->context_validity);
285  EXPECT_EQ(return_address, frame1->context.iregs[MD_CONTEXT_ARM_REG_PC]);
286  EXPECT_EQ(frame1_sp.Value(), frame1->context.iregs[MD_CONTEXT_ARM_REG_SP]);
287  EXPECT_EQ("marsupial", frame1->function_name);
288  EXPECT_EQ(0x50000100, frame1->function_base);
289}
290
291struct CFIFixture: public StackwalkerARMFixture {
292  CFIFixture() {
293    // Provide a bunch of STACK CFI records; we'll walk to the caller
294    // from every point in this series, expecting to find the same set
295    // of register values.
296    SetModuleSymbols(&module1,
297                     // The youngest frame's function.
298                     "FUNC 4000 1000 10 enchiridion\n"
299                     // Initially, nothing has been pushed on the stack,
300                     // and the return address is still in the link register.
301                     "STACK CFI INIT 4000 100 .cfa: sp .ra: lr\n"
302                     // Push r4, the frame pointer, and the link register.
303                     "STACK CFI 4001 .cfa: sp 12 + r4: .cfa 12 - ^"
304                     " r11: .cfa 8 - ^ .ra: .cfa 4 - ^\n"
305                     // Save r4..r7 in r0..r3: verify that we populate
306                     // the youngest frame with all the values we have.
307                     "STACK CFI 4002 r4: r0 r5: r1 r6: r2 r7: r3\n"
308                     // Restore r4..r7. Save the non-callee-saves register r1.
309                     "STACK CFI 4003 .cfa: sp 16 + r1: .cfa 16 - ^"
310                     " r4: r4 r5: r5 r6: r6 r7: r7\n"
311                     // Move the .cfa back four bytes, to point at the return
312                     // address, and restore the sp explicitly.
313                     "STACK CFI 4005 .cfa: sp 12 + r1: .cfa 12 - ^"
314                     " r11: .cfa 4 - ^ .ra: .cfa ^ sp: .cfa 4 +\n"
315                     // Recover the PC explicitly from a new stack slot;
316                     // provide garbage for the .ra.
317                     "STACK CFI 4006 .cfa: sp 16 + pc: .cfa 16 - ^\n"
318
319                     // The calling function.
320                     "FUNC 5000 1000 10 epictetus\n"
321                     // Mark it as end of stack.
322                     "STACK CFI INIT 5000 1000 .cfa: 0 .ra: 0\n"
323
324                     // A function whose CFI makes the stack pointer
325                     // go backwards.
326                     "FUNC 6000 1000 20 palinal\n"
327                     "STACK CFI INIT 6000 1000 .cfa: sp 4 - .ra: lr\n"
328
329                     // A function with CFI expressions that can't be
330                     // evaluated.
331                     "FUNC 7000 1000 20 rhetorical\n"
332                     "STACK CFI INIT 7000 1000 .cfa: moot .ra: ambiguous\n");
333
334    // Provide some distinctive values for the caller's registers.
335    expected.iregs[MD_CONTEXT_ARM_REG_PC] = 0x40005510;
336    expected.iregs[MD_CONTEXT_ARM_REG_SP] = 0x80000000;
337    expected.iregs[4] = 0xb5d55e68;
338    expected.iregs[5] = 0xebd134f3;
339    expected.iregs[6] = 0xa31e74bc;
340    expected.iregs[7] = 0x2dcb16b3;
341    expected.iregs[8] = 0x2ada2137;
342    expected.iregs[9] = 0xbbbb557d;
343    expected.iregs[10] = 0x48bf8ca7;
344    expected.iregs[MD_CONTEXT_ARM_REG_FP] = 0x8112e110;
345
346    // Expect CFI to recover all callee-saves registers. Since CFI is the
347    // only stack frame construction technique we have, aside from the
348    // context frame itself, there's no way for us to have a set of valid
349    // registers smaller than this.
350    expected_validity = (StackFrameARM::CONTEXT_VALID_PC |
351                         StackFrameARM::CONTEXT_VALID_SP |
352                         StackFrameARM::CONTEXT_VALID_R4 |
353                         StackFrameARM::CONTEXT_VALID_R5 |
354                         StackFrameARM::CONTEXT_VALID_R6 |
355                         StackFrameARM::CONTEXT_VALID_R7 |
356                         StackFrameARM::CONTEXT_VALID_R8 |
357                         StackFrameARM::CONTEXT_VALID_R9 |
358                         StackFrameARM::CONTEXT_VALID_R10 |
359                         StackFrameARM::CONTEXT_VALID_FP);
360
361    // By default, context frames provide all registers, as normal.
362    context_frame_validity = StackFrameARM::CONTEXT_VALID_ALL;
363
364    // By default, registers are unchanged.
365    raw_context = expected;
366  }
367
368  // Walk the stack, using stack_section as the contents of the stack
369  // and raw_context as the current register values. (Set the stack
370  // pointer to the stack's starting address.) Expect two stack
371  // frames; in the older frame, expect the callee-saves registers to
372  // have values matching those in 'expected'.
373  void CheckWalk() {
374    RegionFromSection();
375    raw_context.iregs[MD_CONTEXT_ARM_REG_SP] = stack_section.start().Value();
376
377    StackwalkerARM walker(&system_info, &raw_context, -1, &stack_region,
378                          &modules, &supplier, &resolver);
379    walker.SetContextFrameValidity(context_frame_validity);
380    ASSERT_TRUE(walker.Walk(&call_stack));
381    frames = call_stack.frames();
382    ASSERT_EQ(2U, frames->size());
383
384    StackFrameARM *frame0 = static_cast<StackFrameARM *>(frames->at(0));
385    EXPECT_EQ(StackFrame::FRAME_TRUST_CONTEXT, frame0->trust);
386    ASSERT_EQ(context_frame_validity, frame0->context_validity);
387    EXPECT_EQ("enchiridion", frame0->function_name);
388    EXPECT_EQ(0x40004000U, frame0->function_base);
389
390    StackFrameARM *frame1 = static_cast<StackFrameARM *>(frames->at(1));
391    EXPECT_EQ(StackFrame::FRAME_TRUST_CFI, frame1->trust);
392    ASSERT_EQ(expected_validity, frame1->context_validity);
393    if (expected_validity & StackFrameARM::CONTEXT_VALID_R1)
394      EXPECT_EQ(expected.iregs[1], frame1->context.iregs[1]);
395    if (expected_validity & StackFrameARM::CONTEXT_VALID_R4)
396      EXPECT_EQ(expected.iregs[4], frame1->context.iregs[4]);
397    if (expected_validity & StackFrameARM::CONTEXT_VALID_R5)
398      EXPECT_EQ(expected.iregs[5], frame1->context.iregs[5]);
399    if (expected_validity & StackFrameARM::CONTEXT_VALID_R6)
400      EXPECT_EQ(expected.iregs[6], frame1->context.iregs[6]);
401    if (expected_validity & StackFrameARM::CONTEXT_VALID_R7)
402      EXPECT_EQ(expected.iregs[7], frame1->context.iregs[7]);
403    if (expected_validity & StackFrameARM::CONTEXT_VALID_R8)
404      EXPECT_EQ(expected.iregs[8], frame1->context.iregs[8]);
405    if (expected_validity & StackFrameARM::CONTEXT_VALID_R9)
406      EXPECT_EQ(expected.iregs[9], frame1->context.iregs[9]);
407    if (expected_validity & StackFrameARM::CONTEXT_VALID_R10)
408      EXPECT_EQ(expected.iregs[10], frame1->context.iregs[10]);
409    if (expected_validity & StackFrameARM::CONTEXT_VALID_FP)
410      EXPECT_EQ(expected.iregs[MD_CONTEXT_ARM_REG_FP],
411                frame1->context.iregs[MD_CONTEXT_ARM_REG_FP]);
412
413    // We would never have gotten a frame in the first place if the SP
414    // and PC weren't valid or ->instruction weren't set.
415    EXPECT_EQ(expected.iregs[MD_CONTEXT_ARM_REG_SP],
416              frame1->context.iregs[MD_CONTEXT_ARM_REG_SP]);
417    EXPECT_EQ(expected.iregs[MD_CONTEXT_ARM_REG_PC],
418              frame1->context.iregs[MD_CONTEXT_ARM_REG_PC]);
419    EXPECT_EQ(expected.iregs[MD_CONTEXT_ARM_REG_PC],
420              frame1->instruction + 2);
421    EXPECT_EQ("epictetus", frame1->function_name);
422  }
423
424  // The values we expect to find for the caller's registers.
425  MDRawContextARM expected;
426
427  // The validity mask for expected.
428  int expected_validity;
429
430  // The validity mask to impose on the context frame.
431  int context_frame_validity;
432};
433
434class CFI: public CFIFixture, public Test { };
435
436TEST_F(CFI, At4000) {
437  stack_section.start() = expected.iregs[MD_CONTEXT_ARM_REG_SP];
438  raw_context.iregs[MD_CONTEXT_ARM_REG_PC] = 0x40004000;
439  raw_context.iregs[MD_CONTEXT_ARM_REG_LR] = 0x40005510;
440  CheckWalk();
441}
442
443TEST_F(CFI, At4001) {
444  Label frame1_sp = expected.iregs[MD_CONTEXT_ARM_REG_SP];
445  stack_section
446    .D32(0xb5d55e68)            // saved r4
447    .D32(0x8112e110)            // saved fp
448    .D32(0x40005510)            // return address
449    .Mark(&frame1_sp);          // This effectively sets stack_section.start().
450  raw_context.iregs[MD_CONTEXT_ARM_REG_PC] = 0x40004001;
451  raw_context.iregs[4] = 0x635adc9f;                     // distinct callee r4
452  raw_context.iregs[MD_CONTEXT_ARM_REG_FP] = 0xbe145fc4; // distinct callee fp
453  CheckWalk();
454}
455
456// As above, but unwind from a context that has only the PC and SP.
457TEST_F(CFI, At4001LimitedValidity) {
458  context_frame_validity =
459    StackFrameARM::CONTEXT_VALID_PC | StackFrameARM::CONTEXT_VALID_SP;
460  raw_context.iregs[MD_CONTEXT_ARM_REG_PC] = 0x40004001;
461  raw_context.iregs[MD_CONTEXT_ARM_REG_FP] = 0xbe145fc4; // distinct callee fp
462  Label frame1_sp = expected.iregs[MD_CONTEXT_ARM_REG_SP];
463  stack_section
464    .D32(0xb5d55e68)            // saved r4
465    .D32(0x8112e110)            // saved fp
466    .D32(0x40005510)            // return address
467    .Mark(&frame1_sp);          // This effectively sets stack_section.start().
468  expected_validity = (StackFrameARM::CONTEXT_VALID_PC
469                       | StackFrameARM::CONTEXT_VALID_SP
470                       | StackFrameARM::CONTEXT_VALID_FP
471                       | StackFrameARM::CONTEXT_VALID_R4);
472  CheckWalk();
473}
474
475TEST_F(CFI, At4002) {
476  Label frame1_sp = expected.iregs[MD_CONTEXT_ARM_REG_SP];
477  stack_section
478    .D32(0xfb81ff3d)            // no longer saved r4
479    .D32(0x8112e110)            // saved fp
480    .D32(0x40005510)            // return address
481    .Mark(&frame1_sp);          // This effectively sets stack_section.start().
482  raw_context.iregs[MD_CONTEXT_ARM_REG_PC] = 0x40004002;
483  raw_context.iregs[0] = 0xb5d55e68;  // saved r4
484  raw_context.iregs[1] = 0xebd134f3;  // saved r5
485  raw_context.iregs[2] = 0xa31e74bc;  // saved r6
486  raw_context.iregs[3] = 0x2dcb16b3;  // saved r7
487  raw_context.iregs[4] = 0xfdd35466;  // distinct callee r4
488  raw_context.iregs[5] = 0xf18c946c;  // distinct callee r5
489  raw_context.iregs[6] = 0xac2079e8;  // distinct callee r6
490  raw_context.iregs[7] = 0xa449829f;  // distinct callee r7
491  raw_context.iregs[MD_CONTEXT_ARM_REG_FP] = 0xbe145fc4; // distinct callee fp
492  CheckWalk();
493}
494
495TEST_F(CFI, At4003) {
496  Label frame1_sp = expected.iregs[MD_CONTEXT_ARM_REG_SP];
497  stack_section
498    .D32(0x48c8dd5a)            // saved r1 (even though it's not callee-saves)
499    .D32(0xcb78040e)            // no longer saved r4
500    .D32(0x8112e110)            // saved fp
501    .D32(0x40005510)            // return address
502    .Mark(&frame1_sp);          // This effectively sets stack_section.start().
503  raw_context.iregs[MD_CONTEXT_ARM_REG_PC] = 0x40004003;
504  raw_context.iregs[1] = 0xfb756319;                     // distinct callee r1
505  raw_context.iregs[MD_CONTEXT_ARM_REG_FP] = 0x0a2857ea; // distinct callee fp
506  expected.iregs[1] = 0x48c8dd5a;    // caller's r1
507  expected_validity |= StackFrameARM::CONTEXT_VALID_R1;
508  CheckWalk();
509}
510
511// We have no new rule at module offset 0x4004, so the results here should
512// be the same as those at module offset 0x4003.
513TEST_F(CFI, At4004) {
514  Label frame1_sp = expected.iregs[MD_CONTEXT_ARM_REG_SP];
515  stack_section
516    .D32(0x48c8dd5a)            // saved r1 (even though it's not callee-saves)
517    .D32(0xcb78040e)            // no longer saved r4
518    .D32(0x8112e110)            // saved fp
519    .D32(0x40005510)            // return address
520    .Mark(&frame1_sp);          // This effectively sets stack_section.start().
521  raw_context.iregs[MD_CONTEXT_ARM_REG_PC] = 0x40004004;
522  raw_context.iregs[1] = 0xfb756319; // distinct callee r1
523  expected.iregs[1] = 0x48c8dd5a; // caller's r1
524  expected_validity |= StackFrameARM::CONTEXT_VALID_R1;
525  CheckWalk();
526}
527
528// Here we move the .cfa, but provide an explicit rule to recover the SP,
529// so again there should be no change in the registers recovered.
530TEST_F(CFI, At4005) {
531  Label frame1_sp = expected.iregs[MD_CONTEXT_ARM_REG_SP];
532  stack_section
533    .D32(0x48c8dd5a)            // saved r1 (even though it's not callee-saves)
534    .D32(0xf013f841)            // no longer saved r4
535    .D32(0x8112e110)            // saved fp
536    .D32(0x40005510)            // return address
537    .Mark(&frame1_sp);          // This effectively sets stack_section.start().
538  raw_context.iregs[MD_CONTEXT_ARM_REG_PC] = 0x40004005;
539  raw_context.iregs[1] = 0xfb756319; // distinct callee r1
540  expected.iregs[1] = 0x48c8dd5a; // caller's r1
541  expected_validity |= StackFrameARM::CONTEXT_VALID_R1;
542  CheckWalk();
543}
544
545// Here we provide an explicit rule for the PC, and have the saved .ra be
546// bogus.
547TEST_F(CFI, At4006) {
548  Label frame1_sp = expected.iregs[MD_CONTEXT_ARM_REG_SP];
549  stack_section
550    .D32(0x40005510)            // saved pc
551    .D32(0x48c8dd5a)            // saved r1 (even though it's not callee-saves)
552    .D32(0xf013f841)            // no longer saved r4
553    .D32(0x8112e110)            // saved fp
554    .D32(0xf8d15783)            // .ra rule recovers this, which is garbage
555    .Mark(&frame1_sp);          // This effectively sets stack_section.start().
556  raw_context.iregs[MD_CONTEXT_ARM_REG_PC] = 0x40004006;
557  raw_context.iregs[1] = 0xfb756319; // callee's r1, different from caller's
558  expected.iregs[1] = 0x48c8dd5a; // caller's r1
559  expected_validity |= StackFrameARM::CONTEXT_VALID_R1;
560  CheckWalk();
561}
562
563// Check that we reject rules that would cause the stack pointer to
564// move in the wrong direction.
565TEST_F(CFI, RejectBackwards) {
566  raw_context.iregs[MD_CONTEXT_ARM_REG_PC] = 0x40006000;
567  raw_context.iregs[MD_CONTEXT_ARM_REG_SP] = 0x80000000;
568  raw_context.iregs[MD_CONTEXT_ARM_REG_LR] = 0x40005510;
569  StackwalkerARM walker(&system_info, &raw_context, -1, &stack_region, &modules,
570                        &supplier, &resolver);
571  ASSERT_TRUE(walker.Walk(&call_stack));
572  frames = call_stack.frames();
573  ASSERT_EQ(1U, frames->size());
574}
575
576// Check that we reject rules whose expressions' evaluation fails.
577TEST_F(CFI, RejectBadExpressions) {
578  raw_context.iregs[MD_CONTEXT_ARM_REG_PC] = 0x40007000;
579  raw_context.iregs[MD_CONTEXT_ARM_REG_SP] = 0x80000000;
580  StackwalkerARM walker(&system_info, &raw_context, -1, &stack_region, &modules,
581                        &supplier, &resolver);
582  ASSERT_TRUE(walker.Walk(&call_stack));
583  frames = call_stack.frames();
584  ASSERT_EQ(1U, frames->size());
585}
586
587class StackwalkerARMFixtureIOS : public StackwalkerARMFixture {
588 public:
589  StackwalkerARMFixtureIOS() {
590    system_info.os = "iOS";
591    system_info.os_short = "ios";
592  }
593};
594
595class GetFramesByFramePointer: public StackwalkerARMFixtureIOS, public Test { };
596
597TEST_F(GetFramesByFramePointer, OnlyFramePointer) {
598  stack_section.start() = 0x80000000;
599  u_int32_t return_address1 = 0x50000100;
600  u_int32_t return_address2 = 0x50000900;
601  Label frame1_sp, frame2_sp;
602  Label frame1_fp, frame2_fp;
603  stack_section
604    // frame 0
605    .Append(32, 0)           // Whatever values on the stack.
606    .D32(0x0000000D)         // junk that's not
607    .D32(0xF0000000)         // a return address.
608
609    .Mark(&frame1_fp)        // Next fp will point to the next value.
610    .D32(frame2_fp)          // Save current frame pointer.
611    .D32(return_address2)    // Save current link register.
612    .Mark(&frame1_sp)
613
614    // frame 1
615    .Append(32, 0)           // Whatever values on the stack.
616    .D32(0x0000000D)         // junk that's not
617    .D32(0xF0000000)         // a return address.
618
619    .Mark(&frame2_fp)
620    .D32(0)
621    .D32(0)
622    .Mark(&frame2_sp)
623
624    // frame 2
625    .Append(32, 0)           // Whatever values on the stack.
626    .D32(0x0000000D)         // junk that's not
627    .D32(0xF0000000);        // a return address.
628  RegionFromSection();
629
630
631  raw_context.iregs[MD_CONTEXT_ARM_REG_PC] = 0x40005510;
632  raw_context.iregs[MD_CONTEXT_ARM_REG_LR] = return_address1;
633  raw_context.iregs[MD_CONTEXT_ARM_REG_IOS_FP] = frame1_fp.Value();
634  raw_context.iregs[MD_CONTEXT_ARM_REG_SP] = stack_section.start().Value();
635
636  StackwalkerARM walker(&system_info, &raw_context, MD_CONTEXT_ARM_REG_IOS_FP,
637                        &stack_region, &modules, &supplier, &resolver);
638
639  ASSERT_TRUE(walker.Walk(&call_stack));
640  frames = call_stack.frames();
641  ASSERT_EQ(3U, frames->size());
642
643  StackFrameARM *frame0 = static_cast<StackFrameARM *>(frames->at(0));
644  EXPECT_EQ(StackFrame::FRAME_TRUST_CONTEXT, frame0->trust);
645  ASSERT_EQ(StackFrameARM::CONTEXT_VALID_ALL, frame0->context_validity);
646  EXPECT_EQ(0, memcmp(&raw_context, &frame0->context, sizeof(raw_context)));
647
648  StackFrameARM *frame1 = static_cast<StackFrameARM *>(frames->at(1));
649  EXPECT_EQ(StackFrame::FRAME_TRUST_FP, frame1->trust);
650  ASSERT_EQ((StackFrameARM::CONTEXT_VALID_PC |
651             StackFrameARM::CONTEXT_VALID_LR |
652             StackFrameARM::RegisterValidFlag(MD_CONTEXT_ARM_REG_IOS_FP) |
653             StackFrameARM::CONTEXT_VALID_SP),
654            frame1->context_validity);
655  EXPECT_EQ(return_address1, frame1->context.iregs[MD_CONTEXT_ARM_REG_PC]);
656  EXPECT_EQ(return_address2, frame1->context.iregs[MD_CONTEXT_ARM_REG_LR]);
657  EXPECT_EQ(frame1_sp.Value(), frame1->context.iregs[MD_CONTEXT_ARM_REG_SP]);
658  EXPECT_EQ(frame2_fp.Value(),
659            frame1->context.iregs[MD_CONTEXT_ARM_REG_IOS_FP]);
660
661  StackFrameARM *frame2 = static_cast<StackFrameARM *>(frames->at(2));
662  EXPECT_EQ(StackFrame::FRAME_TRUST_FP, frame2->trust);
663  ASSERT_EQ((StackFrameARM::CONTEXT_VALID_PC |
664             StackFrameARM::CONTEXT_VALID_LR |
665             StackFrameARM::RegisterValidFlag(MD_CONTEXT_ARM_REG_IOS_FP) |
666             StackFrameARM::CONTEXT_VALID_SP),
667            frame2->context_validity);
668  EXPECT_EQ(return_address2, frame2->context.iregs[MD_CONTEXT_ARM_REG_PC]);
669  EXPECT_EQ(0, frame2->context.iregs[MD_CONTEXT_ARM_REG_LR]);
670  EXPECT_EQ(frame2_sp.Value(), frame2->context.iregs[MD_CONTEXT_ARM_REG_SP]);
671  EXPECT_EQ(0, frame2->context.iregs[MD_CONTEXT_ARM_REG_IOS_FP]);
672}
673
674TEST_F(GetFramesByFramePointer, FramePointerAndCFI) {
675  // Provide the standatd STACK CFI records that is obtained when exmining an
676  // executable produced by XCode.
677  SetModuleSymbols(&module1,
678                     // Adding a function in CFI.
679                     "FUNC 4000 1000 10 enchiridion\n"
680
681                     "STACK CFI INIT 4000 100 .cfa: sp 0 + .ra: lr\n"
682                     "STACK CFI 4001 .cfa: sp 8 + .ra: .cfa -4 + ^"
683                     " r7: .cfa -8 + ^\n"
684                     "STACK CFI 4002 .cfa: r7 8 +\n"
685                  );
686
687  stack_section.start() = 0x80000000;
688  u_int32_t return_address1 = 0x40004010;
689  u_int32_t return_address2 = 0x50000900;
690  Label frame1_sp, frame2_sp;
691  Label frame1_fp, frame2_fp;
692  stack_section
693    // frame 0
694    .Append(32, 0)           // Whatever values on the stack.
695    .D32(0x0000000D)         // junk that's not
696    .D32(0xF0000000)         // a return address.
697
698    .Mark(&frame1_fp)        // Next fp will point to the next value.
699    .D32(frame2_fp)          // Save current frame pointer.
700    .D32(return_address2)    // Save current link register.
701    .Mark(&frame1_sp)
702
703    // frame 1
704    .Append(32, 0)           // Whatever values on the stack.
705    .D32(0x0000000D)         // junk that's not
706    .D32(0xF0000000)         // a return address.
707
708    .Mark(&frame2_fp)
709    .D32(0)
710    .D32(0)
711    .Mark(&frame2_sp)
712
713    // frame 2
714    .Append(32, 0)           // Whatever values on the stack.
715    .D32(0x0000000D)         // junk that's not
716    .D32(0xF0000000);        // a return address.
717  RegionFromSection();
718
719
720  raw_context.iregs[MD_CONTEXT_ARM_REG_PC] = 0x50000400;
721  raw_context.iregs[MD_CONTEXT_ARM_REG_LR] = return_address1;
722  raw_context.iregs[MD_CONTEXT_ARM_REG_IOS_FP] = frame1_fp.Value();
723  raw_context.iregs[MD_CONTEXT_ARM_REG_SP] = stack_section.start().Value();
724
725  StackwalkerARM walker(&system_info, &raw_context, MD_CONTEXT_ARM_REG_IOS_FP,
726                        &stack_region, &modules, &supplier, &resolver);
727
728  ASSERT_TRUE(walker.Walk(&call_stack));
729  frames = call_stack.frames();
730  ASSERT_EQ(3U, frames->size());
731
732  StackFrameARM *frame0 = static_cast<StackFrameARM *>(frames->at(0));
733  EXPECT_EQ(StackFrame::FRAME_TRUST_CONTEXT, frame0->trust);
734  ASSERT_EQ(StackFrameARM::CONTEXT_VALID_ALL, frame0->context_validity);
735  EXPECT_EQ(0, memcmp(&raw_context, &frame0->context, sizeof(raw_context)));
736
737  StackFrameARM *frame1 = static_cast<StackFrameARM *>(frames->at(1));
738  EXPECT_EQ(StackFrame::FRAME_TRUST_FP, frame1->trust);
739  ASSERT_EQ((StackFrameARM::CONTEXT_VALID_PC |
740             StackFrameARM::CONTEXT_VALID_LR |
741             StackFrameARM::RegisterValidFlag(MD_CONTEXT_ARM_REG_IOS_FP) |
742             StackFrameARM::CONTEXT_VALID_SP),
743            frame1->context_validity);
744  EXPECT_EQ(return_address1, frame1->context.iregs[MD_CONTEXT_ARM_REG_PC]);
745  EXPECT_EQ(return_address2, frame1->context.iregs[MD_CONTEXT_ARM_REG_LR]);
746  EXPECT_EQ(frame1_sp.Value(), frame1->context.iregs[MD_CONTEXT_ARM_REG_SP]);
747  EXPECT_EQ(frame2_fp.Value(),
748            frame1->context.iregs[MD_CONTEXT_ARM_REG_IOS_FP]);
749  EXPECT_EQ("enchiridion", frame1->function_name);
750  EXPECT_EQ(0x40004000U, frame1->function_base);
751
752
753  StackFrameARM *frame2 = static_cast<StackFrameARM *>(frames->at(2));
754  EXPECT_EQ(StackFrame::FRAME_TRUST_CFI, frame2->trust);
755  ASSERT_EQ((StackFrameARM::CONTEXT_VALID_PC |
756             StackFrameARM::CONTEXT_VALID_LR |
757             StackFrameARM::RegisterValidFlag(MD_CONTEXT_ARM_REG_IOS_FP) |
758             StackFrameARM::CONTEXT_VALID_SP),
759            frame2->context_validity);
760  EXPECT_EQ(return_address2, frame2->context.iregs[MD_CONTEXT_ARM_REG_PC]);
761  EXPECT_EQ(0, frame2->context.iregs[MD_CONTEXT_ARM_REG_LR]);
762  EXPECT_EQ(frame2_sp.Value(), frame2->context.iregs[MD_CONTEXT_ARM_REG_SP]);
763  EXPECT_EQ(0, frame2->context.iregs[MD_CONTEXT_ARM_REG_IOS_FP]);
764}