Static Analysis of Embedded C John Regehr University of Utah Joint work with Nathan Cooprider.

Slides:



Advertisements
Similar presentations
Dataflow Analysis for Datarace-Free Programs (ESOP 11) Arnab De Joint work with Deepak DSouza and Rupesh Nasre Indian Institute of Science, Bangalore.
Advertisements

P3 / 2004 Register Allocation. Kostis Sagonas 2 Spring 2004 Outline What is register allocation Webs Interference Graphs Graph coloring Spilling Live-Range.
Context-Sensitive Interprocedural Points-to Analysis in the Presence of Function Pointers Presentation by Patrick Kaleem Justin.
Architecture-dependent optimizations Functional units, delay slots and dependency analysis.
Ch. 7 Process Synchronization (1/2) I Background F Producer - Consumer process :  Compiler, Assembler, Loader, · · · · · · F Bounded buffer.
Extensibility, Safety and Performance in the SPIN Operating System Presented by Allen Kerr.
Program Representations. Representing programs Goals.
Sensor Network Platforms and Tools
Overview: Chapter 7  Sensor node platforms must contend with many issues  Energy consumption  Sensing environment  Networking  Real-time constraints.
Eliminating Stack Overflow by Abstract Interpretation John Regehr Alastair Reid Kirk Webb University of Utah.
Static Analysis of Embedded C Code John Regehr University of Utah Joint work with Nathan Cooprider.
Vertically Integrated Analysis and Transformation for Embedded Software John Regehr University of Utah.
Contiki A Lightweight and Flexible Operating System for Tiny Networked Sensors Presented by: Jeremy Schiff.
Systems Wireless EmBedded nesC Update Eric Brewer with help from David Culler, David Gay, Phil Levis, Rob von Behren, and Matt Welsh.
1 Efficient Memory Safety for TinyOS Nathan Cooprider Will Archer Eric Eide David Gay † John Regehr University of Utah School of Computing † Intel Research.
CSCI 8150 Advanced Computer Architecture Hwang, Chapter 2 Program and Network Properties 2.3 Program Flow Mechanisms.
Next Section: Pointer Analysis Outline: –What is pointer analysis –Intraprocedural pointer analysis –Interprocedural pointer analysis (Wilson & Lam) –Unification.
1 Intermediate representation Goals: –encode knowledge about the program –facilitate analysis –facilitate retargeting –facilitate optimization scanning.
Random Testing of Interrupt-Driven Software John Regehr University of Utah.
Lock Inference for Systems Software John Regehr Alastair Reid University of Utah March 17, 2003.
Generic Sensor Platform for Networked Sensors Haywood Ho.
Active Messages: a Mechanism for Integrated Communication and Computation von Eicken et. al. Brian Kazian CS258 Spring 2008.
Data-flow Analysis for Interrupt- driven Microcontroller Software Nathan Cooprider Advisor: John Regehr Dissertation defense School of Computing University.
1 Efficient Memory Safety for TinyOS 2.1 Yang Chen Nathan Cooprider Will Archer Eric Eide David Gay † John Regehr University of Utah School of Computing.
TinyOS Software Engineering Sensor Networks for the Masses.
State Machines Timing Computer Bus Computer Performance Instruction Set Architectures RISC / CISC Machines.
A Static Analysis Framework For Embedded Systems Nathan Cooprider John Regehr's Embedded Systems Group.
Direction of analysis Although constraints are not directional, flow functions are All flow functions we have seen so far are in the forward direction.
Topic ? Course Overview. Guidelines Questions are rated by stars –One Star Question  Easy. Small definition, examples or generic formulas –Two Stars.
Composing Dataflow Analyses and Transformations Sorin Lerner (University of Washington) David Grove (IBM T.J. Watson) Craig Chambers (University of Washington)
Evolving Real-Time Systems using Hierarchical Scheduling and Concurrency Analysis John Regehr Alastair Reid Kirk Webb Michael Parker Jay Lepreau School.
Copyright Arshi Khan1 System Programming Instructor Arshi Khan.
Precision Going back to constant prop, in what cases would we lose precision?
1 Software Development Infrastructure for Sensor Networks  Operating systems ( TinyOS )  Resource (device) management  Basic primitives  Protocols.
Computer Organization
Secure Virtual Architecture John Criswell, Arushi Aggarwal, Andrew Lenharth, Dinakar Dhurjati, and Vikram Adve University of Illinois at Urbana-Champaign.
By: R Jayampathi Sampath
April 15, 2005TinyOS: A Component Based OSPage 1 of 27 TinyOS A Component-Based Operating System for Networked Embedded Systems Tom Bush Graduate College.
Parallel Programming Models Jihad El-Sana These slides are based on the book: Introduction to Parallel Computing, Blaise Barney, Lawrence Livermore National.
Eric Keller, Evan Green Princeton University PRESTO /22/08 Virtualizing the Data Plane Through Source Code Merging.
©Ian Sommerville 2000 Software Engineering, 6th edition. Chapter 10Slide 1 Architectural Design l Establishing the overall structure of a software system.
1 Efficient Type and Memory Safety for Tiny Embedded Systems John Regehr Nathan Cooprider Will Archer Eric Eide University of Utah School of Computing.
System Architecture Directions for Networked Sensors Jason Hill, Robert Szewczyk, Alec Woo, Seth Hollar, David Culler, Kris Pister Presented by Yang Zhao.
Simon Han – Ram Kumar Rengaswamy – Roy Shea – Mani Srivastava – Eddie Kohler –
Hardware-software Interface Xiaofeng Fan
Processes Introduction to Operating Systems: Module 3.
Xiong Junjie Node-level debugging based on finite state machine in wireless sensor networks.
CSE 153 Design of Operating Systems Winter 2015 Midterm Review.
System Architecture Directions for Networked Sensors.
R-Verify: Deep Checking of Embedded Code James Ezick † Donald Nguyen † Richard Lethin † Rick Pancoast* (†) Reservoir Labs (*) Lockheed Martin The Eleventh.
Specifying Multithreaded Java semantics for Program Verification Abhik Roychoudhury National University of Singapore (Joint work with Tulika Mitra)
1 Why Threads are a Bad Idea (for most purposes) based on a presentation by John Ousterhout Sun Microsystems Laboratories Threads!
1PLDI 2000 Off-line Variable Substitution for Scaling Points-to Analysis Atanas (Nasko) Rountev PROLANGS Group Rutgers University Satish Chandra Bell Labs.
Slide 1 Chapter 8 Architectural Design. Slide 2 Topics covered l System structuring l Control models l Modular decomposition l Domain-specific architectures.
Software Architecture of Sensors. Hardware - Sensor Nodes Sensing: sensor --a transducer that converts a physical, chemical, or biological parameter into.
The Structuring of Systems Using Upcalls By David D. Clark Presented by Samuel Moffatt.
TinyOS and nesC. Outline ● Wireless sensor networks and TinyOS ● Networked embedded system C (nesC) – Components – Interfaces – Concurrency model – Tool.
Nathan Cooprider and John Regehr University of Utah School of Computing Pluggable Abstract Domains for Analyzing Embedded Software.
Introduction to Operating Systems Concepts
INTRODUCTION TO WIRELESS SENSOR NETWORKS
Code Optimization.
Effective Data-Race Detection for the Kernel
TinyOS CSE466 Final Project Presentation
Top Half / Bottom Half Processing
CSE 451: Operating Systems Autumn 2003 Lecture 2 Architectural Support for Operating Systems Hank Levy 596 Allen Center 1.
CSE 451: Operating Systems Winter 2003 Lecture 2 Architectural Support for Operating Systems Hank Levy 412 Sieg Hall 1.
Foundations and Definitions
Modeling Event-Based Systems in Ptolemy II EE249 Project Status Report
Pointer analysis John Rollinson & Kaiyuan Li
Prof. Onur Mutlu Carnegie Mellon University
Presentation transcript:

Static Analysis of Embedded C John Regehr University of Utah Joint work with Nathan Cooprider

2 Motivating Platform: TinyOS Embedded software for wireless sensor network nodes Has lots of SW components for MAC, routing, collection, data acq., etc. Runs on highly constrained MCUs Goal of TinyOS is to get MCU into lowest possible power state as soon as possible

3 TinyOS Execution Model Application = interrupts + tasks + scheduler –Interrupt handlers natively supported by the HW platform –Tasks are deferred function calls No blocking – tasks are not threads Scheduled FIFO TinyOS scheduler is about 100 lines of code –Concurrency control by disabling interrupts

4 TinyOS Constraints Energy –2 AA batteries == ~4 Watt-hours SRAM for data storage –0.5 KB – 10 KB –SRAM can dominate power consumed by sleeping MCU Flash for code and read-only data –4 KB – 256 KB

5 nesC – the TinyOS language C dialect with –Static component model –Race condition detection –Generics nesC compiler –Input: Collection of nesC components –Output: Monolithic C program We analyze compiler output

6 Thesis 1.Embedded codes contain significant structure not exploited by compilers and other existing analyzers 2.Static analysis based on a language model and a system model can uncover and exploit this structure 3.Analysis results are useful

7 Using Analysis Results To support program transformations –Optimizing Type-Safe TinyOS (SenSys 2007) Reduce CPU overhead from 24% to 5% Reduce code size overhead from 35% to 13% –Offline RAM compression (PLDI 2007) Reduce SRAM usage of TinyOS applications by 23% To cheaply discover program facts supporting verification?

8 What’s different about this work vs. what you see at PLDI and ICSE? It’s easier… –Scalability not so important –Simple locking model –We know the platform It’s harder… –Unstructured interrupt-driven concurrency –Direct hardware access –We know the platform

9 Analysis Overview Flow sensitive Path and context insensitive Scalars analyzed using value sets –Bounded-size sets of concrete values Pointers analyzed using pointer sets –Bounded-size sets of pointers + specific support for non-null –Supports both must and may alias analysis

10 Analysis Overview 2 Precede analysis with function inlining pass –Compensates somewhat for context insensitivity Arrays summarized as single abstract value

11 What do you get by applying an analyzer for sequential C code to TinyOS applications? –Can analyze local variables Assumption: No pointers across stacks –Cannot generally analyze globals Many are touched by both main() and interrupts

12 Analyzing Global Variables “Synchronous” global state == Not used for communication between concurrent flows –Sequential semantics apply Problem: –Conservative identification of synchronous variables requires pointer analysis –Many global variables are pointers –Circular dependency between analyses

13 Finding Synchronous State Solution: Integrate 1.Pointer analysis 2.Synchronous / asynchronous classification Next question: How to analyze variables that are not synchronous?

14 Analyzing Asynchronous State 1 Strawman algorithm: 1.Find program points where interrupts cannot be shown to be disabled 2.Add a flow edge from each such point to the start of each interrupt handler (and back) This is overkill: Too many extra flow edges  long analysis time This is unsound: C statements are not atomic operations

15 Analyzing Asynchronous State 2 Better algorithm: 1.Find blocks that execute with interrupts disabled (“atomic blocks” in nesC) 2.Conservatively find “racing” variables == Asynchronous and stored-to outside of atomic blocks 3.Analyze non-racing variables only –Racing == homebrew synchronization protocol –Cannot reuse nesC compiler’s race detection which is unsound

16 Finding Atomic Blocks nesC guarantees that atomic blocks have lexical scope –This makes the problem easy When analyzing code not output by nesC… 1.Conservatively check for lexical interrupt use 2.If yes, all is good (this is often the case) 3.If no, context sensitive analysis is required –We don’t do that

17 Analyzing Asynchronous State 3 Add a flow edge from the end of each atomic section to the start of each interrupt handler Add a flow edge from the end of each interrupt handler back to the end of each atomic section Claim: This is a sound model for dataflow analysis of non-racing asynchronous global variables

18

19 Analyzing Racing Variables Need to know the underlying atomic memory operations –Exploit knowledge of compiler and target Easy in some cases E.g. word sized, word aligned scalar variables Difficult for compound variables When atomic operations are known, add flow edge to interrupts after each one –When not known, treat racing variable as ┴

20 Variable Classification Results For 15 applications totaling 145 Kloc –12 TinyOS apps for AVR-based MicaZ platform –3 other applications for AVR MCUs Conservative classification: –57% variables synchronous –34% variables asynchronous and not racing –8% variables racing

21 Analyzing Volatile Variables In C volatiles are opaque –“may change in ways unknown to the impl.” However: We are not analyzing “C” but rather “C + processor model” –We can perform dataflow analysis through some volatile locations First – For each volatile location: –Is it backed by SRAM or by a device register?

22 Analyzing Volatile SRAM volatile used to prevent loads and stores from being added, eliminated, or reordered Claim: Dataflow analysis can ignore the volatile qualifier for variables in SRAM –Basis: We have a sound model of all possible mutators No DMA on these architectures –Volatiles opaque at the language level but not the system level

23 Analyzing Device Registers Treat registers as SRAM except… –Load from a bit in a hardware register may return: Fixed value Last value stored Undeterminable value –Device register accesses may also have exploitable side effects – more on this soon

24 Analyzing Device Registers Currently we analyze interrupt control bits Plan to pursue this further –E.g. to infer predicates on device state “ADC is powered up, enabled, and configured to deliver repeating interrupts” Would be nice to have a tool for translating device register specifications into program analysis code

25 Embedded codes have “hidden” indirect control flow relations –Interrupts: 1.ADC driver code requests a conversion 2.ADC completion interrupt fires –TinyOS tasks (deferred procedure calls): 1.Task or interrupt posts a task 2.TinyOS main() loop dispatches the task Idea: Represent the hidden state and exploit it to increase analysis precision

26 Add models of pending interrupts and tasks to abstract machine state –Abstract interrupt and task schedulers decide when to fire these –These replace the default models where: Any interrupt may fire any time interrupts are enabled Any task may fire any time scheduler is reached –Effect is to prune edges from the flow graph

27 Example of causal relations: This work is preliminary and ongoing

28 Analyzer Big Picture Integrated into a single analysis pass: –Control flow graph discovery –Atomic block detection –Synchronous / asynchronous / racing analysis –Value set analysis –Pointer set analysis –Dead code detection –(Soon) Indirect control flow analysis

29 Reduction in code size by using analysis results to drive constant propagation, dead code elimination, etc. Reduction in RAM consumption by using analysis results to drive offline RAM compression Each result is geometric mean of improvements over same 15 AVR applications Baseline: No concurrency analysis, no analysis of volatile SRAM, address-taken pointer analysis

30 volatilesconcurrency p+v p+v+c pointers p+cv+c Improvement: 9% code, 6% data Improvement : 21% code, 23% data

31 Lessons Integrated analyses a necessary evil –Lots of interactions to keep track of  No fun to design and implement Precise analysis of low-level codes requires knowledge of HW and SW platform properties

32 Conclusion High-precision static analysis of MCU codes is possible and useful Open source cXprop tool –