diff --git a/.vscode/tasks.json b/.vscode/tasks.json index f027524374..d1f141cced 100644 --- a/.vscode/tasks.json +++ b/.vscode/tasks.json @@ -220,6 +220,7 @@ "Invariants", "Iterators", "Lambdas", + "Language1", "Literals", "Loops", "Macros", diff --git a/c/misra/src/rules/DIR-4-3/LanguageNotEncapsulatedAndIsolated.ql b/c/misra/src/rules/DIR-4-3/LanguageNotEncapsulatedAndIsolated.ql new file mode 100644 index 0000000000..fb9f00e9c4 --- /dev/null +++ b/c/misra/src/rules/DIR-4-3/LanguageNotEncapsulatedAndIsolated.ql @@ -0,0 +1,28 @@ +/** + * @id c/misra/language-not-encapsulated-and-isolated + * @name DIR-4-3: Assembly language shall be encapsulated and isolated + * @description Failing to encapsulate assembly language limits the portability, reliability, and + * readability of programs. + * @kind problem + * @precision very-high + * @problem.severity error + * @tags external/misra/id/dir-4-3 + * maintainability + * readability + * external/misra/obligation/required + */ + +import cpp +import codingstandards.c.misra + +from AsmStmt asm +where + not isExcluded(asm, Language1Package::languageNotEncapsulatedAndIsolatedQuery()) and + not exists(asm.getEnclosingFunction()) + or + // in concept statements within the body constitute intermingling assembly, + // rather than expressions and are more general. + exists(Stmt sp | sp = asm.getEnclosingFunction().getEntryPoint().getASuccessor*() | + not sp instanceof AsmStmt and not sp instanceof ReturnStmt and not sp instanceof BlockStmt + ) +select asm, "Usage of non-isolated and non-encapsulated assembly language." diff --git a/c/misra/test/rules/DIR-4-3/LanguageNotEncapsulatedAndIsolated.expected b/c/misra/test/rules/DIR-4-3/LanguageNotEncapsulatedAndIsolated.expected new file mode 100644 index 0000000000..04304fa22b --- /dev/null +++ b/c/misra/test/rules/DIR-4-3/LanguageNotEncapsulatedAndIsolated.expected @@ -0,0 +1,5 @@ +| test.c:6:3:6:5 | asm statement | Usage of non-isolated and non-encapsulated assembly language. | +| test.c:11:3:11:5 | asm statement | Usage of non-isolated and non-encapsulated assembly language. | +| test.c:32:3:32:5 | asm statement | Usage of non-isolated and non-encapsulated assembly language. | +| test.c:37:3:37:5 | asm statement | Usage of non-isolated and non-encapsulated assembly language. | +| test.c:58:3:58:5 | asm statement | Usage of non-isolated and non-encapsulated assembly language. | diff --git a/c/misra/test/rules/DIR-4-3/LanguageNotEncapsulatedAndIsolated.qlref b/c/misra/test/rules/DIR-4-3/LanguageNotEncapsulatedAndIsolated.qlref new file mode 100644 index 0000000000..df3d8afc8c --- /dev/null +++ b/c/misra/test/rules/DIR-4-3/LanguageNotEncapsulatedAndIsolated.qlref @@ -0,0 +1 @@ +rules/DIR-4-3/LanguageNotEncapsulatedAndIsolated.ql \ No newline at end of file diff --git a/c/misra/test/rules/DIR-4-3/test.c b/c/misra/test/rules/DIR-4-3/test.c new file mode 100644 index 0000000000..c031f2ab8a --- /dev/null +++ b/c/misra/test/rules/DIR-4-3/test.c @@ -0,0 +1,80 @@ +#define N1 asm("HCF") +#define N2 __asm__("HCF") + +void f1() { + int a; + N1; // NON_COMPLIANT +} + +void f2() { + int a; + N2; // NON_COMPLIANT +} + +void f3() { + N1; // COMPLIANT +} + +void f4() { + N2; // COMPLIANT +} + +void f5() { + __asm__("HCF"); // COMPLIANT +} + +void f6() { + asm("HCF"); // COMPLIANT +} + +inline void f7() { + int a; + N1; // NON_COMPLIANT +} + +inline void f8() { + int a; + N2; // NON_COMPLIANT +} + +inline void f9() { + N1; // COMPLIANT +} + +inline void f10() { + N2; // COMPLIANT +} + +inline void f11() { + __asm__("HCF"); // COMPLIANT +} + +inline void f12() { + asm("HCF"); // COMPLIANT +} + +inline int f13() { + int a; + N2; // NON_COMPLIANT + return 0; +} + +inline int f14() { + N1; // COMPLIANT + return 0; +} + +inline int f15() { + N2; // COMPLIANT + return 0; +} + +inline int f16() { + __asm__("HCF"); // COMPLIANT + return 0; +} + +inline int f17() { + asm("HCF"); // COMPLIANT + return 0; +} \ No newline at end of file diff --git a/cpp/common/src/codingstandards/cpp/exclusions/c/Language1.qll b/cpp/common/src/codingstandards/cpp/exclusions/c/Language1.qll new file mode 100644 index 0000000000..a2787698cc --- /dev/null +++ b/cpp/common/src/codingstandards/cpp/exclusions/c/Language1.qll @@ -0,0 +1,25 @@ +//** THIS FILE IS AUTOGENERATED, DO NOT MODIFY DIRECTLY. **/ +import cpp +import RuleMetadata +import codingstandards.cpp.exclusions.RuleMetadata + +newtype Language1Query = TLanguageNotEncapsulatedAndIsolatedQuery() + +predicate isLanguage1QueryMetadata(Query query, string queryId, string ruleId) { + query = + // `Query` instance for the `languageNotEncapsulatedAndIsolated` query + Language1Package::languageNotEncapsulatedAndIsolatedQuery() and + queryId = + // `@id` for the `languageNotEncapsulatedAndIsolated` query + "c/misra/language-not-encapsulated-and-isolated" and + ruleId = "DIR-4-3" +} + +module Language1Package { + Query languageNotEncapsulatedAndIsolatedQuery() { + //autogenerate `Query` type + result = + // `Query` type for `languageNotEncapsulatedAndIsolated` query + TQueryC(TLanguage1PackageQuery(TLanguageNotEncapsulatedAndIsolatedQuery())) + } +} diff --git a/cpp/common/src/codingstandards/cpp/exclusions/c/RuleMetadata.qll b/cpp/common/src/codingstandards/cpp/exclusions/c/RuleMetadata.qll index 4664c7df34..3f7f68057b 100644 --- a/cpp/common/src/codingstandards/cpp/exclusions/c/RuleMetadata.qll +++ b/cpp/common/src/codingstandards/cpp/exclusions/c/RuleMetadata.qll @@ -18,6 +18,7 @@ import IO1 import IO2 import IO3 import IO4 +import Language1 import Misc import Pointers1 import Pointers2 @@ -51,6 +52,7 @@ newtype TCQuery = TIO2PackageQuery(IO2Query q) or TIO3PackageQuery(IO3Query q) or TIO4PackageQuery(IO4Query q) or + TLanguage1PackageQuery(Language1Query q) or TMiscPackageQuery(MiscQuery q) or TPointers1PackageQuery(Pointers1Query q) or TPointers2PackageQuery(Pointers2Query q) or @@ -84,6 +86,7 @@ predicate isQueryMetadata(Query query, string queryId, string ruleId) { isIO2QueryMetadata(query, queryId, ruleId) or isIO3QueryMetadata(query, queryId, ruleId) or isIO4QueryMetadata(query, queryId, ruleId) or + isLanguage1QueryMetadata(query, queryId, ruleId) or isMiscQueryMetadata(query, queryId, ruleId) or isPointers1QueryMetadata(query, queryId, ruleId) or isPointers2QueryMetadata(query, queryId, ruleId) or diff --git a/rule_packages/c/Language1.json b/rule_packages/c/Language1.json new file mode 100644 index 0000000000..50aed45c55 --- /dev/null +++ b/rule_packages/c/Language1.json @@ -0,0 +1,24 @@ +{ + "MISRA-C-2012": { + "DIR-4-3": { + "properties": { + "obligation": "required" + }, + "queries": [ + { + "description": "Failing to encapsulate assembly language limits the portability, reliability, and readability of programs.", + "kind": "problem", + "name": "Assembly language shall be encapsulated and isolated", + "precision": "very-high", + "severity": "error", + "short_name": "LanguageNotEncapsulatedAndIsolated", + "tags": [ + "maintainability", + "readability" + ] + } + ], + "title": "Assembly language shall be encapsulated and isolated" + } + } +} \ No newline at end of file diff --git a/rules.csv b/rules.csv index cb14aec2c0..0e771b8548 100755 --- a/rules.csv +++ b/rules.csv @@ -604,7 +604,7 @@ c,MISRA-C-2012,RULE-2-1,Yes,Required,,,All source files shall compile without an c,MISRA-C-2012,RULE-3-1,No,Required,,,All code shall be traceable to documented requirements,,,, c,MISRA-C-2012,RULE-4-1,No,Required,,,Run-time failures shall be minimized,,,, c,MISRA-C-2012,RULE-4-2,Yes,Advisory,,,All usage of assembly language should be documented,M7-4-1,Language,Import, -c,MISRA-C-2012,RULE-4-3,Yes,Required,,,Assembly language shall be encapsulated and isolated,,Language,Medium, +c,MISRA-C-2012,DIR-4-3,Yes,Required,,,Assembly language shall be encapsulated and isolated,,Language1,Medium, c,MISRA-C-2012,RULE-4-4,Yes,Advisory,,,Sections of code should not be commented out,A2-7-2,Syntax,Import, c,MISRA-C-2012,DIR-4-5,Yes,Advisory,,,Identifiers in the same name space with overlapping visibility should be typographically unambiguous,M2-10-1,Syntax,Easy, c,MISRA-C-2012,RULE-4-6,Yes,Advisory,,,typedefs that indicate size and signedness should be used in place of the basic numerical types,,Types,Hard,