xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/brk/brk01.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (c) 2017 Cyril Hrubis <[email protected]>
4  */
5 
6 #include <unistd.h>
7 #include <stdint.h>
8 #include <inttypes.h>
9 #include <errno.h>
10 
11 #include "tst_test.h"
12 #include "lapi/syscalls.h"
13 
verify_brk(void)14 static void verify_brk(void)
15 {
16 	void *cur_brk, *new_brk;
17 	size_t inc = getpagesize() * 2 - 1;
18 	unsigned int i;
19 
20 	if (tst_variant) {
21 		tst_res(TINFO, "Testing syscall variant");
22 		cur_brk = (void *)tst_syscall(__NR_brk, 0);
23 	} else {
24 		tst_res(TINFO, "Testing libc variant");
25 		cur_brk = (void *)sbrk(0);
26 
27 		if (cur_brk == (void *)-1)
28 			tst_brk(TCONF, "sbrk() not implemented");
29 
30 		/*
31 		 * Check if brk itself is implemented: updating to the current break
32 		 * should be a no-op.
33 		 */
34 		if (brk(cur_brk) != 0)
35 			tst_brk(TCONF, "brk() not implemented");
36 	}
37 
38 	for (i = 0; i < 33; i++) {
39 		switch (i % 3) {
40 		case 0:
41 			new_brk = cur_brk + inc;
42 		break;
43 		case 1:
44 			new_brk = cur_brk;
45 		break;
46 		case 2:
47 			new_brk = cur_brk - inc;
48 		break;
49 		}
50 
51 		if (tst_variant) {
52 			cur_brk = (void *)tst_syscall(__NR_brk, new_brk);
53 		} else {
54 			TST_EXP_PASS_SILENT(brk(new_brk), "brk()");
55 			cur_brk = sbrk(0);
56 		}
57 
58 		if (cur_brk != new_brk) {
59 			tst_res(TFAIL,
60 				"brk() failed to set address have %p expected %p",
61 				cur_brk, new_brk);
62 			return;
63 		}
64 
65 		/* Try to write to the newly allocated heap */
66 		if (i % 3 == 0)
67 			*((char *)cur_brk) = 0;
68 	}
69 
70 	tst_res(TPASS, "brk() works fine");
71 }
72 
73 static struct tst_test test = {
74 	.test_all = verify_brk,
75 	.test_variants = 2,
76 };
77