[PATCH v2] Add test for mincore syscall

Fei Jie feij.fnst at cn.fujitsu.com
Wed Jan 27 03:01:53 UTC 2016


Check how mincore syscall is traced.

* tests/mincore.c: New file.
* tests/mincore.test: New test.
* tests/Makefile.am: (check_PROGRAMS): Add mincore.
(TESTS): Add mincore.test.
* tests/.gitignore: Add mincore.
---
 tests/.gitignore   |  1 +
 tests/Makefile.am  |  2 ++
 tests/mincore.c    | 72 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 tests/mincore.test | 13 ++++++++++
 4 files changed, 88 insertions(+)
 create mode 100644 tests/mincore.c
 create mode 100755 tests/mincore.test

diff --git a/tests/.gitignore b/tests/.gitignore
index 207a9b2..10214d3 100644
--- a/tests/.gitignore
+++ b/tests/.gitignore
@@ -50,6 +50,7 @@ lstat
 lstat64
 membarrier
 memfd_create
+mincore
 mlock2
 mmap
 mmap64
diff --git a/tests/Makefile.am b/tests/Makefile.am
index f634583..e31be34 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -96,6 +96,7 @@ check_PROGRAMS = \
 	lstat64 \
 	membarrier \
 	memfd_create \
+	mincore \
 	mlock2 \
 	mmap \
 	mmap64 \
@@ -235,6 +236,7 @@ TESTS = \
 	lstat64.test \
 	membarrier.test \
 	memfd_create.test \
+	mincore.test \
 	mlock2.test \
 	mmap.test \
 	mmap64.test \
diff --git a/tests/mincore.c b/tests/mincore.c
new file mode 100644
index 0000000..9bdba30
--- /dev/null
+++ b/tests/mincore.c
@@ -0,0 +1,72 @@
+/*
+ * Copyright (c) 2015-2016 Fei, Jie <feij.fnst at cn.fujitsu.com>
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. The name of the author may not be used to endorse or promote products
+ *    derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include "tests.h"
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/mman.h>
+#include <sys/types.h>
+#include <unistd.h>
+
+int main(void)
+{
+	char file_name[] = "mincore_XXXXXX";
+	int pg_size = getpagesize();
+	int global_len = pg_size * 2;
+	char *buf = (char *)malloc(global_len);
+	char *global_pointer = NULL;
+	int file_desc, res;
+	memset(buf, 42, global_len);
+
+	if ((file_desc = mkstemp(file_name)) == -1) {
+		 perror_msg_and_fail("Error while creating temporary file");
+	}
+
+	if (write(file_desc, buf, global_len) == -1) {
+		perror_msg_and_fail("Error while writing to temporary file");
+	}
+	free(buf);
+
+	global_pointer = (char *)mmap(NULL, global_len,
+		PROT_READ|PROT_WRITE|PROT_EXEC, MAP_SHARED, file_desc, 0);
+	if (global_pointer == MAP_FAILED) {
+		perror_msg_and_fail("Temporary file could not be mmapped");
+	}
+
+	unsigned char *global_vec = malloc((global_len + pg_size - 1) / pg_size);
+	res = mincore(global_pointer, global_len, global_vec);
+	if (res == -1) {
+		perror_msg_and_fail("mincore fail!\n");
+	}
+	printf("mincore\\(%p, %d, \[[0-1]*\\.\\.\\.]\\) = %d\n",
+	       global_pointer, global_len, res);
+
+	free(global_vec);
+	return 0;
+}
diff --git a/tests/mincore.test b/tests/mincore.test
new file mode 100755
index 0000000..29e2177
--- /dev/null
+++ b/tests/mincore.test
@@ -0,0 +1,13 @@
+#!/bin/sh
+
+# Check how mincore syscall is traced.
+
+. "${srcdir=.}/init.sh"
+
+OUT="$LOG.out"
+run_prog
+run_strace -emincore -qq $args > "$OUT"
+match_grep "$LOG" "$OUT"
+rm -f "$OUT" mincore_*
+
+exit 0
-- 
1.8.3.1







More information about the Strace-devel mailing list