University ¡of ¡Washington ¡
Today ¡
¢ More ¡on ¡memory ¡bugs ¡ ¢ Java ¡vs ¡C, ¡the ¡ba:le. ¡
1 ¡
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
University ¡of ¡Washington ¡
¢ More ¡on ¡memory ¡bugs ¡ ¢ Java ¡vs ¡C, ¡the ¡ba:le. ¡
1 ¡
University ¡of ¡Washington ¡
¢ Dereferencing ¡bad ¡pointers ¡ ¢ Reading ¡uniniEalized ¡memory ¡ ¢ OverwriEng ¡memory ¡ ¢ Referencing ¡nonexistent ¡variables ¡ ¢ Freeing ¡blocks ¡mulEple ¡Emes ¡ ¢ Referencing ¡freed ¡blocks ¡ ¢ Failing ¡to ¡free ¡blocks ¡
2 ¡
University ¡of ¡Washington ¡
¢ The ¡classic ¡scanf ¡bug ¡
3 ¡
University ¡of ¡Washington ¡
¢ Assuming ¡that ¡heap ¡data ¡is ¡iniEalized ¡to ¡zero ¡
4 ¡
University ¡of ¡Washington ¡
¢ AllocaEng ¡the ¡(possibly) ¡wrong ¡sized ¡object ¡
5 ¡
University ¡of ¡Washington ¡
¢ Off-‑by-‑one ¡error ¡
6 ¡
University ¡of ¡Washington ¡
¢ Not ¡checking ¡the ¡max ¡string ¡size ¡ ¢ Basis ¡for ¡classic ¡buffer ¡overflow ¡a:acks ¡
7 ¡
University ¡of ¡Washington ¡
¢ Misunderstanding ¡pointer ¡arithmeEc ¡
8 ¡
University ¡of ¡Washington ¡
¢ Forge\ng ¡that ¡local ¡variables ¡disappear ¡when ¡a ¡funcEon ¡
9 ¡
University ¡of ¡Washington ¡
¢ Nasty! ¡ ¢ What ¡does ¡the ¡free ¡list ¡look ¡like? ¡
10 ¡
University ¡of ¡Washington ¡
¢ Evil! ¡ ¡
11 ¡
University ¡of ¡Washington ¡
¢ Slow, ¡silent, ¡long-‑term ¡killer! ¡ ¡
12 ¡
University ¡of ¡Washington ¡
¢ Freeing ¡only ¡part ¡of ¡a ¡data ¡structure ¡
13 ¡
University ¡of ¡Washington ¡
¢ Referencing ¡a ¡pointer ¡instead ¡of ¡the ¡object ¡it ¡points ¡to ¡
14 ¡
University ¡of ¡Washington ¡
¢ Leaks? ¡ ¢ UniEalized ¡reads? ¡ ¢ Double ¡free? ¡
15 ¡
University ¡of ¡Washington ¡
¢ ConvenEonal ¡debugger ¡(gdb) ¡
¢ Debugging ¡malloc ¡(UToronto ¡CSRI ¡malloc) ¡
§ Memory ¡overwrites ¡that ¡corrupt ¡heap ¡structures ¡ § Some ¡instances ¡of ¡freeing ¡blocks ¡mul;ple ¡;mes ¡ § Memory ¡leaks ¡
§ Overwrites ¡into ¡the ¡middle ¡of ¡allocated ¡blocks ¡ § Freeing ¡block ¡twice ¡that ¡has ¡been ¡reallocated ¡in ¡the ¡interim ¡ § Referencing ¡freed ¡blocks ¡
16 ¡
University ¡of ¡Washington ¡
¢ Does ¡garbage ¡collecEon ¡solve ¡everything? ¡ ¢ If ¡not, ¡what ¡else ¡do ¡we ¡need? ¡
17 ¡
University ¡of ¡Washington ¡
¢ ReconnecEng ¡to ¡Java ¡
¢ Java ¡running ¡naEve ¡(compiled ¡to ¡C/assembly) ¡
¢ Java ¡on ¡a ¡virtual ¡machine ¡
18 ¡
University ¡of ¡Washington ¡
¢ None ¡of ¡this ¡data ¡representaEon ¡we ¡are ¡going ¡to ¡ ¡
¢ 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 ¡
19 ¡
University ¡of ¡Washington ¡
¢ Integers, ¡floats, ¡doubles, ¡pointers ¡– ¡same ¡as ¡C ¡
¢ Null ¡is ¡typically ¡represented ¡as ¡0 ¡ ¢ Characters ¡and ¡strings ¡ ¢ Arrays ¡ ¢ Objects ¡
20 ¡
University ¡of ¡Washington ¡
¢ Characters ¡and ¡strings ¡
§ Represents ¡most ¡of ¡the ¡world’s ¡alphabets ¡
§ Bounded ¡by ¡hidden ¡length ¡field ¡at ¡beginning ¡of ¡string ¡
21 ¡
University ¡of ¡Washington ¡
¢ Arrays ¡
§ array.length ¡returns ¡value ¡of ¡this ¡field ¡ § Hmm, ¡since ¡it ¡had ¡this ¡info, ¡what ¡can ¡it ¡do? ¡
22 ¡
University ¡of ¡Washington ¡
¢ Arrays ¡
§ array.length ¡returns ¡value ¡of ¡this ¡field ¡
§ Code ¡is ¡added ¡to ¡ensure ¡the ¡index ¡is ¡within ¡bounds ¡ § Trap ¡if ¡out-‑of-‑bounds ¡
23 ¡
University ¡of ¡Washington ¡
¢ Objects ¡(structs) ¡can ¡only ¡include ¡primiEve ¡data ¡types ¡
24 ¡
University ¡of ¡Washington ¡
¢ Pointers ¡in ¡C ¡can ¡point ¡to ¡any ¡memory ¡address ¡ ¢ References ¡in ¡Java ¡can ¡only ¡point ¡to ¡an ¡object ¡
25 ¡
University ¡of ¡Washington ¡
¢ In ¡C, ¡we ¡have ¡“-‑>” ¡and ¡“.” ¡for ¡field ¡selecEon ¡depending ¡on ¡
¢ In ¡Java, ¡all ¡variables ¡are ¡references ¡to ¡objects ¡
26 ¡
University ¡of ¡Washington ¡
¢ We ¡can ¡cast ¡any ¡pointer ¡into ¡any ¡other ¡pointer ¡
27 ¡
Cast ¡p ¡into ¡char ¡ pointer ¡so ¡that ¡ you ¡can ¡add ¡byte ¡
scaling ¡ Cast ¡back ¡into ¡ BlockInfo ¡pointer ¡ so ¡you ¡can ¡use ¡it ¡ as ¡BlockInfo ¡struct ¡
University ¡of ¡Washington ¡
¢ Can ¡only ¡cast ¡compaEble ¡object ¡references ¡
28 ¡
class Parent { int address; }; class Sister extends Parent{ int hers; }; class Brother extends Parent{ int his; }; // Parent is a super class of Brother and Sister, which are siblings Parent a = new Parent(); Sister xx = new Sister(); Brother xy = new Brother(); Parent p1 = new Sister(); // ok, everything needed for Parent
Parent p2 = p1; // ok, p1 is already a Parent Sister xx2 = new Brother(); // incompatible type – Brother and
Sister xx3 = new Parent(); // wrong direction; elements in Sister
Brother xy2 = (Brother) a; // run-time error; Parent does not contain
Sister xx4 = (Sister) p2; // ok, p2 started out as Sister Sister xx5 = (Sister) xy; // inconvertible types, xy is Brother class Object{ … };
University ¡of ¡Washington ¡
29 ¡
University ¡of ¡Washington ¡
¢ “new” ¡
§ Includes ¡space ¡for ¡“sta;c ¡fields” ¡and ¡pointers ¡to ¡methods’ ¡code ¡
¢ Runs ¡“constructor” ¡method ¡ ¢ Eventually ¡garbage ¡collected ¡if ¡all ¡references ¡ ¡
30 ¡
constructor samePlace
University ¡of ¡Washington ¡
¢ newPoint’s ¡fields ¡are ¡iniEalized ¡starEng ¡with ¡the ¡vtable ¡
¢ The ¡next ¡step ¡is ¡to ¡call ¡the ¡‘constructor’ ¡for ¡this ¡object ¡type ¡ ¢ Constructor ¡code ¡is ¡found ¡using ¡the ¡‘vtable ¡pointer’ ¡and ¡
31 ¡
constructor samePlace
University ¡of ¡Washington ¡
¢ Array ¡of ¡pointers ¡to ¡every ¡method ¡defined ¡for ¡the ¡object ¡
¢ Compiler ¡decided ¡in ¡which ¡element ¡of ¡the ¡array ¡to ¡put ¡each ¡
¢ Methods ¡are ¡just ¡funcEons ¡(as ¡in ¡C) ¡but ¡with ¡an ¡extra ¡
32 ¡
constructor samePlace
University ¡of ¡Washington ¡
¢ newPoint.samePlace(p2) ¡is ¡a ¡call ¡to ¡the ¡samePlace ¡method ¡of ¡
33 ¡
University ¡of ¡Washington ¡
¢ newPoint.samePlace(p2) ¡is ¡a ¡call ¡to ¡the ¡samePlace ¡method ¡of ¡
¢ In ¡C ¡
§ Gets ¡address ¡of ¡method’s ¡code ¡
§ Calls ¡method ¡with ¡references ¡to ¡object ¡and ¡parameter ¡
¢ We ¡need ¡‘this’ ¡so ¡that ¡we ¡can ¡read ¡the ¡x ¡and ¡y ¡of ¡our ¡object ¡
34 ¡
University ¡of ¡Washington ¡
¢ Where ¡does ¡“aNewField” ¡go? ¡
¢ Where ¡does ¡pointer ¡to ¡code ¡for ¡two ¡new ¡methods ¡go? ¡
35 ¡
class PtSubClass extends Point{ int aNewField; boolean samePlace(Point p2) { return false; } void sayHi() { System.out.println("hello"); } }
University ¡of ¡Washington ¡
36 ¡
class PtSubClass extends Point{ int aNewField; boolean samePlace(Point p2) { return false; } void sayHi() { System.out.println("hello"); } }
constructor samePlace
sayHi
University ¡of ¡Washington ¡
¢ Many ¡choices ¡in ¡how ¡to ¡implement ¡programming ¡models ¡ ¢ We’ve ¡talked ¡about ¡compilaEon, ¡can ¡also ¡interpret ¡
¢ InterpreEng ¡languages ¡has ¡a ¡long ¡history ¡
¢ Interpreted ¡implementaEons ¡are ¡very ¡much ¡with ¡us ¡today ¡
37 ¡
University ¡of ¡Washington ¡
¢ Really ¡a ¡conEnuum, ¡a ¡choice ¡to ¡be ¡made ¡
¢ Java ¡programs ¡are ¡usually ¡run ¡by ¡a ¡virtual ¡machine ¡
¢ Java ¡can ¡also ¡be ¡compiled ¡(just ¡as ¡a ¡C ¡program ¡is) ¡or ¡at ¡ ¡
38 ¡
University ¡of ¡Washington ¡
39 ¡
University ¡of ¡Washington ¡
¢ Making ¡Java ¡machine-‑independent ¡ ¢ Providing ¡stronger ¡protecEons ¡ ¢ VM ¡usually ¡implemented ¡in ¡C ¡ ¢ Stack ¡execuEon ¡model ¡ ¢ There ¡are ¡many ¡JVMs ¡
40 ¡
University ¡of ¡Washington ¡
41 ¡
mov 0x8001, %eax mov 0x8002, %edx add %edx, %eax mov %eax, 0x8003
iload 1 // push 1st argument from table onto stack iload 2 // push 2nd argument from table onto stack iadd // add and pop top 2 element, push result istore 3 // pop result and put it into third slot in table
University ¡of ¡Washington ¡
42 ¡
Method java.lang.String employeeName()
// pop an element from top of stack, retrieve the
aload_0 areturn getfield 00 05 00 05 B0 B4 2A
h:p://en.wikipedia.org/wiki/ Java_bytecode_instrucEon_lisEngs ¡ ¡
University ¡of ¡Washington ¡
¢ 10 ¡secEons ¡to ¡the ¡Java ¡class ¡file ¡structure ¡
43 ¡
University ¡of ¡Washington ¡
44 ¡
Compiled from Employee.java class Employee extends java.lang.Object { public Employee(java.lang.String,int); public java.lang.String employeeName(); public int employeeNumber(); }
0 aload_0 1 invokespecial #3 <Method java.lang.Object()> 4 aload_0 5 aload_1 6 putfield #5 <Field java.lang.String name> 9 aload_0 10 iload_2 11 putfield #4 <Field int idNumber> 14 aload_0 15 aload_1 16 iload_2 17 invokespecial #6 <Method void
20 return
0 aload_0 1 getfield #5 <Field java.lang.String name> 4 areturn
0 aload_0 1 getfield #4 <Field int idNumber> 4 ireturn
… javac Employee.java javap -c Employee > Employee.bc
University ¡of ¡Washington ¡
¢ Apart ¡from ¡the ¡Java ¡language ¡itself, ¡The ¡most ¡common ¡or ¡
45 ¡
University ¡of ¡Washington ¡
¢ C# ¡has ¡similar ¡moEvaEons ¡as ¡Java ¡ ¢ Virtual ¡machine ¡is ¡called ¡the ¡Common ¡Language ¡RunEme ¡(CLR) ¡ ¢ Common ¡Intermediate ¡Language ¡(CLI) ¡is ¡C#’s ¡byte-‑code ¡
46 ¡