Skip to content

Commit 60ff1e4

Browse files
Merge pull request #4093 from adrian-prantl/overflow
Guard against unsigned overflow.
2 parents f934c4c + df5acb7 commit 60ff1e4

File tree

1 file changed

+44
-34
lines changed

1 file changed

+44
-34
lines changed

lldb/source/Plugins/LanguageRuntime/Swift/LLDBMemoryReader.cpp

Lines changed: 44 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -196,9 +196,9 @@ LLDBMemoryReader::resolvePointer(swift::remote::RemoteAddress address,
196196
// We haven't registered the image that contains the pointer.
197197
if (pair_iterator == m_range_module_map.end()) {
198198
LLDB_LOG(log,
199-
"[MemoryReader] Could not resolve find module containing pointer "
200-
"{0:x} read from {1:x}.",
201-
readValue, address.getAddressData());
199+
"[MemoryReader] Could not resolve find module containing pointer "
200+
"{0:x} read from {1:x}.",
201+
readValue, address.getAddressData());
202202
return process_pointer;
203203
}
204204

@@ -230,10 +230,18 @@ LLDBMemoryReader::resolvePointer(swift::remote::RemoteAddress address,
230230

231231
bool LLDBMemoryReader::readBytes(swift::remote::RemoteAddress address,
232232
uint8_t *dest, uint64_t size) {
233+
Log *log = GetLog(LLDBLog::Types);
233234
if (m_local_buffer) {
235+
bool overflow = false;
234236
auto addr = address.getAddressData();
237+
auto end = llvm::SaturatingAdd(addr, size, &overflow);
238+
if (overflow) {
239+
LLDB_LOGV(log, "[MemoryReader] address {0:x} + size {1} overflows", addr,
240+
size);
241+
return false;
242+
}
235243
if (addr >= *m_local_buffer &&
236-
addr + size <= *m_local_buffer + m_local_buffer_size) {
244+
end <= *m_local_buffer + m_local_buffer_size) {
237245
// If this crashes, the assumptions stated in
238246
// GetDynamicTypeAndAddress_Protocol() most likely no longer
239247
// hold.
@@ -242,16 +250,13 @@ bool LLDBMemoryReader::readBytes(swift::remote::RemoteAddress address,
242250
}
243251
}
244252

245-
Log *log = GetLog(LLDBLog::Types);
246-
247253
LLDB_LOGV(log, "[MemoryReader] asked to read {0} bytes at address {1:x}",
248254
size, address.getAddressData());
249255
llvm::Optional<Address> maybeAddr =
250256
resolveRemoteAddressFromSymbolObjectFile(address.getAddressData());
251257

252-
if (!maybeAddr)
253-
maybeAddr =
254-
resolveRemoteAddress(address.getAddressData());
258+
if (!maybeAddr)
259+
maybeAddr = resolveRemoteAddress(address.getAddressData());
255260

256261
if (!maybeAddr) {
257262
LLDB_LOGV(log, "[MemoryReader] could not resolve address {0:x}",
@@ -276,7 +281,7 @@ bool LLDBMemoryReader::readBytes(swift::remote::RemoteAddress address,
276281
}
277282
Target &target(m_process.GetTarget());
278283
Status error;
279-
// We only want to allow the file-cache optimization if we resolved the
284+
// We only want to allow the file-cache optimization if we resolved the
280285
// address to section + offset.
281286
const bool force_live_memory =
282287
!readMetadataFromFileCacheEnabled() || !addr.IsSectionOffset();
@@ -309,27 +314,26 @@ bool LLDBMemoryReader::readString(swift::remote::RemoteAddress address,
309314
std::string &dest) {
310315
Log *log = GetLog(LLDBLog::Types);
311316

312-
auto format_string = [](const std::string &dest) {
313-
StreamString stream;
314-
for (auto c : dest) {
315-
if (c >= 32 && c <= 127) {
316-
stream << c;
317-
} else {
318-
stream << "\\0";
319-
stream.PutHex8(c);
320-
}
317+
auto format_string = [](const std::string &dest) {
318+
StreamString stream;
319+
for (auto c : dest) {
320+
if (c >= 32 && c <= 127) {
321+
stream << c;
322+
} else {
323+
stream << "\\0";
324+
stream.PutHex8(c);
321325
}
322-
return std::string(stream.GetData());
323-
};
326+
}
327+
return std::string(stream.GetData());
328+
};
324329
LLDB_LOGV(log, "[MemoryReader] asked to read string data at address {0:x}",
325330
address.getAddressData());
326331

327332
llvm::Optional<Address> maybeAddr =
328333
resolveRemoteAddressFromSymbolObjectFile(address.getAddressData());
329334

330-
if (!maybeAddr)
331-
maybeAddr =
332-
resolveRemoteAddress(address.getAddressData());
335+
if (!maybeAddr)
336+
maybeAddr = resolveRemoteAddress(address.getAddressData());
333337

334338
if (!maybeAddr) {
335339
LLDB_LOGV(log, "[MemoryReader] could not resolve address {0:x}",
@@ -344,15 +348,15 @@ bool LLDBMemoryReader::readString(swift::remote::RemoteAddress address,
344348
LLDB_LOGV(log, "[MemoryReader] Reading memory from symbol rich binary");
345349

346350
dest = object_file->GetCStrFromSection(section.get(), addr.GetOffset());
347-
LLDB_LOGV(log, "[MemoryReader] memory read returned string: \"{0}\"",
348-
format_string(dest));
351+
LLDB_LOGV(log, "[MemoryReader] memory read returned string: \"{0}\"",
352+
format_string(dest));
349353
return true;
350354
}
351355
}
352356

353357
Target &target(m_process.GetTarget());
354358
Status error;
355-
// We only want to allow the file-cache optimization if we resolved the
359+
// We only want to allow the file-cache optimization if we resolved the
356360
// address to section + offset.
357361
const bool force_live_memory =
358362
!readMetadataFromFileCacheEnabled() || !addr.IsSectionOffset();
@@ -381,7 +385,8 @@ void LLDBMemoryReader::popLocalBuffer() {
381385
}
382386

383387
llvm::Optional<std::pair<uint64_t, uint64_t>>
384-
LLDBMemoryReader::addModuleToAddressMap(ModuleSP module, bool register_symbol_obj_file) {
388+
LLDBMemoryReader::addModuleToAddressMap(ModuleSP module,
389+
bool register_symbol_obj_file) {
385390
if (!readMetadataFromFileCacheEnabled())
386391
return {};
387392

@@ -402,11 +407,10 @@ LLDBMemoryReader::addModuleToAddressMap(ModuleSP module, bool register_symbol_ob
402407

403408
#ifndef NDEBUG
404409
static std::initializer_list<uint64_t> objc_bits = {
405-
SWIFT_ABI_ARM_IS_OBJC_BIT,
406-
SWIFT_ABI_X86_64_IS_OBJC_BIT,
410+
SWIFT_ABI_ARM_IS_OBJC_BIT, SWIFT_ABI_X86_64_IS_OBJC_BIT,
407411
SWIFT_ABI_ARM64_IS_OBJC_BIT};
408412

409-
for (auto objc_bit : objc_bits)
413+
for (auto objc_bit : objc_bits)
410414
assert((module_start_address & objc_bit) != objc_bit &&
411415
"LLDB file address bit clashes with an obj-c bit!");
412416
#endif
@@ -523,7 +527,9 @@ LLDBMemoryReader::resolveRemoteAddress(uint64_t address) const {
523527
return resolved;
524528
}
525529

526-
llvm::Optional<Address> LLDBMemoryReader::resolveRemoteAddressFromSymbolObjectFile(uint64_t address) const {
530+
llvm::Optional<Address>
531+
LLDBMemoryReader::resolveRemoteAddressFromSymbolObjectFile(
532+
uint64_t address) const {
527533
Log *log(GetLog(LLDBLog::Types));
528534

529535
if (!m_process.GetTarget().GetSwiftReadMetadataFromDSYM())
@@ -555,8 +561,12 @@ llvm::Optional<Address> LLDBMemoryReader::resolveRemoteAddressFromSymbolObjectFi
555561
file_address, object_file->GetFileSpec().GetFilename());
556562
return {};
557563
}
558-
559-
if (!resolved.GetSection()->GetParent()->GetName().GetStringRef().contains_insensitive("DWARF")) {
564+
565+
if (!resolved.GetSection()
566+
->GetParent()
567+
->GetName()
568+
.GetStringRef()
569+
.contains_insensitive("DWARF")) {
560570
auto *main_object_file = module->GetObjectFile();
561571
resolved = Address(file_address, main_object_file->GetSectionList());
562572
}

0 commit comments

Comments
 (0)