Michaelrfairhurst/rule 4 1 3 detect data races#1077
Michaelrfairhurst/rule 4 1 3 detect data races#1077MichaelRFairhurst wants to merge 6 commits intomichaelrfairhurst/package-undefined-behaviorfrom
Conversation
There was a problem hiding this comment.
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.qlinto a new shared libraryPossibleDataRaceBetweenThreadsShared.qllundercpp/common/src/codingstandards/cpp/rules/. - Added a new C++ MISRA 2023 query
RULE-4-1-3/PossibleDataRaceBetweenThreads.qlthat instantiates the shared library with C++ object/sub-object types. - Added test infrastructure (shared test files,
.testreffiles,.expectedfiles, 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", |
There was a problem hiding this comment.
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).
cpp/misra/src/rules/RULE-4-1-3/PossibleDataRaceBetweenThreads.ql
Outdated
Show resolved
Hide resolved
| srand(0); // NON-COMPLIANT | ||
| getenv("PATH"); // NON-COMPLIANT | ||
| getenv_s(NULL, NULL, 0, NULL); // NON-COMPLIANT | ||
| strtok("a", "b"); // NON-COMPLIANT |
There was a problem hiding this comment.
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.
…aelrfairhurst/rule-4-1-3-detect-data-races
…aelrfairhurst/rule-4-1-3-detect-data-races
…aelrfairhurst/rule-4-1-3-detect-data-races
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
.ql,.qll,.qlsor unit tests)Rules with added or modified queries
RULE-4-1-3DIR-5-1Release change checklist
A change note (development_handbook.md#change-notes) is required for any pull request which modifies:
If you are only adding new rule queries, a change note is not required.
Author: Is a change note required?
🚨🚨🚨
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.
Reviewer: Confirm that either a change note is not required or the change note is required and has been added.
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
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.
Reviewer
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.