PageRenderTime 118ms CodeModel.GetById 47ms app.highlight 66ms RepoModel.GetById 1ms app.codeStats 0ms

/thirdparty/breakpad/processor/fast_source_line_resolver_unittest.cc

http://github.com/tomahawk-player/tomahawk
C++ | 480 lines | 371 code | 51 blank | 58 comment | 14 complexity | 81ee460d834924e4858a09d5ec4e40bd 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// fast_source_line_resolver_unittest.cc: Unit tests for FastSourceLineResolver.
 31// Two different approaches for testing fast source line resolver:
 32// First, use the same unit test data for basic source line resolver.
 33// Second, read data from symbol files, load them as basic modules, and then
 34// serialize them and load the serialized data as fast modules.  Then compare
 35// modules to assure the fast module contains exactly the same data as
 36// basic module.
 37//
 38// Author: Siyang Xie (lambxsy@google.com)
 39
 40#include <stdio.h>
 41
 42#include <sstream>
 43#include <string>
 44
 45#include "breakpad_googletest_includes.h"
 46#include "google_breakpad/processor/code_module.h"
 47#include "google_breakpad/processor/stack_frame.h"
 48#include "google_breakpad/processor/memory_region.h"
 49#include "processor/logging.h"
 50#include "processor/module_serializer.h"
 51#include "processor/module_comparer.h"
 52
 53namespace {
 54
 55using std::string;
 56using google_breakpad::SourceLineResolverBase;
 57using google_breakpad::BasicSourceLineResolver;
 58using google_breakpad::FastSourceLineResolver;
 59using google_breakpad::ModuleSerializer;
 60using google_breakpad::ModuleComparer;
 61using google_breakpad::CFIFrameInfo;
 62using google_breakpad::CodeModule;
 63using google_breakpad::MemoryRegion;
 64using google_breakpad::StackFrame;
 65using google_breakpad::WindowsFrameInfo;
 66using google_breakpad::linked_ptr;
 67using google_breakpad::scoped_ptr;
 68
 69class TestCodeModule : public CodeModule {
 70 public:
 71  explicit TestCodeModule(string code_file) : code_file_(code_file) {}
 72  virtual ~TestCodeModule() {}
 73
 74  virtual u_int64_t base_address() const { return 0; }
 75  virtual u_int64_t size() const { return 0xb000; }
 76  virtual string code_file() const { return code_file_; }
 77  virtual string code_identifier() const { return ""; }
 78  virtual string debug_file() const { return ""; }
 79  virtual string debug_identifier() const { return ""; }
 80  virtual string version() const { return ""; }
 81  virtual const CodeModule* Copy() const {
 82    return new TestCodeModule(code_file_);
 83  }
 84
 85 private:
 86  string code_file_;
 87};
 88
 89// A mock memory region object, for use by the STACK CFI tests.
 90class MockMemoryRegion: public MemoryRegion {
 91  u_int64_t GetBase() const { return 0x10000; }
 92  u_int32_t GetSize() const { return 0x01000; }
 93  bool GetMemoryAtAddress(u_int64_t address, u_int8_t *value) const {
 94    *value = address & 0xff;
 95    return true;
 96  }
 97  bool GetMemoryAtAddress(u_int64_t address, u_int16_t *value) const {
 98    *value = address & 0xffff;
 99    return true;
100  }
101  bool GetMemoryAtAddress(u_int64_t address, u_int32_t *value) const {
102    switch (address) {
103      case 0x10008: *value = 0x98ecadc3; break;  // saved %ebx
104      case 0x1000c: *value = 0x878f7524; break;  // saved %esi
105      case 0x10010: *value = 0x6312f9a5; break;  // saved %edi
106      case 0x10014: *value = 0x10038;    break;  // caller's %ebp
107      case 0x10018: *value = 0xf6438648; break;  // return address
108      default: *value = 0xdeadbeef;      break;  // junk
109    }
110    return true;
111  }
112  bool GetMemoryAtAddress(u_int64_t address, u_int64_t *value) const {
113    *value = address;
114    return true;
115  }
116};
117
118// Verify that, for every association in ACTUAL, EXPECTED has the same
119// association. (That is, ACTUAL's associations should be a subset of
120// EXPECTED's.) Also verify that ACTUAL has associations for ".ra" and
121// ".cfa".
122static bool VerifyRegisters(
123    const char *file, int line,
124    const CFIFrameInfo::RegisterValueMap<u_int32_t> &expected,
125    const CFIFrameInfo::RegisterValueMap<u_int32_t> &actual) {
126  CFIFrameInfo::RegisterValueMap<u_int32_t>::const_iterator a;
127  a = actual.find(".cfa");
128  if (a == actual.end())
129    return false;
130  a = actual.find(".ra");
131  if (a == actual.end())
132    return false;
133  for (a = actual.begin(); a != actual.end(); a++) {
134    CFIFrameInfo::RegisterValueMap<u_int32_t>::const_iterator e =
135      expected.find(a->first);
136    if (e == expected.end()) {
137      fprintf(stderr, "%s:%d: unexpected register '%s' recovered, value 0x%x\n",
138              file, line, a->first.c_str(), a->second);
139      return false;
140    }
141    if (e->second != a->second) {
142      fprintf(stderr,
143              "%s:%d: register '%s' recovered value was 0x%x, expected 0x%x\n",
144              file, line, a->first.c_str(), a->second, e->second);
145      return false;
146    }
147    // Don't complain if this doesn't recover all registers. Although
148    // the DWARF spec says that unmentioned registers are undefined,
149    // GCC uses omission to mean that they are unchanged.
150  }
151  return true;
152}
153
154static bool VerifyEmpty(const StackFrame &frame) {
155  if (frame.function_name.empty() &&
156      frame.source_file_name.empty() &&
157      frame.source_line == 0)
158    return true;
159  return false;
160}
161
162static void ClearSourceLineInfo(StackFrame *frame) {
163  frame->function_name.clear();
164  frame->module = NULL;
165  frame->source_file_name.clear();
166  frame->source_line = 0;
167}
168
169class TestFastSourceLineResolver : public ::testing::Test {
170 public:
171  void SetUp() {
172    testdata_dir = string(getenv("srcdir") ? getenv("srcdir") : ".") +
173                         "/src/processor/testdata";
174  }
175
176  string symbol_file(int file_index) {
177    std::stringstream ss;
178    ss << testdata_dir << "/module" << file_index << ".out";
179    return ss.str();
180  }
181
182  ModuleSerializer serializer;
183  BasicSourceLineResolver basic_resolver;
184  FastSourceLineResolver fast_resolver;
185  ModuleComparer module_comparer;
186
187  string testdata_dir;
188};
189
190// Test adapted from basic_source_line_resolver_unittest.
191TEST_F(TestFastSourceLineResolver, TestLoadAndResolve) {
192  TestCodeModule module1("module1");
193  ASSERT_TRUE(basic_resolver.LoadModule(&module1, symbol_file(1)));
194  ASSERT_TRUE(basic_resolver.HasModule(&module1));
195  // Convert module1 to fast_module:
196  ASSERT_TRUE(serializer.ConvertOneModule(
197      module1.code_file(), &basic_resolver, &fast_resolver));
198  ASSERT_TRUE(fast_resolver.HasModule(&module1));
199
200  TestCodeModule module2("module2");
201  ASSERT_TRUE(basic_resolver.LoadModule(&module2, symbol_file(2)));
202  ASSERT_TRUE(basic_resolver.HasModule(&module2));
203  // Convert module2 to fast_module:
204  ASSERT_TRUE(serializer.ConvertOneModule(
205      module2.code_file(), &basic_resolver, &fast_resolver));
206  ASSERT_TRUE(fast_resolver.HasModule(&module2));
207
208  StackFrame frame;
209  scoped_ptr<WindowsFrameInfo> windows_frame_info;
210  scoped_ptr<CFIFrameInfo> cfi_frame_info;
211  frame.instruction = 0x1000;
212  frame.module = NULL;
213  fast_resolver.FillSourceLineInfo(&frame);
214  ASSERT_FALSE(frame.module);
215  ASSERT_TRUE(frame.function_name.empty());
216  ASSERT_EQ(frame.function_base, 0);
217  ASSERT_TRUE(frame.source_file_name.empty());
218  ASSERT_EQ(frame.source_line, 0);
219  ASSERT_EQ(frame.source_line_base, 0);
220
221  frame.module = &module1;
222  fast_resolver.FillSourceLineInfo(&frame);
223  ASSERT_EQ(frame.function_name, "Function1_1");
224  ASSERT_TRUE(frame.module);
225  ASSERT_EQ(frame.module->code_file(), "module1");
226  ASSERT_EQ(frame.function_base, 0x1000);
227  ASSERT_EQ(frame.source_file_name, "file1_1.cc");
228  ASSERT_EQ(frame.source_line, 44);
229  ASSERT_EQ(frame.source_line_base, 0x1000);
230  windows_frame_info.reset(fast_resolver.FindWindowsFrameInfo(&frame));
231  ASSERT_TRUE(windows_frame_info.get());
232  ASSERT_FALSE(windows_frame_info->allocates_base_pointer);
233  ASSERT_EQ(windows_frame_info->program_string,
234            "$eip 4 + ^ = $esp $ebp 8 + = $ebp $ebp ^ =");
235
236  ClearSourceLineInfo(&frame);
237  frame.instruction = 0x800;
238  frame.module = &module1;
239  fast_resolver.FillSourceLineInfo(&frame);
240  ASSERT_TRUE(VerifyEmpty(frame));
241  windows_frame_info.reset(fast_resolver.FindWindowsFrameInfo(&frame));
242  ASSERT_FALSE(windows_frame_info.get());
243
244  frame.instruction = 0x1280;
245  fast_resolver.FillSourceLineInfo(&frame);
246  ASSERT_EQ(frame.function_name, "Function1_3");
247  ASSERT_TRUE(frame.source_file_name.empty());
248  ASSERT_EQ(frame.source_line, 0);
249  windows_frame_info.reset(fast_resolver.FindWindowsFrameInfo(&frame));
250  ASSERT_TRUE(windows_frame_info.get());
251  ASSERT_EQ(windows_frame_info->type_, WindowsFrameInfo::STACK_INFO_UNKNOWN);
252  ASSERT_FALSE(windows_frame_info->allocates_base_pointer);
253  ASSERT_TRUE(windows_frame_info->program_string.empty());
254
255  frame.instruction = 0x1380;
256  fast_resolver.FillSourceLineInfo(&frame);
257  ASSERT_EQ(frame.function_name, "Function1_4");
258  ASSERT_TRUE(frame.source_file_name.empty());
259  ASSERT_EQ(frame.source_line, 0);
260  windows_frame_info.reset(fast_resolver.FindWindowsFrameInfo(&frame));
261  ASSERT_TRUE(windows_frame_info.get());
262  ASSERT_EQ(windows_frame_info->type_, WindowsFrameInfo::STACK_INFO_FRAME_DATA);
263  ASSERT_FALSE(windows_frame_info->allocates_base_pointer);
264  ASSERT_FALSE(windows_frame_info->program_string.empty());
265
266  frame.instruction = 0x2000;
267  windows_frame_info.reset(fast_resolver.FindWindowsFrameInfo(&frame));
268  ASSERT_FALSE(windows_frame_info.get());
269
270  // module1 has STACK CFI records covering 3d40..3def;
271  // module2 has STACK CFI records covering 3df0..3e9f;
272  // check that FindCFIFrameInfo doesn't claim to find any outside those ranges.
273  frame.instruction = 0x3d3f;
274  frame.module = &module1;
275  cfi_frame_info.reset(fast_resolver.FindCFIFrameInfo(&frame));
276  ASSERT_FALSE(cfi_frame_info.get());
277
278  frame.instruction = 0x3e9f;
279  frame.module = &module1;
280  cfi_frame_info.reset(fast_resolver.FindCFIFrameInfo(&frame));
281  ASSERT_FALSE(cfi_frame_info.get());
282
283  CFIFrameInfo::RegisterValueMap<u_int32_t> current_registers;
284  CFIFrameInfo::RegisterValueMap<u_int32_t> caller_registers;
285  CFIFrameInfo::RegisterValueMap<u_int32_t> expected_caller_registers;
286  MockMemoryRegion memory;
287
288  // Regardless of which instruction evaluation takes place at, it
289  // should produce the same values for the caller's registers.
290  expected_caller_registers[".cfa"] = 0x1001c;
291  expected_caller_registers[".ra"]  = 0xf6438648;
292  expected_caller_registers["$ebp"] = 0x10038;
293  expected_caller_registers["$ebx"] = 0x98ecadc3;
294  expected_caller_registers["$esi"] = 0x878f7524;
295  expected_caller_registers["$edi"] = 0x6312f9a5;
296
297  frame.instruction = 0x3d40;
298  frame.module = &module1;
299  current_registers.clear();
300  current_registers["$esp"] = 0x10018;
301  current_registers["$ebp"] = 0x10038;
302  current_registers["$ebx"] = 0x98ecadc3;
303  current_registers["$esi"] = 0x878f7524;
304  current_registers["$edi"] = 0x6312f9a5;
305  cfi_frame_info.reset(fast_resolver.FindCFIFrameInfo(&frame));
306  ASSERT_TRUE(cfi_frame_info.get());
307  ASSERT_TRUE(cfi_frame_info.get()
308              ->FindCallerRegs<u_int32_t>(current_registers, memory,
309                                          &caller_registers));
310  ASSERT_TRUE(VerifyRegisters(__FILE__, __LINE__,
311                              expected_caller_registers, caller_registers));
312
313  frame.instruction = 0x3d41;
314  current_registers["$esp"] = 0x10014;
315  cfi_frame_info.reset(fast_resolver.FindCFIFrameInfo(&frame));
316  ASSERT_TRUE(cfi_frame_info.get());
317  ASSERT_TRUE(cfi_frame_info.get()
318              ->FindCallerRegs<u_int32_t>(current_registers, memory,
319                                          &caller_registers));
320  ASSERT_TRUE(VerifyRegisters(__FILE__, __LINE__,
321                              expected_caller_registers, caller_registers));
322
323  frame.instruction = 0x3d43;
324  current_registers["$ebp"] = 0x10014;
325  cfi_frame_info.reset(fast_resolver.FindCFIFrameInfo(&frame));
326  ASSERT_TRUE(cfi_frame_info.get());
327  ASSERT_TRUE(cfi_frame_info.get()
328              ->FindCallerRegs<u_int32_t>(current_registers, memory,
329                                          &caller_registers));
330  VerifyRegisters(__FILE__, __LINE__,
331                  expected_caller_registers, caller_registers);
332
333  frame.instruction = 0x3d54;
334  current_registers["$ebx"] = 0x6864f054U;
335  cfi_frame_info.reset(fast_resolver.FindCFIFrameInfo(&frame));
336  ASSERT_TRUE(cfi_frame_info.get());
337  ASSERT_TRUE(cfi_frame_info.get()
338              ->FindCallerRegs<u_int32_t>(current_registers, memory,
339                                          &caller_registers));
340  VerifyRegisters(__FILE__, __LINE__,
341                  expected_caller_registers, caller_registers);
342
343  frame.instruction = 0x3d5a;
344  current_registers["$esi"] = 0x6285f79aU;
345  cfi_frame_info.reset(fast_resolver.FindCFIFrameInfo(&frame));
346  ASSERT_TRUE(cfi_frame_info.get());
347  ASSERT_TRUE(cfi_frame_info.get()
348              ->FindCallerRegs<u_int32_t>(current_registers, memory,
349                                          &caller_registers));
350  VerifyRegisters(__FILE__, __LINE__,
351                  expected_caller_registers, caller_registers);
352
353  frame.instruction = 0x3d84;
354  current_registers["$edi"] = 0x64061449U;
355  cfi_frame_info.reset(fast_resolver.FindCFIFrameInfo(&frame));
356  ASSERT_TRUE(cfi_frame_info.get());
357  ASSERT_TRUE(cfi_frame_info.get()
358              ->FindCallerRegs<u_int32_t>(current_registers, memory,
359                                          &caller_registers));
360  VerifyRegisters(__FILE__, __LINE__,
361                  expected_caller_registers, caller_registers);
362
363  frame.instruction = 0x2900;
364  frame.module = &module1;
365  fast_resolver.FillSourceLineInfo(&frame);
366  ASSERT_EQ(frame.function_name, string("PublicSymbol"));
367
368  frame.instruction = 0x4000;
369  frame.module = &module1;
370  fast_resolver.FillSourceLineInfo(&frame);
371  ASSERT_EQ(frame.function_name, string("LargeFunction"));
372
373  frame.instruction = 0x2181;
374  frame.module = &module2;
375  fast_resolver.FillSourceLineInfo(&frame);
376  ASSERT_EQ(frame.function_name, "Function2_2");
377  ASSERT_EQ(frame.function_base, 0x2170);
378  ASSERT_TRUE(frame.module);
379  ASSERT_EQ(frame.module->code_file(), "module2");
380  ASSERT_EQ(frame.source_file_name, "file2_2.cc");
381  ASSERT_EQ(frame.source_line, 21);
382  ASSERT_EQ(frame.source_line_base, 0x2180);
383  windows_frame_info.reset(fast_resolver.FindWindowsFrameInfo(&frame));
384  ASSERT_TRUE(windows_frame_info.get());
385  ASSERT_EQ(windows_frame_info->type_, WindowsFrameInfo::STACK_INFO_FRAME_DATA);
386  ASSERT_EQ(windows_frame_info->prolog_size, 1);
387
388  frame.instruction = 0x216f;
389  fast_resolver.FillSourceLineInfo(&frame);
390  ASSERT_EQ(frame.function_name, "Public2_1");
391
392  ClearSourceLineInfo(&frame);
393  frame.instruction = 0x219f;
394  frame.module = &module2;
395  fast_resolver.FillSourceLineInfo(&frame);
396  ASSERT_TRUE(frame.function_name.empty());
397
398  frame.instruction = 0x21a0;
399  frame.module = &module2;
400  fast_resolver.FillSourceLineInfo(&frame);
401  ASSERT_EQ(frame.function_name, "Public2_2");
402}
403
404TEST_F(TestFastSourceLineResolver, TestInvalidLoads) {
405  TestCodeModule module3("module3");
406  ASSERT_FALSE(basic_resolver.LoadModule(&module3,
407                                         testdata_dir + "/module3_bad.out"));
408  ASSERT_FALSE(basic_resolver.HasModule(&module3));
409  // Convert module3 to fast_module:
410  ASSERT_FALSE(serializer.ConvertOneModule(module3.code_file(),
411                                           &basic_resolver,
412                                           &fast_resolver));
413  ASSERT_FALSE(fast_resolver.HasModule(&module3));
414
415  TestCodeModule module4("module4");
416  ASSERT_FALSE(basic_resolver.LoadModule(&module4,
417                                         testdata_dir + "/module4_bad.out"));
418  ASSERT_FALSE(basic_resolver.HasModule(&module4));
419  // Convert module4 to fast_module:
420  ASSERT_FALSE(serializer.ConvertOneModule(module4.code_file(),
421                                           &basic_resolver,
422                                           &fast_resolver));
423  ASSERT_FALSE(fast_resolver.HasModule(&module4));
424
425  TestCodeModule module5("module5");
426  ASSERT_FALSE(fast_resolver.LoadModule(&module5,
427                                         testdata_dir + "/invalid-filename"));
428  ASSERT_FALSE(fast_resolver.HasModule(&module5));
429
430  TestCodeModule invalidmodule("invalid-module");
431  ASSERT_FALSE(fast_resolver.HasModule(&invalidmodule));
432}
433
434TEST_F(TestFastSourceLineResolver, TestUnload) {
435  TestCodeModule module1("module1");
436  ASSERT_FALSE(basic_resolver.HasModule(&module1));
437
438  ASSERT_TRUE(basic_resolver.LoadModule(&module1, symbol_file(1)));
439  ASSERT_TRUE(basic_resolver.HasModule(&module1));
440  // Convert module1 to fast_module.
441  ASSERT_TRUE(serializer.ConvertOneModule(module1.code_file(),
442                                          &basic_resolver,
443                                          &fast_resolver));
444  ASSERT_TRUE(fast_resolver.HasModule(&module1));
445  basic_resolver.UnloadModule(&module1);
446  fast_resolver.UnloadModule(&module1);
447  ASSERT_FALSE(fast_resolver.HasModule(&module1));
448
449  ASSERT_TRUE(basic_resolver.LoadModule(&module1, symbol_file(1)));
450  ASSERT_TRUE(basic_resolver.HasModule(&module1));
451  // Convert module1 to fast_module.
452  ASSERT_TRUE(serializer.ConvertOneModule(module1.code_file(),
453                                          &basic_resolver,
454                                          &fast_resolver));
455  ASSERT_TRUE(fast_resolver.HasModule(&module1));
456}
457
458TEST_F(TestFastSourceLineResolver, CompareModule) {
459  char *symbol_data;
460  string symbol_data_string;
461  string filename;
462
463  for (int module_index = 0; module_index < 3; ++module_index) {
464    std::stringstream ss;
465    ss << testdata_dir << "/module" << module_index << ".out";
466    filename = ss.str();
467    ASSERT_TRUE(SourceLineResolverBase::ReadSymbolFile(
468        &symbol_data, symbol_file(module_index)));
469    symbol_data_string = symbol_data;
470    delete [] symbol_data;
471    ASSERT_TRUE(module_comparer.Compare(symbol_data_string));
472  }
473}
474
475}  // namespace
476
477int main(int argc, char *argv[]) {
478  ::testing::InitGoogleTest(&argc, argv);
479  return RUN_ALL_TESTS();
480}