1e41f4b71Sopenharmony_ci# Memory Corruption Check 2e41f4b71Sopenharmony_ci 3e41f4b71Sopenharmony_ci 4e41f4b71Sopenharmony_ci## Basic Concepts 5e41f4b71Sopenharmony_ci 6e41f4b71Sopenharmony_ciAs an optional function of the kernel, memory corruption check is used to check the integrity of a dynamic memory pool. This mechanism can detect memory corruption errors in the memory pool in a timely manner and provide alerts. It helps reduce problem locating costs and increase troubleshooting efficiency. 7e41f4b71Sopenharmony_ci 8e41f4b71Sopenharmony_ci 9e41f4b71Sopenharmony_ci## Function Configuration 10e41f4b71Sopenharmony_ci 11e41f4b71Sopenharmony_ci**LOSCFG_BASE_MEM_NODE_INTEGRITY_CHECK** specifies the setting of the memory corruption check. This function is disabled by default. You can enable it in **Debug -> Enable integrity check or not**. 12e41f4b71Sopenharmony_ci 13e41f4b71Sopenharmony_ciIf this macro is enabled, the memory pool integrity will be checked in real time upon each memory allocation. 14e41f4b71Sopenharmony_ci 15e41f4b71Sopenharmony_ciIf this macro is not enabled, you can call **LOS_MemIntegrityCheck** to check the memory pool integrity when required. Using **LOS_MemIntegrityCheck** does not affect the system performance. However, the check accuracy decreases because the node header does not contain the magic number (which is available only when **LOSCFG_BASE_MEM_NODE_INTEGRITY_CHECK** is enabled). 16e41f4b71Sopenharmony_ci 17e41f4b71Sopenharmony_ciThis check only detects the corrupted memory node and provides information about the previous node (because memory is contiguous, a node is most likely corrupted by the previous node). To further determine the location where the previous node is requested, you need to enable the memory leak check and use LRs to locate the fault. 18e41f4b71Sopenharmony_ci 19e41f4b71Sopenharmony_ci> **CAUTION**<br/> 20e41f4b71Sopenharmony_ci> If memory corruption check is enabled, a magic number is added to the node header, which increases the size of the node header. The real-time integrity check has a great impact on the performance. In performance-sensitive scenarios, you are advised to disable this function and use **LOS_MemIntegrityCheck** to check the memory pool integrity. 21e41f4b71Sopenharmony_ci 22e41f4b71Sopenharmony_ci 23e41f4b71Sopenharmony_ci## Development Guidelines 24e41f4b71Sopenharmony_ci 25e41f4b71Sopenharmony_ci 26e41f4b71Sopenharmony_ci### How to Develop 27e41f4b71Sopenharmony_ci 28e41f4b71Sopenharmony_ciUse **LOS_MemIntegrityCheck** to check for memory corruption. If no memory corruption occurs, **0** is returned and no log is output. If memory corruption occurs, the related log is output. For details, see the output of the following example. 29e41f4b71Sopenharmony_ci 30e41f4b71Sopenharmony_ci 31e41f4b71Sopenharmony_ci### Development Example 32e41f4b71Sopenharmony_ci 33e41f4b71Sopenharmony_ciThis example implements the following: 34e41f4b71Sopenharmony_ci 35e41f4b71Sopenharmony_ci1. Request two physically adjacent memory blocks. 36e41f4b71Sopenharmony_ci 37e41f4b71Sopenharmony_ci2. Use **memset** to construct an out-of-bounds access and overwrites the first four bytes of the next node. 38e41f4b71Sopenharmony_ci 39e41f4b71Sopenharmony_ci3. Call **LOS_MemIntegrityCheck** to check for memory corruption. 40e41f4b71Sopenharmony_ci 41e41f4b71Sopenharmony_ci 42e41f4b71Sopenharmony_ci**Sample Code** 43e41f4b71Sopenharmony_ci 44e41f4b71Sopenharmony_ciYou can add the test function of the sample code to **TestTaskEntry** in **kernel/liteos_a/testsuites/kernel/src/osTest.c** for testing. 45e41f4b71Sopenharmony_ciThe sample code is as follows: 46e41f4b71Sopenharmony_ci 47e41f4b71Sopenharmony_ci 48e41f4b71Sopenharmony_ci 49e41f4b71Sopenharmony_ci```c 50e41f4b71Sopenharmony_ci#include <stdio.h> 51e41f4b71Sopenharmony_ci#include <string.h> 52e41f4b71Sopenharmony_ci#include "los_memory.h" 53e41f4b71Sopenharmony_ci#include "los_config.h" 54e41f4b71Sopenharmony_ci 55e41f4b71Sopenharmony_civoid MemIntegrityTest(void) 56e41f4b71Sopenharmony_ci{ 57e41f4b71Sopenharmony_ci /* Request two physically adjacent memory blocks. */ 58e41f4b71Sopenharmony_ci void *ptr1 = LOS_MemAlloc(LOSCFG_SYS_HEAP_ADDR, 8); 59e41f4b71Sopenharmony_ci void *ptr2 = LOS_MemAlloc(LOSCFG_SYS_HEAP_ADDR, 8); 60e41f4b71Sopenharmony_ci /* Construct an out-of-bounds access to cause memory corruption. The memory block of the first node is 8 bytes. Clearing 12 bytes overwrites the header of the second memory node. */ 61e41f4b71Sopenharmony_ci memset(ptr1, 0, 8 + 4); 62e41f4b71Sopenharmony_ci LOS_MemIntegrityCheck(LOSCFG_SYS_HEAP_ADDR); 63e41f4b71Sopenharmony_ci} 64e41f4b71Sopenharmony_ci``` 65e41f4b71Sopenharmony_ci 66e41f4b71Sopenharmony_ci**Verification** 67e41f4b71Sopenharmony_ci 68e41f4b71Sopenharmony_ci 69e41f4b71Sopenharmony_ciThe log is as follows: 70e41f4b71Sopenharmony_ci 71e41f4b71Sopenharmony_ci 72e41f4b71Sopenharmony_ci 73e41f4b71Sopenharmony_ci``` 74e41f4b71Sopenharmony_ci[ERR][OsMemMagicCheckPrint], 2028, memory check error! 75e41f4b71Sopenharmony_cimemory used but magic num wrong, magic num = 0x00000000 /* Error information, indicating that the first four bytes, that is, the magic number, of the next node are corrupted. */ 76e41f4b71Sopenharmony_ci 77e41f4b71Sopenharmony_ci broken node head: 0x20003af0 0x00000000 0x80000020, prev node head: 0x20002ad4 0xabcddcba 0x80000020 78e41f4b71Sopenharmony_ci/* Key information about the corrupted node and its previous node, including the address of the previous node, magic number of the node, and sizeAndFlag of the node. In this example, the magic number of the corrupted node is cleared. */ 79e41f4b71Sopenharmony_ci 80e41f4b71Sopenharmony_ci broken node head LR info: /* The node LR information can be output only after the memory leak check is enabled. */ 81e41f4b71Sopenharmony_ci LR[0]:0x0800414e 82e41f4b71Sopenharmony_ci LR[1]:0x08000cc2 83e41f4b71Sopenharmony_ci LR[2]:0x00000000 84e41f4b71Sopenharmony_ci 85e41f4b71Sopenharmony_ci pre node head LR info: /* Based on the LR information, you can find where the previous node is requested in the assembly file and then perform further analysis. */ 86e41f4b71Sopenharmony_ci LR[0]:0x08004144 87e41f4b71Sopenharmony_ci LR[1]:0x08000cc2 88e41f4b71Sopenharmony_ci LR[2]:0x00000000 89e41f4b71Sopenharmony_ci[ERR]Memory integrity check error, cur node: 0x20003b10, pre node: 0x20003af0 /* Addresses of the corrupted node and its previous node */ 90e41f4b71Sopenharmony_ci``` 91