Docsity

Memory Management III: Perils and Pitfalls - Memory-related Bugs and Debugging Techniques, Slides of Introduction to Computers

Partial preview of the text

Download Memory Management III: Perils and Pitfalls - Memory-related Bugs and Debugging Techniques and more Slides Introduction to Computers in PDF only on Docsity!

Memory Management III:

Perils and pitfalls

Oct 15, 1998

Topics

Review of C pointer references

Memory-related bugs

Debugging versions of malloc

Binary translation

Garbage collection

Introduction to Computer Systems

  • 2 –

class16.ppt

C operators

Operators

Associativity

[]

left to right

(type)

sizeof

right to left

left to right

left to right

left to right

left to right

left to right

left to right

^

left to right

left to right

left to right

left to right

right to left

^=

right to left

left to right

Note: Unary +, -, and * have higher precedence than binary forms

  • 4 –

class16.ppt

Memory-related bugs

Failing to free blocksReferencing freed blocksFreeing blocks multiple timesReferencing nonexistent variablesOverwriting memoryReading uninitialized memoryDereferencing bad pointers

  • 5 –

Dereferencing bad pointers

The classic scanf bug scanf(“%d”, val);

  • 7 –

Allocating the wrong sized object Overwriting memory

for (i=0; i<N; i++) {p = malloc(N*sizeof(int)); int **p; p[i] =

(^) malloc(M*sizeof(int));

  • 8 –

Overwriting memory

Off-by-one

for (i=0; i<=N; i++) {p = malloc(N*sizeof(int *)); int **p; p[i] =

(^) malloc(M*sizeof(int));

  • 10 –

Forgetting that strings end with ‘/0’ Overwriting memory

strcpy(t, s);char s[8] = “1234567”; char t[7];

  • 11 –

Not checking the max string size^ Overwriting memory

gets(s);int i; char s[8];

/* reads “123456789” from stdin */

  • 13 –

search(int *array,^ Misunderstanding pointer arithmetic^ Overwriting memory

(^) int val) {

while (*p && *p !=

(^) val)

p += (^) sizeof(int);

  • 14 –

class16.ppt^ Referencing nonexistent variables

Forgetting that local variables disappear when a

function returns

int *foo () { return &val;int val;

  • 16 –

Referencing freed blocks

Evil!

for (i=0; i<M; i++)y = malloc(Msizeof(int));...free(x); x = malloc(Nsizeof(int)); y[i] = x[i]++;

  • 17 –

Failing to free blocks

(memory leaks)

slow, long-term killer!

foo() { int (^) x = malloc(Nsizeof(int));

return;...

  • 19 –

class16.ppt

Dealing with memory bugs

Conventional debugger (gdb)

good for finding bad pointer dereferences

hard to detect the other memory bugs

Debugging malloc (CSRI UToronto malloc)

wrapper around conventional malloc

  • memory leaks– some instances of freeing blocks multiple times – memory overwrites that corrupt heap structures detects memory bugs at malloc and free boundaries
  • referencing freed blocks– freeing block twice that has been reallocated in the interim – overwrites into the middle of allocated blocks Cannot detect all memory bugs
  • 20 –

class16.ppt Dealing with memory bugs (cont)

Binary translator (Atom, Purify)

powerful debugging and analysis technique

rewrites text section of executable object file

can detect all errors as debugging malloc

  • referencing outside of allocated block– overwriting – bad pointers can also check each individual reference at runtime

Garbage collection (Boehm-Weiser Conservative GC)

let the system free blocks instead of the programmer