[LTP] [PATCH v3] syscalls/mlock03: Convert into new api
Yang Xu
xuyang2018.jy@fujitsu.com
Mon May 8 11:31:50 CEST 2023
This case is a regression test on old RHEL5.6.
You can see the following url:
https://bugzilla.redhat.com/show_bug.cgi?id=643426
Still keep this case is meaningful because we can test
whether kernel has bug on stack guard page reporting
through /proc/self/maps in the future.
Also remove tst_require_root. Test mlock/munlock instead
of mlock by default.
Signed-off-by: Yang Xu <xuyang2018.jy@fujitsu.com>
---
testcases/kernel/syscalls/mlock/mlock03.c | 131 ++++++++--------------
1 file changed, 47 insertions(+), 84 deletions(-)
diff --git a/testcases/kernel/syscalls/mlock/mlock03.c b/testcases/kernel/syscalls/mlock/mlock03.c
index 8bc65701c..429fb1c2a 100644
--- a/testcases/kernel/syscalls/mlock/mlock03.c
+++ b/testcases/kernel/syscalls/mlock/mlock03.c
@@ -1,5 +1,16 @@
+// SPDX-License-Identifier: GPL-2.0
/*
+ * Copyright (C) 2010 Red Hat, Inc.
+ */
+
+/*\
+ * [Description]
+ *
+ * This case is a regression test on old RHEL5.
+ *
* Stack size mapping is decreased through mlock/munlock call.
+ * See the following url:
+ * https://bugzilla.redhat.com/show_bug.cgi?id=643426
*
* This is to test kernel if it has a problem with shortening [stack]
* mapping through several loops of mlock/munlock of /proc/self/maps.
@@ -11,109 +22,61 @@
* munlock 44KiB bfefa000-bff05000 rw-p 00000000 00:00 0 [stack]
*
* with more iterations - could drop to 0KiB.
- *
- * Copyright (C) 2010 Red Hat, Inc.
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of version 2 of the GNU General Public
- * License as published by the Free Software Foundation.
- *
- * This program is distributed in the hope that it would be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
- *
- * Further, this software is distributed without any warranty that it
- * is free of the rightful claim of any third person regarding
- * infringement or the like. Any license provided herein, whether
- * implied or otherwise, applies only to this software file. Patent
- * licenses, if any, provided herein do not apply to combinations of
- * this program with other software, or any other product whatsoever.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
- * 02110-1301, USA.
*/
+
#include <sys/mman.h>
#include <stdio.h>
#include <string.h>
-#include "test.h"
+#include <pwd.h>
+#include "tst_test.h"
+#include "tst_safe_stdio.h"
#define KB 1024
-char *TCID = "mlock03";
-int TST_TOTAL = 1;
-
-static void setup(void);
-static void cleanup(void);
-
-int main(int argc, char *argv[])
+static void verify_mlock(void)
{
- int lc;
long from, to;
long first = -1, last = -1;
char b[KB];
FILE *fp;
- tst_parse_opts(argc, argv, NULL, NULL);
-
- setup();
+ fp = SAFE_FOPEN("/proc/self/maps", "r");
+ while (!feof(fp)) {
+ if (!fgets(b, KB - 1, fp))
+ break;
+ b[strlen(b) - 1] = '\0';
+ if (sscanf(b, "%lx-%lx", &from, &to) != 2) {
+ tst_brk(TBROK, "parse %s start and end address failed",
+ b);
+ continue;
+ }
- for (lc = 0; TEST_LOOPING(lc); lc++) {
- fp = fopen("/proc/self/maps", "r");
- if (fp == NULL)
- tst_brkm(TBROK | TERRNO, cleanup, "fopen");
- while (!feof(fp)) {
- if (!fgets(b, KB - 1, fp))
- break;
- b[strlen(b) - 1] = '\0';
- sscanf(b, "%lx-%lx", &from, &to);
+ /* Record the initial stack size. */
+ if (strstr(b, "[stack]") != NULL)
+ first = (to - from) / KB;
- /* Record the initial stack size. */
- if (lc == 0 && strstr(b, "[stack]") != NULL)
- first = (to - from) / KB;
+ tst_res(TINFO, "mlock[%lx,%lx]", from, to);
+ if (mlock((const void *)from, to - from) == -1)
+ tst_res(TINFO | TERRNO, "mlock failed");
- switch (lc & 1) {
- case 0:
- if (mlock((const void *)from, to - from) == -1)
- tst_resm(TINFO | TERRNO,
- "mlock failed");
- break;
- case 1:
- if (munlock((void *)from, to - from) == -1)
- tst_resm(TINFO | TERRNO,
- "munlock failed");
- break;
- default:
- break;
- }
- tst_resm(TINFO, "%s from %lx to %0lx",
- (lc & 1) ? "munlock" : "mlock", from, to);
+ tst_res(TINFO, "munlock [%lx,%lx]", from, to);
+ if (munlock((void *)from, to - from) == -1)
+ tst_res(TINFO | TERRNO, "munlock failed");
- /* Record the final stack size. */
- if (strstr(b, "[stack]") != NULL)
- last = (to - from) / KB;
- }
- fclose(fp);
+ /* Record the final stack size. */
+ if (strstr(b, "[stack]") != NULL)
+ last = (to - from) / KB;
}
- tst_resm(TINFO, "starting stack size is %ld", first);
- tst_resm(TINFO, "final stack size is %ld", last);
+ SAFE_FCLOSE(fp);
+
+ tst_res(TINFO, "starting stack size is %ld", first);
+ tst_res(TINFO, "final stack size is %ld", last);
if (last < first)
- tst_resm(TFAIL, "stack size is decreased.");
+ tst_res(TFAIL, "stack size is decreased.");
else
- tst_resm(TPASS, "stack size is not decreased.");
-
- cleanup();
- tst_exit();
+ tst_res(TPASS, "stack size is not decreased.");
}
-void setup(void)
-{
- tst_require_root();
-
- tst_sig(FORK, DEF_HANDLER, cleanup);
- TEST_PAUSE;
-}
-
-void cleanup(void)
-{
-}
+static struct tst_test test = {
+ .test_all = verify_mlock,
+};
--
2.39.1
More information about the ltp
mailing list