Volatiles Are Miscompiled, and What to Do about It

Slides:



Advertisements
Similar presentations
Code Optimization and Performance Chapter 5 CS 105 Tour of the Black Holes of Computing.
Advertisements

Overview of programming in C C is a fast, efficient, flexible programming language Paradigm: C is procedural (like Fortran, Pascal), not object oriented.
Machine Instructions Operations
Making Choices in C if/else statement logical operators break and continue statements switch statement the conditional operator.
Native x86 Decompilation Using Semantics-Preserving Structural Analysis and Iterative Control-Flow Structuring Edward J. Schwartz *, JongHyup Lee ✝, Maverick.
Inline Assembly Section 1: Recitation 7. In the early days of computing, most programs were written in assembly code. –Unmanageable because No type checking,
1 Homework Turn in HW2 at start of next class. Starting Chapter 2 K&R. Read ahead. HW3 is on line. –Due: class 9, but a lot to do! –You may want to get.
Introduction to Computers and Programming Lecture 4: Mathematical Operators New York University.
Data Representation Kieran Mathieson. Outline Digital constraints Data types Integer Real Character Boolean Memory address.
4/6/08Prof. Hilfinger CS164 Lecture 291 Code Generation Lecture 29 (based on slides by R. Bodik)
1 Assembly Language: Overview. 2 If you’re a computer, What’s the fastest way to multiply by 5? What’s the fastest way to divide by 5?
1 Homework Reading –PAL, pp Machine Projects –MP2 due at start of Class 12 Labs –Continue labs with your assigned section.
Assembly Questions תרגול 12.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 CST 221 OBJECT ORIENTED PROGRAMMING(OOP) ( 2 CREDITS.
Goals: To gain an understanding of assembly To get your hands dirty in GDB.
Computer Science Detecting Memory Access Errors via Illegal Write Monitoring Ongoing Research by Emre Can Sezer.
Summary of what we learned yesterday Basics of C++ Format of a program Syntax of literals, keywords, symbols, variables Simple data types and arithmetic.
Testing and Debugging Version 1.0. All kinds of things can go wrong when you are developing a program. The compiler discovers syntax errors in your code.
Volatiles Are Miscompiled, and What to Do about It Eric Eide and John Regehr University of Utah EMSOFT 2008 / October 22, 2008.
16 October Reminder Types of Testing: Purpose  Functional testing  Usability testing  Conformance testing  Performance testing  Acceptance.
Semantics. Semantics is a precise definition of the meaning of a syntactically and type-wise correct program. Ideas of meaning: –Operational Semantics.
University of Amsterdam Computer Systems – optimizing program performance Arnoud Visser 1 Computer Systems Optimizing program performance.
1 Carnegie Mellon Assembly and Bomb Lab : Introduction to Computer Systems Recitation 4, Sept. 17, 2012.
COIT29222 Structured Programming 1 COIT29222-Structured Programming Lecture Week 02  Reading: Textbook(4 th Ed.), Chapter 2 Textbook (6 th Ed.), Chapters.
CSE 332: C++ expressions Expressions: Operators and Operands Operators obey arity, associativity, and precedence int result = 2 * 3 + 5; // assigns 11.
Announcements Assignment 2 Out Today Quiz today - so I need to shut up at 4:25 1.
Introduction to Algorithmic Processes CMPSC 201C Fall 2000.
Finding and Understanding Bugs in C Compilers Xuejun Yang Yang Chen Eric Eide John Regehr University of Utah.
Software Testing and QA Theory and Practice (Chapter 5: Data Flow Testing) © Naik & Tripathy 1 Software Testing and Quality Assurance Theory and Practice.
Chapter 3 Math Operations. Objectives Use the assignment and arithmetic operators. Use operators in output statements. Explain the problem with division.
Test-Case Reduction for C Compiler Bugs
Bitwise Operations C includes operators that permit working with the bit-level representation of a value. You can: - shift the bits of a value to the left.
Dynamic Allocation in C
Secure Coding Rules for C++ Copyright © 2016 Curt Hill
Chapter 9: Value-Returning Functions
Introduction to the C Language
This Week: Integers Integers Summary
A bit of C programming Lecture 3 Uli Raich.
BASIC ELEMENTS OF A COMPUTER PROGRAM
Homework Reading Labs PAL, pp
Instructor: David Ferry
Chapter 2 Assignment and Interactive Input
Optimization Code Optimization ©SoftMoore Consulting.
Structural testing, Path Testing
COMP3221: Microprocessors and Embedded Systems
Machine-Level Programming 6 Structured Data
Character Set The character set of C represents alphabet, digit or any symbol used to represent information. Types Character Set Uppercase Alphabets A,
Machine-Level Programming: Control Flow
Comp Integers Spring 2015 Topics Numeric Encodings
C Prog. To Object Code text text binary binary Code in files p1.c p2.c
Portability CPSC 315 – Programming Studio
Lecture 30 (based on slides by R. Bodik)
Bitwise Operations C includes operators that permit working with the bit-level representation of a value. You can: - shift the bits of a value to the left.
C Operators, Operands, Expressions & Statements
Homework Reading Machine Projects Labs PAL, pp
Effective and Efficient memory Protection Using Dynamic Tainting
Chapter 4 Action Routines.
Dynamic Program Analysis
Homework Starting Chapter 2 K&R. Read ahead. Questions?
Instruction Set Principles
15-110: Principles of Computing
9-10 Classes: A Deeper Look.
C structures and Compilation to IA32
Summary of what we learned yesterday
Operations and Arithmetic
Java Basics Data Types in Java.
02/02/10 20:53 Assembly Questions תרגול 12 1.
C Language B. DHIVYA 17PCA140 II MCA.
9-10 Classes: A Deeper Look.
Software Testing and QA Theory and Practice (Chapter 5: Data Flow Testing) © Naik & Tripathy 1 Software Testing and Quality Assurance Theory and Practice.
Presentation transcript:

Volatiles Are Miscompiled, and What to Do about It Eric Eide and John Regehr University of Utah EMSOFT 2008 / October 22, 2008

Code Meets World volatile int TIME; volatile int LED; int get_time() { 70F1 0001 int get_time() { return TIME; } void set_led() { LED = 1; int get_time() { // … } void set_led() {

Volatile Semantics compiled program must “do” what the source program says i.e., volatile side-effects must occur volatile int WATCHDOG; void reset_watchdog() { WATCHDOG = WATCHDOG; } GCC / IA32 GCC / MSP430 reset_watchdog: movl WATCHDOG, %eax movl %eax, WATCHDOG ret reset_watchdog: ret

Our Contributions performed study of volatile bugs developed automated testing framework “careful” random program generator access summary testing found defects in all compilers we tested evaluated a workaround for volatile errors helped to make one compiler “10,000× better”

Talk Outline random program gen. .c access summary testing randprog compiler access summary testing exe checker examine error rates evaluate workaround investigate compiler defects help make compiler better

Generating Good Test Cases our test cases are C programs a good test case has a “right answer” an “answer” for us is an executable we judge “rightness” by inspecting its output the computed result and the trail of side-effects  we must generate C programs that have predictable behaviors independent of compiler, compiler options, …

Our Test Programs randprog creates programs that compute over integer variables signed/unsigned; 8/16/32 bits some globals declared volatile functions take and return integer values assignments, for-loops, arithmetic & logical operators no pointers, arrays, structs, or unions .c randprog

Test Program I/O no input (“closed”) two outputs now we must… a checksum over global variables a sequence of accesses to volatile variables now we must… …ensure that every test has a “right answer” not just the checksum, but also the volatile invariant …figure out what that answer is

Strictly Conforming avoid creating programs whose output depends on unspecified behavior — e.g., evaluation order impl.-defined behavior — e.g., range of int undefined behavior — e.g., division by zero …according to the C standard enforce statically & dynamically

Evaluation Order ensure that expression value is independent of evaluation order track read/write effect of expressions as they are built may-read set may-write set volatile-access flag clear @ sequence point volatile int vol_1; int glo_2; int func_3(void) { vol_1 = glo_2; return 7; } void func_4() { int loc_5 = …; int loc_6 = func_3() + ???;

Dealing with Integers avoid most problematic behaviors, e.g. integer range issues — avoid statically signed shifts, div-by-zero — avoid dynamically but still there are issues… signed integer overflow & underflow arithmetic & logical operators in combination integer promotions these do not matter in practice for us so, “nearly strictly conforming” programs

Evaluating Test Cases random program gen. access summary testing .c exe randprog compiler checker

Access Summary Testing exe compiler checker ✔/✖ compile the test case run executable in instrumented environment map memory accesses to volatile variables create an access summary compare to the correct access summary

Access Summary Implementation two instrumented environments volcheck — binary rewriting for IA32 (Valgrind) Avrora — an AVR platform simulator each outputs a log of memory accesses creating the summary scan source & object code  volatile variables count total # of loads & stores to each volatile effective: compact & sufficiently precise

identical checksum & summaries? Is It Right? ? .c exe compiler checker ✔/✖ ✔ ✖ -O1 exe -O2 -O3 identical checksum & summaries? yes ✔ no ✖

From Errors to Defects volatile error functional error volatile-access summary differs across the executables functional error output checksum differs across the executables a single test case can be both

…and what to do about them Experimental Results …and what to do about them

Methodology examined 13 production-quality C compilers IA32 GCC (×5), LLVM-GCC, Intel, Sun AVR GCC (×3) Coldfire CodeWarrior MSP430 GCC all: handwritten tests + manual inspection 9: random tests + access summary testing 250,000 test programs

Access Summary Results arch. / compiler version volatile errors (%) functional errors (%) IA32 / GCC 3.4.6 1.228 0.004 4.0.4 0.038 0.031 4.1.2 0.195 0.025 4.2.4 0.766 0.003 4.3.1 0.709 IA32 / LLVM-GCC 2.2 18.720 0.126 AVR / GCC 3.4.3 1.928 0.391 0.037 0.254 4.2.2 0.727 0.214

Work Around Volatile Errors idea: “protect” volatile accesses from overeager compilers via helper functions opaque int vol_read_int(volatile int *vp) { return *vp; } volatile int *vol_id_int(volatile int *vp) { return vp; } x = vol_1; vol_1 = 0; x = vol_read_int(vol_1); *vol_id_int(&vol_1) = 0;

Volatile Helper Results arch. / compiler vers. volatile errs. (%) vol. errs. w/help (%) vol. errs. fixed (%) IA32 / GCC 3.4.6 1.228 0.300 76 4.0.4 0.038 0.018 51 4.1.2 0.195 0.016 92 4.2.4 0.766 0.002 100 4.3.1 0.709 0.000 IA32 / LLVM-GCC 2.2 18.720 0.047 AVR / GCC 3.4.3 1.928 0.434 77 0.037 0.033 10 4.2.2 0.727 0.021 97

Sample GCC Bug (#1) GCC 4.3.0 / IA32 / -Os const volatile int x; volatile int y; void foo(void) { for (y=0; y>10; y++) { int z = x; } foo: movl $0, y movl x, %eax jmp .L3 .L2: movl y, %eax incl %eax movl %eax, y .L3: movl y, %eax cmpl $10, %eax jg .L3 ret

Sample LLVM-GCC Bug LLVM-GCC 2.2 / IA32 / -O2 volatile int a; void baz(void) { int i; for (i=0; i<3; i++) { a += 7; } baz: movl a, %eax leal 7(%eax), %ecx movl %ecx, a leal 14(%eax), %ecx addl $21, %eax movl %eax, a ret LLVM-GCC 2.2 / IA32 / -O2

Toward Zero Volatile Bugs we distilled random-program errors into bug reports against LLVM-GCC Mar–Jul 2008: 5 volatile + 8 functional bugs fixed over our 250,000 test programs: 10,000× improvement LLVM-GCC for IA32 version volatile errors (%) volatile errors w/helpers (%) errors fixed by helpers (%) functional errors (%) 2.2 18.720 0.047 100 0.126 r53339 0.002 0.009

Summary we developed an automated and effective framework for discovering volatile-related defects in C compilers “careful” random program generation access summary testing first published study of volatile bugs that we know of the miscompilation of volatiles is disturbingly common serious consequences for critical & embedded software what to do about it? a simple workaround can avoid 96% of volatile errors report bugs to compiler writers give advice to developers & compiler writers (in paper)

Thank you! questions?