blob: 315d5f988c204e3796c55344a2293bd3b3aae943 [file] [log] [blame]
Soby Mathewb4c6df42022-11-09 11:13:29 +00001.. SPDX-License-Identifier: BSD-3-Clause
2.. SPDX-FileCopyrightText: Copyright TF-RMM Contributors.
3
4Coding Standard
5===============
6
7This document describes the coding rules to follow to contribute to the project.
8
9General
10-------
11
12The following coding standard is derived from `MISRA C:2012 Guidelines`_,
13`TF-A coding style`_ and `Linux kernel coding style`_ coding standards.
14
15File Encoding
16-------------
17
18The source code must use the **UTF-8** character encoding. Comments and
19documentation may use non-ASCII characters when required (e.g. Greek letters
20used for units) but code itself is still limited to ASCII characters.
21
22Language
23--------
24
25The primary language for comments and naming must be International English. In
26cases where there is a conflict between the American English and British English
27spellings of a word, the American English spelling is used.
28
29Exceptions are made when referring directly to something that does not use
30international style, such as the name of a company. In these cases the existing
31name should be used as-is.
32
33C Language Standard
34-------------------
35
36The C language mode used for |RMM| is *GNU11*. This is the "GNU dialect of ISO
37C11", which implies the *ISO C11* standard with GNU extensions.
38
39Both GCC and Clang compilers have support for *GNU11* mode, though
40Clang does lack support for a small number of GNU extensions. These
41missing extensions are rarely used, however, and should not pose a problem.
42
43Length
44------
45
46- Each file, function and scopes should have a logical uniting theme.
47
48 No length limit is set for a file.
49
50- A function should be 24 lines maximum.
51
52 This will not be enforced, any function being longer should trigger a
53 discussion during the review process.
54
55- A line must be <= 80 characters, except for string literals as it would make
56 any search for it more difficult.
57
58- A variable should not be longer than 31 characters.
59
60 Although the `C11 specification`_ specifies that the number of signitificant
61 characters in an identifier is implementation defined it sets the translation
62 limit to the 31 initial characters.
63
64+--------------+-----------------------------------+
65| TYPE | LIMIT |
66+==============+===================================+
67| function | 24 lines (not enforced) |
68+--------------+-----------------------------------+
69| line | 80 characters |
70+--------------+-----------------------------------+
71| identifier | 31 characters |
72+--------------+-----------------------------------+
73
74
75Headers/Footers
76---------------
77
78- Include guards:
79
80.. code:: c
81
82 #ifndef FILE_NAME_H
83 #define FILE_NAME_H
84
85 <header content>
86
87 #endif /* FILE_NAME_H */
88
89- Include statement variant is <>:
90
91.. code:: c
92
93 #include <file.h>
94
95
96- Include files should be alphabetically ordered:
97
98.. code:: c
99
100 #include <axxxx.h>
101 #include <bxxxx.h>
102 [...]
103 #include <zxxxx.h>
104
105- If possible, use forward declaration of struct types in public headers.
106 This will reduce interdependence of header file inclusion.
107
108.. code:: c
109
110 #include <axxxx.h>
111 #include <bxxxx.h>
112 [...]
113 /* forward declaration */
114 struct x;
115 void foo(struct *x);
116
117
118Naming conventions
119------------------
120
121- Case:
122 Functions and variables must be in Snake Case
123
124.. code:: c
125
126 unsigned int my_snake_case_variable = 0U;
127
128 void my_snake_case_function(void)
129 {
130 [...]
131 }
132
133
134- Local variables should be declared at the top of the closest opening scope
135 and should be short.
136
137 We won't enforce a length, and defining short is difficult, this motto
138 (from Linux) catches the spirit
139
140 +---------------------------------------------------------------------------+
141 | LOCAL variable names should be short, and to the point. |
142 | |
143 | If you have some random integer loop counter, |
144 | it should probably be called i. |
145 | |
146 | Calling it loop_counter is non-productive, |
147 | if there is no chance of it being mis-understood. |
148 | |
149 | Similarly, tmp can be just about any type of variable that is |
150 | used to hold a temporary value. |
151 | |
152 | If you are afraid to mix up your local variable names, |
153 | you have another problem. |
154 +---------------------------------------------------------------------------+
155
156.. code:: c
157
158 int foo(const int a)
159 {
160 int c; /* needed in the function */
161 c = a; /* MISRA-C rules recommend to not modify arguments variables */
162
163 if (c == 42) {
164 int b; /* needed only in this "if" statment */
165
166 b = bar(); /* bar will return an int */
167 if (b != -1) {
168 c += b;
169 }
170 }
171 return c;
172 }
173
174- Use an appropraite prefix for public API of a component. For example,
175 if the component name is `bar`, then the init API of the component
176 should be called `bar_init()`.
177
178Indentation
179-----------
180
181Use **tabs** for indentation. The use of spaces for indentation is forbidden
182except in the case where a term is being indented to a boundary that cannot be
183achieved using tabs alone.
184
185Tab spacing should be set to **8 characters**.
186
187Trailing whitespaces or tabulations are not allowed and must be trimmed.
188
189Spacing
190-------
191
192Single spacing should be used around most operators, including:
193
194- Arithmetic operators (``+``, ``-``, ``/``, ``*``, ``%``)
195- Assignment operators (``=``, ``+=``, etc)
196- Boolean operators (``&&``, ``||``)
197- Comparison operators (``<``, ``>``, ``==``, etc)
198- Shift operators (``>>``, ``<<``)
199- Logical operators (``&``, ``|``, etc)
200- Flow control (``if``, ``else``, ``switch``, ``while``, ``return``, etc)
201
202No spacing should be used around the following operators
203
204- Cast (``()``)
205- Indirection (``*``)
206
207Braces
208------
209
210- Use K&R style for statements.
211
212- Function opening braces are on a new line.
213
214- Use braces even for singled line.
215
216
217.. code:: c
218
219 void function(void)
220 {
221 /* if statement */
222 if (my_test) {
223 do_this();
224 do_that();
225 }
226
227 /* if/else statement */
228 if (my_Test) {
229 do_this();
230 do_that();
231 } else {
232 do_other_this();
233 }
234 }
235
236Commenting
237----------
238
239Double-slash style of comments (//) is not allowed, below are examples of
240correct commenting.
241
242.. code:: c
243
244 /*
245 * This example illustrates the first allowed style for multi-line comments.
246 *
247 * Blank lines within multi-lines are allowed when they add clarity or when
248 * they separate multiple contexts.
249 */
250
251.. code:: c
252
253 /**************************************************************************
254 * This is the second allowed style for multi-line comments.
255 *
256 * In this style, the first and last lines use asterisks that run the full
257 * width of the comment at its widest point.
258 *
259 * This style can be used for additional emphasis.
260 *************************************************************************/
261
262.. code:: c
263
264 /* Single line comments can use this format */
265
266.. code:: c
267
268 /***************************************************************************
269 * This alternative single-line comment style can also be used for emphasis.
270 **************************************************************************/
271
272
273Error return values and Exception handling
274------------------------------------------
275
276- Function return type must be explicitly defined.
277
278- Unless specifed otherwise by an official specification, return values must be
279 used to return success or failure (Standard Posix error codes).
280
281 Return an integer if the function is an action or imperative command
282 Failure: -Exxx (STD posix error codes, unless specified otherwise)
283
284 Success: 0
285
286 Return a boolean if the function is as predicate
287 Failure: false
288
289 Success: true
290
291- If a function returns error information, then that error information shall
292 be tested.
293
294 Exceptions are allowed for STDLIB functions (memcpy/printf/...) in which case
295 it must be void casted.
296
297.. code:: c
298
299 #define MY_TRANSFORMED_ERROR (-1)
300
301 void my_print_function(struct my_struct in_mystruct)
302 {
303 long long transformed_a = my_transform_a(in_mystruct.a);
304
305 if (transform_a != MY_TRANSFORMED_ERROR) {
306 (void)printf("STRUCT\n\tfield(a): %ll\n", transformed_a);
307 } else {
308 (void)printf("STRUCT\n\tERROR %ll\n", transformed_a);
309 }
310 }
311
312
313Use of asserts and panic
314------------------------
315
316Assertions, as a general rule, are only used to catch errors during
317development cycles and are removed from production binaries. They are
318useful to document pre-conditions for a function or impossible conditions
319in code. They are not substitutes for proper error checking and any
320expression used to test an assertion must not have a side-effect.
321
322For example,
323
324.. code:: c
325
326 assert(--i == 0);
327
328should not be used in code.
329
330Assertions can be used to validate input arguments to an API as long as
331the caller and callee are within the same trust boundary.
332
333``panic()`` is used in places wherein it is not possible to continue the
334execution of program sensibly. It should be used sparingly within code
335and, if possible, instead of panic(), components should return error
336back to the caller and the caller can decide on the appropriate action.
337This is particularly useful to build resilence to the program wherein
338non-functional part of the program can be disabled and, if possible,
339other functional aspects of the program can be kept running.
340
341Using COMPILER_ASSERT to check for compile time data errors
342-----------------------------------------------------------
343
344Where possible, use the ``COMPILER_ASSERT`` macro to check the validity of
345data known at compile time instead of checking validity at runtime, to avoid
346unnecessary runtime code.
347
348For example, this can be used to check that the assembler's and compiler's views
349of the size of an array is the same.
350
351.. code:: c
352
353 #include <utils_def.h>
354
355 define MY_STRUCT_SIZE 8 /* Used by assembler source files */
356
357 struct my_struct {
358 uint32_t arg1;
359 uint32_t arg2;
360 };
361
362 COMPILER_ASSERT(MY_STRUCT_SIZE == sizeof(struct my_struct));
363
364
365If ``MY_STRUCT_SIZE`` in the above example were wrong then the compiler would
366emit an error like this:
367
368::
369
370 my_struct.h:10:1: note: in expansion of macro 'COMPILER_ASSERT'
371 10 | COMPILER_ASSERT(MY_STRUCT_SIZE == sizeof(struct my_struct));
372 | ^~~~~~~~~~~~~~~
373
374Data types, structures and typedefs
375-----------------------------------
376
377- Data Types:
378
379The |RMM| codebase should be kept as portable as possible for 64-bits platforms.
380To help with this, the following data type usage guidelines should be followed:
381
382- Where possible, use the built-in *C* data types for variable storage (for
383 example, ``char``, ``int``, ``long long``, etc) instead of the standard *C11*
384 types. Most code is typically only concerned with the minimum size of the
385 data stored, which the built-in *C* types guarantee.
386
387- Avoid using the exact-size standard *C11* types in general (for example,
388 ``uint16_t``, ``uint32_t``, ``uint64_t``, etc) since they can prevent the
389 compiler from making optimizations. There are legitimate uses for them,
390 for example to represent data of a known structure. When using them in a
391 structure definition, consider how padding in the structure will work across
392 architectures.
393
394- Use ``int`` as the default integer type - it's likely to be the fastest on all
395 systems. Also this can be assumed to be 32-bit as a consequence of the
396 `Procedure Call Standard for the Arm 64-bit Architecture`_ .
397
398- Avoid use of ``short`` as this may end up being slower than ``int`` in some
399 systems. If a variable must be exactly 16-bit, use ``int16_t`` or
400 ``uint16_t``.
401
402- ``long`` are defined as LP64 (64-bit), this is guaranteed to be 64-bit.
403
404- Use ``char`` for storing text. Use ``uint8_t`` for storing other 8-bit data.
405
406- Use ``unsigned`` for integers that can never be negative (counts,
407 indices, sizes, etc). |RMM| intends to comply with MISRA "essential type"
408 coding rules (10.X), where signed and unsigned types are considered different
409 essential types. Choosing the correct type will aid this. MISRA static
410 analysers will pick up any implicit signed/unsigned conversions that may lead
411 to unexpected behaviour.
412
413- For pointer types:
414
415 - If an argument in a function declaration is pointing to a known type then
416 simply use a pointer to that type (for example: ``struct my_struct *``).
417
418 - If a variable (including an argument in a function declaration) is pointing
419 to a general, memory-mapped address, an array of pointers or another
420 structure that is likely to require pointer arithmetic then use
421 ``uintptr_t``. This will reduce the amount of casting required in the code.
Soby Mathew86693432023-05-11 16:04:26 +0100422 Avoid using ``unsigned long`` or ``unsigned long long`` for this purpose;
423 it may work but is less portable.
Soby Mathewb4c6df42022-11-09 11:13:29 +0000424
Soby Mathew86693432023-05-11 16:04:26 +0100425 - Use of ``void *`` is generally discouraged. Although it is useful to
426 represent pointers to types that are abstracted away from the callers and
427 has useful implicit cast properties, for the sake of a more uniform code
428 base, we encourage use of ``uintptr_t`` where possible.
Soby Mathewb4c6df42022-11-09 11:13:29 +0000429
430 - Avoid pointer arithmetic generally (as this violates MISRA C 2012 rule
431 18.4) and especially on void pointers (as this is only supported via
432 language extensions and is considered non-standard). In |RMM|, setting the
433 ``W`` build flag to ``W=3`` enables the *-Wpointer-arith* compiler flag and
434 this will emit warnings where pointer arithmetic is used.
435
436 - Use ``ptrdiff_t`` to compare the difference between 2 pointers.
437
438- Use ``size_t`` when storing the ``sizeof()`` something.
439
Soby Mathew86693432023-05-11 16:04:26 +0100440- Use ``ssize_t`` when returning the ``sizeof()`` something from a function
441 that can also return an error code; the signed type allows for a negative
442 return code in case of error. This practice should be used sparingly.
Soby Mathewb4c6df42022-11-09 11:13:29 +0000443
Soby Mathew86693432023-05-11 16:04:26 +0100444- Use ``uint64_t`` to store the contents of an AArch64 register or
445 represent a 64-bit value. Use of ``unsigned long`` or ``u_register_t``
446 for these purposes is discouraged.
Soby Mathewb4c6df42022-11-09 11:13:29 +0000447
448These guidelines should be updated if additional types are needed.
449
450- Typedefs:
451
452Typedef should be avoided and used only to create opaque types.
453An opaque data type is one whose concrete data structure is not publicly
454defined. Opaque data types can be used on handles to resources that the caller
455is not expected to address directly.
456
457.. code:: c
458
459 /* File main.c */
460 #include <my_lib.h>
461
462 int main(void)
463 {
464 context_t *context;
465 int res;
466
467 context = my_lib_init();
468
469 res = my_lib_compute(context, "2x2");
470 if (res == -EMYLIB_ERROR) {
471 return -1
472 }
473
474 return res;
475 }
476
477.. code:: c
478
479 /* File my_lib.h */
480 #ifndef MY_LIB_H
481 #define MY_LIB_H
482
483 typedef struct my_lib_context {
484 [...] /* whatever internal private variables you need in my_lib */
485 } context_t;
486
487 #endif /* MY_LIB_H */
488
489Macros and Enums
490----------------
491
492- Favor functions over macros.
493
494- Preprocessor macros and enums values are written in all uppercase text.
495
496- A numerical value shall be typed.
497
498.. code:: c
499
500 /* Common C usage​ */
501 #define MY_MACRO 4UL
502
503 /* If used in C and ASM (included from a .S file)​ */
504 #define MY_MACRO UL(4)
505
506- Expressions resulting from the expansion of macro parameters must be enclosed
507 in parentheses.
508
509- A macro parameter immediately following a # operator mustn't be immediately
510 followed by a ## operator.
511
512.. code:: c
513
514 #define SINGLE_HASH_OP(x) (#x) /* allowed */
515 #define SINGLE_DOUBLE_HASH_OP(x, y) (x ## y) /* allowed */
516 #define MIXED_HASH_OP(x, y) (#x ## y) /* not allowed */
517
518- Avoid defining macros that affect the control flow (i.e. avoid using
519 return/goto in a macro).
520
521- Macro with multiple statements can be enclosed in a do-while block or in a
522 expression statement.
523
524.. code:: c
525
526 int foo(char **b);
527
528 #define M1(a, b) \
529 do { \
530 if ((a) == 5) { \
531 foo((b)); \
532 } \
533 } while (false)
534
535 #define M2(a, b) \
536 ({ \
537 if ((a) == 5) { \
538 foo((b)); \
539 } \
540 })
541
542 int foo(char **b)
543 {
544 return 42;
545 }
546
547 int main(int ac, char **av)
548 {
549 if (ac == 1) {
550 M1(ac, av);
551 } else if (ac == 2) {
552 M2(ac, av);
553 } else {
554 return -1;
555 }
556
557 return ac;
558 }
559
560Switch statements
561-----------------
562
563- Return in a *case* are allowed.
564
565- Fallthrough are allowed as long as they are commented.
566
567- Do not rely on type promotion between the switch type and the case type.
568
569Inline assembly
570---------------
571
572- Favor C language over assembly language.
573
574- Document all usage of assembly.
575
576- Do not mix C and ASM in the same file.
577
578Libc functions that are banned or to be used with caution
579---------------------------------------------------------
580
581Below is a list of functions that present security risks.
582
583+------------------------+--------------------------------------+
584| libc function | Comments |
585+========================+======================================+
586| ``strcpy, wcscpy``, | use strlcpy instead |
587| ``strncpy`` | |
588+------------------------+--------------------------------------+
589| ``strcat, wcscat``, | use strlcat instead |
590| ``strncat`` | |
591+------------------------+--------------------------------------+
592| ``sprintf, vsprintf`` | use snprintf, vsnprintf |
593| | instead |
594+------------------------+--------------------------------------+
595| ``snprintf`` | if used, ensure result fits in buffer|
596| | i.e : snprintf(buf,size...) < size |
597+------------------------+--------------------------------------+
598| ``vsnprintf`` | if used, inspect va_list match types |
599| | specified in format string |
600+------------------------+--------------------------------------+
601| ``strtok, strtok_r``, | Should not be used |
602| ``strsep`` | |
603+------------------------+--------------------------------------+
604| ``ato*`` | Should not be used |
605+------------------------+--------------------------------------+
606| ``*toa`` | Should not be used |
607+------------------------+--------------------------------------+
608
609The use of above functions are discouraged and will only be allowed
610in justified cases after a discussion has been held either on the mailing
611list or during patch review and it is agreed that no alternative to their
612use is available. The code containing the banned APIs must properly justify
613their usage in the comments.
614
615The above restriction does not apply to Third Party IP code inside the ``ext/``
616directory.
617
618-----------
619
620.. _`Procedure Call Standard for the Arm 64-bit Architecture`: https://developer.arm.com/docs/ihi0055/latest/
621.. _`Linux kernel coding style`: https://www.kernel.org/doc/html/latest/process/coding-style.html
622.. _`MISRA C:2012 Guidelines`: https://www.misra.org.uk/Activities/MISRAC/tabid/160/Default.aspx
623.. _`TF-A coding style`: https://trustedfirmware-a.readthedocs.io/en/latest/process/coding-style.html
624.. _`C11 specification`: https://en.wikipedia.org/wiki/C11_(C_standard_revision)