[Lldb-commits] [PATCH] D106584: [lldb] Improve checking of file cache read eligibility for mach-O

Augusto Noronha via Phabricator via lldb-commits lldb-commits at lists.llvm.org
Tue Jul 27 05:28:47 PDT 2021


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


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D106584.361991.patch
Type: text/x-patch
Size: 3025 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/lldb-commits/attachments/20210727/5cd727e1/attachment.bin>


More information about the lldb-commits mailing list