Finding Errors in Multithreaded GUI Applications
Total Page:16
File Type:pdf, Size:1020Kb
Finding Errors in Multithreaded GUI Applications Sai Zhang Hao Lü Michael D. Ernst Department of Computer Science & Engineering University of Washington, USA {szhang, hlv, mernst}@cs.washington.edu ABSTRACT All GUI objects, including visual components and data models, To keep a Graphical User Interface (GUI) responsive and active, must be accessed exclusively from the event dispatching thread. a GUI application often has a main UI thread (or event dispatching The event dispatching thread, also called the UI thread, is a single thread) and spawns separate threads to handle lengthy operations special thread initialized by the GUI framework, where all event- in the background, such as expensive computation, I/O tasks, and handling code is executed. All code that interacts with GUI objects network requests. Many GUI frameworks require all GUI objects to must also execute on that thread. There are several advantages to be accessed exclusively by the UI thread. If a GUI object is accessed the single-GUI-thread rule: from a non-UI thread, an invalid thread access error occurs and the whole application may abort. • Concurrency errors, such as races and deadlocks, never occur This paper presents a general technique to find such invalid thread on GUI objects. GUI developers need not become experts at access errors in multithreaded GUI applications. We formulate find- concurrent programming. Programming the framework itself is ing invalid thread access errors as a call graph reachability problem also simpler and less error-prone. with thread spawning as the sources and GUI object accessing as • The single-GUI-thread rule incurs less overhead. Otherwise, the sinks. Standard call graph construction algorithms fail to build whenever the framework calls a method that might be imple- a good call graph for some modern GUI applications, because of mented in client code (e.g., any non-final public or protected heavy use of reflection. Thus, our technique builds reflection-aware method in a public class), the framework must save its state and call graphs. release all locks so that the client code can grab locks if necessary. We implemented our technique and instantiated it for four popular When GUI objects return from the method, the framework must Java GUI frameworks: SWT, the Eclipse plugin framework, Swing, re-grab their locks and restore states. Even applications that do and Android. In an evaluation on 9 programs comprising 89273 not require concurrent access to the GUI must bear this cost. LOC, our technique found 5 previously-known errors and 5 new • GUI events are dispatched in a predictable order from a single ones. event queue. If the thread scheduler could arbitrarily interleave component changes, then event processing, program comprehen- Categories and Subject Descriptors: D.2.5 [Software Engineer- sion, and testing would be more difficult. ing]: Testing and Debugging. General Terms: Reliability, Experimentation. Keywords: Static analysis, invalid thread access error. 1.2 The Invalid Thread Access Error The single-GUI-thread rule requires GUI application developers to ensure that all GUI objects are accessed only by the UI thread. If 1. INTRODUCTION not, an invalid thread access error will occur. This may terminate the End-user satisfaction depends in part on the responsiveness and application — doing so is considered preferable to nondeterministic robustness of a software application’s GUI. concurrency errors. To make the GUI more responsive, GUI applications often spawn The single-GUI-thread rule can be easily violated, since a spawned separate threads to handle time-consuming operations in the back- non-UI thread often needs to update the GUI after its task is finished. ground, such as expensive computation, I/O tasks, and network In practice, invalid thread access errors are frequent, severe, and requests. This permits the GUI to respond to new events even before hard to debug. the lengthy task completes. However, the use of multiple threads Take the popular Standard Widget Toolkit (SWT) GUI framework enables new types of errors that may compromise robustness. We as an example. Invalid thread access is one of the top 3 bugs in de- now discuss a standard programming rule for multithreaded GUI veloping a SWT application, and is the source of many concurrency applications, the consequences of violating it, and a technique for bugs [35]. A Google search for “SWTException:Invalid thread ac- statically detecting such violations. cess” returns over 11,800 hits, consisting of numerous bug reports, forum posts, and mailing list threads on this problem. Eclipse, the 1.1 The Single-GUI-Thread Rule IDE for Java development, is built on top of SWT. Searching for Many popular GUI frameworks such as Swing [18], SWT [34], “SWTException:Invalid thread access” in Eclipse’s bug repository Eclipse plugin [9], Android [1], Qt [28], and MacOS Cocoa [22] and discussion forum returns over 2700 bug reports and 350 dis- adopt the single-GUI-thread rule for GUI object access: tinct discussion threads, respectively. We manually studied all 156 distinct confirmed bug reports, and found this error has been con- Permission to make digital or hard copies of all or part of this work for firmed in at least 20 distinct Eclipse projects and 40 distinct Eclipse personal or classroom use is granted without fee provided that copies are components. Even after over 10 years of active development, a not made or distributed for profit or commercial advantage and that copies recent release of Eclipse still contains this error (bug id: 333533, bear this notice and the full citation on the first page. To copy otherwise, to reported in January 2011). In addition, the invalid thread access republish, to post on servers or to redistribute to lists, requires prior specific permission and/or a fee. error is severe. It is user-perceivable, it cannot be recovered by ISSTA ’12, July 15-20, 2012, Minneapolis, MN, USA the program itself, and it often terminates the whole application. Copyright 12 ACM 978-1-4503-1454-1/12/07 ...$10.00. In many circumstances as described in the bug reports, users must In class: org.mozilla.gecko.gfx.LayerView org.mozilla.gecko.gfx.LayerView.<init>(Context;LayerController) 68. public LayerView(Context context, LayerController controller) { -> android.opengl.GLSurfaceView.setRenderer(GLSurfaceView$Renderer;) 69. super(context); -> java.lang.Thread.start() .... -> android.opengl.GLSurfaceView$GLThread.run() 73. mRenderer = new LayerRenderer(this); -> android.opengl.GLSurfaceView$GLThread.guardedRun() 74. setRenderer(mRenderer); -> org.mozilla.gecko.gfx.LayerRenderer.onSurfaceChanged(GL10;II) ... -> org.mozilla.gecko.gfx.LayerView.setViewportSize(IntSize;) } ... -> android.view.ViewRoot.recomputeViewAttributes(View;) In Android library class: android.opengl.GLSurfaceView -> android.view.ViewRoot.checkThread() 272. public void setRenderer(Renderer renderer) { Figure 2: Our tool reports a method call chain that reveals the potential ... error in Figure 1. ! represents the call relationship between methods, 282. mGLThread = new GLThread(renderer); and checkThread is an Android library method that checks whether 283. mGLThread.start(); the current thread is the event dispatching thread before accessing a } GUI object. 8 more methods in the call chain, shown as “..." above, In class: org.mozilla.gecko.gfx.LayerRenderer are omitted for brevity. 220. public void onSurfaceChanged(GL10 gl, int width, int height) { 221. gl.glViewport(0, 0, width, height); operations to be in a message issued an unacceptable number of 222. mView.setViewportSize(new IntSize(width, height)); } warnings. Furthermore, this approach is dangerous for accesses Figure 1: Bug 703256 reported on 11/17/2011 in Fennec (Mozilla Fire- from the UI thread. Asynchronous message passing offers no timing fox for Android) revision d7fa4814218d. On line 74, LayerView’s guarantee, so a GUI object may have already been disposed before constructor calls method setRenderer which spawns a new thread the message sent to it arrives, causing other bugs. on line 283. This newly created, non-UI thread calls back method Our approach: static analysis. This paper uses static analysis onSurfaceChanged that accesses GUI objects on line 222 of LayerRenderer, causing an invalid thread access error. Our tool to find potential GUI errors. Static analysis has two advantages finds this error and generates a report as shown in Figure 2. compared to dynamic approaches such as testing. First, a static analysis can explore paths of the program without executing the restart the application to recover from the error. Furthermore, many code, and without the need for a test suite. Second, a static analysis reported errors are non-trivial to diagnose and fix. Developers usu- can verify the code: if a sound static analysis reports no warnings, ally need non-local reasoning to find the specific UI interactions the code is guaranteed to be bug-free. that can trigger the bug; it took developers 2 years to fix Eclipse bug Our static analysis formulates finding invalid thread access as a 51757 and verify the patch. call graph reachability problem. Given a call graph, our technique The invalid thread access error is not unique to the SWT frame- traverses paths from its entry nodes, checking whether any path work. Other recent GUI frameworks like Android suffer from similar accesses a GUI object from a non-UI thread. If a suspicious path problems. For example, Figure 1 shows a recently-reported bug in is found, the static analysis warns of a potential error. The warning the Android version of Mozilla Firefox. This bug is particularly is in the form of a method call chain from the starting point. As an difficult to diagnose, since the code that spawns a new thread inside example, Figure 2 shows a report produced by our static analysis for the setRenderer method is in an Android library. the buggy code in Figure 1. This report indicates how a new, non-UI thread is spawned and accesses GUI objects. The generated report 1.3 Finding Invalid Thread Access Errors allows developers to inspect the method call chain, understand how To ensure that GUIs behave correctly, developers must prevent the error could be triggered, and fix it if it is a real bug.