Stack Discipline Jan. 19, 2018

Similar documents
An Experience Like No Other. Stack Discipline Aug. 30, 2006

CS213. Machine-Level Programming III: Procedures

ASSEMBLY III: PROCEDURES. Jo, Heeseung

Assembly III: Procedures. Jo, Heeseung

Assembly III: Procedures. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

The course that gives CMU its Zip! Machine-Level Programming III: Procedures Sept. 17, 2002

Systems I. Machine-Level Programming V: Procedures

Machine-level Programming (3)

X86 Assembly -Procedure II:1

Machine-Level Programming III: Procedures

Region of memory managed with stack discipline Grows toward lower addresses. Register %esp contains lowest stack address = address of top element

IA32 Stack. Stack BoDom. Region of memory managed with stack discipline Grows toward lower addresses. Register %esp contains lowest stack address

Machine Programming 3: Procedures

Machine- Level Programming III: Switch Statements and IA32 Procedures

Sungkyunkwan University

IA32 Stack The course that gives CMU its Zip! Machine-Level Programming III: Procedures Sept. 17, IA32 Stack Popping. IA32 Stack Pushing

University of Washington

IA32 Stack. Lecture 5 Machine-Level Programming III: Procedures. IA32 Stack Popping. IA32 Stack Pushing. Topics. Pushing. Popping

Assembly I: Basic Operations. Jo, Heeseung

Giving credit where credit is due

ASSEMBLY I: BASIC OPERATIONS. Jo, Heeseung

Assembly Language: Function Calls

Assembly Language: Function Calls" Goals of this Lecture"

Assembly I: Basic Operations. Computer Systems Laboratory Sungkyunkwan University

Machine- Level Programming III: Switch Statements and IA32 Procedures

Assembly Language: Function Calls" Goals of this Lecture"

Assembly Language: Function Calls. Goals of this Lecture. Function Call Problems

Turning C into Object Code Code in files p1.c p2.c Compile with command: gcc -O p1.c p2.c -o p Use optimizations (-O) Put resulting binary in file p

Procedure Calls. Young W. Lim Mon. Young W. Lim Procedure Calls Mon 1 / 29

hnp://

Procedure Calls. Young W. Lim Sat. Young W. Lim Procedure Calls Sat 1 / 27

CS241 Computer Organization Spring 2015 IA

CS429: Computer Organization and Architecture

Function Calls COS 217. Reading: Chapter 4 of Programming From the Ground Up (available online from the course Web site)

CS 31: Intro to Systems Functions and the Stack. Martin Gagne Swarthmore College February 23, 2016

Mechanisms in Procedures. CS429: Computer Organization and Architecture. x86-64 Stack. x86-64 Stack Pushing

CSE 351: Week 4. Tom Bergan, TA

CISC 360. Machine-Level Programming I: Introduction Sept. 18, 2008

Machine-Level Programming I: Introduction Jan. 30, 2001

Machine-Level Programming III: Procedures

Machine Programming 1: Introduction

1 /* file cpuid2.s */ 4.asciz "The processor Vendor ID is %s \n" 5.section.bss. 6.lcomm buffer, section.text. 8.globl _start.

Page # CISC 360. Machine-Level Programming I: Introduction Sept. 18, IA32 Processors. X86 Evolution: Programmerʼs View.

X86 Stack Calling Function POV

Assembly Programmer s View Lecture 4A Machine-Level Programming I: Introduction

x86 assembly CS449 Fall 2017

Page 1. IA32 Stack CISC 360. Machine-Level Programming III: Procedures Sept. 22, IA32 Stack Popping Stack Bottom. IA32 Stack Pushing

Machine-level Programs Procedure

CSC 2400: Computing Systems. X86 Assembly: Function Calls"

MACHINE-LEVEL PROGRAMMING I: BASICS COMPUTER ARCHITECTURE AND ORGANIZATION

Bryant and O Hallaron, Computer Systems: A Programmer s Perspective, Third Edition. Carnegie Mellon

CS241 Computer Organization Spring Addresses & Pointers

W4118: PC Hardware and x86. Junfeng Yang

IA32 Stack Discipline Aug 30, 2017

CSC 2400: Computing Systems. X86 Assembly: Function Calls

Instruction Set Architecture

Processes (Intro) Yannis Smaragdakis, U. Athens

Instruction Set Architecture

The course that gives CMU its Zip! Machine-Level Programming I: Introduction Sept. 10, 2002

CS 33: Week 3 Discussion. x86 Assembly (v1.0) Section 1G

Machine-Level Programming Introduction

Data Representa/ons: IA32 + x86-64

211: Computer Architecture Summer 2016

THEORY OF COMPILATION

Assembly III: Procedures. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

CMSC 313 Lecture 12. Project 3 Questions. How C functions pass parameters. UMBC, CMSC313, Richard Chang

CIT Week13 Lecture

Systems Programming and Computer Architecture ( )

IA32 Processors The course that gives CMU its Zip! Machine-Level Programming I: Introduction Sept. 10, X86 Evolution: Programmer s View

! Starting in 1978 with ! Added more features as time goes on. ! Still support old features, although obsolete

AS08-C++ and Assembly Calling and Returning. CS220 Logic Design AS08-C++ and Assembly. AS08-C++ and Assembly Calling Conventions

MACHINE-LEVEL PROGRAMMING I: BASICS

CISC 360 Instruction Set Architecture

CS165 Computer Security. Understanding low-level program execution Oct 1 st, 2015

CPEG421/621 Tutorial

Instruction Set Architecture

Lecture 4 CIS 341: COMPILERS

X86 Assembly -Data II:1

The Hardware/Software Interface CSE351 Spring 2015

Machine- level Programming

CS429: Computer Organization and Architecture

administrivia today start assembly probably won t finish all these slides Assignment 4 due tomorrow any questions?

Today: Machine Programming I: Basics

University*of*Washington*

The IA-32 Stack and Function Calls. CS4379/5375 Software Reverse Engineering Dr. Jaime C. Acosta

Carnegie Mellon. 5 th Lecture, Jan. 31, Instructors: Todd C. Mowry & Anthony Rowe

Stacks and Frames Demystified. CSCI 3753 Operating Systems Spring 2005 Prof. Rick Han

Lecture #16: Introduction to Runtime Organization. Last modified: Fri Mar 19 00:17: CS164: Lecture #16 1

Y86 Processor State. Instruction Example. Encoding Registers. Lecture 7A. Computer Architecture I Instruction Set Architecture Assembly Language View

Instruction Set Architecture

Ge-ng at things on the chip you can t easily reach from C

Process Layout and Function Calls

CMSC 313 Lecture 12 [draft] How C functions pass parameters

Machine Program: Procedure. Zhaoguo Wang

ANITA S SUPER AWESOME RECITATION SLIDES

Machine-Level Programming (2)

Topic 7: Activation Records

Practical Malware Analysis

Lab 10: Introduction to x86 Assembly

by Marina Cholakyan, Hyduke Noshadi, Sepehr Sahba and Young Cha

Transcription:

15-410 An Experience Like No Other Discipline Jan. 19, 2018 Dave Eckhardt Brian Railing Slides originally stolen from 15-213 1 15-410, S 18

Synchronization Registration The wait list will probably be done today or tomorrow If you're here but not on any wait list, see me right away If you are an M.S. or or Ph.D. student and have not discussed this class with your advisor, do so today We will not be registering graduate students without hearing from their advisors If you haven't taken 15-213 (A/B, malloc lab ok) Contact me no later than today 2 15-410, S 18

Outline Topics Process memory model IA32 stack organization Register saving conventions Before & after main() Project 0 3 15-410, S 18

Why Only 32? You may have learned x86-64 aka EMT64 aka AMD64 x86-64 is simpler than x86(-32) for user program code Lots of registers, registers more orthogonal Why will 410 be x86 / IA32? x86-64 is not simpler for kernel code Machine begins in 16-bit mode, then 32, finally 64» You don't have time to write 32 64 transition code» If we gave it to you, it would be a big black box Interrupts are more complicated x86-64 is not simpler during debugging More registers means more registers to have wrong values x86-64 virtual memory is a bit of a drag More steps than x86-32, but not more intellectually stimulating There are still a lot of 32-bit machines in the world CS:APP 32-bit guide 4 http://csapp.cs.cmu.edu/3e/waside/waside-ia32.pdf 15-410, S 18

Private Address Spaces Each process has its own private address space. 0xffffffff 0xc0000000 kernel virtual memory (code, data, heap, stack) user stack (created at runtime) memory invisible to user code (stack pointer) Warning: numbers specific to Linux 2.x on IA32!! 0x40000000 memory mapped region for shared libraries run-time heap (managed by malloc) read/write segments (.data,.bss) read-only segments 0x08048000 (.init,.text,.rodata) 5 0 unused 15-410, S 18 brk loaded from the executable file Warning: details vary by OS and kernel version!

IA32 Region of memory managed with stack discipline Grows toward lower addresses Register indicates lowest stack address address of top element stack pointer Bottom Increasing Addresses Pointer Grows Down Top 6 15-410, S 18

IA32 Pushing Pushing pushl Src Fetch operand from Src Maybe a register: %ebp Maybe memory: 8(%ebp) Decrement by 4 Store operand in memory at address given by Bottom Increasing Addresses Pointer -4 Grows Down Top 7 15-410, S 18

IA32 Popping Popping popl Dest Read memory at address given by Increment by 4 Store into Dest operand Bottom Increasing Addresses Pointer +4 Grows Down Top 8 15-410, S 18

Operation Examples pushl %eax popl %edx 0x110 0x110 0x110 0x10c 0x10c 0x10c 0x108 123 0x108 123 0x108 123 0x104 213 0x104 213 %eax 213 %eax 213 %eax 213 %edx 555 %edx 555 %edx 555 213 0x108 0x108 0x104 0x104 0x108 9 15-410, S 18

Procedure Control Flow Use stack to support procedure call and return Procedure call: call label Return address? Address of instruction after call Example from disassembly Push return address; Jump to label 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 10 15-410, F'11

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 0x108 0x108 0x104 %eip 0x804854e %eip 0x804854e 0x8048b90 %eip is program counter 11 15-410, S 18

Procedure Return Example 8048591: c3 ret ret 0x110 0x110 0x10c 0x10c 0x108 123 0x108 123 0x104 0x8048553 0x8048553 0x104 0x104 0x108 %eip 0x8048591 %eip 0x8048591 0x8048553 %eip is program counter 12 15-410, S 18

-Based Languages Languages that support recursion e.g., C, Pascal, Java Code must be reentrant Multiple instantiations of a single procedure live at same time Need some place to store state of each instantiation Arguments Local variables Return pointer (maybe) Weird things (static links, exception handling, ) discipline key observation State for given procedure needed for limited time From time of call to time of return Note: callee returns before caller does Therefore stack allocated in nested frames State for single procedure instantiation 13 15-410, S 18

Call Chain Example Code Structure yoo( ) { who(); } who( ) { ami(); ami(); } Procedure ami() recursive ami( ) { ami(); } Call Chain yoo who ami ami ami ami 14 15-410, S 18

Frames Contents Local variables Return information Temporary space yoo who Management Pointers Space allocated when enter procedure Set-up code Deallocated when return Finish code pointer indicates stack top Frame pointer %ebp indicates start of current frame Frame Pointer %ebp Pointer proc ami Top 15 15-410, S 18

IA32/Linux Frame Current Frame ( Top to Bottom ) Parameters for function we're about to call Argument build Local variables If don't all fit in registers Caller's saved registers Caller's saved frame pointer Caller's Frame Return address Pushed by call instruction Arguments for this call Caller Frame Frame Pointer (%ebp) Pointer () Arguments Return Addr Old %ebp Saved Registers + Local Variables Argument Build 16 15-410, S 18

swap() int zip1 = 15213; int zip2 = 91125; void call_swap() { swap(&zip1, &zip2); } Calling swap from call_swap call_swap: pushl $zip2 pushl $zip1 call swap # Global var # Global var void swap(int *xp, int *yp) { int t0 = *xp; int t1 = *yp; *xp = t1; *yp = t0; } &zip2 &zip1 Rtn adr Resulting 17 15-410, S 18

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

swap() Setup Entering %ebp &zip2 &zip1 Rtn adr swap: pushl %ebp movl,%ebp pushl %ebx 19 15-410, S 18

swap() Setup #1 Entering Resulting %ebp %ebp &zip2 yp &zip1 xp Rtn adr Rtn adr Old %ebp swap: pushl %ebp movl,%ebp pushl %ebx 20 15-410, S 18

swap() Setup #2 Entering Resulting %ebp &zip2 &zip1 Rtn adr swap: pushl %ebp movl,%ebp pushl %ebx yp xp Rtn adr Old %ebp %ebp 21 15-410, S 18

swap() Setup #3 Entering Resulting %ebp &zip2 &zip1 Rtn adr swap: pushl %ebp movl,%ebp pushl %ebx yp xp Rtn adr Old %ebp Old %ebx %ebp 22 15-410, S 18

Effect of swap() Setup Entering %ebp Offset (relative to %ebp) Resulting &zip2 &zip1 Rtn adr 12 yp 8 xp 4 Rtn adr 0 Old %ebp %ebp -4 Old %ebx movl 12(%ebp),%ecx # get yp movl 8(%ebp),%edx # get xp... Body 23 15-410, S 18

swap() Finish #1 swap s Offset Offset 12 yp 8 xp 4 Rtn adr 0 Old %ebp %ebp -4 Old %ebx 12 yp 8 xp 4 Rtn adr 0 Old %ebp %ebp -4 Old %ebx Observation Restoring saved register %ebx Hold that thought movl -4(%ebp),%ebx movl %ebp, popl %ebp ret 24 15-410, S 18

swap() Finish #2 swap s Offset swap s Offset 12 yp 8 xp 4 Rtn adr 0 Old %ebp %ebp -4 Old %ebx 12 yp 8 xp 4 Rtn adr 0 Old %ebp %ebp movl -4(%ebp),%ebx movl %ebp, popl %ebp ret 25 15-410, S 18

swap() Finish #3 swap s Offset swap s Offset %ebp 12 8 4 yp xp Rtn adr 0 Old %ebp %ebp 12 8 4 yp xp Rtn adr movl -4(%ebp),%ebx movl %ebp, popl %ebp ret 26 15-410, S 18

swap() Finish #4 swap s Offset 12 8 4 yp xp Rtn adr %ebp &zip2 &zip1 %ebp Exiting Observation/query Saved & restored caller's register %ebx Didn't do so for %eax, %ecx, or %edx! movl -4(%ebp),%ebx movl %ebp, popl %ebp ret 27 15-410, S 18

Register Saving Conventions When procedure yoo() calls who(): yoo() is the caller, who() is the callee Can a register be used for temporary storage? yoo: movl $15213, %edx call who addl %edx, %eax ret who: movl 8(%ebp), %edx addl $91125, %edx ret Contents of register %edx overwritten by who() 28 15-410, S 18

Register Saving Conventions When procedure yoo() calls who(): yoo() is the caller, who() is the callee Can a register be used for temporary storage? Definitions Caller Save register Caller saves temporary in its frame before calling Callee Save register Callee saves temporary in its frame before using Conventions Which registers are caller-save, callee-save? 29 15-410, S 18

IA32/Linux Register Usage Integer Registers Two have special uses %ebp, 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 holds return value Caller-Save Temporaries Callee-Save Temporaries Special %eax %edx %ecx %ebx %esi %edi %ebp 30 15-410, S 18

Summary 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: instructions + conventions call / ret instructions mix %eip, in a fixed way Register usage conventions Caller / Callee save %ebp and frame organization conventions Which argument is pushed first 31 15-410, S 18

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 (0); } 32 15-410, S 18

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(0) return(0)??? There's no more program to run...right? Where does the 0 go? How does it get there? 410 students should seek to abolish mystery 33 15-410, S 18

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 Other weird things (environment, auxiliary vector) (above argv) arg vector main() printf()... 34 15-410, S 18

The Mysterious Parts What happens when main() does return(0) return(0)? Defined by C to have same effect as exit(0) But how?? 35 15-410, S 18

The Mysterious Parts What happens when main() does return(0) return(0)? Defined by C to have same effect as exit(0) But how?? 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 a legal C function name) /* not actual code */ void ~~main(int argc, char *argv[]) { exit(main(argc, argv)); } 36 15-410, S 18

Project 0 - Crawler C/Assembly function Can be called by any C function Prints stack frames in a symbolic way --- Trace Follows--- Function fun3(c='c', d=2.090000), in Function fun2(f=35.000000), in Function fun1(count=0), in Function fun1(count=1), in Function fun1(count=2), in... 37 15-410, S 18

Project 0 - Crawler Conceptually easy Calling convention specifies layout of stack is just memory - C happily lets you read & write Key questions How do I know 0x80334720 is fun1? How do I know fun3()'s second parameter is called d? 38 15-410, S 18

Project 0 Data Flow fun.c tb.c tb_globals.c symbol-table array many slots, blank 39 15-410, S 18

Project 0 Data Flow - Compilation libtraceback.a fun.o tb.o tb_globals.o 40 15-410, S 18

Project 0 Data Flow - Linking fun fun.o tb.o tb_globals.o debugger info 41 15-410, S 18

Project 0 Data Flow - P0 Post-Linking fun mutate symtabgen simplify fun.o tb.o tb_globals.o debugger info 42 15-410, S 18

Summary Review of stack knowledge What makes main() special Project 0 overview Look for handout this afternoon/evening Start interviewing Project 2/3/4 partners! 43 15-410, S 18