summaryrefslogtreecommitdiff
path: root/source/Core/DataBufferMemoryMap.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'source/Core/DataBufferMemoryMap.cpp')
-rw-r--r--source/Core/DataBufferMemoryMap.cpp66
1 files changed, 46 insertions, 20 deletions
diff --git a/source/Core/DataBufferMemoryMap.cpp b/source/Core/DataBufferMemoryMap.cpp
index b3211b35af85..48fe9e58d07d 100644
--- a/source/Core/DataBufferMemoryMap.cpp
+++ b/source/Core/DataBufferMemoryMap.cpp
@@ -7,19 +7,44 @@
//
//===----------------------------------------------------------------------===//
-
-#include <errno.h>
+// C Includes
#include <fcntl.h>
-#include <limits.h>
#include <sys/stat.h>
#ifdef _WIN32
#include "lldb/Host/windows/windows.h"
#else
#include <sys/mman.h>
-#endif
+#define MAP_EXTRA_HOST_READ_FLAGS 0
+
+#if defined (__APPLE__)
+//----------------------------------------------------------------------
+// Newer versions of MacOSX have a flag that will allow us to read from
+// binaries whose code signature is invalid without crashing by using
+// the MAP_RESILIENT_CODESIGN flag. Also if a file from removable media
+// is mapped we can avoid crashing and return zeroes to any pages we try
+// to read if the media becomes unavailable by using the
+// MAP_RESILIENT_MEDIA flag.
+//----------------------------------------------------------------------
+#if defined(MAP_RESILIENT_CODESIGN)
+ #undef MAP_EXTRA_HOST_READ_FLAGS
+ #if defined(MAP_RESILIENT_MEDIA)
+ #define MAP_EXTRA_HOST_READ_FLAGS MAP_RESILIENT_CODESIGN | MAP_RESILIENT_MEDIA
+ #else
+ #define MAP_EXTRA_HOST_READ_FLAGS MAP_RESILIENT_CODESIGN
+ #endif
+#endif // #if defined(MAP_RESILIENT_CODESIGN)
+#endif // #if defined (__APPLE__)
+
+#endif // #else #ifdef _WIN32
+// C++ Includes
+#include <cerrno>
+#include <climits>
+
+// Other libraries and framework includes
#include "llvm/Support/MathExtras.h"
+// Project includes
#include "lldb/Core/DataBufferMemoryMap.h"
#include "lldb/Core/Error.h"
#include "lldb/Host/File.h"
@@ -34,9 +59,9 @@ using namespace lldb_private;
// Default Constructor
//----------------------------------------------------------------------
DataBufferMemoryMap::DataBufferMemoryMap() :
- m_mmap_addr(NULL),
+ m_mmap_addr(nullptr),
m_mmap_size(0),
- m_data(NULL),
+ m_data(nullptr),
m_size(0)
{
}
@@ -51,7 +76,7 @@ DataBufferMemoryMap::~DataBufferMemoryMap()
}
//----------------------------------------------------------------------
-// Return a pointer to the bytes owned by this object, or NULL if
+// Return a pointer to the bytes owned by this object, or nullptr if
// the object contains no bytes.
//----------------------------------------------------------------------
uint8_t *
@@ -61,7 +86,7 @@ DataBufferMemoryMap::GetBytes()
}
//----------------------------------------------------------------------
-// Return a const pointer to the bytes owned by this object, or NULL
+// Return a const pointer to the bytes owned by this object, or nullptr
// if the object contains no bytes.
//----------------------------------------------------------------------
const uint8_t *
@@ -86,7 +111,7 @@ DataBufferMemoryMap::GetByteSize() const
void
DataBufferMemoryMap::Clear()
{
- if (m_mmap_addr != NULL)
+ if (m_mmap_addr != nullptr)
{
Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_MMAP));
if (log)
@@ -97,9 +122,9 @@ DataBufferMemoryMap::Clear()
#else
::munmap((void *)m_mmap_addr, m_mmap_size);
#endif
- m_mmap_addr = NULL;
+ m_mmap_addr = nullptr;
m_mmap_size = 0;
- m_data = NULL;
+ m_data = nullptr;
m_size = 0;
}
}
@@ -118,7 +143,7 @@ DataBufferMemoryMap::MemoryMapFromFileSpec (const FileSpec* filespec,
size_t length,
bool writeable)
{
- if (filespec != NULL)
+ if (filespec != nullptr)
{
Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_MMAP));
if (log)
@@ -150,7 +175,6 @@ DataBufferMemoryMap::MemoryMapFromFileSpec (const FileSpec* filespec,
return 0;
}
-
#ifdef _WIN32
static size_t win32memmapalignment = 0;
void LoadWin32MemMapAlignment ()
@@ -208,8 +232,8 @@ DataBufferMemoryMap::MemoryMapFromFileDescriptor (int fd,
if (length > 0)
{
- HANDLE fileMapping = CreateFileMapping(handle, NULL, writeable ? PAGE_READWRITE : PAGE_READONLY, file_size_high, file_size_low, NULL);
- if (fileMapping != NULL)
+ HANDLE fileMapping = CreateFileMapping(handle, nullptr, writeable ? PAGE_READWRITE : PAGE_READONLY, file_size_high, file_size_low, nullptr);
+ if (fileMapping != nullptr)
{
if (win32memmapalignment == 0) LoadWin32MemMapAlignment();
lldb::offset_t realoffset = offset;
@@ -252,14 +276,16 @@ DataBufferMemoryMap::MemoryMapFromFileDescriptor (int fd,
if (length > 0)
{
int prot = PROT_READ;
+ int flags = MAP_PRIVATE;
if (writeable)
prot |= PROT_WRITE;
+ else
+ flags |= MAP_EXTRA_HOST_READ_FLAGS;
- int flags = MAP_PRIVATE;
if (fd_is_file)
flags |= MAP_FILE;
- m_mmap_addr = (uint8_t *)::mmap(NULL, length, prot, flags, fd, offset);
+ m_mmap_addr = (uint8_t *)::mmap(nullptr, length, prot, flags, fd, offset);
Error error;
if (m_mmap_addr == (void*)-1)
@@ -271,13 +297,13 @@ DataBufferMemoryMap::MemoryMapFromFileDescriptor (int fd,
size_t page_offset = offset % HostInfo::GetPageSize();
if (page_offset != 0)
{
- m_mmap_addr = (uint8_t *)::mmap(NULL, length + page_offset, prot, flags, fd, offset - page_offset);
+ m_mmap_addr = (uint8_t *)::mmap(nullptr, length + page_offset, prot, flags, fd, offset - page_offset);
if (m_mmap_addr == (void*)-1)
{
// Failed to map file
- m_mmap_addr = NULL;
+ m_mmap_addr = nullptr;
}
- else if (m_mmap_addr != NULL)
+ else if (m_mmap_addr != nullptr)
{
// We recovered and were able to memory map
// after we aligned things to page boundaries