forked from OSchip/llvm-project
[Support] Add error handling to sys::Process::getPageSize().
This patch changes the return type of sys::Process::getPageSize to Expected<unsigned> to account for the fact that the underlying syscalls used to obtain the page size may fail (see below). For clients who use the page size as an optimization only this patch adds a new method, getPageSizeEstimate, which calls through to getPageSize but discards any error returned and substitues a "reasonable" page size estimate estimate instead. All existing LLVM clients are updated to call getPageSizeEstimate rather than getPageSize. On Unix, sys::Process::getPageSize is implemented in terms of getpagesize or sysconf, depending on which macros are set. The sysconf call is documented to return -1 on failure. On Darwin getpagesize is implemented in terms of sysconf and may also fail (though the manpage documentation does not mention this). These failures have been observed in practice when highly restrictive sandbox permissions have been applied. Without this patch, the result is that getPageSize returns -1, which wreaks havoc on any subsequent code that was assuming a sane page size value. <rdar://problem/41654857> Reviewers: dblaikie, echristo Subscribers: kristina, llvm-commits Tags: #llvm Differential Revision: https://reviews.llvm.org/D59107 llvm-svn: 360221
This commit is contained in:
parent
83eb9c5b58
commit
e4b4ab6d26
|
@ -146,13 +146,13 @@ private:
|
||||||
std::error_code EC;
|
std::error_code EC;
|
||||||
auto TrampolineBlock =
|
auto TrampolineBlock =
|
||||||
sys::OwningMemoryBlock(sys::Memory::allocateMappedMemory(
|
sys::OwningMemoryBlock(sys::Memory::allocateMappedMemory(
|
||||||
sys::Process::getPageSize(), nullptr,
|
sys::Process::getPageSizeEstimate(), nullptr,
|
||||||
sys::Memory::MF_READ | sys::Memory::MF_WRITE, EC));
|
sys::Memory::MF_READ | sys::Memory::MF_WRITE, EC));
|
||||||
if (EC)
|
if (EC)
|
||||||
return errorCodeToError(EC);
|
return errorCodeToError(EC);
|
||||||
|
|
||||||
unsigned NumTrampolines =
|
unsigned NumTrampolines =
|
||||||
(sys::Process::getPageSize() - ORCABI::PointerSize) /
|
(sys::Process::getPageSizeEstimate() - ORCABI::PointerSize) /
|
||||||
ORCABI::TrampolineSize;
|
ORCABI::TrampolineSize;
|
||||||
|
|
||||||
uint8_t *TrampolineMem = static_cast<uint8_t *>(TrampolineBlock.base());
|
uint8_t *TrampolineMem = static_cast<uint8_t *>(TrampolineBlock.base());
|
||||||
|
|
|
@ -299,13 +299,13 @@ private:
|
||||||
std::error_code EC;
|
std::error_code EC;
|
||||||
auto TrampolineBlock =
|
auto TrampolineBlock =
|
||||||
sys::OwningMemoryBlock(sys::Memory::allocateMappedMemory(
|
sys::OwningMemoryBlock(sys::Memory::allocateMappedMemory(
|
||||||
sys::Process::getPageSize(), nullptr,
|
sys::Process::getPageSizeEstimate(), nullptr,
|
||||||
sys::Memory::MF_READ | sys::Memory::MF_WRITE, EC));
|
sys::Memory::MF_READ | sys::Memory::MF_WRITE, EC));
|
||||||
if (EC)
|
if (EC)
|
||||||
return errorCodeToError(EC);
|
return errorCodeToError(EC);
|
||||||
|
|
||||||
uint32_t NumTrampolines =
|
uint32_t NumTrampolines =
|
||||||
(sys::Process::getPageSize() - TargetT::PointerSize) /
|
(sys::Process::getPageSizeEstimate() - TargetT::PointerSize) /
|
||||||
TargetT::TrampolineSize;
|
TargetT::TrampolineSize;
|
||||||
|
|
||||||
uint8_t *TrampolineMem = static_cast<uint8_t *>(TrampolineBlock.base());
|
uint8_t *TrampolineMem = static_cast<uint8_t *>(TrampolineBlock.base());
|
||||||
|
@ -335,7 +335,7 @@ private:
|
||||||
handleGetRemoteInfo() {
|
handleGetRemoteInfo() {
|
||||||
std::string ProcessTriple = sys::getProcessTriple();
|
std::string ProcessTriple = sys::getProcessTriple();
|
||||||
uint32_t PointerSize = TargetT::PointerSize;
|
uint32_t PointerSize = TargetT::PointerSize;
|
||||||
uint32_t PageSize = sys::Process::getPageSize();
|
uint32_t PageSize = sys::Process::getPageSizeEstimate();
|
||||||
uint32_t TrampolineSize = TargetT::TrampolineSize;
|
uint32_t TrampolineSize = TargetT::TrampolineSize;
|
||||||
uint32_t IndirectStubSize = TargetT::IndirectStubsInfo::StubSize;
|
uint32_t IndirectStubSize = TargetT::IndirectStubsInfo::StubSize;
|
||||||
LLVM_DEBUG(dbgs() << " Remote info:\n"
|
LLVM_DEBUG(dbgs() << " Remote info:\n"
|
||||||
|
|
|
@ -28,6 +28,7 @@
|
||||||
#include "llvm/Support/Allocator.h"
|
#include "llvm/Support/Allocator.h"
|
||||||
#include "llvm/Support/Chrono.h"
|
#include "llvm/Support/Chrono.h"
|
||||||
#include "llvm/Support/DataTypes.h"
|
#include "llvm/Support/DataTypes.h"
|
||||||
|
#include "llvm/Support/Error.h"
|
||||||
#include <system_error>
|
#include <system_error>
|
||||||
|
|
||||||
namespace llvm {
|
namespace llvm {
|
||||||
|
@ -41,7 +42,25 @@ namespace sys {
|
||||||
/// current executing process.
|
/// current executing process.
|
||||||
class Process {
|
class Process {
|
||||||
public:
|
public:
|
||||||
static unsigned getPageSize();
|
/// Get the process's page size.
|
||||||
|
/// This may fail if the underlying syscall returns an error. In most cases,
|
||||||
|
/// page size information is used for optimization, and this error can be
|
||||||
|
/// safely discarded by calling consumeError, and an estimated page size
|
||||||
|
/// substituted instead.
|
||||||
|
static Expected<unsigned> getPageSize();
|
||||||
|
|
||||||
|
/// Get the process's estimated page size.
|
||||||
|
/// This function always succeeds, but if the underlying syscall to determine
|
||||||
|
/// the page size fails then this will silently return an estimated page size.
|
||||||
|
/// The estimated page size is guaranteed to be a power of 2.
|
||||||
|
static unsigned getPageSizeEstimate() {
|
||||||
|
if (auto PageSize = getPageSize())
|
||||||
|
return *PageSize;
|
||||||
|
else {
|
||||||
|
consumeError(PageSize.takeError());
|
||||||
|
return 4096;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Return process memory usage.
|
/// Return process memory usage.
|
||||||
/// This static function will return the total amount of memory allocated
|
/// This static function will return the total amount of memory allocated
|
||||||
|
|
|
@ -194,12 +194,12 @@ InProcessMemoryManager::allocate(const SegmentsRequestMap &Request) {
|
||||||
for (auto &KV : Request) {
|
for (auto &KV : Request) {
|
||||||
auto &Seg = KV.second;
|
auto &Seg = KV.second;
|
||||||
|
|
||||||
if (Seg.getContentAlignment() > sys::Process::getPageSize())
|
if (Seg.getContentAlignment() > sys::Process::getPageSizeEstimate())
|
||||||
return make_error<StringError>("Cannot request higher than page "
|
return make_error<StringError>("Cannot request higher than page "
|
||||||
"alignment",
|
"alignment",
|
||||||
inconvertibleErrorCode());
|
inconvertibleErrorCode());
|
||||||
|
|
||||||
if (sys::Process::getPageSize() % Seg.getContentAlignment() != 0)
|
if (sys::Process::getPageSizeEstimate() % Seg.getContentAlignment() != 0)
|
||||||
return make_error<StringError>("Page size is not a multiple of "
|
return make_error<StringError>("Page size is not a multiple of "
|
||||||
"alignment",
|
"alignment",
|
||||||
inconvertibleErrorCode());
|
inconvertibleErrorCode());
|
||||||
|
|
|
@ -147,7 +147,7 @@ Error OrcAArch64::emitIndirectStubsBlock(IndirectStubsInfo &StubsInfo,
|
||||||
const unsigned StubSize = IndirectStubsInfo::StubSize;
|
const unsigned StubSize = IndirectStubsInfo::StubSize;
|
||||||
|
|
||||||
// Emit at least MinStubs, rounded up to fill the pages allocated.
|
// Emit at least MinStubs, rounded up to fill the pages allocated.
|
||||||
unsigned PageSize = sys::Process::getPageSize();
|
static const unsigned PageSize = sys::Process::getPageSizeEstimate();
|
||||||
unsigned NumPages = ((MinStubs * StubSize) + (PageSize - 1)) / PageSize;
|
unsigned NumPages = ((MinStubs * StubSize) + (PageSize - 1)) / PageSize;
|
||||||
unsigned NumStubs = (NumPages * PageSize) / StubSize;
|
unsigned NumStubs = (NumPages * PageSize) / StubSize;
|
||||||
|
|
||||||
|
@ -229,7 +229,7 @@ Error OrcX86_64_Base::emitIndirectStubsBlock(IndirectStubsInfo &StubsInfo,
|
||||||
const unsigned StubSize = IndirectStubsInfo::StubSize;
|
const unsigned StubSize = IndirectStubsInfo::StubSize;
|
||||||
|
|
||||||
// Emit at least MinStubs, rounded up to fill the pages allocated.
|
// Emit at least MinStubs, rounded up to fill the pages allocated.
|
||||||
unsigned PageSize = sys::Process::getPageSize();
|
static const unsigned PageSize = sys::Process::getPageSizeEstimate();
|
||||||
unsigned NumPages = ((MinStubs * StubSize) + (PageSize - 1)) / PageSize;
|
unsigned NumPages = ((MinStubs * StubSize) + (PageSize - 1)) / PageSize;
|
||||||
unsigned NumStubs = (NumPages * PageSize) / StubSize;
|
unsigned NumStubs = (NumPages * PageSize) / StubSize;
|
||||||
|
|
||||||
|
@ -497,7 +497,7 @@ Error OrcI386::emitIndirectStubsBlock(IndirectStubsInfo &StubsInfo,
|
||||||
const unsigned StubSize = IndirectStubsInfo::StubSize;
|
const unsigned StubSize = IndirectStubsInfo::StubSize;
|
||||||
|
|
||||||
// Emit at least MinStubs, rounded up to fill the pages allocated.
|
// Emit at least MinStubs, rounded up to fill the pages allocated.
|
||||||
unsigned PageSize = sys::Process::getPageSize();
|
static const unsigned PageSize = sys::Process::getPageSizeEstimate();
|
||||||
unsigned NumPages = ((MinStubs * StubSize) + (PageSize - 1)) / PageSize;
|
unsigned NumPages = ((MinStubs * StubSize) + (PageSize - 1)) / PageSize;
|
||||||
unsigned NumStubs = (NumPages * PageSize) / StubSize;
|
unsigned NumStubs = (NumPages * PageSize) / StubSize;
|
||||||
|
|
||||||
|
@ -683,7 +683,7 @@ Error OrcMips32_Base::emitIndirectStubsBlock(IndirectStubsInfo &StubsInfo,
|
||||||
const unsigned StubSize = IndirectStubsInfo::StubSize;
|
const unsigned StubSize = IndirectStubsInfo::StubSize;
|
||||||
|
|
||||||
// Emit at least MinStubs, rounded up to fill the pages allocated.
|
// Emit at least MinStubs, rounded up to fill the pages allocated.
|
||||||
unsigned PageSize = sys::Process::getPageSize();
|
static const unsigned PageSize = sys::Process::getPageSizeEstimate();
|
||||||
unsigned NumPages = ((MinStubs * StubSize) + (PageSize - 1)) / PageSize;
|
unsigned NumPages = ((MinStubs * StubSize) + (PageSize - 1)) / PageSize;
|
||||||
unsigned NumStubs = (NumPages * PageSize) / StubSize;
|
unsigned NumStubs = (NumPages * PageSize) / StubSize;
|
||||||
|
|
||||||
|
@ -929,7 +929,7 @@ Error OrcMips64::emitIndirectStubsBlock(IndirectStubsInfo &StubsInfo,
|
||||||
const unsigned StubSize = IndirectStubsInfo::StubSize;
|
const unsigned StubSize = IndirectStubsInfo::StubSize;
|
||||||
|
|
||||||
// Emit at least MinStubs, rounded up to fill the pages allocated.
|
// Emit at least MinStubs, rounded up to fill the pages allocated.
|
||||||
unsigned PageSize = sys::Process::getPageSize();
|
static const unsigned PageSize = sys::Process::getPageSizeEstimate();
|
||||||
unsigned NumPages = ((MinStubs * StubSize) + (PageSize - 1)) / PageSize;
|
unsigned NumPages = ((MinStubs * StubSize) + (PageSize - 1)) / PageSize;
|
||||||
unsigned NumStubs = (NumPages * PageSize) / StubSize;
|
unsigned NumStubs = (NumPages * PageSize) / StubSize;
|
||||||
|
|
||||||
|
|
|
@ -172,7 +172,7 @@ bool SectionMemoryManager::finalizeMemory(std::string *ErrMsg) {
|
||||||
}
|
}
|
||||||
|
|
||||||
static sys::MemoryBlock trimBlockToPageSize(sys::MemoryBlock M) {
|
static sys::MemoryBlock trimBlockToPageSize(sys::MemoryBlock M) {
|
||||||
static const size_t PageSize = sys::Process::getPageSize();
|
static const size_t PageSize = sys::Process::getPageSizeEstimate();
|
||||||
|
|
||||||
size_t StartOverlap =
|
size_t StartOverlap =
|
||||||
(PageSize - ((uintptr_t)M.base() % PageSize)) % PageSize;
|
(PageSize - ((uintptr_t)M.base() % PageSize)) % PageSize;
|
||||||
|
@ -244,7 +244,7 @@ public:
|
||||||
unsigned Flags, std::error_code &EC) override {
|
unsigned Flags, std::error_code &EC) override {
|
||||||
// allocateMappedMemory calls mmap(2). We round up a request size
|
// allocateMappedMemory calls mmap(2). We round up a request size
|
||||||
// to page size to get extra space for free.
|
// to page size to get extra space for free.
|
||||||
static const size_t PageSize = sys::Process::getPageSize();
|
static const size_t PageSize = sys::Process::getPageSizeEstimate();
|
||||||
size_t ReqBytes = (NumBytes + PageSize - 1) & ~(PageSize - 1);
|
size_t ReqBytes = (NumBytes + PageSize - 1) & ~(PageSize - 1);
|
||||||
return sys::Memory::allocateMappedMemory(ReqBytes, NearBlock, Flags, EC);
|
return sys::Memory::allocateMappedMemory(ReqBytes, NearBlock, Flags, EC);
|
||||||
}
|
}
|
||||||
|
|
|
@ -417,7 +417,7 @@ static ErrorOr<std::unique_ptr<MB>>
|
||||||
getOpenFileImpl(int FD, const Twine &Filename, uint64_t FileSize,
|
getOpenFileImpl(int FD, const Twine &Filename, uint64_t FileSize,
|
||||||
uint64_t MapSize, int64_t Offset, bool RequiresNullTerminator,
|
uint64_t MapSize, int64_t Offset, bool RequiresNullTerminator,
|
||||||
bool IsVolatile) {
|
bool IsVolatile) {
|
||||||
static int PageSize = sys::Process::getPageSize();
|
static int PageSize = sys::Process::getPageSizeEstimate();
|
||||||
|
|
||||||
// Default is to map the full file.
|
// Default is to map the full file.
|
||||||
if (MapSize == uint64_t(-1)) {
|
if (MapSize == uint64_t(-1)) {
|
||||||
|
|
|
@ -103,7 +103,7 @@ Memory::allocateMappedMemory(size_t NumBytes,
|
||||||
// Use any near hint and the page size to set a page-aligned starting address
|
// Use any near hint and the page size to set a page-aligned starting address
|
||||||
uintptr_t Start = NearBlock ? reinterpret_cast<uintptr_t>(NearBlock->base()) +
|
uintptr_t Start = NearBlock ? reinterpret_cast<uintptr_t>(NearBlock->base()) +
|
||||||
NearBlock->size() : 0;
|
NearBlock->size() : 0;
|
||||||
static const size_t PageSize = Process::getPageSize();
|
static const size_t PageSize = Process::getPageSizeEstimate();
|
||||||
if (Start && Start % PageSize)
|
if (Start && Start % PageSize)
|
||||||
Start += PageSize - Start % PageSize;
|
Start += PageSize - Start % PageSize;
|
||||||
|
|
||||||
|
@ -149,7 +149,7 @@ Memory::releaseMappedMemory(MemoryBlock &M) {
|
||||||
|
|
||||||
std::error_code
|
std::error_code
|
||||||
Memory::protectMappedMemory(const MemoryBlock &M, unsigned Flags) {
|
Memory::protectMappedMemory(const MemoryBlock &M, unsigned Flags) {
|
||||||
static const size_t PageSize = Process::getPageSize();
|
static const size_t PageSize = Process::getPageSizeEstimate();
|
||||||
if (M.Address == nullptr || M.Size == 0)
|
if (M.Address == nullptr || M.Size == 0)
|
||||||
return std::error_code();
|
return std::error_code();
|
||||||
|
|
||||||
|
|
|
@ -786,7 +786,7 @@ const char *mapped_file_region::const_data() const {
|
||||||
}
|
}
|
||||||
|
|
||||||
int mapped_file_region::alignment() {
|
int mapped_file_region::alignment() {
|
||||||
return Process::getPageSize();
|
return Process::getPageSizeEstimate();
|
||||||
}
|
}
|
||||||
|
|
||||||
std::error_code detail::directory_iterator_construct(detail::DirIterState &it,
|
std::error_code detail::directory_iterator_construct(detail::DirIterState &it,
|
||||||
|
|
|
@ -69,7 +69,7 @@ static std::pair<std::chrono::microseconds, std::chrono::microseconds> getRUsage
|
||||||
|
|
||||||
// On Cygwin, getpagesize() returns 64k(AllocationGranularity) and
|
// On Cygwin, getpagesize() returns 64k(AllocationGranularity) and
|
||||||
// offset in mmap(3) should be aligned to the AllocationGranularity.
|
// offset in mmap(3) should be aligned to the AllocationGranularity.
|
||||||
unsigned Process::getPageSize() {
|
Expected<unsigned> Process::getPageSize() {
|
||||||
#if defined(HAVE_GETPAGESIZE)
|
#if defined(HAVE_GETPAGESIZE)
|
||||||
static const int page_size = ::getpagesize();
|
static const int page_size = ::getpagesize();
|
||||||
#elif defined(HAVE_SYSCONF)
|
#elif defined(HAVE_SYSCONF)
|
||||||
|
@ -77,6 +77,9 @@ unsigned Process::getPageSize() {
|
||||||
#else
|
#else
|
||||||
#error Cannot get the page size on this machine
|
#error Cannot get the page size on this machine
|
||||||
#endif
|
#endif
|
||||||
|
if (page_size == -1)
|
||||||
|
return errorCodeToError(std::error_code(errno, std::generic_category()));
|
||||||
|
|
||||||
return static_cast<unsigned>(page_size);
|
return static_cast<unsigned>(page_size);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -56,7 +56,7 @@ static unsigned computePageSize() {
|
||||||
return static_cast<unsigned>(info.dwPageSize);
|
return static_cast<unsigned>(info.dwPageSize);
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned Process::getPageSize() {
|
Expected<unsigned> Process::getPageSize() {
|
||||||
static unsigned Ret = computePageSize();
|
static unsigned Ret = computePageSize();
|
||||||
return Ret;
|
return Ret;
|
||||||
}
|
}
|
||||||
|
|
|
@ -50,7 +50,7 @@ class MappedMemoryTest : public ::testing::TestWithParam<unsigned> {
|
||||||
public:
|
public:
|
||||||
MappedMemoryTest() {
|
MappedMemoryTest() {
|
||||||
Flags = GetParam();
|
Flags = GetParam();
|
||||||
PageSize = sys::Process::getPageSize();
|
PageSize = sys::Process::getPageSizeEstimate();
|
||||||
}
|
}
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
|
Loading…
Reference in New Issue