Skip to content

C#: Fix join order in InterpretedCallable characteristic predicate. #10433

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
Changes from 1 commit
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
25 changes: 18 additions & 7 deletions csharp/ql/lib/semmle/code/csharp/dataflow/ExternalFlow.qll
Original file line number Diff line number Diff line change
Expand Up @@ -504,15 +504,26 @@ class UnboundCallable extends Callable {
}
}

pragma[nomagic]
private predicate callableSpecInfo(Callable c, string namespace, string type, string name) {
c.getDeclaringType().hasQualifiedName(namespace, type) and
c.getName() = name
}

pragma[nomagic]
private predicate subtypeSpecCandidate(Callable c, UnboundValueOrRefType t) {
elementSpec(_, _, true, c.getName(), _, _, t)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks like a potentially bad join, as we will relate all calls with matching names. How about first joining on getASubTypeUnbound+() instead?

pragma[nomagic]
private predicate subtypeSpecCandidate(string name, UnboundValueOrRefType t) {
  exists(UnboundValueOrRefType t0 |
    elementSpec(_, _, true, name, _, _, t0) and
    t = t0.getASubTypeUnbound+()
  )
}

pragma[nomagic]
private predicate callableInfo(Callable c, string name, UnboundValueOrRefType decl) {
  name = c.getName() and
  decl = c.getDeclaringType()
}

and then use it in InterpretedCallable as

exists(string name, UnboundValueOrRefType t |
  callableInfo(this, name, t) and
  subtypeSpecCandidate(name, t)
)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I think you are right! However, in this particular case the join is worsened slightly.
Let us try with your solution and run DCA.

image

image

image

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

However, in this particular case the join is worsened slightly.

I think that is only because we are currently lucky that elementSpec(_, _, true, name, _, _, t) is rather small. But even so, if, say, elementSpec(_, _, "ToString", _, _, t) held, we would join with all ToString methods, which can be a large set.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes - I totally agree.

}

private class InterpretedCallable extends Callable {
InterpretedCallable() {
exists(UnboundValueOrRefType t, boolean subtypes, string name |
elementSpec(_, _, subtypes, name, _, _, t) and
this.hasName(name)
|
this.getDeclaringType() = t
or
subtypes = true and
exists(string namespace, string type, string name |
callableSpecInfo(this, namespace, type, name) and
elementSpec(namespace, type, _, name, _, _)
)
or
exists(UnboundValueOrRefType t |
subtypeSpecCandidate(this, t) and
this.getDeclaringType() = t.getASubTypeUnbound+()
)
}
Expand Down