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