Recitation11 Malloc2
Recitation11 Malloc2
Instructor: TA(s)
0 4 4 4 4 6 6 04 4 0
Unallocated
Size b0
Free
Block
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 4
Carnegie Mellon
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
Use tools
Use mm_checkheap()
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.
mdriver-emulate
Testing for 64-bit address space
Garbled Bytes
Malloc library returns a block
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?
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)
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:
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.
MallocLab
Due Thursday