+ All Categories
Home > Documents > 6/3/20151 Mapping PL Objects to the Machine – managing the address space David E. Culler CS61CL...

6/3/20151 Mapping PL Objects to the Machine – managing the address space David E. Culler CS61CL...

Date post: 19-Dec-2015
Category:
View: 216 times
Download: 2 times
Share this document with a friend
21
06/27/22 1 Mapping PL Objects to the Machine – managing the address space David E. Culler CS61CL Feb 9, 2009 Lecture 3 UCB CS61CL F09 Lec 3
Transcript
Page 1: 6/3/20151 Mapping PL Objects to the Machine – managing the address space David E. Culler CS61CL Feb 9, 2009 Lecture 3 UCB CS61CL F09 Lec 3.

04/18/23 1

Mapping PL Objects to the Machine – managing the address space

David E. Culler

CS61CL

Feb 9, 2009

Lecture 3

UCB CS61CL F09 Lec 3

Page 2: 6/3/20151 Mapping PL Objects to the Machine – managing the address space David E. Culler CS61CL Feb 9, 2009 Lecture 3 UCB CS61CL F09 Lec 3.

Big Ideas

• Review: – Computers manipulate finite representations of things.

– A bunch of bits can represent anything, it is all a matter of what you do with it.

– Finite representations have limitations.

• Today– Type constructors to compose complex type

– Mapping of program objects to machine storage

– An object, its value, its location, its reference

• Pointers are THE most subtle concept in C– Very powerful

– Easy to misuse

– Completely hidden in Java

04/18/23 UCB CS61CL F09 Lec 3 2

Page 3: 6/3/20151 Mapping PL Objects to the Machine – managing the address space David E. Culler CS61CL Feb 9, 2009 Lecture 3 UCB CS61CL F09 Lec 3.

C Types - the big picture

• Basic Types– “understood” by the machine

• Array– sequence of indexed objects of

homogeneous type

• Struct– collection of named objects of

heterogeneous types

• Pointer– reference to an object of

specified type

• Union– an object of one of a specific

collection of types

04/18/23 UCB CS61CL F09 Lec 3 3

char unsigned int float

double

Page 4: 6/3/20151 Mapping PL Objects to the Machine – managing the address space David E. Culler CS61CL Feb 9, 2009 Lecture 3 UCB CS61CL F09 Lec 3.

Composing Complex Types in C

• Complex types are really tools for composing new types

– Strings – sequences of characters

– Vectors – sequences of numbers

– Matrixes – 2D collections of numbers

– Records – finite sets of strings and numbers

– Lists, Tables

– Sounds, Images, Graphs

– …

– Think induction

• Pointers are fundamentally “understood” by the machine as well

– address

04/18/23 UCB CS61CL F09 Lec 3 4

Page 5: 6/3/20151 Mapping PL Objects to the Machine – managing the address space David E. Culler CS61CL Feb 9, 2009 Lecture 3 UCB CS61CL F09 Lec 3.

Where do Objects live and work?

04/18/23 UCB CS61CL F09 Lec 3 5

°°°

Processor

Memory

000..0:

FFF..F:

n:

register

load

operate

store

word

0F..FAC0:

00..1AA0:

Page 6: 6/3/20151 Mapping PL Objects to the Machine – managing the address space David E. Culler CS61CL Feb 9, 2009 Lecture 3 UCB CS61CL F09 Lec 3.

Where do complex objects reside?

• Arrays are stored in memory

• The variable (i.e., name) is associated with the location (i.e., address) of the collection

– Just like variables of basic type

• Elements are stored consecutively

– Can locate each of the elements

• Can operate on the indexed object just like an object of that type

– A[2] = x + Y[i] – 3;

04/18/23 UCB CS61CL F09 Lec 3 6

°°°

000..0:

FFF..F:

A:

Page 7: 6/3/20151 Mapping PL Objects to the Machine – managing the address space David E. Culler CS61CL Feb 9, 2009 Lecture 3 UCB CS61CL F09 Lec 3.

Where do complex objects reside?

• Struct are stored in memory

• The variable (i.e., name) is associated with the location (i.e., address) of the collection

– Just like variables of any type

• Elements are stored at fixed offsets

– Can locate each of the elements

• Can operate on the named member object just like an object of that type

– S.row = x + S.col – 3;

04/18/23 UCB CS61CL F09 Lec 3 7

°°°

000..0:

FFF..F:

S:

Page 8: 6/3/20151 Mapping PL Objects to the Machine – managing the address space David E. Culler CS61CL Feb 9, 2009 Lecture 3 UCB CS61CL F09 Lec 3.

All objects have a size

• The size of their representation

• The size of static objects is given by sizeof operator

04/18/23 UCB CS61CL F09 Lec 3 8

#include <stdio.h>int main() { char c = 'a'; int x = 34; int y[4]; printf("sizeof(c)=%d\n", sizeof(c) ); printf("sizeof(char)=%d\n",sizeof(char)); printf("sizeof(x)=%d\n", sizeof(x) ); printf("sizeof(int)=%d\n", sizeof(int) ); printf("sizeof(y)=%d\n", sizeof(y) ); printf("sizeof(7)=%d\n", sizeof(7) );}

Page 9: 6/3/20151 Mapping PL Objects to the Machine – managing the address space David E. Culler CS61CL Feb 9, 2009 Lecture 3 UCB CS61CL F09 Lec 3.

What can be done with a complex object?

• Access its elements– A[i], S.row

• Pass it around– Sort(A)

– x = max(A, n)

• Copy it– T = S

– z = munge(S, 3)

• Note the name of an array behaves as a reference to the object

• The name of a struct behaves as the object

04/18/23 UCB CS61CL F09 Lec 3 9

Page 10: 6/3/20151 Mapping PL Objects to the Machine – managing the address space David E. Culler CS61CL Feb 9, 2009 Lecture 3 UCB CS61CL F09 Lec 3.

Administration

• HW3 due at start of next week’s lab– Try to have it give practice in test tools

• Lab changers and waitlisters must give target TA your prioritized lab request list this week

• Readings are shifting for K&R to P&H

• Project 1 goes out on Tuesday, Due Friday 10/1

04/18/23 UCB CS61CL F09 Lec 3 10

Page 11: 6/3/20151 Mapping PL Objects to the Machine – managing the address space David E. Culler CS61CL Feb 9, 2009 Lecture 3 UCB CS61CL F09 Lec 3.

An object and its value…

X = X + 1;

04/18/23 UCB CS61CL F09 Lec 3 11

°°°

000..0:

FFF..F:

x: 3

°°°

000..0:

FFF..F:

x: 4

The value of variable X

The storage that holds the value X

Page 12: 6/3/20151 Mapping PL Objects to the Machine – managing the address space David E. Culler CS61CL Feb 9, 2009 Lecture 3 UCB CS61CL F09 Lec 3.

Every object in memory has an address

• That address is a pointer to the object.

• It is a fixed size object itself

• Just like basic type

04/18/23 UCB CS61CL F09 Lec 3 12

°°°

000..0:

FFF..F:

S:

Page 13: 6/3/20151 Mapping PL Objects to the Machine – managing the address space David E. Culler CS61CL Feb 9, 2009 Lecture 3 UCB CS61CL F09 Lec 3.

What can be done with a reference?

• Dereference it– Obtain the object that it refers (points) to

– X = *P; Y = S->row; z = A[0]; z = A[i];

• Pass it around, copy it, store it– Q = P;

– clearfields(S);

• Do type-based arithmetic on it– P-1

– Q++

• Do both– S->next = P;

– A[i] = 3;

• Cast it to an uint and mess with it (!!!)

04/18/23 UCB CS61CL F09 Lec 3 13

Page 14: 6/3/20151 Mapping PL Objects to the Machine – managing the address space David E. Culler CS61CL Feb 9, 2009 Lecture 3 UCB CS61CL F09 Lec 3.

Array variables are also a reference for the object

• Array name is essentially the address of (pointer to) the zeroth object in the array

• There are a few subtle differences– Can change what c refers to, but not what ac refers to

04/18/23 UCB CS61CL F09 Lec 3 14

int main() { char *c = "abc"; char ac[4] = "def"; printf("c[1]=%c\n",c[1] ); printf("ac[1]=%c\n",ac[1] );} °°°

000..0:

c: *

‘a’ ‘b’ ‘c’ \0

‘d’ ‘e’ ‘f’ \0ac:

Page 15: 6/3/20151 Mapping PL Objects to the Machine – managing the address space David E. Culler CS61CL Feb 9, 2009 Lecture 3 UCB CS61CL F09 Lec 3.

What kinds of variables (storage)?

• Visibility vs Lifetime

• Variables declared within a function– Arguments and Local Variables

– Visible in remainder of function

– Lifetime = Function Call

– Each call obtains a new set of variables

» Recursive calls too

– C “internals”

• Variables declared outside any function– Visible in remainder of file (!!!)

» include .h file

» extern vs static

– Lifetime = Whole Program

– C “externals”

• Malloc’d objects04/18/23 UCB CS61CL F09 Lec 3 15

int ave(int A, int B) { int C = (A + B)/2; return C;}

int count = 0;…int fib(int n) { count++; if (n <= 2) return 1; return fib(n-1)+fib(n-2);}

Page 16: 6/3/20151 Mapping PL Objects to the Machine – managing the address space David E. Culler CS61CL Feb 9, 2009 Lecture 3 UCB CS61CL F09 Lec 3.

Where does the program itself reside?

• In memory, just like the data

• Processor contains a special register – PC

– Program counter

– Address of the instruction to execute (i.e. ptr)

• Instruction Execution Cycle

– Instruction fetch

– Decode

– Operand fetch

– Execute

– Result Store

– Update PC

04/18/23 UCB CS61CL F09 Lec 3 16

°°°

000..0:

FFF..F:

n:0020FAC0:

PC

main:00401B20:

Instruction Fetch

Execute

Page 17: 6/3/20151 Mapping PL Objects to the Machine – managing the address space David E. Culler CS61CL Feb 9, 2009 Lecture 3 UCB CS61CL F09 Lec 3.

What’s a Process

• Address Space + a thread of control

04/18/23 UCB CS61CL F09 Lec 3 17

Page 18: 6/3/20151 Mapping PL Objects to the Machine – managing the address space David E. Culler CS61CL Feb 9, 2009 Lecture 3 UCB CS61CL F09 Lec 3.

Logical Structure of an Executing Program

04/18/23 UCB CS61CL F09 Lec 3 18

PC

regs

code

printf:

main:

nextword:

stack

heap

static data

Page 19: 6/3/20151 Mapping PL Objects to the Machine – managing the address space David E. Culler CS61CL Feb 9, 2009 Lecture 3 UCB CS61CL F09 Lec 3.

Address Space

04/18/23 UCB CS61CL F09 Lec 3 19

0000000:

<= Local variables

<= malloc

<= instructions

<= externs

<= OS, etc.

code

stack

heap

static data

reserved

unused

FFFFFFFF:

1000000:

0040000:

7FFFFFFF:

1008000:PC

regs

spgp

Page 20: 6/3/20151 Mapping PL Objects to the Machine – managing the address space David E. Culler CS61CL Feb 9, 2009 Lecture 3 UCB CS61CL F09 Lec 3.

Breaking the Abstraction…• Attack

– Cause the OS (or service or application) to do things it should not.

– Pass unterminated strings, bad length paramters, bad ptrs

– Corrupting system data may cause it to do other harm

• “Smashing the stack”– Send bad mesg causing system

code to overwrite parts of its stack

» Local vars and rtn address

» Bad return

– OS or app starts executing out of stack as if it were instructions

– Message contains jump instructions to send it off to attacker code

04/18/23 UCB CS61CL F09 Lec 3 20

code

stack

heap

static data

0000000:

1000000:

0040000:

reserved

<= Local variables

<= malloc

7FFFFFFF:

<= instructions

<= externs

<= OS, etc.unusedFFFFFFFF:

1008000:

RA

Page 21: 6/3/20151 Mapping PL Objects to the Machine – managing the address space David E. Culler CS61CL Feb 9, 2009 Lecture 3 UCB CS61CL F09 Lec 3.

Summary

• Arrays, Structs, and Pointers allow you define sophisticated data structures

– Compiler protects you by enforcing type system

– Avoid dropping beneath the abstraction and munging the bits

• All map into untyped storage, ints, and addresses

• Executing program has a specific structure– Code, Static Data, Stack, and Heap

– Mapped into address space

– “Holes” allow stack and heap to grow

– Compiler defines what the bits mean by enforcing type

» Chooses which operations to perform

• Poor coding practices, bugs, and architecture limitations lead to vulnerabilities

04/18/23 UCB CS61CL F09 Lec 3 21


Recommended