xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/sbrk/sbrk02.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1*49cdfc7eSAndroid Build Coastguard Worker // SPDX-License-Identifier: GPL-2.0-only
2*49cdfc7eSAndroid Build Coastguard Worker /*
3*49cdfc7eSAndroid Build Coastguard Worker  * Copyright (c) 2014 Fujitsu Ltd.
4*49cdfc7eSAndroid Build Coastguard Worker  * Author: Zeng Linggang <[email protected]>
5*49cdfc7eSAndroid Build Coastguard Worker  * Copyright (c) 2023 SUSE LLC Avinesh Kumar <[email protected]>
6*49cdfc7eSAndroid Build Coastguard Worker  */
7*49cdfc7eSAndroid Build Coastguard Worker 
8*49cdfc7eSAndroid Build Coastguard Worker /*\
9*49cdfc7eSAndroid Build Coastguard Worker  * [Description]
10*49cdfc7eSAndroid Build Coastguard Worker  *
11*49cdfc7eSAndroid Build Coastguard Worker  * Verify that sbrk() on failure sets errno to ENOMEM.
12*49cdfc7eSAndroid Build Coastguard Worker  */
13*49cdfc7eSAndroid Build Coastguard Worker 
14*49cdfc7eSAndroid Build Coastguard Worker 
15*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
16*49cdfc7eSAndroid Build Coastguard Worker 
17*49cdfc7eSAndroid Build Coastguard Worker #define INC (16*1024*1024)
18*49cdfc7eSAndroid Build Coastguard Worker static long increment = INC;
19*49cdfc7eSAndroid Build Coastguard Worker 
run(void)20*49cdfc7eSAndroid Build Coastguard Worker static void run(void)
21*49cdfc7eSAndroid Build Coastguard Worker {
22*49cdfc7eSAndroid Build Coastguard Worker 	TESTPTR(sbrk(increment));
23*49cdfc7eSAndroid Build Coastguard Worker 
24*49cdfc7eSAndroid Build Coastguard Worker 	if (TST_RET_PTR != (void *)-1) {
25*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL, "sbrk(%ld) unexpectedly passed and returned %p, "
26*49cdfc7eSAndroid Build Coastguard Worker 						"expected (void *)-1 with errno=%d",
27*49cdfc7eSAndroid Build Coastguard Worker 						increment, TST_RET_PTR, ENOMEM);
28*49cdfc7eSAndroid Build Coastguard Worker 		return;
29*49cdfc7eSAndroid Build Coastguard Worker 	}
30*49cdfc7eSAndroid Build Coastguard Worker 
31*49cdfc7eSAndroid Build Coastguard Worker 	if (TST_ERR == ENOMEM)
32*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TPASS | TTERRNO, "sbrk(%ld) failed as expected", increment);
33*49cdfc7eSAndroid Build Coastguard Worker 	else
34*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL | TTERRNO, "sbrk(%ld) failed but unexpected errno, "
35*49cdfc7eSAndroid Build Coastguard Worker 								"expected errno=%d - %s",
36*49cdfc7eSAndroid Build Coastguard Worker 								increment, ENOMEM, strerror(ENOMEM));
37*49cdfc7eSAndroid Build Coastguard Worker }
38*49cdfc7eSAndroid Build Coastguard Worker 
setup(void)39*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
40*49cdfc7eSAndroid Build Coastguard Worker {
41*49cdfc7eSAndroid Build Coastguard Worker 	void *ret = NULL;
42*49cdfc7eSAndroid Build Coastguard Worker 
43*49cdfc7eSAndroid Build Coastguard Worker 	while (ret != (void *)-1 && increment > 0) {
44*49cdfc7eSAndroid Build Coastguard Worker 		ret = sbrk(increment);
45*49cdfc7eSAndroid Build Coastguard Worker 		increment += INC;
46*49cdfc7eSAndroid Build Coastguard Worker 	}
47*49cdfc7eSAndroid Build Coastguard Worker }
48*49cdfc7eSAndroid Build Coastguard Worker 
49*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
50*49cdfc7eSAndroid Build Coastguard Worker 	.test_all = run,
51*49cdfc7eSAndroid Build Coastguard Worker 	.setup = setup
52*49cdfc7eSAndroid Build Coastguard Worker };
53