Code defect static detection method and system for Internet of Things operating system

Through path-based alias analysis and state migration technology, the efficiency and accuracy of code defect detection in the Internet of Things operating system is solved, efficient detection of complex alias relationships is achieved, detailed error reports are generated, and system security is improved.

CN113220302BActive Publication Date: 2024-11-12TSINGHUA UNIVERSITY
View PDF 3 Cites 0 Cited by

Patent Information

Application Number
CN202110400944.8
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2021-04-14
Publication Date
2024-11-12
Estimated Expiration
2041-04-14

AI Technical Summary

Technical Problem

Due to the complex alias relationship between data structures and pointers in the Internet of Things operating system, difficult-to-detect defects in the code such as dereference of null pointers, access to uninitialized variables and memory leaks, and it is difficult for the existing technology to efficiently calculate the reachable code paths. This results in huge time overhead for defect detection.

Method used

Using a path-based alias analysis method, intermediate code is generated through the LLVM compiler framework, a code analyzer abstracts alias relationship diagram, combined with state migration technology to detect defects, and accessibility verification of possible code paths to filter false positives. Generate detailed error reports.

Benefits of technology

It realizes accurate detection of multiple defects in the code of the Internet of Things operating system, improves the efficiency and accuracy of defect detection, reduces the need for manual analysis, and improves the security of the system.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN113220302B_ABST
    Figure CN113220302B_ABST
Patent Text Reader

Abstract

The present invention provides a method and system for static detection of code defects for an Internet of Things operating system, wherein the detection method comprises: compiling the source code of a target Internet of Things operating system to obtain an intermediate code; performing path-based alias analysis on the intermediate code to obtain the alias relationship between variables in each intermediate code path, and determining possible code defects; based on the alias relationship, verifying the reachability of each path of possible code defects, filtering out false positive code defects that fail to pass the reachability verification among possible code defects, and generating an error report for the determined detected code defects. The embodiment of the present invention adopts a static detection method, which can cover most of the codes in the Internet of Things operating system. The present invention can conveniently and effectively perform error detection and improve the security of the Internet of Things operating system.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the field of Internet of Things operating systems and software testing technology, and in particular to a method and system for statically detecting code defects in an Internet of Things operating system. Background Art

[0002] The IoT operating system is the foundation of IoT software. In order to efficiently transfer data between functions, a large number of data structures and pointers are used in the code of the IoT operating system. Due to the assignment operations between structures and pointers, and the transfer of structures and pointers across functions, there are complex alias relationships in these data structures and pointers. Many functions of the IoT operating system are provided to upper-level applications in the form of function interfaces. In the IoT operating system, these interface functions do not have clear callers, so the alias relationships related to their parameters are difficult to calculate. Due to the widespread use of data structures and pointers and the complexity of the alias relationships between them, the code of the IoT operating system is prone to a variety of difficult-to-detect defects, such as null pointer dereference, access to uninitialized variables, memory leaks, etc. If a null pointer is dereferenced, a null pointer dereference defect will be caused; if a variable is accessed before initialization, an access to uninitialized variables defect will be caused; if a dynamically requested memory is not released, a memory leak defect will be caused. Defects in the code are often related to specific paths, but some paths are unreachable. In order to reduce false positives, it is necessary to calculate the reachability of the paths in the code. However, there are many branch statements in the code of the IoT operating system, so the number of code paths is huge, so calculating the reachability of all paths in the code requires a high time overhead.

[0003] Therefore, designing an efficient and accurate static detection method for code defects in IoT operating systems has become an urgent problem to be solved in the development and testing of IoT operating systems. Summary of the invention

[0004] The present invention provides a method and system for statically detecting code defects in an Internet of Things operating system, so as to solve the technical defects existing in the prior art.

[0005] The present invention provides a static code defect detection method for an Internet of Things operating system, comprising:

[0006] Compile the source code of the target IoT operating system to obtain the intermediate code;

[0007] Performing path-based alias analysis on the intermediate code to obtain alias relationships between variables in each intermediate code path and determine possible code defects;

[0008] Based on the alias relationship, the reachability of each path of a possible code defect is verified, false positive code defects that fail the reachability verification are filtered out from the possible code defects, and an error report is generated for the determined detected code defects.

[0009] According to the static code defect detection method for an Internet of Things operating system provided by the present invention, the source code of the target Internet of Things operating system is compiled to obtain the intermediate code, which specifically includes:

[0010] Modify the compilation logic of the IoT operating system, use the LLVM compiler framework to compile each source code file of the IoT operating system, and generate the corresponding intermediate code files; collect the intermediate code files where the function definitions are located for cross-file analysis.

[0011] According to the static code defect detection method for an Internet of Things operating system provided by the present invention, the path-based alias analysis of the intermediate code is performed to obtain the alias relationship between variables in each intermediate code path, including:

[0012] A code analyzer is used to perform static analysis on the intermediate code file, and each intermediate code path is analyzed separately; during the analysis process, the alias relationship between variables is abstracted into an alias relationship graph and the alias relationship graph is updated in real time based on the instructions of the intermediate code, and the alias relationship between variables is inferred from the alias relationship graph.

[0013] According to the static code defect detection method for an Internet of Things operating system provided by the present invention, the determining of possible code defects includes:

[0014] According to the instructions related to the triggering defect and the current states of the variables operated by the instructions, the states of the operated variables are changed, and the variables that are aliases of each other are maintained in the same state;

[0015] During the process of variable state migration, if a state that can trigger a defect is reached, it is determined to be a possible code defect.

[0016] According to the static code defect detection method for an Internet of Things operating system provided by the present invention, the verification of the reachability of each path of a possible code defect based on the alias relationship includes:

[0017] Based on the alias relationship, the variables that are aliased to each other in the intermediate code of each possible code defect are mapped to the same variable in the theorem prover, and the instructions in the intermediate code are abstracted into constraints in the theorem prover. The reachability of the path is verified by solving the satisfiability of these constraints.

[0018] According to the static code defect detection method for an Internet of Things operating system provided by the present invention, the defect report includes the function call path of the detected code defect and the corresponding source code file name and line number.

[0019] The present invention also provides a code defect static detection system for an Internet of Things operating system, comprising:

[0020] A code compiler is used to compile the source code of the target IoT operating system to obtain an intermediate code;

[0021] A code analyzer, used to perform path-based alias analysis on the intermediate code, obtain the alias relationship between variables in each intermediate code path, and determine possible code defects;

[0022] A code path verifier is used to verify the reachability of each path of possible code defects based on the alias relationship, filter out false positive code defects that fail the reachability verification among possible code defects, and generate an error report for the determined detected code defects.

[0023] According to the code defect static detection system for the Internet of Things operating system provided by the present invention, the code analyzer includes:

[0024] An alias relationship determination module is used to use a code analyzer to perform static analysis on the intermediate code file, and analyze each intermediate code path separately; during the analysis process, the alias relationship between variables is abstracted into an alias relationship graph and the alias relationship graph is updated in real time based on the instructions in the intermediate code, and the alias relationship between variables is inferred from the alias relationship graph;

[0025] The pre-judgment module is used to change the state of the operated variable according to the instructions related to triggering the defect and the current state of the variable operated by the instruction, and maintain the same state of the variables that are aliases to each other; during the process of variable state migration, if a state that can trigger a defect is reached, it is determined to be a possible code defect.

[0026] The present invention also provides an electronic device, comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein when the processor executes the program, the steps of any of the above-described methods for static detection of code defects for an Internet of Things operating system are implemented.

[0027] The present invention also provides a non-transitory computer-readable storage medium having a computer program stored thereon. When the computer program is executed by a processor, the steps of the static code defect detection method for an Internet of Things operating system as described in any one of the above are implemented.

[0028] The present invention proposes a static detection method for code defects in an Internet of Things operating system, which performs path-based alias analysis to obtain accurate alias relationships. While performing alias analysis, defect detection based on state migration is adopted to detect multiple code defects at the same time. In addition, the method performs reachability detection on code paths that may have defects to filter out false positives, thereby improving the accuracy of defect detection while ensuring analysis efficiency. The method can generate a relatively detailed error report to facilitate developers and testers to read and check. By adopting a static detection method, most of the codes in the Internet of Things operating system can be covered. The workflow of the present invention is automatic, and developers and testers do not need to manually analyze and modify the source code of the Internet of Things operating system under test. Therefore, the present invention can conveniently and effectively perform error detection and improve the security of the Internet of Things operating system. BRIEF DESCRIPTION OF THE DRAWINGS

[0029] In order to more clearly illustrate the technical solutions in the present invention or the prior art, the following briefly introduces the drawings required for use in the embodiments or the description of the prior art. Obviously, the drawings described below are some embodiments of the present invention. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying creative work.

[0030] Figure 1 Schematic diagram of a static code defect detection method for an Internet of Things operating system according to an embodiment of the present invention;

[0031] Figure 2 Compilation process for IoT operating system code

[0032] Figure 3 It is a state transition diagram for detecting null pointer dereference;

[0033] Figure 4 It is a state transition diagram for detecting the use of uninitialized variables;

[0034] Figure 5 It is a state transition diagram for detecting memory leaks;

[0035] Figure 6 This is a piece of code. The path 2->3->4->5->7->8 in the code may have a null pointer dereference defect;

[0036] Figure 7 These are the constraints that need to be established when there is no alias information to guide;

[0037] Figure 8 These are the constraints that need to be added when there is guidance from alias information;

[0038] Fig. 9It is a schematic diagram of a code defect static detection system for an Internet of Things operating system provided by an embodiment of the present invention;

[0039] Fig.10 It is a structural schematic diagram of the electronic device provided by the present invention. DETAILED DESCRIPTION

[0040] In order to make the purpose, technical solution and advantages of the present invention clearer, the technical solution of the present invention will be clearly and completely described below in conjunction with the drawings of the present invention. Obviously, the described embodiments are 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 ordinary technicians in this field without creative work are within the scope of protection of the present invention.

[0041] The embodiment of the present invention discloses a static code defect detection method for an Internet of Things operating system. Figure 1 ,include:

[0042] S1: compile the source code of the target IoT operating system to obtain the intermediate code;

[0043] The main condition for the application of this method is that the source code of the IoT operating system under test can be compiled normally by the LLVM compiler framework and can successfully generate the corresponding intermediate code. Therefore, the compilation process of the IoT operating system needs to be changed to meet the above conditions.

[0044] The S1 includes:

[0045] Modify the compilation logic of the IoT operating system, use the LLVM compiler framework to compile each source code file of the IoT operating system, and generate the corresponding intermediate code file;

[0046] Collects the intermediate code files where function definitions are located for cross-file analysis.

[0047] The source code of the IoT operating system is usually written in C language, but due to the flexibility of C language, it is difficult to directly analyze the source code. Each IoT operating system uses a customized compiler. Due to the flexibility of the LLVM compiler framework, these customized compilers can be replaced by it to compile intermediate codes that are convenient for program analysis. Therefore, the present invention first uses the LLVM compiler framework to compile the source code of the IoT operating system.

[0048] Figure 2 The compilation process of the IoT operating system code is mainly divided into two steps:

[0049] (1) Use the front-end compiler Clang provided by LLVM to compile each source code file in the IoT operating system and generate the corresponding intermediate code file.

[0050] (2) Analyze and record the function information in each intermediate code file to facilitate subsequent analysis of cross-file function calls.

[0051] S2: performing path-based alias analysis on the intermediate code to obtain alias relationships between variables in each intermediate code path, and determining possible code defects;

[0052] The S2 includes:

[0053] The intermediate code file generated in S1 is statically analyzed using a code analyzer 20. This step uses functions that are not explicitly called (by function name) as the entry point for analysis and performs path-based alias analysis. Specifically, each code path is analyzed separately. During the analysis process, the alias relationship between variables is abstracted into an alias relationship graph, and the graph is updated according to the instructions in the code, and the alias relationship between variables is inferred from the graph. While performing the alias analysis, a state transition-based analysis technique is used to detect code defects. Specifically, the state of the operated variable is changed according to the instruction related to the triggering defect and the current state of the variable operated by the instruction. Because aliased variables have the same state, this technology maintains the same state for aliased variables, reduces the number of maintained states, and thus reduces the analysis time and memory overhead. During the variable state migration process, if a state that can trigger a defect is reached, a possible code defect is detected.

[0054] S3: Based on the alias relationship, the reachability of each path of a possible code defect is verified, false positive code defects that fail the reachability verification are filtered out from the possible code defects, and an error report is generated for the determined detected code defects.

[0055] The S3 includes:

[0056] Alias-guided path reachability detection is performed on paths that may have code defects. Specifically, the results of alias analysis are used to map aliased variables in the code to the same variable in the theorem prover, thereby reducing the number of constraints, analysis time and memory overhead. In addition, only paths that may have defects are detected, which improves the efficiency of defect detection while ensuring the accuracy of defect detection.

[0057] The static code defect detection method for the Internet of Things operating system of the embodiment of the present invention performs path-based alias analysis to obtain accurate alias relationships. While performing alias analysis, defect detection based on state transition is adopted to detect multiple code defects at the same time. In addition, the method performs reachability detection on code paths that may have defects to filter out false positives, thereby improving the accuracy of defect detection while ensuring analysis efficiency. The method can generate a relatively detailed error report to facilitate developers and testers to read and check. By adopting a static detection method, most of the code in the Internet of Things operating system can be covered. The workflow of the present invention is automatic, and developers and testers do not need to manually analyze and modify the source code of the Internet of Things operating system under test. Therefore, the present invention can conveniently and effectively perform error detection and improve the security of the Internet of Things operating system.

[0058] According to the static code defect detection method for an Internet of Things operating system provided by the present invention, the path-based alias analysis of the intermediate code is performed to obtain the alias relationship between variables in each intermediate code path, including:

[0059] Use a code analyzer 20 to perform static analysis on the intermediate code file, and analyze each intermediate code path separately; during the analysis process, the alias relationship between variables is abstracted into an alias relationship graph and the alias relationship graph is updated in real time based on the instructions in the intermediate code, and the alias relationship between variables is inferred from the alias relationship graph.

[0060] In a specific implementation, based on the path alias analysis, a pointer relationship graph is used to represent the alias relationship between variables. The pointer relationship graph is a directed graph.<N,E> , where N is the set of nodes in the graph. At the beginning of the analysis, each node in the graph represents a variable in the program; E is a set of labeled directed edges, labeled as the member name of the structure or the dereference operator *, which is used to represent the way of accessing another memory object through one memory object. In the pointer relationship graph, the variables represented by all paths to the same node are aliases of each other. Path-based alias analysis, for each path in the program, updates the pointer relationship graph according to the instructions in the path, and the alias relationship between variables in the program can be inferred from the graph. There are 4 instructions related to alias analysis in the LLVM intermediate code: move(v1=v2), store(*v2=v1), load(v1=*v2), getelementptr(v1=&v2->f). In the pseudocode diagram, represents the edge from n2 to n1 and is labeled x.

[0061] Here is how the move instruction is processed:

[0062]

[0063] GetNode(v,N) returns the node representing variable v in node set N, and Vars(n) returns the variable set represented by node n. First, find nodes n1 and n2 that represent variables v1 and v2 related to the move instruction. Then, transfer v1 from the variable set represented by n1 to the variable set represented by n2.

[0064] Here is how the store instruction is processed:

[0065]

[0066]

[0067] First, find nodes n1 and n2 that represent store instruction related variables v1 and v2. If node n2 has an edge marked with *, delete the edge. Then, add a directed edge from n2 to n1 to the edge set E and mark it with *.

[0068] Here is how the load instruction is processed:

[0069]

[0070] First, find nodes n1 and n2 that represent the variables v1 and v2 associated with the load instruction. Then, if node n2 has an edge marked with *, assume that its destination node is n x , then transfer v1 from the variable set represented by n1 to n x In the set of variables represented by, otherwise, add a directed edge from n2 to n1 to the edge set E and mark it as *.

[0071] Here is how the getelementptr instruction is processed:

[0072]

[0073] First, find nodes n1 and n2 that represent variables v1 and v2 associated with the getelementptr instruction. Then, if node n2 has an edge labeled with the name of the member variable f taken by the instruction, assume that its destination node is n x , then transfer v1 from the variable set represented by n1 to n x In the set of variables represented by , otherwise, add a directed edge from n2 to n1 to the edge set E and mark it as f.

[0074] The path-based alias analysis framework adopted by S2 in the present invention is as follows: the framework includes a function processing module (HandleFunc), a function call processing module (HandleCall), and an instruction processing module (HandleInst), which implement cross-function analysis through recursive calls. The algorithm maintains an instruction stack path during the analysis process to record the analysis path.

[0075] The initialization operations for analyzing each function in the code are as follows:

[0076]

[0077] For functions that are not explicitly called in the IoT operating system code, analysis starts from their first instruction. At this time, the instruction stack path only contains the first instruction of the analyzed function.

[0078] The processing of the function call instruction in the intermediate code is as follows:

[0079]

[0080] This process is represented by HandleCall, which replaces the transfer of each parameter with a move instruction. UpdateAliasGraph is used to update the alias relationship graph under the current path. After processing the parameter transfer, HandleFunc is called to analyze the called function. Similarly, the function return is converted into a move operation.

[0081] The operation method of updating the alias relationship diagram according to the instruction type is:

[0082]

[0083]

[0084] The top of the instruction stack is the instruction currently being analyzed, and GetAliasGraph(path) returns the alias graph under the current path. If this graph is an empty graph, it means that the path has not been analyzed. For all variables in the program, a node representing the variable is inserted into the graph accordingly. According to the type of instruction being analyzed, the algorithm is used to update the alias graph. If the alias graphs before and after the update are different, the new graph is used as the alias graph under the path (UpdateAliasGraph). In addition, the updated alias graph is used to perform defect detection based on state transition, and BugDetection represents the defect detection process. Finally, the successor instructions of the analyzed instruction are pushed into the instruction stack path, and HandleInst is called to analyze these successor instructions. After the analysis, the top element of the stack is popped out.

[0085] Defect detection based on state transition can be done using state machine FSM type =<Σ,S,init,δ,BUG type >, where Σ is the set of instructions related to defect detection, S is the set of states of the variable, init is the initial state of the variable, and δ is the state transition function, which can be expressed as Figure 3-5 The state transition diagram shown shows that BUG type is the final state, indicating that a possible defect of type type is detected. Thanks to the accurate alias relationship obtained by path-based alias analysis, the same state can be maintained for all aliased variables during the defect detection process, thereby reducing the time and memory overhead of the algorithm.

[0086] Figure 3-5 The state transition diagrams for detecting null pointer dereference (NPD), access to uninitialized variables (UVA) and memory leak defects (ML) are listed, and * represents any instruction in the diagram. These three examples are only used to illustrate the present invention, but not to limit the present invention. Various defects can be detected using the ideas of the above examples, and they all belong to the protection scope of the present invention.

[0087] Figure 3 This is a state transition diagram for detecting null pointer dereference. If a variable is assigned to NULL (assign_null), or a variable is compared to NULL and jumps to the NULL branch (br_null), then the state of all variables that are aliases of the variable is changed to NULL. If a variable is compared to NULL and jumps to the non-NULL branch (br_not_null), then the state of all variables that are aliases of the variable is changed to NOT_NULL. If a variable with a NULL state is dereferenced (deref), a possible null pointer dereference bug (BUG 100) is detected. NPD ).

[0088] Figure 4 It is a state transition diagram for detecting the use of uninitialized variables. If a variable is assigned a constant (assign_const), then all variables that are aliases to it are initialized (INIT). If a variable is declared (alloc) or retrieved from an uninitialized structure (load), then all variables that are aliases to it are uninitialized (UNINIT). If an uninitialized variable is used (use), then a possible bug (BUG) in accessing an uninitialized variable is detected. UVA ).

[0089] Figure 5It is a state transition diagram for detecting memory leaks. If the memory pointed to by a pointer is dynamically allocated (malloc), then the memory pointed to by the variables that are aliases with it is not released (NOT_FREED). If the memory pointed to by a pointer is released (free), then the state of the variables that are aliases with the pointer changes to FREED. If there is still unreleased memory when a function returns (ret), a possible memory leak defect (BUG) is detected. ML ).

[0090] The conversion rules of the alias-guided path reachability detection method are shown in the following code:

[0091]

[0092]

[0093] The transformation rule describes how to transform the intermediate code into the formula in the theorem prover, and the transformation function is represented by T. The function N(v) is used to map the variable v in the intermediate code to the variable in the theorem prover. Benefiting from the accurate alias relationship obtained by path-based alias analysis, the variables that are aliases in the intermediate code are mapped to the same variable in the theorem prover, thereby reducing the complexity of solving the constraint satisfiability. Transformations can be divided into three categories:

[0094] Lvalue conversion: For an lvalue, the conversion result is the variable it maps to in the theorem prover. This conversion is done with T var (v) indicates.

[0095] Expression conversion: For a constant expression c, the conversion result is the constant. This conversion is done with T exp (c) means: for a binary operation e1 opb e2, the conversion result is the conversion result of expressions e1 and e2. b Operation, the conversion is done with T exp (e1 opb e2) means; for a unary operation op u e1, the conversion result is the conversion result of expression e1. u Operation, the conversion is done with T exp (op u e1) indicates.

[0096] Statement conversion: For a statement var:=e, the conversion result is that the conversion result of the left value var and the conversion result of the expression e are logically equal. This conversion is done with T stm(var:=e) means; for a branch statement with the condition e, if it jumps to the branch brt(e) where the condition is true, then its conversion result is the conversion result of condition e which is logically equal to 1. This conversion is represented by T stm (brt(e)) indicates that, otherwise, the conversion result is equal to the conversion result of condition e and is logically equal to 0. This conversion is represented by T stm (brf(e)) indicates.

[0097] Figure 6-8 This is an example of an alias-guided path reachability detection method, which demonstrates the role of alias guidance in reducing solution complexity. Figure 6 This is a piece of code. The path 2->3->4->5->7->8 in the code may have a null pointer dereference defect. Figure 7 It is the constraint condition that needs to be established in the absence of alias information guidance. The -> symbol in the figure indicates implication, that is, the equality of pointers implies that the objects they point to are equal. This constraint is called implicit constraint in this invention. In the absence of alias information, implicit constraints need to be added between pointers. Figure 8 These are the constraints that need to be added when there is alias information guidance. Since all aliased variables in the code are mapped to the same variable in the theorem prover, the constraint t=p in line 5 and all implicit constraints can be omitted. Figure 7 and Figure 8 By comparing the results of the above, we can find that the alias-guided path reachability detection can reduce the complexity of constraint solving. In this example, there are four code paths, but only one path may have a null pointer dereference defect. Only the reachability of this path needs to be checked. If it fails the test, it is found that the defect is a false positive and there is no need to generate a defect report. This shows that this method improves detection efficiency while ensuring accuracy.

[0098] Furthermore, the possible code defects determined include:

[0099] According to the instructions related to the triggering defect and the current states of the variables operated by the instructions, the states of the operated variables are changed, and the variables that are aliases of each other are maintained in the same state;

[0100] During the process of variable state migration, if a state that can trigger a defect is reached, it is determined to be a possible code defect.

[0101] According to the static code defect detection method for an Internet of Things operating system provided by the present invention, the verification of the reachability of each path of a possible code defect based on the alias relationship includes:

[0102] Based on the alias relationship, the variables that are aliased to each other in the intermediate code of each possible code defect are mapped to the same variable in the theorem prover, and the instructions in the intermediate code are abstracted into constraints in the theorem prover. The reachability of the path is verified by solving the satisfiability of these constraints.

[0103] According to the static code defect detection method for an Internet of Things operating system provided by the present invention, the defect report includes the function call path of the detected code defect and the corresponding source code file name and line number.

[0104] The embodiment of the present invention discloses a code defect static detection system for an Internet of Things operating system, see Fig. 9 , mainly including: mainly composed of three parts: code compiler 10, code analyzer 20 and code path verifier 30: code compiler 10 is responsible for compiling the C language source code of the tested IoT operating system to generate intermediate code, which is convenient for subsequent static analysis; code analyzer 20 is responsible for analyzing the intermediate code, obtaining the alias relationship between variables in the intermediate code, and analyzing possible code defects; the defect filter is responsible for verifying the accessibility of the code path that may have defects, in order to filter out false positives, and generate the final error report. The generated error report contains the function call path and the corresponding source code file name and line number. Specifically, the code compiler 10 is used to compile the source code of the target IoT operating system to obtain the intermediate code;

[0105] A code analyzer 20, configured to perform path-based alias analysis on the intermediate code, obtain alias relationships between variables in each intermediate code path, and determine possible code defects;

[0106] The code path verifier 30 is used to verify the reachability of each path of possible code defects based on the alias relationship, filter out false positive code defects that fail to pass the reachability verification among possible code defects, and generate an error report for the determined detected code defects.

[0107] According to the code defect static detection system for the Internet of Things operating system provided by the present invention, the code analyzer 20 includes:

[0108] An alias relationship determination module is used to use the code analyzer 20 to perform static analysis on the intermediate code file, and analyze each intermediate code path separately; during the analysis process, the alias relationship between variables is abstracted into an alias relationship graph and the alias relationship graph is updated in real time based on the instructions in the intermediate code, and the alias relationship between variables is inferred from the alias relationship graph;

[0109] The pre-judgment module is used to change the state of the operated variable according to the instructions related to triggering the defect and the current state of the variable operated by the instruction, and maintain the same state of the variables that are aliases to each other; during the process of variable state migration, if a state that can trigger a defect is reached, it is determined to be a possible code defect.

[0110] Fig.10 An example of a physical structure diagram of an electronic device is provided, and the electronic device may include: a processor 310, a communication interface 320, a memory 330, and a communication bus 340, wherein the processor 310, the communication interface 320, and the memory 330 communicate with each other through the communication bus 340. The processor 310 may call the logic instructions in the memory 330 to execute a static code defect detection method for an Internet of Things operating system, and the method includes:

[0111] S1: compile the source code of the target IoT operating system to obtain the intermediate code;

[0112] S2: performing path-based alias analysis on the intermediate code to obtain alias relationships between variables in each intermediate code path, and determining possible code defects;

[0113] S3: Based on the alias relationship, the reachability of each path of a possible code defect is verified, false positive code defects that fail the reachability verification are filtered out from the possible code defects, and an error report is generated for the determined detected code defects.

[0114] In addition, the logic instructions in the above-mentioned memory can be implemented in the form of software functional units and can be stored in a computer-readable storage medium when sold or used as an independent product. Based on such an understanding, the technical solution of the present invention, in essence, or the part that contributes to the prior art or the part of the technical solution, can be embodied in the form of a software product, which is stored in a storage medium and includes several instructions for a computer device (which can be a personal computer, a server, or a network device, etc.) to perform all or part of the steps of the method described in each embodiment of the present invention. The aforementioned storage medium includes: various media that can store program codes, such as a USB flash drive, a mobile hard disk, a read-only memory (ROM), a random access memory (RAM), a magnetic disk or an optical disk.

[0115] On the other hand, the present invention further provides a computer program product, the computer program product comprising a computer program stored on a non-transitory computer-readable storage medium, the computer program comprising program instructions, and when the program instructions are executed by a computer, the computer can execute a code defect static detection method for an Internet of Things operating system, the method comprising:

[0116] S1: compile the source code of the target IoT operating system to obtain the intermediate code;

[0117] S2: performing path-based alias analysis on the intermediate code to obtain alias relationships between variables in each intermediate code path, and determining possible code defects;

[0118] S3: Based on the alias relationship, the reachability of each path of a possible code defect is verified, false positive code defects that fail the reachability verification are filtered out from the possible code defects, and an error report is generated for the determined detected code defects.

[0119] In another aspect, the present invention further provides a non-transitory computer-readable storage medium having a computer program stored thereon, wherein when the computer program is executed by a processor, a method for statically detecting code defects in an Internet of Things operating system is implemented, the method comprising:

[0120] S1: compile the source code of the target IoT operating system to obtain the intermediate code;

[0121] S2: performing path-based alias analysis on the intermediate code to obtain alias relationships between variables in each intermediate code path, and determining possible code defects;

[0122] S3: Based on the alias relationship, the reachability of each path of a possible code defect is verified, false positive code defects that fail the reachability verification are filtered out from the possible code defects, and an error report is generated for the determined detected code defects.

[0123] The system embodiment described above is merely illustrative, wherein the units described as separate components may or may not be physically separated, and the components shown as units may or may not be physical units, that is, they may be located in one place, or they may be distributed on multiple network units. Some or all of the modules may be selected according to actual needs to achieve the purpose of the solution of this embodiment. Those of ordinary skill in the art may understand and implement it without creative work.

[0124] Through the description of the above implementation methods, those skilled in the art can clearly understand that each implementation method can be implemented by means of software plus a necessary general hardware platform, and of course, it can also be implemented by hardware. Based on this understanding, the above technical solution is essentially or the part that contributes to the prior art can be embodied in the form of a software product, and the computer software product can be stored in a computer-readable storage medium, such as ROM / RAM, a disk, an optical disk, etc., including a number of instructions for a computer device (which can be a personal computer, a server, or a network device, etc.) to execute the methods described in each embodiment or some parts of the embodiments.

[0125] Finally, it should be noted that the above embodiments are only used to illustrate the technical solutions of the present invention, rather than to limit it. Although the present invention has been described in detail with reference to the aforementioned embodiments, those skilled in the art should understand that they can still modify the technical solutions described in the aforementioned embodiments, or make equivalent replacements for some of the technical features therein. However, these modifications or replacements do not deviate the essence of the corresponding technical solutions from the spirit and scope of the technical solutions of the embodiments of the present invention.

Claims

1. A static code defect detection method for an Internet of Things operating system, characterized in that: include: Compile the source code of the target IoT operating system to obtain the intermediate code; Performing path-based alias analysis on the intermediate code to obtain alias relationships between variables in each intermediate code path and determine possible code defects; Based on the alias relationship, the reachability of each possible path of the code defect is verified, false positive code defects that fail the reachability verification are filtered out from the possible code defects, and an error report is generated for the determined detected code defects; The possible code defects determined include: According to the instructions related to the triggering defect and the current states of the variables operated by the instructions, the states of the operated variables are changed, and the variables that are aliases of each other are maintained in the same state; During the process of variable state migration, if a state that can trigger a defect is reached, it is determined to be a possible code defect; The verification of the reachability of each path of a possible code defect based on the alias relationship includes: Based on the alias relationship, the variables that are aliased to each other in the intermediate code of each possible code defect are mapped to the same variable in the theorem prover, and the instructions in the intermediate code are abstracted into constraints in the theorem prover. The reachability of the path is verified by solving the satisfiability of these constraints.

2. The method for static code defect detection for an Internet of Things operating system according to claim 1, characterized in that: The source code of the target IoT operating system is compiled to obtain the intermediate code, which specifically includes: Modify the compilation logic of the IoT operating system, use the LLVM compiler framework to compile each source code file of the IoT operating system, and generate the corresponding intermediate code files; collect the intermediate code files where the function definitions are located for cross-file analysis.

3. The method for static code defect detection for an Internet of Things operating system according to claim 1, characterized in that: The path-based alias analysis of the intermediate code to obtain the alias relationship between variables in each intermediate code path includes: A code analyzer is used to perform static analysis on the intermediate code file, and each intermediate code path is analyzed separately; during the analysis process, the alias relationship between variables is abstracted into an alias relationship graph and the alias relationship graph is updated in real time based on the instructions in the intermediate code, and the alias relationship between variables is inferred from the alias relationship graph.

4. The method for static code defect detection for an Internet of Things operating system according to claim 1, characterized in that: The error report includes the function call path of the detected code defect and the corresponding source code file name and line number.

5. A code defect static detection system for an Internet of Things operating system, characterized in that: include: A code compiler is used to compile the source code of the target IoT operating system to obtain an intermediate code; A code analyzer, used to perform path-based alias analysis on the intermediate code, obtain the alias relationship between variables in each intermediate code path, and determine possible code defects; A code path verifier is used to verify the reachability of each path of a possible code defect based on the alias relationship, filter out false positive code defects that fail to pass the reachability verification among possible code defects, and generate an error report for the determined detected code defects; The possible code defects determined include: According to the instructions related to the triggering defect and the current states of the variables operated by the instructions, the states of the operated variables are changed, and the variables that are aliases of each other are maintained in the same state; During the process of variable state migration, if a state that can trigger a defect is reached, it is determined to be a possible code defect; The verification of the reachability of each path of a possible code defect based on the alias relationship includes: Based on the alias relationship, the variables that are aliased to each other in the intermediate code of each possible code defect are mapped to the same variable in the theorem prover, and the instructions in the intermediate code are abstracted into constraints in the theorem prover. The reachability of the path is verified by solving the satisfiability of these constraints.

6. The code defect static detection system for the Internet of Things operating system according to claim 5 is characterized in that: The code analyzer comprises: An alias relationship determination module is used to use a code analyzer to perform static analysis on the intermediate code file, and analyze each intermediate code path separately; during the analysis process, the alias relationship between variables is abstracted into an alias relationship graph and the alias relationship graph is updated in real time based on the instructions in the intermediate code, and the alias relationship between variables is inferred from the alias relationship graph; The pre-judgment module is used to change the state of the operated variable according to the instructions related to triggering the defect and the current state of the variable operated by the instruction, and maintain the same state of the variables that are aliases to each other; during the process of variable state migration, if a state that can trigger a defect is reached, it is determined to be a possible code defect.

7. An electronic device comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, characterized in that: When the processor executes the program, the steps of the static detection method for code defects for an Internet of Things operating system as described in any one of claims 1 to 4 are implemented.

8. A non-transitory computer-readable storage medium having a computer program stored thereon, characterized in that: When the computer program is executed by a processor, the steps of the static detection method for code defects in an Internet of Things operating system as described in any one of claims 1 to 4 are implemented.

Citation Information

Patent Citations

  • Automatic software code detection method

    CN108804326A

  • C language defect detection method and device based on multi-level analysis

    CN111104335A

  • Source code static analysis device

    CN111240687A