augusto2112 updated this revision to Diff 361991.
augusto2112 added a comment.

Keep only filecache and live memory equality assertion


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D106584/new/

https://reviews.llvm.org/D106584

Files:
  lldb/include/lldb/Core/Section.h
  lldb/source/Core/Section.cpp
  lldb/source/Target/Target.cpp


Index: lldb/source/Target/Target.cpp
===================================================================
--- lldb/source/Target/Target.cpp
+++ lldb/source/Target/Target.cpp
@@ -1761,21 +1761,34 @@
   // Read from file cache if read-only section.
   if (!force_live_memory && resolved_addr.IsSectionOffset()) {
     SectionSP section_sp(resolved_addr.GetSection());
-    if (section_sp) {
-      auto permissions = Flags(section_sp->GetPermissions());
-      bool is_readonly = !permissions.Test(ePermissionsWritable) &&
-                         permissions.Test(ePermissionsReadable);
-      if (is_readonly) {
-        file_cache_bytes_read =
-            ReadMemoryFromFileCache(resolved_addr, dst, dst_len, error);
-        if (file_cache_bytes_read == dst_len)
-          return file_cache_bytes_read;
-        else if (file_cache_bytes_read > 0) {
-          file_cache_read_buffer =
-              std::make_unique<uint8_t[]>(file_cache_bytes_read);
-          std::memcpy(file_cache_read_buffer.get(), dst, 
file_cache_bytes_read);
+    if (section_sp && section_sp->IsReadOnly()) {
+      file_cache_bytes_read =
+          ReadMemoryFromFileCache(resolved_addr, dst, dst_len, error);
+
+#ifndef NDEBUG // Assert that file cache matches the process memory
+      if (ProcessIsValid() && file_cache_bytes_read == dst_len) {
+        if (load_addr == LLDB_INVALID_ADDRESS)
+          load_addr = resolved_addr.GetLoadAddress(this);
+        if (load_addr != LLDB_INVALID_ADDRESS) {
+          uint8_t *live_buf = (uint8_t *)malloc(dst_len);
+          bytes_read =
+              m_process_sp->ReadMemory(load_addr, live_buf, dst_len, error);
+          if (bytes_read == dst_len) {
+            assert(memcmp(live_buf, dst, dst_len) == 0 &&
+                   "File cache and live memory diverge!");
+          }
+          free(live_buf);
         }
       }
+#endif
+
+      if (file_cache_bytes_read == dst_len)
+        return file_cache_bytes_read;
+      if (file_cache_bytes_read > 0) {
+        file_cache_read_buffer =
+            std::make_unique<uint8_t[]>(file_cache_bytes_read);
+        std::memcpy(file_cache_read_buffer.get(), dst, file_cache_bytes_read);
+      }
     }
   }
 
Index: lldb/source/Core/Section.cpp
===================================================================
--- lldb/source/Core/Section.cpp
+++ lldb/source/Core/Section.cpp
@@ -599,3 +599,9 @@
   }
   return count;
 }
+
+bool Section::IsReadOnly() {
+  auto permissions = Flags(GetPermissions());
+  return !permissions.Test(ePermissionsWritable) &&
+         permissions.Test(ePermissionsReadable);
+}
Index: lldb/include/lldb/Core/Section.h
===================================================================
--- lldb/include/lldb/Core/Section.h
+++ lldb/include/lldb/Core/Section.h
@@ -236,6 +236,8 @@
 
   void SetIsRelocated(bool b) { m_relocated = b; }
 
+  bool IsReadOnly();
+
 protected:
   ObjectFile *m_obj_file;   // The object file that data for this section 
should
                             // be read from


Index: lldb/source/Target/Target.cpp
===================================================================
--- lldb/source/Target/Target.cpp
+++ lldb/source/Target/Target.cpp
@@ -1761,21 +1761,34 @@
   // Read from file cache if read-only section.
   if (!force_live_memory && resolved_addr.IsSectionOffset()) {
     SectionSP section_sp(resolved_addr.GetSection());
-    if (section_sp) {
-      auto permissions = Flags(section_sp->GetPermissions());
-      bool is_readonly = !permissions.Test(ePermissionsWritable) &&
-                         permissions.Test(ePermissionsReadable);
-      if (is_readonly) {
-        file_cache_bytes_read =
-            ReadMemoryFromFileCache(resolved_addr, dst, dst_len, error);
-        if (file_cache_bytes_read == dst_len)
-          return file_cache_bytes_read;
-        else if (file_cache_bytes_read > 0) {
-          file_cache_read_buffer =
-              std::make_unique<uint8_t[]>(file_cache_bytes_read);
-          std::memcpy(file_cache_read_buffer.get(), dst, file_cache_bytes_read);
+    if (section_sp && section_sp->IsReadOnly()) {
+      file_cache_bytes_read =
+          ReadMemoryFromFileCache(resolved_addr, dst, dst_len, error);
+
+#ifndef NDEBUG // Assert that file cache matches the process memory
+      if (ProcessIsValid() && file_cache_bytes_read == dst_len) {
+        if (load_addr == LLDB_INVALID_ADDRESS)
+          load_addr = resolved_addr.GetLoadAddress(this);
+        if (load_addr != LLDB_INVALID_ADDRESS) {
+          uint8_t *live_buf = (uint8_t *)malloc(dst_len);
+          bytes_read =
+              m_process_sp->ReadMemory(load_addr, live_buf, dst_len, error);
+          if (bytes_read == dst_len) {
+            assert(memcmp(live_buf, dst, dst_len) == 0 &&
+                   "File cache and live memory diverge!");
+          }
+          free(live_buf);
         }
       }
+#endif
+
+      if (file_cache_bytes_read == dst_len)
+        return file_cache_bytes_read;
+      if (file_cache_bytes_read > 0) {
+        file_cache_read_buffer =
+            std::make_unique<uint8_t[]>(file_cache_bytes_read);
+        std::memcpy(file_cache_read_buffer.get(), dst, file_cache_bytes_read);
+      }
     }
   }
 
Index: lldb/source/Core/Section.cpp
===================================================================
--- lldb/source/Core/Section.cpp
+++ lldb/source/Core/Section.cpp
@@ -599,3 +599,9 @@
   }
   return count;
 }
+
+bool Section::IsReadOnly() {
+  auto permissions = Flags(GetPermissions());
+  return !permissions.Test(ePermissionsWritable) &&
+         permissions.Test(ePermissionsReadable);
+}
Index: lldb/include/lldb/Core/Section.h
===================================================================
--- lldb/include/lldb/Core/Section.h
+++ lldb/include/lldb/Core/Section.h
@@ -236,6 +236,8 @@
 
   void SetIsRelocated(bool b) { m_relocated = b; }
 
+  bool IsReadOnly();
+
 protected:
   ObjectFile *m_obj_file;   // The object file that data for this section should
                             // be read from
_______________________________________________
lldb-commits mailing list
lldb-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits

Reply via email to