Debian Codegraph @

docs/misra/deviations.rst

raw
1 .. SPDX-License-Identifier: CC-BY-4.0
2
3 MISRA C deviations for Xen
4 ==========================
5
6 The following is the list of MISRA C:2012 deviations for the Xen codebase that
7 are not covered by a `SAF-x-safe` or `SAF-x-false-positive-<tool>` comment, as
8 specified in docs/misra/documenting-violations.rst; the lack of
9 such comments is usually due to the excessive clutter they would bring to the
10 codebase or the impossibility to express such a deviation (e.g., if it's
11 composed of several conditions).
12
13 Deviations related to MISRA C:2012 Directives:
14 ----------------------------------------------
15
16 .. list-table::
17 :header-rows: 1
18
19 * - Directive identifier
20 - Justification
21 - Notes
22
23 * - D4.3
24 - Accepted for the ARM64 codebase
25 - Tagged as `disapplied` for ECLAIR on any other violation report.
26
27 * - D4.3
28 - The inline asm in 'xen/arch/arm/arm64/lib/bitops.c' is tightly coupled
29 with the surronding C code that acts as a wrapper, so it has been decided
30 not to add an additional encapsulation layer.
31 - Tagged as `deliberate` for ECLAIR.
32
33 * - D4.10
34 - Including multiple times a .c file is safe because every function or data item
35 it defines would in (the common case) be already defined.
36 Peer reviewed by the community.
37 - Tagged as `safe` for ECLAIR.
38
39 Deviations related to MISRA C:2012 Rules:
40 -----------------------------------------
41
42 .. list-table::
43 :header-rows: 1
44
45 * - Rule identifier
46 - Justification
47 - Notes
48
49 * - R2.1
50 - The compiler implementation guarantees that the unreachable code is
51 removed. Constant expressions and unreachable branches of if and switch
52 statements are expected.
53 - Tagged as `safe` for ECLAIR.
54
55 * - R2.1
56 - Unreachability caused by calls to the following functions or macros is
57 deliberate and there is no risk of code being unexpectedly left out.
58 - Tagged as `deliberate` for ECLAIR. Such macros are:
59 - BUG
60 - assert_failed
61 - __builtin_unreachable
62 - ASSERT_UNREACHABLE
63
64 * - R2.1
65 - Pure declarations, that is, declarations without initializations are not
66 executable, and therefore it is safe for them to be unreachable. The most
67 notable example of such a pattern being used in the codebase is that of
68 a variable declaration that should be available in all the clauses of a
69 switch statement.
70 - ECLAIR has been configured to ignore those statements.
71
72 * - R2.1
73 - The asm-offset files are not linked deliberately, since they are used to
74 generate definitions for asm modules.
75 - Tagged as `deliberate` for ECLAIR.
76
77 * - R2.2
78 - Proving compliance with respect to Rule 2.2 is generally impossible:
79 see `<https://arxiv.org/abs/2212.13933>`_ for details. Moreover, peer
80 review gives us confidence that no evidence of errors in the program's
81 logic has been missed due to undetected violations of Rule 2.2, if any.
82 Testing on time behavior gives us confidence on the fact that, should the
83 program contain dead code that is not removed by the compiler, the
84 resulting slowdown is negligible.
85 - Project-wide deviation, tagged as `disapplied` for ECLAIR.
86
87 * - R2.6
88 - Labels deliberately marked as unused trough the pseudo-attribute
89 `__maybe_unused` are either the result of them not being in certain build
90 configurations, or as a deliberate practice (e.g., `unimplemented_insn`).
91 Given that the compiler is then entitled to remove them, the presence of
92 such labels poses no risks.
93 - Tagged as `deliberate` for ECLAIR.
94
95 * - R3.1
96 - Comments starting with '/\*' and containing hyperlinks are safe as they
97 are not instances of commented-out code.
98 - Tagged as `safe` for ECLAIR.
99
100 * - R5.3
101 - As specified in rules.rst, shadowing due to macros being used as macro
102 arguments is allowed, as it's deemed not at risk of causing developer
103 confusion.
104 - Tagged as `safe` for ECLAIR. So far, the following macros are deviated:
105 - READ_SYSREG and WRITE_SYSREG
106 - max_{t}? and min_{t}?
107 - read_[bwlq] and read_[bwlq]_relaxed
108 - per_cpu and this_cpu
109 - __emulate_2op and __emulate_2op_nobyte
110 - read_debugreg and write_debugreg
111
112 * - R5.5
113 - Macros expanding to their own name are allowed.
114 - Tagged as `deliberate` for ECLAIR.
115
116 * - R5.5
117 - Clashes between names of function-like macros and identifiers of
118 non-callable entities are allowed.
119 - Tagged as `deliberate` for ECLAIR.
120
121 * - R5.5
122 - Clashes between function names and macros are deliberate for string
123 handling functions since some architectures may want to use their own
124 arch-specific implementation.
125 - Tagged as `deliberate` for ECLAIR.
126
127 * - R5.5
128 - In libelf, clashes between macros and function names are deliberate and
129 needed to prevent the use of undecorated versions of memcpy, memset and
130 memmove.
131 - Tagged as `deliberate` for ECLAIR.
132
133 * - R5.6
134 - The type ret_t is deliberately defined multiple times depending on the
135 type of guest to service.
136 - Tagged as `deliberate` for ECLAIR.
137
138 * - R5.6
139 - On X86, some types are deliberately defined multiple times, depending on
140 the number of guest paging levels.
141 - Tagged as `deliberate` for ECLAIR. Such types are:
142 - guest_intpte_t
143 - guest_l[12]e_t
144
145 * - R5.6
146 - Some files are not subject to respect MISRA rules at
147 the moment, but, among these out-of-scope files, there are definitions
148 of typedef names that are already defined within in-scope files and
149 therefore, ECLAIR does report a violation since not all the files
150 involved in the violation are excluded from the analysis.
151 - Tagged as `deliberate` for ECLAIR. Such excluded files are:
152 - xen/include/efi/*
153 - xen/arch/*/include/asm/*/efibind.h
154
155 * - R7.1
156 - It is safe to use certain octal constants the way they are defined
157 in specifications, manuals, and algorithm descriptions. Such places
158 are marked safe with a /\* octal-ok \*/ in-code comment.
159 - Tagged as `safe` for ECLAIR.
160
161 * - R7.2
162 - Violations caused by __HYPERVISOR_VIRT_START are related to the
163 particular use of it done in xen_mk_ulong.
164 - Tagged as `deliberate` for ECLAIR.
165
166 * - R7.4
167 - Allow pointers of non-character type as long as the pointee is
168 const-qualified.
169 - ECLAIR has been configured to ignore these assignments.
170
171 * - R8.3
172 - The type ret_t is deliberately used and defined as int or long depending
173 on the architecture.
174 - Tagged as `deliberate` for ECLAIR.
175
176 * - R8.3
177 - Some files are not subject to respect MISRA rules at
178 the moment, but some entity from a file in scope is used; therefore
179 ECLAIR does report a violation, since not all the files involved in the
180 violation are excluded from the analysis.
181 - Tagged as `deliberate` for ECLAIR. Such excluded files are:
182 - xen/arch/x86/time.c
183 - xen/arch/x86/acpi/cpu_idle.c
184 - xen/arch/x86/mpparse.c
185 - xen/common/bunzip2.c
186 - xen/common/unlz4.c
187 - xen/common/unlzma.c
188 - xen/common/unlzo.c
189 - xen/common/unxz.c
190 - xen/common/unzstd.c
191
192 * - R8.3
193 - Parameter name "unused" (with an optional numeric suffix) is deliberate
194 and makes explicit the intention of not using such parameter within the
195 function.
196 - Tagged as `deliberate` for ECLAIR.
197
198 * - R8.4
199 - The definitions present in the files 'asm-offsets.c' for any architecture
200 are used to generate definitions for asm modules, and are not called by
201 C code. Therefore the absence of prior declarations is safe.
202 - Tagged as `safe` for ECLAIR.
203
204 * - R8.4
205 - The functions defined in the file xen/common/coverage/gcov_base.c are
206 meant to be called from gcc-generated code in a non-release build
207 configuration. Therefore, the absence of prior declarations is safe.
208 - Tagged as `safe` for ECLAIR.
209
210 * - R8.4
211 - Functions and variables used only by asm modules are marked with
212 the `asmlinkage` macro. Existing code may use a SAF-1-safe
213 textual deviation (see safe.json), but new code should not use
214 it.
215 - Tagged as `safe` for ECLAIR.
216
217 * - R8.4
218 - Some functions are excluded from non-debug build, therefore the absence
219 of declaration is safe.
220 - Tagged as `safe` for ECLAIR, such functions are:
221 - apei_read_mce()
222 - apei_check_mce()
223 - apei_clear_mce()
224
225 * - R8.4
226 - Given that bsearch and sort are defined with the attribute 'gnu_inline',
227 it's deliberate not to have a prior declaration.
228 See Section \"6.33.1 Common Function Attributes\" of \"GCC_MANUAL\" for
229 a full explanation of gnu_inline.
230 - Tagged as `deliberate` for ECLAIR.
231
232 * - R8.4
233 - first_valid_mfn is defined in this way because the current lack of NUMA
234 support in Arm and PPC requires it.
235 - Tagged as `deliberate` for ECLAIR.
236
237 * - R8.6
238 - The following variables are compiled in multiple translation units
239 belonging to different executables and therefore are safe.
240
241 - current_stack_pointer
242 - bsearch
243 - sort
244 - Tagged as `safe` for ECLAIR.
245
246 * - R8.6
247 - Declarations without definitions are allowed (specifically when the
248 definition is compiled-out or optimized-out by the compiler).
249 - Tagged as `deliberate` in ECLAIR.
250
251 * - R8.6
252 - The search procedure for Unix linkers is well defined, see ld(1) manual:
253 "The linker will search an archive only once, at the location where it
254 is specified on the command line. If the archive defines a symbol which
255 was undefined in some object which appeared before the archive on the
256 command line, the linker will include the appropriate file(s) from the
257 archive".
258 In Xen, thanks to the order in which file names appear in the build
259 commands, if arch-specific definitions are present, they get always
260 linked in before searching in the lib.a archive resulting from xen/lib.
261 - Tagged as `deliberate` for ECLAIR.
262
263 * - R8.10
264 - The gnu_inline attribute without static is deliberately allowed.
265 - Tagged as `deliberate` for ECLAIR.
266
267 * - R9.5
268 - The possibility of committing mistakes by specifying an explicit
269 dimension is higher than omitting the dimension, therefore all such
270 instances of violations are deviated.
271 - Project-wide deviation, tagged as `deliberate` for ECLAIR.
272
273 * - R10.1, R10.3, R10.4
274 - The value-preserving conversions of integer constants are safe.
275 - Tagged as `safe` for ECLAIR.
276
277 * - R10.1
278 - Shifting non-negative integers to the right is safe.
279 - Tagged as `safe` for ECLAIR.
280
281 * - R10.1
282 - Shifting non-negative integers to the left is safe if the result is still
283 non-negative.
284 - Tagged as `safe` for ECLAIR.
285
286 * - R10.1
287 - Bitwise logical operations on non-negative integers are safe.
288 - Tagged as `safe` for ECLAIR.
289
290 * - R10.1
291 - The implicit conversion to Boolean for logical operator arguments is
292 well-known to all Xen developers to be a comparison with 0.
293 - Tagged as `safe` for ECLAIR.
294
295 * - R10.1
296 - Xen only supports architectures where signed integers are representend
297 using two's complement and all the Xen developers are aware of this. For
298 this reason, bitwise operations are safe.
299 - Tagged as `safe` for ECLAIR.
300
301 * - R10.1
302 - Given the assumptions on the toolchain detailed in
303 docs/misra/C-language-toolchain.rst and the build flags used by the
304 project, it is deemed safe to use bitwise shift operators.
305 See automation/eclair_analysis/deviations.ecl for the full explanation.
306 - Tagged as `safe` for ECLAIR.
307
308 * - R10.1
309 - The macro ISOLATE_LSB encapsulates the well-known pattern (x & -x)
310 applied to unsigned integer values on 2's complement architectures
311 (i.e., all architectures supported by Xen), used to obtain a mask where
312 just the least significant nonzero bit of x is set.
313 If no bits are set, 0 is returned.
314 - Tagged as `safe` for ECLAIR.
315
316 * - R11.1
317 - The conversion from a function pointer to unsigned long or (void \*) does
318 not lose any information, provided that the target type has enough bits
319 to store it.
320 - Tagged as `safe` for ECLAIR.
321
322 * - R11.1
323 - The conversion from a function pointer to a boolean has a well-known
324 semantics that do not lead to unexpected behaviour.
325 - Tagged as `safe` for ECLAIR.
326
327 * - R11.2
328 - The conversion from a pointer to an incomplete type to unsigned long
329 does not lose any information, provided that the target type has enough
330 bits to store it.
331 - Tagged as `safe` for ECLAIR.
332
333 * - R11.3
334 - Conversions to object pointers that have a pointee type with a smaller
335 (i.e., less strict) alignment requirement are safe.
336 - Tagged as `safe` for ECLAIR.
337
338 * - R11.6
339 - Conversions from and to integral types are safe, in the assumption that
340 the target type has enough bits to store the value.
341 See also Section \"4.7 Arrays and Pointers\" of \"GCC_MANUAL\"
342 - Tagged as `safe` for ECLAIR.
343
344 * - R11.6
345 - The conversion from a pointer to a boolean has a well-known semantics
346 that do not lead to unexpected behaviour.
347 - Tagged as `safe` for ECLAIR.
348
349 * - R11.8
350 - Violations caused by container_of are due to pointer arithmetic operations
351 with the provided offset. The resulting pointer is then immediately cast back to its
352 original type, which preserves the qualifier. This use is deemed safe.
353 Fixing this violation would require to increase code complexity and lower readability.
354 - Tagged as `safe` for ECLAIR.
355
356 * - R11.8
357 - Violations caused by function __hvm_copy occur when a const void
358 argument is passed, as the const qualifier is stripped. However, in such
359 cases, the function ensures that it does not modify the buffer
360 referenced by the argument, therefore, this use is deemed safe. Fixing
361 this violation would require to increase code complexity and lower
362 readability.
363 - Tagged as `safe` for ECLAIR.
364
365 * - R11.9
366 - __ACCESS_ONCE uses an integer, which happens to be zero, as a
367 compile time check. The typecheck uses a cast. The usage of zero or other
368 integers for this purpose is allowed.
369 - Tagged as `deliberate` for ECLAIR.
370
371 * - R13.5
372 - All developers and reviewers can be safely assumed to be well aware of
373 the short-circuit evaluation strategy for logical operators.
374 - Project-wide deviation; tagged as `disapplied` for ECLAIR.
375
376 * - R13.6
377 - On x86, macros alternative_v?call[0-9] use sizeof and typeof to check
378 that the argument types match the corresponding parameter ones.
379 - Tagged as `deliberate` for ECLAIR.
380
381 * - R13.6
382 - Anything, no matter how complicated, inside the BUILD_BUG_ON macro is
383 subject to a compile-time evaluation without relevant side effects."
384 - Tagged as `safe` for ECLAIR.
385
386 * - R14.2
387 - The severe restrictions imposed by this rule on the use of 'for'
388 statements are not counterbalanced by the presumed facilitation of the
389 peer review activity.
390 - Project-wide deviation; tagged as `disapplied` for ECLAIR.
391
392 * - R14.3
393 - The Xen team relies on the fact that invariant conditions of 'if'
394 statements and conditional operators are deliberate.
395 - Tagged as `deliberate` for ECLAIR.
396
397 * - R14.3
398 - Switches having a 'sizeof' operator as the condition are deliberate and
399 have limited scope.
400 - Tagged as `deliberate` for ECLAIR.
401
402 * - R14.3
403 - The use of an invariant size argument in {put,get}_unsafe_size and
404 array_access_ok, as defined in arch/x86(_64)?/include/asm/uaccess.h is
405 deliberate and is deemed safe.
406 - Tagged as `deliberate` for ECLAIR.
407
408 * - R14.4
409 - A controlling expression of 'if' and iteration statements having
410 integer, character or pointer type has a semantics that is well-known to
411 all Xen developers.
412 - Tagged as `deliberate` for ECLAIR.
413
414 * - R14.4
415 - The XEN team relies on the fact that the enum is_dying has the
416 constant with assigned value 0 act as false and the other ones as true,
417 therefore have the same behavior of a boolean.
418 - Tagged as `deliberate` for ECLAIR.
419
420 * - R16.2
421 - Complying with the Rule would entail a lot of code duplication in the
422 implementation of the x86 emulator, therefore it is deemed better to
423 leave such files as is.
424 - Tagged as `deliberate` for ECLAIR.
425
426 * - R16.3
427 - Statements that change the control flow (i.e., break, continue, goto,
428 return) and calls to functions that do not return the control back are
429 \"allowed terminal statements\".
430 - Tagged as `safe` for ECLAIR.
431
432 * - R16.3
433 - An if-else statement having both branches ending with one of the allowed
434 terminal statemets is itself an allowed terminal statements.
435 - Tagged as `safe` for ECLAIR.
436
437 * - R16.3
438 - An if-else statement having an always true condition and the true
439 branch ending with an allowed terminal statement is itself an allowed
440 terminal statement.
441 - Tagged as `safe` for ECLAIR.
442
443 * - R16.3
444 - A switch clause ending with a statement expression which, in turn, ends
445 with an allowed terminal statement (e.g., the expansion of
446 generate_exception()) is safe.
447 - Tagged as `safe` for ECLAIR.
448
449 * - R16.3
450 - A switch clause ending with a do-while-false the body of which, in turn,
451 ends with an allowed terminal statement (e.g., PARSE_ERR_RET()) is safe.
452 An exception to that is the macro ASSERT_UNREACHABLE() which is
453 effective in debug build only: a switch clause ending with
454 ASSERT_UNREACHABLE() is not considered safe.
455 - Tagged as `safe` for ECLAIR.
456
457 * - R16.3
458 - Switch clauses ending with pseudo-keyword \"fallthrough\" are safe.
459 - Tagged as `safe` for ECLAIR.
460
461 * - R16.3
462 - Switch clauses ending with failure method \"BUG()\" are safe.
463 - Tagged as `safe` for ECLAIR.
464
465 * - R16.3
466 - Existing switch clauses not ending with the break statement are safe if
467 an explicit comment indicating the fallthrough intention is present.
468 However, the use of such comments in new code is deprecated:
469 the pseudo-keyword "fallthrough" shall be used.
470 - Tagged as `safe` for ECLAIR. The accepted comments are:
471 - /\* fall through \*/
472 - /\* fall through. \*/
473 - /\* fallthrough \*/
474 - /\* fallthrough. \*/
475 - /\* Fall through \*/
476 - /\* Fall through. \*/
477 - /\* Fallthrough \*/
478 - /\* Fallthrough. \*/
479
480 * - R16.4
481 - Switch statements having a controlling expression of enum type
482 deliberately do not have a default case: gcc -Wall enables -Wswitch
483 which warns (and breaks the build as we use -Werror) if one of the enum
484 labels is missing from the switch.
485 - Tagged as `deliberate` for ECLAIR.
486
487 * - R16.4
488 - A switch statement with a single switch clause and no default label may
489 be used in place of an equivalent if statement if it is considered to
490 improve readability.
491 - Tagged as `deliberate` for ECLAIR.
492
493 * - R16.6
494 - A switch statement with a single switch clause and no default label may
495 be used in place of an equivalent if statement if it is considered to
496 improve readability.
497 - Tagged as `deliberate` for ECLAIR.
498
499 * - R17.1
500 - printf()-like functions are allowed to use the variadic features provided
501 by `stdarg.h`.
502 - Tagged as `deliberate` for ECLAIR.
503
504 * - R17.7
505 - Not using the return value of a function does not endanger safety if it
506 coincides with an actual argument.
507 - Tagged as `safe` for ECLAIR. Such functions are:
508 - __builtin_memcpy()
509 - __builtin_memmove()
510 - __builtin_memset()
511 - cpumask_check()
512
513 * - R18.2
514 - Subtractions between pointers where at least one of the operand is a
515 pointer to a symbol defined by the linker are safe.
516 - Tagged as `safe` for ECLAIR.
517
518 * - R18.2
519 - Subtraction between pointers encapsulated by macro page_to_mfn
520 are safe.
521 - Tagged as `safe` for ECLAIR.
522
523 * - R20.4
524 - The override of the keyword \"inline\" in xen/compiler.h is present so
525 that section contents checks pass when the compiler chooses not to
526 inline a particular function.
527 - Comment-based deviation.
528
529 * - R20.7
530 - Code violating Rule 20.7 is safe when macro parameters are used:
531 (1) as function arguments;
532 (2) as macro arguments;
533 (3) as array indices;
534 (4) as lhs in assignments;
535 (5) as initializers, possibly designated, in initalizer lists;
536 (6) as constant expressions of switch case labels.
537 - Tagged as `safe` for ECLAIR.
538
539 * - R20.7
540 - Violations due to the use of macros defined in files that are not
541 in scope for compliance are allowed, as that is imported code.
542 - Tagged as `safe` for ECLAIR.
543
544 * - R20.7
545 - To avoid compromising readability, the macros `alternative_(v)?call[0-9]`
546 are allowed not to parenthesize their arguments, as there are already
547 sanity checks in place.
548 - Tagged as `safe` for ECLAIR.
549
550 * - R20.7
551 - The macro `count_args_` is not compliant with the rule, but is not likely
552 to incur in the risk of being misused or lead to developer confusion, and
553 refactoring it to add parentheses breaks its functionality.
554 - Tagged as `safe` for ECLAIR.
555
556 * - R20.7
557 - The macros `{COMPILE,RUNTIME}_CHECK` defined in
558 `xen/include/xen/self-tests.h` are allowed not to parenthesize the "fn"
559 argument, to allow function-like macros to be tested as well as
560 functions. Given the specialized use of these macros and their limited
561 usage scope, omitting parentheses is deemed unlikely to cause issues.
562 - Tagged as `deliberate` for ECLAIR.
563
564 * - R20.7
565 - Problems related to operator precedence can not occur if the expansion
566 of the macro argument is surrounded by tokens '{', '}' and ';'.
567 - Tagged as `safe` for ECLAIR.
568
569 * - R20.12
570 - Variadic macros that use token pasting often employ the gcc extension
571 `ext_paste_comma`, as detailed in `C-language-toolchain.rst`, which is
572 not easily replaceable; macros that in addition perform regular argument
573 expansion on the same argument subject to the # or ## operators violate
574 the Rule if the argument is a macro.
575 - Tagged as `deliberate` for ECLAIR.
576
577 * - R20.12
578 - Macros that are used for runtime or build-time assertions contain
579 deliberate uses of an argument as both a regular argument and a
580 stringification token, to provide useful diagnostic messages.
581 - Tagged as `deliberate` for ECLAIR.
582
583 * - R20.12
584 - GENERATE_CASE is a local helper macro that allows some selected switch
585 statements to be more compact and readable. As such, the risk of
586 developer confusion in using such macro is deemed negligible. This
587 construct is deviated only in Translation Units that present a violation
588 of the Rule due to uses of this macro.
589 - Tagged as `deliberate` for ECLAIR.
590
591 * - R21.9
592 - Xen does not use the `bsearch` and `qsort` functions provided by the C
593 Standard Library, but provides in source form its own implementation,
594 therefore any unspecified or undefined behavior associated to the
595 functions provided by the Standard Library does not apply. Any such
596 behavior that may exist in such functions is therefore under the
597 jurisdiction of other MISRA C rules.
598 - Project-wide deviation, tagged as `deliberate` for ECLAIR.
599
600 * - R21.10
601 - Xen does not use the facilities provided by the `\<time.h\>` provided by the C
602 Standard Library, but provides in source form its own implementation,
603 therefore any unspecified, undefined or implementation-defined behavior
604 associated to the functions provided by the Standard Library does not
605 apply. Any such behavior that may exist in such functions is therefore
606 under the jurisdiction of other MISRA C rules.
607 - Project-wide deviation, tagged as `deliberate` for ECLAIR.
608
609 Other deviations:
610 -----------------
611
612 .. list-table::
613 :header-rows: 1
614
615 * - Deviation
616 - Justification
617
618 * - do-while-0 and do-while-1 loops
619 - The do-while-0 and do-while-1 loops are well-recognized loop idioms used
620 by the Xen community and can therefore be used, even though they would
621 cause a number of violations in some instances.
622
623 * - while-0 and while-1 loops
624 - while-0 and while-1 are well-recognized loop idioms used by the Xen
625 community and can therefore be used, even though they would cause a
626 number of violations in some instances.