Source code scanning method, device, electronic device and storage medium
By performing equivalent conversion processing and abstract syntax tree construction on the target function in the source code, defect scanning is performed based on preset rules, which solves the problem of low efficiency of source code scanning in the existing technology and achieves more efficient and accurate defect detection. .
Patent Information
- Application Number
- CN202110113984.4
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2021-01-27
- Publication Date
- 2024-09-24
- Estimated Expiration
- 2041-01-27
AI Technical Summary
Existing static source code scanning methods are inefficient, prone to false negatives, and difficult to effectively detect defects and vulnerabilities in the source code.
By performing equivalent conversion processing on the target function in the source code, a conversion function that is easy to scan is generated, an abstract syntax tree is constructed, and scanning is performed based on the preset defect detection rules to detect defects related to the target function.
It improves the efficiency of source code scanning, can more accurately detect defects and vulnerabilities in source code, and improves detection efficiency and accuracy.
Smart Images

Figure CN112733153B
Abstract
Description
Technical Field
[0001] The present invention relates to the field of computers, and in particular to a source code scanning method, device, electronic equipment and storage medium. Background Art
[0002] Static Code Analysis is one of the software application security solutions that has been mentioned frequently in recent years. It means that in software engineering, the source code does not need to be compiled by a compiler, but is directly scanned by some scanning tools to find some defects and vulnerabilities in it, thereby helping developers to quickly find, locate, repair and manage security risks in the source code.
[0003] However, current static source code scanning methods often have problems such as underreporting, and therefore, current source code scanning methods are inefficient. Summary of the invention
[0004] Embodiments of the present invention provide a source code scanning method, device, electronic device and storage medium, which can improve the efficiency of source code scanning.
[0005] An embodiment of the present invention provides a source code scanning method, comprising:
[0006] Get the source code, which includes the target function;
[0007] Performing equivalent conversion processing on the objective function to obtain at least one conversion function corresponding to the objective function;
[0008] Using the conversion function to replace the target function in the source code to obtain the replacement code;
[0009] Construct an abstract syntax tree based on the alternative code;
[0010] Based on the preset defect detection rules, the abstract syntax tree is scanned for defects to obtain the scanning results of defects in the source code.
[0011] The embodiment of the present invention further provides a source code scanning device, comprising:
[0012] An acquisition unit, used for acquiring source code, the source code including a target function;
[0013] A conversion unit, used to perform equivalent conversion processing on the objective function to obtain at least one conversion function corresponding to the objective function;
[0014] A substitution unit, used for performing substitution processing on a target function in a source code by using a conversion function to obtain a substitution code;
[0015] A construction unit, used to construct an abstract syntax tree based on the alternative code;
[0016] The scanning unit is used to perform defect scanning on the abstract syntax tree based on preset defect detection rules to obtain scanning results of defects in the source code.
[0017] In some embodiments, a construction unit is used to:
[0018] Determine the array object that calls the conversion function in the replacement code, as well as the function parameters of the conversion function;
[0019] When the conversion function is a memory allocation function, the return value of the conversion function is determined based on the function parameters;
[0020] The return value of the conversion function is defined in the replacement code, and the array object that calls the conversion function is modified to the array object pointing to the return value of the target function, thereby obtaining another replacement code;
[0021] Construct an abstract syntax tree based on the alternative code again.
[0022] In some embodiments, the source code includes a first function, and the construction unit, after being used to construct an abstract syntax tree based on the replacement code, is further used to:
[0023] Determine a function body of the first function and external variable parameters in the function body;
[0024] Determine the position of the function body of the first function in the abstract syntax tree;
[0025] Store the location in a call table.
[0026] In some embodiments, the source code includes a second function that calls the first function, and a scanning unit that is configured to:
[0027] When the second function is scanned in the abstract syntax tree, determining a next scanning position in the abstract syntax tree based on the call relationship table;
[0028] Determining a function body of the first function and external variable parameters in the function body based on the next scanning position;
[0029] Based on the preset defect detection rules, the abstract syntax tree is scanned for defects using the function body and the external variable parameters in the function body to obtain the scanning results of defects in the source code.
[0030] In some embodiments, the abstract syntax tree includes a loop structure, a scanning unit, including:
[0031] The local subunit is used to determine the current value of the local variable when scanning the local variable in the loop structure;
[0032] The scanning subunit is used to perform a defect scan on the local variables in the abstract syntax tree based on the preset defect detection rules and the current fixed values of the local variables, and obtain the scanning results of the defects of the local variables.
[0033] In some embodiments, the local subunit is further configured to:
[0034] A first data structure defining local variables;
[0035] In some embodiments, the local subunit is used to:
[0036] When scanning the local variable in the loop structure, the last certain value of the local variable is obtained from the first data structure;
[0037] Based on the last set value, calculate the current set value of the local variable;
[0038] Storing the current fixed value of the local variable into the first data structure of the local variable;
[0039] Rescan loop structures.
[0040] In some embodiments, the local subunit is used to rescan the loop structure and also to:
[0041] Determine the loop type and loop condition threshold of the loop structure;
[0042] When the loop type is the target loop type, determine the change type of the local variable;
[0043] When the change type of the local variable is monotonic, and the current value of the local variable is greater than the loop condition threshold, the loop structure is exited; or,
[0044] When the change type of the local variable is a monotonic change type and the loop condition threshold is greater than the preset threshold, the loop structure is exited.
[0045] In some embodiments, the conversion unit is configured to:
[0046] When the target function is an unknown library function, obtaining a preset unknown library function mapping relationship;
[0047] Based on the mapping relationship of the unknown library function, a conversion function having a mapping relationship with the unknown library function is determined.
[0048] In some embodiments, the conversion unit is configured to:
[0049] When the target function is a target library function, obtaining a preset target library function mapping relationship;
[0050] Based on the target library function mapping relationship, a conversion function set having a mapping relationship with the target library function is determined, and the conversion function set includes at least one conversion function.
[0051] In some embodiments, the scanning unit includes:
[0052] An assignment subunit, for determining an assignment object and an assignment operation in the assignment structure when an assignment structure in the abstract syntax tree is scanned;
[0053] A detection value subunit, used to determine the detection value of the assigned object when the value of the assigned object is unknown;
[0054] The structural defect subunit is used to determine defects in the assigned structure based on the detection value of the assigned object and the assigned operation.
[0055] In some embodiments, the detection value subunit is used to:
[0056] Determine the data type of the assignment object, which includes signed types and unsigned types;
[0057] When the data type of the assigned object is a signed type, a preset positive boundary value, a negative boundary value, a positive middle value, a negative middle value and a zero value are determined as the detection value of the assigned object;
[0058] When the data type of the assigned object is an unsigned type, a preset boundary value, an intermediate value, and a zero value are determined as the detection value of the assigned object.
[0059] In some embodiments, the assignment object includes a first assignment object and a second assignment object, and a structural defect subunit, which is used to:
[0060] Determine a value range corresponding to the data type of the first object;
[0061] When the detection value of the second object does not belong to the value range corresponding to the data type of the first object, it is determined that a numerical overflow defect exists in the assignment structure.
[0062] An embodiment of the present invention further provides an electronic device, comprising a memory storing a plurality of instructions; the processor loads instructions from the memory to execute the steps in any one of the source code scanning methods provided in the embodiments of the present invention.
[0063] An embodiment of the present invention further provides a computer-readable storage medium, wherein the computer-readable storage medium stores a plurality of instructions, wherein the instructions are suitable for being loaded by a processor to execute the steps in any one of the source code scanning methods provided in the embodiments of the present invention.
[0064] An embodiment of the present invention can obtain source code, where the source code includes a target function; perform equivalent conversion processing on the target function to obtain at least one conversion function corresponding to the target function; use the conversion function to replace the target function in the source code to obtain a replacement code; construct an abstract syntax tree based on the replacement code; and perform defect scanning on the abstract syntax tree based on preset defect detection rules to obtain a scan result of defects in the source code.
[0065] In the present invention, by replacing the unknown or difficult-to-process target function in the source code with a known, easy-to-scan conversion function that is equivalent to the target function, the source code logic is simplified and merged when constructing the abstract syntax tree, so that when scanning for defects, defects related to the target function can be detected. Therefore, this solution can improve the efficiency of source code scanning. BRIEF DESCRIPTION OF THE DRAWINGS
[0066] In order to more clearly illustrate the technical solutions in the embodiments of the present invention, the drawings required for use in the description of the embodiments will be briefly introduced below. Obviously, the drawings described below are only some embodiments of the present invention. For those skilled in the art, other drawings can be obtained based on these drawings without creative work.
[0067] Figure 1a Schematic diagram of a cloud service scenario of a source code scanning method provided by an embodiment of the present invention;
[0068] Figure 1b It is a flowchart of a source code scanning method provided by an embodiment of the present invention;
[0069] Figure 1c is a schematic diagram of a flow chart of equivalent conversion processing provided by an embodiment of the present invention;
[0070] Figure 1d is a schematic diagram of an abstract syntax tree provided by an embodiment of the present invention;
[0071] Figure 1e is a schematic diagram of an example of an abstract syntax tree provided by an embodiment of the present invention;
[0072] Figure 2 It is a flowchart of a source code scanning method provided by an embodiment of the present invention;
[0073] Figure 3 is a schematic diagram of the structure of a source code scanning device provided by an embodiment of the present invention;
[0074] Figure 4 It is a schematic diagram of the structure of an electronic device provided by an embodiment of the present invention. DETAILED DESCRIPTION
[0075] The following will be combined with the drawings in the embodiments of the present invention to clearly and completely describe the technical solutions in the embodiments of the present invention. Obviously, the described embodiments are only part of the embodiments of the present invention, not all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by those skilled in the art without creative work are within the scope of protection of the present invention.
[0076] Embodiments of the present invention provide a source code scanning method, device, electronic device and storage medium.
[0077] The source code scanning device can be integrated into an electronic device, which can be a terminal, a server, etc. The terminal can be a mobile phone, a tablet computer, a smart Bluetooth device, a notebook computer, a vehicle terminal, or a personal computer (PC); the server can be a single server or a server cluster composed of multiple servers.
[0078] In some embodiments, the source code scanning device may also be integrated into multiple electronic devices. For example, the source code scanning device may be integrated into multiple servers, and the source code scanning method of the present invention may be implemented by multiple servers.
[0079] Among them, the server can be an independent physical server, or a server cluster or distributed system composed of multiple physical servers. It can also be a cloud server that provides basic cloud computing services such as cloud services, cloud databases, cloud computing, cloud functions, cloud storage, network services, cloud communications, middleware services, domain name services, security services, CDN, and big data and artificial intelligence platforms. It can also be a dedicated or platform server that provides Internet of Vehicles services, road network collaboration, vehicle-road collaboration, intelligent transportation, automatic driving, industrial Internet services, data communications (such as 4G, 5G, etc.). The terminal can be a smart phone, a tablet computer, a laptop computer, a desktop computer, a smart speaker, a smart watch, a car terminal, etc., but is not limited to this. The terminal and the server can be directly or indirectly connected via wired or wireless communication, and this application is not limited here.
[0080] Among them, cloud computing is a computing model that distributes computing tasks on a resource pool composed of a large number of computers, so that various application systems can obtain computing power, storage space and information services as needed. The network that provides resources is called "cloud". The resources in the "cloud" are infinitely expandable in the eyes of users, and can be obtained at any time, used on demand, expanded at any time, and paid for by use.
[0081] As a provider of basic cloud computing capabilities, a cloud computing resource pool (referred to as a cloud platform, generally referred to as an IaaS (Infrastructure as a Service) platform) will be established, and various types of virtual resources will be deployed in the resource pool for external customers to choose to use. The cloud computing resource pool mainly includes: computing devices (virtualized machines, including operating systems), storage devices, and network devices.
[0082] According to the logical function division, the PaaS (Platform as a Service) layer can be deployed on the IaaS (Infrastructure as a Service) layer, and the SaaS (Software as a Service) layer can be deployed on the PaaS layer. SaaS can also be deployed directly on IaaS. PaaS is a platform for software operation, such as databases, web containers, etc. SaaS is a variety of business software, such as web portals, SMS mass senders, etc. Generally speaking, SaaS and PaaS are upper layers relative to IaaS.
[0083] For example, refer to Figure 1a The electronic device may be a cloud server. A user may upload a source code containing a target function to the cloud server using a personal computer. The cloud server may perform an equivalent conversion process on the target function to obtain at least one conversion function corresponding to the target function; use the conversion function to replace the target function in the source code to obtain a replacement code; construct an abstract syntax tree based on the replacement code; perform a defect scan on the abstract syntax tree based on a preset defect detection rule to obtain a scan result of defects in the source code, and then return the scan result to the user's personal computer for display, and so on.
[0084] It should be noted that the serial numbers of the following embodiments are not intended to limit the preferred order of the embodiments.
[0085] In this embodiment, a source code scanning method running in a cloud server is provided. Figure 1b As shown, the specific process of the source code scanning method can be as follows:
[0086] 101. Obtain source code, which includes the target function.
[0087] Source code refers to an uncompiled text file written in accordance with certain programming language specifications. It is a series of human-readable computer language instructions.
[0088] The source code may include a target function, which is a function that meets any of the following conditions:
[0089] (1) A known library function in the code base;
[0090] (2) Functions that are not defined in the code base, but whose function effects are very similar to known library functions in the code base;
[0091] (3) A function that has no definition in the code base but is defined by a macro as a combination of one or more library functions in the code base.
[0092] For example, a function that has no definition in the code base but whose semantics are very close to known library functions in the code base can be the g_malloc() function (a function used to allocate memory) in the glib library (a C language function library commonly used under the Linux platform). The g_malloc() function meets condition (2), that is, the g_mallo(size) function is not defined in the code base, but its effect is almost the same as the library function calloc(size, 1) in the code base.
[0093] For example, a function that has no definition in the code base but is defined by a macro as a combination of one or more library functions in the code base may be the realloc() function (a function used to release memory before applying for memory). The realloc() function meets condition (3), that is, the realloc(mem, size) function has no definition in the code base but can be defined by a macro as a combination of the free(mem) function (a function used to release memory) and the malloc(size) library function in the code base.
[0094] 102. Perform equivalent conversion processing on the objective function to obtain at least one conversion function corresponding to the objective function.
[0095] In this embodiment, tokens can be extracted from source code and an abstract syntax tree can be constructed. During the construction, some preprocessing can be performed on the tokens to simplify the abstract syntax tree that is finally constructed.
[0096] In this solution, the attributes of each token can be confirmed, such as the type, implicit value, and relevance of the token.
[0097] When a function-type token is being processed, if the token represents a target function, all target functions corresponding to the token in the source file can be merged into one or more known library functions, thereby achieving the effect of streamlining the abstract syntax tree.
[0098] In some embodiments, reference Figure 1c , step 102 may include the following steps:
[0099] When the target function is an unknown library function, obtaining a preset unknown library function mapping relationship;
[0100] Based on the mapping relationship of the unknown library function, a conversion function having a mapping relationship with the unknown library function is determined.
[0101] The preset unknown library function mapping relationship may include a mapping relationship between a target function X and a function Y whose function effect is very close to that of the target function X.
[0102] For example, the preset unknown library function mapping relationship may include a mapping relationship between g_mallo(size) and calloc(size, 1).
[0103] In some embodiments, the step of “performing equivalent conversion processing on the target function to obtain at least one conversion function” may include the following steps:
[0104] When the target function is a target library function, obtaining a preset target library function mapping relationship;
[0105] Based on the target library function mapping relationship, a conversion function set having a mapping relationship with the target library function is determined, and the conversion function set may include at least one conversion function.
[0106] The preset target library function mapping relationship may include a mapping relationship between a target function X and a function y1+y2 defined by a macro for the target function X.
[0107] For example, the preset target library function mapping relationship may include a mapping relationship between realloc(mem, size) and free(mem); malloc(size).
[0108] 103. Use a conversion function to replace the target function in the source code to obtain a replacement code.
[0109] For example, based on the mapping relationship between g_mallo(size) and calloc(size, 1), g_mallo(size) in the source code is replaced with calloc(size, 1) to obtain a replacement code.
[0110] For example, based on the mapping relationship between realloc(mem, size) and free(mem); malloc(size), realloc(mem, size) in the source code is replaced with free(mem); malloc(size) to obtain the replacement code.
[0111] 104. Construct an abstract syntax tree based on the alternative code.
[0112] Among them, the Abstract Syntax Tree (AST) is a tree representation of the abstract syntax in the replacement code, which is composed of tokens. Each node on the tree represents a structure in the source code.
[0113] For example, the code shown below can be used as Figure 1d The abstract syntax tree representation of :
[0114]
[0115] in, Figure 1d The abstract syntax tree in includes the root node statement squence representing the code, as well as the loop structure while and the return structure return. The loop structure while includes the conditional structure compare≠ and the branch structure branch; the branch structure branch includes the conditional structure compare> and the if structure and the else structure. The if structure and the else structure both include the subtraction operation structure -, and so on.
[0116] In this solution, the bounds of the array can be checked.
[0117] Before constructing the abstract syntax tree, the source code must first be lexically analyzed.
[0118] In some embodiments, the method of lexical analysis may be to convert a character sequence of a source code into a token sequence. Hereinafter, a program or function for lexical analysis may be referred to as a lexical analyzer (Lexer) or a scanner.
[0119] Token is a computer term, and the process of converting a character stream into a Token is called Tokenization. In some embodiments, the lexical analyzer can also classify the tokens during this process.
[0120] In some embodiments, the lexical analyzer can derive an abstract syntax tree based on incomplete semantics, thereby greatly increasing the compatibility during static scanning.
[0121] For example, it is possible to detect whether the access to the array subscript is out of bounds. For example, define an array A with a dimension of 7 (subscript 0, subscript 1, subscript 2..., subscript 6), where the array subscript represents the dimension of the array, the array lower bound is the smallest subscript, which is 0 by default, and the array upper bound is the largest subscript, which is 6 in array A. When array A[subscript 7] appears in the source code, since subscript 7 exceeds its upper bound, the array A[subscript 7] has an out-of-bounds defect.
[0122] For another example, refer to the following code:
[0123]
[0124] In the fifth line of the code, p[3] will be accessed, but p[3] exceeds the upper bound of the array p[2], so p[3] has an out-of-bounds defect.
[0125] In this embodiment, in addition to determining whether an array out-of-bounds defect occurs based on a known value, it is also possible to determine whether an array out-of-bounds defect occurs when the value is unknown.
[0126] For example, when an array is dynamically allocated, in order to prevent the scanning of the dynamically allocated array for out-of-bounds defects from being stopped due to the inability to obtain a known value, in some embodiments, reference Figure 1e , step 104 may include the following steps:
[0127] Determine the array object that calls the conversion function in the replacement code, as well as the function parameters of the conversion function;
[0128] When the conversion function is a memory allocation function, the return value of the conversion function is determined based on the function parameters;
[0129] The return value of the conversion function is defined in the replacement code, and the array object that calls the conversion function is modified to the array object pointing to the return value of the target function, thereby obtaining another replacement code;
[0130] An abstract syntax tree is constructed based on the replacement code again.
[0131] For example, the return value of the conversion function calloc() in the replacement code can be defined separately as an array of int (integer) type and length [size], as the content pointed to by the value of the array int a* that calls calloc(), so as to correctly calculate the upper and lower bounds of the array and determine the array out-of-bounds defects in subsequent scans.
[0132] For example, replace the following code:
[0133] int*a=calloc(3,sizeof(int));
[0134] Modified to:
[0135] int*_temp=calloc(3,sizeof(int));
[0136] int* a = _temp;
[0137] Thereby obtaining a new replacement code, and finally constructing an abstract syntax tree based on the new replacement code.
[0138] In this embodiment, in addition to determining whether an array out-of-bounds defect occurs based on a known value, it is also possible to determine whether an array out-of-bounds defect occurs when the value is unknown.
[0139] For example, when performing defect detection based on a function body, it may be impossible to know the external variable parameters called in the function body, and the function body may often be skipped for detection.
[0140] For example, consider the following code:
[0141] void foo(int index){
[0142] return a[index];
[0143] }
[0144] The curly braces contain the function body, and the curly braces contain the function description. That is, in this case, only {return a[index];} will be checked, and in the function body, index is an unknown external variable parameter. Therefore, in this case, the unknown index will be skipped, resulting in missed detection.
[0145] In order to solve this problem and prevent the failure to detect array out-of-bounds defects when the array subscript is an externally input variable, in some embodiments, after step 104, the following steps may be further included:
[0146] Determine a function body of the first function and external variable parameters in the function body;
[0147] Determine the position of the function body of the first function in the abstract syntax tree;
[0148] Store the location in a call table.
[0149] The source code may include a first function, the first function having a function description and a function body, and the first function refers to a function including external variable parameters in its function body as in the above example.
[0150] In the stage of establishing the abstract syntax tree, this solution generates a call relationship table for each function (for example, if three functions call foo(), the positions of these three functions are recorded and stored in the call relationship table). When the abstract syntax tree is subsequently backtracked, there is no need to re-perform complex abstract syntax tree traversal and region detection, but only needs to access the function body and its external variable parameters through the call relationship table. While reducing the complexity, the external variable parameters directly accessed can be regarded as known parameters, so that the external variable parameters can be regarded as a common local variable for subsequent deduction.
[0151] 105. Based on the preset defect detection rules, the abstract syntax tree is scanned for defects to obtain the scanning results of defects in the source code.
[0152] Among them, defects refer to errors that may cause security, stability, and recovery-related problems to the software, or errors that may produce unexpected results.
[0153] In some embodiments, the present solution uses embedded defect detection rules (ie, check classes) to implement defect detection in the lexical analysis phase, and then stores the error defect line number according to the value of the variable in the Token, thereby locating the defect.
[0154] Among them, this solution has many defect detection rules, so it can perform strict logical checks, such as automatic variable checks, array boundary checks, class checks, expired functions, abandoned function call checks, abnormal memory usage, release checks, memory leak checks, mainly through memory reference pointers, operating system resource release checks, interrupts, file descriptors, etc., abnormal STL function usage checks, code format errors, and performance factor checks, etc.
[0155] For example, in some embodiments, the source code may include the first function introduced in step 104, and may also include a second function that calls the first function. Therefore, after the external variable parameter is regarded as a common local variable, step 105 may include the following steps:
[0156] When the second function is scanned in the abstract syntax tree, determining a next scanning position in the abstract syntax tree based on the call relationship table;
[0157] Determining a function body of the first function and external variable parameters in the function body based on the next scanning position;
[0158] Based on the preset defect detection rules, the abstract syntax tree is scanned for defects using the function body and the external variable parameters in the function body to obtain the scanning results of defects in the source code.
[0159] In this embodiment, in addition to determining whether an array out-of-bounds defect occurs based on a known value, it is also possible to determine whether an array out-of-bounds defect occurs when the value is unknown.
[0160] For example, if the detection does not record the transfer of value streams, if the value stream transfer appears in the array subscript, the out-of-bounds detection of the array will often be skipped.
[0161] For example, the following code describes that x is increasing iteratively and cannot be calculated all at once, that is, the value stream x is transferred:
[0162] int x = 1;
[0163] a[x]=2;
[0164] x++;
[0165] a[x]=3;
[0166] x++;
[0167] a[x]=4;
[0168] Therefore, in order to solve the problem in the above situation where the out-of-bounds check of the array is skipped when the value stream is passed in the array subscript, in some embodiments, a first data structure of local variables can be established before scanning to store the local variables of the value stream, and the content of this first data structure can be continuously updated as the number of scan lines increases.
[0169] The first data structure can be used as a value transfer container, that is, the data structure can be used to transfer values.
[0170] In some embodiments, the abstract syntax tree may include a loop structure (i.e., a loop structure in the source code), and the transfer of value streams often occurs in the loop structure. Therefore, step 105 may include the following steps:
[0171] When scanning local variables in a loop structure, determine the current value of the local variables;
[0172] Based on the preset defect detection rules and the current fixed values of the local variables, a defect scan is performed on the local variables in the abstract syntax tree to obtain the scan results of the defects of the local variables.
[0173] Due to the particularity of the loop statement, when the loop is conventionally solved, the processing of the loop may be abandoned. In this embodiment, in order to reduce the problem of missing checks in this case, the local variables in the loop can be abstractly solved approximately.
[0174] Therefore, in some embodiments, before the step of “determining the current fixed value of the local variable when scanning the local variable in the loop structure”, the following steps may also be included:
[0175] Define the first data structure of local variables.
[0176] For example, define a two-tuple vector vector<local variable N, current value> of a local variable N as a value transfer container.
[0177] Then, in the step "When scanning the local variables in the loop structure, determine the current set value of the local variables", perform the following steps:
[0178] When scanning the local variable in the loop structure, the last certain value of the local variable is obtained from the first data structure;
[0179] Based on the last set value, calculate the current set value of the local variable;
[0180] Storing the current fixed value of the local variable into the first data structure of the local variable;
[0181] Rescan loop structures.
[0182] For example, when the loop is known and has a finite number of exits, such as for(int x=0; x<16; x++), it is necessary to check the local variable x, that is, each time the current value of the local variable x is calculated, the two-tuple vector vector<local variable x, current value> is updated.
[0183] In some embodiments, when the loop condition of the loop is unknown and the loop condition is related to the local variables of the value stream, it is necessary to determine whether to stop scanning the loop structure according to the change type of the local variables, so the step of "re-scanning the loop structure" may also include the following steps:
[0184] Determine the loop type and loop condition threshold of the loop structure;
[0185] When the loop type is the target loop type, determine the change type of the local variable;
[0186] When the change type of the local variable is monotonic, and the current value of the local variable is greater than the loop condition threshold, the loop structure is exited; or,
[0187] When the change type of the local variable is a monotonic change type and the loop condition threshold is greater than the preset threshold, the loop structure is exited.
[0188] The target loop type refers to a loop whose exit condition is unknown; the change type includes monotonic change (such as monotonically increasing change and monotonically decreasing change) and non-monotonic change.
[0189] When the loop is known but the exit condition of the loop is unknown, such as while(…){…break;}, and the loop condition is related to the local variable to be detected, for example, detecting x, and the loop condition is if(x == 20)break; it is necessary to determine whether the local variable changes monotonically. If it does, it is detected that the local variable is greater than the loop condition threshold, or the loop condition threshold is greater than the preset threshold.
[0190] For example, consider the following code:
[0191]
[0192]
[0193] The above code includes a while type loop, which determines that the change type of the local variable x is a single-increment change. When the current constant value of the local variable x is 21, which is greater than the loop condition threshold 20, the loop structure is exited and the two-tuple vector is updated to vector<local variable x, 21> and no longer modified.
[0194] For another example, assuming the preset threshold is set to 500, refer to the following code:
[0195]
[0196] The above code includes an if-type loop, which determines that the change type of the local variable x is a single-increment change. However, since the loop condition threshold 9999999999 is too large, far exceeding the preset threshold 500, the loop structure is directly exited, and the two-tuple vector is updated to vector<local variable x, 9999999999>, and then no longer modified.
[0197] This solution is especially targeted at situations where loops are used extensively in source code. By simulating loop evaluation, the defect detection rate in the source code can be greatly increased.
[0198] In addition, for the assignment structure:
[0199] void foo(unsigned int p){
[0200] unsigned char o = p;
[0201] }
[0202] The value range of the assigned object o is 0 to 255. If the value of the assigned object p is greater than 255, it will definitely cause an integer overflow problem in the assigned object o. Integer overflow is one of the most common factors that lead to security problems in modern codes. Since the value of the assigned object is unknown, the defect detection of the assigned object with unknown value may be skipped.
[0203] Therefore, in order to solve the above problem, in some embodiments, the value of the assignment object is often unknown during scanning. In this case, integer overflow often occurs when performing assignment calculation. Therefore, step 105 may include the following steps:
[0204] When the assignment structure in the abstract syntax tree is scanned, the assignment object and the assignment operation in the assignment structure are determined;
[0205] When the value of the assigned object is unknown, determining the test value of the assigned object;
[0206] Defects in the assignment structure are determined based on the detected values of the assignment objects and the assignment operations.
[0207] For example, this embodiment can detect that when the variable width of the assignment object changes, a value truncation defect may occur when the value of the assignment object with a larger value range (such as long type, occupying 4 bytes) is assigned to the value of the assignment object with a smaller value range (such as char type, occupying 1 byte).
[0208] For example, this embodiment can detect that when the sign type of the assignment object changes, the assignment of a signed type assignment object to an unsigned type assignment object may cause the value of the unsigned type assignment object to become too large.
[0209] For example, the following code:
[0210] unsigned long index = 0;
[0211] signed char x = -1;
[0212] index = x;
[0213] At this time, the value of index will become 4294967295 (0xFFFFFFFF), which is a defect that the developer did not expect.
[0214] For example, this embodiment can detect a defect in which a char type value is accumulated with a long type value, resulting in an integer overflow of the char type value.
[0215] In some embodiments, when the value of the valued object is unknown, it can be estimated by using edge values, where edge values can include boundary values, intermediate values, etc. Therefore, the step of "when the value of the valued object is unknown, determining the detection value of the valued object" can include the following steps:
[0216] Determine the data type of the assignment object, which can include signed types and unsigned types;
[0217] When the data type of the assigned object is a signed type, a preset positive boundary value, a negative boundary value, a positive middle value, a negative middle value and a zero value are determined as the detection value of the assigned object;
[0218] When the data type of the assigned object is an unsigned type, a preset boundary value, an intermediate value, and a zero value are determined as the detection value of the assigned object.
[0219] For example, if the length of the unknown assignment object is N, the signed type uses the positive boundary value -2. N-1 and negative boundary value 2 N-1 -1, positive intermediate value 1, 0, negative intermediate value -1, unsigned number uses 0, intermediate value 1, boundary value 2 N-1 The possible range of edge value calculations is used. These edge values are sequentially calculated with the known values of the assignment object to determine whether integer overflow may occur.
[0220] For example, in some embodiments, the assignment object may include a first assignment object and a second assignment object. The step of "determining defects in the assignment structure based on the detection value and the assignment operation of the assignment object" may include the following steps:
[0221] Determine a value range corresponding to the data type of the first object;
[0222] When the detection value of the second object does not belong to the value range corresponding to the data type of the first object, it is determined that a numerical overflow defect exists in the assignment structure.
[0223] The source code scanning solution provided by the embodiment of the present invention can be applied in various code scanning scenarios.
[0224] For example, refer to Figure 2 ,This solution can first read the path of the detection project through the command line, and then determine whether the path is correct. When the path is correct, the ,folder traversal algorithm is used to traverse the source files under all ,folders, such as C++ source files (.ccp format files), C source files (.c format files), etc. Otherwise, the source code scanning is terminated directly.
[0225] If the file contains a header file (a file in .h format), the header file is read into the source file, and then the source file containing the header file is lexically analyzed. Otherwise, the source file is directly lexically analyzed to establish an abstract syntax tree consisting of Tokens, and semantic analysis is performed on the AST. Defect detection is achieved by cyclically calling the defect detection rules defined in the check subclass. Finally, defects in the program are located through ErrorLogger, so that similar defects in the software can be marked to achieve automatic detection of software defects.
[0226] In some embodiments, each time a .cpp / c file is traversed, the program read therefrom may be preprocessed to remove comment statements.
[0227] This solution can simplify the logic of the code to be detected when building AST, and when scanning defects, it can deduce the value range of uncertain variables through a new method. By performing logical judgment on the Tokens in the AST, it can determine whether there are security issues in the code, and can correctly obtain the deduced value, thereby improving detection efficiency.
[0228] When the applicant uses this solution to scan defects in the test program, the fault tolerance rate is greatly improved, and security issues hidden in the program code can be quickly discovered.
[0229] This solution will not ignore library functions when generating call relationships. When deducing suspicious values, it will also judge the feasibility of unknown values, and can effectively scan loop structures, optimizing the problem that open source projects cannot correctly obtain deduced values, thereby improving detection efficiency.
[0230] As can be seen from the above, an embodiment of the present invention can obtain source code, where the source code includes a target function; perform equivalent conversion processing on the target function to obtain at least one conversion function corresponding to the target function; use the conversion function to replace the target function in the source code to obtain a replacement code; construct an abstract syntax tree based on the replacement code; based on preset defect detection rules, perform defect scanning on the abstract syntax tree to obtain a scan result of defects in the source code.
[0231] Therefore, this solution can replace the unknown or difficult-to-handle target function in the source code with a known, easy-to-scan conversion function that is equivalent to the target function, so that when scanning for defects, defects related to the target function can be detected. Therefore, this solution can improve the efficiency of source code scanning.
[0232] In order to better implement the above method, an embodiment of the present invention further provides a source code scanning device, which can be integrated in an electronic device, and the electronic device can be a terminal, a server, etc. Among them, the terminal can be a mobile phone, a tablet computer, a smart Bluetooth device, a notebook computer, a personal computer, a vehicle-mounted device, etc.; the server can be a single server or a server cluster composed of multiple servers.
[0233] For example, in this embodiment, the method of the embodiment of the present invention is described in detail by taking the source code scanning device being specifically integrated into a server as an example.
[0234] For example, Figure 3 As shown, the source code scanning device may include an acquisition unit 301, a conversion unit 302, a substitution unit 303, a construction unit 304, and a scanning unit 305, as follows:
[0235] (i) Acquisition unit 301.
[0236] The acquisition unit 301 is used to acquire source code, where the source code includes a target function.
[0237] (ii) Conversion unit 302.
[0238] The conversion unit 302 is used to perform equivalent conversion processing on the objective function to obtain at least one conversion function corresponding to the objective function.
[0239] In some embodiments, the conversion unit 302 is configured to:
[0240] When the target function is an unknown library function, obtaining a preset unknown library function mapping relationship;
[0241] Based on the mapping relationship of the unknown library function, a conversion function having a mapping relationship with the unknown library function is determined.
[0242] In some embodiments, the conversion unit 302 is configured to:
[0243] When the target function is a target library function, obtaining a preset target library function mapping relationship;
[0244] Based on the target library function mapping relationship, a conversion function set having a mapping relationship with the target library function is determined, and the conversion function set includes at least one conversion function.
[0245] (iii) Alternative unit 303.
[0246] The replacement unit 303 is used to use the conversion function to replace the target function in the source code to obtain a replacement code.
[0247] (iv) Construction unit 304.
[0248] The construction unit 304 is used to construct an abstract syntax tree based on the replacement code.
[0249] In some embodiments, the construction unit 304 is used to:
[0250] Determine the array object that calls the conversion function in the replacement code, as well as the function parameters of the conversion function;
[0251] When the conversion function is a memory allocation function, the return value of the conversion function is determined based on the function parameters;
[0252] The return value of the conversion function is defined in the replacement code, and the array object that calls the conversion function is modified to the array object pointing to the return value of the target function, thereby obtaining another replacement code;
[0253] Construct an abstract syntax tree based on the alternative code again.
[0254] In some embodiments, the source code includes a first function, and the construction unit 304 is further configured to:
[0255] Determine a function body of the first function and external variable parameters in the function body;
[0256] Determine the position of the function body of the first function in the abstract syntax tree;
[0257] Store the location in a call table.
[0258] (V) Scanning unit 305.
[0259] The scanning unit 305 is used to perform defect scanning on the abstract syntax tree based on a preset defect detection rule to obtain a scanning result of defects in the source code.
[0260] In some embodiments, the source code includes a second function that calls the first function, and the scanning unit 305 is configured to:
[0261] When the second function is scanned in the abstract syntax tree, determining a next scanning position in the abstract syntax tree based on the call relationship table;
[0262] Determining a function body of the first function and external variable parameters in the function body based on the next scanning position;
[0263] Based on the preset defect detection rules, the abstract syntax tree is scanned for defects using the function body and the external variable parameters in the function body to obtain the scanning results of defects in the source code.
[0264] In some embodiments, the abstract syntax tree includes a loop structure, and the scanning unit 305 includes:
[0265] The local subunit is used to determine the current value of the local variable when scanning the local variable in the loop structure;
[0266] The scanning subunit is used to perform a defect scan on the local variables in the abstract syntax tree based on the preset defect detection rules and the current fixed values of the local variables, and obtain the scanning results of the defects of the local variables.
[0267] In some embodiments, the local subunit is further configured to:
[0268] A first data structure defining local variables;
[0269] In some embodiments, the local subunit is used to:
[0270] When scanning the local variable in the loop structure, the last certain value of the local variable is obtained from the first data structure;
[0271] Based on the last set value, calculate the current set value of the local variable;
[0272] Storing the current fixed value of the local variable into the first data structure of the local variable;
[0273] Rescan loop structures.
[0274] In some embodiments, the local subunit is used to rescan the loop structure and also to:
[0275] Determine the loop type and loop condition threshold of the loop structure;
[0276] When the loop type is the target loop type, determine the change type of the local variable;
[0277] When the change type of the local variable is monotonic, and the current value of the local variable is greater than the loop condition threshold, the loop structure is exited; or,
[0278] When the change type of the local variable is a monotonic change type and the loop condition threshold is greater than the preset threshold, the loop structure is exited.
[0279] In some embodiments, the scanning unit 305 includes:
[0280] An assignment subunit, for determining an assignment object and an assignment operation in the assignment structure when an assignment structure in the abstract syntax tree is scanned;
[0281] A detection value subunit, used to determine the detection value of the assigned object when the value of the assigned object is unknown;
[0282] The structural defect subunit is used to determine defects in the assigned structure based on the detection value of the assigned object and the assigned operation.
[0283] In some embodiments, the detection value subunit is used to:
[0284] Determine the data type of the assignment object, which includes signed types and unsigned types;
[0285] When the data type of the assigned object is a signed type, a preset positive boundary value, a negative boundary value, a positive middle value, a negative middle value and a zero value are determined as the detection value of the assigned object;
[0286] When the data type of the assigned object is an unsigned type, a preset boundary value, an intermediate value, and a zero value are determined as the detection value of the assigned object.
[0287] In some embodiments, the assignment object includes a first assignment object and a second assignment object, and a structural defect subunit, which is used to:
[0288] Determine a value range corresponding to the data type of the first object;
[0289] When the detection value of the second object does not belong to the value range corresponding to the data type of the first object, it is determined that a numerical overflow defect exists in the assignment structure.
[0290] In specific implementation, the above units can be implemented as independent entities, or can be arbitrarily combined to be implemented as the same or several entities. The specific implementation of the above units can refer to the previous method embodiments, which will not be repeated here.
[0291] As can be seen from the above, the source code scanning device of this embodiment acquires source code by an acquisition unit, and the source code includes a target function; the conversion unit performs equivalent conversion processing on the target function to obtain at least one conversion function corresponding to the target function; the replacement unit uses the conversion function to replace the target function in the source code to obtain a replacement code; the construction unit constructs an abstract syntax tree based on the replacement code; the scanning unit performs defect scanning on the abstract syntax tree based on preset defect detection rules to obtain the scanning results of defects in the source code.
[0292] Therefore, the embodiment of the present invention can improve the efficiency of source code scanning.
[0293] The embodiment of the present invention further provides an electronic device, which may be a terminal, a server, or the like. The terminal may be a mobile phone, a tablet computer, a smart Bluetooth device, a notebook computer, a personal computer, a vehicle-mounted device, or the like; the server may be a single server, or a server cluster composed of multiple servers, or the like.
[0294] In some embodiments, the source code scanning device may also be integrated into multiple electronic devices. For example, the source code scanning device may be integrated into multiple servers, and the source code scanning method of the present invention may be implemented by multiple servers.
[0295] In this embodiment, the electronic device of this embodiment is an electronic device as an example for detailed description, for example, Figure 4 As shown, it shows a schematic diagram of the structure of an electronic device involved in an embodiment of the present invention, specifically:
[0296] The electronic device may include components such as a processor 401 with one or more processing cores, a memory 402 with one or more computer-readable storage media, a power supply 403, an input module 404, and a communication module 405. Those skilled in the art will appreciate that Figure 4 The electronic device structure shown in the figure does not constitute a limitation on the electronic device, and may include more or fewer components than shown in the figure, or combine certain components, or arrange the components differently.
[0297] The processor 401 is the control center of the electronic device. It uses various interfaces and lines to connect various parts of the entire electronic device. By running or executing software programs and / or modules stored in the memory 402, and calling data stored in the memory 402, it performs various functions of the electronic device and processes data, thereby performing overall detection of the electronic device. In some embodiments, the processor 401 may include one or more processing cores; in some embodiments, the processor 401 may integrate an application processor and a modem processor, wherein the application processor mainly processes the operating system, user interface, and application programs, and the modem processor mainly processes wireless communications. It is understandable that the above-mentioned modem processor may not be integrated into the processor 401.
[0298] The memory 402 can be used to store software programs and modules. The processor 401 executes various functional applications and data processing by running the software programs and modules stored in the memory 402. The memory 402 may mainly include a program storage area and a data storage area, wherein the program storage area may store an operating system, an application required for at least one function (such as a sound playback function, an image playback function, etc.), etc.; the data storage area may store data created according to the use of the electronic device, etc. In addition, the memory 402 may include a high-speed random access memory, and may also include a non-volatile memory, such as at least one disk storage device, a flash memory device, or other volatile solid-state storage devices. Accordingly, the memory 402 may also include a memory controller to provide the processor 401 with access to the memory 402.
[0299] The electronic device also includes a power supply 403 for supplying power to various components. In some embodiments, the power supply 403 can be logically connected to the processor 401 through a power management system, so as to manage charging, discharging, and power consumption through the power management system. The power supply 403 can also include any components such as one or more DC or AC power supplies, recharging systems, power failure detection circuits, power converters or inverters, and power status indicators.
[0300] The electronic device may further include an input module 404, which may be used to receive input digital or character information and generate keyboard, mouse, joystick, optical or trackball signal input related to user settings and function control.
[0301] The electronic device may further include a communication module 405. In some embodiments, the communication module 405 may include a wireless module. The electronic device may perform short-range wireless transmission through the wireless module of the communication module 405, thereby providing the user with wireless broadband Internet access. For example, the communication module 405 may be used to help the user send and receive emails, browse web pages, and access streaming media.
[0302] Although not shown, the electronic device may further include a display unit, etc., which will not be described in detail herein. Specifically in this embodiment, the processor 401 in the electronic device will load the executable files corresponding to the processes of one or more application programs into the memory 402 according to the following instructions, and the processor 401 will run the application programs stored in the memory 402, thereby realizing various functions, as follows:
[0303] Get the source code, which includes the target function;
[0304] Performing equivalent conversion processing on the objective function to obtain at least one conversion function corresponding to the objective function;
[0305] Using the conversion function to replace the target function in the source code to obtain the replacement code;
[0306] Construct an abstract syntax tree based on the alternative code;
[0307] Based on the preset defect detection rules, the abstract syntax tree is scanned for defects to obtain the scanning results of defects in the source code.
[0308] The specific implementation of the above operations can be found in the previous embodiments, which will not be described in detail here.
[0309] From the above, it can be seen that this solution can improve the efficiency of source code scanning.
[0310] A person of ordinary skill in the art will appreciate that all or part of the steps in the various methods of the above embodiments may be completed by instructions, or by controlling related hardware through instructions. The instructions may be stored in a computer-readable storage medium and loaded and executed by a processor.
[0311] To this end, an embodiment of the present invention provides a computer-readable storage medium, in which a plurality of instructions are stored, and the instructions can be loaded by a processor to execute the steps in any one of the source code scanning methods provided in the embodiments of the present invention. For example, the instructions can execute the following steps:
[0312] Get the source code, which includes the target function;
[0313] Performing equivalent conversion processing on the objective function to obtain at least one conversion function corresponding to the objective function;
[0314] Using the conversion function to replace the target function in the source code to obtain the replacement code;
[0315] Construct an abstract syntax tree based on the alternative code;
[0316] Based on the preset defect detection rules, the abstract syntax tree is scanned for defects to obtain the scanning results of defects in the source code.
[0317] The storage medium may include: a read-only memory (ROM), a random access memory (RAM), a magnetic disk or an optical disk, etc.
[0318] According to one aspect of the present application, a computer program product or a computer program is provided, the computer program product or the computer program including computer instructions, the computer instructions being stored in a computer-readable storage medium. A processor of a computer device reads the computer instructions from the computer-readable storage medium, and the processor executes the computer instructions, so that the computer device executes the method provided in various optional implementations of the source code scanning aspect or the code scanning cloud service aspect provided in the above embodiments.
[0319] Since the instructions stored in the storage medium can execute the steps in any source code scanning method provided in the embodiments of the present invention, the beneficial effects that can be achieved by any source code scanning method provided in the embodiments of the present invention can be achieved. Please refer to the previous embodiments for details and will not be repeated here.
[0320] The source code scanning method, device, electronic device and computer-readable storage medium provided in the embodiments of the present invention are introduced in detail above. The principles and implementation methods of the present invention are explained in this article by using specific examples. The description of the above embodiments is only used to help understand the method of the present invention and its core idea. At the same time, for those skilled in the art, according to the idea of the present invention, there will be changes in the specific implementation method and application scope. In summary, the content of this specification should not be understood as limiting the present invention.
Claims
1. A source code scanning method, characterized in that: include: Obtaining source code, wherein the source code includes a target function; Performing an equivalent conversion process on the target function to obtain at least one conversion function corresponding to the target function, wherein the function effect of the conversion function is close to that of the target function, and the conversion function is easier to scan than the target function; wherein performing an equivalent conversion process on the target function to obtain at least one conversion function corresponding to the target function comprises: when the target function is an unknown library function, obtaining a preset unknown library function mapping relationship; based on the unknown library function mapping relationship, determining a conversion function that has a mapping relationship with the unknown library function; or When the target function is a target library function, obtaining a preset target library function mapping relationship; based on the target library function mapping relationship, determining a conversion function set having a mapping relationship with the target library function, wherein the conversion function set includes at least one conversion function; The target function in the source code is replaced by the conversion function to obtain a replacement code, wherein the target function in the source code is replaced by the conversion function, including: for the target function and a conversion function that has a mapping relationship with the target function, replacing the target function with the conversion function; constructing an abstract syntax tree based on the replacement code; Based on preset defect detection rules, the abstract syntax tree is scanned for defects to obtain scanning results of defects in the source code.
2. The source code scanning method according to claim 1, characterized in that: The constructing an abstract syntax tree based on the replacement code comprises: Determine an array object for calling the conversion function in the replacement code, and function parameters of the conversion function; When the conversion function is a memory allocation function, determining a return value of the conversion function based on the function parameter; The return value of the conversion function is defined in the replacement code, and the array object that calls the conversion function is modified so that the array object points to the return value of the target function, thereby obtaining a second replacement code; An abstract syntax tree is constructed based on the replacement code.
3. The source code scanning method according to claim 1, characterized in that: The source code includes a first function, and after constructing an abstract syntax tree based on the replacement code, further includes: Determine a function body of the first function and external variable parameters in the function body; Determine a position of a function body of the first function in the abstract syntax tree; The position is saved in a call relation table.
4. The source code scanning method according to claim 3, characterized in that: The source code includes a second function that calls the first function, and the defect scanning of the abstract syntax tree based on a preset defect detection rule to obtain a scan result of defects in the source code includes: When the second function is scanned in the abstract syntax tree, determining a next scanning position in the abstract syntax tree based on the call relationship table; Determining a function body of a first function and external variable parameters in the function body based on the next scanning position; Based on preset defect detection rules, the abstract syntax tree is scanned for defects using the function body and external variable parameters in the function body to obtain scanning results for defects in the source code.
5. The source code scanning method according to claim 1, characterized in that: The abstract syntax tree includes a loop structure, and the defect scanning is performed on the abstract syntax tree based on a preset defect detection rule to obtain a scanning result of defects in the source code, including: When scanning the local variables in the loop structure, determining the current fixed value of the local variables; Based on a preset defect detection rule and the current fixed value of the local variable, a defect scan is performed on the local variable in the abstract syntax tree to obtain a scan result of the defect of the local variable.
6. The source code scanning method according to claim 5, characterized in that: When scanning the local variables in the loop structure, before determining the current fixed value of the local variables, the method further includes: A first data structure defining local variables; Determining the current fixed value of the local variable when scanning the local variable in the loop structure includes: When a local variable in the loop structure is scanned, obtaining a previous certain value of the local variable from the first data structure; Based on the last set value, calculating the current set value of the local variable; Storing the current fixed value of the local variable in the first data structure of the local variable; The loop structure is rescanned.
7. The source code scanning method according to claim 6, characterized in that: The rescanning of the loop structure further comprises: Determine the loop type and loop condition threshold of the loop structure; When the loop type is a target loop type, determining a change type of the local variable; When the change type of the local variable is a monotonic change type and the current constant value of the local variable is greater than the loop condition threshold, then exit the loop structure; or, When the change type of the local variable is a monotonic change type and the loop condition threshold is greater than a preset threshold, the loop structure is exited.
8. The source code scanning method according to claim 1, characterized in that: The defect scanning of the abstract syntax tree based on the preset defect detection rule to obtain the scanning result of the defect in the source code includes: When an assignment structure in the abstract syntax tree is scanned, determining an assignment object and an assignment operation in the assignment structure; When the value of the assignment object is unknown, determining the detection value of the assignment object; Based on the detected value of the assignment object and the assignment operation, a defect in the assignment structure is determined.
9. The source code scanning method according to claim 8, characterized in that: When the value of the assignment object is unknown, determining the detection value of the assignment object comprises: Determine the data type of the assignment object, the data type including a signed type and an unsigned type; When the data type of the assignment object is a signed type, a preset positive boundary value, a negative boundary value, a positive middle value, a negative middle value and a zero value are determined as the detection value of the assignment object; When the data type of the assignment object is an unsigned type, a preset boundary value, an intermediate value and a zero value are determined as the detection value of the assignment object.
10. The source code scanning method according to claim 9, characterized in that: The assignment object includes a first assignment object and a second assignment object, and the determining of the defect in the assignment structure based on the detection value of the assignment object and the assignment operation includes: Determine a value range corresponding to the data type of the first assignment object; When the detection value of the second assignment object does not belong to the value range corresponding to the data type of the first assignment object, it is determined that a numerical overflow defect exists in the assignment structure.
11. A source code scanning device, characterized in that: include: An acquisition unit, used for acquiring source code, wherein the source code includes a target function; A conversion unit is used to perform an equivalent conversion process on the target function to obtain at least one conversion function corresponding to the target function, wherein the function effect of the conversion function is close to that of the target function, and the conversion function is easier to scan than the target function; wherein the equivalent conversion process on the target function to obtain at least one conversion function corresponding to the target function includes: when the target function is an unknown library function, obtaining a preset unknown library function mapping relationship; based on the unknown library function mapping relationship, determining a conversion function that has a mapping relationship with the unknown library function; or When the target function is a target library function, obtaining a preset target library function mapping relationship; based on the target library function mapping relationship, determining a conversion function set having a mapping relationship with the target library function, wherein the conversion function set includes at least one conversion function; a replacement unit, configured to use the conversion function to perform replacement processing on the target function in the source code to obtain a replacement code, wherein the use of the conversion function to perform replacement processing on the target function in the source code includes: for the target function and a conversion function that has a mapping relationship with the target function, using the conversion function to replace the target function; A construction unit, configured to construct an abstract syntax tree based on the replacement code; The scanning unit is used to perform defect scanning on the abstract syntax tree based on a preset defect detection rule to obtain a scanning result of defects in the source code.
12. An electronic device, characterized in that: The system comprises a processor and a memory, wherein the memory stores a plurality of instructions; the processor loads instructions from the memory to execute the steps in the source code scanning method according to any one of claims 1 to 10.
13. A computer-readable storage medium, characterized in that: The computer-readable storage medium stores a plurality of instructions, and the instructions are suitable for being loaded by a processor to execute the steps in the source code scanning method according to any one of claims 1 to 10.
