The content of the invention
The present invention is the weak point for overcoming prior art to exist, and proposes a kind of abnormal information method for building up based on CFG,
To the deficiency of compiler pin function can be supplemented, and the nest relation in abnormal information between try-catch code blocks is pointed out,
So as to must quickly and accurately obtain the abnormal information in C++ programs.
The present invention is adopted the following technical scheme that to solve technical problem:
A kind of abnormal information method for building up based on CFG of the present invention, is applied in C++ exception handling code, described
Try blocks and corresponding one or more catch process blocks are included in exception handling code;It is characterized in, the abnormal information is built
Cube method is to carry out as follows:
Step 1, by pile pitching method, the starting position of all try blocks is marked in the exception handling code, from
And obtain the pitching pile code comprising label information;
Step 2, the pitching pile code is compiled, the intermediate code represented with CFG obtained, in the intermediate code
According to the label information, the starting position of all try blocks is extracted, so as to form the try agllutinations for only including the starting position
The chained list of point composition, and it is set to the initial chained lists of TRY_LIST;
All catch process blocks in code between step 3, traversal are described, according to the start bit of each catch process blocks itself
Put and extracted with end position, so as to form the chain of the catch process blocks node composition comprising starting position and end position
Table, and it is set to CATCH_LIST chained lists;
Step 4, the traversal CATCH_LIST chained lists, according to each catch agllutination points in the CATCH_LIST chained lists
The subsidiary numbering in starting position, extract all catch process block nodes with identical numbering and be organized into a child list,
So as to generate catch process block lists, each the list node in the catch process blocks list is all pointed to each with identical
The child list of numbering;
Step 5, using the numbering in the catch process blocks list as keyword to each list node by from small to large
Order be ranked up, formed sequence after catch process block lists;
It is step 6, each list node and the TRY_LIST in the catch process block lists after the sequence is initial
Each try agllutinations point in chained list carries out correspondence successively so that a list node only corresponds to a try agllutinations point, so as to obtain
Catch process block lists after must corresponding to;
Whether catch process block lists after step 7, the traversal correspondence, judge there is a catch in the child list
The end position of process block node includes the jump information of itself, if so, then being obtained and the list according to the jump information
The end position of the corresponding try agllutinations point of node;Otherwise, the end position of try blocks corresponding with the list node is marked
For mark ∞;So as to obtain all TRY_LIST chained lists comprising starting position and end position, by the catch after the correspondence
Process block list and the TRY_LIST chained lists set up the preliminary abnormal information of try-catch;
Include n try node in step 8, the hypothesis TRY_LIST chained lists, be designated as X={ x1,x2,…,xi,…,
xn, xiRepresent any try nodes, 1≤i≤n;By any try nodes xiStarting position and end position be designated as respectivelyWith
Step 9, each the try node for traveling through the TRY_LIST chained lists, judge the try nodes x of current traversaliWhether be
It is empty;If, then it represents that the set membership in the TRY_LIST chained lists between try nodes is set up and is completed, so that described first
Step abnormal information generates complete abnormal information, and performs step 11;Otherwise, step 10 is performed;
Step 10, judge try nodes xjWhether it is empty, i+1≤j≤n;If it is empty, then i+1 value is assigned to i, and performed
Step 9;Otherwise, try nodes x is judgediEnd positionWhether sign of inclusion ∞, if comprising, the value of i+1 is assigned to i, and
Perform step 9;Otherwise, try nodes x is judgedjEnd positionWhether sign of inclusion ∞, if comprising the value of j+1 is assigned to
J, and perform step 10;Otherwise, by try nodes xiStarting positionAnd end positionRespectively with try nodes xi+1Beginning
PositionAnd end positionIt is compared successively, ifAndThen by try nodes xiAs try nodes xj's
Father node, ifAndThen by try nodes xjAs try nodes xiFather node;The value of j+1 is assigned to j, and is held
Row step 10;
Step 11, using the complete abnormal information as the corresponding abnormal information of the abnormality code.
Compared with prior art, the beneficial effects of the present invention are:
The present invention to be solved and can not position try starting positions in the intermediate code represented with CFG by source code pitching pile
Problem;By compiler technologies, the complete semanteme of source program is maintained, an accurate abnormal information is maintained;By simple
Sort algorithm, maintain the relation that top-down try-catch in source program processes code;Pass through different interval beginnings
Relation between position and end position, solves the nest relation between try-catch treatment codes;After tested, using this
Inventive method can quickly and accurately maintain all abnormal informations in C++ programs.
Embodiment
The present invention is based on based on the intermediate code that CFG is represented, the abnormality processing portion generated by CFG, also primary
Into the abnormality processing data structure of each function, to perform exception handling code part;The plug-in unit provided by GCC compilers
The preliminary abnormal information of gain-of-function, with reference to the pitching pile technology of source code, explores the abnormal information identification of complete set and builds
Cube method, to obtain complete abnormal information;Include in abnormal information at try code blocks and catch codes for core code
The acquisition of corresponding relation between reason block, and the nest relation between try-catch code blocks.The present invention is to static analysis
Once supplement very well, by identification and the existing abnormal information of construction procedures, so as to obtain possible different before program operation
Normal generating process, is that static source code analysis are prepared.
Specifically, a kind of abnormal information method for building up based on CFG is to carry out according to the following procedure:
Step 1, by pile pitching method, the starting position of all try blocks is marked in the exception handling code, from
And obtain the pitching pile code for including label information;
Step 2, the pitching pile code is compiled, the intermediate code that acquisition is represented with CFG, in the intermediate code
According to the label information, the starting position of all try blocks is extracted, so as to form the try agllutinations for only including the starting position
The chained list of point composition, and it is set to the initial chained lists of TRY_LIST;
All catch process blocks in code between step 3, traversal are described, according to the start bit of each catch process blocks itself
Put and extracted with end position, so as to form the chain of the catch process blocks node composition comprising starting position and end position
Table, and it is set to CATCH_LIST chained lists;
Step 4, the traversal CATCH_LIST chained lists, according to each catch agllutination points in the CATCH_LIST chained lists
The subsidiary numbering in starting position, extract all catch process block nodes with identical numbering and be organized into a child list,
So as to generate catch process block lists, each the list node in the catch process blocks list is all pointed to each with identical
The child list of numbering;
Step 5, using the numbering in the catch process blocks list as keyword to each list node by from small to large
Order be ranked up, formed sequence after catch process block lists;
It is step 6, each the list node in the catch process block lists after the sequence is initial with the TRY_LIST
Each try agllutinations point in chained list carries out correspondence successively so that a list node only corresponds to a try agllutinations point, so as to obtain
Catch process block lists after must corresponding to;
Whether catch process block lists after step 7, the traversal correspondence, judge there is a catch in the child list
The end position of process block node includes the jump information of itself, if so, then being obtained and the list according to the jump information
The end position of the corresponding try agllutinations point of node;Otherwise, the end position of try blocks corresponding with the list node is marked
It is mark ∞;So as to obtain all TRY_LIST chained lists comprising starting position and end position, by the catch after the correspondence
Process block list and the TRY_LIST chained lists set up try-catch preliminary abnormal information;
Include n try node in step 8, the hypothesis TRY_LIST chained lists, be designated as X={ x1,x2,…,xi,…,
xn, xiRepresent any try nodes, 1≤i≤n;By any try nodes xiStarting position and end position be designated as respectivelyWith
Step 9, each the try node for traveling through the TRY_LIST chained lists, judge the try nodes x of current traversaliWhether be
It is empty;If, then it represents that the set membership in the TRY_LIST chained lists between try nodes, which is set up, to be completed, so that described first
Walk abnormal information and generate complete abnormal information, and perform step 11;Otherwise, step 10 is performed;
Step 10, judge try nodes xjWhether it is empty, i+1≤j≤n;If it is empty, then the value of i+1 is assigned to i, and is performed
Step 9;Otherwise, try nodes x is judgediEnd positionWhether sign of inclusion ∞, if comprising, the value of i+1 is assigned to i, and
Perform step 9;Otherwise, try nodes x is judgedjEnd positionWhether sign of inclusion ∞, if comprising the value of j+1 is assigned to
J, and perform step 10;Otherwise, by try nodes xiStarting positionAnd end positionRespectively with try nodes xi+1Beginning
PositionAnd end positionIt is compared successively, ifAndThen by try nodes xiAs try nodes xj's
Father node, ifAndThen by try nodes xjIt is used as try nodes xiFather node;The value of j+1 is assigned to j, and is held
Row step 10;
Step 11, using the complete abnormal information as the corresponding abnormal information of the abnormality code.
In the present embodiment, illustrate for convenience, by taking abnormal source code in simple C++ programs as an example as shown in Figure 1;Its
In, a leftmost row represent line number;Comprising nested try-catch code blocks in Fig. 1 structures, in first try code block
Comprising second try code block, each try code block two catch process blocks of correspondence.
The code of Fig. 1 is as do not added italic in Fig. 2 by the intermediate code schematic configuration represented with CFG that compiler is generated
Partial code, two parts are divided into the substantial result, and top is divided into the normal code block comprising try, and bottom is divided into all
Catch treatment code block.
We process the normal code block comprising try first, do not add the code of italicized item raw after understanding to compile from Fig. 2
Into result without the try keywords in source code, it is therefore desirable to the pitching pile code in source code, i.e., by step 1,
Following code is inserted before Fig. 1 (1), (2) position:{cout<<“trybegin”;, the code represents the starting position of try,
So Fig. 2 is the intermediate code represented with CFG generated after pitching pile.The code of italicized item can identify opening for try from Fig. 2
Beginning position, formed initialization TRY_LIST chained lists, as shown in figure 3, Fig. 3 is the operating result of step 2.
Starting position bb blocks where pitching pile code correspondence in each node of TRY_LIST chained lists are initialized in Fig. 3
Numeral numbering replacing (as occurred in fig. 2 "<bb 2>" when, the numeral numbering for representing bb blocks is 2), so for step 10 is done
Prepare.
All of catch treatment code block in lower treatment Fig. 2 is connect, according to step 3, by opening for each catch process block
Beginning position and end position, form the CATCH_LIST chained lists such as Fig. 4, each node in the chained list is subsidiary in starting position
Numbering (such as the 12nd row occurs in fig. 2 " _ _ builtin_eh_pointer (2) ", represents that subsidiary numbering is 2), to terminate
Position has attached jump information (if yes, such as " the goto of the 17th row appearance in Fig. 2<bb 5>", expression jumps to the 5th bb
Block, is represented with numeral 5).
Traversal CATCH_LIST chained lists, according to step 4 and step 5, with the beginning of each node in CATCH_LIST chained lists
The subsidiary numbering in position is keyword, according to from small to large order, formed sequence after catch process block lists, such as Fig. 5,
Each node of catch process block lists contains the numbering with numeral and points to catch process block child lists.
According to step 6, substantially obtain in Fig. 5 and Fig. 3 and corresponded between each node.
According to step 7, it may be determined that the end position of each node in the TRY_LIST chained lists of initialization, and ultimately form
TRY_LIST chained lists comprising starting position and end position.The preliminary different of try-catch can be formd by step 6 and step 7
The part of dotted line is not added in normal information, such as Fig. 6.
According to the formalized description of step 8, step 9 and step 10, the position area of the nodes of try 1 and the nodes of try 2 is judged
Between relation, due to 3>2 and 55, therefore the father node of the nodes of try 2 points to the nodes of try 1, the dotted line part added in such as Fig. 6
Point, it is to be nested in the inter-nodes of try 1 to indicate the nodes of try 2.
Each try code block can be obtained by the starting position of each node in TRY_LIST chained lists and end position in Fig. 6
Try codes distribution in corresponding diagram 2, and the nest relation between try code blocks;Obtain each by the list of catch process blocks
Catch codes distribution in catch processing code blocks corresponding diagram 2, it is corresponding final complete result in formation of the code in Fig. 1
Abnormal information.
For C++ codes more complicated in actual items, the middle generation represented with CFG generated after compiler is compiled
Code, is similar on framework substantially with Fig. 2 result.Therefore, the comprehensive abnormal information based on CFG using the present invention is built
After cube method, after tested, for the engineering code of any C++ exploitations, the institute in C++ programs can quickly and be accurately maintained
There is abnormal information.