• 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 <cstdlib>
17 #include <unistd.h>
18 #include <sys/wait.h>
19 #include <csignal>
20 #include <cerrno>
21 #include <cstring>
22 #include <gtest/gtest.h>
23 
24 #define UNIT            16
25 #define OFF_OFFSET      2
26 #define FIRST_OFFSET    -4
27 #define FIRST_OFF_OFFSET 8
28 #define MALLOC_SIZE_S   40
29 #define MALLOC_SIZE_L   500
30 #define TEST_NUM        512
31 
32 using namespace testing::ext;
33 
34 class MallocSafeUnlink : public testing::Test {
35 public:
36 
37     static void SetUpTestCase();
38     static void TearDownTestCase();
39     void SetUp();
40     void TearDown();
41 private:
42 };
43 
SetUp()44 void MallocSafeUnlink::SetUp()
45 {
46 }
TearDown()47 void MallocSafeUnlink::TearDown()
48 {
49 }
SetUpTestCase()50 void MallocSafeUnlink::SetUpTestCase()
51 {
52 }
TearDownTestCase()53 void MallocSafeUnlink::TearDownTestCase()
54 {
55 }
56 
57 struct meta_in {
58     struct meta_in *prev, *next;
59     uintptr_t *mem;
60 };
61 
62 struct group_in {
63     struct meta_in *meta;
64 };
65 
get_group(volatile const uint8_t * p)66 static struct group_in *get_group(volatile const uint8_t *p)
67 {
68     int offset = *(const uint16_t *)((uintptr_t)p - OFF_OFFSET);
69 
70     if (p[FIRST_OFFSET]) {
71         offset = *(uintptr_t *)((uintptr_t)p - FIRST_OFF_OFFSET);
72     }
73 
74     struct group_in *base = (struct group_in *)((uintptr_t)p - UNIT*offset - UNIT);
75     return base;
76 }
77 
handler(int s)78 static void handler(int s)
79 {
80 }
81 
82 volatile void *tmp;
83 
set_devide_chunk(size_t size)84 int set_devide_chunk(size_t size)
85 {
86     if (!(tmp = malloc(size))) {
87         printf("Malloc failed: %s\n", strerror(errno));
88         return -1;
89     }
90     return 0;
91 }
92 extern int g_test_d;
93 
child(void)94 static int child(void)
95 {
96     volatile uint8_t *c1;
97     volatile uint8_t *c2;
98     uint8_t *c1_tmp;
99     uint8_t *c2_tmp;
100 
101     struct group_in *g1 = nullptr;
102     struct group_in *g2 = nullptr;
103 
104     for (int i = 0; i < TEST_NUM; ++i) {
105         c1_tmp = (uint8_t *)malloc(MALLOC_SIZE_S + 0x10 * i);
106         c2_tmp = (uint8_t *)malloc(MALLOC_SIZE_L + 0x10 * i);
107 
108         c1 = (uint8_t *)malloc(MALLOC_SIZE_S + 0x10 * i);
109         if ((!c1) || ((uintptr_t)c1 & 15))  {
110             printf("Malloc failed: %s\n", strerror(errno));
111             return -1;
112         }
113         g1 = get_group(c1);
114 
115         c2 = (uint8_t *)malloc(MALLOC_SIZE_L + 0x10 * i);
116         if ((!c2) || ((uintptr_t)c2 & 15))  {
117             printf("Malloc failed: %s\n", strerror(errno));
118             return -1;
119         }
120 
121         g2 = get_group(c2);
122         g2->meta = g1->meta;
123 
124         free((void *)c1);
125         free((void *)c2);
126 
127     }
128 
129     return 0;
130 }
131 
start_child(void)132 static pid_t start_child(void)
133 {
134     pid_t pid;
135     int ret;
136     pid = fork();
137     if (pid == 0) {
138         ret = child();
139         printf("child process normally out with %d\n", ret);
140         return ret;
141     }
142     return pid;
143 }
144 
145 /*
146  * @tc.number SAFE_UNLINK_TEST_0100
147  * @tc.name Apply part of the memory camfree reduction
148  * @tc.desc Test the safeunlink
149 */
150 HWTEST_F(MallocSafeUnlink, safeunlinkTest0100, Function | MediumTest | Level1) {
151     sigset_t set;
152     int status;
153     pid_t pid;
154     int flag = 0;
155     struct timespec time1 = {5, 0};
156 
157     sigemptyset(&set);
158     sigaddset(&set, SIGCHLD);
159     sigprocmask(SIG_BLOCK, &set, 0);
160     signal(SIGCHLD, handler);
161 
162     pid = start_child();
163     if (pid == -1) {
164         printf("fork failed: %s\n",strerror(errno));
165     }
166     if (sigtimedwait(&set, 0, &time1)  == -1) { /* Wait for 5 seconds */
167         if (errno == EAGAIN) {
168             flag = 1;
169         }
170         else {
171             printf("sigtimedwait failed: %s\n",strerror(errno));
172         }
173         if (kill(pid, SIGKILL) == -1) {
174             printf("kill failed: %s\n", strerror(errno));
175         }
176     }
177 
178     if (waitpid(pid, &status, 0) != pid) {
179         printf("waitpid failed: %s\n", strerror(errno));
180     }
181 
182     if (flag) {
183         printf("Child process time out\n");
184     }
185 
186     if (WIFSIGNALED(status)) {
187         ASSERT_TRUE(WTERMSIG(status) == SIGSEGV || WTERMSIG(status) == SIGILL) << "child process out with %s\n" <<
188         WTERMSIG(status);
189     } else {
190         ASSERT_TRUE(false) << "child process finished normally\n";
191     }
192 }
193