diff options
author | Duncan P. N. Exon Smith <dexonsmith@apple.com> | 2015-03-27 23:05:04 +0000 |
---|---|---|
committer | Duncan P. N. Exon Smith <dexonsmith@apple.com> | 2015-03-27 23:05:04 +0000 |
commit | 53855f05d315ccfbdc14d0ef2230b008a8e44adc (patch) | |
tree | 092e3039f9dc7366982f99b185da72ef0029f8a1 /llvm/lib/IR | |
parent | d9ccfb9e0160dc91afafc665b45c1121cc20302c (diff) | |
download | bcm5719-llvm-53855f05d315ccfbdc14d0ef2230b008a8e44adc.tar.gz bcm5719-llvm-53855f05d315ccfbdc14d0ef2230b008a8e44adc.zip |
Verifier: Check operands of MDType subclasses and MDCompileUnit
Add verify checks for `MDType` subclasses and for `MDCompileUnit`.
These new checks don't yet incorporate everything from `Verify()`, but
at least they sanity check the operands. Also downcast accessors as
possible.
A lot of these accessors can't be downcast as far as we'd like because
of arrays of typed objects (stored in a generic `MDTuple`) and
`MDString`-based type references. Eventually I'll port over `DIRef<>`
and `DITypedArray<>` from `DebugInfo.h` to clean those up as well.
Updated bitrotted testcases separately in r233415 and r233443 to reduce
churn on the off-chance this needs to be reverted.
llvm-svn: 233446
Diffstat (limited to 'llvm/lib/IR')
-rw-r--r-- | llvm/lib/IR/AsmWriter.cpp | 35 | ||||
-rw-r--r-- | llvm/lib/IR/DIBuilder.cpp | 4 | ||||
-rw-r--r-- | llvm/lib/IR/LLVMContextImpl.h | 69 | ||||
-rw-r--r-- | llvm/lib/IR/Verifier.cpp | 80 |
4 files changed, 135 insertions, 53 deletions
diff --git a/llvm/lib/IR/AsmWriter.cpp b/llvm/lib/IR/AsmWriter.cpp index 460e3f1b619..a3dde894070 100644 --- a/llvm/lib/IR/AsmWriter.cpp +++ b/llvm/lib/IR/AsmWriter.cpp @@ -1539,16 +1539,16 @@ static void writeMDDerivedType(raw_ostream &Out, const MDDerivedType *N, MDFieldPrinter Printer(Out, TypePrinter, Machine, Context); Printer.printTag(N); Printer.printString("name", N->getName()); - Printer.printMetadata("scope", N->getScope()); - Printer.printMetadata("file", N->getFile()); + Printer.printMetadata("scope", N->getRawScope()); + Printer.printMetadata("file", N->getRawFile()); Printer.printInt("line", N->getLine()); - Printer.printMetadata("baseType", N->getBaseType(), + Printer.printMetadata("baseType", N->getRawBaseType(), /* ShouldSkipNull */ false); Printer.printInt("size", N->getSizeInBits()); Printer.printInt("align", N->getAlignInBits()); Printer.printInt("offset", N->getOffsetInBits()); Printer.printDIFlags("flags", N->getFlags()); - Printer.printMetadata("extraData", N->getExtraData()); + Printer.printMetadata("extraData", N->getRawExtraData()); Out << ")"; } @@ -1559,19 +1559,19 @@ static void writeMDCompositeType(raw_ostream &Out, const MDCompositeType *N, MDFieldPrinter Printer(Out, TypePrinter, Machine, Context); Printer.printTag(N); Printer.printString("name", N->getName()); - Printer.printMetadata("scope", N->getScope()); - Printer.printMetadata("file", N->getFile()); + Printer.printMetadata("scope", N->getRawScope()); + Printer.printMetadata("file", N->getRawFile()); Printer.printInt("line", N->getLine()); - Printer.printMetadata("baseType", N->getBaseType()); + Printer.printMetadata("baseType", N->getRawBaseType()); Printer.printInt("size", N->getSizeInBits()); Printer.printInt("align", N->getAlignInBits()); Printer.printInt("offset", N->getOffsetInBits()); Printer.printDIFlags("flags", N->getFlags()); - Printer.printMetadata("elements", N->getElements()); + Printer.printMetadata("elements", N->getRawElements()); Printer.printDwarfEnum("runtimeLang", N->getRuntimeLang(), dwarf::LanguageString); - Printer.printMetadata("vtableHolder", N->getVTableHolder()); - Printer.printMetadata("templateParams", N->getTemplateParams()); + Printer.printMetadata("vtableHolder", N->getRawVTableHolder()); + Printer.printMetadata("templateParams", N->getRawTemplateParams()); Printer.printString("identifier", N->getIdentifier()); Out << ")"; } @@ -1582,7 +1582,8 @@ static void writeMDSubroutineType(raw_ostream &Out, const MDSubroutineType *N, Out << "!MDSubroutineType("; MDFieldPrinter Printer(Out, TypePrinter, Machine, Context); Printer.printDIFlags("flags", N->getFlags()); - Printer.printMetadata("types", N->getTypeArray(), /* ShouldSkipNull */ false); + Printer.printMetadata("types", N->getRawTypeArray(), + /* ShouldSkipNull */ false); Out << ")"; } @@ -1604,7 +1605,7 @@ static void writeMDCompileUnit(raw_ostream &Out, const MDCompileUnit *N, MDFieldPrinter Printer(Out, TypePrinter, Machine, Context); Printer.printDwarfEnum("language", N->getSourceLanguage(), dwarf::LanguageString, /* ShouldSkipZero */ false); - Printer.printMetadata("file", N->getFile(), /* ShouldSkipNull */ false); + Printer.printMetadata("file", N->getRawFile(), /* ShouldSkipNull */ false); Printer.printString("producer", N->getProducer()); Printer.printBool("isOptimized", N->isOptimized()); Printer.printString("flags", N->getFlags()); @@ -1613,11 +1614,11 @@ static void writeMDCompileUnit(raw_ostream &Out, const MDCompileUnit *N, Printer.printString("splitDebugFilename", N->getSplitDebugFilename()); Printer.printInt("emissionKind", N->getEmissionKind(), /* ShouldSkipZero */ false); - Printer.printMetadata("enums", N->getEnumTypes()); - Printer.printMetadata("retainedTypes", N->getRetainedTypes()); - Printer.printMetadata("subprograms", N->getSubprograms()); - Printer.printMetadata("globals", N->getGlobalVariables()); - Printer.printMetadata("imports", N->getImportedEntities()); + Printer.printMetadata("enums", N->getRawEnumTypes()); + Printer.printMetadata("retainedTypes", N->getRawRetainedTypes()); + Printer.printMetadata("subprograms", N->getRawSubprograms()); + Printer.printMetadata("globals", N->getRawGlobalVariables()); + Printer.printMetadata("imports", N->getRawImportedEntities()); Out << ")"; } diff --git a/llvm/lib/IR/DIBuilder.cpp b/llvm/lib/IR/DIBuilder.cpp index 4b837af9d44..dfbc1315218 100644 --- a/llvm/lib/IR/DIBuilder.cpp +++ b/llvm/lib/IR/DIBuilder.cpp @@ -269,8 +269,8 @@ DIDerivedType DIBuilder::createReferenceType(unsigned Tag, DIType RTy) { DIDerivedType DIBuilder::createTypedef(DIType Ty, StringRef Name, DIFile File, unsigned LineNo, DIDescriptor Context) { - return MDDerivedType::get(VMContext, dwarf::DW_TAG_typedef, Name, - File.getFileNode(), LineNo, + return MDDerivedType::get(VMContext, dwarf::DW_TAG_typedef, Name, File, + LineNo, DIScope(getNonCompileUnitScope(Context)).getRef(), Ty.getRef(), 0, 0, 0, 0); } diff --git a/llvm/lib/IR/LLVMContextImpl.h b/llvm/lib/IR/LLVMContextImpl.h index d735c33d2ac..21999822346 100644 --- a/llvm/lib/IR/LLVMContextImpl.h +++ b/llvm/lib/IR/LLVMContextImpl.h @@ -347,20 +347,20 @@ template <> struct MDNodeKeyImpl<MDDerivedType> { BaseType(BaseType), SizeInBits(SizeInBits), AlignInBits(AlignInBits), OffsetInBits(OffsetInBits), Flags(Flags), ExtraData(ExtraData) {} MDNodeKeyImpl(const MDDerivedType *N) - : Tag(N->getTag()), Name(N->getName()), File(N->getFile()), - Line(N->getLine()), Scope(N->getScope()), BaseType(N->getBaseType()), - SizeInBits(N->getSizeInBits()), AlignInBits(N->getAlignInBits()), - OffsetInBits(N->getOffsetInBits()), Flags(N->getFlags()), - ExtraData(N->getExtraData()) {} + : Tag(N->getTag()), Name(N->getName()), File(N->getRawFile()), + Line(N->getLine()), Scope(N->getRawScope()), + BaseType(N->getRawBaseType()), SizeInBits(N->getSizeInBits()), + AlignInBits(N->getAlignInBits()), OffsetInBits(N->getOffsetInBits()), + Flags(N->getFlags()), ExtraData(N->getRawExtraData()) {} bool isKeyOf(const MDDerivedType *RHS) const { return Tag == RHS->getTag() && Name == RHS->getName() && - File == RHS->getFile() && Line == RHS->getLine() && - Scope == RHS->getScope() && BaseType == RHS->getBaseType() && + File == RHS->getRawFile() && Line == RHS->getLine() && + Scope == RHS->getRawScope() && BaseType == RHS->getRawBaseType() && SizeInBits == RHS->getSizeInBits() && AlignInBits == RHS->getAlignInBits() && OffsetInBits == RHS->getOffsetInBits() && Flags == RHS->getFlags() && - ExtraData == RHS->getExtraData(); + ExtraData == RHS->getRawExtraData(); } unsigned getHashValue() const { return hash_combine(Tag, Name, File, Line, Scope, BaseType, SizeInBits, @@ -397,26 +397,26 @@ template <> struct MDNodeKeyImpl<MDCompositeType> { RuntimeLang(RuntimeLang), VTableHolder(VTableHolder), TemplateParams(TemplateParams), Identifier(Identifier) {} MDNodeKeyImpl(const MDCompositeType *N) - : Tag(N->getTag()), Name(N->getName()), File(N->getFile()), - Line(N->getLine()), Scope(N->getScope()), BaseType(N->getBaseType()), - SizeInBits(N->getSizeInBits()), AlignInBits(N->getAlignInBits()), - OffsetInBits(N->getOffsetInBits()), Flags(N->getFlags()), - Elements(N->getElements()), RuntimeLang(N->getRuntimeLang()), - VTableHolder(N->getVTableHolder()), - TemplateParams(N->getTemplateParams()), Identifier(N->getIdentifier()) { - } + : Tag(N->getTag()), Name(N->getName()), File(N->getRawFile()), + Line(N->getLine()), Scope(N->getRawScope()), + BaseType(N->getRawBaseType()), SizeInBits(N->getSizeInBits()), + AlignInBits(N->getAlignInBits()), OffsetInBits(N->getOffsetInBits()), + Flags(N->getFlags()), Elements(N->getRawElements()), + RuntimeLang(N->getRuntimeLang()), VTableHolder(N->getRawVTableHolder()), + TemplateParams(N->getRawTemplateParams()), + Identifier(N->getIdentifier()) {} bool isKeyOf(const MDCompositeType *RHS) const { return Tag == RHS->getTag() && Name == RHS->getName() && - File == RHS->getFile() && Line == RHS->getLine() && - Scope == RHS->getScope() && BaseType == RHS->getBaseType() && + File == RHS->getRawFile() && Line == RHS->getLine() && + Scope == RHS->getRawScope() && BaseType == RHS->getRawBaseType() && SizeInBits == RHS->getSizeInBits() && AlignInBits == RHS->getAlignInBits() && OffsetInBits == RHS->getOffsetInBits() && Flags == RHS->getFlags() && - Elements == RHS->getElements() && + Elements == RHS->getRawElements() && RuntimeLang == RHS->getRuntimeLang() && - VTableHolder == RHS->getVTableHolder() && - TemplateParams == RHS->getTemplateParams() && + VTableHolder == RHS->getRawVTableHolder() && + TemplateParams == RHS->getRawTemplateParams() && Identifier == RHS->getIdentifier(); } unsigned getHashValue() const { @@ -433,10 +433,10 @@ template <> struct MDNodeKeyImpl<MDSubroutineType> { MDNodeKeyImpl(int64_t Flags, Metadata *TypeArray) : Flags(Flags), TypeArray(TypeArray) {} MDNodeKeyImpl(const MDSubroutineType *N) - : Flags(N->getFlags()), TypeArray(N->getTypeArray()) {} + : Flags(N->getFlags()), TypeArray(N->getRawTypeArray()) {} bool isKeyOf(const MDSubroutineType *RHS) const { - return Flags == RHS->getFlags() && TypeArray == RHS->getTypeArray(); + return Flags == RHS->getFlags() && TypeArray == RHS->getRawTypeArray(); } unsigned getHashValue() const { return hash_combine(Flags, TypeArray); } }; @@ -484,27 +484,28 @@ template <> struct MDNodeKeyImpl<MDCompileUnit> { Subprograms(Subprograms), GlobalVariables(GlobalVariables), ImportedEntities(ImportedEntities) {} MDNodeKeyImpl(const MDCompileUnit *N) - : SourceLanguage(N->getSourceLanguage()), File(N->getFile()), + : SourceLanguage(N->getSourceLanguage()), File(N->getRawFile()), Producer(N->getProducer()), IsOptimized(N->isOptimized()), Flags(N->getFlags()), RuntimeVersion(N->getRuntimeVersion()), SplitDebugFilename(N->getSplitDebugFilename()), - EmissionKind(N->getEmissionKind()), EnumTypes(N->getEnumTypes()), - RetainedTypes(N->getRetainedTypes()), Subprograms(N->getSubprograms()), - GlobalVariables(N->getGlobalVariables()), - ImportedEntities(N->getImportedEntities()) {} + EmissionKind(N->getEmissionKind()), EnumTypes(N->getRawEnumTypes()), + RetainedTypes(N->getRawRetainedTypes()), + Subprograms(N->getRawSubprograms()), + GlobalVariables(N->getRawGlobalVariables()), + ImportedEntities(N->getRawImportedEntities()) {} bool isKeyOf(const MDCompileUnit *RHS) const { return SourceLanguage == RHS->getSourceLanguage() && - File == RHS->getFile() && Producer == RHS->getProducer() && + File == RHS->getRawFile() && Producer == RHS->getProducer() && IsOptimized == RHS->isOptimized() && Flags == RHS->getFlags() && RuntimeVersion == RHS->getRuntimeVersion() && SplitDebugFilename == RHS->getSplitDebugFilename() && EmissionKind == RHS->getEmissionKind() && - EnumTypes == RHS->getEnumTypes() && - RetainedTypes == RHS->getRetainedTypes() && - Subprograms == RHS->getSubprograms() && - GlobalVariables == RHS->getGlobalVariables() && - ImportedEntities == RHS->getImportedEntities(); + EnumTypes == RHS->getRawEnumTypes() && + RetainedTypes == RHS->getRawRetainedTypes() && + Subprograms == RHS->getRawSubprograms() && + GlobalVariables == RHS->getRawGlobalVariables() && + ImportedEntities == RHS->getRawImportedEntities(); } unsigned getHashValue() const { return hash_combine(SourceLanguage, File, Producer, IsOptimized, Flags, diff --git a/llvm/lib/IR/Verifier.cpp b/llvm/lib/IR/Verifier.cpp index 22b6bf80807..b2a86e27b75 100644 --- a/llvm/lib/IR/Verifier.cpp +++ b/llvm/lib/IR/Verifier.cpp @@ -298,6 +298,8 @@ private: #define HANDLE_SPECIALIZED_MDNODE_LEAF(CLASS) void visit##CLASS(const CLASS &N); #include "llvm/IR/Metadata.def" + void visitMDScope(const MDScope &N); + void visitMDDerivedTypeBase(const MDDerivedTypeBase &N); void visitMDVariable(const MDVariable &N); // InstVisitor overrides... @@ -668,6 +670,15 @@ static bool isTypeRef(const Metadata *MD) { return isa<MDType>(MD); } +/// \brief Check if a value can be a ScopeRef. +static bool isScopeRef(const Metadata *MD) { + if (!MD) + return true; + if (auto *S = dyn_cast<MDString>(MD)) + return !S->getString().empty(); + return isa<MDScope>(MD); +} + void Verifier::visitMDLocation(const MDLocation &N) { Assert(N.getRawScope() && isa<MDLocalScope>(N.getRawScope()), "location requires a valid scope", &N, N.getRawScope()); @@ -679,8 +690,14 @@ void Verifier::visitGenericDebugNode(const GenericDebugNode &N) { Assert(N.getTag(), "invalid tag", &N); } +void Verifier::visitMDScope(const MDScope &N) { + if (auto *F = N.getRawFile()) + Assert(isa<MDFile>(F), "invalid file", &N, F); +} + void Verifier::visitMDSubrange(const MDSubrange &N) { Assert(N.getTag() == dwarf::DW_TAG_subrange_type, "invalid tag", &N); + Assert(N.getCount() >= -1, "invalid subrange count", &N); } void Verifier::visitMDEnumerator(const MDEnumerator &N) { @@ -693,7 +710,18 @@ void Verifier::visitMDBasicType(const MDBasicType &N) { "invalid tag", &N); } +void Verifier::visitMDDerivedTypeBase(const MDDerivedTypeBase &N) { + // Common scope checks. + visitMDScope(N); + + Assert(isScopeRef(N.getScope()), "invalid scope", &N, N.getScope()); + Assert(isTypeRef(N.getBaseType()), "invalid base type", &N, N.getBaseType()); +} + void Verifier::visitMDDerivedType(const MDDerivedType &N) { + // Common derived type checks. + visitMDDerivedTypeBase(N); + Assert(N.getTag() == dwarf::DW_TAG_typedef || N.getTag() == dwarf::DW_TAG_pointer_type || N.getTag() == dwarf::DW_TAG_ptr_to_member_type || @@ -709,6 +737,9 @@ void Verifier::visitMDDerivedType(const MDDerivedType &N) { } void Verifier::visitMDCompositeType(const MDCompositeType &N) { + // Common derived type checks. + visitMDDerivedTypeBase(N); + Assert(N.getTag() == dwarf::DW_TAG_array_type || N.getTag() == dwarf::DW_TAG_structure_type || N.getTag() == dwarf::DW_TAG_union_type || @@ -716,10 +747,24 @@ void Verifier::visitMDCompositeType(const MDCompositeType &N) { N.getTag() == dwarf::DW_TAG_subroutine_type || N.getTag() == dwarf::DW_TAG_class_type, "invalid tag", &N); + + Assert(!N.getRawElements() || isa<MDTuple>(N.getRawElements()), + "invalid composite elements", &N, N.getRawElements()); + Assert(isTypeRef(N.getRawVTableHolder()), "invalid vtable holder", &N, + N.getRawVTableHolder()); + Assert(!N.getRawElements() || isa<MDTuple>(N.getRawElements()), + "invalid composite elements", &N, N.getRawElements()); } void Verifier::visitMDSubroutineType(const MDSubroutineType &N) { Assert(N.getTag() == dwarf::DW_TAG_subroutine_type, "invalid tag", &N); + Assert(N.getRawElements() && isa<MDTuple>(N.getRawElements()), + "invalid composite elements", &N, N.getRawElements()); + + for (Metadata *Ty : N.getTypeArray()->operands()) { + Assert(isTypeRef(Ty), "invalid subroutine type ref", &N, N.getTypeArray(), + Ty); + } } void Verifier::visitMDFile(const MDFile &N) { @@ -728,6 +773,41 @@ void Verifier::visitMDFile(const MDFile &N) { void Verifier::visitMDCompileUnit(const MDCompileUnit &N) { Assert(N.getTag() == dwarf::DW_TAG_compile_unit, "invalid tag", &N); + + if (auto *Array = N.getRawEnumTypes()) { + Assert(isa<MDTuple>(Array), "invalid enum list", &N, Array); + for (Metadata *Op : N.getEnumTypes()->operands()) { + auto *Enum = dyn_cast_or_null<MDCompositeType>(Op); + Assert(Enum && Enum->getTag() == dwarf::DW_TAG_enumeration_type, + "invalid enum type", &N, N.getEnumTypes(), Op); + } + } + if (auto *Array = N.getRawRetainedTypes()) { + Assert(isa<MDTuple>(Array), "invalid retained type list", &N, Array); + for (Metadata *Op : N.getRetainedTypes()->operands()) { + Assert(Op && isa<MDType>(Op), "invalid retained type", &N, Op); + } + } + if (auto *Array = N.getRawSubprograms()) { + Assert(isa<MDTuple>(Array), "invalid subprogram list", &N, Array); + for (Metadata *Op : N.getSubprograms()->operands()) { + Assert(Op && isa<MDSubprogram>(Op), "invalid subprogram ref", &N, Op); + } + } + if (auto *Array = N.getRawGlobalVariables()) { + Assert(isa<MDTuple>(Array), "invalid global variable list", &N, Array); + for (Metadata *Op : N.getGlobalVariables()->operands()) { + Assert(Op && isa<MDGlobalVariable>(Op), "invalid global variable ref", &N, + Op); + } + } + if (auto *Array = N.getRawImportedEntities()) { + Assert(isa<MDTuple>(Array), "invalid imported entity list", &N, Array); + for (Metadata *Op : N.getImportedEntities()->operands()) { + Assert(Op && isa<MDImportedEntity>(Op), "invalid imported entity ref", &N, + Op); + } + } } void Verifier::visitMDSubprogram(const MDSubprogram &N) { |