Author: Paolo Severini
Date: 2020-02-27T11:17:10-08:00
New Revision: 256e61699b19c8e3545c948547c12872a8567250

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

LOG: [LLDB] Fix AddressSanitizer failure in MemoryCache

The lldb sanitizer bot is flagging a container-overflow error after we
introduced test TestWasm.py. MemoryCache::Read didn't behave correctly
in case of partial reads that can happen with object files whose size is
smaller that the cache size. It should return the actual number of bytes
read and not try to fill the buffer with random memory.
Module::GetMemoryObjectFile needs to be modified accordingly, to resize
its buffer to only the size that was read.

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

Added: 
    

Modified: 
    lldb/source/Core/Module.cpp
    lldb/source/Target/Memory.cpp

Removed: 
    


################################################################################
diff  --git a/lldb/source/Core/Module.cpp b/lldb/source/Core/Module.cpp
index e917980791c2..cff74dc9d518 100644
--- a/lldb/source/Core/Module.cpp
+++ b/lldb/source/Core/Module.cpp
@@ -297,7 +297,9 @@ ObjectFile *Module::GetMemoryObjectFile(const 
lldb::ProcessSP &process_sp,
       const size_t bytes_read =
           process_sp->ReadMemory(header_addr, data_up->GetBytes(),
                                  data_up->GetByteSize(), readmem_error);
-      if (bytes_read == size_to_read) {
+      if (bytes_read < size_to_read)
+        data_up->SetByteSize(bytes_read);
+      if (data_up->GetByteSize() > 0) {
         DataBufferSP data_sp(data_up.release());
         m_objfile_sp = ObjectFile::FindPlugin(shared_from_this(), process_sp,
                                               header_addr, data_sp);

diff  --git a/lldb/source/Target/Memory.cpp b/lldb/source/Target/Memory.cpp
index 9e1b7245bd51..a7ed1a3d97b7 100644
--- a/lldb/source/Target/Memory.cpp
+++ b/lldb/source/Target/Memory.cpp
@@ -232,8 +232,13 @@ size_t MemoryCache::Read(addr_t addr, void *dst, size_t 
dst_len,
         if (process_bytes_read == 0)
           return dst_len - bytes_left;
 
-        if (process_bytes_read != cache_line_byte_size)
+        if (process_bytes_read != cache_line_byte_size) {
+          if (process_bytes_read < data_buffer_heap_up->GetByteSize()) {
+            dst_len -= data_buffer_heap_up->GetByteSize() - process_bytes_read;
+            bytes_left = process_bytes_read;
+          }
           data_buffer_heap_up->SetByteSize(process_bytes_read);
+        }
         m_L2_cache[curr_addr] = DataBufferSP(data_buffer_heap_up.release());
         // We have read data and put it into the cache, continue through the
         // loop again to get the data out of the cache...


        
_______________________________________________
lldb-commits mailing list
lldb-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits

Reply via email to