• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (C) 2017 Red Hat, Inc.  All rights reserved.
4  *
5  * Attempt to run a trivial binary with stack < 1MB.
6  *
7  * Early patches for stack guard gap caused that gap size was
8  * contributing to stack limit. This caused failures
9  * for new processes (E2BIG) when ulimit was set to anything
10  * lower than size of gap. commit 1be7107fbe18 "mm: larger
11  * stack guard gap, between vmas" sets default gap size to 1M
12  * (for systems with 4k pages), so let's set stack limit to 512kB
13  * and confirm we can still run some trivial binary.
14  */
15 
16 #define _GNU_SOURCE
17 #include <sys/resource.h>
18 #include <sys/time.h>
19 #include <sys/wait.h>
20 
21 #include "tst_test.h"
22 
23 #define STACK_LIMIT (512 * 1024)
24 
test_setrlimit(void)25 static void test_setrlimit(void)
26 {
27 	int status;
28 	struct rlimit rlim;
29 	pid_t child;
30 
31 	rlim.rlim_cur = STACK_LIMIT;
32 	rlim.rlim_max = STACK_LIMIT;
33 
34 	SAFE_SETRLIMIT(RLIMIT_STACK, &rlim);
35 
36 	child = SAFE_FORK();
37 	if (child == 0)
38 		SAFE_EXECLP("/bin/true", "/bin/true", NULL);
39 	SAFE_WAITPID(child, &status, 0);
40 
41 	if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
42 		tst_res(TPASS, "child process completed OK");
43 		return;
44 	}
45 
46 	tst_res(TFAIL, "child %s", tst_strstatus(status));
47 }
48 
49 static struct tst_test test = {
50 	.test_all     = test_setrlimit,
51 	.forks_child  = 1,
52 	.needs_root = 1,
53 };
54