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

Implement InvalidMemory3, Rule 18-8 amendment. #750

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

MichaelRFairhurst
Copy link
Contributor

Description

Implements package InvalidMemory3, which detects pointer-to-array conversions and modification of array values with temporary lifetimes, (RULE-18-9) and bans pointers to variably modified types (RULE-18-10).

RULE-18-10 is marked as "split" from RULE-18-8 in the amendments file, so update RULE-18-8 to only detect allocating VLA declarations. In the process, remove potential false positives around incomplete array parameter types, eg, f(int arr[]). To protect against the same false positives from occurring in RULE-18-10, use locations that correspond with VlaDimensionStmt instances.

If it is better to split this PR up, I'm happy to do so!

Change request type

  • Release or process automation (GitHub workflows, internal scripts)
  • Internal documentation
  • External documentation
  • Query files (.ql, .qll, .qls or 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-18-9
    • RULE-18-10
  • Queries have been modified for the following rules:
    • RULE-18-8

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?

  • 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

  • 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!)

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!)

Copy link
Collaborator

@lcartey lcartey left a comment

Choose a reason for hiding this comment

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

Thanks! Some suggestions and comments to follow up on individual files.


// A typedef is not a VLA. However, `VlaDeclStmt`s match the typedef.
typedef int vlaTypedef[n]; // COMPLIANT[FALSE_POSITIVE]
vlaTypedef t1; // NON_COMPLIANT[FALSE_NEGATIVE]
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think we should remove the false positive and false negative markers here, and accept the existing results as correct.

I justify this in two ways:

  1. It's more developer friendly to highlight the typedef that introduces the VLA, rather than the actual declaration. It's where they would likely need to fix the problem, and it reduces the number of results that need to be managed vs. every use of the typedef.
  2. I think that's consistent with how MISRA intended to report the results. This forum post https://forum.misra.org.uk/archive/index.php?thread-1384.html, while not being 100% clear, I think is indicative that typedefs themselves should be considered a use of variable-length arrays.

arrayType = v.getVariable().getType()
or
arrayType = v.getType().getUnspecifiedType()
) and
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can you add a comment to the two cases above, noting that the getVariable() holds if this is a direct declaration of a variable, and the .getType() case holds is this is a typedef (but not vice versa). I wasn't sure when I first read this why we had both cases.

select v, "Variable length array declared."
size = v.getVlaDimensionStmt(0).getDimensionExpr() and
(
arrayType = v.getVariable().getType()
Copy link
Collaborator

Choose a reason for hiding this comment

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

Do we need to strip top-level specifiers here as well?

(
arrayType = v.getVariable().getType()
or
arrayType = v.getType().getUnspecifiedType()
Copy link
Collaborator

Choose a reason for hiding this comment

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

Rather than getUnspecifiedType(), I think it would be better to use stripTopLevelSpecifiers. getUnspecifiedType resolves typedefs, which can be confusing if the base type of the array is a complex type in itself.

or
innerType = this.(TypedefType).getBaseType()
or
innerType = this.(SpecifiedType).getBaseType()
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can we simplify this disjunctive clause by using DerivedType.getBaseType()?

s.getADeclaration() = entry.getDeclaration() and
before = s.getLocation() and
after = before and
before.subsumes(inner)
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think we should consider what happens if the declaration is in a macro. Specifically, what happens if a macro has multiple array declarations, only one of which is a VM. I suspect we'll end up flagging all of them, as they will all have the same location.

Note: that is not a problem for the parameter case, because we require a strict before/after location.

getUnconverted().getUnspecifiedType() instanceof StructOrUnionTypeWithArrayField and
not isCLValue(this)
or
this.(ArrayExpr).getArrayBase() instanceof TemporaryLifetimeArrayAccess
Copy link
Collaborator

Choose a reason for hiding this comment

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

Should this case also use getUnconverted().getUnspecifiedType()?

or
result = expr.(BinaryOperation).getRightOperand()
or
result = expr.(UnaryOperation).getOperand()
Copy link
Collaborator

Choose a reason for hiding this comment

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

The three above can be replaced with Operation.getAnOperand()

* A pointer-to-array conversion does not need to be flagged if the result of
* that conversion is not used or stored.
*/
Expr usedValuesOf(Expr expr) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

The parameter is unused. I'd suggest using the same model as isStored(..), and instead do:

predicate isUsed(Expr e) {

I would also integrate the isStored predicate here.

predicate isStored(Expr e) {
e = any(VariableDeclarationEntry d).getDeclaration().getInitializer().getExpr()
or
e = any(ClassAggregateLiteral l).getAFieldExpr(_)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Integrate this predicate into the usedValuesOf predicate.

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

Successfully merging this pull request may close these issues.

2 participants