Making Address Spaces Smaller

Similar documents
Main Memory. ICS332 Operating Systems

CIT 595 Spring System Software: Programming Tools. Assembly Process Example: First Pass. Assembly Process Example: Second Pass.

(Extract from the slides by Terrance E. Boult

Generating Programs and Linking. Professor Rick Han Department of Computer Science University of Colorado at Boulder

Executables and Linking. CS449 Spring 2016

Project #1: Tracing, System Calls, and Processes

Debugging. ICS312 Machine-Level and Systems Programming. Henri Casanova

Reviewing gcc, make, gdb, and Linux Editors 1

Memory Management. To do. q Basic memory management q Swapping q Kernel memory allocation q Next Time: Virtual memory

Executables and Linking. CS449 Fall 2017

CSCI-243 Exam 2 Review February 22, 2015 Presented by the RIT Computer Science Community

CS2141 Software Development using C/C++ Libraries

Operating Systems. 09. Memory Management Part 1. Paul Krzyzanowski. Rutgers University. Spring 2015

Generic Programming in C

ECE 598 Advanced Operating Systems Lecture 12

Operating System Principles: Memory Management Swapping, Paging, and Virtual Memory CS 111. Operating Systems Peter Reiher

Memory Management: Virtual Memory and Paging CS 111. Operating Systems Peter Reiher

Page 1. Goals for Today" Virtualizing Resources" Important Aspects of Memory Multiplexing" CS162 Operating Systems and Systems Programming Lecture 20

ECE 598 Advanced Operating Systems Lecture 10

Week 5, continued. This is CS50. Harvard University. Fall Cheng Gong

Inter-Process Communications (IPC)

Revising CS-M41. Oliver Kullmann Computer Science Department Swansea University. Linux Lab Swansea, December 13, 2011.

Slide Set 4. for ENCM 369 Winter 2018 Section 01. Steve Norman, PhD, PEng

CS 322 Operating Systems Programming Assignment 4 Writing a memory manager Due: November 16, 11:30 PM

Department of Computer Science and Engineering Yonghong Yan

CS 322 Operating Systems Programming Assignment 4 Writing a memory manager Due: April 5, 11:30 PM

Profilers and Debuggers. Introductory Material. One-Slide Summary

Computer Systems and Networks

Process Address Spaces and Binary Formats

COSC 2P91. Introduction Part Deux. Week 1b. Brock University. Brock University (Week 1b) Introduction Part Deux 1 / 14

CMSC 216 Introduction to Computer Systems Lecture 23 Libraries

COS 318: Operating Systems

A Simplistic Program Translation Scheme

Language Translation. Compilation vs. interpretation. Compilation diagram. Step 1: compile. Step 2: run. compiler. Compiled program. program.

Today s Big Adventure

Today s Big Adventure

Reliable C++ development - session 1: From C to C++ (and some C++ features)

Static Methods. Why use methods?

CS 33. Libraries. CS33 Intro to Computer Systems XXIX 1 Copyright 2017 Thomas W. Doeppner. All rights reserved.

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

Linking and Loading. ICS312 - Spring 2010 Machine-Level and Systems Programming. Henri Casanova

LECTURE 2. Compilers and Interpreters

Multiple file project management & Makefile

[07] SEGMENTATION 1. 1

Operating Systems. Objective

An Introduction to Python (TEJ3M & TEJ4M)

Scientific Programming in C IX. Debugging

Continue: How do I learn C? C Primer Continued (Makefiles, debugging, and more ) Last Time: A Simple(st) C Program 1-hello-world.c!

A software view. Computer Systems. The Compilation system. How it works. 1. Preprocesser. 1. Preprocessor (cpp)

Embedded Systems Programming

Programming Tools. Venkatanatha Sarma Y. Lecture delivered by: Assistant Professor MSRSAS-Bangalore

CSE 303: Concepts and Tools for Software Development

Basic Keywords Practice Session

Introduction to Operating Systems Prof. Chester Rebeiro Department of Computer Science and Engineering Indian Institute of Technology, Madras

Re-entrant code and Libraries

Project 2 Overview: Part A: User space memory allocation

Introduction to RISC-V

Final Examination CS 111, Fall 2016 UCLA. Name:

Midterm 1. Administrivia. Virtualizing Resources. CS162 Operating Systems and Systems Programming Lecture 12. Address Translation

Cost of Your Programs

CSC209. Software Tools and Systems Programming.

COMP 3500 Introduction to Operating Systems Project 5 Virtual Memory Manager

My malloc: mylloc and mhysa. Johan Montelius HT2016

CS 31: Intro to Systems Pointers and Memory. Martin Gagne Swarthmore College February 16, 2016

CS 326: Operating Systems. Lecture 1

Computer Science 322 Operating Systems Mount Holyoke College Spring Topic Notes: Processes and Threads

Operating-System Structures

CS 220: Introduction to Parallel Computing. Arrays. Lecture 4

Linkers and Loaders. CS 167 VI 1 Copyright 2008 Thomas W. Doeppner. All rights reserved.

Laboratorio di Tecnologie dell'informazione

Page 1. Goals for Today" Important Aspects of Memory Multiplexing" Virtualizing Resources" CS162 Operating Systems and Systems Programming Lecture 9

CSC209. Software Tools and Systems Programming.

QUIZ. What are 3 differences between C and C++ const variables?

CS 33. Libraries. CS33 Intro to Computer Systems XXVIII 1 Copyright 2018 Thomas W. Doeppner. All rights reserved.

CS429: Computer Organization and Architecture

From Source to Execution:

Computer Science 322 Operating Systems Mount Holyoke College Spring Topic Notes: Processes and Threads

Chapter01.fm Page 1 Monday, August 23, :52 PM. Part I of Change. The Mechanics. of Change

COS 318: Operating Systems. Overview. Andy Bavier Computer Science Department Princeton University

CIS 190: C/C++ Programming. Lecture 3 Memory Management in C

2SKILL. Variables Lesson 6. Remembering numbers (and other stuff)...

Computer Basics 1/6/16. Computer Organization. Computer systems consist of hardware and software.

assembler Machine Code Object Files linker Executable File

Obtained the source code to gcc, one can just follow the instructions given in the INSTALL file for GCC.

(page 0 of the address space) (page 1) (page 2) (page 3) Figure 12.1: A Simple 64-byte Address Space

CS 31: Intro to Systems Pointers and Memory. Kevin Webb Swarthmore College October 2, 2018

Where Does Global Static Local Register Variables Memory And C Program Instructions Get Stored

Memory Management. q Basic memory management q Swapping q Kernel memory allocation q Next Time: Virtual memory

Chapter 2: Operating-System Structures. Operating System Concepts Essentials 8 th Edition

QUIZ. Source:

CSCI-1200 Data Structures Spring 2016 Lecture 6 Pointers & Dynamic Memory

CS 220: Introduction to Parallel Computing. Beginning C. Lecture 2

Chapter 2: Operating-System

Intro to x86 Binaries. From ASM to exploit

Ch. 3: The C in C++ - Continued -

Deadlock Detection. Several Instances of a Resource Type. Single Instance of Each Resource Type

Getting started with UNIX/Linux for G51PRG and G51CSA

CS 61C: Great Ideas in Computer Architecture Lecture 2: Introduction to C, Part I

Systems Programming. Fatih Kesgin &Yusuf Yaslan Istanbul Technical University Computer Engineering Department 18/10/2005

CS140 Operating Systems and Systems Programming

Transcription:

ICS332 Operating Systems Spring 2018

Smaller Address Spaces Having small address spaces is always a good idea This is good for swapping: don t swap as often (because if address spaces are small, then RAM looks bigger), not as slow to swap (because reading/writing a smaller address space from/to disk is faster)

Smaller Address Spaces Having small address spaces is always a good idea This is good for swapping: don t swap as often (because if address spaces are small, then RAM looks bigger), not as slow to swap (because reading/writing a smaller address space from/to disk is faster) Three common-place techniques: Dynamic Memory Allocation Ask programs to tell the OS exactly how much memory they need (malloc, new) so that we don t always allocate the maximum allowed RAM to each process You all know about this one Dynamic Loading Show of hands, who s heard of this? Dynamic Linking Show of hands, who s heard of this?

Dynamic Loading Dynamic loading: only load code/text when it s needed Dynamic loading is the programmer responsibility The OS is not involved, although it provides tools to make dynamic loading possible Supported in all (decent) programming languages / OSes: in C/C++: POSIX: dlopen, dlsym... Windows: LoadLibrary e.g. in Python (let s look at this one...) import statement anywhere in the program! e.g. in Java ClassLoader class and others Dynamic unloading is usually possible Note: We talk of dynamic loading but not of static loading (the default behavior)

Static / Dynamic Linking Static Linking is the historical way of reusing code Add the assembly code of useful functions (printf...) collected in an archive or library to your own executable. e.g., libc.a for Linux; MSVCRT.LIB for Windows) Example on Linux: gcc -static HelloWorld.c -o HelloWorld nm HelloWorld; objdump -d HelloWorld

Static / Dynamic Linking Static Linking is the historical way of reusing code Add the assembly code of useful functions (printf...) collected in an archive or library to your own executable. e.g., libc.a for Linux; MSVCRT.LIB for Windows) Example on Linux: gcc -static HelloWorld.c -o HelloWorld nm HelloWorld; objdump -d HelloWorld Issue 1: Large text On my Linux VM, the HelloWorld executable is 892 KiB! Issue 2: Some code is (very likely) duplicated in memory My program is (very likely) not the only one to use printf!

Static / Dynamic Linking Static Linking is the historical way of reusing code Add the assembly code of useful functions (printf...) collected in an archive or library to your own executable. e.g., libc.a for Linux; MSVCRT.LIB for Windows) Example on Linux: gcc -static HelloWorld.c -o HelloWorld nm HelloWorld; objdump -d HelloWorld Issue 1: Large text On my Linux VM, the HelloWorld executable is 892 KiB! Issue 2: Some code is (very likely) duplicated in memory My program is (very likely) not the only one to use printf! Key idea: Why not share text (i.e., code) between processes in a similar way as data can be shared through shared memory?

Dynamic Linking In spirit very similar to dynamic loading But the OS is in charge of loading the code While also means that the OS needs to be told where to load it (at compile time)

Dynamic Linking In spirit very similar to dynamic loading But the OS is in charge of loading the code While also means that the OS needs to be told where to load it (at compile time) The code is shared in shared libraries: e.g., libc.so for Linux (so = shared object) MSVCRT.DLL for Windows (DLL = Dynamic-link library)

Dynamic Linking In spirit very similar to dynamic loading But the OS is in charge of loading the code While also means that the OS needs to be told where to load it (at compile time) The code is shared in shared libraries: e.g., libc.so for Linux (so = shared object) MSVCRT.DLL for Windows (DLL = Dynamic-link library) Linux example: gcc -shared -fpic HelloWorld.c -o HelloWorld nm HelloWorld; objdump -d HelloWorld On my Linux VM, the HelloWorld executable is 8.5KiB (compared to the 892KiB statically linked one)!

Shared Libraries How does it work? When dynamic linking is enabled, the linker just puts a stub in the binary for each shared library routine reference

Shared Libraries How does it work? When dynamic linking is enabled, the linker just puts a stub in the binary for each shared library routine reference That stub is a piece of code that: checks whether the routine is loaded in memory if not, then loads it into memory shared (with all processes) then replaces itself with a simple call to the routine future calls will be for free

Shared Libraries How does it work? When dynamic linking is enabled, the linker just puts a stub in the binary for each shared library routine reference That stub is a piece of code that: checks whether the routine is loaded in memory if not, then loads it into memory shared (with all processes) then replaces itself with a simple call to the routine future calls will be for free Chances are that when you run HelloWorld, the printf code is already in memory:

Shared Libraries How does it work? When dynamic linking is enabled, the linker just puts a stub in the binary for each shared library routine reference That stub is a piece of code that: checks whether the routine is loaded in memory if not, then loads it into memory shared (with all processes) then replaces itself with a simple call to the routine future calls will be for free Chances are that when you run HelloWorld, the printf code is already in memory: Save space!

Shared Libraries How does it work? When dynamic linking is enabled, the linker just puts a stub in the binary for each shared library routine reference That stub is a piece of code that: checks whether the routine is loaded in memory if not, then loads it into memory shared (with all processes) then replaces itself with a simple call to the routine future calls will be for free Chances are that when you run HelloWorld, the printf code is already in memory: Save space! Save time!

Shared Libraries Easy Updates Haven t you wondered how come you can update your system (i.e., libraries) and not have to recompile all your executables??? This would be insanely inconvenient!

Shared Libraries Easy Updates Haven t you wondered how come you can update your system (i.e., libraries) and not have to recompile all your executables??? This would be insanely inconvenient! Provided the APIs haven t not changed you can just: Replace a shared library (.so,.dll) by a new one Ask the system to reload it And now it all magically works! If the update was critical (i.e., security) then a reboot may be required

Shared Libraries Easy Updates Haven t you wondered how come you can update your system (i.e., libraries) and not have to recompile all your executables??? This would be insanely inconvenient! Provided the APIs haven t not changed you can just: Replace a shared library (.so,.dll) by a new one Ask the system to reload it And now it all magically works! If the update was critical (i.e., security) then a reboot may be required Dynamic Linking requires help from the OS To break memory isolation and allow shared text segments among processes We will see that this comes for free with virtual memory (next Module)

Details of Shared Libraries On Linux system the ldd command will print the shared libraries required by a program For instance, let us look at the shared libraries used by /bin/date The compiler adds stuff in the executable so that ldd can find this information and display it It turns out that, in Linux, you can override functions from loaded shared libraries by creating yourself a small shared library Let s try this to do something useful...

Example: C and Memory Leaks As you know, in C you allocate/free memory with malloc() and free() Every call to malloc() should have a matching call to free() This is easier said than done, as you might know But perhaps you didn t care about memory leaks when writing C

Example: C and Memory Leaks As you know, in C you allocate/free memory with malloc() and free() Every call to malloc() should have a matching call to free() This is easier said than done, as you might know But perhaps you didn t care about memory leaks when writing C Wouldn t it be great if somehow the code counted calls to malloc() and free()?

Example: C and Memory Leaks As you know, in C you allocate/free memory with malloc() and free() Every call to malloc() should have a matching call to free() This is easier said than done, as you might know But perhaps you didn t care about memory leaks when writing C Wouldn t it be great if somehow the code counted calls to malloc() and free()? Let s do that with a small shared library...

First Step: Overriding exit() I ve written the code for a shared library that overrides the exit() library function To create a shared library.so file: gcc -fpic -DPIC -c custom shared library.c -o custom shared library.o ld -shared -ldl -o custom shared library.so custom shared library.o Then, one can enable the shared library by setting th LD PRELOAD environment variable to the path of the shared library I ve put together the code above and a Makefile that does everything, including compiling a small program that just does an exit (See the Example source code reading in this module) Let s look at all this and run it...

Overriding malloc() and exit()! Let s now augment our custom shared library to override malloc() and free() The code is on the web site (in the Example source code reading), but let s try to do it lilive... Objective: keep counts of calls to malloc() and calls to free(), and print a warning if they don t match! Let s then try to run a leaky C program, a Java program, make, etc. Note we don t have to re-compile any of those programs!

This is very useful! We could augment what we just did to make it way more useful and user friendly For instance, we could find out where calls to malloc() are placed (i.e., which lines of code) and then report on which ones were not freed! Turns out, tools exist that do this already: valgrind, purify...

This is very useful! We could augment what we just did to make it way more useful and user friendly For instance, we could find out where calls to malloc() are placed (i.e., which lines of code) and then report on which ones were not freed! Turns out, tools exist that do this already: valgrind, purify... Let s run valgrind on our leaky C program... More generally: You re not happy with the performance of one function in a standard lib? Rewrite it and replace it on-the-fly!

This is very useful! We could augment what we just did to make it way more useful and user friendly For instance, we could find out where calls to malloc() are placed (i.e., which lines of code) and then report on which ones were not freed! Turns out, tools exist that do this already: valgrind, purify... Let s run valgrind on our leaky C program... More generally: You re not happy with the performance of one function in a standard lib? Rewrite it and replace it on-the-fly! And since this is very useful / powerful, it can also be dangerous If you have a bug in your shared library, then you re stuck and nothing will work (e.g., you broke malloc()!! ) Which is why we test with the LD PRELOAD environment variable instead of making the new shared library the default system-wide

Conclusions In the previous set of lecture notes we saw that we may have to swap because we run out of RAM Making address spaces as small as possible is thus a good idea Won t have to swap as much Not as costly to swap when swapping is needed Bottom Line: do not waste bytes! Part of this is on the developer: Just use space-efficient data structures Use Dynamic Memory Allocation Part of this is provided by languages/compilers/os: Dynamic loading Dynamic linking We ll have a QUIZ on this entire module next week