Summer School on Language-Based Techniques for Integrating with the External World Types for Safe C-Level Programming Part 2: Quantified-Types in C Dan.

Slides:



Advertisements
Similar presentations
Sml2java a source to source translator Justin Koser, Haakon Larsen, Jeffrey Vaughan PLI 2003 DP-COOL.
Advertisements

Automated Theorem Proving Lecture 1. Program verification is undecidable! Given program P and specification S, does P satisfy S?
Type Checking, Inference, & Elaboration CS153: Compilers Greg Morrisett.
CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists Dan Grossman Winter 2013.
Type Analysis and Typed Compilation Stephanie Weirich Cornell University.
CSE341: Programming Languages Lecture 16 Datatype-Style Programming With Lists or Structs Dan Grossman Winter 2013.
Composition CMSC 202. Code Reuse Effective software development relies on reusing existing code. Code reuse must be more than just copying code and changing.
INF 212 ANALYSIS OF PROG. LANGS Type Systems Instructors: Crista Lopes Copyright © Instructors.
Catriel Beeri Pls/Winter 2004/5 last 55 Two comments on let polymorphism I. What is the (time, space) complexity of type reconstruction? In practice –
Typed Assembly Languages COS 441, Fall 2004 Frances Spalding Based on slides from Dave Walker and Greg Morrisett.
CS 330 Programming Languages 10 / 16 / 2008 Instructor: Michael Eckmann.
Summer School on Language-Based Techniques for Integrating with the External World Types for Safe C-Level Programming Part 3: Basic Cyclone-Style Region-
Summer School on Language-Based Techniques for Integrating with the External World Types for Safe C-Level Programming Part 1: Quantified-Types Background.
Survey of Typed Assembly Language (TAL) Introduction and Motivation –Conventional untyped compiler < Typed intermediate languages –Typed intermediate language.
Existential Types for Imperative Languages Dan Grossman Cornell University Eleventh European Symposium on Programming April 2002.
Region-Based Memory Management in Cyclone Dan Grossman Cornell University June 2002 Joint work with: Greg Morrisett, Trevor Jim (AT&T), Michael Hicks,
Tim Sheard Oregon Graduate Institute Lecture 11: A Reduction Semantics for MetaML CS510 Section FSC Winter 2005 Winter 2005.
Playing With Fire: Mutation and Quantified Types CIS670, University of Pennsylvania 2 October 2002 Dan Grossman Cornell University.
CSE341: Programming Languages Lecture 11 Type Inference Dan Grossman Winter 2013.
11 Values and References Chapter Objectives You will be able to: Describe and compare value types and reference types. Write programs that use variables.
CSC3315 (Spring 2009)1 CSC 3315 Programming Languages Hamid Harroud School of Science and Engineering, Akhawayn University
Functional Programming Universitatea Politehnica Bucuresti Adina Magda Florea
CSE 425: Data Types II Survey of Common Types I Records –E.g., structs in C++ –If elements are named, a record is projected into its fields (e.g., via.
ML Datatypes.1 Standard ML Data types. ML Datatypes.2 Concrete Datatypes  The datatype declaration creates new types  These are concrete data types,
Programming Languages and Design Lecture 7 Subroutines and Control Abstraction Instructor: Li Ma Department of Computer Science Texas Southern University,
CSC 580 – Theory of Programming Languages, Spring, 2009 Week 9: Functional Languages ML and Haskell, Dr. Dale E. Parson.
Formal Semantics Chapter Twenty-ThreeModern Programming Languages, 2nd ed.1.
C++ Memory Overview 4 major memory segments Key differences from Java
COP4020 Programming Languages Names, Scopes, and Bindings Prof. Xin Yuan.
Objects & Dynamic Dispatch CSE 413 Autumn Plan We’ve learned a great deal about functional and object-oriented programming Now,  Look at semantics.
Recap form last time How to do for loops map, filter, reduce Next up: dictionaries.
CSE 332: Design Patterns Review: Design Pattern Structure A design pattern has a name –So when someone says “Adapter” you know what they mean –So you can.
CSE 425: Control Abstraction I Functions vs. Procedures It is useful to differentiate functions vs. procedures –Procedures have side effects but usually.
Topic 3: C Basics CSE 30: Computer Organization and Systems Programming Winter 2011 Prof. Ryan Kastner Dept. of Computer Science and Engineering University.
CSE 413 Languages & Implementation Hal Perkins Autumn 2012 Structs, Implementing Languages (credits: Dan Grossman, CSE 341) 1.
CMSC 202 Advanced Section Classes and Objects: Object Creation and Constructors.
Semantic Analysis II Type Checking EECS 483 – Lecture 12 University of Michigan Wednesday, October 18, 2006.
Advanced Functional Programming Tim Sheard 1 Lecture 17 Advanced Functional Programming Tim Sheard Oregon Graduate Institute of Science & Technology Lecture:
1 Scheme (Section 11.2) CSCI 431 Programming Languages Fall 2003.
CSE 332: C++ pointers, arrays, and references Overview of Pointers and References Often need to refer to another object –Without making a copy of the object.
CMSC 330: Organization of Programming Languages Operational Semantics.
CSE341: Programming Languages Lecture 17 Implementing Languages Including Closures Dan Grossman Winter 2013.
Language-Based Security: Overview of Types Deepak Garg Foundations of Security and Privacy October 27, 2009.
CSE341: Programming Languages Lecture 11 Type Inference
CSE341: Programming Languages Lecture 9 Function-Closure Idioms
CSE341: Programming Languages Lecture 16 Datatype-Style Programming With Lists or Structs Dan Grossman Spring 2013.
CSE341: Programming Languages Lecture 16 Datatype-Style Programming With Lists or Structs Dan Grossman Spring 2017.
Dan Grossman University of Washington 25 July 2007
CSE341: Programming Languages Lecture 9 Function-Closure Idioms
CSE341: Programming Languages Lecture 16 Datatype-Style Programming With Lists or Structs Dan Grossman Autumn 2018.
CSE341: Programming Languages Lecture 9 Function-Closure Idioms
CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists
CSE341: Programming Languages Lecture 9 Function-Closure Idioms
CSE341: Programming Languages Lecture 16 Datatype-Style Programming With Lists or Structs Dan Grossman Autumn 2017.
CSE341: Programming Languages Lecture 11 Type Inference
CSE341: Programming Languages Lecture 9 Function-Closure Idioms
CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists
Dan Grossman University of Washington 18 July 2007
CSE341: Programming Languages Lecture 16 Datatype-Style Programming With Lists or Structs Dan Grossman Spring 2016.
CSE341: Programming Languages Lecture 11 Type Inference
CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists
CSE341: Programming Languages Lecture 11 Type Inference
CSE341: Programming Languages Lecture 9 Function-Closure Idioms
CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists
Classes and Objects Object Creation
CSE341: Programming Languages Lecture 9 Function-Closure Idioms
CSE341: Programming Languages Lecture 11 Type Inference
Brett Wortzman Summer 2019 Slides originally created by Dan Grossman
CSE341: Programming Languages Lecture 16 Datatype-Style Programming With Lists or Structs Dan Grossman Spring 2019.
CMSC 202 Constructors Version 9/10.
Presentation transcript:

Summer School on Language-Based Techniques for Integrating with the External World Types for Safe C-Level Programming Part 2: Quantified-Types in C Dan Grossman University of Washington 25 July 2007

Dan Grossman, 2007 Summer School2 C-level Most PL theory is done for safe, high-level languages A lot of software is written in C Me: Adapt and extend our theory to make a safe C –Last week: review theory for high-level languages –Today (+?): Theory of type variables for a safe C –Tomorrow: Safe region-based memory management Uses type variables (and more)! –Off-line: Engineering a safe systems language

25 July 2007Dan Grossman, 2007 Summer School3 How is C different? C has “left expressions” and “address-of” operator { int* y[7]; int x = 17; y[0] = &x; } C has explicit pointers, “unboxed” structures struct T vs. struct T * C function pointers are not objects or closures void apply_to_list(void (*f)(void*,int), void*, IntList); C has manual memory management

25 July 2007Dan Grossman, 2007 Summer School4 Context: Why Cyclone? A type-safe language at the C-level of abstraction Type-safe: Memory safety, abstract types, … C-level: explicit pointers, data representation, memory management. Semi-portable. Niche: Robust/extensible systems code –Looks like, acts like, and interfaces easily with C –Used in several research projects –Doesn’t “fix” non-safety issues (syntax, switch, …) Modern: patterns, tuples, exceptions, …

25 July 2007Dan Grossman, 2007 Summer School5 Context: Why quantified types? The usual reasons: –Code reuse, container types –Abstraction –Fancy stuff: phantom types, iterators, … Because low-level –Implement closures with existentials –Pass environment fields to functions For other kinds of invariants –Memory regions, array-lengths, locks –Same theory and more important in practice

25 July 2007Dan Grossman, 2007 Summer School6 Context: Why novel? Left vs. right expressions and the & operator Aggregate assignment (record copy) First-class existential types in an imperative language Types of unknown size And any new combination of effects, aliasing, and polymorphism invites trouble…

25 July 2007Dan Grossman, 2007 Summer School7 Getting burned… decent company To: From: Harper and Lillibridge Sent: 08 Jul 91 Subject: Subject: ML with callcc is unsound The Standard ML of New Jersey implementation of callcc is not type safe, as the following counterexample illustrates:… Making callcc weakly polymorphic … rules out the counterexample

25 July 2007Dan Grossman, 2007 Summer School8 Getting burned… decent company From: Alan Jeffrey Sent: 17 Dec 2001 To: Types List Subject: Generic Java type inference is unsound The core of the type checking system was shown to be safe… but the type inference system for generic method calls was not subjected to formal proof. In fact, it is unsound … This problem has been verified by the JSR14 committee, who are working on a revised langauge specification…

25 July 2007Dan Grossman, 2007 Summer School9 Getting burned… decent company From: Xavier Leroy Sent: 30 Jul 2002 To: John Prevost Cc: Caml-list Subject: Re: [Caml-list] Serious typechecking error involving new polymorphism (crash) … Yes, this is a serious bug with polymorphic methods and fields. Expect a 3.06 release as soon as it is fixed. …

25 July 2007Dan Grossman, 2007 Summer School10 Getting burned…I’m in the club From: Dan Grossman Sent: Thursday 02 Aug 2001 To: Gregory Morrisett Subject: Unsoundness Discovered! In the spirit of recent worms and viruses, please compile the code below and run it. Yet another interesting combination of polymorphism, mutation, and aliasing. The best fix I can think of for now is …

25 July 2007Dan Grossman, 2007 Summer School11 The plan from here Brief tour of Cyclone polymorphism C-level polymorphic references –Formal model with “left” and “right” –Comparison with actual languages C-level existential types –Description of “new” soundness issue –Some non-problems C-level type sizes –Not a soundness issue

25 July 2007Dan Grossman, 2007 Summer School12 “Change void* to alpha” struct L { void* hd; struct L* tl; }; typedef struct L* l_t; l_t map(void* f(void*), l_t); l_t append(l_t, l_t); struct L { `a hd; struct L * tl; }; typedef struct L * l_t ; l_t map (`b f(`a), l_t ); l_t append (l_t, l_t );

25 July 2007Dan Grossman, 2007 Summer School13 Not much new here struct Lst is a recursive type constructor: L = λα. { α hd; (L α) * tl; } The functions are polymorphic: map :  α, β. (α  β, L α)  (L β) Closer to C than ML –less type inference allows first-class polymorphism and polymorphic recursion –data representation restricts `a to pointers, int (why not structs? why not float ? why int ?) Not C++ templates

25 July 2007Dan Grossman, 2007 Summer School14 Existential types Programs need a way for “call-back” types: struct T { int (*f)(int,void*); void* env; }; We use an existential type (simplified): struct T { int (*f)(int,`a); `a env; }; more C-level than baked-in closures/objects

25 July 2007Dan Grossman, 2007 Summer School15 Existential types cont’d struct T { int (*f)(int,`a); `a env; }; creation requires a “consistent witness” type is just struct T use requires an explicit “unpack” or “open”: int apply(struct T pkg, int arg) { let T{.f=fp,.env=ev} = pkg; return fp(arg,ev); }

25 July 2007Dan Grossman, 2007 Summer School16 Sizes Types have known or unknown size (a kind distinction) As in C, unknown-size types can’t be used for fields, variables, etc.: must use pointers to them Unlike C, we allow last-field-unknown-size: struct T1 { struct T1* tl; char data[1]; }; struct T2 { int len; int arr[1]; }; 3 5

25 July 2007Dan Grossman, 2007 Summer School17 Sizes Types have known or unknown size (a kind distinction) As in C, unknown-size types can’t be used for fields, variables, etc.: must use pointers to them Unlike C, we allow last-field-unknown-size: struct T1 { struct T1 * tl; `a data; }; struct T2 { tag_t len; int arr[valueof(`i)]; }; struct T1 { struct T1* tl; char data[1]; }; struct T2 { int len; int arr[1]; };

25 July 2007Dan Grossman, 2007 Summer School18 The plan from here Brief tour of Cyclone polymorphism C-level polymorphic references –Formal model with “left” and “right” –Comparison with actual languages C-level existential types –Description of “new” soundness issue –Some non-problems C-level type sizes –Not a soundness issue

25 July 2007Dan Grossman, 2007 Summer School19 Mutation e1=e2 means: –Left-evaluate e1 to a location –Right-evaluate e2 to a value –Change the location to hold the value Locations are “left values”: x.f1.f2…fn Values are “right values”, include &x.f1.f2…fn (a pointer to a location) Having interdependent left/right evaluation is no problem

25 July 2007Dan Grossman, 2007 Summer School20 Left vs. Right Syntax Expressions: e ::= x | λx:τ. e | e(e) | c | e=e | &e | *e | (e,e) | e.1 | e.2 Right-Values: v ::= c | λx:τ. e | &l | (v,v) Left-Values: l ::= x | l.1 | l.2 Heaps: H ::=. | H,x → v Types τ ::= int | τ → τ | (τ, τ) | τ*

25 July 2007Dan Grossman, 2007 Summer School21 Of note Everything is mutable, so no harm in combining variables and locations –Heap-allocate everything (so fun-call makes a “ref”) Pairs are “flat”; all pointers are explicit A right value can point to a left value A left value is (part of) a location In C, functions are top-level and closed, but it doesn’t matter.

25 July 2007Dan Grossman, 2007 Summer School22 Small-step semantics – the set-up Two mutually recursive forms of evaluation context R ::= [] r | L=e | l=R | &L | *R | (R,e) | (v,R) | R.1 | R.2 | R(e) | v(R) L ::= [] l | L.1 | L.2 | *R H, e → r H’, e’ H, e → l H’, e’––––––––––––––––––––––– H, R[e] r → H’, R[e’] r H, R[e] l → H’, R[e’] l Rest-of-program is a right-expression Next “thing to do” is either a left-primitive-step or a right-primitive-step

25 July 2007Dan Grossman, 2007 Summer School23 Small-step primitive reductions H, *(&l) → r H, l not a right-value H, x → r H, H(x) H, (v1,v2).1 → r H, v1 H, (v1,v2).2 → r H, v2 H, l=v → r need helper since l may be some x.i.j.k (replace flat subtree) H, (λx:τ.e)(v) → r H, x → v, e H, *(&l) → l H, l a left-value

25 July 2007Dan Grossman, 2007 Summer School24 Typing (Left- on next slide) Type-check left- and right-expressions differently with two mutually recursive judgments Γ ├ r e1:τ Γ ├ l e1:τ Today, not tomorrow: left-rules are just a subset ––––––––– Γ ├ r c:int ––––––––– Γ ├ r x: Γ( x) Γ, x : τ1 ├ r e:τ2 –––––––––––––––––––– Γ ├ r λx:τ1.e: τ1 → τ2 Γ├ r e1:τ1 → τ2 Γ├ r e2:τ1 ––––––––––––––– Γ ├ r e1(e2): τ2 Γ├ r e1:τ1 Γ├ r e2:τ2 ––––––––––––––––––– Γ ├ r (e1,e2):(τ1,τ2) Γ├ r e:(τ1,τ2) ––––––––––––– Γ ├ r e.1:τ1 Γ├ r e:(τ1,τ2) ––––––––––––– Γ ├ r e.2:τ2 Γ├ l e1:τ Γ├ r e2:τ ––––––––––– Γ ├ r e1=e2:τ Γ├ r e:τ* ––––––––– Γ ├ r *e:τ Γ ├ l e:τ –––––––––– Γ ├ r &e:τ*

25 July 2007Dan Grossman, 2007 Summer School25 Typing Left-Expressions Just like in C, most expressions are not left-expressions But dereference of a pointer is Γ ├ l e:(τ1,τ2) ––––––––––––– Γ ├ l e.1:τ1 Γ ├ l e:(τ1,τ2) ––––––––––––– Γ ├ l e.2:τ2 ––––––––– Γ ├ l x: Γ( x) Γ├ r e:τ* ––––––––– Γ ├ l *e:τ Now we can prove Preservation and Progress After extending type-checking to program states By mutual induction on left and right expressions No surprises –Left-expressions evaluate to locations –Right-expressions evaluate to values

25 July 2007Dan Grossman, 2007 Summer School26 Universal quantification Adding universal types is completely standard: e ::= … | Λα. e | e [τ] v ::= … | Λα. e τ ::= … | α |  α. τ Γ ::= … | Γ, α L unchanged R ::= … | R [τ] (Λα. e) [τ] → r e{τ/α} Γ, α ├ r e:τ Γ ├ r e:  α.τ1 Γ ├ τ2 –––––––––––––––––– ––––––––––––––––––––––– Γ ├ r (Λα. e):  α.τ Γ ├ r e [τ2] : τ1{τ2/α}

25 July 2007Dan Grossman, 2007 Summer School27 Polymorphic-references? In C-like pseudocode, core of the poly-ref problem: (  α. α → α) id = Λα. λx:α. x; int i = 0; int* p = &i; id [int] = λx:int. x+17; p = (id [int*]) (p); /* set p to (&i)+17 ?!?!*/ Fortunately, this won’t type-check And in fact Preservation and Progress still hold So we never try to evaluate something like (&i) + 17

25 July 2007Dan Grossman, 2007 Summer School28 The punch-line Type applications are not left-expressions There is no derivation of Γ ├ l e[τ1]:τ2 Really! That’s all we need to do. Related idea: subsumption not allowed on left- expressions (cf. Java) Non-problems: Types like (  α. α list)* –Can only mutate to “other” (  α. α list) values Types like (  α. ((α list)*)) –No values have this type

25 July 2007Dan Grossman, 2007 Summer School29 What we learned Left vs. right formalizes fine e [ τ] is not a left-expression –Necessary and sufficient for soundness In practice, Cyclone (and other languages) even more restrictive: –If only (immutable) functions can be polymorphic, then there’s no way to create a location with a polymorphic type –A function pointer is (  α. …)*, not (  α.(… *))

25 July 2007Dan Grossman, 2007 Summer School30 The plan from here Brief tour of Cyclone polymorphism C-level polymorphic references –Formal model with “left” and “right” –Comparison with actual languages C-level existential types –Description of “new” soundness issue –Some non-problems C-level type sizes –Not a soundness issue

25 July 2007Dan Grossman, 2007 Summer School31 C Meets  Existential types in a safe low-level language –why (again) –features (mutation, aliasing) The problem The solutions Some non-problems Related work (why it’s new)

25 July 2007Dan Grossman, 2007 Summer School32 Low-level languages want  Major goal: expose data representation (no hidden fields, tags, environments,...) Languages need data-hiding constructs Don’t provide closures/objects struct T { int (*f)(int,`a); `a env; }; C “call-backs” use void* ; we use 

25 July 2007Dan Grossman, 2007 Summer School33 Normal  feature: Introduction int add (int a, int b) {return a+b; } int addp(int a, char* b) {return a+*b;} struct T x1 = T(add, 37); struct T x2 = T(addp,"a"); Compile-time: check for appropriate witness type Type is just struct T Run-time: create / initialize (no witness type) struct T { int (*f)(int,`a); `a env; };

25 July 2007Dan Grossman, 2007 Summer School34 Normal  feature: Elimination Destruction via pattern matching: void apply(struct T x) { let T{.f=fn,.env=ev} = x; // ev : `b, fn : int(*f)(int,`b) fn(42,ev); } Clients use the data without knowing the type struct T { int (*f)(int,`a); `a env; };

25 July 2007Dan Grossman, 2007 Summer School35 Low-level feature: Mutation Mutation, changing witness type struct T fn1 = f(); struct T fn2 = g(); fn1 = fn2; // record-copy Orthogonality and abstraction encourage this feature Useful for registering new call-backs without allocating new memory Now memory words are not type-invariant!

25 July 2007Dan Grossman, 2007 Summer School36 Low-level feature: Address-of field Let client update fields of an existential package –access only through pattern-matching –variable pattern copies fields A reference pattern binds to the field’s address: void apply2(struct T x) { let T{.f=fn,.env=*ev} = x; // ev : `b*, fn : int(*f)(int,`b) fn(42,*ev); } C uses &x.env ; we use a reference pattern

25 July 2007Dan Grossman, 2007 Summer School37 More on reference patterns Orthogonality: already allowed in Cyclone’s other patterns (e.g., tagged-union fields) Can be useful for existential types: struct Pr { `a fst; `a snd; }; void swap (`a* x, `a* y); void swapPr(struct Pr pr) { let Pr{.fst=*a,.snd=*b} = pr; swap(a,b); }

25 July 2007Dan Grossman, 2007 Summer School38 Summary of features struct definition can bind existential type variables construction, destruction traditional mutation via struct assignment reference patterns for aliasing A nice adaptation to a “safe C” setting?

25 July 2007Dan Grossman, 2007 Summer School39 Explaining the problem Violation of type safety Two solutions (restrictions) Some non-problems

25 July 2007Dan Grossman, 2007 Summer School40 Oops! struct T { void (*f)(int,`a); `a env;}; void ignore(int x, int y) {} void assign(int x, int* p) { *p = x; } void g(int* ptr) { struct T pkg1 = T(ignore, 0xBAD); //α=int struct T pkg2 = T(assign, ptr); //α=int* let T{.f=fn,.env=*ev} = pkg2; //alias pkg2 = pkg1; //mutation fn(37, *ev); //write 37 to 0xBAD }

25 July 2007Dan Grossman, 2007 Summer School41 With pictures… pkg1pkg2ignoreassign0xABCD let T{.f=fn,.env=*ev} = pkg2; //alias pkg1pkg2ignoreassign0xABCD assignfnev

25 July 2007Dan Grossman, 2007 Summer School42 With pictures… pkg2 = pkg1; //mutation pkg1pkg2ignoreassign0xABCD assignfnev pkg1pkg2ignore 0xABCD assignfnev

25 July 2007Dan Grossman, 2007 Summer School43 With pictures… pkg1pkg2ignore 0xABCD assignfnev fn(37, *ev); //write 37 to 0xABCD call assign with 0xABCD for p : void assign(int x, int* p) {*p = x;}

25 July 2007Dan Grossman, 2007 Summer School44 What happened? 1.Type `b establishes a compile-time equality relating types of fn ( void(*f)(int,`b) ) and ev ( `b* ) 2.Mutation makes this equality false 3.Safety of call needs the equality We must rule out this program… let T{.f=fn,.env=*ev} = pkg2; //alias pkg2 = pkg1; //mutation fn(37, *ev); //write 37 to 0xABCD

25 July 2007Dan Grossman, 2007 Summer School45 Two solutions Solution #1: Reference patterns do not match against fields of existential packages Note: Other reference patterns still allowed  cannot create the type equality Solution #2: Type of assignment cannot be an existential type (or have a field of existential type) Note: pointers to existentials are no problem  restores memory type-invariance

25 July 2007Dan Grossman, 2007 Summer School46 Independent and easy Either solution is easy to implement They are independent: A language can have two styles of existential types, one for each restriction Cyclone takes solution #1 (no reference patterns for existential fields), making it a safe language without type-invariance of memory!

25 July 2007Dan Grossman, 2007 Summer School47 Are the solutions sufficient (correct)? Small formal language proves type safety Highlights: –Left vs. right distinction –Both solutions –Memory invariant (necessarily) includes: “if a reference pattern is used for a location, then that location never changes type”

25 July 2007Dan Grossman, 2007 Summer School48 Nonproblem: Pointers to witnesses struct T2 { void (*f)(int, `a); `a* env; }; … let T2{.f=fn,.env=ev} = pkg2; pkg2 = pkg1; … pkg2assign fnev

25 July 2007Dan Grossman, 2007 Summer School49 Nonproblem: Pointers to packages struct T * p = &pkg1; p = &pkg2; pkg1pkg2ignoreassign0xABCD p Aliases are fine. Aliases of pkg1 at the “unpacked type” are not.

25 July 2007Dan Grossman, 2007 Summer School50 Problem appears new Existential types: –seminal use [Mitchell/Plotkin 1985] –closure/object encodings [Bruce et al, Minimade et al, …] –first-class types in Haskell [Läufer] None incorporate mutation Safe low-level languages with  –Typed Assembly Language [Morrisett et al] –Xanadu [Xi], uses  over ints None have reference patterns or similar Linear types, e.g. Vault [DeLine, Fähndrich] No aliases, destruction destroys the package

25 July 2007Dan Grossman, 2007 Summer School51 Duals? Two problems with α, mutation, and aliasing –One used , one used  –So are they the same problem? Conjecture: Similar, but not true duals Fact: Thinking dually hasn’t helped me here

25 July 2007Dan Grossman, 2007 Summer School52 The plan from here Brief tour of Cyclone polymorphism C-level polymorphic references –Formal model with “left” and “right” –Comparison with actual languages C-level existential types –Description of “new” soundness issue –Some non-problems C-level type sizes –Not a soundness issue

25 July 2007Dan Grossman, 2007 Summer School53 Size in C C has abstract types (not just void* ): struct T1; struct T2 { int len; int arr[*];//C99, much better than [1] }; And rules on their use that make sense at the C-level:* E.g., variables, fields, and assignment targets cannot have type struct T1. * Key corollary: C hackers don’t mind the restrictions

25 July 2007Dan Grossman, 2007 Summer School54 Size in Cyclone Kind distinction among: 1.B “pointer size” < 2.M “known size” < 3.A “unknown size” Killer app: Cyclone interface to C functions void memcopy (`a*,`a*, sizeof_t ); Should we be worried about soundness?

25 July 2007Dan Grossman, 2007 Summer School55 Why is size an issue in C? “Only” reason C restricts types of unknown size: Efficient and transparent implementation: –No run-time size passing –Statically known field and stack offsets This is important for translation, but has nothing to do with soundness Indeed, our formal model is “too high level” to motivate the kind distinction

25 July 2007Dan Grossman, 2007 Summer School56 The plan from here Brief tour of Cyclone polymorphism C-level polymorphic references –Formal model with “left” and “right” –Comparison with actual languages C-level existential types –Description of “new” soundness issue –Some non-problems C-level type sizes –Not a soundness issue Conclusions

25 July 2007Dan Grossman, 2007 Summer School57 Conclusions If you see an α near an assignment statement: Remain vigilant Do not be afraid of C-level thinking Surprisingly: –This work has really guided the design and implementation of Cyclone –The design space of imperative, polymorphic languages is not fully explored –“Dan’s unsoundness” has come up > n times Have (and use) datatypes with the “other” solution