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
base: main
Are you sure you want to change the base?
Conversation
…sing models-as-data.
There was a problem hiding this 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 } |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 👍
There was a problem hiding this comment.
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.
| string encodeContent(ContentSet cs, string arg) { | ||
| exists(FieldContent c | | ||
| cs.isSingleton(c) and | ||
| result = "Field" and | ||
| arg = c.getField().getName() | ||
| ) |
There was a problem hiding this comment.
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 👍
There was a problem hiding this comment.
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].
There was a problem hiding this comment.
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
FieldContentthough - 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.
There was a problem hiding this comment.
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)
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 betweenArgument[0].*fieldNameand*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 🤞.
| string encodeReturn(ReturnKind rk, string arg) { | ||
| rk != getStandardReturnValueKind() and | ||
| result = "ReturnValue" and | ||
| arg = rk.toString() | ||
| } |
There was a problem hiding this comment.
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 👍
| // 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 | ||
| ) |
There was a problem hiding this comment.
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.
| 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() | ||
| ) |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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. |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
| // 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 |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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'.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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).
There was a problem hiding this comment.
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 👍
|
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. |
|
I’ve fixed We do, however, introduce a new bodge in |
| @@ -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() } | |||
There was a problem hiding this comment.
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 { |
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() } |
There was a problem hiding this comment.
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:
| Element getCallTarget() { result = this.asCall().getAst().(Call).getTarget() } | |
| Element getCallTarget() { result = this.asCall().getStaticCallTarget() } |


Implement models-as-data for C++. That is, support for CSV formatted flow sources, sinks and summaries that look something like this:
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
getcand 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:
TODO: fix this, there's no good reason for it..encodeContent.@MathiasVP I would appreciate an early review and constructive feedback, when you're available.