• 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 <errno.h>
20 #include "test.h"
21 
22 #define MALLOC_SIZE_S 0x20
23 #define MALLOC_SIZE_L 0x40
24 #define IDX_IN_CHUNK (-3)
25 
get_slotnum(uint8_t * p)26 static int get_slotnum(uint8_t *p)
27 {
28 	return (p[IDX_IN_CHUNK] & 0x1f);
29 }
30 
main(void)31 int main(void)
32 {
33 	void *p = malloc(MALLOC_SIZE_S);
34 	void *q = malloc(MALLOC_SIZE_S);
35 	void *r = malloc(MALLOC_SIZE_S);
36 	if (!p || !q || !r)
37 		t_error("malloc returned NULL\n");
38 
39 	int slot_p_1 = get_slotnum((uint8_t *)p);
40 	int slot_q_1 = get_slotnum((uint8_t *)q);
41 	int slot_r_1 = get_slotnum((uint8_t *)r);
42 
43 	free(p);
44 	free(q);
45 	free(r);
46 
47 	p = malloc(MALLOC_SIZE_L);
48 	q = malloc(MALLOC_SIZE_L);
49 	r = malloc(MALLOC_SIZE_L);
50 	if (!p || !q || !r)
51 		t_error("malloc returned NULL\n");
52 
53 	int slot_p_2 = get_slotnum((uint8_t *)p);
54 	int slot_q_2 = get_slotnum((uint8_t *)q);
55 	int slot_r_2 = get_slotnum((uint8_t *)r);
56 
57 	free(p);
58 	free(q);
59 	free(r);
60 
61 	if (((slot_p_1 + 1) == slot_q_1) && ((slot_q_1 + 1) == slot_r_1)) {
62 		if (((slot_p_2 + 1) == slot_q_2) && ((slot_q_2 + 1) == slot_r_2))
63 			t_error("malloc(0) random error\n");
64 	}
65 	return t_status;
66 }
67 
68