Skip to content

Commit 721f329

Browse files
committed
Consistency
1 parent 069a110 commit 721f329

File tree

3 files changed

+65
-2
lines changed

3 files changed

+65
-2
lines changed

csharp/ql/consistency-queries/SsaConsistency.ql

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import csharp
2-
import semmle.code.csharp.dataflow.internal.SsaImpl::Consistency
2+
import semmle.code.csharp.dataflow.internal.SsaImpl as Impl
3+
import Impl::Consistency
34
import Ssa
45

56
class MyRelevantDefinition extends RelevantDefinition, Ssa::Definition {
@@ -10,6 +11,14 @@ class MyRelevantDefinition extends RelevantDefinition, Ssa::Definition {
1011
}
1112
}
1213

14+
class MyRelevantDefinitionExt extends RelevantDefinitionExt, Impl::DefinitionExt {
15+
override predicate hasLocationInfo(
16+
string filepath, int startline, int startcolumn, int endline, int endcolumn
17+
) {
18+
this.getLocation().hasLocationInfo(filepath, startline, startcolumn, endline, endcolumn)
19+
}
20+
}
21+
1322
query predicate localDeclWithSsaDef(LocalVariableDeclExpr d) {
1423
// Local variables in C# must be initialized before every use, so uninitialized
1524
// local variables should not have an SSA definition, as that would imply that
Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import codeql.ruby.dataflow.SSA
2-
import codeql.ruby.dataflow.internal.SsaImpl::Consistency
2+
import codeql.ruby.dataflow.internal.SsaImpl
3+
import Consistency
34

45
class MyRelevantDefinition extends RelevantDefinition, Ssa::Definition {
56
override predicate hasLocationInfo(
@@ -8,3 +9,11 @@ class MyRelevantDefinition extends RelevantDefinition, Ssa::Definition {
89
this.getLocation().hasLocationInfo(filepath, startline, startcolumn, endline, endcolumn)
910
}
1011
}
12+
13+
class MyRelevantDefinitionExt extends RelevantDefinitionExt, DefinitionExt {
14+
override predicate hasLocationInfo(
15+
string filepath, int startline, int startcolumn, int endline, int endcolumn
16+
) {
17+
this.getLocation().hasLocationInfo(filepath, startline, startcolumn, endline, endcolumn)
18+
}
19+
}

shared/ssa/codeql/ssa/Ssa.qll

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1055,18 +1055,40 @@ module Make<InputSig Input> {
10551055
);
10561056
}
10571057

1058+
/** A definition that is relevant for the consistency queries. */
1059+
abstract class RelevantDefinitionExt extends DefinitionExt {
1060+
/** Override this predicate to ensure locations in consistency results. */
1061+
abstract predicate hasLocationInfo(
1062+
string filepath, int startline, int startcolumn, int endline, int endcolumn
1063+
);
1064+
}
1065+
10581066
/** Holds if a read can be reached from multiple definitions. */
10591067
query predicate nonUniqueDef(RelevantDefinition def, SourceVariable v, BasicBlock bb, int i) {
10601068
ssaDefReachesRead(v, def, bb, i) and
10611069
not exists(unique(Definition def0 | ssaDefReachesRead(v, def0, bb, i)))
10621070
}
10631071

1072+
/** Holds if a read can be reached from multiple definitions. */
1073+
query predicate nonUniqueDefExt(
1074+
RelevantDefinitionExt def, SourceVariable v, BasicBlock bb, int i
1075+
) {
1076+
ssaDefReachesReadExt(v, def, bb, i) and
1077+
not exists(unique(DefinitionExt def0 | ssaDefReachesReadExt(v, def0, bb, i)))
1078+
}
1079+
10641080
/** Holds if a read cannot be reached from a definition. */
10651081
query predicate readWithoutDef(SourceVariable v, BasicBlock bb, int i) {
10661082
variableRead(bb, i, v, _) and
10671083
not ssaDefReachesRead(v, _, bb, i)
10681084
}
10691085

1086+
/** Holds if a read cannot be reached from a definition. */
1087+
query predicate readWithoutDefExt(SourceVariable v, BasicBlock bb, int i) {
1088+
variableRead(bb, i, v, _) and
1089+
not ssaDefReachesReadExt(v, _, bb, i)
1090+
}
1091+
10701092
/** Holds if a definition cannot reach a read. */
10711093
query predicate deadDef(RelevantDefinition def, SourceVariable v) {
10721094
v = def.getSourceVariable() and
@@ -1075,6 +1097,15 @@ module Make<InputSig Input> {
10751097
not uncertainWriteDefinitionInput(_, def)
10761098
}
10771099

1100+
/** Holds if a definition cannot reach a read. */
1101+
query predicate deadDefExt(RelevantDefinitionExt def, SourceVariable v) {
1102+
v = def.getSourceVariable() and
1103+
not ssaDefReachesReadExt(_, def, _, _) and
1104+
not phiHasInputFromBlock(_, def, _) and
1105+
not phiReadHasInputFromBlock(_, def, _) and
1106+
not uncertainWriteDefinitionInput(_, def)
1107+
}
1108+
10781109
/** Holds if a read is not dominated by a definition. */
10791110
query predicate notDominatedByDef(RelevantDefinition def, SourceVariable v, BasicBlock bb, int i) {
10801111
exists(BasicBlock bbDef, int iDef | def.definesAt(v, bbDef, iDef) |
@@ -1086,5 +1117,19 @@ module Make<InputSig Input> {
10861117
not def.definesAt(v, getImmediateBasicBlockDominator*(bb), _)
10871118
)
10881119
}
1120+
1121+
/** Holds if a read is not dominated by a definition. */
1122+
query predicate notDominatedByDefExt(
1123+
RelevantDefinitionExt def, SourceVariable v, BasicBlock bb, int i
1124+
) {
1125+
exists(BasicBlock bbDef, int iDef | def.definesAt(v, bbDef, iDef, _) |
1126+
ssaDefReachesReadWithinBlock(v, def, bb, i) and
1127+
(bb != bbDef or i < iDef)
1128+
or
1129+
ssaDefReachesReadExt(v, def, bb, i) and
1130+
not ssaDefReachesReadWithinBlock(v, def, bb, i) and
1131+
not def.definesAt(v, getImmediateBasicBlockDominator*(bb), _, _)
1132+
)
1133+
}
10891134
}
10901135
}

0 commit comments

Comments
 (0)