-
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 4 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
8 changes: 4 additions & 4 deletions
8
c/cert/test/rules/CON40-C/AtomicVariableTwiceInExpression.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:7:18:7:39 | ATOMIC_VAR_INIT(value) | Atomic variable possibly referred to twice in an $@. | test.c:33:3:33:10 | ... += ... | expression | | ||
| test.c:7:18:7:39 | ATOMIC_VAR_INIT(value) | Atomic variable possibly referred to twice in an $@. | test.c:34:3:34:13 | ... = ... | expression | | ||
| test.c:11:3:11:23 | atomic_store(a,b) | Atomic variable possibly referred to twice in an $@. | test.c:11:3:11:23 | atomic_store(a,b) | expression | | ||
| test.c:12:3:12:35 | atomic_store_explicit(a,b,c) | Atomic variable possibly referred to twice in an $@. | test.c:12:3:12:35 | atomic_store_explicit(a,b,c) | expression | | ||
| test.c:25:3:25:49 | atomic_compare_exchange_weak(a,b,c) | Atomic variable possibly referred to twice in an $@. | test.c:25:3:25:49 | atomic_compare_exchange_weak(a,b,c) | expression | | ||
| test.c:26:3:27:42 | atomic_compare_exchange_weak_explicit(a,b,c,d,e) | Atomic variable possibly referred to twice in an $@. | test.c:26:3:27:42 | atomic_compare_exchange_weak_explicit(a,b,c,d,e) | expression | | ||
| test.c:11:3:11:23 | atomic_store(object,desired) | Atomic variable possibly referred to twice in an $@. | test.c:11:3:11:23 | atomic_store(object,desired) | expression | | ||
| test.c:12:3:12:23 | atomic_store_explicit | Atomic variable possibly referred to twice in an $@. | test.c:12:3:12:23 | atomic_store_explicit | expression | | ||
| test.c:25:3:25:49 | atomic_compare_exchange_weak(object,expected,desired) | Atomic variable possibly referred to twice in an $@. | test.c:25:3:25:49 | atomic_compare_exchange_weak(object,expected,desired) | expression | | ||
| test.c:26:3:26:39 | atomic_compare_exchange_weak_explicit | Atomic variable possibly referred to twice in an $@. | test.c:26:3:26:39 | atomic_compare_exchange_weak_explicit | expression | |
8 changes: 4 additions & 4 deletions
8
c/cert/test/rules/CON41-C/WrapFunctionsThatCanFailSpuriouslyInLoop.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,4 +1,4 @@ | ||
| test.c:6:8:6:46 | atomic_compare_exchange_weak(a,b,c) | Function that can spuriously fail not wrapped in a loop. | | ||
| test.c:10:3:10:41 | atomic_compare_exchange_weak(a,b,c) | Function that can spuriously fail not wrapped in a loop. | | ||
| test.c:12:8:13:47 | atomic_compare_exchange_weak_explicit(a,b,c,d,e) | Function that can spuriously fail not wrapped in a loop. | | ||
| test.c:17:3:17:56 | atomic_compare_exchange_weak_explicit(a,b,c,d,e) | Function that can spuriously fail not wrapped in a loop. | | ||
| test.c:6:8:6:46 | atomic_compare_exchange_weak(object,expected,desired) | Function that can spuriously fail not wrapped in a loop. | | ||
| test.c:10:3:10:41 | atomic_compare_exchange_weak(object,expected,desired) | Function that can spuriously fail not wrapped in a loop. | | ||
| test.c:12:8:12:44 | atomic_compare_exchange_weak_explicit | Function that can spuriously fail not wrapped in a loop. | | ||
| test.c:17:3:17:39 | atomic_compare_exchange_weak_explicit | Function that can spuriously fail not wrapped in a loop. | |
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,9 +1,69 @@ | ||
#define atomic_compare_exchange_weak(a, b, c) 0 | ||
#define atomic_compare_exchange_weak_explicit(a, b, c, d, e) 0 | ||
#define atomic_load(a) 0 | ||
#define atomic_load_explicit(a, b) | ||
#define atomic_store(a, b) 0 | ||
#define atomic_store_explicit(a, b, c) 0 | ||
#define ATOMIC_VAR_INIT(value) (value) | ||
#define atomic_is_lock_free(obj) __c11_atomic_is_lock_free(sizeof(*(obj))) | ||
typedef _Atomic(int) atomic_int; | ||
typedef _Atomic(int) atomic_int; | ||
|
||
#define __ATOMIC_RELAXED 0 | ||
#define __ATOMIC_CONSUME 1 | ||
#define __ATOMIC_ACQUIRE 2 | ||
#define __ATOMIC_RELEASE 3 | ||
#define __ATOMIC_ACQ_REL 4 | ||
#define __ATOMIC_SEQ_CST 5 | ||
|
||
typedef enum memory_order { | ||
memory_order_relaxed = __ATOMIC_RELAXED, | ||
memory_order_consume = __ATOMIC_CONSUME, | ||
memory_order_acquire = __ATOMIC_ACQUIRE, | ||
memory_order_release = __ATOMIC_RELEASE, | ||
memory_order_acq_rel = __ATOMIC_ACQ_REL, | ||
memory_order_seq_cst = __ATOMIC_SEQ_CST | ||
} memory_order; | ||
|
||
void atomic_thread_fence(memory_order); | ||
void atomic_signal_fence(memory_order); | ||
|
||
#define atomic_thread_fence(order) __c11_atomic_thread_fence(order) | ||
#define atomic_signal_fence(order) __c11_atomic_signal_fence(order) | ||
|
||
#define atomic_store(object, desired) __c11_atomic_store(object, desired, __ATOMIC_SEQ_CST) | ||
#define atomic_store_explicit __c11_atomic_store | ||
|
||
#define atomic_load(object) __c11_atomic_load(object, __ATOMIC_SEQ_CST) | ||
#define atomic_load_explicit __c11_atomic_load | ||
|
||
#define atomic_exchange(object, desired) __c11_atomic_exchange(object, desired, __ATOMIC_SEQ_CST) | ||
#define atomic_exchange_explicit __c11_atomic_exchange | ||
|
||
#define atomic_compare_exchange_strong(object, expected, desired) __c11_atomic_compare_exchange_strong(object, expected, desired, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST) | ||
#define atomic_compare_exchange_strong_explicit __c11_atomic_compare_exchange_strong | ||
|
||
#define atomic_compare_exchange_weak(object, expected, desired) __c11_atomic_compare_exchange_weak(object, expected, desired, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST) | ||
#define atomic_compare_exchange_weak_explicit __c11_atomic_compare_exchange_weak | ||
|
||
#define atomic_fetch_add(object, operand) __c11_atomic_fetch_add(object, operand, __ATOMIC_SEQ_CST) | ||
#define atomic_fetch_add_explicit __c11_atomic_fetch_add | ||
|
||
#define atomic_fetch_sub(object, operand) __c11_atomic_fetch_sub(object, operand, __ATOMIC_SEQ_CST) | ||
#define atomic_fetch_sub_explicit __c11_atomic_fetch_sub | ||
|
||
#define atomic_fetch_or(object, operand) __c11_atomic_fetch_or(object, operand, __ATOMIC_SEQ_CST) | ||
#define atomic_fetch_or_explicit __c11_atomic_fetch_or | ||
|
||
#define atomic_fetch_xor(object, operand) __c11_atomic_fetch_xor(object, operand, __ATOMIC_SEQ_CST) | ||
#define atomic_fetch_xor_explicit __c11_atomic_fetch_xor | ||
|
||
#define atomic_fetch_and(object, operand) __c11_atomic_fetch_and(object, operand, __ATOMIC_SEQ_CST) | ||
#define atomic_fetch_and_explicit __c11_atomic_fetch_and | ||
|
||
typedef struct atomic_flag { _Atomic(_Bool) _Value; } atomic_flag; | ||
|
||
_Bool atomic_flag_test_and_set(volatile atomic_flag *); | ||
_Bool atomic_flag_test_and_set_explicit(volatile atomic_flag *, memory_order); | ||
|
||
void atomic_flag_clear(volatile atomic_flag *); | ||
void atomic_flag_clear_explicit(volatile atomic_flag *, memory_order); | ||
|
||
#define atomic_flag_test_and_set(object) __c11_atomic_exchange(&(object)->_Value, 1, __ATOMIC_SEQ_CST) | ||
#define atomic_flag_test_and_set_explicit(object, order) __c11_atomic_exchange(&(object)->_Value, 1, order) | ||
|
||
#define atomic_flag_clear(object) __c11_atomic_store(&(object)->_Value, 0, __ATOMIC_SEQ_CST) | ||
#define atomic_flag_clear_explicit(object, order) __c11_atomic_store(&(object)->_Value, 0, order) |
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,92 @@ | ||
/** | ||
* @id c/misra/unsequenced-atomic-reads | ||
* @name RULE-13-2: The value of an atomic variable depend on its evaluation order and interleave of 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 { | ||
pragma[noinline] | ||
MichaelRFairhurst marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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:87,31-39) | ||
WARNING: module 'DataFlow' has been deprecated and may be removed in future (UnsequencedAtomicReads.ql:87,67-75) | ||
WARNING: module 'TaintTracking' has been deprecated and may be removed in future (UnsequencedAtomicReads.ql:87,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 |
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.