From e5d844b5874488599dc79e788a2dd6efa02940fb Mon Sep 17 00:00:00 2001 From: Stephen Tozer Date: Wed, 21 Apr 2021 16:56:38 +0100 Subject: [Bitcode] Ensure DIArgList in bitcode has no null or forward metadata refs This patch fixes an issue in which ConstantAsMetadata arguments to a DIArglist, as well as the Constant values referenced by that metadata, would not be always be emitted correctly into bitcode. This patch fixes this issue firstly by searching for ConstantAsMetadata in DIArgLists (previously we would only search for them when directly wrapped in MetadataAsValue), and secondly by enumerating all of a DIArgList's arguments directly prior to enumerating the DIArgList itself. This patch also adds a number of asserts, and no longer treats the arguments to a DIArgList as optional fields when reading/writing to bitcode. Differential Revision: https://reviews.llvm.org/D100572 --- llvm/lib/Bitcode/Reader/MetadataLoader.cpp | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) (limited to 'llvm/lib/Bitcode/Reader/MetadataLoader.cpp') diff --git a/llvm/lib/Bitcode/Reader/MetadataLoader.cpp b/llvm/lib/Bitcode/Reader/MetadataLoader.cpp index 0cf547c57d9d..8493eb7a28b2 100644 --- a/llvm/lib/Bitcode/Reader/MetadataLoader.cpp +++ b/llvm/lib/Bitcode/Reader/MetadataLoader.cpp @@ -2078,8 +2078,15 @@ Error MetadataLoader::MetadataLoaderImpl::parseOneMetadata( case bitc::METADATA_ARG_LIST: { SmallVector Elts; Elts.reserve(Record.size()); - for (uint64_t Elt : Record) - Elts.push_back(dyn_cast_or_null(getMDOrNull(Elt))); + for (uint64_t Elt : Record) { + Metadata *MD = getMD(Elt); + if (isa(MD) && cast(MD)->isTemporary()) + return error( + "Invalid record: DIArgList should not contain forward refs"); + if (!isa(MD)) + return error("Invalid record"); + Elts.push_back(cast(MD)); + } MetadataList.assignValue(DIArgList::get(Context, Elts), NextMetadataNo); NextMetadataNo++; -- cgit v1.2.3