los_lmslibc.c 6.1 KB
Newer Older
L
LiteOS2021 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
/*
 * 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"

ATTRIBUTE_NO_SANITIZE_ADDRESS void LmsFree(void *ptr)
{
    if (ptr == NULL) {
        return;
    }

    size_t allocSize = malloc_usable_size(ptr);
    uintptr_t shadowAddr, offset;
    LmsMem2Shadow((uintptr_t)ptr, &shadowAddr, &offset);

    LmsLock(&g_lmsMutex);
    if (LmsIsShadowAddrMapped(shadowAddr, shadowAddr) == LMS_OK) {
        uint32_t acShadowValue;
        LmsGetShadowValue((uintptr_t)ptr, &acShadowValue);
        if (acShadowValue != LMS_SHADOW_ACCESSABLE) {
            char erroMode = (acShadowValue == LMS_SHADOW_AFTERFREE ? FREE_ERRORMODE : UNKNOWN_ERROR);
            LmsReportError((uintptr_t)ptr, MEM_REGION_SIZE_1, erroMode);
            goto UNLOCK_OUT;
        }
    } else {
        LMS_OUTPUT_ERROR("Error! Free an unallocated memory:%p!\n", ptr);
        goto UNLOCK_OUT;
    }

    __real_free(ptr);
    LmsSetShadowValue((uintptr_t)ptr, (uintptr_t)ptr + allocSize, LMS_SHADOW_AFTERFREE_U8);

UNLOCK_OUT:
    LmsUnlock(&g_lmsMutex);
}

ATTRIBUTE_NO_SANITIZE_ADDRESS void *LmsMalloc(size_t size)
{
    void *ptr = __real_malloc(size);
    if (ptr == NULL) {
        return ptr;
    }
    return LmsTagMem(ptr, size);
}

ATTRIBUTE_NO_SANITIZE_ADDRESS void *LmsRealloc(void *ptr, size_t size)
{
    void *p = __real_realloc(ptr, size);
    if (p == NULL) {
        return p;
    }
    return LmsTagMem(p, size);
}

ATTRIBUTE_NO_SANITIZE_ADDRESS void *LmsCalloc(size_t m, size_t n)
{
    void *p = __real_calloc(m, n);
    if (p == NULL) {
        return p;
    }
    return LmsTagMem(p, n * m);
}

ATTRIBUTE_NO_SANITIZE_ADDRESS void *LmsValloc(size_t size)
{
    void *p = __real_valloc(size);
    if (p == NULL) {
        return p;
    }
    return LmsTagMem(p, size);
}

ATTRIBUTE_NO_SANITIZE_ADDRESS void *LmsAlignedAlloc(size_t align, size_t len)
{
    void *p = __real_aligned_alloc(align, len);
    if (p == NULL) {
        return p;
    }
    return LmsTagMem(p, len);
}

ATTRIBUTE_NO_SANITIZE_ADDRESS void *__wrap_aligned_alloc(size_t align, size_t len)
{
    return LmsAlignedAlloc(align, len);
}

ATTRIBUTE_NO_SANITIZE_ADDRESS void *__wrap_valloc(size_t size)
{
    return LmsValloc(size);
}

ATTRIBUTE_NO_SANITIZE_ADDRESS void *__wrap_calloc(size_t m, size_t n)
{
    return LmsCalloc(m, n);
}

ATTRIBUTE_NO_SANITIZE_ADDRESS void *__wrap_realloc(void *p, size_t n)
{
    return LmsRealloc(p, n);
}

ATTRIBUTE_NO_SANITIZE_ADDRESS void *__wrap_malloc(size_t size)
{
    return LmsMalloc(size);
}

ATTRIBUTE_NO_SANITIZE_ADDRESS void __wrap_free(void *ptr)
{
    return LmsFree(ptr);
}

ATTRIBUTE_NO_SANITIZE_ADDRESS void *LmsMemset(void *p, int n, size_t size)
{
    __asan_storeN_noabort((uintptr_t)p, size);

    return __real_memset(p, n, size);
}

ATTRIBUTE_NO_SANITIZE_ADDRESS void *__wrap_memset(void *p, int n, size_t size)
{
    return LmsMemset(p, n, size);
}

ATTRIBUTE_NO_SANITIZE_ADDRESS void *LmsMemcpy(void *__restrict dest, const void *__restrict src, size_t size)
{
    __asan_loadN_noabort((uintptr_t)src, size);
    __asan_storeN_noabort((uintptr_t)dest, size);

    return __real_memcpy(dest, src, size);
}

ATTRIBUTE_NO_SANITIZE_ADDRESS void *__wrap_memcpy(void *__restrict dest, const void *__restrict src, size_t size)
{
    return LmsMemcpy(dest, src, size);
}

ATTRIBUTE_NO_SANITIZE_ADDRESS void *LmsMemmove(void *dest, const void *src, size_t len)
{
    __asan_loadN_noabort((uintptr_t)src, len);
    __asan_storeN_noabort((uintptr_t)dest, len);

    return __real_memmove(dest, src, len);
}

ATTRIBUTE_NO_SANITIZE_ADDRESS void *__wrap_memmove(void *dest, const void *src, size_t len)
{
    return LmsMemmove(dest, src, len);
}

ATTRIBUTE_NO_SANITIZE_ADDRESS char *LmsStrcat(char *s, const char *append)
{
    if ((s == NULL) || (append == NULL)) {
        return NULL;
    }

    char *save = s;
    for (; *s != '\0'; ++s) {
    }
    LmsCheckValid(s, append);

    return __real_strcat(save, append);
}

ATTRIBUTE_NO_SANITIZE_ADDRESS char *__wrap_strcat(char *s, const char *append)
{
    return LmsStrcat(s, append);
}

ATTRIBUTE_NO_SANITIZE_ADDRESS char *LmsStrcpy(char *dest, const char *src)
{
    if ((dest == NULL) || (src == NULL)) {
        return NULL;
    }

    LmsCheckValid(dest, src);
    return __real_strcpy(dest, src);
}

ATTRIBUTE_NO_SANITIZE_ADDRESS char *__wrap_strcpy(char *dest, const char *src)
{
    return LmsStrcpy(dest, src);
}