Carnegie Mellon Machine Level Programming V Advanced Topics 15 213 Introduc0on to Computer Systems 8th Lecture Sep 16 2010 Instructors Randy Bryant Dave O Hallaron 1 Carnegie Mellon Today Structures Alignment Unions Memory Layout Bu er Over ow Vulnerability Protec0on 2 Carnegie Mellon Structures Alignment Unaligned Data c i 0 p p 1 i 1 p 5 v p 9 p 17 struct S1 char c int i 2 double v p Aligned Data Primi0ve data type requires K bytes Address must be mul0ple of K c p 0 3 bytes i 0 p 4 MulIple of 4 MulIple of 8 i 1 p 8 4 bytes p 16 v p 24 MulIple of 8 MulIple of 8 3 Carnegie Mellon Alignment Principles Aligned Data Primi0ve data type requires K bytes Address must be mul0ple of K Required on some machines advised on IA32 treated di erently by IA32 Linux x86 64 Linux and Windows MoIvaIon for Aligning Data Memory accessed by aligned chunks of 4 or 8 bytes system dependent Ine cient to load or store datum that spans quad word boundaries Virtual memory very tricky when datum spans 2 pages Compiler Inserts gaps in structure to ensure correct alignment of elds 4 Carnegie Mellon Speci c Cases of Alignment IA32 1 byte char no restric0ons on address 2 bytes short lowest 1 bit of address must be 02 4 bytes int float char lowest 2 bits of address must be 002 8 bytes double Windows and most other OS s instruc0on sets lowest 3 bits of address must be 0002 Linux lowest 2 bits of address must be 002 i e treated the same as a 4 byte primi0ve data type 12 bytes long double Windows Linux lowest 2 bits of address must be 002 i e treated the same as a 4 byte primi0ve data type 5 Carnegie Mellon Speci c Cases of Alignment x86 64 1 byte char no restric0ons on address 2 bytes short lowest 1 bit of address must be 02 4 bytes int float lowest 2 bits of address must be 002 8 bytes double char Windows Linux lowest 3 bits of address must be 0002 16 bytes long double Linux lowest 3 bits of address must be 0002 i e treated the same as a 8 byte primi0ve data type 6 Carnegie Mellon SaIsfying Alignment with Structures Within structure Must sa0sfy each element s alignment requirement Overall structure placement Each structure has alignment requirement K struct S1 char c int i 2 double v p K Largest alignment of any element Ini0al address structure length must be mul0ples of K Example under Windows or x86 64 K 8 due to double element c p 0 3 bytes i 0 p 4 MulIple of 4 MulIple of 8 i 1 p 8 4 bytes p 16 v p 24 MulIple of 8 MulIple of 8 7 Carnegie Mellon Di erent Alignment ConvenIons struct S1 char c int i 2 double v p x86 64 or IA32 Windows K 8 due to double element c p 0 3 bytes i 0 p 4 4 bytes i 1 p 8 v p 16 p 24 IA32 Linux K 4 double treated like a 4 byte data type c p 0 3 bytes p 4 i 0 i 1 p 8 v p 12 p 20 8 Carnegie Mellon MeeIng Overall Alignment Requirement For largest alignment requirement K Overall structure must be mulIple of K v p 0 i 0 p 8 i 1 struct S2 double v int i 2 char c p c p 16 7 bytes p 24 9 Carnegie Mellon Arrays of Structures struct S2 double v int i 2 char c a 10 Overall structure length mulIple of K SaIsfy alignment requirement for every element a 0 a 0 a 1 a 24 v a 24 i 0 a 32 a 2 a 48 i 1 a 72 c a 40 7 bytes a 48 10 Carnegie Mellon Accessing Array Elements Compute array o set 12i sizeof S3 including alignment spacers struct S3 short i float v short j a 10 Element j is at o set 8 within structure Assembler gives o set a 8 Resolved during linking a 0 a 0 a 12 a i a 12i i a 12i short get j int idx return a idx j 2 bytes v j a 12i 8 2 bytes eax idx leal eax eax 2 eax 3 idx movswl a 8 eax 4 eax 11 Carnegie Mellon Saving Space Put large data types rst struct S5 int i char c char d p struct S4 char c int i char d p E ect K 4 c 3 bytes i i c d d 3 bytes 2 bytes 12 Carnegie Mellon Today Structures Alignment Unions Memory Layout Bu er Over ow Vulnerability Protec0on 13 Carnegie Mellon Union AllocaIon Allocate according to largest element Can only use one eld at a Ime union U1 char c int i 2 double v up c i 0 v up 0 struct S1 char c int i 2 double v sp c sp 0 3 bytes sp 4 i 1 i 0 i 1 sp 8 up 4 4 bytes sp 16 up 8 v sp 24 14 Carnegie Mellon Using Union to Access Bit Pa erns typedef union float f unsigned u bit float t u f 0 4 float bit2float unsigned u bit float t arg arg u u return arg f unsigned float2bit float f bit float t arg arg f f return arg u Same as float u Same as unsigned f 15 Carnegie Mellon Byte Ordering Revisited Idea Short long quad words stored in memory as 2 4 8 consecu0ve bytes Which is most least signi cant Can cause problems when exchanging binary data between machines Big Endian Most signi cant byte has lowest address Sparc Li le Endian Least signi cant byte has lowest address Intel x86 16 Carnegie Mellon Byte Ordering Example union unsigned unsigned unsigned unsigned dw 32 bit char c 8 short s 4 int i 2 long l 1 c 0 c 1 c 2 c 3 c 4 c 5 c 6 c 7 s 0 s 1 s 2 i 0 s 3 i 1 l 0 64 bit c 0 c 1 c 2 c 3 c 4 c 5 c 6 c 7 s 0 s 1 s 2 i 0 s 3 i 1 l 0 17 Carnegie Mellon Byte Ordering Example Cont int j for j 0 j 8 j dw c j 0xf0 j printf Characters 0 7 0x x 0x x 0x x 0x x 0x x 0x x 0x x 0x x n dw c 0 dw c 1 dw c 2 dw c 3 dw c 4 dw c 5 dw c 6 dw c 7 printf Shorts 0 3 0x x 0x x 0x x 0x x n dw s 0 dw s 1 dw s 2 dw s 3 printf Ints 0 1 0x x 0x x n dw i 0 dw i 1 printf Long 0 0x lx n dw l …
View Full Document