Pitfalls in Embedded Software



Similar documents
Embedded Systems. Review of ANSI C Topics. A Review of ANSI C and Considerations for Embedded C Programming. Basic features of C

Boolean Expressions, Conditions, Loops, and Enumerations. Precedence Rules (from highest to lowest priority)

Building Embedded Systems

SYSTEM ecos Embedded Configurable Operating System

Storage Classes CS 110B - Rule Storage Classes Page 18-1 \handouts\storclas

Comp151. Definitions & Declarations

An Incomplete C++ Primer. University of Wyoming MA 5310

Member Functions of the istream Class

Linux Driver Devices. Why, When, Which, How?

SSC - Concurrency and Multi-threading Java multithreading programming - Synchronisation (I)

No no-argument constructor. No default constructor found

PROBLEM SOLVING SEVENTH EDITION WALTER SAVITCH UNIVERSITY OF CALIFORNIA, SAN DIEGO CONTRIBUTOR KENRICK MOCK UNIVERSITY OF ALASKA, ANCHORAGE PEARSON

Jonathan Worthington Scarborough Linux User Group

Debugging Multi-threaded Applications in Windows

Sources: On the Web: Slides will be available on:

How Safe does my Code Need to be? Shawn A. Prestridge, Senior Field Applications Engineer

the high-performance embedded kernel User Guide Version 5.0 Express Logic, Inc Toll Free 888.THREADX FAX

Lab 2: Swat ATM (Machine (Machine))

Virtual Servers. Virtual machines. Virtualization. Design of IBM s VM. Virtual machine systems can give everyone the OS (and hardware) that they want.

Microcontroller Systems. ELET 3232 Topic 8: Slot Machine Example

Get the Better of Memory Leaks with Valgrind Whitepaper

Facing the Challenges for Real-Time Software Development on Multi-Cores

Device Driver Best Practices in Windows Embedded Compact 7. Douglas Boling Boling Consulting Inc.

Keil C51 Cross Compiler

Memory Allocation. Static Allocation. Dynamic Allocation. Memory Management. Dynamic Allocation. Dynamic Storage Allocation

Embedded C Programming, Linux, and Vxworks. Synopsis

Chapter 6, The Operating System Machine Level

MQX Lite Real-Time Operating System User Guide

Operating System Manual. Realtime Communication System for netx. Kernel API Function Reference.

Coding conventions and C++-style

How to design and implement firmware for embedded systems

How To Port A Program To Dynamic C (C) (C-Based) (Program) (For A Non Portable Program) (Un Portable) (Permanent) (Non Portable) C-Based (Programs) (Powerpoint)

Using the RDTSC Instruction for Performance Monitoring

Real-Time Systems Prof. Dr. Rajib Mall Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

Object Oriented Software Design II

Introduction to Embedded Systems. Software Update Problem

XMOS Programming Guide

Informatica e Sistemi in Tempo Reale

CSCI E 98: Managed Environments for the Execution of Programs

umps software development

How To Write A Multi Threaded Software On A Single Core (Or Multi Threaded) System

Software Development to Control the Scorbot ER VII Robot With a PC

Curriculum Map. Discipline: Computer Science Course: C++

Embedded Programming in C/C++: Lesson-1: Programming Elements and Programming in C

Eliminate Memory Errors and Improve Program Stability

Basics of I/O Streams and File I/O

Simple Cooperative Scheduler for Arduino ARM & AVR. Aka «SCoop»

A C Test: The 0x10 Best Questions for Would-be Embedded Programmers

Bypassing Browser Memory Protections in Windows Vista

Java Virtual Machine Locks

A Comparison Of Shared Memory Parallel Programming Models. Jace A Mogill David Haglin

The C Programming Language course syllabus associate level

Memory management. Announcements. Safe user input. Function pointers. Uses of function pointers. Function pointer example

Real World Software Assurance Test Suite: STONESOUP

Top 10 Bug-Killing Coding Standard Rules

Name: Class: Date: 9. The compiler ignores all comments they are there strictly for the convenience of anyone reading the program.

Real-Time Component Software. slide credits: H. Kopetz, P. Puschner

CpSc212 Goddard Notes Chapter 6. Yet More on Classes. We discuss the problems of comparing, copying, passing, outputting, and destructing

Software Engineering for LabVIEW Applications. Elijah Kerry LabVIEW Product Manager

Parallel Computing. Shared memory parallel programming with OpenMP

Free Java textbook available online. Introduction to the Java programming language. Compilation. A simple java program

Summary. I. V. Arzamartsev, G. I. Borzunov A Method of Analysis of Multithreaded Applications Based on Symbolic Execution

C Compiler Targeting the Java Virtual Machine

Free Java textbook available online. Introduction to the Java programming language. Compilation. A simple java program

USING THE FREERTOS REAL TIME KERNEL

Overview Motivating Examples Interleaving Model Semantics of Correctness Testing, Debugging, and Verification

sqlite driver manual

Software Development Tools for Embedded Systems. Hesen Zhang

The University of Alabama in Huntsville Electrical and Computer Engineering CPE Test #4 November 20, True or False (2 points each)

An Analysis of Address Space Layout Randomization on Windows Vista

CISC 181 Project 3 Designing Classes for Bank Accounts

Monday, April 8, 13. Creating Successful Magento ERP Integrations

The programming language C. sws1 1

So far we have considered only numeric processing, i.e. processing of numeric data represented

Source Code Security Analysis Tool Functional Specification Version 1.0

Kernel Synchronization and Interrupt Handling

Polymorphism. Problems with switch statement. Solution - use virtual functions (polymorphism) Polymorphism

C++FA 3.1 OPTIMIZING C++

I Control Your Code Attack Vectors Through the Eyes of Software-based Fault Isolation. Mathias Payer, ETH Zurich

MPLAB Harmony System Service Libraries Help

Data Structures using OOP C++ Lecture 1

快 速 porting μc/os-ii 及 driver 解 說

EE8205: Embedded Computer System Electrical and Computer Engineering, Ryerson University. Multitasking ARM-Applications with uvision and RTX

Systemnahe Programmierung KU

Mimer SQL Real-Time Edition White Paper

Simple C++ Programs. Engineering Problem Solving with C++, Etter/Ingber. Dev-C++ Dev-C++ Windows Friendly Exit. The C++ Programming Language

Tasks Schedule Analysis in RTAI/Linux-GPL

Chapter 2: OS Overview

Illustration 1: Diagram of program function and data flow

Transcription:

Pitfalls in Embedded Software..and how to avoid them Sagar Behere 31 March 2014 Pitfalls in Embedded Software 1 / 19

What is wrong with this code? unsigned int count = BigValue; for (int i = 0; i < count; i++) { ; } Pitfalls in Embedded Software 2 / 19

Who am I? 10+ years of systems development Diesel engines, traction control, autonomous driving Robotics, articial intelligence, unmanned aerial vehicles Pitfalls in Embedded Software 3 / 19

What will I talk about? 1 Inconsistent bugs 2 How to debug 3 Take-home puzzles Pitfalls in Embedded Software 4 / 19

Race conditions Thread 1: Thread 2: global_counter += 1; global_counter = 0; What if the increment operation cannot be performed atomically? Best practices: Surrounded critical sections by preemption limiting mechanisms For ISRs: Interrupt must be disabled For RTOS Tasks: Mutexes Tip Look up Scoped Mutexes Pitfalls in Embedded Software 5 / 19

Non-reentrant functions ETH driver functions MUST manipulate the same global objects! Best practice: Use Mutexes But is that sucient? Pitfalls in Embedded Software 6 / 19

Missing volatile keyword g_alarm = ALARM_ON; // // Code that does not access g_alarm // g_alarm = ALARM_OFF; What happens when compiled with optimization enabled? Best practices: Use volatile to declare every Global variable accessed by an ISR Global variable accessed by two or more RTOS tasks Pointers to memory-mapped registers Delay loop counters Pitfalls in Embedded Software 7 / 19

Stack overow Eects and timing both unpredictable Embedded systems are especially vulnerable Best practice: Limited RAM. No virtual memory RTOS based designs typically have one-stack-per-thread; each must be correctly sized Interrupt handlers may try to use those 1 During init, paint an unlikely memory pattern throughout the stack. 2 During runtime, supervisor task periodically checks for 'scratches in the paint' above a 'high water mark' Pitfalls in Embedded Software 8 / 19

Heap fragmentation 1 Start with a 10KB heap 2 malloc() two blocks of 4 KB 3 free() one of the blocks 4 malloc() a block of 6 KB What will happen? Best practice: All memory requests should have the same size. Pitfalls in Embedded Software 9 / 19

Memory leaks Number of malloc()s does not match number of free()s int *x; x = malloc(sizeof(int)); *x = 100; Why is the above code dangerous? Best practice: Design patterns: Clearly dene ownership pattern or lifetime of each type of heap-allocated object. Valgrind!!! Pitfalls in Embedded Software 10 / 19

Deadlocks Best practices: Do not attempt simultaneous acquisition of two or more mutexes Assign an ordering to all mutexes. Always acquire multiple mutexes in that same order Pitfalls in Embedded Software 11 / 19

Priority inversion Not always reproducible Best practice: 1 Choose RTOS that has priority-inversion work-arounds in its API 2 Do not forget execution time cost of work-around Pitfalls in Embedded Software 12 / 19

Jitter Best practice: Set correct relative priorities.. or cheat! Pitfalls in Embedded Software 13 / 19

A scientic approach to debugging 1 Verify the bug, determine correct behavior 2 Stabilize, isolate, minimize 2 Can you make the bug appear consistently? 2 What is the minimum input needed to make it appear? 3 Estimate a probability distribution Pitfalls in Embedded Software 14 / 19

A scientic approach to debugging 4 Devise and run an experiment 5 Iterate - but remember to change one thing at a time 6 Fix bug, verify x 7 Undo changes 8 Find the bug's parents, friends and relatives Pitfalls in Embedded Software 15 / 19

If you are stuck What if the probability distribution looks like this? Take a break Talk with someone Sit and stare at the code Reduce size of failure-inducing input Find a tool to bring out more information Pitfalls in Embedded Software 16 / 19

Take home puzzle #1 1 #include <iostream> 2 #include <string> 3 using namespace std; 4 int main(void) 5 { 6 string s = "abc"; 7 char delim = ':'; 8 unsigned int position = s.nd(delim); 9 // if no matches are found, nd() returns string::npos, else position of delim 10 // see http://www.cplusplus.com/reference/string/string/nd/ 11 if(string::npos!= position) { 12 cout << delim << " FOUND in " << s << endl; 13 } else { 14 cout << delim << " NOT FOUND in " << s << endl; 15 } 16 return 0; 17 } Pitfalls in Embedded Software 17 / 19

Take home puzzle #2 #include <stdio.h> main(t,_,a) char a; {return!0<t?t<3?main( 79, 13,a+main( 87,1 _, main( 86, 0, a+1 )+a)):1,t<_?main(t+1, _, a ):3,main ( 94, 27+t, a )&&t == 2?_<13?main ( 2, _+1, "%s %d %d\n" ):9:16:t<0?t< 72?main(_, t,"@n'+,#'/ {}w+/w#cdnr/+,{}r/ de}+,/ { +,/w{%+,/w#q#n+,/#{l,+,/n{n+\,/+#n+,/#;#q#n+,/+k#; +,/'r :'d '3,}{w+K w'k:'+}e#';dq#'l q#'+d'k#!/\ +k#;q#'r}ekk#}w'r}ekk{nl]'/#;#q#n'){)#}w'){){nl]'/+#n';d}rw' i;# ){n\ l]!/n{n#'; r{#w'r nc{nl]'/#{l,+'k {rw' ik{;[{nl]'/w#q#\ n'wk nw' iwk{kk{nl]!/w{%'l##w#' i; :{nl]'/ {q#'ld;r'}{nlwb!/ de}'c \ ;;{nl' {}rw]'/+,}##' }#nc,',#nw]'/+kd'+e}+;\ #'rdq#w! nr'/ ') }+}{rl#'{n' ')# }'+}##(!!/") :t< 50?_== a?putchar(a[31]):main( 65,_,a+1):main(( a == '/')+t,_,a\ +1 ):0<t?main ( 2, 2, "%s"): a=='/' main(0,main( 61, a, "!ek;dc \ i@bk'(q) [w] %n+r3#l,{}:\nuwloca O;m.vpbks,fxntdCeghiry"),a+1);} Pitfalls in Embedded Software 18 / 19

References & further reading Material in these slides has been taken from.. Embedded.com: Five top causes of nasty embedded software bugs Embedded.com: Five more top causes of nasty embedded software bugs John Regehr's blog post: How to debug?..you really should read those articles! Pitfalls in Embedded Software 19 / 19