[llvm] r224077 - Bitcode: Use unsigned char to record MDStrings

Duncan P. N. Exon Smith dexonsmith at apple.com
Thu Dec 11 15:34:30 PST 2014


Author: dexonsmith
Date: Thu Dec 11 17:34:30 2014
New Revision: 224077

URL: http://llvm.org/viewvc/llvm-project?rev=224077&view=rev
Log:
Bitcode: Use unsigned char to record MDStrings

`MDString`s can have arbitrary characters in them.  Prevent an assertion
that fired in `BitcodeWriter` because of sign extension by copying the
characters into the record as `unsigned char`s.

Based on a patch by Keno Fischer; fixes PR21882.

Added:
    llvm/trunk/test/Bitcode/mdstring-high-bits.ll
Modified:
    llvm/trunk/include/llvm/ADT/StringRef.h
    llvm/trunk/include/llvm/IR/Metadata.h
    llvm/trunk/lib/Bitcode/Writer/BitcodeWriter.cpp

Modified: llvm/trunk/include/llvm/ADT/StringRef.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/ADT/StringRef.h?rev=224077&r1=224076&r2=224077&view=diff
==============================================================================
--- llvm/trunk/include/llvm/ADT/StringRef.h (original)
+++ llvm/trunk/include/llvm/ADT/StringRef.h Thu Dec 11 17:34:30 2014
@@ -91,6 +91,13 @@ namespace llvm {
 
     iterator end() const { return Data + Length; }
 
+    const unsigned char *bytes_begin() const {
+      return reinterpret_cast<const unsigned char *>(begin());
+    }
+    const unsigned char *bytes_end() const {
+      return reinterpret_cast<const unsigned char *>(end());
+    }
+
     /// @}
     /// @name String Operations
     /// @{

Modified: llvm/trunk/include/llvm/IR/Metadata.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/IR/Metadata.h?rev=224077&r1=224076&r2=224077&view=diff
==============================================================================
--- llvm/trunk/include/llvm/IR/Metadata.h (original)
+++ llvm/trunk/include/llvm/IR/Metadata.h Thu Dec 11 17:34:30 2014
@@ -459,6 +459,9 @@ public:
   /// \brief Pointer to one byte past the end of the string.
   iterator end() const { return getString().end(); }
 
+  const unsigned char *bytes_begin() const { return getString().bytes_begin(); }
+  const unsigned char *bytes_end() const { return getString().bytes_end(); }
+
   /// \brief Methods for support type inquiry through isa, cast, and dyn_cast.
   static bool classof(const Metadata *MD) {
     return MD->getMetadataID() == MDStringKind;

Modified: llvm/trunk/lib/Bitcode/Writer/BitcodeWriter.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Bitcode/Writer/BitcodeWriter.cpp?rev=224077&r1=224076&r2=224077&view=diff
==============================================================================
--- llvm/trunk/lib/Bitcode/Writer/BitcodeWriter.cpp (original)
+++ llvm/trunk/lib/Bitcode/Writer/BitcodeWriter.cpp Thu Dec 11 17:34:30 2014
@@ -800,7 +800,7 @@ static void WriteModuleMetadata(const Mo
       }
 
       // Code: [strchar x N]
-      Record.append(MDS->begin(), MDS->end());
+      Record.append(MDS->bytes_begin(), MDS->bytes_end());
 
       // Emit the finished record.
       Stream.EmitRecord(bitc::METADATA_STRING, Record, MDSAbbrev);

Added: llvm/trunk/test/Bitcode/mdstring-high-bits.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Bitcode/mdstring-high-bits.ll?rev=224077&view=auto
==============================================================================
--- llvm/trunk/test/Bitcode/mdstring-high-bits.ll (added)
+++ llvm/trunk/test/Bitcode/mdstring-high-bits.ll Thu Dec 11 17:34:30 2014
@@ -0,0 +1,9 @@
+; RUN: llvm-as < %s | llvm-dis | FileCheck %s
+
+; PR21882: confirm we don't crash when high bits are set in a character in a
+; metadata string.
+
+; CHECK: !name = !{!0}
+!name = !{!0}
+; CHECK: !0 = metadata !{metadata !"\80"}
+!0 = metadata !{metadata !"\80"}





More information about the llvm-commits mailing list