diff --git a/Kconfig b/Kconfig index 0f80f4a5..98747fa4 100644 --- a/Kconfig +++ b/Kconfig @@ -355,6 +355,9 @@ config PLATFORM_EXC ######################### config options of trace ######################### source "components/trace/Kconfig" +######################### config options of lms ######################### +source "components/lms/Kconfig" + endmenu ######################### config options of lib ######################## diff --git a/components/BUILD.gn b/components/BUILD.gn index e666373e..6d92c1ac 100644 --- a/components/BUILD.gn +++ b/components/BUILD.gn @@ -37,6 +37,7 @@ group("components") { "dynlink", "exchook", "fs", + "lms", "net", "power", "shell", @@ -56,5 +57,6 @@ config("public") { "power:public", "shell:public", "trace:public", + "lms:public", ] } diff --git a/components/lms/BUILD.gn b/components/lms/BUILD.gn new file mode 100644 index 00000000..d25d8987 --- /dev/null +++ b/components/lms/BUILD.gn @@ -0,0 +1,43 @@ +# Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved. +# Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved. +# +# Redistribution and use in source and binary forms, with or without modification, +# are permitted provided that the following conditions are met: +# +# 1. Redistributions of source code must retain the above copyright notice, this list of +# conditions and the following disclaimer. +# +# 2. Redistributions in binary form must reproduce the above copyright notice, this list +# of conditions and the following disclaimer in the documentation and/or other materials +# provided with the distribution. +# +# 3. Neither the name of the copyright holder nor the names of its contributors may be used +# to endorse or promote products derived from this software without specific prior written +# permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, +# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR +# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, +# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, +# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; +# OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, +# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR +# OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF +# ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +import("//kernel/liteos_m/liteos.gni") + +module_switch = defined(LOSCFG_KERNEL_LMS) +module_name = get_path_info(rebase_path("."), "name") +kernel_module(module_name) { + sources = [ + "lms_libc.c", + "los_lms.c", + ] +} + +config("public") { + include_dirs = [ "." ] +} diff --git a/components/lms/Kconfig b/components/lms/Kconfig new file mode 100644 index 00000000..68f739c6 --- /dev/null +++ b/components/lms/Kconfig @@ -0,0 +1,41 @@ +config KERNEL_LMS + bool "Enable Lite Memory Sanitizer" + default n + depends on KERNEL_EXTKERNEL && DEBUG_VERSION && KERNEL_BACKTRACE + help + Select y to build LiteOS with memory sanitizer. + +config LMS_MAX_RECORD_POOL_NUM + int "Lms check pool max num" + default 50 + depends on KERNEL_LMS + help + The Max num of lms check pool + +config LMS_LOAD_CHECK + bool "Enable lms read check" + default y + depends on KERNEL_LMS + help + Select y to enable read check. + +config LMS_STORE_CHECK + bool "Enable lms write check" + default y + depends on KERNEL_LMS + help + Select y to enable write check. + +config LMS_CHECK_STRICT + bool "Enable lms strict check, byte-by-byte" + default n + depends on KERNEL_LMS + help + Select y to enable byte-by-byte check in lms + +config LMS_LIBC_FULL_CHECK + bool "Enable libc all function do lms check" + default n + depends on KERNEL_LMS + help + Select y to enable libc full check diff --git a/components/lms/lms_libc.c b/components/lms/lms_libc.c new file mode 100755 index 00000000..101cba13 --- /dev/null +++ b/components/lms/lms_libc.c @@ -0,0 +1,122 @@ +/* + * Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved. + * Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this list of + * conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this list + * of conditions and the following disclaimer in the documentation and/or other materials + * provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its contributors may be used + * to endorse or promote products derived from this software without specific prior written + * permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "string.h" +#include "los_lms_pri.h" + +#undef memset +void *memset(void *addr, int c, size_t len) +{ + __asan_storeN_noabort((UINTPTR)addr, len); + return __memset(addr, c, len); +} + +#undef memmove +void *memmove(void *dest, const void *src, size_t len) +{ + __asan_loadN_noabort((UINTPTR)src, len); + __asan_storeN_noabort((UINTPTR)dest, len); + return __memmove(dest, src, len); +} + +#undef memcpy +void *memcpy(void *dest, const void *src, size_t len) +{ + __asan_loadN_noabort((UINTPTR)src, len); + __asan_storeN_noabort((UINTPTR)dest, len); + return __memcpy(dest, src, len); +} + +#undef strcat +char *strcat (char *s, const char *append) +{ + if ((s == NULL) || (append == NULL)) { + return NULL; + } + + CHAR *end = s; + size_t len = strlen(append); + for (; *end != '\0'; ++end) { + } + + __asan_storeN_noabort((UINTPTR)end, len + 1); + __asan_loadN_noabort((UINTPTR)append, len + 1); + + return __strcat(s, append); +} + +#undef strcpy +char *strcpy(char *dest, const char *src) +{ + if ((dest == NULL) || (src == NULL)) { + return NULL; + } + + size_t len = strlen(src); + __asan_storeN_noabort((UINTPTR)dest, len + 1); + __asan_loadN_noabort((UINTPTR)src, len + 1); + + return __strcpy(dest, src); +} + +#undef strncat +char *strncat(char *dest, const char *src, size_t n) +{ + if ((dest == NULL) || (src == NULL)) { + return NULL; + } + + CHAR *end = dest; + size_t len = strlen(src); + size_t size = len > n ? n : len; + for (; *end != '\0'; ++end) { + } + + __asan_storeN_noabort((UINTPTR)end, size + 1); + __asan_loadN_noabort((UINTPTR)src, size + 1); + + return __strncat(dest, src, n); +} + +#undef strncpy +char *strncpy(char *dest, const char *src, size_t n) +{ + if ((dest == NULL) || (src == NULL)) { + return NULL; + } + + size_t len = strlen(src); + size_t size = len > n ? n : len; + + __asan_storeN_noabort((UINTPTR)dest, n); + __asan_loadN_noabort((UINTPTR)src, size + 1); + return __strncpy(dest, src, n); +} diff --git a/components/lms/los_lms.c b/components/lms/los_lms.c new file mode 100755 index 00000000..be43c415 --- /dev/null +++ b/components/lms/los_lms.c @@ -0,0 +1,780 @@ +/* + * Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved. + * Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this list of + * conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this list + * of conditions and the following disclaimer in the documentation and/or other materials + * provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its contributors may be used + * to endorse or promote products derived from this software without specific prior written + * permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "los_lms_pri.h" +#include "los_config.h" +#include "los_debug.h" +#if (LOSCFG_KERNEL_SMP == 1) +#include "los_spinlock.h" +#else +#include "los_interrupt.h" +#endif + +#if (LOSCFG_BACKTRACE_TYPE != 0) +#include "los_backtrace.h" +#endif +#include "los_sched.h" + +LITE_OS_SEC_BSS STATIC LmsMemListNode g_lmsCheckPoolArray[LOSCFG_LMS_MAX_RECORD_POOL_NUM]; +LITE_OS_SEC_BSS STATIC LOS_DL_LIST g_lmsCheckPoolList; +STATIC UINT32 g_checkDepth = 0; +LmsHook *g_lms = NULL; + +#if (LOSCFG_KERNEL_SMP == 1) +LITE_OS_SEC_BSS SPIN_LOCK_INIT(g_lmsSpin); +#define LMS_LOCK(state) LOS_SpinLockSave(&g_lmsSpin, &(state)) +#define LMS_UNLOCK(state) LOS_SpinUnlockRestore(&g_lmsSpin, (state)) +#else +#define LMS_LOCK(state) (state) = LOS_IntLock() +#define LMS_UNLOCK(state) LOS_IntRestore(state) +#endif + +#define OS_MEM_ALIGN_BACK(value, align) (((UINT32)(value)) & ~((UINT32)((align) - 1))) +#define IS_ALIGNED(value, align) ((((UINTPTR)(value)) & ((UINTPTR)((align) - 1))) == 0) +#define OS_MEM_ALIGN_SIZE sizeof(UINTPTR) +#define POOL_ADDR_ALIGNSIZE 64 +#define LMS_POOL_UNUSED 0 +#define LMS_POOL_USED 1 +#define INVALID_SHADOW_VALUE 0xFFFFFFFF + +STATIC UINT32 OsLmsPoolResize(UINT32 size) +{ + return OS_MEM_ALIGN_BACK(LMS_POOL_RESIZE(size), POOL_ADDR_ALIGNSIZE); +} + +STATIC LmsMemListNode *OsLmsGetPoolNode(const VOID *pool) +{ + UINTPTR poolAddr = (UINTPTR)pool; + LmsMemListNode *current = NULL; + LOS_DL_LIST *listHead = &g_lmsCheckPoolList; + + if (LOS_ListEmpty(&g_lmsCheckPoolList)) { + goto EXIT; + } + + LOS_DL_LIST_FOR_EACH_ENTRY(current, listHead, LmsMemListNode, node) { + if (current->poolAddr == poolAddr) { + return current; + } + } + +EXIT: + return NULL; +} + +STATIC LmsMemListNode *OsLmsGetPoolNodeFromAddr(UINTPTR addr) +{ + LmsMemListNode *current = NULL; + LOS_DL_LIST *listHead = &g_lmsCheckPoolList; + + if (LOS_ListEmpty(&g_lmsCheckPoolList)) { + goto EXIT; + } + + LOS_DL_LIST_FOR_EACH_ENTRY(current, listHead, LmsMemListNode, node) { + if ((addr >= current->poolAddr) && (addr < current->poolAddr + current->poolSize)) { + return current; + } + } + +EXIT: + return NULL; +} + +STATIC LmsMemListNode *OsLmsCheckPoolCreate(VOID) +{ + UINT32 i; + LmsMemListNode *current = NULL; + for (i = 0; i < LOSCFG_LMS_MAX_RECORD_POOL_NUM; i++) { + current = &g_lmsCheckPoolArray[i]; + if (current->used == LMS_POOL_UNUSED) { + current->used = LMS_POOL_USED; + return current; + } + } + return NULL; +} + +UINT32 LOS_LmsCheckPoolAdd(const VOID *pool, UINT32 size) +{ + UINT32 intSave; + UINTPTR poolAddr = (UINTPTR)pool; + UINT32 realSize; + LmsMemListNode *lmsPoolNode = NULL; + + if (pool == NULL) { + return 0; + } + + LMS_LOCK(intSave); + + lmsPoolNode = OsLmsGetPoolNodeFromAddr((UINTPTR)pool); + if (lmsPoolNode != NULL) { /* if pool range already on checklist */ + if (lmsPoolNode->poolAddr != (UINTPTR)pool) { /* pool is a subset of lmsPoolNode->poolAddr */ + /* do not add it again, just return */ + PRINT_DEBUG("[LMS]pool %p already on lms checklist !\n", pool); + LMS_UNLOCK(intSave); + return size; /* return size indicate the shadow memory init successful */ + } else { /* Re-initialize the same pool, maybe with different size */ + /* delete the old node, then add a new one */ + lmsPoolNode->used = LMS_POOL_UNUSED; + LOS_ListDelete(&(lmsPoolNode->node)); + } + } + + lmsPoolNode = OsLmsCheckPoolCreate(); + if (lmsPoolNode == NULL) { + PRINT_DEBUG("[LMS]the num of lms check pool is max already !\n"); + LMS_UNLOCK(intSave); + return 0; + } + realSize = OsLmsPoolResize(size); + + lmsPoolNode->poolAddr = poolAddr; + lmsPoolNode->poolSize = realSize; + lmsPoolNode->shadowStart = (UINTPTR)poolAddr + realSize; + lmsPoolNode->shadowSize = poolAddr + size - lmsPoolNode->shadowStart; + /* init shadow value */ + (VOID)memset((VOID *)lmsPoolNode->shadowStart, LMS_SHADOW_AFTERFREE_U8, lmsPoolNode->shadowSize); + + LOS_ListAdd(&g_lmsCheckPoolList, &(lmsPoolNode->node)); + + LMS_UNLOCK(intSave); + return realSize; +} + +VOID LOS_LmsCheckPoolDel(const VOID *pool) +{ + UINT32 intSave; + if (pool == NULL) { + return; + } + + LMS_LOCK(intSave); + LmsMemListNode *delNode = OsLmsGetPoolNode(pool); + if (delNode == NULL) { + PRINT_ERR("[LMS]pool %p is not on lms checklist !\n", pool); + goto Release; + } + delNode->used = LMS_POOL_UNUSED; + LOS_ListDelete(&(delNode->node)); +Release: + LMS_UNLOCK(intSave); +} + +VOID OsLmsInit(VOID) +{ + memset(g_lmsCheckPoolArray, 0, sizeof(g_lmsCheckPoolArray)); + LOS_ListInit(&g_lmsCheckPoolList); + static LmsHook hook = { + .init = LOS_LmsCheckPoolAdd, + .mallocMark = OsLmsLosMallocMark, + .freeMark = OsLmsLosFreeMark, + .simpleMark = OsLmsSimpleMark, + .check = OsLmsCheckValid, + }; + g_lms = &hook; +} + +STATIC INLINE UINT32 OsLmsMem2Shadow(LmsMemListNode *node, UINTPTR memAddr, UINTPTR *shadowAddr, UINT32 *shadowOffset) +{ + if ((memAddr < node->poolAddr) || (memAddr >= node->poolAddr + node->poolSize)) { /* check ptr valid */ + PRINT_ERR("[LMS]memAddr %p is not in pool region [%p, %p)\n", memAddr, node->poolAddr, + node->poolAddr + node->poolSize); + return LOS_NOK; + } + + UINT32 memOffset = memAddr - node->poolAddr; + *shadowAddr = node->shadowStart + memOffset / LMS_SHADOW_U8_REFER_BYTES; + *shadowOffset = ((memOffset % LMS_SHADOW_U8_REFER_BYTES) / LMS_SHADOW_U8_CELL_NUM) * + LMS_SHADOW_BITS_PER_CELL; /* (memOffset % 16) / 4 */ + return LOS_OK; +} + +STATIC INLINE VOID OsLmsGetShadowInfo(LmsMemListNode *node, UINTPTR memAddr, LmsAddrInfo *info) +{ + UINTPTR shadowAddr; + UINT32 shadowOffset; + UINT32 shadowValue; + + if (OsLmsMem2Shadow(node, memAddr, &shadowAddr, &shadowOffset) != LOS_OK) { + return; + } + + shadowValue = ((*(UINT8 *)shadowAddr) >> shadowOffset) & LMS_SHADOW_MASK; + info->memAddr = memAddr; + info->shadowAddr = shadowAddr; + info->shadowOffset = shadowOffset; + info->shadowValue = shadowValue; +} + +VOID OsLmsSetShadowValue(LmsMemListNode *node, UINTPTR startAddr, UINTPTR endAddr, UINT8 value) +{ + UINTPTR shadowStart; + UINTPTR shadowEnd; + UINT32 startOffset; + UINT32 endOffset; + + UINT8 shadowValueMask; + UINT8 shadowValue; + + /* endAddr -1, then we mark [startAddr, endAddr) to value */ + if (OsLmsMem2Shadow(node, startAddr, &shadowStart, &startOffset) || + OsLmsMem2Shadow(node, endAddr - 1, &shadowEnd, &endOffset)) { + return; + } + + if (shadowStart == shadowEnd) { /* in the same u8 */ + /* because endAddr - 1, the endOffset falls into the previous cell, + so endOffset + 2 is required for calculation */ + shadowValueMask = LMS_SHADOW_MASK_U8; + shadowValueMask = + (shadowValueMask << startOffset) & (~(shadowValueMask << (endOffset + LMS_SHADOW_BITS_PER_CELL))); + shadowValue = value & shadowValueMask; + *(UINT8 *)shadowStart &= ~shadowValueMask; + *(UINT8 *)shadowStart |= shadowValue; + } else { + /* Adjust startAddr to left util it reach the beginning of a u8 */ + if (startOffset > 0) { + shadowValueMask = LMS_SHADOW_MASK_U8; + shadowValueMask = shadowValueMask << startOffset; + shadowValue = value & shadowValueMask; + *(UINT8 *)shadowStart &= ~shadowValueMask; + *(UINT8 *)shadowStart |= shadowValue; + shadowStart += 1; + } + + /* Adjust endAddr to right util it reach the end of a u8 */ + if (endOffset < (LMS_SHADOW_U8_CELL_NUM - 1) * LMS_SHADOW_BITS_PER_CELL) { + shadowValueMask = LMS_SHADOW_MASK_U8; + shadowValueMask &= ~(shadowValueMask << (endOffset + LMS_SHADOW_BITS_PER_CELL)); + shadowValue = value & shadowValueMask; + *(UINT8 *)shadowEnd &= ~shadowValueMask; + *(UINT8 *)shadowEnd |= shadowValue; + shadowEnd -= 1; + } + + if (shadowEnd + 1 > shadowStart) { + memset((VOID *)shadowStart, value & LMS_SHADOW_MASK_U8, shadowEnd + 1 - shadowStart); + } + } +} + +VOID OsLmsGetShadowValue(LmsMemListNode *node, UINTPTR addr, UINT32 *shadowValue) +{ + UINTPTR shadowAddr; + UINT32 shadowOffset; + if (OsLmsMem2Shadow(node, addr, &shadowAddr, &shadowOffset) != LOS_OK) { + return; + } + + *shadowValue = ((*(UINT8 *)shadowAddr) >> shadowOffset) & LMS_SHADOW_MASK; +} + +VOID OsLmsSimpleMark(UINTPTR startAddr, UINTPTR endAddr, UINT32 value) +{ + UINT32 intSave; + if (endAddr <= startAddr) { + PRINT_DEBUG("[LMS]mark 0x%x, 0x%x, 0x%x\n", startAddr, endAddr, (UINTPTR)__builtin_return_address(0)); + return; + } + + if (!IS_ALIGNED(startAddr, OS_MEM_ALIGN_SIZE) || !IS_ALIGNED(endAddr, OS_MEM_ALIGN_SIZE)) { + PRINT_ERR("[LMS]mark addr is not aligned! 0x%x, 0x%x\n", startAddr, endAddr); + return; + } + + LMS_LOCK(intSave); + + LmsMemListNode *node = OsLmsGetPoolNodeFromAddr(startAddr); + if (node == NULL) { + LMS_UNLOCK(intSave); + return; + } + + OsLmsSetShadowValue(node, startAddr, endAddr, value); + LMS_UNLOCK(intSave); +} + +VOID OsLmsLosMallocMark(const VOID *curNodeStart, const VOID *nextNodeStart, UINT32 nodeHeadSize) +{ + UINT32 intSave; + UINTPTR curNodeStartAddr = (UINTPTR)curNodeStart; + UINTPTR nextNodeStartAddr = (UINTPTR)nextNodeStart; + + LMS_LOCK(intSave); + LmsMemListNode *node = OsLmsGetPoolNodeFromAddr((UINTPTR)curNodeStart); + if (node == NULL) { + LMS_UNLOCK(intSave); + return; + } + + OsLmsSetShadowValue(node, curNodeStartAddr, curNodeStartAddr + nodeHeadSize, LMS_SHADOW_REDZONE_U8); + OsLmsSetShadowValue(node, curNodeStartAddr + nodeHeadSize, nextNodeStartAddr, LMS_SHADOW_ACCESSABLE_U8); + OsLmsSetShadowValue(node, nextNodeStartAddr, nextNodeStartAddr + nodeHeadSize, LMS_SHADOW_REDZONE_U8); + LMS_UNLOCK(intSave); +} + +VOID OsLmsCheckValid(UINTPTR checkAddr, BOOL isFreeCheck) +{ + UINT32 intSave; + UINT32 shadowValue = INVALID_SHADOW_VALUE; + LMS_LOCK(intSave); + LmsMemListNode *node = OsLmsGetPoolNodeFromAddr(checkAddr); + if (node == NULL) { + LMS_UNLOCK(intSave); + return; + } + + OsLmsGetShadowValue(node, checkAddr, &shadowValue); + LMS_UNLOCK(intSave); + if ((shadowValue == LMS_SHADOW_ACCESSABLE) || ((isFreeCheck) && (shadowValue == LMS_SHADOW_PAINT))) { + return; + } + + OsLmsReportError(checkAddr, MEM_REGION_SIZE_1, isFreeCheck ? FREE_ERRORMODE : COMMON_ERRMODE); +} + +VOID OsLmsLosFreeMark(const VOID *curNodeStart, const VOID *nextNodeStart, UINT32 nodeHeadSize) +{ + UINT32 intSave; + UINT32 shadowValue = INVALID_SHADOW_VALUE; + + LMS_LOCK(intSave); + LmsMemListNode *node = OsLmsGetPoolNodeFromAddr((UINTPTR)curNodeStart); + if (node == NULL) { + LMS_UNLOCK(intSave); + return; + } + + UINTPTR curNodeStartAddr = (UINTPTR)curNodeStart; + UINTPTR nextNodeStartAddr = (UINTPTR)nextNodeStart; + + OsLmsGetShadowValue(node, curNodeStartAddr + nodeHeadSize, &shadowValue); + if ((shadowValue != LMS_SHADOW_ACCESSABLE) && (shadowValue != LMS_SHADOW_PAINT)) { + LMS_UNLOCK(intSave); + OsLmsReportError(curNodeStartAddr + nodeHeadSize, MEM_REGION_SIZE_1, FREE_ERRORMODE); + return; + } + + if (*((UINT8 *)curNodeStart) == 0) { /* if merge the node has memset with 0 */ + OsLmsSetShadowValue(node, curNodeStartAddr, curNodeStartAddr + nodeHeadSize, LMS_SHADOW_AFTERFREE_U8); + } + OsLmsSetShadowValue(node, curNodeStartAddr + nodeHeadSize, nextNodeStartAddr, LMS_SHADOW_AFTERFREE_U8); + + if (*((UINT8 *)nextNodeStart) == 0) { /* if merge the node has memset with 0 */ + OsLmsSetShadowValue(node, nextNodeStartAddr, nextNodeStartAddr + nodeHeadSize, LMS_SHADOW_AFTERFREE_U8); + } + + LMS_UNLOCK(intSave); +} + +VOID LOS_LmsAddrProtect(UINTPTR addrStart, UINTPTR addrEnd) +{ + UINT32 intSave; + if (addrEnd <= addrStart) { + return; + } + LMS_LOCK(intSave); + LmsMemListNode *node = OsLmsGetPoolNodeFromAddr(addrStart); + if (node != NULL) { + OsLmsSetShadowValue(node, addrStart, addrEnd, LMS_SHADOW_REDZONE_U8); + } + LMS_UNLOCK(intSave); +} + +VOID LOS_LmsAddrDisableProtect(UINTPTR addrStart, UINTPTR addrEnd) +{ + UINT32 intSave; + if (addrEnd <= addrStart) { + return; + } + LMS_LOCK(intSave); + LmsMemListNode *node = OsLmsGetPoolNodeFromAddr(addrStart); + if (node != NULL) { + OsLmsSetShadowValue(node, addrStart, addrEnd, LMS_SHADOW_ACCESSABLE_U8); + } + LMS_UNLOCK(intSave); +} + +STATIC UINT32 OsLmsCheckAddr(UINTPTR addr) +{ + UINT32 intSave; + UINT32 shadowValue = INVALID_SHADOW_VALUE; + /* do not check nested or before all cpu start */ + LMS_LOCK(intSave); + if ((g_checkDepth != 0) || (!g_taskScheduled)) { + LMS_UNLOCK(intSave); + return 0; + } + + LmsMemListNode *node = OsLmsGetPoolNodeFromAddr(addr); + if (node == NULL) { + LMS_UNLOCK(intSave); + return LMS_SHADOW_ACCESSABLE_U8; + } + + OsLmsGetShadowValue(node, addr, &shadowValue); + LMS_UNLOCK(intSave); + return shadowValue; +} + +#if (LOSCFG_LMS_CHECK_STRICT == 1) +STATIC INLINE UINT32 OsLmsCheckAddrRegion(UINTPTR addr, UINT32 size) +{ + UINT32 i; + for (i = 0; i < size; i++) { + if (OsLmsCheckAddr(addr + i)) { + return LOS_NOK; + } + } + return LOS_OK; +} + +#else +STATIC INLINE UINT32 OsLmsCheckAddrRegion(UINTPTR addr, UINT32 size) +{ + if (OsLmsCheckAddr(addr) || OsLmsCheckAddr(addr + size - 1)) { + return LOS_NOK; + } else { + return LOS_OK; + } +} +#endif + +VOID OsLmsPrintPoolListInfo(VOID) +{ + UINT32 count = 0; + UINT32 intSave; + LmsMemListNode *current = NULL; + LOS_DL_LIST *listHead = &g_lmsCheckPoolList; + + LMS_LOCK(intSave); + LOS_DL_LIST_FOR_EACH_ENTRY(current, listHead, LmsMemListNode, node) + { + count++; + PRINT_DEBUG( + "[LMS]memory pool[%1u]: totalsize 0x%-8x memstart 0x%-8x memstop 0x%-8x memsize 0x%-8x shadowstart 0x%-8x " + "shadowSize 0x%-8x\n", + count, current->poolSize + current->shadowSize, current->poolAddr, current->poolAddr + current->poolSize, + current->poolSize, current->shadowStart, current->shadowSize); + } + + LMS_UNLOCK(intSave); +} + +VOID OsLmsPrintMemInfo(UINTPTR addr) +{ +#define LMS_DUMP_OFFSET 16 +#define LMS_DUMP_RANGE_DOUBLE 2 + + PRINTK("\n[LMS] Dump info around address [0x%8x]:\n", addr); + const UINT32 printY = LMS_DUMP_OFFSET * LMS_DUMP_RANGE_DOUBLE + 1; + const UINT32 printX = LMS_MEM_BYTES_PER_SHADOW_CELL * LMS_DUMP_RANGE_DOUBLE; + UINTPTR dumpAddr = addr - addr % printX - LMS_DUMP_OFFSET * printX; + UINT32 shadowValue = 0; + UINTPTR shadowAddr = 0; + UINT32 shadowOffset = 0; + LmsMemListNode *nodeInfo = NULL; + INT32 isCheckAddr, x, y; + + nodeInfo = OsLmsGetPoolNodeFromAddr(addr); + if (nodeInfo == NULL) { + PRINT_ERR("[LMS]addr is not in checkpool\n"); + return; + } + + for (y = 0; y < printY; y++, dumpAddr += printX) { + if (dumpAddr < nodeInfo->poolAddr) { /* find util dumpAddr in pool region */ + continue; + } + + if ((dumpAddr + printX) >= + nodeInfo->poolAddr + nodeInfo->poolSize) { /* finish if dumpAddr exceeds pool's upper region */ + goto END; + } + + PRINTK("\n\t[0x%x]: ", dumpAddr); + for (x = 0; x < printX; x++) { + if ((dumpAddr + x) == addr) { + PRINTK("[%02x]", *(UINT8 *)(dumpAddr + x)); + } else { + PRINTK(" %02x ", *(UINT8 *)(dumpAddr + x)); + } + } + + if (OsLmsMem2Shadow(nodeInfo, dumpAddr, &shadowAddr, &shadowOffset) != LOS_OK) { + goto END; + } + + PRINTK("|\t[0x%x | %2u]: ", shadowAddr, shadowOffset); + + for (x = 0; x < printX; x += LMS_MEM_BYTES_PER_SHADOW_CELL) { + OsLmsGetShadowValue(nodeInfo, dumpAddr + x, &shadowValue); + isCheckAddr = dumpAddr + x - (UINTPTR)addr + LMS_MEM_BYTES_PER_SHADOW_CELL; + if ((isCheckAddr > 0) && (isCheckAddr <= LMS_MEM_BYTES_PER_SHADOW_CELL)) { + PRINTK("[%1x]", shadowValue); + } else { + PRINTK(" %1x ", shadowValue); + } + } + } +END: + PRINTK("\n"); +} + +STATIC VOID OsLmsGetErrorInfo(UINTPTR addr, UINT32 size, LmsAddrInfo *info) +{ + LmsMemListNode *node = OsLmsGetPoolNodeFromAddr(addr); + OsLmsGetShadowInfo(node, addr, info); + if (info->shadowValue != LMS_SHADOW_ACCESSABLE_U8) { + return; + } else { + OsLmsGetShadowInfo(node, addr + size - 1, info); + } +} + +STATIC VOID OsLmsPrintErrInfo(LmsAddrInfo *info, UINT32 errMod) +{ + switch (info->shadowValue) { + case LMS_SHADOW_AFTERFREE: + PRINT_ERR("Use after free error detected\n"); + break; + case LMS_SHADOW_REDZONE: + PRINT_ERR("Heap buffer overflow error detected\n"); + break; + case LMS_SHADOW_ACCESSABLE: + PRINT_ERR("No error\n"); + break; + default: + PRINT_ERR("UnKnown Error detected\n"); + break; + } + + switch (errMod) { + case FREE_ERRORMODE: + PRINT_ERR("Illegal Double free address at: [0x%lx]\n", info->memAddr); + break; + case LOAD_ERRMODE: + PRINT_ERR("Illegal READ address at: [0x%lx]\n", info->memAddr); + break; + case STORE_ERRMODE: + PRINT_ERR("Illegal WRITE address at: [0x%lx]\n", info->memAddr); + break; + case COMMON_ERRMODE: + PRINT_ERR("Common Error at: [0x%lx]\n", info->memAddr); + break; + default: + PRINT_ERR("UnKnown Error mode at: [0x%lx]\n", info->memAddr); + break; + } + + PRINT_ERR("Shadow memory address: [0x%lx : %1u] Shadow memory value: [%u] \n", info->shadowAddr, + info->shadowOffset, info->shadowValue); +} + +VOID OsLmsReportError(UINTPTR p, UINT32 size, UINT32 errMod) +{ + UINT32 intSave; + LmsAddrInfo info; + + LMS_LOCK(intSave); + g_checkDepth += 1; + memset(&info, 0, sizeof(LmsAddrInfo)); + + PRINT_ERR("***** Kernel Address Sanitizer Error Detected Start *****\n"); + + OsLmsGetErrorInfo(p, size, &info); + + OsLmsPrintErrInfo(&info, errMod); +#if (LOSCFG_BACKTRACE_TYPE != 0) + LOS_BackTrace(); +#endif + OsLmsPrintMemInfo(info.memAddr); + g_checkDepth -= 1; + LMS_UNLOCK(intSave); + PRINT_ERR("***** Kernel Address Sanitizer Error Detected End *****\n"); +} + +#if (LOSCFG_LMS_STORE_CHECK == 1) +VOID __asan_store1_noabort(UINTPTR p) +{ + if (OsLmsCheckAddr(p) != LMS_SHADOW_ACCESSABLE_U8) { + OsLmsReportError(p, MEM_REGION_SIZE_1, STORE_ERRMODE); + } +} + +VOID __asan_store2_noabort(UINTPTR p) +{ + if (OsLmsCheckAddrRegion(p, MEM_REGION_SIZE_2) != LOS_OK) { + OsLmsReportError(p, MEM_REGION_SIZE_2, STORE_ERRMODE); + } +} + +VOID __asan_store4_noabort(UINTPTR p) +{ + if (OsLmsCheckAddrRegion(p, MEM_REGION_SIZE_4) != LOS_OK) { + OsLmsReportError(p, MEM_REGION_SIZE_4, STORE_ERRMODE); + } +} + +VOID __asan_store8_noabort(UINTPTR p) +{ + if (OsLmsCheckAddrRegion(p, MEM_REGION_SIZE_8) != LOS_OK) { + OsLmsReportError(p, MEM_REGION_SIZE_8, STORE_ERRMODE); + } +} + +VOID __asan_store16_noabort(UINTPTR p) +{ + if (OsLmsCheckAddrRegion(p, MEM_REGION_SIZE_16) != LOS_OK) { + OsLmsReportError(p, MEM_REGION_SIZE_16, STORE_ERRMODE); + } +} + +VOID __asan_storeN_noabort(UINTPTR p, UINT32 size) +{ + if (OsLmsCheckAddrRegion(p, size) != LOS_OK) { + OsLmsReportError(p, size, STORE_ERRMODE); + } +} +#else +VOID __asan_store1_noabort(UINTPTR p) +{ + (VOID)p; +} + +VOID __asan_store2_noabort(UINTPTR p) +{ + (VOID)p; +} + +VOID __asan_store4_noabort(UINTPTR p) +{ + (VOID)p; +} + +VOID __asan_store8_noabort(UINTPTR p) +{ + (VOID)p; +} + +VOID __asan_store16_noabort(UINTPTR p) +{ + (VOID)p; +} + +VOID __asan_storeN_noabort(UINTPTR p, UINT32 size) +{ + (VOID)p; + (VOID)size; +} + +#endif + +#if (LOSCFG_LMS_LOAD_CHECK == 1) +VOID __asan_load1_noabort(UINTPTR p) +{ + if (OsLmsCheckAddr(p) != LMS_SHADOW_ACCESSABLE_U8) { + OsLmsReportError(p, MEM_REGION_SIZE_1, LOAD_ERRMODE); + } +} + +VOID __asan_load2_noabort(UINTPTR p) +{ + if (OsLmsCheckAddrRegion(p, MEM_REGION_SIZE_2) != LOS_OK) { + OsLmsReportError(p, MEM_REGION_SIZE_2, LOAD_ERRMODE); + } +} + +VOID __asan_load4_noabort(UINTPTR p) +{ + if (OsLmsCheckAddrRegion(p, MEM_REGION_SIZE_4) != LOS_OK) { + OsLmsReportError(p, MEM_REGION_SIZE_4, LOAD_ERRMODE); + } +} + +VOID __asan_load8_noabort(UINTPTR p) +{ + if (OsLmsCheckAddrRegion(p, MEM_REGION_SIZE_8) != LOS_OK) { + OsLmsReportError(p, MEM_REGION_SIZE_8, LOAD_ERRMODE); + } +} + +VOID __asan_load16_noabort(UINTPTR p) +{ + if (OsLmsCheckAddrRegion(p, MEM_REGION_SIZE_16) != LOS_OK) { + OsLmsReportError(p, MEM_REGION_SIZE_16, LOAD_ERRMODE); + } +} + +VOID __asan_loadN_noabort(UINTPTR p, UINT32 size) +{ + if (OsLmsCheckAddrRegion(p, size) != LOS_OK) { + OsLmsReportError(p, size, LOAD_ERRMODE); + } +} +#else +VOID __asan_load1_noabort(UINTPTR p) +{ + (VOID)p; +} + +VOID __asan_load2_noabort(UINTPTR p) +{ + (VOID)p; +} + +VOID __asan_load4_noabort(UINTPTR p) +{ + (VOID)p; +} + +VOID __asan_load8_noabort(UINTPTR p) +{ + (VOID)p; +} + +VOID __asan_load16_noabort(UINTPTR p) +{ + (VOID)p; +} + +VOID __asan_loadN_noabort(UINTPTR p, UINT32 size) +{ + (VOID)p; + (VOID)size; +} +#endif +VOID __asan_handle_no_return(VOID) +{ + return; +} diff --git a/components/lms/los_lms.h b/components/lms/los_lms.h new file mode 100755 index 00000000..a88a16bf --- /dev/null +++ b/components/lms/los_lms.h @@ -0,0 +1,59 @@ +/* + * Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved. + * Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this list of + * conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this list + * of conditions and the following disclaimer in the documentation and/or other materials + * provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its contributors may be used + * to endorse or promote products derived from this software without specific prior written + * permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + + +#ifndef _LOS_LMS_H +#define _LOS_LMS_H + +#include "los_compiler.h" + +#ifdef __cplusplus +#if __cplusplus +extern "C" { +#endif /* __cplusplus */ +#endif /* __cplusplus */ + +#ifdef LOSCFG_KERNEL_LMS + +UINT32 LOS_LmsCheckPoolAdd(const VOID *pool, UINT32 size); +VOID LOS_LmsCheckPoolDel(const VOID *pool); +VOID LOS_LmsAddrProtect(UINTPTR addrStart, UINTPTR addrEnd); +VOID LOS_LmsAddrDisableProtect(UINTPTR addrStart, UINTPTR addrEnd); + +#endif /* LOSCFG_KERNEL_LMS */ + +#ifdef __cplusplus +#if __cplusplus +} +#endif /* __cplusplus */ +#endif /* __cplusplus */ + +#endif /* _LOS_LMS_H */ diff --git a/components/lms/los_lms_pri.h b/components/lms/los_lms_pri.h new file mode 100755 index 00000000..00a24a76 --- /dev/null +++ b/components/lms/los_lms_pri.h @@ -0,0 +1,136 @@ +/* + * Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved. + * Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this list of + * conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this list + * of conditions and the following disclaimer in the documentation and/or other materials + * provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its contributors may be used + * to endorse or promote products derived from this software without specific prior written + * permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef _LOS_LMS_PRI_H +#define _LOS_LMS_PRI_H + +#include "los_lms.h" +#include "los_compiler.h" +#include "los_list.h" +#include "securec.h" + +#ifdef __cplusplus +#if __cplusplus +extern "C" { +#endif /* __cplusplus */ +#endif /* __cplusplus */ + +#define COMMON_ERRMODE 3 +#define FREE_ERRORMODE 2 +#define STORE_ERRMODE 1 +#define LOAD_ERRMODE 0 + +#define SANITIZER_INTERFACE_ATTRIBUTE +#define ATTRIBUTE_NO_SANITIZE_ADDRESS __attribute__((no_sanitize_address)) + +#define LMS_SHADOW_BITS_PER_CELL 2 +#define LMS_MEM_BYTES_PER_SHADOW_CELL 4 +#define LMS_SHADOW_U8_CELL_NUM 4 +#define LMS_SHADOW_U8_REFER_BYTES 16 + +#define LMS_POOL_RESIZE(size) ((size) / (LMS_SHADOW_U8_REFER_BYTES + 1) * LMS_SHADOW_U8_REFER_BYTES) +#define LMS_ADDR_ALIGN(p) (((UINTPTR)(p) + sizeof(UINTPTR) - 1) & ~((UINTPTR)(sizeof(UINTPTR) - 1))) + +#define LMS_SHADOW_ACCESSABLE 0x00 +#define LMS_SHADOW_AFTERFREE 0x03 +#define LMS_SHADOW_REDZONE 0x02 +#define LMS_SHADOW_PAINT 0x01 +#define LMS_SHADOW_MASK 0x03 + +#define LMS_SHADOW_ACCESSABLE_U8 0x00 +#define LMS_SHADOW_AFTERFREE_U8 0xFF +#define LMS_SHADOW_REDZONE_U8 0xAA +#define LMS_SHADOW_MASK_U8 0xFF +#define LMS_SHADOW_PAINT_U8 0x55 + +#define MEM_REGION_SIZE_1 1 +#define MEM_REGION_SIZE_2 2 +#define MEM_REGION_SIZE_4 4 +#define MEM_REGION_SIZE_8 8 +#define MEM_REGION_SIZE_16 16 + +typedef struct { + LOS_DL_LIST node; + UINT32 used; + UINTPTR poolAddr; + UINT32 poolSize; + UINTPTR shadowStart; + UINT32 shadowSize; +} LmsMemListNode; + +typedef struct { + UINTPTR memAddr; + UINTPTR shadowAddr; + UINT32 shadowOffset; + UINT32 shadowValue; +} LmsAddrInfo; + +typedef struct { + UINT32 (*init)(const VOID *pool, UINT32 size); + VOID (*mallocMark)(const VOID *curNodeStart, const VOID *nextNodeStart, UINT32 nodeHeadSize); + VOID (*freeMark)(const VOID *curNodeStart, const VOID *nextNodeStart, UINT32 nodeHeadSize); + VOID (*simpleMark)(UINTPTR startAddr, UINTPTR endAddr, UINT32 value); + VOID (*check)(UINTPTR checkAddr, BOOL isFreeCheck); +} LmsHook; +extern LmsHook* g_lms; + +VOID OsLmsInit(VOID); +VOID OsLmsCheckValid(UINTPTR checkAddr, BOOL isFreeCheck); +VOID OsLmsLosMallocMark(const VOID *curNodeStart, const VOID *nextNodeStart, UINT32 nodeHeadSize); +VOID OsLmsLosFreeMark(const VOID *curNodeStart, const VOID *nextNodeStart, UINT32 nodeHeadSize); +VOID OsLmsSimpleMark(UINTPTR startAddr, UINTPTR endAddr, UINT32 value); + +VOID OsLmsPrintPoolListInfo(VOID); +VOID OsLmsReportError(UINTPTR p, UINT32 size, UINT32 errMod); + +VOID CheckValid(const CHAR *dest, const CHAR *src); + +extern SANITIZER_INTERFACE_ATTRIBUTE VOID __asan_store1_noabort(UINTPTR p); +extern SANITIZER_INTERFACE_ATTRIBUTE VOID __asan_store4_noabort(UINTPTR p); +extern SANITIZER_INTERFACE_ATTRIBUTE VOID __asan_load4_noabort(UINTPTR p); +extern SANITIZER_INTERFACE_ATTRIBUTE VOID __asan_load1_noabort(UINTPTR p); +extern SANITIZER_INTERFACE_ATTRIBUTE VOID __asan_loadN_noabort(UINTPTR p, UINT32 size); +extern SANITIZER_INTERFACE_ATTRIBUTE VOID __asan_storeN_noabort(UINTPTR p, UINT32 size); +extern SANITIZER_INTERFACE_ATTRIBUTE VOID __asan_store2_noabort(UINTPTR p); +extern SANITIZER_INTERFACE_ATTRIBUTE VOID __asan_load2_noabort(UINTPTR p); +extern SANITIZER_INTERFACE_ATTRIBUTE VOID __asan_store8_noabort(UINTPTR p); +extern SANITIZER_INTERFACE_ATTRIBUTE VOID __asan_load8_noabort(UINTPTR p); +extern SANITIZER_INTERFACE_ATTRIBUTE VOID __asan_load16_noabort(UINTPTR p); +extern SANITIZER_INTERFACE_ATTRIBUTE VOID __asan_store16_noabort(UINTPTR p); +extern SANITIZER_INTERFACE_ATTRIBUTE VOID __asan_handle_no_return(VOID); + +#ifdef __cplusplus +#if __cplusplus +} +#endif /* __cplusplus */ +#endif /* __cplusplus */ + +#endif /* _LOS_LMS_PRI_H */ \ No newline at end of file diff --git a/kal/libsec/BUILD.gn b/kal/libsec/BUILD.gn index 36179b6a..a8127318 100644 --- a/kal/libsec/BUILD.gn +++ b/kal/libsec/BUILD.gn @@ -33,7 +33,21 @@ import("//third_party/bounds_checking_function/libsec_src.gni") module_name = get_path_info(rebase_path("."), "name") kernel_module(module_name) { sources = libsec_sources - + if (defined(LOSCFG_KERNEL_LMS)) { + if ("$ohos_build_compiler" == "gcc") { + cflags_c = [ "-fsanitize=kernel-address" ] + } else { + cflags_c = [ + "-fsanitize=kernel-address", + "-mllvm", + "-asan-instrumentation-with-call-threshold=0", + "-mllvm", + "-asan-stack=0", + "-mllvm", + "-asan-globals=0", + ] + } + } public_configs = [ ":public" ] } diff --git a/kernel/src/los_init.c b/kernel/src/los_init.c index d2b13a3e..2025b25e 100644 --- a/kernel/src/los_init.c +++ b/kernel/src/los_init.c @@ -66,6 +66,10 @@ #include "los_dynlink.h" #endif +#ifdef LOSCFG_KERNEL_LMS +#include "los_lms_pri.h" +#endif + /***************************************************************************** Function : LOS_Reboot Description : system exception, die in here, wait for watchdog. @@ -125,6 +129,10 @@ LITE_OS_SEC_TEXT_INIT UINT32 LOS_KernelInit(VOID) #endif OsRegister(); + +#ifdef LOSCFG_KERNEL_LMS + OsLmsInit(); +#endif ret = OsMemSystemInit(); if (ret != LOS_OK) { PRINT_ERR("OsMemSystemInit error %d\n", ret); diff --git a/kernel/src/mm/los_memory.c b/kernel/src/mm/los_memory.c index 99e15f12..7009f154 100644 --- a/kernel/src/mm/los_memory.c +++ b/kernel/src/mm/los_memory.c @@ -37,7 +37,9 @@ #include "los_hook.h" #include "los_interrupt.h" #include "los_task.h" - +#ifdef LOSCFG_KERNEL_LMS +#include "los_lms_pri.h" +#endif /* Used to cut non-essential functions. */ #define OS_MEM_EXPAND_ENABLE 0 @@ -534,7 +536,7 @@ RETRY: OsMemFreeNodeAdd(pool, (struct OsMemFreeNodeHead *)newNode); endNode = OS_MEM_END_NODE(newNode, size); - (VOID)memset_s(endNode, sizeof(*endNode), 0, sizeof(*endNode)); + (VOID)memset(endNode, 0, sizeof(*endNode)); endNode->ptr.next = NULL; OS_MEM_SET_MAGIC(endNode); OsMemSentinelNodeSet(endNode, NULL, 0); @@ -553,6 +555,71 @@ VOID LOS_MemExpandEnable(VOID *pool) } #endif +#ifdef LOSCFG_KERNEL_LMS +STATIC INLINE VOID OsLmsFirstNodeMark(VOID *pool, struct OsMemNodeHead *node) +{ + if (g_lms == NULL) { + return; + } + + g_lms->simpleMark((UINTPTR)pool, (UINTPTR)node, LMS_SHADOW_PAINT_U8); + g_lms->simpleMark((UINTPTR)node, (UINTPTR)node + OS_MEM_NODE_HEAD_SIZE, LMS_SHADOW_REDZONE_U8); + g_lms->simpleMark((UINTPTR)OS_MEM_NEXT_NODE(node), (UINTPTR)OS_MEM_NEXT_NODE(node) + OS_MEM_NODE_HEAD_SIZE, + LMS_SHADOW_REDZONE_U8); + g_lms->simpleMark((UINTPTR)node + OS_MEM_NODE_HEAD_SIZE, (UINTPTR)OS_MEM_NEXT_NODE(node), + LMS_SHADOW_AFTERFREE_U8); +} + +STATIC INLINE VOID OsLmsAllocAlignMark(VOID *ptr, VOID *alignedPtr, UINT32 size) +{ + struct OsMemNodeHead *allocNode = NULL; + + if ((g_lms == NULL) || (ptr == NULL)) { + return; + } + allocNode = (struct OsMemNodeHead *)((struct OsMemUsedNodeHead *)ptr - 1); + if (ptr != alignedPtr) { + g_lms->simpleMark((UINTPTR)ptr, (UINTPTR)ptr + sizeof(UINT32), LMS_SHADOW_PAINT_U8); + g_lms->simpleMark((UINTPTR)ptr + sizeof(UINT32), (UINTPTR)alignedPtr, LMS_SHADOW_REDZONE_U8); + } + + /* mark remining as redzone */ + g_lms->simpleMark(LMS_ADDR_ALIGN((UINTPTR)alignedPtr + size), (UINTPTR)OS_MEM_NEXT_NODE(allocNode), + LMS_SHADOW_REDZONE_U8); +} + +STATIC INLINE VOID OsLmsReallocMergeNodeMark(struct OsMemNodeHead *node) +{ + if (g_lms == NULL) { + return; + } + + g_lms->simpleMark((UINTPTR)node + OS_MEM_NODE_HEAD_SIZE, (UINTPTR)OS_MEM_NEXT_NODE(node), + LMS_SHADOW_ACCESSABLE_U8); +} + +STATIC INLINE VOID OsLmsReallocSplitNodeMark(struct OsMemNodeHead *node) +{ + if (g_lms == NULL) { + return; + } + /* mark next node */ + g_lms->simpleMark((UINTPTR)OS_MEM_NEXT_NODE(node), + (UINTPTR)OS_MEM_NEXT_NODE(node) + OS_MEM_NODE_HEAD_SIZE, LMS_SHADOW_REDZONE_U8); + g_lms->simpleMark((UINTPTR)OS_MEM_NEXT_NODE(node) + OS_MEM_NODE_HEAD_SIZE, + (UINTPTR)OS_MEM_NEXT_NODE(OS_MEM_NEXT_NODE(node)), LMS_SHADOW_AFTERFREE_U8); +} + +STATIC INLINE VOID OsLmsReallocResizeMark(struct OsMemNodeHead *node, UINT32 resize) +{ + if (g_lms == NULL) { + return; + } + /* mark remaining as redzone */ + g_lms->simpleMark((UINTPTR)node + resize, (UINTPTR)OS_MEM_NEXT_NODE(node), LMS_SHADOW_REDZONE_U8); +} +#endif + #if (LOSCFG_MEM_LEAKCHECK == 1) struct OsMemLeakCheckInfo { struct OsMemNodeHead *node; @@ -568,7 +635,7 @@ STATIC INLINE VOID OsMemLeakCheckInfoRecord(struct OsMemNodeHead *node) if (!OS_MEM_NODE_GET_LEAK_FLAG(node->sizeAndFlag)) { info->node = node; - (VOID)memcpy_s(info->linkReg, sizeof(info->linkReg), node->linkReg, sizeof(node->linkReg)); + (VOID)memcpy(info->linkReg, node->linkReg, sizeof(node->linkReg)); OS_MEM_NODE_SET_LEAK_FLAG(node->sizeAndFlag); g_leakCheckRecordCnt++; if (g_leakCheckRecordCnt >= LOSCFG_MEM_LEAKCHECK_RECORD_MAX_NUM) { @@ -579,14 +646,13 @@ STATIC INLINE VOID OsMemLeakCheckInfoRecord(struct OsMemNodeHead *node) STATIC INLINE VOID OsMemLeakCheckInit(VOID) { - (VOID)memset_s(g_leakCheckRecord, sizeof(struct OsMemLeakCheckInfo) * LOSCFG_MEM_LEAKCHECK_RECORD_MAX_NUM, - 0, sizeof(struct OsMemLeakCheckInfo) * LOSCFG_MEM_LEAKCHECK_RECORD_MAX_NUM); + (VOID)memset(g_leakCheckRecord, 0, sizeof(struct OsMemLeakCheckInfo) * LOSCFG_MEM_LEAKCHECK_RECORD_MAX_NUM); g_leakCheckRecordCnt = 0; } STATIC INLINE VOID OsMemLinkRegisterRecord(struct OsMemNodeHead *node) { - (VOID)memset_s(node->linkReg, sizeof(node->linkReg), 0, sizeof(node->linkReg)); + (VOID)memset(node->linkReg, 0, sizeof(node->linkReg)); OsBackTraceHookCall(node->linkReg, LOSCFG_MEM_RECORD_LR_CNT, LOSCFG_MEM_OMIT_LR_CNT, 0); } @@ -840,6 +906,12 @@ STATIC INLINE VOID *OsMemCreateUsedNode(VOID *addr) OsMemNodeSetTaskID(node); #endif +#ifdef LOSCFG_KERNEL_LMS + struct OsMemNodeHead *newNode = (struct OsMemNodeHead *)node; + if (g_lms != NULL) { + g_lms->mallocMark(newNode, OS_MEM_NEXT_NODE(newNode), OS_MEM_NODE_HEAD_SIZE); + } +#endif return node + 1; } @@ -848,8 +920,18 @@ STATIC UINT32 OsMemPoolInit(VOID *pool, UINT32 size) struct OsMemPoolHead *poolHead = (struct OsMemPoolHead *)pool; struct OsMemNodeHead *newNode = NULL; struct OsMemNodeHead *endNode = NULL; - - (VOID)memset_s(poolHead, sizeof(struct OsMemPoolHead), 0, sizeof(struct OsMemPoolHead)); +#ifdef LOSCFG_KERNEL_LMS + UINT32 resize = 0; + if (g_lms != NULL) { + /* + * resize == 0, shadow memory init failed, no shadow memory for this pool, set poolSize as original size. + * resize != 0, shadow memory init successful, set poolSize as resize. + */ + resize = g_lms->init(pool, size); + size = (resize == 0) ? size : resize; + } +#endif + (VOID)memset(poolHead, 0, sizeof(struct OsMemPoolHead)); poolHead->info.pool = pool; poolHead->info.totalSize = size; @@ -878,13 +960,18 @@ STATIC UINT32 OsMemPoolInit(VOID *pool, UINT32 size) poolHead->info.waterLine = poolHead->info.curUsedSize; #endif +#ifdef LOSCFG_KERNEL_LMS + if (resize != 0) { + OsLmsFirstNodeMark(pool, newNode); + } +#endif return LOS_OK; } #if (LOSCFG_MEM_MUL_POOL == 1) STATIC VOID OsMemPoolDeinit(VOID *pool) { - (VOID)memset_s(pool, sizeof(struct OsMemPoolHead), 0, sizeof(struct OsMemPoolHead)); + (VOID)memset(pool, 0, sizeof(struct OsMemPoolHead)); } STATIC UINT32 OsMemPoolAdd(VOID *pool, UINT32 size) @@ -1118,6 +1205,9 @@ VOID *LOS_MemAllocAlign(VOID *pool, UINT32 size, UINT32 boundary) ptr = OsMemAlloc(pool, useSize, intSave); alignedPtr = (VOID *)OS_MEM_ALIGN(ptr, boundary); if (ptr == alignedPtr) { +#ifdef LOSCFG_KERNEL_LMS + OsLmsAllocAlignMark(ptr, alignedPtr, size); +#endif break; } @@ -1127,6 +1217,9 @@ VOID *LOS_MemAllocAlign(VOID *pool, UINT32 size, UINT32 boundary) OS_MEM_NODE_SET_ALIGNED_FLAG(allocNode->header.sizeAndFlag); OS_MEM_SET_GAPSIZE_ALIGNED_FLAG(gapSize); *(UINT32 *)((UINTPTR)alignedPtr - sizeof(gapSize)) = gapSize; +#ifdef LOSCFG_KERNEL_LMS + OsLmsAllocAlignMark(ptr, alignedPtr, size); +#endif ptr = alignedPtr; } while (0); MEM_UNLOCK(poolHead, intSave); @@ -1251,6 +1344,13 @@ STATIC INLINE UINT32 OsMemFree(struct OsMemPoolHead *pool, struct OsMemNodeHead node->sizeAndFlag = OS_MEM_NODE_GET_SIZE(node->sizeAndFlag); #if (LOSCFG_MEM_LEAKCHECK == 1) OsMemLinkRegisterRecord(node); +#endif +#ifdef LOSCFG_KERNEL_LMS + struct OsMemNodeHead *nextNodeBackup = OS_MEM_NEXT_NODE(node); + struct OsMemNodeHead *curNodeBackup = node; + if (g_lms != NULL) { + g_lms->check((UINTPTR)node + OS_MEM_NODE_HEAD_SIZE, TRUE); + } #endif struct OsMemNodeHead *preNode = node->ptr.prev; /* merage preNode */ if ((preNode != NULL) && !OS_MEM_NODE_GET_USED_FLAG(preNode->sizeAndFlag)) { @@ -1278,7 +1378,11 @@ STATIC INLINE UINT32 OsMemFree(struct OsMemPoolHead *pool, struct OsMemNodeHead #endif OsMemFreeNodeAdd(pool, (struct OsMemFreeNodeHead *)node); - +#ifdef LOSCFG_KERNEL_LMS + if (g_lms != NULL) { + g_lms->freeMark(curNodeBackup, nextNodeBackup, OS_MEM_NODE_HEAD_SIZE); + } +#endif return ret; } @@ -1342,6 +1446,11 @@ STATIC INLINE VOID OsMemReAllocSmaller(VOID *pool, UINT32 allocSize, struct OsMe OsMemSplitNode(pool, node, allocSize); #if (LOSCFG_MEM_WATERLINE == 1) poolInfo->info.curUsedSize -= nodeSize - allocSize; +#endif +#ifdef LOSCFG_KERNEL_LMS + OsLmsReallocSplitNodeMark(node); + } else { + OsLmsReallocResizeMark(node, allocSize); #endif } OS_MEM_NODE_SET_USED_FLAG(node->sizeAndFlag); @@ -1356,8 +1465,16 @@ STATIC INLINE VOID OsMemMergeNodeForReAllocBigger(VOID *pool, UINT32 allocSize, node->sizeAndFlag = nodeSize; OsMemFreeNodeDelete(pool, (struct OsMemFreeNodeHead *)nextNode); OsMemMergeNode(nextNode); +#ifdef LOSCFG_KERNEL_LMS + OsLmsReallocMergeNodeMark(node); +#endif if ((allocSize + OS_MEM_MIN_LEFT_SIZE) <= node->sizeAndFlag) { OsMemSplitNode(pool, node, allocSize); +#ifdef LOSCFG_KERNEL_LMS + OsLmsReallocSplitNodeMark(node); + } else { + OsLmsReallocResizeMark(node, allocSize); +#endif } OS_MEM_NODE_SET_USED_FLAG(node->sizeAndFlag); OsMemWaterUsedRecord((struct OsMemPoolHead *)pool, node->sizeAndFlag - nodeSize); @@ -1752,10 +1869,8 @@ struct OsMemIntegrityCheckInfo g_integrityCheckRecord = {0}; STATIC INLINE VOID OsMemCheckInfoRecord(const struct OsMemNodeHead *errNode, const struct OsMemNodeHead *preNode) { - (VOID)memcpy_s(&g_integrityCheckRecord.preNode, sizeof(struct OsMemNodeHead), - preNode, sizeof(struct OsMemNodeHead)); - (VOID)memcpy_s(&g_integrityCheckRecord.errNode, sizeof(struct OsMemNodeHead), - errNode, sizeof(struct OsMemNodeHead)); + (VOID)memcpy(&g_integrityCheckRecord.preNode, preNode, sizeof(struct OsMemNodeHead)); + (VOID)memcpy(&g_integrityCheckRecord.errNode, errNode, sizeof(struct OsMemNodeHead)); } STATIC VOID OsMemIntegrityCheckError(struct OsMemPoolHead *pool, @@ -1891,7 +2006,7 @@ UINT32 LOS_MemInfoGet(VOID *pool, LOS_MEM_POOL_STATUS *poolStatus) return LOS_NOK; } - (VOID)memset_s(poolStatus, sizeof(LOS_MEM_POOL_STATUS), 0, sizeof(LOS_MEM_POOL_STATUS)); + (VOID)memset(poolStatus, 0, sizeof(LOS_MEM_POOL_STATUS)); OsAllMemNodeDoHandle(pool, OsMemNodeInfoGetHandle, (VOID *)poolStatus); @@ -2054,9 +2169,19 @@ STATIC INLINE VOID OsMemMulRegionsLink(struct OsMemPoolHead *poolHead, VOID *las curStartAddress = memRegion->startAddress; curLength = memRegion->length; - +#ifdef LOSCFG_KERNEL_LMS + UINT32 resize = 0; + if (g_lms != NULL) { + /* + * resize == 0, shadow memory init failed, no shadow memory for this pool, set poolSize as original size. + * resize != 0, shadow memory init successful, set poolSize as resize. + */ + resize = g_lms->init(curStartAddress, curLength); + curLength = (resize == 0) ? curLength : resize; + } +#endif // mark the gap between two regions as one used node - gapSize = (UINT8 *)(curStartAddress) - ((UINT8 *)(lastStartAddress) + lastLength); + gapSize = (UINT8 *)(curStartAddress) - ((UINT8 *)(poolHead) + poolHead->info.totalSize); lastEndNode->sizeAndFlag = gapSize + OS_MEM_NODE_HEAD_SIZE; OS_MEM_SET_MAGIC(lastEndNode); OS_MEM_NODE_SET_USED_FLAG(lastEndNode->sizeAndFlag); @@ -2122,7 +2247,7 @@ UINT32 LOS_MemRegionsAdd(VOID *pool, const LosMemRegion *const memRegions, UINT3 } firstFreeNode = OS_MEM_FIRST_NODE(lastStartAddress); - lastEndNode = OS_MEM_END_NODE(lastStartAddress, lastLength); + lastEndNode = OS_MEM_END_NODE(lastStartAddress, poolHead->info.totalSize); /* traverse the rest memory regions, and initialize them as free nodes and link together */ while (regionCount < memRegionCount) { curStartAddress = memRegion->startAddress; @@ -2131,7 +2256,7 @@ UINT32 LOS_MemRegionsAdd(VOID *pool, const LosMemRegion *const memRegions, UINT3 OsMemMulRegionsLink(poolHead, lastStartAddress, lastLength, lastEndNode, memRegion); lastStartAddress = curStartAddress; lastLength = curLength; - lastEndNode = OS_MEM_END_NODE(curStartAddress, curLength); + lastEndNode = OS_MEM_END_NODE(poolHead, poolHead->info.totalSize); memRegion++; regionCount++; } @@ -2163,7 +2288,7 @@ STATIC VOID OsMemExcInfoGetSub(struct OsMemPoolHead *pool, MemInfoCB *memExcInfo UINT32 taskID = OS_TASK_ERRORID; UINT32 intSave = 0; - (VOID)memset_s(memExcInfo, sizeof(MemInfoCB), 0, sizeof(MemInfoCB)); + (VOID)memset(memExcInfo, 0, sizeof(MemInfoCB)); MEM_LOCK(pool, intSave); memExcInfo->type = MEM_MANG_MEMORY; diff --git a/testsuites/BUILD.gn b/testsuites/BUILD.gn index 40722a3a..c9dc7801 100644 --- a/testsuites/BUILD.gn +++ b/testsuites/BUILD.gn @@ -69,6 +69,9 @@ group("testsuites") { if (defined(LOSCFG_DYNLINK)) { deps += [ "sample/kernel/dynlink:test_dynlink" ] } + if (defined(LOSCFG_KERNEL_LMS)) { + deps += [ "sample/kernel/lms:test_lms" ] + } if (!module_switch) { deps = [] } diff --git a/testsuites/include/iCunit.h b/testsuites/include/iCunit.h index 5d17a11c..72d8f23d 100644 --- a/testsuites/include/iCunit.h +++ b/testsuites/include/iCunit.h @@ -162,6 +162,7 @@ typedef enum { #endif TEST_DRIVERBASE, TEST_DYNLINK, + TEST_LMS, } LiteOS_test_module; typedef enum { diff --git a/testsuites/include/osTest.h b/testsuites/include/osTest.h index 0aa028ee..0827b8e8 100644 --- a/testsuites/include/osTest.h +++ b/testsuites/include/osTest.h @@ -85,6 +85,7 @@ extern "C" { #define LOS_KERNEL_DYNLINK_TEST 0 #define LOS_KERNEL_TICKLESS_TEST 0 #define LOS_KERNEL_PM_TEST 1 +#define LOS_KERNEL_LMS_TEST 0 #define LITEOS_CMSIS_TEST 0 #define LOS_CMSIS2_CORE_TASK_TEST 0 diff --git a/testsuites/sample/kernel/lms/BUILD.gn b/testsuites/sample/kernel/lms/BUILD.gn new file mode 100644 index 00000000..a9ea3da6 --- /dev/null +++ b/testsuites/sample/kernel/lms/BUILD.gn @@ -0,0 +1,78 @@ +# Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved. +# Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved. +# +# Redistribution and use in source and binary forms, with or without modification, +# are permitted provided that the following conditions are met: +# +# 1. Redistributions of source code must retain the above copyright notice, this list of +# conditions and the following disclaimer. +# +# 2. Redistributions in binary form must reproduce the above copyright notice, this list +# of conditions and the following disclaimer in the documentation and/or other materials +# provided with the distribution. +# +# 3. Neither the name of the copyright holder nor the names of its contributors may be used +# to endorse or promote products derived from this software without specific prior written +# permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, +# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR +# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, +# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, +# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; +# OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, +# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR +# OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF +# ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +static_library("test_lms") { + sources = [ + "It_los_lms.c", + "It_los_lms_001.c", + "It_los_lms_002.c", + "It_los_lms_003.c", + "It_los_lms_004.c", + "It_los_lms_005.c", + "It_los_lms_006.c", + "It_los_lms_007.c", + "It_los_lms_008.c", + "It_los_lms_009.c", + "It_los_lms_010.c", + "It_los_lms_011.c", + "It_los_lms_012.c", + "It_los_lms_013.c", + "It_los_lms_014.c", + "It_los_lms_015.c", + "It_los_lms_016.c", + "It_los_lms_017.c", + "It_los_lms_018.c", + "It_los_lms_019.c", + "It_los_lms_020.c", + "It_los_lms_021.c", + "It_los_lms_022.c", + "It_los_lms_023.c", + "It_los_lms_024.c", + ] + + if ("$ohos_build_compiler_specified" == "gcc") { + cflags_c = [ + "-O0", + "-fsanitize=kernel-address", + ] + } else { + cflags_c = [ + "-O0", + "-fsanitize=kernel-address", + "-mllvm", + "-asan-instrumentation-with-call-threshold=0", + "-mllvm", + "-asan-stack=0", + "-mllvm", + "-asan-globals=0", + ] + } + + configs += [ "//kernel/liteos_m/testsuites:include" ] +} diff --git a/testsuites/sample/kernel/lms/It_los_lms.c b/testsuites/sample/kernel/lms/It_los_lms.c new file mode 100644 index 00000000..22b3841a --- /dev/null +++ b/testsuites/sample/kernel/lms/It_los_lms.c @@ -0,0 +1,72 @@ +/* + * Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved. + * Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this list of + * conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this list + * of conditions and the following disclaimer in the documentation and/or other materials + * provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its contributors may be used + * to endorse or promote products derived from this software without specific prior written + * permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "It_los_lms.h" + +char g_testLmsPool[2 * PAGE_SIZE]; + +STATIC VOID testPoolInit(void) +{ + UINT32 ret = LOS_MemInit(g_testLmsPool, 2 * PAGE_SIZE); + if (ret != 0) { + PRINT_ERR("%s failed, ret = 0x%x\n", __FUNCTION__, ret); + return; + } +} + +VOID ItSuiteLosLms(void) +{ + testPoolInit(); + ItLosLms001(); + ItLosLms002(); + ItLosLms003(); + ItLosLms004(); + ItLosLms005(); + ItLosLms006(); + ItLosLms007(); + ItLosLms008(); + ItLosLms009(); + ItLosLms010(); + ItLosLms011(); + ItLosLms012(); + ItLosLms013(); + ItLosLms014(); + ItLosLms015(); + ItLosLms016(); + ItLosLms017(); + ItLosLms018(); + ItLosLms019(); + ItLosLms020(); + ItLosLms021(); + ItLosLms022(); + ItLosLms023(); + ItLosLms024(); +} diff --git a/testsuites/sample/kernel/lms/It_los_lms.h b/testsuites/sample/kernel/lms/It_los_lms.h new file mode 100644 index 00000000..e52a8c94 --- /dev/null +++ b/testsuites/sample/kernel/lms/It_los_lms.h @@ -0,0 +1,82 @@ +/* + * Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved. + * Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this list of + * conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this list + * of conditions and the following disclaimer in the documentation and/or other materials + * provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its contributors may be used + * to endorse or promote products derived from this software without specific prior written + * permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef IT_LOS_LMS_H +#define IT_LOS_LMS_H + +#include "osTest.h" +#include "los_memory.h" +#include "los_config.h" +#include "iCunit.h" + +#ifdef __cplusplus +#if __cplusplus +extern "C" { +#endif /* __cplusplus */ +#endif /* __cplusplus */ + +#define INDEX_MAX 20 +#define PAGE_SIZE (0x1000U) + +extern char g_testLmsPool[2 * PAGE_SIZE]; + +VOID ItLosLms001(void); +VOID ItLosLms002(void); +VOID ItLosLms003(void); +VOID ItLosLms004(void); +VOID ItLosLms005(void); +VOID ItLosLms006(void); +VOID ItLosLms007(void); +VOID ItLosLms008(void); +VOID ItLosLms009(void); +VOID ItLosLms010(void); +VOID ItLosLms011(void); +VOID ItLosLms012(void); +VOID ItLosLms013(void); +VOID ItLosLms014(void); +VOID ItLosLms015(void); +VOID ItLosLms016(void); +VOID ItLosLms017(void); +VOID ItLosLms018(void); +VOID ItLosLms019(void); +VOID ItLosLms020(void); +VOID ItLosLms021(void); +VOID ItLosLms022(void); +VOID ItLosLms023(void); +VOID ItLosLms024(void); + +#ifdef __cplusplus +#if __cplusplus +} +#endif /* __cplusplus */ +#endif /* __cplusplus */ + +#endif /* IT_LOS_LMS_H */ diff --git a/testsuites/sample/kernel/lms/It_los_lms_001.c b/testsuites/sample/kernel/lms/It_los_lms_001.c new file mode 100644 index 00000000..97f363df --- /dev/null +++ b/testsuites/sample/kernel/lms/It_los_lms_001.c @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved. + * Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this list of + * conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this list + * of conditions and the following disclaimer in the documentation and/or other materials + * provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its contributors may be used + * to endorse or promote products derived from this software without specific prior written + * permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "osTest.h" +#include "It_los_lms.h" + +static UINT32 TestCase(VOID) +{ + UINT32 i; + char *str = (char*)LOS_MemAlloc(g_testLmsPool, INDEX_MAX); + for (i = 0; i < INDEX_MAX + 1; i++) { + if (i % 4 == 0) { + PRINTK("\n"); + } + PRINTK("str[%2d]=0x%2x ", i, str[i]); + } + + return LOS_OK; +} + +/* LmsTestOsmallocOverflow */ +VOID ItLosLms001(void) +{ + TEST_ADD_CASE("ItLosLms001", TestCase, TEST_LOS, TEST_LMS, TEST_LEVEL1, TEST_FUNCTION); +} + diff --git a/testsuites/sample/kernel/lms/It_los_lms_002.c b/testsuites/sample/kernel/lms/It_los_lms_002.c new file mode 100644 index 00000000..63d4aab5 --- /dev/null +++ b/testsuites/sample/kernel/lms/It_los_lms_002.c @@ -0,0 +1,58 @@ +/* + * Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved. + * Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this list of + * conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this list + * of conditions and the following disclaimer in the documentation and/or other materials + * provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its contributors may be used + * to endorse or promote products derived from this software without specific prior written + * permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "osTest.h" +#include "It_los_lms.h" + +static UINT32 TestCase(VOID) +{ + UINT32 boundary; + CHAR *str = NULL; + UINT32 i; + UINT32 size = 20; /* mem size 20 */ + for (i = 2; i < 8; i++) { /* boundary loop from 2 to 8 */ + boundary = 1 << i; + str = (CHAR *)LOS_MemAllocAlign(m_aucSysMem0, size, boundary); + ICUNIT_ASSERT_NOT_EQUAL(str, NULL, str); + PRINTK("str = 0x%x, boundary = %d\n", str, boundary); + PRINTK("0x%x\n", str[size + 1]); /* trigger read overflow at size + 1 */ + (VOID)LOS_MemFree(m_aucSysMem0, str); + } + + return LOS_OK; +} + +/* LmsTestMemAlignOverflow */ +VOID ItLosLms002(void) +{ + TEST_ADD_CASE("ItLosLms002", TestCase, TEST_LOS, TEST_LMS, TEST_LEVEL1, TEST_FUNCTION); +} + diff --git a/testsuites/sample/kernel/lms/It_los_lms_003.c b/testsuites/sample/kernel/lms/It_los_lms_003.c new file mode 100644 index 00000000..2aa11b27 --- /dev/null +++ b/testsuites/sample/kernel/lms/It_los_lms_003.c @@ -0,0 +1,59 @@ +/* + * Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved. + * Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this list of + * conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this list + * of conditions and the following disclaimer in the documentation and/or other materials + * provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its contributors may be used + * to endorse or promote products derived from this software without specific prior written + * permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "osTest.h" +#include "It_los_lms.h" + +static UINT32 TestCase(VOID) +{ + UINT32 boundary; + CHAR *str = NULL; + UINT32 i; + UINT32 size = 0x8 + 0x2; /* mem size 0x8 + 0x2 */ + for (i = 2; i < 8; i++) { /* boundary loop from 2 to 8 */ + boundary = 1 << i; + str = (CHAR *)LOS_MemAllocAlign(m_aucSysMem0, size, boundary); + ICUNIT_ASSERT_NOT_EQUAL(str, NULL, str); + PRINTK("str = 0x%x, boundary = %d\n", str, boundary); + PRINTK("0x%x\n", str[size + 1]); /* not trigger read overflow at size + 1 */ + PRINTK("0x%x\n", str[size + 2]); /* trigger read overflow at size + 2 */ + (VOID)LOS_MemFree(m_aucSysMem0, str); + } + + return LOS_OK; +} + +/* LmsTestMemAlignOverflow_Not4Align */ +VOID ItLosLms003(void) +{ + TEST_ADD_CASE("ItLosLms003", TestCase, TEST_LOS, TEST_LMS, TEST_LEVEL1, TEST_FUNCTION); +} + diff --git a/testsuites/sample/kernel/lms/It_los_lms_004.c b/testsuites/sample/kernel/lms/It_los_lms_004.c new file mode 100644 index 00000000..003a530e --- /dev/null +++ b/testsuites/sample/kernel/lms/It_los_lms_004.c @@ -0,0 +1,72 @@ +/* + * Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved. + * Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this list of + * conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this list + * of conditions and the following disclaimer in the documentation and/or other materials + * provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its contributors may be used + * to endorse or promote products derived from this software without specific prior written + * permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "osTest.h" +#include "It_los_lms.h" + +static UINT32 TestCase(VOID) +{ +#define PTR_NUM 100 +#define SIZE_NUM 2 + CHAR *str[PTR_NUM] = {NULL}; + UINT32 size[SIZE_NUM] = {20, 10}; /* mem size 20, 10 */ + UINT32 index; + UINT32 i, j, k; + UINT32 boundary; + for (k = 0; k < SIZE_NUM; k++) { + index = 0; + for (j = 0; j < 10; j++) { /* loop 10 times each size */ + for (i = 2; i < 8; i++, index++) { /* boundary loop from 2 to 8 */ + boundary = 1 << i; + PRINT_DEBUG("size = %d, boundary = %d\n", size[k], boundary); + str[index] = (char *)LOS_MemAllocAlign(m_aucSysMem0, size[k], boundary); + ICUNIT_GOTO_NOT_EQUAL(str[index], NULL, str[index], EXIT); + } + } + for (i = 0; i < index; i++) { + (VOID)LOS_MemFree(m_aucSysMem0, str[i]); + } + } + + return LOS_OK; +EXIT: + for (i = 0; i < index; i++) { + (VOID)LOS_MemFree(m_aucSysMem0, str[i]); + } + return LOS_OK; +} + +/* LmsTestMemAlignFree */ +VOID ItLosLms004(void) +{ + TEST_ADD_CASE("ItLosLms004", TestCase, TEST_LOS, TEST_LMS, TEST_LEVEL1, TEST_FUNCTION); +} + diff --git a/testsuites/sample/kernel/lms/It_los_lms_005.c b/testsuites/sample/kernel/lms/It_los_lms_005.c new file mode 100644 index 00000000..bb7f1ee2 --- /dev/null +++ b/testsuites/sample/kernel/lms/It_los_lms_005.c @@ -0,0 +1,61 @@ +/* + * Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved. + * Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this list of + * conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this list + * of conditions and the following disclaimer in the documentation and/or other materials + * provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its contributors may be used + * to endorse or promote products derived from this software without specific prior written + * permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "osTest.h" +#include "It_los_lms.h" + +static UINT32 TestCase(VOID) +{ + UINT32 size = 20; /* mem size 20 */ + + CHAR *str = (CHAR *)LOS_MemAlloc(g_testLmsPool, size); + ICUNIT_ASSERT_NOT_EQUAL(str, NULL, str); + PRINT_DEBUG("str = 0x%x\n", str); + (VOID)memset_s(str, size, 0xca, size); + + /* oldSize - newsize < OS_MEM_NODE_HEAD_SIZE + OS_MEM_ALIGN_SIZE */ + CHAR *newPtr = LOS_MemRealloc(g_testLmsPool, str, size - 4); /* mem size - 4 */ + ICUNIT_GOTO_NOT_EQUAL(newPtr, NULL, newPtr, EXIT); + + PRINTK("newPtr = 0x%x\n", newPtr[size - 4]); /* trigger read overflow at size - 4 */ + (VOID)LOS_MemFree(g_testLmsPool, newPtr); + return LOS_OK; +EXIT: + (VOID)LOS_MemFree(g_testLmsPool, str); + return LOS_OK; +} + +/* LmsTestReallocTest1 */ +VOID ItLosLms005(void) +{ + TEST_ADD_CASE("ItLosLms005", TestCase, TEST_LOS, TEST_LMS, TEST_LEVEL1, TEST_FUNCTION); +} + diff --git a/testsuites/sample/kernel/lms/It_los_lms_006.c b/testsuites/sample/kernel/lms/It_los_lms_006.c new file mode 100644 index 00000000..c072fb18 --- /dev/null +++ b/testsuites/sample/kernel/lms/It_los_lms_006.c @@ -0,0 +1,61 @@ +/* + * Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved. + * Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this list of + * conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this list + * of conditions and the following disclaimer in the documentation and/or other materials + * provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its contributors may be used + * to endorse or promote products derived from this software without specific prior written + * permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "osTest.h" +#include "It_los_lms.h" + +static UINT32 TestCase(VOID) +{ + UINT32 size = 20 * 3; /* mem size 20 * 3 */ + + CHAR *str = (CHAR *)LOS_MemAlloc(g_testLmsPool, size); + ICUNIT_ASSERT_NOT_EQUAL(str, NULL, str); + PRINT_DEBUG("str = 0x%x\n", str); + (VOID)memset_s(str, size, 0xca, size); + + /* oldSize - newSize >= OS_MEM_NODE_HEAD_SIZE + OS_MEM_ALIGN_SIZE */ + CHAR *newPtr = LOS_MemRealloc(g_testLmsPool, str, size - 32); /* mem size - 32 */ + ICUNIT_GOTO_NOT_EQUAL(newPtr, NULL, newPtr, EXIT); + + PRINTK("newPtr = 0x%x\n", newPtr[size - 30]); /* trigger read overflow at size - 30 */ + (VOID)LOS_MemFree(g_testLmsPool, newPtr); + return LOS_OK; +EXIT: + (VOID)LOS_MemFree(g_testLmsPool, str); + return LOS_OK; +} + +/* LmsTestReallocTest2 */ +VOID ItLosLms006(void) +{ + TEST_ADD_CASE("ItLosLms006", TestCase, TEST_LOS, TEST_LMS, TEST_LEVEL1, TEST_FUNCTION); +} + diff --git a/testsuites/sample/kernel/lms/It_los_lms_007.c b/testsuites/sample/kernel/lms/It_los_lms_007.c new file mode 100644 index 00000000..6cc040a3 --- /dev/null +++ b/testsuites/sample/kernel/lms/It_los_lms_007.c @@ -0,0 +1,81 @@ +/* + * Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved. + * Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this list of + * conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this list + * of conditions and the following disclaimer in the documentation and/or other materials + * provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its contributors may be used + * to endorse or promote products derived from this software without specific prior written + * permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "osTest.h" +#include "It_los_lms.h" + +static UINT32 TestCase(VOID) +{ + UINT32 size = 20; /* mem size 20 */ + + CHAR *str = (CHAR *)LOS_MemAlloc(g_testLmsPool, size); + ICUNIT_ASSERT_NOT_EQUAL(str, NULL, str); + PRINT_DEBUG("str = 0x%x\n", str); + + CHAR *bigger = (CHAR *)LOS_MemAlloc(g_testLmsPool, size * 3); /* mem size * 3 */ + ICUNIT_GOTO_NOT_EQUAL(bigger, NULL, bigger, EXIT); + PRINT_DEBUG("bigger addr = 0x%x, %d\n", bigger, __LINE__); + + CHAR *tmp = (CHAR *)LOS_MemAlloc(g_testLmsPool, size); /* do not release */ + ICUNIT_GOTO_NOT_EQUAL(tmp, NULL, tmp, EXIT1); + PRINT_DEBUG("tmp addr = 0x%x,%d\n", tmp, __LINE__); + + (VOID)LOS_MemFree(g_testLmsPool, bigger); + + /* resize < size + next size && split node */ + CHAR *newPtr = LOS_MemRealloc(g_testLmsPool, str, size * 2); /* mem size * 2 */ + ICUNIT_GOTO_NOT_EQUAL(newPtr, NULL, newPtr, EXIT2); + + PRINTK("0x%x\n", newPtr[size * 2]); /* trigger read overflow at size * 2 */ + + (VOID)LOS_MemFree(g_testLmsPool, tmp); + (VOID)LOS_MemFree(g_testLmsPool, newPtr); + return LOS_OK; + +EXIT: + (VOID)LOS_MemFree(g_testLmsPool, str); + return LOS_OK; + EXIT1: + (VOID)LOS_MemFree(g_testLmsPool, str); + (VOID)LOS_MemFree(g_testLmsPool, bigger); + return LOS_OK; + EXIT2: + (VOID)LOS_MemFree(g_testLmsPool, str); + (VOID)LOS_MemFree(g_testLmsPool, tmp); + return LOS_OK; +} + +/* LmsTestReallocTest3 */ +VOID ItLosLms007(void) +{ + TEST_ADD_CASE("ItLosLms007", TestCase, TEST_LOS, TEST_LMS, TEST_LEVEL1, TEST_FUNCTION); +} + diff --git a/testsuites/sample/kernel/lms/It_los_lms_008.c b/testsuites/sample/kernel/lms/It_los_lms_008.c new file mode 100644 index 00000000..dc9d8885 --- /dev/null +++ b/testsuites/sample/kernel/lms/It_los_lms_008.c @@ -0,0 +1,81 @@ +/* + * Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved. + * Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this list of + * conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this list + * of conditions and the following disclaimer in the documentation and/or other materials + * provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its contributors may be used + * to endorse or promote products derived from this software without specific prior written + * permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "osTest.h" +#include "It_los_lms.h" + +static UINT32 TestCase(VOID) +{ + UINT32 size = 20; /* mem size 20 */ + + CHAR *str = (CHAR *)LOS_MemAlloc(g_testLmsPool, size); + ICUNIT_ASSERT_NOT_EQUAL(str, NULL, str); + PRINT_DEBUG("str = 0x%x\n", str); + + CHAR *bigger = (CHAR *)LOS_MemAlloc(g_testLmsPool, size * 3); /* mem size * 3 */ + ICUNIT_GOTO_NOT_EQUAL(bigger, NULL, bigger, EXIT); + PRINT_DEBUG("bigger = 0x%x,%d\n", bigger, __LINE__); + + CHAR *tmp = (CHAR *)LOS_MemAlloc(g_testLmsPool, size); /* do not release */ + ICUNIT_GOTO_NOT_EQUAL(tmp, NULL, tmp, EXIT1); + PRINT_DEBUG("tmp = 0x%x,%d\n", tmp, __LINE__); + + (VOID)LOS_MemFree(g_testLmsPool, bigger); + + /* resize < size + next size && not split node */ + CHAR *newPtr = LOS_MemRealloc(g_testLmsPool, str, size * 4 + 8); /* mem size * 4 - 8 */ + ICUNIT_GOTO_NOT_EQUAL(newPtr, NULL, newPtr, EXIT2); + PRINT_DEBUG("newPtr = 0x%x,%d\n", newPtr, __LINE__); + PRINTK("0x%x\n", newPtr[size * 4 + 8]); /* trigger read overflow at size * 4 - 8 */ + + (VOID)LOS_MemFree(g_testLmsPool, tmp); + (VOID)LOS_MemFree(g_testLmsPool, newPtr); + + return LOS_OK; +EXIT: + (VOID)LOS_MemFree(g_testLmsPool, str); + return LOS_OK; + EXIT1: + (VOID)LOS_MemFree(g_testLmsPool, str); + (VOID)LOS_MemFree(g_testLmsPool, bigger); + return LOS_OK; + EXIT2: + (VOID)LOS_MemFree(g_testLmsPool, str); + (VOID)LOS_MemFree(g_testLmsPool, tmp); + return LOS_OK; +} + +/* LmsTestReallocTest4 */ +VOID ItLosLms008(void) +{ + TEST_ADD_CASE("ItLosLms008", TestCase, TEST_LOS, TEST_LMS, TEST_LEVEL1, TEST_FUNCTION); +} + diff --git a/testsuites/sample/kernel/lms/It_los_lms_009.c b/testsuites/sample/kernel/lms/It_los_lms_009.c new file mode 100644 index 00000000..42fa6e53 --- /dev/null +++ b/testsuites/sample/kernel/lms/It_los_lms_009.c @@ -0,0 +1,81 @@ +/* + * Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved. + * Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this list of + * conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this list + * of conditions and the following disclaimer in the documentation and/or other materials + * provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its contributors may be used + * to endorse or promote products derived from this software without specific prior written + * permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "osTest.h" +#include "It_los_lms.h" + +static UINT32 TestCase(VOID) +{ + UINT32 size = 20; /* mem size 20 */ + + CHAR *str = (CHAR *)LOS_MemAlloc(g_testLmsPool, size); + ICUNIT_ASSERT_NOT_EQUAL(str, NULL, str); + PRINT_DEBUG("str = 0x%x,%d\n", str, __LINE__); + + CHAR *bigger = (CHAR *)LOS_MemAlloc(g_testLmsPool, size * 3); /* mem size * 3 */ + ICUNIT_GOTO_NOT_EQUAL(bigger, NULL, bigger, EXIT); + PRINT_DEBUG("bigger = 0x%x,%d\n", bigger, __LINE__); + + CHAR *tmp = (CHAR *)LOS_MemAlloc(g_testLmsPool, size); /* do not release */ + ICUNIT_GOTO_NOT_EQUAL(tmp, NULL, tmp, EXIT1); + PRINT_DEBUG("tmp = 0x%x,%d\n", tmp, __LINE__); + + (VOID)LOS_MemFree(g_testLmsPool, bigger); + + /* resize > size + next size */ + CHAR *newPtr = LOS_MemRealloc(g_testLmsPool, str, size * 6); /* mem size * 6 */ + ICUNIT_GOTO_NOT_EQUAL(newPtr, NULL, newPtr, EXIT2); + PRINT_DEBUG("newPtr = 0x%x,%d\n", newPtr, __LINE__); + PRINTK("0x%x\n", newPtr[size * 6]); /* trigger overflow at size * 6 */ + + (VOID)LOS_MemFree(g_testLmsPool, tmp); + (VOID)LOS_MemFree(g_testLmsPool, newPtr); + + return LOS_OK; +EXIT: + (VOID)LOS_MemFree(g_testLmsPool, str); + return LOS_OK; + EXIT1: + (VOID)LOS_MemFree(g_testLmsPool, str); + (VOID)LOS_MemFree(g_testLmsPool, bigger); + return LOS_OK; + EXIT2: + (VOID)LOS_MemFree(g_testLmsPool, str); + (VOID)LOS_MemFree(g_testLmsPool, tmp); + return LOS_OK; +} + +/* LmsTestReallocTest5 */ +VOID ItLosLms009(void) +{ + TEST_ADD_CASE("ItLosLms009", TestCase, TEST_LOS, TEST_LMS, TEST_LEVEL1, TEST_FUNCTION); +} + diff --git a/testsuites/sample/kernel/lms/It_los_lms_010.c b/testsuites/sample/kernel/lms/It_los_lms_010.c new file mode 100644 index 00000000..a5532c0c --- /dev/null +++ b/testsuites/sample/kernel/lms/It_los_lms_010.c @@ -0,0 +1,79 @@ +/* + * Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved. + * Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this list of + * conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this list + * of conditions and the following disclaimer in the documentation and/or other materials + * provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its contributors may be used + * to endorse or promote products derived from this software without specific prior written + * permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "osTest.h" +#include "It_los_lms.h" + +static UINT32 TestCase(VOID) +{ + UINT32 size = 20; /* mem size 20 */ + CHAR *backStr; + + CHAR *str = (CHAR *)LOS_MemAlloc(g_testLmsPool, size); + ICUNIT_ASSERT_NOT_EQUAL(str, NULL, str); + PRINT_DEBUG("str = 0x%x,%d\n", str, __LINE__); + + CHAR *newPtr = LOS_MemRealloc(g_testLmsPool, str, 0x8+0x2); /* reaSize 0x2 is not 4Align */ + ICUNIT_GOTO_NOT_EQUAL(newPtr, NULL, newPtr, EXIT); + PRINT_DEBUG("newPtr = 0x%x,%d\n", newPtr, __LINE__); + + PRINTK("0x%x\n", newPtr[0x8+0x3]); /* not trigger overflow at newPtr[0x3] */ + + PRINTK("Trigger write overflow\n"); + newPtr[0x8+0x4] = 0x01; /* write 0x1,trigger overflow at newPtr[0x4] */ + + PRINTK("Trigger read overflow\n"); + PRINTK("0x%x\n", newPtr[0x8+0x4]); /* trigger read overflow at newPtr[0x4] */ + + backStr = (CHAR *)(newPtr - 1); /* Add offset -1 */ + PRINTK("Trigger back offset write overflow\n"); + *backStr = 0x01; + + PRINTK("Trigger back offset read overflow\n"); + PRINTK("0x%x\n", *backStr); /* trigger overflow */ + + PRINTK("Trigger read overflow\n"); /* trigger read overflow */ + PRINTK("0x%x\n", newPtr[0x8+0x5]); /* trigger overflow at newPtr[0x5] */ + + (VOID)LOS_MemFree(g_testLmsPool, newPtr); + + return LOS_OK; +EXIT: + (VOID)LOS_MemFree(g_testLmsPool, str); + return LOS_OK; +} + +/* LmsTestReallocTest6 */ +VOID ItLosLms010(void) +{ + TEST_ADD_CASE("ItLosLms010", TestCase, TEST_LOS, TEST_LMS, TEST_LEVEL1, TEST_FUNCTION); +} + diff --git a/testsuites/sample/kernel/lms/It_los_lms_011.c b/testsuites/sample/kernel/lms/It_los_lms_011.c new file mode 100644 index 00000000..30bca052 --- /dev/null +++ b/testsuites/sample/kernel/lms/It_los_lms_011.c @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved. + * Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this list of + * conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this list + * of conditions and the following disclaimer in the documentation and/or other materials + * provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its contributors may be used + * to endorse or promote products derived from this software without specific prior written + * permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "osTest.h" +#include "It_los_lms.h" + +static UINT32 TestCase(VOID) +{ + UINT32 i; + char *str = (char*)LOS_MemAlloc(m_aucSysMem0, INDEX_MAX); + for (i = 0; i < INDEX_MAX; i++) { + if (i % 4 == 0) { + PRINTK("\n"); + } + PRINTK("str[%2d]=0x%2x ", i, str[i]); + } + LOS_MemFree(m_aucSysMem0, str); + PRINTK("str[%2d]=0x%2x ", 0, str[0]); + + return LOS_OK; +} + +/* LmsTestUseAfterFree */ +VOID ItLosLms011(void) +{ + TEST_ADD_CASE("ItLosLms011", TestCase, TEST_LOS, TEST_LMS, TEST_LEVEL1, TEST_FUNCTION); +} + diff --git a/testsuites/sample/kernel/lms/It_los_lms_012.c b/testsuites/sample/kernel/lms/It_los_lms_012.c new file mode 100644 index 00000000..6818752d --- /dev/null +++ b/testsuites/sample/kernel/lms/It_los_lms_012.c @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved. + * Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this list of + * conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this list + * of conditions and the following disclaimer in the documentation and/or other materials + * provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its contributors may be used + * to endorse or promote products derived from this software without specific prior written + * permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "osTest.h" +#include "It_los_lms.h" + +static UINT32 TestCase(VOID) +{ + UINT32 i; + char *str = (char*)LOS_MemAlloc(m_aucSysMem0, INDEX_MAX); + for (i = 0; i < INDEX_MAX; i++) { + if (i % 4 == 0) { + PRINTK("\n"); + } + PRINTK("str[%2d]=0x%2x ", i, str[i]); + } + LOS_MemFree(m_aucSysMem0, str); + LOS_MemFree(m_aucSysMem0, str); + + return LOS_OK; +} + +/* LmsTestDoubleFree */ +VOID ItLosLms012(void) +{ + TEST_ADD_CASE("ItLosLms012", TestCase, TEST_LOS, TEST_LMS, TEST_LEVEL1, TEST_FUNCTION); +} + diff --git a/testsuites/sample/kernel/lms/It_los_lms_013.c b/testsuites/sample/kernel/lms/It_los_lms_013.c new file mode 100644 index 00000000..a7231fb6 --- /dev/null +++ b/testsuites/sample/kernel/lms/It_los_lms_013.c @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved. + * Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this list of + * conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this list + * of conditions and the following disclaimer in the documentation and/or other materials + * provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its contributors may be used + * to endorse or promote products derived from this software without specific prior written + * permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "osTest.h" +#include "It_los_lms.h" + +static UINT32 TestCase(VOID) +{ + UINT32 i; + char str2[INDEX_MAX + 1] = "1234567890abcdefghij"; + char *str = (char*)LOS_MemAlloc(m_aucSysMem0, INDEX_MAX); + memcpy(str, str2, INDEX_MAX + 1); + for (i = 0; i < INDEX_MAX + 1; i++) { + if (i % 4 == 0) { + PRINTK("\n"); + } + PRINTK("str[%2d]=0x%2x ", i, str[i]); + } + + return LOS_OK; +} + +/* LmsTestMemcpyOverflow */ +VOID ItLosLms013(void) +{ + TEST_ADD_CASE("ItLosLms013", TestCase, TEST_LOS, TEST_LMS, TEST_LEVEL1, TEST_FUNCTION); +} + diff --git a/testsuites/sample/kernel/lms/It_los_lms_014.c b/testsuites/sample/kernel/lms/It_los_lms_014.c new file mode 100644 index 00000000..4f94d5b0 --- /dev/null +++ b/testsuites/sample/kernel/lms/It_los_lms_014.c @@ -0,0 +1,55 @@ +/* + * Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved. + * Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this list of + * conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this list + * of conditions and the following disclaimer in the documentation and/or other materials + * provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its contributors may be used + * to endorse or promote products derived from this software without specific prior written + * permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "osTest.h" +#include "It_los_lms.h" + +static UINT32 TestCase(VOID) +{ + UINT32 i; + char *str = (char*)LOS_MemAlloc(m_aucSysMem0, INDEX_MAX); + memset(str, 0xca, INDEX_MAX + 1); + for (i = 0; i < INDEX_MAX + 1; i++) { + if (i % 4 == 0) { + PRINTK("\n"); + } + PRINTK("str[%2d]=0x%2x ", i, str[i]); + } + + return LOS_OK; +} + +/* LmsTestMemsetSOverflow */ +VOID ItLosLms014(void) +{ + TEST_ADD_CASE("ItLosLms014", TestCase, TEST_LOS, TEST_LMS, TEST_LEVEL1, TEST_FUNCTION); +} + diff --git a/testsuites/sample/kernel/lms/It_los_lms_015.c b/testsuites/sample/kernel/lms/It_los_lms_015.c new file mode 100644 index 00000000..7dae9d8c --- /dev/null +++ b/testsuites/sample/kernel/lms/It_los_lms_015.c @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved. + * Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this list of + * conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this list + * of conditions and the following disclaimer in the documentation and/or other materials + * provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its contributors may be used + * to endorse or promote products derived from this software without specific prior written + * permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "osTest.h" +#include "It_los_lms.h" + +static UINT32 TestCase(VOID) +{ +#define SIZEE 100 + CHAR src[SIZEE + 1] = {0}; + CHAR *p = (CHAR *)LOS_MemAlloc(m_aucSysMem0, SIZEE); + ICUNIT_ASSERT_NOT_EQUAL(p, NULL, 0); + + memmove(p, src, SIZEE); + PRINTK("p[0] = %d\n", p[0]); + memmove(p, src, SIZEE + 1); /* trigger overflow */ + + return LOS_OK; +} + +/* LmsTestMemmoveOverflow */ +VOID ItLosLms015(void) +{ + TEST_ADD_CASE("ItLosLms015", TestCase, TEST_LOS, TEST_LMS, TEST_LEVEL1, TEST_FUNCTION); +} + diff --git a/testsuites/sample/kernel/lms/It_los_lms_016.c b/testsuites/sample/kernel/lms/It_los_lms_016.c new file mode 100644 index 00000000..2ec0709f --- /dev/null +++ b/testsuites/sample/kernel/lms/It_los_lms_016.c @@ -0,0 +1,68 @@ +/* + * Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved. + * Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this list of + * conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this list + * of conditions and the following disclaimer in the documentation and/or other materials + * provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its contributors may be used + * to endorse or promote products derived from this software without specific prior written + * permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "osTest.h" +#include "It_los_lms.h" + +static UINT32 TestCase(VOID) +{ + CHAR *string = "LMS_TestCase"; + CHAR *src; + CHAR *buf; + CHAR *str; + UINT32 ret; + + src = LOS_MemAlloc(m_aucSysMem0, strlen(string)); + ICUNIT_ASSERT_NOT_EQUAL(src, NULL, src); + + buf = LOS_MemAlloc(m_aucSysMem0, (strlen(string) + 10)); /* mem size 10 */ + ICUNIT_ASSERT_NOT_EQUAL(buf, NULL, buf); + + str = strcpy(src, string); /* write overflow */ + ICUNIT_ASSERT_NOT_EQUAL(str, NULL, str); + + (VOID)strcpy(buf, src); /* Check LMS detection information when the strcpy src overflows. */ + + ret = LOS_MemFree(m_aucSysMem0, buf); + ICUNIT_ASSERT_NOT_EQUAL(ret, LOS_NOK, ret); + + ret = LOS_MemFree(m_aucSysMem0, src); + ICUNIT_ASSERT_NOT_EQUAL(ret, LOS_NOK, ret); + + return LOS_OK; +} + +/* LmsTestStrcpyOverflow */ +VOID ItLosLms016(void) +{ + TEST_ADD_CASE("ItLosLms016", TestCase, TEST_LOS, TEST_LMS, TEST_LEVEL1, TEST_FUNCTION); +} + diff --git a/testsuites/sample/kernel/lms/It_los_lms_017.c b/testsuites/sample/kernel/lms/It_los_lms_017.c new file mode 100644 index 00000000..5458d4c0 --- /dev/null +++ b/testsuites/sample/kernel/lms/It_los_lms_017.c @@ -0,0 +1,71 @@ +/* + * Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved. + * Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this list of + * conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this list + * of conditions and the following disclaimer in the documentation and/or other materials + * provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its contributors may be used + * to endorse or promote products derived from this software without specific prior written + * permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "osTest.h" +#include "It_los_lms.h" + +static UINT32 TestCase(VOID) +{ + CHAR *string = "LMS_TestCase"; + CHAR *src; + CHAR *buf; + CHAR *str; + UINT32 ret; + + src = LOS_MemAlloc(m_aucSysMem0, (strlen(string) + 1)); /* mem size 1 */ + ICUNIT_ASSERT_NOT_EQUAL(src, NULL, src); + + str = strcpy(src, string); + ICUNIT_ASSERT_NOT_EQUAL(str, NULL, str); + + buf = LOS_MemAlloc(m_aucSysMem0, (strlen(string) + 4)); /* mem size 10 */ + ICUNIT_ASSERT_NOT_EQUAL(buf, NULL, buf); + (VOID)memset(buf, 0, (strlen(string) + 4)); /* memset size 10 */ + + (VOID)strcat(buf, src); /* no overflows. */ + (VOID)strcat(buf, "123"); /* no overflows. */ + (VOID)strcat(buf, "4"); /* write overflows. */ + + ret = LOS_MemFree(m_aucSysMem0, buf); + ICUNIT_ASSERT_NOT_EQUAL(ret, LOS_NOK, ret); + + ret = LOS_MemFree(m_aucSysMem0, src); + ICUNIT_ASSERT_NOT_EQUAL(ret, LOS_NOK, ret); + + return LOS_OK; +} + +/* LmsTestStrcatOverflow */ +VOID ItLosLms017(void) +{ + TEST_ADD_CASE("ItLosLms017", TestCase, TEST_LOS, TEST_LMS, TEST_LEVEL1, TEST_FUNCTION); +} + diff --git a/testsuites/sample/kernel/lms/It_los_lms_018.c b/testsuites/sample/kernel/lms/It_los_lms_018.c new file mode 100644 index 00000000..34976f3b --- /dev/null +++ b/testsuites/sample/kernel/lms/It_los_lms_018.c @@ -0,0 +1,74 @@ +/* + * Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved. + * Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this list of + * conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this list + * of conditions and the following disclaimer in the documentation and/or other materials + * provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its contributors may be used + * to endorse or promote products derived from this software without specific prior written + * permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "osTest.h" +#include "It_los_lms.h" + +static UINT32 TestCase(VOID) +{ + CHAR *src; + CHAR *buf; + + src = LOS_MemAlloc(m_aucSysMem0, 9); + ICUNIT_ASSERT_NOT_EQUAL(src, NULL, src); + + (VOID)memset(src, 0, 9); + src[0] = 49; + src[1] = 50; + src[2] = 51; + src[3] = 52; + src[4] = 53; + src[5] = 54; + src[6] = 55; + src[7] = 56; + PRINTK("strlen(src) = %d\n", strlen(src)); + + buf = LOS_MemAlloc(m_aucSysMem0, 7); + ICUNIT_ASSERT_NOT_EQUAL(buf, NULL, buf); + buf[0] = 0; + strncat(buf, src, 8); /* trigger buf overflow */ + buf[0] = 0; + strncat(buf, src, 9); /* trigger buf overflow */ + buf[0] = 0; + strncat(buf, src, 20); /* trigger buf overflow */ + buf[0] = 0; + strncat(buf, src, 21); /* trigger buf overflow */ + PRINTK("\n######%s stop ######\n", __FUNCTION__); + + return LOS_OK; +} + +/* LmsTestStrncatOverflow */ +VOID ItLosLms018(void) +{ + TEST_ADD_CASE("ItLosLms018", TestCase, TEST_LOS, TEST_LMS, TEST_LEVEL1, TEST_FUNCTION); +} + diff --git a/testsuites/sample/kernel/lms/It_los_lms_019.c b/testsuites/sample/kernel/lms/It_los_lms_019.c new file mode 100644 index 00000000..6bc98370 --- /dev/null +++ b/testsuites/sample/kernel/lms/It_los_lms_019.c @@ -0,0 +1,72 @@ +/* + * Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved. + * Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this list of + * conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this list + * of conditions and the following disclaimer in the documentation and/or other materials + * provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its contributors may be used + * to endorse or promote products derived from this software without specific prior written + * permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "osTest.h" +#include "It_los_lms.h" + +static UINT32 TestCase(VOID) +{ + CHAR *src; + CHAR *buf; + + src = LOS_MemAlloc(m_aucSysMem0, 9); + ICUNIT_ASSERT_NOT_EQUAL(src, NULL, src); + + (VOID)memset(src, 0, 9); + src[0] = 49; + src[1] = 50; + src[2] = 51; + src[3] = 52; + src[4] = 53; + src[5] = 54; + src[6] = 55; + src[7] = 56; + PRINTK("strlen(src) = %d\n", strlen(src)); + + buf = LOS_MemAlloc(m_aucSysMem0, 20); + ICUNIT_ASSERT_NOT_EQUAL(buf, NULL, buf); + buf[0] = 0; + (VOID)strncpy(buf, src, 8); /* no trigger overflow */ + buf[0] = 0; + (VOID)strncpy(buf, src, 9); /* no trigger overflow */ + buf[0] = 0; + (VOID)strncpy(buf, src, 20); /* no trigger overflow */ + buf[0] = 0; + (VOID)strncpy(buf, src, 21); /* trigger buf overflow */ + return LOS_OK; +} + +/* LmsTestStrncpyOverflow */ +VOID ItLosLms019(void) +{ + TEST_ADD_CASE("ItLosLms019", TestCase, TEST_LOS, TEST_LMS, TEST_LEVEL1, TEST_FUNCTION); +} + diff --git a/testsuites/sample/kernel/lms/It_los_lms_020.c b/testsuites/sample/kernel/lms/It_los_lms_020.c new file mode 100644 index 00000000..16719d2a --- /dev/null +++ b/testsuites/sample/kernel/lms/It_los_lms_020.c @@ -0,0 +1,51 @@ +/* + * Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved. + * Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this list of + * conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this list + * of conditions and the following disclaimer in the documentation and/or other materials + * provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its contributors may be used + * to endorse or promote products derived from this software without specific prior written + * permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "osTest.h" +#include "It_los_lms.h" + +static UINT32 TestCase(VOID) +{ + CHAR *p = (CHAR *)LOS_MemAlloc(g_testLmsPool, INDEX_MAX); + ICUNIT_ASSERT_NOT_EQUAL(p, NULL, 0); + memset_s(p, INDEX_MAX, 0, INDEX_MAX + 1); + PRINTK("p[0] = %d\n", p[0]); + memset_s(p, INDEX_MAX + 1, 0, INDEX_MAX + 1); /* trigger overflow */ + + return LOS_OK; +} + +/* LmsTestMemset_sOverflow */ +VOID ItLosLms020(void) +{ + TEST_ADD_CASE("ItLosLms020", TestCase, TEST_LOS, TEST_LMS, TEST_LEVEL1, TEST_FUNCTION); +} + diff --git a/testsuites/sample/kernel/lms/It_los_lms_021.c b/testsuites/sample/kernel/lms/It_los_lms_021.c new file mode 100644 index 00000000..dd6895e4 --- /dev/null +++ b/testsuites/sample/kernel/lms/It_los_lms_021.c @@ -0,0 +1,53 @@ +/* + * Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved. + * Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this list of + * conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this list + * of conditions and the following disclaimer in the documentation and/or other materials + * provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its contributors may be used + * to endorse or promote products derived from this software without specific prior written + * permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "osTest.h" +#include "It_los_lms.h" + +static UINT32 TestCase(VOID) +{ + CHAR src[INDEX_MAX + 1] = {0}; + CHAR *p = (CHAR *)LOS_MemAlloc(g_testLmsPool, INDEX_MAX); + ICUNIT_ASSERT_NOT_EQUAL(p, NULL, 0); + + memcpy_s(p, INDEX_MAX, src, INDEX_MAX + 1); + PRINTK("p[0] = %d\n", p[0]); + memcpy_s(p, INDEX_MAX + 1, 0, INDEX_MAX + 1); /* trigger overflow */ + + return LOS_OK; +} + +/* LmsTestMemcpy_sOverflow */ +VOID ItLosLms021(void) +{ + TEST_ADD_CASE("ItLosLms021", TestCase, TEST_LOS, TEST_LMS, TEST_LEVEL1, TEST_FUNCTION); +} + diff --git a/testsuites/sample/kernel/lms/It_los_lms_022.c b/testsuites/sample/kernel/lms/It_los_lms_022.c new file mode 100644 index 00000000..99f570b7 --- /dev/null +++ b/testsuites/sample/kernel/lms/It_los_lms_022.c @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved. + * Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this list of + * conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this list + * of conditions and the following disclaimer in the documentation and/or other materials + * provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its contributors may be used + * to endorse or promote products derived from this software without specific prior written + * permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "osTest.h" +#include "It_los_lms.h" + +static UINT32 TestCase(VOID) +{ + CHAR *p = (CHAR *)LOS_MemAlloc(g_testLmsPool, INDEX_MAX); + ICUNIT_ASSERT_NOT_EQUAL(p, NULL, 0); + + memmove_s(p, INDEX_MAX, 0, INDEX_MAX + 1); + PRINTK("p[0] = %d\n", p[0]); + memmove_s(p, INDEX_MAX + 1, 0, INDEX_MAX + 1); /* trigger overflow */ + + return LOS_OK; +} + +/* LmsTestMemmove_sOverflow */ +VOID ItLosLms022(void) +{ + TEST_ADD_CASE("ItLosLms022", TestCase, TEST_LOS, TEST_LMS, TEST_LEVEL1, TEST_FUNCTION); +} + diff --git a/testsuites/sample/kernel/lms/It_los_lms_023.c b/testsuites/sample/kernel/lms/It_los_lms_023.c new file mode 100644 index 00000000..310ee2c5 --- /dev/null +++ b/testsuites/sample/kernel/lms/It_los_lms_023.c @@ -0,0 +1,71 @@ +/* + * Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved. + * Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this list of + * conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this list + * of conditions and the following disclaimer in the documentation and/or other materials + * provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its contributors may be used + * to endorse or promote products derived from this software without specific prior written + * permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "osTest.h" +#include "It_los_lms.h" + +static UINT32 TestCase(VOID) +{ + CHAR *string = "LMS_TestCase"; + CHAR *src; + CHAR *buf; + CHAR *str; + UINT32 ret; + src = LOS_MemAlloc(g_testLmsPool, (strlen(string) + 1)); + ICUNIT_ASSERT_NOT_EQUAL(src, NULL, src); + PRINTK("%d\n", __LINE__); + (VOID)__memset(src, 0, (strlen(string) + 1)); + + PRINTK("%d\n", __LINE__); + str = __strcpy(src, string); + ICUNIT_ASSERT_NOT_EQUAL(str, NULL, str); + PRINTK("%d\n", __LINE__); + + buf = LOS_MemAlloc(g_testLmsPool, 8); /* mem size 8 */ + ICUNIT_ASSERT_NOT_EQUAL(buf, NULL, buf); + buf[7] = '\0'; /* end index 7 */ + PRINTK("%d\n", __LINE__); + ret = strcat_s(buf, 100, src); /* Check LMS detection information when the strcat dest max set 100 overflows. */ + PRINTK("%d\n", __LINE__); + ret = LOS_MemFree(g_testLmsPool, buf); + ICUNIT_ASSERT_NOT_EQUAL(ret, LOS_NOK, ret); + ret = LOS_MemFree(g_testLmsPool, src); + ICUNIT_ASSERT_NOT_EQUAL(ret, LOS_NOK, ret); + + return LOS_OK; +} + +/* LmsTestStrcat_sOverflow */ +VOID ItLosLms023(void) +{ + TEST_ADD_CASE("ItLosLms023", TestCase, TEST_LOS, TEST_LMS, TEST_LEVEL1, TEST_FUNCTION); +} + diff --git a/testsuites/sample/kernel/lms/It_los_lms_024.c b/testsuites/sample/kernel/lms/It_los_lms_024.c new file mode 100644 index 00000000..a2279638 --- /dev/null +++ b/testsuites/sample/kernel/lms/It_los_lms_024.c @@ -0,0 +1,71 @@ +/* + * Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved. + * Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this list of + * conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this list + * of conditions and the following disclaimer in the documentation and/or other materials + * provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its contributors may be used + * to endorse or promote products derived from this software without specific prior written + * permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "osTest.h" +#include "It_los_lms.h" + +static UINT32 TestCase(VOID) +{ + CHAR *string = "LMS_TestCase"; + CHAR *src; + CHAR *buf; + CHAR *str; + UINT32 ret; + src = LOS_MemAlloc(g_testLmsPool, strlen(string)); + ICUNIT_ASSERT_NOT_EQUAL(src, NULL, src); + (VOID)__memset(src, 0, strlen(string)); + + PRINTK("%d\n", __LINE__); + str = __strcpy(src, string); + ICUNIT_ASSERT_NOT_EQUAL(str, NULL, str); + PRINTK("%d\n", __LINE__); + + buf = LOS_MemAlloc(g_testLmsPool, 8); /* mem size 8 */ + ICUNIT_ASSERT_NOT_EQUAL(buf, NULL, buf); + buf[7] = '\0'; /* end index 7 */ + PRINTK("%d\n", __LINE__); + ret = strcpy_s(buf, 100, src); /* Check LMS detection information when the strcpy_s dest max set 100 overflows. */ + PRINTK("%d\n", __LINE__); + + ret = LOS_MemFree(g_testLmsPool, buf); + ICUNIT_ASSERT_NOT_EQUAL(ret, LOS_NOK, ret); + ret = LOS_MemFree(g_testLmsPool, src); + ICUNIT_ASSERT_NOT_EQUAL(ret, LOS_NOK, ret); + + return LOS_OK; +} + +/* LmsTestStrcpy_sOverflow */ +VOID ItLosLms024(void) +{ + TEST_ADD_CASE("ItLosLms024", TestCase, TEST_LOS, TEST_LMS, TEST_LEVEL1, TEST_FUNCTION); +} + diff --git a/testsuites/src/osTest.c b/testsuites/src/osTest.c index bd42741a..83c4832f 100644 --- a/testsuites/src/osTest.c +++ b/testsuites/src/osTest.c @@ -130,6 +130,10 @@ void TestKernel(void) ItSuiteLosDynlink(); #endif +#if (LOS_KERNEL_LMS_TEST == 1) + ItSuiteLosLms(); +#endif + #if (LOS_KERNEL_PM_TEST == 1) ItSuiteLosPm(); #endif