A source code classification model robustness enhancement method, system and processor

CN116842515BActive Publication Date: 2026-11-03HUAZHONG UNIV OF SCI & TECH
View PDF 5 Cites 0 Cited by

Patent Information

Application Number
CN202310812492.3
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2023-07-04
Publication Date
2026-11-03
Estimated Expiration
2043-07-04

AI Technical Summary

Technical Problem

Enhancing the robustness of deep learning models in source code classification faces challenges such as insufficient data, complex feature extraction, and adversarial attacks. Existing methods struggle to effectively improve the robustness of these models.

Method used

By generating multiple style templates, the input code training set is transformed into new code of different styles, expanding the training set and converting it into code images. A contrastive learning method is used to iteratively update the feature extractor and sample matching pairs, extract invariant features and train the classifier, thereby enhancing the robustness of the model.

Benefits of technology

It improves the model's adaptability to diverse code texts, enhances its defense against adversarial attacks, reduces the success rate of attacks, and is applicable to various classification models such as vulnerability detection, code author identification, and clone detection.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN116842515B_ABST
    Figure CN116842515B_ABST
Patent Text Reader

Abstract

The present application relates to a kind of source code classification model robustness enhancement method, system and processor based on invariant feature, the method includes: non-robust feature is combined, to generate multiple style templates, using code conversion program, the code in input code training set is transformed into new code of different style, to obtain the transformed code training set of new code composition, input code training set is combined with transformed code training set and is expanded training set, code text in expansion training set is converted into code picture;Code picture is converted into the vector required for model training by data preprocessing, in expansion training set, randomly extract the sample of the same category and input to feature extractor, using the method of contrast learning, iteratively update feature extractor and sample matching pair and extract target feature, the invariant feature extracted is input into classifier and is trained, to output model the system includes: directional training set expansion module and strong robustness classification model training module.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This invention relates to the field of enhancing the robustness of source code classification models in deep learning model robustness research, and particularly to a method, system, and processor for enhancing the robustness of source code classification models based on invariant features. Background Technology

[0002] With the development and popularization of computer technology, software has been widely used in various fields. However, this software has also brought about security problems. To solve these problems, source code classification technology has become an important direction in security research, including techniques such as vulnerability detection, code author identification, and clone detection. Compared with traditional methods, deep learning models can learn various types of vulnerability patterns, coding styles, habits, syntax, structure, and style features, thereby achieving automated vulnerability discovery, automatic code author identification, and detection of similar code clones. Deep learning models have higher accuracy and scalability, and can more accurately identify the structure and patterns in code.

[0003] Currently, methods for enhancing model robustness are widely used in the training of deep learning models, and mainly include the following:

[0004] 1. Data augmentation: Transform the training data to increase data diversity and reduce overfitting of the model to a specific data distribution;

[0005] 2. Regularization: Add a regularization term to the loss function to limit the size of the weight parameters and prevent the model from overfitting;

[0006] 3. Adversarial training: Improve the robustness of the model by having it predict the correctness of adversarial examples;

[0007] 4. Classification network structure design: Use more robust network structures, such as convolutional neural networks, to improve the generalization ability of the model.

[0008] CN114503108A discloses a method and system for protecting a trained machine learning model using one or more processors in a computing system. By adding adversarial protection to one or more trained machine learning models, one or more reinforced machine learning models can resist adversarial attacks.

[0009] CN108108184A discloses a source code author identification method based on deep learning networks, belonging to the field of Web mining and information extraction. The invention includes the following steps: constructing a source code dataset and preprocessing the source code data; extracting source code features based on a continuous n-gram code segment model; training a deep learning network model based on training source code file samples; using the trained deep learning network model to identify the author of the source code file, and outputting the author identification result.

[0010] CN113760358A discloses an adversarial example generation method for source code classification models, including data preprocessing, extraction of code transformation methods, selection of candidate transformation methods, execution of transformation, attack testing, and a reward mechanism. This invention extracts executable adversarial operations based on the structural information of the source code and introduces the ideas of Markov decision processes and temporal difference algorithms. While executing adversarial operations, it incorporates the influence coefficient of the operation to guide the selection and execution of the operation, and continuously improves the adversarial example generation method through decision learning.

[0011] However, robustness enhancement methods for deep learning models are not entirely applicable to source code classification models. Enhancing the robustness of source code classification models still faces several challenges:

[0012] Insufficient data is a significant challenge. Deep learning models require vast amounts of data for training, while the amount of data in the source code domain is often relatively small. Therefore, effectively utilizing limited data for model training and improving model robustness is a key difficulty.

[0013] Feature extraction is a challenging problem in the source code domain, where code features are often expressed in complex ways. Extracting effective code features is difficult because different types of code may require different feature extraction methods. Furthermore, feature extraction can be influenced by factors such as coding style and naming conventions; therefore, combating these interfering factors is also a problem that needs to be addressed.

[0014] Adversarial attacks are a common problem in enhancing the robustness of deep learning models. These attacks involve attackers intentionally interfering with the model to deceive it. In the realm of source code, adversarial attacks may target different features of the code. Therefore, how to enhance the robustness of the model and avoid interference from adversarial attacks is a problem that needs to be solved.

[0015] Furthermore, on the one hand, there are differences in understanding among those skilled in the art; on the other hand, the applicant studied a large number of documents and patents when making this invention, but due to space limitations, not all details and contents were listed in detail. However, this does not mean that the present invention does not possess the features of these prior art. On the contrary, the present invention already possesses all the features of the prior art, and the applicant reserves the right to add relevant prior art to the background art. Summary of the Invention

[0016] To address the shortcomings of existing technologies, this invention aims to provide a method, system, and processor for enhancing the robustness of source code classification models, particularly a method, system, and processor for enhancing the robustness of source code classification models based on invariant features, thereby at least overcoming the difficulty in improving the robustness of existing source code models. This invention summarizes the characteristics of source code and existing methods for enhancing model robustness, expands the training set by changing the code style, and improves the robustness of the source code classification model, while also defending against attacks targeting the source code model.

[0017] This invention discloses a robustness enhancement method for source code classification models based on invariant features, characterized in that the method includes at least the following steps:

[0018] Non-robust features are combined to generate templates of various styles. A code transformation program is used to transform the code in the input code training set into new code of different styles to obtain a transformed code training set composed of new code. The input code training set and the transformed code training set are combined into an expanded training set. The code text in the expanded training set is converted into code images.

[0019] Data preprocessing transforms the code images into vectors required for model training. Samples of the same category are randomly selected from the expanded training set, paired, and input into the feature extractor. Contrastive learning is used to iteratively update the feature extractor and sample matching pairs and extract target features. The extracted invariant features are then input into the classifier for training to output the model.

[0020] According to a preferred embodiment, “combining non-robust features to generate multiple style templates” includes: analyzing existing source code classification models and attack methods targeting these source code classification models, summarizing the transformation target features and transformation patterns of attack code samples generated by the attack methods, wherein the attacked target features are classified and grouped as non-robust features, and different combinations of non-robust features are extracted to form multiple mutually distinguishable style templates.

[0021] According to a preferred embodiment, "using a code transformation program to transform the code in the input code training set into new code of different styles to obtain a transformed code training set composed of new code, and combining the input code training set and the transformed code training set into an expanded training set" includes: using a code transformation program on the input code training set, performing targeted style template transformation on the code in the input code training set according to a code style template, generating new code with the same semantics but different styles, wherein one style template generates a set of transformed code training sets, and the input code training set and the transformed code training set are combined into an expanded training set.

[0022] According to a preferred embodiment, "converting code text in the expanded training set into code images" includes: using a text-to-image tool on the code text in the expanded training set to generate specially processed code images based on the input code text.

[0023] According to a preferred embodiment, "data preprocessing" includes converting the preprocessed image into a vector that can be used for model training, wherein the preprocessing includes, but is not limited to, scaling, cropping, and / or normalization.

[0024] According to a preferred embodiment, “randomly selecting samples of the same category from the expanded training set for pairing and inputting them into the feature extractor” includes: randomly selecting samples of the same category but different training sets from the expanded training set consisting of the input code training set and the transform code training set for pairing.

[0025] According to a preferred embodiment, “iteratively updating the feature extractor and sample matching pairs using a contrastive learning method and extracting target features” includes: dividing the model into two parts, a feature extractor and a classifier; inputting randomly selected sample pairs of the same category into the feature extractor; calculating the sample difference through a contrastive learning loss function; iteratively updating the feature extractor and sample matching pairs; replacing the random sample matching pairs with new sample matching pairs; and iteratively training until the feature extractor training converges.

[0026] According to a preferred embodiment, “training the classifier by inputting the extracted invariant features” includes: inputting the latest sample matching pairs into the feature extractor to extract the target features, and inputting these target features into the classifier for training until the classifier training converges.

[0027] This invention discloses a robustness enhancement system for source code classification models based on invariant features, comprising:

[0028] The targeted training set expansion module is used to combine non-robust features to generate templates of various styles. The code transformation program transforms the code in the input code training set into new code of different styles to obtain a transformed code training set composed of new code. The input code training set and the transformed code training set are combined into an expanded training set. The code text in the expanded training set is converted into code images.

[0029] The robust classification model training module is used to convert code images into vectors required for model training through data preprocessing. Samples of the same category are randomly selected from the expanded training set, paired, and input into the feature extractor. The feature extractor and sample matching pairs are iteratively updated using a contrastive learning method to extract target features. The extracted invariant features are input into the classifier for training to output the model.

[0030] According to a preferred embodiment, the directional training set expansion module includes a non-robust feature combination to generate style templates submodule, a directional code transformation expansion submodule, and a code text to code image conversion submodule; the robust classification model training module includes a data preprocessing submodule, a random sampling of similar sample pairs from the expanded training set submodule, a feature extractor and sample matching pair iterative update submodule, and an invariant feature training model classifier submodule.

[0031] According to a preferred embodiment, the non-robust feature combination generation style template submodule is used to analyze existing source code classification models and attack methods against them, summarize the transformation target features and transformation patterns of attack methods to generate attack code samples, refer to the attacked target features as non-robust features, classify and group non-robust features, and extract different combinations of features to form a variety of mutually distinguishable style templates.

[0032] According to a preferred embodiment, the directional code transformation expansion submodule is used to apply a code synonym transformation procedure to the input code training set, and to perform directional template style transformation on the code samples according to the code style template to generate new codes with the same semantics but different styles. One template style generates a set of transformed code training sets, and the input code training set and the transformed code training set are combined into the expansion training set.

[0033] According to a preferred embodiment, the code text to code image conversion submodule is used to generate specially processed code images based on the input code text of the expanded training set using a text-to-image tool, wherein the specially processed code images are preferably highlighted code images.

[0034] According to a preferred embodiment, the data preprocessing submodule is used to perform necessary processing on the image, such as scaling, cropping, and normalization, and then convert the image into a vector that the model can train.

[0035] According to a preferred embodiment, the module for randomly selecting similar samples from the expanded training set is used to randomly select samples of the same category but different training sets from the expanded training set, which consists of the input code training set and the transformation code training set, for pairing.

[0036] According to a preferred embodiment, the feature extractor and sample matching pair iterative update submodule is used to divide the model into two parts: a feature extractor and a classifier. Random samples of the same class are input into the feature extractor. The difference between samples is calculated by the contrastive learning loss function. The feature extractor and sample matching pair are iteratively updated. The random sample matching pair is replaced with the new sample matching pair. The iterative training continues until the feature extractor training converges.

[0037] According to a preferred embodiment, the invariant feature training model classifier submodule is used to input the latest sample matching pair into the feature extractor to extract the target features, and then input these features into the classifier for training until the classifier training converges, wherein the target features are invariant strong robust features.

[0038] The present invention discloses a processor comprising the system described above or executing the method described above by a computer program, wherein the computer program can be stored in a storage medium.

[0039] The overall idea of ​​this invention is to use a code transformation program to directionally expand the input code training set into training sets of different styles. After transformation and preprocessing, samples of the same category are randomly selected from the expanded training set, paired, and input into the feature extractor of the model. The feature extractor and sample matching pairs are iteratively updated using a contrastive learning method to train a feature extractor that can extract invariant features. Finally, the extracted invariant features are used to train the classifier part of the model, outputting a model with strong robustness.

[0040] The beneficial technical effects of this invention are as follows:

[0041] 1. Supports multiple classification models: The method and system of this invention can be applied to multiple classification models such as vulnerability detection, code author attribution, and clone detection;

[0042] 2. Improve model robustness: Expanding the training set for non-robust features makes the model more adaptable to diverse code texts. The robustness of the model is improved by training with invariant features.

[0043] 3. Reduce the success rate of various attacks: In addition to the ability to defend against adversarial sample attacks through strong robustness, it also effectively defends against the latest attack methods of invisible characters by extracting features from images. Attached Figure Description

[0044] Figure 1This is a schematic diagram of a preferred embodiment of the robustness enhancement method for source code classification models based on invariant features provided by the present invention.

[0045] Figure 2 This is a flowchart illustrating the template style extension code of a preferred embodiment of the present invention;

[0046] Figure 3 A schematic diagram of the iterative update sample matching pairs and feature extractor process for a preferred embodiment of the present invention;

[0047] Figure 4 This is a schematic diagram of the robustness enhancement system for a source code classification model based on invariant features, which is a preferred embodiment of the present invention. Detailed Implementation

[0048] The following is a detailed explanation with reference to the accompanying drawings.

[0049] Example 1

[0050] This invention discloses a robustness enhancement method for source code classification models based on invariant features, such as... Figure 1 The diagram shows the flowchart of this method. Preferably, some features in the code will not change the semantics and functionality of the code after being changed, such as changing variable names, changing multiple variable definitions from a single line to multiple lines, or changing a for loop to a while loop. Furthermore, after excluding these non-robust features, the remaining features will change the semantics and functionality of the code after being changed; these features can be called invariant features.

[0051] Preferably, the method includes at least the following steps:

[0052] S1. Targeted training set expansion steps: Combine non-robust features to generate multiple style templates, use a code transformation program to transform the code in the input code training set into new code of different styles to obtain a transformed code training set composed of new code, combine the input code training set and the transformed code training set into an expanded training set, and convert the code text in the expanded training set into code images.

[0053] S2. Training steps for a robust model: Convert the code image into vectors required for model training through data preprocessing. Randomly select samples of the same category from the expanded training set, pair them, and input them into the feature extractor. Use contrastive learning to iteratively update the feature extractor and sample matching pairs and extract the target features. Input the extracted invariant features into the classifier for training to output a robust model.

[0054] Preferably, in this invention, "style template" refers to a combination of non-robust features, such as... Figure 2As shown, "Style Template 1" is one of the style templates, which guides the direction of code transformation.

[0055] Preferably, in this invention, the "code transformation program" can identify styles in the code sample that differ from those in the style template, rewrite the code, and generate new code that has a different style from the source code but the same function.

[0056] Preferably, in this invention, the deep learning model (n layers) is divided into two parts: a feature extractor and a classifier. The feature extractor is defined as the first r layers of the model (n>r), which extracts features from the input samples and represents them using feature vectors. The classifier is defined as the last nr layers of the model, which uses the feature vectors output by the feature extractor to perform classification.

[0057] Preferably, the neural network model can be any of the network structures such as ResNet, AlexNet, DenseNet, and VGG, with ResNet being the preferred choice.

[0058] Preferably, step S1 may include the following sub-steps:

[0059] S1.1 Generating multiple style templates from non-robust features: Analyze the existing source code classification model and the attack methods against it, and summarize the transformation target features and transformation patterns of the attack methods in generating attack code samples. Among them, the target features that are attacked are classified and grouped as non-robust features, and different combinations of non-robust features are extracted to form multiple mutually distinguishable style templates.

[0060] S1.2 Targeted Code Transformation and Expansion: The code transformation program is used on the input code training set to perform targeted style template transformation on the code in the input code training set according to the code style template, generating new code with the same semantics but different styles. Among them, one style template generates a set of transformed code training sets, and the input code training set and the transformed code training set are combined into the expanded training set.

[0061] S1.3, Converting code text to code image: Use a text-to-image tool to generate specially processed code images based on the input code text for the expanded training set. The specially processed code images are preferably highlighted code images.

[0062] Preferably, in step S1.2, a preferred embodiment of the code synonym transformation program performing directional template style transformation is as follows: the code is transformed into an intermediate format, the intermediate format text is transformed according to the template style, and finally the intermediate format text is converted into code form to generate new code with the same semantics but different styles. Each template style can generate a set of transformed code training sets.

[0063] Preferably, in step S1.3, a preferred embodiment of highlighting the code image is as follows: the code text is converted into intermediate format text, which can be HTML, Markdown, or other formats. Code keywords are highlighted on the intermediate format text to convert it into a code image sample using a tool. These code keywords are predefined special words in programming languages, possessing specific functions and meanings. For example, "if," "else," and "for" are keywords used to control the flow of code, much like verbs in human language, driving the execution of code.

[0064] According to a preferred embodiment, the following sub-steps may be performed when executing step S1:

[0065] S1.1 Generating Multiple Style Templates from Non-Robust Features: This section analyzes existing source code classification models and attack methods targeting them, summarizing the transformation target features and transformation patterns of attack methods in generating attack code samples. The attacked target features are termed non-robust features. These non-robust features are categorized and grouped, and different combinations of features are extracted to form multiple distinct style templates. Transformation target features may include variable naming conventions, control flow, array space allocation methods, etc. Transformation patterns may include equivalent transformations of for loops and while loops, and changes in variable naming conventions from camelCase to underscore-separated type, etc. Examples of style templates can be found in [reference needed]. Figure 2 As shown;

[0066] S1.2, Directional Code Transformation and Expansion: Reference Figure 2 As shown, the specific steps involve inputting code samples and template styles, scanning the input code and finding style points that differ from the template. Here, srcML is an XML format used for source code. The srcML toolkit is used to convert the code into an intermediate XML form. Then, an XML tool is used to transform the XML format code at the different points in the code sample. After style transformation, the XML is converted back into code form. The new code sample uses the category tags of the input code. A template style generates a set of transformed code training sets. The input code training set and the transformed code training set are used to expand the training set.

[0067] S1.3, Converting Code Text to Code Images: Convert all code and input code text into HTML format. Use a JS template to highlight the keyword parts of the code. Then use the imgkit package to convert the generated HTML file into a highlighted code image. The imgkit package is a tool for converting HTML into images.

[0068] Preferably, step S2 may include the following sub-steps:

[0069] S2.1 Data preprocessing: Perform necessary processing on the images, such as scaling, cropping, and normalization, and then convert the images into vectors that the model can train.

[0070] S2.2, Random matching of the same category from the expanded training set: Randomly select samples of the same category but different training sets from the expanded training set consisting of the input code training set and the transformed code training set for matching;

[0071] S2.3, Contrastive learning method iteratively updates feature extractor and sample matching pairs: The model is divided into two parts: feature extractor and classifier. Randomly selected sample pairs of the same category are input into the feature extractor. The difference between the samples is calculated through the contrastive learning loss function. The feature extractor and sample matching pairs are iteratively updated. The random sample matching pairs are replaced with new sample matching pairs. The training is iteratively continued until the feature extractor training converges.

[0072] S2.4 Invariant Feature Training Model Classifier Part: The latest sample matching pairs are input to the feature extractor to extract the target features. These target features are then input into the classifier for training until the classifier training converges. The target features are invariant and robust features.

[0073] Preferably, in step S2.3, a preferred implementation of the contrastive learning loss function calculation is as follows: the vector pair of the sample pair is used as input, a pair of feature vectors is output through the feature extractor, and the difference between the pair of feature vectors is calculated using the contrastive learning loss function.

[0074] Preferably, in this invention, the "contrastive loss function" is a loss function in machine learning used to compare the similarity of two data points. Typically, during training, if two data points are similar, it is desirable that their outputs are also similar; if they are dissimilar, it is desirable that their outputs are also dissimilar. This goal is achieved using a contrastive loss function.

[0075] According to a preferred embodiment, the following sub-steps may be performed when executing step S2:

[0076] S2.1 Data preprocessing: By organizing the shape of the color channels, width and height of the image, and then performing scaling, cropping, normalization and other processing, the image is converted into a vector that the model can train.

[0077] S2.2, Expanding the training set by randomly selecting pairs of the same category: (Refer to...) Figure 3As shown, the specific steps involve the input code training set and the transformed code training set forming an expanded training set. Each row contains all samples of a dataset, and each column contains a sample and its corresponding transformed sample. Here, D0-DN represent sample sets 1-N, Class1-ClassM represent categories 1-M, and S represents code samples. If two different sample sets have the same code Si, it means that the two codes have the same content but different styles. Random samples of the same category are randomly selected from different sample sets for pairing to generate random sample matching pairs.

[0078] S2.3, Contrastive learning method iteratively updates the feature extractor and sample matching pairs: Reference Figure 3 As shown, this step-by-step process involves dividing the n-layer model into two parts: the first r layers of the model are invariant feature extractors F, and the last nr layers are classifiers C. Random pairs of samples of the same class are input into the feature extractor. The difference between the samples is calculated by the contrastive learning loss function. The contrastive learning loss function can be any one of ERM, MMD, or CORAL. The feature extractor and the sample matching pairs are iteratively updated. The random sample matching pairs are replaced with new sample matching pairs. The training is iteratively continued until the feature extractor training converges.

[0079] S2.4 Invariant Feature Training Model Classifier Part: The latest sample matching pairs are input to the feature extractor F* to extract the target features. These features are then input to the classifier C for training until the classifier training converges. The loss function can be the cross-entropy loss function, where the target features are invariant and robust features.

[0080] Example 2

[0081] This embodiment is a further improvement on embodiment 1, and repeated content will not be described again.

[0082] This invention also discloses a robustness enhancement system for source code classification models based on invariant features, such as... Figure 4 The diagram shown illustrates the structure of this system, which includes the following modules:

[0083] The targeted training set expansion module is used to combine non-robust features to generate templates of various styles. The code transformation program transforms the code in the input code training set into new code of different styles to obtain a transformed code training set composed of new code. The input code training set and the transformed code training set are combined into an expanded training set. The code text in the expanded training set is converted into code images.

[0084] The robust classification model training module is used to convert code images into vectors required for model training through data preprocessing. Samples of the same category are randomly selected from the expanded training set, paired, and input into the feature extractor. The feature extractor and sample matching pairs are iteratively updated using a contrastive learning method to extract target features. The extracted invariant features are input into the classifier for training, and a robust model is output.

[0085] According to a preferred embodiment, the targeted training set expansion module may include:

[0086] The non-robust feature combination generation style template submodule is used to analyze existing source code classification models and attack methods against them, summarize the transformation target features and transformation patterns of attack methods to generate attack code samples, and call the attacked target features non-robust features. Non-robust features are classified and grouped, and different combinations of features are extracted to form a variety of mutually distinguishable style templates. Among them, the transformation target features may include variable naming conventions, control flow, array space allocation methods, etc., and the transformation patterns may include equivalent transformations of for loops and while loops, variable naming conventions changing from camelCase to underscore-separated type, etc.

[0087] The targeted code transformation augmentation submodule is used to scan the input code samples and template styles and find style points that differ from the template. Here, srcML is an XML format for source code. The srcML toolkit is used to convert the code into an intermediate XML form. Then, an XML tool is used to transform the XML format code at the different points in the code sample. After style transformation, the XML is converted back into code form. The new code sample uses the category tags of the input code. A template style generates a set of transformed code training sets. The input code training set and the transformed code training set are combined into the augmentation training set.

[0088] The code text to code image conversion submodule is used to convert the code text of the expanded training set in the targeted code transformation expansion submodule into HTML format. The keyword part of the code is highlighted using a JS template. Then, the imgkit package is used to convert the generated HTML file into a highlighted code image. The imgkit package is a tool for converting HTML into images.

[0089] According to a preferred embodiment, the robust classification model training module may include:

[0090] The data preprocessing submodule is used to organize the shape of the color channels, width, and height of the image, and then perform scaling, cropping, normalization, and other processing to convert the image into a vector that the model can train.

[0091] The module for randomly selecting similar samples from the expanded training set is used to randomly select samples of the same category but from different training sets from the expanded training set consisting of the input code training set and the transformation code training set for pairing.

[0092] The feature extractor and sample matching pair iterative update submodule is used to divide the model into two parts: a feature extractor and a classifier, and to train the feature extractor. Here, random samples of the same class are input into the feature extractor, and the difference between the samples is calculated by the contrastive learning loss function. The contrastive learning loss function can be any one of ERM, MMD, or CORAL. The feature extractor and sample matching pair are iteratively updated, and the random sample matching pair is replaced with the new sample matching pair. The training is iteratively continued until the feature extractor training converges.

[0093] The invariant feature training model classifier submodule receives the latest matching samples, extracts invariant features using a feature extractor, and inputs these features into the classifier for training until convergence, outputting a robust model.

[0094] Example 3

[0095] This embodiment is a further improvement on Embodiment 1 and / or 2, and the repeated content will not be described again.

[0096] The present invention discloses a processor that includes the system as described in Embodiment 2 or executes the method as described in Embodiment 1 by means of a computer program, wherein the computer program can be stored in a storage medium.

[0097] For example, the server device used in the method executed by the computer program of the present invention is a Dell R740, whose central processing unit (CPU) is an Intel(R) Xeon(R) Gold 6132@2.60GHz, the graphics processing unit (GPU) is a Tesla M40, the memory capacity is 128GB DDR4 RAM, and the storage capacity is 1TB SSD + 4TB HDD.

[0098] Preferably, the method, system, and / or processor of the present invention are typically used to process large amounts of source code data to perform code classification tasks. Examples include code function classification, code clone detection, code author attribution, and vulnerability detection. In source code vulnerability detection, the present invention can be used to classify code in a codebase to distinguish between secure code and code that may have security vulnerabilities. Code classification models trained using the present invention achieve higher classification accuracy, while also improving robustness and reducing the success rate of various attacks.

[0099] Preferably, the processor of this invention processes primarily source code (.c, .java, .cpp), which can be text files containing code from different programming languages. The data is transmitted to the server via a high-speed network interface or USB interface and stored on the server's SSD or HDD. The processor reads the data to be processed from these storage media and performs data cleaning, normalization, and other processing operations. Finally, the processed data is used to train a classification model.

[0100] Preferably, the processed data includes the trained model and its output, typically sent to server storage media such as SSDs or HDDs. This data can be used for subsequent code analysis tasks, such as code security checks and code quality assessments. These analysis results can guide developers to improve code quality and fix security vulnerabilities, ultimately achieving the goals of actual production or application.

[0101] Preferably, after processing, the data is stored in the server's storage medium in the form of model files and output results. When this data is needed, it can be transferred to the device requiring the data, such as the developer's personal computer or other servers, via a network interface or USB interface; alternatively, it can provide code classification services and code classification model training services to the integrated technology service platform via an API interface.

[0102] It should be noted that the specific embodiments described above are exemplary. Those skilled in the art can devise various solutions inspired by the disclosure of this invention, and these solutions all fall within the scope of this invention and its protection. Those skilled in the art should understand that this specification and its accompanying drawings are illustrative and do not constitute a limitation on the claims. The scope of protection of this invention is defined by the claims and their equivalents. This specification contains multiple inventive concepts; terms such as "preferredly," "according to a preferred embodiment," or "optionally" indicate that the corresponding paragraph discloses an independent concept. The applicant reserves the right to file divisional applications based on each inventive concept. Throughout the text, the feature introduced by "preferredly" is only an optional approach and should not be construed as mandatory. Therefore, the applicant reserves the right to abandon or delete the relevant preferred feature at any time.

Claims

1. A method for enhancing the robustness of source code classification models based on invariant features, characterized in that, The method includes at least the following steps: Combining non-robust features to generate multiple style templates includes: analyzing existing source code classification models and attack methods targeting these models, summarizing the transformation target features and transformation patterns of attack code samples generated by attack methods, wherein the attacked target features are classified and grouped as non-robust features, and different combinations of non-robust features are extracted to form multiple mutually distinguishable style templates. The code transformation program is used to transform the code in the input code training set into new code of different styles to obtain a transformed code training set composed of new code. The input code training set and the transformed code training set are combined into an expanded training set. The process includes: using the code transformation program on the input code training set, performing targeted style template transformation on the code in the input code training set according to the code style template, and generating new code with the same semantics but different styles. In this case, one style template generates a set of transformed code training sets, and the input code training set and the transformed code training set are combined into an expanded training set. Convert the code text in the expanded training set into code images; The code images are converted into vectors required for model training through data preprocessing. Randomly select samples of the same category from the expanded training set, pair them, and input them into the feature extractor. This includes: randomly selecting samples of the same category but different training sets from the expanded training set, which consists of the input code training set and the transform code training set, and pairing them. The method of contrastive learning is used to iteratively update the feature extractor and sample matching pairs and extract the target features. It includes: dividing the model into two parts, a feature extractor and a classifier; inputting randomly selected sample pairs of the same category into the feature extractor; calculating the sample difference through the contrastive learning loss function; iteratively updating the feature extractor and sample matching pairs; replacing the random sample matching pairs with new sample matching pairs; and iteratively training until the feature extractor training converges. The extracted invariant features are input into a classifier for training to output a model, which includes: inputting the latest sample matching pairs into a feature extractor to extract target features, and inputting these target features into a classifier for training until the classifier training converges.

2. The method according to claim 1, characterized in that, Converting code text in the expanded training set into code images involves using a text-to-image tool to generate specially processed code images based on the input code text.

3. The method according to claim 1, characterized in that, Data preprocessing includes converting the preprocessed image into a vector that can be used for model training, wherein preprocessing includes, but is not limited to, scaling, cropping, and / or normalization.

4. A robustness enhancement system for source code classification models based on invariant features, used to perform the method as described in any one of claims 1 to 3, characterized in that, It includes: The targeted training set expansion module is used to combine non-robust features to generate templates of various styles. The code transformation program transforms the code in the input code training set into new code of different styles to obtain a transformed code training set composed of new code. The input code training set and the transformed code training set are combined into an expanded training set. The code text in the expanded training set is converted into code images. The robust classification model training module is used to convert code images into vectors required for model training through data preprocessing. Samples of the same category are randomly selected from the expanded training set, paired, and input into the feature extractor. The feature extractor and sample matching pairs are iteratively updated using a contrastive learning method to extract target features. The extracted invariant features are input into the classifier for training to output the model.

5. A processor, characterized in that, Includes the system as described in claim 4 or the method as described in any one of claims 1 to 3 performed by a computer program, wherein the computer program can be stored in a storage medium.

Citation Information

Patent Citations

  • Source code author identification method based on deep belief network

    CN108108184A

  • Adversarial sample generation method for source code classification model

    CN113760358A

  • Adding adversarial robustness to trained machine learning models

    CN114503108A

  • Code classification deep learning model interpretation method and system based on sample difference

    CN115905926A

  • Deep-learning based device and method for detecting source-code vulnerability with improved robustness

    US20220292200A1