P: Safe Asynchronous Event-Driven Programming

P: Safe Asynchronous Event-Driven Programming

P: Safe Asynchronous Event-Driven Programming Ankush Desai Vivek Gupta Damien Zufferey Ethan Jackson Shaz Qadeer IST Austria Sriram Rajamani Microsoft Abstract 1. Introduction We describe the design and implementation of P, a domain-specific Asynchronous systems code that is both performant and correct is language to write asynchronous event driven code. P allows the hard to write. Engineers typically design asynchronous code us- programmer to specify the system as a collection of interacting ing state machine notations, use modeling and verification tools to state machines, which communicate with each other using events. make sure that they have covered corner cases, and then implement P unifies modeling and programming into one activity for the the design in a language like C. They use a variety of performance programmer. Not only can a P program be compiled into executable tricks, as a result of which the structure of the state machines is code, but it can also be tested using model checking techniques. P lost in myriad of details. Clean state machine diagrams that were allows the programmer to specify the environment, used to “close” initially written down become out-of-date with the actual code as it the system during testing, as nondeterministic ghost machines. evolves, and the resulting system becomes hard to understand and Ghost machines are erased during compilation to executable code; maintain. During the development of Windows 8, the USB team a type system ensures that the erasure is semantics preserving. took a bold step and decided to unify modeling and programming. The P language is designed so that a P program can be checked Various components of the USB driver stack are now specified as for responsiveness—the ability to handle every event in a timely state machines and asynchronous driver code is generated from manner. By default, a machine needs to handle every event that these state machines. We are able to use state exploration tech- arrives in every state. But handling every event in every state is im- niques directly on the state machines to find and fix design bugs. practical. The language provides a notion of deferred events where Since the executable code was generated from the source, we could the programmer can annotate when she wants to delay processing make changes at the level of state machines and perform both ver- an event. The default safety checker looks for presence of unhan- ification and compilation from one description. This methodology dled events. The language also provides default liveness checks that is used to design, validate and generate code for the USB stack that an event cannot be potentially deferred forever. ships with Windows 8. The resulting driver stack is not only more P was used to implement and verify the core of the USB device reliable, but also more performant. driver stack that ships with Microsoft Windows 8. The resulting In this paper, we formalize and present the salient aspects of driver is more reliable and performs better than its prior incarnation this methodology as a domain-specific language P. Though P has a (which did not use P); we have more confidence in the robustness of visual programming interface, we represent P as a textual language its design due to the language abstractions and verification provided with a simple core calculus, so that we can give a formal treatment by P. of the language, compiler and verification algorithms. A P program is a collection of state machines communicating via events. Each Categories and Subject Descriptors D.2.4 [Software Engineer- state machine has a collection of states, local variables, and actions. ing]: Software/Program Verification– Model checking; D.2.5 The states and actions are annotated with code statements to read [Software Engineering]: Testing and Debugging; D.3.2 [Pro- and update local variables, send events to other state machines, gramming Languages]: Language Classifications– Specialized raise events locally, or call external C functions. The external C application languages, Concurrent, distributed, and parallel lan- functions are used to write parts of the code that have do with guages; D.3.3 [Programming Languages]: Language Constructs data transfer. A machine responds to received events by executing and Features– Concurrent programming structures, Control struc- transitions and actions which in turn causes the aforementioned tures code fragments to execute. For programming convenience, call transitions are used to factor out common code that needs to be Keywords domain-specific language; device driver; event-driven reused (similar to nested modes in state charts [11]). programming; state machine; verification; systematic testing Components in an operating system are required to be respon- sive. Consequently P programs are required to handle every mes- sage that can possibly arrive in every state. Our notion of respon- siveness is weaker than synchronous languages like Esterel [4] (which require input events to be handled synchronously during every clock tick, and are hence too strong to be implemented in Permission to make digital or hard copies of all or part of this work for personal or classroom use is granted without fee provided that copies are not made or distributed asynchronous software), but stronger than purely asynchronous for profit or commercial advantage and that copies bear this notice and the full citation languages like Rhapsody [12] (where asynchronous events can be on the first page. To copy otherwise, to republish, to post on servers or to redistribute queued arbitrarily long before being handled). Thus, our notion to lists, requires prior specific permission and/or a fee. of responsiveness lies in an interesting design point between syn- PLDI’13, June 16–19, 2013, Seattle, WA, USA. chrony and asynchrony. In practice, handling every event at every Copyright c 2013 ACM 978-1-4503-2014-6/13/06. $15.00 321 state would lead to combinatorial explosion in the number of con- default, attempts to schedule events according to their causal trol states, and is hence impractical. The language provides a notion order. of deferred events to handle such situations and allow a program- • We report on the use of P in a production environment; our case mer to explicitly specify that it is acceptable to delay processing of study is the USB stack in Windows 8. certain events in certain states. Reactive systems, such as device drivers, typically interact with their environment, both synchonously via function calls and asyn- 2. Overview chronously via events. The reliability of the system depends crit- Init ically on the correct handling of all interactions via stateful pro- Deferred: Action: StoppingDoor tocols. To allow reasoning about such interactions, we allow pro- start Timer = new Timer(Elevator = this); Deferred: CloseDoor grammers to model the environment of a P program using ghost Door = new Door(Elevator = this); Action: raise(unit) (OpenDoor, Ignore), machines and variables. These ghost elements are used only during (ObjectDetected, Ignore), (DoorClosed, Ignore) modeling and verification and elided during compilation. The type unit DoorClosed send(Door, SendCmdToStop) system of P ensures that the ghost machines can be erased dur- Closed ing compilation without changing the semantics of the program. It Deferred: DoorStopped is worth noting that both the real and ghost parts of a P program Action: (CloseDoor, Ignore) OpenDoor are based on the computational model of communicating state ma- send(Door, SendCmdToReset) chines. This aspect of the P language effectively blurs the distinc- OpenDoor DoorOpened DoorClosed tion between modeling and programming and makes the specifica- Closing Opening Deferred: CloseDoor tion capabilities in the language more accessible to programmers. ObjectDetected Deferred: CloseDoor Action: A P program is validated via systematic testing [9, 17] of its in- Action: (OpenDoor, Ignore) send(Door, SendCmdToClose) herent nondeterminism. Systematic testing is accomplished by in- send(Door, SendCmdToOpen) StopTimerReturned terpreting the operational semantics of a P program (closed using DoorOpened TimerFired Opened ghost machines) in the explicit-state model checker Zing [2]. All OkToClose Deferred: CloseDoor aspects of the operational semantics of the program are interpreted Deferred: OpenDoor Action: TimerFired StopTimerReturned Action: including the code statements labeling the states and actions of a send(Door, SendCmdToReset); send(Timer, StartTimer) P machine. The model checker takes care of systematically enu- send(Timer, StartTimer) merating all implicit scheduling and explicit modeling choices in ReturnState OpenDoor the program. The number of states and executions of a P program Deferred: CloseDoor Action: OperationSuccess StoppingTimer is unbounded in general (in fact, reachability analysis of P pro- raise(StopTimerReturned) Deferred: OpenDoor, grams is undecidable). Therefore, in practice, the enumeration is TimerFired ObjectDetected, CloseDoor controlled by bounding techniques. WaitingForTimer Action: The simplest approach to bounding the exploration of nonde- Deferred: OpenDoor, send(Timer, StopTimer) ObjectDetected, OperationFailure terministic transition systems is depth-bounding [19]. We have im- CloseDoor plemented this approach and found it useful for discovering errors Action: skip witnessed by short executions. However, the complexity of depth- bounded search increases exponentially with execution depth, and Figure 1: Elevator example consequently does not scale

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    11 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us