• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *   http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include <malloc.h>
17 #include <functionalext.h>
18 #include <stdio.h>
19 #include <sys/mman.h>
20 
21 #define TEST_MEM_SIZE 256
22 #define NO_JEMALLOC_ZONE 0
23 #define JEMALLOC_ZONE 1
24 #define UN_JEMALLOC (-1)
25 #define NUM_MALLOCS 256
26 
27 #define EXPECT_EQ_ONE_OF(fun, val, exp1, exp2) do { \
28     if (((val) != (exp1)) && ((val) != (exp2))) { \
29         t_error("[%s] failed: %d != %d and %d != %d\n", \
30                 #fun, (int)(val), (int)(exp1), (int)(val), (int)(exp2)); \
31     } \
32 } while (0)
33 
free_ptrs_range(void ** ptrs,int count)34 static void free_ptrs_range(void **ptrs, int count)
35 {
36     if (!ptrs || count <= 0) {
37         return;
38     }
39 
40     for (int i = 0; i < count; ++i) {
41         if (ptrs[i]) {
42             free(ptrs[i]);
43             ptrs[i] = NULL;
44         }
45     }
46 }
47 
48 /**
49  * @tc.name      : malloc_check_from_ptr_0100
50  * @tc.desc      : The memory block was allocated using malloc
51  * @tc.level     : Level 0
52  */
malloc_check_from_ptr_0100(void)53 void malloc_check_from_ptr_0100(void)
54 {
55     void *p = malloc(TEST_MEM_SIZE);
56     EXPECT_PTRNE("malloc_check_from_ptr_0100", p, NULL);
57     if (!p) {
58         return;
59     }
60     int ret = malloc_check_from_ptr(p);
61     EXPECT_EQ_ONE_OF("malloc_check_from_ptr_0100", ret, JEMALLOC_ZONE, UN_JEMALLOC);
62     free(p);
63 }
64 
65 /**
66  * @tc.name      : malloc_check_from_ptr_0200
67  * @tc.desc      : The memory block was allocated not using malloc
68  * @tc.level     : Level 0
69  */
malloc_check_from_ptr_0200(void)70 void malloc_check_from_ptr_0200(void)
71 {
72     void *p = mmap(NULL, TEST_MEM_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, -1, 0);
73     EXPECT_PTRNE("malloc_check_from_ptr_0200", p, NULL);
74     if (!p) {
75         return;
76     }
77     int ret = malloc_check_from_ptr(p);
78     EXPECT_EQ_ONE_OF("malloc_check_from_ptr_0200", ret, NO_JEMALLOC_ZONE, UN_JEMALLOC);
79     munmap(p, TEST_MEM_SIZE);
80 }
81 
82 /**
83  * @tc.name      : malloc_check_from_ptr_0300
84  * @tc.desc      : The memory block was allocated using calloc
85  * @tc.level     : Level 0
86  */
malloc_check_from_ptr_0300(void)87 void malloc_check_from_ptr_0300(void)
88 {
89     void *p = calloc(1, TEST_MEM_SIZE);
90     EXPECT_PTRNE("malloc_check_from_ptr_0300", p, NULL);
91     if (!p) {
92         return;
93     }
94     int ret = malloc_check_from_ptr(p);
95     EXPECT_EQ_ONE_OF("malloc_check_from_ptr_0300", ret, JEMALLOC_ZONE, UN_JEMALLOC);
96     free(p);
97 }
98 
99 /**
100  * @tc.name      : malloc_check_from_ptr_0400
101  * @tc.desc      : multiple test malloc_check_from_ptr
102  * @tc.level     : Level 0
103  */
malloc_check_from_ptr_0400(void)104 void malloc_check_from_ptr_0400(void)
105 {
106     void *ptrs[NUM_MALLOCS];
107 
108     // multiple malloc
109     for (int i = 0; i < NUM_MALLOCS; ++i) {
110         ptrs[i] = malloc(TEST_MEM_SIZE);
111         if (!ptrs[i]) {
112             free_ptrs_range(ptrs, i);
113             return;
114         }
115     }
116 
117     // malloc_check_from_ptr in ptrs
118     for (int i = 0; i < NUM_MALLOCS; ++i) {
119         int ret = malloc_check_from_ptr(ptrs[i]);
120         EXPECT_EQ_ONE_OF("malloc_check_from_ptr_0400", ret, JEMALLOC_ZONE, UN_JEMALLOC);
121     }
122 
123     // free all
124     free_ptrs_range(ptrs, NUM_MALLOCS);
125 }
126 
main(void)127 int main(void)
128 {
129     malloc_check_from_ptr_0100();
130     malloc_check_from_ptr_0200();
131     malloc_check_from_ptr_0300();
132     malloc_check_from_ptr_0400();
133     return t_status;
134 }