Skip to content

Commit

Permalink
Squash this2
Browse files Browse the repository at this point in the history
  • Loading branch information
Kwstubbs committed Dec 20, 2024
1 parent deecb41 commit 1255f14
Showing 1 changed file with 37 additions and 12 deletions.
49 changes: 37 additions & 12 deletions go/ql/lib/semmle/go/dataflow/SSA.qll
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,32 @@ class SsaPhiNode extends SsaPseudoDefinition, TPhi {
*/
private newtype TGlobalSsaWithFields =
TGlobalRoot(SsaGlobalVariable v) or
TGlobalStep(GlobalWithFields base, Field f) { exists(accessPathAux(base, f)) }
TGlobalStep(GlobalWithFields base, Field f) { exists(accessGlobalPathAux(base, f)) }


/**
* Gets a representation of `insn` as an ssa-with-fields value if there is one.
*/
private TGlobalSsaWithFields accessGlobalPath(IR::Instruction insn) {
exists(SsaGlobalVariable v | insn = v.getAUse() | result = TGlobalRoot(v))
or
exists(GlobalWithFields base, Field f | insn = accessGlobalPathAux(base, f) | result = TGlobalStep(base, f))
}

/**
* Gets a data-flow node that reads a field `f` from a node that is represented
* by ssa-with-fields value `base`.
*/
private IR::Instruction accessGlobalPathAux(TGlobalSsaWithFields base, Field f) {
exists(IR::FieldReadInstruction fr, IR::Instruction frb |
fr.getBase() = frb or
fr.getBase() = IR::implicitDerefInstruction(frb.(IR::EvalInstruction).getExpr())
|
base = accessGlobalPath(frb) and
f = fr.getField() and
result = fr
)
}

/**
* An SSA variable, possibly with a chain of field reads on it.
Expand Down Expand Up @@ -541,34 +566,34 @@ class SsaWithFields extends TSsaWithFields {


/** An SSA variable with zero or more fields read from it. */
class GlobalWithFields extends TSsaWithFields {
class GlobalWithFields extends TGlobalSsaWithFields {
/**
* Gets the SSA variable corresponding to the base of this SSA variable with fields.
*
* For example, the SSA variable corresponding to `a` for the SSA variable with fields
* corresponding to `a.b`.
*/
SsaVariable getBaseVariable() {
this = TRoot(result)
SsaGlobalVariable getBaseVariable() {
this = TGlobalRoot(result)
or
exists(GlobalWithFields base | this = TStep(base, _) | result = base.getBaseVariable())
exists(GlobalWithFields base | this = TGlobalStep(base, _) | result = base.getBaseVariable())
}

/** Gets a use that refers to this SSA variable with fields. */
DataFlow::Node getAUse() { this = accessPath(result.asInstruction()) }
DataFlow::Node getAUse() { this = accessGlobalPath(result.asInstruction()) }

/** Gets the type of this SSA variable with fields. */
Type getType() {
exists(SsaVariable var | this = TRoot(var) | result = var.getType())
exists(SsaGlobalVariable var | this = TGlobalRoot(var) | result = var.getType())
or
exists(Field f | this = TStep(_, f) | result = f.getType())
exists(Field f | this = TGlobalStep(_, f) | result = f.getType())
}

/** Gets a textual representation of this element. */
string toString() {
exists(SsaVariable var | this = TRoot(var) | result = "(" + var + ")")
exists(SsaGlobalVariable var | this = TGlobalRoot(var) | result = "(" + var + ")")
or
exists(GlobalWithFields base, Field f | this = TStep(base, f) | result = base + "." + f.getName())
exists(GlobalWithFields base, Field f | this = TGlobalStep(base, f) | result = base + "." + f.getName())
}

/**
Expand All @@ -587,9 +612,9 @@ class GlobalWithFields extends TSsaWithFields {
* `"a.b"`.
*/
string getQualifiedName() {
exists(SsaVariable v | this = TRoot(v) and result = v.getSourceVariable().getName())
exists(SsaGlobalVariable v | this = TGlobalRoot(v) and result = v.getSourceVariable().getName())
or
exists(GlobalWithFields base, Field f | this = TStep(base, f) |
exists(GlobalWithFields base, Field f | this = TGlobalStep(base, f) |
result = base.getQualifiedName() + "." + f.getName()
)
}
Expand Down

0 comments on commit 1255f14

Please sign in to comment.