[compiler-rt] bfa0252 - [scudo] The BaseAddr should be MappedBase in releasePagesToOS()

Chia-hung Duan via llvm-commits llvm-commits at lists.llvm.org
Fri Apr 14 12:19:23 PDT 2023


Author: Chia-hung Duan
Date: 2023-04-14T19:18:55Z
New Revision: bfa02523b2e7ed66368ea61866a474e55ef354a3

URL: https://github.com/llvm/llvm-project/commit/bfa02523b2e7ed66368ea61866a474e55ef354a3
DIFF: https://github.com/llvm/llvm-project/commit/bfa02523b2e7ed66368ea61866a474e55ef354a3.diff

LOG: [scudo] The BaseAddr should be MappedBase in releasePagesToOS()

This is used to make MemMapDefault be compliant with legacy APIs.

Reviewed By: fabio-d

Differential Revision: https://reviews.llvm.org/D148141

Added: 
    

Modified: 
    compiler-rt/lib/scudo/standalone/mem_map.cpp
    compiler-rt/lib/scudo/standalone/mem_map.h

Removed: 
    


################################################################################
diff  --git a/compiler-rt/lib/scudo/standalone/mem_map.cpp b/compiler-rt/lib/scudo/standalone/mem_map.cpp
index 662d684e5099a..115cc34e70600 100644
--- a/compiler-rt/lib/scudo/standalone/mem_map.cpp
+++ b/compiler-rt/lib/scudo/standalone/mem_map.cpp
@@ -19,16 +19,19 @@ bool MemMapDefault::mapImpl(uptr Addr, uptr Size, const char *Name,
   if (MappedAddr == nullptr)
     return false;
   Base = reinterpret_cast<uptr>(MappedAddr);
+  MappedBase = Base;
   Capacity = Size;
   return true;
 }
 
 void MemMapDefault::unmapImpl(uptr Addr, uptr Size) {
   if (Size == Capacity) {
-    Base = Capacity = 0;
+    Base = MappedBase = Capacity = 0;
   } else {
-    if (Base == Addr)
+    if (Base == Addr) {
       Base = Addr + Size;
+      MappedBase = MappedBase == 0 ? Base : Max(MappedBase, Base);
+    }
     Capacity -= Size;
   }
 
@@ -37,13 +40,17 @@ void MemMapDefault::unmapImpl(uptr Addr, uptr Size) {
 
 bool MemMapDefault::remapImpl(uptr Addr, uptr Size, const char *Name,
                               uptr Flags) {
-  void *RemappedAddr =
+  void *RemappedPtr =
       ::scudo::map(reinterpret_cast<void *>(Addr), Size, Name, Flags, &Data);
-  return reinterpret_cast<uptr>(RemappedAddr) == Addr;
+  const uptr RemappedAddr = reinterpret_cast<uptr>(RemappedPtr);
+  MappedBase = MappedBase == 0 ? RemappedAddr : Min(MappedBase, RemappedAddr);
+  return RemappedAddr == Addr;
 }
 
 void MemMapDefault::releaseAndZeroPagesToOSImpl(uptr From, uptr Size) {
-  return ::scudo::releasePagesToOS(Base, From - Base, Size, &Data);
+  DCHECK_NE(MappedBase, 0U);
+  DCHECK_GE(From, MappedBase);
+  return ::scudo::releasePagesToOS(MappedBase, From - MappedBase, Size, &Data);
 }
 
 void MemMapDefault::setMemoryPermissionImpl(uptr Addr, uptr Size, uptr Flags) {

diff  --git a/compiler-rt/lib/scudo/standalone/mem_map.h b/compiler-rt/lib/scudo/standalone/mem_map.h
index 6179e8aa0aaaa..0b27fa86c0d78 100644
--- a/compiler-rt/lib/scudo/standalone/mem_map.h
+++ b/compiler-rt/lib/scudo/standalone/mem_map.h
@@ -47,6 +47,7 @@ class MemMapDefault final : public MemMapBase<MemMapDefault> {
 private:
   uptr Base = 0;
   uptr Capacity = 0;
+  uptr MappedBase = 0;
   MapPlatformData Data = {};
 };
 


        


More information about the llvm-commits mailing list