Interface misuse defect detection method, device, readable medium and electronic device
By generating data from adversarial networks and building a call sequence library, the problem of low coverage of existing API misuse defect detection methods is solved, and higher detection accuracy and coverage are achieved.
Patent Information
- Application Number
- CN202111612820.2
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2021-12-27
- Publication Date
- 2025-02-21
- Estimated Expiration
- 2041-12-27
AI Technical Summary
The existing API misuse defect detection methods have low coverage and low accuracy, making it difficult to effectively detect API misuse defects in the code.
The second sample data is generated by generating the adversarial network, and a call sequence library is built in combination with the first sample data, which is used to match the interface call sequence to be detected and determine whether there are API misuse defects.
It significantly improves the coverage of API misuse defect detection, reduces detection false positives, and improves detection accuracy.
Smart Images

Figure CN114253860B_ABST
Abstract
Description
Technical Field
[0001] The present application belongs to the field of computer technology, and specifically relates to an interface misuse defect detection method, device, readable medium and electronic device. Background Art
[0002] With the advent of the big data era, code resources have exploded in the open source community. It has become common to use API (Application Programming Interface) as an access interface to existing code libraries or application frameworks to reuse high-quality code modules. However, due to the complexity of API usage, lack of documentation, or negligence of the user, developers sometimes use API incorrectly, resulting in API misuse defects, which can cause program operation errors under certain conditions and produce abnormal results or behaviors. Therefore, during the code development process, it is usually necessary to detect API useless defects.
[0003] At present, the method for detecting API misuse defects is generally to extract API usage specifications from open source code libraries or API documents, and then use API specification detection code for detection. However, due to the limited quantity and quality of documents or code samples, it often leads to problems such as detection coverage not meeting requirements and low accuracy.
[0004] It should be noted that the information disclosed in the above background technology section is only used to enhance the understanding of the background of the present application, and therefore may include information that does not constitute the prior art known to ordinary technicians in the field. Summary of the invention
[0005] The purpose of this application is to provide an interface misuse defect detection method, device, readable medium and electronic device to solve the problem of low API misuse defect detection coverage in related technologies.
[0006] Other features and advantages of the present application will become apparent from the following detailed description, or may be learned in part by the practice of the present application.
[0007] According to one aspect of an embodiment of the present application, a method for detecting interface misuse defects is provided.
[0008] Based on the first sample data and the preset interface call data, second sample data is obtained by generating an adversarial network; the first sample data is an interface call sequence corresponding to the real code data;
[0009] Forming a call sequence library according to the first sample data and the second sample data; the call sequence library includes normal sample interface call sequences without interface misuse defects;
[0010] Extracting the interface call sequence to be detected from the code data to be detected;
[0011] A match is performed in the call sequence library according to the interface call sequence to be detected to determine whether the code data to be detected has an interface misuse defect.
[0012] According to one aspect of an embodiment of the present application, there is provided an interface misuse defect detection device, comprising:
[0013] A sample data generation module, used to obtain second sample data by generating an adversarial network based on first sample data and preset interface call data; the first sample data is an interface call sequence corresponding to the real code data;
[0014] A sequence library construction module, used to form a call sequence library according to the first sample data and the second sample data; the call sequence library includes normal sample interface call sequences without interface misuse defects;
[0015] A sequence extraction module is used to extract the interface call sequence to be detected from the code data to be detected;
[0016] The defect detection module is used to match the interface call sequence to be detected in the call sequence library to determine whether the code data to be detected has an interface misuse defect.
[0017] In one embodiment of the present application, the adversarial generative network includes a generative network and a discriminative network; the sample data generation module is specifically used to:
[0018] Processing the preset interface call data through the generation network to obtain first generated data;
[0019] Based on the first sample data, the first generated data is discriminated by the discriminant network to obtain a discriminant result;
[0020] Based on the discrimination result and separate alternating iterative training, the network parameters of the generating network or the discriminating network are updated to obtain a trained generating network and a discriminating network;
[0021] The preset interface call data is processed by the trained generation network to obtain second sample data.
[0022] In one embodiment of the present application, the sample data generation module is further used to:
[0023] Performing low-dimensional vector mapping on the preset interface call data to obtain first vector data;
[0024] Performing attention processing on the first vector data to obtain second vector data;
[0025] The second vector data is subjected to reverse mapping processing to obtain first generated data.
[0026] In one embodiment of the present application, the sequence extraction module is specifically used to:
[0027] Constructing an interface call graph in the code data to be detected according to the code data to be detected;
[0028] Based on a depth-first search algorithm, an interface call sequence to be detected is extracted from the interface call graph.
[0029] In one embodiment of the present application, the defect detection module is specifically used to:
[0030] Matching the interface sequence to be detected with each normal sample interface call sequence in the call sequence library;
[0031] If a normal sample interface call sequence that is the same as the interface sequence to be detected exists in the call sequence library, it is determined that the code data to be detected does not have an interface misuse defect;
[0032] If there is no normal sample interface calling sequence that is the same as the interface calling sequence to be detected in the calling sequence library, it is determined that the code data to be detected has an interface misuse defect.
[0033] In one embodiment of the present application, the device further includes:
[0034] A correct sequence extraction module, for obtaining a normal sample interface call sequence corresponding to the interface name from the call sequence library according to the interface name of the interface call sequence to be detected if it is determined that the code data to be detected has an interface misuse defect;
[0035] The defect repair module is used to repair the defect code fragment corresponding to the interface call sequence to be detected according to the normal sample interface call sequence.
[0036] In one embodiment of the present application, the defect repair module is specifically used to:
[0037] Extracting interface parameter information of the defective code fragment corresponding to the interface call sequence to be detected;
[0038] The interface parameter information is fit-processed to the normal sample interface call sequence to repair the defective code fragment.
[0039] According to one aspect of an embodiment of the present application, a computer-readable medium is provided, on which a computer program is stored. When the computer program is executed by a processor, the interface misuse defect detection method in the above technical solution is implemented.
[0040] According to one aspect of an embodiment of the present application, an electronic device is provided, comprising: a processor; and a memory for storing executable instructions of the processor; wherein the processor executes the executable instructions to enable the electronic device to perform an interface misuse defect detection method as in the above technical solution.
[0041] According to one aspect of the embodiments of the present application, a computer program product or a computer program is provided, the computer program product or the computer program includes computer instructions, and the computer instructions are stored in a computer-readable storage medium. A processor of a computer device reads the computer instructions from the computer-readable storage medium, and the processor executes the computer instructions, so that the computer device executes the interface misuse defect detection method in the above technical solution.
[0042] In the technical solution provided in the embodiment of the present application, second sample data is generated by a generative adversarial network, and then the first sample data and the second sample data are combined to form a call sequence library, which greatly enriches the number of samples in the call sequence library and improves the coverage of API misuse defect detection. Therefore, when detecting the call sequence of the interface to be detected, the occurrence of false alarms is reduced and the detection accuracy is improved.
[0043] It should be understood that the foregoing general description and the following detailed description are exemplary and explanatory only and are not restrictive of the present application. BRIEF DESCRIPTION OF THE DRAWINGS
[0044] The drawings herein are incorporated into the specification and constitute a part of the specification, illustrate embodiments consistent with the present application, and together with the specification are used to explain the principles of the present application. Obviously, the drawings described below are only some embodiments of the present application, and for ordinary technicians in this field, other drawings can be obtained based on these drawings without creative work.
[0045] Figure 1 The exemplary system architecture block diagram applying the technical solution of the present application is schematically shown.
[0046] Figure 2 A flowchart of an interface misuse defect detection method provided by an embodiment of the present application is schematically shown.
[0047] Figure 3 A schematic diagram of a training framework for a generative adversarial network provided by an embodiment of the present application is schematically shown.
[0048] Figure 4 The schematic diagram schematically shows the architecture of a generation network provided by an embodiment of the present application.
[0049] Figure 5A flowchart of a process for repairing a defective code fragment provided by an embodiment of the present application is schematically shown.
[0050] Figure 6 The flowchart of the interface misuse defect detection method provided by one embodiment of the present application is schematically shown.
[0051] Figure 7 The structural block diagram of the interface misuse defect detection device provided in an embodiment of the present application is schematically shown.
[0052] Figure 8 The structure block diagram of a computer system suitable for implementing an electronic device according to an embodiment of the present application is schematically shown. DETAILED DESCRIPTION
[0053] Example embodiments will now be described more fully with reference to the accompanying drawings. However, example embodiments can be implemented in a variety of forms and should not be construed as limited to the examples set forth herein; rather, these embodiments are provided so that this application will be more comprehensive and complete and fully convey the concept of the example embodiments to those skilled in the art.
[0054] In addition, described feature, structure or characteristic can be combined in one or more embodiments in any suitable manner. In the following description, many specific details are provided to provide a full understanding of the embodiments of the present application. However, those skilled in the art will appreciate that the technical scheme of the present application can be put into practice without one or more of the specific details, or other methods, components, devices, steps, etc. can be adopted. In other cases, known methods, devices, realizations or operations are not shown or described in detail to avoid blurring the various aspects of the application.
[0055] The block diagrams shown in the accompanying drawings are merely functional entities and do not necessarily correspond to physically independent entities. That is, these functional entities may be implemented in software form, or in one or more hardware modules or integrated circuits, or in different networks and / or processor devices and / or microcontroller devices.
[0056] The flowcharts shown in the accompanying drawings are only exemplary and do not necessarily include all the contents and operations / steps, nor must they be executed in the order described. For example, some operations / steps can be decomposed, and some operations / steps can be combined or partially combined, so the actual execution order may change according to actual conditions.
[0057] Figure 1 The exemplary system architecture block diagram applying the technical solution of the present application is schematically shown.
[0058] like Figure 1As shown, the system architecture 100 may include a terminal device 110, a network 120, and a server 130. The terminal device 110 may include various electronic devices such as a smart phone, a tablet computer, a laptop computer, and a desktop computer. The server 130 may be an independent physical server, or a server cluster or a distributed system composed of multiple physical servers, or a cloud server providing cloud computing services. The network 120 may be a communication medium of various connection types that can provide a communication link between the terminal device 110 and the server 130, such as a wired communication link or a wireless communication link.
[0059] According to the implementation requirements, the system architecture in the embodiment of the present application can have any number of terminal devices, networks and servers. For example, the server 130 can be a server group composed of multiple server devices. In addition, the technical solution provided in the embodiment of the present application can be applied to the terminal device 110, can also be applied to the server 130, or can be implemented by the terminal device 110 and the server 130 together, and the present application does not make any special restrictions on this.
[0060] In one embodiment of the present application, the interface misuse defect detection method is executed by the server 130. For example, the server 130 obtains the second sample data through the generative adversarial network based on the first sample data and the preset interface call data. Then the server 130 forms a call sequence library according to the first sample data and the second sample data; the call sequence library includes normal sample interface call sequences that do not have interface misuse defects. Next, the server 130 extracts the interface call sequence to be detected from the code data to be detected. Finally, the server 130 matches the interface call sequence to be detected in the call sequence library to determine whether the code data to be detected has an interface misuse defect.
[0061] In one embodiment of the present application, the interface misuse defect detection method is performed by the terminal device 110. For example, the terminal device 110 obtains the second sample data through a generative adversarial network based on the first sample data and the preset interface call data. Then the terminal device 110 forms a call sequence library according to the first sample data and the second sample data; the call sequence library includes normal sample interface call sequences that do not have interface misuse defects. Next, the terminal device 110 extracts the interface call sequence to be detected from the code data to be detected. Finally, the terminal device 110 matches the interface call sequence to be detected in the call sequence library to determine whether the code data to be detected has an interface misuse defect.
[0062] The interface misuse defect detection method provided by the present application is described in detail below in conjunction with specific implementation methods.
[0063] Figure 2The flowchart of the interface misuse defect detection method provided by an embodiment of the present application is schematically shown. The method can be implemented by a server, such as Figure 1 The method can also be implemented by a terminal device, such as Figure 1 The terminal device 110 shown in FIG. Figure 2 As shown, the interface misuse defect detection method provided in the embodiment of the present application includes steps 210 to 240, which are specifically as follows:
[0064] Step 210: Based on the first sample data and the preset interface call data, obtain the second sample data by generating an adversarial network, where the first sample data is an interface call sequence corresponding to the real code data.
[0065] Specifically, the interface refers to the API (Application Programming Interface), also known as the API interface. API is the code provided by the computer operating system (OS) or program library for application programs to use. Its main purpose is to allow application developers to call a set of routine functions without having to consider the underlying source code or understand the details of its internal working mechanism.
[0066] Interface call data refers to the data required to call an API, which is a variable that affects the API call, including variables related to the API call sequence, parameter content, usage, and noise factors that affect the generation of the API call sequence. Among them, the noise factor z obeys an artificially selected prior probability distribution, such as uniform distribution, Gaussian distribution, etc.
[0067] The first sample data refers to the API call sequence corresponding to the real code data, and the API call sequence refers to the sequence formed by multiple APIs in the calling order. In addition, the API call sequence in the first sample data is a normal API call sequence, that is, there is no API misuse defect. The first sample data can be obtained from some API open source data sets.
[0068] The adversarial generative network is a generative model used to generate a large amount of required data. In the embodiment of the present application, the second sample data generated by the adversarial generative network is an API call sequence. Compared with the first sample data, the API call sequence represented by the second sample data is a "fake" API call sequence, that is, the API call sequence is a false API call sequence generated by the adversarial generative network based on the first sample data and then based on the preset interface call data, rather than a real API call sequence corresponding to the real code data.
[0069] In one embodiment of the present application, the adversarial generation network includes a generation network and a discriminant network. The generation network is used to generate a false API call sequence, and the discriminant network is used to discriminate the false API call sequence generated by the generation network to determine whether the false API call sequence generated by the generation network can be used.
[0070] In one embodiment of the present application, the process of generating second sample data includes: processing preset interface call data through a generation network to obtain first generation data; based on the first sample data, discriminating the first generation data through a discriminant network to obtain a discrimination result; based on the discrimination result and separate alternating iterative training, updating the network parameters of the generation network or the discriminant network to obtain a trained generation network and a discriminant network; processing the preset interface call data through the trained generation network to obtain second sample data.
[0071] Specifically, since the adversarial generative network includes two models, the generative network and the discriminative network, and the two models cannot be trained at the same time, the two models are trained using a separate alternating iterative training method. Separate alternating iterative training means that, first, one of the two models (denoted as model 1) is fixed and the other model (denoted as model 2) is trained a certain number of times; then, model 2 that has been trained a certain number of times is fixed and model 1 is trained a certain number of times; next, model 1 that has been trained a certain number of times is fixed and model 2 is trained, and the training is repeated in this way until the outputs of the two models reach the expected value.
[0072] In the embodiment of the present application, the preset interface call data is first processed by a generation network with initialized network parameters, that is, the generation network generates the first generation data according to the preset interface call data, and the first generation data obtained at this time will be marked as false data (false), that is, the first generation data is not the data corresponding to the real code data. At the same time, the first sample data is marked as true data (true). Next, the first generation data and the first sample data are simultaneously input into the discriminant network, and the discriminant network discriminates the true or false of the input data, and the discrimination result reflects the true or false of the input data by the probability that the input data is true. It can be seen that the input data of the discriminant network is labeled data, then according to the discrimination result and the label of the input data, the loss function of the discriminant network can be obtained, so that the network parameters of the discriminant network can be updated based on the loss function. After the network parameters of the discriminant network are updated, the network parameters of the generation network are kept unchanged (that is, the network parameters of the generation network are still the initialized network parameters), and the preset interface call data is processed again by the generation network to obtain new first generation data, and then the new generation data and the first generation data are input into the discriminant network for discrimination, and then the network parameters of the discriminant network are updated according to the new loss function. The training is repeated in this way. When the training reaches a certain number of times (or the loss function converges), a trained discriminant network is obtained. The trained discriminant network here can distinguish true and false data very well.
[0073] Next, the trained discriminant network is fixed, the generative network processes the preset interface call data, and inputs the processing results into the discriminant network for discrimination. The network parameters of the generative network are updated according to the discrimination results until the generative network is trained a certain number of times (or the loss function converges), and the trained generative network is obtained. The training goal of the generative network is to make the discriminant network unable to distinguish the true from the false of the data generated by the generative network.
[0074] After the generative network is trained, the generative network can be fixed to train the discriminative network, and then the discriminative network can be fixed to train the generative network. This cycle of training is called single alternating iterative training. When the objective function (also called loss function) of the discriminative network is maximized and the objective function of the generative network is minimized, the false data generated by the generative network is infinitely close to the real data, and the discriminative network cannot distinguish the true or false data generated by the generative network. The training of the generative network and the discriminative network is completed, and a trained generative adversarial network is obtained.
[0075] Finally, the pre-set interface call data is processed by the generative network in the trained generative adversarial network, and the data generated by the generative network is the second sample data. In this way, the second sample data is a "false" normal API call sequence, which is equivalent to the expansion of the first sample data.
[0076] For example, Figure 3 The following schematically shows a training framework for a generative adversarial network provided by an embodiment of the present application. Figure 3 As shown, the training framework of the generative adversarial network includes a noise input module 310, a generator network (Generator) 320, a discriminator network (Discriminator) 330, a sample acquisition module 340 and a sample feature extraction module 350.
[0077] The noise input module 310 is used to input the noise factor z that affects the generation of the API call sequence. The generation network 320 is used to process the noise factor z and generate the first generated data G(z). The sample acquisition module 340 is used to obtain the API call sequence of the real sample (including labeled samples and unlabeled samples). The sample feature extraction module 350 is used to extract features from the real sample API call sequence to obtain sample features (including labeled sample features and unlabeled sample features). The discriminant network discriminates the sample features and the first generated data G(z) and divides the data into three categories: abnormal API call sequences (i.e., data with API call defects), normal API call sequences (i.e., data with normal API calls) and false data; wherein false data is data that is not related to the API call, and the rest is true data. During the training process, the network parameters are updated according to the discrimination results, and the generation network and the discriminant network adopt adversarial training (i.e., separate alternating iterative training).
[0078] In one embodiment of the present application, the process of generating a network to process preset interface call data specifically includes: performing low-dimensional vector mapping on the preset interface call data to obtain first vector data; performing attention processing on the first vector data to obtain second vector data; and performing reverse mapping processing on the second vector data to obtain first generated data.
[0079] Specifically, the generating network is an NMT (Neural Machine Translation) network that introduces a state code feedback mechanism, including an encoder and a decoder. First, the encoder performs low-dimensional vector mapping on the preset interface call data to obtain first vector data. The input preset interface call data is a discrete data, which is represented in the form of a vector after low-dimensional vector mapping. The first vector data is also called a context vector.
[0080] After obtaining the first vector data, in order to make the data obtained by the subsequent decoder more accurate, an attention mechanism is added to the first vector data to form the second vector data. Then, the decoder performs reverse mapping processing on the second vector data to obtain the first generated data.
[0081] In the embodiment of the present application, an additional state code feedback mechanism is introduced to recursively update the encoder short annotation information (i.e., source end annotation information) gradually with the decoding process. The state code is the difference between the discriminator's judgment result of the generated network output data and the real data. The improved recursive update state code can be regarded as the generalization of the manual translation process in the neural network. The source end annotation information is accurately updated by gradually integrating the decoding end state. The update formula is as follows:
[0082] H k =GRU(H k-1 ,t k )
[0083] Among them, H k It represents the source annotation information related to the kth step, t is the related time variable, and the GRU function is used as the experimental nonlinear function. By directly fusing the encoding and decoding information and using the self-learning ability of the neural network to directly learn a subtraction operation, each step of the update can use the decoding state to subtract the source annotation information to be translated, which can better alleviate the problems of over-translation and under-translation.
[0084] For example, Figure 4 The schematic diagram of the architecture of the generation network provided by one embodiment of the present application is schematically shown. Figure 4 As shown, the generation network includes: an encoder 410, an attention module 420 and a decoder 430. The encoder 410 (that is, the source end) performs low-dimensional vector mapping on the preset interface call data (discrete code) to obtain the first vector data (context word vector). The attention module adds attention weights to the first vector data to obtain second vector data. The decoder 430 (that is, the target end) performs reverse mapping processing on the second vector data (the source end context vector after adding attention) according to the target end character mapping relationship to obtain the first generated data (target end code). At the same time, a status code is generated based on the discrimination result of the discriminant network on the first generated data, and according to the status code, a reverse recursive mechanism is used to update the annotation information of the source end context vector.
[0085] Step 220: Form a call sequence library according to the first sample data and the second sample data; the call sequence library includes normal sample interface call sequences without interface misuse defects.
[0086] Specifically, after obtaining the second sample data, the first sample data is combined with the second sample data to form a call sequence library. Then, the data in the call sequence library is a normal sample interface call sequence without interface misuse defects, that is, a normal API call sequence. In this way, the call sequence library will have enough sample data (normal API call sequence), thereby improving the coverage of subsequent API misuse defect detection.
[0087] Step 230: extract the interface call sequence to be detected from the code data to be detected.
[0088] Specifically, the code data to be detected is code data that needs to be tested for API misuse defects. The API call sequence is extracted from the code data to be detected as the interface call sequence to be detected.
[0089] In one embodiment of the present application, the process of extracting an API call sequence from code data to be detected specifically includes: constructing an interface call graph in the code data to be detected based on the code data to be detected; and extracting an interface call sequence to be detected from the interface call graph based on a depth-first search algorithm.
[0090] Specifically, first, an interface call graph is constructed based on the code data to be detected. Interface calls are actually function calls, so the interface call graph is actually a graph constructed based on the function call relationship in the code data to be detected. Next, in the interface call graph, a depth-first search (DFS) algorithm is used to search to obtain the API interface call sequence. The interface call graph is equivalent to a tree diagram. When using the depth-first search algorithm, a node in the interface call graph is used as the starting node, and every possible branch path of the starting node is searched until it cannot go deeper, and each node can only be visited once, thereby obtaining the API interface call sequence. An interface call graph may obtain multiple API call sequences.
[0091] In one embodiment of the present application, the process of extracting the interface call sequence to be detected may also be: first, performing AST syntax tree parsing on the code data to be detected, and then extracting the API sequence token from the syntax tree to obtain the interface call sequence to be detected.
[0092] Step 240: Match the interface call sequence to be detected in the call sequence library to determine whether the code data to be detected has an interface misuse defect.
[0093] Specifically, the call sequence library includes multiple normal sample interface call sequences, and the interface call sequence to be detected is matched with each normal sample interface call sequence in the call sequence library. When there is a normal sample interface call sequence identical to the interface sequence to be detected in the call sequence library, it indicates that the interface call sequence to be detected is a normal interface call sequence, so it can be determined that there is no interface misuse defect in the code data to be detected. When there is no normal sample interface call sequence identical to the interface sequence to be detected in the call sequence library, it indicates that the interface call sequence to be detected is not a normal interface call sequence, so it is considered that the interface call sequence to be detected is an abnormal interface call sequence, and therefore it is considered that there is an interface misuse defect in the code data to be detected.
[0094] APIs usually have usage constraints, such as restrictions on the calling order or calling conditions. API misuse refers to the behavior that violates the usage constraints, which may eventually lead to software crashes, defects or vulnerabilities. There are many cases of API misuse, such as redundant API calls, missing API calls or wrong API calls. For example, if you open a file in Java for writing, if you do not close the file after writing, the file writing will fail, which is an interface misuse defect.
[0095] In the technical solution provided in the embodiment of the present application, second sample data is generated by a generative adversarial network, and then the first sample data and the second sample data are combined to form a call sequence library, which greatly enriches the number of samples in the call sequence library and improves the coverage of API misuse defect detection. Therefore, when detecting the call sequence of the interface to be detected, the occurrence of false alarms is reduced and the detection accuracy is improved.
[0096] In one embodiment of the present application, when it is determined that the code data to be detected has an interface misuse defect, the defective fragment in the code to be detected can be further repaired, specifically: if it is determined that the code data to be detected has an interface misuse defect, then according to the interface name of the interface call sequence to be detected, a normal sample interface call sequence corresponding to the interface name is obtained from a call sequence library; and the defect code fragment corresponding to the interface call sequence to be detected is repaired according to the normal sample interface call sequence.
[0097] Specifically, for an interface call sequence with an interface misuse defect, its interface name (that is, the API method name) can be determined, and based on the interface name, an interface call sequence without an interface misuse defect, that is, a normal sample interface call sequence, can be obtained from the call sequence library. Then, the defect code snippet corresponding to the interface call sequence to be detected is repaired through the normal sample interface call sequence, and a defect-free code snippet can be obtained after the repair is completed.
[0098] In one embodiment of the present application, the process of repairing a defective code fragment according to a normal sample interface call sequence is specifically as follows: extracting interface parameter information of the defective code fragment corresponding to the interface call sequence to be detected; fitting the interface parameter information to the normal sample interface call sequence to repair the defective code fragment.
[0099] Specifically, the interface parameter information includes the location information of the code snippet, parameter information (i.e. which parameters are referenced, what are the parameter names, etc.), API call process, etc. The interface parameter information is abstracted, and an abstract comparison is established to make the parameters and method names of the normal sample interface call sequence consistent with the interface call sequence to be detected, so as to correct the interface call sequence to be detected. After modifying the interface call sequence to be detected to be the same as the normal sample interface call sequence, reverse abstraction is performed to restore the original parameters and method names. At this time, the obtained API call sequence is the correct API call sequence, thereby achieving the repair of the defective code snippet.
[0100] For example, Figure 5 The flowchart of the repair process of a defective code fragment provided by an embodiment of the present application is schematically shown. Figure 5 As shown in the figure, the repair process of the defective code snippet includes:
[0101] S510: Determine the defective code fragment corresponding to the interface call sequence to be detected.
[0102] S520 , extracting key parameters, that is, extracting interface parameter information of the defective code fragment corresponding to the interface call sequence to be detected.
[0103] S530: Fitting the API call sequence based on the API call sequence model. The API call sequence model is the call sequence library mentioned above, and the abnormal API call sequence corresponding to the defective code snippet is fitted and repaired by using the normal API call sequence in the call sequence library.
[0104] S540, call parameter adjustment, that is, putting the extracted parameter information back into the repaired API call sequence.
[0105] S550, fix code output.
[0106] For example, Figure 6 The flowchart of the interface misuse defect detection method provided by an embodiment of the present application is schematically shown as follows: Figure 6 As shown, the method includes:
[0107] S610: Perform AST syntax tree analysis on the source code (code data to be detected).
[0108] S620: Based on the AST syntax tree parsing result, extract the API sequence token to obtain the API call sequence to be detected.
[0109] S630, obtain an open source data set, generate a sample API call sequence through a generator (generator network), discriminate the sample API call sequence through a discriminator (discriminator network), and conduct adversarial training between the generator and the discriminator based on the discrimination result. Based on the trained generator, obtain a normal sample API call sequence, combine the open source data set with the normal sample API call sequence, and form a call sequence model (call sequence library).
[0110] S640: Match the API call sequence to be detected in the call sequence model to determine whether the API call sequence to be detected is a normal API call sequence, thereby determining the API misuse defect detection result of the source code.
[0111] S650: Output defect detection results.
[0112] S660: If the API call sequence to be detected is an abnormal API call sequence, a normal sample API call sequence (normal approximation) is extracted from the call sequence model to perform fitting and repair on the API call sequence to be detected, and a repair result is output.
[0113] It should be noted that although the steps of the method in the present application are described in a specific order in the drawings, this does not require or imply that the steps must be performed in this specific order, or that all the steps shown must be performed to achieve the desired results. Additionally or alternatively, some steps may be omitted, multiple steps may be combined into one step, and / or one step may be decomposed into multiple steps, etc.
[0114] The following introduces an apparatus embodiment of the present application, which can be used to execute the interface misuse defect detection method in the above-mentioned embodiment of the present application. Figure 7 The structure block diagram of the interface misuse defect detection device provided by the embodiment of the present application is schematically shown. Figure 7 As shown, the device comprises:
[0115] The sample data generation module 710 is used to obtain second sample data by generating an adversarial network based on the first sample data and the preset interface call data; the first sample data is the interface call sequence corresponding to the real code data;
[0116] A sequence library construction module 720 is used to form a call sequence library according to the first sample data and the second sample data; the call sequence library includes normal sample interface call sequences without interface misuse defects;
[0117] A sequence extraction module 730 is used to extract the interface call sequence to be detected from the code data to be detected;
[0118] The defect detection module 740 is used to match the interface call sequence to be detected in the call sequence library to determine whether the code data to be detected has an interface misuse defect.
[0119] In one embodiment of the present application, the adversarial generative network includes a generative network and a discriminative network; the sample data generation module 710 is specifically used to:
[0120] Processing the preset interface call data through the generation network to obtain first generated data;
[0121] Based on the first sample data, the first generated data is discriminated by the discriminant network to obtain a discriminant result;
[0122] Based on the discrimination result and separate alternating iterative training, the network parameters of the generating network or the discriminating network are updated to obtain a trained generating network and a discriminating network;
[0123] The preset interface call data is processed by the trained generation network to obtain second sample data.
[0124] In one embodiment of the present application, the sample data generation module 710 is further used to:
[0125] Performing low-dimensional vector mapping on the preset interface call data to obtain first vector data;
[0126] Performing attention processing on the first vector data to obtain second vector data;
[0127] The second vector data is subjected to reverse mapping processing to obtain first generated data.
[0128] In one embodiment of the present application, the sequence extraction module 730 is specifically used to:
[0129] Constructing an interface call graph in the code data to be detected according to the code data to be detected;
[0130] Based on a depth-first search algorithm, an interface call sequence to be detected is extracted from the interface call graph.
[0131] In one embodiment of the present application, the defect detection module 740 is specifically used to:
[0132] Matching the interface sequence to be detected with each normal sample interface call sequence in the call sequence library;
[0133] If a normal sample interface call sequence that is the same as the interface sequence to be detected exists in the call sequence library, it is determined that the code data to be detected does not have an interface misuse defect;
[0134] If there is no normal sample interface calling sequence that is the same as the interface calling sequence to be detected in the calling sequence library, it is determined that the code data to be detected has an interface misuse defect.
[0135] In one embodiment of the present application, the device further includes:
[0136] The correct sequence extraction module 730 is used to obtain a normal sample interface call sequence corresponding to the interface name from the call sequence library according to the interface name of the interface call sequence to be detected if it is determined that the code data to be detected has an interface misuse defect;
[0137] The defect repair module is used to repair the defect code fragment corresponding to the interface call sequence to be detected according to the normal sample interface call sequence.
[0138] In one embodiment of the present application, the defect repair module is specifically used to:
[0139] Extracting interface parameter information of the defective code fragment corresponding to the interface call sequence to be detected;
[0140] The interface parameter information is fit-processed to the normal sample interface call sequence to repair the defective code fragment.
[0141] The specific details of the interface misuse defect detection device provided in each embodiment of the present application have been described in detail in the corresponding method embodiments and will not be repeated here.
[0142] Figure 8 The structure block diagram of a computer system for implementing an electronic device according to an embodiment of the present application is schematically shown.
[0143] It should be noted that Figure 8 The computer system 800 of the electronic device shown is only an example and should not bring any limitation to the functions and scope of use of the embodiments of the present application.
[0144] like Figure 8As shown, the computer system 800 includes a central processing unit 801 (CPU), which can perform various appropriate actions and processes according to the program stored in the read-only memory 802 (ROM) or the program loaded from the storage part 808 to the random access memory 803 (RAM). Various programs and data required for system operation are also stored in the random access memory 803. The central processing unit 801, the read-only memory 802 and the random access memory 803 are connected to each other through a bus 804. The input / output interface 805 (Input / Output interface, i.e., I / O interface) is also connected to the bus 804.
[0145] The following components are connected to the input / output interface 805: an input section 806 including a keyboard, a mouse, etc.; an output section 807 including a cathode ray tube (CRT), a liquid crystal display (LCD), etc., and a speaker; a storage section 808 including a hard disk, etc.; and a communication section 809 including a network interface card such as a local area network card, a modem, etc. The communication section 809 performs communication processing via a network such as the Internet. A drive 810 is also connected to the input / output interface 805 as needed. A removable medium 811, such as a magnetic disk, an optical disk, a magneto-optical disk, a semiconductor memory, etc., is installed on the drive 810 as needed so that a computer program read therefrom is installed into the storage section 808 as needed.
[0146] In particular, according to an embodiment of the present application, the process described in each method flow chart can be implemented as a computer software program. For example, an embodiment of the present application includes a computer program product, which includes a computer program carried on a computer readable medium, and the computer program contains a program code for executing the method shown in the flow chart. In such an embodiment, the computer program can be downloaded and installed from the network through the communication part 809, and / or installed from the removable medium 811. When the computer program is executed by the central processor 801, various functions defined in the system of the present application are executed.
[0147] It should be noted that the computer-readable medium shown in the embodiment of the present application may be a computer-readable signal medium or a computer-readable storage medium or any combination of the above two. The computer-readable storage medium may be, for example, but not limited to, a system, device or device of electricity, magnetism, light, electromagnetic, infrared, or semiconductor, or any combination of the above. More specific examples of computer-readable storage media may include, but are not limited to: an electrical connection with one or more wires, a portable computer disk, a hard disk, a random access memory (RAM), a read-only memory (ROM), an erasable programmable read-only memory (EPROM), a flash memory, an optical fiber, a portable compact disk read-only memory (CD-ROM), an optical storage device, a magnetic storage device, or any suitable combination of the above. In the present application, a computer-readable storage medium may be any tangible medium containing or storing a program, which may be used by an instruction execution system, device or device or used in combination with it. In the present application, a computer-readable signal medium may include a data signal propagated in a baseband or as part of a carrier wave, wherein a computer-readable program code is carried. Such propagated data signals may take a variety of forms, including but not limited to electromagnetic signals, optical signals, or any suitable combination of the above. Computer readable signal media may also be any computer readable medium other than computer readable storage media, which may send, propagate, or process programs for use by or in conjunction with an instruction execution system, apparatus, or device. The program code contained on the computer readable medium may be processed using any suitable medium, including but not limited to: wireless, wired, etc., or any suitable combination of the above.
[0148] The flow chart and block diagram in the accompanying drawings illustrate the possible architecture, function and operation of the system, method and computer program product according to various embodiments of the present application. In this regard, each box in the flow chart or block diagram can represent a module, a program segment or a part of a code, and the above-mentioned module, program segment or a part of a code contains one or more executable instructions for realizing the specified logical function. It should also be noted that in some alternative implementations, the functions marked in the box can also occur in a different order from the order marked in the accompanying drawings. For example, two boxes represented in succession can actually be executed substantially in parallel, and they can sometimes be executed in the opposite order, depending on the functions involved. It should also be noted that each box in the block diagram or flow chart, and the combination of the boxes in the block diagram or flow chart can be implemented with a dedicated hardware-based system that performs a specified function or operation, or can be implemented with a combination of dedicated hardware and computer instructions.
[0149] It should be noted that, although several modules or units of the equipment for action execution are mentioned in the above detailed description, this division is not mandatory. In fact, according to the embodiments of the present application, the features and functions of two or more modules or units described above can be embodied in one module or unit. On the contrary, the features and functions of one module or unit described above can be further divided into being embodied by multiple modules or units.
[0150] Through the description of the above implementation methods, it is easy for those skilled in the art to understand that the example implementation methods described here can be implemented by software or by combining software with necessary hardware. Therefore, the technical solution according to the implementation methods of the present application can be embodied in the form of a software product, which can be stored in a non-volatile storage medium (which can be a CD-ROM, a USB flash drive, a mobile hard disk, etc.) or on a network, and includes several instructions to enable a computing device (which can be a personal computer, a server, a touch terminal, or a network device, etc.) to execute the method according to the implementation methods of the present application.
[0151] Those skilled in the art will readily appreciate other embodiments of the present application after considering the specification and practicing the invention disclosed herein. The present application is intended to cover any modification, use or adaptation of the present application, which follows the general principles of the present application and includes common knowledge or customary technical means in the art that are not disclosed in the present application.
[0152] It should be understood that the present application is not limited to the precise structures that have been described above and shown in the drawings, and that various modifications and changes may be made without departing from the scope thereof. The scope of the present application is limited only by the appended claims.
Claims
1. A method for detecting interface misuse defects, characterized in that: include: Performing low-dimensional vector mapping on the preset interface call data to obtain first vector data; Performing attention processing on the first vector data to obtain second vector data; Performing reverse mapping processing on the second vector data to obtain first generated data; Based on the first sample data, the first generated data is discriminated by a discriminant network in a generative adversarial network to obtain a discriminant result; The first sample data is an interface call sequence corresponding to the real code data; Based on the discrimination result and separate alternating iterative training, the network parameters of the generative network or the discriminative network in the generative adversarial network are updated to obtain a trained generative network and a discriminative network; Processing the preset interface call data through the trained generation network to obtain second sample data; Forming a call sequence library according to the first sample data and the second sample data; the call sequence library includes normal sample interface call sequences without interface misuse defects; Extracting the interface call sequence to be detected from the code data to be detected; A match is performed in the call sequence library according to the interface call sequence to be detected to determine whether the code data to be detected has an interface misuse defect.
2. The interface misuse defect detection method according to claim 1, characterized in that: Extract the interface call sequence to be detected from the code data to be detected, including: Constructing an interface call graph in the code data to be detected according to the code data to be detected; Based on a depth-first search algorithm, an interface call sequence to be detected is extracted from the interface call graph.
3. The interface misuse defect detection method according to claim 1, characterized in that: Matching the call sequence library according to the interface call sequence to be detected to determine whether the code data to be detected has an interface misuse defect, including: Matching the interface call sequence to be detected with each normal sample interface call sequence in the call sequence library; If a normal sample interface call sequence that is the same as the interface call sequence to be detected exists in the call sequence library, it is determined that the code data to be detected does not have an interface misuse defect; If there is no normal sample interface calling sequence that is the same as the interface calling sequence to be detected in the calling sequence library, it is determined that the code data to be detected has an interface misuse defect.
4. The interface misuse defect detection method according to claim 1, characterized in that: The method further comprises: If it is determined that the code data to be detected has an interface misuse defect, obtaining a normal sample interface call sequence corresponding to the interface name from the call sequence library according to the interface name of the interface call sequence to be detected; Defect repair is performed on the defective code fragment corresponding to the interface call sequence to be detected according to the normal sample interface call sequence.
5. The interface misuse defect detection method according to claim 4, characterized in that: Repairing defects in the interface call sequence to be detected according to the normal sample interface call sequence includes: Extracting interface parameter information of the defective code fragment corresponding to the interface call sequence to be detected; The interface parameter information is fit-processed to the normal sample interface call sequence to repair the defective code fragment.
6. An interface misuse defect detection device, characterized in that: include: A sample data generation module, used for performing low-dimensional vector mapping on the preset interface call data to obtain first vector data; Performing attention processing on the first vector data to obtain second vector data; performing reverse mapping processing on the second vector data to obtain first generated data; It is also used to discriminate the first generated data through a discriminant network in a generative adversarial network based on the first sample data to obtain a discrimination result; The first sample data is an interface call sequence corresponding to the real code data; Based on the discrimination result and separate alternating iterative training, the network parameters of the generative network or the discriminative network in the generative adversarial network are updated to obtain a trained generative network and a discriminative network; Processing the preset interface call data through the trained generation network to obtain second sample data; A sequence library construction module, used to form a call sequence library according to the first sample data and the second sample data; the call sequence library includes normal sample interface call sequences without interface misuse defects; A sequence extraction module is used to extract the interface call sequence to be detected from the code data to be detected; The defect detection module is used to match the interface call sequence to be detected in the call sequence library to determine whether the code data to be detected has an interface misuse defect.
7. A computer readable medium having a computer program stored thereon, characterized in that: When the computer program is executed by a processor, the interface misuse defect detection method described in any one of claims 1 to 5 is implemented.
8. An electronic device, characterized in that: include: processor; as well as A memory, configured to store executable instructions of the processor; The processor executes the executable instructions to enable the electronic device to execute the interface misuse defect detection method described in any one of claims 1 to 5.
Citation Information
Patent Citations
Code detection method and device and storage medium
CN113836020A
Method for automatic verification of correctness of API sequences, recording medium and device for performing the method
KR101548364B1