Visitar URL original
C++: Implement models-as-data by geoffw0 · Pull Request #15371 · github/codeql · GitHub
The Wayback Machine - https://web.archive.org/web/20240125083528/https://github.com/github/codeql/pull/15371
Skip to content
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

C++: Implement models-as-data #15371

Draft
wants to merge 16 commits into
base: main
Choose a base branch
from
Draft

C++: Implement models-as-data #15371

wants to merge 16 commits into from

Conversation

geoffw0
Copy link
Contributor

@geoffw0 geoffw0 commented Jan 18, 2024

Implement models-as-data for C++. That is, support for CSV formatted flow sources, sinks and summaries that look something like this:

    ";;false;getc;;;ReturnValue;remote",

The implementation is ported from Swift, and uses the shared MAD library to do the heavy lifting. I've created a range of "synthetic" tests (that is, tests that use models defined in the tests), and also created "real" sources for getc and friends (which have corresponding "real" tests).

This is currently a draft PR, and is missing most of the results it should find in tests. Things I need to do:

  • address TODO: fix this, there's no good reason for it..
  • finish implementation of encodeContent.
  • get sinks working
    • and add / convert a query sink to MAD?
  • get summaries working
    • and add / convert a summary model to MAD?
  • get indirection working
    • syntax TBD
  • check performance on DCA
    • there are a couple of “pre-requisites” we expect to need to fix here
  • add change note(s)

@MathiasVP I would appreciate an early review and constructive feedback, when you're available.

Copy link
Contributor

@MathiasVP MathiasVP left a comment

Choose a reason for hiding this comment

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

Some initial comments. Looking good so far! I think starting simply with sources and sinks was a great idea as it avoids you having to deal with a large part of the dataflow library right away!

NormalReturnKind() { this = TNormalReturnKind(index) }
NormalReturnKind() { this = TNormalReturnKind(indirectionIndex) }

int getIndirectionIndex() { result = indirectionIndex }
Copy link
Contributor

Choose a reason for hiding this comment

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

I think you can actually move the int indirectionIndex field, and the getIndirectionIndex method. up to the ReturnKind class now.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Moving indirectionIndex itself means ReturnKind would need an ugly constructor to bind it. I've moved just the getIndirectionIndex method, so that is now available consistently.

Copy link
Contributor

Choose a reason for hiding this comment

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

That's fine with me. The other solution would be something like:

class ReturnKind extends TReturnKind {
  int indirectionIndex;
  
  bindingset[indirectionIndex]
  ReturnKind() { any() }

  final int getIndirectionIndex() { result = indirectionIndex }
}

but I don't really mind whichever one you prefer 👍

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ahah, I was missing the possibility of using bindingset like that. I'll try it tomorrow.

Comment on lines +30 to +35
string encodeContent(ContentSet cs, string arg) {
exists(FieldContent c |
cs.isSingleton(c) and
result = "Field" and
arg = c.getField().getName()
)
Copy link
Contributor

Choose a reason for hiding this comment

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

A FieldContent is defined by a field name and an indirection index. So eventually this will need to include the indirection index in the syntax somehow - either through the arg column or the result column. Right now I think we're just conflating all indirections of a field when using encodeContent) which is probably fine for now 👍

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 we are conflating indirections, and it's definitely something I want to fix (though not necessarily in this PR - as you say it's probably fine for now). I hadn't realized indirection is built into FieldContent though - why is that? I was imagining it as perhaps its own content type, so you might have an access path something like Argument[0].fieldName.Indirect[1] or ReturnValue.Indirect[2].

Copy link
Contributor

@MathiasVP MathiasVP Jan 23, 2024 •

Choose a reason for hiding this comment

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

I hadn't realized indirection is built into FieldContent though - why is that?

A FieldContent is defined as a pair consisting of a field and an indirection index because a read of a FieldContent (field, n) corresponds to flow through an FieldAddressInstruction that's associated with field, followed by flow through n LoadInstructions (see here).

Other languages have both FieldContent and ArrayContent which splits out this into two contents, but when we tried to do in C/C++ a long time ago and gave huge performance problems because of the way the shared dataflow library calculated the list of successor contents that could follow an ArrayContent.

Copy link
Contributor

Choose a reason for hiding this comment

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

I was imagining it as perhaps its own content type, so you might have an access path something like Argument[0].fieldName.Indirect[1] or ReturnValue.Indirect[2].

We could probably implement that syntax, but the syntax that would naturally follow from the way we've designed the C/C++ content-based flow is Argument[0].fieldName[1] (or something like Argument[0].*fieldName which I'd probably prefer since it looks more like C/C++ syntax)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We'll need the same syntax for return values (e.g. *ReturnValue) and for non-field arguments / parameters, like maybe *Argument[0]. Then there's the difference between Argument[0].*fieldName and *Argument[0].fieldName. It might get a bit fiddly, but if indirections are robustly supported at the dataflow level this should all be do-able.

Copy link
Contributor

Choose a reason for hiding this comment

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

We'll need the same syntax for return values (e.g. *ReturnValue) and for non-field arguments / parameters, like maybe *Argument[0]. Then there's the difference between Argument[0].*fieldName and *Argument[0].fieldName.

Indeed, Argument[0].*fieldName would be "the value pointed to by the fieldName field of the 0'th argument" and *Argument[0].fieldName would be "the value of fieldName that you can get to by dereferencing the 0'th argument".

It might get a bit fiddly, but if indirections are robustly supported at the dataflow level this should all be do-able.

I really hope so 😂. That is certainly my current belief 🤞.

Comment on lines +24 to +28
string encodeReturn(ReturnKind rk, string arg) {
rk != getStandardReturnValueKind() and
result = "ReturnValue" and
arg = rk.toString()
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Since the toString on return kinds doesn't fully determine the return kind (because the indirection index isn't part of the toString) the MaD syntax will conflate all return kind indirections. That's probably fine for now, but we should fix this before claiming any MaD support 👍

Comment on lines +177 to +184
// Allow fields to be picked as output nodes.
exists(Node n, Element ast |
n = node.asNode() and
ast = mid.asElement()
|
c = "" and
n.asExpr().(VariableAccess).getTarget() = ast
)
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm not sure we should have this initially for C/C++. We had a clear need for it in Swift which is why we added it. But I don't think we should blindly do the same for C/C++ until we have a use-case for adding anything here.

Comment on lines +190 to +203
exists(Node n, Element ast, VariableAccess e |
n = node.asNode() and
ast = mid.asElement() and
e.getTarget() = ast
|
// Allow fields to be picked as input nodes.
c = "" and
e.getQualifier() = n.asExpr()
or
// Allow post update nodes to be picked as input nodes when the `input` column
// of the row is `PostUpdate`.
c = "PostUpdate" and
e.getQualifier() = n.(PostUpdateNode).getPreUpdateNode().asExpr()
)
Copy link
Contributor

Choose a reason for hiding this comment

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

Similarly, I don't think we should include anything here until we have a clear use-case for it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The counter-argument is that it's frustrating when you're writing CSV models to find that basic features aren't supported, so you have to pause your work and go and implement them.

That said, I should ensure this case has test coverage if I'm to keep it.

string namespace, string type, boolean subtypes, string name, string signature
) {
elementSpec(namespace, type, subtypes, name, signature, _) and
namespace = "" and // TODO: Fill out when we properly extract modules.
Copy link
Contributor

Choose a reason for hiding this comment

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

I actually think namespace should be used now to support selecting functions in certain namespaces. We can use Declaration.hasQualifiedName/2 for this.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good idea. This limitation is copied from Swift and has no place in C++.

Note to self: needs a test as well.

cpp/ql/lib/semmle/code/cpp/dataflow/ExternalFlow.qll Outdated Show resolved Hide resolved
Comment on lines +423 to +437
// Member variables
signature = "" and
exists(Class namedClass, Class classWithMember, MemberVariable member |
member.getName() = name and
member = classWithMember.getAMember() and
namedClass.getName() = type and
result = member
|
// field declared in the named type or a subtype of it (or an extension of any)
subtypes = true and
classWithMember = namedClass.getADerivedClass*()
or
// field declared directly in the named type (or an extension of it)
subtypes = false and
classWithMember = namedClass
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm not quite convinced we need these for our own use-cases just yet. Could we delay this part until we have a concrete case where we need this?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Supporting derived classes is just what the subtypes field does, and it's there, so I feel it should work. The comments are off though - C++ doesn't have 'extensions'.

Copy link
Contributor

Choose a reason for hiding this comment

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

I mean the whole "member variables" thing. The only query I know that uses a member variable as a source is the cpp/cleartext-transmission query and that one has quite a lot of machinery for selecting variables with appropriate names, so I don't think this query will be a candidate for MaD initially.

Thus, I think we should wait with the whole "member variable" case in this predicate.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The lack of (or incomplete?) support for variables as sources/sinks is one of the things that frustrated me with the initial Swift MAD implementation, until we fixed it. I strongly believe leaving this out is just leaving a trap for someone in near future who expects it to 'just work'.

Also this is already tested and, I think, working (see memberRemoteMadSourceVar in the test).

Copy link
Contributor

Choose a reason for hiding this comment

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

Alright, that's fair. If you believe this will be useful then let's keep it 👍

@geoffw0
Copy link
Contributor Author

geoffw0 commented Jan 23, 2024

Thanks for the early review @MathiasVP . It's going to take me a bit of time to address everything, along with the known issues in this PR.

@geoffw0
Copy link
Contributor Author

geoffw0 commented Jan 24, 2024

I’ve fixed TInterpretNode by removing the TDataFlowCall_ case - which is redundant as the Element case can already represent a call, and that ambiguity was causing stuff to get lost. Fixed up asCall and getCalltarget in that class. We get more results now, and we don’t need the bodge in shared/dataflow any more.

We do, however, introduce a new bodge in TReturnKind, which I believe will is a symptom of not having addressed the ‘pre-requisite’ tasks yet, so I’ll be addressing that next.

@@ -142,13 +141,13 @@ module SourceSinkInterpretationInput implements
Node asNode() { this = TNode_(result) }

/** Gets the call that this node corresponds to, if any. */
DataFlowCall asCall() { this = TDataFlowCall_(result) }
DataFlowCall asCall() { this.asElement() = result.getAst() }
Copy link
Contributor

@MathiasVP MathiasVP Jan 24, 2024 •

Choose a reason for hiding this comment

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

Could you use result.getUnconvertedResultExpression() and achieve the same thing here? Multiple instructions may give the same result for getAst (in case a single AST element is associated with multiple instructions).

Using getAst here may work simple because the qualifier of getAst has to be a CallInstruction (a DataFlowCall is just a CallInstruction:

class DataFlowCall extends CallInstruction {
), and there's probably only one CallInstruction associated with each function call. But I don't think we should rely on this property (which may not even be true in general).

DataFlowCallable asCallable() { result.(Function) = this.asElement() }

/** Gets the target of this call, if any. */
Element getCallTarget() { result = this.asCall().getAst().(Call).getTarget() }
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this is equivalent, but avoids going to the AST world:

Suggested change
Element getCallTarget() { result = this.asCall().getAst().(Call).getTarget() }
Element getCallTarget() { result = this.asCall().getStaticCallTarget() }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants