Advanced topics in X86 assembly by Istvan Haller.

Slides:



Advertisements
Similar presentations
CS 4284 Systems Capstone Godmar Back Processes and Threads.
Advertisements

1 Exceptions, Interrupts & Traps Operating System Hebrew University Spring 2007.
Machine/Assembler Language Putting It All Together Noah Mendelsohn Tufts University Web:
Computer Architecture CSCE 350
Chapter 6 Limited Direct Execution
CPSC 388 – Compiler Design and Construction
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:
CS 536 Spring Run-time organization Lecture 19.
PC hardware and x86 3/3/08 Frans Kaashoek MIT
3/17/2008Prof. Hilfinger CS 164 Lecture 231 Run-time organization Lecture 23.
1 Function Calls Professor Jennifer Rexford COS 217 Reading: Chapter 4 of “Programming From the Ground Up” (available online from the course Web site)
Run time vs. Compile time
1 Homework Reading –PAL, pp , Machine Projects –Finish mp2warmup Questions? –Start mp2 as soon as possible Labs –Continue labs with your.
Semantics of Calls and Returns
Hardware Support for Operating Systems Sunny Gleason Vivek Uppal COM S 414
Microprocessors Frame Pointers and the use of the –fomit-frame-pointer switch Feb 25th, 2002.
Assembly תרגול 8 פונקציות והתקפת buffer.. Procedures (Functions) A procedure call involves passing both data and control from one part of the code to.
Run-time Environment and Program Organization
1 Run time vs. Compile time The compiler must generate code to handle issues that arise at run time Representation of various data types Procedure linkage.
Stacks and Frames Demystified CSCI 3753 Operating Systems Spring 2005 Prof. Rick Han.
CSE 451: Operating Systems Autumn 2013 Module 6 Review of Processes, Kernel Threads, User-Level Threads Ed Lazowska 570 Allen.
1 CS503: Operating Systems Part 1: OS Interface Dongyan Xu Department of Computer Science Purdue University.
System Calls 1.
Interrupts. What Are Interrupts? Interrupts alter a program’s flow of control  Behavior is similar to a procedure call »Some significant differences.
6.828: PC hardware and x86 Frans Kaashoek
Fall 2008CS 334: Computer SecuritySlide #1 Smashing The Stack A detailed look at buffer overflows as described in Smashing the Stack for Fun and Profit.
OPERATING SYSTEM OVERVIEW. Contents Basic hardware elements.
Runtime Environments Compiler Construction Chapter 7.
Programming Language Principles Lecture 24 Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Subroutines.
Compiler Construction
Operating Systems ECE344 Ashvin Goel ECE University of Toronto OS-Related Hardware.
Practical Session 4. Labels Definition - advanced label: (pseudo) instruction operands ; comment valid characters in labels are: letters, numbers, _,
CSc 453 Runtime Environments Saumya Debray The University of Arizona Tucson.
Assembly Language for Intel-Based Computers, 6 th Edition Chapter 8: Advanced Procedures (c) Pearson Education, All rights reserved. You may.
1 CS/COE0447 Computer Organization & Assembly Language Chapter 5 part 4 Exceptions.
RUN-Time Organization Compiler phase— Before writing a code generator, we must decide how to marshal the resources of the target machine (instructions,
Microprocessors The ia32 User Instruction Set Jan 31st, 2002.
Operating Systems 1 K. Salah Module 1.2: Fundamental Concepts Interrupts System Calls.
Operating Systems CSE 411 CPU Management Sept Lecture 10 Instructor: Bhuvan Urgaonkar.
Processes and Virtual Memory
© Janice Regan, CMPT 300, May CMPT 300 Introduction to Operating Systems Operating Systems Processes and Threads.
Functions/Methods in Assembly
Compiler Construction Code Generation Activation Records
1 Assembly Language: Function Calls Jennifer Rexford.
Calling Procedures C calling conventions. Outline Procedures Procedure call mechanism Passing parameters Local variable storage C-Style procedures Recursion.
Interrupts and Exception Handling. Execution We are quite aware of the Fetch, Execute process of the control unit of the CPU –Fetch and instruction as.
LECTURE 19 Subroutines and Parameter Passing. ABSTRACTION Recall: Abstraction is the process by which we can hide larger or more complex code fragments.
Chapter 6 Limited Direct Execution Chien-Chung Shen CIS/UD
7-Nov Fall 2001: copyright ©T. Pearce, D. Hutchinson, L. Marshall Oct lecture23-24-hll-interrupts 1 High Level Language vs. Assembly.
Of Privilege, Traps, Interrupts & Exceptions Prof. Sirer CS 316 Cornell University.
Section 5: Procedures & Stacks
Programs – Calling Conventions
Processes and threads.
Microprocessor Systems Design I
Anton Burtsev February, 2017
Protection of System Resources
143A: Principles of Operating Systems Lecture 4: Calling conventions
Introduction to Compilers Tim Teitelbaum
Chapter 9 :: Subroutines and Control Abstraction
Optimizing Malloc and Free
Stack Frames and Advanced Procedures
CSE 351 Section 10 The END…Almost 3/7/12
Assembly Language Programming II: C Compiler Calling Sequences
CSE 451: Operating Systems Spring 2012 Module 6 Review of Processes, Kernel Threads, User-Level Threads Ed Lazowska 570 Allen.
Multi-modules programming
System Calls David Ferry CSCI 3500 – Operating Systems
Where is all the knowledge we lost with information? T. S. Eliot
CSC 497/583 Advanced Topics in Computer Security
Return-to-libc Attacks
Presentation transcript:

Advanced topics in X86 assembly by Istvan Haller

Function calls ● Need to enter / exit function ● Implicit manipulation of stack (for IP) ● Enter function: CALL X / EAX PUSH EIP JMP X / EAX ● Exit function: RET X (0) POP HIDDEN_REGISTER JMP HIDDEN_REGISTER ADD ESP, X (POP X bytes)

Example of basic call sequence

Functions in assembly ● Label: Entry point of function, target of CALL FuncName PROC – Label syntax cosmetic – End label available, but cosmetic – FuncName ENDP ● Sequence of code from entry to RET ● Arguments and locals on stack as follows ● Return value typically in registers (EAX)

Function frame management ● Function frame: set of arguments and locals ● Ensure fixed address → Frame Pointer ● Management performed by the compiler ● When entering function (ENTER) PUSH EBP ← Save previous base pointer MOV EBP, ESP ← Get current base pointer ● When exiting function (LEAVE) MOV ESP, EBP ← Restore stack pointer POP EBP ← Restore base pointer

Local arguments on the stack ● Allocated on stack beneath base pointer int arr[100] ← SUB ESP, 400 (ESP below EBP) ● No typing information ← Stream of bytes ● No initial value, just junk on stack – Compilers insert initialization in debug mode ● Allocations may be combined int a int arr[100] ← SUB ESP, 408 int b

Example with frame pointer

Compiler transformations ● Locals may be allocated separately or grouped – Typically grouped together ● ESP typically 16 byte aligned (for performance) – Padding added after allocation – SUB ESP, 416 (4 * for padding) ● Temporary allocations may also be merged – Scoped allocations, registers saved to stack, etc.

Calling conventions ● Defines the standard for passing arguments ● Caller and callee need to agree ● Enforced by compiler ● Important when using 3 rd party libraries ● Different styles ↔ different advantages

Register calling convention ● Pass arguments though registers ● Register roles clearly defined ● Only for limited argument count ● Registers still save on stack in callee ● Extra registers for arguments on 64 bit

System V AMD64 ABI ● Used on *NIX systems ● Integer or pointer arguments passed in: – RDI, RSI, RDX, RCX, R8, and R9 ● System calls: R10 is used instead of RCX ● Floating point arguments use XMM registers ● Additional arguments on stack ● Microsoft x64 calling convention similar – Uses: RCX, RDX, R8, R9

Pascal calling convention ● Defined by Pascal programming language ● Argument ordering: left → right ● Callee cleans stack – Code not repeated for every caller – Argument count must be fixed ● _stdcall (Win32 API) is a variation of it – Reverse argument ordering

Pascal calling convention example

What about varargs? ● Variable argument count relevant – printf function in C ● Only caller knows exact argument count – Caller needs to clean arguments ● What about argument ordering? – Let's look at the stack again!

Left→Right argument ordering

Right→Left argument ordering

Using varargs ● Compiler has no information about length ● Argument count encoded in fixed arguments – Like the format string of printf ● Nothing enforces correctness! ● Does a mismatch crash the application? – No; Arguments are cleaned by caller – Still a security vulnerability

Mismatched varargs

C calling convention ● Argument ordering: right → left – Location of first arguments known on stack ● Caller cleans stack – Allows variable argument count ● Default convention for GCC ● Known as _cdecl

C calling convention example

Hardware interaction with interrupts ● Multiple hardware devices connected to CPU ● Cannot poll devices continuously ● Necessity for notification mechanism – Asynchronous events – Specialized handling routine – Ability to run in parallel with regular code

Operating principle of interrupts ● Interrupt occurs, CPU stops regular execution – After finishing current instruction ● Function table contains pointers for handlers ● CPU looks up table entry based on identifier ● Execution jumps to handler (CALL) ● Handler saves registers to stack! ● Handler finishes and returns execution

Influence on regular execution ● Execution interrupted after current instruction ● Program state not saved! ● Temporary computations still in registers! ● Flag register is saved automatically ● Handler should consider rest of state

Writing interrupt handlers ● Same as regular function ● Terminates with IRET ● No arguments ● Stack not relevant above initial stack pointer ● Interact through global memory ● Simplicity is key, minimize “interruption”

Software interrupts ● Interrupts can also incur from software ● Software exceptions generate interrupts – Divide by zero, Page fault, etc. ● Interrupts can be triggered manually – INT X – Useful to notify kernel from user code

System calls using interrupts ● Traditional implementation of system calls ● Software interrupt can bridge privilege levels ● Execution under “user” control – Possible to use arguments in registers ● User space code manages interrupt – Moves arguments to specific registers – Triggers system interrupt (Linux: 80h)

System call flow

Preemption in operating systems ● How to schedule multiple tasks on s single CPU? ● Preemptive operating systems – Scheduling performed by the OS – Applications forced to accept scheduling policy ● Non-preemptive operating systems – Cooperative scheduling – Tasks control their own execution – Resources handed over when reaching checkpoints

Preemption with interrupts ● Task has monopoly over CPU when executing ● Interrupt can pause task execution ● OS kernel notified and performs scheduling ● Task can resume as normal whenever OS wants – Same effect as long “interrupt” ● Typical interrupt for preemption: timer – Invoke scheduler every X time-units

Dynamic memory management ● Stack+Globals for allocation known in advance ● Adaptive allocation necessary sometimes ● Why not avoid stack for more security? ● Dedicated part of memory for run-time use – Heap ● Managed using: malloc/free

Requirements of malloc void* malloc(size) ● Need to search for available memory chunk ● Bitmap: mapping memory bytes to boolean flag – Bitmap representing entire address space – Allocation only in multiples of X bytes – Need to traverse large bitmap to find free space ● Free-list: list of free chunks – Split most suitable free chunk in list – Quick to find free chunk

Requirements of free void free(ptr) ● Need to release memory chunk ● Bitmap: reset boolean flags ● Free-list: add chunk back to list – Fragmentation: malloc splits, free does not restore – Need to merge with neighboring free chunk ● Inline free-list: allocation info inline with data – Quickly check neighbors of chunk

Doug Lea’s Malloc ● Memory chunks can be allocated or free ● Free chunks cannot be neighbors ● Inline free-list: – Allocated chunk contain: ● SIZE: size of chunk + status bits ● PREVIOUS_SIZE: size of previous chunk (“pseudo” ptr) – Free chunks also contain: ● FORWARD: next free chunk in doubly linked list ● BACKWARD: previous free chunk in list

Memory allocation ● Find suitable free memory chunk in free-list – Can be split from larger free chunk ● Remove chunk from free-list (unlink) – If split occurred, add back remainder ● Set up SIZE for chunk – Also PREVIOUS_SIZE for next one ● Return pointer to user

Memory deallocation ● Check memory chunk at the front if allocated – Using status bits – Consolidate into single chunk if free ● Check memory chunk at the back if allocated – Using current pointer and SIZE to find its start – Consolidate into single chunk if free ● Consolidation requires removing old chunk (unlink) and adding inserting new one in list