• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) 2010  Red Hat, Inc.
4  */
5 
6 /*\
7  * [Description]
8  *
9  * This case is a regression test on old RHEL5.
10  *
11  * Stack size mapping is decreased through mlock/munlock call.
12  * See the following url:
13  * https://bugzilla.redhat.com/show_bug.cgi?id=643426
14  *
15  * This is to test kernel if it has a problem with shortening [stack]
16  * mapping through several loops of mlock/munlock of /proc/self/maps.
17  *
18  * From:
19  * munlock     76KiB bfef2000-bff05000 rw-p 00000000 00:00 0          [stack]
20  *
21  * To:
22  * munlock     44KiB bfefa000-bff05000 rw-p 00000000 00:00 0          [stack]
23  *
24  * with more iterations - could drop to 0KiB.
25  */
26 
27 #include <sys/mman.h>
28 #include <stdio.h>
29 #include <string.h>
30 #include <pwd.h>
31 #include "tst_test.h"
32 #include "tst_safe_stdio.h"
33 
verify_mlock(void)34 static void verify_mlock(void)
35 {
36 	long from, to;
37 	long first = -1, last = -1;
38 	char b[TST_KB];
39 	FILE *fp;
40 
41 	fp = SAFE_FOPEN("/proc/self/maps", "r");
42 	while (!feof(fp)) {
43 		if (!fgets(b, TST_KB - 1, fp))
44 			break;
45 		b[strlen(b) - 1] = '\0';
46 		if (sscanf(b, "%lx-%lx", &from, &to) != 2) {
47 			tst_brk(TBROK, "parse %s start and end address failed",
48 					b);
49 			continue;
50 		}
51 
52 		/* Record the initial stack size. */
53 		if (strstr(b, "[stack]") != NULL)
54 			first = (to - from) / TST_KB;
55 
56 		tst_res(TINFO, "mlock [%lx,%lx]", from, to);
57 		if (mlock((const void *)from, to - from) == -1)
58 			tst_res(TINFO | TERRNO, "mlock failed");
59 
60 		tst_res(TINFO, "munlock [%lx,%lx]", from, to);
61 		if (munlock((void *)from, to - from) == -1)
62 			tst_res(TINFO | TERRNO, "munlock failed");
63 
64 		/* Record the final stack size. */
65 		if (strstr(b, "[stack]") != NULL)
66 			last = (to - from) / TST_KB;
67 	}
68 	SAFE_FCLOSE(fp);
69 
70 	tst_res(TINFO, "starting stack size is %ld", first);
71 	tst_res(TINFO, "final stack size is %ld", last);
72 	if (last < first)
73 		tst_res(TFAIL, "stack size is decreased.");
74 	else
75 		tst_res(TPASS, "stack size is not decreased.");
76 }
77 
78 static struct tst_test test = {
79 	.test_all = verify_mlock,
80 };
81