Code detection method and device and storage medium

Through the combination of syntax analysis tree and large language model, code risks are accurately detected, and the accuracy of risk detection under complex code structures is solved, and efficient code risk assessment is achieved.

CN119720218APending Publication Date: 2025-03-2858 CHANG LIFE (BEIJING) INFORMATION TECH CO LTD
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202411824061.X
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-12-11
Publication Date
2025-03-28

AI Technical Summary

Technical Problem

How to accurately detect whether there are risks in code, especially when the updated development code structure is complex.

Method used

The class to be detected is determined through the syntax analysis tree, and the call relationship list and detection results are output using a large language model to accurately reflect the structure and execution logic of the class to be detected.

Benefits of technology

Accurate detection of code risks is achieved, and the accuracy and reliability of detection results are improved.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119720218A_ABST
    Figure CN119720218A_ABST
Patent Text Reader

Abstract

The embodiment of the invention provides a code detection method and device and a storage medium, and the method comprises the steps: determining a to-be-detected class in an original code according to a grammar analysis tree corresponding to the original code; and then, inputting different code snippets in the to-be-detected class and cue words for describing method calling rules in the class into the large language model, so that the large language model outputs a calling relation list of the to-be-detected class. And then, inputting the to-be-detected class and the calling relation list of the to-be-detected class into the large language model, and outputting a detection result of the to-be-detected class by the large language model. Therefore, by means of the cue word, the large language model can better understand the semantic information contained in the to-be-detected class, so that the calling relation list of the to-be-detected class is accurately obtained. And the calling relation list can accurately reflect the structural composition of the to-be-detected class, so that further, by means of the calling relation list, the large language model can more accurately detect whether the to-be-detected class has a risk or not.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the field of computer technology, and in particular to a code detection method, device and storage medium. Background Art

[0002] With the rapid development of computer technology, the iteration speed of applications is getting faster and faster, and the corresponding development codes must be continuously updated according to user needs. In practice, after updating the development code, developers are often required to test it to determine whether the updated development code has risks.

[0003] Based on the above description, how to accurately detect whether the code has risks becomes a technical problem that needs to be solved urgently. Summary of the invention

[0004] In view of this, an embodiment of the present invention provides a code detection method, device and storage medium to accurately detect whether a code has risks.

[0005] In a first aspect, an embodiment of the present invention provides a code detection method, comprising:

[0006] Determine the class to be detected in the original code according to the syntax analysis tree corresponding to the original code, wherein the syntax analysis tree describes the dependency relationship between the class and the method in the original code;

[0007] Inputting different code snippets in the class to be detected and prompt words describing method calling rules in the class into a large language model, so that the large language model outputs a call relationship list of the class to be detected;

[0008] The class to be detected and the call relationship list of the class to be detected are input into the large language model, so that the large language model outputs the detection result of the class to be detected.

[0009] In a second aspect, an embodiment of the present invention provides a code detection device, including:

[0010] A module for determining a class to be detected, used to determine a class to be detected in the original code according to a syntax analysis tree corresponding to the original code, wherein the syntax analysis tree describes the dependency relationship between classes and methods in the original code;

[0011] A calling relationship determination module, used for inputting different code snippets in the class to be detected and prompt words describing method calling rules in the class into a large language model, so that the large language model outputs a calling relationship list of the class to be detected;

[0012] The detection module is used to input the class to be detected and the call relationship list of the class to be detected into the large language model, so that the large language model outputs the detection result of the class to be detected.

[0013] In a third aspect, an embodiment of the present invention provides an electronic device, comprising a processor and a memory, wherein the memory is used to store one or more computer instructions, wherein the one or more computer instructions are executed by the processor to implement the code detection method in the first aspect. The electronic device may also include a communication interface for communicating with other devices or communication systems.

[0014] In a fourth aspect, an embodiment of the present invention provides a non-temporary machine-readable storage medium having executable code stored thereon. When the executable code is executed by a processor of an electronic device, the processor can at least implement the code detection method as described in the first aspect above.

[0015] In the code detection method provided by the embodiment of the present invention, the class to be detected in the original code is determined according to the grammatical analysis tree corresponding to the original code. The grammatical analysis tree describes the dependency relationship between the class and the method in the original code. Then, different code snippets in the class to be detected and the prompt words describing the method call rules in the class can be input into the large language model, so that the large language model outputs the call relationship list of the class to be detected. After that, the class to be detected and the call relationship list of the class to be detected can be input into the large language model, so that the large language model outputs the detection result of the class to be detected.

[0016] It can be seen that in the above method, the class to be detected can be accurately determined from the original code through the syntax analysis tree. Furthermore, with the help of the prompt words that describe the method calling rules in the class, the large language model can better understand the semantic information contained in the class to be detected, so that the large language model can more accurately determine the call relationship list of the class to be detected. And because the call relationship list can accurately reflect the structural composition of the class to be detected and reveal the execution order and call relationship between the methods in the class to be detected, therefore, further, with the help of the call relationship list, the large language model can better understand the structure and execution logic of the class to be detected, so as to more accurately detect whether there is a risk in the class to be detected. BRIEF DESCRIPTION OF THE DRAWINGS

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

[0018] Figure 1 A flowchart of a code detection method provided by an embodiment of the present invention;

[0019] Figure 2 A schematic diagram of the structure of a grammar analysis tree provided by an embodiment of the present invention;

[0020] Figure 3 A flowchart of another code detection method provided by an embodiment of the present invention;

[0021] Figure 4 A schematic diagram of a process for determining a call relationship list provided by an embodiment of the present invention;

[0022] Figure 5 A schematic diagram of a working process of code detection provided by an embodiment of the present invention;

[0023] Figure 6 A flowchart of another code detection method provided by an embodiment of the present invention;

[0024] Figure 7 A flow chart of a method for determining a risk level provided by an embodiment of the present invention;

[0025] Figure 8 A schematic diagram of the structure of a code detection device provided by an embodiment of the present invention;

[0026] Fig. 9 For Figure 8 A schematic diagram of the structure of an electronic device corresponding to the code detection device provided in the illustrated embodiment. DETAILED DESCRIPTION

[0027] In order to make the purpose, technical solution and advantages of the embodiments of the present invention clearer, the technical solution in the embodiments of the present invention will be clearly and completely described below in conjunction with the drawings in the embodiments of the present invention. Obviously, the described embodiments are part of the embodiments of the present invention, not all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of the present invention.

[0028] The terms used in the embodiments of the present invention are only for the purpose of describing specific embodiments, and are not intended to limit the present invention. The singular forms "a", "said" and "the" used in the embodiments of the present invention and the appended claims are also intended to include plural forms, unless the context clearly indicates other meanings, and "multiple" generally includes at least two, but does not exclude the situation of including at least one.

[0029] It should be understood that the term "and / or" used in this article is only a description of the association relationship of associated objects, indicating that there can be three relationships. For example, A and / or B can represent: A exists alone, A and B exist at the same time, and B exists alone. In addition, the character " / " in this article generally indicates that the associated objects before and after are in an "or" relationship.

[0030] As used herein, the words "if" and "if" may be interpreted as "at the time of" or "when" or "in response to determining" or "in response to identifying", depending on the context. Similarly, the phrases "if it is determined" or "if (stated condition or event) is identified" may be interpreted as "when determining" or "in response to determining" or "when identifying (stated condition or event)" or "in response to identifying (stated condition or event)", depending on the context.

[0031] It should be noted that the terms "include", "comprises" or any other variations thereof are intended to cover non-exclusive inclusion, so that a product or system including a series of elements includes not only those elements, but also other elements not explicitly listed, or also includes elements inherent to such product or system. In the absence of more restrictions, the elements defined by the sentence "comprises a ..." do not exclude the existence of other identical elements in the product or system including the elements.

[0032] It should also be noted that the user information (including but not limited to user device information, user personal information, etc.) and data (including but not limited to data used for analysis, stored data, displayed data, etc.) involved in the present invention are all information and data authorized by the user or fully authorized by all parties, and the collection, use and processing of relevant data must comply with the relevant laws, regulations and standards of the relevant countries and regions, and provide corresponding operation entrances for users to choose to authorize or refuse.

[0033] Before describing in detail the code detection method provided by each embodiment of the present invention, an application scenario of code detection may also be described schematically:

[0034] In actual applications, after iterative updates to the APP development code, code vulnerabilities may be introduced, increasing the risk of illegal code theft leading to data leakage or service interruption. Therefore, developers are required to perform code detection on the updated development code to determine whether the updated development code poses risks.

[0035] However, in practice, the structure of the updated development code is usually more complex, which may affect the accuracy of code risk detection. In order to accurately detect whether the code has risks, the code detection method provided by the following embodiments of the present invention can be used.

[0036] Based on the above description, some embodiments of the present invention are described in detail below in conjunction with the accompanying drawings. In the case where there is no conflict between the embodiments, the following embodiments and the features in the embodiments can be combined with each other. In addition, the step sequence in the following method embodiments is only an example and not a strict limitation.

[0037] Figure 1 The flowchart of a code detection method provided by an embodiment of the present invention is shown in FIG. The code detection method provided by an embodiment of the present invention can be executed by a detection device. It is understood that the detection device can be implemented as software, or a combination of software and hardware. Figure 1 As shown, the method comprises the following steps:

[0038] 101, determining the class to be detected in the original code according to the syntax analysis tree corresponding to the original code, the syntax analysis tree describing the dependency relationship between the class and the method in the original code.

[0039] 102 , input different code snippets in the class to be detected and prompt words describing method calling rules in the class into the large language model, so that the large language model outputs a calling relationship list of the class to be detected.

[0040] 103 , input the class to be detected and the call relationship list corresponding to the class to be detected into the large language model, so that the large language model outputs the detection result of the class to be detected.

[0041] The detection device can obtain the original code and perform syntax analysis on the original code to obtain a syntax analysis tree corresponding to the original code. Optionally, the original code may include the development code of the APP or the development code of the page in the browser. The syntax analysis tree describes the dependency relationship between classes and methods in the original code.

[0042] For example, suppose the original code includes class A to be detected, and class A to be detected includes methods B and C. Then the structure of the syntax analysis tree corresponding to class A to be detected can be combined with Figure 2 Understand. Figure 2 As shown, the declaration of the class A to be detected includes the class name A and the class block. The class block may specifically include the declaration of method B and the declaration of method C. Further, the declaration of method B may include the method name B and the method block, and the declaration of method name C may include the method name C and the method block.

[0043] Then, the detection device can determine the class to be detected in the original code according to the syntax analysis tree corresponding to the original code. Optionally, the original code may include multiple classes to be detected. Based on this, optionally, the detection device can traverse the syntax analysis tree based on the identification information corresponding to the class to be detected to extract the class to be detected from the syntax analysis tree.

[0044] In practical applications, the class to be detected may include multiple code snippets, and different code snippets may contain call relationships between different methods. The detection device may input different code snippets in the class to be detected and prompt words describing the method call rules in the class into the large language model, so that the large language model can output a call relationship list of the class to be detected.

[0045] The call relationship list of the class to be detected describes the interactions and dependencies between different methods in the class to be detected, which can reflect the execution process and structure of the class to be detected.

[0046] The prompt word may include the result of splicing the natural language text and the code. For the generation of the prompt word, optionally, the detection device may obtain a preset prompt word template and a method call rule in the class, and then may generate a prompt word describing the method call rule in the class according to the preset prompt word template and the method call rule in the class. Among them, the preset prompt word template may include a natural language text describing an object, scene or concept, and the method call rule in the class may be expressed as a code. That is, the prompt word may include the result of splicing the natural language text and the code.

[0047] Optionally, the method calling rules in the class may include the type of the method, access modifiers, parameters and return values, etc. Optionally, the large language model can be deployed locally on the detection device, or deployed remotely. The large language model can specifically include a Generative Pre-training Transformer (GPT) model with strong semantic understanding capabilities, and a bidirectional encoder representation (Bert) model from Transformers.

[0048] Based on the above steps 101 and 102, the class to be detected and the call relationship list of the class to be detected can be obtained respectively. Further, the detection device can input the class to be detected and the call relationship list of the class to be detected into the large language model, so that the large language model outputs the detection result of the class to be detected. Among them, the detection result may include whether the class to be detected has risks, the reasons for the risks, and code modification suggestions.

[0049] In addition, it should be explained here that, according to the above introduction to the call relationship list, the call relationship list of the class to be detected can reflect the execution process and structure of the class to be detected. Therefore, it can serve as a prompt for the large language model to output the detection results corresponding to the class to be detected. Therefore, the call relationship list of the class to be detected can also be called a prompt word to ensure that the large language model outputs accurate detection results.

[0050] In this embodiment, the class to be detected can be accurately determined from the original code through the grammatical analysis tree. Furthermore, with the help of the prompt words describing the method calling rules in the class, the large language model can better understand the semantic information contained in the class to be detected, so that the large language model can more accurately determine the call relationship list of the class to be detected. And because the call relationship list can accurately reflect the structural composition of the class to be detected and reveal the execution order and call relationship between the methods in the class to be detected, therefore, further, with the help of the call relationship list, the large language model can better understand the structure and execution logic of the class to be detected, so as to more accurately detect whether there is a risk in the class to be detected.

[0051] Figure 1 In the embodiment shown, it has been mentioned that different code snippets of the class to be detected can be input into the large language model to obtain a call relationship list of the class to be detected. The specific method of determining the call relationship list can be referred to as follows Figure 3 Related description in the illustrated embodiment. Figure 3 Flow chart of another code detection method provided by an embodiment of the present invention. Figure 3 As shown, the following steps may be included:

[0052] 301. Determine, based on a syntax analysis tree corresponding to the original code, a first code fragment containing a first method and a second method in the original code and a second code fragment containing the second method and a third method in the original code, wherein the first method is located before the second method and the second method is located before the third method.

[0053] 302 , input the first code snippet and the prompt word into the large language model, so that when there is a calling relationship between the methods in the first code snippet, the large language model outputs a first calling relationship list of the first code snippet.

[0054] 303 , in response to the output of the first call relationship list, input the second code snippet and the prompt word into the large language model, so that when there is no call relationship between the methods in the second code snippet, the first call relationship list is output as the call relationship list of the class to be detected.

[0055] 304 , input the class to be detected and the call relationship list corresponding to the class to be detected into the large language model, so that the large language model outputs the detection result of the class to be detected.

[0056] In practical applications, optionally, the detection device can use a breadth-first search (BFS) algorithm to traverse the class to be detected, find multiple methods that have a calling relationship with the class to be detected in turn, and use a large language model to output a list of calling relationships between methods in the class to be detected.

[0057] For example, assuming that the class to be detected includes a first method, a second method, and a third method, the specific determination process of the call relationship list can be combined with Figure 4 Understand. Figure 4 As shown, before executing the BFS algorithm, the developer can first set an empty method queue and an empty visited method set, that is, initialize the method queue and the visited method set.

[0058] Then the detection device may add the first method to the method queue in response to the acquisition of the first method, and determine whether the method queue is empty. Optionally, if it is determined that the first method has been accessed, the detection device may skip the first method and re-determine whether the method queue is empty.

[0059] If it is determined that the method queue is not empty, indicating that the first method has been added to the method queue, the detection device can take out a method, namely the first method, from the method queue. Then, the detection device can determine whether the first method has been accessed. If it is determined that the first method has not been accessed, the first method can be marked as an accessed method. Further, the first code fragment containing the first method and the second method in the original code can be determined based on the syntax analysis tree corresponding to the original code, that is, the first code fragment in the class to be detected is determined. Among them, in the class to be detected, the first method is located before the second method.

[0060] Afterwards, the detection device may input the first code snippet and the prompt word into the large language model, so that when there is a calling relationship between the methods in the first code snippet, the large language model outputs a first calling relationship list in the first code snippet, wherein the first calling relationship list includes the calling relationship between the first method and the second method.

[0061] After obtaining the first call relationship list, that is, after identifying the existence of a call relationship between the first method and the second method through the large language model, the detection device can add the identified second method, that is, a new method other than the first method, to the method queue, and determine whether the method queue is empty after the new method is added.

[0062] If it is determined that the method queue is not empty, indicating that the second method has been added to the method queue, a method, namely the second method, can be taken out of the method queue, and it is determined whether the second method has been accessed. If it is determined that the second method has been accessed, the second method can be skipped and it is determined again whether the method queue is empty.

[0063] If it is determined that the second method has not been accessed, the detection device can mark the second method as an accessed method, and further, according to the syntax analysis tree corresponding to the original code, determine the second code fragment containing the second method and the third method in the original code, that is, determine the second code fragment in the class to be detected. In the class to be detected, the second method is located before the third method.

[0064] Afterwards, the detection device can input the second code snippet and the prompt word into the large language model in response to the output of the first call relationship list, so that when there is no call relationship between the methods in the second code snippet, the first call relationship list is output as the call relationship list of the class to be detected. In other words, if the large language model determines that there is no call relationship between the third method and the second method, that is, the large language model does not identify a new method that has a call relationship with the second method, then it can be determined that the method queue is empty, and further, the first call relationship list can be directly output as the call relationship list of the class to be detected.

[0065] Finally, the detection device may input the class to be detected and the determined first call relationship list into the large language model, so that the large language model outputs the detection result of the class to be detected, that is, determines whether there is a risk in the class to be detected.

[0066] It should be noted that, in practice, a class to be detected can usually contain an entry method, which can be considered as the entry point of the class to be detected. That is, starting from the entry method, non-entry methods that have a calling relationship with the entry method can be searched in sequence. That is, the first method mentioned above can include the entry method, and the second method and the third method can include different non-entry methods.

[0067] In addition, although this embodiment only mentions the case where the class to be detected includes the first method, the second method and the third method, in actual application, the class to be detected may include more methods. In this case, the BFS algorithm can also be used. Figure 4 The process of determining the call relationship list shown in the figure sequentially finds all non-entry methods that have a call relationship with the entry method in the class to be detected, and obtains the call relationship list of the class to be detected.

[0068] And in actual application, the original code may include multiple classes to be detected. Correspondingly, the detection device can determine multiple call relationship lists. Further, the multiple call relationship lists can be integrated to obtain a complete call relationship list corresponding to the original code.

[0069] In this embodiment, the BFS algorithm can accurately determine the entry method in the class to be detected and all non-entry methods that have a calling relationship with the entry method. Furthermore, with the help of the grammar analysis tree and the large language model, the calling relationship between the methods in the class to be detected can be accurately and completely analyzed, and the calling relationship list containing the calling relationship is used as prompt information, so that the large language model can more accurately output the detection result of the class to be detected, that is, it can more accurately identify whether the class to be detected has risks.

[0070] In addition, for the contents not described in detail in this embodiment and the technical effects that can be achieved, please refer to the relevant descriptions in the above embodiments, and will not be repeated here.

[0071] Optionally, in order to improve the efficiency of the large language model in determining the call relationship list of the class to be detected, the detection device can also filter the class to be detected, and input the filtered class to be detected into the large language model, so that the large language model outputs the call relationship list of the class to be detected.

[0072] Specifically, the detection device may first obtain preset filtering keywords. Then, the detection device may process the class to be detected based on the preset filtering keywords to obtain the filtered class to be detected. Afterwards, the detection device may input different code snippets in the filtered class to be detected and the prompt words describing the method calling rules in the class into the large language model, so that the large language model can output the calling relationship list of the class to be detected.

[0073] The filtering keywords can be preset by the developer, which may include public methods that do not need to be detected. Public methods refer to methods that implement common functions, which may include string concatenation, data acquisition, setting, calculation or conversion, etc.

[0074] For example, assuming that the filtering keyword is a method for implementing string concatenation, the detection device can process the detection class based on the filtering keyword, that is, delete the method for implementing string concatenation in the class to be detected to obtain a filtered class to be detected.

[0075] In this embodiment, based on the preset filtering keywords, the class to be detected is processed to obtain a filtered class to be detected with a smaller data volume. Further, the class to be detected with a smaller data volume is input into the large language model so that the large language model can quickly output a call relationship list.

[0076] According to the descriptions in the above embodiments, in the process of obtaining the detection results of the class to be detected, the large language model is used twice at different stages. Specifically, in the first stage, the large language model can be used to analyze different code snippets in the class to be detected to obtain a call relationship list of the class to be detected. In the second stage, the large language model can be used to detect the class to be detected to obtain the detection results of the class to be detected. Among them, the call relationship list of the class to be detected output by the large language model in the first stage can be used as a prompt word of the large language model in the second stage to prompt the structural composition of the class to be detected, so as to ensure the accuracy of the detection results.

[0077] Based on this, in order to further improve the accuracy of the test results, the detection device can optionally be combined with Figure 5 The process shown in the figure can be used to improve the accuracy of the detection result. Specifically, the detection device can generate a general prompt word template containing user demand information. Then, the detection device can generate a comprehensive prompt word based on the general prompt word template containing user demand information and the call relationship list of the class to be detected output by the first stage large language model. After that, the detection device can input the class to be detected and the comprehensive prompt word into the large language model, so that the large language model can output the detection result of the class to be detected.

[0078] The user requirement information includes the user's detection requirements for the class to be detected, such as detecting whether the class to be detected has a risk of being attacked. In addition to the user requirement information, the general prompt word template may also include the format of the detection result output by the large language model, etc.

[0079] In this embodiment, since the comprehensive prompt word includes a call relationship list of the class to be detected and a general prompt word template containing user demand information, that is, the comprehensive prompt word integrates information from different aspects, it can provide more comprehensive and accurate prompt information, so that the large language model can output more accurate detection results.

[0080] Optionally, the detection device may further evaluate the correlation coefficient between the comprehensive prompt word and the class to be detected, so as to optimize the input of the large language model according to the correlation coefficient, thereby further improving the accuracy of the detection result. Figure 6 A flowchart of another code detection method provided by an embodiment of the present invention. Figure 6 As shown, the following steps may be included:

[0081] 601, determining the class to be detected in the original code according to the syntax analysis tree corresponding to the original code, the syntax analysis tree describing the dependency relationship between the class and the method in the original code.

[0082] 602 , input different code snippets in the class to be detected and prompt words describing method calling rules in the class into the large language model, so that the large language model outputs a calling relationship list of the class to be detected.

[0083] 603, obtaining the semantic similarity between the comprehensive prompt word and the class to be detected, and the structural matching degree between the comprehensive prompt word and the words included in the class to be detected.

[0084] 604 , determining the correlation coefficient between the comprehensive prompt word and the class to be detected according to the semantic similarity and the structural matching degree.

[0085] 605 , if the correlation coefficient is less than a preset threshold, obtaining the class to be detected containing annotation information, and determining a specific prompt word template corresponding to the class to be detected.

[0086] 606 , input the class to be detected containing annotation information and the specific prompt word template into the large language model, so that the large language model outputs the detection result of the class to be detected.

[0087] The specific implementation process of the above steps 601 to 602 can be found in Figure 1 The specific description of the illustrated embodiments will not be repeated here.

[0088] according to Figure 5 As can be seen from the illustrated embodiment, the detection device can generate a comprehensive prompt word based on the call relationship list of the to-be-detected class obtained in step 602 and a general prompt word template containing user demand information.

[0089] Furthermore, the detection device can calculate the semantic similarity between the comprehensive prompt word and the class to be detected, and the structural matching degree between the comprehensive prompt word and the words contained in the class to be detected. Then, the detection device can determine the correlation coefficient between the comprehensive prompt word and the class to be detected based on the semantic similarity and the structural matching degree.

[0090] The calculation process of the above correlation coefficient is shown in formula (1):

[0091] Relevance(P,Q)=α*SemanticSimilarity(P,Q)+β*StructuralMatch(P,Q) (1)

[0092] In formula (1), P represents the comprehensive prompt word, Q represents the class to be detected, and Relevance represents the correlation coefficient between P and Q. α and β represent weight coefficients, which are used to adjust the relative importance between semantic similarity and structural matching. Optionally, α can be set to 0.6 and β can be set to 0.4, which means that semantic similarity is relatively more important than structural matching. In addition, it should be noted that in practice, the weight coefficient can be appropriately adjusted according to the specific application scenario, and no specific limitation is made here.

[0093] SemanticSimilarity is used to calculate the semantic similarity between P and Q. Optionally, the cosine similarity algorithm or word embedding model can be used to calculate the semantic similarity. StructuralMatch is used to calculate the structural match between P and Q. Optionally, considering that P is the result of splicing code and natural language text, that is, P can also be understood as a mixed text, the word frequency statistics algorithm can be used to calculate the structural match. Among them, the word frequency statistics algorithm is a common text analysis algorithm, which is mainly used to analyze the most frequently appearing words in mixed texts.

[0094] If the correlation coefficient is less than the preset threshold, it indicates that the comprehensive prompt word is not related to the class to be detected. This situation may affect the accuracy of the detection result output by the large language model. The detection device can obtain the class to be detected containing annotation information and determine the specific prompt word template corresponding to the class to be detected. In other words, the detection device can optimize the class to be detected and the comprehensive prompt word, that is, optimize the input of the large language model to improve the accuracy of the detection result output by the large language model. Afterwards, the detection device can input the class to be detected containing annotation information and the specific prompt word template into the large language model, so that the large language model can output the detection result of the class to be detected.

[0095] If the correlation coefficient is not less than the preset threshold, it indicates that the comprehensive prompt word is related to the class to be detected. This situation will not affect the accuracy of the detection result output by the large language model. The detection device does not need to optimize the class to be detected and the comprehensive prompt word. The class to be detected and the comprehensive prompt word can be directly input into the large language model, so that the large language model can output the detection result of the class to be detected.

[0096] For the optimization of the class to be detected, optionally, the developer may add annotation information to the class to be detected, such as marking the definition and dependency relationship of the class to be detected, etc. Then the detection device may obtain the class to be detected containing the annotation information.

[0097] In practice, the multiple classes to be detected included in the original code may correspond to different path information. For the optimization of the comprehensive prompt words, an optional way is to generate different specific prompt word templates for different path information, that is, to generate different specific prompt word templates for different classes to be detected. Another optional way is to divide the classes to be detected into layers to obtain a controller layer, a service layer, and a data access layer. Furthermore, different specific prompt word templates can be generated for different layers.

[0098] It should be noted that the specific prompt word template obtained after optimization also includes the user demand information in the comprehensive prompt words before optimization and the call relationship list of the class to be detected.

[0099] In this embodiment, by calculating the semantic similarity and structural matching between the comprehensive prompt word and the class to be detected, the correlation coefficient between the comprehensive prompt word and the class to be detected can be accurately determined. Further, when the correlation coefficient is less than a preset threshold, the accuracy of the detection result output by the large language model can be better guaranteed by optimizing the input of the large language model.

[0100] In the above embodiments, it has been mentioned that the detection result of the class to be detected can be output by the large language model. In practice, the original code may contain multiple classes to be detected, and correspondingly, the detection results corresponding to the multiple classes to be detected can be obtained. Optionally, the detection result may include the original risk score of the class to be detected and the original confidence of the original risk score.

[0101] After obtaining the test results corresponding to the multiple classes to be tested, the testing device can also use the following Figure 7 The method provided by the illustrated embodiment is used to determine the risk level corresponding to the original code. Figure 7 A flow chart of a method for determining a risk level provided by an embodiment of the present invention. Figure 7 As shown, the method may include the following steps:

[0102] 701, determine the confidence of the large language model according to the historical detection results output by the large language model.

[0103] 702 : For any class to be detected among the multiple classes to be detected, update the historical confidence of any class to be detected according to the confidence of the large language model.

[0104] 703 , determining the risk score of the original code according to the updated confidences and preset weights of the multiple classes to be detected.

[0105] 704. Determine the risk level corresponding to the original code according to the risk score of the original code.

[0106] The detection device can obtain the historical detection results output by the large language model, and determine the confidence of the large language model based on the historical detection results. For any of the multiple classes to be detected, the detection device can update the historical confidence of any class to be detected based on the confidence of the large language model to obtain the updated confidence of each of the multiple classes to be detected in the original code.

[0107] The updating process of the historical confidence of any of the above-mentioned classes to be detected is shown in formula (2):

[0108] Confidence=(A*B) / (A*B+(1-A)*(1-B)) (2)

[0109] In formula (2), A represents the historical confidence of any class to be detected, B represents the confidence of the large language model, and Confidence represents the updated confidence of any class to be detected. When the large language model is more confident about the detection result of any class to be detected (higher A value) and has good historical performance (higher B value), the updated confidence of any class to be detected will also increase accordingly.

[0110] For the determination of A, the detection device can optionally obtain the historical confidence of any class to be detected by adjusting the temperature parameters for multiple sampling or using calibration technology. For example, assuming that the detection results outputted are the same in two of the three samplings, the confidence of any class to be detected can be determined to be 0.66.

[0111] For the determination of B, optionally, the detection device can use a cross-validation technique, such as a K-fold cross-validation method, to evaluate the historical detection results output by the large language model for different classes to be detected, so as to obtain the confidence of the large language model. It is understandable that if the error between the historical detection results output by the large language model for multiple classes to be detected is small, it can indicate that the confidence of the large language model is high. Conversely, if the error between the historical detection results is large, it can indicate that the confidence of the large language model is low. Optionally, the developers can also verify and evaluate the historical detection results output by the large language model based on their own knowledge and experience to obtain the confidence of the large language model.

[0112] Based on the above formula (2), the detection device can obtain the updated confidence of each of the multiple classes to be detected in the original code. Further, the detection device can also determine the risk score of the original code according to the updated confidence of each of the multiple classes to be detected and the preset weight.

[0113] The process of determining the risk score of the above original code is shown in formula (3):

[0114] Score=∑(ω i *Ci ) / ∑ω i (3)

[0115] In formula (3), C i represents the updated confidence of any class to be detected, ω i represents the preset weight corresponding to any class to be detected. Score represents the risk score of the original code. Optionally, ω i The size of can be set according to the importance of the class to be detected. And in response to changes in user needs, ω i It can also be adjusted dynamically.

[0116] In addition, the calculation process of the above formula (3) can essentially be understood as a weighted average algorithm, the purpose of which is to ensure that more important classes to be detected can occupy a larger proportion in the final risk score evaluation process.

[0117] Finally, the detection device can determine the risk level corresponding to the original code according to the risk score of the original code. Optionally, the risk level corresponding to the original code can include any one of low, medium, and high. It can be understood that the lower the risk score, the lower the risk level corresponding to the original code. Conversely, the higher the risk score, the higher the risk level corresponding to the original code.

[0118] In this embodiment, the historical confidences of multiple classes to be detected in the original code are updated respectively by the confidence of the large language model to obtain the updated confidences of each of the multiple classes to be detected. Since the updated confidences can reflect the reliability of the large language model, further, according to the updated confidences of each of the multiple classes to be detected and the preset weights, the risk score of the original code can be accurately determined, and then the risk level corresponding to the original code can be accurately determined.

[0119] After determining the risk level corresponding to the original code, the detection device can further generate a comprehensive analysis report including the risk level, risk cause, and code modification suggestions. The detection device can also adjust the comprehensive analysis report into a structured document format to facilitate review and discussion by developers.

[0120] The following will describe in detail the code detection device of one or more embodiments of the present invention. Those skilled in the art will appreciate that these code detection devices can be configured using commercially available hardware components through the steps taught in this solution.

[0121] Figure 8 A schematic diagram of the structure of a code detection device provided by an embodiment of the present invention is shown in FIG. Figure 8 As shown, the device comprises:

[0122] The to-be-detected class determination module 11 is used to determine the to-be-detected class in the original code according to the syntax analysis tree corresponding to the original code, wherein the syntax analysis tree describes the dependency relationship between classes and methods in the original code.

[0123] The calling relationship determination module 12 is used to input different code snippets in the class to be detected and prompt words describing method calling rules in the class into the large language model, so that the large language model outputs a calling relationship list of the class to be detected.

[0124] The detection module 13 is used to input the class to be detected and the call relationship list of the class to be detected into the large language model, so that the large language model outputs the detection result of the class to be detected.

[0125] Optionally, the class to be detected includes a first code fragment and a second code fragment. Based on this, the class to be detected determination module 11 is used to determine the first code fragment containing the first method and the second method in the original code and the second code fragment containing the second method and the third method in the original code according to the syntax analysis tree corresponding to the original code, the first method is located before the second method, and the second method is located before the third method.

[0126] Wherein, the first method comprises an entry method, and the second method and the third method comprise different non-entry methods.

[0127] Optionally, the call relationship determination module 12 is used to input the first code snippet and the prompt word into a large language model, so that when a call relationship exists among the methods in the first code snippet, the large language model outputs a first call relationship list of the first code snippet; in response to the output of the first call relationship list, the second code snippet and the prompt word are input into the large language model, so that when a call relationship does not exist among the methods in the second code snippet, the first call relationship list is output as the call relationship list of the class to be detected.

[0128] Optionally, the device further comprises a filtering module 14, which is used to obtain preset filtering keywords; based on the preset filtering keywords, the class to be detected is processed to obtain a filtered class to be detected.

[0129] The calling relationship determination module 12 is used to input the different code snippets in the filtered class to be detected and the prompt words describing the method calling rules in the class into the large language model.

[0130] Optionally, the detection module 13 is used to generate comprehensive prompt words based on a general prompt word template containing user demand information and a call relationship list of the class to be detected; input the class to be detected and the comprehensive prompt words into the large language model, so that the large language model outputs the detection result of the class to be detected.

[0131] Optionally, the device also includes a model input optimization module 15, which is used to obtain the semantic similarity between the comprehensive prompt word and the class to be detected, and the structural matching degree between the comprehensive prompt word and the words contained in the class to be detected; according to the semantic similarity and the structural matching degree, determine the correlation coefficient between the comprehensive prompt word and the class to be detected; if the correlation coefficient is less than a preset threshold, obtain the class to be detected containing annotation information, and determine the specific prompt word template corresponding to the class to be detected.

[0132] The detection module 13 is used to input the class to be detected containing annotation information and the prompt word generated based on the specific prompt word template into the large language model.

[0133] Optionally, the detection result includes the original risk score of the class to be detected and the original confidence of the original risk score; the original code contains multiple classes to be detected; based on this, the device also includes a risk level determination module 16, which is used to determine the confidence of the large language model according to the historical detection results output by the large language model; for any class to be detected among the multiple classes to be detected, according to the confidence of the large language model, update the historical confidence of any class to be detected; according to the updated confidence of each of the multiple classes to be detected and the preset weight, determine the risk score of the original code; according to the risk score of the original code, determine the risk level corresponding to the original code.

[0134] Figure 8 The device shown can perform Figures 1 to 7 For the method of the embodiment shown in the figure, the part not described in detail in this embodiment can be referred to Figures 1 to 7 The implementation process and technical effects of this technical solution refer to Figures 1 to 7 The description in the illustrated embodiment will not be repeated here.

[0135] In a possible design, the structure of the code detection device can be implemented as an electronic device, such as Fig. 9 As shown, the electronic device may include: a processor 21 and a memory 22. The memory 22 is used to store the electronic device to perform the above Figures 1 to 7 The program of the code detection method provided in the illustrated embodiment, the processor 21 is configured to execute the program stored in the memory 22 .

[0136] The program includes one or more computer instructions, wherein the one or more computer instructions can implement the following steps when executed by the processor 21:

[0137] Determine the class to be detected in the original code according to the syntax analysis tree corresponding to the original code, wherein the syntax analysis tree describes the dependency relationship between the class and the method in the original code;

[0138] Inputting different code snippets in the class to be detected and prompt words describing method calling rules in the class into a large language model, so that the large language model outputs a call relationship list of the class to be detected;

[0139] The class to be detected and the call relationship list of the class to be detected are input into the large language model, so that the large language model outputs the detection result of the class to be detected.

[0140] Optionally, the processor 21 is further configured to execute the aforementioned Figures 1 to 7 All or part of the steps in the illustrated embodiments.

[0141] The structure of the electronic device may further include a communication interface 23 for the electronic device to communicate with other devices or a communication network.

[0142] In addition, an embodiment of the present invention provides a non-transitory machine-readable storage medium for storing computer software instructions used by the above electronic device, which includes instructions for executing the above Figure 1 to Figure 7 The procedures involved in the code detection method shown.

[0143] In addition, an embodiment of the present invention provides a computer program product. The computer program product includes a computer program or an instruction. When the computer program or the instruction is executed by a processor, the processor is enabled to implement the above Figure 1 to Figure 7 The steps or functions of the code detection method shown.

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

Claims

1. A code detection method, characterized in that: include: Determine the class to be detected in the original code according to the syntax analysis tree corresponding to the original code, wherein the syntax analysis tree describes the dependency relationship between the class and the method in the original code; Inputting different code snippets in the class to be detected and prompt words describing method calling rules in the class into a large language model, so that the large language model outputs a call relationship list of the class to be detected; The class to be detected and the call relationship list of the class to be detected are input into the large language model, so that the large language model outputs the detection result of the class to be detected.

2. The method according to claim 1, characterized in that The class to be detected includes a first code snippet and a second code snippet; and determining the class to be detected in the original code according to the syntax analysis tree corresponding to the original code includes: According to the syntax analysis tree corresponding to the original code, determine the first code fragment containing the first method and the second method in the original code and the second code fragment containing the second method and the third method in the original code, the first method is located before the second method, and the second method is located before the third method.

3. The method according to claim 2, characterized in that The step of inputting different code snippets in the class to be detected and prompt words describing method calling rules in the class into a large language model so that the large language model outputs a call relationship list of the class to be detected includes: Inputting the first code snippet and the prompt word into a large language model, so that when there is a calling relationship between methods in the first code snippet, the large language model outputs a first calling relationship list of the first code snippet; In response to the output of the first call relationship list, the second code snippet and the prompt word are input into a large language model, so that when there is no call relationship between the methods in the second code snippet, the first call relationship list is output as the call relationship list of the class to be detected.

4. The method according to claim 2 or 3, characterized in that: The first method comprises an entry method, and the second method and the third method comprise different non-entry methods.

5. The method according to claim 1, characterized in that After determining the class to be detected in the original code, the method further includes: Get the preset filtering keywords; Based on the preset filtering keywords, the class to be detected is processed to obtain a filtered class to be detected; The step of inputting different code snippets in the class to be detected and prompt words describing method call rules in the class into a large language model comprises: The different code snippets in the filtered class to be detected and the prompt words describing the method calling rules in the class are input into the large language model.

6. The method according to claim 1, characterized in that The step of inputting the class to be detected and the call relationship list of the class to be detected into the large language model so that the large language model outputs the detection result of the class to be detected includes: Generate a comprehensive prompt word according to a general prompt word template containing user demand information and a call relationship list of the class to be detected; The class to be detected and the comprehensive prompt word are input into the large language model, so that the large language model outputs a detection result of the class to be detected.

7. The method according to claim 6, characterized in that The method further comprises: Obtaining the semantic similarity between the comprehensive prompt word and the class to be detected, and the structural matching degree between the comprehensive prompt word and the words included in the class to be detected; Determining the correlation coefficient between the comprehensive prompt word and the class to be detected according to the semantic similarity and the structural matching degree; If the correlation coefficient is less than a preset threshold, the class to be detected containing annotation information is obtained, and a specific prompt word template corresponding to the class to be detected is determined; The step of inputting the class to be detected and the comprehensive prompt word into the large language model comprises: The class to be detected containing annotation information and the specific prompt word template are input into the large language model.

8. The method according to claim 1, characterized in that: The detection result includes the original risk score of the class to be detected and the original confidence of the original risk score; the original code includes a plurality of the classes to be detected; After obtaining the detection result, the method further includes: Determining the confidence of the large language model according to the historical detection results output by the large language model; For any class to be detected among the multiple classes to be detected, updating the historical confidence of any class to be detected according to the confidence of the large language model; Determining the risk score of the original code according to the updated confidences and preset weights of the multiple classes to be detected; A risk level corresponding to the original code is determined according to the risk score of the original code.

9. An electronic device, characterized in that: include: A memory and a processor; wherein the memory stores executable code, and when the executable code is executed by the processor, the processor executes the code detection method according to any one of claims 1 to 8.

10. A non-transitory machine-readable storage medium, characterized in that: The non-transitory machine-readable storage medium stores executable code, and when the executable code is executed by a processor of an electronic device, the processor is caused to execute the code detection method according to any one of claims 1 to 8.