Skip to content

Michaelrfairhurst/rule 4 1 3 detect data races#1077

Open
MichaelRFairhurst wants to merge 6 commits intomichaelrfairhurst/package-undefined-behaviorfrom
michaelrfairhurst/rule-4-1-3-detect-data-races
Open

Michaelrfairhurst/rule 4 1 3 detect data races#1077
MichaelRFairhurst wants to merge 6 commits intomichaelrfairhurst/package-undefined-behaviorfrom
michaelrfairhurst/rule-4-1-3-detect-data-races

Conversation

@MichaelRFairhurst
Copy link
Collaborator

@MichaelRFairhurst MichaelRFairhurst commented Mar 10, 2026

Description

One commonly referred to kind of undefined behavior in the standard is data races. We can import this from our c query.

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-4-1-3
  • Queries have been modified for the following rules:
    • DIR-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?

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

@MichaelRFairhurst MichaelRFairhurst marked this pull request as ready for review March 10, 2026 00:59
Copilot AI review requested due to automatic review settings March 10, 2026 00:59
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 refactors the existing C MISRA DIR-5-1 data race detection query into a shared library (PossibleDataRaceBetweenThreadsShared.qll) and adds a new C++ MISRA 2023 RULE-4-1-3 query that reuses the same shared implementation. The shared library uses a parameterized module pattern with a config signature to support different object identity and sub-object models for C vs C++.

Changes:

  • Extracted the inline data race detection logic from c/misra/src/rules/DIR-5-1/PossibleDataRaceBetweenThreads.ql into a new shared library PossibleDataRaceBetweenThreadsShared.qll under cpp/common/src/codingstandards/cpp/rules/.
  • Added a new C++ MISRA 2023 query RULE-4-1-3/PossibleDataRaceBetweenThreads.ql that instantiates the shared library with C++ object/sub-object types.
  • Added test infrastructure (shared test files, .testref files, .expected files, exclusions, and metadata) for both the C and C++ variants.

Reviewed changes

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

Show a summary per file
File Description
rule_packages/cpp/Undefined.json Adds new PossibleDataRaceBetweenThreads query entry under RULE-4-1-3
rule_packages/c/Concurrency9.json Adds shared_implementation_short_name to DIR-5-1 query
cpp/misra/src/rules/RULE-4-1-3/PossibleDataRaceBetweenThreads.ql New C++ query file using the shared library
cpp/misra/test/rules/RULE-4-1-3/PossibleDataRaceBetweenThreads.testref Points to shared C++ test
cpp/common/src/codingstandards/cpp/rules/possibledataracebetweenthreadsshared/PossibleDataRaceBetweenThreadsShared.qll New shared library with parameterized module
cpp/common/test/rules/possibledataracebetweenthreadsshared/test.cpp C++ test file with compliant and non-compliant cases
cpp/common/test/rules/possibledataracebetweenthreadsshared/PossibleDataRaceBetweenThreadsShared.ql C++ shared test query
cpp/common/test/rules/possibledataracebetweenthreadsshared/PossibleDataRaceBetweenThreadsShared.expected Expected results for C++ test
cpp/common/src/codingstandards/cpp/exclusions/cpp/Undefined.qll Exclusion definitions for the Undefined package
cpp/common/src/codingstandards/cpp/exclusions/cpp/RuleMetadata.qll Registers Undefined package in metadata
change_notes/2026-03-09-possible-data-race-between-threads-shared.md Change note for the refactoring
c/misra/src/rules/DIR-5-1/PossibleDataRaceBetweenThreads.ql Refactored to use shared library
c/misra/test/rules/DIR-5-1/PossibleDataRaceBetweenThreads.qlref Deleted (replaced by .testref)
c/misra/test/rules/DIR-5-1/PossibleDataRaceBetweenThreads.testref Points to shared C test
c/common/test/rules/possibledataracebetweenthreadsshared/test.c C test file with compliant and non-compliant cases
c/common/test/rules/possibledataracebetweenthreadsshared/PossibleDataRaceBetweenThreadsShared.ql C shared test query
c/common/test/rules/possibledataracebetweenthreadsshared/PossibleDataRaceBetweenThreadsShared.expected Expected results for C test

TStdFunctionCall(FunctionCall call) {
call.getTarget()
.hasName([
"setlocale", "tmpnam", "rand", "srand", "getenv", "getenv_s", "strok", "strerror",
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: "strok" should be "strtok". This means calls to strtok will never be matched as non-reentrant, resulting in false negatives. The C test (line 85) and C++ test (lines 99-100) both test strtok but it isn't detected. The C++ test already annotates these as NON-COMPLIANT[False negative], but fixing this typo would resolve the false negative. The CERT-C query CON33-C correctly uses "strtok" (see c/cert/src/rules/CON33-C/RaceConditionsWhenUsingLibraryFunctions.ql:28).

Copilot uses AI. Check for mistakes.
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

filed #1078

srand(0); // NON-COMPLIANT
getenv("PATH"); // NON-COMPLIANT
getenv_s(NULL, NULL, 0, NULL); // NON-COMPLIANT
strtok("a", "b"); // 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.

strtok("a", "b") on line 85 is annotated NON-COMPLIANT but has no corresponding entry in the .expected file. This is caused by the "strok" typo in the shared implementation (should be "strtok"). Once the typo is fixed, this will produce a result and the .expected file will need to be updated accordingly.

Copilot generated this review using guidance from repository custom instructions.
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

filed #1078

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