University of Washington Today Lab 3 out Buffer overflow! Finish-up data structures 1.

29
University of Washington Today Lab 3 out Buffer overflow! Finish-up data structures 1

description

University of Washington Multi-Level Array Example 3 Variable univ denotes array of 3 elements Each element is a pointer  4 bytes Each pointer points to array of int s zip_dig cmu = { 1, 5, 2, 1, 3 }; zip_dig uw = { 9, 8, 1, 9, 5 }; zip_dig ucb = { 9, 4, 7, 2, 0 }; #define UCOUNT 3 int *univ[UCOUNT] = {uw, cmu, ucb}; univ cmu uw ucb How do access an element?

Transcript of University of Washington Today Lab 3 out Buffer overflow! Finish-up data structures 1.

Page 1: University of Washington Today Lab 3 out  Buffer overflow! Finish-up data structures 1.

University of Washington

1

Today Lab 3 out

Buffer overflow!

Finish-up data structures

Page 2: University of Washington Today Lab 3 out  Buffer overflow! Finish-up data structures 1.

University of Washington

2

Multi-Level Array Examplezip_dig cmu = { 1, 5, 2, 1, 3 };zip_dig uw = { 9, 8, 1, 9, 5 };zip_dig ucb = { 9, 4, 7, 2, 0 };

int *univ[3] = {uw, cmu, ucb};

Same thing as a 2D array?

int univ2D[3] = {{ 9, 8, 1, 9, 5 },{ 1, 5, 2, 1, 3 },{ 9, 4, 7, 2, 0 }};

Page 3: University of Washington Today Lab 3 out  Buffer overflow! Finish-up data structures 1.

University of Washington

3

Multi-Level Array Example Variable univ denotes

array of 3 elements Each element is a pointer

4 bytes Each pointer points to array

of ints

zip_dig cmu = { 1, 5, 2, 1, 3 };zip_dig uw = { 9, 8, 1, 9, 5 };zip_dig ucb = { 9, 4, 7, 2, 0 };

#define UCOUNT 3int *univ[UCOUNT] = {uw, cmu, ucb};

361601656

164168

univ

cmu

uw

ucb

1 5 2 1 3

16 20 24 28 32 369 8 1 9 5

36 40 44 48 52 56

9 4 7 2 0

56 60 64 68 72 76

How do access an element?

Page 4: University of Washington Today Lab 3 out  Buffer overflow! Finish-up data structures 1.

University of Washington

4

Element Access in Multi-Level Array

Computation (IA32) Element access Mem[Mem[univ+4*index]+4*dig] Must do two memory reads

First get pointer to row array Then access element within array

# %ecx = index# %eax = digleal 0(,%ecx,4),%edx # 4*indexmovl univ(%edx),%edx # Mem[univ+4*index]movl (%edx,%eax,4),%eax # Mem[...+4*dig]

int get_univ_digit (int index, int dig){ return univ[index][dig];}

Page 5: University of Washington Today Lab 3 out  Buffer overflow! Finish-up data structures 1.

University of Washington

5

Array Element Accesses

int get_sea_digit (int index, int dig){ return sea[index][dig];}

int get_univ_digit (int index, int dig){ return univ[index][dig];}

Nested array Multi-level array

Access looks similar, but it isn’t:

Mem[sea+20*index+4*dig] Mem[Mem[univ+4*index]+4*dig]

Page 6: University of Washington Today Lab 3 out  Buffer overflow! Finish-up data structures 1.

University of Washington

6

Strange Referencing Examples

Reference Address Value Guaranteed?univ[2][3]univ[1][5]univ[2][-1]univ[3][-1]univ[1][12]

361601656

164168

univ

cmu

uw

ucb

1 5 2 1 3

16 20 24 28 32 369 8 1 9 5

36 40 44 48 52 56

9 4 7 2 0

56 60 64 68 72 76

Page 7: University of Washington Today Lab 3 out  Buffer overflow! Finish-up data structures 1.

University of Washington

7

Strange Referencing Examples

Reference Address Value Guaranteed?univ[2][3]univ[1][5]univ[2][-1]univ[3][-1]univ[1][12] Code does not do any bounds checking Location of each lower-level array in memory is not guaranteed

361601656

164168

univ

cmu

uw

ucb

1 5 2 1 3

16 20 24 28 32 369 8 1 9 5

36 40 44 48 52 56

9 4 7 2 0

56 60 64 68 72 76

56+4*3 = 68 2 Yes16+4*5 = 36 9 No56+4*-1 = 52 5 No?? ?? No16+4*12 = 64 7 No

Page 8: University of Washington Today Lab 3 out  Buffer overflow! Finish-up data structures 1.

University of Washington

8

Using Nested Arrays#define N 16typedef int fix_matrix[N][N];

/* Compute element i,k of fixed matrix product */int fix_prod_ele(fix_matrix a, fix_matrix b, int i, int k){ int j; int result = 0; for (j = 0; j < N; j++) result += a[i][j]*b[j][k]; return result;}

Page 9: University of Washington Today Lab 3 out  Buffer overflow! Finish-up data structures 1.

University of Washington

9

Using Nested Arrays Strengths

Generates very efficientassembly code

Avoids multiply in index computation

Limitation Only works for fixed array size

#define N 16typedef int fix_matrix[N][N];

/* Compute element i,k of fixed matrix product */int fix_prod_ele(fix_matrix a, fix_matrix b, int i, int k){ int j; int result = 0; for (j = 0; j < N; j++) result += a[i][j]*b[j][k]; return result;}

a b

i-th row

k-th columnx

Page 10: University of Washington Today Lab 3 out  Buffer overflow! Finish-up data structures 1.

University of Washington

10

Dynamic Nested Arrays Strength

Can create matrix of any size Programming

Must do index computation explicitly

Performance Accessing single element costly Must do multiplication

int * new_var_matrix(int n){ return (int *) calloc(sizeof(int), n*n);}

int var_ele (int *a, int i, int j, int n){ return a[i*n+j];}

movl 12(%ebp),%eax # imovl 8(%ebp),%edx # aimull 20(%ebp),%eax # n*iaddl 16(%ebp),%eax # n*i+jmovl (%edx,%eax,4),%eax # Mem[a+4*(i*n+j)]

Page 11: University of Washington Today Lab 3 out  Buffer overflow! Finish-up data structures 1.

University of Washington

11

Arrays in C Contiguous allocations of memory No bounds checking Can usually be treated like a pointer to first element

Page 12: University of Washington Today Lab 3 out  Buffer overflow! Finish-up data structures 1.

University of Washington

12

Data Structures in Assembly Arrays

One-dimensional Multi-dimensional (nested) Multi-level

Structs Alignment

Unions

Page 13: University of Washington Today Lab 3 out  Buffer overflow! Finish-up data structures 1.

University of Washington

13

struct rec { int i; int a[3]; int *p;};

Structures

Page 14: University of Washington Today Lab 3 out  Buffer overflow! Finish-up data structures 1.

University of Washington

14

struct rec { int i; int a[3]; int *p;};

Structures

Characteristics Contiguously-allocated region of memory Refer to members within structure by names Members may be of different types

Memory Layouti a p

0 4 16 20

Page 15: University of Washington Today Lab 3 out  Buffer overflow! Finish-up data structures 1.

University of Washington

15

struct rec { int i; int a[3]; int *p;};

Structures Accessing Structure Member

Given an instance of the struct, we can usethe . operator, just like Java:

struct rec r1; r1.i = val; What if we have a pointer to a struct: struct rec *r = &r1;

Page 16: University of Washington Today Lab 3 out  Buffer overflow! Finish-up data structures 1.

University of Washington

16

struct rec { int i; int a[3]; int *p;};

IA32 Assembly# %eax = val# %edx = rmovl %eax,(%edx) # Mem[r] = val

voidset_i(struct rec *r, int val){ r->i = val; }

Structures Accessing Structure Member

Given an instance of the struct, we can usethe . operator, just like Java:

struct rec r1; r1.i = val; What if we have a pointer to a struct: struct rec *r = &r1;

Using * and . operators: Or, use -> operator for short:

Pointer indicates first byte of structure; access members with offsets

(*r).i = val; r->i = val;

Page 17: University of Washington Today Lab 3 out  Buffer overflow! Finish-up data structures 1.

University of Washington

17

# %ecx = idx# %edx = rleal 0(,%ecx,4),%eax # 4*idxleal 4(%eax,%edx),%eax # r+4*idx+4

int *find_a (struct rec *r, int idx){ return &r->a[idx];}

Generating Pointer to Structure Member

Generating Pointer to Array Element Offset of each structure

member determined at compile time

struct rec { int i; int a[3]; int *p;};

i a p0 4 16 20

r+4+4*idxr

Page 18: University of Washington Today Lab 3 out  Buffer overflow! Finish-up data structures 1.

University of Washington

18

Structures & Alignment Unaligned Data

How would it look like if data items were aligned (address multiple of type size) ?

c i[0] i[1] vp p+1 p+5 p+9 p+17

struct S1 { char c; int i[2]; double v;} *p;

Page 19: University of Washington Today Lab 3 out  Buffer overflow! Finish-up data structures 1.

University of Washington

19

Structures & Alignment Unaligned Data

Aligned Data Primitive data type requires K bytes Address must be multiple of K

c i[0] i[1] v3 bytes 4 bytesp+0 p+4 p+8 p+16 p+24

Multiple of 4 Multiple of 8

Multiple of 8

Multiple of 8

c i[0] i[1] vp p+1 p+5 p+9 p+17

struct S1 { char c; int i[2]; double v;} *p;

Page 20: University of Washington Today Lab 3 out  Buffer overflow! Finish-up data structures 1.

University of Washington

20

Alignment Principles Aligned Data

Primitive data type requires K bytes Address must be multiple of K

Aligned data is required on some machines; it is advisedon IA32 Treated differently by IA32 Linux, x86-64 Linux, and Windows!

What is the motivation for alignment?

Page 21: University of Washington Today Lab 3 out  Buffer overflow! Finish-up data structures 1.

University of Washington

21

Alignment Principles Aligned Data

Primitive data type requires K bytes Address must be multiple of K

Aligned data is required on some machines; it is advisedon IA32 Treated differently by IA32 Linux, x86-64 Linux, and Windows!

Motivation for Aligning Data Physical memory is accessed by aligned chunks of 4 or 8 bytes (system-

dependent) Inefficient to load or store datum that spans quad word boundaries

Also, virtual memory is very tricky when datum spans two pages (later…) Compiler

Inserts padding in structure to ensure correct alignment of fields sizeof() should be used to get true size of structs

Page 22: University of Washington Today Lab 3 out  Buffer overflow! Finish-up data structures 1.

University of Washington

22

Specific Cases of Alignment (IA32) 1 byte: char, …

no restrictions 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 OSs & instruction sets): lowest 3 bits 0002

Linux: lowest 2 bits of address must be 002

i.e., treated the same as a 4-byte primitive data type 12 bytes: long double

Windows, Linux: lowest 2 bits of address must be 002

Page 23: University of Washington Today Lab 3 out  Buffer overflow! Finish-up data structures 1.

University of Washington

23

Saving Space

c i[0] i[1] v3 bytes 4 bytes

p1+0 p1+4 p1+8 p1+16 p1+24

struct S1 { char c; int i[2]; double v;} *p1;

Page 24: University of Washington Today Lab 3 out  Buffer overflow! Finish-up data structures 1.

University of Washington

24

Saving Space Put large data types first:

Effect (example x86-64, both have K=8)

c i[0] i[1] v3 bytes 4 bytes

p1+0 p1+4 p1+8 p1+16 p1+24

struct S1 { char c; int i[2]; double v;} *p1;

struct S2 { double v; int i[2]; char c;} *p2;

ci[0] i[1]vp2+0 p2+8 p2+16

Unfortunately, doesn’t satisfy requirement that struct’s total size is a multiple of K

Page 25: University of Washington Today Lab 3 out  Buffer overflow! Finish-up data structures 1.

University of Washington

25

Arrays of Structures Satisfy alignment requirement

for every element How would accessing an element work?

struct S2 { double v; int i[2]; char c;} a[10];

ci[0] i[1]va+24 a+32 a+40

a[0]a+0

a[1] a[2]a+24 a+48 a+72

• • •

7 bytes

a+48

Page 26: University of Washington Today Lab 3 out  Buffer overflow! Finish-up data structures 1.

University of Washington

26

Unions Allocated according to largest element Can only use one member at a time

union U1 { char c; int i[2]; double v;} *up;

struct S1 { char c; int i[2]; double v;} *sp;

c 3 bytes i[0] i[1] 4 bytes vsp+0 sp+4 sp+8 sp+16 sp+24

ci[0] i[1]

vup+0 up+4 up+8

Page 27: University of Washington Today Lab 3 out  Buffer overflow! Finish-up data structures 1.

University of Washington

27

What Are Unions Good For? Unions allow the same region of memory to be referenced as

different types Different “views” of the same memory location Can be used to circumvent C’s type system (bad idea)

Better idea: use a struct inside a union to access some memory location either as a whole or by its parts

Page 28: University of Washington Today Lab 3 out  Buffer overflow! Finish-up data structures 1.

University of Washington

28

Unions For Embedded Programmingtypedef union{ unsigned char byte; struct { unsigned char b0:1; unsigned char b1:1; unsigned char b2:1; unsigned char b3:1; unsigned char reserved:4; } bits;} hw_register;

hw_register reg;reg.byte = 0x3F; // 001111112

reg.bits.b2 = 0; // 001110112

reg.bits.b3 = 0; // 001100112

unsigned short a = reg.byte;printf("0x%X\n", a); // output: 0x33

(Note: the placement of these fields and other parts of this example are implementation-dependent)

Page 29: University of Washington Today Lab 3 out  Buffer overflow! Finish-up data structures 1.

University of Washington

29

Summary Arrays in C

Contiguous allocations of memory No bounds checking Can usually be treated like a pointer to first element

Structures Allocate bytes in order declared Pad in middle and at end to satisfy alignment

Unions Provide different views of the same memory location