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