Program verification methods, processors, electronic devices, and readable storage media

By generating expected member layout information during program compilation and periodically comparing it with real-time member layout information, the problem of program crashes caused by inconsistent memory layout in existing technologies is solved, achieving automated fault detection and rapid response, and improving program stability and security.

CN123114932APending Publication Date: 2026-11-17LOONGSON ZHONGKE (XIAN) TECH CO LTD
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202611169485.6
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2026-08-03
Publication Date
2026-11-17

AI Technical Summary

Technical Problem

Existing debugging tools cannot effectively prevent inconsistencies in memory layout during program execution, which can lead to program crashes. Furthermore, the lack of automated inspection mechanisms increases the difficulty of software development and maintenance.

Method used

During program compilation, expected member layout information is generated as a verification standard. The processor periodically obtains real-time member layout information and compares it with expected member layout information to generate layout warnings, promptly detect memory layout inconsistencies, and prevent program crashes.

Benefits of technology

It enables rapid fault detection during program execution, reduces manual intervention, improves fault identification and handling efficiency, and ensures program stability and security.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN123114932A_ABST
    Figure CN123114932A_ABST
Patent Text Reader

Abstract

This application provides a program verification method, processor, electronic device, and readable storage medium. The method includes: acquiring, at a preset period, the real-time member layout information corresponding to the compiled program to be tested; wherein the member layout information includes the member type and member offset corresponding to each program component in the program to be tested, used to identify the memory layout corresponding to the program to be tested; and generating a layout warning prompt when it is determined that the member layout information is inconsistent with the expected member layout information, so as to process the program to be tested; wherein the expected member layout information is obtained based on the compiler during the compilation of the program to be tested. This application can detect memory layout inconsistencies in a timely manner and provide risk warnings, enabling users or subsequent fault handling devices / applications to handle program crashes and other program operation failures caused by memory layout inconsistencies in a timely manner, reducing fault handling latency and improving program verification efficiency.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This invention relates to the field of computer technology, and in particular to a program checking and verification method, a processor, an electronic device, and a readable storage medium. Background Technology

[0002] In modern computer systems, the compatibility of application binary interfaces (ABIs) is crucial for ensuring software interoperability across different compilers, platforms, or versions. In related technologies, binary compatibility often involves multiple aspects. Debugging tools, by parsing information provided by the compiler, can reconstruct the source code-level execution logic and effectively diagnose control flow anomalies and logical errors in the program.

[0003] However, existing debugging mechanisms mainly analyze the correctness of program execution logic, and usually ignore whether there are deviations in the memory layout of data structures in the program. Summary of the Invention

[0004] In view of the above problems, this application proposes an embodiment to provide a program verification method that overcomes or at least partially solves the above problems. This method can promptly confirm the program's memory layout, issue warnings when memory layout anomalies are detected, and prompt the user to perform corresponding processing. For program crashes caused by memory inconsistencies, this method avoids manually calling other tools to identify the cause of the fault after the program crashes, reducing the delay in fault identification and processing.

[0005] Accordingly, embodiments of the present invention also provide a processor, an electronic device, and a computer program product to ensure the implementation and application of the above methods.

[0006] In a first aspect, embodiments of the present invention disclose a program verification method applied to a processor, the method comprising: According to a preset cycle, the member layout information corresponding to the compiled program to be tested in real time is obtained; wherein, the member layout information includes the member type and member offset corresponding to each program component in the program to be tested, which is used to identify the memory layout corresponding to the program to be tested. If the member layout information is determined to be inconsistent with the expected member layout information, a layout warning is generated to process the program under test; wherein the expected member layout information is obtained by the compiler during the compilation of the program under test.

[0007] In a second aspect, embodiments of the present invention disclose a processor, the processor comprising: The parameter acquisition module is used to acquire the real-time member layout information of the compiled program to be tested according to a preset period; wherein, the member layout information includes the member type and member offset of each program component in the program to be tested, which is used to identify the memory layout of the program to be tested. A warning generation module is used to generate a layout warning prompt when it is determined that the member layout information is inconsistent with the expected member layout information, so as to process the program to be tested; wherein the expected member layout information is obtained based on the compiler during the compilation of the program to be tested.

[0008] Thirdly, embodiments of the present invention disclose an electronic device, including: a processor, a memory, a communication interface, and a communication bus, wherein the processor, the memory, and the communication interface communicate with each other through the communication bus; the memory is used to store at least one executable instruction, wherein the executable instruction causes the processor to perform the steps of any of the aforementioned program verification methods.

[0009] Fourthly, embodiments of the present invention disclose a readable storage medium storing a program or instructions, which, when executed by a processor, can implement any of the program verification methods described above.

[0010] Fifthly, embodiments of the present invention disclose a computer program product, including a computer program, wherein when the computer program is executed by a processor, the steps of the program verification method described in any of the foregoing are as follows.

[0011] The embodiments of this application have the following advantages: In this embodiment, the expected member layout information of the program under test is generated by the compiler during program compilation and used as a verification standard for the memory layout of the program under test. The member layout information includes member types and member offsets, which can comprehensively identify the memory layout of the current program. Subsequently, the member layout information corresponding to the program under test in real time is obtained multiple times according to a preset period, and compared with the expected member layout information generated by the compiler during compilation. The correctness of the member layout information is confirmed in a timely manner, that is, the memory layout information of the program during runtime can be confirmed in a timely manner, and the runtime risks of the program under test in the current period can be quickly detected or detected in advance, so as to achieve rapid verification. This allows for rapid fault handling when the program under test crashes due to inconsistency between the memory layout and the expected layout. Attached Figure Description

[0012] Figure 1 This is a flowchart illustrating the steps of an embodiment of the procedure verification method of this application; Figure 2 This is a schematic diagram of a debugging information parsing step according to this application; Figure 3 This is a schematic diagram of a layout consistency check step according to this application; Figure 4 This is a structural block diagram of one embodiment of the processor in this application; Figure 5 This is a schematic diagram of the structure of the electronic device provided in an embodiment of the present invention. Detailed Implementation

[0013] To make the above-mentioned objects, features and advantages of the present invention more apparent and understandable, the present invention will be further described in detail below with reference to the accompanying drawings and specific embodiments.

[0014] The terms "first," "second," etc., used in the specification and claims of this invention are used to distinguish similar objects and not to describe a specific order or sequence. It should be understood that such data can be interchanged where appropriate so that embodiments of the invention can be implemented in orders other than those illustrated or described herein, and the objects distinguished by "first," "second," etc., are generally of the same class and the number of objects is not limited; for example, the first object can be one or more. Furthermore, the term "and / or" in the specification and claims is used to describe the relationship between related objects, indicating that three relationships can exist. For example, A and / or B can represent: A alone, A and B simultaneously, and B alone. The character " / " generally indicates that the preceding and following related objects are in an "or" relationship. In embodiments of this invention, the term "multiple" refers to two or more, and other quantifiers are similar.

[0015] Binary interface (ABI) compatibility is crucial for ensuring software interoperability across different compilers, platforms, and versions. Binary compatibility involves many aspects, one of the most important being memory layout. In particular, the consistency of memory layout for structures (or classes) ensures that the program can correctly access data in different environments.

[0016] If the memory layout of a structure changes, even if the code logic remains the same, it may cause errors during program execution, manifesting as data access errors or memory overflows, or even causing the program to crash.

[0017] Debugging tools in related technologies can help developers analyze control flow and logic errors in programs, but they have the following problems: Post-incident diagnosis rather than pre-incident prevention: Existing debugging tools mainly focus on capturing control flow or logic errors during program execution, lacking effective preventative mechanisms for low-level structural issues such as memory layout inconsistencies. Developers only become aware of problems when inconsistencies in structural memory layout cause program crashes or other abnormal behaviors. This approach fails to detect potential memory layout issues in advance and is inefficient for prevention and early problem detection.

[0018] The inability to automate debugging: Existing debugging tools generally rely on manual intervention, requiring users to manually operate the tool after a program failure and step-by-step trace the program's execution to discover potential layout issues. This manual debugging method is inefficient and unsuitable for large-scale application in large software projects or complex systems. It is also too cumbersome for projects that require frequent verification of memory layout consistency. This lack of proactive checks increases the difficulty of software development and maintenance, and may lead to serious system errors caused by memory layout problems.

[0019] Reference Figure 1 The diagram illustrates a flowchart of an embodiment of a program verification method according to this application. The method is applied to a processor and may include the following steps: Step 101: According to a preset period, obtain the real-time member layout information corresponding to the compiled program to be tested; wherein, the member layout information includes the member type and member offset corresponding to each program component in the program to be tested, which is used to identify the memory layout corresponding to the program to be tested. Step 102: If the member layout information is determined to be inconsistent with the expected member layout information, a layout warning is generated to process the program to be tested; wherein the expected member layout information is obtained by the compiler during the compilation of the program to be tested.

[0020] The program verification method provided in this embodiment of the invention can be applied to processors to optimize fault detection methods during program execution and improve program fault detection efficiency.

[0021] In this embodiment, during the compilation of the program to be tested, the compiler generates expected member layout information by parsing the source code of the program to be tested. During the execution of the program after compilation, the member layout information of the program to be tested within the current period is obtained in real time according to a preset period, and compared with the expected member layout information. If the two are inconsistent, a layout warning is generated to indicate that the member layout information (i.e., the memory layout of the program to be tested in the current period) is incorrect, so that the user or other fault handling applications / devices can perform corresponding processing in a timely manner.

[0022] It is understood that the embodiments of this application can be executed by the processor alone, that is, only the program verification method described in this application is executed according to a preset cycle. Alternatively, this embodiment can also be integrated into a debugging tool deployed on the processor (for example, the code segment implementing this embodiment is written into the debugging tool), that is, the debugging tool is activated according to a preset cycle. While the debugging tool executes the execution logic of the program under test within the current cycle, it can also verify the memory layout of the program under test within the current cycle according to the program verification method provided in this application, so as to perform a comprehensive verification of the program under test, thereby timely discovering and handling corresponding problems in the program under test.

[0023] The preset period can be, for example, a preset time period or a preset code segment / line period. That is, it can be set according to user needs to automatically trigger the execution of the program verification method mentioned in the current embodiment after the program has run for a certain period of time (preset time period); or, after the program under test has run a certain number of code lines / segments (preset code segment / line period), it can automatically trigger the execution of the program verification method mentioned in the current embodiment. In other words, this application proposes to automatically execute the memory layout verification of the program under test after preset conditions, eliminating the need for manual debugging and avoiding confirmation only after the program under test crashes. It can promptly extract and verify the memory layout state of the program under test.

[0024] Specifically, after the program to be tested is compiled (e.g., during runtime), the member layout information corresponding to the program to be tested will be periodically obtained according to a preset period. This information will be compared with the expected member layout information generated by the compiler during compilation to confirm whether the member layout information is correct.

[0025] The memory layout of the program under test includes, for example, the memory address space allocated by the operating system to the running program, and the region divisions within that memory address space. The member layout information of the program under test, such as member type and member offset, is the actual manifestation of the arrangement rules of fields in the allocated address storage space of the program under test. Specifically, the member type may include regular layout information such as the total size of each member, alignment requirements, and related padding bytes; the member offset specifically includes the offset of each member relative to the total (or a specified) starting address. Both together reflect the memory layout of the program under test in the current cycle.

[0026] If the real-time member layout information of the current program under test is found to be inconsistent with the expected member layout information, a layout warning message in a predetermined format can be generated.

[0027] Understandably, inconsistent memory layout can lead to program crashes. For example, widespread inconsistencies or inconsistencies in the memory layout of a critical part of the program can easily cause crashes. However, small-scale inconsistencies, such as inconsistencies in the member layout information corresponding to parameters of non-critical nodes, do not affect program operation and will not cause crashes. For instance, if the member type of a member is changed but the member offset remains the same (e.g., still occupying 4 bytes of memory), the program may not be affected (if the program's execution logic has a certain tolerance, this error can be ignored), and it will continue to run normally. However, this may be the early stage of a large-scale memory layout inconsistency. For example, after running for a period of time, more members may have inconsistent member layout information. In other words, the program may be in a pre-crash stage, able to run but at risk of crashing.

[0028] The program verification method provided in this embodiment can identify this potential problem in a timely manner. Even if there are only a few instances of memory inconsistency, it will provide timely feedback and remind users or other fault handling devices to pay attention to memory layout issues through layout warning prompts, and perform corresponding fault handling to prevent program crashes and improve program running efficiency.

[0029] Alternatively, if the program under test has already crashed, the periodic layout warnings can quickly identify a memory layout inconsistency, indicating that the crash was caused by this inconsistency, allowing for timely intervention. All of the above fault identification processes require no manual user intervention, enabling rapid response to memory inconsistencies and prompt resolution of corresponding security risks.

[0030] In some cases, the compiler does not provide expected member layout information by default because it would increase the compiler's computational complexity during compilation, and the generated expected member layout information would also require additional storage space. In the embodiments of this application, a compiler with member layout information generation capabilities is required to compile the program to be tested, and the compiler's member layout information generation capability must be enabled before the compiler runs; or a member layout information generation capability can be added to the compiler to be used, so as to synchronously generate the expected member layout information corresponding to the program to be tested during compilation.

[0031] Optionally, for some compilers that currently have member layout information generation capabilities, this feature can be enabled by adding the "-g" suffix to the compiler's invocation command. This can be done either by the user manually entering the invocation command and adding the "-g" suffix, or by modifying the configuration of the application calling the compiler so that it adds "-g" to the invocation command, resulting in an invocation command that indicates "debug information needs to be generated." The runtime memory layout can then be viewed from the generated debug information using a debugger such as GDB (GNU Debugger).

[0032] In this embodiment, during program compilation, the compiler generates expected member layout information for the program under test, which serves as a verification standard for the program's memory layout. The member layout information includes member types and member offsets, comprehensively identifying the current program's memory layout. Subsequently, the member layout information corresponding to the program under test in real time is acquired multiple times at preset intervals and compared with the expected member layout information generated by the compiler during compilation. This promptly confirms whether the member layout information is correct, thus enabling timely confirmation of the program's memory layout information during runtime. This allows for rapid or early detection of runtime risks within the current period, facilitating rapid verification. Furthermore, it enables quick fault handling when the program crashes due to inconsistencies between its memory layout and the expected layout.

[0033] Furthermore, programs are vulnerable to security attacks during modification or updates, which typically manifest as alterations to the memory layout of the program under test. Unauthorized intruders can exploit these changes to hijack internal functions, access sensitive data, and cause discrepancies between the actual and expected memory layouts of the program. For example, changing a member type from `int` to `unsigned int` doesn't change the size (both types occupy 4 bytes) or the offsets of subsequent members. Modifying only this one member might fall within the tolerance range of the program's execution logic, allowing it to continue running normally. The periodic memory consistency verification method proposed in this application can promptly detect memory inconsistencies, including those caused by security attacks. Therefore, this implementation further enhances program security.

[0034] In an optional embodiment of this application, the method may further include: if the member layout information and the expected member layout information are the same, then determine that the member layout information and the expected member layout information are consistent; otherwise, determine that the member layout information and the expected member layout information are inconsistent.

[0035] It is understandable that the memory layout of the program under test at the binary level refers to the memory address space allocated by the operating system to the running program under test, and the division of that memory address space into regions for different parameters. In other words, the memory layout includes what content the program under test stores at each memory location, and the size of that content in memory. In this example, the content pointed to by each member can be represented by its member type (to confirm how that content is subsequently read), and the size of that content in memory can be determined. The storage location of each member can be represented by its member offset (i.e., the offset from the initial position). Both of these can uniquely determine the memory layout of the program under test.

[0036] The member type determines the memory size occupied by the member (e.g., 4 bytes for int, 8 bytes for double) and how the member is read. If the member types are the same, it means that the bit width and interpretation method for that member are the same during compilation and after compilation, and therefore they are considered to be the same member. Additionally, if the member offsets are the same, it means that the same member is stored in the same location in memory, resulting in the same final memory layout.

[0037] In addition, member names can be used as a criterion for consistency verification of memory layout. Member names are usually user-defined and generated for easy subsequent independent use. When multiple members of the same type exist, their names can be used for further identification to confirm whether they are the same member, thus further confirming memory layout consistency.

[0038] In this example, a unique memory layout is identified by member type and member offset. The unique memory layout obtained from real-time member layout information is then compared with the memory layout obtained from expected member layout information to confirm whether the memory layouts are consistent. Compared to individually identifying memory layout characteristics such as member type or member offset, a more comprehensive memory layout can be obtained, thus generating a more accurate memory consistency comparison result.

[0039] In some implementations, the member layout information further includes the number of members; in this case, the method further includes: S11, if the number of members is different from the number of members in the expected member layout information, it is determined that the member layout information is inconsistent with the expected member layout information; S12, otherwise, obtain the member type and the member offset; S13, if the member type and the member offset are the same as those in the expected member layout information, it is determined that the member layout information is consistent with the expected member layout information; otherwise, it is determined that the member layout information is inconsistent with the expected member layout information.

[0040] In this example, the system first filters based on the number of members to identify cases where the member layout information does not match the expected member layout information, such as when the number of members is the same. Then, it performs further consistency verification based on the member type and member offset. This hierarchical filtering can improve the speed of consistency verification.

[0041] Specifically, the number of members can be obtained quickly. For example, the current member layout information can be directly traversed, and the count can be performed simply by using sequence macros, without needing to read the complete member content and further parse it to obtain the specific member type, member offset, etc. At the same time, the comparison of member counts only requires lightweight computation. Compared with the comparison process of parameters such as member type and member offset, the computational cost is close to zero, and the comparison results can be obtained quickly.

[0042] Furthermore, the number of members can be considered a quick filtering condition. If the numbers are inconsistent, meaning there will definitely be different members, then the conclusion that the memory layouts of the two are inconsistent is directly obtained (in this example, this is reflected in the inconsistency between the real-time member layout information and the expected member layout information). There is no need to further compare the member types, member offsets, etc., of the member layout information and the expected member layout information separately, avoiding complex calculations involving detailed comparisons and preventing resource waste.

[0043] If the number of members is consistent, further comparisons are made regarding member types and member offsets to confirm whether the member layout information matches the expected member layout information. Only if both member types and member offsets are the same is the memory layout considered consistent. If either the member type or the member offset differs, the real-time member layout information is determined to be inconsistent with the expected member layout information.

[0044] In this example, the number of members is first obtained for initial quick filtering. Only when the number of members is the same does the subsequent detailed judgment of member type and member offset proceed. This provides an optimized judgment path: quick filtering is performed first, and only those that pass the filtering are compared in detail. This avoids some computationally intensive detailed comparison steps, improves the speed of member layout information judgment, and enhances processing efficiency.

[0045] In one embodiment of this application, the compiler generates basic debugging information of the program to be tested during the compilation process; the basic debugging information includes debugging tags and program components, with different debugging tags corresponding to different program components; wherein, the program components include structures and classes; Then, the method further includes: Filter out debugging tags that are identified as structures or classes from the debugging tags, and use them as target debugging tags; obtain the member layout information of the program component corresponding to the target debugging tag, and use it as the expected member layout information.

[0046] In this example, the basic debugging information directly generated by the compiler is filtered, and only the member layout information corresponding to the structure and class is selected as the expected member layout information. This reduces the amount of information that needs to be processed for memory consistency comparison and improves the comparison speed of memory layout consistency.

[0047] The basic debugging information directly generated by the compiler, such as DWARF (Debugging With Attributed Record Formats) debugging information, typically includes DWARF tags and corresponding program components. Each program component includes members and other constituent elements, and each program component has a predetermined data structure. Program components include data structures such as structures, classes, arrays, and pointers.

[0048] For example, the DWARF label and the program artifact it identifies are, for instance: DW_TAG_structure_type is used to identify the structure; DW_TAG_class_type is used to identify the class; DW_TAG_array_type is used to identify arrays; DW_TAG_pointer_type is used to identify pointers; DW_TAG_union_type is used to identify a union.

[0049] In this context, DW_TAG_ is the naming convention prefix for DWARF tags, and it has no other meaning in the examples above.

[0050] Each program component contains members as constituent elements. For a program component of a structure type, its members include, for example, DW_TAG_member. This member includes at least (i.e., the next level parameter has) the DW_AT_type member type and the DW_AT_data_member_location member offset.

[0051] For example, a member of a structure: DW_TAG_member DW_AT_name: ("x"); DW_AT_type: <int>; DW_AT_data_member_location: 0.

[0052] In the example information above, it is indicated that the name of this member is x, the member type is int, and the member offset is 0.

[0053] In this example, the debug tags (such as the DWARF tag mentioned above) in the basic debug information generated by the compiler are first filtered, and the debug tags that identify the current member as a structure or class are selected as the target debug tags. Then, only the member layout information in the program component corresponding to the target debug tag is obtained, that is, only the member layout information corresponding to the structure and class is obtained, and this is used as the expected member layout information.

[0054] That is, the implementation shown in this example only obtains the member layout information corresponding to the structure and class when filtering the expected member layout information, and uses it as the standard for judging memory layout consistency. It will filter out the member layout information in other program components such as arrays and pointers, reduce the parameters involved in the consistency comparison, thereby reducing the computational complexity of consistency judgment and improving judgment efficiency.

[0055] Specifically, arrays, pointers, and other program components are typically stacked single elements by default. The format or type of this element is determined by the source code of the program being tested; without changes to the source code, the format or type of the element will remain unchanged. Even if illegally accessed during runtime after compilation, only the parameter values ​​of the elements might be altered; conversely, the format or type of the elements usually remains unchanged. With the element format or type unchanged, the memory layout of the program component remains constant because the format or type of the elements has a fixed arrangement and memory size.

[0056] Therefore, if the memory layout of arrays, pointers and other program components in a program under test is always consistent at different time points, then members in program components other than arrays, pointers and other structures and classes will not be retrieved to perform consistency checks.

[0057] However, structs and classes are not simply stacks of single elements. For example, even a single member of a struct in the above example includes parameters in different formats, such as member name, member type, and member offset. Therefore, heterogeneous composite components like structs and classes inherently carry layout inconsistency risks. It is necessary to obtain the member layout information to confirm the consistency of the member layout information, i.e., to perform memory consistency confirmation.

[0058] In other examples, there exists a type of program component called a flexible array. Its member type provided at compile time is char[], which is an incomplete array. Even if this program component identifies the specific type as char, it does not provide the memory size it needs; it is only measured, generated, and recorded at runtime. Therefore, complete memory layout information is not generated during the compilation phase, and thus, this type of program component does not require memory consistency verification.

[0059] It's understandable that a union is not simply a combination of single elements. In some specific implementations, unions can be filtered together with structs and classes. This involves using debug tags that identify structs, classes, and unions as target debug tags and obtaining the member layout information from the structs, classes, and unions as the desired member layout information.

[0060] In this example, for the basic debugging information directly generated by the compiler, the information is filtered based on the corresponding debugging tags. Only the member layout information of structures, classes (and unions) is selected as the expected member layout information. That is, only the members corresponding to structures, classes, and unions (i.e., the aforementioned DW_TAG_member) are selected to obtain the relevant member layout information. Memory layout consistency is then verified based on this member layout information. For other program components in the program to be tested, firstly, they lack the concept of memory layout and are simply single-format numerical aggregates (such as arrays and pointers), so layout consistency verification is unnecessary. Secondly, their memory layout may only be fully generated during program runtime (such as flexible arrays). Currently, the program to be tested has only completed compilation (programs are usually compiled before running), and these program components have not yet generated corresponding memory layouts. Therefore, a complete memory layout cannot be collected as a reference, and layout consistency verification for this type of program component is not required later.

[0061] In one example, the method further includes: skipping the step of obtaining the member layout information of the program component corresponding to the target debug tag when the number of members in the structure or the class is empty.

[0062] In this example, if a program component is found to have zero members, the step of reading the members in the current program component is skipped. This avoids contamination of the expected member layout information and ensures the accuracy of the consistency confirmation between the current member layout information and the expected member layout information.

[0063] For example, let's take a structure in a program component as an example. The program to be tested may contain multiple structures, such as the Point structure and the Empty structure, as follows: DW_TAG_structure_type DW_AT_name: ("Point") DW_TAG_member DW_AT_name: ("x") DW_AT_type: <int> DW_AT_data_member_location:0 DW_TAG_member DW_AT_name: ("y") DW_AT_type: <int> DW_AT_data_member_location:4 The above is the first structure, Point, which includes members x and y. DW_TAG_member identifies the existence of this member, DW_AT_name is the member name, DW_AT_type is the member type, and DW_AT_data_member_location is the member offset. All members are of type int (int). The member offset for x is 0, and the member offset for y is 4.

[0064] DW_TAG_structure_type DW_AT_name: ("Empty") DW_TAG_template_type_parameter DW_AT_name: ("T") DW_AT_type: DW_TAG_variable DW_AT_name: ("count") DW_AT_type: <int> The above describes the second structure, Empty. The GDB member count check command indicates that it contains no members (i.e., no DW_TAG_member), meaning the member count is 0. However, it carries various descriptive information, such as the template type parameter `template_type_parameter`, named `T` and pointing to a 0x00 placeholder, and the static data variable named `count`, with an integer data type. Additionally, it may also carry a constructor function `subprogram` or a template value parameter `value_parameter`.

[0065] Based on the aforementioned example, after obtaining the target debug tag by filtering debug tags, both the Point and Empty structures belong to the program components corresponding to the target debug tag. It is necessary to obtain the real-time member layout information of the program components of the two structure types respectively.

[0066] In this example, since the number of members in the Empty structure is 0, that is, the member data is empty, the step of obtaining the member layout information will be skipped. That is, after recognizing that there are no members in the Empty structure (i.e., there is no DW_TAG_member), the process will jump directly to the subsequent structure and there is no need to traverse all the detailed parameters in the Empty structure.

[0067] Specifically, even if the detailed information in the Empty structure is traversed (e.g., the contents of DW_TAG_template_type_parameter in the example above are read in detail), no member layout information will be read if no members are found. However, if a read or traversal exception occurs at this time, the contents of DW_TAG_template_type_parameter read may be written as member layout information into the expected member layout information, causing distortion of the expected member layout information and affecting the judgment or early warning of memory layout consistency faults in the program under test. Therefore, in this example, actively setting it to skip the detailed reading of the current structure when the number of members is confirmed to be 0 can reduce the probability of exceptions.

[0068] Furthermore, when the number of members is confirmed to be 0, the detailed reading of the current structure can be skipped directly, and the traversal of the current structure can be quickly ended. There is no need to parse redundant data other than members in detail, which can improve the generation rate of expected member layout information.

[0069] In one optional embodiment of this application, obtaining the member layout information corresponding to the compiled program to be detected in real time includes: S21, Obtain real-time debugging information of the compiled program to be tested; the real-time debugging information includes different program components, and the program components include real-time components corresponding to the current period; S22, when the identity identifier of any of the real-time components is the same as the identity identifier of the program components in the expected member layout information, the member layout information in the real-time component is used as the member layout information corresponding to the program to be detected in real time.

[0070] In this example, only program components that are identical to the identity identifier in the expected member layout information are retrieved for consistency comparison, which can reduce noise during consistency comparison.

[0071] Specifically, for reasons similar to obtaining the expected member layout information based on the compiler, this application may also obtain multiple program components for the compiled program to be tested, that is, the collected real-time debugging information may also include multiple program components.

[0072] Understandably, this application expects to retain only the member layout information corresponding to structs and classes in the member layout information for memory layout consistency judgment. Therefore, it is first necessary to filter from the real-time debugging information. For example, if debugging tags also exist in the real-time debugging information, the real-time debugging tags identified as structs and classes in the real-time debugging information can be used as the real-time target debugging tags to obtain the member layout information of the program component (i.e., the real-time component) corresponding to the real-time target debugging tag. Optionally, if the member layout information corresponding to unions is also expected to be retained in the member layout information, the debugging tags identified as unions in the real-time debugging information will also be used as the target debugging tags, and the member layout information of the program component corresponding to the target debugging tag will be obtained.

[0073] Furthermore, based on the identity identifiers of structures, classes (or even unions) in the expected member layout information, the real-time components in the real-time debugging information are filtered.

[0074] Specifically, taking real-time components as structures and identity identifiers as structure names (e.g., Point or Empty in the previous example), we filter for structures in real-time components that have the same name as those in program components in the expected member layout information. If two structures with the same name are collected during both compilation and runtime, they are considered to be the same program component from different time periods. The member layout information of this real-time component is then used as the real-time member layout information corresponding to the program under test, and its consistency is compared with the expected member layout information obtained during compilation.

[0075] In this step, program components in the real-time debugging information are filtered by identity identifiers. Only the member layout information corresponding to the program components recorded in the expected member layout information is retained, and a consistency comparison is performed between the member layout information and the expected member layout information. That is, the member layout information participating in the consistency comparison is filtered and denoised, which can improve the efficiency of the consistency comparison between the member layout information and the expected member layout information, avoid interference from redundant information, and reduce the probability of errors caused by irrelevant information participating in the consistency comparison.

[0076] In one optional embodiment of this application, when the identity identifier of the real-time component is different from the identity identifiers of all program components in the expected member layout information, the member layout information in the real-time component is recorded for backtracking verification when the program to be tested malfunctions.

[0077] It is understandable that during program execution, new structures, classes, and unions may be dynamically generated based on runtime parameters. These program components do not exist during compilation, so it is expected that these program components will not exist in the member layout information, nor will their identity identifiers exist.

[0078] According to steps S21-S22, the member layout information corresponding to the program component will not be written into the member layout information and will be compared with the expected member layout information. These program components are important components of the program to be tested and may also be important causes of program failures or crashes.

[0079] Therefore, although the processor does not collect the member layout information or compare it with the expected member layout information to confirm memory layout consistency, it still records the member layout information of these components and stores it in the corresponding log files. In the event of a failure in the program under test, the processor sequentially traces back the specific information of these newly generated structures, classes, and unions during runtime to verify whether the failure was caused by anomalies in these components. In this example, complete program runtime parameters are preserved, facilitating rapid identification of the cause of the failure or crash in the program under test.

[0080] In one embodiment of this application, a method for automatically checking the consistency of memory layout corresponding to a structure / class is provided, comprising: S1: Debugging information parsing and structure / class information extraction.

[0081] Specifically, the DWARF debugging information generated by the compiler is parsed to extract detailed information about all structures / classes, especially memory layout-related data such as the number of members, member types, and member offsets of each member within the structure. An illustrative process is as follows: Figure 2 As shown.

[0082] DWARF debugging information is generated by the compiler during compilation. Optionally, DWARF debugging information can also be embedded in a pre-formatted file for debugging tools to use when performing consistency comparisons.

[0083] Specifically, the DWARF debugging information is loaded first, and then the DWARF debugging tags in the DWARF debugging information are checked one by one to determine whether all debugging tags have been loaded. Further checks are then performed to determine whether each tag belongs to a structure / class (or union). It can be indirectly confirmed whether all DWARF debugging information has been traversed by verifying whether the DWARF debugging tags have been traversed (e.g., checking whether all debugging tags have been loaded).

[0084] For tags belonging to a struct / class (or union), extract the struct / class (or union)'s identifier (such as its name). This is to uniquely identify each struct / class (or union). If the struct contains members, the program will continue to extract relevant member information, including member name, member type, and member offset. This member information is recorded to prepare for subsequent struct memory layout consistency checks.

[0085] If a structure / class (or union) has no members, or if the current tag indicates that its corresponding program component does not belong to a structure / class (or union), then the DWARF debug tag is skipped, and the next DWARF debug tag is processed, until all DWARF debug tags have been processed, at which point the process ends. In this way, detailed memory layout information of the structure / class (or union) and its members can be filtered out, providing necessary data support for subsequent memory layout consistency checks.

[0086] S2: Layout consistency check.

[0087] In this step, we first extract the member layout information collected in real time after the current program has been compiled by the compiler (such as during runtime), that is, the memory layout information corresponding to the members in each structure or class.

[0088] After extracting the member layout information of the structure / class (or union), a consistency check of the number of members is further performed. If the number of members, member types, member offsets, etc. of a certain structure change, this implementation can promptly detect this inconsistency and provide a layout warning. That is, it can promptly detect potential memory layout problems and avoid program crashes or runtime errors caused by layout inconsistencies. Figure 3 The following is an example of a selected structure.

[0089] Specifically, the process first reads a struct / class (or union) collected in real time and checks if a struct / class (or union) with the same name has already been recorded. If the struct / class (or union) has not yet been recorded, it will be recorded, and the process will continue with the next struct / class (or union). If the struct / class (or union) has already been recorded, its number of members will be further checked to ensure it matches the struct / class (or union) with the same name provided by the compiler.

[0090] For example, this can be achieved through GDB: Python [print(f"{t.name or '<?>'}: {[f.name for f in t.fields()]}") for t in gdb.objfiles()[0].types() if t.code in (3,7,8)] Get the names (i.e., identifiers) of all structs / classes (or unions).

[0091] Here, `print(f"{t.name or '<?>'})` means outputting the name of the type corresponding to the program component `t`, or an empty value. `f.name for f in t.fields()` means collecting and displaying the names of all members of type `t`. `for t in gdb.objfiles()[0].types()` means iterating through the current DWARF debugging information. `t.code in(3,7,8)` is used to define the corresponding type of the program component `t`, including: 3 = TYPE_CODE_STRUCT, 7 = TYPE_CODE_UNION, 8 = TYPE_CODE_CLASS. Among them, 3, 7, and 8 are common type codes in the GDB Python API.

[0092] Then, by iterating through the data, the names are compared to confirm whether the struct / class / union has been recorded.

[0093] Optionally, a member count check command containing the function `len(gdb.lookup_type("type_name").fields())` can be used to output the number of members in each struct / class (or union). If the count is 0, the struct / class (or union) is discarded; if the count is non-zero, its member count is checked against the previously provided struct / class (or union) with the same name. Here, `("type_name")` refers to the name of the struct / class (or union) obtained earlier.

[0094] If the number of members is inconsistent, this inconsistency will be logged and a layout warning will be generated. If the number of members is consistent, the comparison of member types and member offsets will continue. These comparison steps ensure that each member of the struct / class (or union) has the same data type and memory layout location. If inconsistencies in member types or member offsets are found, the inconsistency will also be logged and a layout warning will be generated.

[0095] For example, this can be achieved by including a function: Python [print(f"{f.name}: type={f.type}, offset={f.bitpos / / 8}") for f ingdb.lookup_type("Point").fields()] instruction, Output the member type and member offset for each struct / class (or union). Here, ("type name") is the name of the aforementioned struct / class (or union), type represents the member name, offset represents the member offset, and fields() indicates traversing the current struct / class (or union). Then, compare the member type and member offset with the expected member layout information.

[0096] If the types and offsets of the members of both structures / classes (or unions) are identical, then the memory layout of the structures / classes (or unions) is considered identical, and this result is recorded. Subsequently, it is checked whether all structures / classes (or unions) have been traversed. If all structures / classes (or unions) have been checked, the process ends. If there are still unprocessed structures / classes (or unions), the program continues to compare from the next structure / class (or union).

[0097] This process effectively checks whether the memory layout between structures / classes (or unions) is consistent, ensuring that structures / classes (or unions) in different modules or compilation units behave consistently in memory, thereby supporting binary compatibility and correct memory access for the program.

[0098] This implementation method automates memory layout consistency checks in the development environment. It automatically detects inconsistencies based on debugging information, and if any are detected, it automatically issues a warning and generates a layout alert, requiring no manual intervention from the developer. This significantly improves detection efficiency and reduces the time and effort required for manual debugging.

[0099] It should be noted that, for the sake of simplicity, the method embodiments are all described as a series of actions. However, those skilled in the art should understand that the embodiments of the present invention are not limited to the described order of actions, because according to the embodiments of the present invention, some steps can be performed in other orders or simultaneously. Furthermore, those skilled in the art should also understand that the embodiments described in the specification are preferred embodiments, and the actions involved are not necessarily essential to the embodiments of the present invention.

[0100] Reference Figure 4 The diagram illustrates a structural block diagram of a processor embodiment of the present invention, wherein the processor may include: The parameter acquisition module 201 is used to acquire the real-time member layout information of the compiled program to be tested according to a preset period; wherein, the member layout information includes the member type and member offset of each program component in the program to be tested, which is used to identify the memory layout of the program to be tested. The warning generation module 202 is used to generate a layout warning prompt when it is determined that the member layout information is inconsistent with the expected member layout information, so as to process the program to be tested; wherein the expected member layout information is obtained based on the compiler during the compilation of the program to be tested.

[0101] Optionally, the processor further includes: The consistency determination module is used to determine that the member layout information and the expected member layout information are consistent if the member type and member offset are the same in the member layout information and the expected member layout information; otherwise, it determines that the member layout information and the expected member layout information are inconsistent.

[0102] Optionally, if the member layout information further includes the number of members, then the processor further includes: The first determination module is used to determine that the member layout information is inconsistent with the expected member layout information if the number of members is different from the number of members in the expected member layout information; otherwise, it obtains the member type and the member offset. The second determination module is used to determine that the member layout information is consistent with the expected member layout information if the member type and the member offset are the same as those in the expected member layout information; otherwise, it determines that the member layout information is inconsistent with the expected member layout information.

[0103] Optionally, the compiler generates basic debugging information for the program to be tested during compilation; the basic debugging information includes debugging tags and program components, with different debugging tags corresponding to different program components; wherein, the program components include structures and classes; then, the method further includes: The filtering module is used to filter out the debugging tags that identify the structure or the class from the debugging tags, and use them as target debugging tags; The generation module is used to obtain the member layout information of the program component corresponding to the target debug tag, as the expected member layout information.

[0104] Optionally, the processor further includes: A short-circuit module is used to skip the step of obtaining the member layout information of the program component corresponding to the target debug tag when the number of members in the structure or class is empty.

[0105] Optionally, the parameter acquisition module 201 specifically includes: The first real-time acquisition module is used to acquire real-time debugging information of the program to be tested generated after compilation; the real-time debugging information includes different program components, and the program components include real-time components corresponding to the current period; The second real-time acquisition module is used to take the member layout information in the real-time component as the real-time member layout information corresponding to the program to be detected when the identity identifier of any of the real-time components is the same as the identity identifier of the program component in the expected member layout information.

[0106] Optionally, the processor further includes: The recording module is used to record the member layout information in the real-time component when the identity identifier of the real-time component is different from the identity identifier of all program components in the expected member layout information, so as to perform backtracking verification when the program under test fails.

[0107] This application provides a processor that generates expected member layout information for a program under test during program compilation, based on the compiler, as a verification standard for the memory layout of the program under test. The member layout information includes member types and member offsets, which comprehensively identify the current memory layout of the program. Subsequently, the processor repeatedly acquires the real-time member layout information of the program under test at preset intervals, comparing it with the expected member layout information generated by the compiler during compilation. This promptly confirms whether the member layout information is correct, thus enabling timely confirmation of the program's memory layout information during runtime. This allows for rapid or early detection of runtime risks within the current period of the program under test, achieving rapid verification. Furthermore, it facilitates rapid fault handling when the program under test crashes due to inconsistencies between its memory layout and the expected layout.

[0108] As the device embodiment is basically similar to the method embodiment, the description is relatively simple, and relevant parts can be found in the description of the method embodiment.

[0109] Reference Figure 5 This is a schematic diagram of the structure of the electronic device provided in an embodiment of the present invention. Figure 5 As shown, the electronic device includes: a processor, a memory, a communication interface, and a communication bus. The processor, the memory, and the communication interface communicate with each other through the communication bus. The memory is used to store at least one executable instruction, which causes the processor to execute the steps of the program verification method of the aforementioned embodiment.

[0110] This invention provides a non-transitory computer-readable storage medium that, when the instructions in the storage medium are executed by a program or processor of a terminal, enables the terminal to perform the steps of the program verification method described in the foregoing embodiments. The various embodiments in this specification are described in a progressive manner, with each embodiment focusing on the differences from other embodiments. The same or similar parts between the various embodiments can be referred to each other.

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

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

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

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

[0115] Finally, it should be noted that in this document, relational terms such as "first" and "second" are used only to distinguish one entity or operation from another, and do not necessarily require or imply any such actual relationship or order between these entities or operations. Furthermore, the terms "comprising," "including," or any other variations thereof are intended to cover non-exclusive inclusion, such that a process, method, article, or terminal device that comprises a list of elements includes not only those elements but also other elements not expressly listed, or elements inherent to such a process, method, article, or terminal device. Without further limitations, an element defined by the phrase "comprising one..." does not exclude the presence of other identical elements in the process, method, article, or terminal device that includes said element.

[0116] This document uses specific examples to illustrate the principles and implementation methods of the present invention. The descriptions of the above embodiments are only for the purpose of helping to understand the method and core ideas of the present invention. At the same time, for those skilled in the art, there will be changes in the specific implementation methods and application scope based on the ideas of the present invention. Therefore, the content of this specification should not be construed as a limitation of the present invention.< / int> < / int> < / int> < / int>

Claims

1. A program verification method, characterized in that, Applied to a processor, the method includes: According to a preset cycle, the member layout information corresponding to the compiled program to be tested in real time is obtained; wherein, the member layout information includes the member type and member offset corresponding to each program component in the program to be tested, which is used to identify the memory layout corresponding to the program to be tested. If the member layout information is determined to be inconsistent with the expected member layout information, a layout warning is generated to process the program under test; wherein the expected member layout information is obtained by the compiler during the compilation of the program under test.

2. The method according to claim 1, characterized in that, The method further includes: If the member type and member offset are the same in the member layout information and the expected member layout information, the member layout information and the expected member layout information are determined to be consistent; otherwise, the member layout information and the expected member layout information are determined to be inconsistent.

3. The method according to claim 1, characterized in that, The member layout information also includes the number of members; therefore, the method further includes: If the number of members differs from the number of members in the expected member layout information, it is determined that the member layout information is inconsistent with the expected member layout information. Otherwise, obtain the member type and the member offset; If the member type and the member offset are the same as those in the expected member layout information, the member layout information is determined to be consistent with the expected member layout information; otherwise, the member layout information is determined to be inconsistent with the expected member layout information.

4. The method according to claim 1, characterized in that, The compiler generates basic debugging information for the program to be tested during compilation; the basic debugging information includes debugging tags and program components, with different debugging tags corresponding to different program components; wherein, the program components include structures and classes; then, the method further includes: Filter out the debugging tags that identify the structure or the class from the debugging tags, and use them as target debugging tags; Obtain the member layout information of the program component corresponding to the target debug tag, and use it as the desired member layout information.

5. The method according to claim 4, characterized in that, The method further includes: If the number of members in the structure or class is empty, skip the step of obtaining the member layout information of the program component corresponding to the target debug tag.

6. The method according to claim 1, characterized in that, The step of obtaining the real-time member layout information corresponding to the compiled program to be tested includes: Obtain real-time debugging information of the compiled program to be tested; the real-time debugging information includes different program components, and the program components include real-time components corresponding to the current period; When the identity identifier of any of the real-time components is the same as the identity identifier of the program components in the expected member layout information, the member layout information in the real-time component is used as the member layout information corresponding to the program to be detected in real time.

7. The method according to claim 6, characterized in that, The method also includes: If the identity identifier of the real-time component is different from the identity identifiers of all program components in the expected member layout information, the member layout information in the real-time component is recorded for backtracking verification when the program under test fails.

8. A processor, characterized in that, The processor includes: The parameter acquisition module is used to acquire the real-time member layout information of the compiled program to be tested according to a preset period; wherein, the member layout information includes the member type and member offset of each program component in the program to be tested, which is used to identify the memory layout of the program to be tested. A warning generation module is used to generate a layout warning prompt when it is determined that the member layout information is inconsistent with the expected member layout information, so as to process the program to be tested; wherein the expected member layout information is obtained based on the compiler during the compilation of the program to be tested.

9. An electronic device, characterized in that, include: The processor, memory, communication interface, and communication bus are provided, wherein the processor, memory, and communication interface communicate with each other via the communication bus. The memory is used to store at least one executable instruction that causes the processor to perform the steps of the program verification method as described in any one of claims 1 to 7.

10. A readable storage medium, characterized in that, The readable storage medium stores a program or instructions that, when executed by a processor, implement the steps of the program verification method as described in any one of claims 1 to 7.

11. A computer program product, comprising a computer program, characterized in that, When the computer program is executed by a processor, it implements the steps of the program verification method as described in any one of claims 1 to 7.