• 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 <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 volatile void *tmp;
29 
30 #define MALLOC_SIZE 40
31 
set_devide_chunk(size_t size)32 int set_devide_chunk(size_t size)
33 {
34 	if (!(tmp = malloc(size))) {
35 		t_error("Malloc failed: %s\n", strerror(errno));
36 		return -1;
37 	}
38 	return 0;
39 }
40 
child(void)41 static int child(void)
42 {
43 	uintptr_t *c;
44 	uintptr_t *d;
45 	uintptr_t *temp;
46 
47 	/* Set first dividing chunk */
48 	if (set_devide_chunk(MALLOC_SIZE))
49 		return -1;
50 
51 	/*
52 	 * The init procedure makes the freelist unpredictable. To make sure trigger the safe-unlink
53 	 * check, Here we create as many chunks as possible to make sure there are enough chunks in
54 	 * bin[0] and malloc again. Basically this is heap spray.
55 	 */
56 	for (int i = 0; i < 512; ++i) {
57 		c = (uintptr_t *)malloc(MALLOC_SIZE);
58 		if (!c) {
59 			t_error("Malloc failed: %s\n", strerror(errno));
60 			return -1;
61 		}
62 
63 		if (set_devide_chunk(MALLOC_SIZE)) {
64 			free((void *)c);
65 			return -1;
66 		}
67 
68 		d = (uintptr_t *)malloc(MALLOC_SIZE);
69 		if (!d) {
70 			t_error("Malloc failed: %s\n", strerror(errno));
71 			free((void *)c);
72 			return -1;
73 		}
74 
75 		if (set_devide_chunk(MALLOC_SIZE)) {
76 			free((void *)d);
77 			free((void *)c);
78 			return -1;
79 		}
80 
81 		free((void *)d);
82 		free((void *)c);
83 
84 		/* exchange the prev and next pointer */
85 		uintptr_t temp = c[0];
86 		c[0] = c[1];
87 		c[1] = temp;
88 	}
89 
90 	return 0;
91 }
92 
start_child(void)93 static pid_t start_child(void)
94 {
95 	pid_t pid;
96 	int ret;
97 	pid = fork();
98 	if (pid == 0) {
99 		ret = child();
100 		t_error("child process normally out with %d\n", ret);
101 		return ret;
102 	}
103 	return pid;
104 }
105 
main(int argc,char * argv[])106 int main(int argc, char *argv[])
107 {
108 	sigset_t set;
109 	int status;
110 	pid_t pid;
111 	int flag = 0;
112 
113 	sigemptyset(&set);
114 	sigaddset(&set, SIGCHLD);
115 	sigprocmask(SIG_BLOCK, &set, 0);
116 	signal(SIGCHLD, handler);
117 
118 	pid = start_child();
119 	if (pid == -1) {
120 		t_error("%s fork failed: %s\n", argv[0], strerror(errno));
121 		return -1;
122 	}
123 	if (sigtimedwait(&set, 0, &(struct timespec){5, 0}) == -1) { /* Wait for 5 seconds */
124 		if (errno == EAGAIN)
125 			flag = 1;
126 		else
127 			t_error("%s sigtimedwait failed: %s\n", argv[0], strerror(errno));
128 		if (kill(pid, SIGKILL) == -1)
129 			t_error("%s kill failed: %s\n", argv[0], strerror(errno));
130 	}
131 
132 	if (waitpid(pid, &status, 0) != pid) {
133 		t_error("%s waitpid failed: %s\n", argv[0], strerror(errno));
134 		return -1;
135 	}
136 
137 	if (flag) {
138 		t_error("Child process time out\n");
139 	}
140 
141 	if (WIFSIGNALED(status)) {
142 		if (WTERMSIG(status) != SIGSEGV && WTERMSIG(status) != SIGILL) {
143 			t_error("%s child process out with %s\n", argv[0], strsignal(WTERMSIG(status)));
144 			return -1;
145 		}
146 	} else {
147 		t_error("%s child process finished normally\n", argv[0]);
148 	}
149 	return 0;
150 }
151