Skip to content

Commit 2308767

Browse files
authored
Merge pull request #1920 from AndreiDiaconu1/ircsharp-usingstmt
C# IR: using, checked, unchecked stmts
2 parents dd3fb6c + 99c6a32 commit 2308767

File tree

5 files changed

+359
-99
lines changed

5 files changed

+359
-99
lines changed

csharp/ql/src/semmle/code/csharp/ir/implementation/raw/internal/TranslatedElement.qll

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,10 @@ private predicate ignoreExprOnly(Expr expr) {
9090
or
9191
// Ignore the child expression of a goto case stmt
9292
expr.getParent() instanceof GotoCaseStmt
93+
or
94+
// Ignore the expression (that is not a declaration)
95+
// that appears in a using block
96+
expr.getParent().(UsingBlockStmt).getExpr() = expr
9397
}
9498

9599
/**
@@ -179,6 +183,7 @@ newtype TTranslatedElement =
179183
// expression.
180184
TTranslatedLoad(Expr expr) {
181185
// TODO: Revisit and make sure Loads are only used when needed
186+
not ignoreExpr(expr) and
182187
expr instanceof AssignableRead and
183188
not expr.getParent() instanceof ArrayAccess and
184189
not (

csharp/ql/src/semmle/code/csharp/ir/implementation/raw/internal/TranslatedStmt.qll

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -963,3 +963,114 @@ class TranslatedLockStmt extends TranslatedStmt {
963963
result = LockElements::getLockWasTakenDecl(stmt)
964964
}
965965
}
966+
967+
// TODO: Should be modeled using the desugaring framework for a
968+
// more exact translation.
969+
class TranslatedCheckedUncheckedStmt extends TranslatedStmt {
970+
TranslatedCheckedUncheckedStmt() {
971+
stmt instanceof CheckedStmt or
972+
stmt instanceof UncheckedStmt
973+
}
974+
975+
override TranslatedElement getChild(int id) { id = 0 and result = this.getBody() }
976+
977+
override Instruction getFirstInstruction() { result = this.getBody().getFirstInstruction() }
978+
979+
override Instruction getChildSuccessor(TranslatedElement child) {
980+
child = this.getBody() and
981+
result = this.getParent().getChildSuccessor(this)
982+
}
983+
984+
override predicate hasInstruction(
985+
Opcode opcode, InstructionTag tag, Type resultType, boolean isLValue
986+
) {
987+
none()
988+
}
989+
990+
override Instruction getInstructionSuccessor(InstructionTag tag, EdgeKind kind) { none() }
991+
992+
private TranslatedElement getBody() {
993+
result = getTranslatedStmt(stmt.(CheckedStmt).getBlock()) or
994+
result = getTranslatedStmt(stmt.(UncheckedStmt).getBlock())
995+
}
996+
}
997+
998+
// TODO: Should be modeled using the desugaring framework for a
999+
// more exact translation.
1000+
class TranslatedUsingBlockStmt extends TranslatedStmt {
1001+
override UsingBlockStmt stmt;
1002+
1003+
override TranslatedElement getChild(int id) {
1004+
result = getDecl(id)
1005+
or
1006+
id = getNumberOfDecls() and result = this.getBody()
1007+
}
1008+
1009+
override Instruction getFirstInstruction() {
1010+
if getNumberOfDecls() > 0
1011+
then result = this.getDecl(0).getFirstInstruction()
1012+
else result = this.getBody().getFirstInstruction()
1013+
}
1014+
1015+
override Instruction getChildSuccessor(TranslatedElement child) {
1016+
exists(int id |
1017+
child = this.getDecl(id) and
1018+
result = this.getDecl(id + 1).getFirstInstruction()
1019+
)
1020+
or
1021+
child = this.getDecl(this.getNumberOfDecls() - 1) and
1022+
result = this.getBody().getFirstInstruction()
1023+
or
1024+
child = this.getBody() and result = this.getParent().getChildSuccessor(this)
1025+
}
1026+
1027+
override predicate hasInstruction(
1028+
Opcode opcode, InstructionTag tag, Type resultType, boolean isLValue
1029+
) {
1030+
none()
1031+
}
1032+
1033+
override Instruction getInstructionSuccessor(InstructionTag tag, EdgeKind kind) { none() }
1034+
1035+
private TranslatedLocalDeclaration getDecl(int id) {
1036+
result = getTranslatedLocalDeclaration(stmt.getVariableDeclExpr(id))
1037+
}
1038+
1039+
private int getNumberOfDecls() { result = count(stmt.getAVariableDeclExpr()) }
1040+
1041+
private TranslatedStmt getBody() { result = getTranslatedStmt(stmt.getBody()) }
1042+
}
1043+
1044+
// TODO: Should be modeled using the desugaring framework for a
1045+
// more exact translation.
1046+
class TranslatedUsingDeclStmt extends TranslatedStmt {
1047+
override UsingDeclStmt stmt;
1048+
1049+
override TranslatedElement getChild(int id) { result = getDecl(id) }
1050+
1051+
override Instruction getFirstInstruction() { result = this.getDecl(0).getFirstInstruction() }
1052+
1053+
override Instruction getChildSuccessor(TranslatedElement child) {
1054+
exists(int id |
1055+
child = this.getDecl(id) and
1056+
id < this.noDecls() - 1 and
1057+
result = this.getDecl(id + 1).getFirstInstruction()
1058+
)
1059+
or
1060+
child = this.getDecl(this.noDecls() - 1) and result = this.getParent().getChildSuccessor(this)
1061+
}
1062+
1063+
override predicate hasInstruction(
1064+
Opcode opcode, InstructionTag tag, Type resultType, boolean isLValue
1065+
) {
1066+
none()
1067+
}
1068+
1069+
override Instruction getInstructionSuccessor(InstructionTag tag, EdgeKind kind) { none() }
1070+
1071+
private TranslatedLocalDeclaration getDecl(int id) {
1072+
result = getTranslatedLocalDeclaration(stmt.getVariableDeclExpr(id))
1073+
}
1074+
1075+
private int noDecls() { result = count(stmt.getAVariableDeclExpr()) }
1076+
}

0 commit comments

Comments
 (0)