“Way easier than when we were students”

Slides:



Advertisements
Similar presentations
Machine-Level Programming III: Procedures Feb. 8, 2000 Topics IA32 stack Stack-based languages Stack frames Register saving conventions Creating pointers.
Advertisements

The University of Adelaide, School of Computer Science
Machine Programming – Procedures and IA32 Stack CENG334: Introduction to Operating Systems Instructor: Erol Sahin Acknowledgement: Most of the slides are.
University of Washington Last Time For loops  for loop → while loop → do-while loop → goto version  for loop → while loop → goto “jump to middle” version.
Machine-Level Programming III: Procedures Apr. 17, 2006 Topics IA32 stack discipline Register saving conventions Creating pointers to local variables CS213.
1 Function Calls Professor Jennifer Rexford COS 217 Reading: Chapter 4 of “Programming From the Ground Up” (available online from the course Web site)
Machine-Level Programming III: Procedures Sept. 17, 2007 IA32 stack discipline Register saving conventions Creating pointers to local variablesx86-64 Argument.
– 1 – , F’02 ICS05 Instructor: Peter A. Dinda TA: Bin Lin Recitation 4.
1 Homework Reading –PAL, pp , Machine Projects –Finish mp2warmup Questions? –Start mp2 as soon as possible Labs –Continue labs with your.
Machine-Level Programming III: Procedures Jan 30, 2003
Assembly תרגול 8 פונקציות והתקפת buffer.. Procedures (Functions) A procedure call involves passing both data and control from one part of the code to.
Machine-Level Programming III: Procedures Sept. 15, 2006 IA32 stack discipline Register saving conventions Creating pointers to local variablesx86-64 Argument.
September 22, 2014 Pengju (Jimmy) Jin Section E
Stack Activation Records Topics IA32 stack discipline Register saving conventions Creating pointers to local variables February 6, 2003 CSCE 212H Computer.
Stacks and Frames Demystified CSCI 3753 Operating Systems Spring 2005 Prof. Rick Han.
Introduction to x86 Assembly or “How does someone hack my laptop?”
Y86 Processor State Program Registers
Carnegie Mellon Introduction to Computer Systems /18-243, spring 2009 Recitation, Jan. 14 th.
1 Carnegie Mellon Stacks : Introduction to Computer Systems Recitation 5: September 24, 2012 Joon-Sup Han Section F.
Lee CSCE 312 TAMU 1 Based on slides provided by Randy Bryant and Dave O’Hallaron Machine-Level Programming III: Switch Statements and IA32 Procedures Instructor:
Ithaca College Machine-Level Programming IV: IA32 Procedures Comp 21000: Introduction to Computer Systems & Assembly Lang Spring 2013 * Modified slides.
64-Bit Architectures Topics 64-bit data New registers and instructions Calling conventions CS 105 “Tour of the Black Holes of Computing!”
Machine-Level Programming III: Switch Statements and IA32 Procedures Seoul National University.
University of Washington Today More on procedures, stack etc. Lab 2 due today!  We hope it was fun! What is a stack?  And how about a stack frame? 1.
Fabián E. Bustamante, Spring 2007 Machine-Level Programming III - Procedures Today IA32 stack discipline Register saving conventions Creating pointers.
Recitation 2 – 2/11/02 Outline Stacks & Procedures Homogenous Data –Arrays –Nested Arrays Mengzhi Wang Office Hours: Thursday.
Machine-Level Programming: X86-64 Topics Registers Stack Function Calls Local Storage X86-64.ppt CS 105 Tour of Black Holes of Computing.
Machine-Level Programming III: Procedures Topics IA32 stack discipline Register-saving conventions Creating pointers to local variables CS 105 “Tour of.
Carnegie Mellon 1 Odds and Ends Intro to x86-64 Memory Layout.
Machine-level Programming III: Procedures Topics –IA32 stack discipline –Register saving conventions –Creating pointers to local variables.
1 Procedure Call and Array. 2 Outline Data manipulation Control structure Suggested reading –Chap 3.7, 3.8.
Carnegie Mellon 1 Machine-Level Programming I: Basics Lecture, Feb. 21, 2013 These slides are from website which accompanies the.
F’08 Stack Discipline Aug. 27, 2008 Nathaniel Wesley Filardo Slides stolen from CMU , whence they were stolen from CMU CS 438.
Compiler Construction Code Generation Activation Records
University of Amsterdam Computer Systems – the instruction set architecture Arnoud Visser 1 Computer Systems The instruction set architecture.
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Carnegie Mellon Instructor: San Skulrattanakulchai Machine-Level Programming.
1 Assembly Language: Function Calls Jennifer Rexford.
IA32 Stack –Region of memory managed with stack discipline –Grows toward lower addresses –Register %esp indicates lowest stack address address of top element.
Section 5: Procedures & Stacks
Buffer Overflow Buffer overflows are possible because C doesn’t check array boundaries Buffer overflows are dangerous because buffers for user input are.
Recitation 3: Procedures and the Stack
A job ad at a game programming company
Reading Condition Codes (Cont.)
C function call conventions and the stack
CSCE 212 Computer Architecture
Homework Reading Machine Projects Labs PAL, pp ,
Introduction to Compilers Tim Teitelbaum
Machine-Level Programming III: Procedures
Carnegie Mellon Machine-Level Programming III: Switch Statements and IA32 Procedures / : Introduction to Computer Systems 7th Lecture, Sep.
Machine-Level Programming 1 Introduction
asum.ys A Y86 Programming Example
Machine-Level Programming III: Switch Statements and IA32 Procedures
Y86 Processor State Program Registers
Instructors: Majd Sakr and Khaled Harras
Machine-Level Programming 4 Procedures
Machine-Level Programming III: Procedures /18-213/14-513/15-513: Introduction to Computer Systems 7th Lecture, September 18, 2018.
Carnegie Mellon Machine-Level Programming III: Procedures : Introduction to Computer Systems October 22, 2015 Instructor: Rabi Mahapatra Authors:
Condition Codes Single Bit Registers
Roadmap C: Java: Assembly language: OS: Machine code: Computer system:
Assembly Language Programming II: C Compiler Calling Sequences
Machine-Level Programming III: Procedures Sept 18, 2001
Understanding Program Address Space
Machine Level Representation of Programs (IV)
Machine-Level Programming: Introduction
Roadmap C: Java: Assembly language: OS: Machine code: Computer system:
Ithaca College Machine-Level Programming VII: Procedures Comp 21000: Introduction to Computer Systems & Assembly Lang Spring 2017.
Machine-Level Representation of Programs (x86-64)
Ithaca College Machine-Level Programming VII: Procedures Comp 21000: Introduction to Computer Systems & Assembly Lang Spring 2017.
Presentation transcript:

15-410 “Way easier than when we were students” Stack Discipline Jan. 14, 2004 Dave Eckhardt Bruce Maggs Review slides from 15-213 originally developed by Randy Bryant and Dave O’Halloran. L02_Stack

Outline Topics Process memory model IA32 stack organization Register saving conventions Before & after main() Project 0

Private Address Spaces Each process has its own private address space. 0xffffffff kernel virtual memory (code, data, heap, stack) memory invisible to user code 0xc0000000 user stack (created at runtime) %esp (stack pointer) memory mapped region for shared libraries 0x40000000 brk run-time heap (managed by malloc) read/write segment (.data, .bss) loaded from the executable file read-only segment (.init, .text, .rodata) 0x08048000 unused

Linux Memory Layout Stack Heap FF BF 7F 3F C0 80 40 00 Stack Shared Libraries Text Data Heap 08 Linux Memory Layout Stack Runtime stack (8MB limit) Heap Dynamically allocated storage When call malloc, calloc, new Shared/Dynamic Libraries aka Shared Objects Library routines (e.g., printf, malloc) Linked into object code when first executed Windows has “DLLs” (semantic differences) Data, BSS Statically allocated data E.g., arrays & strings declared in code Text, RODATA Text - Executable machine instructions RODATA – Read-only (e.g., “const”) Upper 2 hex digits of address Red Hat v. 6.2 ~1920MB memory limit

Linux Memory Allocation Some Heap BF 7F 3F 80 40 00 Stack Libraries Text Data Heap 08 More Heap BF 7F 3F 80 40 00 Stack Libraries Text Data 08 Initially BF 7F 3F 80 40 00 Stack Text Data 08 Linked BF 7F 3F 80 40 00 Stack Libraries Text Data 08

IA32 Stack Region of memory managed with stack discipline Stack “Bottom” Region of memory managed with stack discipline Grows toward lower addresses Register %esp indicates lowest stack address address of top element Increasing Addresses Stack Grows Down Stack Pointer %esp Stack “Top”

IA32 Stack Pushing Pushing pushl Src Fetch operand at Src Decrement %esp by 4 Write operand at address given by %esp Stack “Bottom” Increasing Addresses Stack Grows Down Stack Pointer %esp -4 Stack “Top”

IA32 Stack Popping Popping popl Dest Read operand at address given by %esp Increment %esp by 4 Write to Dest Stack “Bottom” Increasing Addresses Stack Pointer %esp Stack Grows Down +4 Stack “Top”

Procedure Control Flow Use stack to support procedure call and return Procedure call: call label Push return address on stack; Jump to label Return address value Address of instruction beyond call Example from disassembly 804854e: e8 3d 06 00 00 call 8048b90 <main> 8048553: 50 pushl %eax Return address = 0x8048553 Procedure return: ret Pop address from stack; Jump to address

Procedure Call Example 804854e: e8 3d 06 00 00 call 8048b90 <main> 8048553: 50 pushl %eax call 8048b90 0x110 0x110 0x10c 0x10c 0x108 123 0x108 123 0x104 0x8048553 %esp 0x108 %esp 0x104 0x108 %eip 0x804854e %eip 0x8048b90 0x804854e %eip is program counter

Procedure Return Example 8048591: c3 ret ret 0x110 0x110 0x10c 0x10c 0x108 123 0x108 123 0x104 0x8048553 0x8048553 %esp 0x104 %esp 0x108 0x104 %eip 0x8048591 %eip 0x8048591 0x8048553 %eip is program counter

Stack-Based Languages Languages that Support Recursion e.g., C, Pascal, Java Code must be “Reentrant” Multiple simultaneous instantiations of single procedure Need some place to store state of each instantiation Arguments Local variables Return pointer Stack Discipline State for given procedure needed for limited time From when called to when return Callee returns before caller does Stack Allocated in Frames state for single procedure instantiation

Call Chain Example Code Structure Call Chain Procedure amI recursive yoo(…) { • who(); } yoo who(…) { • • • amI(); } who amI amI amI(…) { • amI(); } amI amI Procedure amI recursive

Stack Frames Contents Management Pointers Local variables Return information Temporary space Management Space allocated when enter procedure “Set-up” code Deallocated when return “Finish” code Pointers Stack pointer %esp indicates stack top Frame pointer %ebp indicates start of current frame yoo who amI Frame Pointer %ebp proc Stack Pointer %esp Stack “Top”

IA32/Linux Stack Frame Current Stack Frame (“Top” to Bottom) Parameters for function about to call “Argument build” Local variables If can’t keep in registers Saved register context Old frame pointer Caller Stack Frame Return address Pushed by call instruction Arguments for this call Caller Frame Arguments Frame Pointer (%ebp) Return Addr Old %ebp Saved Registers + Local Variables Argument Build Stack Pointer (%esp)

Calling swap from call_swap int zip1 = 15213; int zip2 = 91125; void call_swap() { swap(&zip1, &zip2); } call_swap: • • • pushl $zip2 # Global Var pushl $zip1 # Global Var call swap • Resulting Stack void swap(int *xp, int *yp) { int t0 = *xp; int t1 = *yp; *xp = t1; *yp = t0; } &zip2 &zip1 Rtn adr %esp

swap swap: pushl %ebp movl %esp,%ebp pushl %ebx Set Up movl 12(%ebp),%ecx movl 8(%ebp),%edx movl (%ecx),%eax movl (%edx),%ebx movl %eax,(%edx) movl %ebx,(%ecx) movl -4(%ebp),%ebx movl %ebp,%esp popl %ebp ret Set Up void swap(int *xp, int *yp) { int t0 = *xp; int t1 = *yp; *xp = t1; *yp = t0; } Body Finish

swap Setup #1 Resulting Entering Stack Stack • %ebp yp xp Rtn adr Old %ebp %ebp • %esp &zip2 &zip1 Rtn adr %esp swap: pushl %ebp movl %esp,%ebp pushl %ebx

swap Setup #2 Resulting Entering Stack Stack • %ebp • &zip2 yp &zip1 xp Rtn adr %esp Rtn adr Old %ebp %ebp %esp swap: pushl %ebp movl %esp,%ebp pushl %ebx

swap Setup #3 Resulting Entering Stack Stack • %ebp • &zip2 yp &zip1 xp Rtn adr %esp Rtn adr Old %ebp %ebp Old %ebx %esp swap: pushl %ebp movl %esp,%ebp pushl %ebx

Effect of swap Setup Entering Stack Resulting Stack • %ebp • Offset (relative to %ebp) &zip2 12 yp &zip1 8 xp Rtn adr %esp 4 Rtn adr Old %ebp %ebp Old %ebx %esp movl 12(%ebp),%ecx # get yp movl 8(%ebp),%edx # get xp . . . Body

swap Finish #1 swap’s Stack Observation Saved & restored register %ebx • • Offset Offset 12 yp 12 yp 8 xp 8 xp 4 Rtn adr 4 Rtn adr Old %ebp %ebp Old %ebp %ebp -4 Old %ebx %esp -4 Old %ebx %esp movl -4(%ebp),%ebx movl %ebp,%esp popl %ebp ret Observation Saved & restored register %ebx

swap Finish #2 swap’s swap’s Stack Stack • • Offset Offset 12 yp 12 yp 8 xp 8 xp 4 Rtn adr 4 Rtn adr Old %ebp %ebp Old %ebp %ebp -4 Old %ebx %esp %esp movl -4(%ebp),%ebx movl %ebp,%esp popl %ebp ret

swap Finish #3 swap’s swap’s Stack Stack • %ebp • Offset Offset 12 yp 8 xp 8 xp 4 Rtn adr 4 Rtn adr %esp Old %ebp %ebp %esp movl -4(%ebp),%ebx movl %ebp,%esp popl %ebp ret

swap Finish #4 swap’s Stack Exiting Stack Observation • %ebp • %ebp Exiting Stack Offset 12 yp &zip2 8 xp &zip1 %esp 4 Rtn adr %esp movl -4(%ebp),%ebx movl %ebp,%esp popl %ebp ret Observation Saved & restored register %ebx Didn’t do so for %eax, %ecx, or %edx

Register Saving Conventions When procedure yoo calls who:  yoo is the caller, who is the callee Can Register be Used for Temporary Storage? Contents of register %edx overwritten by who yoo: • • • movl $15213, %edx call who addl %edx, %eax ret who: • • • movl 8(%ebp), %edx addl $91125, %edx ret

Register Saving Conventions When procedure yoo calls who:  yoo is the caller, who is the callee Can Register be Used for Temporary Storage? Conventions “Caller Save” Caller saves temporary in its frame before calling “Callee Save” Callee saves temporary in its frame before using

IA32/Linux Register Usage Integer Registers Two have special uses %ebp, %esp Three managed as callee-save %ebx, %esi, %edi Old values saved on stack prior to using Three managed as caller-save %eax, %edx, %ecx Do what you please, but expect any callee to do so, as well Register %eax also stores returned value %eax Caller-Save Temporaries %edx %ecx %ebx Callee-Save Temporaries %esi %edi %esp Special %ebp

Stack Summary The Stack Makes Recursion Work Private storage for each instance of procedure call Instantiations don’t clobber each other Addressing of locals + arguments can be relative to stack positions Can be managed by stack discipline Procedures return in inverse order of calls IA32 Procedures Combination of Instructions + Conventions Call / Ret instructions Register usage conventions Caller / Callee save %ebp and %esp Stack frame organization conventions

Before & After main() int main(int argc, char *argv[]) { if (argc > 1) { printf(“%s\n”, argv[1]); } else { char *av[3] = { 0, 0, 0 }; av[0] = argv[0]; av[1] = “Fred”; execvp(av[0], av); } return (1);

The Mysterious Parts argc, argv Strings from one program Available while another program is running Which part of the memory map are they in? How did they get there? What happens when main() does “return(1)” ? There's no more program to run...right? Where does the 1 go? How does it get there? 410 students should seek to abolish mystery

The Mysterious Parts argc, argv Strings from one program Available while another program is running Inter-process sharing/information transfer is OS's job OS copies strings from old address space to new in exec() Traditionally placed “below bottom of stack”

The Mysterious Parts /* not actual code */ What happens when main() does “return(1)”??? Defined to have same effect as “exit(1)” The “main() wrapper” Receives argc, argv from OS Calls main(), then calls exit() Provided by C library, traditionally in “crt0.s” Often has a “strange” name /* not actual code */ void ~~main(int argc, char *argv[]) { exit(main(argc, argv)); }

Project 0 - “Stack Crawler” C/Assembly function Can be called by any C function Prints stack frames in a symbolic way ---Stack Trace Follows--- Function fun3(c='c', d=2.090000d), in Function fun2(f=35.000000f), in Function fun1(count=0), in Function fun1(count=1), in Function fun1(count=2), in Key question How do I know 0x80334720 is “fun1”? How do I know fun3()'s second parameter is “d”?

Project 0 “Data Flow”

Project 0 “Data Flow”

Project 0 “Data Flow”

Summary Review of stack knowledge What makes main() special Project 0 overview Start interviewing Project 2/3/4 partners!