Skip to content

Commit 28b0986

Browse files
bruteforceboylanza
authored andcommitted
[CIR][CodeGen] Add InsertionGuard for tryBodyScope (#1498)
This PR adds an insertion guard for the try body scope for try-catch. Currently, the following code snippet fails during CodeGen: ``` void foo() { int r = 1; try { ++r; return; } catch (...) { } } ``` The insertion point doesn't get reset properly and the cleanup is being ran for a wrong/deleted block causing a segmentation fault. I also added a test.
1 parent 8f04109 commit 28b0986

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

clang/lib/CIR/CodeGen/CIRGenException.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,7 @@ CIRGenFunction::emitCXXTryStmtUnderScope(const CXXTryStmt &S) {
382382
enterCXXTryStmt(S, tryOp);
383383
// Emit the body for the `try {}` part.
384384
{
385+
mlir::OpBuilder::InsertionGuard guard(getBuilder());
385386
CIRGenFunction::LexicalScope tryBodyScope{
386387
*this, loc, getBuilder().getInsertionBlock()};
387388
if (emitStmt(S.getTryBlock(), /*useCurrentScope=*/true).failed())

clang/test/CIR/CodeGen/try-catch.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,3 +149,22 @@ void tc6() {
149149
// CHECK: cir.yield
150150
// CHECK: }
151151
// CHECK: }
152+
153+
// CHECK: cir.func @_Z3tc7v()
154+
void tc7() {
155+
int r = 1;
156+
try {
157+
++r;
158+
return;
159+
} catch (...) {
160+
}
161+
}
162+
163+
// CHECK: cir.scope {
164+
// CHECK: cir.try {
165+
// CHECK: %[[V2:.*]] = cir.load {{.*}} : !cir.ptr<!s32i>, !s32i
166+
// CHECK: %[[V3:.*]] = cir.unary(inc, %[[V2]]) nsw : !s32i, !s32i
167+
// CHECK: cir.store %[[V3]], {{.*}} : !s32i, !cir.ptr<!s32i>
168+
// CHECK: cir.return
169+
// CHECK: }
170+
// CHECK: }

0 commit comments

Comments
 (0)