MISRA-C2012
11 minute read
Overview
XWOS code is generally written in compliance with MISRA-C:2012 standards. However, some MISRA-C:2012 rules are not conducive to writing an operating system, so XWOS code tends to prefer better performance and the use of newer language features.
This document describes in detail the method for MISRA-C:2012 checking and the degree of compliance with the MISRA-C:2012 standard.
MISRA-C:2012 Checking Method
Use the misra plugin of the cppcheck tool.
cppcheck -I. -I$(XWOS_ARCH_DIR) -I$(XWOS_CPU_DIR) -I$(XWOS_SOC_DIR) -I$(XWOS_BRD_DIR) \
--force --addon=misra --cppcheck-build-dir=$(CPPCHECK_OUT) \
--template=gcc --inline-suppr --suppressions-list=xwbs/util/cppcheck/misra-c2012-suppressions.txt
-i ignore_path src_path
Degree of Compliance with MISRA-C:2012
Globally Disabled Rules
-
uninitvar- Reason for disabling: False positive. In practice, uninitialized variables are treated as errors by the compiler.
-
misra-c2012-1.4: Emergent language features shall not be used- Reason for disabling: As a long-term continuously maintained project, XWOS tends to introduce new language features in new versions.
-
misra-c2012-2.3: A project should not contain unused type declarations- Reason for disabling: The operating system, as a basic environment of an ecosystem, defines types, macros, functions, and symbols that may be used in various derivative projects.
-
misra-c2012-2.4: A project should not contain unused tag declarations- Reason for disabling: The operating system, as a basic environment of an ecosystem, defines types, macros, functions, and symbols that may be used in various derivative projects.
-
misra-c2012-2.5: A project should not contain unused macro declarations- Reason for disabling: The operating system, as a basic environment of an ecosystem, defines types, macros, functions, and symbols that may be used in various derivative projects.
-
misra-c2012-3.1: The character sequences/*and//shall not be used within a comment- Reason for disabling: XWOS uses doxygen to extract comments for documentation generation. URLs in documentation inevitably contain
//, for examplehttps://.... Apart from this case,//is prohibited in comments.
- Reason for disabling: XWOS uses doxygen to extract comments for documentation generation. URLs in documentation inevitably contain
-
misra-c2012-5.9: Identifiers that define objects or functions with internal linkage should be unique- Reason for disabling: When header files containing
static inlinefunctions are used in multiple places, the inline functions are considered duplicate definitions.
- Reason for disabling: When header files containing
-
misra-c2012-8.7: Functions and objects should not be defined with external linkage if they are referenced in only one translation unit- Reason for disabling: APIs provided by XWOS are not necessarily used by itself. For MISRA-C2012 checking tools, they may report an error because a symbol is only found once.
-
misra-c2012-8.9: An object should be defined at block scope if its identifier only appears in a single function- Reason for disabling: Some data is defined as global in order to be placed in specific sections during linking.
-
misra-c2012-11.3: A cast shall not be performed between a pointer to object type and a pointer to a different object type- Reasons for disabling:
-
- XWOS’s various types of atomic operations and bit operations are implemented by converting to 8-bit, 16-bit, 32-bit, and 64-bit basic types based on bit width;
-
- Dynamic memory management;
-
- C language object-oriented programming.
-
- Reasons for disabling:
-
misra-c2012-11.4: A conversion should not be performed between a pointer to object and an integer type- Reasons for disabling:
-
- Code at the instruction and register level;
-
- Implementation of the
xwcc_derof()macro;
- Implementation of the
-
- Dynamic memory management;
-
- C language object-oriented programming.
-
- Reasons for disabling:
-
misra-c2012-11.5: A conversion should not be performed from pointer to void into pointer to object- Reasons for disabling:
-
- Dynamic memory management;
-
- Variable-length messages in communication protocols;
-
- Callback function user parameters are generic pointers.
-
- Reasons for disabling:
-
misra-c2012-11.6: A cast shall not be performed between pointer to void and an arithmetic type- Reasons for disabling:
-
- XWOS treats pointer values from
-1to-4095as error code pointers. However, pointers are unsigned integers. They are therefore represented in two’s complement form.-1is0xFFFFFFFF,-4095is0xFFFFF001.
- XWOS treats pointer values from
-
- Dynamic memory management.
-
- Reasons for disabling:
-
misra-c2012-15.1: The goto statement should not be used- Reason for disabling: XWOS only uses
gotoin the following two cases:-
- Some core algorithms within XWOS (e.g., red-black trees) use
gototo improve efficiency.
- 1.1. Because these types of code are heavily used throughout the kernel, efficiency is the primary consideration;
- 1.2. These types of code have been the most thoroughly verified and have the highest stability and safety.
- Some core algorithms within XWOS (e.g., red-black trees) use
-
- XWOS uses
gototo handle error return paths in code.
- 2.1. If code contains a large number of operations, each with a return value, using
if...else...would result in deeply nested code. Usinggotomakes the code more elegant and readable; - 2.2. If code contains a large number of operations, each with a corresponding undo operation, where operations and undo operations must follow a LIFO “stack” structure, using
if...else...can easily lead to missing undo operations or incorrect ordering. Usinggotoperfectly solves this problem.
- XWOS uses
-
- Reason for disabling: XWOS only uses
/* If there are many operations, using `if...else...` will make the code lines too long,
* and anti_operationX() is easily misplaced or omitted.
*/
int func1(void)
{
rc = operation1();
if (0 == rc) {
rc = operation3();
if (0 == rc) {
rc = operation8();
if (0 == rc) {
rc = operation2();
if (0 == rc) {
rc = operation6();
if (0 == rc) {
rc = operation5();
if (0 == rc) {
} else {
anti_operation6();
}
} else {
anti_operation2();
}
} else {
anti_operation8();
}
} else {
anti_operation3();
}
} else {
anti_operation1();
}
} else {
}
return rc;
}
/* Using `goto` avoids the two problems of `if...else...` described above. */
int func2(void)
{
rc = operation1();
if (rc < 0) {
goto err_operation1;
}
rc = operation3();
if (rc < 0) {
goto err_operation3;
}
rc = operation8();
if (rc < 0) {
goto err_operation8;
}
rc = operation2();
if (rc < 0) {
goto err_operation2;
}
rc = operation6();
if (rc < 0) {
goto err_operation6;
}
rc = operation5();
if (rc < 0) {
goto err_operation5;
}
return rc;
anti_operation5();
err_operation5:
anti_operation6();
err_operation6:
anti_operation2();
err_operation2:
anti_operation8();
err_operation8:
anti_operation3();
err_operation3:
anti_operation1();
err_operation1:
return rc;
}
-
misra-c2012-17.1: The features of <stdarg.h> shall not be used- Reason for disabling: Language features should not be completely prohibited.
-
misra-c2012-18.8: Variable-length array types shall not be used- Reason for disabling: Variable-length arrays are a feature introduced in the C99 standard. XWOS tends to introduce new language features in new versions.
-
misra-c2012-19.2: The union keyword should not be used- Reason for disabling: When writing an operating system, it is inevitably necessary to convert memory addresses to pointers of different types. Using
unioncan avoid direct conversion ofvoid *, improving readability.
- Reason for disabling: When writing an operating system, it is inevitably necessary to convert memory addresses to pointers of different types. Using
-
misra-c2012-20.9: All identifiers used in the controlling expression of#ifor#elifpreprocessing directives shall be#define’d before evaluation- Reason for disabling: The checking tool gives false positives, but XWOS code follows this rule.
-
misra-c2012-21.1:#defineand#undefshall not be used on a reserved identifier or reserved macro name- Reason for disabling: The C language reserves any symbol starting with
_. However, some symbols come from compiler extended syntax, such as__attribute__((x)). They are usually defined as macros starting with two underscores to show their difference from normal variables, functions, and macros.
- Reason for disabling: The C language reserves any symbol starting with
-
misra-c2012-21.3: The memory allocation and deallocation functions of<stdlib.h>shall not be used- Reason for disabling: Completely prohibiting dynamic memory management is not conducive to introducing high-level languages. Dynamic memory management should only be prohibited when the safety level is very high, in which case some features such as the Lua language also cannot be used.
-
misra-c2012-21.6: The Standard Library input/output functions shall not be used- Reason for disabling: Functions from
stdio.hhave been redefined within XWOS.
- Reason for disabling: Functions from
-
misra-c2012-21.10: The Standard Library time and date functions shall not be used- Reason for disabling: Functions from
time.hhave been redefined within XWOS.
- Reason for disabling: Functions from
Rules Disabled for Specific Files
-
misra-c2012-2.7: There should be no unused parameters in functions- Reason for disabling: This header file contains functions at the CPU architecture instruction level. These functions are written using inline assembly syntax.
At the C syntax level, some parameters appear unused, but in practice, these parameters are used through registers according to
EABIrules.- Applicable scope:
xwcd/soc/arm/v6m/armv6m_isa.hxwcd/soc/arm/v7m/armv7m_isa.hxwcd/soc/powerpc/e200x/e200x_isa.hxwcd/soc/riscv/nuclei/riscv_isa.h
- Applicable scope:
- Reason for disabling: This header file contains functions at the CPU architecture instruction level. These functions are written using inline assembly syntax.
At the C syntax level, some parameters appear unused, but in practice, these parameters are used through registers according to
-
misra-c2012-5.7: A tag name shall be a unique identifier- Reason for disabling: Anonymous structures being considered as duplicate
tag nameis a false positive.- Applicable scope:
xwcd/soc/arm/v6m/armv6m_isa.hxwcd/soc/arm/v7m/armv7m_isa.hxwcd/soc/powerpc/e200x/e200x_isa.hxwcd/soc/riscv/nuclei/riscv_isa.hxwcd/ds/uart/common.hxwcd/ds/uart/controller.hxwcd/ds/soc/chip.hxwos/lib/xwbop.cxwos/mp/skd.hxwos/up/skd.hxwos/mp/thd.hxwos/up/thd.hxwos/mp/sync/obj.hxwos/up/sync/obj.hxwos/mm/mempool/objcache.hxwos/mm/mempool/page.hxwmd/isc/xwssc/protocol.hxwmd/libc/newlibac/lock.cxwmd/libc/picolibac/lock.c
- Applicable scope:
- Reason for disabling: Symbols required by libc
- Applicable scope:
xwmd/libc/newlibac/lock.cxwmd/libc/picolibac/lock.c
- Applicable scope:
- Reason for disabling: Anonymous structures being considered as duplicate
-
misra-c2012-5.8: A tag name shall be a unique identifier- Reason for disabling: Member names within structures are in the structure’s namespace and will not conflict with external symbols.
- Applicable scope:
xwmd/isc/xwssc/hwifal.hxwos/mm/mempool/i_allocator.hxwmd/libc/picolibcac/mem.cxwos/mm/mempool/page.h
- Applicable scope:
- Reason for disabling: Rewriting standard libc functions.
- Applicable scope:
xwmd/libc/picolibcac/fops.c
- Applicable scope:
- Reason for disabling: Member names within structures are in the structure’s namespace and will not conflict with external symbols.
-
misra-c2012-8.4: A compatible declaration shall be visible when an object or function with external linkage is defined- Reason for disabling: False positive
- Applicable scope:
xwos/ospl/xwosplcb.c
- Applicable scope:
- Reason for disabling: Rust’s
ffiis not linked at the C language level.- Applicable scope:
xwmd/xwrust/ffi/*.c
- Applicable scope:
- Reason for disabling: False positive
-
misra-c2012-8.5: An external object or function shall be declared once in one and only one file- Reason for disabling:
xwos/ospl/soc/*.his the code of the OS port layer. Symbols are re-declared for summarization and emphasis, reminding users that definitions for these symbols must be provided during porting.- Applicable scope:
xwos/ospl/soc/*.hxwos/lib/xwbop.hxwos/lib/lfq.h
- Applicable scope:
- Reason for disabling: False positive
- Applicable scope:
xwmd/libc/newlibac/*.cxwmd/libc/picolibcac/*.c
- Applicable scope:
- Reason for disabling:
-
misra-c2012-8.6: An identifier with external linkage shall have exactly one external definition- Reason for disabling: False positive
- Applicable scope:
xwmd/libc/newlibac/*.cxwmd/libc/picolibcac/*.c
- Applicable scope:
- Reason for disabling: False positive
-
misra-c2012-8.14: The restrict type qualifier shall not be used- Reason for disabling: Rewriting standard libc functions.
- Applicable scope:
xwmd/libc/newlibac/string.cxwmd/libc/picolibcac/string.cxwmd/libc/newlibac/time.cxwmd/libc/picolibcac/time.c
- Applicable scope:
- Reason for disabling: Rewriting standard libc functions.
-
misra-c2012-9.2: The initializer for an aggregate or union shall be enclosed in braces- Reason for disabling: False positive
- Applicable scope:
xwmd/libc/newlibac/fops.cxwmd/libc/picolibcac/fops.c
- Applicable scope:
- Reason for disabling: False positive
-
misra-c2012-9.3: Arrays shall not be partially initialized- Reason for disabling: False positive
- Applicable scope:
xwmd/libc/newlibac/fops.cxwmd/libc/picolibcac/fops.c
- Applicable scope:
- Reason for disabling: False positive
-
misra-c2012-10.8: The value of a composite expression shall not be cast to a different essential type category or a wider essential type- Reason for disabling: Macro-defined expressions are composite expressions, requiring the use of explicit type casts.
- Applicable scope:
xwos/mm/mempool/page.c
- Applicable scope:
- Reason for disabling: Macro-defined expressions are composite expressions, requiring the use of explicit type casts.
-
misra-c2012-11.1: Conversions shall not be performed between a pointer to a function and any other type- Reason for disabling: Necessary conversion from the OS abstraction layer to the implementation layer.
- Applicable scope:
xwos/osal/thd.hxwos/osal/swt.h
- Applicable scope:
- Reason for disabling: Necessary conversion from the OS abstraction layer to the implementation layer.
-
misra-c2012-12.3: The comma operator should not be used- Reason for disabling: XWOS’s linked list (
xwos/lib/bclst.h) iteration operation macros must use comma expressions for their definitions.- Applicable scope:
xwos/mp/skd.cxwos/up/skd.c
- Applicable scope:
- Reason for disabling: XWOS’s linked list (
-
misra-c2012-14.2: A for loop shall be well-formed- Reason for disabling: XWOS’s linked list (
xwos/lib/bclst.h) iteration operations are too complex, so macros are used to simplify the code.- Applicable scope:
xwos/mp/skd.cxwos/up/skd.c
- Applicable scope:
- Reason for disabling: XWOS’s linked list (
-
misra-c2012-15.2: The goto statement shall jump to a label declared later in the same function- Reason for disabling: This part of the code is critical for XWOS’s low interrupt latency. Real-time performance and efficiency must be prioritized, so
gotohad to be used.- Applicable scope:
xwos/mp/tt.cxwos/up/tt.c
- Applicable scope:
- Reason for disabling: False positive.
- Applicable scope:
xwmd/isc/xwssc/mif.c
- Applicable scope:
- Reason for disabling: This part of the code is critical for XWOS’s low interrupt latency. Real-time performance and efficiency must be prioritized, so
-
misra-c2012-15.4: There should be no more than one break or goto statement used to terminate any iteration statement- Reason for disabling: This part of the code is critical for XWOS’s low interrupt latency. Real-time performance and efficiency must be prioritized, so
gotohad to be used.- Applicable scope:
xwos/mp/tt.cxwos/up/tt.c
- Applicable scope:
- Reason for disabling: Using only one
breakorgotoin high-usage code affects efficiency.- Applicable scope:
xwos/up/skd.cxwos/mp/sync/evt.cxwos/up/sync/evt.cxwmd/isc/xwssc/hwifal.cxwmd/isc/xwssc/protocol.c
- Applicable scope:
- Reason for disabling: This part of the code is critical for XWOS’s low interrupt latency. Real-time performance and efficiency must be prioritized, so
-
misra-c2012-15.5: A function should have a single point of exit at the end- Reason for disabling: Function parameter validation code is defined as a macro that contains a
return.- Applicable scope:
xwmd/isc/xwssc/mif.c
- Applicable scope:
- Reason for disabling: Function parameter validation code is defined as a macro that contains a
-
misra-c2012-17.2: Functions shall not call themselves, either directly or indirectly- Reason for disabling: This part of the code is key to XWOS’s innovative interrupt-enabled scheduling technique, requiring recursive functions for its implementation. This code has been verified thoroughly enough that its stability and safety are not a concern.
- Applicable scope:
xwos/mp/skd.cxwos/up/skd.c
- Applicable scope:
- Reason for disabling: This part of the code is key to XWOS’s innovative interrupt-enabled scheduling technique, requiring recursive functions for its implementation. This code has been verified thoroughly enough that its stability and safety are not a concern.
-
misra-c2012-17.7: The value returned by a function having non-void return type shall be used- Reason for disabling: C library functions like
memset()andmemcpy()have return values, but they are not used here.- Applicable scope:
xwos/mp/rtrq.cxwos/up/rtrq.cxwos/mp/sync/evt.cxwos/up/sync/evt.cxwmd/isc/xwcq/xwcq.cxwmd/isc/xwssc/hwifal.cxwmd/isc/xwssc/mif.cxwmd/isc/xwssc/protocol.cxwmd/libc/newlibac/mif.cxwmd/libc/picolibcac/mif.cxwmd/libc/newlibac/mem.cxwmd/libc/picolibcac/mem.c
- Applicable scope:
- Reason for disabling: Use of
cppcheck-suppress [misra-c2012-17.7]annotation where return values are determined not to be used.- Applicable scope:
xwos/init.cxwos/mp/lock/mtx.cxwos/up/lock/mtx.cxwos/mp/lock/spinlock.cxwos/up/lock/seqlock.cxwmd/isc/xwssc/mif.cxwmd/isc/xwssc/protocol.cxwos/mm/mempool/objcache.cxwos/mm/mempool/allocator.cxwos/mm/mempool/page.c
- Applicable scope:
- Reason for disabling: C library functions like
-
misra-c2012-17.8: A function parameter should not be modified- Reason for disabling: To pursue efficiency and memory usage, this rule is not followed.
- Applicable scope:
xwos/lib/xwbop.cxwos/mm/bma.cxwos/mm/mempool/objcache.cxwos/mm/mempool/page.cxwos/mm/mempool/allocator.cxwos/mp/lock/mtx.cxwos/up/lock/mtx.cxwmd/isc/xwssc/protocol.cxwmd/libc/newlibac/string.cxwmd/libc/picolibcac/string.c
- Applicable scope:
- Reason for disabling: To pursue efficiency and memory usage, this rule is not followed.
-
misra-c2012-20.7: Expressions resulting from the expansion of macro parameters shall be enclosed in parentheses- Reason for disabling: This part of the code uses macros to emulate C++ function templates, where type name parameters cannot be enclosed in parentheses.
- Applicable scope:
xwos/lib/xwaop.hxwos/lib/xwbop.h
- Applicable scope:
- Reason for disabling: This part of the code uses macros to define arrays, where array name parameters cannot be enclosed in parentheses.
- Applicable scope:
xwos/mm/bma.hxwos/mm/mempool/allocator.hxwmd/isc/xwssc/mif.hxwmd/isc/xwcq/mif.h
- Applicable scope:
- Reason for disabling: This part of the code uses macros to emulate C++ function templates, where type name parameters cannot be enclosed in parentheses.
-
misra-c2012-20.10: The#and##preprocessor operators should not be used- Reason for disabling: This part of the code uses macros to emulate C++ function templates, requiring the use of
#and##.- Applicable scope:
xwos/lib/xwaop.hxwos/lib/xwbop.h
- Applicable scope:
- Reason for disabling: This part of the code uses macros to emulate C++ function templates, requiring the use of
-
misra-c2012-20.12: A macro parameter used as an operand to the#or##operators, which is itself subject to further macro replacement, shall only be used as an operand to these operators- Reason for disabling: This part of the code uses macros to emulate C++ function templates.
- Applicable scope:
xwos/lib/xwaop.hxwos/lib/xwbop.h
- Applicable scope:
- Reason for disabling: This part of the code uses macros to emulate C++ function templates.
-
misra-c2012-21.2: A reserved identifier or macro name shall not be declared- Reason for disabling: Rewriting standard libc functions.
- Applicable scope:
xwmd/libc/newlibac/string.cxwmd/libc/picolibcac/string.cxwmd/libc/picolibcac/fops.cxwmd/libc/picolibcac/mem.c
- Applicable scope:
- Reason for disabling: Rewriting standard libc functions.
-
misra-c2012-21.4: The standard header file<setjmp.h>shall not be used- Reason for disabling: Language features should not be completely prohibited. XWOS provides support for
<setjmp.h>.- Applicable scope:
xwmd/libc/newlibac/setjmp.cxwmd/libc/picolibcac/setjmp.c
- Applicable scope:
- Reason for disabling: Language features should not be completely prohibited. XWOS provides support for