xref: /kernel/liteos_a/apps/lms/src/sample_usr_lms.c (revision 0d163575)
1/*
2 * Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
3 * Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without modification,
6 * are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice, this list of
9 * conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
12 * of conditions and the following disclaimer in the documentation and/or other materials
13 * provided with the distribution.
14 *
15 * 3. Neither the name of the copyright holder nor the names of its contributors may be used
16 * to endorse or promote products derived from this software without specific prior written
17 * permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
26 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
29 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31
32#include <stdio.h>
33#include <stdlib.h>
34#include <string.h>
35
36static void BufWriteTest(void *buf, int start, int end)
37{
38    for (int i = start; i <= end; i++) {
39        ((char *)buf)[i] = 'a';
40    }
41}
42
43static void BufReadTest(void *buf, int start, int end)
44{
45    char tmp;
46    for (int i = start; i <= end; i++) {
47        tmp = ((char *)buf)[i];
48    }
49}
50
51static void LmsMallocTest(void)
52{
53#define TEST_SIZE 16
54    printf("\n-------- LmsMallocTest Start --------\n");
55    char *buf = (char *)malloc(TEST_SIZE);
56    if (buf == NULL) {
57        return;
58    }
59    printf("[LmsMallocTest] read overflow & underflow error should be triggered, read range[-1, TEST_SIZE]\n");
60    BufReadTest(buf, -1, TEST_SIZE);
61    printf("[LmsMallocTest] write overflow error should be triggered, write range[0, TEST_SIZE]\n");
62    BufWriteTest(buf, 0, TEST_SIZE);
63
64    free(buf);
65    printf("\n-------- LmsMallocTest End --------\n");
66}
67
68static void LmsReallocTest(void)
69{
70#define TEST_SIZE     64
71#define TEST_SIZE_MIN 32
72    printf("\n-------- LmsReallocTest Start --------\n");
73    char *buf = (char *)malloc(TEST_SIZE);
74    printf("[LmsReallocTest] read overflow & underflow error should be triggered, read range[-1, TEST_SIZE]\n");
75    BufReadTest(buf, -1, TEST_SIZE);
76    char *buf1 = (char *)realloc(buf, TEST_SIZE_MIN);
77    if (buf1 == NULL) {
78        free(buf);
79        return;
80    }
81    buf = NULL;
82    printf("[LmsReallocTest] read overflow & underflow error should be triggered, read range[-1, TEST_SIZE_MIN]\n");
83    BufReadTest(buf1, -1, TEST_SIZE_MIN);
84    free(buf1);
85    printf("\n-------- LmsReallocTest End --------\n");
86}
87
88static void LmsCallocTest(void)
89{
90#define TEST_SIZE 16
91    printf("\n-------- LmsCallocTest Start --------\n");
92    char *buf = (char *)calloc(4, 4); /* 4: test size */
93    if (buf == NULL) {
94        return;
95    }
96    printf("[LmsCallocTest] read overflow & underflow error should be triggered, read range[-1, TEST_SIZE]\n");
97    BufReadTest(buf, -1, TEST_SIZE);
98    free(buf);
99    printf("\n-------- LmsCallocTest End --------\n");
100}
101
102static void LmsVallocTest(void)
103{
104#define TEST_SIZE 4096
105    printf("\n-------- LmsVallocTest Start --------\n");
106    char *buf = (char *)valloc(TEST_SIZE);
107    if (buf == NULL) {
108        return;
109    }
110    printf("[LmsVallocTest] read overflow & underflow error should be triggered, read range[-1, TEST_SIZE]\n");
111    BufReadTest(buf, -1, TEST_SIZE);
112    free(buf);
113    printf("\n-------- LmsVallocTest End --------\n");
114}
115
116static void LmsAlignedAllocTest(void)
117{
118#define TEST_ALIGN_SIZE 64
119#define TEST_SIZE       128
120    printf("\n-------- LmsAlignedAllocTest Start --------\n");
121    char *buf = (char *)aligned_alloc(TEST_ALIGN_SIZE, TEST_SIZE);
122    if (buf == NULL) {
123        return;
124    }
125    printf("[LmsAlignedAllocTest] read overflow & underflow error should be triggered, read range[-1,128]\n");
126    BufReadTest(buf, -1, 128);
127    free(buf);
128    printf("\n-------- LmsAlignedAllocTest End --------\n");
129}
130
131static void LmsMemsetTest(void)
132{
133#define TEST_SIZE 32
134    printf("\n-------- LmsMemsetTest Start --------\n");
135    char *buf = (char *)malloc(TEST_SIZE);
136    if (buf == NULL) {
137        return;
138    }
139    printf("[LmsMemsetTest] memset overflow & underflow error should be triggered, memset size:%d\n", TEST_SIZE + 1);
140    memset(buf, 0, TEST_SIZE + 1);
141    free(buf);
142    printf("\n-------- LmsMemsetTest End --------\n");
143}
144
145static void LmsMemcpyTest(void)
146{
147#define TEST_SIZE 20
148    printf("\n-------- LmsMemcpyTest Start --------\n");
149    char *buf = (char *)malloc(TEST_SIZE);
150    if (buf == NULL) {
151        return;
152    }
153    char localBuf[32] = {0}; /* 32: test size */
154    printf("[LmsMemcpyTest] memcpy overflow error should be triggered, memcpy size:%d\n", TEST_SIZE + 1);
155    memcpy(buf, localBuf, TEST_SIZE + 1);
156    free(buf);
157    printf("\n-------- LmsMemcpyTest End --------\n");
158}
159
160static void LmsMemmoveTest(void)
161{
162#define TEST_SIZE 20
163    printf("\n-------- LmsMemmoveTest Start --------\n");
164    char *buf = (char *)malloc(TEST_SIZE);
165    if (buf == NULL) {
166        return;
167    }
168    printf("[LmsMemmoveTest] memmove overflow error should be triggered\n");
169    memmove(buf + 12, buf, 10); /* 12 and 10: test size */
170    free(buf);
171    printf("\n-------- LmsMemmoveTest End --------\n");
172}
173
174static void LmsStrcpyTest(void)
175{
176#define TEST_SIZE 16
177    printf("\n-------- LmsStrcpyTest Start --------\n");
178    char *buf = (char *)malloc(TEST_SIZE);
179    if (buf == NULL) {
180        return;
181    }
182    char *testStr = "bbbbbbbbbbbbbbbbb";
183    printf("[LmsStrcpyTest] strcpy overflow error should be triggered, src string buf size:%d\n",
184           (int)strlen(testStr) + 1);
185    strcpy(buf, testStr);
186    free(buf);
187    printf("\n-------- LmsStrcpyTest End --------\n");
188}
189
190static void LmsStrcatTest(void)
191{
192#define TEST_SIZE 16
193    printf("\n-------- LmsStrcatTest Start --------\n");
194    char *buf = (char *)malloc(TEST_SIZE);
195    if (buf == NULL) {
196        return;
197    }
198    buf[0] = 'a';
199    buf[1] = 'b';
200    buf[2] = 0;
201    char *testStr = "cccccccccccccc";
202    printf("[LmsStrcatTest] strcat overflow error should be triggered, src string:%s dest string:%s"
203        "total buf size:%d\n",
204        testStr, buf, strlen(testStr) + strlen(buf) + 1);
205    strcat(buf, testStr);
206    free(buf);
207    printf("\n-------- LmsStrcatTest End --------\n");
208}
209
210static void LmsFreeTest(void)
211{
212#define TEST_SIZE 16
213    printf("\n-------- LmsFreeTest Start --------\n");
214    char *buf = (char *)malloc(TEST_SIZE);
215    if (buf == NULL) {
216        return;
217    }
218    printf("[LmsFreeTest] free size:%d\n", TEST_SIZE);
219    free(buf);
220    printf("[LmsFreeTest] Use after free error should be triggered, read range[1,1]\n");
221    BufReadTest(buf, 1, 1);
222    printf("[LmsFreeTest] double free error should be triggered\n");
223    free(buf);
224    printf("\n-------- LmsFreeTest End --------\n");
225}
226
227int main(int argc, char * const *argv)
228{
229    (void)argc;
230    (void)argv;
231    printf("\n############### Lms Test start ###############\n");
232    char *tmp = (char *)malloc(5000); /* 5000: test mem size */
233    if (tmp == NULL) {
234        return -1;
235    }
236    LmsMallocTest();
237    LmsReallocTest();
238    LmsCallocTest();
239    LmsVallocTest();
240    LmsAlignedAllocTest();
241    LmsMemsetTest();
242    LmsMemcpyTest();
243    LmsMemmoveTest();
244    LmsStrcpyTest();
245    LmsStrcatTest();
246    LmsFreeTest();
247    free(tmp);
248    printf("\n############### Lms Test End ###############\n");
249    return 0;
250}
251