Index: lib/sanitizer_common/sanitizer_fuchsia.cc =================================================================== --- lib/sanitizer_common/sanitizer_fuchsia.cc +++ lib/sanitizer_common/sanitizer_fuchsia.cc @@ -274,20 +274,16 @@ DecreaseTotalMmap(size); } -void ReservedAddressRange::Unmap(uptr fixed_addr, uptr size) { - uptr offset = fixed_addr - reinterpret_cast(base_); - uptr addr = reinterpret_cast(base_) + offset; - void *addr_as_void = reinterpret_cast(addr); - uptr base_as_uptr = reinterpret_cast(base_); - // Only unmap at the beginning or end of the range. - CHECK((addr_as_void == base_) || (addr + size == base_as_uptr + size_)); +void ReservedAddressRange::Unmap(uptr addr, uptr size) { CHECK_LE(size, size_); + if (addr == reinterpret_cast(base_)) + // If we unmap the whole range, just null out the base. + base_ = (size == size_) ? nullptr : reinterpret_cast(addr + size); + else + CHECK_EQ(addr + size, reinterpret_cast(base_) + size_); + size_ -= size; UnmapOrDieVmar(reinterpret_cast(addr), size, static_cast(os_handle_)); - if (addr_as_void == base_) { - base_ = reinterpret_cast(addr + size); - } - size_ = size_ - size; } // This should never be called. Index: lib/sanitizer_common/sanitizer_posix_libcdep.cc =================================================================== --- lib/sanitizer_common/sanitizer_posix_libcdep.cc +++ lib/sanitizer_common/sanitizer_posix_libcdep.cc @@ -346,11 +346,7 @@ // `open` (e.g. TSAN, ESAN), then you'll get a failure during initialization. // TODO(flowerhack): Fix the implementation of GetNamedMappingFd to solve // this problem. - if (fixed_addr) { - base_ = MmapFixedNoAccess(fixed_addr, size); - } else { - base_ = MmapNoAccess(size); - } + base_ = fixed_addr ? MmapFixedNoAccess(fixed_addr, size) : MmapNoAccess(size); size_ = size; name_ = name; (void)os_handle_; // unsupported @@ -368,16 +364,14 @@ } void ReservedAddressRange::Unmap(uptr addr, uptr size) { - void* addr_as_void = reinterpret_cast(addr); - uptr base_as_uptr = reinterpret_cast(base_); - // Only unmap at the beginning or end of the range. - CHECK((addr_as_void == base_) || (addr + size == base_as_uptr + size_)); CHECK_LE(size, size_); + if (addr == reinterpret_cast(base_)) + // If we unmap the whole range, just null out the base. + base_ = (size == size_) ? nullptr : reinterpret_cast(addr + size); + else + CHECK_EQ(addr + size, reinterpret_cast(base_) + size_); + size_ -= size; UnmapOrDie(reinterpret_cast(addr), size); - if (addr_as_void == base_) { - base_ = reinterpret_cast(addr + size); - } - size_ = size_ - size; } void *MmapFixedNoAccess(uptr fixed_addr, uptr size, const char *name) { Index: lib/sanitizer_common/sanitizer_win.cc =================================================================== --- lib/sanitizer_common/sanitizer_win.cc +++ lib/sanitizer_common/sanitizer_win.cc @@ -247,15 +247,12 @@ } void ReservedAddressRange::Unmap(uptr addr, uptr size) { - void* addr_as_void = reinterpret_cast(addr); - uptr base_as_uptr = reinterpret_cast(base_); // Only unmap if it covers the entire range. - CHECK((addr == base_as_uptr) && (size == size_)); - UnmapOrDie(addr_as_void, size); - if (addr_as_void == base_) { - base_ = reinterpret_cast(addr + size); - } - size_ = size_ - size; + CHECK((addr == reinterpret_cast(base_)) && (size == size_)); + // We unmap the whole range, just null out the base. + base_ = nullptr; + size_ = 0; + UnmapOrDie(reinterpret_cast(addr), size); } void *MmapFixedOrDieOnFatalError(uptr fixed_addr, uptr size) { @@ -276,11 +273,7 @@ } uptr ReservedAddressRange::Init(uptr size, const char *name, uptr fixed_addr) { - if (fixed_addr) { - base_ = MmapFixedNoAccess(fixed_addr, size, name); - } else { - base_ = MmapNoAccess(size); - } + base_ = fixed_addr ? MmapFixedNoAccess(fixed_addr, size) : MmapNoAccess(size); size_ = size; name_ = name; (void)os_handle_; // unsupported