Skip to content

Commit

Permalink
Go: Use FlowSummaryImpl from dataflow pack
Browse files Browse the repository at this point in the history
  • Loading branch information
hvitved committed Nov 27, 2023
1 parent afc3403 commit b2ee1f0
Show file tree
Hide file tree
Showing 8 changed files with 296 additions and 1,904 deletions.
82 changes: 74 additions & 8 deletions go/ql/lib/semmle/go/dataflow/ExternalFlow.qll
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,10 @@
private import go
import internal.ExternalFlowExtensions
private import internal.DataFlowPrivate
private import internal.FlowSummaryImpl
private import internal.FlowSummaryImpl::Private
private import internal.FlowSummaryImpl::Private::External
private import internal.FlowSummaryImplSpecific
private import internal.AccessPathSyntax
private import FlowSummary
private import internal.FlowSummaryImpl::Public
private import codeql.mad.ModelValidation as SharedModelVal

/** Holds if `package` have MaD framework coverage. */
Expand Down Expand Up @@ -274,7 +274,7 @@ private string interpretPackage(string p) {
}

/** Gets the source/sink/summary element corresponding to the supplied parameters. */
SourceOrSinkElement interpretElement(
SourceSinkInterpretationInput::SourceOrSinkElement interpretElement(
string pkg, string type, boolean subtypes, string name, string signature, string ext
) {
elementSpec(pkg, type, subtypes, name, signature, ext) and
Expand All @@ -298,8 +298,9 @@ SourceOrSinkElement interpretElement(
predicate hasExternalSpecification(Function f) {
f = any(SummarizedCallable sc).asFunction()
or
exists(SourceOrSinkElement e | f = e.asEntity() |
sourceElement(e, _, _, _) or sinkElement(e, _, _, _)
exists(SourceSinkInterpretationInput::SourceOrSinkElement e | f = e.asEntity() |
SourceSinkInterpretationInput::sourceElement(e, _, _) or
SourceSinkInterpretationInput::sinkElement(e, _, _)
)
}

Expand Down Expand Up @@ -353,7 +354,9 @@ private module Cached {
*/
cached
predicate sourceNode(DataFlow::Node node, string kind) {
exists(InterpretNode n | isSourceNode(n, kind) and n.asNode() = node)
exists(SourceSinkInterpretationInput::InterpretNode n |
isSourceNode(n, kind) and n.asNode() = node
)
}

/**
Expand All @@ -362,8 +365,71 @@ private module Cached {
*/
cached
predicate sinkNode(DataFlow::Node node, string kind) {
exists(InterpretNode n | isSinkNode(n, kind) and n.asNode() = node)
exists(SourceSinkInterpretationInput::InterpretNode n |
isSinkNode(n, kind) and n.asNode() = node
)
}
}

import Cached

private predicate interpretSummary(
Callable c, string input, string output, string kind, string provenance
) {
exists(
string namespace, string type, boolean subtypes, string name, string signature, string ext
|
summaryModel(namespace, type, subtypes, name, signature, ext, input, output, kind, provenance) and
c.asFunction() = interpretElement(namespace, type, subtypes, name, signature, ext).asEntity()
)
}

private predicate relevantSummaryElementManual(
Callable c, string input, string output, string kind, Provenance provenance
) {
interpretSummary(c, input, output, kind, provenance) and
provenance.isManual()
}

private predicate relevantSummaryElementGenerated(
Callable c, string input, string output, string kind, Provenance provenance
) {
interpretSummary(c, input, output, kind, provenance) and
provenance.isGenerated()
}

private class SummarizedCallableAdapter extends SummarizedCallable {
string input_;
string output_;
string kind;
string provenance_;

SummarizedCallableAdapter() {
relevantSummaryElementManual(this, input_, output_, kind, provenance_)
or
relevantSummaryElementGenerated(this, input_, output_, kind, provenance_) and
not relevantSummaryElementManual(this, _, _, _, _)
}

override predicate propagatesFlow(string input, string output, boolean preservesValue) {
input = input_ and
output = output_ and
if kind = "value" then preservesValue = true else preservesValue = false
}

override predicate hasProvenance(Provenance provenance) { provenance = provenance_ }
}

private class NeutralCallableAdapter extends NeutralCallable {

Check warning

Code scanning / CodeQL

UnusedField Warning

This class declares the
field kind
but does not bind it in the characteristic predicate.
This class declares the
field provenance_
but does not bind it in the characteristic predicate.
string kind;
string provenance_;

NeutralCallableAdapter() {
// Neutral models have not been implemented for Go.
none()
}

override string getKind() { result = kind }

override predicate hasProvenance(Provenance provenance) { provenance = provenance_ }
}
36 changes: 5 additions & 31 deletions go/ql/lib/semmle/go/dataflow/FlowSummary.qll
Original file line number Diff line number Diff line change
Expand Up @@ -10,40 +10,14 @@ private import internal.DataFlowUtil
// import all instances below
private module Summaries { }

class SummaryComponent = Impl::Public::SummaryComponent;
deprecated class SummaryComponent = Impl::Private::SummaryComponent;

/** Provides predicates for constructing summary components. */
module SummaryComponent {
import Impl::Public::SummaryComponent
deprecated module SummaryComponent = Impl::Private::SummaryComponent;

/** Gets a summary component that represents a qualifier. */
SummaryComponent qualifier() { result = argument(-1) }
deprecated class SummaryComponentStack = Impl::Private::SummaryComponentStack;

/** Gets a summary component for field `f`. */
SummaryComponent field(Field f) { result = content(any(FieldContent c | c.getField() = f)) }

/** Gets a summary component that represents the return value of a call. */
SummaryComponent return() { result = return(_) }
}

class SummaryComponentStack = Impl::Public::SummaryComponentStack;

/** Provides predicates for constructing stacks of summary components. */
module SummaryComponentStack {
import Impl::Public::SummaryComponentStack

/** Gets a singleton stack representing a qualifier. */
SummaryComponentStack qualifier() { result = singleton(SummaryComponent::qualifier()) }

/** Gets a stack representing a field `f` of `object`. */
SummaryComponentStack fieldOf(Field f, SummaryComponentStack object) {
result = push(SummaryComponent::field(f), object)
}

/** Gets a singleton stack representing a (normal) return. */
SummaryComponentStack return() { result = singleton(SummaryComponent::return()) }
}
deprecated module SummaryComponentStack = Impl::Private::SummaryComponentStack;

class SummarizedCallable = Impl::Public::SummarizedCallable;

class RequiredSummaryComponentStack = Impl::Public::RequiredSummaryComponentStack;
deprecated class RequiredSummaryComponentStack = Impl::Private::RequiredSummaryComponentStack;
182 changes: 0 additions & 182 deletions go/ql/lib/semmle/go/dataflow/internal/AccessPathSyntax.qll

This file was deleted.

6 changes: 4 additions & 2 deletions go/ql/lib/semmle/go/dataflow/internal/DataFlowNodes.qll
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,8 @@ module Private {

/** Holds if this summary node is the `i`th argument of `call`. */
predicate isArgumentOf(DataFlowCall call, int i) {
FlowSummaryImpl::Private::summaryArgumentNode(call, this.getSummaryNode(), i)
// We do not currently have support for callback-based library models.
none()
}

/** Holds if this summary node is a return node. */
Expand All @@ -96,7 +97,8 @@ module Private {

/** Holds if this summary node is an out node for `call`. */
predicate isOut(DataFlowCall call) {
FlowSummaryImpl::Private::summaryOutNode(call, this.getSummaryNode(), _)
// We do not currently have support for callback-based library models.
none()
}
}
}
Expand Down
5 changes: 4 additions & 1 deletion go/ql/lib/semmle/go/dataflow/internal/DataFlowPrivate.qll
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,10 @@ predicate additionalLambdaFlowStep(Node nodeFrom, Node nodeTo, boolean preserves
* by default as a heuristic.
*/
predicate allowParameterReturnInSelf(ParameterNode p) {
FlowSummaryImpl::Private::summaryAllowParameterReturnInSelf(p)
exists(DataFlowCallable c, int pos |
p.isParameterOf(c, pos) and
FlowSummaryImpl::Private::summaryAllowParameterReturnInSelf(c.asSummarizedCallable(), pos)
)
}

/** An approximated `Content`. */
Expand Down
Loading

0 comments on commit b2ee1f0

Please sign in to comment.