[LTP] [PATCH v4 4/4] syscalls/madvise13: new test for madvise(MADV_REMOVE)

Zhao Gongyi zhaogongyi@huawei.com
Thu Oct 13 15:47:28 CEST 2022


Check that the specified address range must be mapped shared and writable
and this flag cannot be applied to locked pages.

Signed-off-by: Zhao Gongyi <zhaogongyi@huawei.com>
---
 runtest/syscalls                              |  1 +
 testcases/kernel/syscalls/madvise/.gitignore  |  1 +
 testcases/kernel/syscalls/madvise/madvise13.c | 80 +++++++++++++++++++
 3 files changed, 82 insertions(+)
 create mode 100644 testcases/kernel/syscalls/madvise/madvise13.c

diff --git a/runtest/syscalls b/runtest/syscalls
index 0697b31ab..bd74373a4 100644
--- a/runtest/syscalls
+++ b/runtest/syscalls
@@ -950,6 +950,7 @@ madvise09 madvise09
 madvise10 madvise10
 madvise11 madvise11
 madvise12 madvise12
+madvise13 madvise13

 newuname01 newuname01

diff --git a/testcases/kernel/syscalls/madvise/.gitignore b/testcases/kernel/syscalls/madvise/.gitignore
index dc82c82bd..fcf04c749 100644
--- a/testcases/kernel/syscalls/madvise/.gitignore
+++ b/testcases/kernel/syscalls/madvise/.gitignore
@@ -10,3 +10,4 @@
 /madvise10
 /madvise11
 /madvise12
+/madvise13
diff --git a/testcases/kernel/syscalls/madvise/madvise13.c b/testcases/kernel/syscalls/madvise/madvise13.c
new file mode 100644
index 000000000..15b17a6a8
--- /dev/null
+++ b/testcases/kernel/syscalls/madvise/madvise13.c
@@ -0,0 +1,80 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) Huawei Technologies Co., Ltd. 2022. All rights reserved.
+ * Author: Zhao Gongyi <zhaogongyi@huawei.com>
+ */
+
+/*\
+ * [Description]
+ *
+ * Check that in madvise(2) MADV_REMOVE operation, the specified address
+ * range must be mapped shared and writable and this flag cannot be applied
+ * to locked pages.
+ */
+
+#define _GNU_SOURCE
+#include <fcntl.h>
+#include "tst_test.h"
+
+#define MAP_SIZE (8 * 1024)
+#define FNAME "madvise_remove"
+#define MOUNT_POINT "mntpoint"
+
+static int fd;
+static struct tcase {
+	int prot;
+	int flags;
+	int exp;
+	int err;
+} tcases[] = {
+	{PROT_READ, MAP_PRIVATE, -1, EACCES},
+	{PROT_READ, MAP_PRIVATE | MAP_LOCKED, -1, EINVAL},
+	{PROT_READ, MAP_SHARED, -1, EACCES},
+	{PROT_READ, MAP_SHARED | MAP_LOCKED, -1, EINVAL},
+	{PROT_WRITE, MAP_PRIVATE, -1, EACCES},
+	{PROT_WRITE, MAP_PRIVATE | MAP_LOCKED, -1, EINVAL},
+	{PROT_WRITE, MAP_SHARED | MAP_LOCKED, -1, EINVAL},
+	{PROT_WRITE, MAP_SHARED, 0, 0},
+	{PROT_WRITE | PROT_READ, MAP_SHARED, 0, 0},
+};
+
+
+static void run(unsigned int i)
+{
+	char *addr = SAFE_MMAP(NULL, MAP_SIZE,
+			tcases[i].prot,
+			tcases[i].flags,
+			fd, 0);
+
+	if (tcases[i].exp)
+		TST_EXP_FAIL(madvise(addr, MAP_SIZE, MADV_REMOVE),
+			     tcases[i].err);
+	else
+		TST_EXP_PASS(madvise(addr, MAP_SIZE, MADV_REMOVE));
+
+	SAFE_MUNMAP(addr, MAP_SIZE);
+}
+
+static void setup(void)
+{
+	fd = SAFE_OPEN(FNAME, O_CREAT | O_RDWR, 0777);
+}
+
+static void cleanup(void)
+{
+	SAFE_CLOSE(fd);
+	SAFE_UNLINK(FNAME);
+}
+
+static struct tst_test test = {
+	.needs_root = 1,
+	.tcnt = ARRAY_SIZE(tcases),
+	.test = run,
+	.min_kver = "2.6.16",
+	.setup = setup,
+	.cleanup = cleanup,
+	.all_filesystems = 1,
+	.mntpoint = MOUNT_POINT,
+	.mount_device = 1,
+};
+
--
2.17.1



More information about the ltp mailing list