[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

GitOrigin-RevId: bfa02523b2e7ed66368ea61866a474e55ef354a3
diff --git a/mem_map.cpp b/mem_map.cpp
index 662d684..115cc34 100644
--- a/mem_map.cpp
+++ b/mem_map.cpp
@@ -19,16 +19,19 @@
   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 @@
 
 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/mem_map.h b/mem_map.h
index 6179e8a..0b27fa8 100644
--- a/mem_map.h
+++ b/mem_map.h
@@ -47,6 +47,7 @@
 private:
   uptr Base = 0;
   uptr Capacity = 0;
+  uptr MappedBase = 0;
   MapPlatformData Data = {};
 };