Pluggable Domains for C Dataflow Analysis Nathan Cooprider and John Regehr {coop, School of Computing, University of Utah.

Slides:



Advertisements
Similar presentations
Dynamic Memory Allocation I Topics Basic representation and alignment (mainly for static memory allocation, main concepts carry over to dynamic memory.
Advertisements

Overview: Chapter 7  Sensor node platforms must contend with many issues  Energy consumption  Sensing environment  Networking  Real-time constraints.
Using Programmer-Written Compiler Extensions to Catch Security Holes Authors: Ken Ashcraft and Dawson Engler Presented by : Hong Chen CS590F 2/7/2007.
Chapter 8 Runtime Support. How program structures are implemented in a computer memory? The evolution of programming language design has led to the creation.
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.
1 HOIST: A System for Automatically Deriving Static Analyzers for Embedded Systems John Regehr Alastair Reid School of Computing, University of Utah.
1 Efficient Memory Safety for TinyOS Nathan Cooprider Will Archer Eric Eide David Gay † John Regehr University of Utah School of Computing † Intel Research.
1 TinyOS 2.1: Deploying Memory Safety Nathan Cooprider Yang Chen Will Archer Eric Eide David Gay † John Regehr University of Utah School of Computing †
Random Testing of Interrupt-Driven Software John Regehr University of Utah.
Static Analysis of Embedded C John Regehr University of Utah Joint work with Nathan Cooprider.
Memory Management 2010.
Lock Inference for Systems Software John Regehr Alastair Reid University of Utah March 17, 2003.
Generic Sensor Platform for Networked Sensors Haywood Ho.
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.
1 Last Class: Introduction Operating system = interface between user & architecture Importance of OS OS history: Change is only constant User-level Applications.
A Static Analysis Framework For Embedded Systems Nathan Cooprider John Regehr's Embedded Systems Group.
Mehmet Can Vuran, Instructor University of Nebraska-Lincoln Acknowledgement: Overheads adapted from those provided by the authors of the textbook.
1 ES 314 Advanced Programming Lec 2 Sept 3 Goals: Complete the discussion of problem Review of C++ Object-oriented design Arrays and pointers.
Procedure Optimizations and Interprocedural Analysis Chapter 15, 19 Mooly Sagiv.
Language Evaluation Criteria
Type-Specialized Staged Programming with Process Separation Yu David Liu, State University of New York at Binghamton Christian Skalka, University of Vermont.
April 15, 2005TinyOS: A Component Based OSPage 1 of 27 TinyOS A Component-Based Operating System for Networked Embedded Systems Tom Bush Graduate College.
15-740/ Oct. 17, 2012 Stefan Muller.  Problem: Software is buggy!  More specific problem: Want to make sure software doesn’t have bad property.
Types for Programs and Proofs Lecture 1. What are types? int, float, char, …, arrays types of procedures, functions, references, records, objects,...
Eric Keller, Evan Green Princeton University PRESTO /22/08 Virtualizing the Data Plane Through Source Code Merging.
Three fundamental concepts in computer security: Reference Monitors: An access control concept that refers to an abstract machine that mediates all accesses.
Algorithm Programming Bar-Ilan University תשס"ח by Moshe Fresko.
1 Efficient Type and Memory Safety for Tiny Embedded Systems John Regehr Nathan Cooprider Will Archer Eric Eide University of Utah School of Computing.
8-1 Embedded Systems Fixed-Point Math and Other Optimizations.
Generative Programming. Automated Assembly Lines.
Advanced Computer Architecture Lab University of Michigan USENIX Security ’03 Slide 1 High Coverage Detection of Input-Related Security Faults Eric Larson.
Chapter 4 Memory Management Virtual Memory.
Pluggable Domains for C Dataflow Analysis Nathan Cooprider and John Regehr {coop, School of Computing, University of Utah.
Slides created by: Professor Ian G. Harris Hello World #include main() { printf(“Hello, world.\n”); }  #include is a compiler directive to include (concatenate)
Xiong Junjie Node-level debugging based on finite state machine in wireless sensor networks.
Processor Structure and Function Chapter8:. CPU Structure  CPU must:  Fetch instructions –Read instruction from memory  Interpret instructions –Instruction.
1 Becoming More Effective with C++ … Day Two Stanley B. Lippman
C LANGUAGE Characteristics of C · Small size
Where Testing Fails …. Problem Areas Stack Overflow Race Conditions Deadlock Timing Reentrancy.
Chapter 1: Preliminaries Lecture # 2. Chapter 1: Preliminaries Reasons for Studying Concepts of Programming Languages Programming Domains Language Evaluation.
A Static Analyzer for Large Safety-­Critical Software Presented by Dario Bösch, ETH Zürich Research Topics in Software Engineering Dario.
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.
Computers’ Basic Organization
Ch 2. Getting Started with the Kernel
MODERN OPERATING SYSTEMS Third Edition ANDREW S
Virtualization Virtualize hardware resources through abstraction CPU
Definition CASE tools are software systems that are intended to provide automated support for routine activities in the software process such as editing.
Types for Programs and Proofs
GPU Memory Details Martin Kruliš by Martin Kruliš (v1.1)
Session 3 Memory Management
ENERGY 211 / CME 211 Lecture 25 November 17, 2008.
Compilers.
Ik-Soon Kim December 18, 2010 Embedded Software Platform Team
5.2 Eleven Advanced Optimizations of Cache Performance
C Basics.
C++ History C++ was designed at AT&T Bell Labs by Bjarne Stroustrup in the early 80's Based on the ‘C’ programming language C++ language standardised in.
High Coverage Detection of Input-Related Security Faults
Page Replacement.
Performance Optimization for Embedded Software
Efficient Memory Safety for TinyOS 2.1
CSE 153 Design of Operating Systems Winter 19
Lecture 4: Instruction Set Design/Pipelining
RUN-TIME STORAGE Chuen-Liang Chen Department of Computer Science
Rohan Yadav and Charles Yuan (rohany) (chenhuiy)
Dynamic Binary Translators and Instrumenters
Presentation transcript:

Pluggable Domains for C Dataflow Analysis Nathan Cooprider and John Regehr {coop, School of Computing, University of Utah

2 Goal ● Aggressive static analysis of embedded C programs – Use compiler technology ● not model checking technology – Perform optimizations

3 Solution ● Conditional X propagation (cXprop) – X is a pluggable abstract value domain ● Handles all of C ● Interprocedural dataflow analysis ● Analyzes concurrent programs ● Multiple code transformations ● Example benefit: – 9.2% average code size reduction in TinyOS applications (this is old, we do better now)

4 Initial contributions ● Clean domain interface for pluggable domains ● Quantitative comparison of abstract domains ● New method for analyzing interrupt-driven concurrency ● Pointer analysis intermixed with dataflow analysis

5 Uses of cXprop ● Compare abstract domains ● Optimize C code – Reduce code size – Lower duty cycle – Identify unused bits ● Clean up after CCured ● RAM compression

C : A million and one ways... ● To shoot yourself (and our analysis) in the foot – Stack manipulations – External calls – Floating point – Order of evaluation – Concurrency – Missing returns

7 Necessary assumptions for cXprop ● Precise and sound dataflow analysis of C is nearly impossible ● Assumptions about – Memory model – External calls – Floating point – Inline assembly – Order of evaluation – Concurrency ● Consider store to array where index is indeterminate – big_array[ ⊥ ] = 42; – Sound analysis must drop all dataflow facts – Could overwrite return address

8 cXprop components CIL Core cXprop Abstract domain CIL's feature interface cXprop's domain interface

CIL ● C Intermediate Language – developed at UCB ● Cleans up C to a few core constructs – removes syntactic sugar (like “->” notation) – arrays become pointers – all loops become while loops ● Works on real programs – handles ANSI-C, C, and GNU C – SPEC 95, linux kernel,, bzip

cXpropcXprop

11 cXprop's abstract domain interface ● Ocaml – Do not need to know CIL internals – Parity domain implemented in under an hour ● 32 transfer functions – 6 backwards functions ● 11 utility functions – widen, concretize, abstract, meet,... shiftrt(abstract value, abstract value, type) ➔ abstract value backwards_eq (abstract value, abstract value, type, abstraction function) ➔ (boolean * (abstract value * abstract value) * (abstract value * abstract value)) mult(abstract value, abstract value, type) ➔ abstract value

12 Results of abstract interpretation opa que x z a=&x z=5 *a=77 z=2 *a=13 a=&y *a=42 *a=26 z=1 z=0 a= x= y= z= a= x= y= z= a= x= y= z= a= x= y= z= T F T F F T

13 Parity domain opa que x z a=&x z=5 *a=77 z=2 *a=13 a=&y *a=42 *a=26 z=1 z=0 a={&x} x= ⊥ y= ⊥ z= ⊥ a={&y} x=odd y=even z= ⊥ a={&y} x=odd y=even z= ⊥ a={&y} x=odd y= ⊥ z= ⊥ T F T F F T

14 Constant domain opa que x z a=&x z=5 *a=77 z=2 *a=13 a=&y *a=42 *a=26 z=1 z=0 a={&x} x= ⊥ y= ⊥ z= ⊥ a={&y} x= ⊥ y= ⊥ z= ⊥ a={&y} x= ⊥ y= ⊥ z= ⊥ a={&y} x= ⊥ y= ⊥ z= ⊥ T F T F F T

15 Value-set domain opa que x z a=&x z=5 *a=77 z=2 *a=13 a=&y *a=42 *a=26 z=1 z=0 a={&x} x= ⊥ y= ⊥ z= ⊥ a={&y} x={13,77 } y={42} z={1} a={&y} x={13,77 } y={42} z={2,5} a={&y} x={13,77 } y= ⊥ z={2,5} T F T F F T

16 Bitwise domain opa que x z a=&x z=5 *a=77 z=2 *a=13 a=&y *a=42 *a=26 z=1 z=0 a={&x} x= ⊥⊥⊥⊥⊥⊥⊥ ⊥ y= ⊥⊥⊥⊥⊥⊥⊥ ⊥ z= ⊥⊥⊥⊥⊥⊥⊥ ⊥ a={&y} x= 0 ⊥ y= z= ⊥ a={&y} x= 0 ⊥ y= z= ⊥⊥⊥ a={&y} x= 0 ⊥ y= ⊥⊥⊥⊥⊥⊥⊥ ⊥ z =00000 ⊥⊥⊥ T F T F F T

17 Interval domain opa que x z a=&x z=5 *a=77 z=2 *a=13 a=&y *a=42 *a=26 z=1 z=0 a={&x} x= ⊥ y= ⊥ z= ⊥ a={&y} x= ⊥ y=[26,42 ] z=[1,1] a={&y} x= ⊥ y=[26,42 ] z=[2,5] a={&y} x= ⊥ y= ⊥ z=[2,5] T F T F F T

18 Constant and value-set domains ● Constant domain ● Transfer functions – Special cases – Handle ⊥ – Call CIL's constant folder ● Value-set domain ● User-defined set size ● Quadratic-time transfer functions using brute force

Constant let mult d1 d2 tp = match d1, d2 with Constant(z), _ | _, Constant(z) when (isZero z) -> Constant (zero) | Bottom, _ | _, Bottom -> Bottom | Constant(e1), Constant(e2) -> conc_to_abs (BinOp(Mult,e1,e2,tp)) 1 int tricky () { 2 int x = 1; 3 int count = 0; 4 do { 5 int b = x; 6 if (b != 1) 7 x = 2; 8 count += x; 9 } while (count < 10); 10 return x; 11 }

20 Bitwise and interval domains ● Bitwise domain ● Vectors of three- valued bits – 0,1, ⊥ ● Regehr and Duongsaa's transfer functions ● Interval domain ● Upper and lower bound on range of values ● Regehr and Duongsaa's transfer functions

Bitwise 1 int tricky () { 2 int x = 1; 3 int count = 0; 4 do { 5 int b = x; 6 if (b != 1) 7 x = 2; 8 count += x; 9 } while (count < 10); 10 return x; 11 } let lnot (d, dk) tp = if ((dk = no_bottoms)&&(d = I.zero)) then TbTrue else if (I.logand d dk) <> I.zero then TbFalse else TbBottom

22 Software and hardware platform ● TinyOS – libraries of code for sensor networks – idioms are conducive to static analysis ● static memory allocation model – written in nesC ● unsafe dialect of C that compiles to C ● Mica2 from Crossbow – ATmega128 8-bit processor – 4 KB RAM, 128 KB flash

23 Sensor network programming ● Component-based – Block-box reuse ● Interrupt-driven ● Very constrained environment ● cXprop features added for sensor network application analysis – Concurrency analysis – Interrupt bit modeling – Atomic section removal

24 Modeling concurrency in cXprop ● Analysis of interrupt-driven concurrency ● Three types of program data – Unshared data – Unprotected shared data – Protected shared data

25 Concurrency

26 cXprop's transformations ● Three primary transformation modes – Normal conditional constant propagation – Assert program state – Dynamic dataflow information x = 1; if (flag == 0) x++; y = x + 1; x = 1; if (flag == 0) x=2; y = x + 1; x = 1; if (flag == 0) x++; y = x + 1; x = 1; assert (x==1); if (flag == 0) { assert(x==1); x++; } assert (x==1 || x==2); y = x + 1; x = 1; if (flag == 0) x++; y = x + 1; x = 1; cXprop_dynamic_meet(& cxp_glob_var_0, x); if (flag == 0) { x++; cXprop_dynamic_meet(& cxp_glob_var_1, x);} y = x + 1; cXprop_dynamic_meet(& cxp_glob_var_2, y);

27 Validation of cXprop ● Specific test cases ● Benchmarks – SPEC 2000: gzip, mcf, bzip2 – MiBench: basicmath, patricia, FFT – TinyOS 1.x: blink, cnttoledsandrfm, hfs, rfmtoleds, surge, taskapp, acoustic, agilla, ecc – TinyOS 2.x: null, basestation – Miscellaneous: rc4, dhrystone, yacr2 ● Random program generator

28 Analysis times ● Analysis time contributors – Size of concretization sets – Transfer functions – Dataflow representation

29 Code size reduction results

30 Comparing abstract domains ● Goal: Apples-to-apples comparison across domains ● Information known metric ● total information known for a program= total # of information bits / total # of bits possible # of information bits ≝ log 2 j – log 2 k j = total # of representable values k = # of possible values

31 Linear vs. logarithmic These are the possible values for an eight-bit variable

32 We know it is not 42 Taking away one is not very useful

33 We know it is odd Taking away half is real progress

34 Iterations later... How about now?

35 We know it is not nine Taking away one is still not very meaningful

36 We know it is less than nine So we use the log scale

37 Example information bit computation ● Consider a 32-bit int with abstract value of {4,17,19,312} ● There are 2 32 representable values ● There are 4 possible values ● log – log 2 4 = 30 information bits

38 Comparison of information known

39 Demo

Application 1: Safe TinyOS

41 A conflict in embedded systems ● Tiny embedded systems coded in C ● These are used in important and even safety critical systems ● Developers are extremely reluctant to change languages ● Developers are extremely reluctant to introduce time, space, and memory overhead

42 Our contribution ● Type and memory safety with little overhead – Lots of engineering required – Must be fast if people are going to use it – Low or no costs ● 2% average decrease in duty cycle ● 8% average increase in code size ● 6% average increase in data size ● Safety with minimal programmer impact – Safe TinyOS – works on unmodified legacy C code

43 Safety for embedded systems ● Type and memory safety – early detection of bugs – observability and predictable consequences of run-time faults ● CCured is the starting point – Safe C dialect developed at Berkeley – Translates a C program into a safe C program by inserting safety checks ● Example of null check: __CHECK_NULL((void *)next_task, 0xAA); ((*next_task))();

44 Initial drawbacks of CCured ● Overhead – All these checks slow down the program and use up memory ● CCured library – Does not fit on motes if unmodified – First estimation at fitting onto Mica2 ● over 1KB of RAM ● over 3KB of ROM

45 Addressing the drawbacks ● One-time, manual changes to CCured library – remove OS and x86 dependencies – drop garbage collector ● Refactor hardware accesses ● Protect non-atomic accesses to fat-pointers ● Compress the error messages ● Optimize

46 Optimize with an inliner ● Inlining can reduce size ● Inlining introduces some context sensitivity ● Parameterize inlining decisions – sweet spot in between ● maximally reduce size AFTER optimization

47 Optimize with cXprop ● Interprocedural dataflow analysis ● Analyzes concurrent programs – Removal of nested atomic sections ● Simultaneous pointer analysis ● Aggressive dead code elimination – whole program

48 Safe TinyOS Toolchain

49 Average 2% decrease in duty cycle via Safe TinyOS

50 Average 8% increase in code size via Safe TinyOS

51 Average 6% increase in data size via Safe TinyOS

Application 2: RAM compression

Observations ● RAM is used inefficiently ● Low-end systems persistently need RAM – Sensor networks use Harvard-architectures ● Resource reduction benefit is 0 after it “fits” ● Manual optimization is difficult ● So we combine cXprop with RAM compression

RAM compression ● Original declaration ● Compression table for task queue ● Structure for holding compressed values ● Original code for reading task queue ● Code for reading the compressed queue typedef struct { void (*tp)(void); } TOSH_sched_entry_T; volatile TOSH_sched_entry_T TOSH_queue[8]; unsigned short const __attribute__((__progmem__)) __valueset_3[4] = {NULL, &BlinkTaskM$processing, &TimerM$HandleFire, &TimerM$signalOneTimer};, struct __compressed { char f9[2] ; unsigned char f0 : 3 ;... }; func = TOSH_queue[old_full].tp; __tmp = __array_read (__compressed.f9, old_full, 2); func = __finv_16 (__valueset_3, __tmp);l

“Current” cXprop optimizations

56 cXprop with RAM compression

57 Tradeoff graphs

58 Another demo (sort of)

59 Conclusion ● Interprocedural dataflow analysis based on abstract interpretation ● Clean domain interface for pluggable domains ● Quantitative comparison of abstract domains ● Additions to handle concurrency in interrupt- driven embedded systems ● Cool applications – Safe TinyOS and RAM compression ● You can download cXprop here: