Summary of the invention
The present invention is the weak point overcoming prior art existence, a kind of abnormal information method for building up based on CFG is proposed, to the deficiency of compiler pin function can be supplemented, and point out the nest relation in abnormal information between try-catch code block, thus the abnormal information in C++ program must be obtained quickly and accurately.
The present invention is that technical solution problem adopts following technical scheme:
A kind of abnormal information method for building up based on CFG of the present invention is applied in the exception handling code of C++, comprises try block and corresponding one or more catch processing blocks in described exception handling code; Be characterized in, described abnormal information method for building up carries out as follows:
Step 1, to be marked by the starting position of pile pitching method all try blocks in described exception handling code, thus obtain the pitching pile code comprising label information;
Step 2, described pitching pile code to be compiled, obtain the intermediate code represented with CFG, according to described label information in described intermediate code, extract the starting position of all try blocks, thus form the chained list of the try agllutination point composition only comprising described starting position, and be set to the initial chained list of TRY_LIST;
All catch processing blocks in code between step 3, traversal are described, extract according to the starting position of each catch processing block self and end position, thus form the chained list of the catch processing block node composition comprising starting position and end position, and be set to CATCH_LIST chained list;
Step 4, travel through described CATCH_LIST chained list, the numbering subsidiary according to the starting position of catch agllutination point each in described CATCH_LIST chained list, extract all catch processing block nodes with identical numbering and be organized into a child list, thus generating the list of catch processing block, each list node in the list of described catch processing block points to the child list separately with identical numbering;
Step 5, using the numbering in the list of described catch processing block as key word, each list node to be sorted by order from small to large, form the catch processing block list after sequence;
Step 6, each list node in the catch processing block list after described sequence is carried out corresponding with each try agllutination point in the initial chained list of described TRY_LIST successively, make an a list node only corresponding try agllutination point, thus obtain the catch processing block list after correspondence;
Step 7, travel through the catch processing block list after described correspondence, judge the jump information whether having the end position of a catch processing block node to comprise self in described child list, if have, then obtain the end position of the try agllutination point corresponding with described list node according to described jump information; Otherwise, self end position of the try block corresponding with described list node is labeled as mark ∞; Thus obtain all TRY_LIST chained lists comprising starting position and end position, the preliminary abnormal information of try-catch is set up by the catch processing block list after described correspondence and described TRY_LIST chained list;
Step 8, suppose to include n try node in described TRY_LIST chained list, be designated as X={x
1, x
2..., x
i..., x
n, x
irepresent arbitrary try node, 1≤i≤n; By described arbitrary try node x
istarting position and end position be designated as respectively
with
Step 9, travel through each try node of described TRY_LIST chained list, judge the try node x of current traversal
iwhether be empty; If so, then represent that the set membership in described TRY_LIST chained list between try node has been set up, thus make described preliminary abnormal information generate complete abnormal information, and perform step 11; Otherwise, perform step 10;
Step 10, judge try node x
jwhether be empty, i+1≤j≤n; If it is empty, then the value of i+1 is assigned to i, and performs step 9; Otherwise, judge try node x
iend position
whether sign of inclusion ∞, if comprise, is then assigned to i by the value of i+1, and performs step 9; Otherwise, judge try node x
jend position
whether sign of inclusion ∞, if comprise, is then assigned to j by the value of j+1, and performs step 10; Otherwise, by try node x
istarting position
and end position
respectively with try node x
i+1starting position
and end position
compare successively, if
and
then by try node x
ias try node x
jfather node, if
and
then by try node x
jas try node x
ifather node; The value of j+1 is assigned to j, and performs step 10;
Step 11, using described complete abnormal information as abnormal information corresponding to described abnormality code.
Compared with prior art, beneficial effect of the present invention is:
The present invention by source code pitching pile, solves the problem can not locating try starting position in the intermediate code represented with CFG; By compiler technologies, maintain the semanteme that source program is complete, maintain an abnormal information accurately; By simple sort algorithm, maintain the relation of top-down try-catch process code in source program; By the relation between different interval starting position and end position, solve the nest relation between try-catch process code; After tested, employing the inventive method can maintain all abnormal informations in C++ program fast and accurately.
Embodiment
The present invention is based on based on the intermediate code that represents by CFG, the abnormality processing portion generated by CFG, reduction generates the abnormality processing data structure of each function, so that execute exception process code section; The pin function provided by GCC compiler obtains preliminary abnormal information, in conjunction with the pitching pile technology of source code, explores abnormal information identification and the method for building up of complete set, to obtain complete abnormal information; Core code is comprised to the acquisition of corresponding relation between try code block and catch code process block in abnormal information, and the nest relation between try-catch code block.The present invention is once supplementing very well static analysis, by identifying and the existing abnormal information of construction procedures, thus obtains possible abnormal generating process, for static father code analysis is prepared before program is run.
Particularly, a kind of abnormal information method for building up based on CFG carries out according to the following procedure:
Step 1, to be marked by the starting position of pile pitching method all try blocks in described exception handling code, thus obtain the pitching pile code comprising label information;
Step 2, described pitching pile code to be compiled, obtain the intermediate code represented with CFG, according to described label information in described intermediate code, extract the starting position of all try blocks, thus form the chained list of the try agllutination point composition only comprising described starting position, and be set to the initial chained list of TRY_LIST;
All catch processing blocks in code between step 3, traversal are described, extract according to the starting position of each catch processing block self and end position, thus form the chained list of the catch processing block node composition comprising starting position and end position, and be set to CATCH_LIST chained list;
Step 4, travel through described CATCH_LIST chained list, the numbering subsidiary according to the starting position of catch agllutination point each in described CATCH_LIST chained list, extract all catch processing block nodes with identical numbering and be organized into a child list, thus generating the list of catch processing block, each list node in the list of described catch processing block points to the child list separately with identical numbering;
Step 5, using the numbering in the list of described catch processing block as key word, each list node to be sorted by order from small to large, form the catch processing block list after sequence;
Step 6, each list node in the catch processing block list after described sequence is carried out corresponding with each try agllutination point in the initial chained list of described TRY_LIST successively, make an a list node only corresponding try agllutination point, thus obtain the catch processing block list after correspondence;
Step 7, travel through the catch processing block list after described correspondence, judge the jump information whether having the end position of a catch processing block node to comprise self in described child list, if have, then obtain the end position of the try agllutination point corresponding with described list node according to described jump information; Otherwise, self end position of the try block corresponding with described list node is labeled as mark ∞; Thus obtain all TRY_LIST chained lists comprising starting position and end position, the preliminary abnormal information of try-catch is set up by the catch processing block list after described correspondence and described TRY_LIST chained list;
Step 8, suppose to include n try node in described TRY_LIST chained list, be designated as X={x
1, x
2..., x
i..., x
n, x
irepresent arbitrary try node, 1≤i≤n; By described arbitrary try node x
istarting position and end position be designated as respectively
with
Step 9, travel through each try node of described TRY_LIST chained list, judge the try node x of current traversal
iwhether be empty; If so, then represent that the set membership in described TRY_LIST chained list between try node has been set up, thus make described preliminary abnormal information generate complete abnormal information, and perform step 11; Otherwise, perform step 10;
Step 10, judge try node x
jwhether be empty, i+1≤j≤n; If it is empty, then the value of i+1 is assigned to i, and performs step 9; Otherwise, judge try node x
iend position
whether sign of inclusion ∞, if comprise, is then assigned to i by the value of i+1, and performs step 9; Otherwise, judge try node x
jend position
whether sign of inclusion ∞, if comprise, is then assigned to j by the value of j+1, and performs step 10; Otherwise, by try node x
istarting position
and end position
respectively with try node x
i+1starting position
and end position
compare successively, if
and
then by try node x
ias try node x
jfather node, if
and
then by try node x
jas try node x
ifather node; The value of j+1 is assigned to j, and performs step 10;
Step 11, using described complete abnormal information as abnormal information corresponding to described abnormality code.
In the present embodiment, conveniently set forth, with anomaly source code in simple C++ program for such as shown in Fig. 1; Wherein, line number is shown in a leftmost list; Comprise nested try-catch code block in Fig. 1 structure, in first try code block, comprise second try code block, corresponding two the catch processing blocks of each try code block.
The code of Fig. 1 is the code as do not added italicized item in Fig. 2 by the intermediate code schematic configuration represented with CFG that compiler generates, two parts are divided in fact in this result, top is divided into the normal code block comprising try, and bottom is divided into all catch process code blocks.
First our pack processing contains the normal code block of try, the result that the known compiling of code not adding italicized item from Fig. 2 generates afterwards is without the try key word in source code, therefore pitching pile code in source code is needed, namely by step 1, before (1), (2) position of Fig. 1, following code is inserted: { cout<< " try begin "; , the starting position of this coded representation try, the intermediate code represented with CFG that such Fig. 2 generates after being pitching pile.From Fig. 2, the code of italicized item can identify the starting position of try, and form initialization TRY_LIST chained list, as shown in Figure 3, Fig. 3 is the operating result of step 2.
In Fig. 3 initialization TRY_LIST chained list each node in the numeral number of starting position pitching pile code corresponding place bb block replace (during as occurred " <bb 2> " in fig. 2, the numeral number of expression bb block is 2), prepare for step 10 like this.
Connect catch process code blocks all in lower process Fig. 2, according to step 3, by starting position and the end position of each catch processing block, form the CATCH_LIST chained list as Fig. 4, each node in this chained list attached in starting position numbering (as in fig. 2 the 12nd row occur " _ _ builtin_eh_pointer (2) ", represent subsidiary and be numbered 2), jump information has been attached (if yes at end position, as " goto<bb 5> " that the 17th row in Fig. 2 occurs, represent and jump to the 5th bb block, represent with numeral 5).
Traversal CATCH_LIST chained list, according to step 4 and step 5, with the starting position of node each in CATCH_LIST chained list subsidiary be numbered key word, according to from small to large order, form the catch processing block list after sequence, each node as the list of Fig. 5, catch processing block contains the numbering of band numeral and points to catch processing block child list.
According to step 6, to obtain in fact in Fig. 5 and Fig. 3 one_to_one corresponding between each node.
According to step 7, the end position of each node in initialized TRY_LIST chained list can be determined, and finally define the TRY_LIST chained list comprising starting position and end position.The preliminary abnormal information of try-catch can be defined, as do not added the part of dotted line in Fig. 6 by step 6 and step 7.
According to the formalized description of step 8, step 9 and step 10, relation between the lane place judging try 1 node and try 2 node, due to 3>2 and 55, therefore the father node of try 2 node points to try 1 node, as the dotted portion added in Fig. 6, indicating try 2 node is be nested in try 1 inter-node.
The try code that can be obtained in each try code block corresponding diagram 2 by the starting position of node each in TRY_LIST chained list and end position in Fig. 6 distributes, and the nest relation between try code block; Obtain catch code in each catch process code block corresponding diagram 2 by the list of catch processing block to distribute, result in formation of the finally complete abnormal information that code in Fig. 1 is corresponding.
For C++ code more complicated in actual items, the intermediate code represented with CFG generated after compiler compiling is similar with the result of Fig. 2 in essence on framework.Therefore, comprehensive use is of the present invention based on after the abnormal information method for building up of CFG, after tested, for the engineering code that any C++ develops, can maintain all abnormal informations in C++ program fast and accurately.