diff options
author | Argyrios Kyrtzidis <akyrtzi@gmail.com> | 2014-02-27 04:11:59 +0000 |
---|---|---|
committer | Argyrios Kyrtzidis <akyrtzi@gmail.com> | 2014-02-27 04:11:59 +0000 |
commit | 1b7ed91e44a8fdcb3217077204d60e7fc8d4561f (patch) | |
tree | 22268eb650ebd65e801631b863d68ea4a3d6bd74 /clang/lib/Frontend/ASTUnit.cpp | |
parent | 08301dee468848e405d2a9c2ca08ad8af042be52 (diff) | |
download | llvm-1b7ed91e44a8fdcb3217077204d60e7fc8d4561f.zip llvm-1b7ed91e44a8fdcb3217077204d60e7fc8d4561f.tar.gz llvm-1b7ed91e44a8fdcb3217077204d60e7fc8d4561f.tar.bz2 |
[ASTUnit] Fix use-after-free bug in ASTUnit::getMainBufferWithPrecompiledPreamble().
With r197755 we started reading the contents of buffer file entries, but the
buffers may point to ASTReader blobs that have been disposed.
Fix this by having the CompilerInstance object keep a reference to the ASTReader
as well as having the ASTContext keep reference to the ExternalASTSource.
This was very difficult to construct a test case for.
rdar://16149782
llvm-svn: 202346
Diffstat (limited to 'clang/lib/Frontend/ASTUnit.cpp')
-rw-r--r-- | clang/lib/Frontend/ASTUnit.cpp | 30 |
1 files changed, 8 insertions, 22 deletions
diff --git a/clang/lib/Frontend/ASTUnit.cpp b/clang/lib/Frontend/ASTUnit.cpp index 8d46a8f..93429cb 100644 --- a/clang/lib/Frontend/ASTUnit.cpp +++ b/clang/lib/Frontend/ASTUnit.cpp @@ -718,8 +718,6 @@ ASTUnit *ASTUnit::LoadFromASTFile(const std::string &Filename, HeaderSearch &HeaderInfo = *AST->HeaderInfo.get(); unsigned Counter; - OwningPtr<ASTReader> Reader; - AST->PP = new Preprocessor(PPOpts, AST->getDiagnostics(), AST->ASTFileLangOpts, /*Target=*/0, AST->getSourceManager(), HeaderInfo, @@ -742,21 +740,17 @@ ASTUnit *ASTUnit::LoadFromASTFile(const std::string &Filename, bool disableValid = false; if (::getenv("LIBCLANG_DISABLE_PCH_VALIDATION")) disableValid = true; - Reader.reset(new ASTReader(PP, Context, + AST->Reader = new ASTReader(PP, Context, /*isysroot=*/"", /*DisableValidation=*/disableValid, - AllowPCHWithCompilerErrors)); - - // Recover resources if we crash before exiting this method. - llvm::CrashRecoveryContextCleanupRegistrar<ASTReader> - ReaderCleanup(Reader.get()); + AllowPCHWithCompilerErrors); - Reader->setListener(new ASTInfoCollector(*AST->PP, Context, + AST->Reader->setListener(new ASTInfoCollector(*AST->PP, Context, AST->ASTFileLangOpts, AST->TargetOpts, AST->Target, Counter)); - switch (Reader->ReadAST(Filename, serialization::MK_MainFile, + switch (AST->Reader->ReadAST(Filename, serialization::MK_MainFile, SourceLocation(), ASTReader::ARR_None)) { case ASTReader::Success: break; @@ -771,21 +765,14 @@ ASTUnit *ASTUnit::LoadFromASTFile(const std::string &Filename, return NULL; } - AST->OriginalSourceFile = Reader->getOriginalSourceFile(); + AST->OriginalSourceFile = AST->Reader->getOriginalSourceFile(); PP.setCounterValue(Counter); // Attach the AST reader to the AST context as an external AST // source, so that declarations will be deserialized from the // AST file as needed. - ASTReader *ReaderPtr = Reader.get(); - OwningPtr<ExternalASTSource> Source(Reader.take()); - - // Unregister the cleanup for ASTReader. It will get cleaned up - // by the ASTUnit cleanup. - ReaderCleanup.unregister(); - - Context.setExternalSource(Source); + Context.setExternalSource(AST->Reader); // Create an AST consumer, even though it isn't used. AST->Consumer.reset(new ASTConsumer); @@ -793,8 +780,7 @@ ASTUnit *ASTUnit::LoadFromASTFile(const std::string &Filename, // Create a semantic analysis object and tell the AST reader about it. AST->TheSema.reset(new Sema(PP, Context, *AST->Consumer)); AST->TheSema->Initialize(); - ReaderPtr->InitializeSema(*AST->TheSema); - AST->Reader = ReaderPtr; + AST->Reader->InitializeSema(*AST->TheSema); // Tell the diagnostic client that we have started a source file. AST->getDiagnostics().getClient()->BeginSourceFile(Context.getLangOpts(),&PP); @@ -1173,7 +1159,7 @@ bool ASTUnit::Parse(llvm::MemoryBuffer *OverrideMainBuffer) { if (OverrideMainBuffer) { std::string ModName = getPreambleFile(this); - TranslateStoredDiagnostics(Clang->getModuleManager(), ModName, + TranslateStoredDiagnostics(Clang->getModuleManager().getPtr(), ModName, getSourceManager(), PreambleDiagnostics, StoredDiagnostics); } |