aboutsummaryrefslogtreecommitdiff
path: root/src/common/linux
diff options
context:
space:
mode:
authorbenchan@chromium.org <benchan@chromium.org@4c0a9323-5329-0410-9bdc-e9ce6186880e>2011-12-16 16:42:59 +0000
committerbenchan@chromium.org <benchan@chromium.org@4c0a9323-5329-0410-9bdc-e9ce6186880e>2011-12-16 16:42:59 +0000
commitf044345c23c0324942b7375e7a09558267651523 (patch)
tree55a0847ef9681e390bad9c97458c352cc1da75ed /src/common/linux
parentFix ContextDeathTest.X86BadFlags unit test on Mac OS X. (diff)
downloadbreakpad-f044345c23c0324942b7375e7a09558267651523.tar.xz
Refactor code in preparation of merging with the fork in Chromium OS.
This patch is part of a bigger patch that helps merging the breakpad code with the modified version in Chromium OS. Specifically, this patch makes the following changes: 1. Add a MemoryRange class for encapsulating and checking read access to a contiguous range of memory. 2. Add a MemoryMappedFile class for mapping a file into memory for read-only access. 3. Refactor other source code to use MemoryMappedFile. BUG=455 TEST=Tested the following: 1. Build on 32-bit and 64-bit Linux with gcc 4.4.3 and gcc 4.6. 2. Build on Mac OS X 10.6.8 with gcc 4.2 and clang 3.0 (with latest gmock). 3. All unit tests pass. 4. Run minidump-2-core to covnert a minidump file to a core file. Review URL: http://breakpad.appspot.com/332001 git-svn-id: http://google-breakpad.googlecode.com/svn/trunk@895 4c0a9323-5329-0410-9bdc-e9ce6186880e
Diffstat (limited to 'src/common/linux')
-rw-r--r--src/common/linux/file_id.cc26
-rw-r--r--src/common/linux/file_id.h2
-rw-r--r--src/common/linux/memory_mapped_file.cc102
-rw-r--r--src/common/linux/memory_mapped_file.h86
-rw-r--r--src/common/linux/memory_mapped_file_unittest.cc192
5 files changed, 386 insertions, 22 deletions
diff --git a/src/common/linux/file_id.cc b/src/common/linux/file_id.cc
index 13f4f3ad..bffef621 100644
--- a/src/common/linux/file_id.cc
+++ b/src/common/linux/file_id.cc
@@ -37,21 +37,17 @@
#include <arpa/inet.h>
#include <assert.h>
#include <elf.h>
-#include <fcntl.h>
#if defined(__ANDROID__)
#include "client/linux/android_link.h"
#else
#include <link.h>
#endif
-#include <stdio.h>
#include <string.h>
-#include <sys/mman.h>
-#include <sys/stat.h>
-#include <unistd.h>
#include <algorithm>
#include "common/linux/linux_libc_support.h"
+#include "common/linux/memory_mapped_file.h"
#include "third_party/lss/linux_syscall_support.h"
namespace google_breakpad {
@@ -231,7 +227,7 @@ static bool HashElfTextSection(const void *elf_mapped_base,
}
// static
-bool FileID::ElfFileIdentifierFromMappedFile(void* base,
+bool FileID::ElfFileIdentifierFromMappedFile(const void* base,
uint8_t identifier[kMDGUIDSize]) {
// Look for a build id note first.
if (FindElfBuildIDNote(base, identifier))
@@ -242,23 +238,11 @@ bool FileID::ElfFileIdentifierFromMappedFile(void* base,
}
bool FileID::ElfFileIdentifier(uint8_t identifier[kMDGUIDSize]) {
- int fd = open(path_, O_RDONLY);
- if (fd < 0)
- return false;
- struct stat st;
- if (fstat(fd, &st) != 0) {
- close(fd);
- return false;
- }
- void* base = mmap(NULL, st.st_size,
- PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
- close(fd);
- if (base == MAP_FAILED)
+ MemoryMappedFile mapped_file(path_);
+ if (!mapped_file.data()) // Should probably check if size >= ElfW(Ehdr)?
return false;
- bool success = ElfFileIdentifierFromMappedFile(base, identifier);
- munmap(base, st.st_size);
- return success;
+ return ElfFileIdentifierFromMappedFile(mapped_file.data(), identifier);
}
// static
diff --git a/src/common/linux/file_id.h b/src/common/linux/file_id.h
index 4d5b52dc..70a6b3f5 100644
--- a/src/common/linux/file_id.h
+++ b/src/common/linux/file_id.h
@@ -57,7 +57,7 @@ class FileID {
// Load the identifier for the elf file mapped into memory at |base| into
// |identifier|. Return false if the identifier could not be created for the
// file.
- static bool ElfFileIdentifierFromMappedFile(void* base,
+ static bool ElfFileIdentifierFromMappedFile(const void* base,
uint8_t identifier[kMDGUIDSize]);
// Convert the |identifier| data to a NULL terminated string. The string will
diff --git a/src/common/linux/memory_mapped_file.cc b/src/common/linux/memory_mapped_file.cc
new file mode 100644
index 00000000..6f450bd3
--- /dev/null
+++ b/src/common/linux/memory_mapped_file.cc
@@ -0,0 +1,102 @@
+// Copyright (c) 2011, 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.
+
+// memory_mapped_file.cc: Implement google_breakpad::MemoryMappedFile.
+// See memory_mapped_file.h for details.
+
+#include "common/linux/memory_mapped_file.h"
+
+#include <fcntl.h>
+#include <sys/mman.h>
+#include <unistd.h>
+
+#include "common/memory_range.h"
+#include "third_party/lss/linux_syscall_support.h"
+
+namespace google_breakpad {
+
+MemoryMappedFile::MemoryMappedFile() {}
+
+MemoryMappedFile::MemoryMappedFile(const char* path) {
+ Map(path);
+}
+
+MemoryMappedFile::~MemoryMappedFile() {
+ Unmap();
+}
+
+bool MemoryMappedFile::Map(const char* path) {
+ Unmap();
+
+ int fd = sys_open(path, O_RDONLY, 0);
+ if (fd == -1) {
+ return false;
+ }
+
+#if defined(__x86_64__)
+ struct kernel_stat st;
+ if (sys_fstat(fd, &st) == -1 || st.st_size < 0) {
+#else
+ struct kernel_stat64 st;
+ if (sys_fstat64(fd, &st) == -1 || st.st_size < 0) {
+#endif
+ sys_close(fd);
+ return false;
+ }
+
+ // If the file size is zero, simply use an empty MemoryRange and return
+ // true. Don't bother to call mmap() even though mmap() can handle an
+ // empty file on some platforms.
+ if (st.st_size == 0) {
+ sys_close(fd);
+ return true;
+ }
+
+#if defined(__x86_64__)
+ void* data = sys_mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
+#else
+ void* data = sys_mmap2(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
+#endif
+ sys_close(fd);
+ if (data == MAP_FAILED) {
+ return false;
+ }
+
+ content_.Set(data, st.st_size);
+ return true;
+}
+
+void MemoryMappedFile::Unmap() {
+ if (content_.data()) {
+ sys_munmap(const_cast<u_int8_t*>(content_.data()), content_.length());
+ content_.Set(NULL, 0);
+ }
+}
+
+} // namespace google_breakpad
diff --git a/src/common/linux/memory_mapped_file.h b/src/common/linux/memory_mapped_file.h
new file mode 100644
index 00000000..6abd5b0c
--- /dev/null
+++ b/src/common/linux/memory_mapped_file.h
@@ -0,0 +1,86 @@
+// Copyright (c) 2011, 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.
+
+// memory_mapped_file.h: Define the google_breakpad::MemoryMappedFile
+// class, which maps a file into memory for read-only access.
+
+#ifndef COMMON_LINUX_MEMORY_MAPPED_FILE_H_
+#define COMMON_LINUX_MEMORY_MAPPED_FILE_H_
+
+#include "common/basictypes.h"
+#include "common/memory_range.h"
+
+namespace google_breakpad {
+
+// A utility class for mapping a file into memory for read-only access of
+// the file content. Its implementation avoids calling into libc functions
+// by directly making system calls for open, close, mmap, and munmap.
+class MemoryMappedFile {
+ public:
+ MemoryMappedFile();
+
+ // Constructor that calls Map() to map a file at |path| into memory.
+ // If Map() fails, the object behaves as if it is default constructed.
+ explicit MemoryMappedFile(const char* path);
+
+ ~MemoryMappedFile();
+
+ // Maps a file at |path| into memory, which can then be accessed via
+ // content() as a MemoryRange object or via data(), and returns true on
+ // success. Mapping an empty file will succeed but with data() and size()
+ // returning NULL and 0, respectively. An existing mapping is unmapped
+ // before a new mapping is created.
+ bool Map(const char* path);
+
+ // Unmaps the memory for the mapped file. It's a no-op if no file is
+ // mapped.
+ void Unmap();
+
+ // Returns a MemoryRange object that covers the memory for the mapped
+ // file. The MemoryRange object is empty if no file is mapped.
+ const MemoryRange& content() const { return content_; }
+
+ // Returns a pointer to the beginning of the memory for the mapped file.
+ // or NULL if no file is mapped or the mapped file is empty.
+ const void* data() const { return content_.data(); }
+
+ // Returns the size in bytes of the mapped file, or zero if no file
+ // is mapped.
+ size_t size() const { return content_.length(); }
+
+ private:
+ // Mapped file content as a MemoryRange object.
+ MemoryRange content_;
+
+ DISALLOW_COPY_AND_ASSIGN(MemoryMappedFile);
+};
+
+} // namespace google_breakpad
+
+#endif // COMMON_LINUX_MEMORY_MAPPED_FILE_H_
diff --git a/src/common/linux/memory_mapped_file_unittest.cc b/src/common/linux/memory_mapped_file_unittest.cc
new file mode 100644
index 00000000..8a3129fb
--- /dev/null
+++ b/src/common/linux/memory_mapped_file_unittest.cc
@@ -0,0 +1,192 @@
+// Copyright (c) 2011, 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.
+
+// memory_mapped_file_unittest.cc:
+// Unit tests for google_breakpad::MemoryMappedFile.
+
+#include <fcntl.h>
+#include <string.h>
+#include <unistd.h>
+
+#include <string>
+
+#include "breakpad_googletest_includes.h"
+#include "common/linux/eintr_wrapper.h"
+#include "common/linux/memory_mapped_file.h"
+#include "common/tests/auto_tempdir.h"
+
+using google_breakpad::AutoTempDir;
+using google_breakpad::MemoryMappedFile;
+using std::string;
+
+namespace {
+
+bool WriteFile(const string& path, const void* buffer, size_t buffer_size) {
+ int fd =
+ HANDLE_EINTR(open(path.c_str(), O_CREAT | O_TRUNC | O_WRONLY, S_IRWXU));
+ if (fd == -1) {
+ perror("open");
+ return false;
+ }
+
+ bool ok = true;
+ if (buffer && buffer_size > 0) {
+ if (HANDLE_EINTR(write(fd, buffer, buffer_size) != buffer_size)) {
+ perror("write");
+ ok = false;
+ }
+ }
+ close(fd);
+ return ok;
+}
+
+class MemoryMappedFileTest : public testing::Test {
+ protected:
+ void ExpectNoMappedData(const MemoryMappedFile& mapped_file) {
+ EXPECT_TRUE(mapped_file.content().IsEmpty());
+ EXPECT_TRUE(mapped_file.data() == NULL);
+ EXPECT_EQ(0, mapped_file.size());
+ }
+};
+
+} // namespace
+
+TEST_F(MemoryMappedFileTest, DefaultConstructor) {
+ MemoryMappedFile mapped_file;
+ ExpectNoMappedData(mapped_file);
+}
+
+TEST_F(MemoryMappedFileTest, UnmapWithoutMap) {
+ MemoryMappedFile mapped_file;
+ mapped_file.Unmap();
+}
+
+TEST_F(MemoryMappedFileTest, MapNonexistentFile) {
+ {
+ MemoryMappedFile mapped_file("nonexistent-file");
+ ExpectNoMappedData(mapped_file);
+ }
+ {
+ MemoryMappedFile mapped_file;
+ EXPECT_FALSE(mapped_file.Map("nonexistent-file"));
+ ExpectNoMappedData(mapped_file);
+ }
+}
+
+TEST_F(MemoryMappedFileTest, MapEmptyFile) {
+ AutoTempDir temp_dir;
+ string test_file = temp_dir.path() + "/empty_file";
+ ASSERT_TRUE(WriteFile(test_file, NULL, 0));
+
+ {
+ MemoryMappedFile mapped_file(test_file.c_str());
+ ExpectNoMappedData(mapped_file);
+ }
+ {
+ MemoryMappedFile mapped_file;
+ EXPECT_TRUE(mapped_file.Map(test_file.c_str()));
+ ExpectNoMappedData(mapped_file);
+ }
+}
+
+TEST_F(MemoryMappedFileTest, MapNonEmptyFile) {
+ char data[256];
+ size_t data_size = sizeof(data);
+ for (size_t i = 0; i < data_size; ++i) {
+ data[i] = i;
+ }
+
+ AutoTempDir temp_dir;
+ string test_file = temp_dir.path() + "/test_file";
+ ASSERT_TRUE(WriteFile(test_file, data, data_size));
+
+ {
+ MemoryMappedFile mapped_file(test_file.c_str());
+ EXPECT_FALSE(mapped_file.content().IsEmpty());
+ EXPECT_TRUE(mapped_file.data() != NULL);
+ EXPECT_EQ(data_size, mapped_file.size());
+ EXPECT_EQ(0, memcmp(data, mapped_file.data(), data_size));
+ }
+ {
+ MemoryMappedFile mapped_file;
+ EXPECT_TRUE(mapped_file.Map(test_file.c_str()));
+ EXPECT_FALSE(mapped_file.content().IsEmpty());
+ EXPECT_TRUE(mapped_file.data() != NULL);
+ EXPECT_EQ(data_size, mapped_file.size());
+ EXPECT_EQ(0, memcmp(data, mapped_file.data(), data_size));
+ }
+}
+
+TEST_F(MemoryMappedFileTest, RemapAfterMap) {
+ char data1[256];
+ size_t data1_size = sizeof(data1);
+ for (size_t i = 0; i < data1_size; ++i) {
+ data1[i] = i;
+ }
+
+ char data2[50];
+ size_t data2_size = sizeof(data2);
+ for (size_t i = 0; i < data2_size; ++i) {
+ data2[i] = 255 - i;
+ }
+
+ AutoTempDir temp_dir;
+ string test_file1 = temp_dir.path() + "/test_file1";
+ string test_file2 = temp_dir.path() + "/test_file2";
+ ASSERT_TRUE(WriteFile(test_file1, data1, data1_size));
+ ASSERT_TRUE(WriteFile(test_file2, data2, data2_size));
+
+ {
+ MemoryMappedFile mapped_file(test_file1.c_str());
+ EXPECT_FALSE(mapped_file.content().IsEmpty());
+ EXPECT_TRUE(mapped_file.data() != NULL);
+ EXPECT_EQ(data1_size, mapped_file.size());
+ EXPECT_EQ(0, memcmp(data1, mapped_file.data(), data1_size));
+
+ mapped_file.Map(test_file2.c_str());
+ EXPECT_FALSE(mapped_file.content().IsEmpty());
+ EXPECT_TRUE(mapped_file.data() != NULL);
+ EXPECT_EQ(data2_size, mapped_file.size());
+ EXPECT_EQ(0, memcmp(data2, mapped_file.data(), data2_size));
+ }
+ {
+ MemoryMappedFile mapped_file;
+ EXPECT_TRUE(mapped_file.Map(test_file1.c_str()));
+ EXPECT_FALSE(mapped_file.content().IsEmpty());
+ EXPECT_TRUE(mapped_file.data() != NULL);
+ EXPECT_EQ(data1_size, mapped_file.size());
+ EXPECT_EQ(0, memcmp(data1, mapped_file.data(), data1_size));
+
+ mapped_file.Map(test_file2.c_str());
+ EXPECT_FALSE(mapped_file.content().IsEmpty());
+ EXPECT_TRUE(mapped_file.data() != NULL);
+ EXPECT_EQ(data2_size, mapped_file.size());
+ EXPECT_EQ(0, memcmp(data2, mapped_file.data(), data2_size));
+ }
+}