Index: compiler-rt/trunk/lib/asan/asan_poisoning.h =================================================================== --- compiler-rt/trunk/lib/asan/asan_poisoning.h +++ compiler-rt/trunk/lib/asan/asan_poisoning.h @@ -86,8 +86,8 @@ } } -// Calls __sanitizer::ReleaseMemoryToOS() on -// [MemToShadow(p), MemToShadow(p+size)] with proper rounding. +// Calls __sanitizer::ReleaseMemoryPagesToOS() on +// [MemToShadow(p), MemToShadow(p+size)]. void FlushUnneededASanShadowMemory(uptr p, uptr size); } // namespace __asan Index: compiler-rt/trunk/lib/asan/asan_poisoning.cc =================================================================== --- compiler-rt/trunk/lib/asan/asan_poisoning.cc +++ compiler-rt/trunk/lib/asan/asan_poisoning.cc @@ -64,12 +64,9 @@ }; void FlushUnneededASanShadowMemory(uptr p, uptr size) { - // Since asan's mapping is compacting, the shadow chunk may be - // not page-aligned, so we only flush the page-aligned portion. - uptr page_size = GetPageSizeCached(); - uptr shadow_beg = RoundUpTo(MemToShadow(p), page_size); - uptr shadow_end = RoundDownTo(MemToShadow(p + size), page_size); - ReleaseMemoryToOS(shadow_beg, shadow_end - shadow_beg); + // Since asan's mapping is compacting, the shadow chunk may be + // not page-aligned, so we only flush the page-aligned portion. + ReleaseMemoryPagesToOS(MemToShadow(p), MemToShadow(p + size)); } void AsanPoisonOrUnpoisonIntraObjectRedzone(uptr ptr, uptr size, bool poison) { Index: compiler-rt/trunk/lib/msan/msan_allocator.cc =================================================================== --- compiler-rt/trunk/lib/msan/msan_allocator.cc +++ compiler-rt/trunk/lib/msan/msan_allocator.cc @@ -33,9 +33,12 @@ // We are about to unmap a chunk of user memory. // Mark the corresponding shadow memory as not needed. - ReleaseMemoryToOS(MEM_TO_SHADOW(p), size); - if (__msan_get_track_origins()) - ReleaseMemoryToOS(MEM_TO_ORIGIN(p), size); + uptr shadow_p = MEM_TO_SHADOW(p); + ReleaseMemoryPagesToOS(shadow_p, shadow_p + size); + if (__msan_get_track_origins()) { + uptr origin_p = MEM_TO_ORIGIN(p); + ReleaseMemoryPagesToOS(origin_p, origin_p + size); + } } }; Index: compiler-rt/trunk/lib/sanitizer_common/sanitizer_allocator_primary64.h =================================================================== --- compiler-rt/trunk/lib/sanitizer_common/sanitizer_allocator_primary64.h +++ compiler-rt/trunk/lib/sanitizer_common/sanitizer_allocator_primary64.h @@ -458,17 +458,11 @@ } } - bool MaybeReleaseChunkRange(uptr region_beg, uptr chunk_size, + void MaybeReleaseChunkRange(uptr region_beg, uptr chunk_size, CompactPtrT first, CompactPtrT last) { uptr beg_ptr = CompactPtrToPointer(region_beg, first); uptr end_ptr = CompactPtrToPointer(region_beg, last) + chunk_size; - const uptr page_size = GetPageSizeCached(); - CHECK_GE(end_ptr - beg_ptr, page_size); - beg_ptr = RoundUpTo(beg_ptr, page_size); - end_ptr = RoundDownTo(end_ptr, page_size); - if (end_ptr == beg_ptr) return false; - ReleaseMemoryToOS(beg_ptr, end_ptr - beg_ptr); - return true; + ReleaseMemoryPagesToOS(beg_ptr, end_ptr); } // Attempts to release some RAM back to OS. The region is expected to be Index: compiler-rt/trunk/lib/sanitizer_common/sanitizer_common.h =================================================================== --- compiler-rt/trunk/lib/sanitizer_common/sanitizer_common.h +++ compiler-rt/trunk/lib/sanitizer_common/sanitizer_common.h @@ -103,7 +103,9 @@ // Used to check if we can map shadow memory to a fixed location. bool MemoryRangeIsAvailable(uptr range_start, uptr range_end); -void ReleaseMemoryToOS(uptr addr, uptr size); +// Releases memory pages entirely within the [beg, end] address range. Noop if +// the provided range does not contain at least one entire page. +void ReleaseMemoryPagesToOS(uptr beg, uptr end); void IncreaseTotalMmap(uptr size); void DecreaseTotalMmap(uptr size); uptr GetRSS(); Index: compiler-rt/trunk/lib/sanitizer_common/sanitizer_posix_libcdep.cc =================================================================== --- compiler-rt/trunk/lib/sanitizer_common/sanitizer_posix_libcdep.cc +++ compiler-rt/trunk/lib/sanitizer_common/sanitizer_posix_libcdep.cc @@ -56,8 +56,12 @@ return (uptr)pthread_self(); } -void ReleaseMemoryToOS(uptr addr, uptr size) { - madvise((void*)addr, size, MADV_DONTNEED); +void ReleaseMemoryPagesToOS(uptr beg, uptr end) { + uptr page_size = GetPageSizeCached(); + uptr beg_aligned = RoundUpTo(beg, page_size); + uptr end_aligned = RoundDownTo(end, page_size); + if (beg_aligned < end_aligned) + madvise((void*)beg_aligned, end_aligned - beg_aligned, MADV_DONTNEED); } void NoHugePagesInRegion(uptr addr, uptr size) { Index: compiler-rt/trunk/lib/sanitizer_common/sanitizer_win.cc =================================================================== --- compiler-rt/trunk/lib/sanitizer_common/sanitizer_win.cc +++ compiler-rt/trunk/lib/sanitizer_common/sanitizer_win.cc @@ -234,8 +234,7 @@ return VirtualProtect((LPVOID)addr, size, PAGE_NOACCESS, &old_protection); } - -void ReleaseMemoryToOS(uptr addr, uptr size) { +void ReleaseMemoryPagesToOS(uptr beg, uptr end) { // This is almost useless on 32-bits. // FIXME: add madvise-analog when we move to 64-bits. } Index: compiler-rt/trunk/lib/tsan/rtl/tsan_mman.cc =================================================================== --- compiler-rt/trunk/lib/tsan/rtl/tsan_mman.cc +++ compiler-rt/trunk/lib/tsan/rtl/tsan_mman.cc @@ -54,7 +54,8 @@ diff = p + size - RoundDown(p + size, kPageSize); if (diff != 0) size -= diff; - ReleaseMemoryToOS((uptr)MemToMeta(p), size / kMetaRatio); + uptr p_meta = (uptr)MemToMeta(p); + ReleaseMemoryPagesToOS(p_meta, p_meta + size / kMetaRatio); } }; Index: compiler-rt/trunk/lib/tsan/rtl/tsan_platform_linux.cc =================================================================== --- compiler-rt/trunk/lib/tsan/rtl/tsan_platform_linux.cc +++ compiler-rt/trunk/lib/tsan/rtl/tsan_platform_linux.cc @@ -134,7 +134,7 @@ void FlushShadowMemoryCallback( const SuspendedThreadsList &suspended_threads_list, void *argument) { - ReleaseMemoryToOS(ShadowBeg(), ShadowEnd() - ShadowBeg()); + ReleaseMemoryPagesToOS(ShadowBeg(), ShadowEnd()); } #endif Index: compiler-rt/trunk/lib/tsan/rtl/tsan_rtl.cc =================================================================== --- compiler-rt/trunk/lib/tsan/rtl/tsan_rtl.cc +++ compiler-rt/trunk/lib/tsan/rtl/tsan_rtl.cc @@ -234,9 +234,7 @@ #endif void DontNeedShadowFor(uptr addr, uptr size) { - uptr shadow_beg = MemToShadow(addr); - uptr shadow_end = MemToShadow(addr + size); - ReleaseMemoryToOS(shadow_beg, shadow_end - shadow_beg); + ReleaseMemoryPagesToOS(MemToShadow(addr), MemToShadow(addr + size)); } void MapShadow(uptr addr, uptr size) { Index: compiler-rt/trunk/lib/tsan/rtl/tsan_rtl_thread.cc =================================================================== --- compiler-rt/trunk/lib/tsan/rtl/tsan_rtl_thread.cc +++ compiler-rt/trunk/lib/tsan/rtl/tsan_rtl_thread.cc @@ -68,7 +68,8 @@ void ThreadContext::OnReset() { CHECK_EQ(sync.size(), 0); - ReleaseMemoryToOS(GetThreadTrace(tid), TraceSize() * sizeof(Event)); + uptr trace_p = GetThreadTrace(tid); + ReleaseMemoryPagesToOS(trace_p, trace_p + TraceSize() * sizeof(Event)); //!!! ReleaseMemoryToOS(GetThreadTraceHeader(tid), sizeof(Trace)); }