CSE 351: Week 4. Tom Bergan, TA

Similar documents
Assembly Language: Function Calls

Assembly Language: Function Calls" Goals of this Lecture"

Assembly Language: Function Calls" Goals of this Lecture"

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

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

CS213. Machine-Level Programming III: Procedures

Machine-level Programming (3)

X86 Stack Calling Function POV

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

Question 4.2 2: (Solution, p 5) Suppose that the HYMN CPU begins with the following in memory. addr data (translation) LOAD 11110

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

Machine-Level Programming III: Procedures

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

ASSEMBLY III: PROCEDURES. Jo, Heeseung

Assembly III: Procedures. Jo, Heeseung

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

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

CPEG421/621 Tutorial

Machine Programming 3: Procedures

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

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

Stack Discipline Jan. 19, 2018

CPS104 Recitation: Assembly Programming

CS241 Computer Organization Spring 2015 IA

CSC 2400: Computing Systems. X86 Assembly: Function Calls

Systems I. Machine-Level Programming V: Procedures

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

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

CS 31: Intro to Systems ISAs and Assembly. Martin Gagné Swarthmore College February 7, 2017

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

COMP 210 Example Question Exam 2 (Solutions at the bottom)

Homework. In-line Assembly Code Machine Language Program Efficiency Tricks Reading PAL, pp 3-6, Practice Exam 1

Register Allocation, iii. Bringing in functions & using spilling & coalescing

X86 Assembly -Procedure II:1

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

Lab 10: Introduction to x86 Assembly

Instruction Set Architectures

4) C = 96 * B 5) 1 and 3 only 6) 2 and 4 only

W4118: PC Hardware and x86. Junfeng Yang

x86 assembly CS449 Fall 2017

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

Implementing Threads. Operating Systems In Depth II 1 Copyright 2018 Thomas W. Doeppner. All rights reserved.

Simple C Program. Assembly Ouput. Using GCC to produce Assembly. Assembly produced by GCC is easy to recognize:

X86 Review Process Layout, ISA, etc. CS642: Computer Security. Drew Davidson

211: Computer Architecture Summer 2016

System Programming and Computer Architecture (Fall 2009)

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

CS642: Computer Security

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING

Lecture 4 CIS 341: COMPILERS

Compiler Construction D7011E

ICS143A: Principles of Operating Systems. Midterm recap, sample questions. Anton Burtsev February, 2017

x86 Assembly Crash Course Don Porter

ANITA S SUPER AWESOME RECITATION SLIDES

You may work with a partner on this quiz; both of you must submit your answers.

Machine- Level Programming III: Switch Statements and IA32 Procedures

Stack -- Memory which holds register contents. Will keep the EIP of the next address after the call

Sungkyunkwan University

ECE 391 Exam 1 Review Session - Spring Brought to you by HKN

Machine Program: Procedure. Zhaoguo Wang

University of Washington

Instructor: Alvin R. Lebeck

MACHINE-LEVEL PROGRAMMING I: BASICS COMPUTER ARCHITECTURE AND ORGANIZATION

CSC 2400: Computer Systems. Using the Stack for Function Calls

Binghamton University. CS-220 Spring X86 Debug. Computer Systems Section 3.11

SYSTEM CALL IMPLEMENTATION. CS124 Operating Systems Fall , Lecture 14

CSC 8400: Computer Systems. Using the Stack for Function Calls

What is a Compiler? Compiler Construction SMD163. Why Translation is Needed: Know your Target: Lecture 8: Introduction to code generation

Program Exploitation Intro

Instruction Set Architectures

CS241 Computer Organization Spring Loops & Arrays

CSC 2400: Computer Systems. Using the Stack for Function Calls

Process Layout and Function Calls

See P&H 2.8 and 2.12, and A.5-6. Prof. Hakim Weatherspoon CS 3410, Spring 2015 Computer Science Cornell University

143A: Principles of Operating Systems. Lecture 5: Calling conventions. Anton Burtsev January, 2017

Instruction Set Architecture

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

CS 31: Intro to Systems ISAs and Assembly. Kevin Webb Swarthmore College September 25, 2018

Calling Conventions. See P&H 2.8 and Hakim Weatherspoon CS 3410, Spring 2013 Computer Science Cornell University

Machine-level Representation of Programs. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

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

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

hnp://

The Hardware/Software Interface CSE351 Spring 2013

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

Machine-Level Programming II: Control and Arithmetic

Intel assembly language using gcc

Giving credit where credit is due

Assignment 11: functions, calling conventions, and the stack

What the CPU Sees Basic Flow Control Conditional Flow Control Structured Flow Control Functions and Scope. C Flow Control.

CS61 Section Solutions 3

Processes (Intro) Yannis Smaragdakis, U. Athens

CIT Week13 Lecture

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

CS 31: Intro to Systems ISAs and Assembly. Kevin Webb Swarthmore College February 9, 2016

CNIT 127: Exploit Development. Ch 3: Shellcode. Updated

CSE2421 FINAL EXAM SPRING Name KEY. Instructions: Signature

Machine Programming 1: Introduction

Ausgewählte Betriebssysteme. Anatomy of a system call

143A: Principles of Operating Systems. Lecture 4: Calling conventions. Anton Burtsev October, 2017

Transcription:

CSE 35 Week 4 Tom Bergan, TA

Does this code look okay? int binarysearch(int a[], int length, int key) { int low = 0; int high = length - ; while (low <= high) { int mid = (low + high) / ; int midval = a[mid]; if (midval < key) low = mid + ; else if (midval > key) high = mid - ; else return mid; // key found return -; // key not found

Does this code look okay? int binarysearch(int a[], int length, int key) { int low = 0; int high = length - ; while (low <= high) { int mid = (low + high) / ; int midval = a[mid]; if (midval < key) low = What mid + if ; length > 30? else if (midval > key) high = mid - ; else return mid; // key found return -; // key not found 3

Does this code look ok? int mid = (low + high) / ; What if length > 30?... then we could have low = 30 = 0x40000000 high = 30 + = 0x4000000 low + high = 3 + = 0x8000000 int midval = a[mid]; Oops, in two s complement, this is a negative number! (low + high) / = 0xC0000000 = -3547 Crashes because mid < 0 4

How can we fix the bug? int mid = (low + high) / ; int mid = low + ((high - low) / ); (There are other ways, but I think this is the simplest to understand) 5

This was an actual bug in Java java.util.arrays.binarysearch This bug went unnoticed for years. See http//googleresearch.blogspot.com/006/06/extra-extra-read-all-about-it-nearly.html Understanding binary number representations is important! 6

Check your textbook Don t use the international edition! The homework problems are different. 7

Today Questions on Hw or Lab? Procedure calls 8

Procedure Call Example Caller int z = sum(, ); Callee int sum(int x, int y) { return x + y; 9

Procedure Call Example (IA3/Linux) Caller int z = sum(, ); Caller in assembly 0x800 pushl $ 0x8005 pushl $ 0x8009 call sum addl $8, *note these instruction addresses are completely made up for this example 0

Procedure Call Example (IA3/Linux) Caller int z = sum(, ); Caller in assembly 0x800 pushl $ 0x8005 pushl $ 0x8009 call sum addl $8, *note these instruction addresses are completely made up for this example

Procedure Call Example (IA3/Linux) Caller int z = sum(, ); Caller in assembly 0x800 pushl $ 0x8005 pushl $ 0x8009 call sum addl $8, *note these instruction addresses are completely made up for this example

Procedure Call Example (IA3/Linux) Caller int z = sum(, ); Caller in assembly 0x800 pushl $ 0x8005 pushl $ 0x8009 call sum addl $8, *note these instruction addresses are completely made up for this example 3

Procedure Call Example (IA3/Linux) Callee int sum(int x, int y) { return x + y; Callee in assembly (simple version) movl 8(), %edi movl 4(), %eax addl %edi, %eax ret 8() 4() y x Registers %edi 4

Procedure Call Example (IA3/Linux) Callee int sum(int x, int y) { return x + y; Callee in assembly (simple version) movl 8(), %edi movl 4(), %eax addl %edi, %eax ret 8() 4() y x Registers %eax %edi 5

Procedure Call Example (IA3/Linux) Callee int sum(int x, int y) { return x + y; Callee in assembly (simple version) movl 8(), %edi movl 4(), %eax addl %edi, %eax ret 8() 4() y x %eax has the return value! Registers %eax 3 %edi 6

Procedure Call Example (IA3/Linux) Callee int sum(int x, int y) { return x + y; Callee in assembly (simple version) movl 8(), %edi movl 4(), %eax addl %edi, %eax ret %eax has the return value! Registers %eax 3 y x %edi 7 %eip

Procedure Call Example (IA3/Linux) Caller int z = sum(, ); Caller in assembly 0x800 pushl $ 0x8005 pushl $ 0x8009 call sum addl $8, *note these instruction addresses are completely made up for this example 8 Registers %eax %edi %eip 3

Procedure Call Example (IA3/Linux) Caller int z = sum(, ); Caller in assembly 0x800 pushl $ 0x8005 pushl $ 0x8009 call sum addl $8, *note these instruction addresses are completely made up for this example 9 Registers %eax %edi %eip 3

Procedure Call Example (IA3/Linux) Caller int z = sum(, ); Problem - What if Caller used %edi before making the call? Caller in assembly 0x800 pushl $ 0x8005 pushl $ 0x8009 call sum addl $8, *note these instruction addresses are completely made up for this example 0 Registers %eax %edi %eip 3

Procedure Call Example (IA3/Linux) Caller int d = 5; int z = sum(, ); Problem - What if Caller used %edi before making the call? Caller in assembly 0x7fff movl $5, %edi 0x800 pushl $ 0x8005 pushl $ 0x8009 call sum addl $8, *note these instruction addresses are completely made up for this example sum() overwrote %edi! Need to save... Registers %eax %edi %eip 3

Saving Registers Some are caller save - IA3 %eax, %edx, %ecx - These are very commonly used (caller should expect they will be clobbered) Some are callee save - IA3 %ebx, %edi, %esi - These are less commonly used from prior example

Callee Procedure Call Example (IA3/Linux) Callee in assembly (better version) setup body int sum(int x, int y) { return x + y; pushl %ebp movl, %ebp pushl %edi movl (%ebp), %edi movl 8(%ebp), %eax addl %edi, %eax %ebp y x cleanup movl (), %edi movl %ebp, popl %ebp ret 3

Callee Procedure Call Example (IA3/Linux) int sum(int x, int y) { return x + y; Callee in assembly (better version) setup body pushl %ebp movl, %ebp pushl %edi movl (%ebp), %edi movl 8(%ebp), %eax addl %edi, %eax %ebp old %ebp y x cleanup movl (), %edi movl %ebp, popl %ebp ret 4

Callee Procedure Call Example (IA3/Linux) int sum(int x, int y) { return x + y; Callee in assembly (better version) setup body cleanup pushl %ebp movl, %ebp pushl %edi movl (%ebp), %edi movl 8(%ebp), %eax addl %edi, %eax movl (), %edi movl %ebp, popl %ebp ret 5 %ebp old %ebp y x

Callee Procedure Call Example (IA3/Linux) int sum(int x, int y) { return x + y; Callee in assembly (better version) setup body cleanup pushl %ebp movl, %ebp pushl %edi movl (%ebp), %edi movl 8(%ebp), %eax addl %edi, %eax movl (), %edi movl %ebp, popl %ebp ret %ebp old %ebp old %edi saved %edi y x 6

Callee Procedure Call Example (IA3/Linux) int sum(int x, int y) { return x + y; Callee in assembly (better version) setup body cleanup pushl %ebp movl, %ebp pushl %edi movl (%ebp), %edi movl 8(%ebp), %eax addl %edi, %eax movl (), %edi movl %ebp, popl %ebp ret 7 (%ebp) 8(%ebp) %ebp old %ebp old %edi Key %ebp is fixed for the entire function y x

Callee Procedure Call Example (IA3/Linux) int sum(int x, int y) { return x + y; Callee in assembly (better version) setup body cleanup pushl %ebp movl, %ebp pushl %edi movl (%ebp), %edi movl 8(%ebp), %eax addl %edi, %eax movl (), %edi movl %ebp, popl %ebp ret (%ebp) 8(%ebp) %ebp restoring %edi old %ebp old %edi y x 8

Callee Procedure Call Example (IA3/Linux) int sum(int x, int y) { return x + y; Callee in assembly (better version) setup body cleanup pushl %ebp movl, %ebp pushl %edi movl (%ebp), %edi movl 8(%ebp), %eax addl %edi, %eax movl (), %edi movl %ebp, popl %ebp ret %ebp restoring %ebp old %ebp old %edi y x 9

Callee Why use a frame pointer? (%ebp) int sum(int x, int y) { return x + y; To make debugging easier - may move - %ebp is fixed Your compiler emits a symbol map y (%ebp) x 8(%ebp) gdb uses this map when you write print x (%ebp) 8(%ebp) %ebp old %ebp old %edi y x 30

Aside how does gdb s backtrace work? Follow return addresses! - use old %ebp to find prior frame Pseudocode while (pc is not in main ) { pc = 4(%ebp) %ebp = (%ebp) (%ebp) 8(%ebp) 4(%ebp) %ebp old %ebp old %edi y x 3

How is x86-64 different? Pass the first six arguments in registers - In this order %rdi,%rsi,%rdx,%rcx,%r8,%r9 New register save convention - Callee save %rbx,%rbp,%r,%r3,%r4,%r5 - Others are caller save By default, gcc omits the frame pointer - It has to emit more complex debug info (e.g., the location of argument x relative to can change) 3

Procedure Call Example (x86-64/linux) Caller int z = sum(, ); Caller in assembly movl $, %edi movl $, %esi call sum edi not rdi because int is 3-bits Callee int sum(int x, int y) { return x + y; Callee in assembly addl %esi, %edi movl %edi, %eax ret x86-64 with gcc does not use a frame pointer 33 Tip you can force gcc to emit code with a frame pointer using gcc -fno-omit-frame-pointer