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