[Lldb-commits] [lldb] r323181 - Prevent unaligned memory read in parseMinidumpString

Raphael Isemann via lldb-commits lldb-commits at lists.llvm.org
Tue Jan 23 00:04:27 PST 2018


Author: teemperor
Date: Tue Jan 23 00:04:27 2018
New Revision: 323181

URL: http://llvm.org/viewvc/llvm-project?rev=323181&view=rev
Log:
Prevent unaligned memory read in parseMinidumpString

Summary:
It's possible to hit an unaligned memory read when reading `source_length` as the `data` array is only aligned with 2 bytes (it's actually a UTF16 array). This patch memcpy's `source_length` into a local variable to prevent this:

```
MinidumpTypes.cpp:49:23: runtime error: load of misaligned address 0x7f0f4792692a for type 'const uint32_t' (aka 'const unsigned int'), which requires 4 byte alignment
```

Reviewers: dvlahovski, zturner, davide

Reviewed By: davide

Subscribers: davide, lldb-commits

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

Modified:
    lldb/trunk/source/Plugins/Process/minidump/MinidumpTypes.cpp

Modified: lldb/trunk/source/Plugins/Process/minidump/MinidumpTypes.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Plugins/Process/minidump/MinidumpTypes.cpp?rev=323181&r1=323180&r2=323181&view=diff
==============================================================================
--- lldb/trunk/source/Plugins/Process/minidump/MinidumpTypes.cpp (original)
+++ lldb/trunk/source/Plugins/Process/minidump/MinidumpTypes.cpp Tue Jan 23 00:04:27 2018
@@ -44,9 +44,14 @@ llvm::Optional<std::string>
 lldb_private::minidump::parseMinidumpString(llvm::ArrayRef<uint8_t> &data) {
   std::string result;
 
-  const uint32_t *source_length;
-  Status error = consumeObject(data, source_length);
-  if (error.Fail() || *source_length > data.size() || *source_length % 2 != 0)
+  const uint32_t *source_length_ptr;
+  Status error = consumeObject(data, source_length_ptr);
+
+  // Copy non-aligned source_length data into aligned memory.
+  uint32_t source_length;
+  std::memcpy(&source_length, source_length_ptr, sizeof(source_length));
+
+  if (error.Fail() || source_length > data.size() || source_length % 2 != 0)
     return llvm::None;
 
   auto source_start = reinterpret_cast<const llvm::UTF16 *>(data.data());
@@ -54,9 +59,9 @@ lldb_private::minidump::parseMinidumpStr
   // we need the length of the string in UTF-16 characters/code points (16 bits
   // per char)
   // that's why it's divided by 2
-  const auto source_end = source_start + (*source_length) / 2;
+  const auto source_end = source_start + source_length / 2;
   // resize to worst case length
-  result.resize(UNI_MAX_UTF8_BYTES_PER_CODE_POINT * (*source_length) / 2);
+  result.resize(UNI_MAX_UTF8_BYTES_PER_CODE_POINT * source_length / 2);
   auto result_start = reinterpret_cast<llvm::UTF8 *>(&result[0]);
   const auto result_end = result_start + result.size();
   llvm::ConvertUTF16toUTF8(&source_start, source_end, &result_start, result_end,




More information about the lldb-commits mailing list