Skip to content

Commit 866fc6b

Browse files
authored
Merge pull request #19235 from geoffw0/ssaconsistency
Rust: SSA inconsistency counts
2 parents 8c878cd + fd3dcb2 commit 866fc6b

File tree

10 files changed

+98
-0
lines changed

10 files changed

+98
-0
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
/**
2+
* @name Static single assignment inconsistencies
3+
* @description Lists the static single assignment inconsistencies in the database. This query is intended for internal use.
4+
* @kind table
5+
* @id rust/diagnostics/ssa-consistency
6+
*/
7+
18
import codeql.rust.dataflow.Ssa
29
import codeql.rust.dataflow.internal.SsaImpl
310
import Consistency

rust/ql/integration-tests/hello-project/summary.expected

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
| Inconsistencies - AST | 0 |
88
| Inconsistencies - CFG | 0 |
99
| Inconsistencies - Path resolution | 0 |
10+
| Inconsistencies - SSA | 0 |
1011
| Inconsistencies - data flow | 0 |
1112
| Lines of code extracted | 6 |
1213
| Lines of user code extracted | 6 |

rust/ql/integration-tests/hello-workspace/summary.cargo.expected

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
| Inconsistencies - AST | 0 |
88
| Inconsistencies - CFG | 0 |
99
| Inconsistencies - Path resolution | 0 |
10+
| Inconsistencies - SSA | 0 |
1011
| Inconsistencies - data flow | 0 |
1112
| Lines of code extracted | 9 |
1213
| Lines of user code extracted | 9 |

rust/ql/integration-tests/hello-workspace/summary.rust-project.expected

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
| Inconsistencies - AST | 0 |
88
| Inconsistencies - CFG | 0 |
99
| Inconsistencies - Path resolution | 0 |
10+
| Inconsistencies - SSA | 0 |
1011
| Inconsistencies - data flow | 0 |
1112
| Lines of code extracted | 9 |
1213
| Lines of user code extracted | 9 |
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/**
2+
* @name Static single assignment inconsistency counts
3+
* @description Counts the number of static single assignment inconsistencies of each type. This query is intended for internal use.
4+
* @kind diagnostic
5+
* @id rust/diagnostics/ssa-consistency-counts
6+
*/
7+
8+
private import codeql.rust.dataflow.internal.SsaImpl::Consistency as SsaConsistency
9+
10+
// see also `rust/diagnostics/ssa-consistency`, which lists the
11+
// individual inconsistency results.
12+
from string type, int num
13+
where num = SsaConsistency::getInconsistencyCounts(type)
14+
select type, num

rust/ql/src/queries/summary/Stats.qll

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ private import codeql.rust.internal.AstConsistency as AstConsistency
1010
private import codeql.rust.internal.PathResolutionConsistency as PathResolutionConsistency
1111
private import codeql.rust.controlflow.internal.CfgConsistency as CfgConsistency
1212
private import codeql.rust.dataflow.internal.DataFlowConsistency as DataFlowConsistency
13+
private import codeql.rust.dataflow.internal.SsaImpl::Consistency as SsaConsistency
1314
private import codeql.rust.Concepts
1415
private import codeql.rust.Diagnostics
1516
private import codeql.rust.security.SensitiveData
@@ -57,6 +58,13 @@ int getTotalCfgInconsistencies() {
5758
result = sum(string type | | CfgConsistency::getCfgInconsistencyCounts(type))
5859
}
5960

61+
/**
62+
* Gets a count of the total number of SSA inconsistencies in the database.
63+
*/
64+
int getTotalSsaInconsistencies() {
65+
result = sum(string type | | SsaConsistency::getInconsistencyCounts(type))
66+
}
67+
6068
/**
6169
* Gets a count of the total number of data flow inconsistencies in the database.
6270
*/
@@ -142,6 +150,8 @@ predicate inconsistencyStats(string key, int value) {
142150
or
143151
key = "Inconsistencies - CFG" and value = getTotalCfgInconsistencies()
144152
or
153+
key = "Inconsistencies - SSA" and value = getTotalSsaInconsistencies()
154+
or
145155
key = "Inconsistencies - data flow" and value = getTotalDataFlowInconsistencies()
146156
}
147157

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
| Definition cannot reach a read | 0 |
2+
| End of a basic block can be reached by multiple definitions | 0 |
3+
| Phi has less than 2 immediately prior references | 0 |
4+
| Phi node has less than two inputs | 0 |
5+
| Phi read has less than 2 immediately prior references | 0 |
6+
| Read can be reached from multiple definitions | 0 |
7+
| Read cannot be reached from a definition | 0 |
8+
| Read does not have a prior reference | 0 |
9+
| Read has multiple prior references | 0 |
10+
| Read is not dominated by a definition | 0 |
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
queries/diagnostics/SsaConsistencyCounts.ql

rust/ql/test/query-tests/diagnostics/SummaryStatsReduced.expected

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
| Inconsistencies - AST | 0 |
88
| Inconsistencies - CFG | 0 |
99
| Inconsistencies - Path resolution | 0 |
10+
| Inconsistencies - SSA | 0 |
1011
| Inconsistencies - data flow | 0 |
1112
| Lines of code extracted | 60 |
1213
| Lines of user code extracted | 60 |

shared/ssa/codeql/ssa/Ssa.qll

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1468,6 +1468,58 @@ module Make<LocationSig Location, InputSig<Location> Input> {
14681468
inputRefs = count(BasicBlock bb, int i | AdjacentSsaRefs::adjacentRefPhi(bb, i, _, bbPhi, v)) and
14691469
inputRefs < 2
14701470
}
1471+
1472+
/**
1473+
* Gets counts of inconsistencies of each type.
1474+
*/
1475+
int getInconsistencyCounts(string type) {
1476+
// total results from all the SSA consistency query predicates.
1477+
type = "Read can be reached from multiple definitions" and
1478+
result =
1479+
count(Definition def, SourceVariable v, BasicBlock bb, int i | nonUniqueDef(def, v, bb, i))
1480+
or
1481+
type = "Read cannot be reached from a definition" and
1482+
result = count(SourceVariable v, BasicBlock bb, int i | readWithoutDef(v, bb, i))
1483+
or
1484+
type = "Definition cannot reach a read" and
1485+
result = count(Definition def, SourceVariable v | deadDef(def, v))
1486+
or
1487+
type = "Read is not dominated by a definition" and
1488+
result =
1489+
count(Definition def, SourceVariable v, BasicBlock bb, int i |
1490+
notDominatedByDef(def, v, bb, i)
1491+
)
1492+
or
1493+
type = "End of a basic block can be reached by multiple definitions" and
1494+
result =
1495+
count(Definition def, SourceVariable v, BasicBlock bb |
1496+
nonUniqueDefReachesEndOfBlock(def, v, bb)
1497+
)
1498+
or
1499+
type = "Phi node has less than two inputs" and
1500+
result = count(PhiNode phi, int inputs | uselessPhiNode(phi, inputs))
1501+
or
1502+
type = "Read does not have a prior reference" and
1503+
result = count(SourceVariable v, BasicBlock bb, int i | readWithoutPriorRef(v, bb, i))
1504+
or
1505+
type = "Read has multiple prior references" and
1506+
result =
1507+
count(SourceVariable v, BasicBlock bb1, int i1, BasicBlock bb2, int i2 |
1508+
readWithMultiplePriorRefs(v, bb1, i1, bb2, i2)
1509+
)
1510+
or
1511+
type = "Phi has less than 2 immediately prior references" and
1512+
result =
1513+
count(PhiNode phi, BasicBlock bbPhi, SourceVariable v, int inputRefs |
1514+
phiWithoutTwoPriorRefs(phi, bbPhi, v, inputRefs)
1515+
)
1516+
or
1517+
type = "Phi read has less than 2 immediately prior references" and
1518+
result =
1519+
count(BasicBlock bbPhi, SourceVariable v, int inputRefs |
1520+
phiReadWithoutTwoPriorRefs(bbPhi, v, inputRefs)
1521+
)
1522+
}
14711523
}
14721524

14731525
/** Provides the input to `DataFlowIntegration`. */

0 commit comments

Comments
 (0)