Memory Allocation III

Similar documents
Memory Allocation III

Memory Allocation III

Memory Allocation III

Memory Allocation III CSE 351 Spring (original source unknown)

Dynamic Memory Allocation II October 22, 2008

Internal Fragmentation

Today. Dynamic Memory Allocation: Advanced Concepts. Explicit Free Lists. Keeping Track of Free Blocks. Allocating From Explicit Free Lists

Dynamic Memory Allocation: Advanced Concepts

Dynamic Memory Allocation: Advanced Concepts

Dynamic Memory Alloca/on: Advanced Concepts

Dynamic Memory Allocation: Advanced Concepts

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING

Dynamic Memory Alloca/on: Advanced Concepts

Dynamic Memory Allocation

Dynamic Memory Allocation

Foundations of Computer Systems

NEXT SET OF SLIDES FROM DENNIS FREY S FALL 2011 CMSC313.

CS 153 Design of Operating Systems

Dynamic Memory Allocation

CS 33. Intro to Storage Allocation. CS33 Intro to Computer Systems XXVI 1 Copyright 2017 Thomas W. Doeppner. All rights reserved.

CS61, Fall 2012 Section 2 Notes

Java and C I. CSE 351 Spring Instructor: Ruth Anderson

Process s Address Space. Dynamic Memory. Backing the Heap. Dynamic memory allocation 3/29/2013. When a process starts the heap is empty

CMSC 330: Organization of Programming Languages

Binghamton University Dynamic Memory Alloca/on

The Stack & Procedures

CMSC 330: Organization of Programming Languages

Roadmap. Java: Assembly language: OS: Machine code: Computer system:

Data III & Integers I

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

Memory Allocation II. CSE 351 Autumn Instructor: Justin Hsia

Garbage Collection. Steven R. Bagley

Lecture Notes on Memory Management

Lecture 13: Garbage Collection

Programming Language Implementation

Automatic Memory Management

L14: Structs and Alignment. Structs and Alignment. CSE 351 Spring Instructor: Ruth Anderson

CSCI-UA /2. Computer Systems Organization Lecture 19: Dynamic Memory Allocation: Basics

CS 31: Intro to Systems Pointers and Memory. Kevin Webb Swarthmore College October 2, 2018

Today. Dynamic Memory Allocation: Basic Concepts. Dynamic Memory Allocation. Dynamic Memory Allocation. malloc Example. The malloc Package

CS 31: Intro to Systems Pointers and Memory. Martin Gagne Swarthmore College February 16, 2016

CS 61C: Great Ideas in Computer Architecture C Memory Management, Usage Models

Lecture 8 Dynamic Memory Allocation

Java and C II. CSE 351 Spring Instructor: Ruth Anderson

CS 11 C track: lecture 5

Deallocation Mechanisms. User-controlled Deallocation. Automatic Garbage Collection

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

Lecture Notes on Memory Management

Today. Dynamic Memory Allocation: Basic Concepts. Dynamic Memory Allocation. Dynamic Memory Allocation. malloc Example. The malloc Package

CSE351: Memory, Data, & Addressing I

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

Class Information ANNOUCEMENTS

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

Dynamic Memory Allocation: Basic Concepts

Week 9 Part 1. Kyle Dewey. Tuesday, August 28, 12

Assembly Programming IV

Heap Management. Heap Allocation

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

Introduction to Computer Systems /18 243, fall th Lecture, Oct. 22 th

CS558 Programming Languages

CS61C : Machine Structures

Caches II. CSE 351 Spring Instructor: Ruth Anderson

Dynamic Memory Allocation I Nov 5, 2002

CS61C : Machine Structures

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

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

CS61C : Machine Structures

Dynamic Memory Allocation: Basic Concepts

Dynamic Memory Allocation. Basic Concepts. Computer Organization 4/3/2012. CSC252 - Spring The malloc Package. Kai Shen

CMSC 341 Lecture 2 Dynamic Memory and Pointers

Memory Management. CS31 Pascal Van Hentenryck CS031. Lecture 19 1

Creating a String Data Type in C

Dynamic Memory Allocation I

CS558 Programming Languages

Dynamic Memory Allocation and Linked Lists

Outline. Briefly review the last class Pointers and Structs Memory allocation Linked lists

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

Princeton University Computer Science 217: Introduction to Programming Systems. Data Structures

APS105. Malloc and 2D Arrays. Textbook Chapters 6.4, Datatype Size

Dynamic Memory Allocation. Gerson Robboy Portland State University. class20.ppt

CS558 Programming Languages

Algorithms for Dynamic Memory Management (236780) Lecture 1

Reference Counting. Reference counting: a way to know whether a record has other users

Lecture Notes on Garbage Collection

Debugging (Part 2) 1

Virtual Memory I. CSE 351 Spring Instructor: Ruth Anderson

Dynamic Allocation in C

C Structures & Dynamic Memory Management

Dynamic Memory Allocation

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

Dynamic Memory Management

G Programming Languages - Fall 2012

2/9/18. Secure Coding. CYSE 411/AIT681 Secure Software Engineering. Agenda. Dynamic Memory Interface. Dynamic Memory Interface

CSE351 Spring 2018, Final Exam June 6, 2018

ANITA S SUPER AWESOME RECITATION SLIDES

CS 241 Honors Memory

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

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

COSC Software Engineering. Lectures 14 and 15: The Heap and Dynamic Memory Allocation

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

Transcription:

Memory Allocation III CSE 351 Spring 2017 Instructor: Ruth Anderson Teaching Assistants: Dylan Johnson Kevin Bi Linxing Preston Jiang Cody Ohlsen Yufang Sun Joshua Curtis

Administrivia Homework 5 Due Wed 5/31 Lab 5 Due Fri 6/2 Do not put this one off!! Get started ASAP! 2

Memory as a Graph We view memory as a directed graph Each allocated heap block is a node in the graph Each pointer is an edge in the graph Locations not in the heap that contain pointers into the heap are called root nodes (e.g. registers, stack locations, global variables) Root nodes Heap nodes reachable not reachable (garbage) A node (block) is reachable if there is a path from any root to that node Non reachable nodes are garbage (cannot be needed by the application) 3

Garbage Collection Dynamic memory allocator can free blocks if there are no pointers to them How can it know what is a pointer and what is not? We ll make some assumptions about pointers: Memory allocator can distinguish pointers from nonpointers All pointers point to the start of a block in the heap Application cannot hide pointers (e.g. by coercing them to an int, and then back again) 4

Classical GC Algorithms Mark and sweep collection (McCarthy, 1960) Does not move blocks (unless you also compact ) Reference counting (Collins, 1960) Does not move blocks (not discussed) Copying collection (Minsky, 1963) Moves blocks (not discussed) Generational Collectors (Lieberman and Hewitt, 1983) Most allocations become garbage very soon, so focus reclamation work on zones of memory recently allocated. For more information: Jones, Hosking, and Moss, The Garbage Collection Handbook: The Art of Automatic Memory Management, CRC Press, 2012. Jones and Lin, Garbage Collection: Algorithms for Automatic Dynamic Memory, John Wiley & Sons, 1996. 5

Mark and Sweep Collecting Can build on top of malloc/free package Allocate using malloc until you run out of space When out of space: Use extra mark bit in the header of each block Mark: Start at roots and set mark bit on each reachable block Sweep: Scan all blocks and free blocks that are not marked Before mark root Arrows are NOT free list pointers After mark Mark bit set After sweep free free 6

Assumptions For a Simple Implementation Application can use functions to allocate memory: b=new(n) returns pointer, b, to new block with all locations cleared b[i] b[i]=v read location i of block b into register write v into location i of block b Non testable Material Each block will have a header word (accessed at b[-1]) Functions used by the garbage collector: is_ptr(p) length(p) get_roots() determines whether p is a pointer to a block returns length of block pointed to by p, not including header returns all the roots 7

Mark Non testable Material Mark using depth first traversal of the memory graph ptr mark(ptr p) { // p: some word in a heap block if (!is_ptr(p)) return; // do nothing if not pointer if (markbitset(p)) return; // check if already marked setmarkbit(p); // set the mark bit for (i=0; i<length(p); i++) // recursively call mark on mark(p[i]); // all words in the block return; } Before mark root After mark Mark bit set 8

Sweep Non testable Material Sweep using sizes in headers ptr sweep(ptr p, ptr end) { // ptrs to start & end of heap while (p < end) { // while not at end of heap if (markbitset(p)) // check if block is marked clearmarkbit(p); // if so, reset mark bit else if (allocatebitset(p)) // if not marked, but allocated free(p); // free the block p += length(p); // adjust pointer to next block } } After mark Mark bit set After sweep free free 9

Conservative Mark & Sweep in C Non testable Material Would mark & sweep work in C? is_ptr determines if a word is a pointer by checking if it points to an allocated block of memory But in C, pointers can point into the middle of allocated blocks (not so in Java) Makes it tricky to find all allocated blocks in mark phase header ptr There are ways to solve/avoid this problem in C, but the resulting garbage collector is conservative: Every reachable node correctly identified as reachable, but some unreachable nodes might be incorrectly marked as reachable In Java, all pointers (i.e. references) point to the starting address of an object structure the start of an allocated block 10

Memory Related Perils and Pitfalls in C A. Failing to Free Blocks B. Misunderstanding pointer arithmetic C. Off by one error D. Freeing Blocks Multiple times E. Referencing a pointer instead of the object it points to F. Not checking the max string size G. Interpreting something that is not a ptr as a ptr H. Referencing Freed Blocks I. Referencing nonexistent variables J. Allocating the (possibly) wrong sized object K. Reading uninitialized memory 11

Find That Bug! [1] The classic scanf bug int scanf(const char *format,...) int val;... scanf("%d", val); Fix: 12

Interpreting something that is not a pointer as a pointer [1] The classic scanf bug int scanf(const char *format,...) int val;... scanf("%d", val); Will cause scanf to interpret contents of val as an address! Best case: program terminates immediately due to segmentation fault Worst case: contents of val correspond to some valid read/write area of virtual memory, causing scanf to overwrite that memory, with disastrous and baffling consequences much later in program execution 13

Find That Bug! [2] /* return y = Ax */ int *matvec(int **A, int *x) { int *y = (int *)malloc( N * sizeof(int) ); int i, j; for (i = 0; i < N; i++) for (j = 0; j < N; j++) y[i] += A[i][j] * x[j]; } return y; A is NxN matrix, x is N sized vector (so product is vector of size N) N defined elsewhere (#define) Fix: 14

Reading Uninitialized Memory [2] /* return y = Ax */ int *matvec(int **A, int *x) { int *y = (int *)malloc( N * sizeof(int) ); int i, j; for (i = 0; i < N; i++) for (j = 0; j < N; j++) y[i] += A[i][j] * x[j]; } return y; A is NxN matrix, x is N sized vector (so product is vector of size N) N defined elsewhere (#define) Don t assume heap data is initialized to zero! 15

Find That Bug! [3] int **p; p = (int **)malloc( N * sizeof(int) ); for (int i = 0; i < N; i++) { p[i] = (int *)malloc( M * sizeof(int) ); } N and M defined elsewhere (#define) Fix: 16

Allocating the (possibly) wrong sized object [3] int **p; p = (int **)malloc( N * sizeof(int) ); for (int i = 0; i < N; i++) { p[i] = (int *)malloc( M * sizeof(int) ); } N and M defined elsewhere (#define) Overwriting Memory! 17

Find That Bug! [4] int **p; p = (int **)malloc( N * sizeof(int*) ); for (int i = 0; i <= N; i++) { p[i] = (int *)malloc( M * sizeof(int) ); } Fix: 18

Off by one error [4] int **p; p = (int **)malloc( N * sizeof(int*) ); for (int i = 0; i <= N; i++) { p[i] = (int *)malloc( M * sizeof(int) ); } Overwriting Memory! 19

Find That Bug! [5] char s[8]; int i; gets(s); /* reads 123456789 from stdin */ Fix: 20

Not checking the max string size [5] char s[8]; int i; gets(s); /* reads 123456789 from stdin */ Overwriting Memory! 21

Find That Bug! [6] int *search(int *p, int val) { while (p && *p!= val) p += sizeof(int); } return p; Fix: 22

Misunderstanding pointer arithmetic [6] int *search(int *p, int val) { while (p && *p!= val) p += sizeof(int); } return p; Overwriting Memory! 23

Find That Bug! [7] int* getpacket(int** packets, int* size) { int* packet; packet = packets[0]; packets[0] = packets[*size - 1]; *size--; // what is happening here? reorderpackets(packets, *size); return packet; } In newer versions of C, the postfix decrement has higher precedence than * so happens first. In older versions of C, -- and * operators have same precedence and associate from right to left, so still happens first Fix: 24

Referencing a pointer instead of the object it points to [7] int* getpacket(int** packets, int* size) { int* packet; packet = packets[0]; packets[0] = packets[*size - 1]; *size--; // what is happening here? reorderpackets(packets, *size); return packet; } In newer versions of C, the postfix decrement has higher precedence than * so happens first. In older versions of C, -- and * operators have same precedence and associate from right to left, so still happens first 25

Find That Bug! [8] int* foo() { int val; } return &val; Fix: 26

Referencing nonexistent variables [8] int* foo() { int val; } return &val; Forgetting that local variables disappear when a function returns! 27

Find That Bug! [9] x = (int*)malloc( N * sizeof(int) ); <manipulate x> free(x);... y = (int*)malloc( M * sizeof(int) ); <manipulate y> free(x); Fix: 28

Freeing Blocks Multiple Times [9] x = (int*)malloc( N * sizeof(int) ); <manipulate x> free(x);... y = (int*)malloc( M * sizeof(int) ); <manipulate y> free(x); 29

Find That Bug! [10] x = (int*)malloc( N * sizeof(int) ); <manipulate x> free(x);... y = (int*)malloc( M * sizeof(int) ); for (i = 0; i < M; i++) y[i] = x[i]++; Fix: 30

Referencing Freed Blocks [10] x = (int*)malloc( N * sizeof(int) ); <manipulate x> free(x);... y = (int*)malloc( M * sizeof(int) ); for (i = 0; i < M; i++) y[i] = x[i]++; 31

Find That Bug! [11] void foo() { int *x = (int *) malloc( N * sizeof(int) );... return; } Fix: 32

Failing to Free Blocks (Memory Leak) [11] void foo() { int *x = (int *) malloc( N * sizeof(int) );... return; } Slow, silent, long term killer! 33

Find That Bug! [12] typedef struct L { int val; struct L *next; } list; void foo() { list *head = (list *) malloc( sizeof(list) ); head->val = 0; head->next = NULL; <create and manipulate the rest of the list>... free(head); return; } Fix: 34

Failing to Free Blocks (Memory Leak)[12] typedef struct L { int val; struct L *next; } list; void foo() { list *head = (list *) malloc( sizeof(list) ); head->val = 0; head->next = NULL; <create and manipulate the rest of the list>... free(head); return; } 35

Dealing With Memory Bugs Conventional debugger (gdb) Good for finding bad pointer dereferences Hard to detect the other memory bugs Debugging malloc (UToronto CSRI malloc) Wrapper around conventional malloc Detects memory bugs at malloc and free boundaries Memory overwrites that corrupt heap structures Some instances of freeing blocks multiple times Memory leaks Cannot detect all memory bugs Overwrites into the middle of allocated blocks Freeing block twice that has been reallocated in the interim Referencing freed blocks 36

Dealing With Memory Bugs (cont.) Some malloc implementations contain checking code Linux glibc malloc: setenv MALLOC_CHECK_ 2 FreeBSD: setenv MALLOC_OPTIONS AJR Binary translator: valgrind (Linux), Purify Powerful debugging and analysis technique Rewrites text section of executable object file Can detect all errors as debugging malloc Can also check each individual reference at runtime Bad pointers Overwriting Referencing outside of allocated block 37

What about Java or ML or Python or? In memory safe languages, most of these bugs are impossible Cannot perform arbitrary pointer manipulation Cannot get around the type system Array bounds checking, null pointer checking Automatic memory management But one of the bugs we saw earlier is possible. Which one? 38

Memory Leaks with GC Not because of forgotten free we have GC! Unneeded leftover roots keep objects reachable Sometimes nullifying a variable is not needed for correctness but is for performance Example: Don t leave big data structures you re done with in a static field Root nodes Heap nodes reachable not reachable (garbage) 39