Extending Type Systems in a Library Yuriy Solodkyy Jaakko Järvi Esam Mlaih.

Slides:



Advertisements
Similar presentations
1 Mooly Sagiv and Greta Yorsh School of Computer Science Tel-Aviv University Modern Compiler Design.
Advertisements

Chapter 2: Using Objects Part 1. To learn about variables To understand the concepts of classes and objects To be able to call methods To learn about.
CERTIFICATION OBJECTIVES Use Class Members Develop Wrapper Code & Autoboxing Code Determine the Effects of Passing Variables into Methods Recognize when.
Type Checking.
Chapter 9 Imperative and object-oriented languages 1.
Detecting Format String Vulnerabilities with Type Qualifier Umesh Shankar, Kunal Talwar, Jeffrey S. Foster, David Wanger University of California at Berkeley.
1 Chapter 11 Structured Types, Data Abstraction and Classes Dale/Weems/Headington.
 2006 Pearson Education, Inc. All rights reserved Midterm review Introduction to Classes and Objects.
Visualizing Type Qualifier Inference with Eclipse David Greenfieldboyce Jeffrey S. Foster University of Maryland.
1 Chapter 2 C++ Syntax and Semantics, and the Program Development Process Dale/Weems/Headington.
Chapter 16 Templates. Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives  Function Templates  Syntax, defining 
A Type-Checked Restrict Qualifier Jeff Foster OSQ Retreat May 9-10, 2001.
1 CS 105 Lecture 3 Constants & Expressions Wed, Jan 26, 2011, 4:15 pm.
1 Type Type system for a programming language = –set of types AND – rules that specify how a typed program is allowed to behave Why? –to generate better.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 11 Object and Object- Relational Databases.
1/25 Pointer Logic Changki PSWLAB Pointer Logic Daniel Kroening and Ofer Strichman Decision Procedure.
Differences between C# and C++ Dr. Catherine Stringfellow Dr. Stewart Carpenter.
XP New Perspectives on XML Tutorial 4 1 XML Schema Tutorial – Carey ISBN Working with Namespaces and Schemas.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
COMPUTER PROGRAMMING. Data Types “Hello world” program Does it do a useful work? Writing several lines of code. Compiling the program. Executing the program.
Extending Type Systems in a Library Yuriy Solodkyy, Jaakko Järvi, Esam Mlaih Department of Computer Science and Engineering Texas A&M University March.
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.
By: Saurabh Dixit.  Groovy server pages  Taglibs  Validators in grails.
C++ Template Metaprogramming Why, When and How? Zoltán Porkoláb Dept. of Programming Languages and Compilers, Faculty of Informatics Eötvös.
1 Programs Composed of Several Functions Syntax Templates Legal C++ Identifiers Assigning Values to Variables Declaring Named Constants String Concatenation.
Introduction to C++ // Program description #include directives int main() { constant declarations variable declarations executable statements return.
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 13: Introduction to Classes.
1 C++ Syntax and Semantics, and the Program Development Process.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
Copyright  Hannu Laine C++-programming Part 1 Hannu Laine.
Introduction to C Programming Chapter 2 : Data Input, Processing and Output.
CSE 425: Data Types I Data and Data Types Data may be more abstract than their representation –E.g., integer (unbounded) vs. 64-bit int (bounded) A language.
Copyright © Curt Hill Generic Classes Template Classes or Container Classes.
An OO schema language for XML SOX W3C Note 30 July 1999.
User-defined type checkers for error detection and prevention in Java Michael D. Ernst MIT Computer Science & AI Lab
CMP-MX21: Lecture 4 Selections Steve Hordley. Overview 1. The if-else selection in JAVA 2. More useful JAVA operators 4. Other selection constructs in.
1 Tutorial 14 Validating Documents with Schemas Exploring the XML Schema Vocabulary.
Tutorial 13 Validating Documents with Schemas
CSCI-383 Object-Oriented Programming & Design Lecture 18.
ISBN Chapter 6 Data Types Introduction Primitive Data Types User-Defined Ordinal Types.
1 Chapter 2 C++ Syntax and Semantics, and the Program Development Process.
APS105 Lists. Structures Arrays allow a collection of elements –All of the same type How to collect elements of different types? –Structures; in C: struct.
GE 211 Dr. Ahmed Telba. // compound assignment operators #include using namespace std; int main () { a =5 int a, b=3; a = b; a+=2; // equivalent to a=a+2.
Types of C Variables:  The following are some types of C variables on the basis of constants values it has. For example: ○ An integer variable can hold.
All Presentation Material Copyright Eurostep Group AB ® A Meta-model of EXPRESS in UML for MOF and UML to EXPRESS David Price April 2002.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Assignment 4 is due Nov. 20 (next Friday). After today you should know everything you need for assignment.
نظام المحاضرات الالكترونينظام المحاضرات الالكتروني Overloading operators C++ incorporates the option to use standard operators to perform operations with.
Object-Oriented Programming in C++ Lecture 4 Constants References Operator overloading.
Semantic Analysis II Type Checking EECS 483 – Lecture 12 University of Michigan Wednesday, October 18, 2006.
M.Sc. Seminar - Keren Lenz Supervisor - Dr. Yossi Gil July 1 st 2007 Simple and Safe SQL Queries with C++ Templates A RA R AT -
LECTURE 3 Compiler Phases. COMPILER PHASES Compilation of a program proceeds through a fixed series of phases.  Each phase uses an (intermediate) form.
C++ Basics Programming. COMP104 Lecture 5 / Slide 2 Introduction to C++ l C is a programming language developed in the 1970s with the UNIX operating system.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 13: Introduction to Classes.
Announcements You will receive your scores back for Assignment 2 this week. You will have an opportunity to correct your code and resubmit it for partial.
Array and Pointers An Introduction Unit Unit Introduction This unit covers the usage of pointers and arrays in C++
Chapter 16 Templates Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe Chapter 12 Outline Overview of Object Database Concepts Object-Relational Features Object Database.
CS 404Ahmed Ezzat 1 CS 404 Introduction to Compiler Design Lecture 1 Ahmed Ezzat.
CS Class 04 Topics  Selection statement – IF  Expressions  More practice writing simple C++ programs Announcements  Read pages for next.
CSE 332: C++ templates and generic programming II Review: Concepts and Models Templates impose requirements on type parameters –Types that are plugged.
1 09/10/04CS150 Introduction to Computer Science 1 What Actions Do We Have Part 2.
C++ Templates.
Chapter 12 Outline Overview of Object Database Concepts
Programming Introduction to C++.
How to organize and document your classes
Objectives You should be able to describe: The while Statement
Semantic Type Qualifiers
Presentation transcript:

Extending Type Systems in a Library Yuriy Solodkyy Jaakko Järvi Esam Mlaih

22 October 2006 Texas A&M University 2 Motivation l Type systems are good for you! –make sure certain bugs never appear –better optimizations l Type systems of a general purpose programming language are typically not extensible –a fixed part of a compiler l There are many domain specific type systems

22 October 2006 Texas A&M University 3 Use cases l Physical quantities l Types to track some semantic properties: –nullability, sign, oddity of a number –security vulnerability to format strings –usage of user pointers in kernel space –deadlocks and data races l Regular Expression Types

22 October 2006 Texas A&M University 4 Goal l Explore how far a pure library solution suffices to extending a type system

22 October 2006 Texas A&M University 5 Contributions l We report on implementing simple type qualifiers as a C++ library l We implement regular expression types (in a limited form) to check XML data l We provide a framework to help others in extending the C++ type system for their abstractions

22 October 2006 Texas A&M University 6 Example // a is a positive value double a = current_height(); // b is a positive value double b = max_allowed_height(); // b-a may be negative though! Result is always positive double c = std::sqrt(b-a); // b+a is assumed to be positive. Unless there is bug! double d = std::sqrt(b+a); returns only positive numbers upper bound for values from previous call difference is positive, but not for type system argument and result are always positive

22 October 2006 Texas A&M University 7 What do we need? l Typing rules l Evaluation rules l Subtyping rules

22 October 2006 Texas A&M University 8 How do we achieve that in C++? l Typing rules –Type construction via class templates l Evaluation rules –Function templates and overloading l Subtyping rules –A dedicated meta-function

22 October 2006 Texas A&M University 9 Useful building blocks l Typing rules –tuple, variant, optional l Evaluation rules –enable_if l Subtyping rules –MPL l Interoperability of the above libraries!

22 October 2006 Texas A&M University 10 Type qualifiers l Allow tracking of semantic properties like: –immutability of a certain value (const) –sign of a number (pos, neg) –assumptions about pointers (optional, nonnull) –trustworthiness of a certain value (tainted, untainted) –oddity of a number (odd, even) –origin of a pointer (user, kernel)

22 October 2006 Texas A&M University 11 Example: Qualifiers’ Hello World #include DECLARE_NEGATIVE_QUALIFIER(pos); DECLARE_POSITIVE_QUALIFIER(tainted); //... Other qualifiers... namespace xtl { template <> struct minus { typedef qual type; }; template <> struct mul { typedef qual type; }; template <> struct div { typedef qual type;}; } // of namespace xtl int main() { untainted > > a(-42); pos > > b(7); neg > c = a * b; // OK: drop negative qualifier untainted //nonnull > d = b - a; // Error: nonnull isn’t carried by - pos > e = b + a*c; // OK to add positive qualifier //pos f = e; // Error:... but not OK to drop it! } Declare few qualifiers: Q is positive if T <: Q T Q is negative if Q T <: T Define how different operations transfer properties Declare your variables with appropriate properties Multiplication carries nonnull & negativeness on pos & neg arguments Subtraction does not carry nonnull Positive qualifiers can be added to result type but not dropped once they are there!

22 October 2006 Texas A&M University 12 Example // Example definition that accepts only positive doubles template typename enable_if< typename is_subtype >::type, void >::type descend(const U& altitude) { pos a = subtype_cast >(altitude); //... }; // Data coming from measurements is marked untainted extern pos > get_corridor_height(); untainted > a = get_corridor_height(); descend(a); // no negative altitudes here! function descend accepts only positive numbers we achieve this by restricting its argument type to be a subtype of pos we convert value of a subtype into a value of a supertype returns a value that is both: positive and untainted a can hold positive, untainted values. order of qualifiers is not important! no negative altitudes can appear here!

22 October 2006 Texas A&M University 13 Qualifiers summary l Easy to define and use l Cannot handle flow-sensitive qualifiers l Cannot handle arbitrary reference qualifiers (e.g. aliasing related)

22 October 2006 Texas A&M University 14 Type system for XML l Types can describe XML elements with certain structure l Subtyping describes structurally more powerful types l Compile-time assurance that only valid XML documents are produced l Value-preserving type conversions

22 October 2006 Texas A&M University 15 typedef element XMLname; typedef element XML ; typedef element XMLicq; typedef element<contact, boost::variant< XML , XMLicq > > XMLcontact; typedef element<person, fusion::tuple< XMLname, XMLcontact > > XMLperson; XML Schema’s choice is mapped to Boost variant back references are mapped to previous typedefs we map XML data types into C++ types for each tag we create a dedicated tag-type Example XML SchemaC++ for each tag we create a dedicated tag-type we map XML data types into C++ types back references are mapped to previous typedefs XML Schema’s sequence is mapped to Fusion’s tuple XML Schema’s choice is mapped to Boost variant we use a dedicated type element to represent XML elements

22 October 2006 Texas A&M University 16 Example //... typedef variant AnyContact; typedef element > Person; typedef element > PersonEx; int main() { Person p(make_tuple(Name("Yuriy"), Tel(" "), ICQ(1234))); PersonEx x = p; // OK: Subtyping conversion // p = x; // ERROR: Not in subtyping relation ifstream xml("old-person.xml"); xml >> x; // read data from XML file. assumes file exist cout << x << endl; // show XML source on the screen } Tel <: AnyContactICQ <: AnyContact Name, Tel, ICQ <: Name, AnyContact, AnyContact Person <: PersonEx Instantiate an XML snippet that corresponds to Person type Person <: PersonEx Assignment involves subtype conversion PersonEx is not a subtype of Person Parses only XML files that correspond to PersonEx schema Produces XML source on the screen

22 October 2006 Texas A&M University 17 XDuce l Type –set of sequences over a certain domain l Regular Expression Types –concatenation : A,B –alternation : A|B –repetition : A* –optional : A? –type construction : l[A] –recursion : X = A,X | ø l Subtyping –inclusion between the sets defined by types

22 October 2006 Texas A&M University 18 C++ l Type –set of sequences over a certain domain l Regular Expression Types –concatenation : A,B tuple –alternation : A|B variant –repetition : A* vector –optional : A? optional –type construction : l[A] element –recursion : X = A,X | ø – l Subtyping –is_subtype and subtype_cast

22 October 2006 Texas A&M University 19 XTL – eXtensible Typing Library l Provides a common interface for defining custom subtyping relation l Provides a common interface for defining conversion from a subtype to a supertype l Provides some ready definitions that can be used in defining other type systems: –subtyping of array types –subtyping of function types –subtyping of sequences and union types –subtyping of qualified types

22 October 2006 Texas A&M University 20 Limitations l Reflexivity of subtyping relation has to be stated manually for each new type. l No implicit transitivity of subtyping relation. Meta-function join may return an arbitrary upper bound.

22 October 2006 Texas A&M University 21 Future work l Look at applying our approach to ownership types l Look at alternative representations of type qualifiers l Extend subtyping of repetitions l Create a library of useful type qualifiers

22 October 2006 Texas A&M University 22 Thank You! Questions?