2011-09-09 19:02:57 +08:00
|
|
|
//===--- JumpDiagnostics.cpp - Protected scope jump analysis ------*- C++ -*-=//
|
2009-04-19 12:46:21 +08:00
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2009-04-19 12:46:21 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file implements the JumpScopeChecker class, which is used to diagnose
|
2011-09-09 19:02:57 +08:00
|
|
|
// jumps that enter a protected scope in an invalid way.
|
2009-04-19 12:46:21 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2010-08-25 15:42:41 +08:00
|
|
|
#include "clang/AST/DeclCXX.h"
|
2009-04-19 12:46:21 +08:00
|
|
|
#include "clang/AST/Expr.h"
|
2011-05-28 00:05:29 +08:00
|
|
|
#include "clang/AST/ExprCXX.h"
|
2009-04-28 04:27:31 +08:00
|
|
|
#include "clang/AST/StmtCXX.h"
|
2012-12-04 17:13:33 +08:00
|
|
|
#include "clang/AST/StmtObjC.h"
|
2020-07-07 01:32:11 +08:00
|
|
|
#include "clang/AST/StmtOpenMP.h"
|
2021-04-16 07:49:19 +08:00
|
|
|
#include "clang/Basic/SourceLocation.h"
|
|
|
|
#include "clang/Sema/SemaInternal.h"
|
2010-08-13 04:07:10 +08:00
|
|
|
#include "llvm/ADT/BitVector.h"
|
2009-04-19 12:46:21 +08:00
|
|
|
using namespace clang;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
/// JumpScopeChecker - This object is used by Sema to diagnose invalid jumps
|
|
|
|
/// into VLA and other protected scopes. For example, this rejects:
|
|
|
|
/// goto L;
|
|
|
|
/// int a[n];
|
|
|
|
/// L:
|
|
|
|
///
|
2021-04-16 07:49:19 +08:00
|
|
|
/// We also detect jumps out of protected scopes when it's not possible to do
|
|
|
|
/// cleanups properly. Indirect jumps and ASM jumps can't do cleanups because
|
|
|
|
/// the target is unknown. Return statements with \c [[clang::musttail]] cannot
|
|
|
|
/// handle any cleanups due to the nature of a tail call.
|
2009-04-19 12:46:21 +08:00
|
|
|
class JumpScopeChecker {
|
|
|
|
Sema &S;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2014-05-09 16:40:10 +08:00
|
|
|
/// Permissive - True when recovering from errors, in which case precautions
|
|
|
|
/// are taken to handle incomplete scope information.
|
|
|
|
const bool Permissive;
|
|
|
|
|
2009-04-19 12:46:21 +08:00
|
|
|
/// GotoScope - This is a record that we use to keep track of all of the
|
|
|
|
/// scopes that are introduced by VLAs and other things that scope jumps like
|
|
|
|
/// gotos. This scope tree has nothing to do with the source scope tree,
|
|
|
|
/// because you can have multiple VLA scopes per compound statement, and most
|
|
|
|
/// compound statements don't introduce any scopes.
|
|
|
|
struct GotoScope {
|
|
|
|
/// ParentScope - The index in ScopeMap of the parent scope. This is 0 for
|
|
|
|
/// the parent scope is the function body.
|
|
|
|
unsigned ParentScope;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2011-10-21 05:42:12 +08:00
|
|
|
/// InDiag - The note to emit if there is a jump into this scope.
|
2010-05-12 08:58:13 +08:00
|
|
|
unsigned InDiag;
|
|
|
|
|
2011-10-21 05:42:12 +08:00
|
|
|
/// OutDiag - The note to emit if there is an indirect jump out
|
2010-05-12 08:58:13 +08:00
|
|
|
/// of this scope. Direct jumps always clean up their current scope
|
|
|
|
/// in an orderly way.
|
|
|
|
unsigned OutDiag;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-04-19 12:46:21 +08:00
|
|
|
/// Loc - Location to emit the diagnostic.
|
|
|
|
SourceLocation Loc;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2010-05-12 08:58:13 +08:00
|
|
|
GotoScope(unsigned parentScope, unsigned InDiag, unsigned OutDiag,
|
|
|
|
SourceLocation L)
|
|
|
|
: ParentScope(parentScope), InDiag(InDiag), OutDiag(OutDiag), Loc(L) {}
|
2009-04-19 12:46:21 +08:00
|
|
|
};
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2011-07-23 18:55:15 +08:00
|
|
|
SmallVector<GotoScope, 48> Scopes;
|
2009-04-19 12:46:21 +08:00
|
|
|
llvm::DenseMap<Stmt*, unsigned> LabelAndGotoScopes;
|
2011-07-23 18:55:15 +08:00
|
|
|
SmallVector<Stmt*, 16> Jumps;
|
2010-05-12 08:58:13 +08:00
|
|
|
|
2019-06-03 23:57:25 +08:00
|
|
|
SmallVector<Stmt*, 4> IndirectJumps;
|
|
|
|
SmallVector<Stmt*, 4> AsmJumps;
|
2021-04-16 07:49:19 +08:00
|
|
|
SmallVector<AttributedStmt *, 4> MustTailStmts;
|
2011-07-23 18:55:15 +08:00
|
|
|
SmallVector<LabelDecl*, 4> IndirectJumpTargets;
|
2019-06-03 23:57:25 +08:00
|
|
|
SmallVector<LabelDecl*, 4> AsmJumpTargets;
|
2009-04-19 12:46:21 +08:00
|
|
|
public:
|
|
|
|
JumpScopeChecker(Stmt *Body, Sema &S);
|
|
|
|
private:
|
2010-06-22 07:44:13 +08:00
|
|
|
void BuildScopeInformation(Decl *D, unsigned &ParentScope);
|
2015-06-05 06:53:21 +08:00
|
|
|
void BuildScopeInformation(VarDecl *D, const BlockDecl *BDecl,
|
2011-07-12 02:04:54 +08:00
|
|
|
unsigned &ParentScope);
|
2020-03-11 05:06:25 +08:00
|
|
|
void BuildScopeInformation(CompoundLiteralExpr *CLE, unsigned &ParentScope);
|
2011-07-12 02:04:54 +08:00
|
|
|
void BuildScopeInformation(Stmt *S, unsigned &origParentScope);
|
2015-06-05 06:53:21 +08:00
|
|
|
|
2009-04-19 12:46:21 +08:00
|
|
|
void VerifyJumps();
|
2019-06-03 23:57:25 +08:00
|
|
|
void VerifyIndirectOrAsmJumps(bool IsAsmGoto);
|
2021-04-16 07:49:19 +08:00
|
|
|
void VerifyMustTailStmts();
|
2012-02-22 17:38:11 +08:00
|
|
|
void NoteJumpIntoScopes(ArrayRef<unsigned> ToScopes);
|
2019-06-03 23:57:25 +08:00
|
|
|
void DiagnoseIndirectOrAsmJump(Stmt *IG, unsigned IGScope, LabelDecl *Target,
|
|
|
|
unsigned TargetScope);
|
2011-09-13 18:26:51 +08:00
|
|
|
void CheckJump(Stmt *From, Stmt *To, SourceLocation DiagLoc,
|
2011-10-21 05:42:12 +08:00
|
|
|
unsigned JumpDiag, unsigned JumpDiagWarning,
|
|
|
|
unsigned JumpDiagCXX98Compat);
|
2014-09-22 10:21:54 +08:00
|
|
|
void CheckGotoStmt(GotoStmt *GS);
|
2021-04-16 07:49:19 +08:00
|
|
|
const Attr *GetMustTailAttr(AttributedStmt *AS);
|
2010-05-12 10:37:54 +08:00
|
|
|
|
|
|
|
unsigned GetDeepestCommonScope(unsigned A, unsigned B);
|
2009-04-19 12:46:21 +08:00
|
|
|
};
|
|
|
|
} // end anonymous namespace
|
|
|
|
|
2014-05-09 16:40:10 +08:00
|
|
|
#define CHECK_PERMISSIVE(x) (assert(Permissive || !(x)), (Permissive && (x)))
|
2009-04-19 12:46:21 +08:00
|
|
|
|
2014-05-09 16:40:10 +08:00
|
|
|
JumpScopeChecker::JumpScopeChecker(Stmt *Body, Sema &s)
|
|
|
|
: S(s), Permissive(s.hasAnyUnrecoverableErrorsInThisFunction()) {
|
2009-04-19 12:46:21 +08:00
|
|
|
// Add a scope entry for function scope.
|
2010-05-12 08:58:13 +08:00
|
|
|
Scopes.push_back(GotoScope(~0U, ~0U, ~0U, SourceLocation()));
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-04-19 12:46:21 +08:00
|
|
|
// Build information for the top level compound statement, so that we have a
|
|
|
|
// defined scope record for every "goto" and label.
|
2011-07-12 02:04:54 +08:00
|
|
|
unsigned BodyParentScope = 0;
|
|
|
|
BuildScopeInformation(Body, BodyParentScope);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-04-19 12:46:21 +08:00
|
|
|
// Check that all jumps we saw are kosher.
|
|
|
|
VerifyJumps();
|
2019-06-03 23:57:25 +08:00
|
|
|
VerifyIndirectOrAsmJumps(false);
|
|
|
|
VerifyIndirectOrAsmJumps(true);
|
2021-04-16 07:49:19 +08:00
|
|
|
VerifyMustTailStmts();
|
2009-04-19 12:46:21 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2010-05-12 10:37:54 +08:00
|
|
|
/// GetDeepestCommonScope - Finds the innermost scope enclosing the
|
|
|
|
/// two scopes.
|
|
|
|
unsigned JumpScopeChecker::GetDeepestCommonScope(unsigned A, unsigned B) {
|
|
|
|
while (A != B) {
|
|
|
|
// Inner scopes are created after outer scopes and therefore have
|
|
|
|
// higher indices.
|
|
|
|
if (A < B) {
|
|
|
|
assert(Scopes[B].ParentScope < B);
|
|
|
|
B = Scopes[B].ParentScope;
|
|
|
|
} else {
|
|
|
|
assert(Scopes[A].ParentScope < A);
|
|
|
|
A = Scopes[A].ParentScope;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return A;
|
|
|
|
}
|
|
|
|
|
2011-06-16 07:02:42 +08:00
|
|
|
typedef std::pair<unsigned,unsigned> ScopePair;
|
|
|
|
|
2009-04-19 12:46:21 +08:00
|
|
|
/// GetDiagForGotoScopeDecl - If this decl induces a new goto scope, return a
|
|
|
|
/// diagnostic that should be emitted if control goes over it. If not, return 0.
|
2013-12-12 09:27:02 +08:00
|
|
|
static ScopePair GetDiagForGotoScopeDecl(Sema &S, const Decl *D) {
|
2009-04-19 12:46:21 +08:00
|
|
|
if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
|
2012-10-28 10:44:03 +08:00
|
|
|
unsigned InDiag = 0;
|
2013-12-12 09:27:02 +08:00
|
|
|
unsigned OutDiag = 0;
|
|
|
|
|
2009-04-19 12:46:21 +08:00
|
|
|
if (VD->getType()->isVariablyModifiedType())
|
2010-05-12 08:58:13 +08:00
|
|
|
InDiag = diag::note_protected_by_vla;
|
|
|
|
|
2011-06-16 07:02:42 +08:00
|
|
|
if (VD->hasAttr<BlocksAttr>())
|
|
|
|
return ScopePair(diag::note_protected_by___block,
|
|
|
|
diag::note_exits___block);
|
|
|
|
|
|
|
|
if (VD->hasAttr<CleanupAttr>())
|
|
|
|
return ScopePair(diag::note_protected_by_cleanup,
|
|
|
|
diag::note_exits_cleanup);
|
|
|
|
|
2013-12-12 09:27:02 +08:00
|
|
|
if (VD->hasLocalStorage()) {
|
|
|
|
switch (VD->getType().isDestructedType()) {
|
|
|
|
case QualType::DK_objc_strong_lifetime:
|
2015-10-22 02:06:38 +08:00
|
|
|
return ScopePair(diag::note_protected_by_objc_strong_init,
|
|
|
|
diag::note_exits_objc_strong);
|
|
|
|
|
2013-12-12 09:27:02 +08:00
|
|
|
case QualType::DK_objc_weak_lifetime:
|
2015-10-22 02:06:38 +08:00
|
|
|
return ScopePair(diag::note_protected_by_objc_weak_init,
|
|
|
|
diag::note_exits_objc_weak);
|
2013-12-12 09:27:02 +08:00
|
|
|
|
2018-02-28 15:15:55 +08:00
|
|
|
case QualType::DK_nontrivial_c_struct:
|
|
|
|
return ScopePair(diag::note_protected_by_non_trivial_c_struct_init,
|
|
|
|
diag::note_exits_dtor);
|
|
|
|
|
2013-12-12 09:27:02 +08:00
|
|
|
case QualType::DK_cxx_destructor:
|
|
|
|
OutDiag = diag::note_exits_dtor;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case QualType::DK_none:
|
|
|
|
break;
|
2011-06-16 07:02:42 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-12 09:27:02 +08:00
|
|
|
const Expr *Init = VD->getInit();
|
|
|
|
if (S.Context.getLangOpts().CPlusPlus && VD->hasLocalStorage() && Init) {
|
2011-10-21 05:42:12 +08:00
|
|
|
// C++11 [stmt.dcl]p3:
|
2011-06-16 07:02:42 +08:00
|
|
|
// A program that jumps from a point where a variable with automatic
|
|
|
|
// storage duration is not in scope to a point where it is in scope
|
|
|
|
// is ill-formed unless the variable has scalar type, class type with
|
2015-06-05 06:53:21 +08:00
|
|
|
// a trivial default constructor and a trivial destructor, a
|
2011-06-16 07:02:42 +08:00
|
|
|
// cv-qualified version of one of these types, or an array of one of
|
|
|
|
// the preceding types and is declared without an initializer.
|
|
|
|
|
|
|
|
// C++03 [stmt.dcl.p3:
|
|
|
|
// A program that jumps from a point where a local variable
|
|
|
|
// with automatic storage duration is not in scope to a point
|
|
|
|
// where it is in scope is ill-formed unless the variable has
|
|
|
|
// POD type and is declared without an initializer.
|
|
|
|
|
2013-12-12 09:27:02 +08:00
|
|
|
InDiag = diag::note_protected_by_variable_init;
|
2012-10-28 10:44:03 +08:00
|
|
|
|
2013-12-12 09:27:02 +08:00
|
|
|
// For a variable of (array of) class type declared without an
|
|
|
|
// initializer, we will have call-style initialization and the initializer
|
|
|
|
// will be the CXXConstructExpr with no intervening nodes.
|
|
|
|
if (const CXXConstructExpr *CCE = dyn_cast<CXXConstructExpr>(Init)) {
|
|
|
|
const CXXConstructorDecl *Ctor = CCE->getConstructor();
|
|
|
|
if (Ctor->isTrivial() && Ctor->isDefaultConstructor() &&
|
|
|
|
VD->getInitStyle() == VarDecl::CallInit) {
|
2012-10-28 10:44:03 +08:00
|
|
|
if (OutDiag)
|
|
|
|
InDiag = diag::note_protected_by_variable_nontriv_destructor;
|
2013-12-12 09:27:02 +08:00
|
|
|
else if (!Ctor->getParent()->isPOD())
|
2012-10-28 10:44:03 +08:00
|
|
|
InDiag = diag::note_protected_by_variable_non_pod;
|
2013-12-12 09:27:02 +08:00
|
|
|
else
|
|
|
|
InDiag = 0;
|
2011-05-28 00:05:29 +08:00
|
|
|
}
|
2012-10-28 00:49:47 +08:00
|
|
|
}
|
|
|
|
}
|
2012-10-28 10:44:03 +08:00
|
|
|
|
2013-12-12 09:27:02 +08:00
|
|
|
return ScopePair(InDiag, OutDiag);
|
2010-05-12 08:58:13 +08:00
|
|
|
}
|
|
|
|
|
2013-12-12 09:27:02 +08:00
|
|
|
if (const TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) {
|
2009-04-19 12:46:21 +08:00
|
|
|
if (TD->getUnderlyingType()->isVariablyModifiedType())
|
2013-12-12 09:27:02 +08:00
|
|
|
return ScopePair(isa<TypedefDecl>(TD)
|
|
|
|
? diag::note_protected_by_vla_typedef
|
|
|
|
: diag::note_protected_by_vla_type_alias,
|
|
|
|
0);
|
2011-04-15 22:24:37 +08:00
|
|
|
}
|
|
|
|
|
2011-06-16 07:02:42 +08:00
|
|
|
return ScopePair(0U, 0U);
|
2009-04-19 12:46:21 +08:00
|
|
|
}
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Build scope information for a declaration that is part of a DeclStmt.
|
2010-06-22 07:44:13 +08:00
|
|
|
void JumpScopeChecker::BuildScopeInformation(Decl *D, unsigned &ParentScope) {
|
|
|
|
// If this decl causes a new scope, push and switch to it.
|
2013-12-12 09:27:02 +08:00
|
|
|
std::pair<unsigned,unsigned> Diags = GetDiagForGotoScopeDecl(S, D);
|
2010-06-22 07:44:13 +08:00
|
|
|
if (Diags.first || Diags.second) {
|
|
|
|
Scopes.push_back(GotoScope(ParentScope, Diags.first, Diags.second,
|
|
|
|
D->getLocation()));
|
|
|
|
ParentScope = Scopes.size()-1;
|
|
|
|
}
|
2015-06-05 06:53:21 +08:00
|
|
|
|
2010-06-22 07:44:13 +08:00
|
|
|
// If the decl has an initializer, walk it with the potentially new
|
|
|
|
// scope we just installed.
|
|
|
|
if (VarDecl *VD = dyn_cast<VarDecl>(D))
|
|
|
|
if (Expr *Init = VD->getInit())
|
|
|
|
BuildScopeInformation(Init, ParentScope);
|
|
|
|
}
|
2009-04-19 12:46:21 +08:00
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Build scope information for a captured block literal variables.
|
2015-06-05 06:53:21 +08:00
|
|
|
void JumpScopeChecker::BuildScopeInformation(VarDecl *D,
|
|
|
|
const BlockDecl *BDecl,
|
2011-07-12 02:04:54 +08:00
|
|
|
unsigned &ParentScope) {
|
|
|
|
// exclude captured __block variables; there's no destructor
|
|
|
|
// associated with the block literal for them.
|
|
|
|
if (D->hasAttr<BlocksAttr>())
|
|
|
|
return;
|
|
|
|
QualType T = D->getType();
|
|
|
|
QualType::DestructionKind destructKind = T.isDestructedType();
|
|
|
|
if (destructKind != QualType::DK_none) {
|
|
|
|
std::pair<unsigned,unsigned> Diags;
|
|
|
|
switch (destructKind) {
|
|
|
|
case QualType::DK_cxx_destructor:
|
|
|
|
Diags = ScopePair(diag::note_enters_block_captures_cxx_obj,
|
|
|
|
diag::note_exits_block_captures_cxx_obj);
|
|
|
|
break;
|
|
|
|
case QualType::DK_objc_strong_lifetime:
|
|
|
|
Diags = ScopePair(diag::note_enters_block_captures_strong,
|
|
|
|
diag::note_exits_block_captures_strong);
|
|
|
|
break;
|
|
|
|
case QualType::DK_objc_weak_lifetime:
|
|
|
|
Diags = ScopePair(diag::note_enters_block_captures_weak,
|
|
|
|
diag::note_exits_block_captures_weak);
|
|
|
|
break;
|
2018-02-28 15:15:55 +08:00
|
|
|
case QualType::DK_nontrivial_c_struct:
|
|
|
|
Diags = ScopePair(diag::note_enters_block_captures_non_trivial_c_struct,
|
|
|
|
diag::note_exits_block_captures_non_trivial_c_struct);
|
|
|
|
break;
|
2011-07-12 02:04:54 +08:00
|
|
|
case QualType::DK_none:
|
2011-10-21 05:42:12 +08:00
|
|
|
llvm_unreachable("non-lifetime captured variable");
|
2011-07-12 02:04:54 +08:00
|
|
|
}
|
|
|
|
SourceLocation Loc = D->getLocation();
|
|
|
|
if (Loc.isInvalid())
|
|
|
|
Loc = BDecl->getLocation();
|
2015-06-05 06:53:21 +08:00
|
|
|
Scopes.push_back(GotoScope(ParentScope,
|
2011-07-12 02:04:54 +08:00
|
|
|
Diags.first, Diags.second, Loc));
|
|
|
|
ParentScope = Scopes.size()-1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-11 05:06:25 +08:00
|
|
|
/// Build scope information for compound literals of C struct types that are
|
|
|
|
/// non-trivial to destruct.
|
|
|
|
void JumpScopeChecker::BuildScopeInformation(CompoundLiteralExpr *CLE,
|
|
|
|
unsigned &ParentScope) {
|
|
|
|
unsigned InDiag = diag::note_enters_compound_literal_scope;
|
|
|
|
unsigned OutDiag = diag::note_exits_compound_literal_scope;
|
|
|
|
Scopes.push_back(GotoScope(ParentScope, InDiag, OutDiag, CLE->getExprLoc()));
|
|
|
|
ParentScope = Scopes.size() - 1;
|
|
|
|
}
|
|
|
|
|
2009-04-19 12:46:21 +08:00
|
|
|
/// BuildScopeInformation - The statements from CI to CE are known to form a
|
|
|
|
/// coherent VLA scope with a specified parent node. Walk through the
|
|
|
|
/// statements, adding any labels or gotos to LabelAndGotoScopes and recursively
|
|
|
|
/// walking the AST as needed.
|
2016-06-22 04:10:11 +08:00
|
|
|
void JumpScopeChecker::BuildScopeInformation(Stmt *S,
|
|
|
|
unsigned &origParentScope) {
|
2011-07-12 02:04:54 +08:00
|
|
|
// If this is a statement, rather than an expression, scopes within it don't
|
|
|
|
// propagate out into the enclosing scope. Otherwise we have to worry
|
|
|
|
// about block literals, which have the lifetime of their enclosing statement.
|
|
|
|
unsigned independentParentScope = origParentScope;
|
2015-06-05 06:53:21 +08:00
|
|
|
unsigned &ParentScope = ((isa<Expr>(S) && !isa<StmtExpr>(S))
|
2011-07-12 02:04:54 +08:00
|
|
|
? origParentScope : independentParentScope);
|
|
|
|
|
2016-07-14 08:11:03 +08:00
|
|
|
unsigned StmtsToSkip = 0u;
|
2015-06-05 06:53:21 +08:00
|
|
|
|
2009-04-19 12:46:21 +08:00
|
|
|
// If we found a label, remember that it is in ParentScope scope.
|
2010-05-12 08:58:13 +08:00
|
|
|
switch (S->getStmtClass()) {
|
|
|
|
case Stmt::AddrLabelExprClass:
|
|
|
|
IndirectJumpTargets.push_back(cast<AddrLabelExpr>(S)->getLabel());
|
|
|
|
break;
|
|
|
|
|
2017-04-20 01:54:08 +08:00
|
|
|
case Stmt::ObjCForCollectionStmtClass: {
|
|
|
|
auto *CS = cast<ObjCForCollectionStmt>(S);
|
|
|
|
unsigned Diag = diag::note_protected_by_objc_fast_enumeration;
|
|
|
|
unsigned NewParentScope = Scopes.size();
|
2018-08-10 05:08:08 +08:00
|
|
|
Scopes.push_back(GotoScope(ParentScope, Diag, 0, S->getBeginLoc()));
|
2017-04-20 01:54:08 +08:00
|
|
|
BuildScopeInformation(CS->getBody(), NewParentScope);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-05-12 08:58:13 +08:00
|
|
|
case Stmt::IndirectGotoStmtClass:
|
2010-10-28 16:53:48 +08:00
|
|
|
// "goto *&&lbl;" is a special case which we treat as equivalent
|
|
|
|
// to a normal goto. In addition, we don't calculate scope in the
|
|
|
|
// operand (to avoid recording the address-of-label use), which
|
|
|
|
// works only because of the restricted set of expressions which
|
|
|
|
// we detect as constant targets.
|
|
|
|
if (cast<IndirectGotoStmt>(S)->getConstantTarget()) {
|
|
|
|
LabelAndGotoScopes[S] = ParentScope;
|
|
|
|
Jumps.push_back(S);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-04-19 12:46:21 +08:00
|
|
|
LabelAndGotoScopes[S] = ParentScope;
|
2019-06-03 23:57:25 +08:00
|
|
|
IndirectJumps.push_back(S);
|
2010-05-12 08:58:13 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case Stmt::SwitchStmtClass:
|
2016-07-14 08:11:03 +08:00
|
|
|
// Evaluate the C++17 init stmt and condition variable
|
|
|
|
// before entering the scope of the switch statement.
|
|
|
|
if (Stmt *Init = cast<SwitchStmt>(S)->getInit()) {
|
|
|
|
BuildScopeInformation(Init, ParentScope);
|
|
|
|
++StmtsToSkip;
|
|
|
|
}
|
2010-06-22 07:44:13 +08:00
|
|
|
if (VarDecl *Var = cast<SwitchStmt>(S)->getConditionVariable()) {
|
|
|
|
BuildScopeInformation(Var, ParentScope);
|
2016-07-14 08:11:03 +08:00
|
|
|
++StmtsToSkip;
|
2010-06-22 07:44:13 +08:00
|
|
|
}
|
2017-12-20 06:06:11 +08:00
|
|
|
LLVM_FALLTHROUGH;
|
2015-06-05 06:53:21 +08:00
|
|
|
|
2010-06-22 07:44:13 +08:00
|
|
|
case Stmt::GotoStmtClass:
|
2009-04-19 12:46:21 +08:00
|
|
|
// Remember both what scope a goto is in as well as the fact that we have
|
|
|
|
// it. This makes the second scan not have to walk the AST again.
|
|
|
|
LabelAndGotoScopes[S] = ParentScope;
|
|
|
|
Jumps.push_back(S);
|
2010-05-12 08:58:13 +08:00
|
|
|
break;
|
|
|
|
|
2019-06-03 23:57:25 +08:00
|
|
|
case Stmt::GCCAsmStmtClass:
|
|
|
|
if (auto *GS = dyn_cast<GCCAsmStmt>(S))
|
|
|
|
if (GS->isAsmGoto()) {
|
|
|
|
// Remember both what scope a goto is in as well as the fact that we
|
|
|
|
// have it. This makes the second scan not have to walk the AST again.
|
|
|
|
LabelAndGotoScopes[S] = ParentScope;
|
|
|
|
AsmJumps.push_back(GS);
|
|
|
|
for (auto *E : GS->labels())
|
|
|
|
AsmJumpTargets.push_back(E->getLabel());
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2016-06-24 03:16:49 +08:00
|
|
|
case Stmt::IfStmtClass: {
|
|
|
|
IfStmt *IS = cast<IfStmt>(S);
|
2016-08-17 01:44:11 +08:00
|
|
|
if (!(IS->isConstexpr() || IS->isObjCAvailabilityCheck()))
|
2016-06-24 03:16:49 +08:00
|
|
|
break;
|
|
|
|
|
2016-08-17 01:44:11 +08:00
|
|
|
unsigned Diag = IS->isConstexpr() ? diag::note_protected_by_constexpr_if
|
|
|
|
: diag::note_protected_by_if_available;
|
|
|
|
|
2016-06-24 03:16:49 +08:00
|
|
|
if (VarDecl *Var = IS->getConditionVariable())
|
|
|
|
BuildScopeInformation(Var, ParentScope);
|
|
|
|
|
|
|
|
// Cannot jump into the middle of the condition.
|
|
|
|
unsigned NewParentScope = Scopes.size();
|
2018-08-10 05:08:08 +08:00
|
|
|
Scopes.push_back(GotoScope(ParentScope, Diag, 0, IS->getBeginLoc()));
|
2016-06-24 03:16:49 +08:00
|
|
|
BuildScopeInformation(IS->getCond(), NewParentScope);
|
|
|
|
|
|
|
|
// Jumps into either arm of an 'if constexpr' are not allowed.
|
|
|
|
NewParentScope = Scopes.size();
|
2018-08-10 05:08:08 +08:00
|
|
|
Scopes.push_back(GotoScope(ParentScope, Diag, 0, IS->getBeginLoc()));
|
2016-06-24 03:16:49 +08:00
|
|
|
BuildScopeInformation(IS->getThen(), NewParentScope);
|
|
|
|
if (Stmt *Else = IS->getElse()) {
|
|
|
|
NewParentScope = Scopes.size();
|
2018-08-10 05:08:08 +08:00
|
|
|
Scopes.push_back(GotoScope(ParentScope, Diag, 0, IS->getBeginLoc()));
|
2016-06-24 03:16:49 +08:00
|
|
|
BuildScopeInformation(Else, NewParentScope);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-11-01 07:55:28 +08:00
|
|
|
case Stmt::CXXTryStmtClass: {
|
|
|
|
CXXTryStmt *TS = cast<CXXTryStmt>(S);
|
2016-06-22 04:10:11 +08:00
|
|
|
{
|
|
|
|
unsigned NewParentScope = Scopes.size();
|
|
|
|
Scopes.push_back(GotoScope(ParentScope,
|
|
|
|
diag::note_protected_by_cxx_try,
|
|
|
|
diag::note_exits_cxx_try,
|
|
|
|
TS->getSourceRange().getBegin()));
|
|
|
|
if (Stmt *TryBlock = TS->getTryBlock())
|
|
|
|
BuildScopeInformation(TryBlock, NewParentScope);
|
|
|
|
}
|
2012-11-01 07:55:28 +08:00
|
|
|
|
|
|
|
// Jump from the catch into the try is not allowed either.
|
|
|
|
for (unsigned I = 0, E = TS->getNumHandlers(); I != E; ++I) {
|
|
|
|
CXXCatchStmt *CS = TS->getHandler(I);
|
2016-06-22 04:10:11 +08:00
|
|
|
unsigned NewParentScope = Scopes.size();
|
2012-11-01 07:55:28 +08:00
|
|
|
Scopes.push_back(GotoScope(ParentScope,
|
|
|
|
diag::note_protected_by_cxx_catch,
|
|
|
|
diag::note_exits_cxx_catch,
|
|
|
|
CS->getSourceRange().getBegin()));
|
2016-06-22 04:10:11 +08:00
|
|
|
BuildScopeInformation(CS->getHandlerBlock(), NewParentScope);
|
2012-11-01 07:55:28 +08:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-02-04 01:06:08 +08:00
|
|
|
case Stmt::SEHTryStmtClass: {
|
|
|
|
SEHTryStmt *TS = cast<SEHTryStmt>(S);
|
2016-06-22 04:10:11 +08:00
|
|
|
{
|
|
|
|
unsigned NewParentScope = Scopes.size();
|
|
|
|
Scopes.push_back(GotoScope(ParentScope,
|
|
|
|
diag::note_protected_by_seh_try,
|
|
|
|
diag::note_exits_seh_try,
|
|
|
|
TS->getSourceRange().getBegin()));
|
|
|
|
if (Stmt *TryBlock = TS->getTryBlock())
|
|
|
|
BuildScopeInformation(TryBlock, NewParentScope);
|
|
|
|
}
|
2015-02-04 01:06:08 +08:00
|
|
|
|
|
|
|
// Jump from __except or __finally into the __try are not allowed either.
|
|
|
|
if (SEHExceptStmt *Except = TS->getExceptHandler()) {
|
2016-06-22 04:10:11 +08:00
|
|
|
unsigned NewParentScope = Scopes.size();
|
2015-02-04 01:06:08 +08:00
|
|
|
Scopes.push_back(GotoScope(ParentScope,
|
|
|
|
diag::note_protected_by_seh_except,
|
|
|
|
diag::note_exits_seh_except,
|
|
|
|
Except->getSourceRange().getBegin()));
|
2016-06-22 04:10:11 +08:00
|
|
|
BuildScopeInformation(Except->getBlock(), NewParentScope);
|
2015-02-04 01:06:08 +08:00
|
|
|
} else if (SEHFinallyStmt *Finally = TS->getFinallyHandler()) {
|
2016-06-22 04:10:11 +08:00
|
|
|
unsigned NewParentScope = Scopes.size();
|
2015-02-04 01:06:08 +08:00
|
|
|
Scopes.push_back(GotoScope(ParentScope,
|
|
|
|
diag::note_protected_by_seh_finally,
|
|
|
|
diag::note_exits_seh_finally,
|
|
|
|
Finally->getSourceRange().getBegin()));
|
2016-06-22 04:10:11 +08:00
|
|
|
BuildScopeInformation(Finally->getBlock(), NewParentScope);
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
case Stmt::DeclStmtClass: {
|
|
|
|
// If this is a declstmt with a VLA definition, it defines a scope from here
|
|
|
|
// to the end of the containing context.
|
|
|
|
DeclStmt *DS = cast<DeclStmt>(S);
|
|
|
|
// The decl statement creates a scope if any of the decls in it are VLAs
|
|
|
|
// or have the cleanup attribute.
|
|
|
|
for (auto *I : DS->decls())
|
|
|
|
BuildScopeInformation(I, origParentScope);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
case Stmt::ObjCAtTryStmtClass: {
|
|
|
|
// Disallow jumps into any part of an @try statement by pushing a scope and
|
|
|
|
// walking all sub-stmts in that scope.
|
|
|
|
ObjCAtTryStmt *AT = cast<ObjCAtTryStmt>(S);
|
|
|
|
// Recursively walk the AST for the @try part.
|
|
|
|
{
|
|
|
|
unsigned NewParentScope = Scopes.size();
|
|
|
|
Scopes.push_back(GotoScope(ParentScope,
|
|
|
|
diag::note_protected_by_objc_try,
|
|
|
|
diag::note_exits_objc_try,
|
|
|
|
AT->getAtTryLoc()));
|
|
|
|
if (Stmt *TryPart = AT->getTryBody())
|
|
|
|
BuildScopeInformation(TryPart, NewParentScope);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Jump from the catch to the finally or try is not valid.
|
|
|
|
for (unsigned I = 0, N = AT->getNumCatchStmts(); I != N; ++I) {
|
|
|
|
ObjCAtCatchStmt *AC = AT->getCatchStmt(I);
|
|
|
|
unsigned NewParentScope = Scopes.size();
|
|
|
|
Scopes.push_back(GotoScope(ParentScope,
|
|
|
|
diag::note_protected_by_objc_catch,
|
|
|
|
diag::note_exits_objc_catch,
|
|
|
|
AC->getAtCatchLoc()));
|
|
|
|
// @catches are nested and it isn't
|
|
|
|
BuildScopeInformation(AC->getCatchBody(), NewParentScope);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Jump from the finally to the try or catch is not valid.
|
|
|
|
if (ObjCAtFinallyStmt *AF = AT->getFinallyStmt()) {
|
|
|
|
unsigned NewParentScope = Scopes.size();
|
|
|
|
Scopes.push_back(GotoScope(ParentScope,
|
|
|
|
diag::note_protected_by_objc_finally,
|
|
|
|
diag::note_exits_objc_finally,
|
|
|
|
AF->getAtFinallyLoc()));
|
|
|
|
BuildScopeInformation(AF, NewParentScope);
|
2015-02-04 01:06:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
Initial support for Win64 SEH IR emission
The lowering looks a lot like normal EH lowering, with the exception
that the exceptions are caught by executing filter expression code
instead of matching typeinfo globals. The filter expressions are
outlined into functions which are used in landingpad clauses where
typeinfo would normally go.
Major aspects that still need work:
- Non-call exceptions in __try bodies won't work yet. The plan is to
outline the __try block in the frontend to keep things simple.
- Filter expressions cannot use local variables until capturing is
implemented.
- __finally blocks will not run after exceptions. Fixing this requires
work in the LLVM SEH preparation pass.
The IR lowering looks like this:
// C code:
bool safe_div(int n, int d, int *r) {
__try {
*r = normal_div(n, d);
} __except(_exception_code() == EXCEPTION_INT_DIVIDE_BY_ZERO) {
return false;
}
return true;
}
; LLVM IR:
define i32 @filter(i8* %e, i8* %fp) {
%ehptrs = bitcast i8* %e to i32**
%ehrec = load i32** %ehptrs
%code = load i32* %ehrec
%matches = icmp eq i32 %code, i32 u0xC0000094
%matches.i32 = zext i1 %matches to i32
ret i32 %matches.i32
}
define i1 zeroext @safe_div(i32 %n, i32 %d, i32* %r) {
%rr = invoke i32 @normal_div(i32 %n, i32 %d)
to label %normal unwind to label %lpad
normal:
store i32 %rr, i32* %r
ret i1 1
lpad:
%ehvals = landingpad {i8*, i32} personality i32 (...)* @__C_specific_handler
catch i8* bitcast (i32 (i8*, i8*)* @filter to i8*)
%ehptr = extractvalue {i8*, i32} %ehvals, i32 0
%sel = extractvalue {i8*, i32} %ehvals, i32 1
%filter_sel = call i32 @llvm.eh.seh.typeid.for(i8* bitcast (i32 (i8*, i8*)* @filter to i8*))
%matches = icmp eq i32 %sel, %filter_sel
br i1 %matches, label %eh.except, label %eh.resume
eh.except:
ret i1 false
eh.resume:
resume
}
Reviewers: rjmccall, rsmith, majnemer
Differential Revision: http://reviews.llvm.org/D5607
llvm-svn: 226760
2015-01-22 09:36:17 +08:00
|
|
|
|
2016-06-22 04:10:11 +08:00
|
|
|
case Stmt::ObjCAtSynchronizedStmtClass: {
|
|
|
|
// Disallow jumps into the protected statement of an @synchronized, but
|
|
|
|
// allow jumps into the object expression it protects.
|
|
|
|
ObjCAtSynchronizedStmt *AS = cast<ObjCAtSynchronizedStmt>(S);
|
|
|
|
// Recursively walk the AST for the @synchronized object expr, it is
|
|
|
|
// evaluated in the normal scope.
|
|
|
|
BuildScopeInformation(AS->getSynchExpr(), ParentScope);
|
|
|
|
|
|
|
|
// Recursively walk the AST for the @synchronized part, protected by a new
|
|
|
|
// scope.
|
|
|
|
unsigned NewParentScope = Scopes.size();
|
|
|
|
Scopes.push_back(GotoScope(ParentScope,
|
|
|
|
diag::note_protected_by_objc_synchronized,
|
|
|
|
diag::note_exits_objc_synchronized,
|
|
|
|
AS->getAtSynchronizedLoc()));
|
|
|
|
BuildScopeInformation(AS->getSynchBody(), NewParentScope);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
case Stmt::ObjCAutoreleasePoolStmtClass: {
|
|
|
|
// Disallow jumps into the protected statement of an @autoreleasepool.
|
|
|
|
ObjCAutoreleasePoolStmt *AS = cast<ObjCAutoreleasePoolStmt>(S);
|
|
|
|
// Recursively walk the AST for the @autoreleasepool part, protected by a
|
|
|
|
// new scope.
|
|
|
|
unsigned NewParentScope = Scopes.size();
|
|
|
|
Scopes.push_back(GotoScope(ParentScope,
|
|
|
|
diag::note_protected_by_objc_autoreleasepool,
|
|
|
|
diag::note_exits_objc_autoreleasepool,
|
|
|
|
AS->getAtLoc()));
|
|
|
|
BuildScopeInformation(AS->getSubStmt(), NewParentScope);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
case Stmt::ExprWithCleanupsClass: {
|
|
|
|
// Disallow jumps past full-expressions that use blocks with
|
|
|
|
// non-trivial cleanups of their captures. This is theoretically
|
|
|
|
// implementable but a lot of work which we haven't felt up to doing.
|
|
|
|
ExprWithCleanups *EWC = cast<ExprWithCleanups>(S);
|
|
|
|
for (unsigned i = 0, e = EWC->getNumObjects(); i != e; ++i) {
|
2020-03-11 05:06:25 +08:00
|
|
|
if (auto *BDecl = EWC->getObject(i).dyn_cast<BlockDecl *>())
|
|
|
|
for (const auto &CI : BDecl->captures()) {
|
|
|
|
VarDecl *variable = CI.getVariable();
|
|
|
|
BuildScopeInformation(variable, BDecl, origParentScope);
|
|
|
|
}
|
|
|
|
else if (auto *CLE = EWC->getObject(i).dyn_cast<CompoundLiteralExpr *>())
|
|
|
|
BuildScopeInformation(CLE, origParentScope);
|
|
|
|
else
|
|
|
|
llvm_unreachable("unexpected cleanup object type");
|
2016-06-22 04:10:11 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case Stmt::MaterializeTemporaryExprClass: {
|
|
|
|
// Disallow jumps out of scopes containing temporaries lifetime-extended to
|
|
|
|
// automatic storage duration.
|
|
|
|
MaterializeTemporaryExpr *MTE = cast<MaterializeTemporaryExpr>(S);
|
|
|
|
if (MTE->getStorageDuration() == SD_Automatic) {
|
|
|
|
SmallVector<const Expr *, 4> CommaLHS;
|
|
|
|
SmallVector<SubobjectAdjustment, 4> Adjustments;
|
|
|
|
const Expr *ExtendedObject =
|
2019-11-17 18:41:55 +08:00
|
|
|
MTE->getSubExpr()->skipRValueSubobjectAdjustments(CommaLHS,
|
|
|
|
Adjustments);
|
2016-06-22 04:10:11 +08:00
|
|
|
if (ExtendedObject->getType().isDestructedType()) {
|
|
|
|
Scopes.push_back(GotoScope(ParentScope, 0,
|
|
|
|
diag::note_exits_temporary_dtor,
|
|
|
|
ExtendedObject->getExprLoc()));
|
|
|
|
origParentScope = Scopes.size()-1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case Stmt::CaseStmtClass:
|
|
|
|
case Stmt::DefaultStmtClass:
|
|
|
|
case Stmt::LabelStmtClass:
|
|
|
|
LabelAndGotoScopes[S] = ParentScope;
|
|
|
|
break;
|
|
|
|
|
2021-04-16 07:49:19 +08:00
|
|
|
case Stmt::AttributedStmtClass: {
|
|
|
|
AttributedStmt *AS = cast<AttributedStmt>(S);
|
|
|
|
if (GetMustTailAttr(AS)) {
|
|
|
|
LabelAndGotoScopes[AS] = ParentScope;
|
|
|
|
MustTailStmts.push_back(AS);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2010-05-12 08:58:13 +08:00
|
|
|
default:
|
2020-07-07 01:32:11 +08:00
|
|
|
if (auto *ED = dyn_cast<OMPExecutableDirective>(S)) {
|
|
|
|
if (!ED->isStandaloneDirective()) {
|
|
|
|
unsigned NewParentScope = Scopes.size();
|
|
|
|
Scopes.emplace_back(ParentScope,
|
|
|
|
diag::note_omp_protected_structured_block,
|
|
|
|
diag::note_omp_exits_structured_block,
|
|
|
|
ED->getStructuredBlock()->getBeginLoc());
|
|
|
|
BuildScopeInformation(ED->getStructuredBlock(), NewParentScope);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2010-05-12 08:58:13 +08:00
|
|
|
break;
|
2009-04-19 12:46:21 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2015-07-03 05:03:14 +08:00
|
|
|
for (Stmt *SubStmt : S->children()) {
|
2016-07-14 08:11:03 +08:00
|
|
|
if (!SubStmt)
|
|
|
|
continue;
|
|
|
|
if (StmtsToSkip) {
|
|
|
|
--StmtsToSkip;
|
2010-06-22 07:44:13 +08:00
|
|
|
continue;
|
|
|
|
}
|
2015-06-05 06:53:21 +08:00
|
|
|
|
2010-08-03 07:33:14 +08:00
|
|
|
// Cases, labels, and defaults aren't "scope parents". It's also
|
|
|
|
// important to handle these iteratively instead of recursively in
|
|
|
|
// order to avoid blowing out the stack.
|
|
|
|
while (true) {
|
|
|
|
Stmt *Next;
|
2016-10-26 10:00:00 +08:00
|
|
|
if (SwitchCase *SC = dyn_cast<SwitchCase>(SubStmt))
|
|
|
|
Next = SC->getSubStmt();
|
2011-02-17 15:39:24 +08:00
|
|
|
else if (LabelStmt *LS = dyn_cast<LabelStmt>(SubStmt))
|
|
|
|
Next = LS->getSubStmt();
|
2010-08-03 07:33:14 +08:00
|
|
|
else
|
|
|
|
break;
|
|
|
|
|
|
|
|
LabelAndGotoScopes[SubStmt] = ParentScope;
|
|
|
|
SubStmt = Next;
|
|
|
|
}
|
|
|
|
|
2009-04-19 12:46:21 +08:00
|
|
|
// Recursively walk the AST.
|
|
|
|
BuildScopeInformation(SubStmt, ParentScope);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// VerifyJumps - Verify each element of the Jumps array to see if they are
|
|
|
|
/// valid, emitting diagnostics if not.
|
|
|
|
void JumpScopeChecker::VerifyJumps() {
|
|
|
|
while (!Jumps.empty()) {
|
|
|
|
Stmt *Jump = Jumps.pop_back_val();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
|
|
|
// With a goto,
|
2009-04-19 12:46:21 +08:00
|
|
|
if (GotoStmt *GS = dyn_cast<GotoStmt>(Jump)) {
|
2014-09-22 10:21:54 +08:00
|
|
|
// The label may not have a statement if it's coming from inline MS ASM.
|
|
|
|
if (GS->getLabel()->getStmt()) {
|
|
|
|
CheckJump(GS, GS->getLabel()->getStmt(), GS->getGotoLoc(),
|
|
|
|
diag::err_goto_into_protected_scope,
|
|
|
|
diag::ext_goto_into_protected_scope,
|
|
|
|
diag::warn_cxx98_compat_goto_into_protected_scope);
|
|
|
|
}
|
|
|
|
CheckGotoStmt(GS);
|
2009-04-19 12:46:21 +08:00
|
|
|
continue;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2010-10-28 16:53:48 +08:00
|
|
|
// We only get indirect gotos here when they have a constant target.
|
|
|
|
if (IndirectGotoStmt *IGS = dyn_cast<IndirectGotoStmt>(Jump)) {
|
2011-02-17 15:39:24 +08:00
|
|
|
LabelDecl *Target = IGS->getConstantTarget();
|
|
|
|
CheckJump(IGS, Target->getStmt(), IGS->getGotoLoc(),
|
2011-09-17 07:15:32 +08:00
|
|
|
diag::err_goto_into_protected_scope,
|
2014-07-19 09:39:17 +08:00
|
|
|
diag::ext_goto_into_protected_scope,
|
2011-10-21 05:42:12 +08:00
|
|
|
diag::warn_cxx98_compat_goto_into_protected_scope);
|
2010-10-28 16:53:48 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2010-05-12 08:58:13 +08:00
|
|
|
SwitchStmt *SS = cast<SwitchStmt>(Jump);
|
|
|
|
for (SwitchCase *SC = SS->getSwitchCaseList(); SC;
|
|
|
|
SC = SC->getNextSwitchCase()) {
|
2014-05-09 16:40:10 +08:00
|
|
|
if (CHECK_PERMISSIVE(!LabelAndGotoScopes.count(SC)))
|
|
|
|
continue;
|
2012-12-25 22:51:39 +08:00
|
|
|
SourceLocation Loc;
|
|
|
|
if (CaseStmt *CS = dyn_cast<CaseStmt>(SC))
|
2018-08-10 05:08:08 +08:00
|
|
|
Loc = CS->getBeginLoc();
|
2012-12-25 22:51:39 +08:00
|
|
|
else if (DefaultStmt *DS = dyn_cast<DefaultStmt>(SC))
|
2018-08-10 05:08:08 +08:00
|
|
|
Loc = DS->getBeginLoc();
|
2012-12-25 22:51:39 +08:00
|
|
|
else
|
2018-08-10 05:08:08 +08:00
|
|
|
Loc = SC->getBeginLoc();
|
2012-12-25 22:51:39 +08:00
|
|
|
CheckJump(SS, SC, Loc, diag::err_switch_into_protected_scope, 0,
|
2011-10-21 05:42:12 +08:00
|
|
|
diag::warn_cxx98_compat_switch_into_protected_scope);
|
2009-04-19 12:46:21 +08:00
|
|
|
}
|
2010-05-12 08:58:13 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-03 23:57:25 +08:00
|
|
|
/// VerifyIndirectOrAsmJumps - Verify whether any possible indirect goto or
|
|
|
|
/// asm goto jump might cross a protection boundary. Unlike direct jumps,
|
|
|
|
/// indirect or asm goto jumps count cleanups as protection boundaries:
|
|
|
|
/// since there's no way to know where the jump is going, we can't implicitly
|
|
|
|
/// run the right cleanups the way we can with direct jumps.
|
|
|
|
/// Thus, an indirect/asm jump is "trivial" if it bypasses no
|
|
|
|
/// initializations and no teardowns. More formally, an indirect/asm jump
|
2010-05-12 10:37:54 +08:00
|
|
|
/// from A to B is trivial if the path out from A to DCA(A,B) is
|
|
|
|
/// trivial and the path in from DCA(A,B) to B is trivial, where
|
|
|
|
/// DCA(A,B) is the deepest common ancestor of A and B.
|
|
|
|
/// Jump-triviality is transitive but asymmetric.
|
2010-05-12 08:58:13 +08:00
|
|
|
///
|
|
|
|
/// A path in is trivial if none of the entered scopes have an InDiag.
|
|
|
|
/// A path out is trivial is none of the exited scopes have an OutDiag.
|
2010-05-12 10:37:54 +08:00
|
|
|
///
|
|
|
|
/// Under these definitions, this function checks that the indirect
|
|
|
|
/// jump between A and B is trivial for every indirect goto statement A
|
|
|
|
/// and every label B whose address was taken in the function.
|
2019-06-03 23:57:25 +08:00
|
|
|
void JumpScopeChecker::VerifyIndirectOrAsmJumps(bool IsAsmGoto) {
|
|
|
|
SmallVector<Stmt*, 4> GotoJumps = IsAsmGoto ? AsmJumps : IndirectJumps;
|
|
|
|
if (GotoJumps.empty())
|
|
|
|
return;
|
|
|
|
SmallVector<LabelDecl *, 4> JumpTargets =
|
|
|
|
IsAsmGoto ? AsmJumpTargets : IndirectJumpTargets;
|
2010-05-12 08:58:13 +08:00
|
|
|
// If there aren't any address-of-label expressions in this function,
|
|
|
|
// complain about the first indirect goto.
|
2019-06-03 23:57:25 +08:00
|
|
|
if (JumpTargets.empty()) {
|
|
|
|
assert(!IsAsmGoto &&"only indirect goto can get here");
|
|
|
|
S.Diag(GotoJumps[0]->getBeginLoc(),
|
2010-05-12 08:58:13 +08:00
|
|
|
diag::err_indirect_goto_without_addrlabel);
|
|
|
|
return;
|
|
|
|
}
|
2010-05-12 10:37:54 +08:00
|
|
|
// Collect a single representative of every scope containing an
|
2019-06-03 23:57:25 +08:00
|
|
|
// indirect or asm goto. For most code bases, this substantially cuts
|
2010-05-12 10:37:54 +08:00
|
|
|
// down on the number of jump sites we'll have to consider later.
|
2019-06-03 23:57:25 +08:00
|
|
|
typedef std::pair<unsigned, Stmt*> JumpScope;
|
2011-07-23 18:55:15 +08:00
|
|
|
SmallVector<JumpScope, 32> JumpScopes;
|
2010-05-12 08:58:13 +08:00
|
|
|
{
|
2019-06-03 23:57:25 +08:00
|
|
|
llvm::DenseMap<unsigned, Stmt*> JumpScopesMap;
|
|
|
|
for (SmallVectorImpl<Stmt *>::iterator I = GotoJumps.begin(),
|
|
|
|
E = GotoJumps.end();
|
|
|
|
I != E; ++I) {
|
|
|
|
Stmt *IG = *I;
|
2014-05-09 16:40:10 +08:00
|
|
|
if (CHECK_PERMISSIVE(!LabelAndGotoScopes.count(IG)))
|
|
|
|
continue;
|
2010-05-12 08:58:13 +08:00
|
|
|
unsigned IGScope = LabelAndGotoScopes[IG];
|
2019-06-03 23:57:25 +08:00
|
|
|
Stmt *&Entry = JumpScopesMap[IGScope];
|
2010-05-12 08:58:13 +08:00
|
|
|
if (!Entry) Entry = IG;
|
|
|
|
}
|
|
|
|
JumpScopes.reserve(JumpScopesMap.size());
|
2019-06-03 23:57:25 +08:00
|
|
|
for (llvm::DenseMap<unsigned, Stmt *>::iterator I = JumpScopesMap.begin(),
|
|
|
|
E = JumpScopesMap.end();
|
|
|
|
I != E; ++I)
|
2010-05-12 08:58:13 +08:00
|
|
|
JumpScopes.push_back(*I);
|
|
|
|
}
|
|
|
|
|
2010-05-12 10:37:54 +08:00
|
|
|
// Collect a single representative of every scope containing a
|
|
|
|
// label whose address was taken somewhere in the function.
|
|
|
|
// For most code bases, there will be only one such scope.
|
2011-02-17 15:39:24 +08:00
|
|
|
llvm::DenseMap<unsigned, LabelDecl*> TargetScopes;
|
2019-06-03 23:57:25 +08:00
|
|
|
for (SmallVectorImpl<LabelDecl *>::iterator I = JumpTargets.begin(),
|
|
|
|
E = JumpTargets.end();
|
2010-05-12 08:58:13 +08:00
|
|
|
I != E; ++I) {
|
2011-02-17 15:39:24 +08:00
|
|
|
LabelDecl *TheLabel = *I;
|
2014-05-09 16:40:10 +08:00
|
|
|
if (CHECK_PERMISSIVE(!LabelAndGotoScopes.count(TheLabel->getStmt())))
|
|
|
|
continue;
|
2011-02-17 15:39:24 +08:00
|
|
|
unsigned LabelScope = LabelAndGotoScopes[TheLabel->getStmt()];
|
|
|
|
LabelDecl *&Target = TargetScopes[LabelScope];
|
2010-05-12 08:58:13 +08:00
|
|
|
if (!Target) Target = TheLabel;
|
|
|
|
}
|
|
|
|
|
2010-05-12 10:37:54 +08:00
|
|
|
// For each target scope, make sure it's trivially reachable from
|
|
|
|
// every scope containing a jump site.
|
|
|
|
//
|
|
|
|
// A path between scopes always consists of exitting zero or more
|
|
|
|
// scopes, then entering zero or more scopes. We build a set of
|
|
|
|
// of scopes S from which the target scope can be trivially
|
|
|
|
// entered, then verify that every jump scope can be trivially
|
|
|
|
// exitted to reach a scope in S.
|
2010-05-12 08:58:13 +08:00
|
|
|
llvm::BitVector Reachable(Scopes.size(), false);
|
2011-02-17 15:39:24 +08:00
|
|
|
for (llvm::DenseMap<unsigned,LabelDecl*>::iterator
|
2010-05-12 08:58:13 +08:00
|
|
|
TI = TargetScopes.begin(), TE = TargetScopes.end(); TI != TE; ++TI) {
|
|
|
|
unsigned TargetScope = TI->first;
|
2011-02-17 15:39:24 +08:00
|
|
|
LabelDecl *TargetLabel = TI->second;
|
2010-05-12 08:58:13 +08:00
|
|
|
|
|
|
|
Reachable.reset();
|
|
|
|
|
|
|
|
// Mark all the enclosing scopes from which you can safely jump
|
2010-05-12 10:37:54 +08:00
|
|
|
// into the target scope. 'Min' will end up being the index of
|
|
|
|
// the shallowest such scope.
|
2010-05-12 08:58:13 +08:00
|
|
|
unsigned Min = TargetScope;
|
|
|
|
while (true) {
|
|
|
|
Reachable.set(Min);
|
|
|
|
|
|
|
|
// Don't go beyond the outermost scope.
|
|
|
|
if (Min == 0) break;
|
|
|
|
|
2010-05-12 10:37:54 +08:00
|
|
|
// Stop if we can't trivially enter the current scope.
|
2010-05-12 08:58:13 +08:00
|
|
|
if (Scopes[Min].InDiag) break;
|
2009-04-19 12:46:21 +08:00
|
|
|
|
2010-05-12 08:58:13 +08:00
|
|
|
Min = Scopes[Min].ParentScope;
|
2009-04-19 12:46:21 +08:00
|
|
|
}
|
|
|
|
|
2010-05-12 08:58:13 +08:00
|
|
|
// Walk through all the jump sites, checking that they can trivially
|
|
|
|
// reach this label scope.
|
2011-07-23 18:55:15 +08:00
|
|
|
for (SmallVectorImpl<JumpScope>::iterator
|
2010-05-12 08:58:13 +08:00
|
|
|
I = JumpScopes.begin(), E = JumpScopes.end(); I != E; ++I) {
|
|
|
|
unsigned Scope = I->first;
|
|
|
|
|
|
|
|
// Walk out the "scope chain" for this scope, looking for a scope
|
2010-05-12 10:37:54 +08:00
|
|
|
// we've marked reachable. For well-formed code this amortizes
|
|
|
|
// to O(JumpScopes.size() / Scopes.size()): we only iterate
|
|
|
|
// when we see something unmarked, and in well-formed code we
|
|
|
|
// mark everything we iterate past.
|
2010-05-12 08:58:13 +08:00
|
|
|
bool IsReachable = false;
|
|
|
|
while (true) {
|
|
|
|
if (Reachable.test(Scope)) {
|
|
|
|
// If we find something reachable, mark all the scopes we just
|
|
|
|
// walked through as reachable.
|
|
|
|
for (unsigned S = I->first; S != Scope; S = Scopes[S].ParentScope)
|
|
|
|
Reachable.set(S);
|
|
|
|
IsReachable = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Don't walk out if we've reached the top-level scope or we've
|
|
|
|
// gotten shallower than the shallowest reachable scope.
|
|
|
|
if (Scope == 0 || Scope < Min) break;
|
|
|
|
|
|
|
|
// Don't walk out through an out-diagnostic.
|
|
|
|
if (Scopes[Scope].OutDiag) break;
|
|
|
|
|
|
|
|
Scope = Scopes[Scope].ParentScope;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Only diagnose if we didn't find something.
|
|
|
|
if (IsReachable) continue;
|
|
|
|
|
2019-06-03 23:57:25 +08:00
|
|
|
DiagnoseIndirectOrAsmJump(I->second, I->first, TargetLabel, TargetScope);
|
2009-04-19 12:46:21 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-21 05:42:12 +08:00
|
|
|
/// Return true if a particular error+note combination must be downgraded to a
|
|
|
|
/// warning in Microsoft mode.
|
|
|
|
static bool IsMicrosoftJumpWarning(unsigned JumpDiag, unsigned InDiagNote) {
|
|
|
|
return (JumpDiag == diag::err_goto_into_protected_scope &&
|
|
|
|
(InDiagNote == diag::note_protected_by_variable_init ||
|
|
|
|
InDiagNote == diag::note_protected_by_variable_nontriv_destructor));
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Return true if a particular note should be downgraded to a compatibility
|
|
|
|
/// warning in C++11 mode.
|
|
|
|
static bool IsCXX98CompatWarning(Sema &S, unsigned InDiagNote) {
|
2013-01-02 19:42:31 +08:00
|
|
|
return S.getLangOpts().CPlusPlus11 &&
|
2011-10-21 05:42:12 +08:00
|
|
|
InDiagNote == diag::note_protected_by_variable_non_pod;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Produce primary diagnostic for an indirect jump statement.
|
2019-06-03 23:57:25 +08:00
|
|
|
static void DiagnoseIndirectOrAsmJumpStmt(Sema &S, Stmt *Jump,
|
|
|
|
LabelDecl *Target, bool &Diagnosed) {
|
2011-10-21 05:42:12 +08:00
|
|
|
if (Diagnosed)
|
|
|
|
return;
|
2019-06-03 23:57:25 +08:00
|
|
|
bool IsAsmGoto = isa<GCCAsmStmt>(Jump);
|
|
|
|
S.Diag(Jump->getBeginLoc(), diag::err_indirect_goto_in_protected_scope)
|
|
|
|
<< IsAsmGoto;
|
|
|
|
S.Diag(Target->getStmt()->getIdentLoc(), diag::note_indirect_goto_target)
|
|
|
|
<< IsAsmGoto;
|
2011-10-21 05:42:12 +08:00
|
|
|
Diagnosed = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Produce note diagnostics for a jump into a protected scope.
|
2012-02-22 17:38:11 +08:00
|
|
|
void JumpScopeChecker::NoteJumpIntoScopes(ArrayRef<unsigned> ToScopes) {
|
2014-05-09 16:40:10 +08:00
|
|
|
if (CHECK_PERMISSIVE(ToScopes.empty()))
|
|
|
|
return;
|
2011-10-21 05:42:12 +08:00
|
|
|
for (unsigned I = 0, E = ToScopes.size(); I != E; ++I)
|
|
|
|
if (Scopes[ToScopes[I]].InDiag)
|
|
|
|
S.Diag(Scopes[ToScopes[I]].Loc, Scopes[ToScopes[I]].InDiag);
|
|
|
|
}
|
|
|
|
|
2010-05-12 10:37:54 +08:00
|
|
|
/// Diagnose an indirect jump which is known to cross scopes.
|
2019-06-03 23:57:25 +08:00
|
|
|
void JumpScopeChecker::DiagnoseIndirectOrAsmJump(Stmt *Jump, unsigned JumpScope,
|
|
|
|
LabelDecl *Target,
|
|
|
|
unsigned TargetScope) {
|
2014-05-09 16:40:10 +08:00
|
|
|
if (CHECK_PERMISSIVE(JumpScope == TargetScope))
|
|
|
|
return;
|
2010-05-12 08:58:13 +08:00
|
|
|
|
2010-05-12 10:37:54 +08:00
|
|
|
unsigned Common = GetDeepestCommonScope(JumpScope, TargetScope);
|
2011-10-21 05:42:12 +08:00
|
|
|
bool Diagnosed = false;
|
2010-05-12 10:37:54 +08:00
|
|
|
|
|
|
|
// Walk out the scope chain until we reach the common ancestor.
|
|
|
|
for (unsigned I = JumpScope; I != Common; I = Scopes[I].ParentScope)
|
2011-10-21 05:42:12 +08:00
|
|
|
if (Scopes[I].OutDiag) {
|
2019-06-03 23:57:25 +08:00
|
|
|
DiagnoseIndirectOrAsmJumpStmt(S, Jump, Target, Diagnosed);
|
2010-05-12 10:37:54 +08:00
|
|
|
S.Diag(Scopes[I].Loc, Scopes[I].OutDiag);
|
2011-10-21 05:42:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SmallVector<unsigned, 10> ToScopesCXX98Compat;
|
2010-05-12 08:58:13 +08:00
|
|
|
|
|
|
|
// Now walk into the scopes containing the label whose address was taken.
|
2010-05-12 10:37:54 +08:00
|
|
|
for (unsigned I = TargetScope; I != Common; I = Scopes[I].ParentScope)
|
2011-10-21 05:42:12 +08:00
|
|
|
if (IsCXX98CompatWarning(S, Scopes[I].InDiag))
|
|
|
|
ToScopesCXX98Compat.push_back(I);
|
|
|
|
else if (Scopes[I].InDiag) {
|
2019-06-03 23:57:25 +08:00
|
|
|
DiagnoseIndirectOrAsmJumpStmt(S, Jump, Target, Diagnosed);
|
2010-05-12 10:37:54 +08:00
|
|
|
S.Diag(Scopes[I].Loc, Scopes[I].InDiag);
|
2011-10-21 05:42:12 +08:00
|
|
|
}
|
2010-05-12 08:58:13 +08:00
|
|
|
|
2011-10-21 05:42:12 +08:00
|
|
|
// Diagnose this jump if it would be ill-formed in C++98.
|
|
|
|
if (!Diagnosed && !ToScopesCXX98Compat.empty()) {
|
2019-06-03 23:57:25 +08:00
|
|
|
bool IsAsmGoto = isa<GCCAsmStmt>(Jump);
|
|
|
|
S.Diag(Jump->getBeginLoc(),
|
|
|
|
diag::warn_cxx98_compat_indirect_goto_in_protected_scope)
|
|
|
|
<< IsAsmGoto;
|
|
|
|
S.Diag(Target->getStmt()->getIdentLoc(), diag::note_indirect_goto_target)
|
|
|
|
<< IsAsmGoto;
|
2011-10-21 05:42:12 +08:00
|
|
|
NoteJumpIntoScopes(ToScopesCXX98Compat);
|
|
|
|
}
|
2011-09-13 18:26:51 +08:00
|
|
|
}
|
|
|
|
|
2009-04-19 12:46:21 +08:00
|
|
|
/// CheckJump - Validate that the specified jump statement is valid: that it is
|
|
|
|
/// jumping within or out of its current scope, not into a deeper one.
|
2011-09-13 18:26:51 +08:00
|
|
|
void JumpScopeChecker::CheckJump(Stmt *From, Stmt *To, SourceLocation DiagLoc,
|
2011-10-21 05:42:12 +08:00
|
|
|
unsigned JumpDiagError, unsigned JumpDiagWarning,
|
|
|
|
unsigned JumpDiagCXX98Compat) {
|
2014-05-09 16:40:10 +08:00
|
|
|
if (CHECK_PERMISSIVE(!LabelAndGotoScopes.count(From)))
|
|
|
|
return;
|
|
|
|
if (CHECK_PERMISSIVE(!LabelAndGotoScopes.count(To)))
|
|
|
|
return;
|
2009-04-19 12:46:21 +08:00
|
|
|
|
2014-05-09 16:40:10 +08:00
|
|
|
unsigned FromScope = LabelAndGotoScopes[From];
|
2009-04-19 12:46:21 +08:00
|
|
|
unsigned ToScope = LabelAndGotoScopes[To];
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-04-19 12:46:21 +08:00
|
|
|
// Common case: exactly the same scope, which is fine.
|
|
|
|
if (FromScope == ToScope) return;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2015-03-09 12:27:56 +08:00
|
|
|
// Warn on gotos out of __finally blocks.
|
|
|
|
if (isa<GotoStmt>(From) || isa<IndirectGotoStmt>(From)) {
|
|
|
|
// If FromScope > ToScope, FromScope is more nested and the jump goes to a
|
|
|
|
// less nested scope. Check if it crosses a __finally along the way.
|
|
|
|
for (unsigned I = FromScope; I > ToScope; I = Scopes[I].ParentScope) {
|
|
|
|
if (Scopes[I].InDiag == diag::note_protected_by_seh_finally) {
|
2018-08-10 05:08:08 +08:00
|
|
|
S.Diag(From->getBeginLoc(), diag::warn_jump_out_of_seh_finally);
|
2015-03-09 12:27:56 +08:00
|
|
|
break;
|
|
|
|
}
|
2020-07-07 01:32:11 +08:00
|
|
|
if (Scopes[I].InDiag == diag::note_omp_protected_structured_block) {
|
|
|
|
S.Diag(From->getBeginLoc(), diag::err_goto_into_protected_scope);
|
|
|
|
S.Diag(To->getBeginLoc(), diag::note_omp_exits_structured_block);
|
|
|
|
break;
|
|
|
|
}
|
2015-03-09 12:27:56 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-05-12 10:37:54 +08:00
|
|
|
unsigned CommonScope = GetDeepestCommonScope(FromScope, ToScope);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2010-05-12 10:37:54 +08:00
|
|
|
// It's okay to jump out from a nested scope.
|
|
|
|
if (CommonScope == ToScope) return;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2010-05-12 10:37:54 +08:00
|
|
|
// Pull out (and reverse) any scopes we might need to diagnose skipping.
|
2011-10-21 05:42:12 +08:00
|
|
|
SmallVector<unsigned, 10> ToScopesCXX98Compat;
|
2011-09-13 18:26:51 +08:00
|
|
|
SmallVector<unsigned, 10> ToScopesError;
|
|
|
|
SmallVector<unsigned, 10> ToScopesWarning;
|
|
|
|
for (unsigned I = ToScope; I != CommonScope; I = Scopes[I].ParentScope) {
|
2014-01-14 20:51:41 +08:00
|
|
|
if (S.getLangOpts().MSVCCompat && JumpDiagWarning != 0 &&
|
2011-09-13 18:26:51 +08:00
|
|
|
IsMicrosoftJumpWarning(JumpDiagError, Scopes[I].InDiag))
|
|
|
|
ToScopesWarning.push_back(I);
|
2011-10-21 05:42:12 +08:00
|
|
|
else if (IsCXX98CompatWarning(S, Scopes[I].InDiag))
|
|
|
|
ToScopesCXX98Compat.push_back(I);
|
2011-09-13 18:26:51 +08:00
|
|
|
else if (Scopes[I].InDiag)
|
|
|
|
ToScopesError.push_back(I);
|
|
|
|
}
|
2010-05-12 08:58:13 +08:00
|
|
|
|
2011-09-13 18:26:51 +08:00
|
|
|
// Handle warnings.
|
|
|
|
if (!ToScopesWarning.empty()) {
|
|
|
|
S.Diag(DiagLoc, JumpDiagWarning);
|
2011-10-21 05:42:12 +08:00
|
|
|
NoteJumpIntoScopes(ToScopesWarning);
|
[Windows SEH]: HARDWARE EXCEPTION HANDLING (MSVC -EHa) - Part 1
This patch is the Part-1 (FE Clang) implementation of HW Exception handling.
This new feature adds the support of Hardware Exception for Microsoft Windows
SEH (Structured Exception Handling).
This is the first step of this project; only X86_64 target is enabled in this patch.
Compiler options:
For clang-cl.exe, the option is -EHa, the same as MSVC.
For clang.exe, the extra option is -fasync-exceptions,
plus -triple x86_64-windows -fexceptions and -fcxx-exceptions as usual.
NOTE:: Without the -EHa or -fasync-exceptions, this patch is a NO-DIFF change.
The rules for C code:
For C-code, one way (MSVC approach) to achieve SEH -EHa semantic is to follow
three rules:
* First, no exception can move in or out of _try region., i.e., no "potential
faulty instruction can be moved across _try boundary.
* Second, the order of exceptions for instructions 'directly' under a _try
must be preserved (not applied to those in callees).
* Finally, global states (local/global/heap variables) that can be read
outside of _try region must be updated in memory (not just in register)
before the subsequent exception occurs.
The impact to C++ code:
Although SEH is a feature for C code, -EHa does have a profound effect on C++
side. When a C++ function (in the same compilation unit with option -EHa ) is
called by a SEH C function, a hardware exception occurs in C++ code can also
be handled properly by an upstream SEH _try-handler or a C++ catch(...).
As such, when that happens in the middle of an object's life scope, the dtor
must be invoked the same way as C++ Synchronous Exception during unwinding
process.
Design:
A natural way to achieve the rules above in LLVM today is to allow an EH edge
added on memory/computation instruction (previous iload/istore idea) so that
exception path is modeled in Flow graph preciously. However, tracking every
single memory instruction and potential faulty instruction can create many
Invokes, complicate flow graph and possibly result in negative performance
impact for downstream optimization and code generation. Making all
optimizations be aware of the new semantic is also substantial.
This design does not intend to model exception path at instruction level.
Instead, the proposed design tracks and reports EH state at BLOCK-level to
reduce the complexity of flow graph and minimize the performance-impact on CPP
code under -EHa option.
One key element of this design is the ability to compute State number at
block-level. Our algorithm is based on the following rationales:
A _try scope is always a SEME (Single Entry Multiple Exits) region as jumping
into a _try is not allowed. The single entry must start with a seh_try_begin()
invoke with a correct State number that is the initial state of the SEME.
Through control-flow, state number is propagated into all blocks. Side exits
marked by seh_try_end() will unwind to parent state based on existing
SEHUnwindMap[].
Note side exits can ONLY jump into parent scopes (lower state number).
Thus, when a block succeeds various states from its predecessors, the lowest
State triumphs others. If some exits flow to unreachable, propagation on those
paths terminate, not affecting remaining blocks.
For CPP code, object lifetime region is usually a SEME as SEH _try.
However there is one rare exception: jumping into a lifetime that has Dtor but
has no Ctor is warned, but allowed:
Warning: jump bypasses variable with a non-trivial destructor
In that case, the region is actually a MEME (multiple entry multiple exits).
Our solution is to inject a eha_scope_begin() invoke in the side entry block to
ensure a correct State.
Implementation:
Part-1: Clang implementation described below.
Two intrinsic are created to track CPP object scopes; eha_scope_begin() and eha_scope_end().
_scope_begin() is immediately added after ctor() is called and EHStack is pushed.
So it must be an invoke, not a call. With that it's also guaranteed an
EH-cleanup-pad is created regardless whether there exists a call in this scope.
_scope_end is added before dtor(). These two intrinsics make the computation of
Block-State possible in downstream code gen pass, even in the presence of
ctor/dtor inlining.
Two intrinsic, seh_try_begin() and seh_try_end(), are added for C-code to mark
_try boundary and to prevent from exceptions being moved across _try boundary.
All memory instructions inside a _try are considered as 'volatile' to assure
2nd and 3rd rules for C-code above. This is a little sub-optimized. But it's
acceptable as the amount of code directly under _try is very small.
Part-2 (will be in Part-2 patch): LLVM implementation described below.
For both C++ & C-code, the state of each block is computed at the same place in
BE (WinEHPreparing pass) where all other EH tables/maps are calculated.
In addition to _scope_begin & _scope_end, the computation of block state also
rely on the existing State tracking code (UnwindMap and InvokeStateMap).
For both C++ & C-code, the state of each block with potential trap instruction
is marked and reported in DAG Instruction Selection pass, the same place where
the state for -EHsc (synchronous exceptions) is done.
If the first instruction in a reported block scope can trap, a Nop is injected
before this instruction. This nop is needed to accommodate LLVM Windows EH
implementation, in which the address in IPToState table is offset by +1.
(note the purpose of that is to ensure the return address of a call is in the
same scope as the call address.
The handler for catch(...) for -EHa must handle HW exception. So it is
'adjective' flag is reset (it cannot be IsStdDotDot (0x40) that only catches
C++ exceptions).
Suppress push/popTerminate() scope (from noexcept/noTHrow) so that HW
exceptions can be passed through.
Original llvm-dev [RFC] discussions can be found in these two threads below:
https://lists.llvm.org/pipermail/llvm-dev/2020-March/140541.html
https://lists.llvm.org/pipermail/llvm-dev/2020-April/141338.html
Differential Revision: https://reviews.llvm.org/D80344/new/
2021-05-18 13:06:32 +08:00
|
|
|
assert(isa<LabelStmt>(To));
|
|
|
|
LabelStmt *Label = cast<LabelStmt>(To);
|
|
|
|
Label->setSideEntry(true);
|
2011-09-13 18:26:51 +08:00
|
|
|
}
|
2010-05-12 08:58:13 +08:00
|
|
|
|
2011-09-13 18:26:51 +08:00
|
|
|
// Handle errors.
|
|
|
|
if (!ToScopesError.empty()) {
|
|
|
|
S.Diag(DiagLoc, JumpDiagError);
|
2011-10-21 05:42:12 +08:00
|
|
|
NoteJumpIntoScopes(ToScopesError);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handle -Wc++98-compat warnings if the jump is well-formed.
|
|
|
|
if (ToScopesError.empty() && !ToScopesCXX98Compat.empty()) {
|
|
|
|
S.Diag(DiagLoc, JumpDiagCXX98Compat);
|
|
|
|
NoteJumpIntoScopes(ToScopesCXX98Compat);
|
2011-09-13 18:26:51 +08:00
|
|
|
}
|
2009-04-19 12:46:21 +08:00
|
|
|
}
|
|
|
|
|
2014-09-22 10:21:54 +08:00
|
|
|
void JumpScopeChecker::CheckGotoStmt(GotoStmt *GS) {
|
|
|
|
if (GS->getLabel()->isMSAsmLabel()) {
|
|
|
|
S.Diag(GS->getGotoLoc(), diag::err_goto_ms_asm_label)
|
|
|
|
<< GS->getLabel()->getIdentifier();
|
|
|
|
S.Diag(GS->getLabel()->getLocation(), diag::note_goto_ms_asm_label)
|
|
|
|
<< GS->getLabel()->getIdentifier();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-16 07:49:19 +08:00
|
|
|
void JumpScopeChecker::VerifyMustTailStmts() {
|
|
|
|
for (AttributedStmt *AS : MustTailStmts) {
|
|
|
|
for (unsigned I = LabelAndGotoScopes[AS]; I; I = Scopes[I].ParentScope) {
|
|
|
|
if (Scopes[I].OutDiag) {
|
|
|
|
S.Diag(AS->getBeginLoc(), diag::err_musttail_scope);
|
|
|
|
S.Diag(Scopes[I].Loc, Scopes[I].OutDiag);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const Attr *JumpScopeChecker::GetMustTailAttr(AttributedStmt *AS) {
|
|
|
|
ArrayRef<const Attr *> Attrs = AS->getAttrs();
|
|
|
|
const auto *Iter =
|
|
|
|
llvm::find_if(Attrs, [](const Attr *A) { return isa<MustTailAttr>(A); });
|
|
|
|
return Iter != Attrs.end() ? *Iter : nullptr;
|
|
|
|
}
|
|
|
|
|
2009-04-19 12:46:21 +08:00
|
|
|
void Sema::DiagnoseInvalidJumps(Stmt *Body) {
|
2009-11-17 14:14:37 +08:00
|
|
|
(void)JumpScopeChecker(Body, *this);
|
2009-04-19 12:46:21 +08:00
|
|
|
}
|