Skip to content

Commit 2b2ff77

Browse files
authored
Merge pull request #7179 from hvitved/ruby/shared-ssa-consistency
Ruby: Move SSA consistency queries into shared SSA library
2 parents 1f3f7e9 + 34feafd commit 2b2ff77

File tree

8 files changed

+159
-20
lines changed

8 files changed

+159
-20
lines changed

cpp/ql/lib/semmle/code/cpp/ir/dataflow/internal/SsaImplCommon.qll

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -634,3 +634,28 @@ class UncertainWriteDefinition extends WriteDefinition {
634634
)
635635
}
636636
}
637+
638+
/** Provides a set of consistency queries. */
639+
module Consistency {
640+
abstract class RelevantDefinition extends Definition {
641+
abstract predicate hasLocationInfo(
642+
string filepath, int startline, int startcolumn, int endline, int endcolumn
643+
);
644+
}
645+
646+
query predicate nonUniqueDef(RelevantDefinition def, SourceVariable v, BasicBlock bb, int i) {
647+
ssaDefReachesRead(v, def, bb, i) and
648+
not exists(unique(Definition def0 | ssaDefReachesRead(_, def0, bb, i)))
649+
}
650+
651+
query predicate readWithoutDef(SourceVariable v, BasicBlock bb, int i) {
652+
variableRead(bb, i, v, _) and
653+
not ssaDefReachesRead(_, _, bb, i)
654+
}
655+
656+
query predicate deadDef(RelevantDefinition def, SourceVariable v) {
657+
v = def.getSourceVariable() and
658+
not ssaDefReachesRead(_, def, _, _) and
659+
not phiHasInputFromBlock(_, def, _)
660+
}
661+
}

csharp/ql/lib/semmle/code/cil/internal/SsaImplCommon.qll

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -634,3 +634,28 @@ class UncertainWriteDefinition extends WriteDefinition {
634634
)
635635
}
636636
}
637+
638+
/** Provides a set of consistency queries. */
639+
module Consistency {
640+
abstract class RelevantDefinition extends Definition {
641+
abstract predicate hasLocationInfo(
642+
string filepath, int startline, int startcolumn, int endline, int endcolumn
643+
);
644+
}
645+
646+
query predicate nonUniqueDef(RelevantDefinition def, SourceVariable v, BasicBlock bb, int i) {
647+
ssaDefReachesRead(v, def, bb, i) and
648+
not exists(unique(Definition def0 | ssaDefReachesRead(_, def0, bb, i)))
649+
}
650+
651+
query predicate readWithoutDef(SourceVariable v, BasicBlock bb, int i) {
652+
variableRead(bb, i, v, _) and
653+
not ssaDefReachesRead(_, _, bb, i)
654+
}
655+
656+
query predicate deadDef(RelevantDefinition def, SourceVariable v) {
657+
v = def.getSourceVariable() and
658+
not ssaDefReachesRead(_, def, _, _) and
659+
not phiHasInputFromBlock(_, def, _)
660+
}
661+
}

csharp/ql/lib/semmle/code/csharp/controlflow/internal/pressa/SsaImplCommon.qll

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -634,3 +634,28 @@ class UncertainWriteDefinition extends WriteDefinition {
634634
)
635635
}
636636
}
637+
638+
/** Provides a set of consistency queries. */
639+
module Consistency {
640+
abstract class RelevantDefinition extends Definition {
641+
abstract predicate hasLocationInfo(
642+
string filepath, int startline, int startcolumn, int endline, int endcolumn
643+
);
644+
}
645+
646+
query predicate nonUniqueDef(RelevantDefinition def, SourceVariable v, BasicBlock bb, int i) {
647+
ssaDefReachesRead(v, def, bb, i) and
648+
not exists(unique(Definition def0 | ssaDefReachesRead(_, def0, bb, i)))
649+
}
650+
651+
query predicate readWithoutDef(SourceVariable v, BasicBlock bb, int i) {
652+
variableRead(bb, i, v, _) and
653+
not ssaDefReachesRead(_, _, bb, i)
654+
}
655+
656+
query predicate deadDef(RelevantDefinition def, SourceVariable v) {
657+
v = def.getSourceVariable() and
658+
not ssaDefReachesRead(_, def, _, _) and
659+
not phiHasInputFromBlock(_, def, _)
660+
}
661+
}

csharp/ql/lib/semmle/code/csharp/dataflow/internal/SsaImplCommon.qll

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -634,3 +634,28 @@ class UncertainWriteDefinition extends WriteDefinition {
634634
)
635635
}
636636
}
637+
638+
/** Provides a set of consistency queries. */
639+
module Consistency {
640+
abstract class RelevantDefinition extends Definition {
641+
abstract predicate hasLocationInfo(
642+
string filepath, int startline, int startcolumn, int endline, int endcolumn
643+
);
644+
}
645+
646+
query predicate nonUniqueDef(RelevantDefinition def, SourceVariable v, BasicBlock bb, int i) {
647+
ssaDefReachesRead(v, def, bb, i) and
648+
not exists(unique(Definition def0 | ssaDefReachesRead(_, def0, bb, i)))
649+
}
650+
651+
query predicate readWithoutDef(SourceVariable v, BasicBlock bb, int i) {
652+
variableRead(bb, i, v, _) and
653+
not ssaDefReachesRead(_, _, bb, i)
654+
}
655+
656+
query predicate deadDef(RelevantDefinition def, SourceVariable v) {
657+
v = def.getSourceVariable() and
658+
not ssaDefReachesRead(_, def, _, _) and
659+
not phiHasInputFromBlock(_, def, _)
660+
}
661+
}

csharp/ql/lib/semmle/code/csharp/dataflow/internal/SsaImplSpecific.qll

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
private import csharp
44
private import AssignableDefinitions
55
private import SsaImpl as SsaImpl
6+
private import semmle.code.csharp.dataflow.SSA
67

78
class BasicBlock = ControlFlow::BasicBlock;
89

@@ -12,7 +13,7 @@ BasicBlock getABasicBlockSuccessor(BasicBlock bb) { result = bb.getASuccessor()
1213

1314
class ExitBasicBlock = ControlFlow::BasicBlocks::ExitBlock;
1415

15-
class SourceVariable = SsaImpl::TSourceVariable;
16+
class SourceVariable = Ssa::SourceVariable;
1617

1718
predicate variableWrite = SsaImpl::variableWrite/4;
1819

csharp/ql/lib/semmle/code/csharp/dataflow/internal/basessa/SsaImplCommon.qll

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -634,3 +634,28 @@ class UncertainWriteDefinition extends WriteDefinition {
634634
)
635635
}
636636
}
637+
638+
/** Provides a set of consistency queries. */
639+
module Consistency {
640+
abstract class RelevantDefinition extends Definition {
641+
abstract predicate hasLocationInfo(
642+
string filepath, int startline, int startcolumn, int endline, int endcolumn
643+
);
644+
}
645+
646+
query predicate nonUniqueDef(RelevantDefinition def, SourceVariable v, BasicBlock bb, int i) {
647+
ssaDefReachesRead(v, def, bb, i) and
648+
not exists(unique(Definition def0 | ssaDefReachesRead(_, def0, bb, i)))
649+
}
650+
651+
query predicate readWithoutDef(SourceVariable v, BasicBlock bb, int i) {
652+
variableRead(bb, i, v, _) and
653+
not ssaDefReachesRead(_, _, bb, i)
654+
}
655+
656+
query predicate deadDef(RelevantDefinition def, SourceVariable v) {
657+
v = def.getSourceVariable() and
658+
not ssaDefReachesRead(_, def, _, _) and
659+
not phiHasInputFromBlock(_, def, _)
660+
}
661+
}
Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,10 @@
1-
import ruby
21
import codeql.ruby.dataflow.SSA
3-
import codeql.ruby.controlflow.ControlFlowGraph
2+
import codeql.ruby.dataflow.internal.SsaImplCommon::Consistency
43

5-
query predicate nonUniqueDef(CfgNode read, Ssa::Definition def) {
6-
read = def.getARead() and
7-
exists(Ssa::Definition other | read = other.getARead() and other != def)
8-
}
9-
10-
query predicate readWithoutDef(LocalVariableReadAccess read) {
11-
exists(CfgNode node |
12-
node = read.getAControlFlowNode() and
13-
not node = any(Ssa::Definition def).getARead()
14-
)
15-
}
16-
17-
query predicate deadDef(Ssa::Definition def, LocalVariable v) {
18-
v = def.getSourceVariable() and
19-
not v.isCaptured() and
20-
not exists(def.getARead()) and
21-
not def = any(Ssa::PhiNode phi).getAnInput()
4+
class MyRelevantDefinition extends RelevantDefinition, Ssa::Definition {
5+
override predicate hasLocationInfo(
6+
string filepath, int startline, int startcolumn, int endline, int endcolumn
7+
) {
8+
this.getLocation().hasLocationInfo(filepath, startline, startcolumn, endline, endcolumn)
9+
}
2210
}

ruby/ql/lib/codeql/ruby/dataflow/internal/SsaImplCommon.qll

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -634,3 +634,28 @@ class UncertainWriteDefinition extends WriteDefinition {
634634
)
635635
}
636636
}
637+
638+
/** Provides a set of consistency queries. */
639+
module Consistency {
640+
abstract class RelevantDefinition extends Definition {
641+
abstract predicate hasLocationInfo(
642+
string filepath, int startline, int startcolumn, int endline, int endcolumn
643+
);
644+
}
645+
646+
query predicate nonUniqueDef(RelevantDefinition def, SourceVariable v, BasicBlock bb, int i) {
647+
ssaDefReachesRead(v, def, bb, i) and
648+
not exists(unique(Definition def0 | ssaDefReachesRead(_, def0, bb, i)))
649+
}
650+
651+
query predicate readWithoutDef(SourceVariable v, BasicBlock bb, int i) {
652+
variableRead(bb, i, v, _) and
653+
not ssaDefReachesRead(_, _, bb, i)
654+
}
655+
656+
query predicate deadDef(RelevantDefinition def, SourceVariable v) {
657+
v = def.getSourceVariable() and
658+
not ssaDefReachesRead(_, def, _, _) and
659+
not phiHasInputFromBlock(_, def, _)
660+
}
661+
}

0 commit comments

Comments
 (0)