Skip to content

Commit f623ea0

Browse files
authored
Merge pull request github#10576 from hvitved/ssa/consistency-queries
SSA: Turn consistency predicates into `query` predicates
2 parents a1edd65 + 4edef87 commit f623ea0

File tree

3 files changed

+16
-28
lines changed

3 files changed

+16
-28
lines changed

csharp/ql/consistency-queries/SsaConsistency.ql

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,15 @@
11
import csharp
2-
import semmle.code.csharp.dataflow.internal.SsaImpl::Consistency as Consistency
2+
import semmle.code.csharp.dataflow.internal.SsaImpl::Consistency
33
import Ssa
44

5-
class MyRelevantDefinition extends Consistency::RelevantDefinition, Ssa::Definition {
5+
class MyRelevantDefinition extends RelevantDefinition, Ssa::Definition {
66
override predicate hasLocationInfo(
77
string filepath, int startline, int startcolumn, int endline, int endcolumn
88
) {
99
this.getLocation().hasLocationInfo(filepath, startline, startcolumn, endline, endcolumn)
1010
}
1111
}
1212

13-
query predicate nonUniqueDef = Consistency::nonUniqueDef/4;
14-
15-
query predicate readWithoutDef = Consistency::readWithoutDef/3;
16-
17-
query predicate deadDef = Consistency::deadDef/2;
18-
19-
query predicate notDominatedByDef = Consistency::notDominatedByDef/4;
20-
2113
query predicate localDeclWithSsaDef(LocalVariableDeclExpr d) {
2214
// Local variables in C# must be initialized before every use, so uninitialized
2315
// local variables should not have an SSA definition, as that would imply that
Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,10 @@
11
import codeql.ruby.dataflow.SSA
2-
import codeql.ruby.dataflow.internal.SsaImpl::Consistency as Consistency
2+
import codeql.ruby.dataflow.internal.SsaImpl::Consistency
33

4-
class MyRelevantDefinition extends Consistency::RelevantDefinition, Ssa::Definition {
4+
class MyRelevantDefinition extends RelevantDefinition, Ssa::Definition {
55
override predicate hasLocationInfo(
66
string filepath, int startline, int startcolumn, int endline, int endcolumn
77
) {
88
this.getLocation().hasLocationInfo(filepath, startline, startcolumn, endline, endcolumn)
99
}
1010
}
11-
12-
query predicate nonUniqueDef = Consistency::nonUniqueDef/4;
13-
14-
query predicate readWithoutDef = Consistency::readWithoutDef/3;
15-
16-
query predicate deadDef = Consistency::deadDef/2;
17-
18-
query predicate notDominatedByDef = Consistency::notDominatedByDef/4;

shared/ssa/codeql/ssa/Ssa.qll

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@ signature module InputSig {
99
* A basic block, that is, a maximal straight-line sequence of control flow nodes
1010
* without branches or joins.
1111
*/
12-
class BasicBlock;
12+
class BasicBlock {
13+
/** Gets a textual representation of this basic block. */
14+
string toString();
15+
}
1316

1417
/**
1518
* Gets the basic block that immediately dominates basic block `bb`, if any.
@@ -43,7 +46,10 @@ signature module InputSig {
4346
class ExitBasicBlock extends BasicBlock;
4447

4548
/** A variable that can be SSA converted. */
46-
class SourceVariable;
49+
class SourceVariable {
50+
/** Gets a textual representation of this variable. */
51+
string toString();
52+
}
4753

4854
/**
4955
* Holds if the `i`th node of basic block `bb` is a (potential) write to source
@@ -844,8 +850,6 @@ module Make<InputSig Input> {
844850
}
845851

846852
/** Provides a set of consistency queries. */
847-
// TODO: Make these `query` predicates once class signatures are supported
848-
// (`SourceVariable` and `BasicBlock` must have `toString`)
849853
module Consistency {
850854
/** A definition that is relevant for the consistency queries. */
851855
abstract class RelevantDefinition extends Definition {
@@ -856,27 +860,27 @@ module Make<InputSig Input> {
856860
}
857861

858862
/** Holds if a read can be reached from multiple definitions. */
859-
predicate nonUniqueDef(RelevantDefinition def, SourceVariable v, BasicBlock bb, int i) {
863+
query predicate nonUniqueDef(RelevantDefinition def, SourceVariable v, BasicBlock bb, int i) {
860864
ssaDefReachesRead(v, def, bb, i) and
861865
not exists(unique(Definition def0 | ssaDefReachesRead(v, def0, bb, i)))
862866
}
863867

864868
/** Holds if a read cannot be reached from a definition. */
865-
predicate readWithoutDef(SourceVariable v, BasicBlock bb, int i) {
869+
query predicate readWithoutDef(SourceVariable v, BasicBlock bb, int i) {
866870
variableRead(bb, i, v, _) and
867871
not ssaDefReachesRead(v, _, bb, i)
868872
}
869873

870874
/** Holds if a definition cannot reach a read. */
871-
predicate deadDef(RelevantDefinition def, SourceVariable v) {
875+
query predicate deadDef(RelevantDefinition def, SourceVariable v) {
872876
v = def.getSourceVariable() and
873877
not ssaDefReachesRead(_, def, _, _) and
874878
not phiHasInputFromBlock(_, def, _) and
875879
not uncertainWriteDefinitionInput(_, def)
876880
}
877881

878882
/** Holds if a read is not dominated by a definition. */
879-
predicate notDominatedByDef(RelevantDefinition def, SourceVariable v, BasicBlock bb, int i) {
883+
query predicate notDominatedByDef(RelevantDefinition def, SourceVariable v, BasicBlock bb, int i) {
880884
exists(BasicBlock bbDef, int iDef | def.definesAt(v, bbDef, iDef) |
881885
ssaDefReachesReadWithinBlock(v, def, bb, i) and
882886
(bb != bbDef or i < iDef)

0 commit comments

Comments
 (0)