Feudal C Automatic memory management with zero runtime overhead CS263 - Spring 1999 Scott McPeak Dan Bonachea Carol Hurwitz C.

Slides:



Advertisements
Similar presentations
Intermediate Code Generation
Advertisements

Chapter 6 Data Types
Programming Languages and Paradigms
ITEC 320 Lecture 11 Pointers(1). Pointers Review Packages –Generic –Child Homework 3 is posted.
CPS3340 COMPUTER ARCHITECTURE Fall Semester, /17/2013 Lecture 12: Procedures Instructor: Ashraf Yaseen DEPARTMENT OF MATH & COMPUTER SCIENCE CENTRAL.
Lecture 10: Heap Management CS 540 GMU Spring 2009.
Prof. Necula CS 164 Lecture 141 Run-time Environments Lecture 8.
Pointer applications. Arrays and pointers Name of an array is a pointer constant to the first element whose value cannot be changed Address and name refer.
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 18.
Engineering Problem Solving With C++ An Object Based Approach Chapter 9 Pointers and Creating Data Structures.
ECE 353: Lab C Pointers and Structs. Basics A pointer holds an address to some variable Notation: – Dereferencing operator: * int *x is a declaration.
Introduction The Approach ’ s Overview A Language of Pointers The Type System Operational Semantics Type Safety Type Inference The Rest of C Experiments.
CSE 2501 Review Declaring a variable allocates space for the type of datum it is to store int x; // allocates space for an int int *px; // allocates space.
CS 536 Spring Run-time organization Lecture 19.
Run-Time Storage Organization
The environment of the computation Declarations introduce names that denote entities. At execution-time, entities are bound to values or to locations:
Memory and C++ Pointers.  C++ objects and memory  C++ primitive types and memory  Note: “primitive types” = int, long, float, double, char, … January.
Run-time Environment and Program Organization
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
Pointers Applications
1 Contents. 2 Run-Time Storage Organization 3 Static Allocation In many early languages, notably assembly and FORTRAN, all storage allocation is static.
1 Procedural Concept The main program coordinates calls to procedures and hands over appropriate data as parameters.
1/25 Pointer Logic Changki PSWLAB Pointer Logic Daniel Kroening and Ofer Strichman Decision Procedure.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes.
Chapter TwelveModern Programming Languages1 Memory Locations For Variables.
Pointer Data Type and Pointer Variables
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 10. Pointers & Dynamic Data Structures.
CS3012: Formal Languages and Compilers The Runtime Environment After the analysis phases are complete, the compiler must generate executable code. The.
EE4E. C++ Programming Lecture 1 From C to C++. Contents Introduction Introduction Variables Variables Pointers and references Pointers and references.
Runtime Environments Compiler Construction Chapter 7.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Tevfik Bultan Lecture 12: Pointers continued, C strings.
Chapter 0.2 – Pointers and Memory. Type Specifiers  const  may be initialised but not used in any subsequent assignment  common and useful  volatile.
Computer Science Department Data Structure & Algorithms Lecture 8 Recursion.
Computer Science and Software Engineering University of Wisconsin - Platteville 2. Pointer Yan Shi CS/SE2630 Lecture Notes.
Pointers. The structure of memory Computer memory is a linear sequence of addressable locations Addresses are numbered starting at zero In personal computers,
CS212: Object Oriented Analysis and Design Lecture 7: Arrays, Pointers and Dynamic Memory Allocation.
Basic Semantics Associating meaning with language entities.
1 Records Record aggregate of data elements –Possibly heterogeneous –Elements/slots are identified by names –Elements in same fixed order in all records.
Programming Languages and Paradigms Imperative Programming.
SPL – Practical Session 2 Topics: – C++ Memory Management – Pointers.
C++ Data Types Structured array struct union class Address pointer reference Simple IntegralFloating char short int long enum float double long double.
Chapter 9 Pointers and Dynamic Arrays (9.1). Pointers A variables which holds the memory address for a variable of a specific type. Call-by-Reference.
CPSC 252 Dynamic Memory Allocation Page 1 Dynamic memory allocation Our first IntVector class has some serious limitations the capacity is fixed at MAX_SIZE.
Pointers in C++. 7a-2 Pointers "pointer" is a basic type like int or double value of a pointer variable contains the location, or address in memory, of.
1 CS 132 Spring 2008 Chapter 3 Pointers and Array-Based Lists read p
Data Structures Using C++ 2E Chapter 3 Pointers. Data Structures Using C++ 2E2 Objectives Learn about the pointer data type and pointer variables Explore.
Copyright 2005, The Ohio State University 1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation.
Pointers in C Computer Organization I 1 August 2009 © McQuain, Feng & Ribbens Memory and Addresses Memory is just a sequence of byte-sized.
CS 261 – Data Structures C Pointers Review. C is Pass By Value Pass-by-value: a copy of the argument is passed in to a parameter void foo (int a) { a.
Review 1 List Data Structure List operations List Implementation Array Linked List.
CSC 8505 Compiler Construction Runtime Environments.
1 Chapter 15-1 Pointers, Dynamic Data, and Reference Types Dale/Weems.
Data Structures in C++ Pointers & Dynamic Arrays Shinta P.
CS5205Semantics1 CS5205: Foundation in Programming Languages Semantics Static Semantics Dynamic Semantics Operational Semantics Big-step Small-Step Denotational.
1 CSC103: Introduction to Computer and Programming Lecture No 17.
Chapter 12: Pointers, Classes, Virtual Functions, Abstract Classes, and Lists.
Recap Resizing the Vector Push_back function Parameters passing Mechanism Primitive Arrays of Constants Multidimensional Arrays The Standard Library string.
Functional Programming
Data Types In Text: Chapter 6.
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
ENERGY 211 / CME 211 Lecture 25 November 17, 2008.
Run-time organization
Dynamic Memory CSCE 121 J. Michael Moore.
Dynamic Memory Allocation
Closure Representations in Higher-Order Programming Languages
Dynamic Memory.
Runtime Environments What is in the memory?.
Dynamic Memory CSCE 121.
SPL – PS2 C++ Memory Handling.
Presentation transcript:

Feudal C Automatic memory management with zero runtime overhead CS263 - Spring 1999 Scott McPeak Dan Bonachea Carol Hurwitz C

Outline Introduction - Ownership and the Owner Tree Goals of Feudal C Syntax, Type Rules, and Operational Semantics Examples Memory Safety Theorem and Proof Outline

The Basic Idea Designate some pointers as "owners" –Obligation to delete the pointed-at object: no memory leaks The rest of the pointers are "serfs" –No deletion obligation (or capability) –Must stop pointing at an object before it is deleted: no dangling pointers Use the type system –Enforce the distinction (type soundness) –Enforce properties of both kinds

Ownership and the Owner Tree Globals main() foo() bar() bigArray ABC localObj dl1dl2dl3dl4 owner serf

Flavors Owners: Serfs: This project: –Prove memory safety of restricted versions No leaks No dangling references –Simplify language exclusiverestricted normal efficientflexible restricted static dynamicfull RC efficientflexible

Goals of Feudal C Provide memory safety (no leaks, no dangling references) through static typing Promote clear design practices Enable passing memory out of a function Still have a language that’s useful

Language Syntax Types TypeAux ::= int | t owner * t  TypeAux Type::= t | t serf * t  TypeAux Typical Imperative Commands & Expressions –if-then-else– Assignment (copy & transfer) –while – Arithmetic & Boolean Expressions (no side effects) –new / delete– Function call & return Rules of Thumb (enforced by typing) –No serf pointers stored in the heap or returned from functions –No owner pointers as parameters to functions –Dereferencing serf pointers gets only serf pointers Limitations: –No Arrays – No “Legacy” Pointers –No Records – No Function types or pointers –No address-of operator– No Recursive Types (*)

Small Example int owner* owner* allocSmallList(int val) { int owner* x; // variable declarations int owner* owner* y; x := new int; // allocate an integer on the heap *x = val; // store val there y := new int owner*; // create a pointer on the heap *y := x; // this nullifies x return y; // safely returns allocated space! }

Partial Typing Rules  |- v 1 :  owner *  |- v 2 :  owner *  |- F v 1 := v 2 Transfer Assign Command  |- v :  owner * serf *  |- *v :  serf * Serf Pointer Dereference  |- e : int  |- F c 1  |- F c 2  |- F if e then c 1 else c 2 Conditional Command  |- x :  serf *  |- e :  owner *  |- F x = e Copy Assign Command  |- x :  owner *  |- e :  serf *  |- F x = e Note: This rule does not exist!  f ( x 1 :  1,..., x n :  n ) x n+1 :  n+1,..., x m :  m c ; rc  F  = x 1 :  1,..., x m :  m  |- F c  |- rc :     ´ serf *  1  i  n.  i   i ´ owner * |- F f Function Definition

Partial Operational Semantics  n (x) = 0 ( hr newaddr )  dom( H )   1,...,  n | H | x := new ; c     1,...,  n [ ( ohr newaddr ) / x ] | H  { ( ( hr newaddr ), 0 ) } | c  Allocation Command f ( x 1,..., x m ) x m+1,..., x p c body  F op  1  i  m. eval( e i,  n, H )  v i  m+1  i  p. v i = 0   1,...,  n | H | x = f ( e 1,..., e m ) ; c     1,...,  n, Frame ( { (x 1, v 1 )..., (x p, v p ) }, c, x ) | H | c body  Function Call Command  y  dom(  n ). y  x   n (y)  (ohr z) val =  n (x)   1,...,  n-1,  n | H | return x     1,...,  n-1 [ val /  n.var ] | H |  n.cmd  Function Return Command Values =   (ohr  )  (shr  )0 or 4 or (ohr 37) or (shr 5) VarValueMap = P(Loc  Values)VVM = { (x, 5), (y, (shr 17)), (z, (ohr 17))} Frame = VarValueMap  Command  Loc  n = Frame(VVM, c, x) Heap = P ((hr  )  Values)H = { ((hr 16), 2), ((hr 30), (ohr 17)) } States = Frame list  Heap  CommandS =   1,...,  n | H | c  Framework

Memory Safety Roadmap

Single Owner, No Cycles Lemma Lemma 1: For every allocated element, there is exactly one owner pointing to it from the heap or from the stack, and there exist no cycles.  ( hr n)  dom(H). |owners((hr n))| = 1 and (hr n)  TC ((hr n)) Initial State: Since the heap is empty the lemma is vacuously true for the base case

Single Owner, No Cycles Lemma y  dom(  n )  n (y) = (ohr objaddr) eval( v,  n, H )  (ohr targetaddr) H((hr targetaddr)) = 0 (hr targetaddr)  TC( H, (hr objaddr) )   1,...,  n | H | *v := y ; c     1,...,  n [ 0 / y ] | H [ (ohr objaddr) / (hr targetaddr) ] | c  Transfer Assign Command

No Memory Leaks Defn: A memory leak is allocated memory which is not reachable from the stack, i.e., is not in the transitive closure of anything on the stack. –Theorem: Feudal C is safe with respect to memory leaks. Initial state: –The heap is empty when the program begins, so they is no allocated memory, and hence no memory leaks.

No Dangling References  n (x) = (ohr addr)  y  dom(  n ). y  x   n (y) = (shr z)  (hr z)  TC(H, (hr addr))   1,...,  n | H | delete ( x ) ; c     1,...,  n [ 0 / x ] | del ( H, (ohr addr) ) | c  DeAllocation Command

Conclusion Feudal C is memory safe The type system is a good place to put memory allocation information!