Skip to content

Commit b94c4a6

Browse files
committed
C++: fix for destructor of while-loop condition
1 parent bac7e46 commit b94c4a6

File tree

4 files changed

+55
-19
lines changed

4 files changed

+55
-19
lines changed

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

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -805,12 +805,10 @@ class TranslatedIfStmt extends TranslatedStmt, ConditionContext {
805805
}
806806

807807
override Instruction getALastInstructionInternal() {
808-
result = this.getElse().getALastInstruction() or result = this.getThen().getALastInstruction() // FIXME: how do we handle the CFG merge here
808+
result = this.getElse().getALastInstruction() or result = this.getThen().getALastInstruction()
809809
}
810810

811-
override TranslatedElement getLastChild() {
812-
result = this.getElse() or result = this.getThen() // FIXME: how do we handle the CFG merge here
813-
}
811+
override TranslatedElement getLastChild() { result = this.getElse() or result = this.getThen() }
814812

815813
override TranslatedElement getChildInternal(int id) {
816814
id = 0 and result = this.getInitialization()
@@ -873,7 +871,7 @@ abstract class TranslatedLoop extends TranslatedStmt, ConditionContext {
873871
override Loop stmt;
874872

875873
override Instruction getALastInstructionInternal() {
876-
result = this.getCondition().getALastInstruction() // FIXME: how do we handle the branch here
874+
result = this.getCondition().getALastInstruction()
877875
}
878876

879877
override TranslatedElement getLastChild() { result = this.getCondition() }
@@ -919,13 +917,36 @@ abstract class TranslatedLoop extends TranslatedStmt, ConditionContext {
919917
class TranslatedWhileStmt extends TranslatedLoop {
920918
TranslatedWhileStmt() { stmt instanceof WhileStmt }
921919

920+
override TranslatedElement getChildInternal(int id) {
921+
id = 0 and result = this.getCondition()
922+
or
923+
id = 1 and result = this.getBody()
924+
or
925+
exists(int n |
926+
result.getAst() = stmt.getImplicitDestructorCall(n) and
927+
id = 2 + n
928+
)
929+
}
930+
931+
override predicate handlesDestructorsExplicitly() { any() }
932+
933+
final override Instruction getChildFalseSuccessor(TranslatedCondition child, EdgeKind kind) {
934+
child = this.getCondition() and
935+
if this.hasAnImplicitDestructorCall()
936+
then result = this.getChild(this.getFirstDestructorCallIndex()).getFirstInstruction(kind)
937+
else result = this.getParent().getChildSuccessor(this, kind)
938+
}
939+
922940
override Instruction getFirstInstruction(EdgeKind kind) {
923941
result = this.getFirstConditionInstruction(kind)
924942
}
925943

926944
override Instruction getChildSuccessorInternal(TranslatedElement child, EdgeKind kind) {
927945
child = this.getBody() and
928946
result = this.getFirstConditionInstruction(kind)
947+
or
948+
child = this.getChild(this.getFirstDestructorCallIndex()) and
949+
result = this.getParent().getChildSuccessor(this, kind)
929950
}
930951
}
931952

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

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13246,11 +13246,19 @@ ir.cpp:
1324613246
# 2204| m2204_8(Bool) = Chi : total:m2202_20, partial:m2204_7
1324713247
#-----| Goto (back edge) -> Block 4
1324813248

13249-
# 2206| Block 6
13250-
# 2206| v2206_1(void) = NoOp :
13251-
# 2193| v2193_7(void) = ReturnVoid :
13252-
# 2193| v2193_8(void) = AliasedUse : ~m2202_17
13253-
# 2193| v2193_9(void) = ExitFunction :
13249+
# 2204| Block 6
13250+
# 2204| r2204_9(glval<Bool>) = VariableAddress[B] :
13251+
# 2204| r2204_10(glval<unknown>) = FunctionAddress[~Bool] :
13252+
# 2204| v2204_11(void) = Call[~Bool] : func:r2204_10, this:r2204_9
13253+
# 2204| m2204_12(unknown) = ^CallSideEffect : ~m2202_17
13254+
# 2204| m2204_13(unknown) = Chi : total:m2202_17, partial:m2204_12
13255+
# 2204| v2204_14(void) = ^IndirectReadSideEffect[-1] : &:r2204_9, m2202_20
13256+
# 2204| m2204_15(Bool) = ^IndirectMayWriteSideEffect[-1] : &:r2204_9
13257+
# 2204| m2204_16(Bool) = Chi : total:m2202_20, partial:m2204_15
13258+
# 2206| v2206_1(void) = NoOp :
13259+
# 2193| v2193_7(void) = ReturnVoid :
13260+
# 2193| v2193_8(void) = AliasedUse : ~m2204_13
13261+
# 2193| v2193_9(void) = ExitFunction :
1325413262

1325513263
perf-regression.cpp:
1325613264
# 6| void Big::Big()

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10760,7 +10760,7 @@
1076010760
| ir.cpp:2182:5:2182:5 | SideEffect | ~m2182_9 |
1076110761
| ir.cpp:2193:6:2193:25 | ChiPartial | partial:m2193_3 |
1076210762
| ir.cpp:2193:6:2193:25 | ChiTotal | total:m2193_2 |
10763-
| ir.cpp:2193:6:2193:25 | SideEffect | ~m2202_17 |
10763+
| ir.cpp:2193:6:2193:25 | SideEffect | ~m2204_13 |
1076410764
| ir.cpp:2193:32:2193:32 | Address | &:r2193_5 |
1076510765
| ir.cpp:2195:16:2195:16 | Address | &:r2195_1 |
1076610766
| ir.cpp:2195:16:2195:16 | Address | &:r2195_1 |
@@ -10820,13 +10820,23 @@
1082010820
| ir.cpp:2203:17:2203:21 | StoreValue | r2203_1 |
1082110821
| ir.cpp:2204:9:2204:9 | Address | &:r2204_1 |
1082210822
| ir.cpp:2204:9:2204:9 | Address | &:r2204_1 |
10823+
| ir.cpp:2204:9:2204:9 | Address | &:r2204_9 |
10824+
| ir.cpp:2204:9:2204:9 | Address | &:r2204_9 |
1082310825
| ir.cpp:2204:9:2204:9 | Arg(this) | this:r2204_1 |
10826+
| ir.cpp:2204:9:2204:9 | Arg(this) | this:r2204_9 |
1082410827
| ir.cpp:2204:9:2204:9 | CallTarget | func:r2204_2 |
10828+
| ir.cpp:2204:9:2204:9 | CallTarget | func:r2204_10 |
1082510829
| ir.cpp:2204:9:2204:9 | ChiPartial | partial:m2204_4 |
1082610830
| ir.cpp:2204:9:2204:9 | ChiPartial | partial:m2204_7 |
10831+
| ir.cpp:2204:9:2204:9 | ChiPartial | partial:m2204_12 |
10832+
| ir.cpp:2204:9:2204:9 | ChiPartial | partial:m2204_15 |
1082710833
| ir.cpp:2204:9:2204:9 | ChiTotal | total:m2202_17 |
10834+
| ir.cpp:2204:9:2204:9 | ChiTotal | total:m2202_17 |
10835+
| ir.cpp:2204:9:2204:9 | ChiTotal | total:m2202_20 |
1082810836
| ir.cpp:2204:9:2204:9 | ChiTotal | total:m2202_20 |
1082910837
| ir.cpp:2204:9:2204:9 | SideEffect | m2202_20 |
10838+
| ir.cpp:2204:9:2204:9 | SideEffect | m2202_20 |
10839+
| ir.cpp:2204:9:2204:9 | SideEffect | ~m2202_17 |
1083010840
| ir.cpp:2204:9:2204:9 | SideEffect | ~m2202_17 |
1083110841
| perf-regression.cpp:6:3:6:5 | Address | &:r6_5 |
1083210842
| perf-regression.cpp:6:3:6:5 | Address | &:r6_5 |

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

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12304,7 +12304,7 @@ ir.cpp:
1230412304
# 2202| mu2202_14(Bool) = ^IndirectMayWriteSideEffect[-1] : &:r2202_9
1230512305
# 2202| r2202_15(bool) = CopyValue : r2202_11
1230612306
# 2202| v2202_16(void) = ConditionalBranch : r2202_15
12307-
#-----| False -> Block 7
12307+
#-----| False -> Block 6
1230812308
#-----| True -> Block 5
1230912309

1231012310
# 2203| Block 5
@@ -12326,13 +12326,10 @@ ir.cpp:
1232612326
# 2204| mu2204_10(unknown) = ^CallSideEffect : ~m?
1232712327
# 2204| v2204_11(void) = ^IndirectReadSideEffect[-1] : &:r2204_7, ~m?
1232812328
# 2204| mu2204_12(Bool) = ^IndirectMayWriteSideEffect[-1] : &:r2204_7
12329-
#-----| Goto -> Block 7
12330-
12331-
# 2206| Block 7
12332-
# 2206| v2206_1(void) = NoOp :
12333-
# 2193| v2193_6(void) = ReturnVoid :
12334-
# 2193| v2193_7(void) = AliasedUse : ~m?
12335-
# 2193| v2193_8(void) = ExitFunction :
12329+
# 2206| v2206_1(void) = NoOp :
12330+
# 2193| v2193_6(void) = ReturnVoid :
12331+
# 2193| v2193_7(void) = AliasedUse : ~m?
12332+
# 2193| v2193_8(void) = ExitFunction :
1233612333

1233712334
perf-regression.cpp:
1233812335
# 6| void Big::Big()

0 commit comments

Comments
 (0)