Skip to content

Commit b287196

Browse files
committed
Adding support for throw inside of microsoft try/except to simplify the IR implementation and revert consistency check issues. There is a larger issue of how to address erroneous mix and match with SEH and traditional exceptions.
1 parent b1bfe2e commit b287196

File tree

5 files changed

+26
-46
lines changed

5 files changed

+26
-46
lines changed

cpp/ql/lib/semmle/code/cpp/ir/implementation/raw/internal/TranslatedCall.qll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ abstract class TranslatedCall extends TranslatedExpr {
9191
exists(ExceptionEdge e | this.hasExceptionBehavior(e) |
9292
this.mayThrowException(e) and
9393
kind = e and
94-
result = this.getParent().getExceptionSuccessorInstruction(any(GotoEdge edge), kind)
94+
result = this.getParent().getExceptionSuccessorInstruction(any(GotoEdge edge))
9595
)
9696
)
9797
}

cpp/ql/lib/semmle/code/cpp/ir/implementation/raw/internal/TranslatedElement.qll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1107,8 +1107,8 @@ abstract class TranslatedElement extends TTranslatedElement {
11071107
* nearest enclosing `try`, or the `Unwind` instruction for the function if
11081108
* there is no enclosing `try`. The successor edge kind is specified by `kind`.
11091109
*/
1110-
Instruction getExceptionSuccessorInstruction(EdgeKind kind, ExceptionEdge exception) {
1111-
result = this.getParent().getExceptionSuccessorInstruction(kind, exception)
1110+
Instruction getExceptionSuccessorInstruction(EdgeKind kind) {
1111+
result = this.getParent().getExceptionSuccessorInstruction(kind)
11121112
}
11131113

11141114
/**

cpp/ql/lib/semmle/code/cpp/ir/implementation/raw/internal/TranslatedExpr.qll

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,14 @@ private import TranslatedInitialization
1515
private import TranslatedStmt
1616
private import TranslatedGlobalVar
1717
private import IRConstruction
18-
private import EdgeKind
1918
import TranslatedCall
2019

20+
predicate tbd(TranslatedElement e, Instruction i, string s) {
21+
e.getInstruction(_) = i and
22+
not exists(i.getSuccessor(_)) and
23+
s = concat(e.getAQlClass(), ",")
24+
}
25+
2126
/**
2227
* Gets the TranslatedExpr for the specified expression. If `expr` is a load or synthesized
2328
* temporary object, the result is the TranslatedExpr for the load or synthetic temporary object
@@ -3045,7 +3050,7 @@ class TranslatedDestructorsAfterThrow extends TranslatedElement, TTranslatedDest
30453050
// And otherwise, exit this element with an exceptional edge
30463051
not exists(this.getChild(id + 1)) and
30473052
kind instanceof CppExceptionEdge and
3048-
result = this.getParent().getExceptionSuccessorInstruction(any(GotoEdge edge), kind)
3053+
result = this.getParent().getExceptionSuccessorInstruction(any(GotoEdge edge))
30493054
)
30503055
}
30513056

@@ -3084,7 +3089,7 @@ abstract class TranslatedThrowExpr extends TranslatedNonConstantExpr {
30843089
or
30853090
not exists(this.getDestructors()) and
30863091
kind instanceof CppExceptionEdge and
3087-
result = this.getParent().getExceptionSuccessorInstruction(any(GotoEdge edge), kind)
3092+
result = this.getParent().getExceptionSuccessorInstruction(any(GotoEdge edge))
30883093
)
30893094
}
30903095

cpp/ql/lib/semmle/code/cpp/ir/implementation/raw/internal/TranslatedFunction.qll

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ private import TranslatedExpr
1111
private import TranslatedInitialization
1212
private import TranslatedStmt
1313
private import VarArgs
14-
private import EdgeKind
1514

1615
/**
1716
* Gets the `TranslatedFunction` that represents function `func`.
@@ -214,10 +213,6 @@ class TranslatedFunction extends TranslatedRootElement, TTranslatedFunction {
214213
or
215214
exists(ThrowExpr throw | throw.getEnclosingFunction() = func)
216215
or
217-
// or
218-
// exists(FunctionCall call | call.getEnclosingFunction() = func |
219-
// getTranslatedExpr(call).(TranslatedCallExpr).mustThrowException(_)
220-
// )
221216
exists(FunctionCall call | call.getEnclosingFunction() = func |
222217
getTranslatedExpr(call).(TranslatedCallExpr).mayThrowException(_)
223218
)
@@ -233,8 +228,7 @@ class TranslatedFunction extends TranslatedRootElement, TTranslatedFunction {
233228
)
234229
}
235230

236-
final override Instruction getExceptionSuccessorInstruction(EdgeKind kind, ExceptionEdge exception) {
237-
(exception = cppExceptionEdge() or exception = sehExceptionEdge()) and
231+
final override Instruction getExceptionSuccessorInstruction(EdgeKind kind) {
238232
result = this.getInstruction(UnwindTag()) and
239233
kind instanceof GotoEdge
240234
}

cpp/ql/lib/semmle/code/cpp/ir/implementation/raw/internal/TranslatedStmt.qll

Lines changed: 14 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ private import TranslatedElement
1010
private import TranslatedExpr
1111
private import TranslatedFunction
1212
private import TranslatedInitialization
13-
private import EdgeKind
1413

1514
TranslatedStmt getTranslatedStmt(Stmt stmt) { result.getAst() = stmt }
1615

@@ -152,7 +151,7 @@ class TranslatedMicrosoftTryExceptHandler extends TranslatedElement,
152151
// TODO: This is not really correct. The semantics of `EXCEPTION_CONTINUE_EXECUTION` is that
153152
// we should continue execution at the point where the exception occurred. But we don't have
154153
// any instruction to model this behavior.
155-
result = this.getExceptionSuccessorInstruction(any(GotoEdge edge), sehExceptionEdge())
154+
result = this.getExceptionSuccessorInstruction(any(GotoEdge edge))
156155
or
157156
kind instanceof FalseEdge and
158157
result = this.getInstruction(TryExceptGenerateZero())
@@ -172,7 +171,7 @@ class TranslatedMicrosoftTryExceptHandler extends TranslatedElement,
172171
tag = TryExceptCompareZeroBranch() and
173172
(
174173
kind instanceof TrueEdge and
175-
result = this.getExceptionSuccessorInstruction(any(GotoEdge edge), sehExceptionEdge())
174+
result = this.getExceptionSuccessorInstruction(any(GotoEdge edge))
176175
or
177176
kind instanceof FalseEdge and
178177
result = this.getInstruction(TryExceptGenerateOne())
@@ -227,10 +226,10 @@ class TranslatedMicrosoftTryExceptHandler extends TranslatedElement,
227226

228227
final override Function getFunction() { result = tryExcept.getEnclosingFunction() }
229228

230-
override Instruction getExceptionSuccessorInstruction(EdgeKind kind, ExceptionEdge exception) {
229+
override Instruction getExceptionSuccessorInstruction(EdgeKind kind) {
231230
// A throw from within a `__except` block flows to the handler for the parent of
232231
// the `__try`.
233-
result = this.getParent().getParent().getExceptionSuccessorInstruction(kind, exception)
232+
result = this.getParent().getParent().getExceptionSuccessorInstruction(kind)
234233
}
235234
}
236235

@@ -283,10 +282,10 @@ class TranslatedMicrosoftTryFinallyHandler extends TranslatedElement,
283282
result = getTranslatedStmt(tryFinally.getFinally())
284283
}
285284

286-
override Instruction getExceptionSuccessorInstruction(EdgeKind kind, ExceptionEdge exception) {
285+
override Instruction getExceptionSuccessorInstruction(EdgeKind kind) {
287286
// A throw from within a `__finally` block flows to the handler for the parent of
288287
// the `__try`.
289-
result = this.getParent().getParent().getExceptionSuccessorInstruction(kind, exception)
288+
result = this.getParent().getParent().getExceptionSuccessorInstruction(kind)
290289
}
291290
}
292291

@@ -735,32 +734,14 @@ class TranslatedTryStmt extends TranslatedStmt {
735734
// of the `try`, because the exception successor of the `try` itself is
736735
// the first catch clause.
737736
handler = this.getHandler(stmt.getNumberOfCatchClauses() - 1) and
738-
exists(ExceptionEdge exception |
739-
stmt instanceof MicrosoftTryStmt and exception instanceof SehExceptionEdge
740-
or
741-
stmt instanceof TryStmt and exception instanceof CppExceptionEdge
742-
|
743-
result = this.getParent().getExceptionSuccessorInstruction(kind, exception)
744-
)
737+
result = this.getParent().getExceptionSuccessorInstruction(kind)
745738
}
746739

747-
final override Instruction getExceptionSuccessorInstruction(EdgeKind kind, ExceptionEdge exception) {
748-
// Seh exceptions are only handled for Seh try statements and
749-
// C++ exceptions for C++ try statements.
750-
// I.e., we are assuming there isn't a mix and match between Seh and C++ exceptions.
751-
// They are either all Seh or all C++ within a single try block depending on the
752-
// try type (TryStmt vs MicrosoftTryStmt).
753-
(
754-
stmt instanceof TryStmt and exception instanceof CppExceptionEdge
755-
or
756-
stmt instanceof MicrosoftTryStmt and exception instanceof SehExceptionEdge
757-
) and
758-
(
759-
result = this.getHandler(0).getFirstInstruction(kind)
760-
or
761-
not exists(this.getHandler(_)) and
762-
result = this.getFinally().getFirstInstruction(kind)
763-
)
740+
final override Instruction getExceptionSuccessorInstruction(EdgeKind kind) {
741+
result = this.getHandler(0).getFirstInstruction(kind)
742+
or
743+
not exists(this.getHandler(_)) and
744+
result = this.getFinally().getFirstInstruction(kind)
764745
}
765746

766747
private TranslatedElement getHandler(int index) { result = stmt.getTranslatedHandler(index) }
@@ -840,10 +821,10 @@ abstract class TranslatedHandler extends TranslatedStmt {
840821
child = this.getBlock() and result = this.getParent().getChildSuccessor(this, kind)
841822
}
842823

843-
override Instruction getExceptionSuccessorInstruction(EdgeKind kind, ExceptionEdge exception) {
824+
override Instruction getExceptionSuccessorInstruction(EdgeKind kind) {
844825
// A throw from within a `catch` block flows to the handler for the parent of
845826
// the `try`.
846-
result = this.getParent().getParent().getExceptionSuccessorInstruction(kind, exception)
827+
result = this.getParent().getParent().getExceptionSuccessorInstruction(kind)
847828
}
848829

849830
TranslatedStmt getBlock() { result = getTranslatedStmt(stmt.getBlock()) }

0 commit comments

Comments
 (0)