A system and method for memory leak analysis and testing based on GCC wrap and Python.

This memory leak analysis system, which combines GCC wrap and Python, uses the wrap library to replace memory functions and record ra register information. This solves the problem of difficulty in detecting small memory leaks in existing technologies, achieves efficient and accurate memory leak localization, and ensures system stability.

CN122240448APending Publication Date: 2026-06-19HEFEI JUNZHENG TECH CO LTD

Patent Information

Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
HEFEI JUNZHENG TECH CO LTD
Filing Date
2024-12-17
Publication Date
2026-06-19

AI Technical Summary

Technical Problem

Existing technologies are easily limited by the kernel and toolchain when locating small memory leaks, making it difficult to detect small memory leaks and pinpoint their location, leading to system stability issues.

Method used

A memory leak analysis system combining GCC wrap and Python is used. The wrap library module replaces the memory allocation and deallocation functions, and the ra register is used to record call information. Combined with the Python program module, the system can accurately locate the memory leak.

Benefits of technology

It achieves highly accurate memory leak detection, is independent of system interference, is easy to operate, and can quickly and accurately locate memory leaks to prevent system crashes.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN122240448A_ABST
    Figure CN122240448A_ABST
Patent Text Reader

Abstract

This invention provides a system and method for memory leak analysis and testing based on GCC wrap and Python. A chip supporting GCC compilation and ra registers includes a wrap library module that supports GCC wrap functionality. After linking with an application module, the application runs on the chip. The application module contains memory allocation and deallocation functions and is the actual running application. It only needs to link the wrap library module during compilation. During program execution, the wrap library functions replace the corresponding functions in the application. The wrap library functions print the addresses and calling functions of memory allocation or deallocation. The Python module performs matching analysis of memory allocation and deallocation. The Python module takes the output of the wrap library module as input to analyze whether memory allocation and deallocation match, providing code location information for locating memory leaks. A serial port is used to print information to the console or receive information sent from the console. The operation is simple, and the measurement is more accurate.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This invention belongs to the field of software detection technology, and specifically relates to a system and method for memory leak analysis and testing based on GCC wrap and Python. Background Technology

[0002] A memory leak occurs when dynamically allocated heap memory in a program is not released or cannot be released for some reason, resulting in wasted system memory, slowing down program execution, or even causing system crashes. Memory leaks are characterized by their insidious and cumulative nature, making them more difficult to detect than other illegal memory access errors. Because memory leaks are caused by unreleased memory blocks, they are considered omission-type defects rather than fault-type defects. Furthermore, memory leaks usually do not produce observable error symptoms directly but accumulate gradually, reducing overall system performance and, in extreme cases, causing system crashes.

[0003] With the increasing demands of computer applications, the design and development of applications have become increasingly complex. Developers handle a significant increase in the number of variables during program implementation, making the effective allocation and release of memory and the prevention of memory leaks increasingly crucial. For example, server applications need to run for extended periods, constantly processing requests from clients. Without effective memory management, each request processing will result in a memory leak. This not only impacts server performance but can also cause the entire system to crash. Therefore, memory management has become a primary consideration for software designers and developers.

[0004] GCC is free software released under the GPL license and is a key part of the GNU project. GCC was originally intended to be a compiler specifically written for the GNU operating system. It has now been adopted as the standard compiler by most Unix-like operating systems (such as Linux, BSD, MacOS X, etc.), and can even be used on Microsoft Windows. GCC supports a variety of computer architecture chips, such as x86, ARM, MIPS, etc., and has been ported to a variety of other hardware platforms [1].

[0005] GCC was originally named the GNU C Compiler, and it could only process the C language. However, it was quickly expanded to handle C++, and later expanded to support more programming languages, such as Fortran, Pascal, Objective-C, Java, Ada, Go, and assembly languages ​​on various processor architectures, so it was renamed the GNU Compiler Collection.

[0006] Valgrind is free software, licensed under the GNU General Public License. Currently, Valgrind supports x86, x86-64, and Linux, MIPS, and ARM on PowerPC. It also supports other unofficially supported Unix-like platforms (such as FreeBSD, NetBSD, and Mac OS X). Valgrind is a powerful development tool primarily used for memory debugging, memory leak detection, and performance analysis. Its memory leak detection capabilities are thanks to its modal dmalloc (dynamic memory debugging), and it also uses GCC's wrapping functionality. However, it cannot analyze and detect small memory leaks and cannot pinpoint the exact location of memory allocations.

[0007] Existing technologies typically use tools such as valgrind or dmalloc to locate memory leaks. These tools are highly dependent on the kernel or toolchain version, cannot locate small memory leaks, and lack comprehensive documentation for wrap and wrap data analysis.

[0008] Currently, Ingenic's T21 / T23 / T31 / T40 / T41 / T32 / A1 / X2000 / X2600 / x2580 / x1630 / x1021 / AD101 / AD102 chips, in addition to supporting GCC's wrap function, also include ra registers. This solution can be used on all of the above chips. In practice, this solution is mainly used in projects involving T41 / T23 / T32 models. During application compilation, due to compiler negligence or changes in code logic, it is easy for allocated memory to not be released. Especially for small memory leaks, it is very difficult to locate them using current methods. However, current routine projects have very strict requirements for memory and stability. Memory leaks can easily lead to crashes due to insufficient memory, requiring timely handling to avoid system stability issues caused by memory leaks.

[0009] Therefore, the shortcomings of existing technology are:

[0010] It is susceptible to limitations of the kernel and toolchain, the environment setup is relatively heavy, small memory leaks cannot be detected, and the location of detected memory leaks is not easy to pinpoint.

[0011] Furthermore, the terminology used in this art includes:

[0012] GCC is an open-source compiler system, originally developed by Richard Stallman in 1987 as part of the GNU operating system. GCC supports a variety of programming languages, including but not limited to C, C++, Fortran, Ada, Go, and D. Ingenic T / A / X / AD / C series chips use GCC as their compiler.

[0013] `wrap`: GCC's `--wrap` option is a linker option used to rewrite or wrap specific library functions in a program. Using this option, all function calls pass through a wrapper function, which then calls the original function. This is very useful for performance analysis, replacing library functions to provide custom functionality, or debugging.

[0014] Python: Python is a high-level, dynamically typed programming language invented by Guido van Rossum in 1989 and first publicly released in 1991. Python's design philosophy emphasizes code readability and conciseness, using indentation to represent code blocks, making the code structure clear and easy to understand.

[0015] ra address: The register in the chip that stores the function call address; register number 31 of the Junzheng T / A / X / AD series is the ra register.

[0016] Compilation is a process that allows the generation of an executable program or library that can run on one platform (host) and run on another platform (target). Typically, the host and target platforms can differ significantly in architecture, operating system, or hardware environment. This technique is particularly important when developing software for embedded systems, mobile devices, or different hardware architectures. Summary of the Invention

[0017] To address the aforementioned issues, the purpose of this application is to provide a simpler and more accurate method for analyzing, detecting, and locating memory leaks based on GCC wrap and Python.

[0018] Specifically, this invention provides a memory leak analysis and testing system based on GCC wrap and Python, the system comprising:

[0019] A chip that supports GCC compilation and ra registers, ensuring that the ra registers correctly reflect the call point of a function or the function that calls it; the chip includes a wrap library module that supports GCC wrap functionality. The wrap library module is a program that runs on the chip after linking the application module, specifically used to replace the memory allocation and deallocation functions in the application with the corresponding memory allocation and deallocation functions in the wrap library during linking.

[0020] The application module, which contains memory allocation and deallocation functions, is the actual running application. It only needs to link the wrap library module during compilation. When the program runs, the wrap library functions will actually replace the functions with the same names in the application. The wrap library functions print the address of memory allocation or deallocation and the calling function, so the Python program module can perform analysis and processing of memory allocation and deallocation matching.

[0021] The Python program module, by taking the output of the wrap library program module as input, analyzes whether memory allocation and deallocation match, providing code location for locating memory leaks;

[0022] The serial port is used for interaction between the chip and the operating system, and is responsible for printing information to the console or receiving information sent by the console.

[0023] The wrap library module is also used to print the addresses of memory allocation and deallocation and the value of the ra register during runtime. The printed information is used to input the Python program module to determine whether the memory allocation and deallocation match.

[0024] The ra register is used to obtain the function above the memory allocation and deallocation function, and is used to obtain the specific location of the allocation and deallocation function during disassembly.

[0025] The wrap library module replaces the corresponding memory allocation and deallocation functions in the application module during compilation, and further includes:

[0026] During compilation, the application links to the wrap library and replaces the application's corresponding memory allocation and deallocation functions;

[0027] When the program runs, it executes the functions in the wrap library, which print the addresses, sizes, and ra registers of memory allocation and deallocation.

[0028] The wrap library module performs the following functions: it writes memory allocation and deallocation functions such as malloc, vmalloc, cmalloc, realloc, and free. Inside the functions, it obtains the current value of the ra register and prints the address, size, and ra register value of the allocated or deallocated memory. The ra register records the function that calls the function.

[0029] The Python program module further includes:

[0030] The wrap library unit is used to replace memory allocation and deallocation functions in applications;

[0031] A storage unit used to record the address, size, and ra address of memory allocation and deallocation;

[0032] The print unit is used to print the address, size, and ra address of memory allocation and deallocation;

[0033] The Python program module takes the output information of the application linked with the wrap library as input, analyzes it line by line to find the location of suspected memory leaks, and then uses GCC tools addr2line -e . / test-f0x74323456 or objdump -Dz . / test>test.s to get the exact location of the corresponding memory allocation, in order to determine whether there is a memory leak.

[0034] The operating system is either Windows or Linux.

[0035] The Python program module, serving as an analysis module for information analysis, can run independently on the operating system architecture.

[0036] The chips include those from the Ingenic T / A / X / AD series.

[0037] This application also relates to a method for memory leak analysis and testing based on GCC wrap and Python, which is applicable to any of the above-mentioned systems and includes the following steps:

[0038] S1. The application software links to the wrap library, which is written in C.

[0039] S2. Run the application to generate wrap information for memory allocation and deallocation;

[0040] S3. Run the Python program and analyze the generated memory allocation and deallocation wrap information;

[0041] S4. Determine if there is a mismatch between memory allocation and deallocation; if they match, continue the analysis; if they do not match, proceed to step S5.

[0042] S5. Analyze the printed memory allocation and deallocation information using Python to obtain the ra address (the address of the memory allocation function that does not match the wrapping information); S6. Use GCC to disassemble the application to obtain the function corresponding to the ra address and the specific location of the allocation function. That is, the disassembler obtains the parent function of the memory allocation function and the specific location of the allocation function based on the ra address.

[0043] S7. By obtaining the location of the ra code, the Python analysis software program confirms whether there is a memory leak here, and returns to step S4 to perform the next analysis until all the mismatch information analyzed by the Python code is completed;

[0044] S8. Complete the memory leak analysis. After analyzing all mismatched memory allocation and deallocation addresses, the process ends.

[0045] Step S4 further includes:

[0046] Based on the wrap function in GCC, the functions malloc, cmalloc, vmalloc, realloc, and free are replaced. By analyzing whether the allocated address and the freed address match, if they match, it means that the memory usage is normal. If they do not match, there is a possibility of memory leak.

[0047] Therefore, this application has the following advantages:

[0048] It can avoid the adverse effects of existing technologies, the test is simple and the test accuracy is high; the method is completely independent, not affected by the system, and the error is very small; it can be fully implemented by software modules and the operation is simple. Attached Figure Description

[0049] The accompanying drawings, which are provided to further illustrate the invention and form part of this application, are not intended to limit the scope of the invention.

[0050] Figure 1 This is a schematic diagram of the overall framework of the system in this application.

[0051] Figure 2 This is a schematic diagram of the overall process of the method described in this application.

[0052] Figure 3 This is a code diagram of the wrap library module in this application.

[0053] Figure 4 This is a code diagram of the Python program module in this application. Detailed Implementation

[0054] To better understand the technical content and advantages of the present invention, the present invention will now be described in further detail with reference to the accompanying drawings.

[0055] This application presents a system and method for memory leak analysis and testing based on GCC wrap and Python. Running this solution before the application is finalized helps to eliminate potential memory leaks, ensuring the system's functionality and reliability. The design implements a memory leak analysis and detection method based on GCC wrap and Python. This solution is simple to operate and offers high measurement reliability.

[0056] Specifically, the system, such as Figure 1 As shown, it includes:

[0057] A chip that supports GCC compilation and ra registers, ensuring that the ra registers correctly reflect the call point of a function or the function that calls it; the chip includes a wrap library module that supports GCC wrap functionality. The wrap library module is a program that runs on the chip after linking the application module, specifically used to replace the memory allocation and deallocation functions in the application with the corresponding memory allocation and deallocation functions in the wrap library during linking.

[0058] The application module, which contains memory allocation and deallocation functions, is the actual running application. It only needs to link the wrap library module during compilation. When the program runs, the wrap library functions will actually replace the functions with the same names in the application. The wrap library functions print the address of memory allocation or deallocation and the calling function, so the Python program module can perform analysis and processing of memory allocation and deallocation matching.

[0059] The Python program module, by taking the output of the wrap library program module as input, analyzes whether memory allocation and deallocation match, providing code location for locating memory leaks;

[0060] The serial port is used for interaction between the chip and the operating system, responsible for printing information to the console or receiving information sent by the console; the console is the software interface for interacting with the chip; the wrap library program module is also used to print the addresses of memory allocation and deallocation, and the value of the ra register during runtime, and the printed information is used to input to the Python program module to obtain whether the memory allocation and deallocation match;

[0061] The ra register is used to obtain the function above the memory allocation and deallocation function, and is used to obtain the specific location of the allocation and deallocation function during disassembly.

[0062] The wrap library module replaces the corresponding memory allocation and deallocation functions in the application module during compilation, and further includes:

[0063] During compilation, the application links to the wrap library and replaces the application's corresponding memory allocation and deallocation functions;

[0064] When the program runs, it executes the functions in the wrap library, which print the addresses, sizes, and ra registers of memory allocation and deallocation.

[0065] The wrap library module performs the following functions: it writes memory allocation and deallocation functions such as malloc, vmalloc, cmalloc, realloc, and free. Inside the functions, it obtains the current value of the ra register and prints the address, size, and ra register value of the allocated or deallocated memory. The ra register records the function that calls the function.

[0066] The Python program module further includes:

[0067] The wrap library unit is used to replace memory allocation and deallocation functions in applications;

[0068] A storage unit used to record the address, size, and ra address of memory allocation and deallocation;

[0069] The print unit is used to print the address, size, and ra address of memory allocation and deallocation;

[0070] The Python program module takes the output information of the application linked with the wrap library as input, analyzes it line by line to find the location of suspected memory leaks, and then uses GCC tools addr2line -e . / test-f0x74323456 or objdump -Dz . / test>test.s to get the exact location of the corresponding memory allocation, in order to determine whether there is a memory leak.

[0071] The operating system is either Windows or Linux.

[0072] The Python program module, serving as an analysis module for information analysis, can run independently on the operating system architecture.

[0073] The chips include those from the Ingenic T / A / X / AD series.

[0074] In addition, the software architecture designed in the system is described as follows:

[0075] The software architecture consists of two parts: the wrap library module and the Python program module.

[0076] wrap library module:

[0077] The wrap library module mainly performs the following functions: writes memory allocation and deallocation functions such as malloc, vmalloc, cmalloc, realloc, and free; inside the functions, it obtains the current value of the ra register and prints the address, size, and ra register value of the allocated or deallocated memory; the ra register records the function that calls the function.

[0078] Python program modules:

[0079] The Python program module primarily takes the output information of the application linked with the wrap library as input, analyzes it line by line to find the location of suspected memory leaks, and then uses GCC tools such as addr2line -e . / test -f 0x74323456 or objdump -Dz . / test>test.s to obtain the exact location of the corresponding memory allocation, in order to determine whether there is a memory leak.

[0080] The method is applicable to any of the systems described above, such as Figure 2 As shown, the process includes the following steps: S1. The application software links to the wrap library written in C language;

[0081] S2. Run the application to generate wrap information for memory allocation and deallocation;

[0082] S3. Run the Python program and analyze the generated memory allocation and deallocation wrap information;

[0083] S4. Determine if there is a mismatch between memory allocation and deallocation; if they match, continue the analysis; if they do not match, proceed to step S5.

[0084] S5. Analyze the printed memory allocation and deallocation information using Python to obtain the ra address (the address of the memory allocation function that does not match the wrapping information); S6. Use GCC to disassemble the application to obtain the function corresponding to the ra address and the specific location of the allocation function. That is, the disassembler obtains the parent function of the memory allocation function and the specific location of the allocation function based on the ra address.

[0085] S7. By analyzing the obtained ra code location, confirm whether there is a memory leak here, return to step S4 for the next analysis, until all mismatch information analyzed by the Python code is completed;

[0086] S8. Complete the memory leak analysis. After analyzing all mismatched memory allocation and deallocation addresses, the process ends.

[0087] Step S4 further includes:

[0088] Based on the wrap function in GCC, the functions malloc, cmalloc, vmalloc, realloc, and free are replaced. By analyzing whether the allocated address and the freed address match, if they match, it means that the memory usage is normal. If they do not match, there is a possibility of memory leak.

[0089] Furthermore, the code illustration of the wrap library program module described in this application is as follows: Figure 3 As shown, the code diagram of the Python program module is as follows: Figure 4 As shown.

[0090] GCC's wrap function, as a memory analysis tool, can replace memory allocation and deallocation functions during compilation, unaffected by main processor load, memory resources, and other resources. Furthermore, the wrap function is simple, easily obtaining the memory allocation and deallocation addresses, sizes, and ra addresses after replacement. Combining these advantages, GCC's wrap function is easy to use, highly accurate, and well-suited for analyzing memory leaks in C language. Python, as a tool available on Windows and Linux, makes programs written in Python easy, convenient, and fast to run.

[0091] The above description is merely a preferred embodiment of the present invention and is not intended to limit the present invention. For those skilled in the art, various modifications and variations can be made to the embodiments of the present invention. Any modifications, equivalent substitutions, improvements, etc., made within the spirit and principles of the present invention should be included within the protection scope of the present invention.

Claims

1. A system for memory leak analysis and testing based on GCC wrap and Python, characterized in that, The system includes: A chip that supports GCC compilation and ra registers, ensuring that the ra registers correctly reflect the call point of a function or the function that calls it; the chip includes a wrap library module that supports GCC wrap functionality. The wrap library module is a program that runs on the chip after linking the application module, specifically used to replace the memory allocation and deallocation functions in the application with the corresponding memory allocation and deallocation functions in the wrap library during linking. The application module, which contains memory allocation and deallocation functions, is the actual running application. It only needs to link the wrap library module during compilation. When the program runs, the wrap library functions will actually replace the functions with the same names in the application. The wrap library functions print the address of memory allocation or deallocation and the calling function, so the Python program module can perform analysis and processing of memory allocation and deallocation matching. The Python program module, by taking the output of the wrap library program module as input, analyzes whether memory allocation and deallocation match, providing code location for locating memory leaks; The serial port is used for interaction between the chip and the operating system, and is responsible for printing information to the console or receiving information sent by the console. The wrap library module is also used to print the addresses of memory allocation and deallocation and the value of the ra register during runtime. The printed information is used to input the Python program module to determine whether the memory allocation and deallocation match. The ra register is used to obtain the function above the memory allocation and deallocation function, and is used to obtain the specific location of the allocation and deallocation function during disassembly.

2. The system for memory leak analysis and testing based on GCC wrap and Python according to claim 1, characterized in that, The wrap library module replaces the corresponding memory allocation and deallocation functions in the application module during compilation, and further includes: During compilation, the application links to the wrap library and replaces the application's corresponding memory allocation and deallocation functions; When the program runs, it executes the functions in the wrap library, which print the addresses, sizes, and ra registers of memory allocation and deallocation. The wrap library module performs the following functions: it writes memory allocation and deallocation functions such as malloc, vmalloc, cmalloc, realloc, and free. Inside the functions, it obtains the current value of the ra register and prints the address, size, and ra register value of the allocated or deallocated memory. The ra register records the function that calls the function.

3. The system for memory leak analysis and testing based on GCC wrap and Python according to claim 1, characterized in that, The Python program module further includes: The wrap library unit is used to replace memory allocation and deallocation functions in applications; A storage unit used to record the address, size, and ra address of memory allocation and deallocation; The print unit is used to print the address, size, and ra address of memory allocation and deallocation; The Python program module takes the output information of the application linked with the wrap library as input, analyzes it line by line to find the location of suspected memory leaks, and then uses GCC tools addr2line -e . / test-f0x74323456 or objdump -Dz . / test>test.s to get the exact location of the corresponding memory allocation, in order to determine whether there is a memory leak.

4. The system for memory leak analysis and testing based on GCC wrap and Python according to claim 1, characterized in that, The operating system is either Windows or Linux.

5. The system for memory leak analysis and testing based on GCC wrap and Python according to claim 4, characterized in that, The Python program module, serving as an analysis module for information analysis, can run independently on the operating system architecture.

6. The system for memory leak analysis and testing based on GCC wrap and Python according to claim 1, characterized in that, The chips include those from the Ingenic T / A / X / AD series.

7. A method for memory leak analysis and testing based on GCC wrap and Python, characterized in that, The method is applicable to any of the systems described in claims 1-6 above, and includes the following steps: S1. The application software links to the wrap library, which is written in C. S2. Run the application to generate wrap information for memory allocation and deallocation; S3. Run the Python program and analyze the generated memory allocation and deallocation wrap information; S4. Determine if there is a mismatch between memory allocation and deallocation; if they match, continue the analysis; if they do not match, proceed to step S5. S5. Obtain the ra address for memory allocation and deallocation that does not match the wrap information through analysis of the Python program; S6. Use the GCC tool to disassemble and obtain the function corresponding to the ra address and the specific location of the allocation function. That is, the disassembler application obtains the parent function and the specific location of the corresponding memory allocation function based on the ra address. S7. By obtaining the location of the ra code, the Python analysis software program confirms whether there is a memory leak here, and returns to step S4 to perform the next analysis until all the mismatch information analyzed by the Python code is completed; S8. Complete the memory leak analysis. After analyzing all mismatched memory allocation and deallocation addresses, the process ends.

8. The method for memory leak analysis and testing based on GCC wrap and Python according to claim 7, characterized in that, Step S4 further includes: Based on the wrap function in GCC, the functions malloc, cmalloc, vmalloc, realloc, and free are replaced. By analyzing whether the allocated address and the freed address match, if they match, it means that the memory usage is normal. If they do not match, there is a possibility of memory leak.