mirror of
https://git.suyu.dev/suyu/breakpad.git
synced 2025-12-27 09:45:27 +01:00
Merge of Breakpad Chrome Linux fork
A=agl, Lei Zhang R=nealsid, agl git-svn-id: http://google-breakpad.googlecode.com/svn/trunk@384 4c0a9323-5329-0410-9bdc-e9ce6186880e
This commit is contained in:
parent
7c48629d49
commit
b0baafc4da
29 changed files with 6750 additions and 2288 deletions
|
|
@ -27,20 +27,22 @@
|
|||
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
|
||||
#include <cstdarg>
|
||||
#include <cstdlib>
|
||||
#include <cstdio>
|
||||
#include <assert.h>
|
||||
#include <cxxabi.h>
|
||||
#include <elf.h>
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <link.h>
|
||||
#include <string.h>
|
||||
#include <sys/mman.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
#include <algorithm>
|
||||
|
||||
#include <algorithm>
|
||||
#include <cstdarg>
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
#include <functional>
|
||||
#include <list>
|
||||
|
|
@ -121,7 +123,7 @@ static const ElfW(Shdr) *FindSectionByName(const char *name,
|
|||
|
||||
for (int i = 0; i < nsection; ++i) {
|
||||
const char *section_name =
|
||||
(char*)(strtab->sh_offset + sections[i].sh_name);
|
||||
reinterpret_cast<char*>(strtab->sh_offset + sections[i].sh_name);
|
||||
if (!strncmp(name, section_name, name_len))
|
||||
return sections + i;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -32,104 +32,74 @@
|
|||
// See file_id.h for documentation
|
||||
//
|
||||
|
||||
#include <cassert>
|
||||
#include <cstdio>
|
||||
#include "common/linux/file_id.h"
|
||||
|
||||
#include <arpa/inet.h>
|
||||
#include <elf.h>
|
||||
#include <fcntl.h>
|
||||
#include <link.h>
|
||||
#include <sys/mman.h>
|
||||
#include <string.h>
|
||||
#include <sys/mman.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "common/linux/file_id.h"
|
||||
#include "common/md5.h"
|
||||
#include <cassert>
|
||||
#include <cstdio>
|
||||
|
||||
namespace google_breakpad {
|
||||
|
||||
static bool FindElfTextSection(const void *elf_mapped_base,
|
||||
const void **text_start,
|
||||
int *text_size) {
|
||||
assert(elf_mapped_base);
|
||||
assert(text_start);
|
||||
assert(text_size);
|
||||
|
||||
const unsigned char *elf_base =
|
||||
static_cast<const unsigned char *>(elf_mapped_base);
|
||||
const ElfW(Ehdr) *elf_header =
|
||||
reinterpret_cast<const ElfW(Ehdr) *>(elf_base);
|
||||
if (memcmp(elf_header, ELFMAG, SELFMAG) != 0)
|
||||
return false;
|
||||
*text_start = NULL;
|
||||
*text_size = 0;
|
||||
const ElfW(Shdr) *sections =
|
||||
reinterpret_cast<const ElfW(Shdr) *>(elf_base + elf_header->e_shoff);
|
||||
const char *text_section_name = ".text";
|
||||
int name_len = strlen(text_section_name);
|
||||
const ElfW(Shdr) *string_section = sections + elf_header->e_shstrndx;
|
||||
const ElfW(Shdr) *text_section = NULL;
|
||||
for (int i = 0; i < elf_header->e_shnum; ++i) {
|
||||
if (sections[i].sh_type == SHT_PROGBITS) {
|
||||
const char *section_name = (char*)(elf_base +
|
||||
string_section->sh_offset +
|
||||
sections[i].sh_name);
|
||||
if (!strncmp(section_name, text_section_name, name_len)) {
|
||||
text_section = §ions[i];
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (text_section != NULL && text_section->sh_size > 0) {
|
||||
int text_section_size = text_section->sh_size;
|
||||
*text_start = elf_base + text_section->sh_offset;
|
||||
*text_size = text_section_size;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
FileID::FileID(const char *path) {
|
||||
FileID::FileID(const char* path) {
|
||||
strncpy(path_, path, sizeof(path_));
|
||||
}
|
||||
|
||||
bool FileID::ElfFileIdentifier(unsigned char identifier[16]) {
|
||||
bool FileID::ElfFileIdentifier(uint8_t identifier[kMDGUIDSize]) {
|
||||
const ssize_t mapped_len = 4096; // Page size (matches WriteMappings())
|
||||
int fd = open(path_, O_RDONLY);
|
||||
if (fd < 0)
|
||||
return false;
|
||||
struct stat st;
|
||||
if (fstat(fd, &st) != 0 && st.st_size <= 0) {
|
||||
if (fstat(fd, &st) != 0 || st.st_size <= mapped_len) {
|
||||
close(fd);
|
||||
return false;
|
||||
}
|
||||
void *base = mmap(NULL, st.st_size,
|
||||
void* base = mmap(NULL, mapped_len,
|
||||
PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
|
||||
if (base == MAP_FAILED) {
|
||||
close(fd);
|
||||
close(fd);
|
||||
if (base == MAP_FAILED)
|
||||
return false;
|
||||
}
|
||||
bool success = false;
|
||||
const void *text_section = NULL;
|
||||
int text_size = 0;
|
||||
if (FindElfTextSection(base, &text_section, &text_size) && (text_size > 0)) {
|
||||
struct MD5Context md5;
|
||||
MD5Init(&md5);
|
||||
MD5Update(&md5,
|
||||
static_cast<const unsigned char*>(text_section),
|
||||
text_size);
|
||||
MD5Final(identifier, &md5);
|
||||
success = true;
|
||||
|
||||
memset(identifier, 0, kMDGUIDSize);
|
||||
uint8_t* ptr = reinterpret_cast<uint8_t*>(base);
|
||||
uint8_t* ptr_end = ptr + mapped_len;
|
||||
while (ptr < ptr_end) {
|
||||
for (unsigned i = 0; i < kMDGUIDSize; i++)
|
||||
identifier[i] ^= ptr[i];
|
||||
ptr += kMDGUIDSize;
|
||||
}
|
||||
|
||||
close(fd);
|
||||
munmap(base, st.st_size);
|
||||
return success;
|
||||
munmap(base, mapped_len);
|
||||
return true;
|
||||
}
|
||||
|
||||
// static
|
||||
void FileID::ConvertIdentifierToString(const unsigned char identifier[16],
|
||||
char *buffer, int buffer_length) {
|
||||
void FileID::ConvertIdentifierToString(const uint8_t identifier[kMDGUIDSize],
|
||||
char* buffer, int buffer_length) {
|
||||
uint8_t identifier_swapped[kMDGUIDSize];
|
||||
|
||||
// Endian-ness swap to match dump processor expectation.
|
||||
memcpy(identifier_swapped, identifier, kMDGUIDSize);
|
||||
uint32_t* data1 = reinterpret_cast<uint32_t*>(identifier_swapped);
|
||||
*data1 = htonl(*data1);
|
||||
uint16_t* data2 = reinterpret_cast<uint16_t*>(identifier_swapped + 4);
|
||||
*data2 = htons(*data2);
|
||||
uint16_t* data3 = reinterpret_cast<uint16_t*>(identifier_swapped + 6);
|
||||
*data3 = htons(*data3);
|
||||
|
||||
int buffer_idx = 0;
|
||||
for (int idx = 0; (buffer_idx < buffer_length) && (idx < 16); ++idx) {
|
||||
int hi = (identifier[idx] >> 4) & 0x0F;
|
||||
int lo = (identifier[idx]) & 0x0F;
|
||||
for (unsigned int idx = 0;
|
||||
(buffer_idx < buffer_length) && (idx < kMDGUIDSize);
|
||||
++idx) {
|
||||
int hi = (identifier_swapped[idx] >> 4) & 0x0F;
|
||||
int lo = (identifier_swapped[idx]) & 0x0F;
|
||||
|
||||
if (idx == 4 || idx == 6 || idx == 8 || idx == 10)
|
||||
buffer[buffer_idx++] = '-';
|
||||
|
|
|
|||
|
|
@ -35,25 +35,30 @@
|
|||
|
||||
#include <limits.h>
|
||||
|
||||
#include "common/linux/guid_creator.h"
|
||||
|
||||
namespace google_breakpad {
|
||||
|
||||
static const size_t kMDGUIDSize = sizeof(MDGUID);
|
||||
|
||||
class FileID {
|
||||
public:
|
||||
FileID(const char *path);
|
||||
~FileID() {};
|
||||
explicit FileID(const char* path);
|
||||
~FileID() {}
|
||||
|
||||
// Load the identifier for the elf file path specified in the constructor into
|
||||
// |identifier|. Return false if the identifier could not be created for the
|
||||
// file.
|
||||
// The current implementation will return the MD5 hash of the file's bytes.
|
||||
bool ElfFileIdentifier(unsigned char identifier[16]);
|
||||
// The current implementation will XOR the first page of data to generate an
|
||||
// identifier.
|
||||
bool ElfFileIdentifier(uint8_t identifier[kMDGUIDSize]);
|
||||
|
||||
// Convert the |identifier| data to a NULL terminated string. The string will
|
||||
// be formatted as a UUID (e.g., 22F065BB-FC9C-49F7-80FE-26A7CEBD7BCE).
|
||||
// The |buffer| should be at least 37 bytes long to receive all of the data
|
||||
// and termination. Shorter buffers will contain truncated data.
|
||||
static void ConvertIdentifierToString(const unsigned char identifier[16],
|
||||
char *buffer, int buffer_length);
|
||||
static void ConvertIdentifierToString(const uint8_t identifier[kMDGUIDSize],
|
||||
char* buffer, int buffer_length);
|
||||
|
||||
private:
|
||||
// Storage for the path specified
|
||||
|
|
@ -63,4 +68,3 @@ class FileID {
|
|||
} // namespace google_breakpad
|
||||
|
||||
#endif // COMMON_LINUX_FILE_ID_H__
|
||||
|
||||
|
|
|
|||
178
src/common/linux/linux_libc_support.h
Normal file
178
src/common/linux/linux_libc_support.h
Normal file
|
|
@ -0,0 +1,178 @@
|
|||
// Copyright (c) 2009, Google Inc.
|
||||
// All rights reserved.
|
||||
//
|
||||
// Redistribution and use in source and binary forms, with or without
|
||||
// modification, are permitted provided that the following conditions are
|
||||
// met:
|
||||
//
|
||||
// * Redistributions of source code must retain the above copyright
|
||||
// notice, this list of conditions and the following disclaimer.
|
||||
// * Redistributions in binary form must reproduce the above
|
||||
// copyright notice, this list of conditions and the following disclaimer
|
||||
// in the documentation and/or other materials provided with the
|
||||
// distribution.
|
||||
// * Neither the name of Google Inc. nor the names of its
|
||||
// contributors may be used to endorse or promote products derived from
|
||||
// this software without specific prior written permission.
|
||||
//
|
||||
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
|
||||
// This header provides replacements for libc functions that we need. We if
|
||||
// call the libc functions directly we risk crashing in the dynamic linker as
|
||||
// it tries to resolve uncached PLT entries.
|
||||
|
||||
#ifndef CLIENT_LINUX_LINUX_LIBC_SUPPORT_H_
|
||||
#define CLIENT_LINUX_LINUX_LIBC_SUPPORT_H_
|
||||
|
||||
#include <stdint.h>
|
||||
#include <limits.h>
|
||||
#include <sys/types.h>
|
||||
|
||||
extern "C" {
|
||||
|
||||
static inline size_t
|
||||
my_strlen(const char* s) {
|
||||
size_t len = 0;
|
||||
while (*s++) len++;
|
||||
return len;
|
||||
}
|
||||
|
||||
static inline int
|
||||
my_strcmp(const char* a, const char* b) {
|
||||
for (;;) {
|
||||
if (*a < *b)
|
||||
return -1;
|
||||
else if (*a > *b)
|
||||
return 1;
|
||||
else if (*a == 0)
|
||||
return 0;
|
||||
a++;
|
||||
b++;
|
||||
}
|
||||
}
|
||||
|
||||
static inline int
|
||||
my_strncmp(const char* a, const char* b, size_t len) {
|
||||
for (size_t i = 0; i < len; ++i) {
|
||||
if (*a < *b)
|
||||
return -1;
|
||||
else if (*a > *b)
|
||||
return 1;
|
||||
else if (*a == 0)
|
||||
return 0;
|
||||
a++;
|
||||
b++;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Parse a non-negative integer.
|
||||
// result: (output) the resulting non-negative integer
|
||||
// s: a NUL terminated string
|
||||
// Return true iff successful.
|
||||
static inline bool
|
||||
my_strtoui(int* result, const char* s) {
|
||||
if (*s == 0)
|
||||
return false;
|
||||
int r = 0;
|
||||
for (;; s++) {
|
||||
if (*s == 0)
|
||||
break;
|
||||
const int old_r = r;
|
||||
r *= 10;
|
||||
if (*s < '0' || *s > '9')
|
||||
return false;
|
||||
r += *s - '0';
|
||||
if (r < old_r)
|
||||
return false;
|
||||
}
|
||||
|
||||
*result = r;
|
||||
return true;
|
||||
}
|
||||
|
||||
// Return the length of the given, non-negative integer when expressed in base
|
||||
// 10.
|
||||
static inline unsigned
|
||||
my_int_len(int i) {
|
||||
if (!i)
|
||||
return 1;
|
||||
|
||||
int len = 0;
|
||||
while (i) {
|
||||
len++;
|
||||
i /= 10;
|
||||
}
|
||||
|
||||
return len;
|
||||
}
|
||||
|
||||
// Convert a non-negative integer to a string
|
||||
// output: (output) the resulting string is written here. This buffer must be
|
||||
// large enough to hold the resulting string. Call |my_int_len| to get the
|
||||
// required length.
|
||||
// i: the non-negative integer to serialise.
|
||||
// i_len: the length of the integer in base 10 (see |my_int_len|).
|
||||
static inline void
|
||||
my_itos(char* output, int i, unsigned i_len) {
|
||||
for (unsigned index = i_len; index; --index, i /= 10)
|
||||
output[index - 1] = '0' + (i % 10);
|
||||
}
|
||||
|
||||
static inline const char*
|
||||
my_strchr(const char* haystack, char needle) {
|
||||
while (*haystack && *haystack != needle)
|
||||
haystack++;
|
||||
if (*haystack == needle)
|
||||
return haystack;
|
||||
return (const char*) 0;
|
||||
}
|
||||
|
||||
// Read a hex value
|
||||
// result: (output) the resulting value
|
||||
// s: a string
|
||||
// Returns a pointer to the first invalid charactor.
|
||||
static inline const char*
|
||||
my_read_hex_ptr(uintptr_t* result, const char* s) {
|
||||
uintptr_t r = 0;
|
||||
|
||||
for (;; ++s) {
|
||||
if (*s >= '0' && *s <= '9') {
|
||||
r <<= 4;
|
||||
r += *s - '0';
|
||||
} else if (*s >= 'a' && *s <= 'f') {
|
||||
r <<= 4;
|
||||
r += (*s - 'a') + 10;
|
||||
} else if (*s >= 'A' && *s <= 'F') {
|
||||
r <<= 4;
|
||||
r += (*s - 'A') + 10;
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
*result = r;
|
||||
return s;
|
||||
}
|
||||
|
||||
static inline void
|
||||
my_memset(void* ip, char c, size_t len) {
|
||||
char* p = (char *) ip;
|
||||
while (len--)
|
||||
*p++ = c;
|
||||
}
|
||||
|
||||
} // extern "C"
|
||||
|
||||
#endif // CLIENT_LINUX_LINUX_LIBC_SUPPORT_H_
|
||||
153
src/common/linux/linux_libc_support_unittest.cc
Normal file
153
src/common/linux/linux_libc_support_unittest.cc
Normal file
|
|
@ -0,0 +1,153 @@
|
|||
// Copyright (c) 2009, Google Inc.
|
||||
// All rights reserved.
|
||||
//
|
||||
// Redistribution and use in source and binary forms, with or without
|
||||
// modification, are permitted provided that the following conditions are
|
||||
// met:
|
||||
//
|
||||
// * Redistributions of source code must retain the above copyright
|
||||
// notice, this list of conditions and the following disclaimer.
|
||||
// * Redistributions in binary form must reproduce the above
|
||||
// copyright notice, this list of conditions and the following disclaimer
|
||||
// in the documentation and/or other materials provided with the
|
||||
// distribution.
|
||||
// * Neither the name of Google Inc. nor the names of its
|
||||
// contributors may be used to endorse or promote products derived from
|
||||
// this software without specific prior written permission.
|
||||
//
|
||||
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
|
||||
#include "breakpad/linux/linux_libc_support.h"
|
||||
#include "testing/gtest/include/gtest/gtest.h"
|
||||
|
||||
namespace {
|
||||
typedef testing::Test LinuxLibcSupportTest;
|
||||
}
|
||||
|
||||
TEST(LinuxLibcSupportTest, strlen) {
|
||||
static const char* test_data[] = { "", "a", "aa", "aaa", "aabc", NULL };
|
||||
for (unsigned i = 0; ; ++i) {
|
||||
if (!test_data[i])
|
||||
break;
|
||||
ASSERT_EQ(strlen(test_data[i]), my_strlen(test_data[i]));
|
||||
}
|
||||
}
|
||||
|
||||
TEST(LinuxLibcSupportTest, strcmp) {
|
||||
static const char* test_data[] = {
|
||||
"", "",
|
||||
"a", "",
|
||||
"", "a",
|
||||
"a", "b",
|
||||
"a", "a",
|
||||
"ab", "aa",
|
||||
"abc", "ab",
|
||||
"abc", "abc",
|
||||
NULL,
|
||||
};
|
||||
|
||||
for (unsigned i = 0; ; ++i) {
|
||||
if (!test_data[i*2])
|
||||
break;
|
||||
ASSERT_EQ(my_strcmp(test_data[i*2], test_data[i*2 + 1]),
|
||||
strcmp(test_data[i*2], test_data[i*2 + 1]));
|
||||
}
|
||||
}
|
||||
|
||||
TEST(LinuxLibcSupportTest, strtoui) {
|
||||
int result;
|
||||
|
||||
ASSERT_FALSE(my_strtoui(&result, ""));
|
||||
ASSERT_FALSE(my_strtoui(&result, "-1"));
|
||||
ASSERT_FALSE(my_strtoui(&result, "-"));
|
||||
ASSERT_FALSE(my_strtoui(&result, "a"));
|
||||
ASSERT_FALSE(my_strtoui(&result, "23472893472938472987987398472398"));
|
||||
|
||||
ASSERT_TRUE(my_strtoui(&result, "0"));
|
||||
ASSERT_EQ(result, 0);
|
||||
ASSERT_TRUE(my_strtoui(&result, "1"));
|
||||
ASSERT_EQ(result, 1);
|
||||
ASSERT_TRUE(my_strtoui(&result, "12"));
|
||||
ASSERT_EQ(result, 12);
|
||||
ASSERT_TRUE(my_strtoui(&result, "123"));
|
||||
ASSERT_EQ(result, 123);
|
||||
ASSERT_TRUE(my_strtoui(&result, "0123"));
|
||||
ASSERT_EQ(result, 123);
|
||||
}
|
||||
|
||||
TEST(LinuxLibcSupportTest, int_len) {
|
||||
ASSERT_EQ(my_int_len(0), 1);
|
||||
ASSERT_EQ(my_int_len(2), 1);
|
||||
ASSERT_EQ(my_int_len(5), 1);
|
||||
ASSERT_EQ(my_int_len(9), 1);
|
||||
ASSERT_EQ(my_int_len(10), 2);
|
||||
ASSERT_EQ(my_int_len(99), 2);
|
||||
ASSERT_EQ(my_int_len(100), 3);
|
||||
ASSERT_EQ(my_int_len(101), 3);
|
||||
ASSERT_EQ(my_int_len(1000), 4);
|
||||
}
|
||||
|
||||
TEST(LinuxLibcSupportTest, itos) {
|
||||
char buf[10];
|
||||
|
||||
my_itos(buf, 0, 1);
|
||||
ASSERT_EQ(0, memcmp(buf, "0", 1));
|
||||
|
||||
my_itos(buf, 1, 1);
|
||||
ASSERT_EQ(0, memcmp(buf, "1", 1));
|
||||
|
||||
my_itos(buf, 10, 2);
|
||||
ASSERT_EQ(0, memcmp(buf, "10", 2));
|
||||
|
||||
my_itos(buf, 63, 2);
|
||||
ASSERT_EQ(0, memcmp(buf, "63", 2));
|
||||
|
||||
my_itos(buf, 101, 3);
|
||||
ASSERT_EQ(0, memcmp(buf, "101", 2));
|
||||
}
|
||||
|
||||
TEST(LinuxLibcSupportTest, strchr) {
|
||||
ASSERT_EQ(NULL, my_strchr("abc", 'd'));
|
||||
ASSERT_EQ(NULL, my_strchr("", 'd'));
|
||||
ASSERT_EQ(NULL, my_strchr("efghi", 'd'));
|
||||
|
||||
ASSERT_TRUE(my_strchr("a", 'a'));
|
||||
ASSERT_TRUE(my_strchr("abc", 'a'));
|
||||
ASSERT_TRUE(my_strchr("bcda", 'a'));
|
||||
ASSERT_TRUE(my_strchr("sdfasdf", 'a'));
|
||||
}
|
||||
|
||||
TEST(LinuxLibcSupportTest, read_hex_ptr) {
|
||||
uintptr_t result;
|
||||
const char* last;
|
||||
|
||||
last = my_read_hex_ptr(&result, "");
|
||||
ASSERT_EQ(result, 0);
|
||||
ASSERT_EQ(*last, 0);
|
||||
|
||||
last = my_read_hex_ptr(&result, "0");
|
||||
ASSERT_EQ(result, 0);
|
||||
ASSERT_EQ(*last, 0);
|
||||
|
||||
last = my_read_hex_ptr(&result, "0123");
|
||||
ASSERT_EQ(result, 0x123);
|
||||
ASSERT_EQ(*last, 0);
|
||||
|
||||
last = my_read_hex_ptr(&result, "0123a");
|
||||
ASSERT_EQ(result, 0x123a);
|
||||
ASSERT_EQ(*last, 0);
|
||||
|
||||
last = my_read_hex_ptr(&result, "0123a-");
|
||||
ASSERT_EQ(result, 0x123a);
|
||||
ASSERT_EQ(*last, '-');
|
||||
}
|
||||
2800
src/common/linux/linux_syscall_support.h
Normal file
2800
src/common/linux/linux_syscall_support.h
Normal file
File diff suppressed because it is too large
Load diff
176
src/common/linux/memory.h
Normal file
176
src/common/linux/memory.h
Normal file
|
|
@ -0,0 +1,176 @@
|
|||
// Copyright (c) 2009, Google Inc.
|
||||
// All rights reserved.
|
||||
//
|
||||
// Redistribution and use in source and binary forms, with or without
|
||||
// modification, are permitted provided that the following conditions are
|
||||
// met:
|
||||
//
|
||||
// * Redistributions of source code must retain the above copyright
|
||||
// notice, this list of conditions and the following disclaimer.
|
||||
// * Redistributions in binary form must reproduce the above
|
||||
// copyright notice, this list of conditions and the following disclaimer
|
||||
// in the documentation and/or other materials provided with the
|
||||
// distribution.
|
||||
// * Neither the name of Google Inc. nor the names of its
|
||||
// contributors may be used to endorse or promote products derived from
|
||||
// this software without specific prior written permission.
|
||||
//
|
||||
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
|
||||
#ifndef CLIENT_LINUX_HANDLER_MEMORY_H_
|
||||
#define CLIENT_LINUX_HANDLER_MEMORY_H_
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/mman.h>
|
||||
|
||||
#include "common/linux/linux_syscall_support.h"
|
||||
|
||||
namespace google_breakpad {
|
||||
|
||||
// This is very simple allocator which fetches pages from the kernel directly.
|
||||
// Thus, it can be used even when the heap may be corrupted.
|
||||
//
|
||||
// There is no free operation. The pages are only freed when the object is
|
||||
// destroyed.
|
||||
class PageAllocator {
|
||||
public:
|
||||
PageAllocator()
|
||||
: page_size_(getpagesize()),
|
||||
last_(NULL),
|
||||
current_page_(NULL),
|
||||
page_offset_(0) {
|
||||
}
|
||||
|
||||
~PageAllocator() {
|
||||
FreeAll();
|
||||
}
|
||||
|
||||
void *Alloc(unsigned bytes) {
|
||||
if (!bytes)
|
||||
return NULL;
|
||||
|
||||
if (current_page_ && page_size_ - page_offset_ >= bytes) {
|
||||
uint8_t *const ret = current_page_ + page_offset_;
|
||||
page_offset_ += bytes;
|
||||
if (page_offset_ == page_size_) {
|
||||
page_offset_ = 0;
|
||||
current_page_ = NULL;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
const unsigned pages =
|
||||
(bytes + sizeof(PageHeader) + page_size_ - 1) / page_size_;
|
||||
uint8_t *const ret = GetNPages(pages);
|
||||
if (!ret)
|
||||
return NULL;
|
||||
|
||||
page_offset_ = (page_size_ - (page_size_ * pages - (bytes + sizeof(PageHeader)))) % page_size_;
|
||||
current_page_ = page_offset_ ? ret + page_size_ * (pages - 1) : NULL;
|
||||
|
||||
return ret + sizeof(PageHeader);
|
||||
}
|
||||
|
||||
private:
|
||||
uint8_t *GetNPages(unsigned num_pages) {
|
||||
void *a = sys_mmap2(NULL, page_size_ * num_pages, PROT_READ | PROT_WRITE,
|
||||
MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
|
||||
if (a == MAP_FAILED)
|
||||
return NULL;
|
||||
|
||||
struct PageHeader *header = reinterpret_cast<PageHeader*>(a);
|
||||
header->next = last_;
|
||||
header->num_pages = num_pages;
|
||||
last_ = header;
|
||||
|
||||
return reinterpret_cast<uint8_t*>(a);
|
||||
}
|
||||
|
||||
void FreeAll() {
|
||||
PageHeader *next;
|
||||
|
||||
for (PageHeader *cur = last_; cur; cur = next) {
|
||||
next = cur->next;
|
||||
sys_munmap(cur, cur->num_pages * page_size_);
|
||||
}
|
||||
}
|
||||
|
||||
struct PageHeader {
|
||||
PageHeader *next; // pointer to the start of the next set of pages.
|
||||
unsigned num_pages; // the number of pages in this set.
|
||||
};
|
||||
|
||||
const unsigned page_size_;
|
||||
PageHeader *last_;
|
||||
uint8_t *current_page_;
|
||||
unsigned page_offset_;
|
||||
};
|
||||
|
||||
// A wasteful vector is like a normal std::vector, except that it's very much
|
||||
// simplier and it allocates memory from a PageAllocator. It's wasteful
|
||||
// because, when resizing, it always allocates a whole new array since the
|
||||
// PageAllocator doesn't support realloc.
|
||||
template<class T>
|
||||
class wasteful_vector {
|
||||
public:
|
||||
wasteful_vector(PageAllocator *allocator, unsigned size_hint = 16)
|
||||
: allocator_(allocator),
|
||||
a_((T*) allocator->Alloc(sizeof(T) * size_hint)),
|
||||
allocated_(size_hint),
|
||||
used_(0) {
|
||||
}
|
||||
|
||||
void push_back(const T& new_element) {
|
||||
if (used_ == allocated_)
|
||||
Realloc(allocated_ * 2);
|
||||
a_[used_++] = new_element;
|
||||
}
|
||||
|
||||
size_t size() const {
|
||||
return used_;
|
||||
}
|
||||
|
||||
T& operator[](size_t index) {
|
||||
return a_[index];
|
||||
}
|
||||
|
||||
const T& operator[](size_t index) const {
|
||||
return a_[index];
|
||||
}
|
||||
|
||||
private:
|
||||
void Realloc(unsigned new_size) {
|
||||
T *new_array =
|
||||
reinterpret_cast<T*>(allocator_->Alloc(sizeof(T) * new_size));
|
||||
memcpy(new_array, a_, used_ * sizeof(T));
|
||||
a_ = new_array;
|
||||
allocated_ = new_size;
|
||||
}
|
||||
|
||||
PageAllocator *const allocator_;
|
||||
T *a_; // pointer to an array of |allocated_| elements.
|
||||
unsigned allocated_; // size of |a_|, in elements.
|
||||
unsigned used_; // number of used slots in |a_|.
|
||||
};
|
||||
|
||||
} // namespace google_breakpad
|
||||
|
||||
inline void* operator new(size_t nbytes,
|
||||
google_breakpad::PageAllocator& allocator) {
|
||||
return allocator.Alloc(nbytes);
|
||||
}
|
||||
|
||||
#endif // CLIENT_LINUX_HANDLER_MEMORY_H_
|
||||
84
src/common/linux/memory_unittest.cc
Normal file
84
src/common/linux/memory_unittest.cc
Normal file
|
|
@ -0,0 +1,84 @@
|
|||
// Copyright (c) 2009, Google Inc.
|
||||
// All rights reserved.
|
||||
//
|
||||
// Redistribution and use in source and binary forms, with or without
|
||||
// modification, are permitted provided that the following conditions are
|
||||
// met:
|
||||
//
|
||||
// * Redistributions of source code must retain the above copyright
|
||||
// notice, this list of conditions and the following disclaimer.
|
||||
// * Redistributions in binary form must reproduce the above
|
||||
// copyright notice, this list of conditions and the following disclaimer
|
||||
// in the documentation and/or other materials provided with the
|
||||
// distribution.
|
||||
// * Neither the name of Google Inc. nor the names of its
|
||||
// contributors may be used to endorse or promote products derived from
|
||||
// this software without specific prior written permission.
|
||||
//
|
||||
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
|
||||
#include "breakpad/linux/memory.h"
|
||||
#include "testing/gtest/include/gtest/gtest.h"
|
||||
|
||||
using namespace google_breakpad;
|
||||
|
||||
namespace {
|
||||
typedef testing::Test PageAllocatorTest;
|
||||
}
|
||||
|
||||
TEST(PageAllocatorTest, Setup) {
|
||||
PageAllocator allocator;
|
||||
}
|
||||
|
||||
TEST(PageAllocatorTest, SmallObjects) {
|
||||
PageAllocator allocator;
|
||||
|
||||
for (unsigned i = 1; i < 1024; ++i) {
|
||||
uint8_t *p = reinterpret_cast<uint8_t*>(allocator.Alloc(i));
|
||||
ASSERT_FALSE(p == NULL);
|
||||
memset(p, 0, i);
|
||||
}
|
||||
}
|
||||
|
||||
TEST(PageAllocatorTest, LargeObject) {
|
||||
PageAllocator allocator;
|
||||
|
||||
uint8_t *p = reinterpret_cast<uint8_t*>(allocator.Alloc(10000));
|
||||
ASSERT_FALSE(p == NULL);
|
||||
for (unsigned i = 1; i < 10; ++i) {
|
||||
uint8_t *p = reinterpret_cast<uint8_t*>(allocator.Alloc(i));
|
||||
ASSERT_FALSE(p == NULL);
|
||||
memset(p, 0, i);
|
||||
}
|
||||
}
|
||||
|
||||
namespace {
|
||||
typedef testing::Test WastefulVectorTest;
|
||||
}
|
||||
|
||||
TEST(WastefulVectorTest, Setup) {
|
||||
PageAllocator allocator_;
|
||||
wasteful_vector<int> v(&allocator_);
|
||||
ASSERT_EQ(v.size(), 0u);
|
||||
}
|
||||
|
||||
TEST(WastefulVectorTest, Simple) {
|
||||
PageAllocator allocator_;
|
||||
wasteful_vector<int> v(&allocator_);
|
||||
|
||||
for (unsigned i = 0; i < 256; ++i)
|
||||
v.push_back(i);
|
||||
ASSERT_EQ(v.size(), 256u);
|
||||
for (unsigned i = 0; i < 256; ++i)
|
||||
ASSERT_EQ(v[i], i);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue