Recitation 11: More Malloc Lab

Slides:



Advertisements
Similar presentations
Dynamic Memory Allocation in C.  What is Memory What is Memory  Memory Allocation in C Memory Allocation in C  Difference b\w static memory allocation.
Advertisements

Dynamic Memory Allocation I Topics Simple explicit allocators Data structures Mechanisms Policies CS 105 Tour of the Black Holes of Computing.
Week 12 (March 28th) Outline Memory Allocation Lab 6 Reminders Lab 6: April 7 th (next Thurs) Start Early!!! Exam 2: April 5 th (Next Tue) TA: Kun Gao.
Carnegie Mellon 1 Debugging : Introduction to Computer Systems Recitation 12: Monday, Nov. 9 th, 2013 Yixun Xu Section K.
Recitation 10 Anubhav Gupta Section D.
DEBUGGING IN THE REAL WORLD : Recitation 4.
18-213: Introduction to Computer Systems April 6, 2015
Lab 3: Malloc Lab. “What do we need to do?”  Due 11/26  One more assignment after this one  Partnering  Non-Honors students may work with one other.
Pointers Applications
Debugger Presented by 李明璋 2012/05/08. The Definition of Bug –Part of the code which would result in an error, fault or malfunctioning of the program.
L6: Malloc Lab Writing a Dynamic Storage Allocator October 30, 2006
Memory & Storage Architecture Seoul National University GDB commands Hyeon-gyu School of Computer Science and Engineering.
Carnegie Mellon 1 Debugging Malloc Lab : Introduction to Computer Systems Recitation 12: Monday, Nov. 11 th, 2013 Marjorie Carlson Section A.
CS 11 C track: lecture 5 Last week: pointers This week: Pointer arithmetic Arrays and pointers Dynamic memory allocation The stack and the heap.
Agenda Attack Lab C Exercises C Conventions C Debugging
Topics memory alignment and structures typedef for struct names bitwise & for viewing bits malloc and free (dynamic storage in C) new and delete (dynamic.
Debugging 1/6/2016. Debugging 1/6/2016 Debugging  Debugging is a methodical process of finding and reducing the number of bugs, or defects, in a program.
CSE 374 Programming Concepts & Tools Hal Perkins Fall 2015 Lecture 10 – C: the heap and manual memory management.
Carnegie Mellon Dynamic Memory Allocation : Introduction to Computer Systems Monday March 30th,
1 Lecture07: Memory Model 5/2/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
Malloc Lab : Introduction to Computer Systems Recitation 11: Nov. 4, 2013 Marjorie Carlson Recitation A.
CSE 351 Dynamic Memory Allocation 1. Dynamic Memory Dynamic memory is memory that is “requested” at run- time Solves two fundamental dilemmas: How can.
1 Dynamic Memory Allocation (II) Implementation. 2 Outline Implementation of a simple allocator Explicit Free List Segregated Free List Suggested reading:
Carnegie Mellon 1 Malloc Lab : Introduction to Computer Systems Friday, July 10, 2015 Shen Chen Xu.
Carnegie Mellon 1 Malloc Recitation Ben Spinelli Recitation 11: November 9, 2015.
Carnegie Mellon Dynamic Memory Allocation : Introduction to Computer Systems Recitation 11: Monday, Nov 3, 2014 SHAILIN DESAI SECTION L 1.
Recitation 5: Attack Lab
Section 10: Memory Allocation Topics
Course Contents KIIT UNIVERSITY Sr # Major and Detailed Coverage Area
Data Watch Presenter Information Jason Puncher and François Tétreault
CSE 374 Programming Concepts & Tools
Recitation 6: C Review 30 Sept 2016.
Recitation: Bomb Lab _______________ 18 Sep 2017.
CSE 374 Programming Concepts & Tools
Recitation: Attack Lab
Testing and Debugging.
Recitation 11: More Malloc Lab
Recitation: Bomb Lab _______________ 06 Feb 2017.
Checking Memory Management
Lab: ssh, scp, gdb, valgrind
COMP 2710 Software Construction Introduction to GDB
This pointer, Dynamic memory allocation, Constructors and Destructor
Recitation: C Review TA’s 19 Feb 2018.
Prof. Neary Adapted from slides by Dr. Katherine Gibson
Recitation 10: Malloc Lab
Optimizing Malloc and Free
Lab: ssh, scp, gdb, valgrind
Recitation: Attack Lab
Memory Management III: Perils and pitfalls Mar 13, 2001
Recitation 10: Malloc Lab
Recitation 11: More Malloc Lab
Review and Q/A.
Tonga Institute of Higher Education
File I/O in C Lecture 7 Narrator: Lecture 7: File I/O in C.
CSc 352 Debugging Tools Saumya Debray Dept. of Computer Science
Effective and Efficient memory Protection Using Dynamic Tainting
CSCI 380: Operating Systems Lecture #11 Instructor: William Killian
Memory Allocation II CSE 351 Autumn 2017
Memory Allocation II CSE 351 Winter 2018
Memory Allocation II CSE 351 Winter 2018
CSCI 380: Operating Systems Instructor: William Killian
Malloc Lab CSCI 380: Operating Systems
TUTORIAL 7 CS 137 F18 October 30th.
Data Structures & Algorithms
Linked lists Prof. Noah Snavely CS1114
C Programming - Lecture 5
Understanding mm-helper.c Adding debugging info to mm-helper.c
Makefiles, GDB, Valgrind
Recitation 10: Malloc Lab
Presentation transcript:

Recitation 11: More Malloc Lab Instructor: TA(s)

Understanding Your Code Sketch out the heap Add Instrumentation Use tools

Sketch out the Heap Start with a heap, in this case implicit list Now try something, in this case, extend_heap block_t *block = payload_to_header(bp); write_header(block, size, false); write_footer(block, size, false); // Create new epilogue header block_t *block_next = find_next(block); write_header(block_next, 0, true); 4 4 4 4 6 6 4 4

Sketch out the Heap Here is a free block based on lectures 19 and 20 Explicit pointers (will be well-defined see writeup and Piazza) Optional boundary tags If you make changes to your design beyond this Draw it out. If you have bugs, pictures can help the staff help you Size Unallocated b0 1 word Free Block Next Prev

Add Instrumentation Remember that measurements inform insights. Add temporary code to understand aspects of malloc Code can violate style rules or 128 byte limits, because it is temporary Particularly important to develop insights into performance before making changes What is expensive throughput-wise? How much might a change benefit utilization? Questions are rhetorical here

Add Instrumentation example Searching in find_fit is often the slowest step How efficient is your code? How might you know? Compute the ratio of blocks viewed to calls static block_t *find_fit(size_t asize) { block_t *block; for (block = heap_listp; get_size(block) > 0; block = find_next(block)) if (!(get_alloc(block)) && (asize <= get_size(block))) return block; } return NULL; // no fit found call_count++; block_count++; Print values in mm_init()

Add Instrumentation cont. What size of requests? How many 8 bytes or less? How many 16 bytes or less? What other sizes? What else could you measure? Why? Remember that although the system’s performance varies The mdriver’s traces are deterministic Measured results should not change between runs For example, depth of lists, frequency of splitting / coalescing, gains from first +N fit.

Use tools Use mm_checkheap() Use gdb Write it if you haven’t done so already Add new invariants when you add new features Know how to use the heap checker. Why do you need a heap checker? 2 reasons. Use gdb You can call print or mm_checkheap whenever you want in gdb. No need to add a while lot of printf’s. Offers useful information whenever you crash, like backtrace. 1: Find out what specific error has occurred. 2: Finds errors as soon as they occur.

mdriver-emulate Testing for 64-bit address space Use correctly sized masks, constants, and other variables Be careful about subtraction between size types (may re result in underflow/overflow) Reinitialize your pointers in mm_init

Garbled Bytes Malloc library returns a block Now what? mdriver writes bytes into payload (using memcpy) mdriver will check that those bytes are still present If malloc library has overwritten any bytes, then report garbled bytes Also checks for other kinds of bugs Now what? The mm_checkheap call is catching it right? If not, we want to find the garbled address and watch it

Garbled Bytes and gdb Get out a laptop Login to shark machine wget http://www.cs.cmu.edu/~213/activities/rec11b.tar tar xf rec11b.tar mm.c is a fake explicit list implementation. Source code is based on mm_baseline.c A few lines of code are added that vaguely resembles what an explicit list implementation could have.

GDB Exercise gdb --args ./mdriver -c ./traces/syn-array-short.rep -D (gdb) r // Sample output follows Throughput targets: min=6528, max=11750, benchmark=13056 Malloc size 9904 on address 0x800000010. ... ERROR [trace ././traces/syn-array-short.rep, line 12]: block 0 has 8 garbled bytes, starting at byte 0 Terminated with 2 errors [Inferior 1 (process 13470) exited normally] (gdb)

GDB Exercise cont. What is the first address that was garbled? Use gdb watch to find out when / what garbled it. (gdb) watch * 0x800000010 (gdb) run // Keep continuing through the breaks: // mm_init() // 4 x memcpy Hardware watchpoint 1: *0x800000010 Old value = -7350814 New value = 0 mm_malloc (size=50084) at mm.c:272 We just broke in after overwriting The below lines of code overwrite data (the latter causes garbled byte messages) explicit_list->previous = NULL; explicit_list->next = block;

Second Exercise Well fine, the bug from the first exercise was very artificial. No one just sets bytes to 0 for no reason. Try this more plausible exercise: $ gdb --args ./mdriver-2 -c traces/syn-array-short.rep What error was printed to the console? The function that prints the error is named malloc_error. Add a breakpoint for it if you want. This time, case 3 of coalesce returned block_next instead. There will be a message about an out-of-bounds block.

Second Exercise The library must’ve written the header and footer for the out-of-bounds payload at some point. Add a watchpoint for either address, or both. …So, the writes occurred in place. Is the place function wrong, or was it just given a bad argument? Hint: the bug is found in at basically the same place as last recitation’s bug. It’s caused by a careless typo, like nearly all others bugs.

Tips for using our tools Run mdriver with the –D option to detect garbled bytes as early as possible. Run it with –V to find out which trace caused the error. Note that sometimes, you get the error within the first few allocations. If so, you could set a breakpoint for mm_malloc / mm_free and step though every line. Print out local variables and convince yourself that they have the right values. For mdriver-emulate, you can still read memory from the simulated 64-bit address space using mem_read(address, 8) instead of x /gx.

MallocLab Due Thursday 7% of final grade (+ 4% for checkpoint) Read the writeup. It even has a list of tips on how to improve memory utilization. Rubber duck method If you explain to a rubber duck / TA what your function does step-by-step, while occasionally stopping to explain why you need each of those steps, you’d may very well find the bug in the middle of your explanation. Remember the “debug thought process” slide from Recitation 10?