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
handler(int s)24 static void handler(int s)
25 {
26 }
27
28 uint8_t *p0;
29 uint8_t *p1;
30
31 #define UNIT 16
32 #define OFF_OFFSET 2
33 #define FIRST_OFFSET (-4)
34 #define FIRST_OFF_OFFSET 8
35 #define MALLOC_SIZE_S (10 * sizeof(uintptr_t))
36 #define TEST_NUM 512
37
38 volatile void *tmp[TEST_NUM];
39
40 struct meta_in {
41 struct meta_in *prev, *next;
42 uintptr_t *mem;
43 };
44
45 struct group_in {
46 struct meta_in *meta;
47 };
48
get_group(uint8_t * p)49 static struct group_in *get_group(uint8_t *p)
50 {
51 int offset = *(uint16_t *)(p - OFF_OFFSET);
52
53 if (p[FIRST_OFFSET]) {
54 offset = *(uint32_t *)(p - FIRST_OFF_OFFSET);
55 }
56
57 struct group_in *base = (void *)(p - UNIT*offset - UNIT);
58 return base;
59 }
60
61
child(void)62 static int child(void)
63 {
64 struct group_in *g = NULL;
65
66 p0 = (uint8_t *)malloc(MALLOC_SIZE_S);
67 if (!p0) {
68 t_error("Malloc failed:%s\n", strerror(errno));
69 return -1;
70 }
71 /* Malloc a dividing chunk to avoid combination of neighbouring freed chunk */
72 tmp[0] = malloc(MALLOC_SIZE_S);
73
74 g = get_group(p0);
75 free((void *)tmp[0]);
76 g->meta += 1;
77
78 for (int i = 0; i < TEST_NUM; ++i) {
79 tmp[i] = malloc(MALLOC_SIZE_S);
80 }
81
82 for (int i = 0; i < TEST_NUM; ++i) {
83 free((void *)tmp[i]);
84 }
85 return 0;
86 }
87
start_child(void)88 static pid_t start_child(void)
89 {
90 pid_t pid;
91 int ret;
92 pid = fork();
93 if (pid == 0) {
94 ret = child();
95 t_error("child process normally out with %d\n", ret);
96 return ret;
97 }
98 return pid;
99 }
100
main(int argc,char * argv[])101 int main(int argc, char *argv[])
102 {
103 sigset_t set;
104 int status;
105 pid_t pid;
106 int flag = 0;
107
108 sigemptyset(&set);
109 sigaddset(&set, SIGCHLD);
110 sigprocmask(SIG_BLOCK, &set, 0);
111 signal(SIGCHLD, handler);
112
113 pid = start_child();
114 if (pid == -1) {
115 t_error("%s fork failed: %s\n", argv[0], strerror(errno));
116 return -1;
117 }
118 if (sigtimedwait(&set, 0, &(struct timespec){5, 0}) == -1) { /* Wait for 5 seconds */
119 if (errno == EAGAIN)
120 flag = 1;
121 else
122 t_error("%s sigtimedwait failed: %s\n", argv[0], strerror(errno));
123 if (kill(pid, SIGKILL) == -1)
124 t_error("%s kill failed: %s\n", argv[0], strerror(errno));
125 }
126
127 if (waitpid(pid, &status, 0) != pid) {
128 t_error("%s waitpid failed: %s\n", argv[0], strerror(errno));
129 return -1;
130 }
131
132 if (flag) {
133 t_error("Child process time out\n");
134 }
135
136 if (WIFSIGNALED(status)) {
137 if (WTERMSIG(status) != SIGSEGV && WTERMSIG(status) != SIGILL) {
138 t_error("%s child process out with %s\n", argv[0], strsignal(WTERMSIG(status)));
139 return -1;
140 }
141 } else {
142 t_error("%s child process finished normally\n", argv[0]);
143 }
144 return 0;
145 }
146