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