MISRA C rules for Xen¶
Note
IMPORTANT All MISRA C rules, text, and examples are copyrighted by the MISRA Consortium Limited and used with permission.
Please refer to https://www.misra.org.uk/ to obtain a copy of MISRA C, or for licensing options for other use of the rules.
The following is the list of MISRA C rules that apply to the Xen hypervisor.
It is possible that in specific circumstances it is best not to follow a rule because it is not possible or because the alternative leads to better code quality. Those cases are called “deviations”. They are permissible as long as they are documented. For details, please refer to docs/misra/documenting-violations.rst and docs/misra/deviations.rst
Other documentation mechanisms are work-in-progress.
The existing codebase is not 100% compliant with the rules. Some of the violations are meant to be documented as deviations, while some others should be fixed. Both compliance and documenting deviations on the existing codebase are work-in-progress.
The list below might need to be updated over time. Reach out to THE REST maintainers if you want to suggest a change.
Dir number |
Severity |
Summary |
Notes |
---|---|---|---|
Required |
Any implementation-defined behaviour on which the output of the program depends shall be documented and understood |
||
Required |
All source files shall compile without any compilation errors |
||
Required |
If a function returns error information then that error information shall be tested |
||
Required |
Precautions shall be taken in order to prevent the contents of a header file being included more than once |
Files that are intended to be included more than once do not need to conform to the directive |
|
Required |
The validity of values passed to library functions shall be checked |
We do not have libraries in Xen (libfdt and others are not considered libraries from MISRA C point of view as they are imported in source form) |
|
Required |
The validity of values received from external sources shall be checked |
Rule number |
Severity |
Summary |
Notes |
---|---|---|---|
Required |
The program shall contain no violations of the standard C syntax and constraints, and shall not exceed the implementation’s translation limits |
We make use of several compiler extensions as documented by C-language-toolchain.rst |
|
Required |
There shall be no occurrence of undefined or critical unspecified behaviour |
||
Required |
Emergent language features shall not be used |
Emergent language features, such as C11 features, should not be confused with similar compiler extensions, which we use. When the time comes to adopt C11, this rule will be revisited. |
|
Required |
A project shall not contain unreachable code |
|
|
Advisory |
A function should not contain unused label declarations |
||
Required |
The character sequences /* and // shall not be used within a comment |
Comments containing hyperlinks inside C-style block comments are safe |
|
Required |
Line-splicing shall not be used in // comments |
||
Required |
Octal and hexadecimal escape sequences shall be terminated |
||
Advisory |
Trigraphs should not be used |
||
Required |
External identifiers shall be distinct |
The Xen characters limit for identifiers is 40. Public headers (xen/include/public/) are allowed to retain longer identifiers for backward compatibility. |
|
Required |
Identifiers declared in the same scope and name space shall be distinct |
The Xen characters limit for identifiers is 40. Public headers (xen/include/public/) are allowed to retain longer identifiers for backward compatibility. |
|
Required |
An identifier declared in an inner scope shall not hide an identifier declared in an outer scope |
Using macros as macro parameters at invocation time is allowed even if both macros use identically named local variables, e.g. max(var0, min(var1, var2)) |
|
Required |
Macro identifiers shall be distinct |
The Xen characters limit for macro identifiers is 40. Public headers (xen/include/public/) are allowed to retain longer identifiers for backward compatibility. |
|
Required |
A typedef name shall be a unique identifier |
||
Required |
Bit-fields shall only be declared with an appropriate type |
In addition to the C99 types, we also consider appropriate types enum and all explicitly signed / unsigned integer types. |
|
Required |
Single-bit named bit fields shall not be of a signed type |
||
Required |
Octal constants shall not be used |
||
Required |
A “u” or “U” suffix shall be applied to all integer constants that are represented in an unsigned type |
The rule asks that any integer literal that is implicitly unsigned is made explicitly unsigned by using one of the indicated suffixes. As an example, on a machine where the int type is 32-bit wide, 0x77777777 is signed whereas 0x80000000 is (implicitly) unsigned. In order to comply with the rule, the latter should be rewritten as either 0x80000000u or 0x80000000U. Consistency considerations may suggest using the same suffix even when not required by the rule. For instance, if one has: Original: f(0x77777777); f(0x80000000); one should do Solution 1: f(0x77777777U); f(0x80000000U); over Solution 2: f(0x77777777); f(0x80000000U); after having ascertained that “Solution 1” is compatible with the intended semantics. |
|
Required |
The lowercase character l shall not be used in a literal suffix |
||
Required |
A string literal shall not be assigned to an object unless the object type is pointer to const-qualified char |
All “character types” are permitted, as long as the string element type and the character type match. (There should be no casts.) Assigning a string literal to any object with type “pointer to const-qualified void” is allowed. |
|
Required |
Types shall be explicitly specified |
||
Required |
Function types shall be in prototype form with named parameters |
Clarification: both function and function pointers types shall have named parameters. |
|
Required |
All declarations of an object or function shall use the same names and type qualifiers |
The type ret_t maybe be deliberately used and defined as int or long depending on the type of guest to service |
|
Required |
A compatible declaration shall be visible when an object or function with external linkage is defined |
Allowed exceptions: asm-offsets.c, definitions for asm modules not called from C code, gcov_base.c |
|
Required |
An external object or function shall be declared once in one and only one file |
||
Required |
An identifier with external linkage shall have exactly one external definition |
Declarations without definitions are allowed (specifically when the definition is compiled-out or optimized-out by the compiler) |
|
Required |
The static storage class specifier shall be used in all declarations of objects and functions that have internal linkage |
||
Required |
An inline function shall be declared with the static storage class |
gnu_inline (without static) is allowed. |
|
Required |
Within an enumerator list the value of an implicitly-specified enumeration constant shall be unique |
||
Required |
The restrict type qualifier shall not be used |
||
Mandatory |
The value of an object with automatic storage duration shall not be read before it has been set |
Rule clarification: do not use variables before they are initialized. An explicit initializer is not necessarily required. Try reducing the scope of the variable. If an explicit initializer is added, consider initializing the variable to a poison value. |
|
Required |
The initializer for an aggregate or union shall be enclosed in braces |
||
Required |
Arrays shall not be partially initialized |
{} is also allowed to specify explicit zero-initialization |
|
Required |
An element of an object shall not be initialized more than once |
||
Required |
Operands shall not be of an inappropriate essential type |
|
|
Required |
Expressions of essentially character type shall not be used inappropriately in addition and subtraction operations |
||
Required |
The value of an expression shall not be assigned to an object with a narrower essential type or of a different essential type category |
Please beware that this rule has many violations in the Xen codebase today, and its adoption is aspirational. However, when submitting new patches please try to decrease the number of violations when possible. gcc has a helpful warning that can help you spot and remove violations of this kind: conversion. For instance, you can use it as follows: CFLAGS=”-Wconversion -Wno-error=sign-conversion -Wno-error=conversion” make -C xen |
|
Required |
Both operands of an operator in which the usual arithmetic conversions are performed shall have the same essential type category |
Please beware that this rule has many violations in the Xen codebase today, and its adoption is aspirational. However, when submitting new patches please try to decrease the number of violations when possible. gcc has a helpful warning that can help you spot and remove violations of this kind: arith-conversion. For instance, you can use it as follows: CFLAGS=”-Warith-conversion -Wno-error=arith-conversion” make -C xen |
|
Required |
A cast shall not be performed between pointer to object and a noninteger arithmetic type |
||
Required |
A cast shall not remove any const or volatile qualification from the type pointed to by a pointer |
||
Required |
The macro NULL shall be the only permitted form of null pointer constant |
||
Mandatory |
The sizeof operator shall not have an operand which is a function parameter declared as “array of type” |
||
Mandatory |
The operand of the sizeof operator shall not contain any expression which has potential side effects |
||
Required |
Initializer lists shall not contain persistent side effects |
||
Required |
A loop counter shall not have essentially floating type |
||
Required |
Controlling expressions shall not be invariant |
Due to the extensive usage of IS_ENABLED, sizeof compile-time checks, and other constructs that are detected as errors by MISRA C scanners, managing the configuration of a MISRA C scanner for this rule would be unmanageable. Thus, this rule is adopted with a project-wide deviation on if, ?:, switch(sizeof(…)), and switch(offsetof(…)) statements. while(0) and while(1) and alike are allowed. |
|
Required |
A switch-expression shall not have essentially Boolean type |
||
Mandatory |
A function shall not be declared implicitly |
||
Mandatory |
All exit paths from a function with non-void return type shall have an explicit return statement with an expression |
||
Mandatory |
The declaration of an array parameter shall not contain the static keyword between the [ ] |
||
Required |
The relational operators > >= < and <= shall not be applied to objects of pointer type except where they point into the same object |
||
Mandatory |
An object shall not be assigned or copied to an overlapping object |
Be aware that the static analysis tool Eclair might report several findings for Rule 19.1 of type “caution”. These are instances where Eclair is unable to verify that the code is valid in regard to Rule 19.1. Caution reports are not violations. |
|
Required |
Expressions resulting from the expansion of macro parameters shall be enclosed in parentheses |
Extra parentheses are not required when macro parameters are used as function arguments, as macro arguments, array indices, lhs in assignments |
|
Required |
A line whose first token is # shall be a valid preprocessing directive |
||
Required |
All #else #elif and #endif preprocessor directives shall reside in the same file as the #if #ifdef or #ifndef directive to which they are related |
||
Mandatory |
Any value passed to a function in <ctype.h> shall be representable as an unsigned char or be the value EOF |
||
Mandatory |
Use of the string handling functions from <string.h> shall not result in accesses beyond the bounds of the objects referenced by their pointer parameters |
||
Mandatory |
The size_t argument passed to any function in <string.h> shall have an appropriate value |
||
Mandatory |
The pointers returned by the Standard Library functions localeconv, getenv, setlocale or, strerror shall only be used as if they have pointer to const-qualified type |
||
Mandatory |
The pointer returned by the Standard Library functions asctime ctime gmtime localtime localeconv getenv setlocale or strerror shall not be used following a subsequent call to the same function |
||
Required |
The Standard Library function system of <stdlib.h> shall not be used |
||
Mandatory |
A block of memory shall only be freed if it was allocated by means of a Standard Library function |
||
Mandatory |
There shall be no attempt to write to a stream which has been opened as read-only |
||
Mandatory |
A pointer to a FILE object shall not be dereferenced |
||
Mandatory |
The value of a pointer to a FILE shall not be used after the associated stream has been closed |