DOC PREVIEW
CMU CS 15213 - 08-machine-advanced

This preview shows page 1-2-3-4-24-25-26-50-51-52-53 out of 53 pages.

Save
View full document
Premium Document
Do you want full access? Go Premium and unlock all 53 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

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

CMU CS 15213 - 08-machine-advanced

Documents in this Course
lecture

lecture

14 pages

lecture

lecture

46 pages

Caches

Caches

9 pages

lecture

lecture

39 pages

Lecture

Lecture

36 pages

Lecture

Lecture

45 pages

Lecture

Lecture

56 pages

lecture

lecture

11 pages

lecture

lecture

9 pages

Lecture

Lecture

36 pages

Lecture

Lecture

37 pages

Exam

Exam

16 pages

Lecture

Lecture

10 pages

Lecture

Lecture

43 pages

Lecture

Lecture

8 pages

Lecture

Lecture

8 pages

Lecture

Lecture

36 pages

Lecture

Lecture

43 pages

Lecture

Lecture

12 pages

Lecture

Lecture

37 pages

Lecture

Lecture

6 pages

Lecture

Lecture

40 pages

coding

coding

2 pages

Exam

Exam

17 pages

Exam

Exam

14 pages

Lecture

Lecture

29 pages

Lecture

Lecture

34 pages

Exam

Exam

11 pages

Lecture

Lecture

9 pages

Lecture

Lecture

37 pages

Lecture

Lecture

36 pages

lecture

lecture

46 pages

Lecture

Lecture

33 pages

Lecture

Lecture

57 pages

Lecture

Lecture

32 pages

Lecture

Lecture

46 pages

Lecture

Lecture

40 pages

Lecture

Lecture

11 pages

Lecture

Lecture

6 pages

Lecture

Lecture

43 pages

Lecture

Lecture

12 pages

Lecture

Lecture

18 pages

Exam

Exam

10 pages

Lecture

Lecture

45 pages

Lecture

Lecture

37 pages

Exam

Exam

24 pages

class09

class09

21 pages

class22

class22

37 pages

class20

class20

30 pages

class27

class27

33 pages

class25

class25

21 pages

class04

class04

31 pages

Lecture

Lecture

59 pages

class01a

class01a

14 pages

class12

class12

45 pages

class29

class29

33 pages

Lecture

Lecture

39 pages

Lecture

Lecture

6 pages

class03

class03

34 pages

lecture

lecture

42 pages

Lecture

Lecture

40 pages

Lecture

Lecture

47 pages

Exam

Exam

19 pages

R06-B

R06-B

25 pages

class17

class17

37 pages

class25

class25

31 pages

Lecture

Lecture

15 pages

final-f06

final-f06

17 pages

Lecture

Lecture

9 pages

lecture

lecture

9 pages

Exam

Exam

15 pages

Lecture

Lecture

22 pages

class11

class11

45 pages

lecture

lecture

50 pages

Linking

Linking

37 pages

Lecture

Lecture

64 pages

Integers

Integers

40 pages

Exam

Exam

11 pages

Lecture

Lecture

37 pages

Lecture

Lecture

44 pages

Lecture

Lecture

37 pages

Lecture

Lecture

9 pages

Lecture

Lecture

37 pages

Lecture

Lecture

45 pages

Final

Final

25 pages

lecture

lecture

9 pages

Lecture

Lecture

30 pages

Lecture

Lecture

16 pages

Final

Final

17 pages

Lecture

Lecture

8 pages

Exam

Exam

11 pages

Lecture

Lecture

47 pages

Lecture

Lecture

9 pages

lecture

lecture

39 pages

Exam

Exam

11 pages

lecture

lecture

41 pages

lecture

lecture

37 pages

Lecture

Lecture

59 pages

Lecture

Lecture

45 pages

Exam 1

Exam 1

18 pages

Lecture

Lecture

41 pages

Lecture

Lecture

32 pages

Lecture

Lecture

30 pages

Lecture

Lecture

9 pages

Lecture

Lecture

9 pages

Lecture

Lecture

15 pages

Lecture

Lecture

11 pages

Lecture

Lecture

9 pages

Lecture

Lecture

34 pages

Lecture

Lecture

40 pages

Lecture

Lecture

4 pages

Lecture

Lecture

46 pages

Lecture

Lecture

8 pages

Lecture

Lecture

65 pages

Lecture

Lecture

38 pages

Lecture

Lecture

35 pages

Lecture

Lecture

8 pages

Lecture

Lecture

34 pages

Lecture

Lecture

8 pages

Exam

Exam

13 pages

Lecture

Lecture

43 pages

Lecture

Lecture

9 pages

Lecture

Lecture

12 pages

Lecture

Lecture

9 pages

Lecture

Lecture

34 pages

Lecture

Lecture

43 pages

Lecture

Lecture

7 pages

Lecture

Lecture

45 pages

Lecture

Lecture

24 pages

Lecture

Lecture

47 pages

Lecture

Lecture

12 pages

Lecture

Lecture

20 pages

Lecture

Lecture

9 pages

Exam

Exam

11 pages

Lecture

Lecture

52 pages

Lecture

Lecture

20 pages

Exam

Exam

11 pages

Lecture

Lecture

35 pages

Lecture

Lecture

47 pages

Lecture

Lecture

18 pages

Lecture

Lecture

30 pages

Lecture

Lecture

59 pages

Lecture

Lecture

37 pages

Lecture

Lecture

22 pages

Lecture

Lecture

35 pages

Exam

Exam

23 pages

Lecture

Lecture

9 pages

Lecture

Lecture

22 pages

class12

class12

32 pages

Lecture

Lecture

8 pages

Lecture

Lecture

39 pages

Lecture

Lecture

44 pages

Lecture

Lecture

38 pages

Lecture

Lecture

69 pages

Lecture

Lecture

41 pages

Lecture

Lecture

12 pages

Lecture

Lecture

52 pages

Lecture

Lecture

59 pages

Lecture

Lecture

39 pages

Lecture

Lecture

83 pages

Lecture

Lecture

59 pages

class01b

class01b

17 pages

Exam

Exam

21 pages

class07

class07

47 pages

Lecture

Lecture

11 pages

Odyssey

Odyssey

18 pages

multicore

multicore

66 pages

Lecture

Lecture

6 pages

lecture

lecture

41 pages

lecture

lecture

55 pages

lecture

lecture

52 pages

lecture

lecture

33 pages

lecture

lecture

46 pages

lecture

lecture

55 pages

lecture

lecture

17 pages

lecture

lecture

49 pages

Exam

Exam

17 pages

lecture

lecture

56 pages

Exam 2

Exam 2

16 pages

Exam 2

Exam 2

16 pages

Notes

Notes

37 pages

Lecture

Lecture

40 pages

Lecture

Lecture

36 pages

Lecture

Lecture

43 pages

Lecture

Lecture

25 pages

Exam

Exam

13 pages

Lecture

Lecture

32 pages

Lecture

Lecture

12 pages

Lecture

Lecture

58 pages

Lecture

Lecture

29 pages

Lecture

Lecture

59 pages

Lecture

Lecture

41 pages

Lecture

Lecture

50 pages

Exam

Exam

17 pages

Lecture

Lecture

29 pages

Lecture

Lecture

44 pages

Lecture

Lecture

41 pages

Lecture

Lecture

52 pages

Lecture

Lecture

40 pages

Lecture

Lecture

33 pages

lecture

lecture

10 pages

Lecture

Lecture

27 pages

Lecture

Lecture

29 pages

Lecture

Lecture

39 pages

Lecture

Lecture

9 pages

Lecture

Lecture

29 pages

Lecture

Lecture

8 pages

Lecture

Lecture

43 pages

Lecture

Lecture

43 pages

Lecture

Lecture

75 pages

Lecture

Lecture

55 pages

Exam

Exam

12 pages

Lecture

Lecture

43 pages

Lecture

Lecture

35 pages

lecture

lecture

36 pages

Exam

Exam

33 pages

lecture

lecture

56 pages

lecture

lecture

64 pages

lecture

lecture

8 pages

Exam

Exam

14 pages

Lecture

Lecture

43 pages

Lecture

Lecture

36 pages

lecture

lecture

56 pages

lecture

lecture

75 pages

lecture

lecture

36 pages

Lecture

Lecture

50 pages

Lecture

Lecture

45 pages

Lecture

Lecture

13 pages

Exam

Exam

23 pages

Lecture

Lecture

10 pages

Lecture

Lecture

48 pages

Lecture

Lecture

83 pages

lecture

lecture

57 pages

Lecture

Lecture

33 pages

Lecture

Lecture

39 pages

Lecture

Lecture

33 pages

lecture

lecture

54 pages

Lecture

Lecture

30 pages

Exam

Exam

13 pages

Lecture

Lecture

36 pages

Lecture

Lecture

40 pages

Exam

Exam

17 pages

Lecture

Lecture

9 pages

Exam

Exam

15 pages

Lecture

Lecture

44 pages

Lecture

Lecture

34 pages

Lecture

Lecture

24 pages

Lecture

Lecture

29 pages

class12

class12

43 pages

lecture

lecture

43 pages

class22

class22

22 pages

R06-B

R06-B

25 pages

class01b

class01b

19 pages

lecture

lecture

29 pages

lab1

lab1

8 pages

Caches

Caches

36 pages

lecture

lecture

55 pages

Lecture,

Lecture,

37 pages

Integers

Integers

40 pages

Linking

Linking

38 pages

lecture

lecture

45 pages

Lecture

Lecture

61 pages

Linking

Linking

33 pages

lecture

lecture

40 pages

lecture

lecture

40 pages

Lecture

Lecture

32 pages

lecture

lecture

48 pages

lecture

lecture

44 pages

Exam

Exam

11 pages

Lecture

Lecture

31 pages

Lecture

Lecture

46 pages

Lecture

Lecture

40 pages

Lecture

Lecture

40 pages

Exam

Exam

12 pages

Lecture

Lecture

42 pages

Lecture

Lecture

36 pages

Lecture

Lecture

45 pages

Lecture

Lecture

41 pages

Lecture

Lecture

13 pages

Lecture

Lecture

35 pages

Lecture

Lecture

20 pages

Final

Final

19 pages

Lecture

Lecture

33 pages

Lecture

Lecture

50 pages

Lecture

Lecture

33 pages

Lecture

Lecture

27 pages

Lecture

Lecture

6 pages

Exam

Exam

15 pages

Lecture

Lecture

24 pages

Lecture

Lecture

23 pages

Lecture

Lecture

43 pages

Lecture

Lecture

32 pages

Lecture

Lecture

52 pages

Lecture

Lecture

37 pages

Lecture

Lecture

36 pages

Lecture

Lecture

34 pages

Lecture

Lecture

40 pages

Lecture

Lecture

15 pages

lecture

lecture

21 pages

Lecture

Lecture

58 pages

Lecture

Lecture

49 pages

Lecture

Lecture

36 pages

Lecture

Lecture

11 pages

Lecture

Lecture

12 pages

Lecture

Lecture

58 pages

Lecture

Lecture

33 pages

Exam

Exam

15 pages

Lecture

Lecture

35 pages

Lecture

Lecture

10 pages

Lecture

Lecture

25 pages

Lecture

Lecture

31 pages

Lecture

Lecture

24 pages

Lecture

Lecture

34 pages

Lecture

Lecture

50 pages

lecture

lecture

35 pages

Lecture

Lecture

11 pages

Lecture

Lecture

39 pages

Lecture

Lecture

45 pages

Lecture

Lecture

41 pages

exam1-f05

exam1-f05

11 pages

Lecture

Lecture

4 pages

Lecture

Lecture

17 pages

Exam

Exam

17 pages

malloc()

malloc()

12 pages

Lecture

Lecture

57 pages

Lecture

Lecture

30 pages

Lecture

Lecture

30 pages

Lecture

Lecture

47 pages

Lecture

Lecture

33 pages

Exam

Exam

12 pages

Lecture

Lecture

43 pages

Lectures

Lectures

33 pages

Lecture

Lecture

36 pages

lecture

lecture

33 pages

Exam

Exam

14 pages

Lecture

Lecture

43 pages

Lecture

Lecture

25 pages

Load more
Download 08-machine-advanced
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view 08-machine-advanced and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view 08-machine-advanced and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?