Skip to content

Shared: Refactor DataFlowStack #157

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 30, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 0 additions & 18 deletions cpp/ql/lib/semmle/code/cpp/dataflow/internal/DataFlowPrivate.qll
Original file line number Diff line number Diff line change
Expand Up @@ -255,27 +255,9 @@ class DataFlowCall extends Expr instanceof Call {
*/
Expr getArgument(int n) { result = super.getArgument(n) }

/** Gets an argument to this call. */
Expr getAnArgument(){ result = super.getAnArgument() }

/** Gets an argument to this call as a Node. */
ArgumentNode getAnArgumentNode(){ result = this.getNode() }

/** Gets the data flow node corresponding to this call. */
ExprNode getNode() { result.getExpr() = this }

/** Gets the data flow node corresponding to this call. (Alias of `getNode()`) */
ExprNode getDataFlowNode() { result = this.getNode() }

/** Gets the target of the call, as best as makes sense for this kind of call.
*
* The precise meaning depends on the kind of call it is:
* - For a call to a function, it’s the function being called.
* - For a C++ method call, it’s the statically resolved method.
* - For an Objective C message expression, it’s the statically resolved method, and it might not exist.
* - For a variable call, it never exists.
*/
DataFlowCallable getARuntimeTarget(){ result = super.getTarget() }
/** Gets the enclosing callable of this call. */
DataFlowCallable getEnclosingCallable() { result = this.getEnclosingFunction() }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1058,16 +1058,6 @@ class DataFlowCallable extends TDataFlowCallable {
result = this.asSummarizedCallable() or // SummarizedCallable = Function (in CPP)
result = this.asSourceCallable()
}

/** Gets a best-effort total ordering. */
int totalorder() {
this =
rank[result](DataFlowCallable c, string file, int startline, int startcolumn |
c.getLocation().hasLocationInfo(file, startline, startcolumn, _, _)
|
c order by file, startline, startcolumn
)
}
}

/**
Expand Down Expand Up @@ -1169,23 +1159,6 @@ class DataFlowCall extends TDataFlowCall {
* Gets the location of this call.
*/
Location getLocation() { none() }

// #43: Stub Implementation
/** Gets an argument to this call as a Node. */
ArgumentNode getAnArgumentNode(){ none() } // TODO: JB1 return an argument as a DataFlow ArgumentNode

// #43: Stub Implementation
/** Gets the target of the call, as a DataFlowCallable. */
DataFlowCallable getARuntimeTarget(){ none() } // TODO getCallTarget() returns `Instruction`
/** Gets a best-effort total ordering. */
int totalorder() {
this =
rank[result](DataFlowCall c, int startline, int startcolumn |
c.getLocation().hasLocationInfo(_, startline, startcolumn, _, _)
|
c order by startline, startcolumn
)
}
}

/**
Expand Down Expand Up @@ -1280,15 +1253,6 @@ module IsUnreachableInCall {
string toString() { result = "NodeRegion" }

predicate contains(Node n) { this = n.getBasicBlock() }

int totalOrder() {
this =
rank[result](IRBlock b, int startline, int startcolumn |
b.getLocation().hasLocationInfo(_, startline, startcolumn, _, _)
|
b order by startline, startcolumn
)
}
}

predicate isUnreachableInCall(NodeRegion block, DataFlowCall call) {
Expand Down
35 changes: 28 additions & 7 deletions csharp/ql/lib/semmle/code/csharp/dataflow/DataFlowStack.qll
Original file line number Diff line number Diff line change
@@ -1,15 +1,36 @@

import csharp
private import codeql.dataflow.DataFlow
private import semmle.code.csharp.dataflow.internal.DataFlowImplSpecific

private import codeql.dataflowstack.DataFlowStack as DFS
private import DFS::DataFlowStackMake<Location, CsharpDataFlow> as DataFlowStackFactory

module DataFlowStackMake<DataFlowStackFactory::DataFlow::GlobalFlowSig Flow>{
import DataFlowStackFactory::FlowStack<Flow>
private module DataFlowStackInput<DataFlowStackFactory::DataFlow::ConfigSig Config> implements
DFS::DataFlowStackSig<Location, CsharpDataFlow, Config>
{
private module Flow = DataFlow::Global<Config>;

CsharpDataFlow::Node getNode(Flow::PathNode n) { result = n.getNode() }

predicate isSource(Flow::PathNode n) { n.isSource() }

Flow::PathNode getASuccessor(Flow::PathNode n) { result = n.getASuccessor() }

CsharpDataFlow::DataFlowCallable getARuntimeTarget(CsharpDataFlow::DataFlowCall call) {
result = call.getARuntimeTarget()
}

CsharpDataFlow::Node getAnArgumentNode(CsharpDataFlow::DataFlowCall call) {
result = call.getArgument(_)
}
}

module BiStackAnalysisMake<DataFlowStackFactory::DataFlow::GlobalFlowSig FlowA, DataFlowStackFactory::DataFlow::GlobalFlowSig FlowB>{
import DataFlowStackFactory::BiStackAnalysis<FlowA, FlowB>
}
module DataFlowStackMake<DataFlowStackFactory::DataFlow::ConfigSig Config> {
import DataFlowStackFactory::FlowStack<Config, DataFlowStackInput<Config>>
}

module BiStackAnalysisMake<
DataFlowStackFactory::DataFlow::ConfigSig ConfigA,
DataFlowStackFactory::DataFlow::ConfigSig ConfigB>
{
import DataFlowStackFactory::BiStackAnalysis<ConfigA, DataFlowStackInput<ConfigA>, ConfigB, DataFlowStackInput<ConfigB>>
}
Original file line number Diff line number Diff line change
Expand Up @@ -288,16 +288,6 @@ class DataFlowCallable extends TDataFlowCallable {
or
result = this.asCapturedVariable().getLocation()
}

/** Gets a best-effort total ordering. */
int totalorder() {
this =
rank[result](DataFlowCallable c, string file, int startline, int startcolumn |
c.getLocation().hasLocationInfo(file, startline, startcolumn, _, _)
|
c order by file, startline, startcolumn
)
}
}

/** A call relevant for data flow. */
Expand All @@ -323,9 +313,6 @@ abstract class DataFlowCall extends TDataFlowCall {
/** Gets the argument at position `pos` of this call. */
final ArgumentNode getArgument(ArgumentPosition pos) { result.argumentOf(this, pos) }

/** Gets an argument of this call. */
final ArgumentNode getAnArgumentNode() { result.argumentOf(this, _) }

/** Gets a textual representation of this call. */
abstract string toString();

Expand All @@ -344,16 +331,6 @@ abstract class DataFlowCall extends TDataFlowCall {
) {
this.getLocation().hasLocationInfo(filepath, startline, startcolumn, endline, endcolumn)
}

/** Gets a best-effort total ordering. */
int totalorder() {
this =
rank[result](DataFlowCall c, int startline, int startcolumn |
c.hasLocationInfo(_, startline, startcolumn, _, _)
|
c order by startline, startcolumn
)
}
}

private predicate relevantFolder(Folder f) {
Expand Down
36 changes: 0 additions & 36 deletions go/ql/lib/semmle/go/dataflow/internal/DataFlowPrivate.qll
Original file line number Diff line number Diff line change
Expand Up @@ -315,16 +315,6 @@ class DataFlowCallable extends TDataFlowCallable {
result = this.asFileScope().getLocation() or
result = getCallableLocation(this.asSummarizedCallable())
}

/** Gets a best-effort total ordering. */
int totalorder() {
this =
rank[result](DataFlowCallable c, string file, int startline, int startcolumn |
c.hasLocationInfo(file, startline, startcolumn, _, _)
|
c order by file, startline, startcolumn
)
}
}

private Location getCallableLocation(Callable c) {
Expand Down Expand Up @@ -358,23 +348,6 @@ class DataFlowCall extends Expr {

/** Gets the location of this call. */
Location getLocation() { result = super.getLocation() }

// #45 - Stub Implementation
/** Gets an argument to this call as a Node. */
ArgumentNode getAnArgumentNode(){ result = this.getArgument(_) }

/** Gets the target of the call, as a DataFlowCallable. */
DataFlowCallable getARuntimeTarget(){ result.asCallable() = call.getACalleeIncludingExternals() }

/** Gets a best-effort total ordering. */
int totalorder() {
this =
rank[result](DataFlowCall c, int startline, int startcolumn |
c.getLocation().hasLocationInfo(_, startline, startcolumn, _, _)
|
c order by startline, startcolumn
)
}
}

/** Holds if `e` is an expression that always has the same Boolean value `val`. */
Expand Down Expand Up @@ -417,15 +390,6 @@ class NodeRegion instanceof BasicBlock {
string toString() { result = "NodeRegion" }

predicate contains(Node n) { n.getBasicBlock() = this }

int totalOrder() {
this =
rank[result](BasicBlock b, int startline, int startcolumn |
b.hasLocationInfo(_, startline, startcolumn, _, _)
|
b order by startline, startcolumn
)
}
}

/**
Expand Down
62 changes: 0 additions & 62 deletions java/ql/lib/semmle/code/java/dataflow/internal/DataFlowPrivate.qll
Original file line number Diff line number Diff line change
Expand Up @@ -423,21 +423,6 @@ predicate cloneStep(Node n1, Node n2) {
bindingset[node1, node2]
predicate validParameterAliasStep(Node node1, Node node2) { not cloneStep(node1, node2) }

private predicate id_member(Member x, Member y) { x = y }

private predicate idOf_member(Member x, int y) = equivalenceRelation(id_member/2)(x, y)

private int summarizedCallableId(SummarizedCallable c) {
c =
rank[result](SummarizedCallable c0, int b, int i, string s |
b = 0 and idOf_member(c0.asCallable(), i) and s = ""
or
b = 1 and i = 0 and s = c0.asSyntheticCallable()
|
c0 order by b, i, s
)
}

private newtype TDataFlowCallable =
TSrcCallable(Callable c) or
TSummarizedCallable(SummarizedCallable c) or
Expand Down Expand Up @@ -471,28 +456,10 @@ class DataFlowCallable extends TDataFlowCallable {
result = this.asSummarizedCallable().getLocation() or
result = this.asFieldScope().getLocation()
}

/** Gets a best-effort total ordering. */
int totalorder() {
this =
rank[result](DataFlowCallable c, int b, int i |
b = 0 and idOf_member(c.asCallable(), i)
or
b = 1 and i = summarizedCallableId(c.asSummarizedCallable())
or
b = 2 and idOf_member(c.asFieldScope(), i)
|
c order by b, i
)
}
}

class DataFlowExpr = Expr;

private predicate id_call(Call x, Call y) { x = y }

private predicate idOf_call(Call x, int y) = equivalenceRelation(id_call/2)(x, y)

private newtype TDataFlowCall =
TCall(Call c) or
TSummaryCall(SummarizedCallable c, FlowSummaryImpl::Private::SummaryNode receiver) {
Expand Down Expand Up @@ -525,29 +492,6 @@ class DataFlowCall extends TDataFlowCall {
) {
this.getLocation().hasLocationInfo(filepath, startline, startcolumn, endline, endcolumn)
}

/** Gets an argument to this call as a Node. */
ArgumentNode getAnArgumentNode(){
result = exprNode(this.asCall().getAnArgument())
}

/** Gets the target of the call, as a DataFlowCallable. */
DataFlowCallable getARuntimeTarget(){
result.asCallable() = this.asCall().getCallee()
}

/** Gets a best-effort total ordering. */
int totalorder() {
this =
rank[result](DataFlowCall c, int b, int i |
b = 0 and idOf_call(c.asCall(), i)
or
b = 1 and // not guaranteed to be total
exists(SummarizedCallable sc | c = TSummaryCall(sc, _) and i = summarizedCallableId(sc))
|
c order by b, i
)
}
}

/** A source call, that is, a `Call`. */
Expand Down Expand Up @@ -582,16 +526,10 @@ class SummaryCall extends DataFlowCall, TSummaryCall {
override Location getLocation() { result = c.getLocation() }
}

private predicate id(BasicBlock x, BasicBlock y) { x = y }

private predicate idOf(BasicBlock x, int y) = equivalenceRelation(id/2)(x, y)

class NodeRegion instanceof BasicBlock {
string toString() { result = "NodeRegion" }

predicate contains(Node n) { n.asExpr().getBasicBlock() = this }

int totalOrder() { idOf(this, result) }
}

/** Holds if `e` is an expression that always has the same Boolean value `val`. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -347,16 +347,6 @@ abstract class DataFlowCallable extends TDataFlowCallable {

/** Gets the location of this dataflow callable. */
abstract Location getLocation();

/** Gets a best-effort total ordering. */
int totalorder() {
this =
rank[result](DataFlowCallable c, string file, int startline, int startcolumn |
c.getLocation().hasLocationInfo(file, startline, startcolumn, _, _)
|
c order by file, startline, startcolumn
)
}
}

/** A callable function. */
Expand Down Expand Up @@ -1439,23 +1429,6 @@ abstract class DataFlowCall extends TDataFlowCall {
) {
this.getLocation().hasLocationInfo(filepath, startline, startcolumn, endline, endcolumn)
}

// #47: Stubs below
/** Gets an argument to this call as a Node. */
ArgumentNode getAnArgumentNode(){ none() } // TODO: JB1 return an argument as a DataFlow ArgumentNode

/** Gets the target of the call, as a DataFlowCallable. */
DataFlowCallable getARuntimeTarget(){ none() } // TODO

/** Gets a best-effort total ordering. */
int totalorder() {
this =
rank[result](DataFlowCall c, int startline, int startcolumn |
c.hasLocationInfo(_, startline, startcolumn, _, _)
|
c order by startline, startcolumn
)
}
}

/** A call found in the program source (as opposed to a synthesised call). */
Expand Down
Loading