llvm-capstone/lldb/unittests/Utility/DataBufferTest.cpp
Jonas Devlieghere fc54427e76
[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
2022-04-05 13:46:37 -07:00

57 lines
2.0 KiB
C++

//===-- DataBufferTest.cpp ------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "gtest/gtest.h"
#include "lldb/Utility/DataBuffer.h"
#include "lldb/Utility/DataBufferHeap.h"
#include "lldb/Utility/DataBufferLLVM.h"
#include "llvm/Support/Casting.h"
#include "llvm/Support/MemoryBuffer.h"
using namespace lldb_private;
using namespace lldb;
TEST(DataBufferTest, RTTI) {
{
DataBufferSP data_buffer_sp = std::make_shared<DataBufferHeap>();
DataBuffer *data_buffer = data_buffer_sp.get();
EXPECT_TRUE(llvm::isa<DataBuffer>(data_buffer));
EXPECT_TRUE(llvm::isa<WritableDataBuffer>(data_buffer));
EXPECT_TRUE(llvm::isa<DataBufferHeap>(data_buffer));
EXPECT_FALSE(llvm::isa<DataBufferLLVM>(data_buffer));
}
{
llvm::StringRef data;
DataBufferSP data_buffer_sp = std::make_shared<DataBufferLLVM>(
llvm::MemoryBuffer::getMemBufferCopy(data));
DataBuffer *data_buffer = data_buffer_sp.get();
EXPECT_TRUE(llvm::isa<DataBuffer>(data_buffer));
EXPECT_TRUE(llvm::isa<DataBufferLLVM>(data_buffer));
EXPECT_FALSE(llvm::isa<WritableDataBuffer>(data_buffer));
EXPECT_FALSE(llvm::isa<WritableDataBufferLLVM>(data_buffer));
EXPECT_FALSE(llvm::isa<DataBufferHeap>(data_buffer));
}
{
DataBufferSP data_buffer_sp = std::make_shared<WritableDataBufferLLVM>(
llvm::WritableMemoryBuffer::getNewMemBuffer(1));
DataBuffer *data_buffer = data_buffer_sp.get();
EXPECT_TRUE(llvm::isa<DataBuffer>(data_buffer));
EXPECT_TRUE(llvm::isa<WritableDataBuffer>(data_buffer));
EXPECT_TRUE(llvm::isa<WritableDataBufferLLVM>(data_buffer));
EXPECT_FALSE(llvm::isa<DataBufferLLVM>(data_buffer));
EXPECT_FALSE(llvm::isa<DataBufferHeap>(data_buffer));
}
}