DYNAMIC MEMORY ALLOCATION: ADVANCED...

44
DYNAMIC MEMORY ALLOCATION: ADVANCED CONCEPTS CS 045 Computer Organization and Architecture Prof. Donald J. Patterson Adapted from Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition

Transcript of DYNAMIC MEMORY ALLOCATION: ADVANCED...

Page 1: DYNAMIC MEMORY ALLOCATION: ADVANCED CONCEPTSdjp3.westmont.edu/classes/2017_01_CS045/Lectures/Lecture_2017_04... · DYNAMIC MEMORY ALLOCATION: ADVANCED CONCEPTS CS 045 Computer Organization

DYNAMIC MEMORY ALLOCATION:ADVANCED CONCEPTSCS 045

Computer Organization and Architecture

Prof. Donald J. PattersonAdapted from Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition

Page 2: DYNAMIC MEMORY ALLOCATION: ADVANCED CONCEPTSdjp3.westmont.edu/classes/2017_01_CS045/Lectures/Lecture_2017_04... · DYNAMIC MEMORY ALLOCATION: ADVANCED CONCEPTS CS 045 Computer Organization

• EXPLICIT FREE LISTS

• SEGREGATED FREE LISTS

• GARBAGE COLLECTION

• MEMORY-RELATED PERILS AND PITFALLS

DYNAMIC MEMORY ALLOCATION: BASIC

Page 3: DYNAMIC MEMORY ALLOCATION: ADVANCED CONCEPTSdjp3.westmont.edu/classes/2017_01_CS045/Lectures/Lecture_2017_04... · DYNAMIC MEMORY ALLOCATION: ADVANCED CONCEPTS CS 045 Computer Organization

KEEPING TRACK OF FREE BLOCKS¢  Method1:Implicitfreelistusinglength—linksallblocks

¢  Method2:Explicitfreelistamongthefreeblocksusingpointers

¢  Method3:Segregatedfreelist

!  Differentfreelistsfordifferentsizeclasses

¢  Method4:Blockssortedbysize!  Canuseabalancedtree(e.g.Red-Blacktree)withpointerswithineach

freeblock,andthelengthusedasakey

5 4 26

5 4 26

Page 4: DYNAMIC MEMORY ALLOCATION: ADVANCED CONCEPTSdjp3.westmont.edu/classes/2017_01_CS045/Lectures/Lecture_2017_04... · DYNAMIC MEMORY ALLOCATION: ADVANCED CONCEPTS CS 045 Computer Organization

EXPLICIT FREE LISTS

¢  Maintainlist(s)offreeblocks,notallblocks!  The“next”freeblockcouldbeanywhere

!  Soweneedtostoreforward/backpointers,notjustsizes!  S>llneedboundarytagsforcoalescing!  Luckilywetrackonlyfreeblocks,sowecanusepayloadarea

Size

Payloadandpadding

a

Size a

Size a

Size a

Next

Prev

Allocated(asbefore) Free

Page 5: DYNAMIC MEMORY ALLOCATION: ADVANCED CONCEPTSdjp3.westmont.edu/classes/2017_01_CS045/Lectures/Lecture_2017_04... · DYNAMIC MEMORY ALLOCATION: ADVANCED CONCEPTS CS 045 Computer Organization

EXPLICIT FREE LISTS

¢  Logically:

¢  Physically:blockscanbeinanyorder

A B C

4 4 4 4 66 44 4 4

Forward(next)links

Back(prev)links

A B

C

Page 6: DYNAMIC MEMORY ALLOCATION: ADVANCED CONCEPTSdjp3.westmont.edu/classes/2017_01_CS045/Lectures/Lecture_2017_04... · DYNAMIC MEMORY ALLOCATION: ADVANCED CONCEPTS CS 045 Computer Organization

EXPLICIT FREE LISTS

¢  Logically:

¢  Physically:blockscanbeinanyorder

A B C

4 4 4 4 66 44 4 4

Forward(next)links

Back(prev)links

A B

C

Page 7: DYNAMIC MEMORY ALLOCATION: ADVANCED CONCEPTSdjp3.westmont.edu/classes/2017_01_CS045/Lectures/Lecture_2017_04... · DYNAMIC MEMORY ALLOCATION: ADVANCED CONCEPTS CS 045 Computer Organization

ALLOCATING FROM EXPLICIT FREE LISTS

Before

A(er

= malloc(…)

(withspli1ng)

conceptualgraphic

Page 8: DYNAMIC MEMORY ALLOCATION: ADVANCED CONCEPTSdjp3.westmont.edu/classes/2017_01_CS045/Lectures/Lecture_2017_04... · DYNAMIC MEMORY ALLOCATION: ADVANCED CONCEPTS CS 045 Computer Organization

FREEING WITH EXPLICIT FREE LISTS¢  Inser&onpolicy:Whereinthefreelistdoyouputanewly

freedblock?¢  LIFO(last-in-first-out)policy

!  Insertfreedblockatthebeginningofthefreelist!  Pro:simpleandconstant5me

!  Con:studiessuggestfragmenta5onisworsethanaddressordered

¢  Address-orderedpolicy!  Insertfreedblockssothatfreelistblocksarealwaysinaddressorder:

addr(prev)<addr(curr)<addr(next)

!  Con:requiressearch!  Pro:studiessuggestfragmenta5onislowerthanLIFO

Page 9: DYNAMIC MEMORY ALLOCATION: ADVANCED CONCEPTSdjp3.westmont.edu/classes/2017_01_CS045/Lectures/Lecture_2017_04... · DYNAMIC MEMORY ALLOCATION: ADVANCED CONCEPTS CS 045 Computer Organization

FREEING WITH A LIFO POLICY (CASE 1)

¢  Insertthefreedblockattherootofthelist

free( )

Root

Root

Before

A(er

conceptualgraphic

Page 10: DYNAMIC MEMORY ALLOCATION: ADVANCED CONCEPTSdjp3.westmont.edu/classes/2017_01_CS045/Lectures/Lecture_2017_04... · DYNAMIC MEMORY ALLOCATION: ADVANCED CONCEPTS CS 045 Computer Organization

FREEING WITH A LIFO POLICY (CASE 2)

¢  Spliceoutsuccessorblock,coalescebothmemoryblocksandinsertthenewblockattherootofthelist

free( )

Root

Before

Root

A(er

conceptualgraphic

Page 11: DYNAMIC MEMORY ALLOCATION: ADVANCED CONCEPTSdjp3.westmont.edu/classes/2017_01_CS045/Lectures/Lecture_2017_04... · DYNAMIC MEMORY ALLOCATION: ADVANCED CONCEPTS CS 045 Computer Organization

FREEING WITH A LIFO POLICY (CASE 3)

¢  Spliceoutpredecessorblock,coalescebothmemoryblocks,andinsertthenewblockattherootofthelist

free( )

Root

Root

Before

A(er

conceptualgraphic

Page 12: DYNAMIC MEMORY ALLOCATION: ADVANCED CONCEPTSdjp3.westmont.edu/classes/2017_01_CS045/Lectures/Lecture_2017_04... · DYNAMIC MEMORY ALLOCATION: ADVANCED CONCEPTS CS 045 Computer Organization

FREEING WITH A LIFO POLICY (CASE 4)

¢  Spliceoutpredecessorandsuccessorblocks,coalesceall3memoryblocksandinsertthenewblockattherootofthelist

free( )

Root

Before

Root

A(er

conceptualgraphic

Page 13: DYNAMIC MEMORY ALLOCATION: ADVANCED CONCEPTSdjp3.westmont.edu/classes/2017_01_CS045/Lectures/Lecture_2017_04... · DYNAMIC MEMORY ALLOCATION: ADVANCED CONCEPTS CS 045 Computer Organization

EXPLICIT LIST SUMMARY¢  Comparisontoimplicitlist:

!  Allocateislinear-meinnumberoffreeblocksinsteadofallblocks

!  Muchfasterwhenmostofthememoryisfull!  Slightlymorecomplicatedallocateandfreesinceneedstospliceblocks

inandoutofthelist

!  Someextraspaceforthelinks(2extrawordsneededforeachblock)

!  Doesthisincreaseinternalfragmenta-on?

¢  Mostcommonuseoflinkedlistsisinconjunc6onwithsegregatedfreelists!  Keepmul-plelinkedlistsofdifferentsizeclasses,orpossiblyfor

differenttypesofobjects

Page 14: DYNAMIC MEMORY ALLOCATION: ADVANCED CONCEPTSdjp3.westmont.edu/classes/2017_01_CS045/Lectures/Lecture_2017_04... · DYNAMIC MEMORY ALLOCATION: ADVANCED CONCEPTS CS 045 Computer Organization

KEEPING TRACK OF FREE BLOCKS

¢  Method1:Implicitlistusinglength—linksallblocks

¢  Method2:Explicitlistamongthefreeblocksusingpointers

¢  Method3:Segregatedfreelist

!  Differentfreelistsfordifferentsizeclasses

¢  Method4:Blockssortedbysize!  Canuseabalancedtree(e.g.Red-Blacktree)withpointerswithineach

freeblock,andthelengthusedasakey

5 4 26

5 4 26

Page 15: DYNAMIC MEMORY ALLOCATION: ADVANCED CONCEPTSdjp3.westmont.edu/classes/2017_01_CS045/Lectures/Lecture_2017_04... · DYNAMIC MEMORY ALLOCATION: ADVANCED CONCEPTS CS 045 Computer Organization

• EXPLICIT FREE LISTS

• SEGREGATED FREE LISTS

• GARBAGE COLLECTION

• MEMORY-RELATED PERILS AND PITFALLS

DYNAMIC MEMORY ALLOCATION: BASIC

Page 16: DYNAMIC MEMORY ALLOCATION: ADVANCED CONCEPTSdjp3.westmont.edu/classes/2017_01_CS045/Lectures/Lecture_2017_04... · DYNAMIC MEMORY ALLOCATION: ADVANCED CONCEPTS CS 045 Computer Organization

SEGREGATED LIST (SEGLIST) ALLOCATORS¢  Eachsizeclassofblockshasitsownfreelist

¢  O3enhaveseparateclassesforeachsmallsize¢  Forlargersizes:Oneclassforeachtwo-powersize

1-2

3

4

5-8

9-inf

Page 17: DYNAMIC MEMORY ALLOCATION: ADVANCED CONCEPTSdjp3.westmont.edu/classes/2017_01_CS045/Lectures/Lecture_2017_04... · DYNAMIC MEMORY ALLOCATION: ADVANCED CONCEPTS CS 045 Computer Organization

SEGLIST ALLOCATOR

¢  Givenanarrayoffreelists,eachoneforsomesizeclass

¢  Toallocateablockofsizen:!  Searchappropriatefreelistforblockofsizem>n!  Ifanappropriateblockisfound:

!  Splitblockandplacefragmentonappropriatelist(op:onal)!  Ifnoblockisfound,trynextlargerclass!  Repeatun:lblockisfound

¢  Ifnoblockisfound:!  Requestaddi:onalheapmemoryfromOS(usingsbrk())!  Allocateblockofnbytesfromthisnewmemory!  Placeremainderasasinglefreeblockinlargestsizeclass.

Page 18: DYNAMIC MEMORY ALLOCATION: ADVANCED CONCEPTSdjp3.westmont.edu/classes/2017_01_CS045/Lectures/Lecture_2017_04... · DYNAMIC MEMORY ALLOCATION: ADVANCED CONCEPTS CS 045 Computer Organization

SEGLIST ALLOCATOR (CONT.)

¢  Tofreeablock:!  Coalesceandplaceonappropriatelist

¢  Advantagesofseglistallocators!  Higherthroughput

!  log3meforpower-of-twosizeclasses

!  Be:ermemoryu3liza3on

!  First-fitsearchofsegregatedfreelistapproximatesabest-fitsearchofen3reheap.

!  Extremecase:Givingeachblockitsownsizeclassisequivalenttobest-fit.

Page 19: DYNAMIC MEMORY ALLOCATION: ADVANCED CONCEPTSdjp3.westmont.edu/classes/2017_01_CS045/Lectures/Lecture_2017_04... · DYNAMIC MEMORY ALLOCATION: ADVANCED CONCEPTS CS 045 Computer Organization

• D. Knuth, “The Art of Computer Programming”, 2nd edition, Addison Wesley, 1973

• The classic reference on dynamic storage allocation

• Wilson et al, “Dynamic Storage Allocation: A Survey and Critical Review”, Proc. 1995 Int’l Workshop on Memory Management, Kinross, Scotland, Sept, 1995.

• Comprehensive survey

• Available on Canvas

MORE INFO ON ALLOCATORS

Page 20: DYNAMIC MEMORY ALLOCATION: ADVANCED CONCEPTSdjp3.westmont.edu/classes/2017_01_CS045/Lectures/Lecture_2017_04... · DYNAMIC MEMORY ALLOCATION: ADVANCED CONCEPTS CS 045 Computer Organization

SIDENOTE: FAITH AND COMPUTER SCIENCE

How does a computer scientist understand infinity? What can probability theory teach us about free will? Can mathematical notions be used to enhance one's personal understanding of the Bible?

Perhaps no one is more qualified to address these questions than Donald E. Knuth, whose massive contributions to computing have led others to nickname him "The Father of Computer Science"—and whose religious faith led him to understand a fascinating analysis of the Bible called the 3:16 project. In this series of six spirited, informal lectures, Knuth explores the relationships between his vocation and his faith, revealing the unique perspective that his work with computing has lent to his understanding of God.

His starting point is the 3:16 project, an application of mathematical "random sampling" to the books of the Bible. The first lectures tell the story of the project's conception and execution, exploring its many dimensions of language translation, aesthetics, and theological history. Along the way, Knuth explains the many insights he gained from such interdisciplinary work. These theological musings culminate in a surprising final lecture tackling the ideas of infinity, free will, and some of the other big questions that lie at the juncture of theology and computation.

Things a Computer Scientist Rarely Talks About, with its charming and user-friendly format—each lecture ends with a question and answer exchange, and the book itself contains more than 100 illustrations—is a readable and intriguing approach to a crucial topic, certain to edify both those who are serious and curious about their faiths and those who look at the science of computation and wonder what it might teach them about their spiritual world.

from Publisher’s comments on “Things”

Page 21: DYNAMIC MEMORY ALLOCATION: ADVANCED CONCEPTSdjp3.westmont.edu/classes/2017_01_CS045/Lectures/Lecture_2017_04... · DYNAMIC MEMORY ALLOCATION: ADVANCED CONCEPTS CS 045 Computer Organization

• EXPLICIT FREE LISTS

• SEGREGATED FREE LISTS

• GARBAGE COLLECTION

• MEMORY-RELATED PERILS AND PITFALLS

DYNAMIC MEMORY ALLOCATION: BASIC

Page 22: DYNAMIC MEMORY ALLOCATION: ADVANCED CONCEPTSdjp3.westmont.edu/classes/2017_01_CS045/Lectures/Lecture_2017_04... · DYNAMIC MEMORY ALLOCATION: ADVANCED CONCEPTS CS 045 Computer Organization

IMPLICIT MEMORY MANAGEMENT:GARBAGE COLLECTION

¢  Garbagecollec+on:automa&creclama&onofheap-allocatedstorage—applica&onneverhastofree

¢  Commoninmanydynamiclanguages:!  Python,Ruby,Java,Perl,ML,Lisp,Mathema8ca

¢  Variants(“conserva&ve”garbagecollectors)existforCandC++!  However,cannotnecessarilycollectallgarbage

void foo() { int *p = malloc(128); return; /* p block is now garbage */ }

Page 23: DYNAMIC MEMORY ALLOCATION: ADVANCED CONCEPTSdjp3.westmont.edu/classes/2017_01_CS045/Lectures/Lecture_2017_04... · DYNAMIC MEMORY ALLOCATION: ADVANCED CONCEPTS CS 045 Computer Organization

GARBAGE COLLECTION

¢  Howdoesthememorymanagerknowwhenmemorycanbefreed?!  Ingeneralwecannotknowwhatisgoingtobeusedinthefuturesinceit

dependsoncondi6onals!  Butwecantellthatcertainblockscannotbeusedifthereareno

pointerstothem

¢  Mustmakecertainassump9onsaboutpointers!  Memorymanagercandis6nguishpointersfromnon-pointers!  Allpointerspointtothestartofablock!  Cannothidepointers

(e.g.,bycoercingthemtoanint,andthenbackagain)

Page 24: DYNAMIC MEMORY ALLOCATION: ADVANCED CONCEPTSdjp3.westmont.edu/classes/2017_01_CS045/Lectures/Lecture_2017_04... · DYNAMIC MEMORY ALLOCATION: ADVANCED CONCEPTS CS 045 Computer Organization

CLASSIC GC ALGORITHMS

¢  Mark-and-sweepcollec0on(McCarthy,1960)!  Doesnotmoveblocks(unlessyoualso“compact”)

¢  Referencecoun0ng(Collins,1960)!  Doesnotmoveblocks(notdiscussed)

¢  Copyingcollec0on(Minsky,1963)!  Movesblocks(notdiscussed)

¢  Genera0onalCollectors(LiebermanandHewiG,1983)!  Collec:onbasedonlife:mes

!  Mostalloca:onsbecomegarbageverysoon

!  Sofocusreclama:onworkonzonesofmemoryrecentlyallocated

¢  Formoreinforma0on:JonesandLin,“GarbageCollec,on:AlgorithmsforAutoma,cDynamicMemory”,JohnWiley&Sons,1996.

Page 25: DYNAMIC MEMORY ALLOCATION: ADVANCED CONCEPTSdjp3.westmont.edu/classes/2017_01_CS045/Lectures/Lecture_2017_04... · DYNAMIC MEMORY ALLOCATION: ADVANCED CONCEPTS CS 045 Computer Organization

MEMORY AS A GRAPH¢  Weviewmemoryasadirectedgraph

!  Eachblockisanodeinthegraph!  Eachpointerisanedgeinthegraph!  Loca4onsnotintheheapthatcontainpointersintotheheaparecalled

rootnodes(e.g.registers,loca4onsonthestack,globalvariables)

Rootnodes

Heapnodes

Not-reachable(garbage)

reachable

Anode(block)isreachableifthereisapathfromanyroottothatnode.

Non-reachablenodesaregarbage(cannotbeneededbytheapplica>on)

Page 26: DYNAMIC MEMORY ALLOCATION: ADVANCED CONCEPTSdjp3.westmont.edu/classes/2017_01_CS045/Lectures/Lecture_2017_04... · DYNAMIC MEMORY ALLOCATION: ADVANCED CONCEPTS CS 045 Computer Organization

MARK AND SWEEP COLLECTING¢  Canbuildontopofmalloc/freepackage

!  Allocateusingmallocun.lyou“runoutofspace”¢  Whenoutofspace:

!  Useextramarkbitintheheadofeachblock

!  Mark:Startatrootsandsetmarkbitoneachreachableblock!  Sweep:Scanallblocksandfreeblocksthatarenotmarked

A0ermark Markbitset

A0ersweep freefree

root

Beforemark

Note:arrowsheredenote

memoryrefs,notfreelistptrs.

Page 27: DYNAMIC MEMORY ALLOCATION: ADVANCED CONCEPTSdjp3.westmont.edu/classes/2017_01_CS045/Lectures/Lecture_2017_04... · DYNAMIC MEMORY ALLOCATION: ADVANCED CONCEPTS CS 045 Computer Organization

ASSUMPTIONS FOR A SIMPLE IMPLEMENTATION

¢  Applica'on!  new(n):returnspointertonewblockwithallloca2onscleared!  read(b,i):readloca2oniofblockbintoregister!  write(b,i,v): writevintoloca2oniofblockb

¢  Eachblockwillhaveaheaderword!  addressedasb[-1],forablockb !  Usedfordifferentpurposesindifferentcollectors

¢  Instruc'onsusedbytheGarbageCollector!  is_ptr(p):determineswhetherpisapointer!  length(b):returnsthelengthofblockb,notincludingtheheader!  get_roots():returnsalltheroots

Page 28: DYNAMIC MEMORY ALLOCATION: ADVANCED CONCEPTSdjp3.westmont.edu/classes/2017_01_CS045/Lectures/Lecture_2017_04... · DYNAMIC MEMORY ALLOCATION: ADVANCED CONCEPTS CS 045 Computer Organization

MARK AND SWEEP (CONT.)

ptr mark(ptr p) { 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++) // call mark on all words mark(p[i]); // in the block return; }

Markusingdepth-firsttraversalofthememorygraph

Sweepusinglengthstofindnextblockptr sweep(ptr p, ptr end) { while (p < end) { if markBitSet(p) clearMarkBit(); else if (allocateBitSet(p)) free(p); p += length(p); }

Page 29: DYNAMIC MEMORY ALLOCATION: ADVANCED CONCEPTSdjp3.westmont.edu/classes/2017_01_CS045/Lectures/Lecture_2017_04... · DYNAMIC MEMORY ALLOCATION: ADVANCED CONCEPTS CS 045 Computer Organization

CONSERVATIVE MARK & SWEEP IN C

¢  A“conserva,vegarbagecollector”forCprograms!  is_ptr()determinesifawordisapointerbycheckingifitpointsto

anallocatedblockofmemory

!  But,inCpointerscanpointtothemiddleofablock

¢  Sohowtofindthebeginningoftheblock?!  Canuseabalancedbinarytreetokeeptrackofallallocatedblocks(key

isstart-of-block)

!  Balanced-treepointerscanbestoredinheader(usetwoaddi=onalwords)

Header

ptr

Head Data

LeA Right

SizeLeA:smalleraddressesRight:largeraddresses

Page 30: DYNAMIC MEMORY ALLOCATION: ADVANCED CONCEPTSdjp3.westmont.edu/classes/2017_01_CS045/Lectures/Lecture_2017_04... · DYNAMIC MEMORY ALLOCATION: ADVANCED CONCEPTS CS 045 Computer Organization

• EXPLICIT FREE LISTS

• SEGREGATED FREE LISTS

• GARBAGE COLLECTION

• MEMORY-RELATED PERILS AND PITFALLS

DYNAMIC MEMORY ALLOCATION: BASIC

Page 31: DYNAMIC MEMORY ALLOCATION: ADVANCED CONCEPTSdjp3.westmont.edu/classes/2017_01_CS045/Lectures/Lecture_2017_04... · DYNAMIC MEMORY ALLOCATION: ADVANCED CONCEPTS CS 045 Computer Organization

MEMORY-RELATED PERILS AND PITFALLS

• Dereferencing bad pointers

• Reading uninitialized memory

• Overwriting memory

• Referencing nonexistent variables

• Freeing blocks multiple times

• Referencing freed blocks

• Failing to free blocks

Page 32: DYNAMIC MEMORY ALLOCATION: ADVANCED CONCEPTSdjp3.westmont.edu/classes/2017_01_CS045/Lectures/Lecture_2017_04... · DYNAMIC MEMORY ALLOCATION: ADVANCED CONCEPTS CS 045 Computer Organization

C OPERATORSOperators Associa-vity() [] -> . le#toright! ~ ++ -- + - * & (type) sizeof righttole#* / % le#toright+ - le#toright<< >> le#toright< <= > >= le#toright== != le#toright& le#toright^ le#toright| le#toright&& le#toright|| le#toright?: righttole#= += -= *= /= %= &= ^= != <<= >>= righttole#, le#toright

¢  ->,(),and[]havehighprecedence,with*and&justbelow¢  Unary+, -,and*havehigherprecedencethanbinaryforms

Source:K&Rpage53

Page 33: DYNAMIC MEMORY ALLOCATION: ADVANCED CONCEPTSdjp3.westmont.edu/classes/2017_01_CS045/Lectures/Lecture_2017_04... · DYNAMIC MEMORY ALLOCATION: ADVANCED CONCEPTS CS 045 Computer Organization

DEREFERENCING BAD POINTERS

¢  Theclassicscanfbug

int val; ... scanf(“%d”, val);

Page 34: DYNAMIC MEMORY ALLOCATION: ADVANCED CONCEPTSdjp3.westmont.edu/classes/2017_01_CS045/Lectures/Lecture_2017_04... · DYNAMIC MEMORY ALLOCATION: ADVANCED CONCEPTS CS 045 Computer Organization

READING UNINITIALIZED MEMORY

¢  Assumingthatheapdataisini/alizedtozero

/* return y = Ax */ int *matvec(int **A, int *x) { int *y = 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; }

Page 35: DYNAMIC MEMORY ALLOCATION: ADVANCED CONCEPTSdjp3.westmont.edu/classes/2017_01_CS045/Lectures/Lecture_2017_04... · DYNAMIC MEMORY ALLOCATION: ADVANCED CONCEPTS CS 045 Computer Organization

OVERWRITING MEMORY

¢  Alloca&ngthe(possibly)wrongsizedobject

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

Page 36: DYNAMIC MEMORY ALLOCATION: ADVANCED CONCEPTSdjp3.westmont.edu/classes/2017_01_CS045/Lectures/Lecture_2017_04... · DYNAMIC MEMORY ALLOCATION: ADVANCED CONCEPTS CS 045 Computer Organization

OVERWRITING MEMORY

¢  Off-by-oneerror

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

Page 37: DYNAMIC MEMORY ALLOCATION: ADVANCED CONCEPTSdjp3.westmont.edu/classes/2017_01_CS045/Lectures/Lecture_2017_04... · DYNAMIC MEMORY ALLOCATION: ADVANCED CONCEPTS CS 045 Computer Organization

OVERWRITING MEMORY

¢  Notcheckingthemaxstringsize

¢  Basisforclassicbufferoverflowa;acks

char s[8]; int i; gets(s); /* reads “123456789” from stdin */

Page 38: DYNAMIC MEMORY ALLOCATION: ADVANCED CONCEPTSdjp3.westmont.edu/classes/2017_01_CS045/Lectures/Lecture_2017_04... · DYNAMIC MEMORY ALLOCATION: ADVANCED CONCEPTS CS 045 Computer Organization

OVERWRITING MEMORY

¢  Misunderstandingpointerarithme1c

int *search(int *p, int val) { while (*p && *p != val) p += sizeof(int); return p; }

Page 39: DYNAMIC MEMORY ALLOCATION: ADVANCED CONCEPTSdjp3.westmont.edu/classes/2017_01_CS045/Lectures/Lecture_2017_04... · DYNAMIC MEMORY ALLOCATION: ADVANCED CONCEPTS CS 045 Computer Organization

OVERWRITING MEMORY

¢  Referencingapointerinsteadoftheobjectitpointsto

int *BinheapDelete(int **binheap, int *size) { int *packet; packet = binheap[0]; binheap[0] = binheap[*size - 1]; *size--; Heapify(binheap, *size, 0); return(packet); }

Page 40: DYNAMIC MEMORY ALLOCATION: ADVANCED CONCEPTSdjp3.westmont.edu/classes/2017_01_CS045/Lectures/Lecture_2017_04... · DYNAMIC MEMORY ALLOCATION: ADVANCED CONCEPTS CS 045 Computer Organization

REFERENCING NONEXISTENT VARIABLES

¢  Forge&ngthatlocalvariablesdisappearwhenafunc7onreturns

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

Page 41: DYNAMIC MEMORY ALLOCATION: ADVANCED CONCEPTSdjp3.westmont.edu/classes/2017_01_CS045/Lectures/Lecture_2017_04... · DYNAMIC MEMORY ALLOCATION: ADVANCED CONCEPTS CS 045 Computer Organization

FREEING BLOCKS MULTIPLE TIMES

¢  Nasty!

x = malloc(N*sizeof(int)); <manipulate x> free(x); y = malloc(M*sizeof(int)); <manipulate y> free(x);

Page 42: DYNAMIC MEMORY ALLOCATION: ADVANCED CONCEPTSdjp3.westmont.edu/classes/2017_01_CS045/Lectures/Lecture_2017_04... · DYNAMIC MEMORY ALLOCATION: ADVANCED CONCEPTS CS 045 Computer Organization

DEALING WITH MEMORY BUGS

¢  Debugger:gdb !  Goodforfindingbadpointerdereferences!  Hardtodetecttheothermemorybugs

¢  Datastructureconsistencychecker!  Runssilently,printsmessageonlyonerror!  Useasaprobetozeroinonerror

¢  Binarytranslator:valgrind!  Powerfuldebuggingandanalysistechnique!  Rewritestextsec@onofexecutableobjectfile!  Checkseachindividualreferenceatrun@me

!  Badpointers,overwrites,refsoutsideofallocatedblock

¢  glibcmalloccontainscheckingcode!  setenv MALLOC_CHECK_ 3

Page 43: DYNAMIC MEMORY ALLOCATION: ADVANCED CONCEPTSdjp3.westmont.edu/classes/2017_01_CS045/Lectures/Lecture_2017_04... · DYNAMIC MEMORY ALLOCATION: ADVANCED CONCEPTS CS 045 Computer Organization
Page 44: DYNAMIC MEMORY ALLOCATION: ADVANCED CONCEPTSdjp3.westmont.edu/classes/2017_01_CS045/Lectures/Lecture_2017_04... · DYNAMIC MEMORY ALLOCATION: ADVANCED CONCEPTS CS 045 Computer Organization

SUMMARY