Large language model intelligent contract defect detection method based on level protection strategy

By using soft hints generated by GNNs to guide pre-trained LLMs for smart contract defect detection, this technology solves the problem of not being able to understand the semantics and structure of code in existing technologies, and achieves efficient and accurate smart contract vulnerability detection.

CN121118902APending Publication Date: 2025-12-12THE THIRD RES INST OF MIN OF PUBLIC SECURITY
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202511187806.0
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2025-08-25
Publication Date
2025-12-12

AI Technical Summary

Technical Problem

Existing smart contract vulnerability detection technologies suffer from the inability to fully understand code semantics and structure, and large language models (LLMs) have high computational costs when handling deep structure perception tasks.

Method used

A method for detecting defects in smart contracts using a large language model based on a graded protection strategy is adopted. The method generates soft hints by analyzing the code attribute graph (CPG) of the smart contract through a GNN, and inputs them into a pre-trained LLM with frozen parameters. This method integrates structural analysis and semantic reasoning to guide the LLM in defect detection.

Benefits of technology

It achieves efficient, accurate, and comprehensive smart contract defect detection, avoiding expensive fine-tuning processes and improving the comprehensiveness and accuracy of detection.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN121118902A_ABST
    Figure CN121118902A_ABST
Patent Text Reader

Abstract

The invention discloses a large language model intelligent contract defect detection method based on a level protection strategy, comprising the following steps: analyzing and processing a code attribute graph (CPG) of an intelligent contract based on GNN, and generating a soft prompt which is an LLM-oriented learnable continuous instruction vector; and adding the generated soft prompt into a text instruction, and directly inputting the text instruction into a large-scale, parameter-freezing and pre-trained language model to guide the language model to detect the defects of the smart contract. According to the scheme, structural analysis of GNN and semantic reasoning of LLM are cooperatively fused, and the intelligent contract can be accurately, comprehensively, efficiently and safely analyzed.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This invention relates to network security technology, specifically to smart contract security analysis technology. Background Technology

[0002] Blockchain technology has become a revolutionary force, fundamentally reshaping digital trust and interaction. At its core, blockchain is a distributed, immutable, and transparent ledger where transactions are recorded in blocks and cryptographically linked together. This decentralized architecture, maintained by a peer-to-peer network, eliminates the need for a central intermediary, creating a trustless environment that guarantees data integrity and provenance. The inherent characteristics of blockchain, including decentralization, immutability, and transparency, have enabled its applications to extend beyond cryptocurrencies into areas such as supply chain management, digital identity, and finance.

[0003] The transformative potential of blockchain is most profoundly demonstrated through smart contracts. Smart contracts refer to modern blockchain-based programs (typically written in high-level languages ​​such as Solidity) deployed on a decentralized ledger. These contracts can execute automatically, automatically fulfilling the terms of the agreement when predetermined conditions are met. This automation eliminates ambiguity, reduces execution costs, and ensures the fulfillment of contractual obligations without relying on a trusted third party. The synergy between blockchain and smart contracts is fundamental; blockchain provides a secure, decentralized environment for the operation of smart contracts, while smart contracts provide dynamic, programmable logic, making blockchain a versatile platform for building decentralized applications (DApps).

[0004] The powerful features of smart contracts (such as immutability and autonomous execution) also bring huge security risks. Once deployed to the blockchain, the code of a smart contract is usually immutable. Therefore, any security vulnerabilities in the code become permanent exploitable vulnerabilities that could lead to catastrophic economic losses[8]. High-profile events, such as the DAO attack, highlight the urgent need to establish a robust and effective vulnerability detection mechanism before deployment.

[0005] Current research on smart contract vulnerability detection mainly includes two aspects:

[0006] On the one hand, methods such as formal verification, symbolic execution, and convolutional neural networks (CNNs) excel at capturing complex structural and relational properties in code. While formal verification can provide mathematical security guarantees, it often requires significant human intervention from experts, and its effectiveness is limited by the completeness of predefined rules. Conversely, symbolic execution faces the challenge of path explosion and struggles to scale to complex contracts. Generative neural networks (GNNs) represent code as graphs and can effectively learn structural patterns related to vulnerabilities, but their inherent limitation lies in their inability to fully understand the natural language semantics embedded in the code, such as the intent behind variable and function names.

[0007] On the other hand, large language models (LLMs) like DeepSeek Coder have demonstrated unprecedented capabilities in understanding the semantics and logical intent of code. However, when dealing with deep structure-aware tasks, LLMs may exhibit "structural blind spots," ignoring complex vulnerability patterns arising from the interaction of control and data flows. More importantly, the computational cost of fully fine-tuning these large-scale models for specific tasks is prohibitive for most development solutions. Summary of the Invention

[0008] To address the limitations of existing smart contract vulnerability detection technologies in blockchain, this invention aims to provide a method for detecting smart contract defects based on a graded protection strategy using a large language model. This solution integrates GNN structural analysis and LLM semantic reasoning, enabling accurate, comprehensive, and efficient security analysis of smart contracts.

[0009] To achieve the above objectives, the present invention provides a method for detecting defects in smart contracts based on a large language model using a graded protection strategy. This method analyzes and processes the code attribute graph (CPG) of a smart contract using a Generative Neural Network (GNN) and generates soft hints, which are learnable continuous instruction vectors oriented towards an LLM (Learning Module Model). The generated soft hints are added to text instructions and directly input into a large, pre-trained language model with frozen parameters to guide the language model in detecting defects in smart contracts.

[0010] Furthermore, the detection method includes a step of preprocessing the smart contract source code and converting it into a code property graph (CPG).

[0011] Furthermore, the detection method, when converting smart contract source code into a code attribute graph (CPG), includes:

[0012] First, the contract source code is parsed into an AST, where each node in the AST represents a code structure and the edges represent their parent-child relationships.

[0013] Next, a control flow graph (CFG) is overlaid on top of the AST;

[0014] Next, inject the data flow graph (DFG) and use DFG edges to connect the definition points and usage points of variables;

[0015] Finally, the CPG is integrated to generate a unified CPG.

[0016] Furthermore, the detection method employs a GNN encoder to extract the structural features of the code attribute graph (CPG) of the smart contract source code, generates a structural embedding containing its key structural characteristics, and maps the generated structural embedding to a learnable continuous instruction vector oriented towards LLM as a soft cue by connecting a projection layer.

[0017] Furthermore, the connection projection layer adds it to the beginning of the text embedding sequence by aligning the dimensions and semantic space of the structure embedding with the text embedding vector of the LLM.

[0018] Furthermore, the detection method also includes a step of training the GNN encoder and the connected projection layer.

[0019] Furthermore, in the detection method, the weights of the projection layer are trained together with the GNN encoder, enabling the projection layer to learn the mapping from structural patterns to semantic concepts.

[0020] To achieve the above objectives, the present invention also provides a computer-readable storage medium having a program stored thereon, which, when executed by a processor, implements the steps of the above-described method for detecting defects in large language model smart contracts based on a graded protection strategy.

[0021] To achieve the above objectives, the present invention also provides a processor for running a program that, when running, executes the steps of the above-described method for detecting defects in large language model smart contracts based on a graded protection strategy.

[0022] To achieve the above objectives, the present invention also provides a terminal device, the device including a processor, a memory, and a program stored in the memory and executable on the processor, wherein the program code is loaded and executed by the processor to implement the steps of the above-described method for detecting defects in large language model smart contracts based on graded protection strategies.

[0023] To achieve the above objectives, the present invention also provides a computer program product that, when executed on a data processing device, is suitable for performing the steps of the above-described method for detecting defects in large language model smart contracts based on a graded protection strategy.

[0024] The present invention provides a method for detecting defects in smart contracts based on a graded protection strategy using a large language model. Addressing the problems faced by existing smart contract defect detection technologies, this invention presents an innovative and efficient hybrid architecture: a GNN-hint-based LLM architecture. This architecture synergistically integrates the structural analysis of GNNs and the semantic reasoning of LLMs, effectively improving the accuracy, comprehensiveness, and efficiency of smart contract defect detection, and effectively overcoming the problems existing in current technologies.

[0025] This invention provides a method for detecting defects in smart contracts using large language models based on a graded protection strategy. By freezing the parameters of the LLM (Language Modeling) system, the method avoids the huge computational overhead of fine-tuning, thus making it highly operable. The training process only updates the lightweight GNN and the connection projection layers, enabling it to learn to transform structural patterns into a format that the LLM can process. This design produces a modular, "plug-and-play" prompt generator that can be paired with future, more powerful LLMs without retraining. Attached Figure Description

[0026] The present invention will be further described below with reference to the accompanying drawings and specific embodiments.

[0027] Figure 1 This is a framework example diagram of the large language model smart contract defect detection system based on the graded protection strategy in this invention.

[0028] Figure 2 This is an example diagram of the training and deployment architecture for smart contract vulnerability detection in the large language model smart contract defect detection system based on the graded protection strategy in this invention. Detailed Implementation

[0029] To make the technical means, creative features, objectives and effects of this invention easier to understand, the invention will be further described below with reference to specific illustrations.

[0030] Existing blockchain smart contract defect detection solutions based on traditional program analysis and graph neural network (GNN) technology suffer from a lack of in-depth semantic understanding; while those based on large language model (LLM) technology suffer from ignoring structural details and are too costly to adjust for specific tasks.

[0031] In response, this invention provides an innovative and efficient hybrid architecture, namely the GNN-hint-based LLM architecture, which integrates GNN and LLM for smart contract defect detection.

[0032] Based on the above architecture, this invention provides a method for detecting defects in smart contracts based on a graded protection strategy using a large language model. In this method, the source code of the smart contract is first represented as a rich code attribute graph (CPG), which integrates syntax, control flow, and data flow information, and can comprehensively capture its syntax, control flow, and data flow characteristics.

[0033] Then, the generated code attribute graph CPG is analyzed and processed using GNN to generate corresponding structural soft hints. These structural soft hints are specifically formed by learnable continuous instruction vectors oriented towards LLM, thereby extracting the structural essence of smart contracts.

[0034] Finally, the generated structural soft hints are added to the text instructions and directly input into a large, frozen-parameter, pre-trained language model to guide the large frozen language model in detecting smart contract defects.

[0035] This detection method effectively combines its powerful semantic analysis with structural awareness without the need for costly fine-tuning; at the same time, the LLM detection method prompted by GNN can effectively, accurately and comprehensively detect defects in smart contracts.

[0036] The following section specifically describes the implementation scheme of the large language model smart contract defect detection method based on the graded protection strategy provided in this invention.

[0037] This invention presents a method for detecting defects in smart contracts based on a graded protection strategy using a large language model. This method is mainly based on a graph neural network (GNN) for structural code representation, a large language model (LLM) for semantic code understanding, and a modal alignment mechanism that connects the two.

[0038] The present invention preprocesses the source code of a smart contract into a code attribute graph (CPG). The CPG unifies the abstract syntax graph (AST), control flow graph (CFG), and data flow graph (DFG) into a single structure, providing an overall view that can not only simulate the syntax of the code but also simulate the execution paths and data dependencies between different parts of the contract.

[0039] This rich and multifaceted representation is crucial for smart contract vulnerability detection. Many vulnerabilities (such as reentrancy or unchecked low-level calls) are not related to a single token, but rather to emerging properties of interactions between different code components. For example, reentrancy vulnerabilities depend on a specific sequence: within the same function, an external call representing a control flow property and a state update representing a data flow property follow closely together.

[0040] To address this, the present invention introduces a graph neural network (GNN) to learn and process the code attribute graph (CPG) of the smart contract source code, thereby extracting the structural features of the smart contract source code and generating a structural embedding containing its key structural characteristics.

[0041] Large Language Models (LLMs) are driven by a converter architecture and large-scale pre-training on massive amounts of text and code. Unlike traditional models trained for specific tasks, LLMs develop a broad and deep understanding of syntax, semantics, and logical reasoning from the training data; when applied to source code, they can not only parse the syntax but also understand the semantic intent behind the code.

[0042] This invention introduces a pre-trained and frozen large-scale language model (LLM) (such as DeepSeek Coder) as a powerful inference engine. By freezing the model, its rich existing knowledge is directly utilized without costly fine-tuning. The role of the LLM in this solution is not to learn vulnerabilities from scratch, but to apply its understanding of deep semantics to the information provided to it, thereby making informed judgments about the security status of contracts.

[0043] This invention incorporates both Graph Neural Networks (GNNs) and Large Language Models (LLMs). GNNs operate in a topological vector space, and their proximity is defined by structural similarity within the graph. In contrast, LLMs operate in a semantic vector space, and their proximity is defined by conceptual and linguistic similarity. Furthermore, directly converting the graph into a text sequence would obscure the complex structural patterns that GNNs aim to capture.

[0044] In large-scale language models (LLMs), prompts are instructions that guide the model's output. Traditional prompts are usually hard prompts, which are manually created text strings.

[0045] To address this, the present invention further constructs a soft cue mechanism to generate soft cues for large language models (LLMs). These soft cues are specifically learnable continuous instruction vectors for LLMs, which can be added to the input sequence. Since these cue vectors are differentiable, end-to-end optimization can be performed using gradient descent, allowing the model to learn the optimal way to self-guide for a given task.

[0046] Based on the aforementioned soft suggestion mechanism, the architecture of this invention introduces a modal alignment mechanism that connects the graph neural network (GNN) and the large language model (LLM). This modal alignment mechanism is specifically implemented through a projection layer, which is configured as a key converter between the two modes to map the structure embedded by the graph neural network (GNN) to the vector in the semantic embedding space of the large language model (LLM).

[0047] Furthermore, in this invention, the weights of the projection layer are trained together with the graph neural network (GNN). This training process enables the projection layer to learn the mapping from structural patterns to semantic concepts. For example, the projection layer can learn to convert the "function node with many outgoing call edges" represented by the GNN into a vector that is interpreted by the LLM as a concept such as "highly complex interaction point".

[0048] Building upon this, the projection layer in this invention can generate corresponding soft cues (i.e., learnable continuous instruction vectors for LLM) by using structural embeddings generated by the Graph Neural Network (GNN) based on the structural features of the code attribute graph (CPG) of smart contract source code, which contain its key structural characteristics. These soft cues are then concatenated with the embeddings of the LLM text input. The resulting combined sequence is directly input into the frozen LLM, effectively injecting structural knowledge extracted from the GNN as a guiding prefix for the powerful semantic analysis of the large-scale language model (LLM). This achieves the utilization of rich graph-based insights to guide the reasoning process of the large-scale language model (LLM), realizing a true integration of structural and semantic understanding.

[0049] Based on the above scheme, it can be seen that the hybrid architecture introduced in this invention effectively integrates GNN and frozen LLM for smart contract vulnerability detection. GNN acts as a learnable hint generator, converting graph-based structural information into a format that LLM can process locally. At the same time, the soft hint strategy does not require fine-tuning of LLM, only training a lightweight GNN and a projection layer, making this invention more efficient and convenient than full fine-tuning.

[0050] The following specific examples further illustrate the implementation process of the method for detecting defects in smart contracts based on the graded protection strategy for large language models proposed in this invention.

[0051] This example demonstrates how to implement a method for detecting defects in large language model smart contracts based on a graded protection strategy. This method can be structured into a corresponding software program, forming a software system for detecting defects in large language model smart contracts based on a graded protection strategy. During runtime, this software program will execute the aforementioned method for detecting defects in large language model smart contracts based on a graded protection strategy, and simultaneously store the results in a suitable storage medium for the processor to retrieve and execute.

[0052] The smart contract defect detection software system based on the graded protection strategy formed in this example is a hybrid architecture. It uses a soft prompting mechanism to work together GNN and LLM, so that GNN is responsible for encoding the complex structure and relational information of the contract code, while LLM is built to handle the deep semantic reasoning required for vulnerability analysis.

[0053] By leveraging structural soft cues, the embedding space of a frozen-parameter large language model (LLM) is directly injected, allowing the explicit structural cues provided by the GNN to guide the powerful pre-trained inference engine of the LLM, thus completely avoiding the costly fine-tuning process. Through end-to-end training, the model only needs to optimize the parameters of the GNN and the projection layer connecting the GNN and the LLM, thereby conveying structural knowledge in a way that the LLM can "understand".

[0054] See Figure 1 In this example, the large language model smart contract defect detection software system 10 based on the graded protection strategy is mainly composed of a graphical representation module 11, a learnable hint generator 12, and a frozen large language model LLM 13.

[0055] The graphical representation module 11 in this system is configured to preprocess the source code of smart contracts and convert it into a rich code property graph (CPG), which includes syntax, control flow, and data flow.

[0056] As further explanation, the graphic representation module 11 can be configured as an offline module.

[0057] The learnable cue generator 12 in this system, as the trainable part of the system, consists of a GNN encoder and a projection layer. The GNN encoder is configured to process the CPG generated by the graph representation module 11 and generate structural embeddings. The projection layer is configured to interact with the GNN encoder to map the structural embeddings generated by the GNN encoder to soft cues, i.e. learnable continuous vector instructions oriented towards LLM, and to concatenate them with the text embedding sequence.

[0058] The frozen large language model LLM13 in this system is configured to receive soft hints and text hints generated by the learnable hint generator 12 and perform the final vulnerability classification.

[0059] As further explanation, the parameters of this frozen large language model LLM13 are kept frozen during training.

[0060] The resulting smart contract defect detection software system based on the graded protection strategy first parses the smart contract's source code and converts it into a code attribute graph (CPG). This graph is then fed into a GNN encoder, which, through graph convolution and information propagation, generates a fixed-dimensional structure embedding vector. This vector encapsulates the contract's syntax, control flow, and data flow information.

[0061] Simultaneously, the original smart contract source code is combined with the instruction prompts for specific tasks, and these texts are then sent to the tokenizer of the Frozen Large Language Model (LLM) for encoding. Finally, through the input embedding layer of the Frozen Large Language Model (LLM), they are mapped into a series of text embedding vectors.

[0062] In this model, the structural embedding vectors generated by the GNN encoder are passed through a trainable projection layer. This projection layer aligns its dimensions and semantic space with the embedding vectors of the LLM. The transformed vectors are then configured as "structural soft cues" and added to the beginning of the text embedding sequence. This concatenated sequence forms the final input to the Frozen Large Language Model (LLM). The LLM processes this input, fusing structural and semantic information. The final hidden state, corresponding to a specific classification label, is input into a simple, trainable linear classifier head, which outputs a binary prediction result.

[0063] See Figure 2 The software system for detecting defects in smart contracts based on the graded protection strategy mainly includes a training phase and a detection phase during deployment and operation.

[0064] During the training phase, firstly, for the smart contract source code dataset, each unstructured program is converted into a CPG (Converter-Generated Parselves-Grammar). Converting to CPG removes redundant descriptive information while fully preserving the code's syntax, control flow, and data flow relationships. This rich structured data is then input into the GNN encoder for training. Simultaneously, the original source code is paired with task instructions in preparation for semantic analysis.

[0065] Building upon this, the output of the GNN encoder is mapped through a trainable projection layer to a "soft cue" (i.e., a dense vector containing the essence of the contractual structure). This soft cue is added to the tokenized text input and fed into a pre-trained but frozen large language model (LLM). The prediction model is then trained based on the output of the LLM. Throughout the training process, only the weights of the GNN encoder and projection layer are updated; the weights of the LLM remain unchanged.

[0066] During the detection phase, the new smart contracts to be audited undergo the same CPG transformation, and the generated graph and original source code are fed into a trained predictive model. The model outputs the vulnerability susceptibility of the contract, classifying it as "secure" or one of several vulnerability types.

[0067] In this system design, since the LLM is never fine-tuned, the trained cue generator (i.e., the GNN and projection layer) can be used as a modular, plug-and-play component. After training, the cue generator can be paired with any compatible, instruction-adjusted LLM code instance for detection without retraining.

[0068] As further clarification, during the training phase of this system, the corresponding dataset can use SmartBugs, a benchmark dataset recognized in the field of smart contract vulnerability analysis. Specifically, it focuses on the sb-curated subset, which has undergone meticulous manual annotation.

[0069] For data labeling, this system employs a contract-level labeling scheme for multi-label classification tasks, where a single contract can be associated with multiple vulnerability types simultaneously. Each contract in the dataset is assigned a multi-label encoding vector, i.e., y∈{0,1}. C , where C is the total number of vulnerability categories. Each dimension of the vector corresponds to a specific vulnerability type in the DASP 10 classification. If a contract is annotated as having a specific vulnerability in the sb-curated metadata, the corresponding entry in its label vector is set to 1; otherwise, it is set to 0. This approach allows the model to learn and predict any combination of vulnerabilities present in a single contract, reflecting real-world scenarios more accurately than single-label classification.

[0070] As a further explanation, in order to enable the model to capture a holistic view of the smart contract source code, the source code of each smart contract in the smart contract source code dataset is converted into a corresponding CPG.

[0071] The CPG generated here can unify various traditional program representations such as Abstract Syntax Tree (AST), Control Flow Graph (CFG), and Data Flow Graph (DFG) into a single structure graph, thereby enabling the execution of complex queries that span syntactic, semantic, and control flow relationships.

[0072] As a further explanation, the process of converting the source code of each smart contract in the smart contract source code dataset into the corresponding CPG in this system solution includes:

[0073] First, the smart contract source code is parsed into an AST using a Solidity parser adapted from ANTLR syntax. This AST accurately represents the syntactic structure of the code in a tree format, where each node represents a code structure (such as function declarations, conditional statements, variable assignments), and edges represent their parent-child relationships (such as function bodies containing lists of statements).

[0074] Next, a control flow graph (CFG) is overlaid on top of the AST.

[0075] In this step, a separate CFG is built for each function in the smart contract. In the CFG, nodes represent basic modules, and directed edges represent possible paths for control transfer; furthermore, CFG edges are added as a new edge type to the CPG, connecting the corresponding AST nodes.

[0076] Next, inject the data flow graph (DFG) and use DFG edges to connect the definition points and usage points of variables.

[0077] As an example, if a variable x is defined for a statement in this step, and a subsequent statement reads the value of x, a DFG edge will be added between the nodes representing these two statements.

[0078] Finally, the CPG is integrated to generate a unified CPG.

[0079] In this way, the model can track the data flow in the program, which is crucial for taint analysis and detection of vulnerabilities related to data manipulation. The nodes in the graph represent different code elements, such as distinguishing syntax elements (e.g., functions, variables), control blocks, and data entities; the edges represent different types of relationships, such as explicitly labeled syntactic parent-child edges (AST), control transfer edges (CFG), and data dependency edges (DFG).

[0080] As further explanation, this system uses a graph attention network (GAT) as the GNN encoder. Addressing the issue that different types of edges in heterogeneous graphs may have vastly different importance for identifying specific vulnerabilities, this system uses GAT to adaptively learn and focus on the neighbors and relationships most relevant to the current task.

[0081] Furthermore, the core of the GAT layer is a self-attention mechanism. For node i in the graph, its updated feature vector is calculated by summing the features of all nodes in its neighborhood. The attention coefficient of a node to its neighbors is calculated as follows:

[0082] First, calculate the unnormalized attention score e. ij :

[0083] e ij =a(Wh i ,Wh j (1)

[0084] Then, the attention scores of all neighbors are normalized using the softmax function to obtain the final attention coefficient: α. ij

[0085]

[0086] Where h i is the input feature vector of node i, W is the trainable weight matrix used for feature transformation, and a is the trainable weight vector of the attention mechanism.

[0087] Furthermore, to stabilize the learning process and capture more diverse neighborhood relationship patterns, this system introduces a multi-head attention mechanism into the GNN encoder. This mechanism executes K independent attention calculations in parallel and then concatenates the results:

[0088]

[0089] in and W kThese are the coefficients and weight matrices of the k-th attention head, respectively. σ is the non-linear activation function, and || represents the connection operation.

[0090] The GNN encoder formed based on the above scheme updates the node features of the input CPG through multiple GAT layers, and then summarizes all the node features into a single layer embedding to represent the structural information of the entire smart contract.

[0091] As a further example, this can be achieved through graph pooling (or readout) functions, such as averaging or summing all node vectors.

[0092] As further explanation, the GNN encoder generates a structure embedding vector. Meanwhile, LLM requires an input embedding vector of dimension d. LLM These two dimensions are usually not the same. In this system design, a projection layer is constructed for modal alignment to bridge the gap between them. This projection layer is specifically composed of a linear transformation matrix:

[0093]

[0094] This layer linearly transforms the output vector of the GNN into a "soft cue" vector and adjusts its dimension and numerical range according to the embedding space of the LLM:

[0095] P soft =W proj ·v GNN (4)

[0096] The training process of the projection layer not only achieves dimension alignment, but also learns cross-modal transformation in an end-to-end manner, learning to map the abstract structural patterns recognized by the GNN into semantic concepts that the LLM can understand.

[0097] As an example, the model is analyzing a contract with a reentrancy vulnerability. Through learning, the GNN can identify a specific subgraph pattern: in the control flow path, an external call node is immediately followed by a state update node. The GNN encodes this pattern into a specific vector, `vreentrancy`. Meanwhile, the LLM, through its vast pre-training data, has seen thousands of secure and insecure contracts and formed the semantic concept of "reentrancy risk," which may be closely related to markers such as `call.value`, `require`, and `balance`, as well as specific code comments.

[0098] When the entire model is trained to classify this contract as "vulnerable," the loss is backpropagated to the GNN through the frozen LLM and projection layer. This process adjusts the weights of the GNN and the projection layer Wproj. As a result, the GNN can better generate unique and distinguishable vectors (vreentrancy) for reentrancy patterns. More importantly, Wproj learns how to transform the vreentrancy vector into a soft cue vector (psoft), which is "close" to the LLM's own semantic concept vector of "reentrancy risk" in the LLM's embedding space.

[0099] Therefore, the projection layer in this system can translate the language of GNN into the language of LLM, and effectively bridge the modal gap between structure and semantics by learning shared risk vocabulary.

[0100] As further explanation, this system solution uses concise static text prompts to provide task context for the LLM. The prompts are designed to be clear and direct, guiding the LLM to perform specific security analysis tasks.

[0101] Specifically, the prompt message is linked to the contract source code and fed into a pre-trained tokenizer associated with a pre-trained but frozen large language model (LLM). The tokenizer converts the text into a sequence of input IDs. These IDs are then mapped to a series of dense text embedding vectors input to an embedding layer.

[0102] Based on the foregoing, this system solution utilizes soft cues generated by the projection layer to achieve the fusion of structural and semantic embeddings. Specifically, a single soft cue vector generated by the projection layer is pre-placed at the beginning of the text embedding sequence. Therefore, the final input sequence processed by the LLM's transformer layer is E. This method effectively injects the overall structural summary extracted by the GNN as a guiding prefix, providing global structural context for the LLM's subsequent sequence analysis of the code text, thereby guiding its reasoning process.

[0103] As a further example, the LLM used for vulnerability classification in this system can choose the DeepSeek Coder model as the semantic inference engine. During the system's training process, all parameters of the DeepSeek Coder model, including the embedding layer and all transformer modules, are frozen and do not participate in gradient updates. It is only used as a powerful fixed feature extractor, utilizing its forward propagation capability. The LLM processes the fused input sequence and extracts the final hidden state (such as CLS Token or First Token) output vector corresponding to the first token. This vector is input into a small, trainable linear classification head with multiple output neurons, one for each vulnerability category. The Sigmoid activation function is applied independently to each neuron, generating a probability score vector.

[0104] As a further explanation, during training, although the gradient is calculated for the entire model, parameter updates are only applied to the weights of the GNN encoder and projection layer. All other parameters remain unchanged. Since a contract may contain multiple vulnerabilities, this task is defined as multi-label classification. The suitable loss function for this task is the binary cross-entropy (BCE) loss, which is calculated independently for each class label and then averaged. For a single contract sample i, the loss is the sum of the BCE losses for all C vulnerability classes:

[0105]

[0106] Where y ic p is the basic true value. ic This represents the predicted probability of the model for that class and sample. The total loss for N samples is the average of these individual losses.

[0107] Based on the above-mentioned smooth curve construction scheme based on linear and cubic splines, this embodiment of the invention also provides a computer-readable storage medium storing a program thereon, which, when executed by a processor, implements the steps of the above-mentioned method for detecting defects in large language model smart contracts based on graded protection strategies.

[0108] This invention also provides a processor for running a program, wherein the program executes the steps of the above-described method for detecting defects in large language model smart contracts based on a graded protection strategy.

[0109] This invention also provides a terminal device, which includes a processor, a memory, and a program stored in the memory and executable on the processor. The program code is loaded and executed by the processor to implement the steps of the above-described method for detecting defects in large language model smart contracts based on a graded protection strategy.

[0110] The present invention also provides a computer program product, which, when executed on a data processing device, is suitable for performing the steps of the above-described method for detecting defects in large language model smart contracts based on a graded protection strategy.

[0111] In the above embodiments, the descriptions of each embodiment have different focuses. For parts not described in detail in a certain embodiment, please refer to the relevant descriptions in other embodiments.

[0112] Those skilled in the art will clearly understand that, for the sake of convenience and brevity, the specific working processes of the systems, devices, and modules described above can be referred to the corresponding processes in the foregoing method embodiments, and will not be repeated here.

[0113] Those skilled in the art will understand that embodiments of the present invention can be provided as methods, systems, or computer program products. Therefore, the present invention can take the form of a completely hardware embodiment, a completely software embodiment, or an embodiment combining software and hardware aspects. Furthermore, the present invention can take the form of a computer program product embodied on one or more computer-usable storage media (including, but not limited to, disk storage, CD-ROM, optical storage, etc.) containing computer-usable program code.

[0114] This invention is described with reference to flowchart illustrations and / or block diagrams of methods, apparatus (systems), and computer program products according to embodiments of the invention. It will be understood that each block of the flowchart illustrations and / or block diagrams, and combinations of blocks in the flowchart illustrations and / or block diagrams, can be implemented by computer program instructions. These computer program instructions can be provided to a processor of a general-purpose computer, special-purpose computer, embedded processor, or other programmable data processing apparatus to produce a machine, such that the instructions, which execute via the processor of the computer or other programmable data processing apparatus, generate instructions for implementing the flowchart illustrations and / or block diagrams. Figure 1 One or more processes and / or boxes Figure 1 A device that provides the functions specified in one or more boxes.

[0115] These computer program instructions may also be stored in a computer-readable storage medium that can direct a computer or other programmable data processing device to function in a particular manner, such that the instructions stored in the computer-readable storage medium produce an article of manufacture including instruction means, which are implemented in a process Figure 1 One or more processes and / or boxes Figure 1 The function specified in one or more boxes.

[0116] These computer program instructions may also be loaded onto a computer or other programmable data processing equipment to cause a series of operational steps to be performed on the computer or other programmable equipment to produce a computer-implemented process, thereby providing instructions that execute on the computer or other programmable equipment for implementing the process. Figure 1 One or more processes and / or boxes Figure 1 The steps of the function specified in one or more boxes.

[0117] In a typical configuration, a computing device includes one or more processors (CPU), input / output interfaces, network interfaces, and memory.

[0118] Memory may include non-persistent memory in computer-readable media, such as random access memory (RAM) and / or non-volatile memory, such as read-only memory (ROM) or flash RAM. Memory is an example of computer-readable media.

[0119] Computer-readable media, including both permanent and non-permanent, removable and non-removable media, can store information using any method or technology. Information can be computer-readable instructions, data structures, program modules, or other data. Examples of computer storage media include, but are not limited to, phase-change memory (PRAM), static random access memory (SRAM), dynamic random access memory (DRAM), other types of random access memory (RAM), read-only memory (ROM), electrically erasable programmable read-only memory (EEPROM), flash memory or other memory technologies, CD-ROM, digital versatile optical disc (DVD) or other optical storage, magnetic tape, magnetic disk storage or other magnetic storage devices, or any other non-transfer medium that can be used to store information accessible by a computing device.

[0120] It should also be noted that 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 process, method, article, or apparatus. Unless otherwise specified, 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.

[0121] Those skilled in the art will understand that embodiments of the present invention can be provided as methods, systems, or computer program products. Therefore, the present invention can take the form of a completely hardware embodiment, a completely software embodiment, or an embodiment combining software and hardware aspects. Furthermore, the present invention can take the form of a computer program product embodied on one or more computer-usable storage media (including, but not limited to, disk storage, CD-ROM, optical storage, etc.) containing computer-usable program code.

[0122] The method, specific system unit, or part thereof of the present invention described above is a pure software architecture. It can be deployed via program code on physical media, such as hard disks, optical discs, or any electronic device (such as smartphones or computer-readable storage media). When a machine loads and executes the program code (e.g., a smartphone loads and executes it), the machine becomes an apparatus for implementing the present invention. The method and apparatus of the present invention can also be transmitted in program code form via transmission media, such as cables, optical fibers, or any transmission method. When the program code is received, loaded, and executed by a machine (e.g., a smartphone), the machine becomes an apparatus for implementing the present invention.

[0123] The foregoing has shown and described the basic principles, main features, and advantages of the present invention. Those skilled in the art should understand that the present invention is not limited to the above embodiments. The embodiments and descriptions in the specification are merely illustrative of the principles of the invention. Various changes and modifications can be made to the invention without departing from its spirit and scope, and all such changes and modifications fall within the scope of the present invention as claimed. The scope of protection of the present invention is defined by the appended claims and their equivalents.

Claims

1. A method for detecting defects in smart contracts based on a graded protection strategy using large language models, characterized in that, The code attribute graph (CPG) of smart contracts is analyzed and processed based on GNN, and soft hints are generated. The soft hints are learnable continuous instruction vectors oriented towards LLM. The generated soft hints are added to text instructions and directly input into a large, frozen-parameter, pre-trained language model to guide the language model to detect defects in smart contracts.

2. The method for detecting defects in large language model smart contracts based on a graded protection strategy according to claim 1, characterized in that, The detection method includes the step of preprocessing the smart contract source code and converting it into a code attribute graph (CPG).

3. The method for detecting defects in large language model smart contracts based on a graded protection strategy according to claim 2, characterized in that, The detection method, when converting smart contract source code into a code attribute graph (CPG), includes: First, the contract source code is parsed into an AST, where each node in the AST represents a code structure and the edges represent their parent-child relationships. Next, a control flow graph (CFG) is overlaid on top of the AST; Next, inject the data flow graph (DFG) and use DFG edges to connect the definition points and usage points of variables; Finally, the CPG is integrated to generate a unified CPG.

4. The method for detecting defects in large language model smart contracts based on a graded protection strategy according to claim 1, characterized in that, The detection method employs a GNN encoder to extract the structural features of the code attribute graph (CPG) of the smart contract source code, generates a structural embedding containing its key structural characteristics, and maps the generated structural embedding into a learnable continuous instruction vector oriented towards LLM as a soft cue by connecting a projection layer.

5. The method for detecting defects in large language model smart contracts based on a graded protection strategy according to claim 4, characterized in that, The connection projection layer adds it to the beginning of the text embedding sequence by aligning the dimensions and semantic space of the structure embedding with the text embedding vector of the LLM.

6. The method for detecting defects in large language model smart contracts based on a graded protection strategy according to claim 4, characterized in that, In the detection method, the weights of the projection layer are trained together with the GNN encoder, enabling the projection layer to learn the mapping from structural patterns to semantic concepts.

7. A computer-readable storage medium having a program stored thereon, characterized in that, When the program is executed by the processor, it implements the steps of the method for detecting defects in large language model smart contracts based on the graded protection strategy as described in any one of claims 1-6.

8. A processor for running a program, characterized in that, When the program runs, it performs the steps of the method for detecting defects in large language model smart contracts based on the graded protection strategy as described in any one of claims 1-6.

9. A terminal device, comprising a processor, a memory, and a program stored in the memory and executable on the processor, characterized in that, The program code is loaded and executed by the processor to implement the steps of the method for detecting defects in large language model smart contracts based on the graded protection strategy as described in any one of claims 1-6.

10. A computer program product, characterized in that, When executed on a data processing device, it is suitable for performing the steps of the method for detecting defects in large language model smart contracts based on a graded protection strategy as described in any one of claims 1-6.