53
1 Prof. Dr. Michael Pradel Software Lab, University of Stuttgart Winter 2019/2020 Programming Paradigms Lecture 12: Subroutines and Control Abstraction (Part 1)

Abstraction (Part 1) Subroutines and Control - Software Labsoftware-lab.org/teaching/winter2019/pp/lecture_control_abstraction… · 1 Prof. Dr. Michael Pradel Software Lab, University

  • Upload
    others

  • View
    3

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Abstraction (Part 1) Subroutines and Control - Software Labsoftware-lab.org/teaching/winter2019/pp/lecture_control_abstraction… · 1 Prof. Dr. Michael Pradel Software Lab, University

1

Prof. Dr. Michael Pradel

Software Lab, University of StuttgartWinter 2019/2020

Programming Paradigms

Lecture 12:

Subroutines and ControlAbstraction (Part 1)

Page 2: Abstraction (Part 1) Subroutines and Control - Software Labsoftware-lab.org/teaching/winter2019/pp/lecture_control_abstraction… · 1 Prof. Dr. Michael Pradel Software Lab, University

2 - 1

Wake-up Exercise

What does the following C code print?

https://ilias3.uni-stuttgart.de/vote/0ZT9

#include <stdio.h>

int *foo() {int a = 5;return &a;

}

int main() {int* p = foo();printf("%p\n", p);

}

Page 3: Abstraction (Part 1) Subroutines and Control - Software Labsoftware-lab.org/teaching/winter2019/pp/lecture_control_abstraction… · 1 Prof. Dr. Michael Pradel Software Lab, University

2 - 2

Wake-up Exercise

What does the following C code print?

https://ilias3.uni-stuttgart.de/vote/0ZT9

#include <stdio.h>

int *foo() {int a = 5;return &a;

}

int main() {int* p = foo();printf("%p\n", p);

}

Result on mymachine: (nil)

In general:Undefined behavior

Page 4: Abstraction (Part 1) Subroutines and Control - Software Labsoftware-lab.org/teaching/winter2019/pp/lecture_control_abstraction… · 1 Prof. Dr. Michael Pradel Software Lab, University

2 - 3

Wake-up Exercise

What does the following C code print?

https://ilias3.uni-stuttgart.de/vote/0ZT9

#include <stdio.h>

int *foo() {int a = 5;return &a;

}

int main() {int* p = foo();printf("%p\n", p);

}

Result on mymachine: (nil)

In general:Undefined behavior

Access to localvariable of foo,which doesn’t existanymore

Page 5: Abstraction (Part 1) Subroutines and Control - Software Labsoftware-lab.org/teaching/winter2019/pp/lecture_control_abstraction… · 1 Prof. Dr. Michael Pradel Software Lab, University

3 - 1

Control vs. Data Abstraction

� Abstract awell-definedoperation

� E.g., a subroutineor an exceptionhandler

� Abstract how torepresentinformation

� E.g., types andclasses

Page 6: Abstraction (Part 1) Subroutines and Control - Software Labsoftware-lab.org/teaching/winter2019/pp/lecture_control_abstraction… · 1 Prof. Dr. Michael Pradel Software Lab, University

3 - 2

Control vs. Data Abstraction

� Abstract awell-definedoperation

� E.g., a subroutineor an exceptionhandler

� Abstract how torepresentinformation

� E.g., types andclasses

Focus of this andnext lecture

Page 7: Abstraction (Part 1) Subroutines and Control - Software Labsoftware-lab.org/teaching/winter2019/pp/lecture_control_abstraction… · 1 Prof. Dr. Michael Pradel Software Lab, University

4

Terminology

� Subroutine: Mechanism for controlabstraction� Function: Subroutine that returns a value

� Procedure: Subroutine that doesn’t return a

value

� Parameters� Actual parameters = arguments: Data passed

by caller

� Formal parameters: Data received by callee

Page 8: Abstraction (Part 1) Subroutines and Control - Software Labsoftware-lab.org/teaching/winter2019/pp/lecture_control_abstraction… · 1 Prof. Dr. Michael Pradel Software Lab, University

5

Overview

� Calling Sequences

� Parameter Passing

� Exception Handling

� Coroutines

� Events

Page 9: Abstraction (Part 1) Subroutines and Control - Software Labsoftware-lab.org/teaching/winter2019/pp/lecture_control_abstraction… · 1 Prof. Dr. Michael Pradel Software Lab, University

6

Calling Sequences

� Low-level code executed to maintaincall stack

� Before subroutine call in caller

� At beginning of subroutine in callee (“prologue”)

� At end of subroutine in callee (“epilogue”)

� After subroutine call in caller

Page 10: Abstraction (Part 1) Subroutines and Control - Software Labsoftware-lab.org/teaching/winter2019/pp/lecture_control_abstraction… · 1 Prof. Dr. Michael Pradel Software Lab, University

7

Why Does It Matter?

� Important to

� Understand performance implications

� Understand security implications, e.g., stack

smashing attacks

� Choose/design/implement compilers

Page 11: Abstraction (Part 1) Subroutines and Control - Software Labsoftware-lab.org/teaching/winter2019/pp/lecture_control_abstraction… · 1 Prof. Dr. Michael Pradel Software Lab, University

8

Reminder: Stack Layout

� Each procedure call:One stack frame (or activation record)

� Frame pointer: Base address used toaccess data in current stack frame

� Stack pointer: First unused (or,sometimes, last used) location incurrent stack frame

Page 12: Abstraction (Part 1) Subroutines and Control - Software Labsoftware-lab.org/teaching/winter2019/pp/lecture_control_abstraction… · 1 Prof. Dr. Michael Pradel Software Lab, University

50

Page 13: Abstraction (Part 1) Subroutines and Control - Software Labsoftware-lab.org/teaching/winter2019/pp/lecture_control_abstraction… · 1 Prof. Dr. Michael Pradel Software Lab, University

10 - 1

Tasks to Perform

� Pass parameters and return value(s)� Update program counter� Save return address� Save and restore registers� Update stack and frame pointers

Page 14: Abstraction (Part 1) Subroutines and Control - Software Labsoftware-lab.org/teaching/winter2019/pp/lecture_control_abstraction… · 1 Prof. Dr. Michael Pradel Software Lab, University

10 - 2

Tasks to Perform

� Pass parameters and return value(s)� Update program counter� Save return address� Save and restore registers� Update stack and frame pointers

Program counter: Address ofcode to execute next

Page 15: Abstraction (Part 1) Subroutines and Control - Software Labsoftware-lab.org/teaching/winter2019/pp/lecture_control_abstraction… · 1 Prof. Dr. Michael Pradel Software Lab, University

10 - 3

Tasks to Perform

� Pass parameters and return value(s)� Update program counter� Save return address� Save and restore registers� Update stack and frame pointers

Otherwise, don’t know what codelocation to return back to

Page 16: Abstraction (Part 1) Subroutines and Control - Software Labsoftware-lab.org/teaching/winter2019/pp/lecture_control_abstraction… · 1 Prof. Dr. Michael Pradel Software Lab, University

10 - 4

Tasks to Perform

� Pass parameters and return value(s)� Update program counter� Save return address� Save and restore registers� Update stack and frame pointers

Registers: Very fast but limitedintermediate memory

Page 17: Abstraction (Part 1) Subroutines and Control - Software Labsoftware-lab.org/teaching/winter2019/pp/lecture_control_abstraction… · 1 Prof. Dr. Michael Pradel Software Lab, University

10 - 5

Tasks to Perform

� Pass parameters and return value(s)� Update program counter� Save return address� Save and restore registers� Update stack and frame pointers

Where to perform those?

� Possibly either in caller or in callee

� Preferably in callee: Requires space only once

per subroutine, not at each call site

Page 18: Abstraction (Part 1) Subroutines and Control - Software Labsoftware-lab.org/teaching/winter2019/pp/lecture_control_abstraction… · 1 Prof. Dr. Michael Pradel Software Lab, University

11

Typical Calling Sequence (1/4)

� Steps performed by caller before thecall

� Save registers whose values may be need after

the call

� Compute values of arguments and move them

into stack or registers

� Pass return address and jump to subroutine

Page 19: Abstraction (Part 1) Subroutines and Control - Software Labsoftware-lab.org/teaching/winter2019/pp/lecture_control_abstraction… · 1 Prof. Dr. Michael Pradel Software Lab, University

51

Page 20: Abstraction (Part 1) Subroutines and Control - Software Labsoftware-lab.org/teaching/winter2019/pp/lecture_control_abstraction… · 1 Prof. Dr. Michael Pradel Software Lab, University

13

Typical Calling Sequence (2/4)

� Steps performed by callee in prologue

� Allocate a frame: Subtract an appropriate

constant from the stack pointer

� Save old frame pointer on stack and update it

to point to newly allocated frame

� Save registers that may be overwritten bycurrent subroutine

Page 21: Abstraction (Part 1) Subroutines and Control - Software Labsoftware-lab.org/teaching/winter2019/pp/lecture_control_abstraction… · 1 Prof. Dr. Michael Pradel Software Lab, University

52

Page 22: Abstraction (Part 1) Subroutines and Control - Software Labsoftware-lab.org/teaching/winter2019/pp/lecture_control_abstraction… · 1 Prof. Dr. Michael Pradel Software Lab, University

15 - 1

Quiz: Stack Frames

Assume the frame pointer is stored inregister ebp, addresses are 4 bytes long,and all arguments are 32-bit integers.

What is the address the callee uses toaccess the second argument?

https://ilias3.uni-stuttgart.de/vote/0ZT9

Page 23: Abstraction (Part 1) Subroutines and Control - Software Labsoftware-lab.org/teaching/winter2019/pp/lecture_control_abstraction… · 1 Prof. Dr. Michael Pradel Software Lab, University

15 - 2

Quiz: Stack Frames

Assume the frame pointer is stored inregister ebp, addresses are 4 bytes long,and all arguments are 32-bit integers.

What is the address the callee uses toaccess the second argument?

https://ilias3.uni-stuttgart.de/vote/0ZT9

Answer: ebp + 12 bytes

Page 24: Abstraction (Part 1) Subroutines and Control - Software Labsoftware-lab.org/teaching/winter2019/pp/lecture_control_abstraction… · 1 Prof. Dr. Michael Pradel Software Lab, University

53

Page 25: Abstraction (Part 1) Subroutines and Control - Software Labsoftware-lab.org/teaching/winter2019/pp/lecture_control_abstraction… · 1 Prof. Dr. Michael Pradel Software Lab, University

16

Typical Calling Sequence (3/4)

� Steps performed by callee in epilogue

� Move return value into register or reserved

location in stack

� Restore registers (to state before call)

� Restore frame pointer and stack pointer

� Jump back to return address

Page 26: Abstraction (Part 1) Subroutines and Control - Software Labsoftware-lab.org/teaching/winter2019/pp/lecture_control_abstraction… · 1 Prof. Dr. Michael Pradel Software Lab, University

54

Page 27: Abstraction (Part 1) Subroutines and Control - Software Labsoftware-lab.org/teaching/winter2019/pp/lecture_control_abstraction… · 1 Prof. Dr. Michael Pradel Software Lab, University

18

Typical Calling Sequence (4/4)

� Steps performed by caller after the call

� Move return value to where it is needed

� Restore registers (to state before call)

Page 28: Abstraction (Part 1) Subroutines and Control - Software Labsoftware-lab.org/teaching/winter2019/pp/lecture_control_abstraction… · 1 Prof. Dr. Michael Pradel Software Lab, University

19

Saving and Restoring Registers

� Which registers to save and restore?

� E.g., x86 has 8 general purpose registers

� Ideally, save if

• Caller may use them after the call

• Callee needs them for other purposes

� In practice, compiler safely overapproximates

• Better to store once too much then to loose data

Page 29: Abstraction (Part 1) Subroutines and Control - Software Labsoftware-lab.org/teaching/winter2019/pp/lecture_control_abstraction… · 1 Prof. Dr. Michael Pradel Software Lab, University

20

Saving and Restoring Registers

� Where to save and restore registers?

� Caller could save (and then restore) all

registers that are in use

� Callee could saves (and then restore) all

registers it overwrites

� In practice: Calling sequence conventions for

each architecture

• E.g., on x86, three registers are caller-saved, restis callee-saved

Page 30: Abstraction (Part 1) Subroutines and Control - Software Labsoftware-lab.org/teaching/winter2019/pp/lecture_control_abstraction… · 1 Prof. Dr. Michael Pradel Software Lab, University

21

Inlining

� Calling sequences are expensive� Optimization, in particular for small

functions:Inlining� Copy of callee becomes part of caller

� Avoids overhead of calling sequence

� Enables other optimizations across subroutine

boundaries

� But: Increases code size

Page 31: Abstraction (Part 1) Subroutines and Control - Software Labsoftware-lab.org/teaching/winter2019/pp/lecture_control_abstraction… · 1 Prof. Dr. Michael Pradel Software Lab, University

22

Example: Inlining Hints in C

� Programmer may suggest whichsubroutine to inline

� Example:inline int max(int a, int b) {return a > b ? a : b;

}

Page 32: Abstraction (Part 1) Subroutines and Control - Software Labsoftware-lab.org/teaching/winter2019/pp/lecture_control_abstraction… · 1 Prof. Dr. Michael Pradel Software Lab, University

23

Application: Stack Smashing

� Special kind of buffer overflowvulnerability

� Lack of bounds checking: May write beyond

space allocated for a local variable

� Malicious input can overwrite return address

� Program will jump into malicious code

Page 33: Abstraction (Part 1) Subroutines and Control - Software Labsoftware-lab.org/teaching/winter2019/pp/lecture_control_abstraction… · 1 Prof. Dr. Michael Pradel Software Lab, University

24

Example: Stack Smashing

int read_nb_from_file(FILE *s) {char buf[100];char *p = buf;do {/* read from stream s */*p = getc(s);} while (*p++ != ’\n’);*p = ’\0’;return atoi(buf);

}

Page 34: Abstraction (Part 1) Subroutines and Control - Software Labsoftware-lab.org/teaching/winter2019/pp/lecture_control_abstraction… · 1 Prof. Dr. Michael Pradel Software Lab, University

55

Page 35: Abstraction (Part 1) Subroutines and Control - Software Labsoftware-lab.org/teaching/winter2019/pp/lecture_control_abstraction… · 1 Prof. Dr. Michael Pradel Software Lab, University

26

Overview

� Calling Sequences

� Parameter Passing

� Exception Handling

� Coroutines

� Events

Page 36: Abstraction (Part 1) Subroutines and Control - Software Labsoftware-lab.org/teaching/winter2019/pp/lecture_control_abstraction… · 1 Prof. Dr. Michael Pradel Software Lab, University

27

Parameter Passing

� What does it mean if a parameter ispassed to a callee?

� Different PLs have different parameterpassing modes

� Call by value

� Call by reference

� Call by value/result

� Call by sharing

Page 37: Abstraction (Part 1) Subroutines and Control - Software Labsoftware-lab.org/teaching/winter2019/pp/lecture_control_abstraction… · 1 Prof. Dr. Michael Pradel Software Lab, University

28 - 1

Call by Value

� Caller passes copy of value to callee

� Once in callee, formal parameter isindependent of the actual parameter

Page 38: Abstraction (Part 1) Subroutines and Control - Software Labsoftware-lab.org/teaching/winter2019/pp/lecture_control_abstraction… · 1 Prof. Dr. Michael Pradel Software Lab, University

28 - 2

Call by Value

� Caller passes copy of value to callee

� Once in callee, formal parameter isindependent of the actual parameter

x : integerprocedure foo(y : integer)y := 3print x

...x := 2foo(x)print(x)

Page 39: Abstraction (Part 1) Subroutines and Control - Software Labsoftware-lab.org/teaching/winter2019/pp/lecture_control_abstraction… · 1 Prof. Dr. Michael Pradel Software Lab, University

28 - 3

Call by Value

� Caller passes copy of value to callee

� Once in callee, formal parameter isindependent of the actual parameter

x : integerprocedure foo(y : integer)y := 3print x

...x := 2foo(x)print(x)

Assignment hasno visible effect

Page 40: Abstraction (Part 1) Subroutines and Control - Software Labsoftware-lab.org/teaching/winter2019/pp/lecture_control_abstraction… · 1 Prof. Dr. Michael Pradel Software Lab, University

28 - 4

Call by Value

� Caller passes copy of value to callee

� Once in callee, formal parameter isindependent of the actual parameter

x : integerprocedure foo(y : integer)y := 3print x

...x := 2foo(x)print(x)

Prints 2 twice

Page 41: Abstraction (Part 1) Subroutines and Control - Software Labsoftware-lab.org/teaching/winter2019/pp/lecture_control_abstraction… · 1 Prof. Dr. Michael Pradel Software Lab, University

29 - 1

Call by Reference

� Formal parameter is new name ofactual parameter

� Both names refer to the same value

Page 42: Abstraction (Part 1) Subroutines and Control - Software Labsoftware-lab.org/teaching/winter2019/pp/lecture_control_abstraction… · 1 Prof. Dr. Michael Pradel Software Lab, University

29 - 2

Call by Reference

� Formal parameter is new name ofactual parameter

� Both names refer to the same value

x : integerprocedure foo(y : integer)y := 3print x

...x := 2foo(x)print(x)

Page 43: Abstraction (Part 1) Subroutines and Control - Software Labsoftware-lab.org/teaching/winter2019/pp/lecture_control_abstraction… · 1 Prof. Dr. Michael Pradel Software Lab, University

29 - 3

Call by Reference

� Formal parameter is new name ofactual parameter

� Both names refer to the same value

x : integerprocedure foo(y : integer)y := 3print x

...x := 2foo(x)print(x)

Refers to samevalue as outervariable x

Page 44: Abstraction (Part 1) Subroutines and Control - Software Labsoftware-lab.org/teaching/winter2019/pp/lecture_control_abstraction… · 1 Prof. Dr. Michael Pradel Software Lab, University

29 - 4

Call by Reference

� Formal parameter is new name ofactual parameter

� Both names refer to the same value

x : integerprocedure foo(y : integer)y := 3print x

...x := 2foo(x)print(x)

Prints 3 twice

Page 45: Abstraction (Part 1) Subroutines and Control - Software Labsoftware-lab.org/teaching/winter2019/pp/lecture_control_abstraction… · 1 Prof. Dr. Michael Pradel Software Lab, University

30 - 1

Call by Value/Result

� Argument is copied on call

� Resulting value is copied back toargument on return

Page 46: Abstraction (Part 1) Subroutines and Control - Software Labsoftware-lab.org/teaching/winter2019/pp/lecture_control_abstraction… · 1 Prof. Dr. Michael Pradel Software Lab, University

30 - 2

Call by Value/Result

� Argument is copied on call

� Resulting value is copied back toargument on return

x : integerprocedure foo(y : integer)y := 3print x

...x := 2foo(x)print(x)

Page 47: Abstraction (Part 1) Subroutines and Control - Software Labsoftware-lab.org/teaching/winter2019/pp/lecture_control_abstraction… · 1 Prof. Dr. Michael Pradel Software Lab, University

30 - 3

Call by Value/Result

� Argument is copied on call

� Resulting value is copied back toargument on return

x : integerprocedure foo(y : integer)y := 3print x

...x := 2foo(x)print(x)

Quiz: What does thecode print?

https://ilias3.uni-stuttgart.de/vote/0ZT9

Page 48: Abstraction (Part 1) Subroutines and Control - Software Labsoftware-lab.org/teaching/winter2019/pp/lecture_control_abstraction… · 1 Prof. Dr. Michael Pradel Software Lab, University

30 - 4

Call by Value/Result

� Argument is copied on call

� Resulting value is copied back toargument on return

x : integerprocedure foo(y : integer)y := 3print x

...x := 2foo(x)print(x)

Refers to valuedifferent fromouter variable x

Page 49: Abstraction (Part 1) Subroutines and Control - Software Labsoftware-lab.org/teaching/winter2019/pp/lecture_control_abstraction… · 1 Prof. Dr. Michael Pradel Software Lab, University

30 - 5

Call by Value/Result

� Argument is copied on call

� Resulting value is copied back toargument on return

x : integerprocedure foo(y : integer)y := 3print x

...x := 2foo(x)print(x)

Prints 2

Prints 3

Page 50: Abstraction (Part 1) Subroutines and Control - Software Labsoftware-lab.org/teaching/winter2019/pp/lecture_control_abstraction… · 1 Prof. Dr. Michael Pradel Software Lab, University

31 - 1

Call by Sharing

� In PLs with reference model ofvariables� Arguments are passed as values, but the

values are references

Page 51: Abstraction (Part 1) Subroutines and Control - Software Labsoftware-lab.org/teaching/winter2019/pp/lecture_control_abstraction… · 1 Prof. Dr. Michael Pradel Software Lab, University

31 - 2

Call by Sharing

� In PLs with reference model ofvariables� Arguments are passed as values, but the

values are referencesx : integerprocedure foo(y : integer)y := 3print x

...x := 2foo(x)print(x)

Prints 3 twice

Page 52: Abstraction (Part 1) Subroutines and Control - Software Labsoftware-lab.org/teaching/winter2019/pp/lecture_control_abstraction… · 1 Prof. Dr. Michael Pradel Software Lab, University

32

Passing Models in Popular PLs

� C: By value, except arrays are passedby reference

� Passing pointers can emulate call by reference

� Fortran: All arguments are passed byreference

� Java: Hybrid passing model

� Built-in, primitive types: By value

� Instances of classes: By reference

Page 53: Abstraction (Part 1) Subroutines and Control - Software Labsoftware-lab.org/teaching/winter2019/pp/lecture_control_abstraction… · 1 Prof. Dr. Michael Pradel Software Lab, University

33

Overview

� Calling Sequences

� Parameter Passing

� Exception Handling

� Coroutines

� Events

4