A compiler test case priority ranking method based on text vector and convolutional neural network

By combining text vectors and convolutional neural networks, efficient and flexible sorting of compiler test code is achieved, solving the problems of limited applicability and high hardware requirements in existing technologies, and improving the efficiency and accuracy of compiler testing.

CN119621563BActive Publication Date: 2025-12-19NANJING UNIV OF AERONAUTICS & ASTRONAUTICS
View PDF 2 Cites 0 Cited by

Patent Information

Application Number
CN202411700250.6
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2024-11-26
Publication Date
2025-12-19
Estimated Expiration
2044-11-26

AI Technical Summary

Technical Problem

Existing compiler test code sorting techniques suffer from limited applicability, high hardware requirements, and low efficiency. In particular, traditional methods struggle to handle test code of varying lengths and origins.

Method used

A method based on text vectors and convolutional neural networks is adopted. The code is converted into an image through code pixelation, and the convolutional neural network is used for feature extraction and ranking. Transfer learning is combined to fine-tune the pre-trained model to achieve priority ranking of test code.

Benefits of technology

It expands the applicability of test code sorting, reduces hardware requirements, improves sorting efficiency and accuracy, and can quickly process C language test code of various lengths and sources.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119621563B_ABST
    Figure CN119621563B_ABST
Patent Text Reader

Abstract

The application provides a compiler test case priority sorting method based on a text vector and a convolutional neural network. In the training stage, the features of each line of training code are extracted and combined into a vector, multiple line vectors are merged into a matrix, and then the matrix is converted into a pixel image for convolutional neural network training. The training data includes code from different compiler versions that can reveal errors. In the scheduling stage, the same method is used to convert the code to be sorted into an image, which is input into the trained model to obtain the code sorting result. The application predicts the potential of the new test code to reveal errors, thereby determining the priority. The application uses a token-based line-level text vector generation method, which has a faster code feature extraction speed, lower hardware requirements, and does not rely on the internal information of the test code generated by the fuzz test generator, and is suitable for C language test codes of various sources and lengths.
Need to check novelty before this filing date? Find Prior Art

Description

TECHNICAL FIELD

[0001] The present application relates to the field of compiler testing, and in particular to a compiler test case priority sorting method based on text vectors and convolutional neural networks. BACKGROUND

[0002] Compiler test code priority sorting is of great significance in the field of embedded systems, especially in cases where commercial compilers cannot be used and maintenance and technical support cannot be obtained. By prioritizing and executing key test cases, potential compiler defects can be discovered and repaired more quickly, improving system stability and reliability. This technology not only ensures the normal operation of embedded systems in specific application environments, but also reduces development and maintenance costs, improves development efficiency, and fills the technical gap caused by the lack of commercial compiler support.

[0003] Currently, the most popular technique in compiler testing is fuzz testing. Fuzz testing generates test cases automatically to explore the input space of the target program to discover new bugs. One of the main fuzz testing techniques is based on generated fuzzing, which generates inputs from 0 based on grammar or corpus. After generating new test cases, differential testing can be used to determine the presence of compiler errors. The main problem with this approach is that the differential testing process will take a long time and resources, and only a relatively small number of compiler errors can be detected. For example, the authors of CSmith (a fuzz test code generator) took three years to detect 325 C compiler errors, while the authors of YARPGen and GrayC took two years to detect approximately 220 C and C++ compiler errors, and the authors of GrayC even failed to detect any bugs in the trunk version of GCC in 6 months using the code generated by CSmith. Most of the code generated by these fuzzers is difficult to reveal compiler errors, and the present application classifies this code as normal code that cannot discover bugs. In contrast, the remaining code that can reveal compiler errors is referred to as bug-triggering code.

[0004] To improve the efficiency of fuzz testing by prioritizing generated test cases in compiler testing, the TB, LET, and COP methods were proposed, which determine the priority of test programs based on the error exposure probability of test programs in unit time predicted by historical error information, and it is shown that certain features in error code can trigger compiler errors. The C-CORE method was also proposed, which uses a large-scale pre-trained language model (PLM) for feature extraction. However, these methods face the following three problems:

[0005] 1. TB, LET and COP methods are based on the additional information generated by CSmith when generating test code in the comments to extract code features, which can greatly reduce the cost of feature extraction, but also greatly limits the application range of the method, and the test code generated by CSmith has been proven to be difficult to find bugs in the trunk version of GCC.

[0006] 2. Prioritizing code based on overall features ignores the impact of different code sizes, which limits the applicability of the method. Methods like YARPGen and Creal generate test codes of different lengths, and traditional prioritization methods may fail due to this variability in length. Extracting features at the entire code level may group codes with different line-level features and bug triggering capabilities, resulting in ineffective prioritization.

[0007] 3. Fine-tuning pre-trained models such as BERT and CodeBERT requires capturing as many common features as possible for a large amount of code (especially long code) before sorting, which requires high performance of the device and consumes a lot of time. C-CORE proposed by Zhou Wei et al. uses distributed training technology to speed up the training process, which is difficult to achieve in many practical application tasks.

[0008] In view of the problems of the above-mentioned existing compiler test code sorting technology, a new feasible technology is needed to expand the application range of test code sorting, without limiting the source of test code and without requiring too high hardware requirements. SUMMARY

[0009] The purpose of the application is to solve the problems of the prior art, and the application provides a compiler test case priority sorting method based on text vector and convolutional neural network, which extracts text vector and combines convolutional neural network model to expand the application range of test code sorting and ensure good sorting effect.

[0010] Technical scheme: A compiler test case priority sorting method based on text vector and convolutional neural network, comprising the following steps:

[0011] Obtain training code, the training code includes code that can trigger bugs and normal code that cannot trigger bugs from different compiler versions;

[0012] Code pixelization processing is performed on the training code, including: extracting code features from each line of training code and combining them into a vector, concatenating multiple vectors into a matrix, converting the matrix into a grayscale image and cutting it to make the image length and width consistent, and filling the insufficient part with black to obtain a training image;

[0013] Input the picture for training into the convolutional neural network for supervised neural network model training, and save the neural network model after fine-tuning to obtain the retrained neural network model;

[0014] The test case code to be sorted is subjected to code pixelization processing using the same code pixelization processing method as the training code, to obtain test case pictures, which are input into the retrained neural network model, and the model outputs the probability of each part of the picture triggering a bug;

[0015] The probabilities output by the model are sorted, and for multiple cropped pictures belonging to the same code, each picture may have a different ranking, and the picture with the highest ranking is taken as the representative ranking of the probability of the complete code triggering a bug, to obtain a code ranking probability as the test code priority sorting result.

[0016] As a preferred embodiment, code features are extracted from each line of training code and combined into a vector, and multiple lines of vectors are spliced into a matrix, and the matrix is converted into a grayscale image, including:

[0017] N features are extracted from each line of training code, and the N features are converted into an N-dimensional vector, one feature corresponding to one dimension in the vector, and the value of each dimension being the number of times the feature appears. Each line of the row vector is spliced into a matrix, and the elements in the matrix are converted into corresponding grayscale image pixels, and the grayscale value is equal to the size of the element value in the matrix.

[0018] As a preferred embodiment, N features are extracted from each line of training code, including:

[0019] For each line of training code, all statement keywords are identified as statement features, type keywords and modifier keywords are identified as type and modifier features, operators are identified as operator features, function declarations and call relationships are identified as function features, and all features of each line of training code are identified.

[0020] As a preferred embodiment, the specific identification method of various code features is as follows: the keywords and phrases of various code features are defined in advance, the corresponding code elements are matched using regular expressions, the strings matching the defined keywords and phrases are extracted as code features.

[0021] As a preferred embodiment, the regular expression matching uses a non-greedy matching method.

[0022] As a preferred embodiment, each line of code features has 53 types of features, including 41 types of keyword features defined by keywords

[0023]

[0024] and the structural features c_assignments, break_blocks, return_blocks, continue_blocks, goto_blocks, def_blocks, struct_blocks, union_blocks, call_blocks, calc_blocks, if_else_blocks, for_blocks defined by the 12th phrase.

[0025] As preferred, before extracting the code features from each line of training code, the code is preprocessed, including deleting code comments, blank lines and "#" lines.

[0026] As preferred, the neural network model is fine-tuned, including using a transfer learning method to fine-tune the neural network model.

[0027] The application also provides a compiler test case priority sorting system based on a text vector and a convolutional neural network, comprising:

[0028] A training data acquisition module is configured to acquire training code, wherein the training code includes code that can trigger a bug and normal code that cannot trigger a bug from different compiler versions;

[0029] A code pixelization processing module is configured to perform code pixelization processing on the training code, including extracting code features from each line of training code and combining them into a vector, splicing multiple lines of vectors into a matrix, converting the matrix into a grayscale image and performing cutting to keep the length and width of the image consistent, and supplementing the insufficient part with black to obtain a training image;

[0030] A model training module is configured to input the training image into a convolutional neural network for supervised neural network model training, fine-tune the neural network model, and save the fine-tuned neural network model to obtain a retrained neural network model;

[0031] A test case code processing module is configured to call the code pixelization processing module, use the same code pixelization processing method as the training code to perform code pixelization processing on the test case code to be sorted, obtain a test case image, input the test case image into the retrained neural network model, and output the probability of each part of the image triggering a bug by the model;

[0032] A code priority sorting module is configured to sort the probabilities output by the model, for multiple cut images belonging to the same code, each image may have a different ranking, and the highest ranked image is taken as the representative ranking of the probability of the complete code triggering a bug, to obtain a code ranking probability as a test code priority sorting result.

[0033] The application further provides a computer readable storage medium, which stores a computer program / instruction, and the computer program / instruction realizes the steps of the compiler test case prioritization method based on a text vector and a convolutional neural network when executed by a processor.

[0034] The application further provides a computer program product, which comprises a computer program / instruction, and the computer program / instruction realizes the steps of the compiler test case prioritization method based on a text vector and a convolutional neural network when executed by a processor.

[0035] Beneficial effects: compared with the prior art, the compiler test case prioritization method based on a text vector and a convolutional neural network provided by the application extracts a feature vector of each line of code by using a text processing method, realizes feature extraction and processing on compiler test code, and improves the efficiency of compiler test code sorting; meanwhile, in view of the problems of excessively high hardware requirement for feature extraction and limited applicable range of the sorting method, the powerful image classification ability of a convolutional neural network model in image recognition is combined, a code is pixelated and converted into an image, and a test code sorting strategy is proposed, so that the overall efficiency of compiler test code sorting is improved. BRIEF DESCRIPTION OF DRAWINGS

[0036] Figure 1 FIG. 1 is a flowchart of a compiler test case prioritization method provided by an embodiment of the application. DETAILED DESCRIPTION

[0037] The technical solutions of the application are further described below with reference to the drawings.

[0038] An embodiment of the application provides a compiler test case prioritization method, CPTCP (Code Pixelization for Test Cases Prioritization), which combines a CNN (Convolutional Neural Networks) and a text vector processing technology, generates a line-level text vector based on a token, and pixelates and converts a code into a picture for CNN training, and the training data comprises C codes from different compiler versions which can reveal errors. In a scheduling stage, the same method is used to convert the code to be sorted into a picture, and the code sorting result is obtained by inputting the code into the trained CNN model, which predicts the potential of the new test code to reveal errors, so as to determine the priority of the new test code. The CPTCP uses the method of generating a line-level text vector based on a token, which has a faster feature extraction speed for the code, a lower hardware requirement, and is not dependent on the information inside the test code generated by a fuzzing generator, and is applicable to C language test codes of various sources and lengths.

[0039] As Figure 1 shown, the embodiment of the application provides a method for compiler test case priority sorting based on text vector combined with convolutional neural network, mainly including a training stage and a scheduling stage, both stages including feature extraction and code pixelization, and an existing CNN model is fine-tuned and trained to obtain a code image classification model. The specific implementation of each step is as follows:

[0040] Step S1, training stage

[0041] The main goal of this step is to prepare a CNN model for subsequent sorting tasks based on existing bug-trigger code. Specifically, the training stage details the process of Code Pixelization, which is consistent in the training stage and the scheduling stage. A large number of bug-triggered code and normal code that cannot trigger bugs are used to generate corresponding images, and these images are input into the CNN as training data. Through training, the CNN gradually extracts important features related to bugs from the images, and then obtains an image classifier that can identify bug features.

[0042] Step S1.1, feature extraction in the training stage

[0043] This method combines the features given by CSmith and general code features to design a set of code features, and extracts features from each line of code. In the embodiment of the application, each line of historical test case code is collected from the GCC bugzilla website or other open source projects, and the code features are extracted in the embodiment of the application. 53 features, the number of features can be appropriately increased or decreased in the actual operation process, but for C language code, the following categories must be included:

[0044] ① Statement feature: In C programs, "statement" is an important piece of information that reflects the fault characteristics of test programs. For example, a test program that lacks a loop statement cannot detect loop optimization errors. Therefore, the presence of a loop statement can indicate a loop optimization error. Specifically, extracting statement keywords from text is the best way to identify statement features. Therefore, all statement keywords in C language are considered as the first type of feature, such as for, while, if, else, goto, etc.

[0045] ② Type and modifier features: In C programs, "type" can also reflect the fault features in the test program. For example, "struct" is a complex and error-prone variable type, which usually triggers alignment errors. In addition, modifier keywords are often used in combination with type keywords. Therefore, type keywords and modifier keywords in C language are regarded as the second type of features, such as struct, union, int, static, const, etc.

[0046] ③ Operator features: Test programs usually contain many operators to achieve certain functions. In particular, when the test program contains a large number of operators and forms a series of complex operations, operation optimization related errors are often triggered. Therefore, all operators in C language are regarded as the third type of features, such as ++, --,!, ||, >>, etc.

[0047] ④ Function features: The test program also contains many function declarations, definitions and calls, as well as inline functions and recursive functions. For example, the compiler may optimize the call of some inline functions to directly calculate the result, or enable tail call optimization (Tail Call Optimization): -foptimize-sibling-calls tail recursive function calls are optimized into a jump, avoiding additional stack frame overhead. Related errors will be triggered. Therefore, all function declarations and calls in C language are regarded as the fourth type of features.

[0048] In the embodiment of the present application, regular matching with small time overhead is used when extracting features, and 41 types of keyword features are included in the 53 types of features:

[0049]

[0050] These features are all standard keywords in C language, and the following is the meaning of the standard keywords in C language:

[0051] 1. auto: used to declare automatic variables, usually omitted, because by default local variables are automatic variables.

[0052] 2. break: used to jump out of a loop or switch statement.

[0053] 3. case: used to define different branches in switch statements.

[0054] 4. char: used to declare character type variables.

[0055] 5. const: used to declare constants, indicating that the value of the variable cannot be modified.

[0056] 6. continue: Used to skip the remaining code in a loop and go directly to the next iteration.

[0057] 7. default: Used in switch statements to define a default branch.

[0058] 8. do: Used in conjunction with while or for to create a do-while loop.

[0059] 9. double: Used to declare a variable of type double-precision floating-point.

[0060] 10. else: Used in conjunction with if to define the code to execute if the condition is not met.

[0061] 11. enum: Used to define an enumeration type.

[0062] 12. extern: Used to declare an external variable or function, indicating that it is defined in another file.

[0063] 13. float: Used to declare a variable of type single-precision floating-point.

[0064] 14. for: Used to define a for loop.

[0065] 15. goto: Used to unconditionally jump to a specified label.

[0066] 16. if: Used to define a conditional statement.

[0067] 17. int: Used to declare a variable of type integer.

[0068] 18. long: Used to declare a variable of type long integer.

[0069] 19. register: Used to suggest to the compiler that a variable should be stored in a register for faster access.

[0070] 20. return: Used to return a value from a function and end its execution.

[0071] 21. short: Used to declare a variable of type short integer.

[0072] 22. signed: Used to declare a variable of type signed integer.

[0073] 23. sizeof: Used to get the size of a data type or variable in bytes.

[0074] 24. static: Used to declare a static variable or function, which retains its value throughout the program's lifetime.

[0075] 25. struct: used to define a structure type.

[0076] 26. switch: used to define a multi-branch selection statement.

[0077] 27. typedef: used to define a new name for a data type.

[0078] 28. union: used to define a union type, members in a union share the same block of memory.

[0079] 29. unsigned: used to declare an unsigned integer type variable.

[0080] 30. void: used to represent a function without return value or pointer type.

[0081] 31. volatile: used to declare a variable that may be modified unexpectedly, such as by hardware or other threads.

[0082] 32. while: used to define a while loop.

[0083] 33. int8_t, int16_t, int32_t, int64_t: used to declare 8, 16, 32, 64-bit signed integer type variables.

[0084] 34. uint8_t, uint16_t, uint32_t, uint64_t: used to declare 8, 16, 32, 64-bit unsigned integer type variables.

[0085] 35. size_t: used to represent the size of an object, usually an unsigned integer type.

[0086] Since the C language standard only specifies the minimum byte length of each basic type, but does not specify the exact byte length, the existing basic data types cannot meet the cross-platform requirements, so the int8_t and other types of data types are needed, which are commonly used in compiler test codes.

[0087] 53Class features also include 12 structural features: c_assignments (assignment statements), break_blocks, return_blocks, continue_blocks, goto_blocks, def_blocks (function definitions, which need to be avoided to prevent excessive processing time due to nesting), struct_blocks, union_blocks, call_blocks (function calls), calc_blocks (calculation statements, including arithmetic, logical, and bitwise operations), if_else_blocks, for_blocks.

[0088] 1. c_assignments (assignment statements): Represents assignment statements in C language, such as `x = 5;` or `y = x + 3;`. Assignment statements are used to assign a value or the result of an expression to a variable.

[0089] 2. break_blocks: Represents code blocks containing `break` statements. `break` statements are typically used to exit a loop or `switch` statement.

[0090] 3. return_blocks: Represents code blocks containing `return` statements. `return` statements are used to return a value from a function and end its execution.

[0091] 4. continue_blocks: Represents code blocks containing `continue` statements. `continue` statements are used to skip the remaining code in a loop and proceed to the next iteration.

[0092] 5. goto_blocks: Represents code blocks containing `goto` statements. `goto` statements are used to unconditionally jump to a specified label.

[0093] 6. def_blocks (function definitions): Represents code blocks of function definitions. Function definitions include the function header and body, and need to be avoided to prevent excessive processing time due to nesting.

[0094] 7. struct_blocks: Represents code blocks of structure definitions. Structures are used to define a composite data type.

[0095] 8. union_blocks: Represents code blocks of union definitions. Members of a union share the same block of memory.

[0096] 9. call_blocks (function calls): Represents code blocks of function calls. Function calls are used to execute defined functions.

[0097] 10. calc_blocks: represents code blocks containing arithmetic, logical, and bitwise operations.

[0098] 11. if_else_blocks: represents code blocks containing if-else statements. If-else statements are used to execute different code blocks based on conditions.

[0099] 12. for_blocks: represents code blocks containing for loops. For loops are used to repeat a block of code. These structural features are used in C language to describe different code blocks and statements, helping to analyze and understand the structure and function of the program.

[0100] It should be understood that the above-mentioned feature categories cannot strictly summarize all the features of the code, and the addition or modification of the number or name of the features is still within the scope of the method.

[0101] In order to expand the scope of the method, CPTCP cannot simply use the additional information of the test code itself as before; moreover, because the test code is generally large in size, and the style and naming method of test code from different sources are also very different, the ASCII code corresponding to the character cannot be mapped to a feature, and the character-level feature extraction cannot be used. CPTCP uses regular expression matching to extract features corresponding to code elements. When designing regular expressions, especially for complex structures such as function recognition, it needs to be considered that complex regular expressions may require a large number of backtracking, greedy matching mode will try to match as many characters as possible, and other issues will cause the feature extraction time to increase seriously. The present application combines the features of the compiler test code itself, simplifies the regular expression matching of complex structures, avoids excessive nesting and recursion, uses non-greedy matching to reduce unnecessary backtracking, and ensures the accuracy of matching as much as possible.

[0102] For example: the regular expression of the function definition def_blocks:

[0103] \b(static\s+)?(void|char|short|int|float|long|double|int8_t|int16_t|int32_t|int64_t|uint8_t|uint16_t|uint32_t|uint64_t)\b(\s*(\*|\*\*)?\s*)(\w+)\s*\((?:(?:const\s+)?(void|char|short|int|float|long|double|int8_t|int16_t|int32_t|int64_t|uint8_t|uint16_t|uint32_t|uint64_t)\s*(\*|\*\*)?\s*\w+\s*(?:,\s*)?)*\)

[0104] Regular expression for assignments c_assignments: \b[a-zA-Z_][a-zA-Z0-9_]*\s*=\s*[^=;]+;

[0105] Regular expression for structs struct_blocks: struct\s+(\w+)\s*\{[^}]*\}

[0106] Regular expression for unions union_blocks: (static\s+)?union\s+(\w+)(\s+\w+\(.*\))?

[0107] Arithmetic operation arithmetic_pattern = r'\b(\w+)\s*[\+\-\*\ / \%]\s*(\w+)\b'

[0108] Logical operation logical_pattern = r'([^\s\&\|\!]+)\s*(\&\&|\|\||\!)\s*([^\s\&\|\!]+)'

[0109] Bitwise operation bitwise_pattern = r'\b\w+\s*[\&\|\^\~\<\<\>\>]+\s*\w+\b'

[0110] Step S1.2, code pixelization in the training phase

[0111] The code is first processed, code comments, blank lines and "#" lines are deleted to reduce conversion time and the number of redundant pictures, then 53 features of each line of the training code are extracted, the 53 features are converted into a 53-dimensional vector (one feature corresponds to one dimension in the vector, and the value of each dimension is the number of times the feature appears), the row vectors of each line are spliced into a matrix, the elements in the matrix are converted into corresponding gray scale image pixels, the gray value is equal to the size of the element value in the matrix, the gray scale image converted from the matrix is cut, the length and width of the image are kept consistent at 53*53, and the insufficient part is completed with black; Since the feature values extracted from the line level code are generally small, the value (pixel gray scale) of each element is enlarged in the matrix conversion to gray scale image in the application, so as to increase the difference between the pixel values, so that the features are more obvious, thereby helping the CNN to more easily distinguish different features.

[0112] Step S1.3, training a CNN model using the image converted from the code

[0113] In the training stage, a CNN model for subsequent ranking tasks is trained, a large number of codes labeled as bug-trigger and normal codes that cannot trigger bugs are used to generate corresponding pictures, and the pictures are input into the CNN as training data. Through more than 100 epochs of training, the CNN can gradually extract important features related to bugs from the pictures. By comparing the error between the predicted results and the actual labels, the parameters of the model are adjusted to gradually improve the performance of the model. After fine-tuning the pre-trained model, save it, and train a picture classifier that can identify bug features.

[0114] In the implementation process of the application, experiments have been conducted on different CNN models to verify that the ResNet model is more suitable for this task. The APFD (Average Fault Detection Percentage) values of different CNN models in different GCC versions reflect the adaptability of the model to ranking. The APFD is used to calculate the priority of test cases as follows:

[0115]

[0116] Where, TF i Refers to the index of the test code that first detects the ith bug in the priority test case, n refers to the total number of test cases, m refers to the number of bugs detected by the test case, and the larger the value is, the better.

[0117] The experimental results are shown in Table 1.

[0118] Table 1 APFD of different CNN models in different GCC versions

[0119]

[0120] wherein the convolution layer (C), the pooling layer (P) and the full connection layer (D). (C2 5) represents the 2nd convolution layer, and the convolution kernel is set to 5*5. According to the experimental results described above, the reason why the APFD value in GCC-4.9.0 and 6.0.0 is larger is that the code triggering the bug and the code not triggering the bug are from different sources, and there may be a large difference in the code structure. Since the method of the present application does not exist to maintain the consistency of the input image size, some pictures may only contain the information of a line of code but are stretched, and some pictures contain multiple lines of code without stretching or even compression. The case where the number of pixels occupied by the single information point reflected by the picture itself is inconsistent is excluded. This also shows that the method of the present application can sensitively capture the structural differences in the code compared with other methods. This is also the reason why the method has a higher improvement than other comparison methods in some GCC version experiments in the subsequent experiments.

[0121] After taking the average value after multiple training, it is found that different CNN sizes actually have a bias for a certain version, but the difference is always small. The method has a significant ranking effect on most versions of GCC. Due to the Bucket effect, the present application selects 2C2P2D which has relatively good effect on GCC 6.2.0 in subsequent comparison.

[0122] If VGG, ResNet is used, the training and ranking time will be prolonged, but ResNet has better effect than 2C2P2D in some GCC versions with poor ranking effect. The experimental results are shown in Table 2, and the numbers in the table are APFD values, the larger the better.

[0123] Table 2 APFD of different networks on different GCC versions

[0124]

[0125] Since there is no predefined ResNet18 in Keras, a simple ResNet18 model architecture can be defined and built by itself for comparison. Since the image size input into the CNN is 53*53, the number of layers cannot be too large (ResNet18 represents an 18-layer ResNet model), otherwise the feature map will become too small in the later convolutional layers, and further convolutional operations cannot be performed, and the default convolution kernel size is 3. The above results show that in the above version of comparison, the model with fewer layers has better ranking effect, since the feature difference between the code that can trigger the bug and the code that cannot trigger the bug is generally not large, a deeper network can cause overfitting, and the training time is longer. The CNN model with fewer layers can converge faster. However, the method of the present application is not limited to a certain type of CNN model, and in the embodiment, the ResNet network is selected, and in actual sorting, the model (such as AlexNet) can be replaced, and the method of the present application does not limit this.

[0126] The method of the present application uses the method of transfer learning to fine-tune the ResNet model, and the ResNet model is trained on the ImageNet dataset. In order to make the model better learn the features of the image converted from the code, the ResNet model needs to be fine-tuned. First, the large number of grayscale image datasets converted by the fuzz test generator are standardized, so that the distribution of the image data is consistent with the data distribution during pre-training. Replace the last fully connected layer with binary classification, the optimizer is Adam, and the loss function is binary cross-entropy. The code grayscale image dataset is used to train the model, aiming to apply the knowledge learned by the model on the ImageNet dataset to this task. The present application does not freeze any layer during fine-tuning, for example, 5000 codes after pixel image conversion, the number of pictures will reach more than 100000, there is enough data to learn and the use of a lower learning rate therefore it is not easy to overfit, and the new dataset and ImageNet have great differences in content or distribution, and not freezing any layer helps the model to adapt to the new task faster. However, it should be understood that whether any layer is frozen or not during fine-tuning can be done, and the method of the present application does not limit this.

[0127] Step S2, scheduling phase

[0128] In this embodiment, when multiple new test codes are input, the converted and cropped pictures are input into the trained model, the model outputs the probability of each part of the picture triggering a bug (for each picture, the probability of triggering a bug and the probability of not triggering a bug sum to 1), and is sorted according to the probability. For multiple cropped images belonging to the same code, each image may have a different ranking. The invention takes the highest ranked image as the representative ranking of the complete code triggering bug probability. Thus, a code ranking probability can be obtained, realizing the function of test code priority ranking.

[0129] Step S2.1, feature extraction in the scheduling stage

[0130] In step S1.1, the feature extraction object is the new compiler test code to be sorted.

[0131] Step S2.2, code pixel mapping in the scheduling stage

[0132] In step S1.2, the code pixel mapping object is the new compiler test code to be sorted with extracted features, and a test case picture is obtained.

[0133] Step S2.3, input the test case picture into the retrained convolutional neural network model (ResNet model in the embodiment of the invention).

[0134] The model outputs the probability of belonging to bug-trigger code and the probability of normal code for all cropped images (the sum of the two probabilities under the same picture is 1). For multiple cropped images belonging to the same code, each image may have a different ranking. The highest ranked image is taken as the ranking of the complete code belonging to bug-trigger code. Finally, the probability of all code ranking in this sorting input is obtained as the test code priority ranking result.

[0135] Overall, the sorting method has the following efficiency advantages:

[0136] Feature extraction efficiency: the invention uses a token-based line-level text vector generation method, which is faster and requires lower hardware than tree or graph-based methods. This is because the token-based method directly extracts keywords and phrases from the code without constructing and processing the abstract syntax tree (AST) or control flow graph (CFG) of the entire code. The construction of these structures can be very time-consuming in complex code.

[0137] Code pixelization: The process of converting code into images (pixelization) simplifies the representation of features, allowing the use of the powerful image recognition capabilities of Convolutional Neural Networks (CNNs). This approach avoids complex structural analysis of the code, reducing computational load; moreover, the conversion of code into images is very fast, with 5000 CSmith codes converted into images in about 37 minutes (2200s), with an average conversion time of 0.44s per code. Image segmentation takes 1 minute and 18 seconds, with an average segmentation time of 0.0156s per code. After segmentation, 94614 small images are generated. The additional processing time per code is 0.4556s on average.

[0138] Application of Convolutional Neural Networks (CNNs): CNNs are well-established in the field of image recognition, capable of efficiently processing and recognizing features in images. By converting code into images, the invention uses CNNs for feature learning, which can process large amounts of data more quickly than traditional software testing methods such as static analysis or dynamic analysis.

[0139] Application of Transfer Learning: The invention uses transfer learning to fine-tune a pre-trained ResNet model, which can use the model weights pre-trained on large-scale datasets such as ImageNet, reducing the time and computational resources required to train the model from scratch.

[0140] Non-greedy matching and simplified regular expressions: In the feature extraction process, the use of non-greedy matching and simplified regular expressions can reduce unnecessary calculations, improve matching efficiency, and thus reduce the overall computational load.

[0141] Ability to handle large-scale datasets: The invention mentions that no layers are frozen during fine-tuning, indicating that the model has the ability to handle large-scale datasets. With sufficient data to learn and a lower learning rate, overfitting is less likely. This allows the model to adapt more quickly to new tasks while maintaining training efficiency.

[0142] Ranking efficiency: In the scheduling phase, the model outputs the probability of each part of the image triggering a bug and ranks them by probability. This approach can prioritize a large number of test cases more quickly than traditional rule-based or static analysis-based methods.

[0143] Applicability and flexibility: The method of the invention does not rely on information within the test code generated by the fuzzing generator, making it applicable to C language test codes of various sources and lengths. This flexibility allows the method to be applied to different compilers and code bases without the need to redesign and adjust the algorithm for each new environment.

[0144] Based on the same technical concept as the method embodiment, the application also provides a compiler test case priority sorting system based on a text vector and a convolutional neural network, comprising:

[0145] A training data acquisition module is configured to acquire training code, wherein the training code comprises code capable of triggering a bug and normal code incapable of triggering a bug from different compiler versions;

[0146] A code pixelization processing module is configured to perform code pixelization processing on the training code, comprising: extracting code features from each line of training code and combining the code features into a vector, splicing multiple lines of vectors into a matrix, converting the matrix into a grayscale image and performing cutting to keep the length and width of the image consistent, and supplementing the insufficient part with black to obtain a training image;

[0147] A model training module is configured to input the training image into a convolutional neural network to perform supervised neural network model training, and save the neural network model after fine-tuning to obtain a retrained neural network model;

[0148] A test case code processing module is configured to call the code pixelization processing module, use the same code pixelization processing method as the training code, perform code pixelization processing on the test case code to be sorted, obtain a test case image, input the test case image into the retrained neural network model, and output the probability of each part of the image capable of triggering a bug by the model;

[0149] A code priority sorting module is configured to sort the probability output by the model, for multiple cut images belonging to the same code, each image may have a different ranking, and the image with the highest ranking is taken as a representative ranking of the probability of the complete code triggering a bug, to obtain a code ranking probability as a test code priority sorting result.

[0150] The embodiment of the application also provides a computer readable storage medium having a computer program / instruction stored thereon, wherein the computer program / instruction is executed by a processor to implement the steps of the compiler test case priority sorting method based on a text vector and a convolutional neural network.

[0151] The embodiment of the application also provides a computer program product comprising a computer program / instruction, wherein the computer program / instruction is executed by a processor to implement the steps of the compiler test case priority sorting method based on a text vector and a convolutional neural network.

[0152] The above only describes the preferred embodiments of the application, and it should be noted that those skilled in the art can make several improvements and refinements without departing from the principles of the application, and these improvements and refinements should also be considered as the protection scope of the application.

Claims

1. A compiler test case priority ordering method based on text vectors and convolutional neural networks, characterized in that, The method comprises the following steps: obtaining training code, the training code comprising code capable of triggering a bug and normal code incapable of triggering a bug from different compiler versions; code pixelization processing is performed on the training code, comprising: extracting code features from each line of training code and combining the code features into a vector, splicing multiple lines of vectors into a matrix, converting the matrix into a grayscale image, and cutting the grayscale image to keep the length and width of the image consistent, and supplementing the insufficient part with black, to obtain a picture for training; the picture for training is input into a convolutional neural network for supervised neural network model training, and the neural network model is fine-tuned and saved, to obtain a retrained neural network model; the same code pixelization processing method as that for the training code is used to perform code pixelization processing on test case code to be sorted, to obtain a test case picture, the test case picture is input into the retrained neural network model, and the model outputs the probability that each part of the picture can trigger a bug; the probabilities output by the model are sorted, for multiple cut pictures belonging to the same code, each picture may have a different ranking, the picture with the highest ranking is taken as a representative ranking of the probability that the complete code triggers a bug, to obtain a code sorting probability as a test code priority sorting result; wherein the extraction of code features from each line of training code and the combination of the code features into a vector, and the conversion of the matrix into a grayscale image comprise: extracting N features of each line of training code, and converting the N features into an N-dimensional vector, one feature corresponding to one dimension in the vector, the value of each dimension being the number of times the feature appears, splicing the row vectors of each line into a matrix, and converting the elements in the matrix into corresponding grayscale image pixels, the grayscale value being equal to the size of the element value in the matrix; the extraction of N features of each line of training code comprises: for each line of training code, identifying all statement keywords as statement features, identifying type keywords and modifier keywords as type and modifier features, identifying operators as operator features, identifying function declarations and call relationships as function features, and identifying all features of each line of training code; the specific identification methods of various code features are as follows: the keywords and phrases of various code features are defined in advance, regular expressions are used to match corresponding code elements, strings matching the defined keywords and phrases are extracted as code features.

2. The method of claim 1, wherein, The regular expression matching uses a non-greedy matching method.

3. The method of claim 1, wherein, There are 53 types of code features in each line of code, including 41 types of keyword-defined keyword features and 12 types of phrase-defined structural features c_assignments, break_blocks, return_blocks, continue_blocks, goto_blocks, def_blocks, struct_blocks, union_blocks, call_blocks, calc_blocks, if_else_blocks, and for_blocks. ​ 4. The method of claim 1, wherein, The neural network model is fine-tuned, including: using a method of transfer learning to fine-tune the neural network model.

5. A compiler test case prioritization system based on text vectors and convolutional neural networks, characterized in that, Comprise: The training data acquisition module is used for acquiring training code, and the training code comprises code capable of triggering a bug and normal code incapable of triggering a bug from different compiler versions; The code pixelization processing module is used for code pixelization processing of the training code, including: extracting code features from each line of training code and combining the code features into a vector, splicing multiple vectors into a matrix, converting the matrix into a grayscale image, and cutting the grayscale image to keep the length and width of the image consistent, and supplementing the insufficient part with black, to obtain a picture for training; The model training module is used for inputting the picture for training into a convolutional neural network for supervised neural network model training, fine-tuning the neural network model, and saving the fine-tuned neural network model, to obtain a retrained neural network model; The test case code processing module is used for calling the code pixelization processing module, using the same code pixelization processing method as the training code to perform code pixelization processing on the test case code to be sorted, obtaining a test case picture, inputting the test case picture into the retrained neural network model, and outputting a probability of each part of the picture capable of triggering a bug by the model; The code priority sorting module is used for sorting the probability output by the model, and for multiple cut pictures belonging to the same code, each picture may have a different ranking, and the picture with the highest ranking is taken as a representative ranking of the probability of the complete code triggering a bug, to obtain a code sorting probability as a test code priority sorting result; Wherein, the extracting code features from each line of training code and combining the code features into a vector, splicing multiple vectors into a matrix, and converting the matrix into a grayscale image, comprise: Extracting N features of each line of training code, and converting the N features into an N-dimensional vector, one feature corresponding to one dimension in the vector, and the value of each dimension being the number of times of occurrence of the feature, splicing the row vectors of each line into a matrix, and converting the elements in the matrix into corresponding grayscale image pixels, with the grayscale value being equal to the size of the element value in the matrix; The extracting N features of each line of training code comprises: For each line of training code, identifying all statement keywords as statement features, identifying type keywords and modifier keywords as type and modifier features, identifying operators as operator features, identifying function declarations and call relationships as function features, and identifying all features of each line of training code; The specific identification method of various code features is as follows: defining keywords and phrases of various code features in advance, matching corresponding code elements using a regular expression, extracting strings matched with the defined keywords and phrases as code features.

6. A computer readable storage medium having stored thereon computer programs / instructions, characterized in that, The computer program / instructions are executed by the processor to implement the steps of the compiler test case priority sorting method based on the text vector and the convolutional neural network according to any one of claims 1-4.

7. A computer program product comprising computer programs / instructions, characterized in that, The computer program / instructions are executed by the processor to implement the steps of the compiler test case priority sorting method based on the text vector and the convolutional neural network according to any one of claims 1-4.

Citation Information

Patent Citations

  • Code and combination coverage-based test case priority ranking method and test system

    CN110134588A

  • Code snippet recommendation method and device based on deep neural network

    CN111142850A