An Abstract Machine for Asynchronous Programs with Closures and - - PowerPoint PPT Presentation

an abstract machine for asynchronous programs with
SMART_READER_LITE
LIVE PREVIEW

An Abstract Machine for Asynchronous Programs with Closures and - - PowerPoint PPT Presentation

An Abstract Machine for Asynchronous Programs with Closures and Priority Queues Giorgio Delzanno D. Ancona L. Franceschini M. Leotta E. Prampolini M. Ribaudo F. Ricca FUSTAQ Project 1 DIBRIS, University of Genoa 1 SEED 2016 project funded


slide-1
SLIDE 1

An Abstract Machine for Asynchronous Programs with Closures and Priority Queues

Giorgio Delzanno

  • D. Ancona
  • L. Franceschini
  • M. Leotta
  • E. Prampolini
  • M. Ribaudo
  • F. Ricca

FUSTAQ Project1

DIBRIS, University of Genoa

1SEED 2016 project funded by DIBRIS, University of Genova

  • G. Delzanno et al.

1 / 48

slide-2
SLIDE 2

Overview

1

Motivations and Goals

2

Abstract Machine for the Host Language

3

Abstract Machine for the Event Loop

4

Formal Reasoning: An Example

  • G. Delzanno et al.

2 / 48

slide-3
SLIDE 3

Motivations and Goals

Plan

1

Motivations and Goals

2

Abstract Machine for the Host Language

3

Abstract Machine for the Event Loop

4

Formal Reasoning: An Example

  • G. Delzanno et al.

3 / 48

slide-4
SLIDE 4

Motivations and Goals

Motivatios

Project on validation (testing, runtime verification, formal methods)

  • f IoT applications developed in Node.js ”Full Stack Quality of

Javascript of Anything” funded by our University Node.js is a JavaScript runtime system built on Chrome’s V8 JavaScript engine. Node.js uses an event-driven, non-blocking I/O model that makes it lightweight and efficient Node.js is becoming a standard for IoT applications (for both server- and client-side software)

  • G. Delzanno et al.

4 / 48

slide-5
SLIDE 5

Motivations and Goals

Motivatios

Project on validation (testing, runtime verification, formal methods)

  • f IoT applications developed in Node.js ”Full Stack Quality of

Javascript of Anything” funded by our University Node.js is a JavaScript runtime system built on Chrome’s V8 JavaScript engine. Node.js uses an event-driven, non-blocking I/O model that makes it lightweight and efficient Node.js is becoming a standard for IoT applications (for both server- and client-side software)

  • G. Delzanno et al.

4 / 48

slide-6
SLIDE 6

Motivations and Goals

Motivatios

Project on validation (testing, runtime verification, formal methods)

  • f IoT applications developed in Node.js ”Full Stack Quality of

Javascript of Anything” funded by our University Node.js is a JavaScript runtime system built on Chrome’s V8 JavaScript engine. Node.js uses an event-driven, non-blocking I/O model that makes it lightweight and efficient Node.js is becoming a standard for IoT applications (for both server- and client-side software)

  • G. Delzanno et al.

4 / 48

slide-7
SLIDE 7

Motivations and Goals

Motivatios

Project on validation (testing, runtime verification, formal methods)

  • f IoT applications developed in Node.js ”Full Stack Quality of

Javascript of Anything” funded by our University Node.js is a JavaScript runtime system built on Chrome’s V8 JavaScript engine. Node.js uses an event-driven, non-blocking I/O model that makes it lightweight and efficient Node.js is becoming a standard for IoT applications (for both server- and client-side software)

  • G. Delzanno et al.

4 / 48

slide-8
SLIDE 8

Motivations and Goals

  • G. Delzanno et al.

5 / 48

slide-9
SLIDE 9

Motivations and Goals

Node.js

var result = db.query(”SELECT...”); // use result

VS

db.query(”SELECT...”, function (result) // use result );

  • G. Delzanno et al.

6 / 48

slide-10
SLIDE 10

Motivations and Goals

Main Features of Node.js

Built on top of Javascript Asynchronous calls to avoid synchronization primitive such as locks Priority queues to model different types of events (input/output, delayed calls, etc); Continuation-style programming: callbacks with highest priority Closures to handle variables used in callbacks but declared in

  • utermost functions
  • G. Delzanno et al.

7 / 48

slide-11
SLIDE 11

Motivations and Goals

Main Features of Node.js

Built on top of Javascript Asynchronous calls to avoid synchronization primitive such as locks Priority queues to model different types of events (input/output, delayed calls, etc); Continuation-style programming: callbacks with highest priority Closures to handle variables used in callbacks but declared in

  • utermost functions
  • G. Delzanno et al.

7 / 48

slide-12
SLIDE 12

Motivations and Goals

Main Features of Node.js

Built on top of Javascript Asynchronous calls to avoid synchronization primitive such as locks Priority queues to model different types of events (input/output, delayed calls, etc); Continuation-style programming: callbacks with highest priority Closures to handle variables used in callbacks but declared in

  • utermost functions
  • G. Delzanno et al.

7 / 48

slide-13
SLIDE 13

Motivations and Goals

Main Features of Node.js

Built on top of Javascript Asynchronous calls to avoid synchronization primitive such as locks Priority queues to model different types of events (input/output, delayed calls, etc); Continuation-style programming: callbacks with highest priority Closures to handle variables used in callbacks but declared in

  • utermost functions
  • G. Delzanno et al.

7 / 48

slide-14
SLIDE 14

Motivations and Goals

Main Features of Node.js

Built on top of Javascript Asynchronous calls to avoid synchronization primitive such as locks Priority queues to model different types of events (input/output, delayed calls, etc); Continuation-style programming: callbacks with highest priority Closures to handle variables used in callbacks but declared in

  • utermost functions
  • G. Delzanno et al.

7 / 48

slide-15
SLIDE 15

Motivations and Goals

Emitter

var EventEmitter = require(’events’); var Emitter = new EventEmitter(); var msg = function msg() { console.log(’ok’); } Emitter.on(’evt1’, msg); Emitter.emit(’evt1’); while (true);

  • G. Delzanno et al.

8 / 48

slide-16
SLIDE 16

Motivations and Goals

Emitter + Setimmediate

var EventEmitter = require(’events’); var Emitter = new EventEmitter(); var msg = function msg() { console.log(’ok’); } Emitter.on(’evt1’, function () { setImmediate(msg); }); Emitter.emit(’evt1’); while (true);

  • G. Delzanno et al.

9 / 48

slide-17
SLIDE 17

Motivations and Goals

Closures and callbacks

function test(){ var d = 5; var foo = function(){ d = 10; } process.nextTick(foo); setImmediate(() => { console.log(d) }) } test();

  • G. Delzanno et al.

10 / 48

slide-18
SLIDE 18

Motivations and Goals

Informal semantics

test is called synchronously foo is delayed till the end of main (closure is stored in the heap) console.log(d) is postponed to the next tick (closure stored in the heap) when the main terminates foo updates d in the next loop tick console.log prints the updated value 10

  • G. Delzanno et al.

11 / 48

slide-19
SLIDE 19

Motivations and Goals

Informal semantics

test is called synchronously foo is delayed till the end of main (closure is stored in the heap) console.log(d) is postponed to the next tick (closure stored in the heap) when the main terminates foo updates d in the next loop tick console.log prints the updated value 10

  • G. Delzanno et al.

11 / 48

slide-20
SLIDE 20

Motivations and Goals

Informal semantics

test is called synchronously foo is delayed till the end of main (closure is stored in the heap) console.log(d) is postponed to the next tick (closure stored in the heap) when the main terminates foo updates d in the next loop tick console.log prints the updated value 10

  • G. Delzanno et al.

11 / 48

slide-21
SLIDE 21

Motivations and Goals

Informal semantics

test is called synchronously foo is delayed till the end of main (closure is stored in the heap) console.log(d) is postponed to the next tick (closure stored in the heap) when the main terminates foo updates d in the next loop tick console.log prints the updated value 10

  • G. Delzanno et al.

11 / 48

slide-22
SLIDE 22

Motivations and Goals

Informal semantics

test is called synchronously foo is delayed till the end of main (closure is stored in the heap) console.log(d) is postponed to the next tick (closure stored in the heap) when the main terminates foo updates d in the next loop tick console.log prints the updated value 10

  • G. Delzanno et al.

11 / 48

slide-23
SLIDE 23

Motivations and Goals

For Devs Only?

Dev documentation is not clear at all Program semantics can be very hard to understand Non-determinism due to possible reorderings of events and delay of asynchronous operations Program transformations and design patterns are often used to simplify Node.js programs Formal semantics/reasoning to increase software quality!

  • G. Delzanno et al.

12 / 48

slide-24
SLIDE 24

Motivations and Goals

For Devs Only?

Dev documentation is not clear at all Program semantics can be very hard to understand Non-determinism due to possible reorderings of events and delay of asynchronous operations Program transformations and design patterns are often used to simplify Node.js programs Formal semantics/reasoning to increase software quality!

  • G. Delzanno et al.

12 / 48

slide-25
SLIDE 25

Motivations and Goals

For Devs Only?

Dev documentation is not clear at all Program semantics can be very hard to understand Non-determinism due to possible reorderings of events and delay of asynchronous operations Program transformations and design patterns are often used to simplify Node.js programs Formal semantics/reasoning to increase software quality!

  • G. Delzanno et al.

12 / 48

slide-26
SLIDE 26

Motivations and Goals

For Devs Only?

Dev documentation is not clear at all Program semantics can be very hard to understand Non-determinism due to possible reorderings of events and delay of asynchronous operations Program transformations and design patterns are often used to simplify Node.js programs Formal semantics/reasoning to increase software quality!

  • G. Delzanno et al.

12 / 48

slide-27
SLIDE 27

Motivations and Goals

For Devs Only?

Dev documentation is not clear at all Program semantics can be very hard to understand Non-determinism due to possible reorderings of events and delay of asynchronous operations Program transformations and design patterns are often used to simplify Node.js programs Formal semantics/reasoning to increase software quality!

  • G. Delzanno et al.

12 / 48

slide-28
SLIDE 28

Motivations and Goals

Our Proposal

An Abstract Machine to describe the semantics of Asynchronous Programs with Priority Queues and Closures inspired to Node.js Built in two steps:

Host language with callback definitions and closures Abstract machine (parametric on the pperational semantics of the host language) to describe event loop, continuations and callbacks with priorities

Closures, the bridge between the two layers, are modeled via a shared heap Meta-interpreter built in Prolog to reason about all possible program executions (non determinism due to event triggering and termination

  • f asynchronous operations)
  • G. Delzanno et al.

13 / 48

slide-29
SLIDE 29

Motivations and Goals

Our Proposal

An Abstract Machine to describe the semantics of Asynchronous Programs with Priority Queues and Closures inspired to Node.js Built in two steps:

Host language with callback definitions and closures Abstract machine (parametric on the pperational semantics of the host language) to describe event loop, continuations and callbacks with priorities

Closures, the bridge between the two layers, are modeled via a shared heap Meta-interpreter built in Prolog to reason about all possible program executions (non determinism due to event triggering and termination

  • f asynchronous operations)
  • G. Delzanno et al.

13 / 48

slide-30
SLIDE 30

Motivations and Goals

Our Proposal

An Abstract Machine to describe the semantics of Asynchronous Programs with Priority Queues and Closures inspired to Node.js Built in two steps:

Host language with callback definitions and closures Abstract machine (parametric on the pperational semantics of the host language) to describe event loop, continuations and callbacks with priorities

Closures, the bridge between the two layers, are modeled via a shared heap Meta-interpreter built in Prolog to reason about all possible program executions (non determinism due to event triggering and termination

  • f asynchronous operations)
  • G. Delzanno et al.

13 / 48

slide-31
SLIDE 31

Motivations and Goals

Our Proposal

An Abstract Machine to describe the semantics of Asynchronous Programs with Priority Queues and Closures inspired to Node.js Built in two steps:

Host language with callback definitions and closures Abstract machine (parametric on the pperational semantics of the host language) to describe event loop, continuations and callbacks with priorities

Closures, the bridge between the two layers, are modeled via a shared heap Meta-interpreter built in Prolog to reason about all possible program executions (non determinism due to event triggering and termination

  • f asynchronous operations)
  • G. Delzanno et al.

13 / 48

slide-32
SLIDE 32

Motivations and Goals

Our Proposal

An Abstract Machine to describe the semantics of Asynchronous Programs with Priority Queues and Closures inspired to Node.js Built in two steps:

Host language with callback definitions and closures Abstract machine (parametric on the pperational semantics of the host language) to describe event loop, continuations and callbacks with priorities

Closures, the bridge between the two layers, are modeled via a shared heap Meta-interpreter built in Prolog to reason about all possible program executions (non determinism due to event triggering and termination

  • f asynchronous operations)
  • G. Delzanno et al.

13 / 48

slide-33
SLIDE 33

Motivations and Goals

Our Proposal

An Abstract Machine to describe the semantics of Asynchronous Programs with Priority Queues and Closures inspired to Node.js Built in two steps:

Host language with callback definitions and closures Abstract machine (parametric on the pperational semantics of the host language) to describe event loop, continuations and callbacks with priorities

Closures, the bridge between the two layers, are modeled via a shared heap Meta-interpreter built in Prolog to reason about all possible program executions (non determinism due to event triggering and termination

  • f asynchronous operations)
  • G. Delzanno et al.

13 / 48

slide-34
SLIDE 34

Abstract Machine for the Host Language

Plan

1

Motivations and Goals

2

Abstract Machine for the Host Language

3

Abstract Machine for the Event Loop

4

Formal Reasoning: An Example

  • G. Delzanno et al.

14 / 48

slide-35
SLIDE 35

Abstract Machine for the Host Language

Host Language

We introduce a host (imperative) language L defined as follows F is a set of function names. Var is a set of variables (it also contains function names in F) Callback is the set of (anonymous) callback definitions of the form λ x.s, where x ∈ Vark are formal parameters and s is a list of statements Val contains primitive values and closures

  • G. Delzanno et al.

15 / 48

slide-36
SLIDE 36

Abstract Machine for the Host Language

Host Language

We introduce a host (imperative) language L defined as follows F is a set of function names. Var is a set of variables (it also contains function names in F) Callback is the set of (anonymous) callback definitions of the form λ x.s, where x ∈ Vark are formal parameters and s is a list of statements Val contains primitive values and closures

  • G. Delzanno et al.

15 / 48

slide-37
SLIDE 37

Abstract Machine for the Host Language

Host Language

We introduce a host (imperative) language L defined as follows F is a set of function names. Var is a set of variables (it also contains function names in F) Callback is the set of (anonymous) callback definitions of the form λ x.s, where x ∈ Vark are formal parameters and s is a list of statements Val contains primitive values and closures

  • G. Delzanno et al.

15 / 48

slide-38
SLIDE 38

Abstract Machine for the Host Language

Host Language

We introduce a host (imperative) language L defined as follows F is a set of function names. Var is a set of variables (it also contains function names in F) Callback is the set of (anonymous) callback definitions of the form λ x.s, where x ∈ Vark are formal parameters and s is a list of statements Val contains primitive values and closures

  • G. Delzanno et al.

15 / 48

slide-39
SLIDE 39

Abstract Machine for the Host Language

Programs

Let B be a finite sequence of instructions in Stmts∗ let x = e in B where x is a local variable, e an expression denoting a primitive value, let f1 = λ y1.P1, . . . , fk = λ yk.Pk in B where P1, . . . , Pk are program expressions, they may contain let declarations to model nested callback declarations Example P = let f = (let (cb = λx. obs(x)) in call(read, cb) · f ) in f ()

  • G. Delzanno et al.

16 / 48

slide-40
SLIDE 40

Abstract Machine for the Host Language

Programs

Let B be a finite sequence of instructions in Stmts∗ let x = e in B where x is a local variable, e an expression denoting a primitive value, let f1 = λ y1.P1, . . . , fk = λ yk.Pk in B where P1, . . . , Pk are program expressions, they may contain let declarations to model nested callback declarations Example P = let f = (let (cb = λx. obs(x)) in call(read, cb) · f ) in f ()

  • G. Delzanno et al.

16 / 48

slide-41
SLIDE 41

Abstract Machine for the Host Language

Programs

Let B be a finite sequence of instructions in Stmts∗ let x = e in B where x is a local variable, e an expression denoting a primitive value, let f1 = λ y1.P1, . . . , fk = λ yk.Pk in B where P1, . . . , Pk are program expressions, they may contain let declarations to model nested callback declarations Example P = let f = (let (cb = λx. obs(x)) in call(read, cb) · f ) in f ()

  • G. Delzanno et al.

16 / 48

slide-42
SLIDE 42

Abstract Machine for the Host Language

(Lightweight) Instruction Set

  • bs(e) to observe a certain event (a value)

store(x, e) to store a value (the evaluation of e) in the global or local variable x. We use the expression any to denote a value non deterministically selected from the set of values. f ( e) to synchronously invoke a callback f with the vector of parameters

  • e. Actual parameters are global or local variables.
  • G. Delzanno et al.

17 / 48

slide-43
SLIDE 43

Abstract Machine for the Host Language

(Lightweight) Instruction Set

  • bs(e) to observe a certain event (a value)

store(x, e) to store a value (the evaluation of e) in the global or local variable x. We use the expression any to denote a value non deterministically selected from the set of values. f ( e) to synchronously invoke a callback f with the vector of parameters

  • e. Actual parameters are global or local variables.
  • G. Delzanno et al.

17 / 48

slide-44
SLIDE 44

Abstract Machine for the Host Language

(Lightweight) Instruction Set

  • bs(e) to observe a certain event (a value)

store(x, e) to store a value (the evaluation of e) in the global or local variable x. We use the expression any to denote a value non deterministically selected from the set of values. f ( e) to synchronously invoke a callback f with the vector of parameters

  • e. Actual parameters are global or local variables.
  • G. Delzanno et al.

17 / 48

slide-45
SLIDE 45

Abstract Machine for the Host Language

Semantic Domains

Env = [Vars → Loc] Closures = Env × Callback Val contains primitive values and closures Heap = [Loc → Val] Frames = Env × Stmts∗

  • G. Delzanno et al.

18 / 48

slide-46
SLIDE 46

Abstract Machine for the Host Language

Semantic Domains

Env = [Vars → Loc] Closures = Env × Callback Val contains primitive values and closures Heap = [Loc → Val] Frames = Env × Stmts∗

  • G. Delzanno et al.

18 / 48

slide-47
SLIDE 47

Abstract Machine for the Host Language

Semantic Domains

Env = [Vars → Loc] Closures = Env × Callback Val contains primitive values and closures Heap = [Loc → Val] Frames = Env × Stmts∗

  • G. Delzanno et al.

18 / 48

slide-48
SLIDE 48

Abstract Machine for the Host Language

Semantic Domains

Env = [Vars → Loc] Closures = Env × Callback Val contains primitive values and closures Heap = [Loc → Val] Frames = Env × Stmts∗

  • G. Delzanno et al.

18 / 48

slide-49
SLIDE 49

Abstract Machine for the Host Language

Semantic Domains

Env = [Vars → Loc] Closures = Env × Callback Val contains primitive values and closures Heap = [Loc → Val] Frames = Env × Stmts∗

  • G. Delzanno et al.

18 / 48

slide-50
SLIDE 50

Abstract Machine for the Host Language

Configurations

G, H, S, where G ∈ Env, H is the global heap, S ∈ Frame∗, i.e., S = ℓ1, S1 . . . ℓn, Sn for i : 1, . . . , n and represents the call stack. In a pair ℓ, w, ℓ is the local environment and w is the corresponding program to be executed.

  • G. Delzanno et al.

19 / 48

slide-51
SLIDE 51

Abstract Machine for the Host Language

Configurations

G, H, S, where G ∈ Env, H is the global heap, S ∈ Frame∗, i.e., S = ℓ1, S1 . . . ℓn, Sn for i : 1, . . . , n and represents the call stack. In a pair ℓ, w, ℓ is the local environment and w is the corresponding program to be executed.

  • G. Delzanno et al.

19 / 48

slide-52
SLIDE 52

Abstract Machine for the Host Language

Configurations

G, H, S, where G ∈ Env, H is the global heap, S ∈ Frame∗, i.e., S = ℓ1, S1 . . . ℓn, Sn for i : 1, . . . , n and represents the call stack. In a pair ℓ, w, ℓ is the local environment and w is the corresponding program to be executed.

  • G. Delzanno et al.

19 / 48

slide-53
SLIDE 53

Abstract Machine for the Host Language

Let Declarations

ℓ′ = ℓ[x/l], lH(e) = v, H′ = H[l/v], l ∈ dom(H) G, H, ℓ, let x = e in B · S →L G, H′, ℓ′, B · S ℓH combines local environment ℓ and heap H

  • G. Delzanno et al.

20 / 48

slide-54
SLIDE 54

Abstract Machine for the Host Language

Let Declarations

ℓ′ = ℓ[f1/l1, . . . , fk/lk], H′ = H[l1/ℓ, λ x1.P1, . . . , lk/ℓ, λ xk.Pk] li ∈ dom(H), li = lj, for i, j : 1, . . . , k, i = j G, H, ℓ, let f1 = λ x1.P1, . . . , fk = λ xk.Pk in B · S →L G, H′, ℓ′, B · S

We adopt static binding as in Javascript We use locations to access variables declared in outermost scopes (an environment is an ordered lists of substitutions)

  • G. Delzanno et al.

21 / 48

slide-55
SLIDE 55

Abstract Machine for the Host Language

Observations

G, H, ℓ, obs(e) · B · S →

  • ℓH(e)

L

G, H, ℓ, B · S

  • G. Delzanno et al.

22 / 48

slide-56
SLIDE 56

Abstract Machine for the Host Language

Store on Global Variables

x ∈ dom(ℓ) G · ℓH(e) = w = λ y.e G, H, ℓ, store(x, e) · B · S →L G[x/w], H, ℓ, B · S

  • G. Delzanno et al.

23 / 48

slide-57
SLIDE 57

Abstract Machine for the Host Language

Store on Local Variables

x ∈ dom(ℓ) ℓH(e) = w = λ y.e ℓ(x) = l G, H, ℓ, store(x, e) · B · S →L G, H[l/w], ℓ, B · S

  • G. Delzanno et al.

24 / 48

slide-58
SLIDE 58

Abstract Machine for the Host Language

Synchronous call

ℓH(f ) = ℓ′, λ y.u, G · (ℓH) · (ℓ′

H)(

v) = v ′, H′ = H[ l/ v ′], ℓ′′ = ℓ[ y/ l], for l = l1, . . . , lk, li ∈ dom(H), li = lj, for i, j : 1, . . . , k, i = j G, H, ℓ, f ( v) · B · S →L G, H′, ℓ′′, u · ℓ, B · S

  • G. Delzanno et al.

25 / 48

slide-59
SLIDE 59

Abstract Machine for the Host Language

Absorbtion

G, H, ℓ, ǫ · S →L G, H, S

  • G. Delzanno et al.

26 / 48

slide-60
SLIDE 60

Abstract Machine for the Event Loop

Plan

1

Motivations and Goals

2

Abstract Machine for the Host Language

3

Abstract Machine for the Event Loop

4

Formal Reasoning: An Example

  • G. Delzanno et al.

27 / 48

slide-61
SLIDE 61

Abstract Machine for the Event Loop

Additional Instructions

reg(e, u): registers callbacks in the word (list) w ∈ F ∗ for event e, we use a list since we the callbacks must be processed in order. call(op, cb): invokes an asynchronous operation op and registers the callback cb to be executed upon its termination. We assume here that the operation generates a vector of input values that are passed, upon termination of op, to the callback cb. nexttick(f , v): enqueues the call to f with parameters v in the nextTick queue. setimmediate(f , v): postpones the call to function f with parameters

  • v to the next tick of the event loop.

trigger(e, v): generates event e ∈ Eventsi (pushing callbacks in the poll queue) with actual parameters v. unreg(e, P): unregisters all callbacks in the set P ∈ P(F) for event e,

  • G. Delzanno et al.

28 / 48

slide-62
SLIDE 62

Abstract Machine for the Event Loop

Additional Instructions

reg(e, u): registers callbacks in the word (list) w ∈ F ∗ for event e, we use a list since we the callbacks must be processed in order. call(op, cb): invokes an asynchronous operation op and registers the callback cb to be executed upon its termination. We assume here that the operation generates a vector of input values that are passed, upon termination of op, to the callback cb. nexttick(f , v): enqueues the call to f with parameters v in the nextTick queue. setimmediate(f , v): postpones the call to function f with parameters

  • v to the next tick of the event loop.

trigger(e, v): generates event e ∈ Eventsi (pushing callbacks in the poll queue) with actual parameters v. unreg(e, P): unregisters all callbacks in the set P ∈ P(F) for event e,

  • G. Delzanno et al.

28 / 48

slide-63
SLIDE 63

Abstract Machine for the Event Loop

Additional Instructions

reg(e, u): registers callbacks in the word (list) w ∈ F ∗ for event e, we use a list since we the callbacks must be processed in order. call(op, cb): invokes an asynchronous operation op and registers the callback cb to be executed upon its termination. We assume here that the operation generates a vector of input values that are passed, upon termination of op, to the callback cb. nexttick(f , v): enqueues the call to f with parameters v in the nextTick queue. setimmediate(f , v): postpones the call to function f with parameters

  • v to the next tick of the event loop.

trigger(e, v): generates event e ∈ Eventsi (pushing callbacks in the poll queue) with actual parameters v. unreg(e, P): unregisters all callbacks in the set P ∈ P(F) for event e,

  • G. Delzanno et al.

28 / 48

slide-64
SLIDE 64

Abstract Machine for the Event Loop

Additional Instructions

reg(e, u): registers callbacks in the word (list) w ∈ F ∗ for event e, we use a list since we the callbacks must be processed in order. call(op, cb): invokes an asynchronous operation op and registers the callback cb to be executed upon its termination. We assume here that the operation generates a vector of input values that are passed, upon termination of op, to the callback cb. nexttick(f , v): enqueues the call to f with parameters v in the nextTick queue. setimmediate(f , v): postpones the call to function f with parameters

  • v to the next tick of the event loop.

trigger(e, v): generates event e ∈ Eventsi (pushing callbacks in the poll queue) with actual parameters v. unreg(e, P): unregisters all callbacks in the set P ∈ P(F) for event e,

  • G. Delzanno et al.

28 / 48

slide-65
SLIDE 65

Abstract Machine for the Event Loop

Additional Instructions

reg(e, u): registers callbacks in the word (list) w ∈ F ∗ for event e, we use a list since we the callbacks must be processed in order. call(op, cb): invokes an asynchronous operation op and registers the callback cb to be executed upon its termination. We assume here that the operation generates a vector of input values that are passed, upon termination of op, to the callback cb. nexttick(f , v): enqueues the call to f with parameters v in the nextTick queue. setimmediate(f , v): postpones the call to function f with parameters

  • v to the next tick of the event loop.

trigger(e, v): generates event e ∈ Eventsi (pushing callbacks in the poll queue) with actual parameters v. unreg(e, P): unregisters all callbacks in the set P ∈ P(F) for event e,

  • G. Delzanno et al.

28 / 48

slide-66
SLIDE 66

Abstract Machine for the Event Loop

Additional Instructions

reg(e, u): registers callbacks in the word (list) w ∈ F ∗ for event e, we use a list since we the callbacks must be processed in order. call(op, cb): invokes an asynchronous operation op and registers the callback cb to be executed upon its termination. We assume here that the operation generates a vector of input values that are passed, upon termination of op, to the callback cb. nexttick(f , v): enqueues the call to f with parameters v in the nextTick queue. setimmediate(f , v): postpones the call to function f with parameters

  • v to the next tick of the event loop.

trigger(e, v): generates event e ∈ Eventsi (pushing callbacks in the poll queue) with actual parameters v. unreg(e, P): unregisters all callbacks in the set P ∈ P(F) for event e,

  • G. Delzanno et al.

28 / 48

slide-67
SLIDE 67

Abstract Machine for the Event Loop

Additional Notation

We now introduce an abstract machine to describes the semantics of the “event loop”’ Events = Eventsi ∪ Eventse is a finite set of (internal/external) event labels CallF is the set of callback calls {f ( v)|f ∈ F, v ∈ Valk, k ≥ 0}. CallA is the set of asynchronous calls {call(a, cb)|a ∈ A, cb ∈ F}, where A is a set of labels.

  • G. Delzanno et al.

29 / 48

slide-68
SLIDE 68

Abstract Machine for the Event Loop

Additional Notation

We now introduce an abstract machine to describes the semantics of the “event loop”’ Events = Eventsi ∪ Eventse is a finite set of (internal/external) event labels CallF is the set of callback calls {f ( v)|f ∈ F, v ∈ Valk, k ≥ 0}. CallA is the set of asynchronous calls {call(a, cb)|a ∈ A, cb ∈ F}, where A is a set of labels.

  • G. Delzanno et al.

29 / 48

slide-69
SLIDE 69

Abstract Machine for the Event Loop

Additional Notation

We now introduce an abstract machine to describes the semantics of the “event loop”’ Events = Eventsi ∪ Eventse is a finite set of (internal/external) event labels CallF is the set of callback calls {f ( v)|f ∈ F, v ∈ Valk, k ≥ 0}. CallA is the set of asynchronous calls {call(a, cb)|a ∈ A, cb ∈ F}, where A is a set of labels.

  • G. Delzanno et al.

29 / 48

slide-70
SLIDE 70

Abstract Machine for the Event Loop

Event Loop Configurations

A configuration is a tuple G, H, E, S, C, Q, P, R, where G ∈ Env, H ∈ Heap, E ∈ Listener, S ∈ Frame∗, C, Q, P ∈ (Env × CallF)∗, R ∈ (Env × CallA)⊗.

  • G. Delzanno et al.

30 / 48

slide-71
SLIDE 71

Abstract Machine for the Event Loop

Event Loop Configurations

A configuration is a tuple G, H, E, S, C, Q, P, R, where G ∈ Env, H ∈ Heap, E ∈ Listener, S ∈ Frame∗, C, Q, P ∈ (Env × CallF)∗, R ∈ (Env × CallA)⊗.

  • G. Delzanno et al.

30 / 48

slide-72
SLIDE 72

Abstract Machine for the Event Loop

Event Loop Configurations

A configuration is a tuple G, H, E, S, C, Q, P, R, where G ∈ Env, H ∈ Heap, E ∈ Listener, S ∈ Frame∗, C, Q, P ∈ (Env × CallF)∗, R ∈ (Env × CallA)⊗.

  • G. Delzanno et al.

30 / 48

slide-73
SLIDE 73

Abstract Machine for the Event Loop

Event Loop Configurations

A configuration is a tuple G, H, E, S, C, Q, P, R, where G ∈ Env, H ∈ Heap, E ∈ Listener, S ∈ Frame∗, C, Q, P ∈ (Env × CallF)∗, R ∈ (Env × CallA)⊗.

  • G. Delzanno et al.

30 / 48

slide-74
SLIDE 74

Abstract Machine for the Event Loop

Event Loop Configurations

A configuration is a tuple G, H, E, S, C, Q, P, R, where G ∈ Env, H ∈ Heap, E ∈ Listener, S ∈ Frame∗, C, Q, P ∈ (Env × CallF)∗, R ∈ (Env × CallA)⊗.

  • G. Delzanno et al.

30 / 48

slide-75
SLIDE 75

Abstract Machine for the Event Loop

Event Loop Configurations

A configuration is a tuple G, H, E, S, C, Q, P, R, where G ∈ Env, H ∈ Heap, E ∈ Listener, S ∈ Frame∗, C, Q, P ∈ (Env × CallF)∗, R ∈ (Env × CallA)⊗.

  • G. Delzanno et al.

30 / 48

slide-76
SLIDE 76

Abstract Machine for the Event Loop

Event Loop Configurations

A configuration is a tuple G, H, E, S, C, Q, P, R, . . . C is the (nexttick) queue of pending callback invocations generated by nexttick. Q is the (poll) queue of pending callback invocations generated by trigger and by external events. P is the (setimmediate) queue of pending callback invocations generated by setimmediate. R models the thread pool executing asynchronous operations Local environments are used to evaluate variables defined in the body

  • f a callback at the moment of registration, synchronous or

asynchronous invocation.

  • G. Delzanno et al.

31 / 48

slide-77
SLIDE 77

Abstract Machine for the Event Loop

Event Loop Configurations

A configuration is a tuple G, H, E, S, C, Q, P, R, . . . C is the (nexttick) queue of pending callback invocations generated by nexttick. Q is the (poll) queue of pending callback invocations generated by trigger and by external events. P is the (setimmediate) queue of pending callback invocations generated by setimmediate. R models the thread pool executing asynchronous operations Local environments are used to evaluate variables defined in the body

  • f a callback at the moment of registration, synchronous or

asynchronous invocation.

  • G. Delzanno et al.

31 / 48

slide-78
SLIDE 78

Abstract Machine for the Event Loop

Event Loop Configurations

A configuration is a tuple G, H, E, S, C, Q, P, R, . . . C is the (nexttick) queue of pending callback invocations generated by nexttick. Q is the (poll) queue of pending callback invocations generated by trigger and by external events. P is the (setimmediate) queue of pending callback invocations generated by setimmediate. R models the thread pool executing asynchronous operations Local environments are used to evaluate variables defined in the body

  • f a callback at the moment of registration, synchronous or

asynchronous invocation.

  • G. Delzanno et al.

31 / 48

slide-79
SLIDE 79

Abstract Machine for the Event Loop

Event Loop Configurations

A configuration is a tuple G, H, E, S, C, Q, P, R, . . . C is the (nexttick) queue of pending callback invocations generated by nexttick. Q is the (poll) queue of pending callback invocations generated by trigger and by external events. P is the (setimmediate) queue of pending callback invocations generated by setimmediate. R models the thread pool executing asynchronous operations Local environments are used to evaluate variables defined in the body

  • f a callback at the moment of registration, synchronous or

asynchronous invocation.

  • G. Delzanno et al.

31 / 48

slide-80
SLIDE 80

Abstract Machine for the Event Loop

Event Loop Configurations

A configuration is a tuple G, H, E, S, C, Q, P, R, . . . C is the (nexttick) queue of pending callback invocations generated by nexttick. Q is the (poll) queue of pending callback invocations generated by trigger and by external events. P is the (setimmediate) queue of pending callback invocations generated by setimmediate. R models the thread pool executing asynchronous operations Local environments are used to evaluate variables defined in the body

  • f a callback at the moment of registration, synchronous or

asynchronous invocation.

  • G. Delzanno et al.

31 / 48

slide-81
SLIDE 81

Abstract Machine for the Event Loop

Transitions in the Host Language

G, H, S →α

L G ′, H′, S′

G, H, E, S, C, Q, P, R →α G ′, H′, E, S′, C, Q, P, R

  • G. Delzanno et al.

32 / 48

slide-82
SLIDE 82

Abstract Machine for the Event Loop

Callback Registration

E ′ = E[evt/(E(evt) · ℓ, u)] G, H, E, ℓ, reg(evt, u) · w · S, C, Q, P, R → G, H, E ′, ℓ, w · S, C, Q, P, R

  • G. Delzanno et al.

33 / 48

slide-83
SLIDE 83

Abstract Machine for the Event Loop

Registration Cancelation

E ′ = E[evt/(E(evt) ⊖ u)] G, H, E, ℓ, unreg(evt, u) · w · S, C, Q, P, R → G, H, E ′, ℓ, w · S, C, Q, P, R

  • G. Delzanno et al.

34 / 48

slide-84
SLIDE 84

Abstract Machine for the Event Loop

Event Triggering

evt ∈ Eventsi E(evt) = ℓ1, u1 . . . ℓm, um ui = pi

1 · . . . · pi ki for i : 1, . . . , m

r = ℓ1, p1

1(

v) · . . . · ℓ1, p1

k1(

v) . . . ℓm, pm

1 (

v) · . . . · ℓm, pm

km(

v) v ∈ Valk G, H, E, ℓ, trigger(evt, v) · w · S, C, Q, P, R → G, H, E, ℓ, w · S, C, Q · r, P, R

  • G. Delzanno et al.

35 / 48

slide-85
SLIDE 85

Abstract Machine for the Event Loop

Asynchronous Call

R′ = R ⊕ {ℓ, call(a, cb)} G, H, E, ℓ, call(a, cb) · w · S, C, Q, P, R → G, H, E, ℓ, w · S, C, Q, P, R′

  • G. Delzanno et al.

36 / 48

slide-86
SLIDE 86

Abstract Machine for the Event Loop

Termination of Async. Call

u = ℓ, cb( v)

  • v ∈ Valk

R′ = R \ {ℓ, call(a, cb)} G, H, E, S, C, Q, P, R → G, H, E, S, C, Q · u, P, R′

  • G. Delzanno et al.

37 / 48

slide-87
SLIDE 87

Abstract Machine for the Event Loop

External Event Triggering

evt ∈ Eventse E(evt) = ℓ1, u1 . . . ℓm, um ui = pi

1 · . . . · pi ki for i : 1, . . . , m

r = ℓ1, p1

1(

v) · . . . · ℓ1, p1

k1(

v) . . . ℓm, pm

1 (

v) · . . . · ℓm, pm

km(

v) v ∈ Valk G, H, E, S, C, Q, P, R → G, H, E, S, C, Q · r, P, R

  • G. Delzanno et al.

38 / 48

slide-88
SLIDE 88

Abstract Machine for the Event Loop

Nexttick

G · ℓH( v) = v ′ G, H, E, ℓ, nextT(f , v) · w · S, C, Q, P, R → G, H, E, ℓ, w · S, C · ℓ, f ( v ′), Q, P, R

  • G. Delzanno et al.

39 / 48

slide-89
SLIDE 89

Abstract Machine for the Event Loop

Setimmediate

G · ℓH( v) = v ′ G, H, E, ℓ, setI(f , v) · w · S, C, Q, P, R → G, H, E, ℓ, w · S, C, Q, P · ℓ, f ( v ′), R

  • G. Delzanno et al.

40 / 48

slide-90
SLIDE 90

Abstract Machine for the Event Loop

Selection from Nexttick Queue

ℓH(p) = ℓ′, λ y.s, G · (ℓH) · (ℓ′

H)(

v) = v ′, H′ = H[ l/ v ′], ℓ′′ = ℓ[ y/ l], for l = l1, . . . , lk, li ∈ dom(H), li = lj, for i, j : 1, . . . , k, i = j G, H, E, ⊥, ℓ, p( v) · C, Q, P, R → G, H′, E, ℓ′, s, C, Q, P, R

  • G. Delzanno et al.

41 / 48

slide-91
SLIDE 91

Abstract Machine for the Event Loop

Selection from Poll Queue

ℓH(f ) = ℓ′, λ y.s, G · (ℓH) · (ℓ′

H)(

v) = v ′, H′ = H[ l/ v ′], ℓ′′ = ℓ[ y/ l], for l = l1, . . . , lk, li ∈ dom(H), li = lj, for i, j : 1, . . . , k, i = j G, H, E, ⊥, ǫ, p( v) · Q, P, R → G, H′, E, ℓ′, s, ǫ, Q, P, R

  • G. Delzanno et al.

42 / 48

slide-92
SLIDE 92

Abstract Machine for the Event Loop

Selection from Pending Queue

G, H, E, ⊥, ǫ, ǫ, P, R → G, H, E, ⊥, ǫ, P, ǫ, R

  • G. Delzanno et al.

43 / 48

slide-93
SLIDE 93

Formal Reasoning: An Example

Plan

1

Motivations and Goals

2

Abstract Machine for the Host Language

3

Abstract Machine for the Event Loop

4

Formal Reasoning: An Example

  • G. Delzanno et al.

44 / 48

slide-94
SLIDE 94

Formal Reasoning: An Example

Simple Node Example

var fs = require(’fs’); fs.readFile(’input.txt’, function cb (data) { console.log(data.toString()); }); console.log(’Program Ended’);

  • G. Delzanno et al.

45 / 48

slide-95
SLIDE 95

Formal Reasoning: An Example

S = call(readFile, cb) · log(...) Q = ǫ R = ∅ ↓ S = log(...) Q = ǫ R = { {cb} } ւ ց S = log(...) Q = cb, v R = ∅ S = ⊥ Q = ǫ R = { {cb} } ց ւ S = ⊥ Q = cb, v R = ∅ ↓ S = log(...) Q = ǫ R = ∅ ↓ S = ⊥ Q = ǫ R = ∅

cb = λdata.log(data)

  • G. Delzanno et al.

46 / 48

slide-96
SLIDE 96

Formal Reasoning: An Example

Conclusions

Event-driven programs have a non-deterministic behavior: difficult to program and to verify The abstract machine can be used to understand the behavior, apply analysis and verification techniques Starting from this model: Js promises, bounded model checking, decidable fragments (?) Tools like Loupe2 can be written

2latentflip.com/loupe/

  • G. Delzanno et al.

47 / 48

slide-97
SLIDE 97

References

  • M. Emmi, P. Ganty, R. Majumdar and F. Rosa-Velardo

Analysis of Asynchronous Programs with Event-Based Synchronization Springer-Verlag Berlin Heidelberg 2015

  • J. Vitek (Ed.): ESOP 2015, LNCS 9032, pp. 535–559, 2015
  • G. Geeraerts, A. Heußner, J.-F. Raskin

On the Verification of Concurrent, Asynchronous Programs with Waiting Queues ACM Trans. Embedd. Comput. Syst. 0, 0, Article 0 (2013), 25 pages.

  • S. Alimadadi et al.

Understanding JavaScript Event-Based Interactions with Clematis ACM Trans. Softw. Eng. Methodol. 2015

  • P. Roberts, What the heck is the event loop anyway?

JSConf EU 2014 (youtube video) https://nodejs.org/en/docs/

  • G. Delzanno et al.

48 / 48