sample_usr_lms.c 8.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
/*
 * 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 <stdio.h>
#include <stdlib.h>
#include <string.h>

static void BufWriteTest(void *buf, int start, int end)
{
    for (int i = start; i <= end; i++) {
        ((char *)buf)[i] = 'a';
    }
}

static void BufReadTest(void *buf, int start, int end)
{
    char tmp;
    for (int i = start; i <= end; i++) {
        tmp = ((char *)buf)[i];
    }
}

static void LmsMallocTest(void)
{
Z
zhushengle 已提交
53
#define TEST_SIZE 16
L
LiteOS2021 已提交
54
    printf("\n-------- LmsMallocTest Start --------\n");
Z
zhushengle 已提交
55
    char *buf = (char *)malloc(TEST_SIZE);
W
wangchen 已提交
56 57 58
    if (buf == NULL) {
        return;
    }
Z
zhushengle 已提交
59 60 61 62
    printf("[LmsMallocTest] read overflow & underflow error should be triggered, read range[-1, TEST_SIZE]\n");
    BufReadTest(buf, -1, TEST_SIZE);
    printf("[LmsMallocTest] write overflow error should be triggered, write range[0, TEST_SIZE]\n");
    BufWriteTest(buf, 0, TEST_SIZE);
L
LiteOS2021 已提交
63 64 65 66 67 68 69

    free(buf);
    printf("\n-------- LmsMallocTest End --------\n");
}

static void LmsReallocTest(void)
{
Z
zhushengle 已提交
70 71
#define TEST_SIZE     64
#define TEST_SIZE_MIN 32
L
LiteOS2021 已提交
72
    printf("\n-------- LmsReallocTest Start --------\n");
Z
zhushengle 已提交
73 74 75 76
    char *buf = (char *)malloc(TEST_SIZE);
    printf("[LmsReallocTest] read overflow & underflow error should be triggered, read range[-1, TEST_SIZE]\n");
    BufReadTest(buf, -1, TEST_SIZE);
    char *buf1 = (char *)realloc(buf, TEST_SIZE_MIN);
Z
zhushengle 已提交
77 78 79 80 81
    if (buf1 == NULL) {
        free(buf);
        return;
    }
    buf = NULL;
Z
zhushengle 已提交
82 83
    printf("[LmsReallocTest] read overflow & underflow error should be triggered, read range[-1, TEST_SIZE_MIN]\n");
    BufReadTest(buf1, -1, TEST_SIZE_MIN);
Z
zhushengle 已提交
84
    free(buf1);
L
LiteOS2021 已提交
85 86 87 88 89
    printf("\n-------- LmsReallocTest End --------\n");
}

static void LmsCallocTest(void)
{
Z
zhushengle 已提交
90
#define TEST_SIZE 16
L
LiteOS2021 已提交
91
    printf("\n-------- LmsCallocTest Start --------\n");
Z
zhushengle 已提交
92
    char *buf = (char *)calloc(4, 4); /* 4: test size */
W
wangchen 已提交
93 94 95
    if (buf == NULL) {
        return;
    }
Z
zhushengle 已提交
96 97
    printf("[LmsCallocTest] read overflow & underflow error should be triggered, read range[-1, TEST_SIZE]\n");
    BufReadTest(buf, -1, TEST_SIZE);
L
LiteOS2021 已提交
98 99 100 101 102 103
    free(buf);
    printf("\n-------- LmsCallocTest End --------\n");
}

static void LmsVallocTest(void)
{
Z
zhushengle 已提交
104
#define TEST_SIZE 4096
L
LiteOS2021 已提交
105
    printf("\n-------- LmsVallocTest Start --------\n");
Z
zhushengle 已提交
106
    char *buf = (char *)valloc(TEST_SIZE);
W
wangchen 已提交
107 108 109
    if (buf == NULL) {
        return;
    }
Z
zhushengle 已提交
110 111
    printf("[LmsVallocTest] read overflow & underflow error should be triggered, read range[-1, TEST_SIZE]\n");
    BufReadTest(buf, -1, TEST_SIZE);
L
LiteOS2021 已提交
112 113 114 115 116 117
    free(buf);
    printf("\n-------- LmsVallocTest End --------\n");
}

static void LmsAlignedAllocTest(void)
{
Z
zhushengle 已提交
118 119
#define TEST_ALIGN_SIZE 64
#define TEST_SIZE       128
L
LiteOS2021 已提交
120
    printf("\n-------- LmsAlignedAllocTest Start --------\n");
Z
zhushengle 已提交
121
    char *buf = (char *)aligned_alloc(TEST_ALIGN_SIZE, TEST_SIZE);
W
wangchen 已提交
122 123 124
    if (buf == NULL) {
        return;
    }
L
LiteOS2021 已提交
125 126 127 128 129 130 131 132
    printf("[LmsAlignedAllocTest] read overflow & underflow error should be triggered, read range[-1,128]\n");
    BufReadTest(buf, -1, 128);
    free(buf);
    printf("\n-------- LmsAlignedAllocTest End --------\n");
}

static void LmsMemsetTest(void)
{
Z
zhushengle 已提交
133
#define TEST_SIZE 32
L
LiteOS2021 已提交
134
    printf("\n-------- LmsMemsetTest Start --------\n");
Z
zhushengle 已提交
135
    char *buf = (char *)malloc(TEST_SIZE);
W
wangchen 已提交
136 137 138
    if (buf == NULL) {
        return;
    }
Z
zhushengle 已提交
139 140
    printf("[LmsMemsetTest] memset overflow & underflow error should be triggered, memset size:%d\n", TEST_SIZE + 1);
    memset(buf, 0, TEST_SIZE + 1);
L
LiteOS2021 已提交
141 142 143 144 145 146
    free(buf);
    printf("\n-------- LmsMemsetTest End --------\n");
}

static void LmsMemcpyTest(void)
{
Z
zhushengle 已提交
147
#define TEST_SIZE 20
L
LiteOS2021 已提交
148
    printf("\n-------- LmsMemcpyTest Start --------\n");
Z
zhushengle 已提交
149
    char *buf = (char *)malloc(TEST_SIZE);
W
wangchen 已提交
150 151 152
    if (buf == NULL) {
        return;
    }
Z
zhushengle 已提交
153 154 155
    char localBuf[32] = {0}; /* 32: test size */
    printf("[LmsMemcpyTest] memcpy overflow error should be triggered, memcpy size:%d\n", TEST_SIZE + 1);
    memcpy(buf, localBuf, TEST_SIZE + 1);
L
LiteOS2021 已提交
156 157 158 159 160 161
    free(buf);
    printf("\n-------- LmsMemcpyTest End --------\n");
}

static void LmsMemmoveTest(void)
{
Z
zhushengle 已提交
162
#define TEST_SIZE 20
L
LiteOS2021 已提交
163
    printf("\n-------- LmsMemmoveTest Start --------\n");
Z
zhushengle 已提交
164
    char *buf = (char *)malloc(TEST_SIZE);
W
wangchen 已提交
165 166 167
    if (buf == NULL) {
        return;
    }
Z
zhushengle 已提交
168 169
    printf("[LmsMemmoveTest] memmove overflow error should be triggered\n");
    memmove(buf + 12, buf, 10); /* 12 and 10: test size */
L
LiteOS2021 已提交
170 171 172 173 174 175
    free(buf);
    printf("\n-------- LmsMemmoveTest End --------\n");
}

static void LmsStrcpyTest(void)
{
Z
zhushengle 已提交
176
#define TEST_SIZE 16
L
LiteOS2021 已提交
177
    printf("\n-------- LmsStrcpyTest Start --------\n");
Z
zhushengle 已提交
178
    char *buf = (char *)malloc(TEST_SIZE);
W
wangchen 已提交
179 180 181
    if (buf == NULL) {
        return;
    }
L
LiteOS2021 已提交
182
    char *testStr = "bbbbbbbbbbbbbbbbb";
183 184
    printf("[LmsStrcpyTest] strcpy overflow error should be triggered, src string buf size:%d\n",
           (int)strlen(testStr) + 1);
L
LiteOS2021 已提交
185 186 187 188 189 190 191
    strcpy(buf, testStr);
    free(buf);
    printf("\n-------- LmsStrcpyTest End --------\n");
}

static void LmsStrcatTest(void)
{
Z
zhushengle 已提交
192
#define TEST_SIZE 16
L
LiteOS2021 已提交
193
    printf("\n-------- LmsStrcatTest Start --------\n");
Z
zhushengle 已提交
194
    char *buf = (char *)malloc(TEST_SIZE);
W
wangchen 已提交
195 196 197
    if (buf == NULL) {
        return;
    }
L
LiteOS2021 已提交
198 199 200 201 202 203 204 205 206 207 208 209 210 211
    buf[0] = 'a';
    buf[1] = 'b';
    buf[2] = 0;
    char *testStr = "cccccccccccccc";
    printf("[LmsStrcatTest] strcat overflow error should be triggered, src string:%s dest string:%s"
        "total buf size:%d\n",
        testStr, buf, strlen(testStr) + strlen(buf) + 1);
    strcat(buf, testStr);
    free(buf);
    printf("\n-------- LmsStrcatTest End --------\n");
}

static void LmsFreeTest(void)
{
Z
zhushengle 已提交
212
#define TEST_SIZE 16
L
LiteOS2021 已提交
213
    printf("\n-------- LmsFreeTest Start --------\n");
Z
zhushengle 已提交
214
    char *buf = (char *)malloc(TEST_SIZE);
W
wangchen 已提交
215 216 217
    if (buf == NULL) {
        return;
    }
Z
zhushengle 已提交
218
    printf("[LmsFreeTest] free size:%d\n", TEST_SIZE);
L
LiteOS2021 已提交
219
    free(buf);
Z
zhushengle 已提交
220
    printf("[LmsFreeTest] Use after free error should be triggered, read range[1,1]\n");
L
LiteOS2021 已提交
221
    BufReadTest(buf, 1, 1);
Z
zhushengle 已提交
222
    printf("[LmsFreeTest] double free error should be triggered\n");
L
LiteOS2021 已提交
223 224 225 226
    free(buf);
    printf("\n-------- LmsFreeTest End --------\n");
}

Z
zhushengle 已提交
227
int main(int argc, char * const *argv)
L
LiteOS2021 已提交
228
{
Z
zhushengle 已提交
229 230
    (void)argc;
    (void)argv;
L
LiteOS2021 已提交
231
    printf("\n############### Lms Test start ###############\n");
Z
zhushengle 已提交
232
    char *tmp = (char *)malloc(5000); /* 5000: test mem size */
W
wangchen 已提交
233 234 235
    if (tmp == NULL) {
        return;
    }
L
LiteOS2021 已提交
236 237 238 239 240 241 242 243 244 245 246
    LmsMallocTest();
    LmsReallocTest();
    LmsCallocTest();
    LmsVallocTest();
    LmsAlignedAllocTest();
    LmsMemsetTest();
    LmsMemcpyTest();
    LmsMemmoveTest();
    LmsStrcpyTest();
    LmsStrcatTest();
    LmsFreeTest();
W
wangchen 已提交
247
    free(tmp);
L
LiteOS2021 已提交
248
    printf("\n############### Lms Test End ###############\n");
Z
zhushengle 已提交
249
}