Add new library, backported to a small handful of queries, for resolving types.
Description
There are problems with the APIs on types for handling typedefs, decltypes, and specifiers, etc. Some of them resolve typedefs and some of them don't, and its not clear. Some of them have incorrect documentation. Some of them have bugs (for instance, ArrayType.resolveTypedefs()). These APIs also have a fundamental flaw: they assume that a given type exists in the database. For instance, if type T has a typedef T_t, then const T_t may exist in the database and const T may not.
At one point I decided to make a new API for these methods that was more clear, and I created PossiblySpecified<T>::Type. One of the important points in PossiblySpecified<T> was that it doesn't resolve typedefs, so that the type T can be a typedef type such as uint8_t. However, what if someone makes a typedef of uint8_t, for instance, typedef uint8_t ubyte_t? Well in this case, PossiblySpecified<Uint8Type> won't match ubtyte_t. So clearly we need to resolve some typedefs. But if we try to write a query that uses expr.getType().resolveTypedefs() instanceof Uint8Type, we'll have no results.
Furthermore, we will have a lot of places in our queries where we want to treat T and T& the same.
So we really want an API that:
- uses clear naming for all of the types of resolution it might perform
- resolves typedefs/decltypes until it matches something of interest and no further
- additionally resolves specifiers only when asked to
- can require certain specifiers to match types (to search for const and/or volatile types) when asked to
- doesn't presume arbitrary types exist in the database
- hopefully has some guard rails to encourage the correct behavior, and few trap cases.
This PR introduces a candidate API and I welcome feedback on it.
Instead of expr.getUnderlyingType() instanceof PointerType, you can write expr.getType() instanceof ResolvesTo<PointerType>::Exactly. This should raise some hairs "what do you mean by exactly?" That's because getUnderlyingType() instanceof PointerType() is usually wrong, or at least incomplete. Exactly means it will not match a cv-qualified pointer type. In this case, typically what we want to write is expr.getType() instanceof ResolvesTo<PointerType>::IgnoringSpecifiers.
Other options exist: ResolvesTo<PointerType>::CvConst will match a const pointer or const volatile pointer. ResolvesTo<PointerType>::Ref matches a reference to a pointer type. ResolvesTo<PointerType>::CvConstRef matches a reference to a const pointer (not to be confused with a const reference to a pointer, which doesn't exist!).
Added some additional classes PointerTo<T>::Type, ReferenceOf<T>::Type, that work well with this API, such that you can write ResolvesTo<PointerTo<FooType>::Type>::IgnoringSpecifiers. Also RawConstType, ConstType, NonConstType.
In addition to replacing the prior usages of PossiblySpecified<T>, I also integrated this API into Jeongsoo's recent query, for comparison, and one other random query I found via grep that was using getUnderlyingType.
Comments and feedback and bikeshedding welcome!
These are some of the improvements I'd like to see in it one day myself:
- Additional features like
ResolvesTo<IntType>::Pointer. - Some improved support for specifiers+references, not just
CvConstRef. This is complicated by CodeQL's handling of nested specified types (it doesn't!). - Some support for
RefOrNonRefkind of concepts (e.g., I want to match bothTandT&).
Additional ideas?
Change request type
- [ ] Release or process automation (GitHub workflows, internal scripts)
- [ ] Internal documentation
- [ ] External documentation
- [x] Query files (
.ql,.qll,.qlsor unit tests) - [ ] External scripts (analysis report or other code shipped as part of a release)
Rules with added or modified queries
- [ ] No rules added
- [ ] Queries have been added for the following rules:
- rule number here
- [x] Queries have been modified for the following rules:
-
INT36-C -
RULE-22-12 -
RULE-22-13 -
RULE-22-14 -
RULE-9-5-1
-
Release change checklist
A change note (development_handbook.md#change-notes) is required for any pull request which modifies:
- The structure or layout of the release artifacts.
- The evaluation performance (memory, execution time) of an existing query.
- The results of an existing query in any circumstance.
If you are only adding new rule queries, a change note is not required.
Author: Is a change note required?
- [x] Yes
- [ ] No
🚨🚨🚨 Reviewer: Confirm that format of shared queries (not the .qll file, the .ql file that imports it) is valid by running them within VS Code.
- [ ] Confirmed
Reviewer: Confirm that either a change note is not required or the change note is required and has been added.
- [ ] Confirmed
Query development review checklist
For PRs that add new queries or modify existing queries, the following checklist should be completed by both the author and reviewer:
Author
- [x] Have all the relevant rule package description files been checked in?
- [x] Have you verified that the metadata properties of each new query is set appropriately?
- [x] Do all the unit tests contain both "COMPLIANT" and "NON_COMPLIANT" cases?
- [x] Are the alert messages properly formatted and consistent with the style guide?
- [ ] Have you run the queries on OpenPilot and verified that the performance and results are acceptable?
As a rule of thumb, predicates specific to the query should take no more than 1 minute, and for simple queries be under 10 seconds. If this is not the case, this should be highlighted and agreed in the code review process. - [x] Does the query have an appropriate level of in-query comments/documentation?
- [x] Have you considered/identified possible edge cases?
- [x] Does the query not reinvent features in the standard library?
- [x] Can the query be simplified further (not golfed!)
Reviewer
- [ ] Have all the relevant rule package description files been checked in?
- [ ] Have you verified that the metadata properties of each new query is set appropriately?
- [ ] Do all the unit tests contain both "COMPLIANT" and "NON_COMPLIANT" cases?
- [ ] Are the alert messages properly formatted and consistent with the style guide?
- [ ] Have you run the queries on OpenPilot and verified that the performance and results are acceptable?
As a rule of thumb, predicates specific to the query should take no more than 1 minute, and for simple queries be under 10 seconds. If this is not the case, this should be highlighted and agreed in the code review process. - [ ] Does the query have an appropriate level of in-query comments/documentation?
- [ ] Have you considered/identified possible edge cases?
- [ ] Does the query not reinvent features in the standard library?
- [ ] Can the query be simplified further (not golfed!)