Introducing ASanAlloc: PieCloudDB Database's Cutting-Edge Memory Manager for Enhanced Product Quality and Stability
NOVEMBER 06TH, 2023

Memory management is a crucial part of computer science, involving dynamic interactions between the operating system, hardware, and software applications. Effective memory management ensures system stability and security, improves system efficiency, helps maximize the utilization of available memory resources, allocates and deallocates memory sensibly, and prevents various types of memory errors. 


In the early days of computer science, memory management was a manual task performed by programmers. However, with the advent of the highly digitized era, computer systems have become increasingly complex, and the tasks of memory management and detection have exceeded the scope of human capability. To address this challenge and assist developers in effectively identifying and fixing memory errors, a range of advanced memory debugging tools has emerged. These tools can precisely detect various types of memory errors and provide detailed diagnostic information, greatly aiding developers in pinpointing and resolving issues. For instance, Valgrind and AddressSanitizer, two widely recognized memory debugging tools, have gained significant popularity. However, these tools have certain limitations. To maintain strict control over the quality of PieCloudDB software, PieCloudDB Database has developed a brand-new memory manager, ASanAlloc. 

 

Limitations of Existing Tools 


PieCloudDB is a cloud-native distributed database. In an attempt to detect and correct memory errors, the PieCloudDB team tried using the Valgrind tool. However, the use of Valgrind can significantly degrade application performance, sometimes slowing it down by a factor of 10 to 50. Additionally, Valgrind cannot detect issues like "stack use after return," "undefined behavior," "stack buffer overflow," and "global buffer overflow," which can be significant concerns for developers. 

 

AddressSanitizer, as an alternative solution, can effectively identify and address the above-mentioned issues. However, PieCloudDB has its own comprehensive internal memory management mechanism, which is specialized. Due to the uniqueness of its memory management system, AddressSanitizer, despite its powerful capabilities, cannot be directly employed in PieCloudDB. 


Implementation Strategy of ASanAlloc


To enable effective memory checking in PieCloudDB, deep exploration into the memory management mechanism was undertaken. Through analysis, we discovered the need to map every memory operation, including palloc and pfree, to malloc and free, to facilitate effective memory checking with AddressSanitizer. Therefore, we needed to innovate and implement an entirely new memory manager. This was crucial to ensure a one-to-one correspondence between palloc and malloc, enabling more precise and efficient memory management and error detection. 


Development Plan of ASanAlloc


In response to the aforementioned issues and solution approach, PieCloudDB has developed a brand new memory manager: ASanAlloc. The ASanAlloc memory manager possesses the following features: 


  • Compatible with AddressSanitizer Memory Detection Tool


The ASanAlloc memory manager seamlessly integrates with the AddressSanitizer memory detection tool. By utilizing ASanAlloc, the capabilities of AddressSanitizer can be effectively harnessed to detect and rectify memory errors, thereby enhancing the stability and security of PieCloudDB. 


  • Memory Usage Statistics


The ASanAlloc memory manager offers memory usage statistics, enabling real-time monitoring and tracking of memory consumption. This feature provides robust data support for memory management decisions, aiding in the further optimization of system performance and resource efficiency. 


  • Memory Context Mapping


The ASanAlloc memory manager also offers memory context relationship mapping. This means the manager can clearly trace and record the allocation and deallocation processes of memory, assisting developers in gaining a better understanding of memory usage patterns and effectively pinpointing and resolving memory-related issues. 


ASanAlloc Memory Manager Components: 


  • Memory Operation Management Module: Responsible for providing memory operation interfaces to the outside. 
  • Global Memory Context Mapping Module: Manages the relationship between memory addresses and memory contexts. 
  • Memory Block Metadata Module: Records the metadata associated with memory addresses. 


The architecture diagram below illustrates the relationships between these modules within the memory context framework used in PieCloudDB with the ASanAlloc memory manager. 



Here is a sequence diagram for the memory context module within PieCloudDB when using the ASanAlloc memory manager. It provides a detailed view of the external interface details and the corresponding internal processes. 



The relevant external interface details include memory allocation interface "palloc" and memory deallocation interface "pfree". Next, we will provide a detailed explanation of the primary logical processing steps for these two interfaces: 


Memory Allocation Interface "palloc"


  • Developers use the palloc interface provided by the MemoryContext class to request memory allocation. 
  • The MemoryContext class invokes the alloc memory operation interface provided by the ASanContext class. 
  • The ASanContext class invokes the malloc system call from the C standard library, responsible for allocating a memory block and returning the corresponding memory block address. 
  • The ASanContext class writes the mapping relationship between the memory block address and the MemoryContext into the GlobalMemoryContextTable. 
  • The ASanContext class writes the mapping relationship between the memory block address and memory block information into the MemoryBlockMetaTable. 
  • The ASanContext class returns the memory block address to the caller. 


Memory Deallocation Interface "pfree"


  • Developers use the pfree interface provided by the MemoryContext class to request the release of memory. 
  • The MemoryContext class invokes the free_p memory operation interface provided by the ASanContext class. 
  • The ASanContext class invokes the free system call from the C standard library, which releases the corresponding memory block based on the address. 
  • The ASanContext class removes the mapping relationship regarding the memory block address from the GlobalMemoryContextTable. 
  • The ASanContext class removes the mapping relationship regarding the memory block address from the MemoryBlockMetaTable. 
  • The ASanContext class's free_p interface call is completed and returns. 


Application Scenarios and Examples


With the ASanAlloc memory manager, we have achieved seamless integration with the AddressSanitizer tool in PieCloudDB. This powerful combination effectively assists PieCloudDB developers in identifying and avoiding various memory errors in complex database environments. 


One of the most prominent issues is out-of-bounds memory access, a common and severe memory error that, if left unhandled, can lead to data corruption or system crashes. ASanAlloc helps us address these issues effectively. 


The following diagram provides a detailed illustration of out-of-bounds memory access. With the assistance of the ASanAlloc memory manager, we can not only promptly detect these issues but also, based on the detailed information it provides, swiftly pinpoint the source of the problem and make necessary corrections. This significantly enhances our efficiency in resolving memory error problems, ensuring the stability and high performance of PieCloudDB. 



To ensure the quality of daily development iterations, we have also integrated the memory checking capabilities based on ASanAlloc into the PieCloudDB daily CI (Continuous Integration) pipeline. Through automated tools and processes, we can conduct real-time memory checks upon code submissions, promptly identify and rectify potential memory issues. This early detection and resolution mechanism are crucial in preventing the impact of memory error problems. 




ASanAlloc: Ensuring Quality and Stability 


Memory checking is crucial for building high-quality, high-performance, and secure software. PieCloudDB has developed a new memory manager called ASanAlloc, which effectively helps identify various memory issues. Additionally, the integration of memory checks in the CI pipeline ensures the quality of daily development iterations and prevents memory errors from affecting system stability. This strategy of integrating memory checks into the development process not only enhances our development efficiency but also allows us to maintain strict control over the quality of PieCloudDB software at all times. 

 


Related Blogs:
no related blog