From f15064871ad933370532f068eca70fb5134ba69f Mon Sep 17 00:00:00 2001 From: David Majnemer Date: Fri, 24 Jun 2016 19:34:41 +0000 Subject: [PATCH] [CodeView] Healthy paranoia around strings Make sure strings don't get too big for a record, truncate them if need-be. llvm-svn: 273710 --- .../llvm/DebugInfo/CodeView/ListRecordBuilder.h | 2 +- .../llvm/DebugInfo/CodeView/TypeRecordBuilder.h | 1 - llvm/lib/DebugInfo/CodeView/ListRecordBuilder.cpp | 2 ++ llvm/lib/DebugInfo/CodeView/TypeRecordBuilder.cpp | 11 +++-------- 4 files changed, 6 insertions(+), 10 deletions(-) diff --git a/llvm/include/llvm/DebugInfo/CodeView/ListRecordBuilder.h b/llvm/include/llvm/DebugInfo/CodeView/ListRecordBuilder.h index cc53b53fe50a..00bf03d417a2 100644 --- a/llvm/include/llvm/DebugInfo/CodeView/ListRecordBuilder.h +++ b/llvm/include/llvm/DebugInfo/CodeView/ListRecordBuilder.h @@ -50,7 +50,7 @@ private: return ContinuationOffsets.empty() ? 0 : ContinuationOffsets.back(); } size_t getLastContinuationEnd() const { return Builder.size(); } - unsigned getLastContinuationSize() const { + size_t getLastContinuationSize() const { return getLastContinuationEnd() - getLastContinuationStart(); } diff --git a/llvm/include/llvm/DebugInfo/CodeView/TypeRecordBuilder.h b/llvm/include/llvm/DebugInfo/CodeView/TypeRecordBuilder.h index 010d06e5e310..eb7993baab89 100644 --- a/llvm/include/llvm/DebugInfo/CodeView/TypeRecordBuilder.h +++ b/llvm/include/llvm/DebugInfo/CodeView/TypeRecordBuilder.h @@ -40,7 +40,6 @@ public: void writeEncodedInteger(int64_t Value); void writeEncodedSignedInteger(int64_t Value); void writeEncodedUnsignedInteger(uint64_t Value); - void writeNullTerminatedString(const char *Value); void writeNullTerminatedString(StringRef Value); void writeGuid(StringRef Guid); void writeBytes(StringRef Value) { Stream << Value; } diff --git a/llvm/lib/DebugInfo/CodeView/ListRecordBuilder.cpp b/llvm/lib/DebugInfo/CodeView/ListRecordBuilder.cpp index dc72f500c879..eb79e8ac9a3f 100644 --- a/llvm/lib/DebugInfo/CodeView/ListRecordBuilder.cpp +++ b/llvm/lib/DebugInfo/CodeView/ListRecordBuilder.cpp @@ -49,8 +49,10 @@ void ListRecordBuilder::finishSubRecord() { // back up and insert a continuation record, sliding the current subrecord // down. if (getLastContinuationSize() > 65535 - 8) { + assert(SubrecordStart != 0 && "can't slide from the start!"); SmallString<128> SubrecordCopy( Builder.str().slice(SubrecordStart, Builder.size())); + assert(SubrecordCopy.size() < 65530 && "subrecord is too large to slide!"); Builder.truncate(SubrecordStart); // Write a placeholder continuation record. diff --git a/llvm/lib/DebugInfo/CodeView/TypeRecordBuilder.cpp b/llvm/lib/DebugInfo/CodeView/TypeRecordBuilder.cpp index f1c293e39fd4..112612cc85ea 100644 --- a/llvm/lib/DebugInfo/CodeView/TypeRecordBuilder.cpp +++ b/llvm/lib/DebugInfo/CodeView/TypeRecordBuilder.cpp @@ -91,15 +91,10 @@ void TypeRecordBuilder::writeEncodedUnsignedInteger(uint64_t Value) { } } -void TypeRecordBuilder::writeNullTerminatedString(const char *Value) { - assert(Value != nullptr); - - size_t Length = strlen(Value); - Stream.write(Value, Length); - writeUInt8(0); -} - void TypeRecordBuilder::writeNullTerminatedString(StringRef Value) { + // Microsoft's linker seems to have trouble with symbol names longer than + // 0xffd8 bytes. + Value = Value.substr(0, 0xffd8); Stream.write(Value.data(), Value.size()); writeUInt8(0); }