X Meets Z: Verifying Correctness in the Presence of POSIX Threads
Total Page:16
File Type:pdf, Size:1020Kb
X Meets Z: Verifying Correctness In The Presence Of POSIX Threads Bart Massey Robert Bauer Computer Science Department Rational Software Corporation Portland State University 1920 Amberglen Pkwy, Suite 200 Portland, Oregon USA 97207–0751 Beaverton, Oregon USA 97006 [email protected] [email protected] Abstract and guide the development of resulting code. While this approach is not a panacea, it typically does dramatically The engineering of freely-available UNIX software nor- reduce the likelihood of defects and increase confidence mally utilizes an informal analysis and design process in the developed system. A more surprising finding of coupled with extensive user testing. While this approach most of these case studies, confirmed by the authors’ ex- is often appropriate, there are situations for which it pro- perience in teaching formal methods to industrial prac- duces less-than-stellar results. titioners, is that Z-like lightweight formal methods are quite accessible to reasonably experienced software en- A case study is given of such a situation that arose dur- gineers. In fact, in many studies, the savings in time and ing the design and implementation of a thread-safe li- effort due to reduced defect rates has exceeded the extra brary for interaction with the X Window System. XCB cost of judiciously-applied formal analysis. is a C binding library for the X protocol designed to work transparently with either single-threaded or multi- Thus, using the Z notation to construct a lightweight threaded clients. Managing XCB client thread access to problem model can enable the construction of an algo- the X server while honoring the constraints of both XCB rithm for controlling client thread access to the server and the X server is thus a delicate matter. that has a high likelihood of correctness in a situation where user testing is likely to be ineffective. The meth- The problem of ensuring that this complex system is ods employed, while challenging, can be learned with coded correctly can be attacked through the use of a reasonable amount of effort by experienced program- lightweight formal methods. A model is constructed of mers. This approach is thus a useful adjunct to more tra- the troublesome portion of the system using the Z formal ditional methods of freely-available software construc- specification notation. This model is used to establish tion. important system properties and produce C code with a high likelihood of correctness. 2 Background 1 Introduction The X Window System [SGN88] has for some 15 years been the underlying basis common to most graphical The design and implementation of multi-process and user interfaces for UNIX systems. During that time, the multi-threaded systems has historically been fraught server and wire protocol have been generally praised as with peril. This sort of code is commonly defective in well-designed. Indeed, the sample server implementa- subtle ways, leading to errors that are difficult to repro- tion provided originally by the MIT X Consortium and duce and troublesome to diagnose. now maintained and enhanced by the XFree86 team is widely regarded as the prime example of a policy-free Each of the past several years, one of the authors networked graphics platform. has taught a course on formal methods for modeling and analysis of software systems using the Z (pro- The client side software, however, has frequently been nounced “Zehd”) specification notation. This course criticized for its somewhat excessive resource consump- has reviewed numerous successful case studies of tion, mediocre performance and reliability, and inflexi- “lightweight” formal methods using Z and related nota- bility in the face of changing application demands. Such tions to analyze and formalize requirements and design criticisms seem especially well-justified in the case of the new generation of handheld UNIX platforms that During the construction of XCB, it became apparent that often come with a modest CPU and memory, are mis- an important component, the XCB Connection layer, sion critical, and feature GUIs constrained by limited is quite difficult to make correct while simultaneously screen size and other factors. A number of recent X accommodating the external design constraints placed “toolkits”, for example GTK+ [Pen99] and Qt [Dal01], upon it. Essentially, a “thread scheduling” algorithm have eschewed the original X GUI approach involving with somewhat unusual requirements is needed. Four Xt [AS90] and the Athena or Motif [You90] widget set. different designs for this algorithm were pseudo-coded, This has led to a marked improvement along the critical and two of these designs were actually implemented in client-side axes. C. Each of these four designs was subsequently shown, after a great deal of thought, to be subtly incorrect. Underneath almost all these modernized toolkits, how- ever, communication with the X server is still typically The task of specifying the thread scheduling algorithm handled by Xlib [SGN88]. The Xlib C binding to the X using Z was begun with the hope that a reasonable protocol is in some ways a superior piece of code: its amount of work could produce a design that was ar- quality is attested to by its longstanding sole ownership guably, if not provably, correct. This hope has proven of this critical task. However, Xlib suffers, to a lesser out: the Z model described here specifies such an algo- degree, to many of the problems that plagued Xt. rithm. The XCB “X C Binding” [MS01] is an attempt to pro- It is somewhat unusual in the literature to present a Z vide a radically shrunken and simplified X protocol model for a real system in its entirety, especially to an translation library for C programs, while simultaneously audience not necessarily comfortable with the Z nota- extending flexibility in a few key ways demanded by tion. Nonetheless, it is hoped that this full presentation modern toolkits and applications. Primary among these will provide a gentle introduction to Z, illustrate some extensions is first-class support for multi-threaded li- interesting details of formal modeling on a real-world brary clients. The XCB interface attempts to provide problem, and show how formal methods can be of as- a convenient latency-hiding interface to multi-threaded sistance to developers of freely-available software for X clients, while cleanly and efficiently handling single- UNIX systems. threaded clients using the same interface. A principal difficulty in the design of the XCB inter- nals was establishing the correctness of the core routines 3 A Z XCB Connection Model that handle the transfer of X requests to the server and server replies and events back to the client. The com- A precise description of the XCB Connection layer of plication here is that these routines must interlock prop- XCB is needed in order to prove the desired prop- erly when being invoked by several threads, and must erties. This layer of XCB handles calls from client also complete successfully in the single-threaded case. threads (through the XCB Protocol layer) to perform Reasoning about multi-threaded code is always difficult. several tasks: enqueueing X server requests, retrieving X The IEEE POSIX 1003.1c-1995 Threads Specification server replies, and retrieving X events (generated spon- (PTHREADS) [IEE95] provides details of the required taneously by the server). semantics of the thread system itself (although this is The principal engineering difficulties of the difficult to verify, since the cost of the printed specifica- XCB Connection layer are twofold. First, it is de- tion is prohibitive and it is not readily available online or sirable for a single API to transparently handle both through the local library). The trick is to avoid the perils the single-threaded and multi-threaded case. Second, of deadlock, races, non-determinism, and spinning that it is desirable that both of these cases be handled with seem to be inherent in real multi-threaded situations. minimum possible client latency: the layer should never The Z specification notation [Spi92] is a specific syntax gratuitously block a thread that could continue. and semantics for first-order logic with finite and inte- In this section, an engineering solution for the ger sorts. It is carefully designed to avoid logical para- XCB Connection layer is described. Semi-formal argu- doxes, to be easy to use to describe realistic software en- ments about correctness of this solution are also offered. gineering problems and solutions, and to be amenable to pencil-and-paper proofs of important properties. There 3.1 The Z Notation are many excellent books available to introduce Z to software engineers (e.g., [Jac97, Wor92]), and the ex- The style of the description of XCB Connection pre- isting knowledge typical of software engineers greatly sented is that standard for Z specifications: natural- assists learning Z. language text interleaved with Z paragraphs. No prior knowledge of Z is assumed here. Thus, in this section design properties. Like other mathematical notations and the succeeding description an attempt will be made used in engineering, Z is a powerful tool for producing to explain the semantics of the various Z constructs used a high-quality product, rather than a substitute for the in the description. For Z novices, it may be desirable to product itself. consult one of the Z references cited in Section 2 before attempting to read this section. 3.2 Modeling System State A Z specification describes a state machine. States in An item of principal interest in the description of the the Z specification correspond to machine states of the XCB system state is the set of client threads using the program being modeled. State transitions in the speci- API. In Z, the existence of a set with no particular defin- fication correspond to execution of the program.