Skip to content

Rust: Make SummarizedCallable extend Function instead of string #19268

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

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
27 changes: 1 addition & 26 deletions rust/ql/lib/codeql/rust/dataflow/FlowSummary.qll
Original file line number Diff line number Diff line change
Expand Up @@ -2,42 +2,17 @@

private import rust
private import internal.FlowSummaryImpl as Impl
private import codeql.rust.elements.internal.CallExprBaseImpl::Impl as CallExprBaseImpl

// import all instances below
private module Summaries {
private import codeql.rust.Frameworks
private import codeql.rust.dataflow.internal.ModelsAsData
}

/** Provides the `Range` class used to define the extent of `LibraryCallable`. */
module LibraryCallable {
/** A callable defined in library code, identified by a unique string. */
abstract class Range extends string {
bindingset[this]
Range() { any() }

/** Gets a call to this library callable. */
CallExprBase getACall() {
exists(Resolvable r, string crate |
r = CallExprBaseImpl::getCallResolvable(result) and
this = crate + r.getResolvedPath()
|
crate = r.getResolvedCrateOrigin() + "::_::"
or
not r.hasResolvedCrateOrigin() and
crate = ""
)
}
}
}

final class LibraryCallable = LibraryCallable::Range;

/** Provides the `Range` class used to define the extent of `SummarizedCallable`. */
module SummarizedCallable {
/** A callable with a flow summary, identified by a unique string. */
abstract class Range extends LibraryCallable::Range, Impl::Public::SummarizedCallable {
abstract class Range extends Impl::Public::SummarizedCallable {
bindingset[this]
Range() { any() }

Expand Down
27 changes: 15 additions & 12 deletions rust/ql/lib/codeql/rust/dataflow/internal/DataFlowImpl.qll
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,12 @@ final class DataFlowCallable extends TDataFlowCallable {
/**
* Gets the underlying library callable, if any.
*/
LibraryCallable asLibraryCallable() { this = TLibraryCallable(result) }
SummarizedCallable asSummarizedCallable() { this = TSummarizedCallable(result) }

/** Gets a textual representation of this callable. */
string toString() { result = [this.asCfgScope().toString(), this.asLibraryCallable().toString()] }
string toString() {
result = [this.asCfgScope().toString(), this.asSummarizedCallable().toString()]
}

/** Gets the location of this callable. */
Location getLocation() { result = this.asCfgScope().getLocation() }
Expand All @@ -68,12 +70,9 @@ final class DataFlowCall extends TDataFlowCall {
}

DataFlowCallable getEnclosingCallable() {
result = TCfgScope(this.asCallBaseExprCfgNode().getExpr().getEnclosingCfgScope())
result.asCfgScope() = this.asCallBaseExprCfgNode().getExpr().getEnclosingCfgScope()
or
exists(FlowSummaryImpl::Public::SummarizedCallable c |
this.isSummaryCall(c, _) and
result = TLibraryCallable(c)
)
this.isSummaryCall(result.asSummarizedCallable(), _)
}

string toString() {
Expand Down Expand Up @@ -418,9 +417,13 @@ module RustDataFlow implements InputSig<Location> {

/** Gets a viable implementation of the target of the given `Call`. */
DataFlowCallable viableCallable(DataFlowCall call) {
result.asCfgScope() = call.asCallBaseExprCfgNode().getCallExprBase().getStaticTarget()
or
result.asLibraryCallable().getACall() = call.asCallBaseExprCfgNode().getCallExprBase()
exists(Callable target |
target = call.asCallBaseExprCfgNode().getCallExprBase().getStaticTarget()
|
target = result.asCfgScope()
or
target = result.asSummarizedCallable()
)
}

/**
Expand Down Expand Up @@ -768,7 +771,7 @@ module RustDataFlow implements InputSig<Location> {
predicate allowParameterReturnInSelf(ParameterNode p) {
exists(DataFlowCallable c, ParameterPosition pos |
p.isParameterOf(c, pos) and
FlowSummaryImpl::Private::summaryAllowParameterReturnInSelf(c.asLibraryCallable(), pos)
FlowSummaryImpl::Private::summaryAllowParameterReturnInSelf(c.asSummarizedCallable(), pos)
)
or
VariableCapture::Flow::heuristicAllowInstanceParameterReturnInSelf(p.(ClosureParameterNode)
Expand Down Expand Up @@ -979,7 +982,7 @@ private module Cached {
cached
newtype TDataFlowCallable =
TCfgScope(CfgScope scope) or
TLibraryCallable(LibraryCallable c)
TSummarizedCallable(SummarizedCallable c)

/** This is the local flow predicate that is exposed. */
cached
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ module Input implements InputSig<Location, RustDataFlow> {
private import codeql.rust.elements.internal.CallExprBaseImpl::Impl as CallExprBaseImpl
private import codeql.rust.frameworks.stdlib.Stdlib

class SummarizedCallableBase = string;
class SummarizedCallableBase = Function;

abstract private class SourceSinkBase extends AstNode {
/** Gets the associated call. */
Expand Down Expand Up @@ -153,7 +153,7 @@ private import Make<Location, RustDataFlow, Input> as Impl

private module StepsInput implements Impl::Private::StepsInputSig {
DataFlowCall getACall(Public::SummarizedCallable sc) {
result.asCallBaseExprCfgNode().getCallExprBase() = sc.(LibraryCallable).getACall()
result.asCallBaseExprCfgNode().getCallExprBase().getStaticTarget() = sc
}

RustDataFlow::Node getSourceNode(Input::SourceBase source, Impl::Private::SummaryComponent sc) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ private import rust
private import codeql.rust.dataflow.FlowSummary
private import codeql.rust.dataflow.FlowSource
private import codeql.rust.dataflow.FlowSink
private import codeql.rust.elements.internal.CallExprBaseImpl::Impl as CallExprBaseImpl

/**
* Holds if in a call to the function with canonical path `path`, defined in the
Expand Down Expand Up @@ -120,7 +121,12 @@ private class SummarizedCallableFromModel extends SummarizedCallable::Range {

SummarizedCallableFromModel() {
summaryModel(crate, path, _, _, _, _, _) and
this = crate + "::_::" + path
exists(CallExprBase call, Resolvable r |
call.getStaticTarget() = this and
r = CallExprBaseImpl::getCallResolvable(call) and
r.getResolvedPath() = path and
r.getResolvedCrateOrigin() = crate
)
}

override predicate propagatesFlow(
Expand Down
8 changes: 4 additions & 4 deletions rust/ql/lib/codeql/rust/dataflow/internal/Node.qll
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ abstract class NodePublic extends TNode {

abstract class Node extends NodePublic {
/** Gets the enclosing callable. */
DataFlowCallable getEnclosingCallable() { result = TCfgScope(this.getCfgScope()) }
DataFlowCallable getEnclosingCallable() { result.asCfgScope() = this.getCfgScope() }

/** Do not call: use `getEnclosingCallable()` instead. */
abstract CfgScope getCfgScope();
Expand Down Expand Up @@ -102,9 +102,9 @@ class FlowSummaryNode extends Node, TFlowSummaryNode {
}

override DataFlowCallable getEnclosingCallable() {
result.asLibraryCallable() = this.getSummarizedCallable()
or
result.asCfgScope() = this.getCfgScope()
or
result.asSummarizedCallable() = this.getSummarizedCallable()
}

override Location getLocation() {
Expand Down Expand Up @@ -195,7 +195,7 @@ final class SummaryParameterNode extends ParameterNode, FlowSummaryNode {
}

override predicate isParameterOf(DataFlowCallable c, ParameterPosition pos) {
this.getSummarizedCallable() = c.asLibraryCallable() and pos = pos_
this.getSummarizedCallable() = c.asSummarizedCallable() and pos = pos_
}
}

Expand Down
9 changes: 2 additions & 7 deletions rust/ql/lib/codeql/rust/frameworks/stdlib/Clone.qll
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,8 @@ private import codeql.rust.dataflow.FlowSummary
/** A `clone` method. */
final class CloneCallable extends SummarizedCallable::Range {
CloneCallable() {
// NOTE: The function target may not exist in the database, so we base this
// on method calls.
exists(MethodCallExpr c |
c.getIdentifier().getText() = "clone" and
c.getArgList().getNumberOfArgs() = 0 and
this = c.getResolvedCrateOrigin() + "::_::" + c.getResolvedPath()
)
this.getParamList().getNumberOfParams() = 1 and
this.getName().getText() = "clone"
}

final override predicate propagatesFlow(
Expand Down
93 changes: 30 additions & 63 deletions rust/ql/lib/codeql/rust/internal/PathResolution.qll
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,6 @@ abstract class ItemNode extends Locatable {
/** Gets the `i`th type parameter of this item, if any. */
abstract TypeParam getTypeParam(int i);

/** Holds if this item is declared as `pub`. */
bindingset[this]
pragma[inline_late]
predicate isPublic() { exists(this.getVisibility()) }

/** Gets an element that has this item as immediately enclosing item. */
pragma[nomagic]
Element getADescendant() {
Expand Down Expand Up @@ -207,6 +202,11 @@ abstract class ItemNode extends Locatable {
result.(CrateItemNode).isPotentialDollarCrateTarget()
}

pragma[nomagic]
private predicate hasSourceFunction(string name) {
this.getASuccessorFull(name).(Function).fromSource()
}

/** Gets a successor named `name` of this item, if any. */
pragma[nomagic]
ItemNode getASuccessor(string name) {
Expand All @@ -219,7 +219,7 @@ abstract class ItemNode extends Locatable {
or
not result instanceof Function
or
not this.getASuccessorFull(name).(Function).fromSource()
not this.hasSourceFunction(name)
)
}

Expand Down Expand Up @@ -266,8 +266,6 @@ private class SourceFileItemNode extends ModuleLikeNode, SourceFile {

override Visibility getVisibility() { none() }

override predicate isPublic() { any() }

override TypeParam getTypeParam(int i) { none() }
}

Expand Down Expand Up @@ -330,8 +328,6 @@ class CrateItemNode extends ItemNode instanceof Crate {

override Visibility getVisibility() { none() }

override predicate isPublic() { any() }

override TypeParam getTypeParam(int i) { none() }
}

Expand Down Expand Up @@ -436,17 +432,17 @@ abstract class ImplOrTraitItemNode extends ItemNode {

pragma[nomagic]
private TypeParamItemNode resolveTypeParamPathTypeRepr(PathTypeRepr ptr) {
result = resolvePath(ptr.getPath())
result = resolvePathFull(ptr.getPath())
}

class ImplItemNode extends ImplOrTraitItemNode instanceof Impl {
Path getSelfPath() { result = super.getSelfTy().(PathTypeRepr).getPath() }

Path getTraitPath() { result = super.getTrait().(PathTypeRepr).getPath() }

ItemNode resolveSelfTy() { result = resolvePath(this.getSelfPath()) }
ItemNode resolveSelfTy() { result = resolvePathFull(this.getSelfPath()) }

TraitItemNode resolveTraitTy() { result = resolvePath(this.getTraitPath()) }
TraitItemNode resolveTraitTy() { result = resolvePathFull(this.getTraitPath()) }

pragma[nomagic]
private TypeRepr getASelfTyArg() {
Expand Down Expand Up @@ -560,7 +556,7 @@ class TraitItemNode extends ImplOrTraitItemNode instanceof Trait {
}

pragma[nomagic]
ItemNode resolveABound() { result = resolvePath(this.getABoundPath()) }
ItemNode resolveABound() { result = resolvePathFull(this.getABoundPath()) }

override AssocItemNode getAnAssocItem() { result = super.getAssocItemList().getAnAssocItem() }

Expand Down Expand Up @@ -634,7 +630,7 @@ class TypeParamItemNode extends ItemNode instanceof TypeParam {
}

pragma[nomagic]
ItemNode resolveABound() { result = resolvePath(this.getABoundPath()) }
ItemNode resolveABound() { result = resolvePathFull(this.getABoundPath()) }

/**
* Holds if this type parameter has a trait bound. Examples:
Expand Down Expand Up @@ -897,12 +893,6 @@ class RelevantPath extends Path {
this.getQualifier().(RelevantPath).isCratePath("$crate", _) and
this.getText() = name
}

// TODO: Remove once the crate graph extractor generates publicly visible paths
predicate requiresExtractorWorkaround() {
not this.fromSource() and
this = any(RelevantPath p).getQualifier()
}
}

private predicate isModule(ItemNode m) { m instanceof Module }
Expand Down Expand Up @@ -1056,8 +1046,14 @@ private predicate pathUsesNamespace(Path p, Namespace n) {
)
}

/**
* Gets the item that `path` resolves to, if any.
*
* Whenever `path` can resolve to both a function in source code and in library
* code, both are included
*/
pragma[nomagic]
private ItemNode resolvePath1(RelevantPath path) {
private ItemNode resolvePathFull(RelevantPath path) {
exists(Namespace ns | result = resolvePath0(path, ns) |
pathUsesNamespace(path, ns)
or
Expand All @@ -1067,58 +1063,29 @@ private ItemNode resolvePath1(RelevantPath path) {
}

pragma[nomagic]
private ItemNode resolvePathPrivate(
RelevantPath path, ModuleLikeNode itemParent, ModuleLikeNode pathParent
) {
not path.requiresExtractorWorkaround() and
result = resolvePath1(path) and
itemParent = result.getImmediateParentModule() and
not result.isPublic() and
(
pathParent.getADescendant() = path
or
pathParent = any(ItemNode mid | path = mid.getADescendant()).getImmediateParentModule()
)
}

pragma[nomagic]
private predicate isItemParent(ModuleLikeNode itemParent) {
exists(resolvePathPrivate(_, itemParent, _))
}

/**
* Gets a module that has access to private items defined inside `itemParent`.
*
* According to [The Rust Reference][1] this is either `itemParent` itself or any
* descendant of `itemParent`.
*
* [1]: https://doc.rust-lang.org/reference/visibility-and-privacy.html#r-vis.access
*/
pragma[nomagic]
private ModuleLikeNode getAPrivateVisibleModule(ModuleLikeNode itemParent) {
isItemParent(itemParent) and
result.getImmediateParentModule*() = itemParent
private predicate resolvesSourceFunction(RelevantPath path) {
resolvePathFull(path).(Function).fromSource()
}

/** Gets the item that `path` resolves to, if any. */
cached
ItemNode resolvePath(RelevantPath path) {
result = resolvePath1(path) and
result = resolvePathFull(path) and
(
result.isPublic()
// when a function exists in both source code and in library code, it is because
// we also extracted the source code as library code, and hence we only want
// the function from source code
result.fromSource()
or
path.requiresExtractorWorkaround()
)
or
exists(ModuleLikeNode itemParent, ModuleLikeNode pathParent |
result = resolvePathPrivate(path, itemParent, pathParent) and
pathParent = getAPrivateVisibleModule(itemParent)
not result instanceof Function
or
not resolvesSourceFunction(path)
)
}

pragma[nomagic]
private ItemNode resolvePathQualifier(RelevantPath path, string name) {
result = resolvePath(path.getQualifier()) and
result = resolvePathFull(path.getQualifier()) and
name = path.getText()
}

Expand Down Expand Up @@ -1164,7 +1131,7 @@ private ItemNode resolveUseTreeListItemQualifier(
pragma[nomagic]
private ItemNode resolveUseTreeListItem(Use use, UseTree tree) {
tree = use.getUseTree() and
result = resolvePath(tree.getPath())
result = resolvePathFull(tree.getPath())
or
result = resolveUseTreeListItem(use, tree, tree.getPath())
}
Expand Down
Loading