sample_usr_lms.c 7.6 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);
L
LiteOS2021 已提交
56

Z
zhushengle 已提交
57 58 59 60
    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 已提交
61 62 63 64 65 66 67

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

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

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

static void LmsVallocTest(void)
{
Z
zhushengle 已提交
99
#define TEST_SIZE 4096
L
LiteOS2021 已提交
100
    printf("\n-------- LmsVallocTest Start --------\n");
Z
zhushengle 已提交
101 102 103
    char *buf = (char *)valloc(TEST_SIZE);
    printf("[LmsVallocTest] read overflow & underflow error should be triggered, read range[-1, TEST_SIZE]\n");
    BufReadTest(buf, -1, TEST_SIZE);
L
LiteOS2021 已提交
104 105 106 107 108 109
    free(buf);
    printf("\n-------- LmsVallocTest End --------\n");
}

static void LmsAlignedAllocTest(void)
{
Z
zhushengle 已提交
110 111
#define TEST_ALIGN_SIZE 64
#define TEST_SIZE       128
L
LiteOS2021 已提交
112
    printf("\n-------- LmsAlignedAllocTest Start --------\n");
Z
zhushengle 已提交
113
    char *buf = (char *)aligned_alloc(TEST_ALIGN_SIZE, TEST_SIZE);
L
LiteOS2021 已提交
114 115 116 117 118 119 120 121
    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 已提交
122
#define TEST_SIZE 32
L
LiteOS2021 已提交
123
    printf("\n-------- LmsMemsetTest Start --------\n");
Z
zhushengle 已提交
124 125 126
    char *buf = (char *)malloc(TEST_SIZE);
    printf("[LmsMemsetTest] memset overflow & underflow error should be triggered, memset size:%d\n", TEST_SIZE + 1);
    memset(buf, 0, TEST_SIZE + 1);
L
LiteOS2021 已提交
127 128 129 130 131 132
    free(buf);
    printf("\n-------- LmsMemsetTest End --------\n");
}

static void LmsMemcpyTest(void)
{
Z
zhushengle 已提交
133
#define TEST_SIZE 20
L
LiteOS2021 已提交
134
    printf("\n-------- LmsMemcpyTest Start --------\n");
Z
zhushengle 已提交
135 136 137 138
    char *buf = (char *)malloc(TEST_SIZE);
    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 已提交
139 140 141 142 143 144
    free(buf);
    printf("\n-------- LmsMemcpyTest End --------\n");
}

static void LmsMemmoveTest(void)
{
Z
zhushengle 已提交
145
#define TEST_SIZE 20
L
LiteOS2021 已提交
146
    printf("\n-------- LmsMemmoveTest Start --------\n");
Z
zhushengle 已提交
147 148 149
    char *buf = (char *)malloc(TEST_SIZE);
    printf("[LmsMemmoveTest] memmove overflow error should be triggered\n");
    memmove(buf + 12, buf, 10); /* 12 and 10: test size */
L
LiteOS2021 已提交
150 151 152 153 154 155
    free(buf);
    printf("\n-------- LmsMemmoveTest End --------\n");
}

static void LmsStrcpyTest(void)
{
Z
zhushengle 已提交
156
#define TEST_SIZE 16
L
LiteOS2021 已提交
157
    printf("\n-------- LmsStrcpyTest Start --------\n");
Z
zhushengle 已提交
158
    char *buf = (char *)malloc(TEST_SIZE);
L
LiteOS2021 已提交
159 160 161 162 163 164 165 166 167
    char *testStr = "bbbbbbbbbbbbbbbbb";
    printf("[LmsStrcpyTest] strcpy overflow error should be triggered, src string buf size:%d\n", strlen(testStr) + 1);
    strcpy(buf, testStr);
    free(buf);
    printf("\n-------- LmsStrcpyTest End --------\n");
}

static void LmsStrcatTest(void)
{
Z
zhushengle 已提交
168
#define TEST_SIZE 16
L
LiteOS2021 已提交
169
    printf("\n-------- LmsStrcatTest Start --------\n");
Z
zhushengle 已提交
170
    char *buf = (char *)malloc(TEST_SIZE);
L
LiteOS2021 已提交
171 172 173 174 175 176 177 178 179 180 181 182 183 184
    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 已提交
185
#define TEST_SIZE 16
L
LiteOS2021 已提交
186
    printf("\n-------- LmsFreeTest Start --------\n");
Z
zhushengle 已提交
187 188
    char *buf = (char *)malloc(TEST_SIZE);
    printf("[LmsFreeTest] free size:%d\n", TEST_SIZE);
L
LiteOS2021 已提交
189
    free(buf);
Z
zhushengle 已提交
190
    printf("[LmsFreeTest] Use after free error should be triggered, read range[1,1]\n");
L
LiteOS2021 已提交
191
    BufReadTest(buf, 1, 1);
Z
zhushengle 已提交
192
    printf("[LmsFreeTest] double free error should be triggered\n");
L
LiteOS2021 已提交
193 194 195 196
    free(buf);
    printf("\n-------- LmsFreeTest End --------\n");
}

Z
zhushengle 已提交
197
int main(int argc, char * const *argv)
L
LiteOS2021 已提交
198
{
Z
zhushengle 已提交
199 200
    (void)argc;
    (void)argv;
L
LiteOS2021 已提交
201
    printf("\n############### Lms Test start ###############\n");
Z
zhushengle 已提交
202
    char *tmp = (char *)malloc(5000); /* 5000: test mem size */
L
LiteOS2021 已提交
203 204 205 206 207 208 209 210 211 212 213
    LmsMallocTest();
    LmsReallocTest();
    LmsCallocTest();
    LmsVallocTest();
    LmsAlignedAllocTest();
    LmsMemsetTest();
    LmsMemcpyTest();
    LmsMemmoveTest();
    LmsStrcpyTest();
    LmsStrcatTest();
    LmsFreeTest();
W
wangchen 已提交
214
    free(tmp);
L
LiteOS2021 已提交
215
    printf("\n############### Lms Test End ###############\n");
Z
zhushengle 已提交
216
}