一句话:提出 CMI-Finder,用神经网络判断代码条件与自然语言消息是否一致,从而自动发现缺陷。
程序常在日志或异常中输出自然语言消息,消息必须在语义上与触发它的条件保持一致,但条件以程序逻辑表达、消息以非正式自然语言表达,二者的一致性检查十分困难。本文提出 CMI-Finder:以条件与消息为输入,用神经模型预测二者是否一致。为获得真实、多样且大规模的监督数据,作者设计了六种自动生成大量不一致样本的技术,并实现与比较了三种神经模型,分别基于二分类、triplet loss 和微调。评估从 4200 万行 Python 代码中抽取 30 万条条件-消息语句,在历史 bug 修复数据集上最佳模型达到 78% 精确率与 72% 召回率;将该方法应用于流行开源项目的最新版本,发现 50 个此前未知的 bug,其中 19 个已被开发者确认。
Programs often emit natural language messages, e.g., in logging statements or exceptions raised on unexpected paths. To be meaningful to users and developers, the message, i.e., what to say, must be consistent with the condition under which it gets triggered, i.e., when to say it. However, checking for inconsistencies between conditions and messages is challenging because the conditions are expressed in the logic of the programming language, while messages are informally expressed in natural language. This paper presents CMI-Finder, an approach for detecting condition-message inconsistencies. CMI-Finder is based on a neural model that takes a condition and a message as its input and then predicts whether the two are consistent. To address the problem of obtaining realistic, diverse, and large-scale training data, we present six techniques to generate large numbers of inconsistent examples to learn from automatically. Moreover, we describe and compare three neural models, which are based on binary classification, triplet loss, and fine-tuning, respectively. Our evaluation applies the approach to 300K condition-message statements extracted from 42 million lines of Python code. The best model achieves a precision of 78% at a recall of 72% on a dataset of past bug fixes. Applying the approach to the newest versions of popular open-source projects reveals 50 previously unknown bugs, 19 of which have been confirmed by the developers so far.