Program Analysis. Uday Khedker. ( uday) Department of Computer Science and Engineering, Indian Institute of Technology, Bombay

Similar documents
Heap, Variables, References, and Garbage. CS152. Chris Pollett. Oct. 13, 2008.

Garbage Collection. Steven R. Bagley

Sustainable Memory Use Allocation & (Implicit) Deallocation (mostly in Java)

Contents. 8-1 Copyright (c) N. Afshartous

Example. program sort; var a : array[0..10] of integer; procedure readarray; : function partition (y, z :integer) :integer; var i, j,x, v :integer; :

Lecture 14. No in-class files today. Homework 7 (due on Wednesday) and Project 3 (due in 10 days) posted. Questions?

Compiler Construction

Run-Time Environments/Garbage Collection

CMSC 330: Organization of Programming Languages

Compiler Construction

CS558 Programming Languages

Run-time Environments - 3

CMSC 330: Organization of Programming Languages

Lecture 13: Garbage Collection

Runtime. The optimized program is ready to run What sorts of facilities are available at runtime

Heap Reference Analysis Using Access Graphs

CS 536 Introduction to Programming Languages and Compilers Charles N. Fischer Lecture 11

CS558 Programming Languages

CMSC 330: Organization of Programming Languages. Memory Management and Garbage Collection

Programming Language Implementation

Item 18: Implement the Standard Dispose Pattern

Quantifying the Performance of Garbage Collection vs. Explicit Memory Management

Run-time Environments -Part 3


Acknowledgements These slides are based on Kathryn McKinley s slides on garbage collection as well as E Christopher Lewis s slides

Programmer Directed GC for C++ Michael Spertus N2286= April 16, 2007

G Programming Languages - Fall 2012

Lecture 15 Garbage Collection

LANGUAGE RUNTIME NON-VOLATILE RAM AWARE SWAPPING

Manual Allocation. CS 1622: Garbage Collection. Example 1. Memory Leaks. Example 3. Example 2 11/26/2012. Jonathan Misurda

Garbage Collection. Akim D le, Etienne Renault, Roland Levillain. May 15, CCMP2 Garbage Collection May 15, / 35

CS 241 Honors Memory

Garbage Collection (1)

6.172 Performance Engineering of Software Systems Spring Lecture 9. P after. Figure 1: A diagram of the stack (Image by MIT OpenCourseWare.

Space Efficient Conservative Garbage Collection

Managed runtimes & garbage collection. CSE 6341 Some slides by Kathryn McKinley

Heap Management. Heap Allocation

Structure of Programming Languages Lecture 10

CS558 Programming Languages

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

Habanero Extreme Scale Software Research Project

Managed runtimes & garbage collection

Run-time Environments -Part 1

Estimating the Impact of Heap Liveness Information on Space Consumption in Java

Name, Scope, and Binding. Outline [1]

Deallocation Mechanisms. User-controlled Deallocation. Automatic Garbage Collection

Lecture Notes on Garbage Collection

About These Slides. GDFA: Generic Data Flow Analyser for GCC. Part 1. Copyright. Outline. Uday Khedker

CS 345. Garbage Collection. Vitaly Shmatikov. slide 1

CS61C : Machine Structures

Garbage Collection. Hwansoo Han

Autonomous Garbage Collection: Resolve Memory

Run-time Environments - 2

Pointers II. Class 31

Lecture 7: Binding Time and Storage

Exploiting the Behavior of Generational Garbage Collector

CS61C : Machine Structures

1. Mark-and-Sweep Garbage Collection

The issues. Programming in C++ Common storage modes. Static storage in C++ Session 8 Memory Management

MEMORY MANAGEMENT HEAP, STACK AND GARBAGE COLLECTION

CSE 100: C++ TEMPLATES AND ITERATORS

Procedure and Function Calls, Part II. Comp 412 COMP 412 FALL Chapter 6 in EaC2e. target code. source code Front End Optimizer Back End

Project. there are a couple of 3 person teams. a new drop with new type checking is coming. regroup or see me or forever hold your peace

Lecture Notes on Garbage Collection

Memory Allocation. Static Allocation. Dynamic Allocation. Dynamic Storage Allocation. CS 414: Operating Systems Spring 2008

Storage. Outline. Variables and updating. Copy vs. Ref semantics Lifetime. Dangling References Garbage collection

Advanced Programming & C++ Language

Storage. Outline. Variables and Updating. Composite Variables. Storables Lifetime : Programming Languages. Course slides - Storage

JVM Troubleshooting MOOC: Troubleshooting Memory Issues in Java Applications

CS 4120 Lecture 37 Memory Management 28 November 2011 Lecturer: Andrew Myers

CS201 Some Important Definitions

Qualifying Exam in Programming Languages and Compilers

Memory Allocation III

Memory Management. Memory Management... Memory Management... Interface to Dynamic allocation

Q1: /8 Q2: /30 Q3: /30 Q4: /32. Total: /100

CSC 1600 Memory Layout for Unix Processes"

One-Slide Summary. Lecture Outine. Automatic Memory Management #1. Why Automatic Memory Management? Garbage Collection.

StackVsHeap SPL/2010 SPL/20

Principles of Programming Languages Topic: Scope and Memory Professor Louis Steinberg Fall 2004

Short Notes of CS201

Lecture Notes on Memory Layout

Proposal to Acknowledge that Garbage Collection for C++ is Possible X3J16/ WG21/N0932. Bjarne Stroustrup. AT&T Research.

CS201 - Introduction to Programming Glossary By

CPSC 213. Introduction to Computer Systems. Instance Variables and Dynamic Allocation. Unit 1c

CS162 - POINTERS. Lecture: Pointers and Dynamic Memory

CA341 - Comparative Programming Languages

Data Structure Series

CS 430 Spring Mike Lam, Professor. Data Types and Type Checking

Garbage Collection Techniques

Lecture 7 More Memory Management Slab Allocator. Slab Allocator

Memory Usage in Programs

Memory Allocation III

CSE 307: Principles of Programming Languages

CPSC 213. Introduction to Computer Systems. Winter Session 2017, Term 2. Unit 1c Jan 24, 26, 29, 31, and Feb 2

I J C S I E International Science Press

ACM Trivia Bowl. Thursday April 3 rd (two days from now) 7pm OLS 001 Snacks and drinks provided All are welcome! I will be there.

Run-time Environments

Run-time Environments

Advances in Programming Languages: Regions

o Code, executable, and process o Main memory vs. virtual memory

Transcription:

Program Analysis (www.cse.iitb.ac.in/ uday) Department of Computer Science and Engineering, Indian Institute of Technology, Bombay January 2008

Part 1 About These Slides

CS 618 Program Analysis: About These Slides 1/19 Copyright These slides are in the series of lectures on CS638 Program Analysis course at. A large part of these slides are based on the following material: 1., Amitabha Sanyal, and Amey Karkare. Heap Reference Analysis Using Access Graphs. ACM Transactions on Programming Languages and Systems, 30(1), Nov 2007. 2., Amitabha Sanyal, and Bageshri Karkare. Data Flow Analysis: Theory and Practice. CRC Press USA. (Under Publication) 3.. Data Flow Analysis. In The Compiler Design Handbook. Editors: Priti Shankar and Y. N. Srikant. CRC Press USA. 2002. (2) is not yet published and the copyrights are reserved by the authors. The copyright for (1) is held by ACM and that for (3) is held by CRC Press USA. These slides may be used only for academic purposes.

CS 618 Program Analysis: About These Slides 2/19 Standard Memory Architecture of Programs Static Data Heap allocation provides the flexibility of Stack Variable Sizes. Data structures can grow or shrink as desired at runtime. (Not bound to the declarations in program.) Heap Code Variable Lifetimes. Data structures can be created and destroyed as desired at runtime. (Not bound to the activations of procedures.)

CS 618 Program Analysis: About These Slides 3/19 Managing Heap Memory Decision 1: When to Allocate? Explicit. Specified in the programs. (eg. Imperative/OO languages) Implicit. Decided by the language processors. (eg. Declarative Languages)

CS 618 Program Analysis: About These Slides 3/19 Managing Heap Memory Decision 1: When to Allocate? Explicit. Specified in the programs. (eg. Imperative/OO languages) Implicit. Decided by the language processors. (eg. Declarative Languages) Decision 2: When to Deallocate? Explicit. Manual Memory Management (eg. C/C++) Implicit. Automatic Memory Management aka Garbage Collection (eg. Java/Declarative languages)

CS 618 Program Analysis: About These Slides 4/19 State of Art in Manual Deallocation Memory leaks 10% to 20% of last development effort goes in plugging leaks Tool assisted manual plugging Purify, Electric Fence, RootCause, GlowCode, yaktest, Leak Tracer, BDW Garbage Collector, mtrace, memwatch, dmalloc etc. All leak detectors are dynamic (and hence specific to execution instances) generate massive reports to be perused by programmers usually do not locate last use but only allocation escaping a call At which program point should a leak be plugged?

CS 618 Program Analysis: About These Slides 5/19 Garbage Collection Automatic Deallocation Retain active data structure. Deallocate inactive data structure. What is an Active Data Structure?

CS 618 Program Analysis: About These Slides 5/19 Garbage Collection Automatic Deallocation Retain active data structure. Deallocate inactive data structure. What is an Active Data Structure? If an object does not have an access path, (i.e. it is unreachable) then its memory can be reclaimed.

CS 618 Program Analysis: About These Slides 5/19 Garbage Collection Automatic Deallocation Retain active data structure. Deallocate inactive data structure. What is an Active Data Structure? If an object does not have an access path, (i.e. it is unreachable) then its memory can be reclaimed. What if an object has an access path, but is not accessed after the given program point?

CS 618 Program Analysis: About These Slides 6/19 What is Garbage? Garbage 1 w = x // x points to m a 2 if (x.data < max) 3 x = x. 4 y = x. 5 z = New class of z 6 y = y. 7 z.sum = x.data + y.data Garbage z x w y Stack p a q b i c f j m Heap d e g h k l n o

CS 618 Program Analysis: About These Slides 6/19 What is Garbage? False Garbage 1 w = x // x points to m a 2 if (x.data < max) 3 x = x. 4 y = x. 5 z = New class of z 6 y = y. 7 z.sum = x.data + y.data Garbage z x w y Stack p a q b i c f j m Heap d e g h k l n o

CS 618 Program Analysis: About These Slides 6/19 What is Garbage? True Garbage 1 w = x // x points to m a 2 if (x.data < max) 3 x = x. 4 y = x. 5 z = New class of z 6 y = y. 7 z.sum = x.data + y.data Garbage z x w y Stack p a q b i c f j m Heap d e g h k l n o

CS 618 Program Analysis: About These Slides 6/19 What is Garbage? Garbage 1 w = x // x points to m a 2 if (x.data < max) 3 x = x. 4 y = x. 5 z = New class of z 6 y = y. 7 z.sum = x.data + y.data Garbage z x w y Stack p a q b i c f j m Heap d e g h k l n o All white nodes are unused and should be considered garbage

CS 618 Program Analysis: About These Slides 7/19 Is Reachable Same as Live? From www.memorymanagement.org/glossary live (also known as alive, active) : Memory(2) or an object is live if the program will read from it in future. The term is often used more broadly to mean reachable. It is not possible, in general, for garbage collectors to determine exactly which objects are still live. Instead, they use some approximation to detect objects that are provably dead, such as those that are not reachable. Similar terms: reachable. Opposites: dead. See also: undead.

CS 618 Program Analysis: About These Slides 8/19 Is Reachable Same as Live? Not really. Most of us know that. Even with the state of art of garbage collection, 24% to 76% unused memory remains unclaimed Yet we have no way of distinguishing. Over a dozen reported approaches (since 1996), no real success.

CS 618 Program Analysis: About These Slides 9/19 Cedar Mesa Folk Wisdom Make the unused memory unreachable by setting references to NULL. (GC FAQ: http://www.iecc.com/gclist/gc-harder.html) z x w y p a q b i X X c f j m d e g h k l n o Stack Heap

CS 618 Program Analysis: About These Slides 9/19 Cedar Mesa Folk Wisdom Make the unused memory unreachable by setting references to NULL. (GC FAQ: http://www.iecc.com/gclist/gc-harder.html) z x w y p a q b i c f j m d e g h k l n o Stack Heap

CS 618 Program Analysis: About These Slides 10/19 Cedar Mesa Folk Wisdom Most promising, simplest to understand, yet the hardest to implement. Which references should be set to NULL? Most approaches rely on feedback from profiling. No systematic and clean solution.

CS 618 Program Analysis: About These Slides 11/19 Distinguishing Between Reachable and Live The state of art Eliminating objects reachable from root variables which are not live. Implemented in current Sun JVMs. Uses liveness data flow analysis of root variables (stack data). What about liveness of heap data?

CS 618 Program Analysis: About These Slides 12/19 Liveness of Stack Data 1 w = x // x points to m a 2 while (x.data < max) Heap 3 x = x. 4 y = x. 5 z = New class of z 6 y = y. Stack 7 z.sum = x.data + y.data if changed to while

CS 618 Program Analysis: About These Slides 13/19 Liveness of Stack Data w = x while (x.data < max) x = x. y = x. z = New class of z y = y. z.sum = x.data + y.data No variable is used beyond this program point

CS 618 Program Analysis: About These Slides 13/19 Liveness of Stack Data w = x while (x.data < max) x = x. y = x. z = New class of z Current values of x, y, and z are used beyond this program point y = y. z.sum = x.data + y.data Live Dead

CS 618 Program Analysis: About These Slides 13/19 Liveness of Stack Data w = x while (x.data < max) y = x. x = x. Current values of x, y, and z are used beyond this program point The value of y is different before and after the assignment to y z = New class of z y = y. z.sum = x.data + y.data

CS 618 Program Analysis: About These Slides 13/19 Liveness of Stack Data w = x while (x.data < max) y = x. x = x. The current values of x and y are used beyond this program point The current value of z is not used beyond this program point z = New class of z y = y. z.sum = x.data + y.data

CS 618 Program Analysis: About These Slides 13/19 Liveness of Stack Data w = x while (x.data < max) x = x. y = x. z = New class of z y = y. The current values of x is used beyond this program point Current values of y and z are not used beyond this program point z.sum = x.data + y.data

CS 618 Program Analysis: About These Slides 13/19 Liveness of Stack Data w = x while (x.data < max) x = x. y = x. z = New class of z y = y. Nothing is known as of now Some information will be available in the next iteration point z.sum = x.data + y.data

CS 618 Program Analysis: About These Slides 13/19 Liveness of Stack Data w = x while (x.data < max) y = x. x = x. z = New class of z Current value of x is used beyond this program point However its value is different before and after the assignment y = y. z.sum = x.data + y.data

CS 618 Program Analysis: About These Slides 13/19 Liveness of Stack Data w = x while (x.data < max) x = x. y = x. z = New class of z Current value of x is used beyond this program point There are two control flow paths beyond this program point y = y. z.sum = x.data + y.data

CS 618 Program Analysis: About These Slides 13/19 Liveness of Stack Data w = x while (x.data < max) x = x. Current value of x is used beyond this program point y = x. z = New class of z y = y. z.sum = x.data + y.data

CS 618 Program Analysis: About These Slides 13/19 Liveness of Stack Data w = x while (x.data < max) Current value of x is used beyond this program point x = x. y = x. z = New class of z y = y. z.sum = x.data + y.data

CS 618 Program Analysis: About These Slides 13/19 Liveness of Stack Data w = x while (x.data < max) x = x. y = x. z = New class of z y = y. z.sum = x.data + y.data End of iteration #1 Live Dead

CS 618 Program Analysis: About These Slides 13/19 Liveness of Stack Data w = x while (x.data < max) x = x. y = x. z = New class of z y = y. z.sum = x.data + y.data End of iteration #2 Live Dead

CS 618 Program Analysis: About These Slides 14/19 Applying Cedar Mesa Folk Wisdom to Heap Data Liveness Analysis of Heap Data If the while loop is not executed even once. 1 w = x // x points to m a 2 while (x.data < max) 3 x = x. 4 y = x. 5 z = New class of z 6 y = y. 7 z.sum = x.data + y.data z x w y p a q b i c f j m d e g h k l n o Stack Heap

CS 618 Program Analysis: About These Slides 14/19 Applying Cedar Mesa Folk Wisdom to Heap Data Liveness Analysis of Heap Data If the while loop is executed once. 1 w = x // x points to m a 2 while (x.data < max) 3 x = x. 4 y = x. 5 z = New class of z 6 y = y. 7 z.sum = x.data + y.data z x w y p a q b i c f j m d e g h k l n o Stack Heap

CS 618 Program Analysis: About These Slides 14/19 Applying Cedar Mesa Folk Wisdom to Heap Data Liveness Analysis of Heap Data If the while loop is executed twice. 1 w = x // x points to m a 2 while (x.data < max) 3 x = x. 4 y = x. 5 z = New class of z 6 y = y. 7 z.sum = x.data + y.data z x w y p a q b i c f j m d e g h k l n o Stack Heap

CS 618 Program Analysis: About These Slides 15/19 The Moral of the Story Mappings between access expressions and l-values keep changing This is a rule for heap data For stack and static data, it is an exception! Static analysis of programs has made significant progress for stack and static data. What about heap data? Given two access expressions at a program point, do they have the same l-value? Given the same access expression at two program points, does it have the same l-value?

CS 618 Program Analysis: About These Slides 16/19 Our Solution 1 w = x 2 while (x.data < max) y = z = null w = null { x. = null 3 x = x. } 4 y = x. 5 z = New class of z 6 y = y. 7 z.sum = x.data + y.data x. = x.. = null x... = null x... = null x. = y. = null y.. = y.. = null z. = z. = null y. = y. = null x = y = z = null

CS 618 Program Analysis: About These Slides 17/19 Our Solution y = z = null 1 w = x w = null 2 while (x.data < max) { x. = null 3 x = x. } x. = x.. = null x... = null x... = null 4 y = x. x. = y. = null y.. = y.. = null 5 z = New class of z z. = z. = null 6 y = y. y. = y. = null 7 z.sum = x.data + y.data x = y = z = null While loop is not executed even once z x w y Stack p a q b i c f j m Heap d e g h k l n o

CS 618 Program Analysis: About These Slides 17/19 Our Solution y = z = null 1 w = x w = null 2 while (x.data < max) { x. = null 3 x = x. } x. = x.. = null x... = null x... = null 4 y = x. x. = y. = null y.. = y.. = null 5 z = New class of z z. = z. = null 6 y = y. y. = y. = null 7 z.sum = x.data + y.data x = y = z = null While loop is not executed even once z x w y Stack p a q b i c f j m Heap d e g h k l n o

CS 618 Program Analysis: About These Slides 17/19 Our Solution y = z = null 1 w = x w = null 2 while (x.data < max) { x. = null 3 x = x. } x. = x.. = null x... = null x... = null 4 y = x. x. = y. = null y.. = y.. = null 5 z = New class of z z. = z. = null 6 y = y. y. = y. = null 7 z.sum = x.data + y.data x = y = z = null While loop is not executed even once z x w y Stack p a q b i c f j m Heap d e g h k l n o

CS 618 Program Analysis: About These Slides 17/19 Our Solution y = z = null 1 w = x w = null 2 while (x.data < max) { x. = null 3 x = x. } x. = x.. = null x... = null x... = null 4 y = x. x. = y. = null y.. = y.. = null 5 z = New class of z z. = z. = null 6 y = y. y. = y. = null 7 z.sum = x.data + y.data x = y = z = null While loop is not executed even once z x w y Stack p a q b i c f j m Heap d e g h k l n o

CS 618 Program Analysis: About These Slides 17/19 Our Solution y = z = null 1 w = x w = null 2 while (x.data < max) { x. = null 3 x = x. } x. = x.. = null x... = null x... = null 4 y = x. x. = y. = null y.. = y.. = null 5 z = New class of z z. = z. = null 6 y = y. y. = y. = null 7 z.sum = x.data + y.data x = y = z = null While loop is not executed even once z x w y Stack p a q b i c f j m Heap d e g h k l n o

CS 618 Program Analysis: About These Slides 17/19 Our Solution y = z = null 1 w = x w = null 2 while (x.data < max) { x. = null 3 x = x. } x. = x.. = null x... = null x... = null 4 y = x. x. = y. = null y.. = y.. = null 5 z = New class of z z. = z. = null 6 y = y. y. = y. = null 7 z.sum = x.data + y.data x = y = z = null While loop is not executed even once z x w y Stack p a q b i c f j m Heap d e g h k l n o

CS 618 Program Analysis: About These Slides 17/19 Our Solution y = z = null 1 w = x w = null 2 while (x.data < max) { x. = null 3 x = x. } x. = x.. = null x... = null x... = null 4 y = x. x. = y. = null y.. = y.. = null 5 z = New class of z z. = z. = null 6 y = y. y. = y. = null 7 z.sum = x.data + y.data x = y = z = null While loop is not executed even once z x w y Stack p a q b i c f j m Heap d e g h k l n o

CS 618 Program Analysis: About These Slides 17/19 Our Solution y = z = null 1 w = x w = null 2 while (x.data < max) { x. = null 3 x = x. } x. = x.. = null x... = null x... = null 4 y = x. x. = y. = null y.. = y.. = null 5 z = New class of z z. = z. = null 6 y = y. y. = y. = null 7 z.sum = x.data + y.data x = y = z = null z x w y Stack While loop is executed once p a q b i c f j m Heap d e g h k l n o

CS 618 Program Analysis: About These Slides 17/19 Our Solution y = z = null 1 w = x w = null 2 while (x.data < max) { x. = null 3 x = x. } x. = x.. = null x... = null x... = null 4 y = x. x. = y. = null y.. = y.. = null 5 z = New class of z z. = z. = null 6 y = y. y. = y. = null 7 z.sum = x.data + y.data x = y = z = null z x w y Stack While loop is executed twice p a q b i c f j m Heap d e g h k l n o

CS 618 Program Analysis: About These Slides 18/19 Some Observations y = z = null 1 w = x w = null 2 while (x.data < max) { x. = null 3 x = x. } x. = x.. = null x... = null x... = null 4 y = x. x. = y. = null y.. = y.. = null 5 z = New class of z z. = z. = null 6 y = y. y. = y. = null 7 z.sum = x.data + y.data x = y = z = null Node i is live but link a i is nullified z x w y Stack p a q b i c f j m Heap d e g h k l n o

CS 618 Program Analysis: About These Slides 18/19 Some Observations y = z = null 1 w = x w = null 2 while (x.data < max) { x. = null 3 x = x. } x. = x.. = null x... = null x... = null 4 y = x. x. = y. = null y.. = y.. = null 5 z = New class of z z. = z. = null 6 y = y. y. = y. = null 7 z.sum = x.data + y.data x = y = z = null New access expressions are created. Can they cause exceptions? z x w y Stack p a q b i c f j m Heap d e g h k l n o

CS 618 Program Analysis: About These Slides 19/19 Goals of This Tutorial Main Goal: Explain how to get the modified program Side goals:

CS 618 Program Analysis: About These Slides 19/19 Goals of This Tutorial Main Goal: Explain how to get the modified program Side goals: Data Flow Values Acceptable Operations Desired Solutions Practical Algorithms Explain the intuitions Relate them to mathematical rigour Highlight the frontiers