Disclosure of Invention
In order to solve the above problems, the present invention provides a detection method, apparatus, device, and storage medium, which can determine a control operation for a target object from an instruction perspective to determine whether there is a shutdown operation for the target object that satisfies a preset rule, and the detection process is performed during program compilation without manual testing or consideration of detection time, so that detection efficiency and accuracy of a detection result can be improved without setting a test case.
In a first aspect, an embodiment of the present application provides a detection method, including:
determining an instruction to be detected;
acquiring at least one target execution path corresponding to the instruction to be detected;
determining an operation instruction set aiming at a target object, which is contained in a target execution path corresponding to the instruction to be detected, wherein the operation instruction set contains at least one control operation aiming at the target object;
judging whether a closing operation meeting a preset rule aiming at a target object exists in an operation instruction set included in a target execution path;
and determining whether the processing flow of the instruction to be detected is in compliance based on the judgment result.
The detection method and the detection device have the advantages that the scheme is used for detecting the instruction to be detected and the target execution path required to be operated by the instruction to be detected, so that the detection process can cover all scenes to the maximum extent, and the detection process does not need to be provided with a test case, so that compared with the existing detection mode through the test case, the detection method and the detection device do not need manual testing, the detection efficiency is improved, and meanwhile, a foundation is laid for improving the accuracy of a detection result.
Moreover, because the scheme of the application is carried out aiming at the target execution path of the instruction to be detected, the detection process can be carried out during the program compiling period without being carried out under the running environment, so that the problem of detection lag of the existing detection mode is solved, meanwhile, the detection process can be carried out during the compiling period, so that the foundation is laid for rapidly solving the problem, improving the program stability, and further laying the foundation for avoiding code resource leakage caused by the problem that the detection lag is not solved in time.
In a specific embodiment, the determining whether a closing operation meeting a preset rule exists for a target object in an operation instruction set included in a target execution path includes:
and judging whether a closing operation which corresponds to the opening operation and meets a preset rule exists after the opening operation aiming at the target object exists in an operation instruction set contained in the target execution path.
The method and the device have the advantages that the detection efficiency is improved to the maximum extent, the operation steps after the starting operation for the target object are detected in the detection process of the target execution path, the foundation is laid for improving the detection efficiency to the maximum extent, meanwhile, the foundation is laid for rapidly solving the problem and improving the program stability, and the foundation is laid for avoiding code resource leakage caused by the fact that the problem is not solved in time due to detection lag.
In a specific embodiment, the method further comprises:
determining at least one function in the program to be detected;
acquiring an instruction corresponding to at least one function;
and taking at least part of instructions corresponding to at least one function as instructions to be detected.
In order to meet the actual requirements, expand the application range and enrich the application scenes, the application scheme can be expanded into instructions corresponding to functions in the detection program, and further the application scheme can be applied to development scenes, such as program development, so that the application program is enriched, a foundation is laid for improving the development efficiency, and a foundation is laid for engineering application.
In a specific embodiment, the taking at least a part of the instructions corresponding to the at least one function as the instructions to be detected includes:
and judging whether the instruction corresponding to the function comprises a control operation aiming at the target object, if so, taking the instruction comprising the control operation aiming at the target object as a to-be-detected instruction.
In a specific embodiment, the method further comprises:
setting a preset operation instruction type;
and taking an instruction matched with a preset operation instruction type in the instructions corresponding to the function as an instruction to be detected, so that the instruction to be detected comprises control operation aiming at the target object.
In the scheme, for fitting an actual scene, the detection efficiency problem under the specific scene is further improved, the operation instruction type can be preset, namely the operation instruction type is preset, and then the instruction matched with the preset operation instruction type is detected, so that the detection efficiency under the specific scene is improved, and a foundation is laid for engineering application.
In a specific embodiment, the obtaining at least one target execution path corresponding to the instruction to be detected includes:
taking an execution path required to be operated by an instruction to be detected as a target execution path corresponding to the instruction to be detected; and the number of the first and second groups,
and taking an execution path to which the instruction to be detected jumps based on the jump instruction in the running process as a target execution path corresponding to the instruction to be detected.
The method and the device solve the problem of detection efficiency under a specific scene to the maximum extent, and cover all practical scenes to the maximum extent, the scheme of the application not only takes the execution path required to be operated by the instruction to be detected as the target execution path, but also takes the execution path jumped to by the instruction to be detected based on the jump instruction in the operation process as the target execution path, and further detects all target execution paths, so that all practical scenes are covered to the maximum extent, a foundation is laid for improving the detection efficiency to the maximum extent, a foundation is laid for solving the problem quickly and improving the program stability, and a foundation is laid for avoiding code resource leakage caused by the problem that detection lags and is not solved in time.
In a second aspect, an embodiment of the present application provides a detection apparatus, including:
the first processing unit is used for determining the instruction to be detected; acquiring at least one target execution path corresponding to the instruction to be detected; determining an operation instruction set aiming at a target object, which is contained in a target execution path corresponding to the instruction to be detected, wherein the operation instruction set contains at least one control operation aiming at the target object;
the second processing unit is used for judging whether a closing operation meeting a preset rule for the target object exists in an operation instruction set included in the target execution path; and determining whether the processing flow of the instruction to be detected is in compliance based on the judgment result.
In a specific embodiment, the second processing unit is further configured to determine whether a closing operation corresponding to the opening operation and meeting a preset rule exists after the opening operation for the target object exists in an operation instruction set included in the target execution path.
In an embodiment, the first processing unit is further configured to:
determining at least one function in the program to be detected;
acquiring an instruction corresponding to at least one function;
and taking at least part of instructions corresponding to at least one function as instructions to be detected.
In an embodiment, the first processing unit is further configured to: and judging whether the instruction corresponding to the function comprises a control operation aiming at the target object, if so, taking the instruction comprising the control operation aiming at the target object as a to-be-detected instruction.
In an embodiment, the first processing unit is further configured to: setting a preset operation instruction type;
and taking an instruction matched with a preset operation instruction type in the instructions corresponding to the function as an instruction to be detected, so that the instruction to be detected comprises control operation aiming at the target object.
In an embodiment, the first processing unit is further configured to:
taking an execution path required to be operated by an instruction to be detected as a target execution path corresponding to the instruction to be detected; and the number of the first and second groups,
and taking an execution path to which the instruction to be detected jumps based on the jump instruction in the running process as a target execution path corresponding to the instruction to be detected.
In a third aspect, an embodiment of the present application provides a detection apparatus, including:
one or more processors;
a memory communicatively coupled to the one or more processors;
one or more applications, wherein the one or more applications are stored in the memory and configured to be executed by the one or more processors, the one or more programs configured to perform the methods described above.
In a fourth aspect, the present application provides a computer-readable storage medium, which stores a computer program, and when the computer program is executed by a processor, the computer program implements the method described above.
Therefore, the scheme of the application detects the instruction to be detected and the target execution path required to be operated by the instruction to be detected, so that the detection flow can be guaranteed to cover all scenes to the maximum extent, and the detection flow does not need to be provided with a test case.
Moreover, because the scheme of the application is carried out aiming at the target execution path of the instruction to be detected, the detection process can be carried out during the program compiling period without being carried out under the running environment, so that the problem of detection lag of the existing detection mode is solved, meanwhile, the detection process can be carried out during the compiling period, so that the foundation is laid for rapidly solving the problem, improving the program stability, and further laying the foundation for avoiding code resource leakage caused by the problem that the detection lag is not solved in time.
Detailed Description
In order to make the objects, technical solutions and advantages of the present application more apparent, the present application is described in further detail below with reference to the accompanying drawings and embodiments. It should be understood that the specific embodiments described herein are merely illustrative of the present application and are not intended to limit the present application.
In some of the flows described in the specification and claims of the present application and in the above-described figures, a number of operations are included that occur in a particular order, but it should be clearly understood that the flows may include more or less operations, and that the operations may be performed sequentially or in parallel.
For the problem in the prior art, in the present application, during the compiling period, the instruction (i.e., the instruction to be detected) in all the methods (i.e., the functions in the present application) is scanned, and all the possible execution paths (e.g., the target execution path in the present application) corresponding to the instruction are traversed, so as to find whether an exception handling process exists, that is, whether a situation that a clear object is not closed exists is found, and a report is generated accordingly, so as to solve the problem in time.
Specifically, fig. 1 is a schematic flow chart of an implementation of the detection method according to the embodiment of the present invention, and as shown in fig. 1, the method includes:
step 101: and determining the instruction to be detected.
Here, in order to enrich the application scenario of the scheme of the present application, the instruction to be detected may specifically be an instruction corresponding to a function in the program to be detected, so as to implement a detection process of the program to be detected, and specifically, determine at least one function in the program to be detected; acquiring an instruction corresponding to at least one function; and taking at least part of instructions corresponding to at least one function as instructions to be detected. For example, in a specific scenario, to detect all scenarios of a program to be detected, all functions of the program to be detected are obtained, all instructions corresponding to all functions are obtained, and then all instructions corresponding to all functions are used as instructions to be detected, or, in order to improve detection efficiency, only instructions of a part of instructions (for example, instructions that satisfy some rules) in all instructions corresponding to all functions may be used as instructions to be detected. Here, it should be noted that the rule of instruction filtering may be set according to the actual scene requirement in the development environment, and the embodiment of the present application does not limit this.
In a specific example, the instruction to be detected may be selected by using the following filtering rule, specifically, it is determined whether the instruction corresponding to the function includes the control operation for the target object, and if so, the instruction including the control operation for the target object is used as the instruction to be detected. That is, in an example, an instruction of a control operation for a target object may be included in all instructions corresponding to all functions in a program to be detected as an instruction to be detected.
In another specific example, the instruction to be detected may also be determined in the following manner, specifically, a preset operation instruction type is set; and taking an instruction matched with a preset operation instruction type in the instructions corresponding to the function as an instruction to be detected, so that the instruction to be detected comprises control operation aiming at the target object. Here, in order to fit an actual scene and further improve the detection efficiency problem in a specific scene, the present example presets an operation instruction type, that is, a preset operation instruction type, which can represent an operation type for operation control of a target object, and then detects an instruction matched with the preset operation instruction type, so that the detection efficiency in the specific scene is improved, and a foundation is laid for engineering application.
Here, it should be noted that in an actual scenario, the number of preset operation instruction types may be set according to actual requirements, for example, 1 or 7, and the like, and this is not limited in the embodiment of the present application.
Here, in practical applications, the target object may be specifically a Closeable object, and of course, may also be another object, which is not limited in this embodiment of the present application.
Step 102: and acquiring at least one target execution path corresponding to the instruction to be detected.
In practical application, because other instructions may be called in the operation process of the instruction, so that the execution path where the instruction operates jumps, that is, one instruction may correspond to one execution path and may also correspond to more than one execution path, for this situation, in order to cover all scenarios to the maximum extent, the target execution path in the present application scheme also includes the execution path after jumping, that is, includes all execution paths involved in the operation of the instruction to be detected, and specifically, the execution path where the instruction to be detected needs to operate is taken as the target execution path corresponding to the instruction to be detected; and taking an execution path to which the instruction to be detected jumps based on the jump instruction in the running process as a target execution path corresponding to the instruction to be detected. Therefore, the accuracy of the detection result is improved to the maximum extent.
Step 103: and determining an operation instruction set aiming at the target object, which is contained in a target execution path corresponding to the instruction to be detected, wherein the operation instruction set contains at least one control operation aiming at the target object.
For example, in an example, in order to detect whether a closing operation for the Closeable object exists in the target execution path executed by the instruction to be detected, all control operations for the Closeable object in the target execution path need to be acquired, and then whether the closing operation for the Closeable object that satisfies the rule is executed is determined based on the acquired control operations.
Step 104: and judging whether a closing operation meeting a preset rule exists in an operation instruction set included in the target execution path or not.
Here, to maximize the detection efficiency, in the detection process of the target execution path, an operation step after the opening operation on the target object may be detected, and specifically, it is determined whether a closing operation corresponding to the opening operation and meeting a preset rule exists after the opening operation on the target object exists in the operation instruction set included in the target execution path. For example, in order to detect whether a closing operation for the Closeable object exists in a target execution path in which the instruction to be detected runs, all control operations (i.e., an operation instruction set) for the Closeable object in the target execution path need to be acquired, and after detecting that a specific control operation in the operation instruction set is an opening operation for the target object, it is further detected whether a closing operation corresponding to the opening operation and meeting a preset rule exists, so that the detection efficiency is improved.
Here, because the actual scenes are different and the business requirements are also different, the closing operation modes can be different in different scenes, and therefore the closing operation meeting the scene requirements (namely, the preset rule) can be set aiming at the scenes, so that the applicability of the scheme of the application is improved, and further a foundation is laid for the subsequent engineering application.
Step 105: and determining whether the processing flow of the instruction to be detected is in compliance based on the judgment result.
Here, when the judgment result indicates that the operation instruction set included in the target execution path has a closing operation meeting the preset rule for the target object, or when the judgment result indicates that the operation instruction set included in the target execution path has an opening operation for the target object, and then has a closing operation meeting the preset rule corresponding to the opening operation, it may be determined that the processing flow of the instruction to be detected is in compliance, otherwise, it may be determined that the processing flow of the instruction to be detected is not in compliance, and an out-of-compliance result is output, so that the research and development staff may modify in time.
Therefore, the scheme of the application detects the instruction to be detected and the target execution path required to be operated by the instruction to be detected, so that the detection flow can be guaranteed to cover all scenes to the maximum extent, and the detection flow does not need to be provided with a test case.
Moreover, because the scheme of the application is carried out aiming at the target execution path of the instruction to be detected, the detection process can be carried out during the program compiling period without being carried out under the running environment, so that the problem of detection lag of the existing detection mode is solved, meanwhile, the detection process can be carried out during the compiling period, so that the foundation is laid for rapidly solving the problem, improving the program stability, and further laying the foundation for avoiding code resource leakage caused by the problem that the detection lag is not solved in time.
The following further details the embodiments of the present invention with reference to specific application scenarios, and the specific steps are shown in fig. 2 and include:
step 201: all instructions in the method (i.e. function) of the program to be detected are determined. For example, a methodview callback is read, where each method corresponds to one methodview, and thus, all instructions corresponding to the method are obtained by using the methodview.
Step 202: it is determined whether there is an operation for the Closeable object in all instructions. Here, if the operation of the Closeable object does not exist in the instruction, the scanning can be terminated quickly, and thus, the scanning speed is increased.
In practical application, in order to improve the detection efficiency, a flag type (that is, a preset operation instruction type) may be preset, all instructions of the method are traversed, and each instruction is marked to screen out a target instruction matched with the preset flag type.
Here, in practical applications, the mark type may be specifically the following types:
return CloseableType (characterizes call back Closeable object); NewType (characterizes the creation of Closeable objects); NullType (a Null judge instruction is characterized and can be used for filtering a judge Null path aiming at a Closeable object); NotNullType (a NotNull decision instruction is characterized for filtering a decision null path for Closeable); ParameterType (parameter passing characterizing Closeable objects); ExceptionType (characterizing normal method calls, but with exception identification); return type (characterizes the Closeable object as a return value).
Step 203: analyzing the execution paths of all target instructions, for example, reading callbacks for all target instructions by using the FlowAnalyzer of Asm, and analyzing all the execution paths of the target instructions.
Specifically, all instructions executed in each execution path corresponding to the target instruction are analyzed, for example, the target instruction is return closeable type or NewType, and the operation record is an open operation, at this time, if the position is recorded as an open operation, it is determined that no close operation is performed (because there is no close in the loop body); for target instructions of NullType and NotNullType, discarding operation is carried out, and the Closeable object is empty and does not need to be closed; for the target instruction being CloseType, a close operation is assumed to be present; for the target instruction of ParameterType or ReturnType, judging whether closing operation exists according to business logic requirements according to conditions, and removing corresponding opening operation when closing operation so as to represent normal closing; and if the target instruction is the ExceptionType, judging whether the corresponding catch code exists and can be caught or not, if the target instruction is not the ExceptionType, considering the target instruction as abnormal exit, and determining that the closing operation is not performed.
Step 204: and outputting the analysis result to a result directory, and finishing the analysis so as to be convenient for modification based on the analysis result.
In this way, by analyzing all possibilities for operating on the Closeable object, possible unclosed execution paths are discovered, thereby facilitating the discovery of potential code resource leakage issues during compilation.
An embodiment of the present application further provides a detection apparatus, as shown in fig. 3, the apparatus includes:
the first processing unit 31 is used for determining the instruction to be detected; acquiring at least one target execution path corresponding to the instruction to be detected; determining an operation instruction set aiming at a target object, which is contained in a target execution path corresponding to the instruction to be detected, wherein the operation instruction set contains at least one control operation aiming at the target object;
the second processing unit 32 is configured to determine whether a closing operation meeting a preset rule exists for the target object in an operation instruction set included in the target execution path; and determining whether the processing flow of the instruction to be detected is in compliance based on the judgment result.
In a specific embodiment, the second processing unit 32 is further configured to determine whether a closing operation corresponding to the opening operation and meeting a preset rule exists after the opening operation for the target object exists in the operation instruction set included in the target execution path.
In an embodiment, the first processing unit 31 is further configured to:
determining at least one function in the program to be detected;
acquiring an instruction corresponding to at least one function;
and taking at least part of instructions corresponding to at least one function as instructions to be detected.
In an embodiment, the first processing unit 31 is further configured to: and judging whether the instruction corresponding to the function comprises a control operation aiming at the target object, if so, taking the instruction comprising the control operation aiming at the target object as a to-be-detected instruction.
In an embodiment, the first processing unit 31 is further configured to: setting a preset operation instruction type;
and taking an instruction matched with a preset operation instruction type in the instructions corresponding to the function as an instruction to be detected, so that the instruction to be detected comprises control operation aiming at the target object.
In an embodiment, the first processing unit 31 is further configured to:
taking an execution path required to be operated by an instruction to be detected as a target execution path corresponding to the instruction to be detected; and the number of the first and second groups,
and taking an execution path to which the instruction to be detected jumps based on the jump instruction in the running process as a target execution path corresponding to the instruction to be detected.
Here, it should be noted that: the descriptions of the embodiments of the apparatus are similar to the descriptions of the methods, and have the same advantages as the embodiments of the methods, and therefore are not repeated herein. For technical details that are not disclosed in the embodiments of the apparatus of the present invention, those skilled in the art should refer to the description of the embodiments of the method of the present invention to understand, and for brevity, will not be described again here.
An embodiment of the present application further provides a detection device, including: one or more processors; a memory communicatively coupled to the one or more processors; one or more application programs; wherein the one or more applications are stored in the memory and configured to be executed by the one or more processors, the one or more programs configured to perform the method described above.
In a specific example, the detection device according to the embodiment of the present application may be embodied as a structure as shown in fig. 4, and the detection device at least includes a processor 41, a storage medium 42, and at least one external communication interface 43; the processor 41, the storage medium 42, and the external communication interface 43 are all connected by a bus 44. The processor 41 may be a microprocessor, a central processing unit, a digital signal processor, or a programmable logic array, etc. electronic components with processing functions. The storage medium has stored therein computer executable code capable of performing the method of any of the above embodiments. In practical applications, the first processing unit 31 and the second processing unit 32 can be implemented by the processor 41.
Here, it should be noted that: the above description of the embodiment of the detection device is similar to the above description of the method, and has the same beneficial effects as the embodiment of the method, and therefore, the description thereof is omitted. For technical details that are not disclosed in the embodiments of the detection apparatus of the present invention, those skilled in the art should refer to the description of the embodiments of the method of the present invention to understand that, for the sake of brevity, detailed description is omitted here.
Embodiments of the present application also provide a computer-readable storage medium, which stores a computer program, and when the program is executed by a processor, the computer program implements the method described above.
A computer-readable storage medium can be any means that can contain, store, communicate, propagate, or transport the program for use by or in connection with the instruction execution system, apparatus, or device. More specific examples (a non-exhaustive list) of the computer readable storage medium would include the following: an electrical connection (electronic device) having one or more wires, a portable computer diskette (magnetic device), a Random Access Memory (RAM), a read-only memory (ROM), an erasable programmable read-only memory (EPROM or flash memory), an optical fiber device, and a portable read-only memory (CDROM). Further, the computer-readable storage medium may even be paper or another suitable medium upon which the program is printed, as the program can be electronically captured, via for instance optical scanning of the paper or other medium, then compiled, interpreted or otherwise processed in a suitable manner if necessary, and then stored in a computer memory.
It should be understood that all or part of the steps carried by the method for implementing the above embodiments can be implemented by hardware related to instructions of a program, which can be stored in a computer readable storage medium, and the program includes one or a combination of the steps of the method embodiments when the program is executed.
In addition, functional units in the embodiments of the present invention may be integrated into one processing module, or each unit may exist alone physically, or two or more units are integrated into one module. The integrated module can be realized in a hardware mode, and can also be realized in a software functional module mode. The integrated module, if implemented in the form of a software functional module and sold or used as a separate product, may also be stored in a computer readable storage medium. The storage medium may be a read-only memory, a magnetic or optical disk, or the like.
The embodiments described above are only a part of the embodiments of the present invention, and not all of them. All other embodiments, which can be derived by a person skilled in the art from the embodiments given herein without making any creative effort, shall fall within the protection scope of the present invention.