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 50
29 #define MALLOC_SIZE_L 100
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
56 volatile void *tmp;
57
set_devide_chunk(size_t size)58 int set_devide_chunk(size_t size)
59 {
60 if (!(tmp = malloc(size))) {
61 t_error("Malloc failed: %s\n", strerror(errno));
62 return -1;
63 }
64 return 0;
65 }
66
child(void)67 static int child(void)
68 {
69 uint8_t *c1;
70 uint8_t *c2;
71
72 uint8_t *temp;
73 struct group_in *g1 = NULL;
74 struct group_in *g2 = NULL;
75
76 for (int i = 0; i < TEST_NUM; ++i) {
77 c1 = (uint8_t *)malloc(MALLOC_SIZE_S);
78 if (!c1) {
79 t_error("Malloc failed: %s\n", strerror(errno));
80 return -1;
81 }
82 g1 = get_group(c1);
83 c2 = (uint8_t *)malloc(MALLOC_SIZE_L);
84 if (!c2) {
85 t_error("Malloc failed: %s\n", strerror(errno));
86 return -1;
87 }
88 g2 = get_group(c2);
89 g2->meta = g1->meta;
90 free(c2);
91 free(c1);
92 }
93
94 return 0;
95 }
96
start_child(void)97 static pid_t start_child(void)
98 {
99 pid_t pid;
100 int ret;
101 pid = fork();
102 if (pid == 0) {
103 ret = child();
104 t_error("child process normally out with %d\n", ret);
105 return ret;
106 }
107 return pid;
108 }
109
main(int argc,char * argv[])110 int main(int argc, char *argv[])
111 {
112 sigset_t set;
113 int status;
114 pid_t pid;
115 int flag = 0;
116
117 sigemptyset(&set);
118 sigaddset(&set, SIGCHLD);
119 sigprocmask(SIG_BLOCK, &set, 0);
120 signal(SIGCHLD, handler);
121 signal(SIGILL, SIG_DFL);
122
123 pid = start_child();
124 if (pid == -1) {
125 t_error("%s fork failed: %s\n", argv[0], strerror(errno));
126 return -1;
127 }
128 if (sigtimedwait(&set, 0, &(struct timespec){5, 0}) == -1) { /* Wait for 5 seconds */
129 if (errno == EAGAIN)
130 flag = 1;
131 else
132 t_error("%s sigtimedwait failed: %s\n", argv[0], strerror(errno));
133 if (kill(pid, SIGKILL) == -1)
134 t_error("%s kill failed: %s\n", argv[0], strerror(errno));
135 }
136
137 if (waitpid(pid, &status, 0) != pid) {
138 t_error("%s waitpid failed: %s\n", argv[0], strerror(errno));
139 return -1;
140 }
141
142 if (flag) {
143 t_error("Child process time out\n");
144 }
145
146 if (WIFSIGNALED(status)) {
147 if (WTERMSIG(status) != SIGSEGV && WTERMSIG(status) != SIGILL) {
148 t_error("%s child process out with %s\n", argv[0], strsignal(WTERMSIG(status)));
149 return -1;
150 }
151 } else {
152 t_error("%s child process finished normally\n", argv[0]);
153 }
154 return 0;
155 }
156