• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
BufWriteTest(void * buf,int start,int end)36 static 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 
BufReadTest(void * buf,int start,int end)43 static 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 
LmsMallocTest(void)51 static 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 
LmsReallocTest(void)68 static 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 
LmsCallocTest(void)88 static 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 
LmsVallocTest(void)102 static 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 
LmsAlignedAllocTest(void)116 static 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 
LmsMemsetTest(void)131 static 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 
LmsMemcpyTest(void)145 static 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 
LmsMemmoveTest(void)160 static 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 
LmsStrcpyTest(void)174 static 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", strlen(testStr) + 1);
184     strcpy(buf, testStr);
185     free(buf);
186     printf("\n-------- LmsStrcpyTest End --------\n");
187 }
188 
LmsStrcatTest(void)189 static void LmsStrcatTest(void)
190 {
191 #define TEST_SIZE 16
192     printf("\n-------- LmsStrcatTest Start --------\n");
193     char *buf = (char *)malloc(TEST_SIZE);
194     if (buf == NULL) {
195         return;
196     }
197     buf[0] = 'a';
198     buf[1] = 'b';
199     buf[2] = 0;
200     char *testStr = "cccccccccccccc";
201     printf("[LmsStrcatTest] strcat overflow error should be triggered, src string:%s dest string:%s"
202         "total buf size:%d\n",
203         testStr, buf, strlen(testStr) + strlen(buf) + 1);
204     strcat(buf, testStr);
205     free(buf);
206     printf("\n-------- LmsStrcatTest End --------\n");
207 }
208 
LmsFreeTest(void)209 static void LmsFreeTest(void)
210 {
211 #define TEST_SIZE 16
212     printf("\n-------- LmsFreeTest Start --------\n");
213     char *buf = (char *)malloc(TEST_SIZE);
214     if (buf == NULL) {
215         return;
216     }
217     printf("[LmsFreeTest] free size:%d\n", TEST_SIZE);
218     free(buf);
219     printf("[LmsFreeTest] Use after free error should be triggered, read range[1,1]\n");
220     BufReadTest(buf, 1, 1);
221     printf("[LmsFreeTest] double free error should be triggered\n");
222     free(buf);
223     printf("\n-------- LmsFreeTest End --------\n");
224 }
225 
main(int argc,char * const * argv)226 int main(int argc, char * const *argv)
227 {
228     (void)argc;
229     (void)argv;
230     printf("\n############### Lms Test start ###############\n");
231     char *tmp = (char *)malloc(5000); /* 5000: test mem size */
232     if (tmp == NULL) {
233         return;
234     }
235     LmsMallocTest();
236     LmsReallocTest();
237     LmsCallocTest();
238     LmsVallocTest();
239     LmsAlignedAllocTest();
240     LmsMemsetTest();
241     LmsMemcpyTest();
242     LmsMemmoveTest();
243     LmsStrcpyTest();
244     LmsStrcatTest();
245     LmsFreeTest();
246     free(tmp);
247     printf("\n############### Lms Test End ###############\n");
248 }
249