today
play

Today More on memory bugs Java vs C, the ba:le. - PowerPoint PPT Presentation

University of Washington Today More on memory bugs Java vs C, the ba:le. 1 University of Washington Memory-Related Perils and PiAalls


  1. University ¡of ¡Washington ¡ Today ¡ ¢ More ¡on ¡memory ¡bugs ¡ ¢ Java ¡vs ¡C, ¡the ¡ba:le. ¡ 1 ¡

  2. University ¡of ¡Washington ¡ Memory-­‑Related ¡Perils ¡and ¡PiAalls ¡ ¢ Dereferencing ¡bad ¡pointers ¡ ¢ Reading ¡uniniEalized ¡memory ¡ ¢ OverwriEng ¡memory ¡ ¢ Referencing ¡nonexistent ¡variables ¡ ¢ Freeing ¡blocks ¡mulEple ¡Emes ¡ ¢ Referencing ¡freed ¡blocks ¡ ¢ Failing ¡to ¡free ¡blocks ¡ 2 ¡

  3. University ¡of ¡Washington ¡ Dereferencing ¡Bad ¡Pointers ¡ ¢ The ¡classic ¡ scanf ¡bug ¡ int val; ... scanf(“%d”, val); 3 ¡

  4. University ¡of ¡Washington ¡ Reading ¡UniniEalized ¡Memory ¡ ¢ Assuming ¡that ¡heap ¡data ¡is ¡iniEalized ¡to ¡zero ¡ /* 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; } 4 ¡

  5. University ¡of ¡Washington ¡ OverwriEng ¡Memory ¡ ¢ AllocaEng ¡the ¡(possibly) ¡wrong ¡sized ¡object ¡ int **p; p = malloc( N * sizeof(int) ); for (i=0; i<N; i++) { p[i] = malloc( M * sizeof(int) ); } 5 ¡

  6. University ¡of ¡Washington ¡ OverwriEng ¡Memory ¡ ¢ Off-­‑by-­‑one ¡error ¡ int **p; p = malloc( N * sizeof(int *) ); for (i=0; i<=N; i++) { p[i] = malloc( M * sizeof(int) ); } 6 ¡

  7. University ¡of ¡Washington ¡ OverwriEng ¡Memory ¡ ¢ Not ¡checking ¡the ¡max ¡string ¡size ¡ char s[8]; int i; gets(s); /* reads “123456789” from stdin */ ¢ Basis ¡for ¡classic ¡buffer ¡overflow ¡a:acks ¡ § Your ¡last ¡assignment ¡ 7 ¡

  8. University ¡of ¡Washington ¡ OverwriEng ¡Memory ¡ ¢ Misunderstanding ¡pointer ¡arithmeEc ¡ int *search(int *p, int val) { while (*p && *p != val) p += sizeof(int); return p; } 8 ¡

  9. University ¡of ¡Washington ¡ Referencing ¡Nonexistent ¡Variables ¡ ¢ Forge\ng ¡that ¡local ¡variables ¡disappear ¡when ¡a ¡funcEon ¡ returns ¡ int *foo () { int val; return &val; } 9 ¡

  10. University ¡of ¡Washington ¡ Freeing ¡Blocks ¡MulEple ¡Times ¡ ¢ Nasty! ¡ x = malloc( N * sizeof(int) ); <manipulate x> free(x); y = malloc( M * sizeof(int) ); <manipulate y> free(x); ¢ What ¡does ¡the ¡free ¡list ¡look ¡like? ¡ x = malloc( N * sizeof(int) ); <manipulate x> free(x); free(x); 10 ¡

  11. University ¡of ¡Washington ¡ Referencing ¡Freed ¡Blocks ¡ ¢ Evil! ¡ ¡ x = malloc( N * sizeof(int) ); <manipulate x> free(x); ... y = malloc( M * sizeof(int) ); for (i=0; i<M; i++) y[i] = x[i]++; 11 ¡

  12. University ¡of ¡Washington ¡ Failing ¡to ¡Free ¡Blocks ¡(Memory ¡Leaks) ¡ ¢ Slow, ¡silent, ¡long-­‑term ¡killer! ¡ ¡ foo() { int *x = malloc(N*sizeof(int)); ... return; } 12 ¡

  13. University ¡of ¡Washington ¡ Failing ¡to ¡Free ¡Blocks ¡(Memory ¡Leaks) ¡ ¢ Freeing ¡only ¡part ¡of ¡a ¡data ¡structure ¡ struct list { int val; struct list *next; }; foo() { struct list *head = malloc( sizeof(struct list) ); head->val = 0; head->next = NULL; <create and manipulate the rest of the list> ... free(head); return; } 13 ¡

  14. University ¡of ¡Washington ¡ OverwriEng ¡Memory ¡ ¢ Referencing ¡a ¡pointer ¡instead ¡of ¡the ¡object ¡it ¡points ¡to ¡ int *getPacket(int **packets, int *size) { int *packet; packet = packets[0]; packets[0] = packets[*size - 1]; *size--; // what is happening here? reorderPackets(packets, *size, 0); return(packet); } 14 ¡

  15. University ¡of ¡Washington ¡ Dealing ¡With ¡Memory ¡Bugs? ¡ ¢ Leaks? ¡ ¢ UniEalized ¡reads? ¡ ¢ Double ¡free? ¡ 15 ¡

  16. University ¡of ¡Washington ¡ Dealing ¡With ¡Memory ¡Bugs ¡ ¢ ConvenEonal ¡debugger ¡( gdb ) ¡ § Good ¡for ¡finding ¡ ¡bad ¡pointer ¡dereferences ¡ § Hard ¡to ¡detect ¡the ¡other ¡memory ¡bugs ¡ ¢ Debugging ¡ malloc ¡(UToronto ¡CSRI ¡ malloc ) ¡ § Wrapper ¡around ¡conven;onal ¡ malloc § Detects ¡memory ¡bugs ¡at ¡ malloc ¡and ¡ free boundaries ¡ § Memory ¡overwrites ¡that ¡corrupt ¡heap ¡structures ¡ § Some ¡instances ¡of ¡freeing ¡blocks ¡mul;ple ¡;mes ¡ § Memory ¡leaks ¡ § Cannot ¡detect ¡all ¡memory ¡bugs ¡ § Overwrites ¡into ¡the ¡middle ¡of ¡allocated ¡blocks ¡ § Freeing ¡block ¡twice ¡that ¡has ¡been ¡reallocated ¡in ¡the ¡interim ¡ § Referencing ¡freed ¡blocks ¡ 16 ¡

  17. University ¡of ¡Washington ¡ How ¡would ¡you ¡make ¡memory ¡bugs ¡go ¡ away? ¡(puff) ¡ ¢ Does ¡garbage ¡collecEon ¡solve ¡everything? ¡ ¢ If ¡not, ¡what ¡else ¡do ¡we ¡need? ¡ 17 ¡

  18. University ¡of ¡Washington ¡ Java ¡vs ¡C ¡ ¢ ReconnecEng ¡to ¡Java ¡ § Back ¡to ¡CSE143! ¡ § But ¡now ¡you ¡know ¡a ¡lot ¡more ¡about ¡what ¡really ¡happens ¡ when ¡we ¡execute ¡programs ¡ ¡ ¢ Java ¡running ¡naEve ¡(compiled ¡to ¡C/assembly) ¡ § Object ¡representa;ons: ¡arrays, ¡strings, ¡etc. ¡ § Bounds ¡checking ¡ § Memory ¡alloca;on, ¡constructors ¡ § Garbage ¡collec;on ¡ ¢ Java ¡on ¡a ¡virtual ¡machine ¡ § Virtual ¡processor ¡ § Another ¡language: ¡byte-­‑codes ¡ 18 ¡

  19. University ¡of ¡Washington ¡ Meta-­‑point ¡to ¡this ¡lecture ¡ ¢ None ¡of ¡this ¡data ¡representaEon ¡we ¡are ¡going ¡to ¡ ¡ talk ¡about ¡is ¡ guaranteed ¡ by ¡Java ¡ ¡ ¢ In ¡fact, ¡the ¡language ¡simply ¡provides ¡an ¡ abstrac-on ¡ ¢ We ¡can't ¡easily ¡tell ¡how ¡things ¡are ¡really ¡represented ¡ ¢ But ¡it ¡is ¡important ¡to ¡understand ¡ an ¡ implementaEon ¡of ¡the ¡ lower ¡levels ¡-­‑-­‑-­‑ ¡ ¡it ¡may ¡be ¡useful ¡in ¡thinking ¡about ¡your ¡ program ¡ 19 ¡

  20. University ¡of ¡Washington ¡ Data ¡in ¡Java ¡ ¢ Integers, ¡floats, ¡doubles, ¡pointers ¡– ¡same ¡as ¡C ¡ § Yes, ¡Java ¡has ¡pointers ¡– ¡they ¡are ¡called ¡‘references’ ¡– ¡however, ¡Java ¡ references ¡are ¡much ¡more ¡constrained ¡than ¡C’s ¡general ¡pointers ¡ ¢ Null ¡is ¡typically ¡represented ¡as ¡0 ¡ ¢ Characters ¡and ¡strings ¡ ¢ Arrays ¡ ¢ Objects ¡ 20 ¡

  21. University ¡of ¡Washington ¡ Data ¡in ¡Java ¡ ¢ Characters ¡and ¡strings ¡ § Two-­‑byte ¡Unicode ¡instead ¡of ¡ASCII ¡ § Represents ¡most ¡of ¡the ¡world’s ¡alphabets ¡ § String ¡not ¡bounded ¡by ¡a ¡‘/0’ ¡(null ¡character) ¡ § Bounded ¡by ¡hidden ¡length ¡field ¡at ¡beginning ¡of ¡string ¡ the ¡string ¡‘CSE351’: ¡ C: ¡ASCII ¡ 53 45 33 35 31 43 \0 16 0 1 4 7 Java: ¡Unicode ¡ 6 00 43 00 53 00 45 00 33 00 35 00 31 21 ¡

  22. University ¡of ¡Washington ¡ Data ¡in ¡Java ¡ ¢ Arrays ¡ § Bounds ¡specified ¡in ¡hidden ¡fields ¡at ¡start ¡of ¡array ¡(int ¡– ¡4 ¡bytes) ¡ § array.length ¡ returns ¡value ¡of ¡this ¡field ¡ § Hmm, ¡since ¡it ¡had ¡this ¡info, ¡what ¡can ¡it ¡do? ¡ § Every ¡element ¡ini;alized ¡to ¡0 ¡ int ¡array[5]: ¡ C ¡ ?? ?? ?? ?? ?? 0 4 20 24 Java ¡ 5 00 00 00 00 00 22 ¡

  23. University ¡of ¡Washington ¡ Data ¡in ¡Java ¡ ¢ Arrays ¡ § Bounds ¡specified ¡in ¡hidden ¡fields ¡at ¡start ¡of ¡array ¡(int ¡– ¡4 ¡bytes) ¡ § array.length ¡ returns ¡value ¡of ¡this ¡field ¡ § Every ¡access ¡triggers ¡a ¡bounds-­‑check ¡ § Code ¡is ¡added ¡to ¡ensure ¡the ¡index ¡is ¡within ¡bounds ¡ § Trap ¡if ¡out-­‑of-­‑bounds ¡ § Every ¡element ¡ini;alized ¡to ¡0 ¡ int ¡array[5]: ¡ C ¡ ?? ?? ?? ?? ?? 0 4 20 24 Java ¡ 5 00 00 00 00 00 23 ¡

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend