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

A class presentation from a computer systems course (cs 213) focusing on memory management in c programming. It covers various memory-related bugs, such as dereferencing bad pointers, reading uninitialized memory, overwriting memory, and referencing nonexistent variables. The document also discusses debugging techniques, including using conventional debuggers, debugging versions of malloc, binary translation, and garbage collection.

Typology: Slides

2010/2011

Uploaded on 10/07/2011

rolla45
rolla45 🇺🇸

4

(6)

133 documents

1 / 37

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
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
class16.ppt
15-213
Introduction to Computer Systems
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25

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