学习如何倾听:在事件驱动的JavaScript API中自动查找错误模式
Learning how to listen: Automatically finding bug patterns in event-driven JavaScript APIs
摘要:
事件驱动编程在JavaScript社区中得到了广泛的实践,在客户端处理UI事件和AJAX请求,在服务器端处理文件或网络I/O等长时间运行的操作。许多流行的基于事件的API允许将事件名称指定为自由格式的字符串而不进行任何验证,这可能导致未注册侦听器的事件丢失,以及从未发出的事件的死侦听器。在以前的工作中,Madsen等人提出了一种用于检测此类问题的精确静态分析,但他们的分析无法扩展,因为它可能需要大量的上下文,这些上下文在程序大小上是指数级的。集中在检测死侦听器的问题,我们提出了一种方法来学习如何正确使用基于事件的API,首先使用一个简单的静态分析挖掘一个大型的JavaScript代码语料库,以识别注册事件侦听器的代码片段,然后应用统计建模来识别异常模式,这往往表明不正确的API使用。通过对127,531个开源JavaScript代码库的大规模评估,我们的技术能够检测到75个异常标记注册模式,同时在我们的验证集上保持90.9%的精度和7.5%的召回率,这表明基于学习的方法来检测事件处理错误是可行的。在另一个实验中,我们调查了25个开源项目中这些模式的实例,并向项目维护人员报告了30个问题,其中7个已被确认为bug。
英文摘要:
Event-driven programming is widely practiced in the JavaScript community, both on the client side to handle UI events and AJAX requests, and on the server side to accommodate long-running operations such as file or network I/O. Many popular event-based APIs allow event names to be specified as free-form strings without any validation, potentially leading to lost events for which no listener has been registered and dead listeners for events that are never emitted. In previous work, Madsen et al. presented a precise static analysis for detecting such problems, but their analysis does not scale because it may require a number of contexts that is exponential in the size of the program. Concentrating on the problem of detecting dead listeners, we present an approach to learn how to correctly use event-based APIs by first mining a large corpus of JavaScript code using a simple static analysis to identify code snippets that register an event listener, and then applying statistical modeling to identify anomalous patterns, which often indicate incorrect API usage. From a large-scale evaluation on 127,531 open-source JavaScript code bases, our technique was able to detect 75 anomalous listener-registration patterns, while maintaining a precision of 90.9% and recall of 7.5% over our validation set, demonstrating that a learning-based approach to detecting event-handling bugs is feasible. In an additional experiment, we investigated instances of these patterns in 25 open-source projects, and reported 30 issues to the project maintainers, of which 7 have been confirmed as bugs.
机构、总结、中文摘要均由AI分析而来,可能存在错误,请以论文正文为准。