Simple example. Analysis of programs with pointers. Program model. Points-to relation

Similar documents
Simple example. Analysis of programs with pointers. Points-to relation. Program model. Points-to graph. Ordering on points-to relation

Points-to Analysis. Xiaokang Qiu Purdue University. November 16, ECE 468 Adapted from Kulkarni 2012

Introduction to Shape and Pointer Analysis

Classes. Compiling Methods. Code Generation for Objects. Implementing Objects. Methods. Fields

Context-Sensitive Pointer Analysis. Recall Context Sensitivity. Partial Transfer Functions [Wilson et. al. 95] Emami 1994

0 COORDINATE GEOMETRY

Variables and Bindings

Compiler construction

CS711 Advanced Programming Languages Pointer Analysis Overview and Flow-Sensitive Analysis

Alias Analysis. Last time Interprocedural analysis. Today Intro to alias analysis (pointer analysis) CS553 Lecture Alias Analysis I 1

Static Program Analysis Part 9 pointer analysis. Anders Møller & Michael I. Schwartzbach Computer Science, Aarhus University

Register Allocation 3/16/11. What a Smart Allocator Needs to Do. Global Register Allocation. Global Register Allocation. Outline.

Name Class Date. subtract 3 from each side. w 5z z 5 2 w p - 9 = = 15 + k = 10m. 10. n =

Intermediate Algebra. Gregg Waterman Oregon Institute of Technology

Object Oriented Languages. Hwansoo Han

Roberto s Notes on Differential Calculus Chapter 8: Graphical analysis Section 5. Graph sketching

Linear Programming. Revised Simplex Method, Duality of LP problems and Sensitivity analysis

CS S-11 Memory Management 1

Determining the 2d transformation that brings one image into alignment (registers it) with another. And

L3 Rigid Motion Transformations 3.1 Sequences of Transformations Per Date

Using the same procedure that was used in Project 5, enter matrix A and its label into an Excel spreadsheet:

Find the Relationship: An Exercise in Graphical Analysis

Lecture 14 Pointer Analysis

Unit 5 Lesson 2 Investigation 1

Lecture 20 Pointer Analysis

What are the rules of elementary algebra

2.2 Differential Forms

ACTIVITY 9 Continued Lesson 9-2

Math 1050 Lab Activity: Graphing Transformations

Winter Compiler Construction T9 IR part 2 + Runtime organization. Announcements. Today. Know thy group s code

A Formal Definition of Limit

Implicit differentiation

Lecture 27. Pros and Cons of Pointers. Basics Design Options Pointer Analysis Algorithms Pointer Analysis Using BDDs Probabilistic Pointer Analysis

EECS1022 Winter 2018 Additional Notes Tracing Point, PointCollector, and PointCollectorTester

Rational Functions with Removable Discontinuities

Sample Problems for Quiz # 3

Lecture Notes: Pointer Analysis

Announcements. CSCI 334: Principles of Programming Languages. Lecture 19: C++

Monday, September 28, 2015

Laurie s Notes. Overview of Section 6.3

B + -trees. Kerttu Pollari-Malmi

Scale Invariant Feature Transform (SIFT) CS 763 Ajit Rajwade

COMP 524 Spring 2018 Midterm Thursday, March 1

Optional: Building a processor from scratch

Systems of Linear Equations

Answers. Chapter 4. Cumulative Review Chapters 1 3, pp Chapter Self-Test, p Getting Started, p a) 49 c) e)

Graphs and Functions

Programming Language Concepts Scoping. Janyl Jumadinova January 31, 2017

EELE 482 Lab #3. Lab #3. Diffraction. 1. Pre-Lab Activity Introduction Diffraction Grating Measure the Width of Your Hair 5

Chapter 5 Names, Binding, Type Checking and Scopes

1.5 LIMITS. The Limit of a Function

LINEAR PROGRAMMING. Straight line graphs LESSON

Lecture 12 Date:

Module 3 Graphing and Optimization

Building up a language SICP Variations on a Scheme. Meval. The Core Evaluator. Eval. Apply. 2. syntax procedures. 1.

CHECK Your Understanding

Chapter Goals: Evaluate limits. Evaluate one-sided limits. Understand the concepts of continuity and differentiability and their relationship.

3x 4y 2. 3y 4. Math 65 Weekly Activity 1 (50 points) Name: Simplify the following expressions. Make sure to use the = symbol appropriately.

Bias-Variance Decomposition Error Estimators

Bias-Variance Decomposition Error Estimators Cross-Validation

Transformations of Functions. 1. Shifting, reflecting, and stretching graphs Symmetry of functions and equations

LESSON 3.1 INTRODUCTION TO GRAPHING

Data, memory. Pointers and Dynamic Variables. Example. Pointers Variables (or Pointers) Fall 2018, CS2

A Context-Sensitive Memory Model for Verification of C/C++ Programs

CO444H. Ben Livshits. Datalog Pointer analysis

2.2 Absolute Value Functions

Wednesday, February 19, 2014

Binary Trees. Binary Search Trees

Transformations of Functions. Shifting Graphs. Similarly, you can obtain the graph of. g x x 2 2 f x 2. Vertical and Horizontal Shifts

An Introduction to Heap Analysis. Pietro Ferrara. Chair of Programming Methodology ETH Zurich, Switzerland

12.4 The Ellipse. Standard Form of an Ellipse Centered at (0, 0) (0, b) (0, -b) center

x Check: p. C) 32 8k D) 3t 15

EXAMPLE A {(1, 2), (2, 4), (3, 6), (4, 8)}

Integrated Algebra A Notes/Homework Packet 9

1.1 Horizontal & Vertical Translations

The code generator must statically assign a location in the AR for each temporary add $a0 $t1 $a0 ; $a0 = e 1 + e 2 addiu $sp $sp 4 ; adjust $sp (!

Modeling and Simulation Exam

Week 27 Algebra 1 Assignment:

Typical Compiler. Ahead- of- time compiler. Compilers... that target interpreters. Interpreter 12/9/15. compile time. run time

Introduction to C Language (M3-R )

Graph Linear Equations

Introduction to Algorithms Massachusetts Institute of Technology Professors Erik D. Demaine and Charles E. Leiserson.

2-1. The Language of Functions. Vocabulary

Matrix Representations

Heap Arrays and Linked Lists. Steven R. Bagley

Transformations of y = x 2

6-1: Solving Systems by Graphing

Recap. Recap. If-then-else expressions. If-then-else expressions. If-then-else expressions. If-then-else expressions

Building Interpreters

Section 4.2 Graphing Lines

Polynomial and Rational Functions

9. f(x) = x f(x) = x g(x) = 2x g(x) = 5 2x. 13. h(x) = 1 3x. 14. h(x) = 2x f(x) = x x. 16.

Section 1.4: Graphing Calculators and Computers

Scene Graphs & Modeling Transformations COS 426

Project 1 Part II: Metaproduct Primes

3.5 Rational Functions

Appendix A.6 Functions

Chap 7, 2009 Spring Yeong Gil Shin

Lecture 16 Pointer Analysis

COP 3223 Introduction to Programming with C - Study Union - Fall 2017

Transcription:

Simple eample Analsis of programs with pointers := 5 ptr := & *ptr := 9 := program S1 S2 S3 S4 What are the defs and uses of in this program? Problem: just looking at variable names will not give ou the correct information After statement S2, program names and *ptr are both epressions that refer to the same memor location (aliases) We sa that ptr points-to after statement S2. In a C-like language that has pointers, we must know the points-to relation to be able to determine defs and uses correctl For now, onl tpes are int, int*, int** etc. No heap All pointers point to onl to stack variables No procedure or function calls Statements involving pointer variables: address: := & cop: := load: := * store: * := Arbitrar computations involving ints Program model a c a c a c a c b d b d b d b d initial state = =* *= Points-to relation Directed graph: nodes are program variables edge (a,b): variable a points-to variable b ptr Can use a special node to represent NULL Points-to relation is different at different program points 1

Points-to graph Out-degree of node ma be more than one if points-to graph has edges (a,b) and (a,c), it means that variable a ma point to either b or c depending on how we got to that point, one or the other will be true path-sensitive analses: track how ou got to a program point (we will not do this) if (p) then := & else := &z.. := & p := &z Ordering on points-to relation Subset ordering: for a given set of variables Least element is graph with no edges 1 <= 2 if 2 has all the edges 1 has and mabe some more iven two points-to relations 1 and 2 1 U 2: least graph that contains all the edges in 1 and in 2 What does point to here? Overview We will look at three different points-to analses. Flow-sensitive points-to analsis Dataflow analsis Computes a different points-to relation at each point in program Flow-insensitive points-to analsis Computes a single points-to graph for entire program Andersen s algorithm Natural simplification of flow-sensitive algorithm Steensgard s algorithm Nodes in tree are equivalence classes of variables if ma point-to either or z, put and z in the same equivalence class Points-to relation is a tree with edges from children to parents rather than a general graph Less precise than Andersen s algorithm but faster := &z ptr := & := &w ptr := & Eample ptr z w ptr z w Andersen s algorithm ptr, Flow-sensitive algorithm z,w Steensgard s algorithm 2

Notation Suppose S and S1 are set-d variables. S S1: strong update set assignment S U S1: weak update set union: this is like S S U S1 Flow-sensitive algorithm Dataflow equations Dataflow equations (contd.) Forward flow, an path analsis Confluence operator: 1 U 2 Statements := & := = with pt () { = with pt () ) := * * := = with pt () U a) for all a in ) = with pt (a) U) for all a in ) := & = with pt () { := = with pt () ) strong updates := * * := = with pt () U a) for all a in ) = with pt (a) U) for all a in ) weak update (wh?) 3

Strong vs. weak updates Strong update: At assignment statement, ou know precisel which variable is being written to Eample: :=. You can remove points-to information about coming into the statement in the dataflow analsis. Weak update: You do not know precisel which variable is being updated; onl that it is one among some set of variables. Eample: * := Problem: at analsis time, ou ma not know which variable points to (see slide on control-flow and out-degree of nodes) Refinement: if out-degree of in points-to graph is 1 and is known not be nil, we can do a strong update even for * := Structures Structure tpes struct cell {int ; struct cell *left, *right; struct cell,; Use a field-sensitive model and are nodes each node has three internal fields labeled, left, right This representation permits pointers into fields of structures If this is not necessar, we can simpl have a node for each structure and label outgoing edges with field name int main(void) { struct cell {int ; struct cell *net; ; struct cell,,z,*p; int sum;. = 5;.net = &;. = 6;.net = &z; z. = 7; z.net = NULL; p = &; sum = 0; while (p!= NULL) { sum = sum + (*p).; p = (*p).net; return sum; Eample net p net p net z net NULL net z net NULL Flow-insensitive algorithms 4

Flow-insensitive analsis Andersen s algorithm Flow-sensitive analsis computes a different graph at each program point. This can be quite epensive. One alternative: flow-insensitive analsis Intuition:compute a points-to relation which is the least upper bound of all the points-to relations computed b the flowsensitive analsis Approach: Ignore control-flow Consider all assignment statements together replace strong updates in dataflow equations with weak updates Compute a single points-to relation that holds regardless of the order in which assignment statements are actuall eecuted Statements := & = with ) U { := = with ) U) := * * := = with ) Ua) for all a in ) weak updates onl = with a) U) for all a in ) int main(void) { struct cell {int ; struct cell *net; ; struct cell,,z,*p; int sum;. = 5;.net = &;. = 6;.net = &z; z. = 7; z.net = NULL; p = &; sum = 0; while (p!= NULL) { sum = sum + (*p).; p = (*p).net; return sum; Eample.net = &;.net = &z; z.net = NULL; p = &; p = (*p).net; Assignments for flow-insensitive analsis... Solution to flow-insensitive equations net p z net net NULL - Compare with points-to graphs for flow-sensitive solution - Wh does p point-to NULL in this graph? 5

Andersen s algorithm formulated using set constraints Steensgard s algorithm Statements pt : var 2 var := & ) := ) ) := * a ). ) a) * := a ). a) ) Flow-insensitive Computes a points-to graph in which there is no fan-out In points-to graph produced b Andersen s algorithm, if points-to and z, and z are collapsed into an equivalence class Less accurate than Andersen s but faster We can eploit this to design an O(N* (N)) algorithm, where N is the number of statements in the program. Statements Steensgard s algorithm using set constraints pt : var 2 var No fan-out., z ). ) z) Trick for one-pass processing Consider the following equations ) ) dumm ) z ) ) ) z ) := & ) := ) ) := * a ). ) a) * := a ). a) ) When first equation on left is processed, and are not pointing to anthing. Once second equation is processed, we need to go back and reprocess first equation. Trick to avoid doing this: when processing first equation, if and are not pointing to anthing, create a dumm node and make and point to that this is like solving the sstem on the right It is eas to show that this avoids the need for revisiting equations. 6

Algorithm Auiliar methods Can be implemented in single pass through program Algorithm uses union-find to maintain equivalence classes (sets) of nodes Points-to relation is implemented as a pointer from a variable to a representative of a set Basic operations for union find: rep(v): find the node that is the representative of the set that v is in union(v1,v2): create a set containing elements in sets containing v1 and v2, and return representative of that set class var { //instance variables points_to: var; name: string; //constructor; also creates singleton set in union-find data structure var(string); //class method; also creates singleton set in union-find data structure make-dumm-var():var; //instance methods get_): var; set_var);//updates rep rec_union(var v1, var v2) { p1 = rep(v1)); p2 = rep(v2)); t1 = union(rep(v1), rep(v2)); if (p1 == p2) return; else if (p1!= null && p2!= null) t2 = rec_union(p1, p2); else if (p1!= null) t2 = p1; else if (p2!= null) t2 = p2; else t2 = null; t1.set_t2); return t1; var v) { //v does not have to be representative t = rep(v); return t.get_); //alwas returns a representative element Algorithm Initialization: make each program variable into an object of tpe var and enter object into union-find data structure for each statement S in the program do S is := &: {if () == null).set-rep()); else rec-union(),); S is := : {if () == null and ) == null).set-var.make-dumm-var());.set-rec-union(),))); S is := *:{if () == null).set-var.make-dumm-var()); var a := ); if(a) == null) a.set-var.make-dumm-var());.set-rec-union(),a))); S is * := :{if () == null).set-var.make-dumm-var()); var a := ); if(a) == null) a.set-var.make-dumm-var());.set-rec-union(),a))); Inter-procedural analsis What do we do if there are function calls? 1 = &a 1 = &b swap(1, 1) swap (p1, p2) { 2 = &a 2 = &b swap(2, 2) 7

Two approaches Contet-sensitive approach: treat each function call separatel just like real program eecution would problem: what do we do for recursive functions? need to approimate Contet-insensitive approach: merge information from all call sites of a particular function in effect, inter-procedural analsis problem is reduced to intra-procedural analsis problem Contet-sensitive approach is obviousl more accurate but also more epensive to compute Contet-insensitive approach 1 = &a 1 = &b swap(1, 1) swap (p1, p2) { 2 = &a 2 = &b swap(2, 2) Contet-sensitive approach Contet-insensitive/Flowinsensitive Analsis 1 = &a 1 = &b swap(1, 1) swap (p1, p2) { 2 = &a 2 = &b swap(2, 2) swap (p1, p2) { For now, assume we do not have function parameters this means we know all the call sites for a given function Set up equations for binding of actual and formal parameters at each call site for that function use same variables for formal parameters for all call sites Intuition: each invocation provides a new set of constraints to formal parameters 8

Swap eample Heap allocation 1 = &a 1 = &b p1 = 1 p2 = 1 2 = &a 2 = &b p1 = 2 p2 = 2 Simplest solution: use one node in points-to graph to represent all heap cells More elaborate solution: use a different node for each malloc site in the program Even more elaborate solution: shape analsis goal: summarize potentiall infinite data structures but keep around enough information so we can disambiguate pointers from stack into the heap, if possible Summar Less precise More precise Equalit-based Subset-based Histor of points-to analsis Flow-insensitive Flow-sensitive Contet-insensitive Contet-sensitive No consensus about which technique to use Eperience: if ou are contet-insensitive, ou might as well be flow-insensitive from Rder and Raside 9