The MISRA C Coding Standard: a Key Enabler for the Development of Safety- and Security-Critical Embedded Software

The MISRA C Coding Standard: a Key Enabler for the Development of Safety- and Security-Critical Embedded Software

The MISRA C Coding Standard: A Key Enabler for the Development of Safety- and Security-Critical Embedded Software Roberto Bagnara* Abramo Bagnara Patricia M. Hill BUGSENG and University of Parma BUGSENG BUGSENG Parma, Italy Parma, Italy Parma, Italy Email: [email protected] Email: [email protected] Email: [email protected] Abstract—Building embedded control systems that embody times even before testing and before using tools for worst- industry best practices for safety and security is a challenging case execution time approximation.2 task: doing so in unrestricted C is even more challenging. C is a • C allows writing compact code thanks to limited verbosity general-purpose programming language, partially defined by an ISO standard written in natural language with a slow evolution and the availability of many built-in operators. over the last 40+ years. Its many strong points make it the • C is defined by international standards: it was first most used language for the development of embedded systems. standardized in 1989 by the American National Stan- Unfortunately, the origin of C’s strength is also the origin of dards Institute (this version of the language is known as C’s weakness: the language has many aspects that are not fully ANSI C) and then by the International Organization for defined, it has some rather obscure aspects that can easily induce programmers into error, and it has no run-time error detection Standardization (ISO) [7], [8], [9], [10], [11], [12]. facilities. MISRA C is a coding standard defining a subset of the • C, possibly with extensions, allows easy access to the C language, initially targeted at the automotive sector, but now hardware and this is a must for the development of adopted across all industry sectors that develop C software in embedded software. safety- and/or security-critical contexts. In this talk, we introduce • C has a long history of usage in all kinds of systems MISRA C, its key role in the development of critical embedded systems’ software and its relevance to industry safety and security including safety-, security-, mission- and business-critical standards. We explain why and how MISRA C retains 95% of systems. the advantages of C and eradicates 95% of its drawbacks: with • C is widely supported by all sorts of tools. the right tools, training and professional expertise, the adoption Claims that C would eventually be superseded by C++ do not of MISRA C, besides satisfying some important requirements im- posed by safety standards, can significantly decrease development seem very plausible, at least as far as the embedded software times and costs. industry is concerned. In addition to the already-stated mo- tives, there is language size and stability: C++ has become a huge, very complex language; moreover it is evolving at a pace I. INTRODUCTION that is in sharp contrast with industrial best practices. The trend whereby C++ rapid evolution clashes with the industry The development of the C programming language started 50 requirements for stability and backward compatibility was put years ago in 1969 at Bell Labs where the language was used in black-and-white at a recent WG21 meeting,3 where the for the development of the Unix operating system [1]. Despite following statement was agreed upon [13]: “The Committee frequent criticism, C is still one of the most used programming should be willing to consider the design / quality of proposals languages overall1 and the most used one for the development even if they may cause a change in behavior or failure to of embedded systems [2], [3]. There are several reasons why compile for existing code.” C has been and is so successful: The characteristics that made the C programming language • C compilers exist for almost any processor, from tiny so successful have downsides: writing safe and secure appli- DSPs used in hearing aids to supercomputers. cations in C requires particular care. The solution mandated • C compiled code can be very efficient and without hidden or strongly suggested by all applicable industrial standards is costs, i.e., programmers can roughly predict running 2This is still true for implementations running on simple processors, with a limited degree of caching and internal parallelism. Prediction of maximum * While Roberto Bagnara is a member of the MISRA C Working Group and running time without tools becomes outright impossible for current multi- of ISO/IEC JTC1/SC22/WG14, a.k.a. the C Standardization Working Group, core designs such as Kalray MPPA, Freescale P4080, or ARM Cortex-A57 the views expressed in this paper are his and his coauthors’ and should not equivalents (see, e.g., [4], [5], [6]). be taken to represent the views of either working group. 3WG21 is a common shorthand for ISO/IEC JTC1/SC22/WG21, a.k.a. 1Source: TIOBE Index for June 2018, see https://www.tiobe.com/ the C++ Standardization Working Group. The cited meeting took place in tiobe-index/. Jacksonville, FL, USA, March 12–17, 2018. www.embedded-world.eu language subsetting. In this paper, we introduce MISRA C, can be written. In particular, C data types and operations can which is increasingly recognized as the most authoritative be directly mapped to data types and operations of the target subset of the C programming language in all industry sectors. machine. This is the reason why the sizes and precise repre- In doing so, we try to clear up some misconceptions around sentations of the standard integer types are implementation- the language, its standardization process and MISRA C itself. defined: the implementation will define them in the most The plan of the paper is the following: Section II intro- efficient way depending on properties of the target CPU duces the main shortcomings of C, explaining why C is (not registers, ALUs and memory hierarchy. completely) defined as it is, why it is not going to change Overflow on signed integer types is undefined behavior substantially any time soon, and why subsetting it is required; because the C Standard allows different representations of Section III introduces the MISRA project and MISRA C focus- signed integers, such as two’s complement, ones’ complement ing on its last edition, MISRA C:2012, with its amendments and sign-magnitude. For the latter two possibilities it is and addenda; Section IV presents a few MISRA C guidelines implementation-defined whether the negative zero bit pattern in order to convey the look and feel of the coding standard; is a trap representation.5 The C compiler can thus assume Section V emphasizes some points that are crucial for a proper signed integer overflow cannot happen, omit all checks for understanding of MISRA C; Section VI emphasizes some key overflow, and compile, e.g., factors for its successful adoption; Section VII concludes. II. C NON-DEFINITE BEHAVIOR int always_true(int v) { § ¤ The main source of problems with C comes from the notion return (v + 1 > v) ? 1 : -1; } of behavior, defined as external appearance or action [10, Par. 3.4] and the so-called as-if rule, whereby the compiler ¦ ¥ is allowed to do any transformation that ensures that the as if it was “observable behavior” of the program is the one described by the Standard [10, Par 5.1.2.3#5].4 While all compiled languages have a sort of as-if rule that allows optimized int always_true(int v) { compilation, one peculiarity of C is that it is not fully defined. § return 1; ¤ There are four classes of not fully defined behaviors (in the } sequel, collectively referred to as “non-definite behaviors”): implementation-defined behavior: unspecified behavior ¦ ¥ where each implementation documents how the choice Incidentally, implementation latitude on the representation of is made [10, Par. 3.4.1]; e.g., the sizes and precise signed integers is also the reason why all bitwise operation on representations of the standard integer types; signed integers have implementation-defined behavior. locale-specific behavior: behavior that depends on local con- Attempting to write on string literal constants is undefined ventions of nationality, culture, and language that each behavior because they may reside in read-only memory and/or implementation documents [10, Par. 3.4.2]; e.g., character may be merged and shared: for example, a program containing "String" "OtherString" sets and how characters are displayed; and may only store the latter undefined behavior: behavior, upon use of a non-portable and use a suffix of that representation to represent the former. or erroneous program construct or of erroneous data, The reason why shifting an expression by a negative number for which this International Standard imposes no require- or by an amount greater than or equal to the width of the ments [10, Par. 3.4.3]; e.g., attempting to write a string promoted expression is undefined behavior is less obvious. literal constant or shifting an expression by a negative What sensible semantics can be assigned to shifting by a number or by an amount greater than or equal to the negative number of bit positions? Shifting in the opposite width of the promoted expression; direction is a possible answer, but this is usually not supported unspecified behavior: use of an unspecified value, or other in hardware, so it would require a test, a jump and a negation. behavior where this International Standard provides two It is a bit more subtle to understand why the following is or more possibilities and imposes no further requirements undefined behavior: on which is chosen in any instance [10, Par. 3.4.4]; e.g., the order in which sub-expressions are evaluated. uint32_t i = 1; Setting aside locale-specific behavior, whose aim is to § i = i << 32; /* Undefined behavior.

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    11 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us