Rule file generation method, electronic device, and storage medium
By automatically generating rule files, the problem of excessively long rule file writing cycles is solved, the efficiency of rule file generation is improved, and the development workflow is simplified.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2023-06-27
- Publication Date
- 2026-04-07
AI Technical Summary
In existing technologies, the writing cycle of rule files is too long, resulting in low efficiency in generating rule files, and developers need to spend a lot of time studying syntax knowledge.
By acquiring code information and rule file template information, a rule file template is selected from the rule template library, and the code information is written into the template to generate the rule file. This avoids manually writing rule files and improves efficiency by utilizing semantic patching language and rule file generation methods.
It enables the automatic generation of rule files, improving the efficiency of rule file generation and thus improving code scanning efficiency.
Smart Images

Figure CN119201087B_ABST
Abstract
Description
TECHNICAL FIELD
[0001] The present application belongs to the field of computer security, and particularly relates to a rule file generation method, an electronic device and a storage medium. BACKGROUND
[0002] Usually, source code defects are introduced in the coding stage, and most of the source code defects are not found in the coding stage, but are found in the testing stage, and the cost of repairing the source code defects is higher over time. And it usually takes a lot of time for the developer to troubleshoot the source code defects. Based on this situation, the developer uses a static scanning tool to replace the manual troubleshooting of the defect code. The static scanning tool scans the defect code by matching the defect code through a rule file.
[0003] However, the rule file is usually complex, and the developer needs to manually write it and spend a lot of time studying syntax knowledge to write the corresponding rule file, which leads to a long period of writing the rule file and low efficiency of generating the rule file. SUMMARY
[0004] The embodiments of the present application provide a rule file generation method, an electronic device and a storage medium, which can solve the problem of long period of manually writing the rule file and low efficiency of generating the rule file.
[0005] In a first aspect, the embodiments of the present application provide a rule file generation method, which comprises: obtaining first code information and information of a rule file template corresponding to the first code information; selecting a rule file template from a rule template library according to the information of the rule file template; and writing the first code information into the rule file template to generate a rule file, the rule file being used to find target code matching the first code information in to-be-scanned code.
[0006] In a second aspect, the embodiments of the present application provide a rule file generation device, which comprises: an obtaining module configured to obtain first code information and information of a rule file template corresponding to the first code information; a selecting module configured to select a rule file template from a rule template library according to the information of the rule file template; and an executing module configured to write the first code information into the rule file template to generate a rule file, the rule file being used to find target code matching the first code information in to-be-scanned code.
[0007] In a third aspect, the embodiments of the present application provide an electronic device, which comprises a processor, a memory and a program or instruction stored on the memory and executable on the processor, and the program or instruction is executed by the processor to implement the steps of the method according to the first aspect.
[0008] In a fourth aspect, an embodiment of the present application provides a readable storage medium, and the readable storage medium stores a program or instructions, and the program or instructions are executed by a processor to implement steps of the method in the first aspect.
[0009] In the embodiment of the present application, the first code information and the information of the rule file template corresponding to the first code information are acquired, the rule file template is selected from the rule template library according to the information of the rule file template, and the first code information is written into the rule file template to generate a rule file. The rule file is used to find target code matching the first code information in the to-be-scanned code. The rule file template is selected from the rule template library, and the rule file is automatically generated according to the first code information, which avoids manually writing a corresponding rule file for each code information, improves the generation efficiency of the rule file, and further improves the code scanning efficiency. BRIEF DESCRIPTION OF DRAWINGS
[0010] Figure 1 FIG. 1 is a flow diagram of a rule file generation method provided by an embodiment of the present application;
[0011] Figure 2 FIG. 2 shows a rule file template and a rule file construction diagram provided by an embodiment of the present application;
[0012] Figure 3 FIG. 3 is an electronic device framework diagram provided by an embodiment of the present application;
[0013] Figure 4 FIG. 4 is a rule generator workflow diagram provided by an embodiment of the present application;
[0014] Figure 5 FIG. 5 is a rule file generation device structure diagram provided by an embodiment of the present application;
[0015] Figure 6 FIG. 6 is a structure diagram of another electronic device of the system provided by an embodiment of the present application. DETAILED DESCRIPTION
[0016] The technical solutions in the embodiments of the present application will be clearly and completely described below with reference to the drawings in the embodiments of the present application. Obviously, the described embodiments are some of the embodiments of the present application, but not all the embodiments of the present application. Based on the embodiments in the present application, all other embodiments obtained by those of ordinary skill in the art without creative work fall within the scope of the present application.
[0017] The terms "first," "second," etc., used in the specification and claims of this application are used to distinguish similar objects and not to describe a specific order or sequence. It should be understood that such use of data can be interchanged where appropriate so that embodiments of this application can be implemented in orders other than those illustrated or described herein, and the objects distinguished by "first," "second," etc., are generally of the same class and the number of objects is not limited; for example, a first object can be one or more. Furthermore, in the specification and claims, "and / or" indicates at least one of the connected objects, and the character " / " generally indicates that the preceding and following objects are in an "or" relationship.
[0018] The following description, in conjunction with the accompanying drawings, details the method for generating rule files, the electronic device, and the storage medium provided in this application through specific embodiments and application scenarios.
[0019] Figure 1 This illustration shows a method for generating a rule file according to an embodiment of the present invention. This method can be executed by an electronic device, which may include a server and / or a terminal device. In other words, the method can be executed by software or hardware installed on the electronic device, and the method includes the following steps:
[0020] Step 102: Obtain the information of the first code and the rule file template corresponding to the first code.
[0021] In this embodiment, user-input parameters can be received and parsed to obtain first code information. This first code information can be a function name or parameter name, etc., and is not limited herein. Simultaneously, information about the rule file template corresponding to the first code information can also be obtained. This rule file template information is used to select a rule file template from a rule template library. Therefore, any information that can be used to select a rule file template corresponding to the first code information from the rule template library can be used as the rule file template information, and is not specifically limited herein.
[0022] Step 104: Select a rule file template from the rule template library based on the information in the rule file template.
[0023] Specifically, after obtaining the information of the rule file template, a rule file template can be selected from the rule template library based on the information of the rule file template. The rule template library contains a large number of different types of rule file templates.
[0024] Step 106: Write the first code information into the rule file template to generate the rule file.
[0025] Specifically, after obtaining the rule file template, the first code information can be written into the rule file template to generate a rule file. This rule file is used to find target code in the code to be scanned that matches the first code information. That is, by using the rule file with a static scanning tool, the target code that matches the first code information can be found in the code to be scanned. This target code may be defective code.
[0026] The rule file generation method provided in this application embodiment obtains first code information and the information of the rule file template corresponding to the first code information; selects a rule file template from the rule template library according to the information of the rule file template; writes the first code information into the rule file template to generate a rule file. The rule file is used to find target code in the code to be scanned that matches the first code information. By selecting a rule file template from the rule template library and automatically generating a rule file according to the first code information, the manual writing of corresponding rule files for each piece of code information is avoided, thereby improving the generation efficiency of rule files and thus improving the code scanning efficiency.
[0027] In one implementation, after writing the first code information into the rule file template and generating the rule file, the method further includes:
[0028] Based on the rule file, if the target code is found in the code to be scanned, the target code information corresponding to the target code is output.
[0029] Specifically, after generating the rule file, a static scanning tool can use this rule file to find the target code corresponding to the first code information in the code to be scanned. Furthermore, if the target code is found in the code to be scanned, the rule file can output the corresponding target code information. This target code information can include the target code and any related information; no specific limitations are imposed on the target code information here. In this way, by using the rule file to output the corresponding target code information when the target code is found in the code to be scanned, developers can gain a comprehensive understanding of the target code information and, based on this information, perform a comprehensive analysis and processing of the target code.
[0030] In one implementation, before obtaining the first code information and the information of the rule file template corresponding to the first code information, the method further includes:
[0031] The source code with defects is analyzed to determine the second code information, which is of the same type as the first code information. The target tracking point is determined based on the second code information and is used to write the first code information. A rule file template is generated using a semantic patching language, and the target tracking point is set in the rule file template.
[0032] Specifically, before obtaining the first code information and the corresponding rule file template, it is necessary to analyze and summarize the source code with defects, analyze the logic of the defective source code, and determine the second code information of the defective source code. This second code information has the same type as the first code information. For example, if the second code information is the function name of the defective code, then the first code information is also the function name of the defective code. Based on the second code information, a target tracking point can be abstracted. This target tracking point is used to write the first code information. A rule file template is generated by combining the target tracking point with the Semantic Patch Language (SmPL). The target tracking point is set in the rule file template. Thus, when generating the rule file by combining the first code information and the rule file template, the first code information can be written into this target tracking point to generate the rule file.
[0033] In this embodiment, by analyzing the defective source code, second code information is determined, and target tracking points are determined based on the second code information. A rule file template is generated using semantic patching language, and the target tracking points are set in the rule file template. The defective source code is logically abstracted to generate corresponding rule file templates, which are stored in a rule template library. A large number of rule file templates can be automatically generated for different code information, and the learning threshold of the SmPL language is reduced. When developers need to use rule file templates, they only need to call the rule file templates from the rule template library without having to study complex SmPL syntax, thus improving the efficiency of rule file generation.
[0034] In one implementation, the first code information is written into a rule file template to generate a rule file, including:
[0035] Write the first code information into the target tracking point in the rule file template to generate the rule file.
[0036] Specifically, the method for generating rule files based on the first code information and the rule file template is to write the first code information into the target tracking points in the rule file template, thereby generating the rule file. This avoids manually writing corresponding rule files for each piece of code information, improving the efficiency of rule file generation. This rule file can then be used to find target code matching the first code information from the code to be scanned, thus improving code scanning efficiency.
[0037] In one implementation, the rule file template includes a parameter declaration area and a code matching area. The parameter declaration area is used to define the parameters of the semantic patching language, and the code matching area includes the target tracking points.
[0038] Specifically, such as Figure 2The rule file template and rule file construction diagram shown are illustrated. The rule file template includes a parameter declaration area and a code matching area. The parameter declaration area is used to define the parameters of the SmPL language, and the code matching area is used to place the target tracking points. This area is the code representation of the abstract logic. The first code information obtained is written into this area. After the first code information is written into the code matching area, the rule file is generated. This rule file also includes a parameter declaration area and a code matching area.
[0039] In one implementation, the rule file template includes a print output area. Before outputting the target code information corresponding to the target code, the method further includes:
[0040] The alarm information is printed out in the print output area. The alarm information is used to indicate that the target code has been found.
[0041] Specifically, the rule templates in the rule template library are divided into four categories: patch, report, context, and org. Among them, the patch and context mode rule file templates do not have a print output area, while the report and org mode rule file templates include an output print area, such as... Figure 2 As shown, the rule file template includes an output print area, and the rule file generated based on the rule file template also naturally includes an output print area. This output print area is used to print alarm information from the output area. The alarm information is used to indicate that the target code has been found, so as to remind developers to find the target code that matches the first code information.
[0042] In one implementation, obtaining the information of the first code information and the rule file template corresponding to the first code information includes:
[0043] Receive command-line arguments to instruct the generation of a rule file; obtain first code information, the storage path of the rule file template, and the file name of the rule file template from the command-line arguments; select a rule file template from the rule template library based on the rule file template information, including: selecting a rule file template from the rule template library based on the storage path and file name.
[0044] Specifically, when scanning code is required, the system can receive command-line arguments from developers to instruct the generation of rule files. These arguments include initial code information and rule file template information. Parsing these arguments yields the initial code information, the storage path of the rule file template, and the filename of the rule file template. Based on these filenames, a rule file template can be selected from the rule template library. For example, the command-line argument `python main.py -s dev_kfree_skb_irq -rttemplete / function.cocci` shows `dev_kfree_skb_irq` as the initial code information, `templete / function.cocci` as the storage path of the rule file template, and `function.cocci` as the filename of the rule file template. In this way, by receiving command-line parameters that indicate the generation of rule files, and obtaining the first code information, the storage path of the rule file template, and the file name of the rule file template from the command-line parameters, a rule file template can be selected from the rule template library based on the storage path and file name, and then a rule file can be generated. Code scanning can be performed using this rule file, avoiding the need to manually write corresponding rule files for each piece of code information, improving the efficiency of rule file generation, and thus improving code scanning efficiency.
[0045] In one implementation, the first code information includes the function name and / or parameter name corresponding to the target code.
[0046] Specifically, the first code information can be obtained from parsing command-line arguments. The first code information can be the function name and / or parameter name corresponding to the target code, as shown in the following command-line argument: python main.py -s dev_kfree_skb_irq -rt template / function.cocci, where dev_kfree_skb_irq is the function name, and this function name is the first code information.
[0047] In one implementation, the target code is defective code, and the output of the target code information includes at least one of the following:
[0048] Output the location of the target code in the code to be scanned; output the target code and its context code, as well as the line of code where the output target code is located, marked by the first marker; output the target code, its context code, and the corresponding replacement code, marked by the second marker and the third marker, where the replacement code is marked. The replacement code is a pre-set rule file template and is used to replace the target code.
[0049] Specifically, the target code is usually defective code. The templates in the rule template library are mainly divided into four categories: patch, report, context, and org. If the rule file template is in report or org mode, then when scanning the code to be scanned using the rule file generated based on that template, the location of the target code within the scanned code will be output. As an example, the following is a rule template in report mode:
[0050] virtual report / / report mode
[0051] @r@
[0052] position p; / / SmPL related parameters, used to bind the position of the matching source code.
[0053] @@
[0054] [function]( ...);@p / / Target tracking point [function]
[0055] @script:python depends on report@
[0056] p< <r.p;
[0057] @@
[0058] msg = "[Zeal Robot] [function]has been discarded" / / This is an alert message displayed after a static code scan tool detects the target code.
[0059] coccilib.report.print_report(msg, p[0]) / / Calls the function to print the output information.
[0060] Select a rule file template to generate a rule file:
[0061] python main.py -s dev_kfree_skb_irq -rt templete / function.cocci;
[0062] `main.py` is the rule generator, and `templete / ` is the template library, containing templates. `-s` specifies code information, and `-rt` specifies report mode. The generated rule file is as follows:
[0063] virtual report
[0064] @r@
[0065] position p;
[0066] @@
[0067] dev_kfree_skb_irq(...);@p
[0068] @script:python depends on report@
[0069] p< <r.p;
[0070] @@
[0071] msg = "[Zeal Robot] dev_kfree_skb_irq has been discarded"
[0072] coccilib.report.print_report(msg,p[0]);
[0073] The dev_kfree_skb_irq code information is written to the instrumentation points in the logical matching area, and finally a rule file dev_kfree_skb_irq.cocci is generated. The purpose of this rule file is to discover the dev_kfree_skb_irq function in the code, report the location of the function, and finally allow the developer to decide whether to modify it.
[0074] The `dev_kfree_skb_irq` function is unsafe and its direct use is not recommended. The rule file in the report mode here only queries and matches the `dev_kfree_skb_irq` function appearing in the code to be scanned, and does not provide methods for modification. The specific modification method should be determined by the source code developer. Generally speaking, developers should use the `dev_consume_skb_irq` function instead.
[0075] Static scanning tools read rule files to match target code. The command is as follows:
[0076] make coccicheck COCCI=<rule file> pattern=report source path to match=drivers / gpu / drm / msm / ;
[0077] Print information similar to the following:
[0078] drivers / gpu / drm / msm / dp / dp_audio.c:98:29-30:*****
[0079] drivers / gpu / drm / msm / dp / dp_ctrl.c:1299:46-47: *****
[0080] drivers / gpu / drm / msm / dp / dp_link.c:1202:25-26: *****
[0081] drivers / gpu / drm / msm / dp / dp_power.c:408:28-29: *****
[0082] Developers can then locate the problematic code based on this information. For example:
[0083] skb_queue_walk_safe(&bcsp->unack, skb, tmp) {if (i>= pkts_to_be_removed)break;i++;skb_unlink(skb,&bcsp->unack);dev_kfree_skb_irq(skb);}
[0084] As another example, the following is a rule file template for the org pattern:
[0085] virtual org / / org mode
[0086] @r@
[0087] position p; / / SmPL related parameters, used to bind the position of the matching source code.
[0088] @@
[0089] [function]( ...);@p / / Target tracking point[function]
[0090] @script:python depends on report@
[0091] p< <r.p;
[0092] @@
[0093] msg = "[Zeal Robot] [function]has been discarded" / / This is the alert message printed by the static scanning tool after it hits the target code.
[0094] coccilib.org.print_todo(msg,p[0]) / / Output print information
[0095] The only difference between org mode and report mode lies in the mode declaration virtual xxx and the function coccilib.org.print_todo called to output the printed information.
[0096] Similarly, rule files are generated using a rule generator:
[0097] python main.py -s dev_kfree_skb_irq -ro templete / function.cocci;
[0098] `main.py` is the rule generator, and `templete / ` is the template library, containing templates. `-s` specifies code information, and `-ro` specifies the `org` mode. The generated rule file is as follows:
[0099] virtual org
[0100] @r@
[0101] position p;
[0102] @@
[0103] dev_kfree_skb_irq(...);@p
[0104] @script:python depends on org@
[0105] p< <r.p;
[0106] @@
[0107] msg = "[Zeal Robot] dev_kfree_skb_irq has been discarded"
[0108] coccilib.org.print_todo(msg,p[0]);
[0109] Scan using static tools:
[0110] make coccicheck COCCI=<rule file> pattern=org source path to match=ipc /
[0111] org generates reports in org schema format, for example:
[0112] *TODO[[view:. / ipc / sem.c::face=ovl-face1::linb=1967::colb=12::cole=13][[Zeal Robot]dev_kfree_skb_irq has been discarded . / ipc / sem.c::1967]];
[0113] Developers view the target code based on the located location, and the specific modification plan is decided by the developers.
[0114] Furthermore, rule files generated using the context mode rule file template, upon finding the target code, will output the target code and its context code, as well as the line number containing the output target code marked by a first marker (e.g., a "-" symbol). Rule files generated using the patch mode rule file template, upon finding the target code, will output the target code, its context code, and the corresponding replacement code. The output target code will be marked by a second marker (e.g., a "-" symbol), and the output replacement code will be marked by a third marker (e.g., a "+" symbol). The replacement code is pre-set in the rule file template and is used to replace the target code.
[0115] As an example, here is a rule file template for patch pattern:
[0116] virtual patch / / Select patch mode
[0117] @r@
[0118] @@
[0119] - [Buried point 1](...);
[0120] +[Buried point 2](...);
[0121] Rule file templates in patch mode do not need to be output to the print area.
[0122] Command line parameters: python main.py -s dev_kfree_skb_irq -d dev_consume_skb_irq-rp template / function.cocci; -s puts dev_kfree_skb_irq into tracking point 1, -d puts dev_consume_skb_irq into tracking point 2, -rp selects the rule file template of patch mode to generate the rule file.
[0123] virtual patch
[0124] @r@
[0125] @@
[0126] - dev_kfree_skb_irq(...);
[0127] +dev_consume_skb_irq(...);
[0128] In the scan results, "+" indicates a suggested modification, i.e., code replacement. This replacement code is determined by the developer who wrote the rule file template. As for the target code scanned by the static scanning tool, it is up to the code developer to decide whether to modify it. The patch mode rule file only provides a modification suggestion.
[0129] Static scanning tools read rule files to match target code. The command is as follows:
[0130] make coccicheck COCCI=<rule file> pattern=patch source code path to match=test / ;
[0131] The scan results will include the target code context and the replacement code.
[0132] The following is the scan information:
[0133] diff -u -pa / test2.cb / test2.c
[0134] --- a / test2.c
[0135] +++ b / test2.c
[0136] @@ -2316,7 +2316,7 @@ static inline void intel_pmu_ack_status(
[0137] skb_queue_walk_safe(&bcsp->unack, skb, tmp) {if (i>= pkts_to_be_removed)break;i++;skb_unlink(skb,&bcsp->unack);-dev_kfree_skb_irq(skb);
[0138] +dev_consume_skb_irq(skb);}
[0139] As another example, the following is a rule file template for context mode:
[0140] @r@
[0141] `pisition p;` / / SmPL related parameters, used to bind the location of the matching source code.
[0142] @@
[0143] *[function]( ...);@p / / Target tracking point[function], in context mode, "*" indicates the row to be tracked.
[0144] Like the patch mode, context mode does not require output to the print area. Use the following command line to generate the specific rule file:
[0145] python main.py -s dev_kfree_skb_irq -rc templete / function.cocci;
[0146] The `-s` option adds `dev_kfree_skb_irq` to the tracking point, and `-rc` selects a rule file template in context mode to generate the rule file. The generated rule file is as follows:
[0147] virtual patch
[0148] @r@
[0149] @@
[0150] *dev_kfree_skb_irq(...)@p;
[0151] make coccicheck COCCI=<rule file> pattern=context source code path to match=test / ;
[0152] Scan results:
[0153] diff -u -pa / test2.cb / test2.c
[0154] --- . / test2.c
[0155] +++ / tmp / nothing / test2.c
[0156] @@ -2316,7 +2316,7 @@ static inline void intel_pmu_ack_status(
[0157] skb_queue_walk_safe(&bcsp->unack, skb, tmp) {if (i>= pkts_to_be_removed)break;i++;skb_unlink(skb,&bcsp->unack);-dev_kfree_skb_irq(skb);
[0158] Both context mode and patch mode provide the context code of the target code in the scan results. However, unlike patch mode, context mode does not provide replacement code. The specific modifications are left to the code developer to decide whether and how to modify.
[0159] In this embodiment, the location of the target code within the code to be scanned is output, enabling developers to locate the target code based on its position. The target code and its context code are also output, along with a first marker indicating the line number where the target code is located. This allows developers to locate the target code based on the first marker and analyze it using the target code and its context code to determine whether and how to modify it. Finally, the target code, its context code, and its corresponding replacement code are output. The target code is marked with a second marker, and the replacement code is marked with a third marker. The replacement code is pre-set in a rule file template and is used to replace the target code. This provides a solution for modifying defective code and improves work efficiency.
[0160] In this embodiment of the application, the electronic device may include a rule generator and a rule template library, such as Figure 3 The schematic diagram of the electronic device framework shown illustrates that the rule generator can parse command-line arguments, obtain first code information and rule file template information from the command-line arguments, select a rule file template from the rule template library based on the information of the rule file template, and then write the first code information into the rule file template to generate a rule file. The rule template library contains various types of rule file templates.
[0161] In one implementation, Figure 4 This is a flowchart of a rule generator workflow provided in an embodiment of this application, such as... Figure 4 As shown, the rule generator workflow includes the following steps:
[0162] Step 401: Obtain command line arguments.
[0163] Step 402: Parse command-line arguments.
[0164] Specifically, the rule generator can parse command-line arguments and obtain the first code information and the corresponding rule file template information from the command-line arguments.
[0165] Step 403: Select the rule file template.
[0166] Specifically, after obtaining the information of the rule file template, the rule generator can select the rule file template corresponding to the information of the rule file template from the rule template library.
[0167] Step 404: Generate rule files.
[0168] Specifically, after obtaining the rule file template, the rule generator can write the first code information into the tracking points of the rule file template to generate the rule file.
[0169] In this embodiment, a rule file template is selected from the rule template library by a rule generator, and a rule file is automatically generated based on the first code information. This avoids manually writing corresponding rule files for each piece of code information, improves the efficiency of rule file generation, and thus improves code scanning efficiency.
[0170] It should be noted that the execution entity of the rule file generation method provided in this application embodiment can be a rule file generation device, or a control module in the rule file generation device for executing the rule file generation method. This application embodiment uses the execution of the rule file generation method by a rule file generation device as an example to illustrate the rule file generation device provided in this application embodiment.
[0171] Figure 5 This is a schematic diagram of the structure of a rule file generation apparatus according to an embodiment of the present invention. Figure 5 As shown, the rule file generation device 500 includes: an acquisition module 510, a selection module 520, and an execution module 530.
[0172] The acquisition module 510 is used to acquire information about the first code information and the rule file template corresponding to the first code information; the selection module 520 is used to select a rule file template from the rule template library according to the information of the rule file template; the execution module 530 is used to write the first code information into the rule file template to generate a rule file, the rule file being used to find target code in the code to be scanned that matches the first code information.
[0173] In one implementation, the execution module 530 is further configured to output target code information corresponding to the target code when the target code is found in the code to be scanned through the rule file.
[0174] In one implementation, the execution module 530 is further configured to analyze the defective source code, determine second code information, the second code information being of the same type as the first code information; determine a target embedding point based on the second code information, the target embedding point being used to write the first code information; and generate the rule file template using a semantic patching language, wherein the rule file template contains the target embedding point.
[0175] In one implementation, the execution module 530 is used to write the first code information into the target embedding point in the rule file template to generate the rule file.
[0176] In one implementation, the target code is defective code, and the execution module 530 is configured to: output the position of the target code in the code to be scanned; output the target code and its context code, and mark the line of code where the output target code is located using a first marker; output the target code, its context code, and a replacement code corresponding to the target code, mark the output target code using a second marker, and mark the output replacement code using a third marker, wherein the replacement code is pre-set in the rule file template and is used to replace the target code.
[0177] In one implementation, the rule file template includes a parameter declaration area and a code matching area, wherein the parameter declaration area is used to define the parameters of the semantic patching language, and the code matching area includes the target tracking point.
[0178] In one implementation, the rule file template includes a printout area, and the execution module 530 is used to output alarm information through the printout area, the alarm information being used to indicate that the target code has been found.
[0179] In one implementation, the acquisition module 510 is configured to receive command-line parameters indicating the generation of the rule file; and to acquire the first code information, the storage path of the rule file template, and the file name of the rule file template from the command-line parameters; the selection module 520 is configured to: select the rule file template from the rule template library according to the storage path and the file name.
[0180] The rule file generation device in this application embodiment can be a device, or a component, integrated circuit, or chip in a terminal. The device can be a mobile electronic device or a non-mobile electronic device. For example, mobile electronic devices can be mobile phones, tablets, laptops, PDAs, in-vehicle electronic devices, wearable devices, ultra-mobile personal computers (UMPCs), netbooks, or personal digital assistants (PDAs), etc., while non-mobile electronic devices can be servers, network-attached storage (NAS), personal computers (PCs), televisions (TVs), ATMs, or self-service machines, etc. This application embodiment does not impose specific limitations.
[0181] The rule file generation device in this application embodiment can be a device with an operating system. This operating system can be Android, iOS, or other possible operating systems; this application embodiment does not specifically limit it.
[0182] The rule file generation device provided in this application embodiment can achieve... Figure 1 The various processes implemented in the method implementation examples will not be described again here to avoid repetition.
[0183] Optionally, such as Figure 6 As shown in the illustration, this application embodiment also provides an electronic device 600, including a processor 601 and a memory 602. The memory 602 stores a program or instructions that can run on the processor 601. When the program or instructions are executed by the processor 601, they perform the following: obtaining first code information and information about a rule file template corresponding to the first code information; selecting a rule file template from a rule template library according to the information of the rule file template; writing the first code information into the rule file template to generate a rule file. The rule file is used to find target code in the code to be scanned that matches the first code information.
[0184] In one implementation, after writing the first code information into the rule file template to generate a rule file, if the target code is found in the code to be scanned through the rule file, the target code information corresponding to the target code is output.
[0185] In one implementation, before obtaining the first code information and the rule file template corresponding to the first code information, the source code with defects is analyzed to determine the second code information, which is of the same type as the first code information; a target tracking point is determined based on the second code information, which is used to write the first code information; the rule file template is generated using a semantic patching language, and the target tracking point is set in the rule file template.
[0186] In one implementation, the first code information is written into the target embedding point in the rule file template to generate the rule file.
[0187] In one implementation, the target code is defective code. The location of the target code in the code to be scanned is output; the target code and its context code are output, and the line of code where the output target code is located is marked by a first marker; the target code, its context code, and the corresponding replacement code are output, the output target code is marked by a second marker, and the output replacement code is marked by a third marker. The replacement code is preset in the rule file template and is used to replace the target code.
[0188] In one implementation, the rule file template includes a parameter declaration area and a code matching area, wherein the parameter declaration area is used to define the parameters of the semantic patching language, and the code matching area includes the target tracking point.
[0189] In one implementation, the rule file template includes a print output area. Before outputting the target code information corresponding to the target code, alarm information is output through the print output area. The alarm information is used to indicate that the target code has been found.
[0190] In one implementation, a command-line parameter is received to instruct the generation of the rule file; the first code information, the storage path of the rule file template, and the file name of the rule file template are obtained from the command-line parameter; and the rule file template is selected from the rule template library according to the storage path and the file name.
[0191] The specific execution steps can be found in the various steps of the above-mentioned rule file generation method embodiment, and can achieve the same technical effect. To avoid repetition, they will not be repeated here.
[0192] It should be noted that the electronic devices in the embodiments of this application include: servers, terminals, or other devices besides terminals.
[0193] The above electronic device structure does not constitute a limitation on the electronic device. An electronic device may include more or fewer components than illustrated, or combine certain components, or arrange them differently. For example, an input unit may include a Graphics Processing Unit (GPU) and a microphone, and a display unit may use a liquid crystal display (LCD), organic light-emitting diode (OLED), or other similar display panels. User input units include at least one of a touch panel and other input devices. A touch panel is also called a touchscreen. Other input devices may include, but are not limited to, physical keyboards, function keys (such as volume control buttons, power buttons, etc.), trackballs, mice, and joysticks, which will not be elaborated further here.
[0194] Memory can be used to store software programs and various data. Memory can primarily include a first storage area for storing programs or instructions and a second storage area for storing data. The first storage area can store the operating system, application programs or instructions required for at least one function (such as sound playback, image playback, etc.). Furthermore, memory can include volatile memory or non-volatile memory, or both. Non-volatile memory can be read-only memory (ROM), programmable read-only memory (PROM), erasable programmable read-only memory (EPROM), electrically erasable programmable read-only memory (EEPROM), or flash memory. Volatile memory can be random access memory (RAM), static random access memory (SRAM), dynamic random access memory (DRAM), synchronous dynamic random access memory (SDRAM), double data rate synchronous dynamic random access memory (DDRSDRAM), enhanced synchronous dynamic random access memory (ESDRAM), synchronous linked dynamic random access memory (Synchlink DRAM, SLDRAM), and direct memory bus RAM (DRRAM).
[0195] The processor may include one or more processing units; optionally, the processor integrates an application processor and a modem processor, wherein the application processor mainly handles operations related to the operating system, user interface, and applications, while the modem processor mainly handles wireless communication signals, such as a baseband processor. It is understood that the aforementioned modem processor may also not be integrated into the processor.
[0196] This application also provides a readable storage medium storing a program or instructions. When the program or instructions are executed by a processor, they implement the various processes of the above-described rule file generation method embodiments and achieve the same technical effect. To avoid repetition, they will not be described again here.
[0197] The processor is the processor in the electronic device described in the above embodiments. The readable storage medium includes computer-readable storage media, such as ROM, RAM, magnetic disk, or optical disk.
[0198] It should be noted that, in this document, the terms "comprising," "including," or any other variations thereof are intended to cover non-exclusive inclusion, such that a process, method, article, or apparatus that comprises a list of elements includes not only those elements but also other elements not expressly listed, or elements inherent to such a process, method, article, or apparatus. Without further limitations, an element defined by the phrase "comprising one..." does not exclude the presence of other identical elements in the process, method, article, or apparatus that includes that element. Furthermore, it should be noted that the scope of the methods and apparatuses in the embodiments of this application is not limited to performing functions in the order shown or discussed, but may also include performing functions substantially simultaneously or in the reverse order, depending on the functions involved. For example, the described methods may be performed in a different order than described, and various steps may be added, omitted, or combined. Additionally, features described with reference to certain examples may be combined in other examples.
[0199] Through the above description of the embodiments, those skilled in the art can clearly understand that the methods of the above embodiments can be implemented by means of software plus necessary general-purpose hardware platforms. Of course, they can also be implemented by hardware, but in many cases the former is a better implementation method. Based on this understanding, the technical solution of this application, in essence, or the part that contributes to the prior art, can be embodied in the form of a computer software product. This computer software product is stored in a storage medium (such as ROM / RAM, magnetic disk, optical disk) and includes several instructions to cause a terminal (which may be a mobile phone, computer, server, or network device, etc.) to execute the methods described in the various embodiments of this application.
[0200] The embodiments of this application have been described above with reference to the accompanying drawings. However, this application is not limited to the specific embodiments described above. The specific embodiments described above are merely illustrative and not restrictive. Those skilled in the art can make many other forms under the guidance of this application without departing from the spirit and scope of the claims, and all of these forms are within the protection scope of this application.
Claims
1. A method for generating rule files, characterized in that, include: Obtain the information of the first code information and the rule file template corresponding to the first code information; Based on the information of the rule file template, select a rule file template from the rule template library; The first code information is written into the rule file template to generate a rule file, which is used to find target code in the code to be scanned that matches the first code information; The step of writing the first code information into the rule file template to generate a rule file includes: Write the first code information into the target embedding point in the rule file template to generate the rule file; Before obtaining the first code information and the information of the rule file template corresponding to the first code information, the method further includes: The defective source code is analyzed to determine the second code information, which is of the same type as the first code information; The target embedding point is determined based on the second code information, and the target embedding point is used to write the first code information; The rule file template is generated using a semantic patching language, and the target embedding point is set in the rule file template.
2. The method according to claim 1, characterized in that, After writing the first code information into the rule file template and generating the rule file, the method further includes: If the target code is found in the code to be scanned using the rule file, the target code information corresponding to the target code is output.
3. The method according to claim 2, characterized in that, The target code is defective code, and the output of the target code information corresponding to the target code includes at least one of the following: Output the position of the target code within the code to be scanned; Output the target code and the context code of the target code, and mark the line of code where the output target code is located using the first marker; The target code, the context code of the target code, and the replacement code corresponding to the target code are output. The output target code is marked with a second marker, and the output replacement code is marked with a third marker. The replacement code is preset in the rule file template and is used to replace the target code.
4. The method according to claim 1, characterized in that, The rule file template includes a parameter declaration area and a code matching area. The parameter declaration area is used to define the parameters of the semantic patching language, and the code matching area includes the target tracking point.
5. The method according to claim 2, characterized in that, The rule file template includes a print output area. Before outputting the target code information corresponding to the target code, the method further includes: The alarm information is output through the printout area, and the alarm information is used to indicate that the target code has been found.
6. The method according to claim 1, characterized in that, The process of obtaining the first code information and the rule file template corresponding to the first code information includes: Receive command-line arguments indicating the generation of the rule file; Obtain the first code information, the storage path of the rule file template, and the file name of the rule file template from the command line parameters; The step of selecting a rule file template from the rule template library based on the information of the rule file template includes: Based on the storage path and the file name, select the rule file template from the rule template library.
7. The method according to claim 1, characterized in that, The first code information includes the function name and / or parameter name corresponding to the target code.
8. An electronic device, characterized in that, It includes a processor, a memory, and a program or instructions stored in the memory and executable on the processor, wherein when the program or instructions are executed by the processor, they implement the steps of the rule file generation method as described in any one of claims 1-7.
9. A readable storage medium, characterized in that, The readable storage medium stores a program or instructions that, when executed by a processor, implement the steps of the rule file generation method as described in any one of claims 1-7.
Citation Information
Patent Citations
Code inspection rule generation method, code inspection method, code inspection device and medium
CN113672512A