Hardening LLVM with Random Testing

Similar documents
Automatic program generation for detecting vulnerabilities and errors in compilers and interpreters

Software Testing CS 408. Lecture 10: Compiler Testing 2/15/18

Page 1. Today. Last Time. Is the assembly code right? Is the assembly code right? Which compiler is right? Compiler requirements CPP Volatile

Important From Last Time

Page 1. Today. Important From Last Time. Is the assembly code right? Is the assembly code right? Which compiler is right?

Important From Last Time

Test- Case Reduc-on for C Compiler Bugs. John Regehr, Yang Chen, Pascal Cuoq, Eric Eide, Chucky Ellison, Xuejun Yang

C++ Undefined Behavior What is it, and why should I care?

Undefined Behaviour in C

Name :. Roll No. :... Invigilator s Signature : INTRODUCTION TO PROGRAMMING. Time Allotted : 3 Hours Full Marks : 70

C++ Undefined Behavior

Random Testing of C Compilers Targeting Arithmetic Optimization

Verified compilers. Guest lecture for Compiler Construction, Spring Magnus Myréen. Chalmers University of Technology

Turning proof assistants into programming assistants

Software Testing CS 408. Lecture 11: Review 2/20/18

CS 61C: Great Ideas in Computer Architecture Introduction to C

Agenda. CS 61C: Great Ideas in Computer Architecture. Lecture 2: Numbers & C Language 8/29/17. Recap: Binary Number Conversion

CS 61C: Great Ideas in Computer Architecture. Lecture 2: Numbers & C Language. Krste Asanović & Randy Katz

Memory Corruption 101 From Primitives to Exploit

CSE 431S Type Checking. Washington University Spring 2013

15-411: LLVM. Jan Hoffmann. Substantial portions courtesy of Deby Katz

Memory Safety for Embedded Devices with nescheck

A brief introduction to C programming for Java programmers

Static Program Analysis Part 1 the TIP language

Non-numeric types, boolean types, arithmetic. operators. Comp Sci 1570 Introduction to C++ Non-numeric types. const. Reserved words.

SOFTWARE QUALITY OBJECTIVES FOR SOURCE CODE

QUIZ. What is wrong with this code that uses default arguments?

PIC 10A Pointers, Arrays, and Dynamic Memory Allocation. Ernest Ryu UCLA Mathematics

Variables Data types Variable I/O. C introduction. Variables. Variables 1 / 14

Compiler Construction: LLVMlite

Kurt Schmidt. October 30, 2018

Hacking in C. Pointers. Radboud University, Nijmegen, The Netherlands. Spring 2019

Tutorial 5. Overflow. Data Types. Structures. Call stack. Stack frames. Debugging Tips. CS 136 Winter 2019 Tutorial 5 1

CS 61c: Great Ideas in Computer Architecture

Accelerating Stateflow With LLVM

Deep C by Olve Maudal

Randomized Stress-Testing of Link-Time Optimizers

Recitation #11 Malloc Lab. November 7th, 2017

XSEDE Scholars Program Introduction to C Programming. John Lockman III June 7 th, 2012

C Review. MaxMSP Developers Workshop Summer 2009 CNMAT

Skip the FFI! Embedding Clang for C Interoperability. Jordan Rose Compiler Engineer, Apple. John McCall Compiler Engineer, Apple

Please refer to the turn-in procedure document on the website for instructions on the turn-in procedure.

Compilers. Type checking. Yannis Smaragdakis, U. Athens (original slides by Sam

CS 61C: Great Ideas in Computer Architecture C Pointers. Instructors: Vladimir Stojanovic & Nicholas Weaver

A complex expression to evaluate we need to reduce it to a series of simple expressions. E.g * 7 =>2+ 35 => 37. E.g.

CS61C : Machine Structures

C Programming SYLLABUS COVERAGE SYLLABUS IN DETAILS

C Language Part 1 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee

In Java we have the keyword null, which is the value of an uninitialized reference type

Writing Program in C Expressions and Control Structures (Selection Statements and Loops)

Part I Part 1 Expressions

Agenda. Peer Instruction Question 1. Peer Instruction Answer 1. Peer Instruction Question 2 6/22/2011

P.G.TRB - COMPUTER SCIENCE. c) data processing language d) none of the above

Pointers, Dynamic Data, and Reference Types

HW1 due Monday by 9:30am Assignment online, submission details to come

A Fast Review of C Essentials Part I

Fixed-Point Math and Other Optimizations

Connecting the EDG front-end to LLVM. Renato Golin, Evzen Muller, Jim MacArthur, Al Grant ARM Ltd.

CS61C Machine Structures. Lecture 4 C Pointers and Arrays. 1/25/2006 John Wawrzynek. www-inst.eecs.berkeley.edu/~cs61c/

C-LANGUAGE CURRICULAM

Deep C (and C++) by Olve Maudal

Tutorial: Building a backend in 24 hours. Anton Korobeynikov

Laboratory 2: Programming Basics and Variables. Lecture notes: 1. A quick review of hello_comment.c 2. Some useful information

CprE 288 Introduction to Embedded Systems Exam 1 Review. 1

C Refresher, Advance C, Coding Standard, Misra C Compliance & Real-time Programming

Aryan College. Fundamental of C Programming. Unit I: Q1. What will be the value of the following expression? (2017) A + 9

C Programming. Course Outline. C Programming. Code: MBD101. Duration: 10 Hours. Prerequisites:

LLVM code generation and implementation of nested functions for the SimpliC language

Formal C semantics: CompCert and the C standard

DEVIRTUALIZATION IN LLVM

The Mercury Frequently Asked Questions List

Undefinedness and Non-determinism in C

CS107 Handout 13 Spring 2008 April 18, 2008 Computer Architecture: Take II

Fundamentals of Programming

Variables in C. Variables in C. What Are Variables in C? CMSC 104, Fall 2012 John Y. Park

Principles of Compiler Design

Test-Case Reduction for C Compiler Bugs

CSE 333 Autumn 2013 Midterm

Chapter 11 Introduction to Programming in C

Lecture 2: C Programm

Character Set. The character set of C represents alphabet, digit or any symbol used to represent information. Digits 0, 1, 2, 3, 9

CS 261 Fall C Introduction. Variables, Memory Model, Pointers, and Debugging. Mike Lam, Professor

Number Review. Lecture #3 More C intro, C Strings, Arrays, & Malloc Variables. Clarification about counting down

Why Pointers. Pointers. Pointer Declaration. Two Pointer Operators. What Are Pointers? Memory address POINTERVariable Contents ...

The Compiler So Far. CSC 4181 Compiler Construction. Semantic Analysis. Beyond Syntax. Goals of a Semantic Analyzer.

Heap Arrays and Linked Lists. Steven R. Bagley

Programming in C and C++

Object Oriented Programming Exception Handling

CS 61C: Great Ideas in Computer Architecture. C Arrays, Strings, More Pointers

Class Information ANNOUCEMENTS

Introduction to C++ with content from

COS 320. Compiling Techniques

Random Testing of Interrupt-Driven Software. John Regehr University of Utah

Page 1. Stuff. Last Time. Today. Safety-Critical Systems MISRA-C. Terminology. Interrupts Inline assembly Intrinsics

3. Java - Language Constructs I

CMPE-013/L. Introduction to C Programming

C Programming Review CSC 4320/6320

INITIALISING POINTER VARIABLES; DYNAMIC VARIABLES; OPERATIONS ON POINTERS

Lectures 5-6: Introduction to C

CS61C : Machine Structures

Transcription:

Hardening LLVM with Random Testing Xuejun Yang, Yang Chen Eric Eide, John Regehr {jxyang, chenyang, eeide, regehr}@cs.utah.edu University of Utah 11/3/2010 1

A LLVM Crash Bug int * p[2]; int i; for (...) { p[i] = &i; } LLVM crashed due to an over-aggressive assertion in Loop Invariant Code Motion in a 2.8 revision For some reason it thought the address-taken variable i must not be part of expression on LHS University of Utah 2 11/3/2010 2

Some LLVM Wrong Code Bugs A 2.4 revision simplifies "(a > 13) & (a == 15)" into "(a > 13) A 2.8 revision folds "((x == x) (y & z)) == 1 into 0 - thought sub-expression (y & z) is constant integer A 2.8 revision reduces this loop into i = 1 for (i = 0; i < 5; i++) { if (i) continue; if (i) break; } University of Utah 3 11/3/2010 3

Tally of Bugs From March, 2008 to present: 170 bugs fixed + 3 reported but not yet fixed 57 wrong code bugs 116 crash bugs Account for 2.6% of total valid bugs filed against LLVM in that period 11/3/2010 University of Utah 4

Goal Use random differential testing to find LLVM bugs! Aimed at deep optimization bugs Systematically Find bugs, Report bugs Automate the process as much as we can Ultimate goal: improve LLVM quality 11/3/2010 University of Utah 5

C program Random Generator clang -O0 clang -O3 gcc -O results majority vote minority University of Utah

1. What we do 2. What we learned 3. How we do it University of Utah 7

An Experiment Compiled and ran 1,000,000 random programs Using LLVM 1.9 2.8 -O0, -O1, -O2, -Os, -O3 x86 only University of Utah 8

Crash Error is the percentage of crashes triggered by 1,000,000 random test cases University of Utah

Distinct crash Errors is the number of distinct crashes triggered by 1,000,000 random test cases University of Utah 10

Wrong Code Error Rate is the percentage of wrong code errors found by 1,000,000 random test cases University of Utah 11

0 10 20 30 40 Dan Gohman Chris Lattner Evan Cheng Eli Friedman Devang Patel Jakob S. Olesen Anton Korobeynikov Duncan Sands Dale Johannesen Nick Lewycky Owen Anderson Bill Wendling Anders Carlsson Jakub Staszak Daniel Dunbar Lang Hames Bob Wilson 11/3/2010 Who fixed the bugs we reported? University of Utah 12

Bug Distribution Across Stages frontend 10 middle-end 61 backend 67 Can t identify 35 total 173 Classification is based on bugzilla records and/or source files committed for bug fixes Source files under Clang or gcc are considered frontend Source files under Analysis and Transforms are considered middle-end Source files under CodeGen and Target are considered backend University of Utah 11/3/2010 13

Bug Distribution Across Files Cpp file Bugs found InstructionCombining 13 SimpleRegisterCoalescing 10 DAGCombiner 6 LoopUnswitch 5 LoopStrengthReduce 4 JumpThreading 4 LICM 4 FastISel 4 llvm-convert 4 ExprConstant 4 X86InstrInfo 3 X86InstrInfo.td 3 X86ISelDAGToDAG 3 BranchFolding 3 (and many more) University of Utah 11/3/2010 14

Increased Coverage by Random Programs 80.00% 74.54% 74.69% 72.90% 72.95% 70.00% 60.00% 59.22% 59.48% 50.00% 40.00% 30.00% 20.00% 10.00% Clang + LLVM test suite Clang + LLVM test suite + 10,000 random programs 0.00% Line Function Branch 11/3/2010 University of Utah 15

1. What we do 2. What we learned 3. How we do it University of Utah 16

Beyond a Random Generator Ability to create random programs with unambiguous meanings undefined behaviors unspecified behaviors Ability to check the meaning of a C program is preserved by the compiler Summarize the meaning - the checksum of global variables Test harness: automatically detect checksum discrepancies 11/3/2010 University of Utah 17

Not a Bug #1 int foo (int x) { return (x+1) > x; } int main (void) { printf ("%d\n", foo (INT_MAX)); return 0; } $ gcc -O1 foo.c -o foo $./foo 0 $ clang -O2 foo.c -o foo $./foo 1 University of Utah 18

Not a Bug #2 int a; void bar (int a, int b) { } int main (void) { bar (a=1, a=2); printf ("%d\n", a); return 0; } $ gcc -O bar.c -o bar $./bar 1 $ clang -O bar.c -o bar $./bar 2 University of Utah 19

Two Goals We Must Balance Be expressive on code generation - is easy if you don t care about undefined / unspecified behavior Avoid undefined behaviors and not depend on unspecified behaviors - is easy if you don t care about expressiveness Expressive code that avoids undefined / unspecified behavior is hard 11/3/2010 University of Utah 20

Design Compromises Implementation-defined behavior is allowed - Avoiding it is too restrictive - We cannot do differential testing of x86 Clang vs. MSP430 Clang No ground truth - If all compilers generate the same wrong answer, we ll never know - Generating self-checking random code restricts expressiveness No attempt to generate terminating programs - Test harness uses timeouts - In practice ~10% of random programs don t terminate within a few seconds 11/3/2010 University of Utah 21

Supported features: Arithmetic, logical, and bit operations on integers Loops Conditionals Function calls Const and volatile Structs and Bitfields Pointers and arrays Goto Break, continue Not supported: Side-effecting expressions Comma operator Interesting type casts Strings Unions Floating point Nontrivial C++ Nonlocal jumps Varargs Recursive functions Function pointers Dynamic memory alloc University of Utah 22

Avoid Undefined Behaviors With Extra Code Use before initialization: declare all variables with explicit initializers Array OOB access: force all indices within bound using modulus Null pointer deference: check nullness before dereference 11/3/2010 University of Utah 23

Undefined Integer Behaviors Problem: These are undefined in C Divide by zero INT_MIN % -1 Debatable in C99 standard but undefined in practice Shift by negative, shift past bitwidth Signed overflow Etc. 11/3/2010 University of Utah 24

Undefined Integer Behaviors Solution: Wrap all potentially undefined operations int safe_signed_left_shift (int si1, int si2) { } if (si1 < 0 si2 < 0 si2 >= sizeof(int) si1 > (INT_MAX >> si2)){ return si1; } else { } return si1 << si2; 11/3/2010 University of Utah 25

Avoid Undefined/Unspecified Behaviors Statically Some undefined / unspecified are too difficult to avoid structurally Dereference dangling pointers Order of evaluation problem: -Two expressions writes to same variable 11/3/2010 University of Utah 26

Code Generator Augmented with Static Analyzer Start Generate Block Generate Function Generate Statement Pointer Analysis Side Effect Analysis Code Validation End Generate Expression University of Utah

Future work For us: Create a turnkey system : Test harness needs a partial rewrite (7000 lines of Perl) Improve test case reduction Support more C features Support more languages and formats (including IR) For you: Please keep fixing bugs we report Tell us where random testing might be useful 11/3/2010 University of Utah 28

Conclusion Random testing is powerful Quickly found bugs in all compilers we tested Found deep optimization bugs unlikely to be uncovered by other means Can generate small test cases pinpoint the problem But has limitations: Never know when to stop testing Coverage is not broad enough Compilers need random testing Fixed test suite is not enough 11/3/2010 University of Utah 29

Thank you And thank Qualcomm for sponsoring my trip! Questions? 11/3/2010 University of Utah 30

Test Case Reduction Is Necessary for - Bug reporting - Bug fixing - Allow causal users find and report bugs using our tool Existing approach: Delta Debugging - Repeatedly remove part of the program and see if it remains interesting - Works well for crash bugs -Works poorly for wrong code bugs - Problem: Throwing away part of a program may introduce undefined behavior University of Utah 11/3/2010 31

Hierarchical Reduction Function Reduction Csmith Block Reduction Statement Reduction Expression Reduction Runtime Verification and Feedback Variable Reduction Initial results show reduced size of failure-inducing test cases by 93% on average in a few minutes University of Utah 11/3/2010 32