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#: Add VS Code model editor queries #14200

Merged
merged 22 commits into from
Sep 28, 2023

Conversation

koesie10
Copy link
Member

This adds two queries that support the CodeQL Model Editor feature in the CodeQL VS Code extension. These queries will be used to retrieve models that can be modeled using MaD. The query results they produce can also be converted to SARIF, hence the static strings in the query.

The AutomodelVsCode.qll file is similar to ExternalApi.qll, but they are used for completely different purposes, and changes to the ExternalApi.qll do not necessarily need to propagate to AutomodelVsCode.qll.

These queries have been in use for a few weeks now in the VS Code extension (see csharp.ts). Using this, we have confirmed that these queries return the results we need. Once these queries are merged and released, we will switch to resolving the queries from the query packs. We will do so based on the tags.

@github-actions github-actions bot added the C# label Sep 13, 2023
@koesie10 koesie10 marked this pull request as ready for review September 13, 2023 12:45
@koesie10 koesie10 requested a review from a team as a code owner September 13, 2023 12:45
@koesie10 koesie10 added the no-change-note-required This PR does not need a change note label Sep 13, 2023
csharp/ql/src/utils/modeleditor/AutomodelVsCode.qll Outdated Show resolved Hide resolved
csharp/ql/src/utils/modeleditor/AutomodelVsCode.qll Outdated Show resolved Hide resolved
csharp/ql/src/utils/modeleditor/AutomodelVsCode.qll Outdated Show resolved Hide resolved
csharp/ql/src/utils/modeleditor/AutomodelVsCode.qll Outdated Show resolved Hide resolved
csharp/ql/src/utils/modeleditor/AutomodelVsCode.qll Outdated Show resolved Hide resolved
csharp/ql/src/utils/modeleditor/AutomodelVsCode.qll Outdated Show resolved Hide resolved
csharp/ql/src/utils/modeleditor/AutomodelVsCode.qll Outdated Show resolved Hide resolved
csharp/ql/src/utils/modeleditor/AutomodelVsCode.qll Outdated Show resolved Hide resolved
csharp/ql/src/utils/modeleditor/AutomodelVsCode.qll Outdated Show resolved Hide resolved
@michaelnebel
Copy link
Contributor

This adds two queries that support the CodeQL Model Editor feature in the CodeQL VS Code extension. These queries will be used to retrieve models that can be modeled using MaD. The query results they produce can also be converted to SARIF, hence the static strings in the query.

The AutomodelVsCode.qll file is similar to ExternalApi.qll, but they are used for completely different purposes, and changes to the ExternalApi.qll do not necessarily need to propagate to AutomodelVsCode.qll.

These queries have been in use for a few weeks now in the VS Code extension (see csharp.ts). Using this, we have confirmed that these queries return the results we need. Once these queries are merged and released, we will switch to resolving the queries from the query packs. We will do so based on the tags.

This is really cool!
Thank you very much for doing this!!!

@koesie10
Copy link
Member Author

@michaelnebel Thank you for the review! I've implemented your feedback, although I'm not very familiar with CodeQL, so I'm not entirely sure about the changes for the unbound declarations.

Copy link
Contributor

@michaelnebel michaelnebel left a comment

Choose a reason for hiding this comment

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

Thank you once again for doing this!
I have added a couple of more comments. Besides the comments in the code, it also worth considering, whether we need a DCA test suite for these queries as they are excluded from all existing test suites (in the selectors). When doing changes to the queries going forward, it is nice to have some tooling to verify that we don't break the results they produce and/or breaks their performance.

For consistency, I will also trigger a DCA run for one of our query suites for check that these queries are indeed excluded by the selector update.

csharp/ql/src/utils/modeleditor/ModelEditor.qll Outdated Show resolved Hide resolved
csharp/ql/test/utils/modeleditor/PublicClass.cs Outdated Show resolved Hide resolved
| PublicGenericInterface.cs:7:10:7:14 | stuff | GitHub.CodeQL.PublicGenericInterface<>#stuff(T) | false | supported | PublicGenericInterface.cs | library | | type | unknown | classification |
| PublicGenericInterface.cs:9:10:9:19 | stuff2<> | GitHub.CodeQL.PublicGenericInterface<>#stuff2<>(T2) | false | supported | PublicGenericInterface.cs | library | | type | unknown | classification |
| PublicGenericInterface.cs:11:17:11:27 | staticStuff | GitHub.CodeQL.PublicGenericInterface<>#staticStuff(System.String) | false | supported | PublicGenericInterface.cs | library | | type | unknown | classification |
| PublicInterface.cs:7:10:7:14 | stuff | GitHub.CodeQL.PublicInterface#stuff(System.String) | false | supported | PublicInterface.cs | library | | type | unknown | classification |
Copy link
Contributor

Choose a reason for hiding this comment

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

All the callables are reported to be not supported as there doesn't exist any models for them.
Maybe consider to add some positive testcases for this as well.
It is possible to add a data extension file that only applies to an individual test case (codeql test run looks for .ext.yml files with the same name as the test case). That is, if you add a file called FetchFrameworkModeMethods.ext.yml you can add models for some of your testmethods.

This will implicitly help in testing that we got the predicates for isSupported right (e.g. getAnInput and getAnOutput). The ApplicationMode testcase primarily targets "source" models.

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks for the suggestion! I've added 4 new methods with the 4 different types of models. The summaryModel and neutralModel seem to work, but I can't get the sinkModel or sourceModel to work. Would you be able to take a look and check why those are not working?

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 the problem is that the implementation requires that for a callable to be identified as a source or sink there needs to be an actual call to it (this works fine for the queries that are just interested in callables NOT the source code). We need to do something else for the analysis that should respect source and sink models for EndPoints in the source code (I will look into this).

@michaelnebel
Copy link
Contributor

@michaelnebel Thank you for the review! I've implemented your feedback, although I'm not very familiar with CodeQL, so I'm not entirely sure about the changes for the unbound declarations.

Thank you and totally understandable! You are doing great!

@michaelnebel
Copy link
Contributor

DCA using the code-scanning query suite looks good (these queries are not executed).

@koesie10
Copy link
Member Author

Besides the comments in the code, it also worth considering, whether we need a DCA test suite for these queries as they are excluded from all existing test suites (in the selectors). When doing changes to the queries going forward, it is nice to have some tooling to verify that we don't break the results they produce and/or breaks their performance.

How would I go about doing that? Would I need to create a new .qls file and add this to DCA?

/** Provides classes and predicates related to handling APIs for the VS Code extension. */

private import csharp
private import dotnet
Copy link
Contributor

Choose a reason for hiding this comment

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

No need to import this (and no need to use DotNet::).

* Otherwise the name of the nested type is prefixed with a `+` and appended to
* the name of the enclosing type, which might be a nested type as well.
*/
private string nestedName(Declaration declaration) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Can be restricted to types instead of declarations in general

}

/** Gets a node that is an output from a call to this API. */
private DataFlow::Node getAnOutput() {
Copy link
Contributor

Choose a reason for hiding this comment

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

This will include post-update nodes for arguments, e.g. for a call

Foo(x)

[post-update] x will be included, which is likely not what you want. Instead I suggest you use getAnOutNode from DataFlowDispatch.qll which will only include normal returns and out/ref returns.

Comment on lines 125 to 128
endpoint.isSupported() and result = true
or
not endpoint.isSupported() and
result = false
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
endpoint.isSupported() and result = true
or
not endpoint.isSupported() and
result = false
if endpoint.isSupported() then result = true else result = false

@michaelnebel
Copy link
Contributor

Besides the comments in the code, it also worth considering, whether we need a DCA test suite for these queries as they are excluded from all existing test suites (in the selectors). When doing changes to the queries going forward, it is nice to have some tooling to verify that we don't break the results they produce and/or breaks their performance.

How would I go about doing that? Would I need to create a new .qls file and add this to DCA?

Yes. I have added one here: https://github.com/github/codeql-dca/pull/1663
Also trying to run this suite against the PR now.

@michaelnebel
Copy link
Contributor

DCA Experiment only on this branch: https://github.com/github/codeql-dca-main/issues/16281

@michaelnebel
Copy link
Contributor

DCA looks good in terms of performance.
@koesie10 : For larger projects the queries can take more than 5 minutes to execute (at least on actions), but I would also expect significant waiting time locally for results:
image

@michaelnebel
Copy link
Contributor

@hvitved : Do you have an objections on merging as is?

@michaelnebel
Copy link
Contributor

Other comments:
(1) If this should be tested via DCA, we need to add the flag -X disable-sarif-files=true to the DCA run for the time being (due to a high number of results).
(2) @koesie10 : If you are planning to integrate this queries in VS Code (or somehow expose these to customers), I would recommend trying to execute them against large databases (maybe use MRVA for this).

Copy link
Contributor

@michaelnebel michaelnebel left a comment

Choose a reason for hiding this comment

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

This looks good to me.
Lets try an run DCA again (I will trigger one more execution).

@michaelnebel
Copy link
Contributor

michaelnebel commented Sep 26, 2023

It looks like the model editor queries are now executed via DCA.
Please note that in the DCA experiment https://github.com/github/codeql-dca-main/issues/16420 Meta Queries actually are the Un-interpreted queries (this is merely a typo that will be fixed soon). Furthermore, the large discrepancy is the diagmetric and uninterpreted queries are causing by sharing cache. The execution against the feature branch executes the model editor queries before the diagmetric queries, which means that the dataflow part of the cache is created at this point in time.

I think the numbers look reasonable.

@michaelnebel michaelnebel requested review from hvitved and removed request for hvitved September 28, 2023 06:20
@koesie10 koesie10 merged commit 0f4f987 into main Sep 28, 2023
14 checks passed
@koesie10 koesie10 deleted the koesie10/add-csharp-model-editor-queries branch September 28, 2023 08:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C# no-change-note-required This PR does not need a change note
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants