From 2724075dec5a4853666009218fda3c9a7f5688eb Mon Sep 17 00:00:00 2001 From: AndreiDiaconu1 Date: Fri, 2 Aug 2019 14:41:33 +0100 Subject: [PATCH] Added stmts Added support for `ForStmt` and `DoWhileStmt` Added test cases --- .../raw/internal/TranslatedStmt.qll | 10 +-- .../test/library-tests/ir/ir/raw_ir.expected | 73 +++++++++++++++++++ csharp/ql/test/library-tests/ir/ir/stmts.cs | 15 ++++ 3 files changed, 93 insertions(+), 5 deletions(-) diff --git a/csharp/ql/src/semmle/code/csharp/ir/implementation/raw/internal/TranslatedStmt.qll b/csharp/ql/src/semmle/code/csharp/ir/implementation/raw/internal/TranslatedStmt.qll index 71dcb3bc2c3..f0d99079f9d 100644 --- a/csharp/ql/src/semmle/code/csharp/ir/implementation/raw/internal/TranslatedStmt.qll +++ b/csharp/ql/src/semmle/code/csharp/ir/implementation/raw/internal/TranslatedStmt.qll @@ -741,14 +741,14 @@ class TranslatedForStmt extends TranslatedLoop { override ForStmt stmt; override TranslatedElement getChild(int id) { - id = 0 and result = getInitialization() or + id = 0 and result = getDeclAndInit() or id = 1 and result = getCondition() or id = 2 and result = getUpdate() or id = 3 and result = getBody() } - private TranslatedStmt getInitialization() { - result = getTranslatedStmt(stmt.getAnInitializer().getEnclosingStmt()) + private TranslatedLocalDeclaration getDeclAndInit() { + result = getTranslatedLocalDeclaration(stmt.getAnInitializer()) } private predicate hasInitialization() { @@ -765,14 +765,14 @@ class TranslatedForStmt extends TranslatedLoop { override Instruction getFirstInstruction() { if hasInitialization() then - result = getInitialization().getFirstInstruction() + result = getDeclAndInit().getFirstInstruction() else result = getFirstConditionInstruction() } override Instruction getChildSuccessor(TranslatedElement child) { ( - child = getInitialization() and + child = getDeclAndInit() and result = getFirstConditionInstruction() ) or ( diff --git a/csharp/ql/test/library-tests/ir/ir/raw_ir.expected b/csharp/ql/test/library-tests/ir/ir/raw_ir.expected index 8ba89b36c0f..d0ab78654a5 100644 --- a/csharp/ql/test/library-tests/ir/ir/raw_ir.expected +++ b/csharp/ql/test/library-tests/ir/ir/raw_ir.expected @@ -532,6 +532,79 @@ stmts.cs: # 53| v8_1(Void) = ReThrow : #-----| Exception -> Block 2 +# 61| test_stmts.forStmt() +# 61| Block 0 +# 61| v0_0(Void) = EnterFunction : +# 61| mu0_1(Object) = AliasedDefinition : +# 61| mu0_2(Object) = UnmodeledDefinition : +# 61| r0_3(lval) = InitializeThis : +# 62| r0_4(lval) = VariableAddress[x] : +# 62| r0_5(Int32) = Constant[0] : +# 62| mu0_6(Int32) = Store : &:r0_4, r0_5 +# 63| r0_7(lval) = VariableAddress[i] : +# 63| r0_8(Int32) = Constant[0] : +# 63| mu0_9(Int32) = Store : &:r0_7, r0_8 +#-----| Goto -> Block 2 + +# 61| Block 1 +# 61| v1_0(Void) = ReturnVoid : +# 61| v1_1(Void) = UnmodeledUse : mu* +# 61| v1_2(Void) = ExitFunction : + +# 63| Block 2 +# 63| r2_0(lval) = VariableAddress[i] : +# 63| r2_1(Int32) = Load : &:r2_0, ~mu0_2 +# 63| r2_2(Int32) = Constant[10] : +# 63| r2_3(Boolean) = CompareLE : r2_1, r2_2 +# 63| v2_4(Void) = ConditionalBranch : r2_3 +#-----| False -> Block 1 +#-----| True -> Block 3 + +# 64| Block 3 +# 64| r3_0(lval) = VariableAddress[x] : +# 64| r3_1(Int32) = Load : &:r3_0, ~mu0_2 +# 64| r3_2(Int32) = Constant[1] : +# 64| r3_3(Int32) = Sub : r3_1, r3_2 +# 64| r3_4(lval) = VariableAddress[x] : +# 64| mu3_5(Int32) = Store : &:r3_4, r3_3 +# 63| r3_6(lval) = VariableAddress[i] : +# 63| r3_7(Int32) = Load : &:r3_6, ~mu0_2 +# 63| r3_8(Int32) = Constant[1] : +# 63| r3_9(Int32) = Add : r3_7, r3_8 +# 63| mu3_10(Int32) = Store : &:r3_6, r3_9 +#-----| Goto (back edge) -> Block 2 + +# 68| test_stmts.doWhile() +# 68| Block 0 +# 68| v0_0(Void) = EnterFunction : +# 68| mu0_1(Object) = AliasedDefinition : +# 68| mu0_2(Object) = UnmodeledDefinition : +# 68| r0_3(lval) = InitializeThis : +# 69| r0_4(lval) = VariableAddress[x] : +# 69| r0_5(Int32) = Constant[0] : +# 69| mu0_6(Int32) = Store : &:r0_4, r0_5 +#-----| Goto -> Block 2 + +# 68| Block 1 +# 68| v1_0(Void) = ReturnVoid : +# 68| v1_1(Void) = UnmodeledUse : mu* +# 68| v1_2(Void) = ExitFunction : + +# 71| Block 2 +# 71| r2_0(lval) = VariableAddress[x] : +# 71| r2_1(Int32) = Load : &:r2_0, ~mu0_2 +# 71| r2_2(Int32) = Constant[1] : +# 71| r2_3(Int32) = Add : r2_1, r2_2 +# 71| r2_4(lval) = VariableAddress[x] : +# 71| mu2_5(Int32) = Store : &:r2_4, r2_3 +# 73| r2_6(lval) = VariableAddress[x] : +# 73| r2_7(Int32) = Load : &:r2_6, ~mu0_2 +# 73| r2_8(Int32) = Constant[10] : +# 73| r2_9(Boolean) = CompareLT : r2_7, r2_8 +# 73| v2_10(Void) = ConditionalBranch : r2_9 +#-----| False -> Block 1 +#-----| True (back edge) -> Block 2 + variables.cs: # 4| f # 4| Block 0 diff --git a/csharp/ql/test/library-tests/ir/ir/stmts.cs b/csharp/ql/test/library-tests/ir/ir/stmts.cs index 463bc84b9a6..370d449c91a 100644 --- a/csharp/ql/test/library-tests/ir/ir/stmts.cs +++ b/csharp/ql/test/library-tests/ir/ir/stmts.cs @@ -58,4 +58,19 @@ public class test_stmts { } } + public static void forStmt() { + int x = 0; + for (int i = 0; i <= 10; i++) { + x = x - 1; + } + } + + public static void doWhile() { + int x = 0; + do { + x = x + 1; + } + while (x < 10); + } + }