Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions compiler-rt/lib/scudo/standalone/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ set(SCUDO_HEADERS
quarantine.h
release.h
report.h
report_linux.h
secondary.h
size_class_map.h
stack_depot.h
Expand Down Expand Up @@ -113,6 +114,7 @@ set(SCUDO_SOURCES
mem_map_linux.cpp
release.cpp
report.cpp
report_linux.cpp
string_utils.cpp
timing.cpp
)
Expand Down
14 changes: 0 additions & 14 deletions compiler-rt/lib/scudo/standalone/common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,4 @@ uptr getPageSizeSlow() {
return PageSizeCached;
}

// Fatal internal map() or unmap() error (potentially OOM related).
void NORETURN dieOnMapUnmapError(uptr SizeIfOOM) {
char Error[128] = "Scudo ERROR: internal map or unmap failure\n";
if (SizeIfOOM) {
formatString(
Error, sizeof(Error),
"Scudo ERROR: internal map failure (NO MEMORY) requesting %zuKB\n",
SizeIfOOM >> 10);
}
outputRaw(Error);
setAbortMessage(Error);
die();
}

} // namespace scudo
4 changes: 0 additions & 4 deletions compiler-rt/lib/scudo/standalone/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -175,10 +175,6 @@ void setMemoryPermission(uptr Addr, uptr Size, uptr Flags,
void releasePagesToOS(uptr BaseAddress, uptr Offset, uptr Size,
MapPlatformData *Data = nullptr);

// Internal map & unmap fatal error. This must not call map(). SizeIfOOM shall
// hold the requested size on an out-of-memory error, 0 otherwise.
void NORETURN dieOnMapUnmapError(uptr SizeIfOOM = 0);

// Logging related functions.

void setAbortMessage(const char *Message);
Expand Down
7 changes: 4 additions & 3 deletions compiler-rt/lib/scudo/standalone/linux.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include "internal_defs.h"
#include "linux.h"
#include "mutex.h"
#include "report_linux.h"
#include "string_utils.h"

#include <errno.h>
Expand Down Expand Up @@ -66,7 +67,7 @@ void *map(void *Addr, uptr Size, UNUSED const char *Name, uptr Flags,
void *P = mmap(Addr, Size, MmapProt, MmapFlags, -1, 0);
if (P == MAP_FAILED) {
if (!(Flags & MAP_ALLOWNOMEM) || errno != ENOMEM)
dieOnMapUnmapError(errno == ENOMEM ? Size : 0);
reportMapError(errno == ENOMEM ? Size : 0);
return nullptr;
}
#if SCUDO_ANDROID
Expand All @@ -80,15 +81,15 @@ void *map(void *Addr, uptr Size, UNUSED const char *Name, uptr Flags,
void unmap(void *Addr, uptr Size, UNUSED uptr Flags,
UNUSED MapPlatformData *Data) {
if (munmap(Addr, Size) != 0)
dieOnMapUnmapError();
reportUnmapError(reinterpret_cast<uptr>(Addr), Size);
}

// TODO: Will be deprecated. Use the interfaces in MemMapLinux instead.
void setMemoryPermission(uptr Addr, uptr Size, uptr Flags,
UNUSED MapPlatformData *Data) {
int Prot = (Flags & MAP_NOACCESS) ? PROT_NONE : (PROT_READ | PROT_WRITE);
if (mprotect(reinterpret_cast<void *>(Addr), Size, Prot) != 0)
dieOnMapUnmapError();
reportProtectError(Addr, Size, Prot);
}

// TODO: Will be deprecated. Use the interfaces in MemMapLinux instead.
Expand Down
11 changes: 6 additions & 5 deletions compiler-rt/lib/scudo/standalone/mem_map_linux.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "internal_defs.h"
#include "linux.h"
#include "mutex.h"
#include "report_linux.h"
#include "string_utils.h"

#include <errno.h>
Expand Down Expand Up @@ -64,7 +65,7 @@ static void *mmapWrapper(uptr Addr, uptr Size, const char *Name, uptr Flags) {
mmap(reinterpret_cast<void *>(Addr), Size, MmapProt, MmapFlags, -1, 0);
if (P == MAP_FAILED) {
if (!(Flags & MAP_ALLOWNOMEM) || errno != ENOMEM)
dieOnMapUnmapError(errno == ENOMEM ? Size : 0);
reportMapError(errno == ENOMEM ? Size : 0);
return nullptr;
}
#if SCUDO_ANDROID
Expand Down Expand Up @@ -101,21 +102,21 @@ void MemMapLinux::unmapImpl(uptr Addr, uptr Size) {
}

if (munmap(reinterpret_cast<void *>(Addr), Size) != 0)
dieOnMapUnmapError();
reportUnmapError(Addr, Size);
}

bool MemMapLinux::remapImpl(uptr Addr, uptr Size, const char *Name,
uptr Flags) {
void *P = mmapWrapper(Addr, Size, Name, Flags);
if (reinterpret_cast<uptr>(P) != Addr)
dieOnMapUnmapError();
reportMapError();
return true;
}

void MemMapLinux::setMemoryPermissionImpl(uptr Addr, uptr Size, uptr Flags) {
int Prot = (Flags & MAP_NOACCESS) ? PROT_NONE : (PROT_READ | PROT_WRITE);
if (mprotect(reinterpret_cast<void *>(Addr), Size, Prot) != 0)
dieOnMapUnmapError();
reportProtectError(Addr, Size, Prot);
}

void MemMapLinux::releaseAndZeroPagesToOSImpl(uptr From, uptr Size) {
Expand All @@ -139,7 +140,7 @@ bool ReservedMemoryLinux::createImpl(uptr Addr, uptr Size, const char *Name,

void ReservedMemoryLinux::releaseImpl() {
if (munmap(reinterpret_cast<void *>(getBase()), getCapacity()) != 0)
dieOnMapUnmapError();
reportUnmapError(getBase(), getCapacity());
}

ReservedMemoryLinux::MemMapT ReservedMemoryLinux::dispatchImpl(uptr Addr,
Expand Down
13 changes: 8 additions & 5 deletions compiler-rt/lib/scudo/standalone/report.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,7 @@ class ScopedErrorReport {
Message.vappend(Format, Args);
va_end(Args);
}
NORETURN ~ScopedErrorReport() {
outputRaw(Message.data());
setAbortMessage(Message.data());
die();
}
NORETURN ~ScopedErrorReport() { reportRawError(Message.data()); }

private:
ScopedString Message;
Expand All @@ -55,6 +51,13 @@ void NORETURN reportError(const char *Message) {
Report.append("%s\n", Message);
}

// Generic fatal error message without ScopedString.
void NORETURN reportRawError(const char *Message) {
outputRaw(Message);
setAbortMessage(Message);
die();
}

void NORETURN reportInvalidFlag(const char *FlagType, const char *Value) {
ScopedErrorReport Report;
Report.append("invalid value for %s option: '%s'\n", FlagType, Value);
Expand Down
5 changes: 4 additions & 1 deletion compiler-rt/lib/scudo/standalone/report.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,12 @@ namespace scudo {

// Reports are *fatal* unless stated otherwise.

// Generic error.
// Generic error, adds newline to end of message.
void NORETURN reportError(const char *Message);

// Generic error, but the message is not modified.
void NORETURN reportRawError(const char *Message);

// Flags related errors.
void NORETURN reportInvalidFlag(const char *FlagType, const char *Value);

Expand Down
58 changes: 58 additions & 0 deletions compiler-rt/lib/scudo/standalone/report_linux.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
//===-- report_linux.cpp ----------------------------------------*- C++ -*-===//
//
// 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 "platform.h"

#if SCUDO_LINUX || SCUDO_TRUSTY

#include "common.h"
#include "internal_defs.h"
#include "report.h"
#include "report_linux.h"
#include "string_utils.h"

#include <errno.h>
#include <stdlib.h>
#include <string.h>

namespace scudo {

// Fatal internal map() error (potentially OOM related).
void NORETURN reportMapError(uptr SizeIfOOM) {
char Error[128] = "Scudo ERROR: internal map failure\n";
if (SizeIfOOM) {
formatString(
Error, sizeof(Error),
"Scudo ERROR: internal map failure (NO MEMORY) requesting %zuKB\n",
SizeIfOOM >> 10);
}
reportRawError(Error);
}

void NORETURN reportUnmapError(uptr Addr, uptr Size) {
char Error[128];
formatString(Error, sizeof(Error),
"Scudo ERROR: internal unmap failure (error desc=%s) Addr 0x%zx "
"Size %zu\n",
strerror(errno), Addr, Size);
reportRawError(Error);
}

void NORETURN reportProtectError(uptr Addr, uptr Size, int Prot) {
char Error[128];
formatString(
Error, sizeof(Error),
"Scudo ERROR: internal protect failure (error desc=%s) Addr 0x%zx "
"Size %zu Prot %x\n",
strerror(errno), Addr, Size, Prot);
reportRawError(Error);
}

} // namespace scudo

#endif // SCUDO_LINUX || SCUDO_TRUSTY
34 changes: 34 additions & 0 deletions compiler-rt/lib/scudo/standalone/report_linux.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
//===-- report_linux.h ------------------------------------------*- C++ -*-===//
//
// 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
//
//===----------------------------------------------------------------------===//

#ifndef SCUDO_REPORT_LINUX_H_
#define SCUDO_REPORT_LINUX_H_

#include "platform.h"

#if SCUDO_LINUX || SCUDO_TRUSTY

#include "internal_defs.h"

namespace scudo {

// Report a fatal error when a map call fails. SizeIfOOM shall
// hold the requested size on an out-of-memory error, 0 otherwise.
void NORETURN reportMapError(uptr SizeIfOOM = 0);

// Report a fatal error when an unmap call fails.
void NORETURN reportUnmapError(uptr Addr, uptr Size);

// Report a fatal error when a mprotect call fails.
void NORETURN reportProtectError(uptr Addr, uptr Size, int Prot);

} // namespace scudo

#endif // SCUDO_LINUX || SCUDO_TRUSTY

#endif // SCUDO_REPORT_LINUX_H_
5 changes: 3 additions & 2 deletions compiler-rt/lib/scudo/standalone/trusty.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

#include "common.h"
#include "mutex.h"
#include "report_linux.h"
#include "trusty.h"

#include <errno.h> // for errno
Expand Down Expand Up @@ -51,7 +52,7 @@ void *map(void *Addr, uptr Size, const char *Name, uptr Flags,
if (IS_ERR(P)) {
errno = lk_err_to_errno(PTR_ERR(P));
if (!(Flags & MAP_ALLOWNOMEM) || errno != ENOMEM)
dieOnMapUnmapError(Size);
reportMapError(Size);
return nullptr;
}

Expand All @@ -61,7 +62,7 @@ void *map(void *Addr, uptr Size, const char *Name, uptr Flags,
void unmap(UNUSED void *Addr, UNUSED uptr Size, UNUSED uptr Flags,
UNUSED MapPlatformData *Data) {
if (_trusty_munmap(Addr, Size) != 0)
dieOnMapUnmapError();
reportUnmapError(Addr, Size);
}

void setMemoryPermission(UNUSED uptr Addr, UNUSED uptr Size, UNUSED uptr Flags,
Expand Down