CS216: Program and Data Representation University of Virginia Computer Science Spring 2006 David Evans Lecture 21: Calling.

Slides:



Advertisements
Similar presentations
Calling sequence ESP.
Advertisements

CS 4284 Systems Capstone Godmar Back Processes and Threads.
University of Washington Procedures and Stacks II The Hardware/Software Interface CSE351 Winter 2013.
C Programming and Assembly Language Janakiraman V – NITK Surathkal 2 nd August 2014.
Compiler Construction Intermediate Representation III Activation Records Ran Shaham and Ohad Shacham School of Computer Science Tel-Aviv University.
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.
1 Storage Registers vs. memory Access to registers is much faster than access to memory Goal: store as much data as possible in registers Limitations/considerations:
Context Switch Animation Another one by Anastasia.
Machine-Level Programming III: Procedures Apr. 17, 2006 Topics IA32 stack discipline Register saving conventions Creating pointers to local variables CS213.
PC hardware and x86 3/3/08 Frans Kaashoek MIT
1 Lecture 5: Procedures Assembly Language for Intel-Based Computers, 4th edition Kip R. Irvine.
1 Function Calls Professor Jennifer Rexford COS 217 Reading: Chapter 4 of “Programming From the Ground Up” (available online from the course Web site)
Accessing parameters from the stack and calling functions.
– 1 – , F’02 ICS05 Instructor: Peter A. Dinda TA: Bin Lin Recitation 4.
Run time vs. Compile time
Machine-Level Programming III: Procedures Jan 30, 2003
Compiler Construction Recap Rina Zviel-Girshin and Ohad Shacham School of Computer Science Tel-Aviv University.
Assembly תרגול 8 פונקציות והתקפת buffer.. Procedures (Functions) A procedure call involves passing both data and control from one part of the code to.
September 22, 2014 Pengju (Jimmy) Jin Section E
Compiler Construction Activation records Code Generation Rina Zviel-Girshin and Ohad Shacham School of Computer Science Tel-Aviv University.
Stack Activation Records Topics IA32 stack discipline Register saving conventions Creating pointers to local variables February 6, 2003 CSCE 212H Computer.
Calling Conventions Hakim Weatherspoon CS 3410, Spring 2013 Computer Science Cornell University See P&H 2.8 and 2.12.
ESP int f(int x) {.... } int g(int y) { …. f(2); …. } int main() { …. g(1); …. } EIP 100: 200: 250: 300: 350:
David Evans CS201j: Engineering Software University of Virginia Computer Science Lecture 18: 0xCAFEBABE (Java Byte Codes)
6.828: PC hardware and x86 Frans Kaashoek
Computer Architecture and Operating Systems CS 3230 :Assembly Section Lecture 7 Department of Computer Science and Software Engineering University of Wisconsin-Platteville.
1 Carnegie Mellon Stacks : Introduction to Computer Systems Recitation 5: September 24, 2012 Joon-Sup Han Section F.
Ithaca College Machine-Level Programming IV: IA32 Procedures Comp 21000: Introduction to Computer Systems & Assembly Lang Spring 2013 * Modified slides.
Code Generation Gülfem Savrun Yeniçeri CS 142 (b) 02/26/2013.
CSc 453 Runtime Environments Saumya Debray The University of Arizona Tucson.
Slides revised 3/25/2014 by Patrick Kelley. 2 Procedures Higher Level languages have adopted a standard Referred to as C-style calling Uses the stack.
Fabián E. Bustamante, Spring 2007 Machine-Level Programming III - Procedures Today IA32 stack discipline Register saving conventions Creating pointers.
Lecture 18: 11/5/2002CS170 Fall CS170 Computer Organization and Architecture I Ayman Abdel-Hamid Department of Computer Science Old Dominion University.
Machine-Level Programming III: Procedures Topics IA32 stack discipline Register-saving conventions Creating pointers to local variables CS 105 “Tour of.
Calling Conventions Hakim Weatherspoon CS 3410, Spring 2013 Computer Science Cornell University See P&H 2.8 and 2.12.
Calling Conventions Hakim Weatherspoon CS 3410, Spring 2012 Computer Science Cornell University See P&H 2.8 and 2.12.
CS216: Program and Data Representation University of Virginia Computer Science Spring 2006 David Evans Lecture 22: Unconventional.
Calling Conventions Hakim Weatherspoon CS 3410, Spring 2012 Computer Science Cornell University See P&H 2.8 and 2.12.
Machine-level Programming III: Procedures Topics –IA32 stack discipline –Register saving conventions –Creating pointers to local variables.
CS412/413 Introduction to Compilers and Translators Spring ’99 Lecture 11: Functions and stack frames.
Functions/Methods in Assembly
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.
Overview of Back-end for CComp Zhaopeng Li Software Security Lab. June 8, 2009.
LECTURE 19 Subroutines and Parameter Passing. ABSTRACTION Recall: Abstraction is the process by which we can hide larger or more complex code fragments.
ICS51 Introductory Computer Organization Accessing parameters from the stack and calling functions.
Section 5: Procedures & Stacks
Programs – Calling Conventions
Assembly function call convention
Reading Condition Codes (Cont.)
C function call conventions and the stack
Anton Burtsev February, 2017
143A: Principles of Operating Systems Lecture 4: Calling conventions
Introduction to Compilers Tim Teitelbaum
Machine-Level Programming 4 Procedures
Stack Frame Linkage.
Procedures – Overview Lecture 19 Mon, Mar 28, 2005.
Machine-Level Programming III: Procedures Sept 18, 2001
MIPS Procedure Calls CSE 378 – Section 3.
The Runtime Environment
Multi-modules programming
X86 Assembly Review.
CSC 497/583 Advanced Topics in Computer Security
ICS51 Introductory Computer Organization
Presentation transcript:

CS216: Program and Data Representation University of Virginia Computer Science Spring 2006 David Evans Lecture 21: Calling Conventions

2 UVa CS216 Spring Lecture 21: Calling Conventions Menu x86 C Calling Convention Java Byte Code Wizards

3 UVa CS216 Spring Lecture 21: Calling Conventions Calling Convention Rules for how the caller and the callee make subroutine calls Caller needs to know: –How to pass parameters –What registers might be bashed by called function –What is state of stack after return –Where to get result

4 UVa CS216 Spring Lecture 21: Calling Conventions Calling Convention Caller Where to put params What registers can I assume are same Where to find result State of stack Callee Where to find params What registers must I not bash Where to put result State of stack

5 UVa CS216 Spring Lecture 21: Calling Conventions Calling Convention: Easiest for Caller Caller Where to put params What registers can I assume are same –All of them Where to find result State of stack Callee Where to find params What registers must I not bash –None of them Where to put result State of stack

6 UVa CS216 Spring Lecture 21: Calling Conventions x86 C Calling Convention Caller Params on stack in reverse order Can assume EBX, EDI and ESI are same Find result in EAX Stack rules next Callee Find params on stack in reverse order Must not bash EBX, EDI or ESI Put result in EAX Stack rules next Need to save and restore (EAX), ECX, EDX Need to save and restore EBX, EDI and ESI

7 UVa CS216 Spring Lecture 21: Calling Conventions Stack Convention EBP: Base Pointer –Reference point for finding local variables and parameters –Manipulated only explicitly ESP: Stack Pointer –Pointer to last element used on the stack The next free element is at [esp]-4 –Manipulated implicitly by many instructions: push, pop, call, ret Do we need both of these?

8 UVa CS216 Spring Lecture 21: Calling Conventions parameter 3 parameter 2 parameter 1 return address saved EBP local variable 1 local variable 2 local variable 3 saved EDI saved ESI Higher Addresses Stack Growth EBP ESP [ebp]+8 [ebp]+12 [ebp]+16 [ebp]-4

9 UVa CS216 Spring Lecture 21: Calling Conventions parameter 3 parameter 2 parameter 1 return address saved EBP local variable 1 local variable 2 local variable 3 saved EDI saved ESI Higher Addresses Stack Growth ESP If first local variable is int a[1] what is a[2] ? In which direction should the stack grow?

10 UVa CS216 Spring Lecture 21: Calling Conventions Maintaining EBP Callee: prologue –Must save old value of EBP: push ebp –Update EBP to point to the new frame: mov EBP, ESP –Use EBP to find parameters: [ebp]+8, [ebp]+12, … Callee: before returning –Restore old value of EBP: pop ebp

11 UVa CS216 Spring Lecture 21: Calling Conventions Maintaining ESP Caller: it is maintained implicitly if parameters are push- ed Callee: prologue –Need to make enough space for local variables: sub esp, Callee: before return –Deallocate local variables from stack: mov esp, ebp ; must be done before pop ebp

12 UVa CS216 Spring Lecture 21: Calling Conventions Caller Rules Caller Before: 1.Save bashable (“caller-save”) registers we use 2.Push parameters on stack 3.call implicit: push EIP Caller After: 2.Restore bashable registers 1.Remove parameters from stack

13 UVa CS216 Spring Lecture 21: Calling Conventions Callee Rules Callee Prologue: 1.Save EBP 2.Update EBP 3.Allocate local variables ( ESP ) 4.Save callee-save registers we use (EBX, EDI, ESI ) Callee Return: 5.ret 4.Restore EBP 3.Deallocate local variables ( ESP ) 2.Restore callee- save registers 1.Leave return value in EAX Which of the callee prologue steps could be done by caller?

14 UVa CS216 Spring Lecture 21: Calling Conventions To work with library code, compiler- generated code, must follow all rules carefully If you don’t care about this, which rules must you follow?

15 UVa CS216 Spring Lecture 21: Calling Conventions Byte Code Wizardry Awards

16 UVa CS216 Spring Lecture 21: Calling Conventions Mystery.java Implementation of SHA-1 cryptographic hash algorithm –Code posted on CS216 website now private static int obfuscate(int num, int cnt) { int a = num; int b = cnt; for (int i = 0; i < 216; i += 3) { b = b + a + i; b = b - b; } if (b == 0) { return num; } else { for (int i = 0; i < 256; i++) { a = a - 1; } return a; } }

17 UVa CS216 Spring Lecture 21: Calling Conventions “Black Hat” Honorable Mentions Call the Tester.generate() method Richard Hsu, Dan Chen244 Michael Thomas, Billy Chantree 244 Mike Liu 316 Jongmin Kim 380

18 UVa CS216 Spring Lecture 21: Calling Conventions Fastest Code Not enough variance to distinguish Effectively tied for fastest: –Bin Zhou, Mark Mitchell –Bijan Vakili –David Trang –Jake Fowler and Sean Talts –Michael Thomas and Billy Chantree –Eric Bradbury and Robert Yip

19 UVa CS216 Spring Lecture 21: Calling Conventions Smallest Code Original Mystery.class 1547 bytes 1300 bytes Removing obfuscate Removing line numbers 1021 bytes Improving logic 1015 bytes Replacing operations with calls to Integer.rotateLeft 1061 bytes Being the Byte Code Wizard priceless

20 UVa CS216 Spring Lecture 21: Calling Conventions Smallest Code Jake Fowler and Sean Talts 1015 bytes Michael Thomas and Billy Chantree1021 bytes Will Ashford and Mike Dietz1061 bytes No others below 1300 bytes

21 UVa CS216 Spring Lecture 21: Calling Conventions Prizes “Byte Code Wizards”: Jake Fowler and Sean Talts Selection of books (members of 3 teams pick in order) –John Battelle, “The Search” (Google) –Richard Feynman, “All the Adventures...” –Paul Graham, “Hackers & Painters” –Greg Hoglung & McGraw, “Exploiting Software” –Gary McGraw, “Software Security” –Simon Singh, “The Code Book” Black Hat awards: Sumatra (Java’s big sister island) Coffee

22 UVa CS216 Spring Lecture 21: Calling Conventions Charge PS7 Due Monday –Lots of interesting things to explore –Don’t wait to make progress on it “x86 Guru” title(s) may be awarded for especially clever answers to Question 10