• 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 
child(void)28 static int child(void)
29 {
30 	void *ptr = malloc(10);
31 	if (!ptr) {
32 		t_error("Malloc failed:%s\n", strerror(errno));
33 		return -1;
34 	}
35 
36 	/* Double free the pointer to trigger double-free check */
37 	free(ptr);
38 	free(ptr);
39 	return 0;
40 }
41 
start_child(void)42 static pid_t start_child(void)
43 {
44 	pid_t pid;
45 	int ret;
46 	pid = fork();
47 	if (pid == 0) {
48 		ret = child();
49 		t_error("child process normally out with %d\n", ret);
50 		return ret;
51 	}
52 	return pid;
53 }
54 
main(int argc,char * argv[])55 int main(int argc, char *argv[])
56 {
57 	sigset_t set;
58 	int status;
59 	pid_t pid;
60 	int flag = 0;
61 
62 	sigemptyset(&set);
63 	sigaddset(&set, SIGCHLD);
64 	sigprocmask(SIG_BLOCK, &set, 0);
65 	signal(SIGCHLD, handler);
66 
67 	pid = start_child();
68 	if (pid == -1) {
69 		t_error("%s fork failed: %s\n", argv[0], strerror(errno));
70 		return -1;
71 	}
72 	if (sigtimedwait(&set, 0, &(struct timespec){5, 0}) == -1) { /* Wait for 5 seconds */
73 		if (errno == EAGAIN)
74 			flag = 1;
75 		else
76 			t_error("%s sigtimedwait failed: %s\n", argv[0], strerror(errno));
77 		if (kill(pid, SIGKILL) == -1)
78 			t_error("%s kill failed: %s\n", argv[0], strerror(errno));
79 	}
80 
81 	if (waitpid(pid, &status, 0) != pid) {
82 		t_error("%s waitpid failed: %s\n", argv[0], strerror(errno));
83 		return -1;
84 	}
85 
86 	if (flag) {
87 		t_error("Child process time out\n");
88 	}
89 
90 	if (WIFSIGNALED(status)) {
91 		if (WTERMSIG(status) != SIGSEGV && WTERMSIG(status) != SIGILL) {
92 			t_error("%s child process out with %s\n", argv[0], strsignal(WTERMSIG(status)));
93 			return -1;
94 		}
95 	} else {
96 		t_error("%s child process finished normally\n", argv[0]);
97 	}
98 	return t_status;
99 }
100