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 <stdlib.h>
17 #include <unistd.h>
18 #include <sys/wait.h>
19 #include <signal.h>
20 #include <errno.h>
21 #include <string.h>
22 #include "test.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 (10 * sizeof(uintptr_t))
29 #define MALLOC_SIZE_L (50 * sizeof(uintptr_t))
30 #define TEST_NUM 512
31
32 struct meta_in {
33 struct meta_in *prev, *next;
34 uintptr_t *mem;
35 };
36
37 struct group_in {
38 struct meta_in *meta;
39 };
40
get_group(const uint8_t * p)41 static struct group_in *get_group(const uint8_t *p)
42 {
43 int offset = *(const uint16_t *)(p - OFF_OFFSET);
44
45 if (p[FIRST_OFFSET]) {
46 offset = *(uint32_t *)(p - FIRST_OFF_OFFSET);
47 }
48 struct group_in *base = (void *)(p - UNIT*offset - UNIT);
49 return base;
50 }
51
handler(int s)52 static void handler(int s)
53 {
54 }
55
child(void)56 static int child(void)
57 {
58 void *d0;
59 void *d1;
60
61 struct group_in *g0 = NULL;
62 struct group_in *g1 = NULL;
63 struct group_in *g2 = NULL;
64
65 for (int i = 0; i < TEST_NUM; ++i) {
66 d0 = malloc(MALLOC_SIZE_S);
67 if (!d0) {
68 t_error("Malloc d0 failed: %s\n", strerror(errno));
69 return -1;
70 }
71
72 g0 = get_group(d0);
73
74 d1 = malloc(MALLOC_SIZE_L);
75 if (!d1) {
76 t_error("Malloc d1 failed: %s\n", strerror(errno));
77 return -1;
78 }
79 g1 = get_group(d1);
80
81 if ((uintptr_t)g0->meta->mem == (uintptr_t)g0)
82 t_error("encoding pointer is equal to real pointer 1\n");
83
84 if ((uintptr_t)g1->meta->mem == (uintptr_t)g1)
85 t_error("encoding pointer is equal to real pointer 2\n");
86
87 if ((uintptr_t)g0->meta->prev->next == (uintptr_t)g0->meta->mem)
88 t_error("encoding pointer is equal to real pointer 1\n");
89
90 if ((uintptr_t)g1->meta->prev->next == (uintptr_t)g1->meta->mem)
91 t_error("encoding pointer is equal to real pointer 2\n");
92
93 free(d0);
94 free(d1);
95 }
96
97 return 0;
98 }
99
start_child(void)100 static pid_t start_child(void)
101 {
102 pid_t pid;
103 int ret;
104 pid = fork();
105 if (pid == 0) {
106 ret = child();
107 t_error("child process normally out with %d\n", ret);
108 return ret;
109 }
110 return pid;
111 }
112
main(int argc,char * argv[])113 int main(int argc, char *argv[])
114 {
115 sigset_t set;
116 int status;
117 pid_t pid;
118 int flag = 0;
119
120 sigemptyset(&set);
121 sigaddset(&set, SIGCHLD);
122 sigprocmask(SIG_BLOCK, &set, 0);
123 signal(SIGCHLD, handler);
124
125 pid = start_child();
126 if (pid == -1) {
127 t_error("%s fork failed: %s\n", argv[0], strerror(errno));
128 return -1;
129 }
130 if (sigtimedwait(&set, 0, &(struct timespec){5, 0}) == -1) { /* Wait for 5 seconds */
131 if (errno == EAGAIN)
132 flag = 1;
133 else
134 t_error("%s sigtimedwait failed: %s\n", argv[0], strerror(errno));
135 if (kill(pid, SIGKILL) == -1)
136 t_error("%s kill failed: %s\n", argv[0], strerror(errno));
137 }
138
139 if (waitpid(pid, &status, 0) != pid) {
140 t_error("%s waitpid failed: %s\n", argv[0], strerror(errno));
141 return -1;
142 }
143
144 if (flag) {
145 t_error("Child process time out\n");
146 }
147
148 if (WIFSIGNALED(status)) {
149 if (WTERMSIG(status) != SIGSEGV && WTERMSIG(status) != SIGILL) {
150 t_error("%s child process out with %s\n", argv[0], strsignal(WTERMSIG(status)));
151 return -1;
152 }
153 } else {
154 t_error("%s child process finished normally\n", argv[0]);
155 }
156 return 0;
157 }
158
159