aboutsummaryrefslogtreecommitdiff
path: root/lldb/source/Host/common/FileSystem.cpp
diff options
context:
space:
mode:
authorJonas Devlieghere <jonas@devlieghere.com>2022-04-01 15:59:18 -0700
committerJonas Devlieghere <jonas@devlieghere.com>2022-04-05 13:46:37 -0700
commitfc54427e76c89e567390dd4a1d64a65568f4ec26 (patch)
treed785373d3b04cb1cee280af9da4d7f510dbeaef0 /lldb/source/Host/common/FileSystem.cpp
parent4169650537622ef278b4d62ef5fb37aeb0ee9f4e (diff)
downloadllvm-fc54427e76c89e567390dd4a1d64a65568f4ec26.zip
llvm-fc54427e76c89e567390dd4a1d64a65568f4ec26.tar.gz
llvm-fc54427e76c89e567390dd4a1d64a65568f4ec26.tar.bz2
[lldb] Refactor DataBuffer so we can map files as read-only
Currently, all data buffers are assumed to be writable. This is a problem on macOS where it's not allowed to load unsigned binaries in memory as writable. To be more precise, MAP_RESILIENT_CODESIGN and MAP_RESILIENT_MEDIA need to be set for mapped (unsigned) binaries on our platform. Binaries are mapped through FileSystem::CreateDataBuffer which returns a DataBufferLLVM. The latter is backed by a llvm::WritableMemoryBuffer because every DataBuffer in LLDB is considered to be writable. In order to use a read-only llvm::MemoryBuffer I had to split our abstraction around it. This patch distinguishes between a DataBuffer (read-only) and WritableDataBuffer (read-write) and updates LLDB to use the appropriate one. rdar://74890607 Differential revision: https://reviews.llvm.org/D122856
Diffstat (limited to 'lldb/source/Host/common/FileSystem.cpp')
-rw-r--r--lldb/source/Host/common/FileSystem.cpp45
1 files changed, 36 insertions, 9 deletions
diff --git a/lldb/source/Host/common/FileSystem.cpp b/lldb/source/Host/common/FileSystem.cpp
index 683b671..501062f 100644
--- a/lldb/source/Host/common/FileSystem.cpp
+++ b/lldb/source/Host/common/FileSystem.cpp
@@ -273,27 +273,54 @@ void FileSystem::Resolve(FileSpec &file_spec) {
file_spec.SetIsResolved(true);
}
-std::shared_ptr<DataBuffer>
-FileSystem::CreateDataBuffer(const llvm::Twine &path, uint64_t size,
- uint64_t offset) {
- const bool is_volatile = !IsLocal(path);
- std::unique_ptr<llvm::WritableMemoryBuffer> buffer;
+template <typename T>
+static std::unique_ptr<T> GetMemoryBuffer(const llvm::Twine &path,
+ uint64_t size, uint64_t offset,
+ bool is_volatile) {
+ std::unique_ptr<T> buffer;
if (size == 0) {
- auto buffer_or_error =
- llvm::WritableMemoryBuffer::getFile(path, is_volatile);
+ auto buffer_or_error = T::getFile(path, is_volatile);
if (!buffer_or_error)
return nullptr;
buffer = std::move(*buffer_or_error);
} else {
- auto buffer_or_error = llvm::WritableMemoryBuffer::getFileSlice(
- path, size, offset, is_volatile);
+ auto buffer_or_error = T::getFileSlice(path, size, offset, is_volatile);
if (!buffer_or_error)
return nullptr;
buffer = std::move(*buffer_or_error);
}
+ return buffer;
+}
+
+std::shared_ptr<WritableDataBuffer>
+FileSystem::CreateWritableDataBuffer(const llvm::Twine &path, uint64_t size,
+ uint64_t offset) {
+ const bool is_volatile = !IsLocal(path);
+ auto buffer = GetMemoryBuffer<llvm::WritableMemoryBuffer>(path, size, offset,
+ is_volatile);
+ if (!buffer)
+ return {};
+ return std::shared_ptr<WritableDataBufferLLVM>(
+ new WritableDataBufferLLVM(std::move(buffer)));
+}
+
+std::shared_ptr<DataBuffer>
+FileSystem::CreateDataBuffer(const llvm::Twine &path, uint64_t size,
+ uint64_t offset) {
+ const bool is_volatile = !IsLocal(path);
+ auto buffer =
+ GetMemoryBuffer<llvm::MemoryBuffer>(path, size, offset, is_volatile);
+ if (!buffer)
+ return {};
return std::shared_ptr<DataBufferLLVM>(new DataBufferLLVM(std::move(buffer)));
}
+std::shared_ptr<WritableDataBuffer>
+FileSystem::CreateWritableDataBuffer(const FileSpec &file_spec, uint64_t size,
+ uint64_t offset) {
+ return CreateWritableDataBuffer(file_spec.GetPath(), size, offset);
+}
+
std::shared_ptr<DataBuffer>
FileSystem::CreateDataBuffer(const FileSpec &file_spec, uint64_t size,
uint64_t offset) {