A source code defect detection method and device
By extracting and slicing the data flow and control flow relationships of the source code, combined with the artificial intelligence model, the problem of insufficient detection ability and high dependence on uncompilable source code in the existing technology is solved, and the detection of compilable and non-compilable source code is realized. Comprehensive detection of code improves the flexibility and convenience of detection.
Patent Information
- Application Number
- CN202011586940.5
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2020-12-29
- Publication Date
- 2024-10-29
- Estimated Expiration
- 2040-12-29
AI Technical Summary
Existing source code defect detection methods have limited ability to detect uncompilable software source code and rely on expert experience, detection rules and compilation environment, which limits detection performance and convenience.
By extracting the data flow relationship and control flow relationship of the software source code, perform slicing processing, generate code fragments, and use artificial intelligence models for defect detection, support compilable and non-compilable source code detection, and reduce the need for detection rules, expert experience and Compilation environment dependencies.
It expands the scope of source code detection, realizes source code defect detection without relying on detection rules, expert experience and compilation environment, and improves the flexibility and convenience of detection.
Smart Images

Figure CN112579469B_ABST
Abstract
Description
Technical Field
[0001] The present application belongs to the field of computer technology, and in particular, relates to a source code defect detection method and device. Background Art
[0002] Information technology is in a stage of rapid development, and the software growth rate is getting faster and faster, resulting in an explosive growth in the size of source code. The source code defects contained therein also make security risks difficult to predict. How to accurately detect software defects that may be contained in the source code, such as API (application programming interface) or misuse of sensitive functions, malicious code, backdoor code, etc., is of great significance for ensuring information security and preventing malicious attacks.
[0003] The existing source code defect detection methods are highly dependent on expert experience, detection rules, and compilation environment, and are insufficient in detection performance, detection result display, and ease of use. The main defects are:
[0004] 1) From the perspective of input data: existing source code defect detection devices or tools generally require the input of compilable software source code or compiled executable binary files, while uncompilable software source code (program source code) cannot be detected. Such devices or tools limit the scope of software source code that can be detected;
[0005] 2) From the perspective of defect detection methods or engines: static analysis methods are mainly used, such as data flow analysis and taint analysis, which are highly dependent on expert experience, detection rules, and compilation environment;
[0006] Therefore, how to at least partially solve the above-mentioned technical problems, overcome the scope limitation of software source code that can be used for defect detection, and / or improve the current situation that source code defect detection is highly dependent on detection rules, compilation environment and expert experience has become a topic worthy of study in this field. Summary of the invention
[0007] In view of this, the present application provides a source code defect detection method and device, which are used to at least partially solve the above-mentioned technical problems, overcome the scope limitation of software source code that can be used for defect detection, and / or improve the current situation that source code defect detection is highly dependent on detection rules, compilation environment and expert experience.
[0008] The specific technical solutions are as follows:
[0009] A source code defect detection method, comprising:
[0010] Obtaining software source code to be tested for defects;
[0011] Extracting data flow relationships and control flow relationships in the software source code based on the grammatical structure of the software source code;
[0012] Slicing the software source code according to the data flow relationship and the control flow relationship to obtain code fragments of the software source code;
[0013] Training an artificial intelligence model based on code snippets of the software source code to obtain an artificial intelligence model for source code defect detection;
[0014] The constructed artificial intelligence model is used to perform defect detection processing on the code fragments of the software source code to obtain the defect detection results of the code fragments of the software source code.
[0015] Optionally, extracting data flow relationships and control flow relationships in the software source code based on the grammatical structure of the software source code includes:
[0016] Parsing and extracting semantic information and / or grammatical information contained in the software source code, and generating an abstract syntax tree containing the semantic information and / or grammatical information;
[0017] Extracting control flow relationships within each function in the software source code according to the abstract syntax tree, and generating control flow graphs corresponding to the control flow relationships within each function;
[0018] Integrate each control flow graph to generate a control flow graph with control flow relationships between functions;
[0019] According to the abstract syntax tree, extract the data flow relationship in the software source code and generate a corresponding data flow graph;
[0020] The control flow graph and the data flow graph are integrated to generate a control flow-data flow graph including control flow relations and data flow relations.
[0021] Optionally, slicing the software source code according to the data flow relationship and the control flow relationship to obtain code fragments of the software source code includes:
[0022] Extracting subgraphs from the control flow-data flow graph to obtain at least one subgraph;
[0023] According to the at least one subgraph, code statements are extracted, and at least one code snippet corresponding to the at least one subgraph and containing a control flow relationship and / or a data flow relationship is generated.
[0024] Optionally, the method of using a pre-built artificial intelligence model to perform defect detection processing on a code fragment of the software source code to obtain a defect detection result of the code fragment of the software source code includes: inputting a vectorized representation of the code fragment into a pre-built artificial intelligence model, and using the artificial intelligence model to perform defect analysis and defect category determination on the code fragment to obtain a defect detection result that includes the defect category of the code fragment.
[0025] Optionally, before performing defect detection processing on the code snippet of the software source code using the pre-built artificial intelligence model, the method further includes:
[0026] Standardizing code snippets of the software source code;
[0027] The normalization process of the code snippets includes: extracting predetermined supplementary information of the code snippets, and / or eliminating duplicate code snippets.
[0028] Optionally, after obtaining the defect detection result of the code snippet of the software source code, the method further includes:
[0029] The defect detection results are output and the detection results are displayed in a visual interface to support interactive information.
[0030] A source code defect detection device, comprising:
[0031] An acquisition unit, used for acquiring software source code to be tested for defects;
[0032] An extraction unit, configured to extract data flow relations and control flow relations in the software source code based on a grammatical structure of the software source code;
[0033] A slicing unit, configured to slice the software source code according to the data flow relationship and the control flow relationship to obtain code fragments of the software source code;
[0034] An artificial intelligence model training unit, used to train an artificial intelligence model according to code snippets of the software source code to obtain an artificial intelligence model for source code defect detection;
[0035] The detection unit is used to use the constructed artificial intelligence model to perform defect detection processing on the code fragment of the software source code to obtain the defect detection result of the code fragment of the software source code.
[0036] Optionally, the extraction unit is specifically used to:
[0037] Parsing and extracting semantic information and / or grammatical information contained in the software source code, and generating an abstract syntax tree containing the semantic information and / or grammatical information;
[0038] Extracting control flow relationships within each function in the software source code according to the abstract syntax tree, and generating control flow graphs corresponding to the control flow relationships within each function;
[0039] Integrate each control flow graph to generate a control flow graph with control flow relationships between functions;
[0040] According to the abstract syntax tree, extract the data flow relationship in the software source code and generate a corresponding data flow graph;
[0041] The control flow graph and the data flow graph are integrated to generate a control flow-data flow graph including control flow relations and data flow relations.
[0042] Optionally, the slicing unit is specifically used to:
[0043] Extracting subgraphs from the control flow-data flow graph to obtain at least one subgraph;
[0044] According to at least one subgraph, code statements are extracted to generate at least one code snippet corresponding to the at least one subgraph and containing a control flow relationship and / or a data flow relationship.
[0045] Optionally, the detection unit is specifically used to:
[0046] Encoding the code snippet to generate a vectorized representation of the code snippet;
[0047] The vectorized representation of the code snippet is input into a pre-built artificial intelligence model, and the artificial intelligence model performs defect analysis and defect category determination on the code snippet to obtain a defect detection result including the defect category of the code snippet.
[0048] Optionally, the above device further includes:
[0049] A normalization processing unit, configured to perform normalization processing on the code fragments of the software source code before performing defect detection processing on the code fragments of the software source code using a pre-built artificial intelligence model;
[0050] The normalization process of the code snippets includes: extracting predetermined supplementary information of the code snippets, and / or eliminating duplicate code snippets.
[0051] Optionally, the above device further includes:
[0052] The result output control unit is used to output the defect detection result so as to support interactive information display of the detection result on a visual interface.
[0053] Compared with the prior art, the above technical solution provided by this application has the following advantages:
[0054] It can be seen from the above technical scheme that the source code defect detection method and device provided by the present application, for the software source code to be defect-detected, based on its grammatical structure, extracts the data flow relationship and control flow relationship in the software source code, and slices the software source code according to the data flow relationship and the control flow relationship, and generates code snippets containing contextual semantics for input into the artificial intelligence model for defect detection. The source code parsing and code snippet generation process can be completed without compilation. Therefore, from the perspective of input data, the present application supports the input of compilable and / or non-compilable software source code at the same time, expanding the scope of software source code defect detection; at the same time, the present application uses artificial intelligence technology to automatically learn the defect modes and normal modes implied in the code context such as syntax trees, data flows, and control flows generated by source code parsing, and can achieve source code defect detection without relying on detection rules, expert experience, and compilation environment. BRIEF DESCRIPTION OF THE DRAWINGS
[0055] In order to more clearly illustrate the embodiments of the present application or the technical solutions in the prior art, the drawings required for use in the embodiments or the description of the prior art will be briefly introduced below. Obviously, the drawings described below are merely embodiments of the present application. For ordinary technicians in this field, other drawings can be obtained based on the provided drawings without paying any creative work.
[0056] Figure 1 It is a flow chart of a source code defect detection method provided by an embodiment of the present application;
[0057] Figure 2 It is a flowchart of extracting data flow relationship and control flow relationship in software source code based on the grammatical structure of software source code provided by an embodiment of the present application;
[0058] Figure 3 It is a logic diagram of source code parsing and code snippet generation provided by an embodiment of the present application;
[0059] Figure 4 It is a schematic diagram of source code defect detection and determination logic based on artificial intelligence provided by an embodiment of the present application;
[0060] Figure 5 It is another flow chart of the source code defect detection method provided in the embodiment of the present application;
[0061] Figure 6 This is another flow chart of the source code defect detection method provided by the embodiment of the present application;
[0062] Figure 7It is a logical schematic diagram of source code defect detection based on artificial intelligence provided by an embodiment of the present application;
[0063] Figure 8 It is a structural schematic diagram of a source code defect detection device provided in an embodiment of the present application;
[0064] Fig. 9 is another structural schematic diagram of the source code defect detection device provided in an embodiment of the present application;
[0065] Fig.10 This is another structural diagram of the source code defect detection device provided in an embodiment of the present application. DETAILED DESCRIPTION
[0066] The following will be combined with the drawings in the embodiments of the present application to clearly and completely describe the technical solutions in the embodiments of the present application. Obviously, the described embodiments are only part of the embodiments of the present application, not all of the embodiments. Based on the embodiments in the present application, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of this application.
[0067] The present application provides a source code defect detection method and device, which is used to automatically learn the defect modes and normal modes contained in the syntax tree, data flow, control flow, etc. generated by source code parsing using artificial intelligence technology, and implement source code defect detection without relying on detection rules, expert experience, and compilation environment. The overall implementation architecture of the present application solution can be, but is not limited to, adopting a B / S architecture to implement software source code defect detection through interaction between the server and the client, and specifically the processing process of the source code defect detection method and device of the present application can be applied to the server.
[0068] See also Figure 1 , is a flow chart of a source code defect detection method provided in an embodiment of the present application. In this embodiment, the source code defect detection method includes:
[0069] Step 101: Obtain software source code to be defect-checked.
[0070] Specifically, it can receive software source code uploaded / loaded by the client.
[0071] The software source code uploaded / loaded by the client may be, but is not limited to, compilable and / or non-compilable software source code, or a compiled executable binary file. In addition, there are many types of source code languages that can be used for defect detection using the present application solution, including but not limited to: C, C++, Java, PHP, javascript, etc., and can be expanded according to needs.
[0072] Step 102: extracting data flow relationships and control flow relationships in the software source code based on the grammatical structure of the software source code.
[0073] See also Figure 2 , step 102 can be implemented by the following process:
[0074] Step 201: Parse and extract semantic information and / or grammatical information contained in the software source code, and generate an abstract syntax tree containing the semantic information and / or grammatical information;
[0075] Specifically, as much semantic information and / or grammatical information as possible contained in the software source code can be parsed and extracted, including but not limited to: declaration definition statements, assignment statements, expression statements, conditional statements, return statements, parameter lists, parameter declarations, whether it is a node of a control flow graph, etc.; and an abstract syntax tree is used to represent the extracted semantic information and / or grammatical information, so as to realize the application of an abstract syntax tree to represent the uploaded / loaded software source code.
[0076] The above-mentioned abstract syntax tree includes a tree structure composed of a series of edges and nodes, wherein the edges represent the relationship between nodes, such as control relationship or data flow relationship, etc.; and the nodes contain information related to the code, such as: the declaration definition node contains specific declaration or definition statements, code line numbers, node numbers, etc.; the information contained in the assignment statement node, expression statement node, and conditional statement node is similar to the above-mentioned declaration definition node, and also includes specific code statements, line numbers, node numbers, etc.
[0077] In addition, different types of nodes contain different information. For example, a parameter declaration node contains the specific parameter name, parameter type, and the initial value of the parameter.
[0078] Step 202: extracting the control flow relationship in each function in the software source code according to the abstract syntax tree, and generating each control flow graph corresponding to the control flow relationship in each function;
[0079] Usually, a software source code includes a plurality of different functions, and a plurality of control flow graphs corresponding to the different functions can be generated accordingly.
[0080] The abstract syntax tree contains a large amount of complex source code information. During implementation, the nodes in the abstract syntax tree can be traversed, and the basic string matching method can be used to retrieve the function definition nodes, declaration nodes, parameter definition nodes, parameter usage nodes, etc. in the source code, and the retrieved nodes can be extracted as nodes of the control flow graph.
[0081] The control flow relationship includes one or more control relationships, which mainly refer to the calling or called relationship between the above nodes, the calling relationship between variables and functions, the calling relationship between variables, etc. On the basis of extracting the retrieved nodes as control flow graph nodes, the control relationship between nodes can be further extracted from the edges corresponding to each node retrieved in the abstract syntax tree, and the extracted control relationship is used as the edge information of the control flow graph.
[0082] Step 203: Integrate each control flow graph to generate a control flow graph with control flow relationships between functions;
[0083] What is extracted in step 202 is the control flow graph corresponding to each function respectively, lacking the control relationship between functions. Therefore, it is necessary to integrate multiple control flow graphs corresponding to different functions to obtain a control flow graph with control flow relationship between functions.
[0084] Specifically, assuming that there is a function call relationship in a certain node in the control flow graph A, such as calling a user-defined test function in the main function, it is necessary to integrate the control flow graph of the main function with the control flow graph of the test function to obtain a control flow graph with a control flow relationship between functions.
[0085] Step 204: extract the data flow relationship in the software source code according to the abstract syntax tree, and generate a corresponding data flow graph;
[0086] Similar to the extraction of control flow graphs, the extraction of data flow graphs can also use string retrieval-matching algorithms to extract the required node information and data flow information from a series of edges that combine and connect nodes included in the abstract syntax tree.
[0087] The difference is that the nodes in the data flow graph mainly refer to the parameter declaration nodes, parameter definition nodes, parameter assignment nodes, etc. in the abstract syntax tree.
[0088] The edges in the data flow graph are used to represent the data flow relationship between the above nodes, such as the edge between the parameter definition node and the parameter assignment node.
[0089] Step 205: Integrate the control flow graph and the data flow graph to generate a control flow-data flow graph including control flow relations and data flow relations.
[0090] Some nodes of the data flow graph may be the same as some nodes of the generated control flow graph, so the data flow graph and the control flow graph may have the same parts, and the only difference is the edge (the edge of the control flow graph represents the control relationship, and the edge of the data flow graph represents the data flow relationship). Therefore, during the integration process, if there are nodes in the control flow graph that are the same as those in the data flow graph, the edge information of the data flow graph (reflecting the corresponding data flow relationship) can be added to the corresponding edge of the control flow graph. If there are nodes in the control flow graph that are different from the data flow graph, these nodes of the data flow graph that are different from the nodes of the control flow graph and their corresponding edge information can be added to the control flow graph to achieve the integration of the control flow graph and the data flow graph.
[0091] After final integration, a control flow-data flow graph containing control flow relationships and data flow relationships is obtained.
[0092] Step 103: Slice the software source code according to the data flow relationship and the control flow relationship to obtain code fragments of the software source code.
[0093] Specifically, based on the control flow-data flow graph including the control flow relationship and the data flow relationship, the software source code may be sliced to obtain its code fragments. The process may include:
[0094] 1) Extracting subgraphs from the control flow-data flow graph to obtain at least one subgraph;
[0095] Specifically, a singular or plural subgraph may be extracted from the generated control flow-data flow graph, wherein the subgraph contains corresponding control flow relations and / or data flow relations in the software source code.
[0096] In implementation, the corresponding subgraph can be extracted from the generated control flow-data flow graph according to different keywords, such as a specified function name or variable name, etc. Preferably, the coupling degree reflected by the control flow relationship and / or data flow relationship between nodes in the same subgraph is relatively large, and the coupling degree reflected by the control flow relationship and / or data flow relationship between nodes in different subgraphs is relatively small.
[0097] 2) According to at least one subgraph, code statements are extracted to generate at least one code snippet corresponding to the at least one subgraph and containing a control flow relationship and / or a data flow relationship.
[0098] Afterwards, code statements may be further extracted based on the extracted singular or plural subgraphs to generate code snippets of the software source code containing control flow relationships and / or data flow relationships.
[0099] There are two ways to implement the subgraph-based extraction of code statements to generate code snippets:
[0100] One is to extract directly from the nodes of the subgraph.
[0101] Since the nodes of a subgraph usually contain information such as node type, code line number, code statement, parent node of the node, child node of the node, etc., the code statement can be directly extracted from the subgraph nodes by using the retrieval-matching method.
[0102] The other is to extract it from the source code file.
[0103] If the subgraph node does not contain a code statement, the code statement can be extracted from the source code file based on the code line number in the subgraph node.
[0104] See also Figure 3 , provides a schematic diagram of the above-mentioned source code parsing and code snippet generation process.
[0105] In the process of source code parsing and code snippet generation, there is no need for compilation. By parsing the source code, the abstract syntax tree, data flow graph and control flow graph in the source code are extracted, and code snippets can be generated based on the data flow relationship and control flow relationship. The code snippets can then be directly input into the pre-built artificial intelligence model for defect detection. There is no need to limit the software source code to be defect-detected to compilable source code or compiled executable binary files. The same applies to non-compilable source code. Therefore, from the perspective of input data, the present application solution supports the input of compilable and / or non-compilable software source code at the same time, which increases the scope of detectable software source code.
[0106] Step 104: train the code snippet data of the software source code to obtain an artificial intelligence model that can support source code defect detection.
[0107] Step 105: Use a pre-built artificial intelligence model to perform defect detection processing on the code fragment of the software source code to obtain a defect detection result of the code fragment of the software source code.
[0108] In order to avoid high reliance on expert experience, detection rules, compilation environment, etc., this application adopts an artificial intelligence model to perform defect detection processing on the code snippets of the software source code obtained through the above-mentioned source code parsing and code snippet generation.
[0109] Specifically, by using artificial intelligence technology to automatically learn the defect patterns and normal patterns contained in the syntax tree, data flow, control flow, etc. generated by source code parsing, source code defect detection can be achieved without relying on detection rules, expert experience and compilation environment.
[0110] The artificial intelligence model in this application is essentially an artificial intelligence-based source code defect detection algorithm model. The constructed artificial intelligence-based source code defect detection algorithm model may include but is not limited to the following:
[0111] 1) Deep learning algorithm model based on CNN (convolutional neural network);
[0112] 2) Deep learning algorithm model based on RNN (recurrent neural network);
[0113] 3) Deep learning algorithm model based on GCN (graph convolutional network).
[0114] 4) Other deep algorithm models.
[0115] For the code snippets of the software source code, the word vector algorithm can be used to encode the generated code snippets to generate a vectorized representation of the code snippets in digital form; and the vectorized representation of the code snippets is further input into a pre-built artificial intelligence model, which extracts feature information from the code snippets and performs feature analysis to implement defect analysis and defect category determination on the code snippets, and obtain defect detection results for the defect categories containing the code snippets.
[0116] The feature information extracted by the artificial intelligence model includes but is not limited to the language type of the code and the code defect pattern. The above feature information is automatically extracted by the deep neural network without manual definition, reducing the dependence on human experts. All that needs to be done is to optimize the neural network training method and parameter settings.
[0117] Feature analysis is also automatically implemented by the deep neural network of the artificial intelligence model. Thanks to the advantages of deep learning and the characteristics of deep neural networks, during the training phase of the artificial intelligence model, only the presence or absence of code defects and defect categories are needed as training guides (i.e., labels) to improve the classification accuracy and detection rate of the model.
[0118] In the actual detection stage, the trained artificial intelligence model can be applied to realize the defect detection of unknown code fragments. In implementation, simple and / or complex source code defect detection can be realized according to different needs, including but not limited to: simple binary classification function, specific binary classification function, simple multi-classification function and integrated multi-classification function.
[0119] In the source code defect detection process based on the artificial intelligence model, by sequentially executing the vectorization, feature extraction, defect detection and classification processes of the source code fragments, it is determined whether each code fragment of the software source code contains a certain type of defect or several types of defects, and finally the defect detection of the code fragments is realized. Figure 4 shown.
[0120] It can be seen from the above technical scheme that the source code defect detection method provided by the present application, for the software source code to be defect-detected, extracts the data flow relationship and control flow relationship in the software source code based on its grammatical structure, and slices the software source code according to the data flow relationship and the control flow relationship, and generates code snippets containing contextual semantics for input into the artificial intelligence model for defect detection. The source code parsing and code snippet generation process can be completed without compilation. Therefore, from the perspective of input data, the present application supports the input of compilable and / or non-compilable software source code at the same time, expanding the scope of software source code defect detection; at the same time, the present application uses artificial intelligence technology to automatically learn the defect modes and normal modes implied in the code context such as syntax trees, data flows, and control flows generated by source code parsing, and can achieve source code defect detection without relying on detection rules, expert experience, and compilation environment.
[0121] In an optional embodiment of the present application, see Figure 5 The above-mentioned source code defect detection method may further include, before step 104 (training the code snippet data of the software source code to obtain an artificial intelligence model capable of supporting source code defect detection):
[0122] Step 103': normalize the code snippets of the software source code.
[0123] The normalization process of the code snippets includes but is not limited to: extracting predetermined supplementary information of the code snippets, and / or eliminating duplicate code snippets.
[0124] Specifically, the generated code snippets can be cleaned and supplementary information corresponding to the code snippets can be extracted. The extracted supplementary information includes but is not limited to: source file path, slice generation basis, etc.; during the training phase, the supplementary information of the extracted code snippets can also include risk level, defect name, label and other information.
[0125] In addition, you can further eliminate duplicate code snippets.
[0126] Finally, the standardized code snippets of the software source code are obtained, which can facilitate the subsequent training of the source code defect detection model and / or the use of artificial intelligence models to perform defect detection on the code snippets.
[0127] In an optional embodiment of the present application, see Figure 6 The above-mentioned source code defect detection method may further include, after step 105 (using the constructed artificial intelligence model to perform defect detection processing on the code fragment of the software source code):
[0128] Step 106: output the defect detection result, and display the detection result in a visual interface to support interactive information display.
[0129] After the source code defect detection result is obtained in the feature extraction and defect detection and classification process based on the artificial intelligence model, the defect detection result can be output and displayed on a visualization interface that supports interactive information display. In addition, the source code defect detection result can also be output to a text document and / or a database table for archiving.
[0130] In implementation, the source code defect detection results can be visualized and interactively displayed on the server and / or client. Thus, this embodiment implements an end-to-end source code defect detection solution that can overcome the scope limitation of the software source code for defect detection and has low dependence on detection rules, compilation environment and expert experience. The end-to-end detection method refers to a convenient method of source code input -> defect detection result display.
[0131] Based on the description of the above embodiments, in the implementation, preferably, four functional modules can be pre-implemented on the server side: source code input and code snippet generation, code snippet preprocessing, artificial intelligence model training module, source code defect detection based on artificial intelligence and result output control, and the software source code defect detection is realized by running the functions of the corresponding modules. Specifically, when the software source code defect detection is realized by running the functions of the corresponding modules, the software source code uploaded / loaded by the front end / client can be received, and the uploaded / loaded software source code files and / or compressed packages can be stored in the default path or the specified path. On this basis, the four modules of "source code input and code snippet generation", "code snippet preprocessing", "artificial intelligence model training" and "source code defect detection based on artificial intelligence" are called in sequence, and the functions of the corresponding modules are run; thereafter, the source code defect detection results are output based on the result output control module and the visual display based on this is performed.
[0132] like Figure 7 As shown, based on the above four functional modules of the server, the source code defect detection of this application can be implemented through the following implementation process:
[0133] 1) Source code parsing and code snippet generation: In the process of code snippet generation, no compilation is required. The source code is parsed, the abstract syntax tree, data flow graph and control flow graph in the source code are extracted, and code snippets are generated based on the data flow relationship and control flow relationship;
[0134] 2) Code snippet preprocessing: normalize the generated code snippets, including but not limited to extracting supplementary information such as source code path, removing duplicate code snippets, etc.;
[0135] 3) AI model training: Using the source code snippets generated by code snippet preprocessing, vectorization and AI algorithm iteration are performed at one time to generate an AI model that can be used for source code defect detection;
[0136] 4) Source code defect detection based on artificial intelligence: vectorization, feature extraction, defect detection and classification of source code fragments are performed in sequence to determine whether they contain a certain type of defect or several types of defects;
[0137] 5) Display of source code defect detection results: supports displaying source code defect detection results on the client.
[0138] In addition, during implementation, the above different processes / steps / modules in the source code defect analysis can also be encapsulated to make it highly maintainable.
[0139] Corresponding to the above-mentioned source code defect detection method, the embodiment of the present application further provides a source code defect detection device, Figure 8 A structural schematic diagram of the device is provided, which specifically includes:
[0140] An acquisition unit 801 is used to acquire software source code to be defect-checked;
[0141] An extraction unit 802, configured to extract a data flow relationship and a control flow relationship in the software source code based on a grammatical structure of the software source code;
[0142] A slicing unit 803 is used to slice the software source code according to the data flow relationship and the control flow relationship to obtain code fragments of the software source code;
[0143] An artificial intelligence model training unit 804 is used to train an artificial intelligence model according to the code snippets of the software source code to obtain an artificial intelligence model for source code defect detection;
[0144] The detection unit 805 is used to use a pre-built artificial intelligence model to perform defect detection processing on the code fragment of the software source code to obtain a defect detection result of the code fragment of the software source code.
[0145] In an optional implementation of the embodiment of the present application, the extraction unit 802 is specifically configured to:
[0146] Parsing and extracting semantic information and / or grammatical information contained in the software source code, and generating an abstract syntax tree containing the semantic information and / or grammatical information;
[0147] Extracting control flow relationships within each function in the software source code according to the abstract syntax tree, and generating control flow graphs corresponding to the control flow relationships within each function;
[0148] Integrate each control flow graph to generate a control flow graph with control flow relationships between functions;
[0149] According to the abstract syntax tree, extract the data flow relationship in the software source code and generate a corresponding data flow graph;
[0150] The control flow graph and the data flow graph are integrated to generate a control flow-data flow graph including control flow relations and data flow relations.
[0151] In an optional implementation of the embodiment of the present application, the slicing unit 803 is specifically configured to:
[0152] Extracting subgraphs from the control flow-data flow graph to obtain at least one subgraph;
[0153] According to at least one subgraph, code statements are extracted to generate at least one code snippet corresponding to the at least one subgraph and containing a control flow relationship and / or a data flow relationship.
[0154] In an optional implementation of the embodiment of the present application, the detection unit 805 is specifically used to:
[0155] Encoding the code snippet to generate a vectorized representation of the code snippet;
[0156] The vectorized representation of the code snippet is input into a pre-built artificial intelligence model, and the artificial intelligence model performs defect analysis and defect category determination on the code snippet to obtain a defect detection result including the defect category of the code snippet.
[0157] In an optional implementation of the embodiment of the present application, if Fig. 9 As shown, the above-mentioned source code defect detection device may further include a normalization processing unit 806, which is used to perform normalization processing on the code fragments of the software source code before performing defect detection processing on the code fragments of the software source code using the pre-built artificial intelligence model;
[0158] The normalization process of the code snippets includes: extracting predetermined supplementary information of the code snippets, and / or eliminating duplicate code snippets.
[0159] In an optional implementation of the embodiment of the present application, if Fig.10 As shown, the above-mentioned source code defect detection device may further include a result output control unit 807, which is used to output the defect detection result to support interactive information display of the detection result on a visual interface.
[0160] As for the source code defect detection device disclosed in the embodiment of the present application, since it corresponds to the source code defect detection method disclosed in any of the above embodiments, the description is relatively simple. For the relevant similarities, please refer to the description of the source code defect detection method in the above embodiments, which will not be described in detail here.
[0161] In summary, the source code defect detection method and device of the present application have the following technical advantages over the existing source code defect detection solutions:
[0162] 1) An end-to-end source code defect detection solution is implemented without relying on detection rules, expert experience, and compilation environment;
[0163] 2) From the perspective of input data, it supports inputting compilable and / or non-compilable software source codes, which increases the scope of detectable software source codes;
[0164] 3) Ability to implement simple and / or complex source code defect detection based on different requirements, including but not limited to: simple binary classification function, specific binary classification function, simple multi-classification function and integrated multi-classification function;
[0165] 4) High scalability and maintainability, which is specifically reflected in: a) There are multiple types of source code languages that can be detected, including but not limited to: C, C++, Java, PHP, javascript, etc., and it can be expanded according to needs; b) Detection function: In addition to the determination of defect types, it can also be expanded to other source code analysis functions, including but not limited to: source analysis, clone detection, etc.; c) Different processes / steps / modules in source code defect analysis are encapsulated, which is highly maintainable;
[0166] 5) Source code defect detection is universal and is not limited to certain and / or certain categories of source code defects.
[0167] It should be noted that the various embodiments in this specification are described in a progressive manner, and each embodiment focuses on the differences from other embodiments. The same or similar parts between the various embodiments can be referenced to each other.
[0168] For the convenience of description, the above system or device is described by dividing it into various modules or units according to its functions. Of course, when implementing the present application, the functions of each unit can be implemented in the same or multiple software and / or hardware.
[0169] It can be known from the description of the above implementation methods that those skilled in the art can clearly understand that the present application can be implemented by means of software plus a necessary general hardware platform. Based on such an understanding, the technical solution of the present application can be essentially or partly contributed to the prior art in the form of a software product, which can be stored in a storage medium such as ROM / RAM, a magnetic disk, an optical disk, etc., and includes several instructions for enabling a computer device (which can be a personal computer, a server, or a network device, etc.) to execute the methods described in the various embodiments of the present application or certain parts of the embodiments.
[0170] Finally, it should be noted that, in this article, relational terms such as first, second, third and fourth are only used to distinguish one entity or operation from another entity or operation, and do not necessarily require or imply any such actual relationship or order between these entities or operations. Moreover, the terms "include", "comprise" or any other variants thereof are intended to cover non-exclusive inclusion, so that the process, method, article or equipment including a series of elements includes not only those elements, but also includes other elements not explicitly listed, or also includes elements inherent to such process, method, article or equipment. In the absence of further restrictions, the elements defined by the statement "comprise a ..." do not exclude the existence of other identical elements in the process, method, article or equipment including the elements.
[0171] The above is only a preferred implementation of the present application. It should be pointed out that for ordinary technicians in this technical field, several improvements and modifications can be made without departing from the principles of the present application. These improvements and modifications should also be regarded as the scope of protection of the present application.
Claims
1. A source code defect detection method, It is characterized in that include: Obtaining the source code of the software to be tested for defects; Extracting data flow relationships and control flow relationships in the software source code based on the grammatical structure of the software source code; Slicing the software source code according to the data flow relationship and the control flow relationship to obtain code fragments of the software source code; Using a pre-built artificial intelligence model to perform defect detection processing on the code fragment of the software source code to obtain a defect detection result of the code fragment of the software source code; The AI model learns the defective and normal patterns of the code with contextual semantics; The step of extracting data flow relationships and control flow relationships in the software source code based on the grammatical structure of the software source code includes: Parsing and extracting semantic information and / or grammatical information contained in the software source code, and generating an abstract syntax tree containing the semantic information and / or grammatical information; Extracting control flow relationships within each function in the software source code according to the abstract syntax tree, and generating control flow graphs corresponding to the control flow relationships within each function; Integrate each control flow graph to generate a control flow graph with control flow relationships between functions; According to the abstract syntax tree, extract the data flow relationship in the software source code and generate a corresponding data flow graph; Integrate the control flow graph and the data flow graph to generate a control flow-data flow graph including a control flow relationship and a data flow relationship; The slicing of the software source code according to the data flow relationship and the control flow relationship to obtain code fragments of the software source code includes: Extracting subgraphs from the control flow-data flow graph to obtain at least one subgraph; Extracting code statements according to the at least one subgraph, and generating at least one code snippet corresponding to the at least one subgraph and containing a control flow relationship and / or a data flow relationship; Before using the pre-built artificial intelligence model to perform defect detection on the code snippet of the software source code, the method further includes: Standardizing code snippets of the software source code; The normalization process of the code snippets includes: extracting predetermined supplementary information of the code snippets, and / or eliminating duplicate code snippets.
2. The method according to claim 1, It is characterized in that The using of the pre-built artificial intelligence model to perform defect detection processing on the code snippet of the software source code to obtain the defect detection result of the code snippet of the software source code includes: Encoding the code snippet to generate a vectorized representation of the code snippet; The vectorized representation of the code snippet is input into a pre-built artificial intelligence model, and the artificial intelligence model performs defect analysis and defect category determination on the code snippet to obtain a defect detection result including the defect category of the code snippet.
3. The method according to claim 1, It is characterized in that After obtaining the defect detection result of the code snippet of the software source code, the method further includes: The defect detection results are output and the detection results are displayed in a visual interface to support interactive information.
4. A source code defect detection device, It is characterized in that include: An acquisition unit, used for acquiring software source code to be tested for defects; An extraction unit, configured to extract data flow relations and control flow relations in the software source code based on a grammatical structure of the software source code; A slicing unit, configured to slice the software source code according to the data flow relationship and the control flow relationship to obtain code fragments of the software source code; A detection unit, configured to perform defect detection processing on a code segment of the software source code using a pre-built artificial intelligence model to obtain a defect detection result of the code segment of the software source code; the artificial intelligence model learns a defect mode and a normal mode of the code containing contextual semantics; Wherein, the extraction unit is specifically used for: Parsing and extracting semantic information and / or grammatical information contained in the software source code, and generating an abstract syntax tree containing the semantic information and / or grammatical information; Extracting control flow relationships within each function in the software source code according to the abstract syntax tree, and generating control flow graphs corresponding to the control flow relationships within each function; Integrate each control flow graph to generate a control flow graph with control flow relationships between functions; According to the abstract syntax tree, extract the data flow relationship in the software source code and generate a corresponding data flow graph; Integrate the control flow graph and the data flow graph to generate a control flow-data flow graph including a control flow relationship and a data flow relationship; The slicing unit is specifically used for: Extracting subgraphs from the control flow-data flow graph to obtain at least one subgraph; Extracting code statements according to at least one subgraph, and generating at least one code snippet corresponding to the at least one subgraph and containing a control flow relationship and / or a data flow relationship; Also includes: A normalization processing unit, configured to perform normalization processing on the code fragments of the software source code before performing defect detection processing on the code fragments of the software source code using a pre-built artificial intelligence model; The normalization process of the code snippets includes: extracting predetermined supplementary information of the code snippets, and / or eliminating duplicate code snippets.
5. The device according to claim 4, It is characterized in that The detection unit is specifically used for: Encoding the code snippet to generate a vectorized representation of the code snippet; The vectorized representation of the code snippet is input into a pre-built artificial intelligence model, and the artificial intelligence model performs defect analysis and defect category determination on the code snippet to obtain a defect detection result including the defect category of the code snippet.
6. The device according to claim 4, It is characterized in that Also includes: The result output control unit is used to output the defect detection result so as to support interactive information display of the detection result on a visual interface.
Citation Information
Patent Citations
Source code static analysis method
CN111240982A
Hybrid depth defect prediction method based on code snippet analysis
CN112035345A
