17330f729Sjoerg //===--- JumpDiagnostics.cpp - Protected scope jump analysis ------*- C++ -*-=//
27330f729Sjoerg //
37330f729Sjoerg // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
47330f729Sjoerg // See https://llvm.org/LICENSE.txt for license information.
57330f729Sjoerg // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
67330f729Sjoerg //
77330f729Sjoerg //===----------------------------------------------------------------------===//
87330f729Sjoerg //
97330f729Sjoerg // This file implements the JumpScopeChecker class, which is used to diagnose
107330f729Sjoerg // jumps that enter a protected scope in an invalid way.
117330f729Sjoerg //
127330f729Sjoerg //===----------------------------------------------------------------------===//
137330f729Sjoerg
147330f729Sjoerg #include "clang/AST/DeclCXX.h"
157330f729Sjoerg #include "clang/AST/Expr.h"
167330f729Sjoerg #include "clang/AST/ExprCXX.h"
177330f729Sjoerg #include "clang/AST/StmtCXX.h"
187330f729Sjoerg #include "clang/AST/StmtObjC.h"
19*e038c9c4Sjoerg #include "clang/AST/StmtOpenMP.h"
20*e038c9c4Sjoerg #include "clang/Basic/SourceLocation.h"
21*e038c9c4Sjoerg #include "clang/Sema/SemaInternal.h"
227330f729Sjoerg #include "llvm/ADT/BitVector.h"
237330f729Sjoerg using namespace clang;
247330f729Sjoerg
257330f729Sjoerg namespace {
267330f729Sjoerg
277330f729Sjoerg /// JumpScopeChecker - This object is used by Sema to diagnose invalid jumps
287330f729Sjoerg /// into VLA and other protected scopes. For example, this rejects:
297330f729Sjoerg /// goto L;
307330f729Sjoerg /// int a[n];
317330f729Sjoerg /// L:
327330f729Sjoerg ///
33*e038c9c4Sjoerg /// We also detect jumps out of protected scopes when it's not possible to do
34*e038c9c4Sjoerg /// cleanups properly. Indirect jumps and ASM jumps can't do cleanups because
35*e038c9c4Sjoerg /// the target is unknown. Return statements with \c [[clang::musttail]] cannot
36*e038c9c4Sjoerg /// handle any cleanups due to the nature of a tail call.
377330f729Sjoerg class JumpScopeChecker {
387330f729Sjoerg Sema &S;
397330f729Sjoerg
407330f729Sjoerg /// Permissive - True when recovering from errors, in which case precautions
417330f729Sjoerg /// are taken to handle incomplete scope information.
427330f729Sjoerg const bool Permissive;
437330f729Sjoerg
447330f729Sjoerg /// GotoScope - This is a record that we use to keep track of all of the
457330f729Sjoerg /// scopes that are introduced by VLAs and other things that scope jumps like
467330f729Sjoerg /// gotos. This scope tree has nothing to do with the source scope tree,
477330f729Sjoerg /// because you can have multiple VLA scopes per compound statement, and most
487330f729Sjoerg /// compound statements don't introduce any scopes.
497330f729Sjoerg struct GotoScope {
507330f729Sjoerg /// ParentScope - The index in ScopeMap of the parent scope. This is 0 for
517330f729Sjoerg /// the parent scope is the function body.
527330f729Sjoerg unsigned ParentScope;
537330f729Sjoerg
547330f729Sjoerg /// InDiag - The note to emit if there is a jump into this scope.
557330f729Sjoerg unsigned InDiag;
567330f729Sjoerg
577330f729Sjoerg /// OutDiag - The note to emit if there is an indirect jump out
587330f729Sjoerg /// of this scope. Direct jumps always clean up their current scope
597330f729Sjoerg /// in an orderly way.
607330f729Sjoerg unsigned OutDiag;
617330f729Sjoerg
627330f729Sjoerg /// Loc - Location to emit the diagnostic.
637330f729Sjoerg SourceLocation Loc;
647330f729Sjoerg
GotoScope__anond6565a0e0111::JumpScopeChecker::GotoScope657330f729Sjoerg GotoScope(unsigned parentScope, unsigned InDiag, unsigned OutDiag,
667330f729Sjoerg SourceLocation L)
677330f729Sjoerg : ParentScope(parentScope), InDiag(InDiag), OutDiag(OutDiag), Loc(L) {}
687330f729Sjoerg };
697330f729Sjoerg
707330f729Sjoerg SmallVector<GotoScope, 48> Scopes;
717330f729Sjoerg llvm::DenseMap<Stmt*, unsigned> LabelAndGotoScopes;
727330f729Sjoerg SmallVector<Stmt*, 16> Jumps;
737330f729Sjoerg
747330f729Sjoerg SmallVector<Stmt*, 4> IndirectJumps;
757330f729Sjoerg SmallVector<Stmt*, 4> AsmJumps;
76*e038c9c4Sjoerg SmallVector<AttributedStmt *, 4> MustTailStmts;
777330f729Sjoerg SmallVector<LabelDecl*, 4> IndirectJumpTargets;
787330f729Sjoerg SmallVector<LabelDecl*, 4> AsmJumpTargets;
797330f729Sjoerg public:
807330f729Sjoerg JumpScopeChecker(Stmt *Body, Sema &S);
817330f729Sjoerg private:
827330f729Sjoerg void BuildScopeInformation(Decl *D, unsigned &ParentScope);
837330f729Sjoerg void BuildScopeInformation(VarDecl *D, const BlockDecl *BDecl,
847330f729Sjoerg unsigned &ParentScope);
85*e038c9c4Sjoerg void BuildScopeInformation(CompoundLiteralExpr *CLE, unsigned &ParentScope);
867330f729Sjoerg void BuildScopeInformation(Stmt *S, unsigned &origParentScope);
877330f729Sjoerg
887330f729Sjoerg void VerifyJumps();
897330f729Sjoerg void VerifyIndirectOrAsmJumps(bool IsAsmGoto);
90*e038c9c4Sjoerg void VerifyMustTailStmts();
917330f729Sjoerg void NoteJumpIntoScopes(ArrayRef<unsigned> ToScopes);
927330f729Sjoerg void DiagnoseIndirectOrAsmJump(Stmt *IG, unsigned IGScope, LabelDecl *Target,
937330f729Sjoerg unsigned TargetScope);
947330f729Sjoerg void CheckJump(Stmt *From, Stmt *To, SourceLocation DiagLoc,
957330f729Sjoerg unsigned JumpDiag, unsigned JumpDiagWarning,
967330f729Sjoerg unsigned JumpDiagCXX98Compat);
977330f729Sjoerg void CheckGotoStmt(GotoStmt *GS);
98*e038c9c4Sjoerg const Attr *GetMustTailAttr(AttributedStmt *AS);
997330f729Sjoerg
1007330f729Sjoerg unsigned GetDeepestCommonScope(unsigned A, unsigned B);
1017330f729Sjoerg };
1027330f729Sjoerg } // end anonymous namespace
1037330f729Sjoerg
1047330f729Sjoerg #define CHECK_PERMISSIVE(x) (assert(Permissive || !(x)), (Permissive && (x)))
1057330f729Sjoerg
JumpScopeChecker(Stmt * Body,Sema & s)1067330f729Sjoerg JumpScopeChecker::JumpScopeChecker(Stmt *Body, Sema &s)
1077330f729Sjoerg : S(s), Permissive(s.hasAnyUnrecoverableErrorsInThisFunction()) {
1087330f729Sjoerg // Add a scope entry for function scope.
1097330f729Sjoerg Scopes.push_back(GotoScope(~0U, ~0U, ~0U, SourceLocation()));
1107330f729Sjoerg
1117330f729Sjoerg // Build information for the top level compound statement, so that we have a
1127330f729Sjoerg // defined scope record for every "goto" and label.
1137330f729Sjoerg unsigned BodyParentScope = 0;
1147330f729Sjoerg BuildScopeInformation(Body, BodyParentScope);
1157330f729Sjoerg
1167330f729Sjoerg // Check that all jumps we saw are kosher.
1177330f729Sjoerg VerifyJumps();
1187330f729Sjoerg VerifyIndirectOrAsmJumps(false);
1197330f729Sjoerg VerifyIndirectOrAsmJumps(true);
120*e038c9c4Sjoerg VerifyMustTailStmts();
1217330f729Sjoerg }
1227330f729Sjoerg
1237330f729Sjoerg /// GetDeepestCommonScope - Finds the innermost scope enclosing the
1247330f729Sjoerg /// two scopes.
GetDeepestCommonScope(unsigned A,unsigned B)1257330f729Sjoerg unsigned JumpScopeChecker::GetDeepestCommonScope(unsigned A, unsigned B) {
1267330f729Sjoerg while (A != B) {
1277330f729Sjoerg // Inner scopes are created after outer scopes and therefore have
1287330f729Sjoerg // higher indices.
1297330f729Sjoerg if (A < B) {
1307330f729Sjoerg assert(Scopes[B].ParentScope < B);
1317330f729Sjoerg B = Scopes[B].ParentScope;
1327330f729Sjoerg } else {
1337330f729Sjoerg assert(Scopes[A].ParentScope < A);
1347330f729Sjoerg A = Scopes[A].ParentScope;
1357330f729Sjoerg }
1367330f729Sjoerg }
1377330f729Sjoerg return A;
1387330f729Sjoerg }
1397330f729Sjoerg
1407330f729Sjoerg typedef std::pair<unsigned,unsigned> ScopePair;
1417330f729Sjoerg
1427330f729Sjoerg /// GetDiagForGotoScopeDecl - If this decl induces a new goto scope, return a
1437330f729Sjoerg /// diagnostic that should be emitted if control goes over it. If not, return 0.
GetDiagForGotoScopeDecl(Sema & S,const Decl * D)1447330f729Sjoerg static ScopePair GetDiagForGotoScopeDecl(Sema &S, const Decl *D) {
1457330f729Sjoerg if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
1467330f729Sjoerg unsigned InDiag = 0;
1477330f729Sjoerg unsigned OutDiag = 0;
1487330f729Sjoerg
1497330f729Sjoerg if (VD->getType()->isVariablyModifiedType())
1507330f729Sjoerg InDiag = diag::note_protected_by_vla;
1517330f729Sjoerg
1527330f729Sjoerg if (VD->hasAttr<BlocksAttr>())
1537330f729Sjoerg return ScopePair(diag::note_protected_by___block,
1547330f729Sjoerg diag::note_exits___block);
1557330f729Sjoerg
1567330f729Sjoerg if (VD->hasAttr<CleanupAttr>())
1577330f729Sjoerg return ScopePair(diag::note_protected_by_cleanup,
1587330f729Sjoerg diag::note_exits_cleanup);
1597330f729Sjoerg
1607330f729Sjoerg if (VD->hasLocalStorage()) {
1617330f729Sjoerg switch (VD->getType().isDestructedType()) {
1627330f729Sjoerg case QualType::DK_objc_strong_lifetime:
1637330f729Sjoerg return ScopePair(diag::note_protected_by_objc_strong_init,
1647330f729Sjoerg diag::note_exits_objc_strong);
1657330f729Sjoerg
1667330f729Sjoerg case QualType::DK_objc_weak_lifetime:
1677330f729Sjoerg return ScopePair(diag::note_protected_by_objc_weak_init,
1687330f729Sjoerg diag::note_exits_objc_weak);
1697330f729Sjoerg
1707330f729Sjoerg case QualType::DK_nontrivial_c_struct:
1717330f729Sjoerg return ScopePair(diag::note_protected_by_non_trivial_c_struct_init,
1727330f729Sjoerg diag::note_exits_dtor);
1737330f729Sjoerg
1747330f729Sjoerg case QualType::DK_cxx_destructor:
1757330f729Sjoerg OutDiag = diag::note_exits_dtor;
1767330f729Sjoerg break;
1777330f729Sjoerg
1787330f729Sjoerg case QualType::DK_none:
1797330f729Sjoerg break;
1807330f729Sjoerg }
1817330f729Sjoerg }
1827330f729Sjoerg
1837330f729Sjoerg const Expr *Init = VD->getInit();
1847330f729Sjoerg if (S.Context.getLangOpts().CPlusPlus && VD->hasLocalStorage() && Init) {
1857330f729Sjoerg // C++11 [stmt.dcl]p3:
1867330f729Sjoerg // A program that jumps from a point where a variable with automatic
1877330f729Sjoerg // storage duration is not in scope to a point where it is in scope
1887330f729Sjoerg // is ill-formed unless the variable has scalar type, class type with
1897330f729Sjoerg // a trivial default constructor and a trivial destructor, a
1907330f729Sjoerg // cv-qualified version of one of these types, or an array of one of
1917330f729Sjoerg // the preceding types and is declared without an initializer.
1927330f729Sjoerg
1937330f729Sjoerg // C++03 [stmt.dcl.p3:
1947330f729Sjoerg // A program that jumps from a point where a local variable
1957330f729Sjoerg // with automatic storage duration is not in scope to a point
1967330f729Sjoerg // where it is in scope is ill-formed unless the variable has
1977330f729Sjoerg // POD type and is declared without an initializer.
1987330f729Sjoerg
1997330f729Sjoerg InDiag = diag::note_protected_by_variable_init;
2007330f729Sjoerg
2017330f729Sjoerg // For a variable of (array of) class type declared without an
2027330f729Sjoerg // initializer, we will have call-style initialization and the initializer
2037330f729Sjoerg // will be the CXXConstructExpr with no intervening nodes.
2047330f729Sjoerg if (const CXXConstructExpr *CCE = dyn_cast<CXXConstructExpr>(Init)) {
2057330f729Sjoerg const CXXConstructorDecl *Ctor = CCE->getConstructor();
2067330f729Sjoerg if (Ctor->isTrivial() && Ctor->isDefaultConstructor() &&
2077330f729Sjoerg VD->getInitStyle() == VarDecl::CallInit) {
2087330f729Sjoerg if (OutDiag)
2097330f729Sjoerg InDiag = diag::note_protected_by_variable_nontriv_destructor;
2107330f729Sjoerg else if (!Ctor->getParent()->isPOD())
2117330f729Sjoerg InDiag = diag::note_protected_by_variable_non_pod;
2127330f729Sjoerg else
2137330f729Sjoerg InDiag = 0;
2147330f729Sjoerg }
2157330f729Sjoerg }
2167330f729Sjoerg }
2177330f729Sjoerg
2187330f729Sjoerg return ScopePair(InDiag, OutDiag);
2197330f729Sjoerg }
2207330f729Sjoerg
2217330f729Sjoerg if (const TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) {
2227330f729Sjoerg if (TD->getUnderlyingType()->isVariablyModifiedType())
2237330f729Sjoerg return ScopePair(isa<TypedefDecl>(TD)
2247330f729Sjoerg ? diag::note_protected_by_vla_typedef
2257330f729Sjoerg : diag::note_protected_by_vla_type_alias,
2267330f729Sjoerg 0);
2277330f729Sjoerg }
2287330f729Sjoerg
2297330f729Sjoerg return ScopePair(0U, 0U);
2307330f729Sjoerg }
2317330f729Sjoerg
2327330f729Sjoerg /// Build scope information for a declaration that is part of a DeclStmt.
BuildScopeInformation(Decl * D,unsigned & ParentScope)2337330f729Sjoerg void JumpScopeChecker::BuildScopeInformation(Decl *D, unsigned &ParentScope) {
2347330f729Sjoerg // If this decl causes a new scope, push and switch to it.
2357330f729Sjoerg std::pair<unsigned,unsigned> Diags = GetDiagForGotoScopeDecl(S, D);
2367330f729Sjoerg if (Diags.first || Diags.second) {
2377330f729Sjoerg Scopes.push_back(GotoScope(ParentScope, Diags.first, Diags.second,
2387330f729Sjoerg D->getLocation()));
2397330f729Sjoerg ParentScope = Scopes.size()-1;
2407330f729Sjoerg }
2417330f729Sjoerg
2427330f729Sjoerg // If the decl has an initializer, walk it with the potentially new
2437330f729Sjoerg // scope we just installed.
2447330f729Sjoerg if (VarDecl *VD = dyn_cast<VarDecl>(D))
2457330f729Sjoerg if (Expr *Init = VD->getInit())
2467330f729Sjoerg BuildScopeInformation(Init, ParentScope);
2477330f729Sjoerg }
2487330f729Sjoerg
2497330f729Sjoerg /// Build scope information for a captured block literal variables.
BuildScopeInformation(VarDecl * D,const BlockDecl * BDecl,unsigned & ParentScope)2507330f729Sjoerg void JumpScopeChecker::BuildScopeInformation(VarDecl *D,
2517330f729Sjoerg const BlockDecl *BDecl,
2527330f729Sjoerg unsigned &ParentScope) {
2537330f729Sjoerg // exclude captured __block variables; there's no destructor
2547330f729Sjoerg // associated with the block literal for them.
2557330f729Sjoerg if (D->hasAttr<BlocksAttr>())
2567330f729Sjoerg return;
2577330f729Sjoerg QualType T = D->getType();
2587330f729Sjoerg QualType::DestructionKind destructKind = T.isDestructedType();
2597330f729Sjoerg if (destructKind != QualType::DK_none) {
2607330f729Sjoerg std::pair<unsigned,unsigned> Diags;
2617330f729Sjoerg switch (destructKind) {
2627330f729Sjoerg case QualType::DK_cxx_destructor:
2637330f729Sjoerg Diags = ScopePair(diag::note_enters_block_captures_cxx_obj,
2647330f729Sjoerg diag::note_exits_block_captures_cxx_obj);
2657330f729Sjoerg break;
2667330f729Sjoerg case QualType::DK_objc_strong_lifetime:
2677330f729Sjoerg Diags = ScopePair(diag::note_enters_block_captures_strong,
2687330f729Sjoerg diag::note_exits_block_captures_strong);
2697330f729Sjoerg break;
2707330f729Sjoerg case QualType::DK_objc_weak_lifetime:
2717330f729Sjoerg Diags = ScopePair(diag::note_enters_block_captures_weak,
2727330f729Sjoerg diag::note_exits_block_captures_weak);
2737330f729Sjoerg break;
2747330f729Sjoerg case QualType::DK_nontrivial_c_struct:
2757330f729Sjoerg Diags = ScopePair(diag::note_enters_block_captures_non_trivial_c_struct,
2767330f729Sjoerg diag::note_exits_block_captures_non_trivial_c_struct);
2777330f729Sjoerg break;
2787330f729Sjoerg case QualType::DK_none:
2797330f729Sjoerg llvm_unreachable("non-lifetime captured variable");
2807330f729Sjoerg }
2817330f729Sjoerg SourceLocation Loc = D->getLocation();
2827330f729Sjoerg if (Loc.isInvalid())
2837330f729Sjoerg Loc = BDecl->getLocation();
2847330f729Sjoerg Scopes.push_back(GotoScope(ParentScope,
2857330f729Sjoerg Diags.first, Diags.second, Loc));
2867330f729Sjoerg ParentScope = Scopes.size()-1;
2877330f729Sjoerg }
2887330f729Sjoerg }
2897330f729Sjoerg
290*e038c9c4Sjoerg /// Build scope information for compound literals of C struct types that are
291*e038c9c4Sjoerg /// non-trivial to destruct.
BuildScopeInformation(CompoundLiteralExpr * CLE,unsigned & ParentScope)292*e038c9c4Sjoerg void JumpScopeChecker::BuildScopeInformation(CompoundLiteralExpr *CLE,
293*e038c9c4Sjoerg unsigned &ParentScope) {
294*e038c9c4Sjoerg unsigned InDiag = diag::note_enters_compound_literal_scope;
295*e038c9c4Sjoerg unsigned OutDiag = diag::note_exits_compound_literal_scope;
296*e038c9c4Sjoerg Scopes.push_back(GotoScope(ParentScope, InDiag, OutDiag, CLE->getExprLoc()));
297*e038c9c4Sjoerg ParentScope = Scopes.size() - 1;
298*e038c9c4Sjoerg }
299*e038c9c4Sjoerg
3007330f729Sjoerg /// BuildScopeInformation - The statements from CI to CE are known to form a
3017330f729Sjoerg /// coherent VLA scope with a specified parent node. Walk through the
3027330f729Sjoerg /// statements, adding any labels or gotos to LabelAndGotoScopes and recursively
3037330f729Sjoerg /// walking the AST as needed.
BuildScopeInformation(Stmt * S,unsigned & origParentScope)3047330f729Sjoerg void JumpScopeChecker::BuildScopeInformation(Stmt *S,
3057330f729Sjoerg unsigned &origParentScope) {
3067330f729Sjoerg // If this is a statement, rather than an expression, scopes within it don't
3077330f729Sjoerg // propagate out into the enclosing scope. Otherwise we have to worry
3087330f729Sjoerg // about block literals, which have the lifetime of their enclosing statement.
3097330f729Sjoerg unsigned independentParentScope = origParentScope;
3107330f729Sjoerg unsigned &ParentScope = ((isa<Expr>(S) && !isa<StmtExpr>(S))
3117330f729Sjoerg ? origParentScope : independentParentScope);
3127330f729Sjoerg
3137330f729Sjoerg unsigned StmtsToSkip = 0u;
3147330f729Sjoerg
3157330f729Sjoerg // If we found a label, remember that it is in ParentScope scope.
3167330f729Sjoerg switch (S->getStmtClass()) {
3177330f729Sjoerg case Stmt::AddrLabelExprClass:
3187330f729Sjoerg IndirectJumpTargets.push_back(cast<AddrLabelExpr>(S)->getLabel());
3197330f729Sjoerg break;
3207330f729Sjoerg
3217330f729Sjoerg case Stmt::ObjCForCollectionStmtClass: {
3227330f729Sjoerg auto *CS = cast<ObjCForCollectionStmt>(S);
3237330f729Sjoerg unsigned Diag = diag::note_protected_by_objc_fast_enumeration;
3247330f729Sjoerg unsigned NewParentScope = Scopes.size();
3257330f729Sjoerg Scopes.push_back(GotoScope(ParentScope, Diag, 0, S->getBeginLoc()));
3267330f729Sjoerg BuildScopeInformation(CS->getBody(), NewParentScope);
3277330f729Sjoerg return;
3287330f729Sjoerg }
3297330f729Sjoerg
3307330f729Sjoerg case Stmt::IndirectGotoStmtClass:
3317330f729Sjoerg // "goto *&&lbl;" is a special case which we treat as equivalent
3327330f729Sjoerg // to a normal goto. In addition, we don't calculate scope in the
3337330f729Sjoerg // operand (to avoid recording the address-of-label use), which
3347330f729Sjoerg // works only because of the restricted set of expressions which
3357330f729Sjoerg // we detect as constant targets.
3367330f729Sjoerg if (cast<IndirectGotoStmt>(S)->getConstantTarget()) {
3377330f729Sjoerg LabelAndGotoScopes[S] = ParentScope;
3387330f729Sjoerg Jumps.push_back(S);
3397330f729Sjoerg return;
3407330f729Sjoerg }
3417330f729Sjoerg
3427330f729Sjoerg LabelAndGotoScopes[S] = ParentScope;
3437330f729Sjoerg IndirectJumps.push_back(S);
3447330f729Sjoerg break;
3457330f729Sjoerg
3467330f729Sjoerg case Stmt::SwitchStmtClass:
3477330f729Sjoerg // Evaluate the C++17 init stmt and condition variable
3487330f729Sjoerg // before entering the scope of the switch statement.
3497330f729Sjoerg if (Stmt *Init = cast<SwitchStmt>(S)->getInit()) {
3507330f729Sjoerg BuildScopeInformation(Init, ParentScope);
3517330f729Sjoerg ++StmtsToSkip;
3527330f729Sjoerg }
3537330f729Sjoerg if (VarDecl *Var = cast<SwitchStmt>(S)->getConditionVariable()) {
3547330f729Sjoerg BuildScopeInformation(Var, ParentScope);
3557330f729Sjoerg ++StmtsToSkip;
3567330f729Sjoerg }
3577330f729Sjoerg LLVM_FALLTHROUGH;
3587330f729Sjoerg
3597330f729Sjoerg case Stmt::GotoStmtClass:
3607330f729Sjoerg // Remember both what scope a goto is in as well as the fact that we have
3617330f729Sjoerg // it. This makes the second scan not have to walk the AST again.
3627330f729Sjoerg LabelAndGotoScopes[S] = ParentScope;
3637330f729Sjoerg Jumps.push_back(S);
3647330f729Sjoerg break;
3657330f729Sjoerg
3667330f729Sjoerg case Stmt::GCCAsmStmtClass:
3677330f729Sjoerg if (auto *GS = dyn_cast<GCCAsmStmt>(S))
3687330f729Sjoerg if (GS->isAsmGoto()) {
3697330f729Sjoerg // Remember both what scope a goto is in as well as the fact that we
3707330f729Sjoerg // have it. This makes the second scan not have to walk the AST again.
3717330f729Sjoerg LabelAndGotoScopes[S] = ParentScope;
3727330f729Sjoerg AsmJumps.push_back(GS);
3737330f729Sjoerg for (auto *E : GS->labels())
3747330f729Sjoerg AsmJumpTargets.push_back(E->getLabel());
3757330f729Sjoerg }
3767330f729Sjoerg break;
3777330f729Sjoerg
3787330f729Sjoerg case Stmt::IfStmtClass: {
3797330f729Sjoerg IfStmt *IS = cast<IfStmt>(S);
3807330f729Sjoerg if (!(IS->isConstexpr() || IS->isObjCAvailabilityCheck()))
3817330f729Sjoerg break;
3827330f729Sjoerg
3837330f729Sjoerg unsigned Diag = IS->isConstexpr() ? diag::note_protected_by_constexpr_if
3847330f729Sjoerg : diag::note_protected_by_if_available;
3857330f729Sjoerg
3867330f729Sjoerg if (VarDecl *Var = IS->getConditionVariable())
3877330f729Sjoerg BuildScopeInformation(Var, ParentScope);
3887330f729Sjoerg
3897330f729Sjoerg // Cannot jump into the middle of the condition.
3907330f729Sjoerg unsigned NewParentScope = Scopes.size();
3917330f729Sjoerg Scopes.push_back(GotoScope(ParentScope, Diag, 0, IS->getBeginLoc()));
3927330f729Sjoerg BuildScopeInformation(IS->getCond(), NewParentScope);
3937330f729Sjoerg
3947330f729Sjoerg // Jumps into either arm of an 'if constexpr' are not allowed.
3957330f729Sjoerg NewParentScope = Scopes.size();
3967330f729Sjoerg Scopes.push_back(GotoScope(ParentScope, Diag, 0, IS->getBeginLoc()));
3977330f729Sjoerg BuildScopeInformation(IS->getThen(), NewParentScope);
3987330f729Sjoerg if (Stmt *Else = IS->getElse()) {
3997330f729Sjoerg NewParentScope = Scopes.size();
4007330f729Sjoerg Scopes.push_back(GotoScope(ParentScope, Diag, 0, IS->getBeginLoc()));
4017330f729Sjoerg BuildScopeInformation(Else, NewParentScope);
4027330f729Sjoerg }
4037330f729Sjoerg return;
4047330f729Sjoerg }
4057330f729Sjoerg
4067330f729Sjoerg case Stmt::CXXTryStmtClass: {
4077330f729Sjoerg CXXTryStmt *TS = cast<CXXTryStmt>(S);
4087330f729Sjoerg {
4097330f729Sjoerg unsigned NewParentScope = Scopes.size();
4107330f729Sjoerg Scopes.push_back(GotoScope(ParentScope,
4117330f729Sjoerg diag::note_protected_by_cxx_try,
4127330f729Sjoerg diag::note_exits_cxx_try,
4137330f729Sjoerg TS->getSourceRange().getBegin()));
4147330f729Sjoerg if (Stmt *TryBlock = TS->getTryBlock())
4157330f729Sjoerg BuildScopeInformation(TryBlock, NewParentScope);
4167330f729Sjoerg }
4177330f729Sjoerg
4187330f729Sjoerg // Jump from the catch into the try is not allowed either.
4197330f729Sjoerg for (unsigned I = 0, E = TS->getNumHandlers(); I != E; ++I) {
4207330f729Sjoerg CXXCatchStmt *CS = TS->getHandler(I);
4217330f729Sjoerg unsigned NewParentScope = Scopes.size();
4227330f729Sjoerg Scopes.push_back(GotoScope(ParentScope,
4237330f729Sjoerg diag::note_protected_by_cxx_catch,
4247330f729Sjoerg diag::note_exits_cxx_catch,
4257330f729Sjoerg CS->getSourceRange().getBegin()));
4267330f729Sjoerg BuildScopeInformation(CS->getHandlerBlock(), NewParentScope);
4277330f729Sjoerg }
4287330f729Sjoerg return;
4297330f729Sjoerg }
4307330f729Sjoerg
4317330f729Sjoerg case Stmt::SEHTryStmtClass: {
4327330f729Sjoerg SEHTryStmt *TS = cast<SEHTryStmt>(S);
4337330f729Sjoerg {
4347330f729Sjoerg unsigned NewParentScope = Scopes.size();
4357330f729Sjoerg Scopes.push_back(GotoScope(ParentScope,
4367330f729Sjoerg diag::note_protected_by_seh_try,
4377330f729Sjoerg diag::note_exits_seh_try,
4387330f729Sjoerg TS->getSourceRange().getBegin()));
4397330f729Sjoerg if (Stmt *TryBlock = TS->getTryBlock())
4407330f729Sjoerg BuildScopeInformation(TryBlock, NewParentScope);
4417330f729Sjoerg }
4427330f729Sjoerg
4437330f729Sjoerg // Jump from __except or __finally into the __try are not allowed either.
4447330f729Sjoerg if (SEHExceptStmt *Except = TS->getExceptHandler()) {
4457330f729Sjoerg unsigned NewParentScope = Scopes.size();
4467330f729Sjoerg Scopes.push_back(GotoScope(ParentScope,
4477330f729Sjoerg diag::note_protected_by_seh_except,
4487330f729Sjoerg diag::note_exits_seh_except,
4497330f729Sjoerg Except->getSourceRange().getBegin()));
4507330f729Sjoerg BuildScopeInformation(Except->getBlock(), NewParentScope);
4517330f729Sjoerg } else if (SEHFinallyStmt *Finally = TS->getFinallyHandler()) {
4527330f729Sjoerg unsigned NewParentScope = Scopes.size();
4537330f729Sjoerg Scopes.push_back(GotoScope(ParentScope,
4547330f729Sjoerg diag::note_protected_by_seh_finally,
4557330f729Sjoerg diag::note_exits_seh_finally,
4567330f729Sjoerg Finally->getSourceRange().getBegin()));
4577330f729Sjoerg BuildScopeInformation(Finally->getBlock(), NewParentScope);
4587330f729Sjoerg }
4597330f729Sjoerg
4607330f729Sjoerg return;
4617330f729Sjoerg }
4627330f729Sjoerg
4637330f729Sjoerg case Stmt::DeclStmtClass: {
4647330f729Sjoerg // If this is a declstmt with a VLA definition, it defines a scope from here
4657330f729Sjoerg // to the end of the containing context.
4667330f729Sjoerg DeclStmt *DS = cast<DeclStmt>(S);
4677330f729Sjoerg // The decl statement creates a scope if any of the decls in it are VLAs
4687330f729Sjoerg // or have the cleanup attribute.
4697330f729Sjoerg for (auto *I : DS->decls())
4707330f729Sjoerg BuildScopeInformation(I, origParentScope);
4717330f729Sjoerg return;
4727330f729Sjoerg }
4737330f729Sjoerg
4747330f729Sjoerg case Stmt::ObjCAtTryStmtClass: {
4757330f729Sjoerg // Disallow jumps into any part of an @try statement by pushing a scope and
4767330f729Sjoerg // walking all sub-stmts in that scope.
4777330f729Sjoerg ObjCAtTryStmt *AT = cast<ObjCAtTryStmt>(S);
4787330f729Sjoerg // Recursively walk the AST for the @try part.
4797330f729Sjoerg {
4807330f729Sjoerg unsigned NewParentScope = Scopes.size();
4817330f729Sjoerg Scopes.push_back(GotoScope(ParentScope,
4827330f729Sjoerg diag::note_protected_by_objc_try,
4837330f729Sjoerg diag::note_exits_objc_try,
4847330f729Sjoerg AT->getAtTryLoc()));
4857330f729Sjoerg if (Stmt *TryPart = AT->getTryBody())
4867330f729Sjoerg BuildScopeInformation(TryPart, NewParentScope);
4877330f729Sjoerg }
4887330f729Sjoerg
4897330f729Sjoerg // Jump from the catch to the finally or try is not valid.
4907330f729Sjoerg for (unsigned I = 0, N = AT->getNumCatchStmts(); I != N; ++I) {
4917330f729Sjoerg ObjCAtCatchStmt *AC = AT->getCatchStmt(I);
4927330f729Sjoerg unsigned NewParentScope = Scopes.size();
4937330f729Sjoerg Scopes.push_back(GotoScope(ParentScope,
4947330f729Sjoerg diag::note_protected_by_objc_catch,
4957330f729Sjoerg diag::note_exits_objc_catch,
4967330f729Sjoerg AC->getAtCatchLoc()));
4977330f729Sjoerg // @catches are nested and it isn't
4987330f729Sjoerg BuildScopeInformation(AC->getCatchBody(), NewParentScope);
4997330f729Sjoerg }
5007330f729Sjoerg
5017330f729Sjoerg // Jump from the finally to the try or catch is not valid.
5027330f729Sjoerg if (ObjCAtFinallyStmt *AF = AT->getFinallyStmt()) {
5037330f729Sjoerg unsigned NewParentScope = Scopes.size();
5047330f729Sjoerg Scopes.push_back(GotoScope(ParentScope,
5057330f729Sjoerg diag::note_protected_by_objc_finally,
5067330f729Sjoerg diag::note_exits_objc_finally,
5077330f729Sjoerg AF->getAtFinallyLoc()));
5087330f729Sjoerg BuildScopeInformation(AF, NewParentScope);
5097330f729Sjoerg }
5107330f729Sjoerg
5117330f729Sjoerg return;
5127330f729Sjoerg }
5137330f729Sjoerg
5147330f729Sjoerg case Stmt::ObjCAtSynchronizedStmtClass: {
5157330f729Sjoerg // Disallow jumps into the protected statement of an @synchronized, but
5167330f729Sjoerg // allow jumps into the object expression it protects.
5177330f729Sjoerg ObjCAtSynchronizedStmt *AS = cast<ObjCAtSynchronizedStmt>(S);
5187330f729Sjoerg // Recursively walk the AST for the @synchronized object expr, it is
5197330f729Sjoerg // evaluated in the normal scope.
5207330f729Sjoerg BuildScopeInformation(AS->getSynchExpr(), ParentScope);
5217330f729Sjoerg
5227330f729Sjoerg // Recursively walk the AST for the @synchronized part, protected by a new
5237330f729Sjoerg // scope.
5247330f729Sjoerg unsigned NewParentScope = Scopes.size();
5257330f729Sjoerg Scopes.push_back(GotoScope(ParentScope,
5267330f729Sjoerg diag::note_protected_by_objc_synchronized,
5277330f729Sjoerg diag::note_exits_objc_synchronized,
5287330f729Sjoerg AS->getAtSynchronizedLoc()));
5297330f729Sjoerg BuildScopeInformation(AS->getSynchBody(), NewParentScope);
5307330f729Sjoerg return;
5317330f729Sjoerg }
5327330f729Sjoerg
5337330f729Sjoerg case Stmt::ObjCAutoreleasePoolStmtClass: {
5347330f729Sjoerg // Disallow jumps into the protected statement of an @autoreleasepool.
5357330f729Sjoerg ObjCAutoreleasePoolStmt *AS = cast<ObjCAutoreleasePoolStmt>(S);
5367330f729Sjoerg // Recursively walk the AST for the @autoreleasepool part, protected by a
5377330f729Sjoerg // new scope.
5387330f729Sjoerg unsigned NewParentScope = Scopes.size();
5397330f729Sjoerg Scopes.push_back(GotoScope(ParentScope,
5407330f729Sjoerg diag::note_protected_by_objc_autoreleasepool,
5417330f729Sjoerg diag::note_exits_objc_autoreleasepool,
5427330f729Sjoerg AS->getAtLoc()));
5437330f729Sjoerg BuildScopeInformation(AS->getSubStmt(), NewParentScope);
5447330f729Sjoerg return;
5457330f729Sjoerg }
5467330f729Sjoerg
5477330f729Sjoerg case Stmt::ExprWithCleanupsClass: {
5487330f729Sjoerg // Disallow jumps past full-expressions that use blocks with
5497330f729Sjoerg // non-trivial cleanups of their captures. This is theoretically
5507330f729Sjoerg // implementable but a lot of work which we haven't felt up to doing.
5517330f729Sjoerg ExprWithCleanups *EWC = cast<ExprWithCleanups>(S);
5527330f729Sjoerg for (unsigned i = 0, e = EWC->getNumObjects(); i != e; ++i) {
553*e038c9c4Sjoerg if (auto *BDecl = EWC->getObject(i).dyn_cast<BlockDecl *>())
5547330f729Sjoerg for (const auto &CI : BDecl->captures()) {
5557330f729Sjoerg VarDecl *variable = CI.getVariable();
5567330f729Sjoerg BuildScopeInformation(variable, BDecl, origParentScope);
5577330f729Sjoerg }
558*e038c9c4Sjoerg else if (auto *CLE = EWC->getObject(i).dyn_cast<CompoundLiteralExpr *>())
559*e038c9c4Sjoerg BuildScopeInformation(CLE, origParentScope);
560*e038c9c4Sjoerg else
561*e038c9c4Sjoerg llvm_unreachable("unexpected cleanup object type");
5627330f729Sjoerg }
5637330f729Sjoerg break;
5647330f729Sjoerg }
5657330f729Sjoerg
5667330f729Sjoerg case Stmt::MaterializeTemporaryExprClass: {
5677330f729Sjoerg // Disallow jumps out of scopes containing temporaries lifetime-extended to
5687330f729Sjoerg // automatic storage duration.
5697330f729Sjoerg MaterializeTemporaryExpr *MTE = cast<MaterializeTemporaryExpr>(S);
5707330f729Sjoerg if (MTE->getStorageDuration() == SD_Automatic) {
5717330f729Sjoerg SmallVector<const Expr *, 4> CommaLHS;
5727330f729Sjoerg SmallVector<SubobjectAdjustment, 4> Adjustments;
5737330f729Sjoerg const Expr *ExtendedObject =
574*e038c9c4Sjoerg MTE->getSubExpr()->skipRValueSubobjectAdjustments(CommaLHS,
575*e038c9c4Sjoerg Adjustments);
5767330f729Sjoerg if (ExtendedObject->getType().isDestructedType()) {
5777330f729Sjoerg Scopes.push_back(GotoScope(ParentScope, 0,
5787330f729Sjoerg diag::note_exits_temporary_dtor,
5797330f729Sjoerg ExtendedObject->getExprLoc()));
5807330f729Sjoerg origParentScope = Scopes.size()-1;
5817330f729Sjoerg }
5827330f729Sjoerg }
5837330f729Sjoerg break;
5847330f729Sjoerg }
5857330f729Sjoerg
5867330f729Sjoerg case Stmt::CaseStmtClass:
5877330f729Sjoerg case Stmt::DefaultStmtClass:
5887330f729Sjoerg case Stmt::LabelStmtClass:
5897330f729Sjoerg LabelAndGotoScopes[S] = ParentScope;
5907330f729Sjoerg break;
5917330f729Sjoerg
592*e038c9c4Sjoerg case Stmt::AttributedStmtClass: {
593*e038c9c4Sjoerg AttributedStmt *AS = cast<AttributedStmt>(S);
594*e038c9c4Sjoerg if (GetMustTailAttr(AS)) {
595*e038c9c4Sjoerg LabelAndGotoScopes[AS] = ParentScope;
596*e038c9c4Sjoerg MustTailStmts.push_back(AS);
597*e038c9c4Sjoerg }
598*e038c9c4Sjoerg break;
599*e038c9c4Sjoerg }
600*e038c9c4Sjoerg
6017330f729Sjoerg default:
602*e038c9c4Sjoerg if (auto *ED = dyn_cast<OMPExecutableDirective>(S)) {
603*e038c9c4Sjoerg if (!ED->isStandaloneDirective()) {
604*e038c9c4Sjoerg unsigned NewParentScope = Scopes.size();
605*e038c9c4Sjoerg Scopes.emplace_back(ParentScope,
606*e038c9c4Sjoerg diag::note_omp_protected_structured_block,
607*e038c9c4Sjoerg diag::note_omp_exits_structured_block,
608*e038c9c4Sjoerg ED->getStructuredBlock()->getBeginLoc());
609*e038c9c4Sjoerg BuildScopeInformation(ED->getStructuredBlock(), NewParentScope);
610*e038c9c4Sjoerg return;
611*e038c9c4Sjoerg }
612*e038c9c4Sjoerg }
6137330f729Sjoerg break;
6147330f729Sjoerg }
6157330f729Sjoerg
6167330f729Sjoerg for (Stmt *SubStmt : S->children()) {
6177330f729Sjoerg if (!SubStmt)
6187330f729Sjoerg continue;
6197330f729Sjoerg if (StmtsToSkip) {
6207330f729Sjoerg --StmtsToSkip;
6217330f729Sjoerg continue;
6227330f729Sjoerg }
6237330f729Sjoerg
6247330f729Sjoerg // Cases, labels, and defaults aren't "scope parents". It's also
6257330f729Sjoerg // important to handle these iteratively instead of recursively in
6267330f729Sjoerg // order to avoid blowing out the stack.
6277330f729Sjoerg while (true) {
6287330f729Sjoerg Stmt *Next;
6297330f729Sjoerg if (SwitchCase *SC = dyn_cast<SwitchCase>(SubStmt))
6307330f729Sjoerg Next = SC->getSubStmt();
6317330f729Sjoerg else if (LabelStmt *LS = dyn_cast<LabelStmt>(SubStmt))
6327330f729Sjoerg Next = LS->getSubStmt();
6337330f729Sjoerg else
6347330f729Sjoerg break;
6357330f729Sjoerg
6367330f729Sjoerg LabelAndGotoScopes[SubStmt] = ParentScope;
6377330f729Sjoerg SubStmt = Next;
6387330f729Sjoerg }
6397330f729Sjoerg
6407330f729Sjoerg // Recursively walk the AST.
6417330f729Sjoerg BuildScopeInformation(SubStmt, ParentScope);
6427330f729Sjoerg }
6437330f729Sjoerg }
6447330f729Sjoerg
6457330f729Sjoerg /// VerifyJumps - Verify each element of the Jumps array to see if they are
6467330f729Sjoerg /// valid, emitting diagnostics if not.
VerifyJumps()6477330f729Sjoerg void JumpScopeChecker::VerifyJumps() {
6487330f729Sjoerg while (!Jumps.empty()) {
6497330f729Sjoerg Stmt *Jump = Jumps.pop_back_val();
6507330f729Sjoerg
6517330f729Sjoerg // With a goto,
6527330f729Sjoerg if (GotoStmt *GS = dyn_cast<GotoStmt>(Jump)) {
6537330f729Sjoerg // The label may not have a statement if it's coming from inline MS ASM.
6547330f729Sjoerg if (GS->getLabel()->getStmt()) {
6557330f729Sjoerg CheckJump(GS, GS->getLabel()->getStmt(), GS->getGotoLoc(),
6567330f729Sjoerg diag::err_goto_into_protected_scope,
6577330f729Sjoerg diag::ext_goto_into_protected_scope,
6587330f729Sjoerg diag::warn_cxx98_compat_goto_into_protected_scope);
6597330f729Sjoerg }
6607330f729Sjoerg CheckGotoStmt(GS);
6617330f729Sjoerg continue;
6627330f729Sjoerg }
6637330f729Sjoerg
6647330f729Sjoerg // We only get indirect gotos here when they have a constant target.
6657330f729Sjoerg if (IndirectGotoStmt *IGS = dyn_cast<IndirectGotoStmt>(Jump)) {
6667330f729Sjoerg LabelDecl *Target = IGS->getConstantTarget();
6677330f729Sjoerg CheckJump(IGS, Target->getStmt(), IGS->getGotoLoc(),
6687330f729Sjoerg diag::err_goto_into_protected_scope,
6697330f729Sjoerg diag::ext_goto_into_protected_scope,
6707330f729Sjoerg diag::warn_cxx98_compat_goto_into_protected_scope);
6717330f729Sjoerg continue;
6727330f729Sjoerg }
6737330f729Sjoerg
6747330f729Sjoerg SwitchStmt *SS = cast<SwitchStmt>(Jump);
6757330f729Sjoerg for (SwitchCase *SC = SS->getSwitchCaseList(); SC;
6767330f729Sjoerg SC = SC->getNextSwitchCase()) {
6777330f729Sjoerg if (CHECK_PERMISSIVE(!LabelAndGotoScopes.count(SC)))
6787330f729Sjoerg continue;
6797330f729Sjoerg SourceLocation Loc;
6807330f729Sjoerg if (CaseStmt *CS = dyn_cast<CaseStmt>(SC))
6817330f729Sjoerg Loc = CS->getBeginLoc();
6827330f729Sjoerg else if (DefaultStmt *DS = dyn_cast<DefaultStmt>(SC))
6837330f729Sjoerg Loc = DS->getBeginLoc();
6847330f729Sjoerg else
6857330f729Sjoerg Loc = SC->getBeginLoc();
6867330f729Sjoerg CheckJump(SS, SC, Loc, diag::err_switch_into_protected_scope, 0,
6877330f729Sjoerg diag::warn_cxx98_compat_switch_into_protected_scope);
6887330f729Sjoerg }
6897330f729Sjoerg }
6907330f729Sjoerg }
6917330f729Sjoerg
6927330f729Sjoerg /// VerifyIndirectOrAsmJumps - Verify whether any possible indirect goto or
6937330f729Sjoerg /// asm goto jump might cross a protection boundary. Unlike direct jumps,
6947330f729Sjoerg /// indirect or asm goto jumps count cleanups as protection boundaries:
6957330f729Sjoerg /// since there's no way to know where the jump is going, we can't implicitly
6967330f729Sjoerg /// run the right cleanups the way we can with direct jumps.
6977330f729Sjoerg /// Thus, an indirect/asm jump is "trivial" if it bypasses no
6987330f729Sjoerg /// initializations and no teardowns. More formally, an indirect/asm jump
6997330f729Sjoerg /// from A to B is trivial if the path out from A to DCA(A,B) is
7007330f729Sjoerg /// trivial and the path in from DCA(A,B) to B is trivial, where
7017330f729Sjoerg /// DCA(A,B) is the deepest common ancestor of A and B.
7027330f729Sjoerg /// Jump-triviality is transitive but asymmetric.
7037330f729Sjoerg ///
7047330f729Sjoerg /// A path in is trivial if none of the entered scopes have an InDiag.
7057330f729Sjoerg /// A path out is trivial is none of the exited scopes have an OutDiag.
7067330f729Sjoerg ///
7077330f729Sjoerg /// Under these definitions, this function checks that the indirect
7087330f729Sjoerg /// jump between A and B is trivial for every indirect goto statement A
7097330f729Sjoerg /// and every label B whose address was taken in the function.
VerifyIndirectOrAsmJumps(bool IsAsmGoto)7107330f729Sjoerg void JumpScopeChecker::VerifyIndirectOrAsmJumps(bool IsAsmGoto) {
7117330f729Sjoerg SmallVector<Stmt*, 4> GotoJumps = IsAsmGoto ? AsmJumps : IndirectJumps;
7127330f729Sjoerg if (GotoJumps.empty())
7137330f729Sjoerg return;
7147330f729Sjoerg SmallVector<LabelDecl *, 4> JumpTargets =
7157330f729Sjoerg IsAsmGoto ? AsmJumpTargets : IndirectJumpTargets;
7167330f729Sjoerg // If there aren't any address-of-label expressions in this function,
7177330f729Sjoerg // complain about the first indirect goto.
7187330f729Sjoerg if (JumpTargets.empty()) {
7197330f729Sjoerg assert(!IsAsmGoto &&"only indirect goto can get here");
7207330f729Sjoerg S.Diag(GotoJumps[0]->getBeginLoc(),
7217330f729Sjoerg diag::err_indirect_goto_without_addrlabel);
7227330f729Sjoerg return;
7237330f729Sjoerg }
7247330f729Sjoerg // Collect a single representative of every scope containing an
7257330f729Sjoerg // indirect or asm goto. For most code bases, this substantially cuts
7267330f729Sjoerg // down on the number of jump sites we'll have to consider later.
7277330f729Sjoerg typedef std::pair<unsigned, Stmt*> JumpScope;
7287330f729Sjoerg SmallVector<JumpScope, 32> JumpScopes;
7297330f729Sjoerg {
7307330f729Sjoerg llvm::DenseMap<unsigned, Stmt*> JumpScopesMap;
7317330f729Sjoerg for (SmallVectorImpl<Stmt *>::iterator I = GotoJumps.begin(),
7327330f729Sjoerg E = GotoJumps.end();
7337330f729Sjoerg I != E; ++I) {
7347330f729Sjoerg Stmt *IG = *I;
7357330f729Sjoerg if (CHECK_PERMISSIVE(!LabelAndGotoScopes.count(IG)))
7367330f729Sjoerg continue;
7377330f729Sjoerg unsigned IGScope = LabelAndGotoScopes[IG];
7387330f729Sjoerg Stmt *&Entry = JumpScopesMap[IGScope];
7397330f729Sjoerg if (!Entry) Entry = IG;
7407330f729Sjoerg }
7417330f729Sjoerg JumpScopes.reserve(JumpScopesMap.size());
7427330f729Sjoerg for (llvm::DenseMap<unsigned, Stmt *>::iterator I = JumpScopesMap.begin(),
7437330f729Sjoerg E = JumpScopesMap.end();
7447330f729Sjoerg I != E; ++I)
7457330f729Sjoerg JumpScopes.push_back(*I);
7467330f729Sjoerg }
7477330f729Sjoerg
7487330f729Sjoerg // Collect a single representative of every scope containing a
7497330f729Sjoerg // label whose address was taken somewhere in the function.
7507330f729Sjoerg // For most code bases, there will be only one such scope.
7517330f729Sjoerg llvm::DenseMap<unsigned, LabelDecl*> TargetScopes;
7527330f729Sjoerg for (SmallVectorImpl<LabelDecl *>::iterator I = JumpTargets.begin(),
7537330f729Sjoerg E = JumpTargets.end();
7547330f729Sjoerg I != E; ++I) {
7557330f729Sjoerg LabelDecl *TheLabel = *I;
7567330f729Sjoerg if (CHECK_PERMISSIVE(!LabelAndGotoScopes.count(TheLabel->getStmt())))
7577330f729Sjoerg continue;
7587330f729Sjoerg unsigned LabelScope = LabelAndGotoScopes[TheLabel->getStmt()];
7597330f729Sjoerg LabelDecl *&Target = TargetScopes[LabelScope];
7607330f729Sjoerg if (!Target) Target = TheLabel;
7617330f729Sjoerg }
7627330f729Sjoerg
7637330f729Sjoerg // For each target scope, make sure it's trivially reachable from
7647330f729Sjoerg // every scope containing a jump site.
7657330f729Sjoerg //
7667330f729Sjoerg // A path between scopes always consists of exitting zero or more
7677330f729Sjoerg // scopes, then entering zero or more scopes. We build a set of
7687330f729Sjoerg // of scopes S from which the target scope can be trivially
7697330f729Sjoerg // entered, then verify that every jump scope can be trivially
7707330f729Sjoerg // exitted to reach a scope in S.
7717330f729Sjoerg llvm::BitVector Reachable(Scopes.size(), false);
7727330f729Sjoerg for (llvm::DenseMap<unsigned,LabelDecl*>::iterator
7737330f729Sjoerg TI = TargetScopes.begin(), TE = TargetScopes.end(); TI != TE; ++TI) {
7747330f729Sjoerg unsigned TargetScope = TI->first;
7757330f729Sjoerg LabelDecl *TargetLabel = TI->second;
7767330f729Sjoerg
7777330f729Sjoerg Reachable.reset();
7787330f729Sjoerg
7797330f729Sjoerg // Mark all the enclosing scopes from which you can safely jump
7807330f729Sjoerg // into the target scope. 'Min' will end up being the index of
7817330f729Sjoerg // the shallowest such scope.
7827330f729Sjoerg unsigned Min = TargetScope;
7837330f729Sjoerg while (true) {
7847330f729Sjoerg Reachable.set(Min);
7857330f729Sjoerg
7867330f729Sjoerg // Don't go beyond the outermost scope.
7877330f729Sjoerg if (Min == 0) break;
7887330f729Sjoerg
7897330f729Sjoerg // Stop if we can't trivially enter the current scope.
7907330f729Sjoerg if (Scopes[Min].InDiag) break;
7917330f729Sjoerg
7927330f729Sjoerg Min = Scopes[Min].ParentScope;
7937330f729Sjoerg }
7947330f729Sjoerg
7957330f729Sjoerg // Walk through all the jump sites, checking that they can trivially
7967330f729Sjoerg // reach this label scope.
7977330f729Sjoerg for (SmallVectorImpl<JumpScope>::iterator
7987330f729Sjoerg I = JumpScopes.begin(), E = JumpScopes.end(); I != E; ++I) {
7997330f729Sjoerg unsigned Scope = I->first;
8007330f729Sjoerg
8017330f729Sjoerg // Walk out the "scope chain" for this scope, looking for a scope
8027330f729Sjoerg // we've marked reachable. For well-formed code this amortizes
8037330f729Sjoerg // to O(JumpScopes.size() / Scopes.size()): we only iterate
8047330f729Sjoerg // when we see something unmarked, and in well-formed code we
8057330f729Sjoerg // mark everything we iterate past.
8067330f729Sjoerg bool IsReachable = false;
8077330f729Sjoerg while (true) {
8087330f729Sjoerg if (Reachable.test(Scope)) {
8097330f729Sjoerg // If we find something reachable, mark all the scopes we just
8107330f729Sjoerg // walked through as reachable.
8117330f729Sjoerg for (unsigned S = I->first; S != Scope; S = Scopes[S].ParentScope)
8127330f729Sjoerg Reachable.set(S);
8137330f729Sjoerg IsReachable = true;
8147330f729Sjoerg break;
8157330f729Sjoerg }
8167330f729Sjoerg
8177330f729Sjoerg // Don't walk out if we've reached the top-level scope or we've
8187330f729Sjoerg // gotten shallower than the shallowest reachable scope.
8197330f729Sjoerg if (Scope == 0 || Scope < Min) break;
8207330f729Sjoerg
8217330f729Sjoerg // Don't walk out through an out-diagnostic.
8227330f729Sjoerg if (Scopes[Scope].OutDiag) break;
8237330f729Sjoerg
8247330f729Sjoerg Scope = Scopes[Scope].ParentScope;
8257330f729Sjoerg }
8267330f729Sjoerg
8277330f729Sjoerg // Only diagnose if we didn't find something.
8287330f729Sjoerg if (IsReachable) continue;
8297330f729Sjoerg
8307330f729Sjoerg DiagnoseIndirectOrAsmJump(I->second, I->first, TargetLabel, TargetScope);
8317330f729Sjoerg }
8327330f729Sjoerg }
8337330f729Sjoerg }
8347330f729Sjoerg
8357330f729Sjoerg /// Return true if a particular error+note combination must be downgraded to a
8367330f729Sjoerg /// warning in Microsoft mode.
IsMicrosoftJumpWarning(unsigned JumpDiag,unsigned InDiagNote)8377330f729Sjoerg static bool IsMicrosoftJumpWarning(unsigned JumpDiag, unsigned InDiagNote) {
8387330f729Sjoerg return (JumpDiag == diag::err_goto_into_protected_scope &&
8397330f729Sjoerg (InDiagNote == diag::note_protected_by_variable_init ||
8407330f729Sjoerg InDiagNote == diag::note_protected_by_variable_nontriv_destructor));
8417330f729Sjoerg }
8427330f729Sjoerg
8437330f729Sjoerg /// Return true if a particular note should be downgraded to a compatibility
8447330f729Sjoerg /// warning in C++11 mode.
IsCXX98CompatWarning(Sema & S,unsigned InDiagNote)8457330f729Sjoerg static bool IsCXX98CompatWarning(Sema &S, unsigned InDiagNote) {
8467330f729Sjoerg return S.getLangOpts().CPlusPlus11 &&
8477330f729Sjoerg InDiagNote == diag::note_protected_by_variable_non_pod;
8487330f729Sjoerg }
8497330f729Sjoerg
8507330f729Sjoerg /// Produce primary diagnostic for an indirect jump statement.
DiagnoseIndirectOrAsmJumpStmt(Sema & S,Stmt * Jump,LabelDecl * Target,bool & Diagnosed)8517330f729Sjoerg static void DiagnoseIndirectOrAsmJumpStmt(Sema &S, Stmt *Jump,
8527330f729Sjoerg LabelDecl *Target, bool &Diagnosed) {
8537330f729Sjoerg if (Diagnosed)
8547330f729Sjoerg return;
8557330f729Sjoerg bool IsAsmGoto = isa<GCCAsmStmt>(Jump);
8567330f729Sjoerg S.Diag(Jump->getBeginLoc(), diag::err_indirect_goto_in_protected_scope)
8577330f729Sjoerg << IsAsmGoto;
8587330f729Sjoerg S.Diag(Target->getStmt()->getIdentLoc(), diag::note_indirect_goto_target)
8597330f729Sjoerg << IsAsmGoto;
8607330f729Sjoerg Diagnosed = true;
8617330f729Sjoerg }
8627330f729Sjoerg
8637330f729Sjoerg /// Produce note diagnostics for a jump into a protected scope.
NoteJumpIntoScopes(ArrayRef<unsigned> ToScopes)8647330f729Sjoerg void JumpScopeChecker::NoteJumpIntoScopes(ArrayRef<unsigned> ToScopes) {
8657330f729Sjoerg if (CHECK_PERMISSIVE(ToScopes.empty()))
8667330f729Sjoerg return;
8677330f729Sjoerg for (unsigned I = 0, E = ToScopes.size(); I != E; ++I)
8687330f729Sjoerg if (Scopes[ToScopes[I]].InDiag)
8697330f729Sjoerg S.Diag(Scopes[ToScopes[I]].Loc, Scopes[ToScopes[I]].InDiag);
8707330f729Sjoerg }
8717330f729Sjoerg
8727330f729Sjoerg /// Diagnose an indirect jump which is known to cross scopes.
DiagnoseIndirectOrAsmJump(Stmt * Jump,unsigned JumpScope,LabelDecl * Target,unsigned TargetScope)8737330f729Sjoerg void JumpScopeChecker::DiagnoseIndirectOrAsmJump(Stmt *Jump, unsigned JumpScope,
8747330f729Sjoerg LabelDecl *Target,
8757330f729Sjoerg unsigned TargetScope) {
8767330f729Sjoerg if (CHECK_PERMISSIVE(JumpScope == TargetScope))
8777330f729Sjoerg return;
8787330f729Sjoerg
8797330f729Sjoerg unsigned Common = GetDeepestCommonScope(JumpScope, TargetScope);
8807330f729Sjoerg bool Diagnosed = false;
8817330f729Sjoerg
8827330f729Sjoerg // Walk out the scope chain until we reach the common ancestor.
8837330f729Sjoerg for (unsigned I = JumpScope; I != Common; I = Scopes[I].ParentScope)
8847330f729Sjoerg if (Scopes[I].OutDiag) {
8857330f729Sjoerg DiagnoseIndirectOrAsmJumpStmt(S, Jump, Target, Diagnosed);
8867330f729Sjoerg S.Diag(Scopes[I].Loc, Scopes[I].OutDiag);
8877330f729Sjoerg }
8887330f729Sjoerg
8897330f729Sjoerg SmallVector<unsigned, 10> ToScopesCXX98Compat;
8907330f729Sjoerg
8917330f729Sjoerg // Now walk into the scopes containing the label whose address was taken.
8927330f729Sjoerg for (unsigned I = TargetScope; I != Common; I = Scopes[I].ParentScope)
8937330f729Sjoerg if (IsCXX98CompatWarning(S, Scopes[I].InDiag))
8947330f729Sjoerg ToScopesCXX98Compat.push_back(I);
8957330f729Sjoerg else if (Scopes[I].InDiag) {
8967330f729Sjoerg DiagnoseIndirectOrAsmJumpStmt(S, Jump, Target, Diagnosed);
8977330f729Sjoerg S.Diag(Scopes[I].Loc, Scopes[I].InDiag);
8987330f729Sjoerg }
8997330f729Sjoerg
9007330f729Sjoerg // Diagnose this jump if it would be ill-formed in C++98.
9017330f729Sjoerg if (!Diagnosed && !ToScopesCXX98Compat.empty()) {
9027330f729Sjoerg bool IsAsmGoto = isa<GCCAsmStmt>(Jump);
9037330f729Sjoerg S.Diag(Jump->getBeginLoc(),
9047330f729Sjoerg diag::warn_cxx98_compat_indirect_goto_in_protected_scope)
9057330f729Sjoerg << IsAsmGoto;
9067330f729Sjoerg S.Diag(Target->getStmt()->getIdentLoc(), diag::note_indirect_goto_target)
9077330f729Sjoerg << IsAsmGoto;
9087330f729Sjoerg NoteJumpIntoScopes(ToScopesCXX98Compat);
9097330f729Sjoerg }
9107330f729Sjoerg }
9117330f729Sjoerg
9127330f729Sjoerg /// CheckJump - Validate that the specified jump statement is valid: that it is
9137330f729Sjoerg /// jumping within or out of its current scope, not into a deeper one.
CheckJump(Stmt * From,Stmt * To,SourceLocation DiagLoc,unsigned JumpDiagError,unsigned JumpDiagWarning,unsigned JumpDiagCXX98Compat)9147330f729Sjoerg void JumpScopeChecker::CheckJump(Stmt *From, Stmt *To, SourceLocation DiagLoc,
9157330f729Sjoerg unsigned JumpDiagError, unsigned JumpDiagWarning,
9167330f729Sjoerg unsigned JumpDiagCXX98Compat) {
9177330f729Sjoerg if (CHECK_PERMISSIVE(!LabelAndGotoScopes.count(From)))
9187330f729Sjoerg return;
9197330f729Sjoerg if (CHECK_PERMISSIVE(!LabelAndGotoScopes.count(To)))
9207330f729Sjoerg return;
9217330f729Sjoerg
9227330f729Sjoerg unsigned FromScope = LabelAndGotoScopes[From];
9237330f729Sjoerg unsigned ToScope = LabelAndGotoScopes[To];
9247330f729Sjoerg
9257330f729Sjoerg // Common case: exactly the same scope, which is fine.
9267330f729Sjoerg if (FromScope == ToScope) return;
9277330f729Sjoerg
9287330f729Sjoerg // Warn on gotos out of __finally blocks.
9297330f729Sjoerg if (isa<GotoStmt>(From) || isa<IndirectGotoStmt>(From)) {
9307330f729Sjoerg // If FromScope > ToScope, FromScope is more nested and the jump goes to a
9317330f729Sjoerg // less nested scope. Check if it crosses a __finally along the way.
9327330f729Sjoerg for (unsigned I = FromScope; I > ToScope; I = Scopes[I].ParentScope) {
9337330f729Sjoerg if (Scopes[I].InDiag == diag::note_protected_by_seh_finally) {
9347330f729Sjoerg S.Diag(From->getBeginLoc(), diag::warn_jump_out_of_seh_finally);
9357330f729Sjoerg break;
9367330f729Sjoerg }
937*e038c9c4Sjoerg if (Scopes[I].InDiag == diag::note_omp_protected_structured_block) {
938*e038c9c4Sjoerg S.Diag(From->getBeginLoc(), diag::err_goto_into_protected_scope);
939*e038c9c4Sjoerg S.Diag(To->getBeginLoc(), diag::note_omp_exits_structured_block);
940*e038c9c4Sjoerg break;
941*e038c9c4Sjoerg }
9427330f729Sjoerg }
9437330f729Sjoerg }
9447330f729Sjoerg
9457330f729Sjoerg unsigned CommonScope = GetDeepestCommonScope(FromScope, ToScope);
9467330f729Sjoerg
9477330f729Sjoerg // It's okay to jump out from a nested scope.
9487330f729Sjoerg if (CommonScope == ToScope) return;
9497330f729Sjoerg
9507330f729Sjoerg // Pull out (and reverse) any scopes we might need to diagnose skipping.
9517330f729Sjoerg SmallVector<unsigned, 10> ToScopesCXX98Compat;
9527330f729Sjoerg SmallVector<unsigned, 10> ToScopesError;
9537330f729Sjoerg SmallVector<unsigned, 10> ToScopesWarning;
9547330f729Sjoerg for (unsigned I = ToScope; I != CommonScope; I = Scopes[I].ParentScope) {
9557330f729Sjoerg if (S.getLangOpts().MSVCCompat && JumpDiagWarning != 0 &&
9567330f729Sjoerg IsMicrosoftJumpWarning(JumpDiagError, Scopes[I].InDiag))
9577330f729Sjoerg ToScopesWarning.push_back(I);
9587330f729Sjoerg else if (IsCXX98CompatWarning(S, Scopes[I].InDiag))
9597330f729Sjoerg ToScopesCXX98Compat.push_back(I);
9607330f729Sjoerg else if (Scopes[I].InDiag)
9617330f729Sjoerg ToScopesError.push_back(I);
9627330f729Sjoerg }
9637330f729Sjoerg
9647330f729Sjoerg // Handle warnings.
9657330f729Sjoerg if (!ToScopesWarning.empty()) {
9667330f729Sjoerg S.Diag(DiagLoc, JumpDiagWarning);
9677330f729Sjoerg NoteJumpIntoScopes(ToScopesWarning);
968*e038c9c4Sjoerg assert(isa<LabelStmt>(To));
969*e038c9c4Sjoerg LabelStmt *Label = cast<LabelStmt>(To);
970*e038c9c4Sjoerg Label->setSideEntry(true);
9717330f729Sjoerg }
9727330f729Sjoerg
9737330f729Sjoerg // Handle errors.
9747330f729Sjoerg if (!ToScopesError.empty()) {
9757330f729Sjoerg S.Diag(DiagLoc, JumpDiagError);
9767330f729Sjoerg NoteJumpIntoScopes(ToScopesError);
9777330f729Sjoerg }
9787330f729Sjoerg
9797330f729Sjoerg // Handle -Wc++98-compat warnings if the jump is well-formed.
9807330f729Sjoerg if (ToScopesError.empty() && !ToScopesCXX98Compat.empty()) {
9817330f729Sjoerg S.Diag(DiagLoc, JumpDiagCXX98Compat);
9827330f729Sjoerg NoteJumpIntoScopes(ToScopesCXX98Compat);
9837330f729Sjoerg }
9847330f729Sjoerg }
9857330f729Sjoerg
CheckGotoStmt(GotoStmt * GS)9867330f729Sjoerg void JumpScopeChecker::CheckGotoStmt(GotoStmt *GS) {
9877330f729Sjoerg if (GS->getLabel()->isMSAsmLabel()) {
9887330f729Sjoerg S.Diag(GS->getGotoLoc(), diag::err_goto_ms_asm_label)
9897330f729Sjoerg << GS->getLabel()->getIdentifier();
9907330f729Sjoerg S.Diag(GS->getLabel()->getLocation(), diag::note_goto_ms_asm_label)
9917330f729Sjoerg << GS->getLabel()->getIdentifier();
9927330f729Sjoerg }
9937330f729Sjoerg }
9947330f729Sjoerg
VerifyMustTailStmts()995*e038c9c4Sjoerg void JumpScopeChecker::VerifyMustTailStmts() {
996*e038c9c4Sjoerg for (AttributedStmt *AS : MustTailStmts) {
997*e038c9c4Sjoerg for (unsigned I = LabelAndGotoScopes[AS]; I; I = Scopes[I].ParentScope) {
998*e038c9c4Sjoerg if (Scopes[I].OutDiag) {
999*e038c9c4Sjoerg S.Diag(AS->getBeginLoc(), diag::err_musttail_scope);
1000*e038c9c4Sjoerg S.Diag(Scopes[I].Loc, Scopes[I].OutDiag);
1001*e038c9c4Sjoerg }
1002*e038c9c4Sjoerg }
1003*e038c9c4Sjoerg }
1004*e038c9c4Sjoerg }
1005*e038c9c4Sjoerg
GetMustTailAttr(AttributedStmt * AS)1006*e038c9c4Sjoerg const Attr *JumpScopeChecker::GetMustTailAttr(AttributedStmt *AS) {
1007*e038c9c4Sjoerg ArrayRef<const Attr *> Attrs = AS->getAttrs();
1008*e038c9c4Sjoerg const auto *Iter =
1009*e038c9c4Sjoerg llvm::find_if(Attrs, [](const Attr *A) { return isa<MustTailAttr>(A); });
1010*e038c9c4Sjoerg return Iter != Attrs.end() ? *Iter : nullptr;
1011*e038c9c4Sjoerg }
1012*e038c9c4Sjoerg
DiagnoseInvalidJumps(Stmt * Body)10137330f729Sjoerg void Sema::DiagnoseInvalidJumps(Stmt *Body) {
10147330f729Sjoerg (void)JumpScopeChecker(Body, *this);
10157330f729Sjoerg }
1016