Method for performing program analysis
By combining the abstract interpretation method of forward and backward analysis, the prerequisites of functions in embedded systems are automatically determined, solving the problem of low efficiency of manually determining prerequisites in the existing technology, and realizing efficient runtime error checking and contracts. describe.
Patent Information
- Application Number
- CN202410277947.0
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Priority Date
- 2023-03-13
- Filing Date
- 2024-03-12
- Publication Date
- 2024-09-13
AI Technical Summary
In embedded systems, when using abstract interpretation for program analysis, determining the prerequisites of a function often requires a lot of manual work and is difficult to automate, resulting in inefficient runtime error checking.
Using a method that combines forward and backward analysis, the possible variable assignments of the function are automatically determined through abstract interpretation, key variable assignments are identified, and then the prerequisites for error-free execution are determined, and the prerequisites are inferred through inverse reasoning and backward calculation. , to achieve automated formal verification and error checking.
It implements automated functional formal verification to ensure error-free execution of functions in embedded systems, improves the efficiency and accuracy of runtime error checking, and can automatically provide contract descriptions between modules, including prerequisites and postconditions.
Smart Images

Figure CN118642941A
Abstract
Description
Technical Field
[0001] The invention relates to a method for performing a program analysis. The invention further relates to a computer program, a device and a storage medium for this purpose. Background Art
[0002] When using safety-related software, which is often found in embedded systems, avoiding runtime errors plays an important role. Thus, for example, division by zero can cause the program to terminate and its actual task (such as actuating the brakes or triggering the airbag) can no longer be correctly performed. In the case of conventional solutions, formal verification is often performed in order to ensure that such errors cannot occur. A common method used for this purpose is abstract interpretation (see, for example, Patrick Cousot, Radhia Cousot: "Systematic design of program analysis frameworks". In: Proceedings of the 6th ACM SIGACT-SIGPLAN Symposium on Principles of Programming Languages. ACM Press, 1979, pp. 269-282.).
[0003] Abstract interpretation can be used as a forward analysis to find potential errors. Especially when applied in a modular way, abstract interpretation can achieve sufficient scalability and accuracy for many situations.
[0004] However, it is still a technical challenge to actually perform program analysis with the aid of abstract interpretation. It is therefore often desirable to have an extensive description of the interfaces between the modules of a system in the form of contracts. These descriptions may include preconditions that must be met in order for a function to be correctly executed upon invocation and postconditions that are then guaranteed. However, the determination of preconditions is often associated with a significant manual effort. Summary of the invention
[0005] The subject matter of the invention is a method having the features of claim 1, a computer program having the features of claim 9, a device having the features of claim 10 and a computer-readable storage medium having the features of claim 11. Further features and details of the invention are derived from the respective dependent claims, the description and the drawings. Features and details described in conjunction with the method according to the invention naturally also apply in conjunction with the computer program according to the invention, the device according to the invention and the computer-readable storage medium according to the invention, and vice versa, respectively, so that reference is always made to one another or can be made to one another with respect to the disclosure of the individual inventive aspects.
[0006] The subject of the invention is in particular a method for program analysis, preferably for runtime error checking, in particular in the case of embedded systems. The method can also be used to determine the prerequisites for error-free execution at at least or exactly one function of the embedded system. To this end, the following steps can be provided, which can be performed automatically and / or repeatedly and / or in the order specified and / or in a computer-aided manner:
[0007] - performing an abstract interpretation, in which the function is analyzed forward (also called forward analysis) in order to determine possible variable assignments for a plurality of points, in particular all points, preferably program points, of the function, wherein the respective point may specify at least or exactly one instruction of the function, wherein the points may also be referred to as statements of the program, nodes, edges or program points, if necessary,
[0008] - checking the obtained possible variable assignments in view of the existence of at least one critical variable assignment, wherein said critical variable assignment is suitable for causing a runtime error of said function,
[0009] - performing an abstract interpretation, wherein the function is analyzed backwards starting from critical variable assignments, in particular starting from the point where the critical variable assignments are identified (also called backward analysis), in order to determine at least one condition for the critical variable assignments, in order to determine therefrom a prerequisite for error-free execution.
[0010] In particular, the following advantages are achieved, namely that the formal verification of the function can be performed automatically in order to check and, if necessary, ensure that no errors occur during the operation of the function. In this case, the method can be implemented as a computer-aided code analysis. In addition, it is possible to determine the prerequisite by reverse reasoning based on at least one condition for the assignment of the key variable. This means that if at least one condition for the assignment of the key variable is not met, the prerequisite may exist.
[0011] Alternatively, it may be possible to perform a backward analysis based on the possible variable assignments obtained, wherein for this purpose an automated backward calculation may be performed starting from the key variable assignments. Here, the variable assignments obtained by the backward calculation may be compared with the possible variable assignments obtained (in the corresponding steps of the forward analysis) in order to exclude conditions that are impossible for at least one condition for determining the key variable assignments. In other words, such conditions that may not exist algorithmically, for example, may be excluded as impossible, which may be derived from the possible variable assignments of the forward analysis. Here, the forward analysis may include a plurality of single results, preferably from a plurality of steps, which are obtained, for example, from the forward calculations for the individual points. The single results may indicate possible variable assignments that may be used for the backward calculations, if necessary.
[0012] In the case of the present invention, it can be provided that backward calculation is used so that backward analysis can be realized and thereby preconditions can be inferred. Backward calculation can be possible, for example, from a point in the program code where runtime errors potentially occur, that is, especially from key variable assignments. Preconditions can include such conditions that must be met when calling, so that the function is correctly executed (so that runtime errors do not occur). In addition, it can be possible through the described solution to also infer postconditions that can be guaranteed when there are prerequisites. The determination of the above conditions can be used to provide an extended description of the interface between the modules of the system according to the present invention, especially in the form of a contract, which can include prerequisites and postconditions.
[0013] In principle, so-called backward calculations are known from the prior art. Some publications on backward calculations whose methodology can at least partially be used are:
[0014] -Edsger W.Dijkstra: "Guarded commands, nondeterminacy, and formalderivation of programs". In: Communications of the ACM, Vol.18, No.8, 1975, pp. 453-457
[0015] -Patrick Cousot, Radhia Cousot, Francesco Logozzo: "Preconditioninference from intermittent assertions and application to contracts on collections". In: International Workshop on Verification, Model Checking, and Abstract Interpretation (VMCAI 2011). Springer, Berlin, Heidelberg, 2011. Pages 150-168
[0016] -Patrick Cousot, Radhia Cousot, Manuel Francesco Logozzo: "Automatic Inference of Necessary Preconditions". In: International Workshop onVerification, Model Checking, and Abstract Interpretation (VMCAI 2013). Springer, Berlin, Heidelberg, 2013. Pages 128-148
[0017] -Xavier Rival, Kwangkeun Yi: "Introduction to Static Analysis: AnAbstract Interpretation Perspective". Mit Press, 2020., pp. 147-152
[0018] The backward calculation of the abstract interpretation occurs, for example, on the control flow graph of the function F to be analyzed (similar to the forward calculation of the abstract interpretation). For the application described in the context of the present invention, the control flow graph can obtain the following node types for the backward calculation processing: entry instructions and exit instructions as the starting point and end point of F, numerical constants as integer and floating point constants, assignment instructions as variables, load instructions and storage instructions for reading from or writing to variables, binary operation instructions for calculating variables and / or constants with each other, comparison instructions for comparing variables and / or constants with each other, conditional branch instructions for providing branches based on the comparison results of the comparison.
[0019] Each step in the backward calculation in F can be performed from the abstract state X′ after the current node to the abstract state X before the node, where X can already be filled by the previous forward analysis and X′ is filled by the current backward analysis. The abstract state can contain the current value range of each variable, for example, represented by an interval:
[0020] - For a load instruction with variable v as source, the required value range of v can be added to the unchanged variable in X' to obtain X,
[0021] - For a store instruction with variable v as the target, the entire range of values for v can be contained in X,
[0022] - For binary operation instructions, the used operator can be inverted and in case of two variables, the other variable with its value from X is considered and the possible results are restricted to this range of values. For example, for the formula v=v1+v2, where in X′ v=[5,10] and in X v1=[0,10] and v2=[4,7], for X the refinement v1=[0,6] is found, while v2 remains unchanged,
[0023] - for comparison instructions, the value ranges X1′ and X2′ of the variables may be appropriately grouped together in comparison, depending on whether in the subsequent code neither case can be realized, the first case (condition is true), the second case (condition is false), or both cases (condition is unclear) can be realized in order to obtain X,
[0024] - For conditional branch instructions, the same actions as in the case of comparison instructions are set, but all variables in both branches can be combined.
[0025] The present invention can be applied to calculate the abstract input variable assignment V' applied at the beginning of the program by backward calculation from a given position P in the code based on the existing program code and the given abstract variable assignment V', so that after the program is executed, the assignment V' (or a subset of V') is realized at the position P. This enables the actual calculation of the prerequisites for the composite runtime error checking of the safety-related system.
[0026] In addition, optionally, it is stipulated that the prerequisite and at least one condition for the key variable assignment specify the parameters for the function, especially the conditions of the parameter range allowed.The key variable assignment can be understood as the assignment of the variable that may cause the function to be wrong or not work at all.If the function with such assignment is executed, this may cause a runtime error in other words.An example for this is to cause the assignment to be divided by 0 by the function.If the function divides the first variable by the second variable and the second variable has the value 0, a runtime error will occur.In this case, the key variable assignment will also be to assign the value 0 to the second variable.Another example is to loop through the array at the function.If the access to the array is carried out outside its effective range, this will also cause a runtime error.In this case, the key variable assignment will be the assignment that the access to the array exceeds the effective range.Since such key variable assignment is usually difficult to predict, the method according to the present invention can help to fully and automatically test the function, so as to check the parameters of the function according to potential key assignments, and avoid runtime errors.
[0027] Furthermore, within the scope of the invention, it can be provided that the forward analysis and the backward analysis are performed alternately and repeatedly in order to further restrict the possible values of at least one condition for the key variable assignment from the respectively obtained variable assignments. If necessary, these iterations of the forward and backward analysis can be performed until changes in the variable assignments (at these points) no longer occur.
[0028] Furthermore, it can be provided within the scope of the invention that the method comprises inlining, wherein within the function a call of at least one other function is replaced by the content of at least one called other function, preferably by the following steps:
[0029] - identifying in the program code of the function one or more function calls to one or more other functions,
[0030] - extract the code of other functions called,
[0031] - embed the extracted code directly into the program code of the called function instead of a function call,
[0032] - Remove function calls from the program code of the called function.
[0033] Furthermore, it may be possible that the method comprises unrolling a loop of the function, preferably by the following steps:
[0034] - identify loops in the program code of a function,
[0035] - determine the loop body of the loop,
[0036] - A new program code is generated by copying the loop body several times in succession.
[0037] Here, the loop can be a for loop, a while loop or a do-while loop. In general, a loop in a program code is a repeated instruction or instruction block that is executed a specific number of times. If the loop should be executed, for example, 10 times, the program repeats the loop block 10 times. When unrolling the loop, the loop block can be copied and inserted into the program code according to the number of iterations that the loop will experience. In other words, if a loop should be experienced 10 times, the instructions in the loop body can be copied into the program code 10 times and executed as sequential code.
[0038] It is further advantageous, within the scope of the present invention, to determine restrictions on possible values of variables at points in the program code of a function, in particular at program points, preferably at each program point, by forward analysis in the case of abstract interpretation, and preferably to determine unreachable program parts formed by loop unrolling, and / or variable assignments that describe all possible assignments that can be assumed at the respective point, and preferably to identify and eliminate non-executable code in the program code based on the determined restrictions and / or variable assignments.
[0039] In another possibility, it can be provided that the function is implemented as an embedded function, wherein the use of recursion and / or dynamic memory allocation is excluded. The method according to the invention can achieve that backward calculations with abstract interpretation can actually be implemented in an efficient and reliable manner. This is possible in particular in the following way, that the relevant results can be achieved on the real embedded code. It is preferred to fully utilize the specific characteristics of the embedded code, for example, recursion cannot occur, the loop usually only undergoes a very limited number of iterations, and the memory is not dynamically allocated. This makes it possible to actually calculate the prerequisites for the composite runtime error check of the safety-related system.
[0040] The function can be, for example, a safety-related function, for example for controlling a vehicle. It is thus possible to use an embedded system at a vehicle. The vehicle can be designed, for example, as a motor vehicle and / or a passenger vehicle and / or an autonomous vehicle. The vehicle can have a vehicle device, for example for providing an autonomous driving function and / or with a driver assistance system. The vehicle device can be implemented for at least partially automatically controlling and / or accelerating and / or braking and / or steering the vehicle. The embedded system or function can be part of the vehicle device.
[0041] A computer program, in particular a computer program product, is also a subject of the present invention, which comprises instructions which, when executed by a computer, cause the computer to perform the method according to the invention. Thus, the computer program according to the invention brings the same advantages as described in detail with reference to the method according to the invention.
[0042] A device for data processing which is designed to carry out the method according to the invention is also a subject of the invention. As a device, for example, a computer which executes a computer program according to the invention can be provided. The computer can have at least one processor for executing the computer program. A non-volatile data memory can also be provided, in which the computer program is stored and from which the computer program can be read out by the processor for execution.
[0043] A computer-readable storage medium can also be a subject of the present invention, which has a computer program according to the present invention and / or comprises instructions which, when executed by a computer, cause the computer to perform the method according to the present invention. The storage medium is, for example, configured as a data memory, such as a hard disk and / or a non-volatile memory and / or a memory card. The storage medium can, for example, be integrated into a computer.
[0044] Furthermore, the method according to the present invention can also be implemented as a computer-implemented method. BRIEF DESCRIPTION OF THE DRAWINGS
[0045] Further advantages, features and details of the invention are apparent from the following description, in which exemplary embodiments of the invention are described in detail with reference to the accompanying drawings. The features mentioned in the claims and in the description may be essential to the invention individually or in any combination. Among them:
[0046] Figure 1 Schematic visualizations of methods, devices, storage media and computer programs according to embodiments of the invention are shown.
[0047] Figure 2 Schematic diagram showing further details of an embodiment. DETAILED DESCRIPTION
[0048] exist Figure 1 Schematically shows a method 100, a device 10, a storage medium 15 and a computer program 20 according to an embodiment of the present invention. In this case, the method 100 is used for program analysis, in particular for runtime error checking in the case of safety-related programs in an embedded system 30. In this case, the method 100 can be used to determine prerequisites for error-free execution at at least one function 40 of the embedded system 30. For example, the prerequisites are conditions for initial variable assignments and / or for parameters of the function 40, in which runtime errors can be avoided.
[0049] According to the first method step 101, an abstract interpretation can be performed, in which the function 40 is analyzed forward in order to obtain possible variable assignments for a plurality of points and in particular all points of the function 40. In this case, the corresponding point can specify at least one instruction of the function 40. It can therefore be provided that the program includes a sequential list of instructions, which can be analyzed forward in a corresponding order in the corresponding steps of the forward calculation. These points can also be referred to as statements of the program or nodes or edges or program points.
[0050] According to the second method step 102, the respectively obtained possible variable assignments can be checked for the presence of at least one critical variable assignment. A critical variable assignment can be characterized in that it is suitable for causing a runtime error in the function 40. This is understood in particular to mean that a runtime error is caused by the assignment of a variable with a specific value.
[0051] According to the third method step 103, an abstract interpretation can be performed in such a way that the function 40 is analyzed backwards starting from the critical variable assignment, that is, starting from the runtime error, in order to determine at least one condition for the critical variable assignment. These can be used as a basis to determine the prerequisites for error-free execution.
[0052] exist Figure 2 In FIG. 1 , a method 100 according to an embodiment of the present invention is shown in a flow chart with further details. The method 100 shown enables backward calculations with relevant results, in particular by a combination of various techniques from compiler construction and correspondingly adapted abstract operations.
[0053] In the illustrated embodiment, the following combination of techniques can be used: Starting at 201, so-called inlining 202 is first performed, i.e., the called function 45 is replaced by its content. Subsequently, the program loop is expanded 203. A forward analysis 204 can then be performed, which is also referred to as "forward abstract interpretation". This step can provide restrictions on variable values to the corresponding program points and is particularly used to identify dead code. According to another step 205, the identified dead code can be eliminated. According to a backward analysis 206, a "backward abstract interpretation" can be performed using the adapted abstract operation while taking into account (i.e. restricting) the results of the forward analysis 204. The iteration of the forward and backward analysis is characterized by 207, and the iteration can be performed until (at any program point) no more variable assignments occur (termination at 208).
[0054] Another example is described below. In particular, given a program having functions and variable assignments with abstract values (e.g., intervals) at a specific point in the program code within a specific function F, backward computation can be performed as follows:
[0055] Firstly, the function called by function F can be used at the call site and thereby replace the call. The data flow into and out of the function with regard to parameters and return values can be adapted accordingly. The result is thus a function F' without a function call.
[0056] In addition, program loops (e.g. for, while, do-while) can be expanded. This means that the loop body including the check of the loop termination condition is replicated several times in succession. The number of repetitions can be chosen arbitrarily. However, the number should be chosen so high that all repetitions that actually occur are achieved. For example, the command:
[0057] while(x>0){...body...}
[0058] can be changed to:
[0059] if(x>0){...body...}
[0060] if(x>0){...body...}
[0061] if(x>0){...body...}
[0062] In addition, the abstract interpretation can be performed "forward", which is also called forward analysis. That is, the program is calculated step by step on the abstract domain. In this case, all instructions of the program are calculated until there is no longer any change at the variable assignment at the corresponding program point (so-called fixed point). The abstract domain can be selected so that it can only be changed in the direction of the so-called top element, so that termination is guaranteed. The result is a variable assignment at each point in the program, which respectively represents all possible values at this point.
[0063] The results of the abstract interpretation can be used to determine whether a specific program section, in particular a program section formed by loop unrolling, cannot be reached at all, for example because the loop termination condition is always met. This is often the case, so the unrolling is usually too much, because the upper limit of the loop is usually not known. The corresponding program section can therefore be removed.
[0064] The abstract interpretation (also called backward analysis) can then be performed "backwards". To this end, the operations of the abstract domain can be defined accordingly as the operations are reversed and thus calculated backwards. For example,
[0065] x=a+1
[0066] Can be based on
[0067] a=x-1
[0068] is reversed.
[0069] According to another example,
[0070] x=0
[0071] Can be based on
[0072] x = any
[0073] is reversed.
[0074] from
[0075] x=a / 4
[0076] For example,
[0077] a=x*4.
[0078] It should be noted here that, for example, in the case of interval abstraction and only positive numbers, the interval [x*4, x*4+3] is possible for a.
[0079] from
[0080] x=4 / a
[0081] For example, it becomes
[0082] a=4 / x.
[0083] Therefore, the division operator cannot simply be inverted.
[0084] If the error branch is unreachable,
[0085] if(a>0)
[0086] For example, it becomes
[0087] a>0.
[0088] If both branches are reachable, then the union of the two possible variable assignments can be formed (the "least upper bound").
[0089] Based on what we know about a and b at this point,
[0090] x=a*b
[0091] For example, it becomes
[0092] a=x / b or b=x / a.
[0093] The corresponding result (or multiple possible results) of the backward calculation step can then be compared with the possible abstract values at this point according to the forward analysis (i.e., an intersection ("greatest lower bound") is formed). If the intersection is empty, the corresponding situation is impossible.
[0094] Furthermore, forward and backward analysis can be performed alternately, wherein at each point in the program the results from the respective previous analysis (variable assignment) are used to restrict the possible values (intersection formation). This can be repeated until no more changes are obtained (ie at a so-called fixed point).
[0095] The above description of the embodiments describes the invention only in the scope of examples. Of course, the individual features of the embodiments can be freely combined with one another as long as it is technically reasonable, without departing from the scope of the invention.
Claims
1. A method (100) for program analysis in an embedded system (30) for determining prerequisites for error-free execution at at least one function (40) of the embedded system (30), the method comprising the following steps which are performed automatically: - performing (101) an abstract interpretation, wherein the function (40) is analyzed forwardly to obtain possible variable assignments for a plurality of points of the function (40), wherein respective points specify at least one instruction of the function (40), - possible variable assignments obtained in view of an existence check (102) of at least one critical variable assignment, said critical variable assignment being suitable for causing a runtime error of said function (40), - executing (103) the abstract interpretation, wherein the function (40) is analyzed backwards starting from the critical variable assignment in order to determine at least one condition for the critical variable assignment in order to determine therefrom a prerequisite for error-free execution.
2. The method (100) according to claim 1, characterized in that: A backward analysis is performed based on the obtained possible variable assignments, wherein for this purpose an automated backward calculation is performed starting from the key variable assignments, wherein the variable assignments obtained by the backward calculation are compared with the obtained possible variable assignments in order to exclude conditions that are impossible for at least one condition for determining the key variable assignments.
3. The method (100) according to any one of the preceding claims, characterized in that The precondition and the at least one condition for the key variable assignment each specify conditions for parameters of the function (40), in particular permissible parameter ranges.
4. The method (100) according to any one of the preceding claims, characterized in that The forward analysis and the backward analysis are performed alternately and repeatedly to further restrict possible values of at least one condition for the key variable assignment from the respectively obtained variable assignments.
5. The method (100) according to any one of the preceding claims, characterized in that The method (100) further comprises inlining, wherein a call of at least one other function (45) within the function (40) is replaced by the content of at least one called other function (45), preferably by the following steps: - identifying one or more function calls of one or more other functions (45) in the program code of said function (40), - extract the code of the other function (45) called, - embedding the extracted code directly into the program code of the called function (40) instead of a function call, - Remove function calls from the program code of the called function.
6. The method (100) according to any one of the preceding claims, characterized in that The method (100) further comprises unrolling a loop of the function (40), preferably by the following steps: - identifying a loop in the program code of said function (40), - determining the loop body of the loop, - generating a new program code by copying the loop body several times in succession, The loop is preferably a for loop, a while loop or a do-while loop.
7. The method (100) according to any one of the preceding claims, characterized in that In the case of abstract interpretation, restrictions on possible values of variables at points in the program code of the function (40), in particular at program points, preferably at each program point, are determined by forward analysis, and preferably unreachable program parts formed by loop unrolling and / or variable assignments are determined to indicate all possible assignments that can be taken at the corresponding point, and preferably non-executable code in the program code is identified and eliminated based on the determined restrictions and / or variable duplication.
8. The method (100) according to any one of the preceding claims, characterized in that The function (40) is implemented as an embedded function (40), wherein the use of recursion and / or dynamic memory allocation is excluded.
9. A computer program (20) comprising instructions which, when the computer program (20) is executed by a computer (10), cause the computer (10) to perform the method (100) according to any one of the preceding claims.
10. A device (10) for data processing, the device being configured to carry out the method (100) according to any one of claims 1 to 8.
11. A computer-readable storage medium (15), comprising instructions which, when executed by a computer (10), cause the computer to perform the steps of the method (100) according to any one of claims 1 to 8.
