1.. 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.
422    Avoid using ``unsigned long`` or ``unsigned long long`` for this purpose; it
423    may work but is less portable.
424
425  - For other pointer arguments in a function declaration, use ``void *``. This
426    includes pointers to types that are abstracted away from the known API and
427    pointers to arbitrary data. This allows the calling function to pass a
428    pointer argument to the function without any explicit casting (the cast to
429    ``void *`` is implicit). The function implementation can then do the
430    appropriate casting to a specific type.
431
432  - Avoid pointer arithmetic generally (as this violates MISRA C 2012 rule
433    18.4) and especially on void pointers (as this is only supported via
434    language extensions and is considered non-standard). In |RMM|, setting the
435    ``W`` build flag to ``W=3`` enables the *-Wpointer-arith* compiler flag and
436    this will emit warnings where pointer arithmetic is used.
437
438  - Use ``ptrdiff_t`` to compare the difference between 2 pointers.
439
440- Use ``size_t`` when storing the ``sizeof()`` something.
441
442- Use ``ssize_t`` when returning the ``sizeof()`` something from a function that
443  can also return an error code; the signed type allows for a negative return
444  code in case of error. This practice should be used sparingly.
445
446- Use ``u_register_t`` when it's important to store the contents of a register
447  in its native size (64-bit in |AArch64|). This is not a
448  standard *C11* type but is widely available in libc implementations.
449  Where possible, cast the variable to a more appropriate type before
450  interpreting the data. For example, the following structure uses this type to
451  minimize the storage required for the set of registers:
452
453.. code:: c
454
455    typedef struct aapcs64_params {
456            u_register_t arg0;
457            u_register_t arg1;
458            u_register_t arg2;
459            u_register_t arg3;
460            u_register_t arg4;
461            u_register_t arg5;
462            u_register_t arg6;
463            u_register_t arg7;
464    } aapcs64_params_t;
465
466If some code wants to operate on ``arg0`` and knows that it represents a 32-bit
467unsigned integer on all systems, cast it to ``unsigned int``.
468
469These guidelines should be updated if additional types are needed.
470
471- Typedefs:
472
473Typedef should be avoided and used only to create opaque types.
474An opaque data type is one whose concrete data structure is not publicly
475defined. Opaque data types can be used on handles to resources that the caller
476is not expected to address directly.
477
478.. code:: c
479
480	  /* File main.c */
481	  #include <my_lib.h>
482
483	  int main(void)
484	  {
485		context_t	*context;
486		int		res;
487
488		context = my_lib_init();
489
490		res = my_lib_compute(context, "2x2");
491		if (res == -EMYLIB_ERROR) {
492			return -1
493		}
494
495		return res;
496	  }
497
498.. code:: c
499
500	  /* File my_lib.h */
501	  #ifndef MY_LIB_H
502	  #define MY_LIB_H
503
504	  typedef struct my_lib_context {
505	    [...] /* whatever internal private variables you need in my_lib */
506	  } context_t;
507
508	  #endif /* MY_LIB_H */
509
510Macros and Enums
511----------------
512
513- Favor functions over macros.
514
515- Preprocessor macros and enums values are written in all uppercase text.
516
517- A numerical value shall be typed.
518
519.. code:: c
520
521	/* Common C usage​ */
522	#define MY_MACRO 4UL
523
524	/* If used in C and ASM (included from a .S file)​ */
525	#define MY_MACRO UL(4)
526
527- Expressions resulting from the expansion of macro parameters must be enclosed
528  in parentheses.
529
530- A macro parameter immediately following a # operator mustn't be immediately
531  followed by a ## operator.
532
533.. code:: c
534
535   #define SINGLE_HASH_OP(x)		(#x)		/* allowed */
536   #define SINGLE_DOUBLE_HASH_OP(x, y)	(x ## y)	/* allowed */
537   #define MIXED_HASH_OP(x, y)		(#x ## y)	/* not allowed */
538
539- Avoid defining macros that affect the control flow (i.e. avoid using
540  return/goto in a macro).
541
542- Macro with multiple statements can be enclosed in a do-while block or in a
543  expression statement.
544
545.. code:: c
546
547	  int foo(char **b);
548
549	  #define M1(a, b)			\
550		  do {				\
551			if ((a) == 5) {		\
552				foo((b));	\
553			}			\
554		  } while (false)
555
556	  #define M2(a, b)		\
557		  ({			\
558		  if ((a) == 5) {	\
559			  foo((b));	\
560		  }			\
561		  })
562
563	  int foo(char **b)
564	  {
565		  return 42;
566	  }
567
568	  int main(int ac, char **av)
569	  {
570		  if (ac == 1) {
571			  M1(ac, av);
572		  } else if (ac == 2) {
573			  M2(ac, av);
574		  } else {
575			  return -1;
576		  }
577
578		  return ac;
579	  }
580
581Switch statements
582-----------------
583
584- Return in a *case* are allowed.
585
586- Fallthrough are allowed as long as they are commented.
587
588- Do not rely on type promotion between the switch type and the case type.
589
590Inline assembly
591---------------
592
593- Favor C language over assembly language.
594
595- Document all usage of assembly.
596
597- Do not mix C and ASM in the same file.
598
599Libc functions that are banned or to be used with caution
600---------------------------------------------------------
601
602Below is a list of functions that present security risks.
603
604+------------------------+--------------------------------------+
605|    libc function       | Comments                             |
606+========================+======================================+
607| ``strcpy, wcscpy``,    | use strlcpy instead                  |
608| ``strncpy``            |                                      |
609+------------------------+--------------------------------------+
610| ``strcat, wcscat``,    | use strlcat instead                  |
611| ``strncat``            |                                      |
612+------------------------+--------------------------------------+
613| ``sprintf, vsprintf``  | use snprintf, vsnprintf              |
614|                        | instead                              |
615+------------------------+--------------------------------------+
616| ``snprintf``           | if used, ensure result fits in buffer|
617|                        | i.e : snprintf(buf,size...) < size   |
618+------------------------+--------------------------------------+
619| ``vsnprintf``          | if used, inspect va_list match types |
620|                        | specified in format string           |
621+------------------------+--------------------------------------+
622| ``strtok, strtok_r``,  | Should not be used                   |
623| ``strsep``             |                                      |
624+------------------------+--------------------------------------+
625| ``ato*``               | Should not be used                   |
626+------------------------+--------------------------------------+
627| ``*toa``               | Should not be used                   |
628+------------------------+--------------------------------------+
629
630The use of above functions are discouraged and will only be allowed
631in justified cases after a discussion has been held either on the mailing
632list or during patch review and it is agreed that no alternative to their
633use is available. The code containing the banned APIs must properly justify
634their usage in the comments.
635
636The above restriction does not apply to Third Party IP code inside the ``ext/``
637directory.
638
639-----------
640
641.. _`Procedure Call Standard for the Arm 64-bit Architecture`: https://developer.arm.com/docs/ihi0055/latest/
642.. _`Linux kernel coding style`: https://www.kernel.org/doc/html/latest/process/coding-style.html
643.. _`MISRA C:2012 Guidelines`: https://www.misra.org.uk/Activities/MISRAC/tabid/160/Default.aspx
644.. _`TF-A coding style`: https://trustedfirmware-a.readthedocs.io/en/latest/process/coding-style.html
645.. _`C11 specification`: https://en.wikipedia.org/wiki/C11_(C_standard_revision)
646