llvmbot wrote:

<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang

Author: None (GkvJwa)

<details>
<summary>Changes</summary>

Consider the following code:
```

#include &lt;windows.h&gt;

class CheckError {
 public:
  static CheckError Check();

  ~CheckError();
};

void Foo(const int* p) {
  int d = 0;
  __try {
    d = *p;
  } __except (EXCEPTION_EXECUTE_HANDLER) {
    ::CheckError::Check();
  }
}

```

The following error will occur in mscv.
```
error C2712: Cannot use __try in functions that require object unwinding
```

However, using LLVM /EHa will cause it to crash.
```
llvm::SmallVectorTemplateCommon&lt;llvm::SEHUnwindMapEntry,void&gt;::operator[](unsigned
 __int64 idx) Line 295  C++
llvm::calculateSEHStateForAsynchEH(const llvm::BasicBlock * BB, int State, 
llvm::WinEHFuncInfo &amp; EHInfo) Line 346   C++
llvm::calculateSEHStateNumbers(const llvm::Function * Fn, llvm::WinEHFuncInfo 
&amp; FuncInfo) Line 612  C++
llvm::FunctionLoweringInfo::set(const llvm::Function &amp; fn, 
llvm::MachineFunction &amp; mf, llvm::SelectionDAG * DAG) Line 114       C++
llvm::SelectionDAGISel::runOnMachineFunction(llvm::MachineFunction &amp; mf) 
Line 588   C++
```

This patch is compatible with the crash.

---
Full diff: https://github.com/llvm/llvm-project/pull/172287.diff


1 Files Affected:

- (modified) clang/lib/Sema/SemaStmt.cpp (+43) 


``````````diff
diff --git a/clang/lib/Sema/SemaStmt.cpp b/clang/lib/Sema/SemaStmt.cpp
index 1b1643250d05e..c64029f269c2b 100644
--- a/clang/lib/Sema/SemaStmt.cpp
+++ b/clang/lib/Sema/SemaStmt.cpp
@@ -4553,6 +4553,45 @@ StmtResult Sema::ActOnSEHTryBlock(bool IsCXXTry, 
SourceLocation TryLoc,
   return SEHTryStmt::Create(Context, IsCXXTry, TryLoc, TryBlock, Handler);
 }
 
+static bool containsNonTrivialObject(Sema &S, const Stmt *Node) {
+  (void)S;
+  if (!Node)
+    return false;
+
+  if (const DeclStmt *DS = dyn_cast<DeclStmt>(Node)) {
+    for (const Decl *D : DS->decls()) {
+      if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
+        QualType T = VD->getType();
+        if (const RecordType *RT = T->getAs<RecordType>()) {
+          if (const CXXRecordDecl *RD = RT->getAsCXXRecordDecl()) {
+            if (RD->hasDefinition() && !RD->hasTrivialDestructor())
+              return true;
+          }
+        }
+      }
+    }
+  }
+
+  if (const Expr *E = dyn_cast<Expr>(Node)) {
+    QualType T = E->getType();
+    if (T->isRecordType() && E->getValueKind() != VK_LValue) {
+      if (const RecordType *RT = T->getAs<RecordType>()) {
+        if (const CXXRecordDecl *RD = RT->getAsCXXRecordDecl()) {
+          if (RD->hasDefinition() && !RD->hasTrivialDestructor())
+            return true;
+        }
+      }
+    }
+  }
+
+  // children.
+  for (const Stmt *Child : Node->children())
+    if (containsNonTrivialObject(S, Child))
+      return true;
+
+  return false;
+}
+
 StmtResult Sema::ActOnSEHExceptBlock(SourceLocation Loc, Expr *FilterExpr,
                                      Stmt *Block) {
   assert(FilterExpr && Block);
@@ -4562,6 +4601,10 @@ StmtResult Sema::ActOnSEHExceptBlock(SourceLocation Loc, 
Expr *FilterExpr,
         Diag(FilterExpr->getExprLoc(), diag::err_filter_expression_integral)
         << FTy);
   }
+  if (containsNonTrivialObject(*this, Block)) {
+    Diag(Loc, diag::err_seh_try_outside_functions);
+    return StmtError();
+  }
   return SEHExceptStmt::Create(Context, Loc, FilterExpr, Block);
 }
 

``````````

</details>


https://github.com/llvm/llvm-project/pull/172287
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to