Skip to content

Commit 829a934

Browse files
authored
Merge pull request #18049 from microsoft/brodes/seh_flow_phase2_splitting_seh_edges
Brodes/seh flow phase2 splitting seh edges
2 parents dce29db + 3619b01 commit 829a934

File tree

6 files changed

+100
-75
lines changed

6 files changed

+100
-75
lines changed

cpp/ql/lib/semmle/code/cpp/ir/implementation/EdgeKind.qll

+30-5
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ private newtype TEdgeKind =
88
TGotoEdge() or // Single successor (including fall-through)
99
TTrueEdge() or // 'true' edge of conditional branch
1010
TFalseEdge() or // 'false' edge of conditional branch
11-
TExceptionEdge() or // Thrown exception
11+
TCppExceptionEdge() or // Thrown C++ exception
12+
TSehExceptionEdge() or // Thrown SEH exception
1213
TDefaultEdge() or // 'default' label of switch
1314
TCaseEdge(string minValue, string maxValue) {
1415
// Case label of switch
@@ -51,12 +52,31 @@ class FalseEdge extends EdgeKindImpl, TFalseEdge {
5152
final override string toString() { result = "False" }
5253
}
5354

55+
abstract private class ExceptionEdgeImpl extends EdgeKindImpl { }
56+
5457
/**
5558
* An "exception" edge, representing the successor of an instruction when that
5659
* instruction's evaluation throws an exception.
60+
*
61+
* Exception edges are expclitly sublcassed to `CppExceptionEdge` and `SehExceptionEdge`
62+
* only. Further sublcasses, if required, should be added privately here for IR efficiency.
5763
*/
58-
class ExceptionEdge extends EdgeKindImpl, TExceptionEdge {
59-
final override string toString() { result = "Exception" }
64+
final class ExceptionEdge = ExceptionEdgeImpl;
65+
66+
/**
67+
* An "exception" edge, representing the successor of an instruction when that
68+
* instruction's evaluation throws a C++ exception.
69+
*/
70+
class CppExceptionEdge extends ExceptionEdgeImpl, TCppExceptionEdge {
71+
final override string toString() { result = "C++ Exception" }
72+
}
73+
74+
/**
75+
* An "exception" edge, representing the successor of an instruction when that
76+
* instruction's evaluation throws an SEH exception.
77+
*/
78+
class SehExceptionEdge extends ExceptionEdgeImpl, TSehExceptionEdge {
79+
final override string toString() { result = "SEH Exception" }
6080
}
6181

6282
/**
@@ -123,9 +143,14 @@ module EdgeKind {
123143
FalseEdge falseEdge() { result = TFalseEdge() }
124144

125145
/**
126-
* Gets the single instance of the `ExceptionEdge` class.
146+
* Gets the single instance of the `CppExceptionEdge` class.
147+
*/
148+
CppExceptionEdge cppExceptionEdge() { result = TCppExceptionEdge() }
149+
150+
/**
151+
* Gets the single instance of the `SehExceptionEdge` class.
127152
*/
128-
ExceptionEdge exceptionEdge() { result = TExceptionEdge() }
153+
SehExceptionEdge sehExceptionEdge() { result = TSehExceptionEdge() }
129154

130155
/**
131156
* Gets the single instance of the `DefaultEdge` class.

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ abstract class TranslatedCall extends TranslatedExpr {
8888
result = this.getParent().getChildSuccessor(this, kind)
8989
or
9090
this.mayThrowException() and
91-
kind instanceof ExceptionEdge and
91+
kind instanceof CppExceptionEdge and
9292
result = this.getParent().getExceptionSuccessorInstruction(any(GotoEdge edge))
9393
)
9494
}

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -3039,7 +3039,7 @@ class TranslatedDestructorsAfterThrow extends TranslatedElement, TTranslatedDest
30393039
or
30403040
// And otherwise, exit this element with an exceptional edge
30413041
not exists(this.getChild(id + 1)) and
3042-
kind instanceof ExceptionEdge and
3042+
kind instanceof CppExceptionEdge and
30433043
result = this.getParent().getExceptionSuccessorInstruction(any(GotoEdge edge))
30443044
)
30453045
}
@@ -3078,7 +3078,7 @@ abstract class TranslatedThrowExpr extends TranslatedNonConstantExpr {
30783078
result = this.getDestructors().getFirstInstruction(kind)
30793079
or
30803080
not exists(this.getDestructors()) and
3081-
kind instanceof ExceptionEdge and
3081+
kind instanceof CppExceptionEdge and
30823082
result = this.getParent().getExceptionSuccessorInstruction(any(GotoEdge edge))
30833083
)
30843084
}

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -932,7 +932,7 @@ class TranslatedCatchByTypeHandler extends TranslatedHandler {
932932
kind instanceof GotoEdge and
933933
result = this.getParameter().getFirstInstruction(kind)
934934
or
935-
kind instanceof ExceptionEdge and
935+
kind instanceof CppExceptionEdge and
936936
if exists(this.getDestructors())
937937
then result = this.getDestructors().getFirstInstruction(any(GotoEdge edge))
938938
else result = this.getParent().(TranslatedTryStmt).getNextHandler(this, any(GotoEdge edge))

cpp/ql/test/library-tests/ir/ir/aliased_ir.expected

+29-29
Original file line numberDiff line numberDiff line change
@@ -2490,7 +2490,7 @@ destructors_for_temps.cpp:
24902490
# 47| v47_6(void) = ^IndirectReadSideEffect[-1] : &:r47_1, ~m47_5
24912491
# 47| m47_7(ClassWithDestructor2) = ^IndirectMayWriteSideEffect[-1] : &:r47_1
24922492
# 47| m47_8(unknown) = Chi : total:m47_5, partial:m47_7
2493-
#-----| Exception -> Block 2
2493+
#-----| C++ Exception -> Block 2
24942494

24952495
# 47| Block 4
24962496
# 47| v47_9(void) = NoOp :
@@ -2574,7 +2574,7 @@ destructors_for_temps.cpp:
25742574
# 52| v52_6(void) = ^IndirectReadSideEffect[-1] : &:r52_1, ~m52_5
25752575
# 52| m52_7(ClassWithDestructor2) = ^IndirectMayWriteSideEffect[-1] : &:r52_1
25762576
# 52| m52_8(unknown) = Chi : total:m52_5, partial:m52_7
2577-
#-----| Exception -> Block 2
2577+
#-----| C++ Exception -> Block 2
25782578

25792579
# 51| Block 4
25802580
# 51| r51_32(glval<ClassWithDestructor2>) = VariableAddress[#temp51:75] :
@@ -2656,7 +2656,7 @@ destructors_for_temps.cpp:
26562656
# 55| m55_29(ClassWithConstructor) = ^IndirectMayWriteSideEffect[-1] : &:r55_5
26572657
# 55| m55_30(unknown) = Chi : total:m55_28, partial:m55_29
26582658
# 55| v55_31(void) = ThrowValue : &:r55_5, ~m55_30
2659-
#-----| Exception -> Block 2
2659+
#-----| C++ Exception -> Block 2
26602660

26612661
# 55| Block 4
26622662
# 55| r55_32(glval<ClassWithDestructor2>) = VariableAddress[#temp55:75] :
@@ -2721,7 +2721,7 @@ destructors_for_temps.cpp:
27212721
# 59| m59_20(unknown) = Chi : total:m59_17, partial:m59_19
27222722
# 59| m59_21(char) = Store[#throw59:9] : &:r59_4, r59_15
27232723
# 59| v59_22(void) = ThrowValue : &:r59_4, m59_21
2724-
#-----| Exception -> Block 2
2724+
#-----| C++ Exception -> Block 2
27252725

27262726
# 59| Block 4
27272727
# 59| r59_23(char) = Constant[97] :
@@ -3120,7 +3120,7 @@ ir.c:
31203120
# 25| v25_4(void) = Call[ExRaiseAccessViolation] : func:r25_1, 0:r25_3
31213121
# 25| m25_5(unknown) = ^CallSideEffect : ~m21_4
31223122
# 25| m25_6(unknown) = Chi : total:m21_4, partial:m25_5
3123-
#-----| Exception -> Block 3
3123+
#-----| C++ Exception -> Block 3
31243124

31253125
# 26| Block 1
31263126
# 26| r26_1(int) = Constant[0] :
@@ -3167,7 +3167,7 @@ ir.c:
31673167
# 36| v36_3(void) = Call[ExRaiseAccessViolation] : func:r36_1, 0:r36_2
31683168
# 36| m36_4(unknown) = ^CallSideEffect : ~m32_4
31693169
# 36| m36_5(unknown) = Chi : total:m32_4, partial:m36_4
3170-
#-----| Exception -> Block 4
3170+
#-----| C++ Exception -> Block 4
31713171

31723172
# 32| Block 1
31733173
# 32| v32_5(void) = Unwind :
@@ -3202,7 +3202,7 @@ ir.c:
32023202
# 40| v40_3(void) = Call[ExRaiseAccessViolation] : func:r40_1, 0:r40_2
32033203
# 40| m40_4(unknown) = ^CallSideEffect : ~m36_5
32043204
# 40| m40_5(unknown) = Chi : total:m36_5, partial:m40_4
3205-
#-----| Exception -> Block 1
3205+
#-----| C++ Exception -> Block 1
32063206

32073207
# 32| Block 6
32083208
# 32| v32_8(void) = Unreached :
@@ -3241,7 +3241,7 @@ ir.c:
32413241
# 62| v62_3(void) = Call[ExRaiseAccessViolation] : func:r62_1, 0:r62_2
32423242
# 62| m62_4(unknown) = ^CallSideEffect : ~m57_4
32433243
# 62| m62_5(unknown) = Chi : total:m57_4, partial:m62_4
3244-
#-----| Exception -> Block 1
3244+
#-----| C++ Exception -> Block 1
32453245

32463246
# 66| Block 1
32473247
# 66| r66_1(int) = Constant[1] :
@@ -3263,7 +3263,7 @@ ir.c:
32633263
# 73| v73_3(void) = Call[ExRaiseAccessViolation] : func:r73_1, 0:r73_2
32643264
# 73| m73_4(unknown) = ^CallSideEffect : ~m70_4
32653265
# 73| m73_5(unknown) = Chi : total:m70_4, partial:m73_4
3266-
#-----| Exception -> Block 2
3266+
#-----| C++ Exception -> Block 2
32673267

32683268
# 70| Block 1
32693269
# 70| v70_5(void) = Unwind :
@@ -3276,7 +3276,7 @@ ir.c:
32763276
# 76| v76_3(void) = Call[ExRaiseAccessViolation] : func:r76_1, 0:r76_2
32773277
# 76| m76_4(unknown) = ^CallSideEffect : ~m73_5
32783278
# 76| m76_5(unknown) = Chi : total:m73_5, partial:m76_4
3279-
#-----| Exception -> Block 1
3279+
#-----| C++ Exception -> Block 1
32803280

32813281
# 80| void raise_access_violation()
32823282
# 80| Block 0
@@ -3289,7 +3289,7 @@ ir.c:
32893289
# 81| v81_3(void) = Call[ExRaiseAccessViolation] : func:r81_1, 0:r81_2
32903290
# 81| m81_4(unknown) = ^CallSideEffect : ~m80_4
32913291
# 81| m81_5(unknown) = Chi : total:m80_4, partial:m81_4
3292-
#-----| Exception -> Block 1
3292+
#-----| C++ Exception -> Block 1
32933293

32943294
# 80| Block 1
32953295
# 80| v80_5(void) = Unwind :
@@ -6976,7 +6976,7 @@ ir.cpp:
69766976
# 728| r728_3(char *) = Convert : r728_2
69776977
# 728| m728_4(char *) = Store[#throw728:7] : &:r728_1, r728_3
69786978
# 728| v728_5(void) = ThrowValue : &:r728_1, m728_4
6979-
#-----| Exception -> Block 6
6979+
#-----| C++ Exception -> Block 6
69806980

69816981
# 730| Block 4
69826982
# 730| r730_1(glval<int>) = VariableAddress[x] :
@@ -6995,7 +6995,7 @@ ir.cpp:
69956995

69966996
# 735| Block 6
69976997
# 735| v735_1(void) = CatchByType[const char *] :
6998-
#-----| Exception -> Block 8
6998+
#-----| C++ Exception -> Block 8
69996999
#-----| Goto -> Block 7
70007000

70017001
# 735| Block 7
@@ -7017,11 +7017,11 @@ ir.cpp:
70177017
# 736| m736_11(String) = ^IndirectMayWriteSideEffect[-1] : &:r736_1
70187018
# 736| m736_12(unknown) = Chi : total:m736_9, partial:m736_11
70197019
# 736| v736_13(void) = ThrowValue : &:r736_1, ~m736_12
7020-
#-----| Exception -> Block 2
7020+
#-----| C++ Exception -> Block 2
70217021

70227022
# 738| Block 8
70237023
# 738| v738_1(void) = CatchByType[const String &] :
7024-
#-----| Exception -> Block 10
7024+
#-----| C++ Exception -> Block 10
70257025
#-----| Goto -> Block 9
70267026

70277027
# 738| Block 9
@@ -7035,7 +7035,7 @@ ir.cpp:
70357035
# 740| Block 10
70367036
# 740| v740_1(void) = CatchAny :
70377037
# 741| v741_1(void) = ReThrow :
7038-
#-----| Exception -> Block 2
7038+
#-----| C++ Exception -> Block 2
70397039

70407040
# 743| Block 11
70417041
# 743| v743_1(void) = NoOp :
@@ -9943,7 +9943,7 @@ ir.cpp:
99439943
# 1195| r1195_3(char *) = Convert : r1195_2
99449944
# 1195| m1195_4(char *) = Store[#throw1195:7] : &:r1195_1, r1195_3
99459945
# 1195| v1195_5(void) = ThrowValue : &:r1195_1, m1195_4
9946-
#-----| Exception -> Block 6
9946+
#-----| C++ Exception -> Block 6
99479947

99489948
# 1197| Block 4
99499949
# 1197| r1197_1(glval<int>) = VariableAddress[x] :
@@ -9962,7 +9962,7 @@ ir.cpp:
99629962

99639963
# 1202| Block 6
99649964
# 1202| v1202_1(void) = CatchByType[const char *] :
9965-
#-----| Exception -> Block 8
9965+
#-----| C++ Exception -> Block 8
99669966
#-----| Goto -> Block 7
99679967

99689968
# 1202| Block 7
@@ -9984,11 +9984,11 @@ ir.cpp:
99849984
# 1203| m1203_11(String) = ^IndirectMayWriteSideEffect[-1] : &:r1203_1
99859985
# 1203| m1203_12(unknown) = Chi : total:m1203_9, partial:m1203_11
99869986
# 1203| v1203_13(void) = ThrowValue : &:r1203_1, ~m1203_12
9987-
#-----| Exception -> Block 2
9987+
#-----| C++ Exception -> Block 2
99889988

99899989
# 1205| Block 8
99909990
# 1205| v1205_1(void) = CatchByType[const String &] :
9991-
#-----| Exception -> Block 2
9991+
#-----| C++ Exception -> Block 2
99929992
#-----| Goto -> Block 9
99939993

99949994
# 1205| Block 9
@@ -16924,7 +16924,7 @@ ir.cpp:
1692416924
# 2281| v2281_6(void) = ^IndirectReadSideEffect[-1] : &:r2281_1, ~m2281_5
1692516925
# 2281| m2281_7(String) = ^IndirectMayWriteSideEffect[-1] : &:r2281_1
1692616926
# 2281| m2281_8(unknown) = Chi : total:m2281_5, partial:m2281_7
16927-
#-----| Exception -> Block 5
16927+
#-----| C++ Exception -> Block 5
1692816928

1692916929
# 2280| Block 4
1693016930
# 2280| r2280_1(glval<String>) = VariableAddress[s2] :
@@ -16956,7 +16956,7 @@ ir.cpp:
1695616956

1695716957
# 2282| Block 5
1695816958
# 2282| v2282_1(void) = CatchByType[const char *] :
16959-
#-----| Exception -> Block 7
16959+
#-----| C++ Exception -> Block 7
1696016960
#-----| Goto -> Block 6
1696116961

1696216962
# 2282| Block 6
@@ -16978,11 +16978,11 @@ ir.cpp:
1697816978
# 2283| m2283_11(String) = ^IndirectMayWriteSideEffect[-1] : &:r2283_1
1697916979
# 2283| m2283_12(unknown) = Chi : total:m2283_9, partial:m2283_11
1698016980
# 2283| v2283_13(void) = ThrowValue : &:r2283_1, ~m2283_12
16981-
#-----| Exception -> Block 2
16981+
#-----| C++ Exception -> Block 2
1698216982

1698316983
# 2285| Block 7
1698416984
# 2285| v2285_1(void) = CatchByType[const String &] :
16985-
#-----| Exception -> Block 9
16985+
#-----| C++ Exception -> Block 9
1698616986
#-----| Goto -> Block 8
1698716987

1698816988
# 2285| Block 8
@@ -16996,7 +16996,7 @@ ir.cpp:
1699616996
# 2287| Block 9
1699716997
# 2287| v2287_1(void) = CatchAny :
1699816998
# 2288| v2288_1(void) = ReThrow :
16999-
#-----| Exception -> Block 2
16999+
#-----| C++ Exception -> Block 2
1700017000

1700117001
# 2290| Block 10
1700217002
# 2290| m2290_1(unknown) = Phi : from 4:~m2281_24, from 8:~m2281_8
@@ -18266,7 +18266,7 @@ ir.cpp:
1826618266
# 2454| v2454_6(void) = ^IndirectReadSideEffect[-1] : &:r2454_1, m2452_8
1826718267
# 2454| m2454_7(ClassWithDestructor) = ^IndirectMayWriteSideEffect[-1] : &:r2454_1
1826818268
# 2454| m2454_8(ClassWithDestructor) = Chi : total:m2452_8, partial:m2454_7
18269-
#-----| Exception -> Block 1
18269+
#-----| C++ Exception -> Block 1
1827018270

1827118271
# 2451| Block 1
1827218272
# 2451| v2451_7(void) = Unwind :
@@ -18693,7 +18693,7 @@ ir.cpp:
1869318693
# 2537| r2537_2(int) = Constant[42] :
1869418694
# 2537| m2537_3(int) = Store[#throw2537:5] : &:r2537_1, r2537_2
1869518695
# 2537| v2537_4(void) = ThrowValue : &:r2537_1, m2537_3
18696-
#-----| Exception -> Block 2
18696+
#-----| C++ Exception -> Block 2
1869718697

1869818698
# 2534| Block 1
1869918699
# 2534| m2534_5(unknown) = Phi : from 3:~m2541_6, from 4:~m2541_14
@@ -18702,7 +18702,7 @@ ir.cpp:
1870218702

1870318703
# 2539| Block 2
1870418704
# 2539| v2539_1(void) = CatchByType[char] :
18705-
#-----| Exception -> Block 4
18705+
#-----| C++ Exception -> Block 4
1870618706
#-----| Goto -> Block 3
1870718707

1870818708
# 2539| Block 3
@@ -38962,7 +38962,7 @@ try_except.cpp:
3896238962
# 48| r48_2(int) = Constant[1] :
3896338963
# 48| m48_3(int) = Store[#throw48:13] : &:r48_1, r48_2
3896438964
# 48| v48_4(void) = ThrowValue : &:r48_1, m48_3
38965-
#-----| Exception -> Block 4
38965+
#-----| C++ Exception -> Block 4
3896638966

3896738967
# 51| Block 2
3896838968
# 51| r51_1(int) = Constant[0] :

0 commit comments

Comments
 (0)