Skip to content

Add lifetime package#1024

Open
knewbury01 wants to merge 11 commits intogithub:mainfrom
knewbury01:knewbury01/Lifetime
Open

Add lifetime package#1024
knewbury01 wants to merge 11 commits intogithub:mainfrom
knewbury01:knewbury01/Lifetime

Conversation

@knewbury01
Copy link
Collaborator

@knewbury01 knewbury01 commented Feb 3, 2026

Description

add rules for lifetime package, two shared rules, one with improvements upon previous, one as simple shared

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-11-6-2
    • RULE-6-8-3
  • Queries have been modified for the following rules:
    • rule number here

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

also adjust formatting on prev changenote which was incorrect
@knewbury01 knewbury01 self-assigned this Feb 3, 2026
Copy link
Collaborator

@MichaelRFairhurst MichaelRFairhurst left a comment

Choose a reason for hiding this comment

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

The test cases look great Kristen, thank you!

AllocatedObjectIdentity() {
this.(FunctionCall).getTarget().(AllocationFunction).requiresDealloc()
or
this = any(NewOrNewArrayExpr new | not exists(new.getPlacementPointer()))
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

borrowed this from here

@knewbury01 knewbury01 marked this pull request as ready for review March 10, 2026 16:38
Copilot AI review requested due to automatic review settings March 10, 2026 16:38
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR adds a new "Lifetime" rule package for MISRA C++ 2023, implementing two new rules: RULE-11-6-2 (reading uninitialized objects) and RULE-6-8-3 (automatic storage assigned to wider lifetime). It also enhances the shared ReadOfUninitializedMemory query to detect uninitialized reads through new expressions without value initialization, and extends the AllocatedObjectIdentity class to recognize new/new[] expressions as allocated storage.

Changes:

  • Adds the Lifetime rule package with RULE-11-6-2 and RULE-6-8-3, including exclusion metadata, query files, test references, and supporting infrastructure.
  • Enhances the ReadOfUninitializedMemory shared library to detect uninitialized memory accessed via new expressions (without value initialization), incorporating a copied InitializationFunctions.qll from the CodeQL standard library.
  • Extends AllocatedObjectIdentity in CppObjects.qll to include new/new[] expressions (excluding placement new) for storage duration tracking.

Reviewed changes

Copilot reviewed 17 out of 17 changed files in this pull request and generated 6 comments.

Show a summary per file
File Description
rules.csv Updates RULE-11-6-2 to reference the Lifetime package
rule_packages/cpp/Lifetime.json New rule package definition for RULE-11-6-2 and RULE-6-8-3
cpp/common/src/codingstandards/cpp/exclusions/cpp/Lifetime.qll Autogenerated exclusion module for Lifetime package
cpp/common/src/codingstandards/cpp/exclusions/cpp/RuleMetadata.qll Integrates Lifetime package into rule metadata
cpp/misra/src/rules/RULE-11-6-2/ValueOfAnObjectMustNotBeReadBeforeItHasBeenSet.ql New query for RULE-11-6-2 using shared ReadOfUninitializedMemory
cpp/misra/src/rules/RULE-6-8-3/AutomaticStorageAssignedToObjectGreaterLifetime.ql New query for RULE-6-8-3 using shared DoNotCopyAddressOfAutoStorage
cpp/common/src/codingstandards/cpp/rules/readofuninitializedmemory/ReadOfUninitializedMemory.qll Enhanced to handle new without value init
cpp/common/src/codingstandards/cpp/rules/readofuninitializedmemory/InitializationFunctions.qll Copied from CodeQL standard library for init function tracking
cpp/common/src/codingstandards/cpp/lifetimes/CppObjects.qll Extended AllocatedObjectIdentity for new/new[] expressions
cpp/common/test/rules/readofuninitializedmemory/test.cpp Added test cases for new uninitialized memory scenarios
cpp/common/test/rules/readofuninitializedmemory/ReadOfUninitializedMemory.expected Updated expected results for new test cases
cpp/common/test/rules/donotcopyaddressofautostorageobjecttootherobject/test.cpp Added test cases for auto-storage address escape
cpp/common/test/rules/donotcopyaddressofautostorageobjecttootherobject/DoNotCopyAddressOfAutoStorageObjectToOtherObject.expected Updated expected results for new test cases
cpp/misra/test/rules/RULE-6-8-3/AutomaticStorageAssignedToObjectGreaterLifetime.testref Test reference for RULE-6-8-3
cpp/misra/test/rules/RULE-11-6-2/ValueOfAnObjectMustNotBeReadBeforeItHasBeenSet.testref Test reference for RULE-11-6-2
change_notes/2026-02-03-uninitialized-mem-improve.md Change note for shared query modifications
.vscode/tasks.json Added Lifetime to VS Code task configuration
Comments suppressed due to low confidence (1)

cpp/common/src/codingstandards/cpp/rules/readofuninitializedmemory/ReadOfUninitializedMemory.qll:207

  • Typo in docstring: "Get a access" should be "Gets an access".
   * Get a access of the variable that is assumed to initialize the variable.

Comment on lines +21 to +29
module AutomaticStorageAssignedToObjectGreaterLifetimeConfig implements
DoNotCopyAddressOfAutoStorageObjectToOtherObjectConfigSig
{
Query getQuery() {
result = LifetimePackage::automaticStorageAssignedToObjectGreaterLifetimeQuery()
}
}

import DoNotCopyAddressOfAutoStorageObjectToOtherObject<AutomaticStorageAssignedToObjectGreaterLifetimeConfig>
Copy link

Copilot AI Mar 10, 2026

Choose a reason for hiding this comment

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

The query references DoNotCopyAddressOfAutoStorageObjectToOtherObjectConfigSig (line 21) and uses DoNotCopyAddressOfAutoStorageObjectToOtherObject<...> as a parameterized module (line 25), but the shared library at cpp/common/src/codingstandards/cpp/rules/donotcopyaddressofautostorageobjecttootherobject/DoNotCopyAddressOfAutoStorageObjectToOtherObject.qll does not define any ConfigSig signature module or parameterized module. It only uses the abstract class pattern (like M7-5-2/AssignmentOfEscapingAutoStorage.ql does). This will fail to compile. The query should either use the existing abstract class pattern (extending DoNotCopyAddressOfAutoStorageObjectToOtherObjectSharedQuery), or the .qll file needs to be refactored to support the parameterized module pattern.

Suggested change
module AutomaticStorageAssignedToObjectGreaterLifetimeConfig implements
DoNotCopyAddressOfAutoStorageObjectToOtherObjectConfigSig
{
Query getQuery() {
result = LifetimePackage::automaticStorageAssignedToObjectGreaterLifetimeQuery()
}
}
import DoNotCopyAddressOfAutoStorageObjectToOtherObject<AutomaticStorageAssignedToObjectGreaterLifetimeConfig>
/**
* Concrete query configuration for RULE-6-8-3 using the shared
* DoNotCopyAddressOfAutoStorageObjectToOtherObject infrastructure.
*/
class AutomaticStorageAssignedToObjectGreaterLifetimeQuery
extends DoNotCopyAddressOfAutoStorageObjectToOtherObjectSharedQuery
{
AutomaticStorageAssignedToObjectGreaterLifetimeQuery() {
this = LifetimePackage::automaticStorageAssignedToObjectGreaterLifetimeQuery()
}
}

Copilot uses AI. Check for mistakes.
cpp,MISRA-C++-2023,RULE-11-3-2,Yes,Advisory,Decidable,Single Translation Unit,The declaration of an object should contain no more than two levels of pointer indirection,A5-0-3,ImportMisra23,Import,
cpp,MISRA-C++-2023,RULE-11-6-1,Yes,Advisory,Decidable,Single Translation Unit,All variables should be initialized,,Declarations2,Easy,
cpp,MISRA-C++-2023,RULE-11-6-2,Yes,Mandatory,Undecidable,System,The value of an object must not be read before it has been set,A8-5-0,Lifetime,Very Hard,
cpp,MISRA-C++-2023,RULE-11-6-2,Yes,Mandatory,Undecidable,System,The value of an object must not be read before it has been set,A8-5-0,Lifetime,Import
Copy link

Copilot AI Mar 10, 2026

Choose a reason for hiding this comment

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

The RULE-11-6-2 row is missing a trailing comma after Import. All other rows in the CSV consistently end with a comma after the difficulty field (e.g., line 929: ...,Import,). This could cause parsing issues with downstream tools that process the CSV.

Suggested change
cpp,MISRA-C++-2023,RULE-11-6-2,Yes,Mandatory,Undecidable,System,The value of an object must not be read before it has been set,A8-5-0,Lifetime,Import
cpp,MISRA-C++-2023,RULE-11-6-2,Yes,Mandatory,Undecidable,System,The value of an object must not be read before it has been set,A8-5-0,Lifetime,Import,

Copilot uses AI. Check for mistakes.
int *p3 = new int(1);
*p3 = *p2; // NON_COMPLIANT -- the pointee of p2 has not been
// initialized
*p3; // NON_COMPLIANT[FALSE_NEGATIVE] -- the pointee of p3 has be overridden
Copy link

Copilot AI Mar 10, 2026

Choose a reason for hiding this comment

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

Typo in comment: "has be overridden" should be "has been overridden".

Suggested change
*p3; // NON_COMPLIANT[FALSE_NEGATIVE] -- the pointee of p3 has be overridden
*p3; // NON_COMPLIANT[FALSE_NEGATIVE] -- the pointee of p3 has been overridden

Copilot uses AI. Check for mistakes.

int *i3;

if (i3 == i1) { // NON_COMPLIANT
Copy link

Copilot AI Mar 10, 2026

Choose a reason for hiding this comment

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

Line 141 is annotated with a single // NON_COMPLIANT comment but produces two alerts in the expected file (lines 8-9): one for i3 (a genuinely uninitialized pointer) and one for i1 (initialized with new int — the pointer value itself is valid, only the pointee is uninitialized). The second alert for i1 in a pointer comparison i3 == i1 appears to be a false positive — comparing a pointer initialized via new int doesn't read uninitialized memory. The annotation should be updated to reflect this, for example by marking one result as NON_COMPLIANT and the other as COMPLIANT[FALSE_POSITIVE], or by adding a comment explaining the two alerts.

Copilot generated this review using guidance from repository custom instructions.

/** Get a read of the variable. */
/**
* Gets an access of the variable `v` which is not used as an lvalue, and not used as an argument
Copy link

Copilot AI Mar 10, 2026

Choose a reason for hiding this comment

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

The docstring references "the variable v" but there is no parameter v in this method. Since this is a method on UninitializedVariable, it should reference "this variable" or just "the variable" instead.

This issue also appears on line 207 of the same file.

Suggested change
* Gets an access of the variable `v` which is not used as an lvalue, and not used as an argument
* Gets an access of this variable which is not used as an lvalue, and not used as an argument

Copilot uses AI. Check for mistakes.
Comment on lines +265 to +266
or
this = any(NewOrNewArrayExpr new | not exists(new.getPlacementPointer()))
Copy link

Copilot AI Mar 10, 2026

Choose a reason for hiding this comment

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

The class docstring says "An object identified by a call to malloc" but now also includes new/new[] expressions (excluding placement new). The documentation should be updated to reflect the expanded scope, e.g. "An object identified by a dynamic allocation expression (malloc, new, new[])."

Copilot uses AI. Check for mistakes.
@mbaluda mbaluda removed their request for review March 12, 2026 16:58
Copy link
Collaborator

@MichaelRFairhurst MichaelRFairhurst left a comment

Choose a reason for hiding this comment

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

OK, there's a lot of text here, but I think it boils down to actually pretty simple changes, its just complicated to think through and fully explain the implications of X vs Y 😕 I would have written less if I olny had more time! :)

I'm 100% glad that you pursued the approach. I think with a small few tweaks this implementation will be very precise, and handling new int without a major overhaul was absolutely the correct choice. 🎉

Let me know if anything here is unclear or if I missed anything!

Comment on lines +149 to +155
//or is a builtin type used with new operator but there is no value initialization as far as we can see
exists(Initializer i, NewExpr n |
i = getInitializer() and
n = i.getExpr() and
this.getUnspecifiedType().stripType() instanceof BuiltInType and
//ignore value init
not exists(n.getAChild())
Copy link
Collaborator

Choose a reason for hiding this comment

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

Would this work?

Suggested change
//or is a builtin type used with new operator but there is no value initialization as far as we can see
exists(Initializer i, NewExpr n |
i = getInitializer() and
n = i.getExpr() and
this.getUnspecifiedType().stripType() instanceof BuiltInType and
//ignore value init
not exists(n.getAChild())
//or is a builtin type used with new operator but there is no value initialization as far as we can see
getInitializer().getExpr() instanceof NewNotInit

}
}

class NewNotInit extends NewExpr {
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 explaining the limitations somewhere in this file, maybe here?

Something like, "Catches new int; as an expression that doesn't initialize its value. Note that the pointer returned has been initialized, but the pointee has not. In our analysis, we simply count x as uninitialized in x = new int for now, though a more thorough analysis might track the initialization of x is and *x separately."

// Not a pointless read
not result = any(ExprStmt es).getExpr() and
// not involved in a new expr assignment since that does not define
not result instanceof VariableAccessOnLHSOfNonInitAssignment
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
not result instanceof VariableAccessOnLHSOfNonInitAssignment
not result = any(NonInitAssignment a).getLValue()

This class name is pretty complicated and makes the code seem maybe more challenging than it is.

not result = any(AssignExpr a).getLValue() and
// Not passed to another initialization function
not exists(Call c, int j | j = c.getTarget().(InitializationFunction).initializedParameter() |
result = c.getArgument(j).(AddressOfExpr).getOperand()
Copy link
Collaborator

Choose a reason for hiding this comment

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

Consider adding or result = c.getArgument(j), that would handle:

void f(int *p) {
  *p = 0;
}

void g() {
  int *p = new int;
  f(p); // compliant
  use(*p); // compliant
}

In fact we might want to rely on .isRValueCategory() and not exists(Call ... | ... result = c.getArgument(j)).

In general, if x appears in &x, that should mean imply not .isRValueCategory() for x. So technically, int *p = &x would previously have not been considered a use and now will be.

Overall, isRValueCategory() should find only cases where x could not be initialized (only reads + copies). So we were already very conservative in what we consider a use.

The catch is that copying or reading x may be how someone initializes *x, such as in the above example with f(p). So with our new x+*x analysis, we can't guarantee isRValueCategory() for x means we don't initialize *x. Really any rvalue use could lead to initialization of *x.

We also don't want to lose the ability to analyze uninitialized pointers more generally (as in, int *p; f(p); which is never valid and can segfault, and the current query should catch).

Ultimately, for *x vs x:

  • if *x is used as an rvalue, that's problematic if x had a non-init new
  • otherwise, all uses of x as an rvalue require that x had any initializer
  • If x is used as an lvalue in *x, that's problematic if x had no initializer
  • All other lvalue uses of x are valid
  • It's always valid if *x is used as an lvalue if x had any initializer

Ultimately, I think it would probably be fine to say:

  • track x = new int as an "uninitialized variable"
  • count rvalue x as a use if not new int
  • count lvalue x as a use if used in *x and not new int
  • count rvalue *x as a use if has new int
  • don't count lvalue *x as a use in and of itself

@@ -0,0 +1,2 @@
- `A8-5-0`, `EXP53-CPP`, `EXP33-C`, `RULE-9-1` - `MemoryNotInitializedBeforeItIsRead.ql`, `DoNotReadUninitializedMemory.ql`, `DoNotReadUninitializedMemory.ql`, `ObjectWithAutoStorageDurationReadBeforeInit.ql`:
- The queries listed now find uses of the operator 'new' where there is no value initialization provided. No newline at end of file
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 note that we don't yet finely track the initialization/use of p vs *p

And add a line that we're now using an existing library to more finely track initialization through functions?

int *p3 = new int(1);
*p3 = *p2; // NON_COMPLIANT -- the pointee of p2 has not been
// initialized
*p3; // NON_COMPLIANT[FALSE_NEGATIVE] -- the pointee of p3 has be overridden
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
*p3; // NON_COMPLIANT[FALSE_NEGATIVE] -- the pointee of p3 has be overridden
use(p3);
use(*p3); // NON_COMPLIANT[FALSE_NEGATIVE] -- the pointee of p3 has be overridden


int *p4;
p4 = new int;
*p4; // NON_COMPLIANT -- the pointee of p4 has not been
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
*p4; // NON_COMPLIANT -- the pointee of p4 has not been
use(*p4); // NON_COMPLIANT -- the pointee of p4 has not been
use(p4)

"severity": "error",
"short_name": "ValueOfAnObjectMustNotBeReadBeforeItHasBeenSet",
"shared_implementation_short_name": "ReadOfUninitializedMemory",
"tags": [
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 an implementation_scope note that we don't track class member initialization?

{
"description": "When storage durations are not compatible between assigned pointers it can lead to referring to objects outside of their lifetime, which is undefined behaviour.",
"kind": "problem",
"name": "Declare objects with appropriate storage durations",
Copy link
Collaborator

Choose a reason for hiding this comment

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

Should the name be about assignments of different lifetimes instead of declarations?

int *p4;
p4 = new int;
*p4; // NON_COMPLIANT -- the pointee of p4 has not been
// initialized
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 add a test for initialization via a function?

int *p5;
initialize(p5); // NON_COMPLIANT -- segfault

int *p6 = new int;
initialize(p6) // COMPLIANT

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.

3 participants