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
24static void handler(int s)
25{
26}
27
28uint8_t *p0;
29uint8_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
38volatile void *tmp[TEST_NUM];
39
40struct meta_in {
41	struct meta_in *prev, *next;
42	uintptr_t *mem;
43};
44
45struct group_in {
46	struct meta_in *meta;
47};
48
49static 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
62static 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
88static 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
101int 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	signal(SIGILL, SIG_DFL);
113
114	pid = start_child();
115	if (pid == -1) {
116		t_error("%s fork failed: %s\n", argv[0], strerror(errno));
117		return -1;
118	}
119	if (sigtimedwait(&set, 0, &(struct timespec){5, 0}) == -1) { /* Wait for 5 seconds */
120		if (errno == EAGAIN)
121			flag = 1;
122		else
123			t_error("%s sigtimedwait failed: %s\n", argv[0], strerror(errno));
124		if (kill(pid, SIGKILL) == -1)
125			t_error("%s kill failed: %s\n", argv[0], strerror(errno));
126	}
127
128	if (waitpid(pid, &status, 0) != pid) {
129		t_error("%s waitpid failed: %s\n", argv[0], strerror(errno));
130		return -1;
131	}
132
133	if (flag) {
134		t_error("Child process time out\n");
135	}
136
137	if (WIFSIGNALED(status)) {
138		if (WTERMSIG(status) != SIGSEGV && WTERMSIG(status) != SIGILL) {
139			t_error("%s child process out with %s\n", argv[0], strsignal(WTERMSIG(status)));
140			return -1;
141		}
142	} else {
143		t_error("%s child process finished normally\n", argv[0]);
144	}
145	return 0;
146}
147