From 1811f4cb6d2fe6cd3b1c42b152b2d3d4e4213fea Mon Sep 17 00:00:00 2001 From: Ilya Biryukov Date: Wed, 22 May 2019 12:50:01 +0000 Subject: [PATCH] [Frontend] Return an error on bad inputs to PrecompiledPreabmle Summary: Instead of failing with assertions. Fixes a crash found by oss-fuzz: https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=12865 Reviewers: sammccall Reviewed By: sammccall Subscribers: cfe-commits Tags: #clang Differential Revision: https://reviews.llvm.org/D62137 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@361376 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/clang/Frontend/PrecompiledPreamble.h | 3 ++- lib/Frontend/ASTUnit.cpp | 1 + lib/Frontend/PrecompiledPreamble.cpp | 17 +++++++++-------- 3 files changed, 12 insertions(+), 9 deletions(-) diff --git a/include/clang/Frontend/PrecompiledPreamble.h b/include/clang/Frontend/PrecompiledPreamble.h index b1d55d8063..1a8a64951e 100644 --- a/include/clang/Frontend/PrecompiledPreamble.h +++ b/include/clang/Frontend/PrecompiledPreamble.h @@ -291,7 +291,8 @@ enum class BuildPreambleError { CouldntCreateTempFile = 1, CouldntCreateTargetInfo, BeginSourceFileFailed, - CouldntEmitPCH + CouldntEmitPCH, + BadInputs }; class BuildPreambleErrorCategory final : public std::error_category { diff --git a/lib/Frontend/ASTUnit.cpp b/lib/Frontend/ASTUnit.cpp index 95d1bf27d7..c4085b699e 100644 --- a/lib/Frontend/ASTUnit.cpp +++ b/lib/Frontend/ASTUnit.cpp @@ -1368,6 +1368,7 @@ ASTUnit::getMainBufferWithPrecompiledPreamble( case BuildPreambleError::CouldntCreateTargetInfo: case BuildPreambleError::BeginSourceFileFailed: case BuildPreambleError::CouldntEmitPCH: + case BuildPreambleError::BadInputs: // These erros are more likely to repeat, retry after some period. PreambleRebuildCountdown = DefaultPreambleRebuildInterval; return nullptr; diff --git a/lib/Frontend/PrecompiledPreamble.cpp b/lib/Frontend/PrecompiledPreamble.cpp index 25f128fe11..1fe8bfcb74 100644 --- a/lib/Frontend/PrecompiledPreamble.cpp +++ b/lib/Frontend/PrecompiledPreamble.cpp @@ -299,14 +299,13 @@ llvm::ErrorOr PrecompiledPreamble::Build( // created. This complexity should be lifted elsewhere. Clang->getTarget().adjust(Clang->getLangOpts()); - assert(Clang->getFrontendOpts().Inputs.size() == 1 && - "Invocation must have exactly one source file!"); - assert(Clang->getFrontendOpts().Inputs[0].getKind().getFormat() == - InputKind::Source && - "FIXME: AST inputs not yet supported here!"); - assert(Clang->getFrontendOpts().Inputs[0].getKind().getLanguage() != - InputKind::LLVM_IR && - "IR inputs not support here!"); + if (Clang->getFrontendOpts().Inputs.size() != 1 || + Clang->getFrontendOpts().Inputs[0].getKind().getFormat() != + InputKind::Source || + Clang->getFrontendOpts().Inputs[0].getKind().getLanguage() == + InputKind::LLVM_IR) { + return BuildPreambleError::BadInputs; + } // Clear out old caches and data. Diagnostics.Reset(); @@ -784,6 +783,8 @@ std::string BuildPreambleErrorCategory::message(int condition) const { return "BeginSourceFile() return an error"; case BuildPreambleError::CouldntEmitPCH: return "Could not emit PCH"; + case BuildPreambleError::BadInputs: + return "Command line arguments must contain exactly one source file"; } llvm_unreachable("unexpected BuildPreambleError"); } -- 2.40.0