Index: lib/sanitizer_common/sanitizer_common.h =================================================================== --- lib/sanitizer_common/sanitizer_common.h +++ lib/sanitizer_common/sanitizer_common.h @@ -83,6 +83,18 @@ uptr *tls_addr, uptr *tls_size); // Memory management +// This class relies on zero-initialization. +class ReservedAddressRange { + public: + uptr Init(uptr size, const char *name = nullptr, uptr fixed_addr = (uptr)0); + uptr Map(uptr offset, uptr size, bool tolerate_enomem = false); + void *base() { return base_; } + private: + uptr os_cookie_; + void* base_; + uptr size_; +}; + void *MmapOrDie(uptr size, const char *mem_type, bool raw_report = false); INLINE void *MmapOrDieQuietly(uptr size, const char *mem_type) { return MmapOrDie(size, mem_type, /*raw_report*/ true); Index: lib/sanitizer_common/sanitizer_fuchsia.cc =================================================================== --- lib/sanitizer_common/sanitizer_fuchsia.cc +++ lib/sanitizer_common/sanitizer_fuchsia.cc @@ -237,6 +237,19 @@ return DoAnonymousMmapOrDie(size, mem_type, false, false); } +uptr ReservedAddressRange::Init(uptr init_size, const char* name = nullptr, + uptr fixed_addr = uptr(0)) { + return reinterpret_cast(MmapNoAccess(init_size)); +} + +// Uses fixed_addr for now. +// Will use offset instead once we've implemented this function for real. +uptr ReservedAddressRange::Map(uptr fixed_addr, uptr map_size, + bool tolerate_enomem = true) { + return reinterpret_cast(MmapFixedOrDie(fixed_addr, size, + tolerate_enomem)); +} + // MmapNoAccess and MmapFixedOrDie are used only by sanitizer_allocator. // Instead of doing exactly what they say, we make MmapNoAccess actually // just allocate a VMAR to reserve the address space. Then MmapFixedOrDie Index: lib/sanitizer_common/sanitizer_posix.cc =================================================================== --- lib/sanitizer_common/sanitizer_posix.cc +++ lib/sanitizer_common/sanitizer_posix.cc @@ -141,6 +141,16 @@ return MmapFixedImpl(fixed_addr, size, false /*tolerate_enomem*/); } +// Uses fixed_addr for now. +// Will use offset instead once we've implemented this function for real. +uptr ReservedAddressRange::Map(uptr fixed_addr, uptr size, + bool tolerate_enomem) { + if (tolerate_enomem) { + return reinterpret_cast(MmapFixedOrDieOnFatalError(fixed_addr, size)); + } + return reinterpret_cast(MmapFixedOrDie(fixed_addr, size)); +} + void *MmapFixedOrDieOnFatalError(uptr fixed_addr, uptr size) { return MmapFixedImpl(fixed_addr, size, true /*tolerate_enomem*/); } Index: lib/sanitizer_common/sanitizer_posix_libcdep.cc =================================================================== --- lib/sanitizer_common/sanitizer_posix_libcdep.cc +++ lib/sanitizer_common/sanitizer_posix_libcdep.cc @@ -337,6 +337,13 @@ return (void *)p; } +uptr ReservedAddressRange::Init(uptr size, const char *name, uptr fixed_addr) { + if (fixed_addr) { + return reinterpret_cast(MmapFixedNoAccess(fixed_addr, size, name)); + } + return reinterpret_cast(MmapNoAccess(size)); +} + void *MmapFixedNoAccess(uptr fixed_addr, uptr size, const char *name) { int fd = name ? GetNamedMappingFd(name, size) : -1; unsigned flags = MAP_PRIVATE | MAP_FIXED | MAP_NORESERVE; Index: lib/sanitizer_common/sanitizer_win.cc =================================================================== --- lib/sanitizer_common/sanitizer_win.cc +++ lib/sanitizer_common/sanitizer_win.cc @@ -236,6 +236,16 @@ return p; } +// Uses fixed_addr for now. +// Will use offset instead once we've implemented this function for real. +uptr ReservedAddressRange::Map(uptr fixed_addr, uptr size, + bool tolerate_enomem) { + if (tolerate_enomem) { + return reinterpret_cast(MmapFixedOrDieOnFatalError(fixed_addr, size)); + } + return reinterpret_cast(MmapFixedOrDie(uptr fixed_addr, uptr size)); +} + void *MmapFixedOrDieOnFatalError(uptr fixed_addr, uptr size) { void *p = VirtualAlloc((LPVOID)fixed_addr, size, MEM_COMMIT, PAGE_READWRITE); @@ -253,6 +263,13 @@ return MmapOrDie(size, mem_type); } +uptr ReservedAddressRange::Init(uptr size, const char *name, uptr fixed_addr) { + if (fixed_addr) { + return reinterpret_cast(MmapFixedNoAccess(fixed_addr, size, name)); + } + return reinterpret_cast(MmapNoAccess(size)); +} + void *MmapFixedNoAccess(uptr fixed_addr, uptr size, const char *name) { (void)name; // unsupported void *res = VirtualAlloc((LPVOID)fixed_addr, size,