An improved source code slicing method based on context edge relationship

By expanding the pointcut type and introducing the edge tree data structure, the source code slicing method is improved, which solves the problem of insufficient and unbalanced slicing data in the existing technology, and improves the performance and efficiency of the vulnerability detection model.

CN117171029BActive Publication Date: 2024-09-17SICHUAN UNIV
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202311128781.8
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2023-09-04
Publication Date
2024-09-17
Estimated Expiration
2043-09-04

AI Technical Summary

Technical Problem

The amount of sliced ​​data generated by existing source code vulnerability detection methods is limited and unbalanced, resulting in long model training time, large amount of calculation, and insufficient data set quality, which affects detection performance.

Method used

An improved source code slicing method based on context edge relationships is adopted. By expanding pointcut types, introducing edge tree data structures and additional variable declaration statements, more lines of code that are highly related to pointcuts are extracted to form richer slices and balance the positive and negative. The number of samples reduces the graph traversal depth.

Benefits of technology

It significantly improves the quantity and quality of sample data sets, reduces the calculation amount and time of model training, and improves the performance of downstream source code vulnerability detection tasks.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN117171029B
    Figure CN117171029B
Patent Text Reader

Abstract

The present invention discloses an improved source code slicing method based on context edge relationship, which can identify more cut points. For the same cut point, the improved slicing method can also extract more vulnerability-related code lines, improve the quantity and quality of sample data sets, and balance the number of positive and negative samples. The method comprises: using the joern-parse module of the joern tool to preliminarily parse the source code file to obtain the corresponding CPG; extracting the nodes of all code lines therefrom, and then extracting the edges containing these nodes and of the types of "REACHES" and "CONTROLS" to form a PDG; according to the cut point type, selecting the code line of the corresponding cut point type as the cut point; for each cut point, extracting the code line highly related to the vulnerability to form a final slice; appending the relevant variable declaration statement to the final slice; replacing the user-defined variable and function names in the source code in a general form; marking each slice as 1 or 0 according to whether each slice contains the vulnerability line; and saving it locally in the form of json.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the fields of source code vulnerability detection technology, and in particular to an improved source code slicing method based on context edge relations. Background Art

[0002] With the development of computer technology, many applications have been widely deployed and applied in various fields of life. However, most software is secondary development based on existing frameworks and modules. There are problems such as non-standard development process and short development cycle in the development process, which leads to similar vulnerabilities widely existing in different software. In recent years, the number of public vulnerabilities has increased year by year, and a large number of software security incidents have caused irreparable losses to users and systems.

[0003] In response to the problem of rampant vulnerabilities, some researchers have proposed using deep learning to establish a vulnerability detection model for source code vulnerabilities, deploying the detection model during the development cycle, focusing on security issues during code development, achieving "security left shift", and reducing the cost of subsequent vulnerability repairs.

[0004] In the current research field of source code vulnerability detection, source code is often converted into a graph representation so that certain features of the source code are explicitly displayed in the graph. The most commonly used graph representation is the Program Dependency Graph (PDG).

[0005] In the specific implementation, the mainstream way to generate PDG is to extract REACHES (data dependency) and CONTROLS (control dependency) edges from the code property graph (CodeProperty Graph) generated by the Joern tool to construct PDG, explicitly showing the data dependency and control dependency relationship between different code lines between source codes.

[0006] In order to generate training samples from the test code, it is necessary to identify the code lines that are prone to vulnerabilities from the source code as the cut points. After obtaining the cut points, almost all studies use forward and backward slicing technology to reduce the size of the source code, and only select code lines that are highly relevant to the cut points to form different slices. Each slice is a sample, which is sent to the model for training after vectorization. However, the slices generated by this method have problems such as limited data volume, unbalanced data sets, and low slice quality. In addition, since forward slicing and backward slicing often perform depth-first traversal of the graph to select the corresponding code lines, this will cause the number of convolutional layers of the downstream graph convolutional network to be large, which makes the computational complexity of the entire model large, the training time of the model is long, and the power consumption is high. The quality of the data set severely limits the detection performance of the model. Summary of the invention

[0007] The purpose of the present invention is to provide an improved source code slicing method based on context edge relationships, which can identify more cut points. For the same cut point, the improved slicing method can also extract more vulnerability-related code lines, improve the quantity and quality of sample data sets, balance the number of positive and negative samples, and improve the model performance of downstream source code vulnerability detection tasks. In particular, if the downstream task uses a graph convolutional network, under the premise of achieving similar model performance, this method can also greatly reduce the amount of calculation required for model training and the time spent.

[0008] The present invention is implemented by the following technical solution: an improved source code slicing method based on context edge relationship, comprising the following steps:

[0009] 1) Preliminary analysis of source code: Use the joern-parse module of the joern tool to preliminarily parse the source code file and obtain the corresponding CPG;

[0010] 2) Generate PDG: Extract all code line nodes from CPG, and then extract the edges containing these nodes and of type "REACHES" and "CONTROLS" to form PDG;

[0011] 3) Extract cut points: According to the cut point type, select the code line of the corresponding cut point type from PDG as the cut point;

[0012] 4) Slicing: For each cut point, extract the code lines that are highly relevant to the vulnerability to form the final slice;

[0013] 5) Append variable declaration statements: Append related variable declaration statements to the final slice;

[0014] 6) Anonymize user variables: Replace user-defined variables and function names in the source code with general forms; this operation can eliminate the interference caused by variables and functions with similar functions but different names on model learning.

[0015] 7) Labeling: Depending on whether each slice contains a vulnerable row, each slice is marked as 1 (a slice with a vulnerability) or 0 (a slice without a vulnerability);

[0016] 8) Persistent storage: Save the data locally in JSON format as a data set for model training. The fields included are:

[0017] keyLine: a number, corresponding to the code line of the cut point of this slice;

[0018] nodes-lineNo: a list containing all the code lines selected by this slice;

[0019] edges-No: a list storing the edges between the code lines selected by this slice in the form of node pairs;

[0020] nodes-line: a list containing the text content of each code line;

[0021] nodes-line-sym: a list containing the anonymized text content of each code line;

[0022] function_name: string, the name of the function to which this table cut point belongs;

[0023] criterionType: string, table name, the cut point type of this slice;

[0024] target: a number, indicating whether this slice of the table name contains the vulnerable row; 0 means not, 1 means included;

[0025] testcaseID: A number that uniquely represents this slice.

[0026] In order to further better implement the improved source code slicing method based on context edge relationship described in the present invention, the following setting method is particularly adopted: the step 1) is specifically as follows: for each source code file, a corresponding CPG will be generated, the joern tool uses the csv file format to represent the graph structure of the CPG, each CPG uses nodes.csv to represent the nodes existing in the graph structure and edges.csv to describe the relationship between different nodes; nodes.csv has fields such as key, code, type, and location. Among them, key is the unique identifier of each node, code is the code text form of the corresponding code line, and type is the type of this node. If this node is a node at the code line granularity, the location field of this node will indicate the code line to which it belongs. Edges.csv has only three fields, namely start, end, type, and var. Start and end use the key field of nodes.csv to indicate the start and end nodes of this edge respectively. The type field indicates the type of the edge connecting the two nodes. If the type of the edge is data dependency, the var field will indicate the identifier of the variable corresponding to this data dependency, and the var field of other types of edges is empty.

[0027] In order to better implement the improved source code slicing method based on context edge relationship described in the present invention, the following setting is particularly adopted: the step 2) is specifically as follows:

[0028] 2.1) For each set of nodes.csv and edges.csv (i.e., for each CPG), extract the nodes in nodes.csv that represent the code lines;

[0029] 2.2) The extracted set of nodes representing code lines is recorded as nodes_list; wherein the location field of the node representing the code line has content indicating the code line to which it belongs, while the location field of the node not representing the code line is empty;

[0030] 2.3) From edges.csv, select the edges whose start and key both appear in nodes_list and whose type is REACHES and CONTROLS. The extracted edge set is called edges_list.

[0031] 2.4) Then nodes_list and edges_list together form PDG; where the nodes are each line of code in the source code, and the edges are the data dependencies and control dependencies between different lines of code.

[0032] The cut point is the starting point of slicing, and slicing is the process of selecting code lines related to the cut point. Currently, the mainstream method only selects four types of slicing: SensiCall, operator, Pointer, and ArrayIndexing. The present invention improves the existing pointcut types and also adds additional new pointcut types. In order to better implement the improved source code slicing method based on context edge relationship described in the present invention, the following setting method is particularly adopted: the pointcut types include SensiCall type (functions that are prone to vulnerabilities are screened out from standard C language library functions. Any code line that calls these functions is extracted as SensiCall (indicating sensitive function call) pointcut), operator type (any code line containing "+, -, *, / " operations will be extracted as operator (indicating arithmetic operator) pointcut), MemberAccess type (any code line that accesses structure members through "." will be extracted as MemberAccess (indicating member variable access) pointcut), RefandDeRef type (any code line that uses "&" to reference a pointer and uses "*" to dereference a pointer will be extracted as RefandDeRef (indicating reference and dereference) pointcut), ArrayIndexing type (any code line that uses "[]" to access array members will be extracted as Ar rayIndexing (indicates array index) cut point), PointerDecl type (any pointer declaration code line will be extracted as PointerDecl (indicates pointer definition) cut point), ex_SensiCall (any non-SensiCall code line that uses pointer variables as parameters will be extracted as ex_SensiCall (indicates extended sensitive function call) cut point); Among them, SensiCall type, operator type, MemberAccess type, RefandDeRef type, ArrayIndexing type, PointerDecl type are scanned once for all nodes, and the cut points are selected through the type, code and other fields in nodes.csv; ex_SensiCall type scans all nodes twice, selects the function call among them, and checks: if this function call uses a pointer variable and does not belong to SensiCall, then this function call is appended to the final slicing result in the form of ex_SensiCall.

[0033] ex_SensiCall is defined as a user-level sensitive function (also an extended sensitive function call). Any pointer operation and non-SensiCall function call will be regarded as ex_SensiCall.

[0034] In order to further better implement the improved source code slicing method based on context edge relationship described in the present invention, the following setting is particularly adopted: the step 4) includes the following steps:

[0035] 4.1) According to the tangent point, select any edge containing the tangent point as the root of the edge tree, denoted as root_e, and construct the corresponding edge tree;

[0036] 4.2) In the edge tree, select an edge that is directly adjacent to root_e, or one level indirectly adjacent to root_e;

[0037] 4.3) Extract the nodes corresponding to each edge from these selected edges, remove duplicates and put them into slice_nodes;

[0038] 4.4) For each node added in step 4.3), repeat steps 4.1) to 4.3) until no new nodes are added to slice_nodes;

[0039] 4.5) For each node in slice_nodes, add the corresponding edge according to PDG;

[0040] 4.6) Rearrange the selected nodes according to the order of the code lines. This set of nodes and edges is the final slice of the corresponding tangent point.

[0041] In order to better implement the improved source code slicing method based on context edge relationship described in the present invention, the following setting method is particularly adopted: the step 5) is specifically: using joern's DEF and USE edges, find the declaration statement of the corresponding variable and append it to the slice. If this variable is in the current slice, append the node to which it belongs to the appropriate position of the slice (the slices are sorted in ascending order).

[0042] In order to better implement the improved source code slicing method based on context edge relationship described in the present invention, the following setting method is particularly adopted: the step 6) is specifically: scanning the source code of each slice, identifying all user-defined variable positions therein, and counting the number of each user-defined variable; replacing the user-defined variable name with VARx, x∈{0,1,...,i} in the order of appearance; replacing the user-defined function name with FUNCy, y∈{0,1,...,i}.

[0043] In order to better implement the improved source code slicing method based on context edge relationship described in the present invention, the following setting method is particularly adopted: the slice marked as 1 represents a slice containing a vulnerability, and the slice marked as 0 represents a slice without a vulnerability.

[0044] Compared with the prior art, the present invention has the following advantages and beneficial effects:

[0045] The present invention expands the types of slicing points, so that the same number of source code files can generate more slicing samples, and more slicing samples can train better data sets.

[0046] When slicing, the present invention selects code lines that are highly concentrated around the cut point. The traditional slicing method, similar to the depth-first traversal, will select many code lines that are far away from the cut point but contain little useful information, and cut out a "slender" slice. However, the present method, similar to the breadth-first traversal, selects code lines that are closely distributed around the code lines and are highly related, and cuts out a "fat and wide" slice.

[0047] Traditional slicing specifically uses forward slicing and backward slicing techniques, but there is a problem that "when recursing forward / backward slicing, for nodes added later through forward / backward slicing, only new nodes related to them can be added through forward / backward slicing", resulting in the omission of some important code lines located before and after the newly added nodes. The present invention adds the lines closely related to each newly added code line before and after it.

[0048] Since the selected code lines are more extensive, the present invention can greatly increase the number of positive samples (lines containing vulnerabilities) in the data set, thereby greatly alleviating the problem of unbalanced data sets.

[0049] The present invention only selects directly related edges or only one layer of indirectly related edges, and the depth of the graph is at most two layers. Therefore, if the downstream uses a graph convolutional network for training, the convolutional layer can be reduced to 2 layers (generally, the network will select 4 or more layers of convolution), which can reduce the amount of calculation during training and prediction.

[0050] The present invention proposes a slicing method that is different from ordinary forward and backward slicing. Under the premise of reducing the depth of graph traversal, richer semantic information can be extracted than forward and backward slicing. The computational complexity of downstream training tasks can be greatly reduced.

[0051] The present invention introduces the USE and DEF edges in the joern tool to extract additional variable type information.

[0052] The present invention proposes a data structure for auxiliary slicing, namely, an edge tree. The slices obtained through the edge tree are called context slices. The edge tree uses the relationship between different nodes in the graph representation of the source code (that is, the edges connecting different nodes in the graph) as nodes, and uses the different relationships between the edges in the graph as edge tree edges to construct an edge tree. Compared with traditional slicing, the perspective of looking at the problem is changed. From the perspective of the edge, the edge directly connected to the cut point in the edge tree is used as the starting point for slicing. After the slicing process is completed, the selected edges and corresponding nodes are extracted from the generated edge tree and converted into the output form of ordinary slices. In terms of the final result, this method is a supplement to the ordinary slicing method: in most cases, the slices generated by this method select more edges and nodes than the ordinary slicing method, and there are few cases where the selected edges and nodes are less than the ordinary slices. Selecting more relevant nodes and edges can enrich the semantic information contained in the slices and improve the model performance of downstream tasks. Secondly, it can also make the slices more likely to cover the vulnerability rows, increase the number of positive samples, and alleviate the problem of imbalanced data sets. .

[0053] The present invention also adds two new types of cut points: user-level sensitive functions and variable declaration statements, enriching the types of cut points.

[0054] The slicing technology proposed in the present invention can balance the positive and negative samples of the sliced ​​data set and improve the quantity and quality of the data set. BRIEF DESCRIPTION OF THE DRAWINGS

[0055] Figure 1 It is a PDG structure diagram.

[0056] Figure 2 for Figure 1 The edge tree generated by the PDG shown is rooted at e1.

[0057] Figure 3 for Figure 1 Another edge tree generated by the PDG shown. DETAILED DESCRIPTION

[0058] The present invention is further described in detail below in conjunction with examples, but the embodiments of the present invention are not limited thereto.

[0059] In order to make the purpose, technical solutions and advantages of the embodiments of the present invention clearer, the technical solutions in the embodiments of the present invention will be clearly and completely described below in conjunction with the drawings in the embodiments of the present invention. Obviously, the described embodiments are part of the embodiments of the present invention, rather than all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without making creative work are within the scope of protection of the present invention. Therefore, the following detailed description of the embodiments of the present invention provided in the drawings is not intended to limit the scope of the invention claimed for protection, but merely represents the selected embodiments of the present invention. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without making creative work are within the scope of protection of the present invention.

[0060] Glossary:

[0061] PDG: Program Dependence Graph. PDG is a graph structure that explicitly represents the source code's control dependency on data dependency information.

[0062] CPG: Code Property Graph, code property graph. It is a composite graph structure that integrates the Abstract Syntax Tree (AST), Control Flow Graph (CFG) and Program Dependence Graph (PDG), which contains the syntax information, control flow information and data dependency information of the source code.

[0063] joern: An open source tool that can generate corresponding CPG for source code.

[0064] joern-parse: A submodule of joern that can generate a CPG for a specified file in a non-interactive way.

[0065] CONTROLS: This is the type of edge generated by joern. A CONTROLS edge represents a control dependency between the code line of its starting node and the code line of its ending node.

[0066] REACHES: This is the type of edge generated by joern. A REACHES edge represents a data dependency between the code line of its starting node and the code line of its ending node.

[0067] DEF: This is the type of edge generated by joern. A DEF edge represents a line of code at its start node that defines a variable at its end node.

[0068] USE: This is the type of edge generated by joern. A DEF edge means that the code line of its starting node uses the variable of the code line of the ending node.

[0069] Forward Slice: Forward slicing is a slicing method. Its purpose is to find the code lines affected by the cut point.

[0070] Backward Slice: Backward slicing is a slicing method. Its purpose is to find the code line that affects the cut point.

[0071] Define edge e:

[0072]

[0073] Among them, Vs, Ve, and t represent the starting node, the ending node, and the type of the edge respectively;

[0074] For two edges , , define the relationship between the four edges :

[0075]

[0076] Multiple edges can form an edge tree. In the edge tree, the nodes are the edges e in the original graph, and the edges are the relations defined above.

[0077] If there is Figure 1 The PDG shown in the figure, with e1 as the root, can generate a Figure 2 The edge tree shown in the figure; since an edge has two nodes, when constructing the edge tree, the same edge can choose to start with two nodes, and different edges can also choose multiple relations. Therefore, the same PDG can generate multiple different edge trees. For example, the PDG can also generate Figure 3 Another edge tree is shown.

[0078] Embodiment 1:

[0079] An improved source code slicing method based on contextual edge relationships can identify more cut points. For the same cut point, the improved slicing method can also extract more vulnerability-related code lines, improve the quantity and quality of sample data sets, balance the number of positive and negative samples, and improve the model performance of downstream source code vulnerability detection tasks. In particular, if the downstream task uses a graph convolutional network, under the premise of achieving similar model performance, this method can also greatly reduce the amount of computation required for model training and the time spent, including the following steps:

[0080] 1) Preliminary analysis of source code: Use the joern-parse module of the joern tool to preliminarily parse the source code file and obtain the corresponding CPG;

[0081] 2) Generate PDG: Extract all code line nodes from CPG, and then extract the edges containing these nodes and of type "REACHES" and "CONTROLS" to form PDG;

[0082] 3) Extract cut points: According to the cut point type, select the code line of the corresponding cut point type from PDG as the cut point;

[0083] 4) Slicing: For each cut point, extract the code lines that are highly relevant to the vulnerability to form the final slice;

[0084] 5) Append variable declaration statements: Append related variable declaration statements to the final slice;

[0085] 6) Anonymize user variables: Replace user-defined variables and function names in the source code with general forms; this operation can eliminate the interference caused by variables and functions with similar functions but different names on model learning.

[0086] 7) Labeling: Depending on whether each slice contains a vulnerable row, each slice is marked as 1 (a slice with a vulnerability) or 0 (a slice without a vulnerability);

[0087] 8) Persistent storage: Save the data locally in JSON format as a data set for model training. The fields included are:

[0088] keyLine: a number, corresponding to the code line of the cut point of this slice;

[0089] nodes-lineNo: a list containing all the code lines selected by this slice;

[0090] edges-No: a list storing the edges between the code lines selected by this slice in the form of node pairs;

[0091] nodes-line: a list containing the text content of each code line;

[0092] nodes-line-sym: a list containing the anonymized text content of each code line;

[0093] function_name: string, the name of the function to which this table cut point belongs;

[0094] criterionType: string, table name, the cut point type of this slice;

[0095] target: a number, indicating whether this slice of the table name contains the vulnerable row; 0 means not, 1 means included;

[0096] testcaseID: A number that uniquely represents this slice.

[0097] Embodiment 2:

[0098] This embodiment is further optimized on the basis of the above embodiment. The similarities with the above technical solutions are not repeated here. In order to better realize the improved source code slicing method based on context edge relationship described in the present invention, the following setting method is particularly adopted: the step 1) is specifically as follows: for each source code file, a corresponding CPG will be generated. The joern tool uses the csv file format to represent the graph structure of the CPG. Each CPG uses nodes.csv to represent the nodes in the graph structure and edges.csv to describe the relationship between different nodes; nodes.csv has key, code, type, location and other fields. Among them, key is the unique identifier of each node, code is the code text form of the corresponding code line, type is the type of this node, if this node is a node at the code line granularity, the location field of this node will indicate the code line to which it belongs. Edges.csv has only three fields, namely start, end, type, and var. Start and end use the key field of nodes.csv to indicate the start and end nodes of this edge respectively. The type field table name connects the two nodes. If the edge type is data dependency, the var field will indicate the identifier of the variable corresponding to this data dependency. For other types of edges, the var field is empty.

[0099] Embodiment 3:

[0100] This embodiment is further optimized on the basis of any of the above embodiments, and the same points as the above technical solutions are not repeated here. In order to better implement the improved source code slicing method based on context edge relationship described in the present invention, the following setting is particularly adopted: the step 2) is specifically:

[0101] 2.1) For each set of nodes.csv and edges.csv (i.e., for each CPG), extract the nodes in nodes.csv that represent the code lines;

[0102] 2.2) The extracted set of nodes representing code lines is recorded as nodes_list; wherein the location field of the node representing the code line has content indicating the code line to which it belongs, while the location field of the node not representing the code line is empty;

[0103] 2.3) From edges.csv, select the edges whose start and key both appear in nodes_list and whose type is REACHES and CONTROLS. The extracted edge set is called edges_list.

[0104] 2.4) Then nodes_list and edges_list together form PDG; where the nodes are each line of code in the source code, and the edges are the data dependencies and control dependencies between different lines of code.

[0105] Embodiment 4:

[0106] This embodiment is further optimized on the basis of any of the above embodiments. The similarities with the above technical solutions are not repeated here. The cut point is the starting point of the slice, and the slice is the process of selecting the code line related to the cut point. The current mainstream method only selects four types of slices: SensiCall, operator, Pointer, and ArrayIndexing. The present invention improves the existing pointcut types and also adds additional new pointcut types. In order to better implement the improved source code slicing method based on context edge relationship described in the present invention, the following setting method is particularly adopted: the pointcut types include SensiCall type (functions that are prone to vulnerabilities are screened out from standard C language library functions. Any code line that calls these functions is extracted as SensiCall (indicating sensitive function call) pointcut), operator type (any code line containing "+, -, *, / " operations will be extracted as operator (indicating arithmetic operator) pointcut), MemberAccess type (any code line that accesses structure members through "." will be extracted as MemberAccess (indicating member variable access) pointcut), RefandDeRef type (any code line that uses "&" to reference a pointer and uses "*" to dereference a pointer will be extracted as RefandDeRef (indicating reference and dereference) pointcut), ArrayIndexing type (any code line that uses "[]" to access array members will be extracted as Ar rayIndexing (indicates array index) cut point), PointerDecl type (any pointer declaration code line will be extracted as PointerDecl (indicates pointer definition) cut point), ex_SensiCall (any non-SensiCall code line that uses pointer variables as parameters will be extracted as ex_SensiCall (indicates extended sensitive function call) cut point); Among them, SensiCall type, operator type, MemberAccess type, RefandDeRef type, ArrayIndexing type, PointerDecl type are scanned once for all nodes, and the cut points are selected through the type, code and other fields in nodes.csv; ex_SensiCall type scans all nodes twice, selects the function call among them, and checks: if this function call uses a pointer variable and does not belong to SensiCall, then this function call is appended to the final slicing result in the form of ex_SensiCall.

[0107] ex_SensiCall is defined as a user-level sensitive function (also an extended sensitive function call). Any pointer operation and non-SensiCall function call will be regarded as ex_SensiCall.

[0108] Embodiment 5:

[0109] This embodiment is further optimized on the basis of any of the above embodiments, and the same points as the above technical solutions are not repeated here. In order to better implement the improved source code slicing method based on context edge relationship described in the present invention, the following setting is particularly adopted: the step 4) includes the following steps:

[0110] 4.1) According to the tangent point, select any edge containing the tangent point as the root of the edge tree, denoted as root_e, and construct the corresponding edge tree;

[0111] 4.2) In the edge tree, select an edge that is directly adjacent to root_e, or one level indirectly adjacent to root_e;

[0112] 4.3) Extract the nodes corresponding to each edge from these selected edges, remove duplicates and put them into slice_nodes;

[0113] 4.4) For each node added in step 4.3), repeat steps 4.1) to 4.3) until no new nodes are added to slice_nodes;

[0114] 4.5) For each node in slice_nodes, add the corresponding edge according to PDG;

[0115] 4.6) Rearrange the selected nodes according to the order of the code lines. This set of nodes and edges is the final slice of the corresponding tangent point.

[0116] Embodiment 6:

[0117] This embodiment is further optimized on the basis of any of the above embodiments, and the similarities with the above technical solutions are not repeated here. In order to better realize the improved source code slicing method based on context edge relationship described in the present invention, the following setting method is particularly adopted: Step 5) is specifically: use joern's DEF and USE edges to find the declaration statement of the corresponding variable and append it to the slice. If this variable is in the current slice, the node to which it belongs is appended to the appropriate position of the slice (the slices are sorted in ascending order).

[0118] Embodiment 7:

[0119] This embodiment is further optimized on the basis of any of the above embodiments, and the similarities with the above technical solutions are not repeated here. In order to better realize the improved source code slicing method based on context edge relationship described in the present invention, the following setting method is particularly adopted: the step 6) is specifically: scan the source code of each slice, identify all user-defined variable positions therein, and count the number of each user-defined variable; replace the user-defined variable name with VARx, x∈{0,1,...,i} in the order of appearance; replace the user-defined function name with FUNCy, y∈{0,1,...,i}.

[0120] Embodiment 8:

[0121] This embodiment is further optimized on the basis of any of the above embodiments, and the similarities with the above technical solutions are not repeated here. In order to better implement the improved source code slicing method based on context edge relationship described in the present invention, the following setting method is particularly adopted: the slice marked as 1 represents a slice containing a vulnerability, and the slice marked as 0 represents a slice without a vulnerability.

[0122] Embodiment 9:

[0123] An improved source code slicing method based on context edge relationships expands the types of slicing, improves the quality of slicing, increases the samples of the slicing data set, balances the number of positive and negative samples in the slicing data set, and reduces the computational complexity of downstream tasks, including the following steps:

[0124] 1) Preliminary analysis of source code: For the collected source code samples, the joern-parse module of the joern tool is used to preliminarily parse the source code. For each source code file, a corresponding CPG will be generated. The joern tool uses the csv file format to represent the graph structure of the CPG. Each CPG uses nodes.csv to represent the nodes in the graph structure and edges.csv to describe the relationship between different nodes; nodes.csv has key, code, type, location and other fields. Among them, key is the unique identifier of each node, code is the code text form of the corresponding code line, type is the type of this node, if this node is a node at the code line granularity, this node location field will indicate the code line to which it belongs. Edges.csv has only three fields, namely start, end, type, and var. Start and end use the key field of nodes.csv to indicate the start and end nodes of this edge respectively. The type field indicates the type of the edge connecting the two nodes. If the type of the edge is data dependency, the var field will indicate the identifier of the variable corresponding to this data dependency. The var field of other types of edges is empty.

[0125] 2) Generate PDG: For each set of nodes.csv and edges.csv (that is, for each CPG), extract the nodes representing the code lines in nodes.csv. Specifically, the location field of this type of node will have content indicating the code line to which it belongs, while the location field of nodes other than this type is empty. The set of extracted nodes is recorded as nodes_list; then select the edges whose start and key both appear in nodes_list and whose type is REACHES and CONTROLS from edges. The set of extracted edges is recorded as edges_list; then nodes_list and edges_list together constitute PDG; among them, the node is each line of code in the source code, and the edge is the data dependency and control dependency relationship between different code lines.

[0126] 3) Extracting cutpoints: The cutpoint types selected in this embodiment are: SensiCall type (functions that are prone to vulnerabilities are selected from standard C language library functions. Any code line that calls these functions is extracted as SensiCall (indicating sensitive function calls) cutpoint), operator type (any code line containing "+, -, *, / " operations will be extracted as operator (indicating arithmetic operators) cutpoint), MemberAccess type (any code line that accesses a structure member through "." will be extracted as MemberAccess (indicating member variable access) cutpoint), RefandDeRef type (any code line that uses "&" to reference a pointer and uses "*" to dereference a pointer will be extracted as RefandDeRef (indicating reference and Dereference) pointcut), ArrayIndexing type (any code line that uses "[]" to access array members will be extracted as ArrayIndexing (indicating array index) pointcut), PointerDecl type (any code line that declares a pointer will be extracted as PointerDecl (indicating pointer definition) pointcut), ex_SensiCall (any code line that uses a pointer variable as a parameter and is not SensiCall will be extracted as ex_SensiCall (indicating extended sensitive function call) pointcut); except for the ex_SensiCall type, other pointcut types can be selected by scanning all nodes (i.e. each code line) once, and selected through the type, code and other fields in nodes.csv. The ex_SensiCall type also needs to scan all nodes again, select the function call in it, and check: if this function call uses a pointer variable and does not belong to SensiCall, then this function call is appended to the final slice result in the form of ex_SensiCall.

[0127] 4) Slicing: For each cut point, perform the following operations.

[0128] 4.1) According to the tangent point, select any edge containing the tangent point as the root of the edge tree, denoted as root_e, and construct the corresponding edge tree;

[0129] 4.2) In the edge tree, select an edge that is directly adjacent to root_e, or one level indirectly adjacent to root_e;

[0130] 4.3) Extract the nodes corresponding to each edge from these selected edges, remove duplicates and put them into slice_nodes;

[0131] 4.4) For each node added in step 4.3), repeat steps 4.1) to 4.3) until no new nodes are added to slice_nodes;

[0132] 4.5) For each node in slice_nodes, add the corresponding edge according to PDG;

[0133] 4.6) Rearrange the selected nodes according to the order of the code lines. This set of nodes and edges is the final slice of the corresponding tangent point.

[0134] Among them, the final output node set is recorded as slice_nodes.

[0135] 5) Append variable declaration statements: In the CPG generated by joern, pure variable declaration statements (such as int a;) do not exist in the PDG. However, such statements record the variable types of related variables, which is also important information, especially for overflow type vulnerabilities. If you don’t know the specific variable type, you can’t know which specific value will cause the variable to overflow. This embodiment uses joern’s DEF and USE edges to find the declaration statements of the corresponding variables. Appending such information to the slice can increase the semantic information contained in the slice.

[0136] 6) Anonymize user variables: Scan the source code of each slice, identify all user-defined variable locations, and count the number of each user-defined variable. Replace the user-defined variable names with VARx, x∈{0,1,...,i} in the order of appearance; replace the user-defined function names with FUNCy, y∈{0,1,...,i}.

[0137] 7) Labeling: For each slice, record the number of code lines it scans, denoted as s1. Find the vulnerability line marked in the source code file that generates the slice, denoted as s. If s exists in s1, the slice is considered to have a vulnerability, otherwise, the slice is considered to have no vulnerability.

[0138] 8) Persistent storage: Save it locally in JSON format as a data set for model training. Store multiple slices of the same file in a JSON file. An example of a slice is as follows:

[0139] {

[0140] "keyLine": 68,

[0141] "nodes-lineNo": [

[0142] 62,

[0143] 63,

[0144] 64, 68

[0146] ],

[0147] "edges-No": [ [

[0149] 0, 3

[0151] ], [

[0153] 1, 2

[0155] ], [

[0157] 2, 3 ]

[0160] ],

[0161] "nodes-line": [

[0162] "map<int, wchar_t *>dataMap;",

[0163] "wchar_tdataBuffer

[100] ;",

[0164] "data = dataBuffer;",

[0165] "dataMap[0] = data;"

[0166] ],

[0167] "nodes-line-sym": [

[0168] "VAR0<int ,wchar_t *>VAR1 ;",

[0169] "wchar_t VAR2 [ 100 ] ;",

[0170] "VAR3 = VAR2 ;",

[0171] "VAR1 [ 0 ] = VAR3 ;"

[0172] ],

[0173] "function_name": "static void goodG2B()",

[0174] "slice_type": "Array",

[0175] "testcaseID": 591497,

[0176] "target": 0

[0177] }

[0178] The edge-No stores the index of the edge in the node-lineNo. For this slice, the code line with the number of lines 128 is represented by 0 in the edge-No, 133 by 1, and 135 by 2.

[0179] The above description is only a preferred embodiment of the present invention and does not limit the present invention in any form. Any simple modification or equivalent change made to the above embodiment based on the technical essence of the present invention shall fall within the protection scope of the present invention.

Claims

1. An improved source code slicing method based on context edge relationship, characterized in that: The steps include: 1) Preliminary analysis of source code: Use the joern-parse module of the joern tool to preliminarily parse the source code file and obtain the corresponding CPG; 2) Generate PDG: Extract all code line nodes from CPG, and then extract the edges containing these nodes and of type "REACHES" and "CONTROLS" to form PDG, specifically: 2.1) For each set of nodes.csv and edges.csv, extract the nodes in nodes.csv that represent the code lines; 2.2) The extracted set of nodes representing code lines is recorded as nodes_list; wherein the location field of the node representing the code line has content indicating the code line to which it belongs, while the location field of the node not representing the code line is empty; 2.3) From edges.csv, select the edges whose start and key both appear in nodes_list and whose type is REACHES and CONTROLS. The extracted edge set is called edges_list. 2.4) Then nodes_list and edges_list together form PDG; where nodes are each line of code in the source code, and edges are data dependencies and control dependencies between different lines of code; 3) Extract cut points: According to the cut point type, select the code line of the corresponding cut point type from PDG as the cut point; 4) Slicing: For each cut point, extract the code lines that are highly relevant to the vulnerability to form the final slice, including the following steps: 4.1) According to the tangent point, select any edge containing the tangent point as the root of the edge tree, denoted as root_e, and construct the corresponding edge tree; 4.2) In the edge tree, select an edge that is directly adjacent to root_e, or one level indirectly adjacent to root_e; 4.3) Extract the nodes corresponding to each edge from these selected edges, remove duplicates and put them into slice_nodes; 4.4) For each node added in step 4.3), repeat steps 4.1) to 4.3) until no new nodes are added to slice_nodes; 4.5) For each node in slice_nodes, add the corresponding edge according to PDG; 4.6) Rearrange the selected nodes according to the order of code lines. The set of nodes and edges is the final slice of the corresponding tangent point. 5) Append variable declaration statements: Append related variable declaration statements to the final slice; 6) Anonymize user variables: Replace user-defined variables and function names in the source code with general forms; 7) Labeling: Mark each slice as 1 or 0 depending on whether it contains a vulnerable row; 8) Persistent storage: Save the data locally in JSON format as a data set for model training.

2. The improved source code slicing method based on context edge relationship according to claim 1, characterized in that: The step 1) is specifically as follows: for each source code file, a corresponding CPG will be generated. The joern tool uses the csv file format to represent the graph structure of the CPG. Each CPG uses nodes.csv to represent the nodes in the graph structure and edges.csv to describe the relationship between different nodes.

3. The improved source code slicing method based on context edge relationship according to claim 2, characterized in that: The tangent point types include SensiCall type, operator type, MemberAccess type, RefandDeRef type, ArrayIndexing type, PointerDecl type, and ex_SensiCall type; among them, SensiCall type, operator type, MemberAccess type, RefandDeRef type, ArrayIndexing type, and PointerDecl type are scanned once for all nodes, and the tangent points are selected through the type and code fields in nodes.csv; the ex_SensiCall type scans all nodes twice, selects the function call therein, and checks: if this function call uses a pointer variable and does not belong to SensiCall, this function call is appended to the final slicing result in the form of ex_SensiCall.

4. The improved source code slicing method based on context edge relationship according to claim 1, characterized in that: The step 5) is specifically as follows: using joern's DEF and USE edges, finding the declaration statement of the corresponding variable and appending it to the slice.

5. The improved source code slicing method based on context edge relationship according to claim 1, characterized in that: The step 6) is specifically as follows: scan the source code of each slice, identify all user-defined variable locations therein, and count the number of each user-defined variable; replace the user-defined variable name with VARx, x∈{0,1,...,i} in the order of appearance; and replace the user-defined function name with FUNCy, y∈{0,1,...,i}.

6. The improved source code slicing method based on context edge relationship according to claim 1, characterized in that: The slice marked as 1 represents a slice containing a vulnerability, and the slice marked as 0 represents a slice without a vulnerability.