Dynamic Program Analysis Xiangyu Zhang. 2 CS510 S o f t w a r e E n g i n e e r i n g Introduction Dynamic program analysis is to solve problems regarding.

Slides:



Advertisements
Similar presentations
Information Retrieval in Practice
Advertisements

Saumya Debray The University of Arizona Tucson, AZ
Introduction to Computer Science 2 Lecture 7: Extended binary trees
Michael Alves, Patrick Dugan, Robert Daniels, Carlos Vicuna
File Processing - Organizing file for Performance MVNC1 Organizing Files for Performance Chapter 6 Jim Skon.
Program Slicing. 2 CS510 S o f t w a r e E n g i n e e r i n g Outline What is slicing? Why use slicing? Static slicing of programs Dynamic Program Slicing.
Program Slicing Mark Weiser and Precise Dynamic Slicing Algorithms Xiangyu Zhang, Rajiv Gupta & Youtao Zhang Presented by Harini Ramaprasad.
Presented By: Krishna Balasubramanian
© 2006 Barton P. MillerFebruary 2006Binary Code Analysis and Editing A Framework for Binary Code Analysis, and Static and Dynamic Patching Barton P. Miller.
Greedy Algorithms (Huffman Coding)
Data Compressor---Huffman Encoding and Decoding. Huffman Encoding Compression Typically, in files and messages, Each character requires 1 byte or 8 bits.
1 S. Tallam, R. Gupta, and X. Zhang PACT 2005 Extended Whole Program Paths Sriraman Tallam Rajiv Gupta Xiangyu Zhang University of Arizona.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming The software development method algorithms.
Program Representations Xiangyu Zhang. CS590F Software Reliability Why Program Representations  Initial representations Source code (across languages).
Wish Branches A Review of “Wish Branches: Enabling Adaptive and Aggressive Predicated Execution” Russell Dodd - October 24, 2006.
Tools and Implementation Xiangyu Zhang. CS590F Software Reliability Outline  Dynamic analysis tools  Binary Decision Diagram  Tools for undeterministic.
Memory Management 1 CS502 Spring 2006 Memory Management CS-502 Spring 2006.
Program Representations Xiangyu Zhang. CS590Z Software Defect Analysis Program Representations  Static program representations Abstract syntax tree;
CS-3013 & CS-502, Summer 2006 Memory Management1 CS-3013 & CS-502 Summer 2006.
CS 206 Introduction to Computer Science II 12 / 10 / 2008 Instructor: Michael Eckmann.
Efficient Instruction Set Randomization Using Software Dynamic Translation Michael Crane Wei Hu.
VPC3: A Fast and Effective Trace-Compression Algorithm Martin Burtscher.
1 Chapter-01 Introduction to Computers and C++ Programming.
An Introduction Chapter Chapter 1 Introduction2 Computer Systems  Programmable machines  Hardware + Software (program) HardwareProgram.
Software (Program) Analysis. Automated Static Analysis Static analyzers are software tools for source text processing They parse the program text and.
Chapter 1 Introduction Dr. Frank Lee. 1.1 Why Study Compiler? To write more efficient code in a high-level language To provide solid foundation in parsing.
Software Overview. Why review software? Software is the set of instructions that tells hardware what to do The reason for hardware is to execute a program.
Interpretation Environments and Evaluation. CS 354 Spring Translation Stages Lexical analysis (scanning) Parsing –Recognizing –Building parse tree.
Chapter 1 Introduction Chapter 1 Introduction 1 st Semester 2015 CSC 1101 Computer Programming-1.
Deriving Input Syntactic Structure From Execution Zhiqiang Lin Xiangyu Zhang Purdue University November 11 th, 2008 The 16th ACM SIGSOFT International.
Introduction to Compilers. Related Area Programming languages Machine architecture Language theory Algorithms Data structures Operating systems Software.
Software Engineering Laboratory, Department of Computer Science, Graduate School of Information Science and Technology, Osaka University IWPSE 2003 Program.
An Undergraduate Course on Software Bug Detection Tools and Techniques Eric Larson Seattle University March 3, 2006.
CS654: Digital Image Analysis Lecture 34: Different Coding Techniques.
Virtual Application Profiler (VAPP) Problem – Increasing hardware complexity – Programmers need to understand interactions between architecture and their.
CS412/413 Introduction to Compilers Radu Rugina Lecture 18: Control Flow Graphs 29 Feb 02.
High Performance Embedded Computing © 2007 Elsevier Lecture 7: Memory Systems & Code Compression Embedded Computing Systems Mikko Lipasti, adapted from.
Compiler Construction CPCS302 Dr. Manal Abdulaziz.
Text TCS INTERNAL Oracle PL/SQL – Introduction. TCS INTERNAL PL SQL Introduction PLSQL means Procedural Language extension of SQL. PLSQL is a database.
Prof. Paolo Ferragina, Algoritmi per "Information Retrieval" Basics
Presented by : A best website designer company. Chapter 1 Introduction Prof Chung. 1.
Hello world !!! ASCII representation of hello.c.
Review A program is… a set of instructions that tell a computer what to do. Programs can also be called… software. Hardware refers to… the physical components.
Introduction to Computer Programming Concepts M. Uyguroğlu R. Uyguroğlu.
Operating Systems A Biswas, Dept. of Information Technology.
IA-64 Architecture Muammer YÜZÜGÜLDÜ CMPE /12/2004.
©SoftMoore ConsultingSlide 1 Code Optimization. ©SoftMoore ConsultingSlide 2 Code Optimization Code generation techniques and transformations that result.
Qin Zhao1, Joon Edward Sim2, WengFai Wong1,2 1SingaporeMIT Alliance 2Department of Computer Science National University of Singapore
Dynamic Instrumentation - Valgrind  Developed by Julian Seward at/around Cambridge University,UK  Google-O'Reilly Open Source Award for "Best Toolmaker"
University of Maryland Baltimore County
Chapter 1 Introduction.
Key Ideas from day 1 slides
Program Representation
Compiler Construction (CS-636)
CS5100 Advanced Computer Architecture Advanced Branch Prediction
A Case of Dynamic Program Analysis
Chapter 1 Introduction.
课程名 编译原理 Compiling Techniques
Optimization Code Optimization ©SoftMoore Consulting.
White-Box Testing.
Dynamic Instrumentation - Valgrind
Page Replacement.
Dynamic Program Analysis
White-Box Testing.
Hyesoon Kim Onur Mutlu Jared Stark* Yale N. Patt
* From AMD 1996 Publication #18522 Revision E
Loop-Level Parallelism
Computer Systems An Introducton.
CSc 453 Interpreters & Interpretation
Dynamic Binary Translators and Instrumenters
Presentation transcript:

Dynamic Program Analysis Xiangyu Zhang

2 CS510 S o f t w a r e E n g i n e e r i n g Introduction Dynamic program analysis is to solve problems regarding software dependability and productivity by inspecting software execution. Program executions vs. programs Not all statements are executed; one statement may be executed many times. Analysis on a single path – the executed path All variables are instantiated (solving the aliasing problem) Resulting in: Relatively lower learning curve. Precision. Applicability. Scalability. Dynamic program analysis can be constructed from a set of primitives Tracing Profiling Checkpointing and replay Dynamic slicing Execution indexing Delta debugging Applications Dynamic information flow tracking Automated debugging

Program Tracing

4 CS510 S o f t w a r e E n g i n e e r i n g Outline What is tracing. Why tracing. How to trace. Reducing trace size.

5 CS510 S o f t w a r e E n g i n e e r i n g What is Tracing Tracing is a process that faithfully records detailed information of program execution (lossless). Control flow tracing the sequence of executed statements. Dependence tracing the sequence of exercised dependences. Value tracing the sequence of values that are produced by each instruction. Memory access tracing the sequence of memory references during an execution The most basic primitive.

6 CS510 S o f t w a r e E n g i n e e r i n g Why Tracing Debugging Enables time travel to understand what has happened. Code optimizations Identify hot program paths; Data compression; Value speculation; Data locality that help cache design; Security Malware analysis Testing Coverage.

7 CS510 S o f t w a r e E n g i n e e r i n g Outline What is tracing. Why tracing. How to trace. Reducing trace size. Trace accessibility

8 CS510 S o f t w a r e E n g i n e e r i n g Tracing by Printf Max = 0; for (p = head; p; p = p->next) { if (p->value > max) { max = p->value; } printf(“In loop\n”); printf(“True branch\n”);

9 CS510 S o f t w a r e E n g i n e e r i n g Tracing by Source Level Instrumentation Read a source file and parse it into ASTs. Annotate the parse trees with instrumentation. Translate the annotated trees to a new source file. Compile the new source. Execute the program and a trace produced.

10 CS510 S o f t w a r e E n g i n e e r i n g An Example

11 CS510 S o f t w a r e E n g i n e e r i n g An Example ; printf(“In loop\n”)

12 CS510 S o f t w a r e E n g i n e e r i n g Limitations of Source Level Instrumentation Hard to handle libraries. Proprietary libraries: communication (MPI, PVM), linear algebra (NGA), database query (SQL libraries). Hard to handle multi-lingual programs Source code level instrumentation is heavily language dependent. Requires source code Worms and viruses are rarely provided with source code

13 CS510 S o f t w a r e E n g i n e e r i n g Tracing by Binary Instrumentation What is binary instrumentation Given a binary executable, parses it into intermediate representation. More advanced representations such as control flow graphs may also be generated. Tracing instrumentation is added to the intermediate representation. A lightweight compiler compiles the instrumented representation into a new executable. Features No source code requirement Easily handle libraries.

14 CS510 S o f t w a r e E n g i n e e r i n g Dynamic Instrumentation - Valgrind Developed by Julian Seward at Cambridge University. Google-O'Reilly Open Source Award for "Best Toolmaker" 2006 A merit (bronze) Open Source Award 2004 Open source works on x86, AMD64 Easy to execute, e.g.: valgrind --tool=memcheck ls It becomes very popular One of the two most popular dynamic instrumentation tools Pin and Valgrind Very good usability, extendibility, robust 25MLOC Mozilla, MIT, CMU-security, Me, and many other places Overhead is the problem 5-10X slowdown without any instrumentation Reading assignment Valgrind: A Framework for Heavyweight Dynamic Binary Instrumentation (PLDI07)

15 CS510 S o f t w a r e E n g i n e e r i n g Valgrind Infrastructure Binary Code VALGRIND CORE Dispatcher BB Decoder BB Compiler Trampoline Tool 1 Tool 2 Runtime Instrumenter Tool n …… Input pc BB New BB New pc state

16 CS510 S o f t w a r e E n g i n e e r i n g Valgrind Infrastructure Binary Code VALGRIND CORE Dispatcher BB Decoder BB Compiler Trampoline Tool 1 Tool 2 Runtime Instrumenter Tool n …… Input 1 1 1: do { 2: i=i+1; 3: s1; 4: } while (i<2) 5: s2; 1: do { 2: i=i+1; 3: s1; 4: } while (i<2) OUTPUT:

17 CS510 S o f t w a r e E n g i n e e r i n g Valgrind Infrastructure Binary Code VALGRIND CORE Dispatcher BB Decoder BB Compiler Trampoline Tool 1 Tool 2 Runtime Instrumenter Tool n …… Input 1: do { 2: i=i+1; 3: s1; 4: } while (i<2) 5: s2; 1: do { 2: i=i+1; 3: s1; 4: } while (i<2) 1: do { 2: i=i+1; 3: s1; 4: } while (i<2) OUTPUT:

18 CS510 S o f t w a r e E n g i n e e r i n g Valgrind Infrastructure Binary Code VALGRIND CORE Dispatcher BB Decoder BB Compiler Trampoline Tool 1 Tool 2 Runtime Instrumenter Tool n …… Input 1: do { 2: i=i+1; 3: s1; 4: } while (i<2) 5: s2; 1: do { print(“1”) 2: i=i+1; 3: s1; 4: } while (i<2) 1: do { 2: i=i+1; 3: s1; 4: } while (i<2) OUTPUT:

19 CS510 S o f t w a r e E n g i n e e r i n g Valgrind Infrastructure Binary Code VALGRIND CORE Dispatcher BB Decoder BB Compiler Trampoline Tool 1 Tool 2 Runtime Instrumenter Tool n …… Input 1: do { 2: i=i+1; 3: s1; 4: } while (i<2) 5: s2; 1: do { 2: i=i+1; 3: s1; 4: } while (i<2) OUTPUT: 1: do { print(“1”) i=i+1; s1; } while (i<2) 1 1 1

20 CS510 S o f t w a r e E n g i n e e r i n g Valgrind Infrastructure Binary Code VALGRIND CORE Dispatcher BB Decoder BB Compiler Trampoline Tool 1 Tool 2 Runtime Instrumenter Tool n …… Input 1: do { 2: i=i+1; 3: s1; 4: } while (i<2) 5: s2; OUTPUT: 1: do { print(“1”) i=i+1; s1; } while (i<2) : s2;

21 CS510 S o f t w a r e E n g i n e e r i n g Valgrind Infrastructure Binary Code VALGRIND CORE Dispatcher BB Decoder BB Compiler Trampoline Tool 1 Tool 2 Runtime Instrumenter Tool n …… Input 1: do { 2: i=i+1; 3: s1; 4: } while (i<2) 5: s2; 1: do { 2: i=i+1; 3: s1; 4: } while (i<2) OUTPUT: 1: do { print(“1”) i=i+1; s1; } while (i<2) 11 5: print (“5”); s2;

22 CS510 S o f t w a r e E n g i n e e r i n g 5: print (“5”); s2; Valgrind Infrastructure Binary Code VALGRIND CORE Dispatcher BB Decoder BB Compiler Trampoline Tool 1 Tool 2 Runtime Instrumenter Tool n …… Input 1: do { 2: i=i+1; 3: s1; 4: } while (i<2) 5: s2; OUTPUT: 1: do { print(“1”) i=i+1; s1; } while (i<2) 11 5

23 CS510 S o f t w a r e E n g i n e e r i n g Instrumentation with Valgrind UCodeBlock* SK_(instrument)(UCodeBlock* cb_in, …) { … UCodeBlock cb = VG_(setup_UCodeBlock)(…); … for (i = 0; i < VG_(get_num_instrs)(cb_in); i++) { u = VG_(get_instr)(cb_in, i); switch (u->opcode) { case LD: … case ST: … case MOV: … case ADD: … case CALL: … return cb; }

24 CS510 S o f t w a r e E n g i n e e r i n g Outline What is tracing. Why tracing. How to trace. Reducing trace size.

25 CS510 S o f t w a r e E n g i n e e r i n g Fine-Grained Tracing is Expensive Trace(N=6): : sum=0 2: i=1 3: while ( i<N) do 4:i=i+1 5:sum=sum+i endwhile 6: print(sum) 3: while ( i<N) do 1: sum=0 2: i=1 4: i=i+1 5: sum=sum+i 6: print (sum) Space Complexity: 4 bytes * Execution length

26 CS510 S o f t w a r e E n g i n e e r i n g Basic Block Level Tracing Trace(N=6): : sum=0 2: i=1 3: while ( i<N) do 4:i=i+1 5:sum=sum+i endwhile 6: print(sum) 3: while ( i<N) do 1: sum=0 2: i=1 4: i=i+1 5: sum=sum+i 6: print (sum) BB Trace:

27 CS510 S o f t w a r e E n g i n e e r i n g More Ideas Would a function level tracing idea work? A trace entry is a function call with its parameters. Predicate tracing 1: sum=0 2: i=1 3: while ( i<N) do 4:i=i+1 5:sum=sum+i endwhile 6: print(sum) Instruction trace Predicate trace F T F Lose random accessibility Path based tracing

28 CS510 S o f t w a r e E n g i n e e r i n g Compression Using zlib Zlib is a software library used for data compression. It wraps the compression algorithm used in gzip. Divide traces into trunks, and then compress them with zlib. Disadvantage: trace can only be accessed after complete decompression; slow Desired features Accessing traces in their compressed form. Traversing forwards and backwards. fast

29 CS510 S o f t w a r e E n g i n e e r i n g Compression using value predictors Last n values predictor Facilitated by a buffer that stores the last n unique values encountered If the next value is one of the n values, the index of the value (in [0, n-1]) is emitted to the encoded trace, prefixed with a bit 0 to indicate the prediction is correct. Otherwise (mis-prediction), the original value (32 bits) is emitted to the encoded trace, prefixed with a bit 1 to indicate mis-prediction. The buffer is updated with least used strategy. Example: use last-2 predictor (underlined are 32 bits)

30 CS510 S o f t w a r e E n g i n e e r i n g Compression using value predictors Decompression Take one bit from the encoded trace, if it is 1, emit the next 32 bits. If it is 0, emit the value in the buffer indexed by the next log n bits. Maintain the table in the same way as compression

31 CS510 S o f t w a r e E n g i n e e r i n g Compression using value predictors Finite Context Method (FCM) Facilitated by a look up table that predicts a value based on the context of left n values. 2-FCM, 3-FCM If the next value can be found in the table through its left context, a bit 0 is emitted to the encoded trace. Otherwise (mis-prediction), the original value (32 bits) is emitted to the encoded trace, prefixed with a bit 1 to indicate mis-prediction. The lookup table is updated accordingly. Example: … … (underlined are 32 bits)

32 CS510 S o f t w a r e E n g i n e e r i n g Compression using value predictors Decompression Take one bit from the encoded trace, if it is 1, emit the next 32 bits. If it is 0, emit the value looked up from the table using the left n values. Maintain the table in the same way as compression

33 CS510 S o f t w a r e E n g i n e e r i n g Compression using value predictors FCM (finite context method). Example, FCM-3 X Y ZA A 1 Compressed UncompressedLeft Context lookup table

34 CS510 S o f t w a r e E n g i n e e r i n g Compression using value predictors FCM (finite context method). Example, FCM-3 X Y Z A B 0B Compressed Uncompressed Length(Compressed) = n/32 + n*(1- prediction rate) Left Context lookup table It was shown that predictors are better than zlib; It works so well because the repetitive pattern caused by loops; Only forward traversable; X Y ZA B

35 CS510 S o f t w a r e E n g i n e e r i n g Enable bidirectional traversal Forward compressed, backward decompressed FCM Traditional FCM is forward compressed, forward decompressed A Left Context lookup table X Y ZA XY Z A Right Context lookup table X Y Z Compressed Uncompressed X Y Z Y Z A1  Bidirectional FCM Right Context lookup table Left Context lookup table X Y Z Uncompressed current context

36 CS510 S o f t w a r e E n g i n e e r i n g Bidirectional FCM - example X Y Z11 A X YZ AX Y Z Right Context lookup table A X Y Z1 Left Context lookup table A X Y1

37 CS510 S o f t w a r e E n g i n e e r i n g Characteristics of bidirectional predictors High compression rate The compression rate is nearly the SAME as unidirectional predictors; Fast compression and de-compression Roughly TWO times slower than unidirectional predictors;

38 CS510 S o f t w a r e E n g i n e e r i n g Tracing On Virtual Machine A virtual machine is a platform that supports the execution of a guest operation system. A guest operation system runs on VM. Applications run on the guest operation system Tracing on VM As each executed instruction of an application has to go through the VM, VM has the view of every detail of execution. System calls, communication, scheduler Particularly useful for security oriented tracing. The working mechanism is very similar to Valgrind QEMU

39 CS510 S o f t w a r e E n g i n e e r i n g Challenge A malware often features an encrypted code body and a decryption engine. Given an executable with an embedded malicious code piece, sketch a plan to acquire the plain text of the malware code body (one extra credit, please make it less than 1 page).