Skip to content

Commit 256e616

Browse files
committed
[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
1 parent 4a966e5 commit 256e616

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

lldb/source/Core/Module.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,9 @@ ObjectFile *Module::GetMemoryObjectFile(const lldb::ProcessSP &process_sp,
297297
const size_t bytes_read =
298298
process_sp->ReadMemory(header_addr, data_up->GetBytes(),
299299
data_up->GetByteSize(), readmem_error);
300-
if (bytes_read == size_to_read) {
300+
if (bytes_read < size_to_read)
301+
data_up->SetByteSize(bytes_read);
302+
if (data_up->GetByteSize() > 0) {
301303
DataBufferSP data_sp(data_up.release());
302304
m_objfile_sp = ObjectFile::FindPlugin(shared_from_this(), process_sp,
303305
header_addr, data_sp);

lldb/source/Target/Memory.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,8 +232,13 @@ size_t MemoryCache::Read(addr_t addr, void *dst, size_t dst_len,
232232
if (process_bytes_read == 0)
233233
return dst_len - bytes_left;
234234

235-
if (process_bytes_read != cache_line_byte_size)
235+
if (process_bytes_read != cache_line_byte_size) {
236+
if (process_bytes_read < data_buffer_heap_up->GetByteSize()) {
237+
dst_len -= data_buffer_heap_up->GetByteSize() - process_bytes_read;
238+
bytes_left = process_bytes_read;
239+
}
236240
data_buffer_heap_up->SetByteSize(process_bytes_read);
241+
}
237242
m_L2_cache[curr_addr] = DataBufferSP(data_buffer_heap_up.release());
238243
// We have read data and put it into the cache, continue through the
239244
// loop again to get the data out of the cache...

0 commit comments

Comments
 (0)