Code detection method and device and loop statement code detection method and device

By performing syntactic analysis on the code to obtain intermediate code and matching it with code templates for detection, the problems of path explosion and performance loss in symbolic execution are solved, achieving efficient and accurate code detection.

CN120974492APending Publication Date: 2025-11-18ALIBABA CLOUD COMPUTING CO LTD
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202410624375.9
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-05-17
Publication Date
2025-11-18

AI Technical Summary

Technical Problem

Existing symbolic execution techniques suffer from path explosion and reduced computer performance in code detection, affecting detection efficiency and accuracy.

Method used

By performing syntax analysis on the code to be detected to obtain intermediate code, and matching it with a set of code templates, detection is performed using code execution conditions, thus avoiding actual code execution and reducing path explosion and computer performance loss.

Benefits of technology

It improves the accuracy and efficiency of code detection, reduces the risk to computer performance, and ensures the accuracy and universality of detection results.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN120974492A_ABST
    Figure CN120974492A_ABST
Patent Text Reader

Abstract

The embodiment of the invention provides a code detection method and device and a loop statement code detection method and device.The code detection method comprises the steps that a to-be-detected code is determined, syntactic analysis is conducted on the to-be-detected code, and an intermediate code corresponding to the to-be-detected code is obtained; matching the intermediate code with a code template in a code template set to obtain a target code template matched with the intermediate code; a code execution condition corresponding to the target code template is determined, code execution detection is conducted on the intermediate code according to the code execution condition, a code detection result is obtained, and the code execution condition is used for limiting the code execution process of the intermediate code. According to the method, the accuracy of code detection results is improved; and the code detection result is obtained by reasoning the intermediate code instead of really executing the code, so that the problems of path explosion and the like are avoided, and the risk of reducing the computer performance by code detection is reduced.
Need to check novelty before this filing date? Find Prior Art

Description

TECHNICAL FIELD

[0001] Embodiments of the present specification relate to the technical field of code detection, and particularly relate to a code detection method. One or more embodiments of the present specification also relate to a code detection device, a loop statement code detection method, and a loop statement code detection device. BACKGROUND

[0002] Code security detection is an important part of the software development process, aiming to identify potential security vulnerabilities and risks in source code, binary code, or applications.

[0003] One code security detection scheme is symbolic execution. Symbolic execution is a program analysis technique that typically uses symbolic values instead of real values to simulate program execution, resulting in expressions composed of symbolic values of input variables and constants, thereby enabling semantic analysis of programs.

[0004] However, symbolic execution poses a risk to computer performance, such as path explosion and excessive solution time, which can lead to reduced computer performance. Therefore, there is an urgent need for a technical solution to address the above technical problems. SUMMARY

[0005] In view of the above, embodiments of the present specification provide a code detection method. One or more embodiments of the present specification also relate to a code detection device, a loop statement code detection method, a loop statement code detection device, a computing device, a computer-readable storage medium, and a computer program product to address the technical defect of reduced computer performance due to the risk of symbolic execution affecting computer performance in the prior art.

[0006] According to a first aspect of embodiments of the present specification, a code detection method is provided, comprising:

[0007] determining a code to be detected, and performing syntax analysis on the code to be detected to obtain an intermediate code corresponding to the code to be detected, wherein the intermediate code contains syntax information of the code to be detected;

[0008] matching the intermediate code with a code template in a code template set to obtain a target code template matching the intermediate code;

[0009] determining a code execution condition corresponding to the target code template, and performing code execution detection on the intermediate code according to the code execution condition to obtain a code detection result, wherein the code execution condition is used to limit the code execution process of the intermediate code.

[0010] According to a second aspect of the embodiments of the present specification, a loop statement code detection method is provided, comprising:

[0011] A loop statement code to be detected is determined, and syntax analysis is performed on the loop statement code to be detected to obtain loop statement intermediate code corresponding to the loop statement code to be detected, wherein the intermediate code contains syntax information of the loop statement code to be detected;

[0012] The loop statement intermediate code is matched with a loop statement code template in a code template set to obtain a target loop statement code template matched with the loop statement intermediate code;

[0013] A loop statement code execution condition corresponding to the target loop statement code template is determined, and code execution detection is performed on the loop statement intermediate code according to the loop statement code execution condition to obtain a loop statement code detection result, wherein the loop statement code execution condition is used to limit a code execution process of the loop statement intermediate code.

[0014] According to a third aspect of the embodiments of the present specification, a code detection apparatus is provided, comprising:

[0015] An intermediate code obtaining module is configured to determine a code to be detected, and perform syntax analysis on the code to be detected to obtain intermediate code corresponding to the code to be detected, wherein the intermediate code contains syntax information of the code to be detected;

[0016] A target code template obtaining module is configured to match the intermediate code with a code template in a code template set to obtain a target code template matched with the intermediate code;

[0017] A code detection module is configured to determine a code execution condition corresponding to the target code template, and perform code execution detection on the intermediate code according to the code execution condition to obtain a code detection result, wherein the code execution condition is used to limit a code execution process of the intermediate code.

[0018] According to a fourth aspect of the embodiments of the present specification, a loop statement code detection apparatus is provided, comprising:

[0019] A loop statement intermediate code obtaining module is configured to determine a loop statement code to be detected, and perform syntax analysis on the loop statement code to be detected to obtain loop statement intermediate code corresponding to the loop statement code to be detected, wherein the intermediate code contains syntax information of the loop statement code to be detected;

[0020] The target loop statement code template obtaining module is configured to match the loop statement intermediate code with a loop statement code template in a code template set, and obtain a target loop statement code template matched with the loop statement intermediate code.

[0021] The loop statement code detection module is configured to determine a loop statement code execution condition corresponding to the target loop statement code template, and perform code execution detection on the loop statement intermediate code according to the loop statement code execution condition to obtain a loop statement code detection result, wherein the loop statement code execution condition is used to limit a code execution process of the loop statement intermediate code.

[0022] According to a fifth aspect of the embodiments of the present specification, a computing device is provided, comprising:

[0023] a memory and a processor;

[0024] The memory is configured to store computer programs / instructions, and the processor is configured to execute the computer programs / instructions, which realize the steps of the code detection method or the loop statement code detection method when executed by the processor.

[0025] According to a sixth aspect of the embodiments of the present specification, a computer readable storage medium is provided, which stores computer programs / instructions, which realize the steps of the code detection method or the loop statement code detection method when executed by the processor.

[0026] According to a seventh aspect of the embodiments of the present specification, a computer program product is provided, comprising computer programs / instructions, which realize the steps of the code detection method or the loop statement code detection method when executed by the processor.

[0027] The one or more embodiments of the specification provide a code detection method. The code detection method includes the following steps: performing syntax analysis on a to-be-detected code to obtain an intermediate code corresponding to the to-be-detected code, the intermediate code containing syntax information of the to-be-detected code, so that the code detection of the to-be-detected code is converted into the detection of the intermediate code containing the syntax information of the to-be-detected code, thereby improving the universality of the detection. Moreover, the intermediate code is obtained by performing syntax analysis on the to-be-detected code, so that the syntax of the to-be-detected code is consistent with the syntax of the intermediate code. Then, the intermediate code is matched with a code template in a code template set to obtain a target code template matched with the intermediate code, thereby realizing the classification of the intermediate code. Then, the intermediate code is subjected to security detection according to a code execution condition corresponding to the target code template, so as to infer whether the intermediate code has a risk of causing a security problem in a code execution process, thereby improving the accuracy of a detection result obtained by inferring the intermediate code. Furthermore, the accuracy of a code detection result of the to-be-detected code with the consistent syntax is improved. Moreover, the code detection result is obtained by inferring the intermediate code, rather than by actually executing the code, thereby avoiding problems such as path explosion or too long solving time, and reducing the risk of reducing the computer performance caused by the code detection. BRIEF DESCRIPTION OF DRAWINGS

[0028] Figure 1 is a specific application scenario of a code detection method provided by one or more embodiments of the specification;

[0029] Figure 2 is a flowchart of a code detection method provided by one or more embodiments of the specification;

[0030] Figure 3 is a process flowchart of a code detection method provided by one or more embodiments of the specification;

[0031] Figure 4 is a flowchart of a loop statement code detection method provided by one or more embodiments of the specification;

[0032] Figure 5 is a structural schematic diagram of a code detection device provided by one or more embodiments of the specification;

[0033] Figure 6 is a structural schematic diagram of a loop statement code detection device provided by one or more embodiments of the specification;

[0034] Figure 7 is a structural block diagram of a computing device provided by one or more embodiments of the specification. DETAILED DESCRIPTION

[0035] In the following description, numerous specific details are set forth in order to provide a thorough understanding of the present description. However, the present description can be practiced without the specific details. In other instances, well-known methods, procedures, components, and circuits have not been described in detail so as not to obscure the present description. The present description can be implemented using a variety of different hardware and software configurations, including hardware implementations, software implementations, and implementations incorporating both hardware and software.

[0036] The terminology used in this description is for the purpose of describing particular embodiments only and is not intended to be limiting of one or more embodiments of the present description. As used in this description and the appended claims, the singular forms "a," "an" and "the" are intended to include the plural forms as well, unless the context clearly indicates otherwise. It will also be understood that the term "and / or" as used herein refers to and encompasses any and all possible combinations of one or more of the associated listed items.

[0037] It will be understood that, although the terms first, second, etc. can be used herein to describe various information, these terms are not intended to denote a temporal or chronological order. Rather, these terms are used solely to distinguish one from another only. For example, a first can be termed a second, and, similarly, a second can be also termed a first, without departing from the scope of one or more embodiments of the present description. As used herein, the term "if' can be construed to mean "when" or "in response to determining" or "in response to a determination" or "in response to the expiration of a period of time” or "upon determination," depending on the context.

[0038] In addition, it should be noted that the user information (including but not limited to user device information, user personal information, etc.) and data (including but not limited to data for analysis, stored data, displayed data, etc.) involved in one or more embodiments of the present description are all information and data authorized by the user or authorized by all parties, and the collection, use and processing of relevant data need to comply with relevant laws, regulations and standards of relevant countries and regions, and provide corresponding operation portal for user to choose authorization or refusal.

[0039] First, the nomenclature involved in one or more embodiments of the present description is explained.

[0040] Prior knowledge: the relevant knowledge or information that the detection personnel or detection tool has before code detection, including but not limited to the structure, syntax, security vulnerability mode, security threat, programming error, function variable security risk information, etc. of the code.

[0041] Intermediate representation (IR): refers to an internal representation generated after processing a source program, which embodies the semantic and syntactic structure of the source program, i.e. a source program representation between syntax analysis and target code generation.

[0042] Abstract Syntax Tree (AST): refers to a tree representation of the abstract syntax structure of source code, wherein a tree node in the Abstract Syntax Tree represents a syntax structure in the source code.

[0043] Token: a word unit obtained by dividing a sentence, code, or the like, for example, a word unit of a sentence can be understood as a word, an operator, an identifier, a punctuation mark, and the like, and a word unit of code can be understood as a constant, a variable, an operator, a punctuation mark, and the like.

[0044] Generally, in order to ensure the safety during code execution, security detection will be performed on the code before code execution.

[0045] The security detection method provided by one or more embodiments of the present specification is based on prior knowledge.

[0046] For example, a function code (denoted as code (1)) uses an unsafe function strcpy, and the code (1) is as follows:

[0047] #include<stdio.h>

[0048] int main(){

[0049] char src

[30] ;

[0050] scanf("%s",src);

[0051] char dest

[20] ;

[0052] strcpy(dest,src);

[0053] return 0;

[0054] }

[0055] Based on prior knowledge, calling strcpy has a risk of causing security problems, and taking the above code (1) as an example, calling strcpy has the following two security problems.

[0056] (1) The length of the src string is greater than the length of the dest buffer, causing write out-of-bound.

[0057] (2) The string of src is not 0-terminated, or the position of 0-terminated exceeds the length of the src buffer, causing read out-of-bound.

[0058] The aforementioned security detection methods based on prior knowledge cannot achieve security detection without using prior knowledge. Another security detection method is symbolic execution, which can perform security detection on code without relying on prior knowledge.

[0059] However, symbolic execution carries the risk of path explosion. For example, a piece of code (2) is as follows:

[0060] "char*my_strcpy(char*dest,const char*src){

[0061] char*p = dest;

[0062] while(*src!='\0'){

[0063] *dest=*src;

[0064] dest++;

[0065] src++;

[0066] }

[0067] return p;

[0068] }”

[0069] In code (2), due to the use of a loop statement, the variable dest and the variable src are not limited in character size. Therefore, code (2) will encounter the path explosion problem during symbol execution, which will lead to a decrease in computer performance.

[0070] In view of this, this specification provides a code testing method, and also relates to a loop statement code testing method, a code testing device, a loop statement code testing device, a computing device, a computer-readable storage medium, and a computer program product, which will be described in detail in the following embodiments.

[0071] See Figure 1 , Figure 1 The diagram illustrates a specific application scenario of a code detection method provided according to one or more embodiments of this specification.

[0072] like Figure 1 As shown, Figure 1 The system includes a client 102 and a code detection server 104. The client 102 includes, but is not limited to, mobile phones, tablets, personal digital assistants, laptops, notebooks, netbooks, smartwatches, and smart glasses. The code detection server 104 includes, but is not limited to, physical servers, standalone cloud servers, and cloud server clusters.

[0073] In particular implementation, the client 102 sends the code to be detected to the code detection server 104, the code detection server 104 determines the code to be detected, and performs syntax analysis on the code to be detected to obtain the intermediate code corresponding to the code to be detected, and then matches the intermediate code with the code templates in the code template set to obtain the target code template matched with the intermediate code, and performs code execution detection on the intermediate code according to the code execution condition corresponding to the target code template to infer whether the intermediate code has the risk of security problem in the code execution process, and obtains the code detection result. After obtaining the code detection result, the code detection server 104 sends the code detection result to the client 102.

[0074] Further, after receiving the code detection result, the client 102 can display the code detection result to the user for subsequent code optimization, code evaluation, etc.

[0075] The code detection method provided by one or more embodiments of the present specification performs syntax analysis on the code to be detected sent by the client to obtain the intermediate code corresponding to the code to be detected, which has syntax-related information, so that the code detection on the code to be detected is converted into the detection on the intermediate code containing syntax-related information, improving the universality of the detection. Moreover, since the intermediate code is obtained by performing syntax analysis on the code to be detected, the syntax of the code to be detected is consistent with the syntax of the intermediate code. Furthermore, the intermediate code is matched with the code templates in the code template set to obtain the target code template matched with the intermediate code, realizing the classification of the intermediate code, so that the intermediate code is detected according to the code execution condition corresponding to the target code template, inferring whether the intermediate code has the risk of security problem in the code execution process, improving the accuracy of the detection result obtained by inferring the intermediate code, and further improving the accuracy of the code detection result of the code to be detected which has consistent syntax with the intermediate code. Moreover, the code detection result is obtained by inferring the intermediate code, rather than by actually executing the code, avoiding the problems of path explosion or too long solving time, reducing the risk of reducing the computer performance caused by code detection. Further, the accurate code detection result is sent to the client for the client to display the detection result of the code to be detected to the user, so that the user can perform more accurate code optimization, code adjustment, code evaluation, etc. on the code to be detected according to the code detection result.

[0076] Referring to Figure 2 , Figure 2 is a flowchart of a code detection method provided by one or more embodiments of the present specification, specifically comprising the following steps.

[0077] Step 202: Determine the code to be detected, and perform syntax analysis on the code to be detected to obtain intermediate code corresponding to the code to be detected.

[0078] The intermediate code contains syntax information of the code to be detected.

[0079] Specifically, the code to be detected can be understood as the code to be detected for security detection. The code to be detected can be obtained through the code stored in the code detection server, or can be obtained through the code detection server receiving the code to be detected sent by the client. The client can obtain the code to be detected by collecting the instructions input by the user. For example, the code to be detected can be understood as the above-mentioned code (1), code (2), etc.

[0080] For ease of understanding, the embodiments of the present specification take the code (3) as an example to explain and describe the code to be detected, which is specifically as follows:

[0081] #include<stdio.h>

[0082] #includestdlib.h>

[0083] #include<string.h>

[0084] char*my_strcpy(char*dest,const char*src){

[0085] char*p=dest;

[0086] while(*src!='\0'){

[0087] *dest=*src;

[0088] dest++;

[0089] src++;

[0090] }

[0091] return p;

[0092] }

[0093] int main(){

[0094] char src[]="Hello,World!";

[0095] my_strcpy(dest,src);

[0096] printf("Copied string: %s\n", dest);

[0097] return 0;

[0098] }”

[0099] Syntax analysis can be understood as analyzing the syntax information contained in the code, including but not limited to lexical analysis, semantic analysis, syntax structure analysis, etc.

[0100] Intermediate code can be understood as code obtained by syntax analysis of the code to be detected. The intermediate code contains syntax information of the code to be detected. For example, the intermediate code can be understood as IR (Intermediate Representation). In the case where syntax analysis includes lexical analysis, semantic analysis, and syntax structure analysis, the syntax information of the code to be detected contained in the intermediate code obtained by syntax analysis of the code to be detected includes but is not limited to tokenization information, semantic information, and syntax structure information.

[0101] In a specific implementation, the code detection server determines the code to be detected, performs syntax analysis on the code to be detected, determines the tokenization information, semantic information, and syntax structure information contained in the code to be detected, and converts the code to be detected into a code representation form according to the tokenization information, semantic information, and syntax structure information contained in the code to be detected, to obtain the intermediate code corresponding to the code to be detected.

[0102] In actual application, the specific implementation of syntax analysis of the code to be detected to obtain the intermediate code corresponding to the code to be detected can be as follows:

[0103] The syntax analysis of the code to be detected to obtain the intermediate code corresponding to the code to be detected includes:

[0104] Syntax recognition is performed on the code to be detected to obtain a syntax tree corresponding to the code to be detected;

[0105] At least two initial syntax nodes in the syntax tree and a syntax relationship between the at least two initial syntax nodes are determined;

[0106] According to the at least two initial syntax nodes and the syntax relationship between the at least two initial syntax nodes, an intermediate code corresponding to the code to be detected is obtained.

[0107] The syntax tree can be understood as a tree representation of abstract syntax structure, for example, the syntax tree can be understood as an AST. Based on this, the syntax tree corresponding to the code to be detected can be understood as a tree representation of abstract syntax structure obtained by formal conversion of the code to be detected.

[0108] The initial syntax node can be understood as a syntax node in the syntax tree, and the syntax relationship can be understood as a syntax association relationship between at least two syntax nodes in the syntax tree.

[0109] In a specific implementation, the above syntax analysis on the to-be-detected code to obtain the intermediate code corresponding to the to-be-detected code can be implemented by performing syntax recognition on the to-be-detected code, converting the to-be-detected code into a code representation form of a syntax tree, and determining at least two initial syntax nodes contained in the syntax tree (for example, an AST) and a syntax relationship between the at least two initial syntax nodes. Further, according to the at least two initial syntax nodes and the syntax relationship between the at least two initial syntax nodes, an intermediate code (for example, an IR) containing semantic information and syntax structure information of the to-be-detected code is abstracted.

[0110] The code detection method provided by one or more embodiments of the present specification can perform syntax recognition on the to-be-detected code to obtain a structured syntax tree corresponding to the to-be-detected code, so that the analysis of the code is more accurate and efficient. According to at least two initial syntax nodes contained in the syntax tree and a syntax relationship between the at least two initial syntax nodes, the syntax information contained in the to-be-detected code is determined, the accuracy of the syntax analysis on the to-be-detected code is improved, and the accuracy of the intermediate code obtained according to the at least two initial syntax nodes and the syntax relationship between the at least two initial syntax nodes is improved. In addition, due to the structuring of the syntax tree, the process of obtaining the intermediate code corresponding to the to-be-detected code according to the at least two initial syntax nodes and the syntax relationship between the at least two initial syntax nodes is more concise and convenient.

[0111] In actual application, in order to improve the accuracy of the syntax tree, a plurality of tokens can be obtained by performing lexical recognition on the to-be-detected code, and a syntax tree can be established according to the plurality of tokens and a syntax relationship between the tokens. The specific implementation is as follows:

[0112] The syntax recognition on the to-be-detected code to obtain the syntax tree corresponding to the to-be-detected code includes:

[0113] Performing lexical recognition on the to-be-detected code to obtain a plurality of tokens corresponding to the to-be-detected code;

[0114] Establishing a syntax tree corresponding to the to-be-detected code according to the plurality of tokens and a syntax relationship between the tokens.

[0115] The token can be understood as a basic element with syntax meaning obtained by cutting the code, for example, the token can be understood as token, and the token includes but is not limited to constant, variable, operator, and punctuation.

[0116] The plurality of tokens corresponding to the to-be-detected code can be understood as basic elements with syntactic meaning contained in the to-be-detected code. For example, the to-be-detected code is "return p;", and the tokens corresponding to the to-be-detected code "return p;" can be understood as "return", "p", and ";".

[0117] The syntactic relationship between the tokens can be understood as the relationship of mutual connection and combination of the tokens in the source code (in the case of understanding each token as a corresponding token of the source code, the source code can be understood as the to-be-detected code). The syntactic relationship between the tokens can represent the code structure, code syntax, code semantics, and code logic of the source code.

[0118] Based on this, the lexical recognition of the to-be-detected code to obtain the plurality of tokens corresponding to the to-be-detected code can be understood as recognizing the tokens in the to-be-detected code and splitting the to-be-detected code to obtain the plurality of tokens corresponding to the to-be-detected code. The establishment of the syntax tree corresponding to the to-be-detected code according to the plurality of tokens and the syntactic relationship between the tokens can be understood as taking each token as a tree node, establishing the tree branches between the tree nodes according to the syntactic relationship between the tokens, and thus establishing the syntax tree corresponding to the to-be-detected code according to the plurality of tree nodes and the tree branches between the tree nodes.

[0119] The code detection method provided by one or more embodiments of the present specification can analyze the to-be-detected code, divide the to-be-detected code into a series of tokens constituting the basic elements of the program logic, and thus more accurately understand the semantics and structure of the to-be-detected code according to the tokens and the syntactic relationship between the tokens. Further, the establishment of the syntax tree corresponding to the to-be-detected code according to the plurality of tokens and the syntactic relationship between the tokens improves the accuracy and structural degree of the syntax tree, thereby improving the convenience and accuracy of subsequent intermediate code acquisition.

[0120] In actual application, in order to reduce the code amount of subsequent code detection, the syntax nodes corresponding to the preset target statement type in the syntax tree can be detected, and the specific implementation manner is as follows:

[0121] The obtaining of the intermediate code corresponding to the to-be-detected code according to the at least two initial syntax nodes and the syntactic relationship between the at least two initial syntax nodes comprises:

[0122] From the at least two initial syntax nodes, at least two target syntax nodes corresponding to the preset target statement type are determined.

[0123] According to the syntactic relationship between the at least two initial syntax nodes, the syntactic relationship between the at least two target syntax nodes is determined.

[0124] According to the at least two target syntax nodes and the syntax relationship between the at least two target syntax nodes, intermediate code corresponding to the to-be-detected code is obtained.

[0125] The preset target statement type includes, but is not limited to, a loop statement type, a conditional statement type, a selection statement type, and the like.

[0126] Specifically, the probability of the occurrence of the code for reducing the computer performance (i.e., the unsafe code) is relatively high in some statement types, and the probability of the occurrence of the code for reducing the computer performance is relatively low in some other statement types. Therefore, the statement type in which the probability of the occurrence of the code for reducing the computer performance is relatively high can be preset as the preset target statement type. For example, the preset target statement type can be understood as the loop statement type.

[0127] Based on this, in the case where the preset target statement type is understood as the loop statement type, at least two target syntax nodes corresponding to the loop statement type can be determined from the at least two initial syntax nodes in the syntax tree obtained above, and the at least two target syntax nodes correspond to the code of the loop statement type in the to-be-detected code.

[0128] Further, after the at least two target syntax nodes are determined, according to the at least two target syntax nodes and the syntax relationship between the at least two target syntax nodes, the intermediate code corresponding to the to-be-detected code can be understood as follows: according to the association relationship between the at least two initial syntax nodes, the association relationship between the at least two target syntax nodes can be determined, and according to the at least two target syntax nodes and the syntax relationship between the at least two target syntax nodes, the intermediate code corresponding to the to-be-detected code is obtained. The specific implementation manner of obtaining the intermediate code corresponding to the to-be-detected code according to the at least two target syntax nodes and the syntax relationship between the at least two target syntax nodes can be referred to the specific implementation manner of obtaining the intermediate code corresponding to the to-be-detected code according to the at least two initial syntax nodes and the syntax relationship between the at least two initial syntax nodes, which will not be described herein again.

[0129] The code detection method provided by one or more embodiments of the present specification improves the simplicity of the intermediate code obtained subsequently by performing syntax analysis on the at least two target syntax nodes corresponding to the preset target statement type in the to-be-detected code, reduces the resource consumption caused by the code that does not contribute to the code detection in the code detection, improves the performance of the computer in code detection, and improves the accuracy and detection efficiency of the entire code detection process.

[0130] Step 204: match the intermediate code with a code template in the code template set to obtain a target code template matched with the intermediate code.

[0131] The code template can be understood as a template of the intermediate code with a security vulnerability. Based on prior knowledge, the intermediate code (for example, IR) with a security vulnerability that usually exists in code security detection can be collected in advance, and a code template is generated. Multiple code templates can be collected into a code template set.

[0132] Based on this, after obtaining the intermediate code, the intermediate code is matched with the code template in the code template set, and the code template matched with the intermediate code can be determined. The code template matched with the intermediate code is the target code template. The code syntax structure, semantic information, and code logic of the target code template are the same as or belong to the same category as the code syntax structure, semantic information, and code logic corresponding to the intermediate code.

[0133] For example, the intermediate code is "determine whether variable B is 0", and the intermediate code is matched with the code template in the code template set. The target code template "determine whether variable B is 0" matched with the intermediate code can be obtained.

[0134] For another example, the intermediate code is "take the value at the address of pointer src and assign it to variable dest", and the intermediate code is matched with the code template in the code template set. The target code template "take the value at the address of pointer A and assign it to variable B" matched with the intermediate code can be obtained.

[0135] It should be noted that A and B in the pointer A and variable B in the above examples are generic data naming and are not limited to the form of pointer naming A and variable naming B. For example, it can also be pointer X, pointer Y, variable X, and variable Y.

[0136] In actual application, the intermediate code is matched with the code template, and the matching can be performed through code execution logic. The specific implementation manner is as follows:

[0137] The intermediate code is matched with the code template in the code template set to obtain a target code template matched with the intermediate code.

[0138] The code execution logic contained in the intermediate code is matched with the code execution logic contained in each code template in the code template set to obtain a target code template matched with the intermediate code.

[0139] The code execution logic can be understood as a syntax logic that the code follows during execution. For example, a code to be detected is "char *p = dest;", and the intermediate code is "declare a pointer variable p of a character, and initialize the pointer variable p to the address of the variable dest." The code execution logic can be understood as "declare a pointer variable of a character, and initialize the pointer variable to the address of another variable." For another example, a code is "dest ++;", and the code execution logic can be understood as "the variable is incremented."

[0140] In a specific implementation, the matching of the intermediate code with the code templates in the code template set can be performed by matching the code execution logic of the intermediate code with the code execution logic of each code template to obtain a code template that is the same as the code execution logic of the intermediate code. The code template is the target code template matched with the intermediate code.

[0141] The code detection method provided by one or more embodiments of the present specification can match the code execution logic from the logical level, so that the target code template with consistent logic is obtained, and the universality and accuracy of the matching are improved.

[0142] In actual applications, the risk of a security problem caused by a loop statement is higher, and therefore, the code corresponding to the loop statement can be mainly detected to improve the code detection efficiency. The specific implementation manner is as follows:

[0143] The code to be detected includes a loop statement code, the intermediate code includes a loop statement intermediate code, and the code execution logic includes a loop type, a loop condition, and a loop body.

[0144] The matching of the code execution logic included in the intermediate code with the code execution logic included in each code template in the code template set to obtain the target code template matched with the intermediate code includes:

[0145] The loop type, the loop condition, and the loop body included in the loop statement intermediate code are matched with the loop type template, the loop condition template, and the loop body template included in each code template in the code template set, respectively, to obtain the target code template matched with the loop statement intermediate code. The loop type included in the loop statement intermediate code is the same as the loop type template of the target code template, the loop condition template included in the loop statement intermediate code is consistent with the loop logic of the loop condition template included in the target code template, and the loop body included in the loop statement intermediate code is consistent with the execution logic of the loop body template included in the target code template.

[0146] The loop statement code can be understood as a code of a loop statement type, such as "for(i=0;i<5;i++){printf("This is iteration %d\n",i);}", "while(i<5){printf("This is iteration %d\n",i);i++;}", and the like.

[0147] The loop type can be understood as a type of the loop statement code, including but not limited to a for loop type, a while loop type, a do-while loop type, an until loop type, and the like.

[0148] The loop condition can be understood as a condition for evaluating whether the loop continues or terminates. Before each loop iteration, the result of evaluating the loop condition can be used to determine whether the code in the loop body should be executed continuously, for example, the loop condition can be set as a condition for the loop to continue, that is, the loop continues to execute when the loop condition is met, or the loop condition can be set as a condition for the loop to terminate, that is, the loop terminates when the loop condition is met.

[0149] The loop body can be understood as a code statement that needs to be executed each time the loop statement code iterates, for example, the loop body can be understood as "*dest=*src;dest++;src++;" in the above code (2), or "printf("This is iteration %d\n",i);" in "for(i=0;i<5;i++){printf("This is iteration %d\n",i);}".

[0150] In a specific implementation, the to-be-detected code can include loop statement code, and the intermediate code can include loop statement intermediate code. Based on this, the code execution logic of the intermediate code includes three parts of a loop type, a loop condition, and a loop body. Taking the above example, for example, the intermediate code corresponding to the above code (2) can be

[0151] "Loop type: while;

[0152] Loop condition: assign the value at the address of pointer A to temporary variable B, and determine whether B is 0;

[0153] Loop body:

[0154] Assign the value at the address of pointer A to temporary variable C,

[0155] Save C to the address where pointer B is located,

[0156] Pointer A is incremented,

[0157] Pointer B increment operation

[0158] Based on this, the loop type, the loop condition and the loop body corresponding to the loop statement intermediate code of the code (2) can be determined according to the loop statement intermediate code of the code (2), and the loop type, the loop condition and the loop body corresponding to each code template in the code template set can be determined in the same way, that is, the loop type template, the loop condition template and the loop body template corresponding to each code template in the code template set.

[0159] Further, the loop type, the loop condition and the loop body contained in the loop statement intermediate code can be matched with the loop type template, the loop condition template and the loop body template contained in each code template in the code template set respectively to obtain a target code template matched with the loop statement intermediate code.

[0160] Specifically, the loop type contained in the loop statement intermediate code is the same as the loop type template of the target code template, which can be understood as the type of the loop statement being the same, for example, the loop type contained in the loop statement intermediate code is a for loop type, the loop type template of the target code template is a for loop type, or both are while loop types, etc.

[0161] The loop condition template contained in the loop statement intermediate code is consistent with the loop logic of the loop condition template contained in the target code template, which can be understood as the continuation condition or the termination condition of the loop condition of the loop statement intermediate code being the same as the continuation condition or the termination condition of the loop condition template contained in the target code template, for example, the loop condition is that the loop is terminated when a variable pointed to by a pointer is 0, or the loop is continued when the variable pointed to by the pointer is not 0, etc.

[0162] The loop body contained in the loop statement intermediate code is consistent with the execution logic of the loop body template contained in the target code template, which can be understood as the execution logic contained in the loop body contained in the loop statement intermediate code being consistent with the execution logic of the loop body template contained in the target code template, for example, the execution logic is both "assigning the value of one variable to another variable and performing self-increment operation on the two variables", "comparing the size relationship of the values of two variables and performing self-increment operation on the variable with smaller value", etc.

[0163] The code detection method provided by one or more embodiments of the present specification can be used to match the loop type, the loop condition and the loop body contained in the loop statement intermediate code with each code template one by one to obtain the target code template corresponding to the loop statement code, so that the main code detection can be performed on the loop statement with a higher probability of security problem, and the efficiency of code detection is improved.

[0164] Step 206: determining a code execution condition corresponding to the target code template, and performing code execution detection on the intermediate code according to the code execution condition to obtain a code detection result.

[0165] The code execution condition is used to limit the code execution process of the intermediate code.

[0166] Specifically, the code execution condition includes but is not limited to a code inference result and an execution limitation condition.

[0167] The inference result can be understood as inferring the code template to predict the code execution result that should be generated when the code template is executed, for example, the inference result of the code (2) can be understood as "assigning the string pointed to by the src buffer to the buffer pointed to by the dest".

[0168] The execution limitation condition can be understood as a limitation condition that needs to be limited in the execution process of the code execution until the inference result is obtained, and the execution limitation condition corresponding to the code (2) can be understood as "the length of the src string is less than or equal to the length of the dest string", "the src string ends with 0", "the position of the src string ending with 0 exceeds the length of the src buffer", and the like.

[0169] In actual implementation, in the case where the target code template corresponding to the intermediate code is determined, the code execution condition corresponding to the target code template can be determined, and the intermediate code is detected according to the code execution condition corresponding to the target code template to determine whether the intermediate code has a security vulnerability and obtain a code detection result.

[0170] In actual application, the intermediate code can be detected according to the code execution condition, the code detection result can be obtained by detecting the corresponding data in the intermediate code, and the specific implementation mode is as follows:

[0171] The code execution detection on the intermediate code according to the code execution condition to obtain a code detection result includes:

[0172] According to the code execution condition, code execution data corresponding to the code execution condition is determined from the intermediate code.

[0173] The data execution rule corresponding to the code execution data is determined from the code execution condition.

[0174] According to the data execution rule corresponding to the execution data, the code execution data is detected to obtain a code detection result of whether the code to be detected is a secure code.

[0175] The code execution data includes, but is not limited to, variables, constants, operators, and the like.

[0176] Specifically, in the case of determining the code execution condition corresponding to the intermediate code, according to the code execution condition, the code execution data corresponding to the code execution condition which needs to be detected can be determined. In the case of the above example, the code execution condition includes "the length of the string of variable src is less than or equal to the length of the string of variable dest", and the code execution data corresponding to the code execution condition can be understood as the string of variable src and the string of variable dest. For another example, the code execution condition includes "the string of variable src ends with 0", and the code execution data corresponding to the code execution condition can be understood as the string of variable src, and the like.

[0177] Further, the data execution rule can be understood as an execution rule that needs to be met by the code execution data in the case of ensuring that the intermediate code is safe code. In the case of the above example, the code execution condition includes "the string of variable src ends with 0", and the data execution rule corresponding to "the string of variable src" can be understood as "the end character of the string of variable src is limited to 0". Or, for example, the code execution condition includes "the string of variable src and the string of variable dest", and the data execution rule corresponding to "the string of variable src and the string of variable dest" can be understood as "the length of the string of variable src needs to be less than or equal to the length of the string of variable dest".

[0178] Further, the code detection result includes, but is not limited to, that the code to be detected is safe code, that the code to be detected is unsafe code, and the like. According to the data execution rule corresponding to the execution data, the safety detection of the code execution data can determine whether the code execution data meets the data execution rule corresponding to the execution data. According to the judgment result of whether the code execution data meets the data execution rule corresponding to the execution data, the code detection result of whether the code to be detected is safe code can be obtained.

[0179] The code detection method provided by one or more embodiments of the present specification determines the code execution data in the intermediate code which needs to be detected by using the code execution condition, so as to detect the code execution data by using the data execution rule in the code execution condition, without the need to detect the complete intermediate code, thereby reducing the complexity of code detection, reducing the computer resource consumption of code detection, improving the performance of computer code detection, and improving the detection efficiency of code detection.

[0180] In actual application, whether the code execution data satisfies the data execution rule can be determined to obtain a code detection result of whether the code to be detected is safe, and the specific implementation manner is as follows:

[0181] The safety of the code execution data is detected according to the data execution rule corresponding to the execution data, and a code detection result of whether the code to be detected is a safe code is obtained, including:

[0182] The safety of the code execution data is detected according to the data execution rule corresponding to the execution data;

[0183] In a case where it is determined that the code execution data satisfies the data execution rule, a code detection result that the code to be detected is a safe code is obtained; or

[0184] In a case where it is determined that the code execution data does not satisfy the data execution rule, a code detection result that the code to be detected is an unsafe code is obtained.

[0185] The safe code can be understood as a code without security vulnerabilities, and the unsafe code can be understood as a code with security vulnerabilities.

[0186] In the above example, in a case where the data execution rule is understood as "the end character of the src string is limited to 0", the end character of the code execution data src string is "0", and a code detection result that the code to be detected is safe is obtained, or for example, in a case where the data execution rule is understood as "the length of the src string needs to be less than or equal to the length of the dest string", the length of the code execution data src string is greater than the length of the code execution data dest string, and a code detection result that the code to be detected is unsafe is obtained.

[0187] The code detection method provided by one or more embodiments of the present specification directly compares whether the data execution data satisfies the data execution rule corresponding thereto, to obtain a code detection result of whether the code to be detected is a safe code or an unsafe code, so that the code detection result is more concise and accurate, and the code to be detected is detected without actually executing the code to be detected, thereby improving the computer performance of code detection.

[0188] In actual application, in a case where the code to be detected is an unsafe code, security risk information of the code to be detected can be provided for subsequent code optimization, code evaluation, etc., and the specific implementation manner is as follows:

[0189] After the code detection result that the code to be detected is an unsafe code is obtained, the method further includes:

[0190] According to the association relationship between the data execution rule and the security risk information, security risk information corresponding to the to-be-detected code is determined, wherein the security risk information contains a security problem existing in the to-be-detected code.

[0191] Specifically, the security risk information can be understood as information of a security risk existing in the code, and the security risk information contains a security problem existing in the code, which includes but is not limited to a security problem such as a buffer overflow of the code itself, a loop logic error, and a security problem existing in a library, a framework, a plug-in, a component, and the like used by the code.

[0192] The association relationship between the data execution rule and the security risk information can be understood as a corresponding relationship between the data execution rule and the corresponding security risk information. In the above example, when the data execution rule is understood as "the end character of the src string is limited to 0", the corresponding security risk information can be understood as "read out of bounds". Specifically, when the end character of the src string is not 0, there is a risk that the position read by the src string is higher than the length of the buffer, so the corresponding security risk information is "read out of bounds". For another example, when the data execution rule is understood as "the length of the src string needs to be less than or equal to the length of the dest string", the corresponding security risk information can be understood as "write out of bounds". Specifically, when the length of the src string is greater than the length of the dest string, there is a security risk that the data written into the destination copy buffer is out of bounds when the to-be-copied buffer (the buffer of the src string) is copied to the destination copy buffer (the buffer of the dest string), so the corresponding security risk information is "write out of bounds".

[0193] Therefore, according to the association relationship between the data execution rule and the security risk information, the security risk information corresponding to the to-be-detected code can be determined.

[0194] The code detection method provided by one or more embodiments of the present specification can specifically give the security risk information corresponding to the to-be-detected code when the code detection result obtained by the to-be-detected code is an unsafe code, so that the detection result of the code detection is more intuitive, and the convenience of using the code detection method by the user is improved.

[0195] In actual application, in order to improve the code detection efficiency, the association relationship between the code template and the code execution condition can be established in advance, and the specific implementation manner is as follows:

[0196] Before the determining of the code execution condition corresponding to the target code template, the method further includes:

[0197] determine code execution logic contained in each code template in the code template set;

[0198] determine code execution conditions corresponding to each code template according to the code execution logic contained in each code template;

[0199] establish an association between each code template and the code execution condition corresponding to each code template.

[0200] Specifically, the code execution logic contained in each code template can be analyzed based on prior knowledge to determine the code execution conditions corresponding to each code template and establish the association between the code template and the code execution condition, which will not be described in detail in the embodiments of the present specification.

[0201] The code detection method provided by one or more embodiments of the present specification establishes the association between each code template and the code execution condition in advance, so that the complex matching process between the target code template and the code execution condition is not required in the code detection process, further improving the detection efficiency of the code detection. Moreover, the association between the code template and the code execution condition is established according to the code execution logic contained in each code template, so that the logic between the code template and the code execution condition is more matched, improving the accuracy of the association between the code template and the code execution condition.

[0202] The code detection method provided by one or more embodiments of the present specification converts the code detection of the to-be-detected code into the detection of the intermediate code containing syntax-related information by performing syntax analysis on the to-be-detected code to obtain the intermediate code corresponding to the to-be-detected code, improving the universality of the detection. Moreover, since the intermediate code is obtained by performing syntax analysis on the to-be-detected code, the syntax of the to-be-detected code is consistent with the syntax of the intermediate code. Then, the intermediate code is matched with the code templates in the code template set to obtain a target code template matched with the intermediate code, realizing the classification of the intermediate code. Then, the intermediate code is subjected to code execution detection according to the code execution condition corresponding to the target code template to infer whether the intermediate code has a risk of security problem in the code execution process, improving the accuracy of the detection result obtained by inferring the intermediate code, further improving the accuracy of the code detection result of the to-be-detected code with the same syntax as the intermediate code. Moreover, the code detection result is obtained by inferring the intermediate code instead of actually executing the code, avoiding the problems of path explosion or too long solving time, and reducing the risk of reducing the computer performance caused by the code detection.

[0203] The following will be described in detail with reference to the accompanying drawings Figure 3With the code detection method provided in the specification as an example, the application of the code detection method to the code detection of the loop statement code is taken as an example to further illustrate the code detection method. Among them, Figure 3 A process flow diagram of a code detection method provided by one or more embodiments of the specification is shown, specifically including the following steps.

[0204] Step 302: Obtain the code to be detected.

[0205] Specifically, the code detection server can obtain the code to be detected by obtaining it locally, or can obtain the code to be detected by receiving the code to be detected sent by the client for security detection. The client can obtain the code to be detected by obtaining the user (e.g., a developer) inputting instructions through the client human-computer interaction interface.

[0206] After obtaining the code to be detected, the code to be detected can be subjected to code security detection.

[0207] Step 304: Perform lexical analysis on the code to be detected to obtain the token corresponding to the code to be detected.

[0208] Specifically, the code to be detected can be subjected to lexical analysis using a lexical tool (e.g., re2c, tree-sitter, etc.) to obtain the token (i.e., the above-mentioned token) corresponding to the code to be detected.

[0209] Step 306: Perform syntax abstraction on the token corresponding to the code to be detected to obtain the abstract syntax tree corresponding to the code to be detected.

[0210] Specifically, the token corresponding to the code to be detected can be subjected to syntax abstraction using a syntax tool to obtain the AST (Abstract Syntax Tree, abstract syntax tree, i.e., the above-mentioned syntax tree) corresponding to the code to be detected.

[0211] Step 308: Generate the intermediate representation corresponding to the code to be detected according to the abstract syntax tree.

[0212] Specifically, the loop syntax nodes corresponding to the AST and the relationships between the loop syntax nodes are obtained, and the IR (Intermediate Representation, intermediate representation, i.e., the above-mentioned intermediate code) corresponding to the loop syntax nodes is generated according to the loop syntax nodes and the relationships between the loop syntax nodes.

[0213] In actual application, the syntax node where the loop structure is located can be located through traversal or the like, and the loop syntax node corresponding to the loop structure is extracted, and the loop syntax node is converted into IR, where the IR includes a series of loop statement instructions corresponding to the loop syntax node and the relationship between the loop syntax nodes, such as a syntax instruction corresponding to a loop type, a syntax instruction corresponding to a loop condition, a syntax instruction corresponding to a loop body, and the like.

[0214] Step 310: matching the intermediate representation with the patterns in the pattern library to obtain a target pattern corresponding to the intermediate representation.

[0215] Specifically, the IR is matched with the patterns stored in the library to obtain a target pattern matched with the IR.

[0216] Specifically, the loop type of the IR is matched with the loop type in the patterns (i.e., the code templates) stored in the library (i.e., the code template set), the patterns with the same loop type as the loop type of the IR are filtered out, then, the patterns with the same loop condition category as the loop condition category of the IR are filtered out from the patterns with the same loop type as the loop type of the IR, and further, the patterns with the same loop body category as the loop body category of the IR are matched from the patterns with the same loop condition category as the loop condition category of the IR, that is, the target pattern (i.e., the target code template) is obtained.

[0217] Step 312: detecting the intermediate representation according to the restriction condition (i.e., the code execution condition) corresponding to the target pattern to obtain a detection result of the code to be detected.

[0218] Specifically, the IR is detected according to the restriction condition corresponding to the target pattern (including but not limited to detecting constants, variables, operators, and the like), a conclusion whether the code to be detected has a security problem is output, and in the case where the code to be detected has a security problem, the security problem existing in the code to be detected is output.

[0219] For example, the code (2) is detected according to steps 302-312, and the target pattern corresponding to the code (2) is obtained as follows:

[0220] Loop type: while

[0221] Loop condition:

[0222] Assign the value at the address of the pointer src to the temporary variable dest,

[0223] Determine whether dest is 0;

[0224] Loop body:

[0225] Assign the value at the address of the pointer src to the temporary variable C,

[0226] C is saved to the address where the pointer dest is located,

[0227] Pointer src increment operation,

[0228] Pointer dest increment operation

[0229] The restriction condition corresponding to the target pattern is "(1) if the length of the src string is greater than the length of the dest, report the write out-of-bound; (2) if the src string is not terminated by 0, or the position of the termination by 0 exceeds the length of the src buffer".

[0230] Therefore, by using the restriction condition corresponding to the target pattern, the code execution process of the code to be detected can be detected, and a code detection result is obtained.

[0231] For example, when the code (2) is called once, the length of the string of the variable src is 10 bytes, the length of the string of the variable dest is 8 bytes, and the length of the string of src is greater than the length of the string of dest, a code detection result of "the code is an unsafe code, and there is a security problem: write out-of-bound" is reported.

[0232] The code detection method provided by one or more embodiments of the present specification matches the IR corresponding to the code to be detected with the patterns existing in the library, and uses the restriction condition corresponding to the target pattern matched with the IR to reason and summarize the IR, so as to obtain the code detection result corresponding to the IR, without obtaining the code detection result by actually executing the code. The risk of path explosion problem caused by actually executing the loop statement in the code to be detected is reduced, thereby reducing the risk of reducing the performance of the computer caused by code detection. Further, since the abstract syntax tree of the code to be detected is obtained by performing lexical analysis and syntax analysis on the code to be detected, and the IR is extracted according to the abstract syntax tree, the consistency between the IR and the code to be detected is ensured, so that the code detection result corresponding to the code to be detected is obtained by accurately reasoning and summarizing the IR using the restriction condition corresponding to the target pattern matched with the IR, thereby improving the accuracy of the code detection result.

[0233] The above is a schematic scheme of the code detection method of the present embodiment. It should be noted that the technical scheme of the code detection method belongs to the same concept as the technical scheme of the code detection method described above, and the details of the technical scheme of the code detection method which are not described in detail can be referred to the description of the technical scheme of the code detection method.

[0234] Referring to Figure 4 , Figure 4 is a flowchart of a loop statement code detection method provided by one or more embodiments of the present specification, specifically comprising the following steps.

[0235] Step 402: determining a to-be-detected loop statement code, and performing syntax analysis on the to-be-detected loop statement code to obtain a loop statement intermediate code corresponding to the to-be-detected loop statement code.

[0236] The intermediate code contains syntax information of the to-be-detected loop statement code.

[0237] Step 404: matching the loop statement intermediate code with a loop statement code template in a code template set to obtain a target loop statement code template matched with the loop statement intermediate code.

[0238] Step 406: determining a loop statement code execution condition corresponding to the target loop statement code template, and performing code execution detection on the loop statement intermediate code according to the loop statement code execution condition to obtain a loop statement code detection result.

[0239] The loop statement code execution condition is used to limit a code execution process of the loop statement intermediate code.

[0240] The loop statement code detection method provided by one or more embodiments of the present specification converts code detection on a loop statement to-be-detected code into detection on a loop statement intermediate code containing syntax-related information, improves the universality of detection, and because the loop statement intermediate code is obtained by performing syntax analysis on the loop statement to-be-detected code, the syntax of the loop statement to-be-detected code is consistent with the syntax of the loop statement intermediate code. Then, the loop statement intermediate code is matched with a loop statement code template in a loop statement code template set to obtain a target loop statement code template matched with the loop statement intermediate code, the loop statement intermediate code is classified, and code execution detection is performed on the loop statement intermediate code according to a loop statement code execution condition corresponding to the target loop statement code template to infer whether the loop statement intermediate code has a risk of causing a security problem in a code execution process, improve the accuracy of a loop statement code detection result obtained by inferring the loop statement intermediate code, and further improve the accuracy of a loop statement code detection result of a loop statement to-be-detected code with consistent syntax with the loop statement intermediate code. Moreover, the loop statement code detection result is obtained by inferring the loop statement intermediate code instead of actually executing the code, which avoids problems such as path explosion or too long solving time, and reduces the risk of reducing computer performance caused by loop statement code detection.

[0241] The foregoing is a schematic scheme of the loop statement code detection method of the embodiment. It should be noted that the technical scheme of the code detection method belongs to the same concept as the technical scheme of the code detection method described above, and the details of the technical scheme of the loop statement code detection method that are not described in detail can be referred to the description of the technical scheme of the code detection method.

[0242] Corresponding to the method embodiments described above, the present specification also provides code detection device embodiments, Figure 5 A structural schematic diagram of a code detection device provided by one or more embodiments of the present specification is shown. As shown in the figure, Figure 5 The device comprises:

[0243] The intermediate code obtaining module 502 is configured to determine a code to be detected, and perform syntax analysis on the code to be detected to obtain an intermediate code corresponding to the code to be detected, wherein the intermediate code contains syntax information of the code to be detected.

[0244] The target code template obtaining module 504 is configured to match the intermediate code with code templates in a code template set to obtain a target code template matched with the intermediate code.

[0245] The code detection module 506 is configured to determine a code execution condition corresponding to the target code template, and perform code execution detection on the intermediate code according to the code execution condition to obtain a code detection result, wherein the code execution condition is used to limit a code execution process of the intermediate code.

[0246] Optionally, the intermediate code obtaining module 502 is further configured to:

[0247] perform syntax recognition on the code to be detected to obtain a syntax tree corresponding to the code to be detected;

[0248] determine at least two initial syntax nodes in the syntax tree and a syntax relationship between the at least two initial syntax nodes;

[0249] obtain an intermediate code corresponding to the code to be detected according to the at least two initial syntax nodes and the syntax relationship between the at least two initial syntax nodes.

[0250] Optionally, the intermediate code obtaining module 502 is further configured to:

[0251] perform lexical recognition on the code to be detected to obtain a plurality of words corresponding to the code to be detected;

[0252] establish a syntax tree corresponding to the code to be detected according to the plurality of words and a syntax relationship between the words.

[0253] Optionally, the intermediate code obtaining module 502 is further configured to:

[0254] determine at least two target syntax nodes corresponding to a preset target sentence type from the at least two initial syntax nodes;

[0255] determine a syntax relationship between the at least two target syntax nodes according to a syntax relationship between the at least two initial syntax nodes;

[0256] obtain an intermediate code corresponding to the code to be detected according to the at least two target syntax nodes and the syntax relationship between the at least two target syntax nodes.

[0257] Optionally, the target code template obtaining module 504 is further configured to:

[0258] match a code execution logic contained in the intermediate code with a code execution logic contained in each code template in the code template set to obtain a target code template matched with the intermediate code.

[0259] Optionally, the code to be detected comprises a loop statement code, and the intermediate code comprises a loop statement intermediate code, and the code execution logic comprises a loop type, a loop condition and a loop body.

[0260] Optionally, the target code template obtaining module 504 is further configured to:

[0261] match the loop type, the loop condition and the loop body contained in the loop statement intermediate code with a loop type template, a loop condition template and a loop body template contained in each code template in the code template set respectively to obtain a target code template matched with the loop statement intermediate code, wherein the loop type contained in the loop statement intermediate code is the same as the loop type template of the target code template, the loop condition template contained in the loop statement intermediate code is consistent with the loop logic of the loop condition template contained in the target code template, and the loop body contained in the loop statement intermediate code is consistent with the execution logic of the loop body template contained in the target code template.

[0262] Optionally, the code detection module 506 is further configured to:

[0263] determine code execution data corresponding to the code execution condition from the intermediate code according to the code execution condition;

[0264] determine a data execution rule corresponding to the code execution data from the code execution condition;

[0265] According to a data execution rule corresponding to the execution data, the code execution data is detected for security to obtain a code detection result of whether the code to be detected is a secure code.

[0266] Optionally, the code detection module 506 is further configured to:

[0267] According to a data execution rule corresponding to the execution data, the code execution data is detected for security;

[0268] In a case where it is determined that the code execution data satisfies the data execution rule, a code detection result that the code to be detected is a secure code is obtained; or

[0269] In a case where it is determined that the code execution data does not satisfy the data execution rule, a code detection result that the code to be detected is an insecure code is obtained.

[0270] Optionally, the apparatus further comprises a security risk information determination module configured to:

[0271] According to an association between the data execution rule and security risk information, security risk information corresponding to the code to be detected is determined, wherein the security risk information contains a security problem existing in the code to be detected.

[0272] Optionally, the apparatus further comprises a code execution condition matching module configured to:

[0273] Determine code execution logic contained in each code template in the code template set;

[0274] According to the code execution logic contained in each code template, determine a code execution condition corresponding to each code template;

[0275] Establish an association between each code template and the code execution condition corresponding to each code template.

[0276] The one or more embodiments of the specification provide a code detection device. The device obtains an intermediate code corresponding to a code to be detected by performing syntax analysis on the code to be detected, so that the code detection of the code to be detected is converted into the detection of the intermediate code containing syntax related information, improving the universality of the detection. Moreover, the intermediate code is obtained by performing syntax analysis on the code to be detected, so that the syntax of the code to be detected is consistent with the syntax of the intermediate code. Then, the intermediate code is matched with a code template in a code template set to obtain a target code template matched with the intermediate code, realizing the classification of the intermediate code. Thus, the code execution detection of the intermediate code is performed according to the code execution condition corresponding to the target code template, the risk of the occurrence of a security problem in the code execution process of the intermediate code is inferred, the accuracy of the detection result obtained by inferring the intermediate code is improved, the accuracy of the code detection result of the code to be detected which is consistent with the syntax of the intermediate code is improved, and the risk of reducing the computer performance caused by the code detection is reduced.

[0277] The above is a schematic scheme of the code detection device of the embodiment. It should be noted that the technical scheme of the code detection device belongs to the same concept as the technical scheme of the code detection method described above. The details of the technical scheme of the code detection device which are not described in detail can be referred to the description of the technical scheme of the code detection method.

[0278] Corresponding to the method embodiments described above, the specification also provides a loop statement code detection device embodiment, Figure 6 A structural schematic diagram of a loop statement code detection device provided by one or more embodiments of the specification is shown. As shown in the figure, Figure 6 The device comprises:

[0279] The loop statement intermediate code obtaining module 602 is configured to determine a code to be detected loop statement, and perform syntax analysis on the code to be detected loop statement to obtain a loop statement intermediate code corresponding to the code to be detected loop statement, wherein the intermediate code contains syntax information of the code to be detected loop statement;

[0280] The target loop statement code template obtaining module 604 is configured to match the loop statement intermediate code with a loop statement code template in a code template set to obtain a target loop statement code template matched with the loop statement intermediate code;

[0281] The loop statement code detection module 606 is configured to determine a loop statement code execution condition corresponding to the target loop statement code template, and perform code execution detection on the loop statement intermediate code according to the loop statement code execution condition, to obtain a loop statement code detection result, wherein the loop statement code execution condition is used to limit the code execution process of the loop statement intermediate code.

[0282] The one or more embodiments of the present specification provide a loop statement code detection device. The device performs syntax analysis on loop statement code to be detected to obtain loop statement intermediate code corresponding to the loop statement code to be detected, which contains syntax related information. The code detection of the loop statement code to be detected is converted into the detection of the loop statement intermediate code containing syntax related information, improving the universality of the detection. Moreover, the syntax of the loop statement intermediate code is consistent with the syntax of the loop statement code to be detected, which is obtained by performing syntax analysis on the loop statement code to be detected. Then, the loop statement intermediate code is matched with loop statement code templates in a loop statement code template set to obtain a target loop statement code template matched with the loop statement intermediate code, which realizes the classification of the loop statement intermediate code. The code execution detection of the loop statement intermediate code is performed according to the loop statement code execution condition corresponding to the target loop statement code template, which infers whether the loop statement intermediate code has a risk of security problems in the code execution process. The accuracy of the loop statement code detection result obtained by inferring the loop statement intermediate code is improved. The accuracy of the loop statement code detection result of the loop statement code to be detected, which has consistent syntax with the loop statement intermediate code, is improved. Moreover, the loop statement code detection result is obtained by inferring the loop statement intermediate code, instead of executing the code, which avoids the problems of path explosion or too long solving time, and reduces the risk of reducing the computer performance caused by the loop statement code detection.

[0283] The above is a schematic scheme of a loop statement code detection device according to the present embodiment. It should be noted that the technical scheme of the loop statement code detection device belongs to the same concept as the technical scheme of the loop statement code detection method described above. The details of the technical scheme of the loop statement code detection device that are not described in detail can be referred to the description of the technical scheme of the loop statement code detection method.

[0284] Referring to Figure 7 , Figure 7A structural block diagram of a computing device 700 is shown, in accordance with one or more embodiments of the present specification. The components of the computing device 700 include, but are not limited to, a memory 710 and a processor 720. The processor 720 is connected to the memory 710 through a bus 730, and a database 750 is used to store data.

[0285] The computing device 700 also includes an access device 740 that enables the computing device 700 to communicate via one or more networks 760. Examples of these networks include a public switched telephone network (PSTN), a local area network (LAN), a wide area network (WAN), a personal area network (PAN), or a combination of communication networks such as the Internet. The access device 740 can include one or more of any type of network interface (e.g., a network interface card (NIC)), wired or wireless, such as an IEEE 802.11 wireless local area network (WLAN) wireless interface, a Worldwide Interoperability for Microwave Access (Wi-MAX) interface, an Ethernet interface, a Universal Serial Bus (USB) interface, a cellular network interface, a Bluetooth interface, a Near Field Communication (NFC) interface, and the like.

[0286] In one embodiment of the present specification, the above-mentioned components of the computing device 700 and other components not shown in the Figure 7 may be connected to each other, for example, through a bus. It should be understood that Figure 7 The structural block diagram of the computing device shown is for the purpose of example only, and is not a limitation on the scope of the present specification. Other components can be added or replaced as needed by those skilled in the art.

[0287] The computing device 700 can be any type of stationary or mobile computing device, including a mobile computer or mobile computing device (e.g., a tablet computer, a personal digital assistant, a laptop computer, a notebook computer, a netbook, etc.), a mobile phone (e.g., a smartphone), a wearable computing device (e.g., a smart watch, smart glasses, etc.), or other type of mobile device, or a stationary computing device such as a desktop computer or a personal computer (PC). The computing device 700 can also be a mobile or stationary server.

[0288] The memory 710 is configured to store computer programs / instructions, and the processor 720 is configured to execute the computer programs / instructions stored in the memory 710. The computer programs / instructions, when executed by the processor, implement the steps of the code detection method or the loop statement code detection method.

[0289] Each of the embodiments in the specification is described in a progressive manner, and the same or similar parts between the embodiments can be referred to each other. Each of the embodiments mainly describes the difference from other embodiments. In particular, the computing device embodiment is basically similar to the code detection method or the loop statement code detection method embodiment, and thus the description is relatively simple, and the relevant parts can be referred to the description of the code detection method or the loop statement code detection method embodiment.

[0290] An embodiment of the specification further provides a computer readable storage medium storing computer programs / instructions. The computer programs / instructions, when executed by a processor, implement the steps of the code detection method or the loop statement code detection method.

[0291] Each of the embodiments in the specification is described in a progressive manner, and the same or similar parts between the embodiments can be referred to each other. Each of the embodiments mainly describes the difference from other embodiments. In particular, the computer readable storage medium embodiment is basically similar to the code detection method or the loop statement code detection method embodiment, and thus the description is relatively simple, and the relevant parts can be referred to the description of the code detection method or the loop statement code detection method embodiment.

[0292] An embodiment of the specification further provides a computer program product including computer programs / instructions. The computer programs / instructions, when executed by a processor, implement the steps of the code detection method or the loop statement code detection method.

[0293] The above is a schematic scheme of a computer program product of the embodiment. It should be noted that the technical scheme of the computer program product and the technical scheme of the code detection method or the loop statement code detection method described above belong to the same concept, and the technical scheme of the computer program product is not described in detail. The contents of the description can be seen from the description of the technical scheme of the code detection method or the loop statement code detection method.

[0294] The above describes specific embodiments of the present specification. Other embodiments are within the scope of the appended claims. In some cases, the actions or steps recited in the claims can be performed in an order different than the order in which they are recited in the embodiments and still achieve desirable results. In addition, the processes depicted in the accompanying drawings do not necessarily require the particular order shown or sequential order to achieve the desired results. In certain implementations, multitasking and parallel processing can be advantageous or possible.

[0295] The computer instructions include computer program code, which can be in the form of source code, object code, executable code, or some intermediate form. The computer-readable medium can include any entity or device capable of carrying the computer program code, recording medium, U disk, mobile hard disk, magnetic disk, optical disk, computer memory, read-only memory (ROM, Read-Only Memory), random access memory (RAM, Random Access Memory), electrical carrier signal, telecommunication signal, and software distribution medium, etc. It should be noted that the contents contained in the computer-readable medium can be appropriately increased or decreased according to the requirements of patent practice, for example, in some regions, according to the patent practice, the computer-readable medium does not include electrical carrier signals and telecommunication signals.

[0296] It should be noted that for the foregoing method embodiments, in order to facilitate description, they are all expressed as a combination of a series of actions, but those skilled in the art should know that the embodiments of the present specification are not limited by the order of the described actions, because according to the embodiments of the present specification, certain steps can be performed in other orders or simultaneously. Secondly, those skilled in the art should know that the embodiments described in the specification are all preferred embodiments, and the actions and modules involved are not necessarily all necessary for the embodiments of the present specification.

[0297] In the above embodiments, the description of each embodiment has its own emphasis, and the parts not described in detail in a certain embodiment can be seen from the related description of other embodiments.

[0298] The preferred embodiments of the present specification disclosed above are only used to help explain the present specification. Alternative embodiments do not describe all the details and limit the present application to the specific embodiments described. Obviously, many modifications and variations of the embodiments of the present specification can be made in light of the content of the present specification. The present specification selects and specifically describes these embodiments in order to better explain the principles and practical applications of the embodiments of the present specification, so that those skilled in the art can well understand and utilize the present specification. The present specification is limited only by the claims and their full scope and equivalents.

Claims

1. A code detection method, comprising: The code to be detected is determined, and the code to be detected is subjected to syntax analysis to obtain intermediate code corresponding to the code to be detected, wherein the intermediate code contains the syntax information of the code to be detected; The intermediate code is matched with the code templates in the code template set to obtain the target code template that matches the intermediate code; Determine the code execution conditions corresponding to the target code template, and perform code execution detection on the intermediate code according to the code execution conditions to obtain code detection results, wherein the code execution conditions are used to limit the code execution process of the intermediate code.

2. The code detection method according to claim 1, wherein performing syntax analysis on the code to be detected to obtain intermediate code corresponding to the code to be detected includes: Perform syntax recognition on the code to be detected to obtain the syntax tree corresponding to the code to be detected; Determine at least two initial syntax nodes in the syntax tree, and the syntactic relationships between the at least two initial syntax nodes; Based on the at least two initial syntax nodes and the syntax relationship between the at least two initial syntax nodes, intermediate code corresponding to the code to be detected is obtained.

3. The code detection method according to claim 2, wherein performing syntax recognition on the code to be detected to obtain the syntax tree corresponding to the code to be detected includes: Lexical recognition is performed on the code to be detected to obtain multiple word segments corresponding to the code to be detected; Based on the multiple word segments and the syntactic relationships between them, a syntax tree corresponding to the code to be detected is established.

4. The code detection method according to claim 2, wherein obtaining intermediate code corresponding to the code to be detected based on the at least two initial syntax nodes and the syntactic relationship between the at least two initial syntax nodes includes: From the at least two initial syntax nodes, determine at least two target syntax nodes that correspond to the preset target statement type; Based on the grammatical relationship between the at least two initial grammatical nodes, determine the grammatical relationship between the at least two target grammatical nodes; Based on the at least two target syntax nodes and the syntax relationship between the at least two target syntax nodes, intermediate code corresponding to the code to be detected is obtained.

5. The code detection method according to claim 1, wherein matching the intermediate code with code templates in the code template set to obtain a target code template matching the intermediate code comprises: The code execution logic contained in the intermediate code is matched with the code execution logic contained in each code template in the code template set to obtain a target code template that matches the intermediate code.

6. The code detection method according to claim 5, wherein the code to be detected includes loop statement code, the intermediate code includes loop statement intermediate code, and the code execution logic includes loop type, loop condition, and loop body; The step of matching the code execution logic contained in the intermediate code with the code execution logic contained in each code template in the code template set to obtain a target code template that matches the intermediate code includes: The loop type, loop condition, and loop body contained in the intermediate code of the loop statement are matched with the loop type template, loop condition template, and loop body template contained in each code template in the code template set, respectively, to obtain a target code template that matches the intermediate code of the loop statement. The loop type contained in the intermediate code of the loop statement is the same as the loop type template of the target code template, the loop condition template contained in the intermediate code of the loop statement has the same loop logic as the loop condition template contained in the target code template, and the loop body contained in the intermediate code of the loop statement has the same execution logic as the loop body template contained in the target code template.

7. The code detection method according to claim 1 or 5, wherein the step of performing code execution detection on the intermediate code based on the code execution conditions to obtain a code detection result includes: Based on the code execution conditions, determine the code execution data corresponding to the code execution conditions from the intermediate code; From the code execution conditions, determine the data execution rules corresponding to the code execution data; Based on the data execution rules corresponding to the execution data, security checks are performed on the code execution data to obtain a code detection result indicating whether the code to be detected is safe code.

8. The code detection method according to claim 7, wherein the step of performing security detection on the code execution data according to the data execution rules corresponding to the execution data to obtain a code detection result indicating whether the code to be detected is safe code includes: The code execution data is subjected to security checks based on the data execution rules corresponding to the code execution data. If the code execution data satisfies the data execution rules, a code detection result is obtained that the code to be detected is safe code; or If it is determined that the code execution data does not meet the data execution rules, a code detection result is obtained that the code to be detected is unsafe code.

9. The code detection method according to claim 8, further comprising, after obtaining the code detection result that the code to be detected is unsafe code: Based on the correlation between the data execution rules and security risk information, the security risk information corresponding to the code to be detected is determined, wherein the security risk information includes the security issues existing in the code to be detected.

10. The code detection method according to claim 1, further comprising, before determining the code execution conditions corresponding to the target code template: Determine the code execution logic contained in each code template in the code template set; Based on the code execution logic contained in each code template, determine the code execution conditions corresponding to each code template; Establish the association between each code template and the code execution conditions.

11. A method for detecting loop statement code, comprising: The code of the loop statement to be detected is determined, and the code of the loop statement to be detected is subjected to syntax analysis to obtain the intermediate code of the loop statement corresponding to the code of the loop statement to be detected, wherein the intermediate code contains the syntax information of the code of the loop statement to be detected; The intermediate code of the loop statement is matched with the loop statement code templates in the code template set to obtain the target loop statement code template that matches the intermediate code of the loop statement; Determine the execution conditions of the loop statement code corresponding to the target loop statement code template, and perform code execution detection on the intermediate code of the loop statement according to the execution conditions of the loop statement code to obtain the loop statement code detection result. The execution conditions of the loop statement code are used to limit the code execution process of the intermediate code of the loop statement.

12. A code detection device, comprising: The intermediate code acquisition module is configured to determine the code to be detected, perform syntax analysis on the code to be detected, and obtain intermediate code corresponding to the code to be detected, wherein the intermediate code contains the syntax information of the code to be detected; The target code template obtaining module is configured to match the intermediate code with code templates in the code template set to obtain a target code template that matches the intermediate code; The code detection module is configured to determine the code execution conditions corresponding to the target code template, and to perform code execution detection on the intermediate code according to the code execution conditions to obtain code detection results, wherein the code execution conditions are used to limit the code execution process of the intermediate code.

13. A loop statement code detection device, comprising: The intermediate code acquisition module for loop statements is configured to determine the loop statement code to be detected, and to perform syntax analysis on the loop statement code to be detected to obtain the intermediate code of the loop statement corresponding to the loop statement code to be detected, wherein the intermediate code contains the syntax information of the loop statement code to be detected; The target loop statement code template obtaining module is configured to match the intermediate code of the loop statement with the loop statement code templates in the code template set to obtain the target loop statement code template that matches the intermediate code of the loop statement; The loop statement code detection module is configured to determine the loop statement code execution conditions corresponding to the target loop statement code template, and to perform code execution detection on the intermediate code of the loop statement according to the loop statement code execution conditions to obtain the loop statement code detection result. The loop statement code execution conditions are used to limit the code execution process of the intermediate code of the loop statement.

14. A computing device, comprising: Memory and processor; The memory is used to store computer programs / instructions, and the processor is used to execute the computer programs / instructions. When the computer programs / instructions are executed by the processor, they implement the steps of the code detection method according to any one of claims 1 to 10 or the loop statement code detection method according to claim 11.

15. A computer-readable storage medium storing a computer program / instructions that, when executed by a processor, implement the steps of the code detection method of any one of claims 1 to 10 or the loop statement code detection method of claim 11.

16. A computer program product comprising a computer program / instructions that, when executed by a processor, implement the steps of the code detection method of any one of claims 1 to 10 or the loop statement code detection method of claim 11.