From: Duncan P. N. Exon Smith Date: Thu, 11 Dec 2014 23:34:30 +0000 (+0000) Subject: Bitcode: Use unsigned char to record MDStrings X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=eca1e031d152fa10861919bafacb769513ce3d2c;p=platform%2Fupstream%2Fllvm.git 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. llvm-svn: 224077 --- diff --git a/llvm/include/llvm/ADT/StringRef.h b/llvm/include/llvm/ADT/StringRef.h index 778fa10..6111c42 100644 --- a/llvm/include/llvm/ADT/StringRef.h +++ b/llvm/include/llvm/ADT/StringRef.h @@ -91,6 +91,13 @@ namespace llvm { iterator end() const { return Data + Length; } + const unsigned char *bytes_begin() const { + return reinterpret_cast(begin()); + } + const unsigned char *bytes_end() const { + return reinterpret_cast(end()); + } + /// @} /// @name String Operations /// @{ diff --git a/llvm/include/llvm/IR/Metadata.h b/llvm/include/llvm/IR/Metadata.h index 5e1b294..fdcff4d 100644 --- a/llvm/include/llvm/IR/Metadata.h +++ b/llvm/include/llvm/IR/Metadata.h @@ -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; diff --git a/llvm/lib/Bitcode/Writer/BitcodeWriter.cpp b/llvm/lib/Bitcode/Writer/BitcodeWriter.cpp index 90ac1f1..c349d10 100644 --- a/llvm/lib/Bitcode/Writer/BitcodeWriter.cpp +++ b/llvm/lib/Bitcode/Writer/BitcodeWriter.cpp @@ -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/llvm/test/Bitcode/mdstring-high-bits.ll b/llvm/test/Bitcode/mdstring-high-bits.ll new file mode 100644 index 0000000..ffc4f6e --- /dev/null +++ b/llvm/test/Bitcode/mdstring-high-bits.ll @@ -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"}