1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/toolkit/crashreporter/google-breakpad/src/common/linux/dump_symbols_unittest.cc Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,168 @@ 1.4 +// Copyright (c) 2011 Google Inc. 1.5 +// All rights reserved. 1.6 +// 1.7 +// Redistribution and use in source and binary forms, with or without 1.8 +// modification, are permitted provided that the following conditions are 1.9 +// met: 1.10 +// 1.11 +// * Redistributions of source code must retain the above copyright 1.12 +// notice, this list of conditions and the following disclaimer. 1.13 +// * Redistributions in binary form must reproduce the above 1.14 +// copyright notice, this list of conditions and the following disclaimer 1.15 +// in the documentation and/or other materials provided with the 1.16 +// distribution. 1.17 +// * Neither the name of Google Inc. nor the names of its 1.18 +// contributors may be used to endorse or promote products derived from 1.19 +// this software without specific prior written permission. 1.20 +// 1.21 +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 1.22 +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 1.23 +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 1.24 +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 1.25 +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 1.26 +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 1.27 +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 1.28 +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 1.29 +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 1.30 +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 1.31 +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 1.32 + 1.33 +// Original author: Ted Mielczarek <ted.mielczarek@gmail.com> 1.34 + 1.35 +// dump_symbols_unittest.cc: 1.36 +// Unittests for google_breakpad::DumpSymbols 1.37 + 1.38 +#include <elf.h> 1.39 +#include <link.h> 1.40 +#include <stdio.h> 1.41 + 1.42 +#include <sstream> 1.43 +#include <vector> 1.44 + 1.45 +#include "breakpad_googletest_includes.h" 1.46 +#include "common/linux/synth_elf.h" 1.47 +#include "common/module.h" 1.48 +#include "common/using_std_string.h" 1.49 + 1.50 +namespace google_breakpad { 1.51 +bool ReadSymbolDataInternal(const uint8_t* obj_file, 1.52 + const string& obj_filename, 1.53 + const std::vector<string>& debug_dir, 1.54 + SymbolData symbol_data, 1.55 + Module** module); 1.56 +} 1.57 + 1.58 +using google_breakpad::synth_elf::ELF; 1.59 +using google_breakpad::synth_elf::StringTable; 1.60 +using google_breakpad::synth_elf::SymbolTable; 1.61 +using google_breakpad::test_assembler::kLittleEndian; 1.62 +using google_breakpad::test_assembler::Section; 1.63 +using google_breakpad::Module; 1.64 +using google_breakpad::ReadSymbolDataInternal; 1.65 +using std::stringstream; 1.66 +using std::vector; 1.67 +using ::testing::Test; 1.68 + 1.69 +class DumpSymbols : public Test { 1.70 + public: 1.71 + void GetElfContents(ELF& elf) { 1.72 + string contents; 1.73 + ASSERT_TRUE(elf.GetContents(&contents)); 1.74 + ASSERT_LT(0U, contents.size()); 1.75 + 1.76 + elfdata_v.clear(); 1.77 + elfdata_v.insert(elfdata_v.begin(), contents.begin(), contents.end()); 1.78 + elfdata = &elfdata_v[0]; 1.79 + } 1.80 + 1.81 + vector<uint8_t> elfdata_v; 1.82 + uint8_t* elfdata; 1.83 +}; 1.84 + 1.85 +TEST_F(DumpSymbols, Invalid) { 1.86 + Elf32_Ehdr header; 1.87 + memset(&header, 0, sizeof(header)); 1.88 + Module* module; 1.89 + EXPECT_FALSE(ReadSymbolDataInternal(reinterpret_cast<uint8_t*>(&header), 1.90 + "foo", 1.91 + vector<string>(), 1.92 + ALL_SYMBOL_DATA, 1.93 + &module)); 1.94 +} 1.95 + 1.96 +TEST_F(DumpSymbols, SimplePublic32) { 1.97 + ELF elf(EM_386, ELFCLASS32, kLittleEndian); 1.98 + // Zero out text section for simplicity. 1.99 + Section text(kLittleEndian); 1.100 + text.Append(4096, 0); 1.101 + elf.AddSection(".text", text, SHT_PROGBITS); 1.102 + 1.103 + // Add a public symbol. 1.104 + StringTable table(kLittleEndian); 1.105 + SymbolTable syms(kLittleEndian, 4, table); 1.106 + syms.AddSymbol("superfunc", (uint32_t)0x1000, (uint32_t)0x10, 1.107 + ELF32_ST_INFO(STB_GLOBAL, STT_FUNC), 1.108 + SHN_UNDEF + 1); 1.109 + int index = elf.AddSection(".dynstr", table, SHT_STRTAB); 1.110 + elf.AddSection(".dynsym", syms, 1.111 + SHT_DYNSYM, // type 1.112 + SHF_ALLOC, // flags 1.113 + 0, // addr 1.114 + index, // link 1.115 + sizeof(Elf32_Sym)); // entsize 1.116 + 1.117 + elf.Finish(); 1.118 + GetElfContents(elf); 1.119 + 1.120 + Module* module; 1.121 + EXPECT_TRUE(ReadSymbolDataInternal(elfdata, 1.122 + "foo", 1.123 + vector<string>(), 1.124 + ALL_SYMBOL_DATA, 1.125 + &module)); 1.126 + 1.127 + stringstream s; 1.128 + module->Write(s, ALL_SYMBOL_DATA); 1.129 + EXPECT_EQ("MODULE Linux x86 000000000000000000000000000000000 foo\n" 1.130 + "PUBLIC 1000 0 superfunc\n", 1.131 + s.str()); 1.132 + delete module; 1.133 +} 1.134 + 1.135 +TEST_F(DumpSymbols, SimplePublic64) { 1.136 + ELF elf(EM_X86_64, ELFCLASS64, kLittleEndian); 1.137 + // Zero out text section for simplicity. 1.138 + Section text(kLittleEndian); 1.139 + text.Append(4096, 0); 1.140 + elf.AddSection(".text", text, SHT_PROGBITS); 1.141 + 1.142 + // Add a public symbol. 1.143 + StringTable table(kLittleEndian); 1.144 + SymbolTable syms(kLittleEndian, 8, table); 1.145 + syms.AddSymbol("superfunc", (uint64_t)0x1000, (uint64_t)0x10, 1.146 + ELF64_ST_INFO(STB_GLOBAL, STT_FUNC), 1.147 + SHN_UNDEF + 1); 1.148 + int index = elf.AddSection(".dynstr", table, SHT_STRTAB); 1.149 + elf.AddSection(".dynsym", syms, 1.150 + SHT_DYNSYM, // type 1.151 + SHF_ALLOC, // flags 1.152 + 0, // addr 1.153 + index, // link 1.154 + sizeof(Elf64_Sym)); // entsize 1.155 + 1.156 + elf.Finish(); 1.157 + GetElfContents(elf); 1.158 + 1.159 + Module* module; 1.160 + EXPECT_TRUE(ReadSymbolDataInternal(elfdata, 1.161 + "foo", 1.162 + vector<string>(), 1.163 + ALL_SYMBOL_DATA, 1.164 + &module)); 1.165 + 1.166 + stringstream s; 1.167 + module->Write(s, ALL_SYMBOL_DATA); 1.168 + EXPECT_EQ("MODULE Linux x86_64 000000000000000000000000000000000 foo\n" 1.169 + "PUBLIC 1000 0 superfunc\n", 1.170 + s.str()); 1.171 +}