a type system for preventing data races and deadlocks in the java virtual machine language pratibha...

44
Preventing Data Races Preventing Data Races and Deadlocks in the and Deadlocks in the Java Virtual Machine Java Virtual Machine Language Language Pratibha Permandla Michael Roberson Chandrasekhar Boyapati University of Michigan

Post on 20-Dec-2015

214 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: A Type System for Preventing Data Races and Deadlocks in the Java Virtual Machine Language Pratibha Permandla Michael Roberson Chandrasekhar Boyapati University

A Type System for Preventing A Type System for Preventing Data Races and Deadlocks in the Data Races and Deadlocks in the Java Virtual Machine LanguageJava Virtual Machine Language

Pratibha Permandla

Michael Roberson

Chandrasekhar Boyapati

University of Michigan

Page 2: A Type System for Preventing Data Races and Deadlocks in the Java Virtual Machine Language Pratibha Permandla Michael Roberson Chandrasekhar Boyapati University

OutlineOutline

Motivation• Data Races

• Deadlocks

• Object EncapsulationType SystemRelated Work

Motivation• Data Races

• Deadlocks

• Object EncapsulationType SystemRelated Work

Page 3: A Type System for Preventing Data Races and Deadlocks in the Java Virtual Machine Language Pratibha Permandla Michael Roberson Chandrasekhar Boyapati University

Data Races in Multithreaded Data Races in Multithreaded ProgramsPrograms

Two threads access the same dataAt least one access is a writeNo synchronization to separate accesses

Thread 1:Thread 1:

x = x + 1; x = x + 1;

Thread 2:Thread 2:

x = x + 2; x = x + 2;

Page 4: A Type System for Preventing Data Races and Deadlocks in the Java Virtual Machine Language Pratibha Permandla Michael Roberson Chandrasekhar Boyapati University

Why Data Races are a ProblemWhy Data Races are a ProblemSome correct programs contain data races

But most races are programming errors

• Code intended to execute atomically

• Synchronization omitted by mistake

Consequences can be severe

• Nondeterministic timing-dependent bugs

• Difficult to detect, reproduce, eliminate

Page 5: A Type System for Preventing Data Races and Deadlocks in the Java Virtual Machine Language Pratibha Permandla Michael Roberson Chandrasekhar Boyapati University

Avoiding Data RacesAvoiding Data Races

Thread 1:Thread 1:

x = x + 1; x = x + 1;

Thread 2:Thread 2:

x = x + 2; x = x + 2;

Page 6: A Type System for Preventing Data Races and Deadlocks in the Java Virtual Machine Language Pratibha Permandla Michael Roberson Chandrasekhar Boyapati University

Avoiding Data RacesAvoiding Data Races

Associate locks with shared mutable dataAcquire lock before data accessRelease lock after data access

Thread 1:Thread 1:

lock(l);lock(l);

x = x + 1; x = x + 1;

unlock(l);unlock(l);

Thread 2:Thread 2:

lock(l); lock(l);

x = x + 2; x = x + 2;

unlock(l);unlock(l);

Page 7: A Type System for Preventing Data Races and Deadlocks in the Java Virtual Machine Language Pratibha Permandla Michael Roberson Chandrasekhar Boyapati University

Avoiding Data RacesAvoiding Data Races

Problem: Locking is not enforced!Inadvertent programming errors…Problem: Locking is not enforced!Inadvertent programming errors…

Thread 1:Thread 1:

lock(l);lock(l);

x = x + 1; x = x + 1;

unlock(l);unlock(l);

Thread 2:Thread 2:

lock(l); lock(l);

x = x + 2; x = x + 2;

unlock(l);unlock(l);

Page 8: A Type System for Preventing Data Races and Deadlocks in the Java Virtual Machine Language Pratibha Permandla Michael Roberson Chandrasekhar Boyapati University

Avoiding DeadlocksAvoiding Deadlocks

Thread 1Thread 1

Thread 2Thread 2

Thread nThread n

……

Lock 1Lock 1 Lock nLock n

Lock 2Lock 2 Lock 3Lock 3

Page 9: A Type System for Preventing Data Races and Deadlocks in the Java Virtual Machine Language Pratibha Permandla Michael Roberson Chandrasekhar Boyapati University

Avoiding DeadlocksAvoiding Deadlocks

Thread 1Thread 1

Thread 2Thread 2

Thread nThread n

……

Lock 1Lock 1 Lock nLock n

Lock 2Lock 2 Lock 3Lock 3

Associate a partial order among locksAcquire locks in order

~~~~

Page 10: A Type System for Preventing Data Races and Deadlocks in the Java Virtual Machine Language Pratibha Permandla Michael Roberson Chandrasekhar Boyapati University

Avoiding DeadlocksAvoiding Deadlocks

Thread 1Thread 1

Thread 2Thread 2

Thread nThread n

……

Lock 1Lock 1 Lock nLock n

Lock 2Lock 2 Lock 3Lock 3

~~~~Problem: Lock ordering is not enforced!Problem: Lock ordering is not enforced!

Inadvertent programming errors…Inadvertent programming errors…

Problem: Lock ordering is not enforced!Problem: Lock ordering is not enforced!

Inadvertent programming errors…Inadvertent programming errors…

Page 11: A Type System for Preventing Data Races and Deadlocks in the Java Virtual Machine Language Pratibha Permandla Michael Roberson Chandrasekhar Boyapati University

Object EncapsulationObject Encapsulation

Enables local reasoning

Stack s is implemented with a linked listOutside objects must not access list nodes

ssoo ~~~~

Page 12: A Type System for Preventing Data Races and Deadlocks in the Java Virtual Machine Language Pratibha Permandla Michael Roberson Chandrasekhar Boyapati University

Object EncapsulationObject EncapsulationStack s is implemented with a linked listOutside objects must not access list nodes

Problem: Encapsulation is not enforced!Problem: Encapsulation is not enforced!

Inadvertent programming errors…Inadvertent programming errors…

Problem: Encapsulation is not enforced!Problem: Encapsulation is not enforced!

Inadvertent programming errors…Inadvertent programming errors…

ssoo ~~~~

Page 13: A Type System for Preventing Data Races and Deadlocks in the Java Virtual Machine Language Pratibha Permandla Michael Roberson Chandrasekhar Boyapati University

SolutionSolution

Type system for object-oriented languagesStatically prevents errors

• data races, deadlocks, representation exposureProgrammers write simple annotations

• how objects are synchronized

• partial ordering on locks to prevent deadlocks

• encapsulation hierarchyType checker statically verifies program

• Objects are used only as specified

Page 14: A Type System for Preventing Data Races and Deadlocks in the Java Virtual Machine Language Pratibha Permandla Michael Roberson Chandrasekhar Boyapati University

Ownership TypesOwnership Types

Every object is owned by• Another object, or

• A thread, or

• A special global owner called world

Ownership forms a tree rooted at world

Thread1Thread1Thread1Thread1 Thread2Thread2Thread2Thread2

Thread2 objectsThread2 objectsThread1 objectsThread1 objects Potentially shared objectsPotentially shared objects

worldworldworldworld

Page 15: A Type System for Preventing Data Races and Deadlocks in the Java Virtual Machine Language Pratibha Permandla Michael Roberson Chandrasekhar Boyapati University

Ownership TypesOwnership Types

Prevent representation exposure• No references from outside object o to objects owned by o

• No references from outside thread t to objects owned by t

Thread1Thread1Thread1Thread1 Thread2Thread2Thread2Thread2

Thread2 objectsThread2 objectsThread1 objectsThread1 objects Potentially shared objectsPotentially shared objects

worldworldworldworld

Page 16: A Type System for Preventing Data Races and Deadlocks in the Java Virtual Machine Language Pratibha Permandla Michael Roberson Chandrasekhar Boyapati University

Ownership TypesOwnership Types Prevent races

• For race free access to an object not owned by a thread The lock on its outermost containing object must be held

• For race free access to an object owned by a thread No lock needs to be held

Thread1Thread1Thread1Thread1 Thread2Thread2Thread2Thread2

Thread2 objectsThread2 objectsThread1 objectsThread1 objects Potentially shared objectsPotentially shared objects

Acquire LocksAcquire Locks

worldworldworldworld

Page 17: A Type System for Preventing Data Races and Deadlocks in the Java Virtual Machine Language Pratibha Permandla Michael Roberson Chandrasekhar Boyapati University

Ownership TypesOwnership Types Prevent Deadlocks

• Locks must be ordered according to a partial order

• Locks must be acquired in descending order

Thread1Thread1Thread1Thread1 Thread2Thread2Thread2Thread2

Thread2 objectsThread2 objectsThread1 objectsThread1 objects Potentially shared objectsPotentially shared objects

1 2

Acquire LocksAcquire Locks

worldworldworldworld

Page 18: A Type System for Preventing Data Races and Deadlocks in the Java Virtual Machine Language Pratibha Permandla Michael Roberson Chandrasekhar Boyapati University

class TStack {class TStack {

TNode head;TNode head;

void push(T value) {…}void push(T value) {…}

T pop() {…}T pop() {…}

}}

class TNode {class TNode {

TNode next; TNode next;

T value;T value;

… …

}}

class T {…}class T {…}

TStack ExampleTStack Example

valuevalue

nextnext

headhead

valuevalue

nextnext

valuevalue

nextnext

…… …………

TStackTStack

TNodeTNode

TT

Page 19: A Type System for Preventing Data Races and Deadlocks in the Java Virtual Machine Language Pratibha Permandla Michael Roberson Chandrasekhar Boyapati University

TStack ExampleTStack Example

class TStackclass TStackstackOwner, TOwnerstackOwner, TOwner { { TNodeTNodethis, TOwnerthis, TOwner head; head;

… …

}}

class TNodeclass TNodenodeOwner, TOwnernodeOwner, TOwner { { TNodeTNodenodeOwner, TOwnernodeOwner, TOwner next; next;

TTTOwnerTOwner value; value;

… …

}}

TStackTStack

TNodeTNode

TT

Page 20: A Type System for Preventing Data Races and Deadlocks in the Java Virtual Machine Language Pratibha Permandla Michael Roberson Chandrasekhar Boyapati University

TStack ExampleTStack Example

class TStackclass TStackstackOwner, TOwnerstackOwner, TOwner { { TNodeTNodethis, TOwnerthis, TOwner head; head;

… …

}}

class TNodeclass TNodenodeOwner, TOwnernodeOwner, TOwner { { TNodeTNodenodeOwner, TOwnernodeOwner, TOwner next; next;

TTTOwnerTOwner value; value;

… …

}}

TStackTStack

TTTStackTStackthisThread, thisThreadthisThread, thisThread s1; s1;

TStackTStackthisThread, worldthisThread, world s2; s2;

TStackTStackworld, worldworld, world s3; s3;

worldworld

Thread1Thread1

Page 21: A Type System for Preventing Data Races and Deadlocks in the Java Virtual Machine Language Pratibha Permandla Michael Roberson Chandrasekhar Boyapati University

Checking ProgramsChecking Programs

Type Type checkerchecker CompilerCompiler

BytecodesBytecodes

+ Extra + Extra typestypes

JavaJava

VirtualVirtualMachineMachine

Previous work was on SafeJava

Page 22: A Type System for Preventing Data Races and Deadlocks in the Java Virtual Machine Language Pratibha Permandla Michael Roberson Chandrasekhar Boyapati University

Our ApproachOur Approach

Type Type checkerchecker CompilerCompiler

BytecodesBytecodes

+ Extra + Extra typestypes

JavaJava

+ Extra+ Extratypes ontypes on

interfacesinterfaces

IntraproceduralIntraproceduralTypeType

InferenceInference

BytecodeBytecodeVerifierVerifier

VirtualVirtualMachineMachine

Previous work was on SafeJava We extend to SafeJVML

• Verifies Java bytecodes

Page 23: A Type System for Preventing Data Races and Deadlocks in the Java Virtual Machine Language Pratibha Permandla Michael Roberson Chandrasekhar Boyapati University

ExampleExample

1234567891011121314151617181920212223242526272829

load 0store 3load 3monitorenterload 1store 4load 4monitorenterload 0getfieldpush 0ifeq 25load 1load 1getfieldload 2addputfieldload 0load 0getfieldload 2subputfieldload 4monitorexitload 3monitorexitreturn

static void transfer(Account, Account, int); class Account { private int balance; static void transfer(Account from, Account to, int x) { synchronized (to) { synchronized (from) { if (from.balance != 0) { to.balance += x; from.balance -= x; } } } }}

No block structure No types on stack or local variables Requires alias analysis

Page 24: A Type System for Preventing Data Races and Deadlocks in the Java Virtual Machine Language Pratibha Permandla Michael Roberson Chandrasekhar Boyapati University

ExampleExamplei Instruction Fi[0] Fi[1] Fi[2] Fi[3] Fi[4] Si LSi

1234567891011121314151617181920212223242526272829

load 0store 3load 3monitorenterload 1store 4load 4monitorenterload 0getfieldpush 0ifeq 25load 1load 1getfieldload 2addputfieldload 0load 0getfieldload 2subputfieldload 4monitorexitload 3monitorexitreturn

Fi[n] : Type of local variable nat instruction i

Si : Types of elements of the stackat instruction iLSi : Types of locks held

at instruction i

Problem: Can’t tell which object islocked based on the type

Page 25: A Type System for Preventing Data Races and Deadlocks in the Java Virtual Machine Language Pratibha Permandla Michael Roberson Chandrasekhar Boyapati University

Indexed TypesIndexed Types

Solution: Use indexed types• Laneve and Bigliardi (TIC ’00)

• Example : Object3

• Objects with identical indexed types are equal

• Otherwise, unknown

Page 26: A Type System for Preventing Data Races and Deadlocks in the Java Virtual Machine Language Pratibha Permandla Michael Roberson Chandrasekhar Boyapati University

ExampleExamplei Instruction Fi[0] Fi[1] Fi[2] Fi[3] Fi[4] Si LSi

1234567891011121314151617181920212223242526272829

load 0store 3load 3monitorenterload 1store 4load 4monitorenterload 0getfieldpush 0ifeq 25load 1load 1getfieldload 2addputfieldload 0load 0getfieldload 2subputfieldload 4monitorexitload 3monitorexitreturn

AccountAccount1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

AccountAccountAccountAccountAccountAccount5

Account5

Account5

Account5

Account5

Account5

Account5

Account5

Account5

Account5

Account5

Account5

Account5

Account5

Account5

Account5

Account5

Account5

Account5

Account5

Account5

Account5

Account5

Account5

intintintintintintintintintintintintintintintintintintintintintintintintintintintintint

----Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

------------Account5

Account5

Account5

Account5

Account5

Account5

Account5

Account5

Account5

Account5

Account5

Account5

Account5

Account5

Account5

Account5

Account5

Account5

Account5

Account5

Account5

Account5

Account5

Account1

Account1

Account5

Account5

Account1

intint.int

Account5

Account5.Account5

int.Account5

int.int.Account5

int.Account5

Account1

Account1.Account1

int.Account1

int.int.Account1

int.Account1

Account5

Account1

Account1

Account1

Account1

Account1

Account1, Account5

Account1, Account5

Account1, Account5

Account1, Account5

Account1, Account5

Account1, Account5

Account1, Account5

Account1, Account5

Account1, Account5

Account1, Account5

Account1, Account5

Account1, Account5

Account1, Account5

Account1, Account5

Account1, Account5

Account1, Account5

Account1, Account5

Account1, Account5

Account1

Account1

Page 27: A Type System for Preventing Data Races and Deadlocks in the Java Virtual Machine Language Pratibha Permandla Michael Roberson Chandrasekhar Boyapati University

Static SemanticsStatic Semantics

i Instruction Fi[0] Fi[1] Fi[2] Fi[3] Fi[4] Si LSi

1234567891011121314151617181920212223242526272829

load 0store 3load 3monitorenterload 1store 4load 4monitorenterload 0getfieldpush 0ifeq 25load 1load 1getfieldload 2addputfieldload 0load 0getfieldload 2subputfieldload 4monitorexitload 3monitorexitreturn

AccountAccount1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

AccountAccountAccountAccountAccountAccount5

Account5

Account5

Account5

Account5

Account5

Account5

Account5

Account5

Account5

Account5

Account5

Account5

Account5

Account5

Account5

Account5

Account5

Account5

Account5

Account5

Account5

Account5

Account5

intintintintintintintintintintintintintintintintintintintintintintintintintintintintint

----Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

------------Account5

Account5

Account5

Account5

Account5

Account5

Account5

Account5

Account5

Account5

Account5

Account5

Account5

Account5

Account5

Account5

Account5

Account5

Account5

Account5

Account5

Account5

Account5

Account1

Account1

Account5

Account5

Account1

intint.int

Account5

Account5.Account5

int.Account5

int.int.Account5

int.Account5

Account1

Account1.Account1

int.Account1

int.int.Account1

int.Account1

Account5

Account1

Account1

Account1

Account1

Account1

Account1, Account5

Account1, Account5

Account1, Account5

Account1, Account5

Account1, Account5

Account1, Account5

Account1, Account5

Account1, Account5

Account1, Account5

Account1, Account5

Account1, Account5

Account1, Account5

Account1, Account5

Account1, Account5

Account1, Account5

Account1, Account5

Account1, Account5

Account1, Account5

Account1

Account1

M[i] = add

i+1 Dom(M)

Fi <: Fi+1

Si = int.int.β

int.β <: Si+1

LSi = LSi+1

Page 28: A Type System for Preventing Data Races and Deadlocks in the Java Virtual Machine Language Pratibha Permandla Michael Roberson Chandrasekhar Boyapati University

Static SemanticsStatic Semanticsi Instruction Fi[0] Fi[1] Fi[2] Fi[3] Fi[4] Si LSi

1234567891011121314151617181920212223242526272829

load 0store 3load 3monitorenterload 1store 4load 4monitorenterload 0getfieldpush 0ifeq 25load 1load 1getfieldload 2addputfieldload 0load 0getfieldload 2subputfieldload 4monitorexitload 3monitorexitreturn

AccountAccount1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

AccountAccountAccountAccountAccountAccount5

Account5

Account5

Account5

Account5

Account5

Account5

Account5

Account5

Account5

Account5

Account5

Account5

Account5

Account5

Account5

Account5

Account5

Account5

Account5

Account5

Account5

Account5

Account5

intintintintintintintintintintintintintintintintintintintintintintintintintintintintint

----Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

------------Account5

Account5

Account5

Account5

Account5

Account5

Account5

Account5

Account5

Account5

Account5

Account5

Account5

Account5

Account5

Account5

Account5

Account5

Account5

Account5

Account5

Account5

Account5

Account1

Account1

Account5

Account5

Account1

intint.int

Account5

Account5.Account5

int.Account5

int.int.Account5

int.Account5

Account1

Account1.Account1

int.Account1

int.int.Account1

int.Account1

Account5

Account1

Account1

Account1

Account1

Account1

Account1, Account5

Account1, Account5

Account1, Account5

Account1, Account5

Account1, Account5

Account1, Account5

Account1, Account5

Account1, Account5

Account1, Account5

Account1, Account5

Account1, Account5

Account1, Account5

Account1, Account5

Account1, Account5

Account1, Account5

Account1, Account5

Account1, Account5

Account1, Account5

Account1

Account1

M[i] = ifeq L

i+1, L Dom(M)

Fi <: Fi+1

Si <: t.t.Si+1

LSi = LSi+1

Fi <: FL

Si <: t.t.SL

LSi = LSL

Page 29: A Type System for Preventing Data Races and Deadlocks in the Java Virtual Machine Language Pratibha Permandla Michael Roberson Chandrasekhar Boyapati University

Static SemanticsStatic Semanticsi Instruction Fi[0] Fi[1] Fi[2] Fi[3] Fi[4] Si LSi

1234567891011121314151617181920212223242526272829

load 0store 3load 3monitorenterload 1store 4load 4monitorenterload 0getfieldpush 0ifeq 25load 1load 1getfieldload 2addputfieldload 0load 0getfieldload 2subputfieldload 4monitorexitload 3monitorexitreturn

AccountAccount1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

AccountAccountAccountAccountAccountAccount5

Account5

Account5

Account5

Account5

Account5

Account5

Account5

Account5

Account5

Account5

Account5

Account5

Account5

Account5

Account5

Account5

Account5

Account5

Account5

Account5

Account5

Account5

Account5

intintintintintintintintintintintintintintintintintintintintintintintintintintintintint

----Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

------------Account5

Account5

Account5

Account5

Account5

Account5

Account5

Account5

Account5

Account5

Account5

Account5

Account5

Account5

Account5

Account5

Account5

Account5

Account5

Account5

Account5

Account5

Account5

Account1

Account1

Account5

Account5

Account1

intint.int

Account5

Account5.Account5

int.Account5

int.int.Account5

int.Account5

Account1

Account1.Account1

int.Account1

int.int.Account1

int.Account1

Account5

Account1

Account1

Account1

Account1

Account1

Account1, Account5

Account1, Account5

Account1, Account5

Account1, Account5

Account1, Account5

Account1, Account5

Account1, Account5

Account1, Account5

Account1, Account5

Account1, Account5

Account1, Account5

Account1, Account5

Account1, Account5

Account1, Account5

Account1, Account5

Account1, Account5

Account1, Account5

Account1, Account5

Account1

Account1

M[i] = monitorenter

i+1 Dom(M)

Fi <: Fi+1

Si <: cnworld,...Si+1

LSi+1 = cnworld,...LSi

Page 30: A Type System for Preventing Data Races and Deadlocks in the Java Virtual Machine Language Pratibha Permandla Michael Roberson Chandrasekhar Boyapati University

Static SemanticsStatic Semanticsi Instruction Fi[0] Fi[1] Fi[2] Fi[3] Fi[4] Si LSi

1234567891011121314151617181920212223242526272829

load 0store 3load 3monitorenterload 1store 4load 4monitorenterload 0getfieldpush 0ifeq 25load 1load 1getfieldload 2addputfieldload 0load 0getfieldload 2subputfieldload 4monitorexitload 3monitorexitreturn

AccountAccount1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

AccountAccountAccountAccountAccountAccount5

Account5

Account5

Account5

Account5

Account5

Account5

Account5

Account5

Account5

Account5

Account5

Account5

Account5

Account5

Account5

Account5

Account5

Account5

Account5

Account5

Account5

Account5

Account5

intintintintintintintintintintintintintintintintintintintintintintintintintintintintint

----Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

------------Account5

Account5

Account5

Account5

Account5

Account5

Account5

Account5

Account5

Account5

Account5

Account5

Account5

Account5

Account5

Account5

Account5

Account5

Account5

Account5

Account5

Account5

Account5

Account1

Account1

Account5

Account5

Account1

intint.int

Account5

Account5.Account5

int.Account5

int.int.Account5

int.Account5

Account1

Account1.Account1

int.Account1

int.int.Account1

int.Account1

Account5

Account1

Account1

Account1

Account1

Account1

Account1, Account5

Account1, Account5

Account1, Account5

Account1, Account5

Account1, Account5

Account1, Account5

Account1, Account5

Account1, Account5

Account1, Account5

Account1, Account5

Account1, Account5

Account1, Account5

Account1, Account5

Account1, Account5

Account1, Account5

Account1, Account5

Account1, Account5

Account1, Account5

Account1

Account1

M[i] = getfield ||cnf1..n,fd,t||Fi+1 Dom(M)

Fi <: Fi+1

Si <: cno1..n.β

t[o1/f1]..[on/fn][cno1..n/this].β <: Si+1

Lock(cno1..n) LSi

LSi = LSi+1

Page 31: A Type System for Preventing Data Races and Deadlocks in the Java Virtual Machine Language Pratibha Permandla Michael Roberson Chandrasekhar Boyapati University

Static SemanticsStatic Semanticsi Instruction Fi[0] Fi[1] Fi[2] Fi[3] Fi[4] Si LSi

1234567891011121314151617181920212223242526272829

load 0store 3load 3monitorenterload 1store 4load 4monitorenterload 0getfieldpush 0ifeq 25load 1load 1getfieldload 2addputfieldload 0load 0getfieldload 2subputfieldload 4monitorexitload 3monitorexitreturn

AccountAccount1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

AccountAccountAccountAccountAccountAccount5

Account5

Account5

Account5

Account5

Account5

Account5

Account5

Account5

Account5

Account5

Account5

Account5

Account5

Account5

Account5

Account5

Account5

Account5

Account5

Account5

Account5

Account5

Account5

intintintintintintintintintintintintintintintintintintintintintintintintintintintintint

----Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

Account1

------------Account5

Account5

Account5

Account5

Account5

Account5

Account5

Account5

Account5

Account5

Account5

Account5

Account5

Account5

Account5

Account5

Account5

Account5

Account5

Account5

Account5

Account5

Account5

Account1

Account1

Account5

Account5

Account1

intint.int

Account5

Account5.Account5

int.Account5

int.int.Account5

int.Account5

Account1

Account1.Account1

int.Account1

int.int.Account1

int.Account1

Account5

Account1

Account1

Account1

Account1

Account1

Account1, Account5

Account1, Account5

Account1, Account5

Account1, Account5

Account1, Account5

Account1, Account5

Account1, Account5

Account1, Account5

Account1, Account5

Account1, Account5

Account1, Account5

Account1, Account5

Account1, Account5

Account1, Account5

Account1, Account5

Account1, Account5

Account1, Account5

Account1, Account5

Account1

Account1

M[i] = getfield ||cnf1..n,fd,t||Fi+1 Dom(M)

Fi <: Fi+1

Si <: cno1..n.β

t[o1/f1]..[on/fn][cno1..n/this].β <: Si+1

Lock(cno1..n) LSi

LSi = LSi+1

M[i] = monitorenter

i+1 Dom(M)

Fi <: Fi+1

Si <: cnworld,...Si+1

LSi+1 = cnworld,...LSi

M[i] = ifeq L

i+1, L Dom(M)

Fi <: Fi+1

Si <: t.t.Si+1

LSi = LSi+1

Fi <: FL

Si <: t.t.SL

LSi = LSL

M[i] = add

i+1 Dom(M)

Fi <: Fi+1

Si = int.int.β

int.β <: Si+1

LSi = LSi+1

Page 32: A Type System for Preventing Data Races and Deadlocks in the Java Virtual Machine Language Pratibha Permandla Michael Roberson Chandrasekhar Boyapati University

Properties of SafeJVMLProperties of SafeJVML SafeJVML programs are free of data races

SafeJVML programs are free of deadlocks

SafeJVML programs are free of encapsulation errors

Need a proof of these properties Need a formalization of dynamic semantics

SafeJVML programs are free of data races

SafeJVML programs are free of deadlocks

SafeJVML programs are free of encapsulation errors

Need a proof of these properties Need a formalization of dynamic semantics

Page 33: A Type System for Preventing Data Races and Deadlocks in the Java Virtual Machine Language Pratibha Permandla Michael Roberson Chandrasekhar Boyapati University

Dynamic SemanticsDynamic Semantics

o1f : o3v : 2

Heap

o2a : o5

o4n : 1

o5

o3a : o3

Thread1Thread2Thread3Thread4

First Activation Record

Second Activation Record

Current Activation Record

M : foo pc : 6

o3 7 o1o1

4

o1

0 1 2

Local Variables: Stack:

o4 o1Locks:

Page 34: A Type System for Preventing Data Races and Deadlocks in the Java Virtual Machine Language Pratibha Permandla Michael Roberson Chandrasekhar Boyapati University

Dynamic SemanticsDynamic SemanticsThread1Thread2Thread3Thread4

First Activation Record

Second Activation Record

Current Activation Record

M : foo pc : 6

o3 7 o1o1

4

o1

0 1 2

Local Variables: Stack:

Locks:

o1f : o3v : 2

Heap

o2a : o5

o4n : 1

o5

o3a : o3

M[pc] = getfield v

(M,pc,f,o.s,ls.A); h →

(M,pc+1,f,(h[o].v).s,ls.A); h

pc : 7

2

o4 o1

Page 35: A Type System for Preventing Data Races and Deadlocks in the Java Virtual Machine Language Pratibha Permandla Michael Roberson Chandrasekhar Boyapati University

Dynamic SemanticsDynamic SemanticsThread1Thread2Thread3Thread4

First Activation Record

Second Activation Record

Current Activation Record

M : foo

o3 7 o12

4

o1

0 1 2

Local Variables: Stack:

Locks:

o1f : o3v : 2

Heap

o2a : o5

o4n : 1

o5

o3a : o3

M[pc] = add

(M,pc,f,v1.v2.s,ls.A); h →

(M,pc+1,f,(v1+v2).s,ls.A); h

pc : 7pc : 8

6

o4 o1

Page 36: A Type System for Preventing Data Races and Deadlocks in the Java Virtual Machine Language Pratibha Permandla Michael Roberson Chandrasekhar Boyapati University

Dynamic SemanticsDynamic SemanticsThread1Thread2Thread3Thread4

First Activation Record

Second Activation Record

Current Activation Record

M : foo

o3 7 o16

o1

0 1 2

Local Variables: Stack:

Locks:

o1f : o3v : 2

Heap

o2a : o5

o4n : 1

o5

o3a : o3

M[pc] = store 0

(M,pc,f,v.s,ls.A); h →

(M,pc+1,f[0 →v],s,ls.A); h

pc : 8pc : 9

6

o4 o1

Page 37: A Type System for Preventing Data Races and Deadlocks in the Java Virtual Machine Language Pratibha Permandla Michael Roberson Chandrasekhar Boyapati University

Dynamic SemanticsDynamic SemanticsThread1Thread2Thread3Thread4

First Activation Record

Second Activation Record

Current Activation Record

M : foo

6 7 o1

o1

0 1 2

Local Variables: Stack:

Locks:

o1f : o3v : 2

Heap

o2a : o5

o4n : 1

o5

o3a : o3

M[pc] = monitorexit

(M,pc,f,o.s,ls {o}.A); h →

(M,pc+1,f,s,ls.A); h

pc : 9pc : 10

o4 o1

Page 38: A Type System for Preventing Data Races and Deadlocks in the Java Virtual Machine Language Pratibha Permandla Michael Roberson Chandrasekhar Boyapati University

Dynamic SemanticsDynamic Semantics

M[pc] = getfield v

(M,pc,f,o.s,ls.A); h →

(M,pc+1,f,(h[o].v).s,ls.A); h

M[pc] = add

(M,pc,f,v1.v2.s,ls.A); h →

(M,pc+1,f,(v1+v2).s,ls.A); h

M[pc] = store 0

(M,pc,f,v.s,ls.A); h →

(M,pc+1,f[0 →v],s,ls.A); h

M[pc] = monitorexit

(M,pc,f,o.s,ls {o}.A); h →

(M,pc+1,f,s,ls.A); h

Page 39: A Type System for Preventing Data Races and Deadlocks in the Java Virtual Machine Language Pratibha Permandla Michael Roberson Chandrasekhar Boyapati University

Proof SketchProof Sketch

Identify runtime invariants• Relating static and dynamic semantics

• States satisfying invariants are well-typedProve that invariants always hold

Page 40: A Type System for Preventing Data Races and Deadlocks in the Java Virtual Machine Language Pratibha Permandla Michael Roberson Chandrasekhar Boyapati University

Proof SketchProof Sketch

Preservation Theorem• A well-typed state only transitions to

other well-typed statesProgress Theorem

• A well-typed program state: transitions to another state, or terminates normally, or has a null dereference

Page 41: A Type System for Preventing Data Races and Deadlocks in the Java Virtual Machine Language Pratibha Permandla Michael Roberson Chandrasekhar Boyapati University

Proof SketchProof Sketch

Identify runtime invariants• Relating static and dynamic semantics

• States satisfying invariants are well-typedProve that invariants always holdUse invariants to prove properties

• There are no data races

• There are no deadlocks

• Encapsulation is never violated

Page 42: A Type System for Preventing Data Races and Deadlocks in the Java Virtual Machine Language Pratibha Permandla Michael Roberson Chandrasekhar Boyapati University

Related WorkRelated Work

Preventing Data Races and Deadlocks in Java• Flanagan and Freund (PLDI ’00)• Bacon, Strom, and Tarafdar (OOPSLA ’00)• Boyapati and Rinard (OOPSLA ’01)• Boyapati, Lee, Rinard (OOPSLA ’02)• Grossman (TLDI ’03)

Enforcing Encapsulation in Java• Clarke, Potter, and Noble (OOPSLA ’98)• Clarke and Drossopoulou (OOPSLA ’02)• Aldrich, Kostadinov, and Chambers (OOPSLA ’02)• Boyapati, Liskov, Shiria (POPL ’03)• Krishnaswamy and Aldrich (PLDI ’05)

Page 43: A Type System for Preventing Data Races and Deadlocks in the Java Virtual Machine Language Pratibha Permandla Michael Roberson Chandrasekhar Boyapati University

Related WorkRelated Work

Formalizing JVML• Freund and Mitchell (OOPSLA ’98)• Bertelsen (WPAM ’98)• Qian (FSSJ ’99)

Formalizing subroutines in JVML• Stata and Abadi (POPL ’98)• Callahan (POPL ’99)• Klein and Wildmoser (JAR ’03)

Tracking aliases in JVML• Laneve and Bigliardi (TIC ’00)• Iwama and Kobayashi (ASIA-PEPM ’02)

Page 44: A Type System for Preventing Data Races and Deadlocks in the Java Virtual Machine Language Pratibha Permandla Michael Roberson Chandrasekhar Boyapati University

A Type System for Preventing A Type System for Preventing Data Races and Deadlocks in the Data Races and Deadlocks in the Java Virtual Machine LanguageJava Virtual Machine Language

Pratibha Permandla

Michael Roberson

Chandrasekhar Boyapati

University of Michigan