diff --git a/compiler-rt/lib/scudo/standalone/allocator_config.h b/compiler-rt/lib/scudo/standalone/allocator_config.h --- a/compiler-rt/lib/scudo/standalone/allocator_config.h +++ b/compiler-rt/lib/scudo/standalone/allocator_config.h @@ -276,15 +276,12 @@ using TSDRegistryT = TSDRegistrySharedT; // Shared, max 1 TSD. struct Primary { - static const bool MaySupportMemoryTagging = false; using SizeClassMap = TrustySizeClassMap; - // Some apps have 1 page of heap total so small regions are necessary. - static const uptr RegionSizeLog = 10U; - static const uptr GroupSizeLog = 10U; + static const uptr RegionSizeLog = 28U; + static const uptr GroupSizeLog = 20U; typedef u32 CompactPtrT; static const bool EnableRandomOffset = false; - // Trusty is extremely memory-constrained so minimally round up map calls. - static const uptr MapSizeIncrement = 1UL << 4; + static const uptr MapSizeIncrement = 1UL << 12; static const uptr CompactPtrScale = SCUDO_MIN_ALIGNMENT_LOG; static const s32 MinReleaseToOsIntervalMs = INT32_MIN; static const s32 MaxReleaseToOsIntervalMs = INT32_MAX; diff --git a/compiler-rt/lib/scudo/standalone/size_class_map.h b/compiler-rt/lib/scudo/standalone/size_class_map.h --- a/compiler-rt/lib/scudo/standalone/size_class_map.h +++ b/compiler-rt/lib/scudo/standalone/size_class_map.h @@ -311,13 +311,11 @@ typedef FixedSizeClassMap SvelteSizeClassMap; -// Trusty is configured to only have one region containing blocks of size -// 2^7 bytes. struct TrustySizeClassConfig { static const uptr NumBits = 1; - static const uptr MinSizeLog = 7; - static const uptr MidSizeLog = 7; - static const uptr MaxSizeLog = 7; + static const uptr MinSizeLog = 5; + static const uptr MidSizeLog = 5; + static const uptr MaxSizeLog = 15; static const u16 MaxNumCachedHint = 12; static const uptr MaxBytesCachedLog = 10; static const uptr SizeDelta = 0; diff --git a/compiler-rt/lib/scudo/standalone/trusty.cpp b/compiler-rt/lib/scudo/standalone/trusty.cpp --- a/compiler-rt/lib/scudo/standalone/trusty.cpp +++ b/compiler-rt/lib/scudo/standalone/trusty.cpp @@ -12,17 +12,17 @@ #include "common.h" #include "mutex.h" -#include "string_utils.h" #include "trusty.h" #include // for errno +#include // for PTR_ERR and IS_ERR #include // for printf() #include // for getenv() #include // for getauxval() #include // for clock_gettime() +#include // for lk_err_to_errno() #include // for _trusty_brk() - -#define SBRK_ALIGN 32 +#include // for MMAP flags namespace scudo { @@ -30,35 +30,35 @@ void NORETURN die() { abort(); } -void *map(UNUSED void *Addr, uptr Size, UNUSED const char *Name, uptr Flags, +void *map(void *Addr, uptr Size, const char *Name, uptr Flags, UNUSED MapPlatformData *Data) { - // Calling _trusty_brk(0) returns the current program break. - uptr ProgramBreak = reinterpret_cast(_trusty_brk(0)); - uptr Start; - uptr End; - - Start = roundUp(ProgramBreak, SBRK_ALIGN); - // Don't actually extend the heap if MAP_NOACCESS flag is set since this is - // the case where Scudo tries to reserve a memory region without mapping - // physical pages. + uint32_t MmapFlags = + MMAP_FLAG_ANONYMOUS | MMAP_FLAG_PROT_READ | MMAP_FLAG_PROT_WRITE; + + // If the MAP_NOACCESS flag is set, Scudo tries to reserve + // a memory region without mapping physical pages. This corresponds + // to MMAP_FLAG_NO_PHYSICAL in Trusty. if (Flags & MAP_NOACCESS) - return reinterpret_cast(Start); - - // Attempt to extend the heap by Size bytes using _trusty_brk. - End = roundUp(Start + Size, SBRK_ALIGN); - ProgramBreak = - reinterpret_cast(_trusty_brk(reinterpret_cast(End))); - if (ProgramBreak < End) { - errno = ENOMEM; + MmapFlags |= MMAP_FLAG_NO_PHYSICAL; + if (Addr) + MmapFlags |= MMAP_FLAG_FIXED_NOREPLACE; + + void *P = (void *)_trusty_mmap(Addr, Size, MmapFlags, 0); + + if (IS_ERR(P)) { + errno = lk_err_to_errno(PTR_ERR(P)); dieOnMapUnmapError(Size); return nullptr; } - return reinterpret_cast(Start); // Base of new reserved region. + + return P; } -// Unmap is a no-op since Trusty uses sbrk instead of memory mapping. void unmap(UNUSED void *Addr, UNUSED uptr Size, UNUSED uptr Flags, - UNUSED MapPlatformData *Data) {} + UNUSED MapPlatformData *Data) { + if (_trusty_munmap(Addr, Size) != 0) + dieOnMapUnmapError(); +} void setMemoryPermission(UNUSED uptr Addr, UNUSED uptr Size, UNUSED uptr Flags, UNUSED MapPlatformData *Data) {}