Defect Analysis: Memory Leaks

Slides:



Advertisements
Similar presentations
CSE 5317/4305 L7: Run-Time Storage Organization1 Run-Time Storage Organization Leonidas Fegaras.
Advertisements

Computer Science and Computer Engineering. parts of the computer.
ISBN Chapter 10 Implementing Subprograms.
Run-Time Storage Organization
Run time vs. Compile time
ISBN Chapter 10 Implementing Subprograms –Semantics of Calls and Returns –Implementing “Simple” Subprograms –Implementing Subprograms with.
Garbage Collection and High-Level Languages Programming Languages Fall 2003.
ISBN Chapter 10 Implementing Subprograms.
Lecture Notes - Copyright © S. C. Kothari, All rights reserved.1 Summary Lecture CPRE 416-Software Evolution and Maintenance-Lecture 6.
Runtime Environments Compiler Construction Chapter 7.
Presentation of Failure- Oblivious Computing vs. Rx OS Seminar, winter 2005 by Lauge Wullf and Jacob Munk-Stander January 4 th, 2006.
Compiler Construction
ITEC 352 Lecture 18 Functions in Assembly. Functions + Assembly Review Questions? Project due on Friday Exam –Average 76 Methods for functions in assembly.
Copyright © 2005 Elsevier Chapter 8 :: Subroutines and Control Abstraction Programming Language Pragmatics Michael L. Scott.
Basic Semantics Associating meaning with language entities.
Principles of Computer Science I Honors Section Note Set 1 CSE 1341 – H 1.
ARM-7 Assembly: Example Programs 1 CSE 2312 Computer Organization and Assembly Language Programming Vassilis Athitsos University of Texas at Arlington.
Unit 4: Processes, Threads & Deadlocks June 2012 Kaplan University 1.
Announcements Partial Credit Due Date for Assignment 2 now due on Sat, Feb 27 I always seem to be behind and get tons of daily. If you me and.
Sections Basic Data Structures. 1.5 Data Structures The way you view and structure the data that your programs manipulate greatly influences your.
Applying and Interviewing
Appendix H Exception Handling: A Deeper Look
Remote Procedure Calls
Object Lifetime and Pointers
Lecture 7 Macro Review Stack Frames
Development Environment
EGR 2261 Unit 11 Pointers and Dynamic Variables
INTERVIEW FORMATS.
Protecting Memory What is there to protect in memory?
Lecture 1: Operating System Services
Implementing Subprograms Chapter 10
Implementing Subprograms
CS 326 Programming Languages, Concepts and Implementation
YAHMD - Yet Another Heap Memory Debugger
Business Case Analysis
Protecting Memory What is there to protect in memory?
Protecting Memory What is there to protect in memory?
© Craig Zilles (adapted from slides by Howard Huang)
Storage Management.
Run-time organization
Intro to Processes CSSE 332 Operating Systems
CS41B recursion David Kauchak CS 52 – Fall 2015.
Functions and Procedures
CSCE 210 Data Structures and Algorithms
8 Pointers.
Chapter 9 :: Subroutines and Control Abstraction
Object Oriented Programming COP3330 / CGS5409
ICS 143 Principles of Operating Systems
Chap. 8 :: Subroutines and Control Abstraction
Chap. 8 :: Subroutines and Control Abstraction
COP 4600 Operating Systems Spring 2011
Human Complexity of Software
COT 5611 Operating Systems Design Principles Spring 2014
Handling Arrays Completion of ideas needed for a general and complete program Final concepts needed for Final.
CS179G, Project In Computer Science
Understanding Program Address Space
Implementing Subprograms
CSE 303 Concepts and Tools for Software Development
Handling Arrays Completion of ideas needed for a general and complete program Final concepts needed for Final.
Data Structures & Algorithms
CPRE 416-Software Evolution and Maintenance-Lecture 11
Foundations and Definitions
Where is all the knowledge we lost with information? T. S. Eliot
CSE 153 Design of Operating Systems Winter 2019
© Craig Zilles (adapted from slides by Howard Huang)
Human and Computer Interaction (H.C.I.) &Communication Skills
SPL – PS3 C++ Classes.
Chapter 10 Def: The subprogram call and return operations of
Dynamic Data Structures
Phil Tayco San Jose City College Slide version 1.2 Updated Sep 9, 2019
Presentation transcript:

Defect Analysis: Memory Leaks CPRE 556: Lecture 5 9/17/2018 Lecture Notes – Copyright © 2006 S. C. Kothari, All rights reserved

What will we cover today? Last time we discussed an example of code and made several observations regarding memory leaks. We will first review those observations and then study a few other examples. 9/17/2018 Lecture Notes – Copyright © 2006 S. C. Kothari, All rights reserved

Lecture Notes – Copyright © 2006 S. C. Kothari, All rights reserved Scenario Memory is allocated in the function f() by calling a dynamic memory allocation function (e.g. getbuf() in our sample code). We want to check if the memory is subsequently deallocated. We will use the term handle to refer to a pointer that stores the address of the dynamically allocated memory. Memory must be deallocated before destroying all the handles to the memory. 9/17/2018 Lecture Notes – Copyright © 2006 S. C. Kothari, All rights reserved

Who may deallocate memory? There are three possibilities – the memory may be deallocated by: The same function (f()) that allocated the memory. The caller of the function f(). One or more functions called by f(). 9/17/2018 Lecture Notes – Copyright © 2006 S. C. Kothari, All rights reserved

Should we analyze the caller? We need to analyze the caller of f() only if the caller can see a handle to the allocated memory. Typical cases would be: There is a handle which is a global pointer. A handle is assigned to a function parameter and communicated to the caller. 9/17/2018 Lecture Notes – Copyright © 2006 S. C. Kothari, All rights reserved

Should we analyze the callee? We need to analyze a function g() called (directly or indirectly through a chain of calls) by f() only if g() can see a handle to the allocated memory. Typical cases would be: A handle is assigned to a function parameter and communicated to g(). 9/17/2018 Lecture Notes – Copyright © 2006 S. C. Kothari, All rights reserved

Lecture Notes – Copyright © 2006 S. C. Kothari, All rights reserved Escape Analysis We will say an handle escapes a function f() if the handle is communicated to other functions through a global variable or a parameter. Based on the previous observations, we need to check all those functions to where a handle escapes. We will call this an escape analysis. The purpose is to determine the functions that need to be looked at. 9/17/2018 Lecture Notes – Copyright © 2006 S. C. Kothari, All rights reserved

Execution Path Analysis The escape analysis leads us to functions that need to be analyzed. We need to analyze all the relevant execution paths within the identified functions. We have to relate the paths with respect to the conditions governing the allocation. What is a governing condition in the code sample for the homework? 9/17/2018 Lecture Notes – Copyright © 2006 S. C. Kothari, All rights reserved

Challenge: Asynchronous Processing Memory allocation is done in f() and deallocation is done g(). The function g() is not a caller of f() and it is also not called by f() (directly or indirectly). Typical cases are: g() is an interrupt-driven function. g() is executed by a concurrent thread. 9/17/2018 Lecture Notes – Copyright © 2006 S. C. Kothari, All rights reserved

How is the handle communicated in asynchronous processing? Suppose the allocation is done in f() and deallocation is done g() that works asynchronously with f(). How is a handle communicated to g()?. Typically it is done by : Inserting the handle in a shared linked list or some other data structure that is accessible to both f() and g(). Does it happen in the homework code? Where? How do we find the g()? 9/17/2018 Lecture Notes – Copyright © 2006 S. C. Kothari, All rights reserved

Lecture Notes – Copyright © 2006 S. C. Kothari, All rights reserved Homework Reminder The first homework is posted on the web. Type the answer and send the answer to me by next Thursday (1/26), 10 pm. Mail a PDF or MS Word document to: kothari@iastate.edu 9/17/2018 Lecture Notes – Copyright © 2006 S. C. Kothari, All rights reserved

C++ Example Mark Roulo is a technical coordinator for Java World magazine who has programmed professionally since 1989 and has screened candidates for C++ projects. As part of his standard interview for C++ candidates, he asks them to write a small class with the intention of evaluating their command of the language. We are going to examine the code most candidates write as a reference point for our discussion.

C++ Example Mark Roulo is a technical coordinator for Java World magazine who has programmed professionally since 1989 and has screened candidates for C++ projects. As part of his standard interview for C++ candidates, he asks them to write a small class with the intention of evaluating their command of the language. We are going to examine the code most candidates write as a reference point for our discussion.

C++ Example “Most of the candidates I interview have already made it to the top of the resume pool -- usually by claiming at least 3 years professional experience with C++ plus large systems experience, degree from a good school, personal recommendation, etc.” “The candidates then must survive a phone screen interview whose job is to weed out candidates that can't, for example, describe any of their projects coherently.”

C++ Example Mark Roulo is a technical coordinator for Java World magazine who has programmed professionally since 1989 and has screened candidates for C++ projects. As part of his standard interview for C++ candidates, he asks them to write a small class with the intention of evaluating their command of the language. We are going to examine the code most candidates write as a reference point for our discussion.

C++ Example “Most of the candidates I interview have already made it to the top of the resume pool -- usually by claiming at least 3 years professional experience with C++ plus large systems experience, degree from a good school, personal recommendation, etc.” “The candidates then must survive a phone screen interview whose job is to weed out candidates that can't, for example, describe any of their projects coherently.”

C++ Example The assignment: Write a Named Point class with three members: two floating point values for the coordinates on an X-Y plane, and a name represented as a 'char *'. Assume that this class will be used for some sort of wargame or simulation program that treats the world as flat and that these named points will be used to represent things like cities, battlefields, etc. The handout has the submissions from the interview candidates.

C++ Example Mark Roulo is a technical coordinator for Java World magazine who has programmed professionally since 1989 and has screened candidates for C++ projects. As part of his standard interview for C++ candidates, he asks them to write a small class with the intention of evaluating their command of the language. We are going to examine the code most candidates write as a reference point for our discussion.

C++ Example “Most of the candidates I interview have already made it to the top of the resume pool -- usually by claiming at least 3 years professional experience with C++ plus large systems experience, degree from a good school, personal recommendation, etc.” “The candidates then must survive a phone screen interview whose job is to weed out candidates that can't, for example, describe any of their projects coherently.”

C++ Example The assignment: Write a Named Point class with three members: two floating point values for the coordinates on an X-Y plane, and a name represented as a 'char *'. Assume that this class will be used for some sort of wargame or simulation program that treats the world as flat and that these named points will be used to represent things like cities, battlefields, etc. The handout has the submissions from the interview candidates.

C++ Example This example shows us that: Even interview candidates with 3 years of experience are proficient at writing buggy code in C++. These defects occur across functions. For a tool to catch these errors it needs to take into account how functions interact. We might think that a bug is fixed, but in C++ we are probably trading it for an even more subtle bug. We need to be able to recheck the code often.