-
Notifications
You must be signed in to change notification settings - Fork 67
Implement most MISRA-C amendment4 rule amendments #828
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
Merged
lcartey
merged 9 commits into
main
from
michaelrfairhurst/implement-misra-amd4-rule-amendments
Mar 15, 2025
Merged
Changes from 8 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
ae63dcb
Implement most MISRA-C amendment4 rule amendments
MichaelRFairhurst 8403a4b
Fix format
MichaelRFairhurst 7732fd6
Fix cert test
MichaelRFairhurst 45e6b52
fix tests
MichaelRFairhurst 433b887
Address feedback
MichaelRFairhurst fcd0059
Merge remote-tracking branch 'origin/main' into michaelrfairhurst/imp…
MichaelRFairhurst 79a1ca3
Regenerate query metadata
MichaelRFairhurst db1061e
Update codeql warning source location expectations
MichaelRFairhurst 66d7f2b
Don't report '//*comment' in RULE-3-1
MichaelRFairhurst File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
/** | ||
* @id c/misra/unsequenced-atomic-reads | ||
* @name RULE-13-2: The value of an atomic variable shall not depend on the evaluation order of interleaved threads | ||
* @description The value of an atomic variable shall not depend on evaluation order and | ||
* interleaving of threads. | ||
* @kind problem | ||
* @precision very-high | ||
* @problem.severity error | ||
* @tags external/misra/id/rule-13-2 | ||
* correctness | ||
* external/misra/c/2012/amendment3 | ||
* external/misra/obligation/required | ||
*/ | ||
|
||
import cpp | ||
import semmle.code.cpp.dataflow.TaintTracking | ||
import codingstandards.c.misra | ||
import codingstandards.c.Ordering | ||
import codingstandards.c.orderofevaluation.VariableAccessOrdering | ||
|
||
class AtomicAccessInFullExpressionOrdering extends Ordering::Configuration { | ||
AtomicAccessInFullExpressionOrdering() { this = "AtomicAccessInFullExpressionOrdering" } | ||
|
||
override predicate isCandidate(Expr e1, Expr e2) { | ||
exists(AtomicVariableAccess a, AtomicVariableAccess b, FullExpr e | a = e1 and b = e2 | | ||
a.getTarget() = b.getTarget() and | ||
a.(ConstituentExpr).getFullExpr() = e and | ||
b.(ConstituentExpr).getFullExpr() = e and | ||
not a = b | ||
) | ||
} | ||
} | ||
|
||
/** | ||
* A read of a variable specified as `_Atomic`. | ||
* | ||
* Note, it may be accessed directly, or by passing its address into the std atomic functions. | ||
*/ | ||
class AtomicVariableAccess extends VariableAccess { | ||
AtomicVariableAccess() { getTarget().getType().hasSpecifier("atomic") } | ||
|
||
/* Get the `atomic_<read|write>()` call this VarAccess occurs in. */ | ||
FunctionCall getAtomicFunctionCall() { | ||
exists(AddressOfExpr addrParent, FunctionCall fc | | ||
fc.getTarget().getName().matches("__c11_atomic%") and | ||
addrParent = fc.getArgument(0) and | ||
addrParent.getAnOperand() = this and | ||
result = fc | ||
) | ||
} | ||
|
||
/** | ||
* Gets an assigned expr, either in the form `x = <result>` or `atomic_store(&x, <result>)`. | ||
*/ | ||
Expr getAnAssignedExpr() { | ||
result = getAtomicFunctionCall().getArgument(1) | ||
or | ||
exists(AssignExpr assign | | ||
assign.getLValue() = this and | ||
result = assign.getRValue() | ||
) | ||
} | ||
|
||
/** | ||
* Gets the expression holding this variable access, either in the form `x` or `atomic_read(&x)`. | ||
*/ | ||
Expr getARead() { | ||
result = getAtomicFunctionCall() | ||
or | ||
result = this | ||
} | ||
} | ||
|
||
from | ||
AtomicAccessInFullExpressionOrdering config, FullExpr e, Variable v, AtomicVariableAccess va1, | ||
AtomicVariableAccess va2 | ||
where | ||
not isExcluded(e, SideEffects3Package::unsequencedAtomicReadsQuery()) and | ||
e = va1.(ConstituentExpr).getFullExpr() and | ||
config.isUnsequenced(va1, va2) and | ||
v = va1.getTarget() and | ||
v = va2.getTarget() and | ||
// Exclude cases where the variable is assigned a value tainted by the other variable access. | ||
not exists(Expr write | | ||
write = va1.getAnAssignedExpr() and | ||
TaintTracking::localTaint(DataFlow::exprNode(va2.getARead()), DataFlow::exprNode(write)) | ||
) and | ||
// Impose an ordering, show the first access. | ||
va1.getLocation().isBefore(va2.getLocation(), _) | ||
select e, "Atomic variable $@ has a $@ that is unsequenced with $@.", v, v.getName(), va1, | ||
"previous read", va2, "another read" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 4 additions & 0 deletions
4
c/misra/test/rules/RULE-11-8/CastRemovesConstOrVolatileQualification.expected
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,6 @@ | ||
| test.c:4:19:4:33 | (const char *)... | Cast of pointer removes volatile qualification from its base type. | | ||
| test.c:6:13:6:21 | (char *)... | Cast of pointer removes const qualification from its base type. | | ||
| test.c:9:3:9:11 | (char *)... | Cast of pointer removes atomic qualification from its base type. | | ||
| test.c:10:7:10:7 | (char *)... | Cast of pointer removes atomic qualification from its base type. | | ||
| test.c:11:3:11:17 | (const char *)... | Cast of pointer removes atomic qualification from its base type. | | ||
| test.c:12:7:12:7 | (const char *)... | Cast of pointer removes atomic qualification from its base type. | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
WARNING: module 'DataFlow' has been deprecated and may be removed in future (UnsequencedAtomicReads.ql:86,31-39) | ||
WARNING: module 'DataFlow' has been deprecated and may be removed in future (UnsequencedAtomicReads.ql:86,67-75) | ||
WARNING: module 'TaintTracking' has been deprecated and may be removed in future (UnsequencedAtomicReads.ql:86,5-18) | ||
| test.c:44:12:44:18 | ... + ... | Atomic variable $@ has a $@ that is unsequenced with $@. | test.c:42:15:42:16 | a1 | a1 | test.c:44:12:44:13 | a1 | previous read | test.c:44:17:44:18 | a1 | another read | | ||
| test.c:46:3:46:37 | ... + ... | Atomic variable $@ has a $@ that is unsequenced with $@. | test.c:42:15:42:16 | a1 | a1 | test.c:46:16:46:17 | a1 | previous read | test.c:46:35:46:36 | a1 | another read | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
rules/RULE-13-2/UnsequencedAtomicReads.ql |
12 changes: 6 additions & 6 deletions
12
c/misra/test/rules/RULE-13-2/UnsequencedSideEffects.expected
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
| test.c:6:12:6:18 | ... + ... | The expression contains unsequenced $@ to $@ and $@ to $@. | test.c:6:12:6:13 | l1 | side effect | test.c:6:12:6:13 | l1 | l1 | test.c:6:17:6:18 | l1 | side effect | test.c:6:17:6:18 | l1 | l1 | | ||
| test.c:7:12:7:18 | ... + ... | The expression contains unsequenced $@ to $@ and $@ to $@. | test.c:7:12:7:13 | l1 | side effect | test.c:7:12:7:13 | l1 | l1 | test.c:7:17:7:18 | l2 | side effect | test.c:7:17:7:18 | l2 | l2 | | ||
| test.c:17:3:17:21 | ... = ... | The expression contains unsequenced $@ to $@ and $@ to $@. | test.c:17:8:17:9 | l1 | side effect | test.c:17:8:17:9 | l1 | l1 | test.c:17:13:17:14 | l1 | side effect | test.c:17:13:17:14 | l1 | l1 | | ||
| test.c:19:3:19:5 | call to foo | The expression contains unsequenced $@ to $@ and $@ to $@. | test.c:19:7:19:8 | l1 | side effect | test.c:19:7:19:8 | l1 | l1 | test.c:19:11:19:12 | l2 | side effect | test.c:19:11:19:12 | l2 | l2 | | ||
| test.c:25:3:25:5 | call to foo | The expression contains unsequenced $@ to $@ and $@ to $@. | test.c:25:7:25:10 | ... ++ | side effect | test.c:25:7:25:8 | l8 | l8 | test.c:25:13:25:14 | l8 | read | test.c:25:13:25:14 | l8 | l8 | | ||
| test.c:35:5:35:13 | ... = ... | The expression contains unsequenced $@ to $@ and $@ to $@. | test.c:35:10:35:12 | ... ++ | side effect | test.c:35:10:35:10 | i | i | test.c:35:10:35:12 | ... ++ | side effect | test.c:35:10:35:10 | i | i | | ||
| test.c:8:12:8:18 | ... + ... | The expression contains unsequenced $@ to $@ and $@ to $@. | test.c:8:12:8:13 | l1 | side effect | test.c:8:12:8:13 | l1 | l1 | test.c:8:17:8:18 | l1 | side effect | test.c:8:17:8:18 | l1 | l1 | | ||
| test.c:9:12:9:18 | ... + ... | The expression contains unsequenced $@ to $@ and $@ to $@. | test.c:9:12:9:13 | l1 | side effect | test.c:9:12:9:13 | l1 | l1 | test.c:9:17:9:18 | l2 | side effect | test.c:9:17:9:18 | l2 | l2 | | ||
| test.c:19:3:19:21 | ... = ... | The expression contains unsequenced $@ to $@ and $@ to $@. | test.c:19:8:19:9 | l1 | side effect | test.c:19:8:19:9 | l1 | l1 | test.c:19:13:19:14 | l1 | side effect | test.c:19:13:19:14 | l1 | l1 | | ||
| test.c:21:3:21:5 | call to foo | The expression contains unsequenced $@ to $@ and $@ to $@. | test.c:21:7:21:8 | l1 | side effect | test.c:21:7:21:8 | l1 | l1 | test.c:21:11:21:12 | l2 | side effect | test.c:21:11:21:12 | l2 | l2 | | ||
| test.c:27:3:27:5 | call to foo | The expression contains unsequenced $@ to $@ and $@ to $@. | test.c:27:7:27:10 | ... ++ | side effect | test.c:27:7:27:8 | l8 | l8 | test.c:27:13:27:14 | l8 | read | test.c:27:13:27:14 | l8 | l8 | | ||
| test.c:37:5:37:13 | ... = ... | The expression contains unsequenced $@ to $@ and $@ to $@. | test.c:37:10:37:12 | ... ++ | side effect | test.c:37:10:37:10 | i | i | test.c:37:10:37:12 | ... ++ | side effect | test.c:37:10:37:10 | i | i | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 2 additions & 0 deletions
2
c/misra/test/rules/RULE-3-1/CharacterSequencesAndUsedWithinAComment.expected
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
| test.c:9:1:9:8 | /* /* */ | Comment contains an illegal sequence '/*' | | ||
| test.c:12:1:12:8 | /* // */ | Comment contains an illegal sequence '//' | | ||
| test.c:21:1:21:7 | // /* | Comment contains an illegal sequence '/*' | | ||
| test.c:30:1:30:27 | /* https://github.com // */ | Comment contains an illegal sequence '//' | | ||
| test.c:33:1:33:60 | /* a://b, a://b., ://a.b, a://b., a://.b, ://, a://, ://b */ | Comment contains an illegal sequence '//' | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
change_notes/2024-12-13-implement-misra-c-amendment4-rule-amendments.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
- `RULE-11-3` - `CastBetweenObjectPointerAndDifferentObjectType.ql` | ||
- Constrain exception that pointer types to may be cast to char types, so that it does not apply to atomic pointer types, in compliance with MISRA-C 2012 Amendment 4. | ||
- `RULE-11-8` - `CastRemovesConstOrVolatileQualification.ql` | ||
- Query expanded to detect cases of removing `_Atomic` qualification, in compliance with MISRA-C 2012 Amendment 4. | ||
- `EXP33-C`, `RULE-9-1`, `A8-5-0`, `EXP53-CPP` - `DoNotReadUninitializedMemory.ql`, `ObjectWithAutoStorageDurationReadBeforeInit.ql`, `MemoryNotInitializedBeforeItIsRead.ql`, `DoNotReadUninitializedMemory.ql` | ||
- Atomic local variables excluded from query results, in compliance with MISRA-C 2012 Amendment 4, and to reduce false positives in the other standards. | ||
- `RULE-13-2` - `UnsequencedAtomicReads.ql` | ||
- New query to find expressions which read an atomic variable more than once between sequence points, to address new case from MISRA-C 2012 Amendment 4. | ||
- `RULE-3-1` - `CharacterSequencesAndUsedWithinAComment.ql` | ||
- Add exception allowing URLs inside of cpp-style `/* ... */` comments, in compliance with MISRA-C 2012 Amendment 4 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.