mirror of
https://github.com/github/codeql.git
synced 2026-08-04 01:13:00 +02:00
Preparatory refactor for the shared-CFG dataflow migration. Adds the new Python SSA adapter additively, without changing any production behaviour. Library additions: - semmle.python.dataflow.new.internal.SsaImpl — Python SSA implementation built on the new (shared) CFG. Mirrors the Java SSA adapter (java/ql/lib/semmle/code/java/dataflow/internal/SsaImpl.qll): an InputSig is defined in terms of positional (BasicBlock, int) variable references, and the shared codeql.ssa.Ssa::Make<Location, Cfg, Input> module is then instantiated. SourceVariable is the AST-level Py::Variable. Variable references are looked up via the new CFG facade's NameNode.defines/uses/deletes predicates (added in the preceding PR), which themselves are one-line bridges to AST-level Name.defines/uses/deletes. Implicit-entry definitions are inserted for non-local/global/builtin reads, captured variables, and (when needed) parameters. Test additions: - library-tests/dataflow-new-ssa/ — exercises the new SSA over a representative test corpus and checks expected def/use chains. - library-tests/dataflow-new-ssa-vs-legacy/ — runs both new SSA and legacy ESSA over the same corpus and diffs the results, so any semantic divergence shows up as a test failure. Production impact: None. The new SSA adapter has zero callers in lib/ and src/ — the legacy ESSA SSA (semmle/python/essa/*) remains the default. The dataflow library is not migrated yet; that lands in a follow-up PR. Verified by: - All 367 lib + src + consistency-queries compile clean. - All 641 ControlFlow + PointsTo + dataflow + essa + consistency library-tests pass. - Both new dataflow-new-ssa[/vs-legacy] test packs pass. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
60 lines
2.1 KiB
Plaintext
60 lines
2.1 KiB
Plaintext
/**
|
|
* Compares the new-CFG SSA against the legacy ESSA on the same Python
|
|
* sources. Reports definitions present in one implementation but not
|
|
* the other, identified by variable name + source position.
|
|
*
|
|
* The `.expected` file records the current diff as a snapshot: as the
|
|
* new SSA matures (closing captured-variable gap, exception bindings,
|
|
* etc.) and tracks more variables, the snapshot should monotonically
|
|
* shrink.
|
|
*
|
|
* Known categories of `def-only-old` mismatches:
|
|
* - Function / class / global definitions with no in-scope read
|
|
* (intentional: SSA is liveness-pruned, write-only variables are
|
|
* not tracked).
|
|
* - Captured / closure variables (gap: new SSA does not yet model
|
|
* closure captures).
|
|
* - Module variables `__name__`, `__package__`, `$` (legacy ESSA
|
|
* adds implicit bindings the new SSA does not).
|
|
* - Exception-handler `as` bindings (depend on raise modelling).
|
|
*
|
|
* `def-only-new` mismatches would indicate the new SSA produces spurious
|
|
* definitions; currently none are expected.
|
|
*/
|
|
|
|
import python
|
|
import semmle.python.dataflow.new.internal.SsaImpl as NewSsa
|
|
import semmle.python.controlflow.internal.Cfg as Cfg
|
|
import semmle.python.essa.Essa
|
|
|
|
string newDefSig(NewSsa::EssaNodeDefinition def) {
|
|
exists(Cfg::ControlFlowNode n | n = def.getDefiningNode() |
|
|
result =
|
|
def.getVariable().getVariable().getId() + ":" + n.getLocation().getStartLine() + ":" +
|
|
n.getLocation().getStartColumn()
|
|
)
|
|
}
|
|
|
|
string legacyDefSig(EssaNodeDefinition def) {
|
|
exists(ControlFlowNode n | n = def.getDefiningNode() |
|
|
result =
|
|
def.getSourceVariable().getName() + ":" + n.getLocation().getStartLine() + ":" +
|
|
n.getLocation().getStartColumn()
|
|
)
|
|
}
|
|
|
|
from string kind, string sig
|
|
where
|
|
kind = "def-only-new" and
|
|
exists(NewSsa::EssaNodeDefinition def |
|
|
sig = newDefSig(def) and
|
|
not exists(EssaNodeDefinition legacyDef | sig = legacyDefSig(legacyDef))
|
|
)
|
|
or
|
|
kind = "def-only-old" and
|
|
exists(EssaNodeDefinition legacyDef |
|
|
sig = legacyDefSig(legacyDef) and
|
|
not exists(NewSsa::EssaNodeDefinition def | sig = newDefSig(def))
|
|
)
|
|
select kind, sig
|