SlideShare ist ein Scribd-Unternehmen logo
1 von 44
JPL Coding Standard
for the C Programming Language
20141589 최광희
영화 Martian에서 마감에 고통 받던 이 분들입니다.
JPL
JPL(Jet Propulsion Laboratory)은
미국 항공우주국(NASA)의
무인 탐사 우주선 등의 연구 개발 및 운용에 종사하는 연구소로,
미국 캘리포니아 주의 로스앤젤레스 근처의 패서디나와 라카냐다플린트리지에 있
다.
캘리포니아 공과대학에 의해 운영되고 있으며,
미국 항공우주국의 우주선들을 만들고 보수한다.
The coding rules defined here primarily target
the development of mission critical flight software
written in the C programming language.
This means that the rules are focused on embedded software applications,
which generally operate under stricter resource constraints
than, e.g., ground software.
Why standardize?
Philosophy for C Programming Language
“High-level Assembly” or “Portable Assembly”
“프로그래머는 C 언어와 해당 기기에 대해서 모든 것을 알고
있다”
Morris Worm
The Morris worm or Internet worm of
November 2, 1988 was one of the first
computer worms distributed via the Internet.
This worm exploited standard C libraries,
such as strcpy, scanf, or gets.
The U.S. Government Accountability Office
put the cost of the damage at $100,000–
10,000,000.
The coding rules defined here primarily target
the development of mission critical flight software
written in the C programming language.
This means that the rules are focused on embedded software applications,
which generally operate under stricter resource constraints
than, e.g., ground software.
Why standardize?
Why standardize?
Failure is not an option.
JPL Standard
Two earlier efforts have most influenced the contents of this standard.
The first is the MISRA-C coding guideline from 2004, which was originally
defined for the development of embedded C code in automobiles, but is today
used broadly for safety critical applications.
The second source is the set of coding rules known as the “Power of Ten.”
Neither of these two sources, though, addresses software risks that are related to
the use of multi-threaded software. This standard aims to fill that void.
JPL Standard
Shall indicates a requirement that must be followed, with compliance verified.
Should indicates a preference that must be addressed,
but with deviations allowed,
provided that an adequate justification is given for each deviation.
Level of Compliance
LOC-1: Language Compliance
LOC-2: Predictable Execution
LOC-3: Defensive Coding
LOC-4: Code Clarity
LOC-5: MISRA-shall rules
LOC-6: MISRA-should rules
Level of Compliance
LOC-1: Language Compliance
LOC-2: Predictable Execution
LOC-3: Defensive Coding
LOC-4: Code Clarity
LOC-5: MISRA-shall rules
LOC-6: MISRA-should rules
LOC-1: Language Compliance
Rule 1 (language)
All C code shall conform to the ISO/IEC 9899-1999(E) standard for the C
programming language, with no reliance on undefined or unspecified behavior.
Rule 2 (routine checking)
All code shall always be compiled with all compiler warnings enabled at the
highest warning level available, with no errors or warnings resulting. All code shall
further be verified with a JPL approved state-of-the-art static source code analyzer,
with no errors or warnings resulting.
gcc compiler examples
gcc –Wall –pedantic –std=iso9899:1999 source.c
-Wtraditional
-Wshadow
-Wpointer-arith
-Wcast-qual
-Wcast-align
-Wstrict-prototypes
-Wmissing-prototypes
-Wconversion
Level of Compliance
LOC-1: Language Compliance
LOC-2: Predictable Execution
LOC-3: Defensive Coding
LOC-4: Code Clarity
LOC-5: MISRA-shall rules
LOC-6: MISRA-should rules
LOC-2: Predictable Execution
Rule 3 (loop bounds)
All loops shall have a statically determinable upper-bound on the maximum
number of loop iterations. It shall be possible for a static compliance checking
tool to affirm the existence of the bound. An exception is allowed for the use of a
single non-terminating loop per task or thread where requests are received and
processed. Such a server loop shall be annotated with the C comment:
/* @non-terminating@ */.
Rule 4 (recursion)
There shall be no direct or indirect use of recursive function calls.
Rule 5 (heap memory)
There shall be no use of dynamic memory allocation after task initialization.
Tip for secure loop bounds
Example. Linked List
Maximum number of iteration을 setting하면 됨
→ Assertion 가능
Recall
ACM 문제풀이 중에서 불가능한 것?
Dynamic Stack, Queue, Priority Queue, …
Recursion
Divide & Conquer
BFS, DFS
LOC-2: Predictable Execution
Rule 6 (inter-process communication)
An IPC mechanism should be used for all task communication. Callbacks
should be avoided. No task should directly execute code or access data that
belongs to a different task. All IPC messages shall be received at a single point
in a task.
Rule 7 (thread safety)
Task synchronization shall not be performed through the use of task delays.
LOC-2: Predictable Execution
Rule 8 (access to shared data)
Data objects in shared memory should have a single owning task. Only the
owner of a data object should be able to modify the object. Ownership should be
passed between tasks explicitly, preferably via IPC messages.
Rule 9 (semaphores and locking)
The use of semaphores or locks to access shared data should be avoided (cf.
Rules 6 and 8). If used, nested use of semaphores or locks should be avoided.
If such use is unavoidable, calls shall always occur in a single predetermined, and
documented, order. Unlock operations shall always appear within the body of the
same function that performs the matching lock operation.
LOC-2: Predictable Execution
Rule 10 (memory protection)
Where available, i.e., when supported by the operating system, memory
protection shall be used to the maximum extent possible. When not available,
safety margins and barrier patterns shall be used to allow detection of access
violations.
Tip for memory protection: Magic Numbers
From https://en.wikipedia.org/wiki/Hexspeak
0x0000000FF1CE
("office") is used as the last part of product codes for Microsoft Office components
0x8BADF00D
("ate bad food") is used by Apple in iOS crash reports, when an application takes
too long to launch, terminate, or respond to system events.
0xDEADBAAD
("dead bad") is used by the Android libc abort() function when native heap
corruption is detected.
LOC-2: Predictable Execution
Rule 11 (simple control flow)
The goto statement shall not be used. There shall be no calls to the functions
setjmp or longjmp.
Rule 12 (enum initialization)
In an enumerator list, the "=" construct shall not be used to explicitly initialize
members other than the first, unless all items are explicitly initialized.
Level of Compliance
LOC-1: Language Compliance
LOC-2: Predictable Execution
LOC-3: Defensive Coding
LOC-4: Code Clarity
LOC-5: MISRA-shall rules
LOC-6: MISRA-should rules
LOC-3: Defensive Coding
Rule 13 (limited scope)
Data objects shall be declared at the smallest possible level of scope. No
declaration in an inner scope shall hide a declaration in an outer scope.
Philosophy behind scoping
Principle of data-hiding
If an object is not in scope, its value cannot be referenced or corrupted.
Principle of preferring pure functions
Avoid storing local state
LOC-3: Defensive Coding
Rule 14 (checking return values)
The return value of non-void functions shall be checked or used by each calling
function, or explicitly cast to (void) if irrelevant.
Rule 15 (checking parameter values)
The validity of function parameters shall be checked at the start of each public
function. The validity of function parameters to other functions shall be checked
by either the function called or by the calling function.
Rule 16 (use of assertions)
Assertions shall be used to perform basic sanity checks throughout the code. All
functions of more than 10 lines should have at least one assertion.
Why assertions?
Statistics for industrial coding efforts indicate that unit tests often find at least
one defect per one hundred lines of code written. The odds of intercepting
defects increase with a liberal use of assertions.
Assertions can be used to verify pre- and post-conditions of functions,
parameter values, expected function return values, and loop-invariants.
Because assertions are side-effect free, they can be selectively disabled after
testing in performance-critical code.
Tips for assertions
// Typical assertion pattern
#define c_assert(e) ((e) ? (true) : 
tst_debugging("%s,%d: assertion '%s' failedn", 
__FILE__, __LINE__, #e), false)
// Real code
if (!c_assert(p >= 0) == true) { return ERROR; }
LOC-3: Defensive Coding
Rule 17 (types)
Typedefs that indicate size and signedness should be used in place of the basic
types.
Rule 18
In compound expressions with multiple sub-expressions the intended order of
evaluation shall be made explicit with parentheses.
Rule 19
The evaluation of a Boolean expression shall have no side effects.
Level of Compliance
LOC-1: Language Compliance
LOC-2: Predictable Execution
LOC-3: Defensive Coding
LOC-4: Code Clarity
LOC-5: MISRA-shall rules
LOC-6: MISRA-should rules
LOC-4: Code Clarity
Rule 20 (preprocessor use)
Use of the C preprocessor shall be limited to file inclusion and simple macros.
Rule 21 (preprocessor use)
Macros shall not be #define'd within a function or a block.
Rule 22 (preprocessor use)
#undef shall not be used.
Rule 23 (preprocessor use)
All #else, #elif and #endif preprocessor directives shall reside in the same file
as the #if or #ifdef directive to which they are related.
Tips for preprocessor use
Specifically, the use of token pasting, variable argument lists (ellipses), and
recursive macro calls are excluded by this rule.
All macros are required to expand into complete syntactic units.
The use of conditional compilation directives (#ifdef, #if, #elif) should be limited
to the standard boilerplate that avoids multiple inclusion of the same header file in
large projects. Each conditional compilation directives doubles the possible
versions of code.
LOC-4: Code Clarity
Rule 24
There should be no more than one statement or variable declaration per line.
A single exception is the C for-loop, where the three controlling expressions
(initialization, loop bound, and increment) can be placed on a single line.
Rule 25
Functions should be no longer than 60 lines of text
and define no more than 6 parameters.
LOC-4: Code Clarity
Rule 26
The declaration of an object should contain no more than two levels of indirection.
Rule 27
Statements should contain no more than two levels of dereferencing per object.
Rule 28
Pointer dereference operations should not be hidden in macro definitions or
inside typedef declarations.
Rule 29
Non-constant pointers to functions should not be used.
Rule 30 (type conversion)
Conversions shall not be performed between a pointer to a function and any type
other than an integral type.
Pointers are Dangerous!
Pointers are easily misused, even by experienced programmers.
They can make it hard to follow or analyze the flow of data in a program,
especially by tool-based checkers.
Function pointers especially can restrict the types of checks that can be
performed by static analyzers and should only be used if there is a strong
justification, and when alternate means are provided to maintain transparency of
the flow of control.
LOC-4: Code Clarity
Rule 31 (preprocessor use)
#include directives in a file shall only be preceded by
other preprocessor directives or comments.
Recommended file format
Include files
Macro definitions
Typedefs (Where not provided in system-wide include files)
External declarations
File static declarations
Function declarations
Level of Compliance
LOC-1: Language Compliance 2 Rules
LOC-2: Predictable Execution 10 Rules
LOC-3: Defensive Coding 7 Rules
LOC-4: Code Clarity 12 Rules
LOC-5: MISRA-shall rules 73 Rules
LOC-6: MISRA-should rules 16 Rules
Real-world Examples
2009–11 Toyota vehicle recalls
caused by sudden unintended acceleration
Why?
Stack Overflow
+ buffer overflow, unsafe casting, race conditions between tasks, …
Why?
The Camry ETCS code was found to have 11,000 global variables. Barr described the code as “spaghetti.”
Using the Cyclomatic Complexity metric, 67 functions were rated untestable (meaning they scored more
than 50). The throttle angle function scored more than 100 (unmaintainable).
Toyota loosely followed the widely adopted MISRA-C coding rules but Barr’s group found 80,000 rule
violations. Toyota's own internal standards make use of only 11 MISRA-C rules, and five of those were
violated in the actual code. MISRA-C:1998, in effect when the code was originally written, has 93 required
and 34 advisory rules. Toyota nailed six of them.
Barr also discovered inadequate and untracked peer code reviews and the absence of any bug-tracking
system at Toyota.
Reference.
http://www.edn.com/design/automotive/4423428/Toyota-s-killer-firmware--Bad-design-and-its-consequences
Michael Barr, CTO and co-founder of Barr Group, an embedded systems consulting firm
Why standardize?
Failure is not an option.

Weitere ähnliche Inhalte

Ähnlich wie Jpl coding standard for the c programming language

Linux randomnumbergenerator
Linux randomnumbergeneratorLinux randomnumbergenerator
Linux randomnumbergeneratorjhonce
 
Mitigating overflows using defense in-depth. What can your compiler do for you?
Mitigating overflows using defense in-depth. What can your compiler do for you?Mitigating overflows using defense in-depth. What can your compiler do for you?
Mitigating overflows using defense in-depth. What can your compiler do for you?Javier Tallón
 
Unit 2
Unit 2Unit 2
Unit 2siddr
 
Ceh v5 module 20 buffer overflow
Ceh v5 module 20 buffer overflowCeh v5 module 20 buffer overflow
Ceh v5 module 20 buffer overflowVi Tính Hoàng Nam
 
Quantstamp Report - LINKSWAP
Quantstamp Report - LINKSWAPQuantstamp Report - LINKSWAP
Quantstamp Report - LINKSWAPRoy Blackstone
 
Highly dependable automotive software
Highly dependable automotive softwareHighly dependable automotive software
Highly dependable automotive softwareAlan Tatourian
 
Unit 3 principles of programming language
Unit 3 principles of programming languageUnit 3 principles of programming language
Unit 3 principles of programming languageVasavi College of Engg
 
Report on hacking blind
Report on hacking blindReport on hacking blind
Report on hacking blindNikitaAndhale
 
Us 13-opi-evading-deep-inspection-for-fun-and-shell-wp
Us 13-opi-evading-deep-inspection-for-fun-and-shell-wpUs 13-opi-evading-deep-inspection-for-fun-and-shell-wp
Us 13-opi-evading-deep-inspection-for-fun-and-shell-wpOlli-Pekka Niemi
 
Parallelization of Coupled Cluster Code with OpenMP
Parallelization of Coupled Cluster Code with OpenMPParallelization of Coupled Cluster Code with OpenMP
Parallelization of Coupled Cluster Code with OpenMPAnil Bohare
 
ipc.pptx
ipc.pptxipc.pptx
ipc.pptxSuhanB
 
KARMA: Adaptive Android Kernel Live Patching
KARMA: Adaptive Android Kernel Live PatchingKARMA: Adaptive Android Kernel Live Patching
KARMA: Adaptive Android Kernel Live PatchingYue Chen
 
Advanced malwareanalysis training session2 botnet analysis part1
Advanced malwareanalysis training session2 botnet analysis part1Advanced malwareanalysis training session2 botnet analysis part1
Advanced malwareanalysis training session2 botnet analysis part1Cysinfo Cyber Security Community
 
ASIP (Application-specific instruction-set processor)
ASIP (Application-specific instruction-set processor)ASIP (Application-specific instruction-set processor)
ASIP (Application-specific instruction-set processor)Hamid Reza
 
17726 bypassing-phpids-0.6.5
17726 bypassing-phpids-0.6.517726 bypassing-phpids-0.6.5
17726 bypassing-phpids-0.6.5Attaporn Ninsuwan
 
Test versus security @ IEEE Concept
Test versus security @ IEEE ConceptTest versus security @ IEEE Concept
Test versus security @ IEEE Conceptkodela3
 

Ähnlich wie Jpl coding standard for the c programming language (20)

Parallel Lint
Parallel LintParallel Lint
Parallel Lint
 
Linux randomnumbergenerator
Linux randomnumbergeneratorLinux randomnumbergenerator
Linux randomnumbergenerator
 
6-Error Handling.pptx
6-Error Handling.pptx6-Error Handling.pptx
6-Error Handling.pptx
 
Mitigating overflows using defense in-depth. What can your compiler do for you?
Mitigating overflows using defense in-depth. What can your compiler do for you?Mitigating overflows using defense in-depth. What can your compiler do for you?
Mitigating overflows using defense in-depth. What can your compiler do for you?
 
Unit 2
Unit 2Unit 2
Unit 2
 
Ceh v5 module 20 buffer overflow
Ceh v5 module 20 buffer overflowCeh v5 module 20 buffer overflow
Ceh v5 module 20 buffer overflow
 
Quantstamp Report - LINKSWAP
Quantstamp Report - LINKSWAPQuantstamp Report - LINKSWAP
Quantstamp Report - LINKSWAP
 
Highly dependable automotive software
Highly dependable automotive softwareHighly dependable automotive software
Highly dependable automotive software
 
Unit 3 principles of programming language
Unit 3 principles of programming languageUnit 3 principles of programming language
Unit 3 principles of programming language
 
Report on hacking blind
Report on hacking blindReport on hacking blind
Report on hacking blind
 
Us 13-opi-evading-deep-inspection-for-fun-and-shell-wp
Us 13-opi-evading-deep-inspection-for-fun-and-shell-wpUs 13-opi-evading-deep-inspection-for-fun-and-shell-wp
Us 13-opi-evading-deep-inspection-for-fun-and-shell-wp
 
Parallelization of Coupled Cluster Code with OpenMP
Parallelization of Coupled Cluster Code with OpenMPParallelization of Coupled Cluster Code with OpenMP
Parallelization of Coupled Cluster Code with OpenMP
 
SOHIL_RM (1).pptx
SOHIL_RM (1).pptxSOHIL_RM (1).pptx
SOHIL_RM (1).pptx
 
ipc.pptx
ipc.pptxipc.pptx
ipc.pptx
 
KARMA: Adaptive Android Kernel Live Patching
KARMA: Adaptive Android Kernel Live PatchingKARMA: Adaptive Android Kernel Live Patching
KARMA: Adaptive Android Kernel Live Patching
 
Advanced malwareanalysis training session2 botnet analysis part1
Advanced malwareanalysis training session2 botnet analysis part1Advanced malwareanalysis training session2 botnet analysis part1
Advanced malwareanalysis training session2 botnet analysis part1
 
ASIP (Application-specific instruction-set processor)
ASIP (Application-specific instruction-set processor)ASIP (Application-specific instruction-set processor)
ASIP (Application-specific instruction-set processor)
 
17726 bypassing-phpids-0.6.5
17726 bypassing-phpids-0.6.517726 bypassing-phpids-0.6.5
17726 bypassing-phpids-0.6.5
 
Test versus security @ IEEE Concept
Test versus security @ IEEE ConceptTest versus security @ IEEE Concept
Test versus security @ IEEE Concept
 
Return address
Return addressReturn address
Return address
 

Mehr von Kwanghee Choi

Trends of ICASSP 2022
Trends of ICASSP 2022Trends of ICASSP 2022
Trends of ICASSP 2022Kwanghee Choi
 
추천 시스템 한 발짝 떨어져 살펴보기 (3)
추천 시스템 한 발짝 떨어져 살펴보기 (3)추천 시스템 한 발짝 떨어져 살펴보기 (3)
추천 시스템 한 발짝 떨어져 살펴보기 (3)Kwanghee Choi
 
Recommendation systems: Vertical and Horizontal Scrolls
Recommendation systems: Vertical and Horizontal ScrollsRecommendation systems: Vertical and Horizontal Scrolls
Recommendation systems: Vertical and Horizontal ScrollsKwanghee Choi
 
추천 시스템 한 발짝 떨어져 살펴보기 (1)
추천 시스템 한 발짝 떨어져 살펴보기 (1)추천 시스템 한 발짝 떨어져 살펴보기 (1)
추천 시스템 한 발짝 떨어져 살펴보기 (1)Kwanghee Choi
 
추천 시스템 한 발짝 떨어져 살펴보기 (2)
추천 시스템 한 발짝 떨어져 살펴보기 (2)추천 시스템 한 발짝 떨어져 살펴보기 (2)
추천 시스템 한 발짝 떨어져 살펴보기 (2)Kwanghee Choi
 
Before and After the AI Winter - Recap
Before and After the AI Winter - RecapBefore and After the AI Winter - Recap
Before and After the AI Winter - RecapKwanghee Choi
 
Mastering Gomoku - Recap
Mastering Gomoku - RecapMastering Gomoku - Recap
Mastering Gomoku - RecapKwanghee Choi
 
Teachings of Ada Lovelace
Teachings of Ada LovelaceTeachings of Ada Lovelace
Teachings of Ada LovelaceKwanghee Choi
 
div, grad, curl, and all that - a review
div, grad, curl, and all that - a reviewdiv, grad, curl, and all that - a review
div, grad, curl, and all that - a reviewKwanghee Choi
 
Neural Architecture Search: Learning How to Learn
Neural Architecture Search: Learning How to LearnNeural Architecture Search: Learning How to Learn
Neural Architecture Search: Learning How to LearnKwanghee Choi
 
Duality between OOP and RL
Duality between OOP and RLDuality between OOP and RL
Duality between OOP and RLKwanghee Choi
 
Bandit algorithms for website optimization - A summary
Bandit algorithms for website optimization - A summaryBandit algorithms for website optimization - A summary
Bandit algorithms for website optimization - A summaryKwanghee Choi
 
Dummy log generation using poisson sampling
 Dummy log generation using poisson sampling Dummy log generation using poisson sampling
Dummy log generation using poisson samplingKwanghee Choi
 
Azure functions: Quickstart
Azure functions: QuickstartAzure functions: Quickstart
Azure functions: QuickstartKwanghee Choi
 
Modern convolutional object detectors
Modern convolutional object detectorsModern convolutional object detectors
Modern convolutional object detectorsKwanghee Choi
 
Usage of Moving Average
Usage of Moving AverageUsage of Moving Average
Usage of Moving AverageKwanghee Choi
 

Mehr von Kwanghee Choi (19)

Visual Transformers
Visual TransformersVisual Transformers
Visual Transformers
 
Trends of ICASSP 2022
Trends of ICASSP 2022Trends of ICASSP 2022
Trends of ICASSP 2022
 
추천 시스템 한 발짝 떨어져 살펴보기 (3)
추천 시스템 한 발짝 떨어져 살펴보기 (3)추천 시스템 한 발짝 떨어져 살펴보기 (3)
추천 시스템 한 발짝 떨어져 살펴보기 (3)
 
Recommendation systems: Vertical and Horizontal Scrolls
Recommendation systems: Vertical and Horizontal ScrollsRecommendation systems: Vertical and Horizontal Scrolls
Recommendation systems: Vertical and Horizontal Scrolls
 
추천 시스템 한 발짝 떨어져 살펴보기 (1)
추천 시스템 한 발짝 떨어져 살펴보기 (1)추천 시스템 한 발짝 떨어져 살펴보기 (1)
추천 시스템 한 발짝 떨어져 살펴보기 (1)
 
추천 시스템 한 발짝 떨어져 살펴보기 (2)
추천 시스템 한 발짝 떨어져 살펴보기 (2)추천 시스템 한 발짝 떨어져 살펴보기 (2)
추천 시스템 한 발짝 떨어져 살펴보기 (2)
 
Before and After the AI Winter - Recap
Before and After the AI Winter - RecapBefore and After the AI Winter - Recap
Before and After the AI Winter - Recap
 
Mastering Gomoku - Recap
Mastering Gomoku - RecapMastering Gomoku - Recap
Mastering Gomoku - Recap
 
Teachings of Ada Lovelace
Teachings of Ada LovelaceTeachings of Ada Lovelace
Teachings of Ada Lovelace
 
div, grad, curl, and all that - a review
div, grad, curl, and all that - a reviewdiv, grad, curl, and all that - a review
div, grad, curl, and all that - a review
 
Gaussian processes
Gaussian processesGaussian processes
Gaussian processes
 
Neural Architecture Search: Learning How to Learn
Neural Architecture Search: Learning How to LearnNeural Architecture Search: Learning How to Learn
Neural Architecture Search: Learning How to Learn
 
Duality between OOP and RL
Duality between OOP and RLDuality between OOP and RL
Duality between OOP and RL
 
JFEF encoding
JFEF encodingJFEF encoding
JFEF encoding
 
Bandit algorithms for website optimization - A summary
Bandit algorithms for website optimization - A summaryBandit algorithms for website optimization - A summary
Bandit algorithms for website optimization - A summary
 
Dummy log generation using poisson sampling
 Dummy log generation using poisson sampling Dummy log generation using poisson sampling
Dummy log generation using poisson sampling
 
Azure functions: Quickstart
Azure functions: QuickstartAzure functions: Quickstart
Azure functions: Quickstart
 
Modern convolutional object detectors
Modern convolutional object detectorsModern convolutional object detectors
Modern convolutional object detectors
 
Usage of Moving Average
Usage of Moving AverageUsage of Moving Average
Usage of Moving Average
 

Kürzlich hochgeladen

chapter 5.pptx: drainage and irrigation engineering
chapter 5.pptx: drainage and irrigation engineeringchapter 5.pptx: drainage and irrigation engineering
chapter 5.pptx: drainage and irrigation engineeringmulugeta48
 
AKTU Computer Networks notes --- Unit 3.pdf
AKTU Computer Networks notes ---  Unit 3.pdfAKTU Computer Networks notes ---  Unit 3.pdf
AKTU Computer Networks notes --- Unit 3.pdfankushspencer015
 
Vivazz, Mieres Social Housing Design Spain
Vivazz, Mieres Social Housing Design SpainVivazz, Mieres Social Housing Design Spain
Vivazz, Mieres Social Housing Design Spaintimesproduction05
 
University management System project report..pdf
University management System project report..pdfUniversity management System project report..pdf
University management System project report..pdfKamal Acharya
 
UNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular ConduitsUNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular Conduitsrknatarajan
 
Call for Papers - International Journal of Intelligent Systems and Applicatio...
Call for Papers - International Journal of Intelligent Systems and Applicatio...Call for Papers - International Journal of Intelligent Systems and Applicatio...
Call for Papers - International Journal of Intelligent Systems and Applicatio...Christo Ananth
 
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordCCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordAsst.prof M.Gokilavani
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Christo Ananth
 
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Call Girls in Nagpur High Profile
 
PVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELL
PVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELLPVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELL
PVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELLManishPatel169454
 
Thermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptThermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptDineshKumar4165
 
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdfONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdfKamal Acharya
 
Double rodded leveling 1 pdf activity 01
Double rodded leveling 1 pdf activity 01Double rodded leveling 1 pdf activity 01
Double rodded leveling 1 pdf activity 01KreezheaRecto
 
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptxBSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptxfenichawla
 
Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...
Booking open Available Pune Call Girls Pargaon  6297143586 Call Hot Indian Gi...Booking open Available Pune Call Girls Pargaon  6297143586 Call Hot Indian Gi...
Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...Call Girls in Nagpur High Profile
 
UNIT-IFLUID PROPERTIES & FLOW CHARACTERISTICS
UNIT-IFLUID PROPERTIES & FLOW CHARACTERISTICSUNIT-IFLUID PROPERTIES & FLOW CHARACTERISTICS
UNIT-IFLUID PROPERTIES & FLOW CHARACTERISTICSrknatarajan
 

Kürzlich hochgeladen (20)

chapter 5.pptx: drainage and irrigation engineering
chapter 5.pptx: drainage and irrigation engineeringchapter 5.pptx: drainage and irrigation engineering
chapter 5.pptx: drainage and irrigation engineering
 
AKTU Computer Networks notes --- Unit 3.pdf
AKTU Computer Networks notes ---  Unit 3.pdfAKTU Computer Networks notes ---  Unit 3.pdf
AKTU Computer Networks notes --- Unit 3.pdf
 
Vivazz, Mieres Social Housing Design Spain
Vivazz, Mieres Social Housing Design SpainVivazz, Mieres Social Housing Design Spain
Vivazz, Mieres Social Housing Design Spain
 
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
 
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar ≼🔝 Delhi door step de...
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar  ≼🔝 Delhi door step de...Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar  ≼🔝 Delhi door step de...
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar ≼🔝 Delhi door step de...
 
University management System project report..pdf
University management System project report..pdfUniversity management System project report..pdf
University management System project report..pdf
 
UNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular ConduitsUNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular Conduits
 
Call for Papers - International Journal of Intelligent Systems and Applicatio...
Call for Papers - International Journal of Intelligent Systems and Applicatio...Call for Papers - International Journal of Intelligent Systems and Applicatio...
Call for Papers - International Journal of Intelligent Systems and Applicatio...
 
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort ServiceCall Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
 
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordCCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
 
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
 
PVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELL
PVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELLPVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELL
PVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELL
 
Thermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptThermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.ppt
 
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdfONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
 
Double rodded leveling 1 pdf activity 01
Double rodded leveling 1 pdf activity 01Double rodded leveling 1 pdf activity 01
Double rodded leveling 1 pdf activity 01
 
Water Industry Process Automation & Control Monthly - April 2024
Water Industry Process Automation & Control Monthly - April 2024Water Industry Process Automation & Control Monthly - April 2024
Water Industry Process Automation & Control Monthly - April 2024
 
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptxBSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
 
Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...
Booking open Available Pune Call Girls Pargaon  6297143586 Call Hot Indian Gi...Booking open Available Pune Call Girls Pargaon  6297143586 Call Hot Indian Gi...
Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...
 
UNIT-IFLUID PROPERTIES & FLOW CHARACTERISTICS
UNIT-IFLUID PROPERTIES & FLOW CHARACTERISTICSUNIT-IFLUID PROPERTIES & FLOW CHARACTERISTICS
UNIT-IFLUID PROPERTIES & FLOW CHARACTERISTICS
 

Jpl coding standard for the c programming language

  • 1. JPL Coding Standard for the C Programming Language 20141589 최광희
  • 2. 영화 Martian에서 마감에 고통 받던 이 분들입니다.
  • 3. JPL JPL(Jet Propulsion Laboratory)은 미국 항공우주국(NASA)의 무인 탐사 우주선 등의 연구 개발 및 운용에 종사하는 연구소로, 미국 캘리포니아 주의 로스앤젤레스 근처의 패서디나와 라카냐다플린트리지에 있 다. 캘리포니아 공과대학에 의해 운영되고 있으며, 미국 항공우주국의 우주선들을 만들고 보수한다.
  • 4. The coding rules defined here primarily target the development of mission critical flight software written in the C programming language. This means that the rules are focused on embedded software applications, which generally operate under stricter resource constraints than, e.g., ground software. Why standardize?
  • 5. Philosophy for C Programming Language “High-level Assembly” or “Portable Assembly” “프로그래머는 C 언어와 해당 기기에 대해서 모든 것을 알고 있다”
  • 6. Morris Worm The Morris worm or Internet worm of November 2, 1988 was one of the first computer worms distributed via the Internet. This worm exploited standard C libraries, such as strcpy, scanf, or gets. The U.S. Government Accountability Office put the cost of the damage at $100,000– 10,000,000.
  • 7. The coding rules defined here primarily target the development of mission critical flight software written in the C programming language. This means that the rules are focused on embedded software applications, which generally operate under stricter resource constraints than, e.g., ground software. Why standardize?
  • 9. JPL Standard Two earlier efforts have most influenced the contents of this standard. The first is the MISRA-C coding guideline from 2004, which was originally defined for the development of embedded C code in automobiles, but is today used broadly for safety critical applications. The second source is the set of coding rules known as the “Power of Ten.” Neither of these two sources, though, addresses software risks that are related to the use of multi-threaded software. This standard aims to fill that void.
  • 10. JPL Standard Shall indicates a requirement that must be followed, with compliance verified. Should indicates a preference that must be addressed, but with deviations allowed, provided that an adequate justification is given for each deviation.
  • 11. Level of Compliance LOC-1: Language Compliance LOC-2: Predictable Execution LOC-3: Defensive Coding LOC-4: Code Clarity LOC-5: MISRA-shall rules LOC-6: MISRA-should rules
  • 12. Level of Compliance LOC-1: Language Compliance LOC-2: Predictable Execution LOC-3: Defensive Coding LOC-4: Code Clarity LOC-5: MISRA-shall rules LOC-6: MISRA-should rules
  • 13. LOC-1: Language Compliance Rule 1 (language) All C code shall conform to the ISO/IEC 9899-1999(E) standard for the C programming language, with no reliance on undefined or unspecified behavior. Rule 2 (routine checking) All code shall always be compiled with all compiler warnings enabled at the highest warning level available, with no errors or warnings resulting. All code shall further be verified with a JPL approved state-of-the-art static source code analyzer, with no errors or warnings resulting.
  • 14.
  • 15. gcc compiler examples gcc –Wall –pedantic –std=iso9899:1999 source.c -Wtraditional -Wshadow -Wpointer-arith -Wcast-qual -Wcast-align -Wstrict-prototypes -Wmissing-prototypes -Wconversion
  • 16. Level of Compliance LOC-1: Language Compliance LOC-2: Predictable Execution LOC-3: Defensive Coding LOC-4: Code Clarity LOC-5: MISRA-shall rules LOC-6: MISRA-should rules
  • 17. LOC-2: Predictable Execution Rule 3 (loop bounds) All loops shall have a statically determinable upper-bound on the maximum number of loop iterations. It shall be possible for a static compliance checking tool to affirm the existence of the bound. An exception is allowed for the use of a single non-terminating loop per task or thread where requests are received and processed. Such a server loop shall be annotated with the C comment: /* @non-terminating@ */. Rule 4 (recursion) There shall be no direct or indirect use of recursive function calls. Rule 5 (heap memory) There shall be no use of dynamic memory allocation after task initialization.
  • 18. Tip for secure loop bounds Example. Linked List Maximum number of iteration을 setting하면 됨 → Assertion 가능
  • 19. Recall ACM 문제풀이 중에서 불가능한 것? Dynamic Stack, Queue, Priority Queue, … Recursion Divide & Conquer BFS, DFS
  • 20. LOC-2: Predictable Execution Rule 6 (inter-process communication) An IPC mechanism should be used for all task communication. Callbacks should be avoided. No task should directly execute code or access data that belongs to a different task. All IPC messages shall be received at a single point in a task. Rule 7 (thread safety) Task synchronization shall not be performed through the use of task delays.
  • 21. LOC-2: Predictable Execution Rule 8 (access to shared data) Data objects in shared memory should have a single owning task. Only the owner of a data object should be able to modify the object. Ownership should be passed between tasks explicitly, preferably via IPC messages. Rule 9 (semaphores and locking) The use of semaphores or locks to access shared data should be avoided (cf. Rules 6 and 8). If used, nested use of semaphores or locks should be avoided. If such use is unavoidable, calls shall always occur in a single predetermined, and documented, order. Unlock operations shall always appear within the body of the same function that performs the matching lock operation.
  • 22. LOC-2: Predictable Execution Rule 10 (memory protection) Where available, i.e., when supported by the operating system, memory protection shall be used to the maximum extent possible. When not available, safety margins and barrier patterns shall be used to allow detection of access violations.
  • 23. Tip for memory protection: Magic Numbers From https://en.wikipedia.org/wiki/Hexspeak 0x0000000FF1CE ("office") is used as the last part of product codes for Microsoft Office components 0x8BADF00D ("ate bad food") is used by Apple in iOS crash reports, when an application takes too long to launch, terminate, or respond to system events. 0xDEADBAAD ("dead bad") is used by the Android libc abort() function when native heap corruption is detected.
  • 24. LOC-2: Predictable Execution Rule 11 (simple control flow) The goto statement shall not be used. There shall be no calls to the functions setjmp or longjmp. Rule 12 (enum initialization) In an enumerator list, the "=" construct shall not be used to explicitly initialize members other than the first, unless all items are explicitly initialized.
  • 25. Level of Compliance LOC-1: Language Compliance LOC-2: Predictable Execution LOC-3: Defensive Coding LOC-4: Code Clarity LOC-5: MISRA-shall rules LOC-6: MISRA-should rules
  • 26. LOC-3: Defensive Coding Rule 13 (limited scope) Data objects shall be declared at the smallest possible level of scope. No declaration in an inner scope shall hide a declaration in an outer scope.
  • 27. Philosophy behind scoping Principle of data-hiding If an object is not in scope, its value cannot be referenced or corrupted. Principle of preferring pure functions Avoid storing local state
  • 28. LOC-3: Defensive Coding Rule 14 (checking return values) The return value of non-void functions shall be checked or used by each calling function, or explicitly cast to (void) if irrelevant. Rule 15 (checking parameter values) The validity of function parameters shall be checked at the start of each public function. The validity of function parameters to other functions shall be checked by either the function called or by the calling function. Rule 16 (use of assertions) Assertions shall be used to perform basic sanity checks throughout the code. All functions of more than 10 lines should have at least one assertion.
  • 29. Why assertions? Statistics for industrial coding efforts indicate that unit tests often find at least one defect per one hundred lines of code written. The odds of intercepting defects increase with a liberal use of assertions. Assertions can be used to verify pre- and post-conditions of functions, parameter values, expected function return values, and loop-invariants. Because assertions are side-effect free, they can be selectively disabled after testing in performance-critical code.
  • 30. Tips for assertions // Typical assertion pattern #define c_assert(e) ((e) ? (true) : tst_debugging("%s,%d: assertion '%s' failedn", __FILE__, __LINE__, #e), false) // Real code if (!c_assert(p >= 0) == true) { return ERROR; }
  • 31. LOC-3: Defensive Coding Rule 17 (types) Typedefs that indicate size and signedness should be used in place of the basic types. Rule 18 In compound expressions with multiple sub-expressions the intended order of evaluation shall be made explicit with parentheses. Rule 19 The evaluation of a Boolean expression shall have no side effects.
  • 32. Level of Compliance LOC-1: Language Compliance LOC-2: Predictable Execution LOC-3: Defensive Coding LOC-4: Code Clarity LOC-5: MISRA-shall rules LOC-6: MISRA-should rules
  • 33. LOC-4: Code Clarity Rule 20 (preprocessor use) Use of the C preprocessor shall be limited to file inclusion and simple macros. Rule 21 (preprocessor use) Macros shall not be #define'd within a function or a block. Rule 22 (preprocessor use) #undef shall not be used. Rule 23 (preprocessor use) All #else, #elif and #endif preprocessor directives shall reside in the same file as the #if or #ifdef directive to which they are related.
  • 34. Tips for preprocessor use Specifically, the use of token pasting, variable argument lists (ellipses), and recursive macro calls are excluded by this rule. All macros are required to expand into complete syntactic units. The use of conditional compilation directives (#ifdef, #if, #elif) should be limited to the standard boilerplate that avoids multiple inclusion of the same header file in large projects. Each conditional compilation directives doubles the possible versions of code.
  • 35. LOC-4: Code Clarity Rule 24 There should be no more than one statement or variable declaration per line. A single exception is the C for-loop, where the three controlling expressions (initialization, loop bound, and increment) can be placed on a single line. Rule 25 Functions should be no longer than 60 lines of text and define no more than 6 parameters.
  • 36. LOC-4: Code Clarity Rule 26 The declaration of an object should contain no more than two levels of indirection. Rule 27 Statements should contain no more than two levels of dereferencing per object. Rule 28 Pointer dereference operations should not be hidden in macro definitions or inside typedef declarations. Rule 29 Non-constant pointers to functions should not be used. Rule 30 (type conversion) Conversions shall not be performed between a pointer to a function and any type other than an integral type.
  • 37. Pointers are Dangerous! Pointers are easily misused, even by experienced programmers. They can make it hard to follow or analyze the flow of data in a program, especially by tool-based checkers. Function pointers especially can restrict the types of checks that can be performed by static analyzers and should only be used if there is a strong justification, and when alternate means are provided to maintain transparency of the flow of control.
  • 38. LOC-4: Code Clarity Rule 31 (preprocessor use) #include directives in a file shall only be preceded by other preprocessor directives or comments.
  • 39. Recommended file format Include files Macro definitions Typedefs (Where not provided in system-wide include files) External declarations File static declarations Function declarations
  • 40. Level of Compliance LOC-1: Language Compliance 2 Rules LOC-2: Predictable Execution 10 Rules LOC-3: Defensive Coding 7 Rules LOC-4: Code Clarity 12 Rules LOC-5: MISRA-shall rules 73 Rules LOC-6: MISRA-should rules 16 Rules
  • 41. Real-world Examples 2009–11 Toyota vehicle recalls caused by sudden unintended acceleration
  • 42. Why? Stack Overflow + buffer overflow, unsafe casting, race conditions between tasks, …
  • 43. Why? The Camry ETCS code was found to have 11,000 global variables. Barr described the code as “spaghetti.” Using the Cyclomatic Complexity metric, 67 functions were rated untestable (meaning they scored more than 50). The throttle angle function scored more than 100 (unmaintainable). Toyota loosely followed the widely adopted MISRA-C coding rules but Barr’s group found 80,000 rule violations. Toyota's own internal standards make use of only 11 MISRA-C rules, and five of those were violated in the actual code. MISRA-C:1998, in effect when the code was originally written, has 93 required and 34 advisory rules. Toyota nailed six of them. Barr also discovered inadequate and untracked peer code reviews and the absence of any bug-tracking system at Toyota. Reference. http://www.edn.com/design/automotive/4423428/Toyota-s-killer-firmware--Bad-design-and-its-consequences Michael Barr, CTO and co-founder of Barr Group, an embedded systems consulting firm
  • 44. Why standardize? Failure is not an option.