1 /*
2 * Copyright (c) 2017 Cyril Hrubis <chrubis@suse.cz>
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 #include <unistd.h>
19 #include <stdint.h>
20 #include <inttypes.h>
21 #include <errno.h>
22
23 #include "tst_test.h"
24
verify_brk(void)25 void verify_brk(void)
26 {
27 uintptr_t cur_brk, new_brk;
28 uintptr_t inc = getpagesize() * 2 - 1;
29 unsigned int i;
30
31 cur_brk = (uintptr_t)sbrk(0);
32
33 for (i = 0; i < 33; i++) {
34 switch (i % 3) {
35 case 0:
36 new_brk = cur_brk + inc;
37 break;
38 case 1:
39 new_brk = cur_brk;
40 break;
41 case 2:
42 new_brk = cur_brk - inc;
43 break;
44 }
45
46 TEST(brk((void *)new_brk));
47
48 if (TST_RET == -1) {
49 tst_res(TFAIL | TERRNO, "brk() failed");
50 return;
51 }
52
53 cur_brk = (uintptr_t)sbrk(0);
54
55 if (cur_brk != new_brk) {
56 tst_res(TFAIL,
57 "brk() failed to set address have %p expected %p",
58 (void *)cur_brk, (void *)new_brk);
59 return;
60 }
61
62 /* Try to write to the newly allocated heap */
63 if (i % 3 == 0)
64 *((char *)cur_brk) = 0;
65 }
66
67 tst_res(TPASS, "brk() works fine");
68 }
69
70 static struct tst_test test = {
71 .test_all = verify_brk,
72 };
73