WO2016004808A1 - 基于多线程程序约束构建的数据竞争检测与证据生成方法 - Google Patents

基于多线程程序约束构建的数据竞争检测与证据生成方法 Download PDF

Info

Publication number
WO2016004808A1
WO2016004808A1 PCT/CN2015/081062 CN2015081062W WO2016004808A1 WO 2016004808 A1 WO2016004808 A1 WO 2016004808A1 CN 2015081062 W CN2015081062 W CN 2015081062W WO 2016004808 A1 WO2016004808 A1 WO 2016004808A1
Authority
WO
WIPO (PCT)
Prior art keywords
constraint
thread
event
data
program
Prior art date
Legal status (The legal status is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the status listed.)
Ceased
Application number
PCT/CN2015/081062
Other languages
English (en)
French (fr)
Inventor
刘烃
张晓东
俞乐晨
刘沛
郑庆华
Current Assignee (The listed assignees may be inaccurate. Google has not performed a legal analysis and makes no representation or warranty as to the accuracy of the list.)
Xian Jiaotong University
Original Assignee
Xian Jiaotong University
Priority date (The priority date is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the date listed.)
Filing date
Publication date
Application filed by Xian Jiaotong University filed Critical Xian Jiaotong University
Publication of WO2016004808A1 publication Critical patent/WO2016004808A1/zh
Anticipated expiration legal-status Critical
Ceased legal-status Critical Current

Links

Images

Classifications

    • GPHYSICS
    • G06COMPUTING OR CALCULATING; COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F11/00Error detection; Error correction; Monitoring
    • G06F11/36Prevention of errors by analysis, debugging or testing of software

Definitions

  • the invention relates to the field of trusted software and software testing, in particular to a data competition detection and evidence generation method based on multi-threaded program constraints.
  • the static method infers all data competition in the program by statically detecting all the paths of the program, and can detect most data competition; however, due to the use of a large number of assumptions, the static analysis method will generate invalid data competition, resulting in a high false positive rate.
  • the dynamic method can provide high-precision detection results by monitoring the memory and synchronization information in one execution to determine whether there is data competition.
  • the dynamic analysis method is affected by the interleaving and the path, and the coverage is often improved by multiple executions. This article combines static code analysis with program execution process monitoring to improve coverage and eliminate false positives as much as possible.
  • the existing dynamic detection technologies are mainly divided into three types: based on lockset, based on the combination of happens-before and the two.
  • the lockset-based method is not sensitive to thread interleaving, but there is a false positive situation, that is, invalid competition.
  • the methods based on happens-before only detect data competition on a particular interleaving sequence. The detection results are reliable, but sensitive to thread interleaving.
  • the hybrid approach combines the advantages of both, and attempts to reduce their respective shortcomings, but also faces problems such as the inability to search for hidden errors, invalid alarms caused by lockset high false positives.
  • the object of the present invention is to provide a data competition detection and evidence generation method based on multi-threaded program constraints, construct a constraint expression according to multi-threaded program semantics, and convert a data competition detection problem into a constraint. Solve the problem, use the constraint solver to detect possible data competition, and generate a program execution path that triggers data competition.
  • a data competition detection and evidence generation method based on multi-threaded program constraints comprising the following steps:
  • a further improvement of the present invention is that the stepping work in the step S1) is not performed on the source code or the binary level, but is performed on the bytecode level.
  • the specific implementation method is as follows: firstly, the multi-threaded program source code to be tested is to be tested. Converted to intermediate bytecode format, ie LLVM bytecode; then the statement with the monitoring function is implanted into the program under test; finally, the bytecode of the implanted monitoring code is linked into an executable program.
  • a further improvement of the present invention is that the multi-threaded program execution path constraint model F in the step S2) implies all possible interleaving sequences of the execution path, including five constraints: path expression, memory model constraint, read-write relationship constraint, partial bias Order constraints and synchronous semantic constraints are defined as follows:
  • path expression describes the definition inside the thread - use the chain, and control the internal state transition of the thread
  • Memory model constraint indicates the relationship between statements and variables in the program, using the semantics of order consistency, and the order consistency specifies that the CPU executes the program according to the order of the statements in the code;
  • read-write relationship constraints define the definition between threads - use the chain, specify the value read by the shared variable, must come from the initial value and the most recent written value;
  • Partial order constraint define the timing relationship between the thread creation thread and the termination thread operation statement between the operated thread statements;
  • Synchronous semantic constraints define the timing relationship between synchronous control operation statements between threads;
  • the definition-use chain is: convert each thread sequence into SSA format. For each execution sequence of SSA format, removing the shared access point is a complete definition-use chain.
  • a further improvement of the present invention is that the method for constructing the multi-threaded program execution path constraint model F in the step S2) comprises the following operations:
  • e r is a read event
  • e w and e x are write events
  • v r and v w are variables operated by events e r and e w
  • the expression of the formula means that if v r in the event e r is taken The value comes from the v w in the event e w , first of all to satisfy e r after e w , ie O(e w ) ⁇ O(e r ); then all the writes are satisfied either before e w or at e r after that;
  • the calculation method of the synchronous semantic constraint includes two operations: lock/unlock and wait/signal:
  • lock/unlock operation is to construct a lock synchronization semantic constraint, requiring that in the lock/unlock set L of the same mutex, for any two lock/unlock event pairs: l i /u i and l k /u k Must meet the formula:
  • the lock pair l i /u i occurs either before the lock pair l k /u k or after it;
  • each wait operation must correspond to a signal operation, and a signal operation wakes up at most one wait operation.
  • WT act as The set of all wait operations on the cond, so that SG as a collection of all signal operations on the cond, in order to meet the above conditions, must have the following formula:
  • SG wt represents a set of signal operations that e wt can match, and e sg is any signal operation event in SG wt , using variables Whether it is equal to 1 to indicate whether e sg matches e wt .
  • Sub formula Indicates that for each wait operation e wt must have a signal operation to match it;
  • first(e c ) is the order of the first event of the thread created by e c
  • e j is the thread termination event
  • last(e j ) is the sequence of the end event of the thread ending with e j ;
  • a further improvement of the present invention is that in step S7), a sequence of evidence is generated for each data competition to describe its triggering process.
  • a multi-threaded program constraint construction model is proposed to transform the data competition detection problem in one execution into the constraint solving problem.
  • This model is constructed constrained by program semantics.
  • the constructed expression contains all possible interleaving sequences to detect all data competition in the execution path.
  • a sequence of evidence is generated for all data competition to provide the user with information on how data competition is triggered.
  • Figure 1 is a general flow chart of the method of the present invention.
  • FIG. 2 is a flow chart of a multi-threaded program path constraint construction method.
  • the program to be tested is as follows, x and y are shared variables, and thread 0 creates thread 1 and thread 2.
  • the data competition detection and evidence generation method of the present invention includes the following steps:
  • the entire constraint model F implies all possible interleaving sequences of the execution path. Specifically, as shown in FIG. 2, the corresponding logical expression is generated according to the following steps:
  • the lower corners of the global variables x and y indicate read (r) or write (w), the upper corner distinguishes between different read or write operations, and the upper corner is marked as 0 to indicate initial assignment.
  • o i represents the permutation sequence number in the interleaved sequence of the i-th row.
  • e r is a read event and e w and e x are write events. Meaning expressed in formula is, if the event e r v r v w values derived from the event e w, the first to meet the following e r e w, i.e. O (e w) ⁇ O ( e r); Then all the writes must be met either before e w or after e r .
  • the reading and writing of the variable x may be listed.
  • the reading of the 9th line x is from the writing of the 1st line x, it should be satisfied: the first line is before the 9th line, and the 5th line is written to the x line. Can't happen between the two.
  • the case of the y variable is similar to x.
  • the lock pair l i /u i either occurs before the lock pair l k /u k or occurs after it.
  • condition variable cond each wait operation must correspond to a signal operation, and a signal operation wakes up at most one wait operation.
  • WT be the set of all wait operations on cond
  • SG be the set of all signal operations on cond.
  • e wt is an element in the WT
  • SG wt represents a set of signal operations that e wt can match
  • WT sg represents a set of wait operations that e sg can match.
  • the constraint expression indicates that either thread 1 first acquires lock o 6 ⁇ o 8 , or thread 2 first acquires lock o 11 ⁇ o 4 .
  • first(e c ) is the order of the first event of the thread created by e c ;
  • e j is the thread termination event;
  • last(e j ) is the sequence of the end event of the thread ending with e j .
  • the constraint indicates that line 2 of the thread creation statement is executed before the first event of thread 1 that was created.
  • Step S3) construct a data competition candidate set, and generate a competition occurrence condition for each candidate.
  • Data competition is defined as when two threads access the same memory at the same time, and there is at least one write operation.
  • o(e 6 ) ⁇ o(e 10 ) ⁇ o(e 9 ) ⁇ o(e 7 ) ⁇ o(e 11 ) indicates that data occurs between the 7th row and the 10th row.
  • the condition of competition where o(e 9 ) ⁇ o(e 7 ) ⁇ o(e 11 ) indicates that the seventh line can occur in the previous event (line 9) and the latter event (line 11) on line 10. Between; because the 7th line is at the end of the thread, only o(e 6 ) ⁇ o(e 10 ) is used. This race condition is established, indicating that line 7 and line 10 can access the same variable y at the same time, and data competition occurs.
  • Verification F ⁇ o(e 5 ) ⁇ o(e 2 ) ⁇ o(e 4 ) ⁇ o(e 1 ) ⁇ o(e 6 ), and it is concluded that no data competition occurs in the first row and the fifth row;
  • Step S8) collects all data competitions and corresponding evidence sequences.

Landscapes

  • Engineering & Computer Science (AREA)
  • Theoretical Computer Science (AREA)
  • Computer Hardware Design (AREA)
  • Quality & Reliability (AREA)
  • Physics & Mathematics (AREA)
  • General Engineering & Computer Science (AREA)
  • General Physics & Mathematics (AREA)
  • Multi Processors (AREA)
  • Debugging And Monitoring (AREA)

Abstract

一种基于多线程程序约束构建的数据竞争检测与证据生成方法,根据多线程程序语义构建约束表达式,将数据竞争检测问题转化为约束求解问题,采用约束求解器检测可能存在的数据竞争,并生成触发数据竞争的程序执行路径,首先插桩被检测程序,执行此程序并得到执行路径;其次根据多线程程序执行语义将执行路径转化为无量词一阶逻辑表达式,此约束表达式涵盖所有可行的线程交织;然后根据发生数据竞争时语句间时序关系构建数据竞争候选集合,生成候选发生竞争的条件;最后遍历候选集合判定是否存在数据竞争,如有则生成对应的证据序列,本方法可找出一次执行中所有的数据竞争且不存在误报情况,对每一个数据竞争都生成一个展示了数据竞争触发过程的证据序列。

Description

基于多线程程序约束构建的数据竞争检测与证据生成方法 技术领域
本发明涉及可信软件及软件测试领域,特别涉及一种基于多线程程序约束构建的数据竞争检测与证据生成方法。
背景技术
随着处理器多核化的普及,多线程技术已经成为软件编程中提高CPU利用率不可或缺的技术。然而,由于线程之间交织的不确定性,多线程程序执行过程中可能会出现一些难以预料的行为导致程序出错,例如对临界区没有做好同步工作而导致的数据竞争问题。数据竞争是两个不同的线程在没有同步保护的情况同时访问一个内存,并且至少有一个写操作。数据竞争不一定导致程序错误,因为有些程序员故意让程序有数据竞争以提高运行的效率,但是有调查表明5-24%的数据竞争会对程序产生坏影响。数据竞争很难以被发现,因为它们经常发生在一些低概率出现的交织序列中,在现实中往往需要花很多时间去定位,其引起的错误如同“corner error”,即使在软件发布时也未必能够完全清除它们。因此,数据竞争检测是多线程程序测试领域最受关注的研究点之一。
过去几十年中数据竞争检测已有大量研究,设计出很多杰出的自动化检测工具,主要分为静态与动态分析技术。静态方法通过静态检测程序所有的路径来推断程序中的所有数据竞争,可以检测出大部分数据竞争;但由于使用大量假设,静态分析方法会产生无效的数据竞争,导致误报率较高。动态方法通过监控一次执行中内存与同步信息以确定是否存在数据竞争,能够提供较高精度的检测结果;但是动态分析方法受到交织与路径的影响,往往要通过多次执行来提高覆盖率。本文将静态代码分析与程序执行过程监测相结合,以提高覆盖率且尽可能消除误报。
现有的动态检测技术主要分为三种:基于lockset、基于happens-before与二者结合的方法。1)基于lockset的方法对线程交织不敏感,但是存在误报情况,即无效竞争。2)基于happens-before的方法只检测某特定交织序列上的数据竞争,检测结果虽可靠,但敏感于线程交织。3)混合方法结合了两者的优点,并且试图减小各自的缺点,但也面临如不能够搜索出隐藏的错误、lockset高误报引起的无效报警等问题。
发明内容
为了克服上述现有技术的缺点,本发明的目的在于提供一种基于多线程程序约束构建的数据竞争检测与证据生成方法,根据多线程程序语义构建约束表达式,将数据竞争检测问题转化为约束求解问题,采用约束求解器检测可能存在的数据竞争,并生成触发数据竞争的程序执行路径。
为了实现上述目的,本发明采用的技术方案是:
一种基于多线程程序约束构建的数据竞争检测与证据生成方法,包括如下步骤:
S1)在给定输入下,通过执行已插桩的待测程序以生成路径记录文件,且识别出执行路径中公有变量的访问点以便于约束构建;
S2)根据程序执行语义将执行路径中状态转移、线程交织关系转化为无量词一阶逻辑表达式,构建蕴含了所有可能的交织序列的多线程程序执行路径约束模型F;
S3)将路径中所有线程上可能发生数据竞争的两点视为数据竞争候选,收集所有候选并构建数据竞争候选集合DRCS,同时根据数据竞争的定义构建每个候选的竞争发生条件ρ;
S4)针对每一个候选竞争发生条件ρ,利用约束求解器验证F∧ρ是否有解;
S5)如果有解,则表示此竞争条件会触发真实的数据竞争;如果无解,则表示此候选不会触发数据竞争;
S6)当存在数据竞争时,输出该数据竞争的证据序列;
S7)对于数据竞争候选集合DRCS,如果遍历结束,则输出所有结果;否则, 继续遍历下一个竞争候选;
S8)验证结束后,输出检测到的所有数据竞争以及对应的证据序列。
本发明进一步的改进在于:所述步骤S1)中插桩工作并非在源码或者二进制的层面上进行,而是在字节码的层面上完成,具体实施方法为:首先将待测多线程程序源码转化为中间字节码格式,即LLVM字节码;然后将具有监控功能的语句植入待测程序;最后将植入监控代码的字节码链接成可执行程序。
本发明进一步的改进在于:所述步骤S2)中多线程程序执行路径约束模型F蕴含了执行路径所有可能的交织序列,包括五种约束:路径表达式、内存模型约束、读写关系约束、偏序约束以及同步语义约束,定义分别如下:
1)路径表达式:描述线程内部的定义-使用链,以及控制线程内部状态转换;
2)内存模型约束:表示程序中语句、变量之间的关系,采用顺序一致性的语义,顺序一致性规定CPU按照代码中语句的顺序来执行程序;
3)读写关系约束:定义线程间的定义-使用链,规定共享变量所读取到的值,必须来自初始值以及最近的写值;
4)偏序约束:定义线程之间创建线程与终止线程操作语句于被操作线程语句之间的时序关系;
5)同步语义约束:定义线程之间同步控制操作语句之间的时序关系;
其中,定义-使用链为:将每一个线程序列转化为SSA格式,对于每一个SSA格式的执行序列,除去共享访问点都是一个完整的定义-使用链。
本发明进一步的改进在于:所述步骤S2)中多线程程序执行路径约束模型F的构建方法包括以下操作:
1)计算路径表达式,以控制线程内部状态转移;
2)计算内存模型约束,以线程内限制语句之间的关系;
3)计算读写关系约束,以建立线程间的定义-使用链;
4)计算同步语义约束,以定义线程间同步关系;
5)计算偏序约束,以描述线程创建与终止的语义;
最后,结合以上五种约束,构成约束模型F。
本发明进一步的改进在于:定义执行路径事件集合
Figure PCTCN2015081062-appb-000001
其中k为线程数量,Ti={e1,e2,…,en}作为线程i的执行序列,en表示Ti的第n个事件,O(en)表示事件en的顺序,n表示Ti的事件数量,则:
所述路径表达式的计算方法:
将每一个线程序列转化为SSA格式,类似于路径条件(Path Condition)的收集,直接将SSA格式序列转化为路径表达式;
所述内存模型约束的计算方法:
采用顺序一致性模型,所有操作完全按程序的顺序执行,线程内的事件顺序符合约束:
Figure PCTCN2015081062-appb-000002
其中ei与ei+1表示同一线程内连续的两个事件,τ表示所有线程序列;
所述读写关系约束的计算方法:
使共享变量的读来自于最近的写,对于同一共享变量v,令R作为所有对其进行读操作的事件集合,令W作为所有对其进行写操作的事件集合,给出以下公式:
Figure PCTCN2015081062-appb-000003
其中,er为读事件,ew与ex为写事件,vr和vw为事件er与ew所操作的变量,公式所表达的意思是,如果事件er中的vr取值来自于事件ew中的vw,首先要满足er在ew之后,即O(ew)<O(er);然后要满足所有的写要么在ew之前,要么在er之后;
所述同步语义约束的计算方法包括lock/unlock与wait/signal两类操作:
1)lock/unlock操作的目的为构建锁同步语义约束,要求在同一互斥锁的lock/unlock集合L中,对于任意两个lock/unlock事件对:li/ui与lk/uk,须满 足公式:
Figure PCTCN2015081062-appb-000004
其中,锁对li/ui要么发生在锁对lk/uk之前,要么发生在其后;
2)wait/signal操作的目的是构建条件变量同步语义约束,要满足条件:每一个wait操作必须对应一个signal操作,而一个signal操作至多唤醒一个wait操作,对于同一条件变量cond,令WT作为在cond上所有wait操作的集合,令SG作为在cond上所有signal操作的集合,如要满足之上的条件,须有以下公式:
Figure PCTCN2015081062-appb-000005
其中,ewt为WT中的任一元素,SGwt表示ewt可以匹配的signal操作的集合,esg为SGwt中任一signal操作事件,利用变量
Figure PCTCN2015081062-appb-000006
是否等于1来表示esg是否与ewt相匹配。子公式
Figure PCTCN2015081062-appb-000007
表示,对于每一个wait操作ewt必须有一个signal操作与之匹配;
所述偏序约束的计算方法:
首先规定:如果事件创建一个线程,那么被创建线程的所有事件都要在此事件之后执行;如果事件执行线程终止操作,那么被终止线程的所有事件都要在此事件之前;令C为create/fork操作的事件集合,令J作为join操作的事件集合;给定约束:
Figure PCTCN2015081062-appb-000008
其中,ec为线程创建事件,first(ec)为ec所创建的线程首个事件的顺序;ej为线程终止事件;last(ej)为ej所结束的线程末尾事件的顺序;
最终将以上五种约束相与构成约束模型F。
本发明进一步的改进在于:所述步骤S3)中竞争发生条件ρ的构建方法如下:如果有一条路径τ=<τ1eiejτ2>,其中τ1是前缀,τ2是后缀,事件ei与 ej属于不同的线程并且都访问同一内存,至少有一个写,那么二者之间发生了数据竞争,而τ看作是ei与ej数据竞争的证据序列;对于访问同一变量的事件ei与ek,e′i与e″i分别表示ei的前一个事件与后一个事件;同样,e′k与e″k分别表示ek的前一个事件与后一个事件,那么两者发生数据竞争即同时访问同一内存的条件ρ为:
O(e′i)<O(ek)<O(e″i)∧O(e′k)<O(ei)<O(e″k)。
本发明进一步的改进在于:所述步骤S7)中对于每一个数据竞争都会生成证据序列,以描述其触发过程。
与现有技术相比,本发明的有益效果是:
(1)提出一种多线程程序约束构建模型,将一次执行中的数据竞争检测问题转化为约束求解问题。此模型按照程序语义进行约束构建,所构建的表达式包含了所有可能的交织序列,进而检测出执行路径中的所有数据竞争。
(2)对所有数据竞争都产生一个证据序列,以给用户提供数据竞争是如何被触发的信息。
(3)对执行序列进行事后分析,不存在on-the-fly技术所产生的巨大运行时开销。
附图说明
图1为本发明方法整体流程图。
图2为多线程程序路径约束构建方法流程图。
具体实施方式
以下结合附图和实例详细说明本发明的实施方式。
待测程序如下所示,x与y为共享变量,线程0创建了线程1与线程2。
共享变量x,y
Figure PCTCN2015081062-appb-000009
如图1所示,本发明数据竞争检测与证据生成方法,包括如下步骤:
步骤S1):将监控代码植入被测程序,以对程序的执行过程进行记录。给定输入下,执行示例程序,记录下路径π=[1,2,3,4,5,6,7,8,9,10,11];然后,识别出共享变量访问点,包括{1,5,7,9,10}。
步骤S2):根据程序执行语义将执行路径中状态转移、线程交织关系转化为无量词一阶逻辑表达式,构建执行路径π的约束模型F,包括路径表达式、内存模型约束、读写关系约束、偏序约束、同步语义约束。整个约束模型F蕴含了执行路径所有可能的交织序列。具体地,如图2所示,按照以下步骤生成对应的逻辑表达式:
S201)首先,根据识别出的共享访问点,将路径π转化为SSA格式,如下所示:
Figure PCTCN2015081062-appb-000010
Figure PCTCN2015081062-appb-000011
2:create(1);
3:create(2);
4:lock(m);
Figure PCTCN2015081062-appb-000012
6:unlock(m);
Figure PCTCN2015081062-appb-000013
8:lock(m);
Figure PCTCN2015081062-appb-000014
Figure PCTCN2015081062-appb-000015
11:unlock(m);
其中,对于全局变量x与y的下角标表示读(r)或写(w),上角标区分不同的读或写操作,上角标为0表示为初始赋值。
然后,根据路径的SSA格式,直接计算出路径π的路径表达式,如下公式:
Figure PCTCN2015081062-appb-000016
Figure PCTCN2015081062-appb-000017
S202)构建内存模型约束,采用顺序一致性模型,规定所有操作按程序的顺序执行。按照公式:
Figure PCTCN2015081062-appb-000018
计算出路径π的内存模型约束,如以下公式:
o(e1)<o(e2)<o(e3)∧
o(e4)<o(e5)<o(e6)<o(e7)∧
o(e8)<o(e9)<o(e10)<o(e11)
其中,oi表示第i行语句交织序列中的排列序号。
S203)计算读写顺序约束,使共享变量的读来自于最近的写。对于同一共享变量v,令R作为所有对其进行读操作的事件集合,令W作为所有对其进行写操作的事件集合。给出以下公式:
Figure PCTCN2015081062-appb-000019
其中,er为读事件,ew与ex为写事件。公式所表达的意思是,如果事件er中的vr取值来自于事件ew中的vw,首先要满足er在ew之后,即O(ew)<O(er);然后要满足所有的写要么在ew之前,要么在er之后。
在路径π中,对于全局变量x,R={e9},W={e0,e1,e5,e9},其读写关系表达式如下公式:
Figure PCTCN2015081062-appb-000020
Figure PCTCN2015081062-appb-000021
其中,对变量x的读写可能进行了罗列,当第9行x的读来自于第1行x的写时,应该满足:第1行在第9行之前,且第5行对x的写不能发生在两者之间。y变量的情况类似于x。
S204)计算同步语义约束,包括lock/unlock与wait/signal两类操作:
1)构建锁同步语义约束(lock/unlock操作)时,要求在同一互斥锁的lock/unlock集合L中,对于任意两个lock/unlock事件对:li/ui与lk/uk,须满足公式:
Figure PCTCN2015081062-appb-000022
其中,锁对li/ui要么发生在锁对lk/uk之前,要么发生在其后。
2)构建条件变量同步语义约束(wait/signal)时,要满足条件:每一个wait操作必须对应一个signal操作,而一个signal操作至多唤醒一个wait操作。对于同一条件变量cond,令WT作为在cond上所有wait操作的集合,令SG作为在cond上所有signal操作的集合。如要满足之上的条件,须有以下公式:
Figure PCTCN2015081062-appb-000023
其中,令ewt为WT中的一个元素,SGwt表示ewt可以匹配的signal操作的集合,WTsg表示esg可以匹配的wait操作的集合。本文利用变量
Figure PCTCN2015081062-appb-000024
是否等于1来表示esg是否与ewt相匹配。子公式
Figure PCTCN2015081062-appb-000025
表示,对于每一个wait操作ewt必须有一个signal操作与之匹配。
在路径π中,只有锁m,同步语义约束公式如下:
o(e6)<o(e8)∨o(e11)<o(e4)
其中,约束表达式表示要么线程1先获取锁o6<o8,要么线程2先获取锁o11<o4
S205)计算偏序约束,其规定:如果事件创建一个线程,那么被创建线程的所有事件都要在此事件之后执行。如果事件执行线程终止操作,那么被终止线程的所有事件都要在此事件之前。令C为create/fork操作的事件集合, 令J作为join操作的事件集合。给定约束:
Figure PCTCN2015081062-appb-000026
其中,ec为线程创建事件,first(ec)为ec所创建的线程首个事件的顺序;ej为线程终止事件;last(ej)为ej所结束的线程末尾事件的顺序。
在路径π中,线程创建语句为O2,O3,其偏序关系约束如下公式:
o(e2)<o(e4)∧o(e3)<o(e8)
其中,约束表示线程创建语句第2行在其被创建线程1的首个事件前执行。
S206)将以上五种约束进行相与,得到约束模型F。
步骤S3):构建数据竞争候选集合,以及生成每一个候选的竞争发生条件。对于访问同一变量的事件ei与ek,e′i与e″i分别表示ei的前一个事件与后一个事件;同样,e′k与e″k分别表示ek的前一个事件与后一个事件,那么两者发生数据竞争(同时访问同一内存)的条件ρ为:
O(e′i)<O(ek)<O(e″i)∧O(e′k)<O(ei)<O(e″k)。
此示例中的候选集合以及竞争发生条件如下:
Figure PCTCN2015081062-appb-000027
数据竞争的定义为当两个线程同时访问同一内存,且至少有一个写操作。以第4个候选为例说明,o(e6)<o(e10)∧o(e9)<o(e7)<o(e11)表示第7行与第10行之间发生数据竞争的条件,其中o(e9)<o(e7)<o(e11)表示第7行能够发生在第10行的前一事件(第9行)与后一事件(第11行)之间;由于第7行为线程末尾,故只用o(e6)<o(e10)。此竞争条件成立说明第7行与第10行可以同时访问同一变量y,则出现数据竞争。
步骤S4—S7):针对每一个候选,利用求解器求解F∧ρ,验证数据竞争候选集合DRCS中所有的候选是否为有效数据竞争。以下逐一验证每个候选:
验证F∧o(e5)<o(e2)∧o(e4)<o(e1)<o(e6),结果得出第1行与第5行不发生数据竞争;
验证F∧o(e9)<o(e2)∧o(e8)<o(e1)<o(e10),结果得出第1行与第9行不发生数据竞争;
验证F∧o(e4)<o(e9)<o(e6)∧o(e8)<o(e5)<o(e10),结果得出第5行与第9行不发生数据竞争;
验证F∧o(e6)<o(e10)∧o(e9)<o(e7)<o(e11),结果得出第7行与第9行之间数据竞争,且证据序列为:1,2,3,4,5,6,8,9,10,7,11。
遍历完DRCS之后,终止验证工作。
步骤S8)收集所有数据竞争以及对应的证据序列。

Claims (7)

  1. 一种基于多线程程序约束构建的数据竞争检测与证据生成方法,其特征在于,包括如下步骤:
    S1)在给定输入下,通过执行已插桩的待测程序以生成路径记录文件,且识别出执行路径中公有变量的访问点以便于约束构建;
    S2)根据程序执行语义将执行路径中状态转移、线程交织关系转化为无量词一阶逻辑表达式,构建蕴含了所有可能的交织序列的多线程程序执行路径约束模型F;
    S3)将路径中所有线程上可能发生数据竞争的两点视为数据竞争候选,收集所有候选并构建数据竞争候选集合DRCS,同时根据数据竞争的定义构建每个候选的竞争发生条件ρ;
    S4)针对每一个候选竞争发生条件ρ,利用约束求解器验证F∧ρ是否有解;
    S5)如果有解,则表示此竞争条件会触发真实的数据竞争;如果无解,则表示此候选不会触发数据竞争;
    S6)当存在数据竞争时,输出该数据竞争的证据序列;
    S7)对于数据竞争候选集合DRCS,如果遍历结束,则输出所有结果;否则,继续遍历下一个竞争候选;
    S8)验证结束后,输出检测到的所有数据竞争以及对应的证据序列。
  2. 根据权利要求1所述基于多线程程序约束构建的数据竞争检测与证据生成方法,其特征在于,所述步骤S1)中插桩工作并非在源码或者二进制的层面上进行,而是在字节码的层面上完成,具体实施方法为:首先将待测多线程程序源码转化为中间字节码格式,即LLVM字节码;然后将具有监控功能的语句植入待测程序;最后将植入监控代码的字节码链接成可执行程序。
  3. 根据权利要求1所述基于多线程程序约束构建的数据竞争检测与证据生成方法,其特征在于,所述步骤S2)中多线程程序执行路径约束模型F蕴含了执行路径所有可能的交织序列,包括五种约束:路径表达式、内存模型约束、 读写关系约束、偏序约束以及同步语义约束,定义分别如下:
    1)路径表达式:描述线程内部的定义-使用链,以及控制线程内部状态转换;
    2)内存模型约束:表示程序中语句、变量之间的关系,采用顺序一致性的语义,顺序一致性规定CPU按照代码中语句的顺序来执行程序;
    3)读写关系约束:定义线程间的定义-使用链,规定共享变量所读取到的值,必须来自初始值以及最近的写值;
    4)偏序约束:定义线程之间创建线程与终止线程操作语句同被操作线程语句之间的时序关系;
    5)同步语义约束:定义线程之间同步控制操作语句之间的时序关系;
    其中,定义-使用链为:将每一个线程序列转化为SSA格式,对于每一个SSA格式的执行序列,除去共享访问点都是一个完整的定义-使用链。
  4. 根据权利要求3所述基于多线程程序约束构建的数据竞争检测与证据生成方法,其特征在于,所述步骤S2)中多线程程序执行路径约束模型F的构建方法包括以下操作:
    1)计算路径表达式,以控制线程内部状态转移;
    2)计算内存模型约束,以线程内限制语句之间的关系;
    3)计算读写关系约束,以建立线程间的定义-使用链;
    4)计算同步语义约束,以定义线程间同步关系;
    5)计算偏序约束,以描述线程创建与终止的语义;
    最后,结合以上五种约束,构成约束模型F。
  5. 根据权利要求4所述基于多线程程序约束构建的数据竞争检测与证据生成方法,其特征在于,定义执行路径事件集合
    Figure PCTCN2015081062-appb-100001
    其中k为线程数量,Ti={e1,e2,…,en}作为线程i的执行序列,en表示Ti的第n个事件,O(en)表示事件en的顺序,n表示Ti的事件数量,则:
    所述路径表达式的计算方法:
    将每一个线程序列转化为SSA格式,直接将SSA格式序列转化为路径 表达式;
    所述内存模型约束的计算方法:
    采用顺序一致性模型,所有操作完全按程序的顺序执行,线程内的事件顺序符合约束:
    Figure PCTCN2015081062-appb-100002
    其中ei与ei+1表示同一线程内连续的两个事件,τ表示所有线程序列;
    所述读写关系约束的计算方法:
    使共享变量的读来自于最近的写,对于同一共享变量v,令R作为所有对其进行读操作的事件集合,令W作为所有对其进行写操作的事件集合,给出以下公式:
    Figure PCTCN2015081062-appb-100003
    其中,er为读事件,ew与ex为写事件,vr和vw为事件er与ew所操作的变量,公式所表达的意思是,如果事件er中的vr取值来自于事件ew中的vw,首先要满足er在ew之后,即O(ew)<O(er);然后要满足所有的写要么在ew之前,要么在er之后;
    所述同步语义约束的计算方法包括lock/unlock与wait/signal两类操作:
    1)lock/unlock操作的目的为构建锁同步语义约束,要求在同一互斥锁的lock/unlock集合L中,对于任意两个lock/unlock事件对:li/ui与lk/uk,须满足公式:
    Figure PCTCN2015081062-appb-100004
    其中,锁对li/ui要么发生在锁对lk/uk之前,要么发生在其后;
    2)wait/signal操作的目的是构建条件变量同步语义约束,要满足条件:每一个wait操作必须对应一个signal操作,而一个signal操作至多唤醒一个wait操作,对于同一条件变量cond,令WT作为在cond上所有wait操作的集合,令SG作为在cond上所有signal操作的集合,如要满足之上的条件,须 有以下公式:
    Figure PCTCN2015081062-appb-100005
    其中,ewt为WT中的任一元素,SGwt表示ewt可以匹配的signal操作的集合,esg为Sgwt中任一signal操作事件,利用变量
    Figure PCTCN2015081062-appb-100006
    是否等于1来表示esg是否与ewt相匹配,子公式
    Figure PCTCN2015081062-appb-100007
    表示,对于每一个wait操作ewt必须有一个signal操作与之匹配;
    所述偏序约束的计算方法:
    首先规定:如果事件创建一个线程,那么被创建线程的所有事件都要在此事件之后执行;如果事件执行线程终止操作,那么被终止线程的所有事件都要在此事件之前;令C为create/fork操作的事件集合,令J作为join操作的事件集合;给定约束:
    Figure PCTCN2015081062-appb-100008
    其中,ec为线程创建事件,first(ec)为ec所创建的线程首个事件的顺序;ej为线程终止事件;last(ej)为ej所结束的线程末尾事件的顺序;
    最终将以上五种约束相与构成约束模型F。
  6. 根据权利要求1所述基于多线程程序约束构建的数据竞争检测与证据生成方法,其特征在于,所述步骤S3)中竞争发生条件ρ的构建方法如下:如果有一条路径τ=<τ1eiejτ2>,其中τ1是前缀,τ2是后缀,事件ei与ej属于不同的线程并且都访问同一内存,至少有一个写,那么二者之间发生了数据竞争,而τ看作是ei与ej数据竞争的证据序列;对于访问同一变量的事件ei与ek,e′i与e″i分别表示ei的前一个事件与后一个事件;同样,e′k与e″k分别表示ek的前一个事件与后一个事件,那么两者发生数据竞争即同时访问同一内存的条件ρ为:
    O(e′i)<O(ek)<O(e″i)∧O(e′k)<O(ei)<O(e″k)。
  7. 根据权利要求1所述基于多线程程序约束构建的数据竞争检测与证据 生成方法,其特征在于,所述步骤S7)中对于每一个数据竞争都会生成证据序列,以描述其触发过程。
PCT/CN2015/081062 2014-07-07 2015-06-09 基于多线程程序约束构建的数据竞争检测与证据生成方法 Ceased WO2016004808A1 (zh)

Applications Claiming Priority (2)

Application Number Priority Date Filing Date Title
CN201410320943.2A CN104077144B (zh) 2014-07-07 2014-07-07 基于多线程程序约束构建的数据竞争检测与证据生成方法
CN201410320943.2 2014-07-07

Publications (1)

Publication Number Publication Date
WO2016004808A1 true WO2016004808A1 (zh) 2016-01-14

Family

ID=51598415

Family Applications (1)

Application Number Title Priority Date Filing Date
PCT/CN2015/081062 Ceased WO2016004808A1 (zh) 2014-07-07 2015-06-09 基于多线程程序约束构建的数据竞争检测与证据生成方法

Country Status (2)

Country Link
CN (1) CN104077144B (zh)
WO (1) WO2016004808A1 (zh)

Cited By (1)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN115495208A (zh) * 2022-08-09 2022-12-20 中国人民解放军国防科技大学 一种面向中断驱动型程序的寄存器访问冲突检测方法

Families Citing this family (18)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN104077144B (zh) * 2014-07-07 2015-06-03 西安交通大学 基于多线程程序约束构建的数据竞争检测与证据生成方法
CN105183655B (zh) * 2015-09-25 2017-12-12 南京大学 基于预测性分析的安卓应用程序数据竞争检测
US10140150B2 (en) * 2016-02-08 2018-11-27 Microsoft Technology Licensing, Llc Thread diversion awaiting log call return
CN105955877B (zh) * 2016-04-19 2017-03-29 西安交通大学 一种基于符号计算的动态并行程序污点分析方法
CN106201732B (zh) * 2016-07-18 2019-05-14 电子科技大学 在粒子模拟算法并行中去除数据竞争的线分配并行方法
CN106227656B (zh) * 2016-07-18 2018-07-17 中国科学院软件研究所 一种基于硬件和线程本地时序的数据竞争采样检测方法
CN106294169B (zh) * 2016-08-17 2018-08-03 华中科技大学 一种基于符号执行虚拟机的数据竞争检测与重放方法
CN106598845B (zh) * 2016-11-18 2019-04-12 华中科技大学 一种基于块偏序的动态数据竞争检测方法及系统
CN107992324A (zh) * 2017-12-22 2018-05-04 南京大学 一种基于约束求解的代码搜索方法
CN110059014B (zh) * 2019-04-28 2022-09-27 西安邮电大学 一种并发程序数据竞争指令级定位方法
CN110083539B (zh) * 2019-04-29 2020-11-13 广州华多网络科技有限公司 一种数据竞争检测方法及装置
CN111431737B (zh) * 2020-03-02 2022-07-12 苏州市职业大学 一种检测软件定义网络中数据竞争的预测性方法
CN111858361B (zh) * 2020-07-23 2023-07-21 中国人民解放军国防科技大学 一种基于预测和并行验证策略的原子性违例缺陷检测方法
CN115080374B (zh) * 2021-03-11 2024-07-02 中国科学院软件研究所 一种基于偏序关系的通用并发缺陷检测方法及系统
CN113407447B (zh) * 2021-06-15 2022-09-27 西安邮电大学 一种自适应轻量级动态混合数据竞争检测方法
CN115981999B (zh) * 2022-11-28 2025-11-14 中科天齐(山西)软件安全技术研究院有限公司 一种程序并发缺陷检测方法、装置、存储介质及电子设备
CN115794592B (zh) * 2023-02-10 2023-07-04 南京邮电大学 一种基于高阶函数的数据竞争检测方法
CN116383076A (zh) * 2023-04-11 2023-07-04 中国科学院软件研究所 兼容控制流变化的基于偏序关系的通用并发缺陷检测方法及系统

Citations (4)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
US20090282288A1 (en) * 2008-05-08 2009-11-12 Nec Laboratories America, Inc. Dynamic model checking with property driven pruning to detect race conditions
CN102760095A (zh) * 2011-04-25 2012-10-31 清华大学 基于静态共享变量识别的动态数据竞争检测方法
CN103488563A (zh) * 2013-09-05 2014-01-01 龙芯中科技术有限公司 并行程序的数据竞争检测方法、装置及多核处理系统
CN104077144A (zh) * 2014-07-07 2014-10-01 西安交通大学 基于多线程程序约束构建的数据竞争检测与证据生成方法

Family Cites Families (2)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
US8719507B2 (en) * 2012-01-04 2014-05-06 International Business Machines Corporation Near neighbor data cache sharing
US20130275951A1 (en) * 2012-04-16 2013-10-17 International Business Machines Corporation Race detection for web applications

Patent Citations (4)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
US20090282288A1 (en) * 2008-05-08 2009-11-12 Nec Laboratories America, Inc. Dynamic model checking with property driven pruning to detect race conditions
CN102760095A (zh) * 2011-04-25 2012-10-31 清华大学 基于静态共享变量识别的动态数据竞争检测方法
CN103488563A (zh) * 2013-09-05 2014-01-01 龙芯中科技术有限公司 并行程序的数据竞争检测方法、装置及多核处理系统
CN104077144A (zh) * 2014-07-07 2014-10-01 西安交通大学 基于多线程程序约束构建的数据竞争检测与证据生成方法

Cited By (1)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN115495208A (zh) * 2022-08-09 2022-12-20 中国人民解放军国防科技大学 一种面向中断驱动型程序的寄存器访问冲突检测方法

Also Published As

Publication number Publication date
CN104077144B (zh) 2015-06-03
CN104077144A (zh) 2014-10-01

Similar Documents

Publication Publication Date Title
WO2016004808A1 (zh) 基于多线程程序约束构建的数据竞争检测与证据生成方法
Netzer et al. Improving the accuracy of data race detection
CN104077226B (zh) 基于程序约束构建的多线程程序输出唯一性检测与证据生成方法
CN103559122B (zh) 基于程序行为切片的测试案例约减方法
CN105955877B (zh) 一种基于符号计算的动态并行程序污点分析方法
Huang et al. Clap: Recording local executions to reproduce concurrency failures
Şerbănuţă et al. Maximal causal models for sequentially consistent systems
CN103365776B (zh) 基于确定性重放的并行系统弱一致性的验证方法和系统
Cai et al. Magiclock: Scalable detection of potential deadlocks in large-scale multithreaded programs
CN112860545B (zh) 一种软件缺陷检测方法与装置
US8141082B2 (en) Node-based representation of multi-threaded computing environment tasks, and node-based data race evaluation
TehraniJamsaz et al. Deeprace: A learning-based data race detector
CN110851353B (zh) 一种基于Delta Debugging与约束求解的并发程序缺陷定位方法
CN106844215B (zh) 一种基于约束求解的原子违背探测方法
Zheng et al. On performance debugging of unnecessary lock contentions on multicore processors: A replay-based approach
CN119848868B (zh) 一种基于代码并发属性图的并发漏洞检测方法
CN119271530B (zh) 一种基于多动态程序执行轨迹的软件缺陷定位方法和系统
Cai et al. LOFT: redundant synchronization event removal for data race detection
CN115438341A (zh) 提取代码循环计数器的方法、装置、存储介质和电子设备
Schwartz-Narbonne et al. Parallel assertions for debugging parallel programs
Jannesari et al. Detecting correlation violations and data races by inferring non-deterministic reads
Carril et al. Interleaving generation for data race and deadlock reproduction
CN108171016B (zh) 一种线性汇编级的可配置容错方法、计算机、计算机程序
Xu et al. Analysis of Concurrent Systems Based on Interval Order
Zhao et al. Formal Verification of Preemptive Interrupt-Driven Programs Based on Partial Order Modeling

Legal Events

Date Code Title Description
121 Ep: the epo has been informed by wipo that ep was designated in this application

Ref document number: 15819663

Country of ref document: EP

Kind code of ref document: A1

NENP Non-entry into the national phase

Ref country code: DE

32PN Ep: public notification in the ep bulletin as address of the adressee cannot be established

Free format text: NOTING OF LOSS OF RIGHTS PURSUANT TO RULE 112(1) EPC (EPO FORM 1205A DATED 12/06/17)

122 Ep: pct application non-entry in european phase

Ref document number: 15819663

Country of ref document: EP

Kind code of ref document: A1