Bitcode: Use unsigned char to record MDStrings
authorDuncan P. N. Exon Smith <dexonsmith@apple.com>
Thu, 11 Dec 2014 23:34:30 +0000 (23:34 +0000)
committerDuncan P. N. Exon Smith <dexonsmith@apple.com>
Thu, 11 Dec 2014 23:34:30 +0000 (23:34 +0000)
`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.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@224077 91177308-0d34-0410-b5e6-96231b3b80d8

include/llvm/ADT/StringRef.h
include/llvm/IR/Metadata.h
lib/Bitcode/Writer/BitcodeWriter.cpp
test/Bitcode/mdstring-high-bits.ll [new file with mode: 0644]

index 778fa10a32c252e7e5bd22bad634ce1dddb804c7..6111c42da9dced36ed905fb0e715865b0f4cdd91 100644 (file)
@@ -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
     /// @{
index 5e1b294fa1ffc3b8f7d3138f70ff62eee479fe26..fdcff4d6237413b16edb4707327416b4be456c95 100644 (file)
@@ -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;
index 90ac1f19b7d297cf2fbb00e501e5e5744bf4cef0..c349d1053ded2b5a3c1f5873345defceb262eae7 100644 (file)
@@ -800,7 +800,7 @@ static void WriteModuleMetadata(const Module *M,
       }
 
       // 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);
diff --git a/test/Bitcode/mdstring-high-bits.ll b/test/Bitcode/mdstring-high-bits.ll
new file mode 100644 (file)
index 0000000..ffc4f6e
--- /dev/null
@@ -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"}