tests: check decoding of madvise, mlockall, mremap, and msync syscalls
* tests/mmap.c (main): Change output format from regexp to verbatim.
Add tests for madvise, mlockall, mremap, and msync syscalls.
* tests/mmap.test: Convert from match_grep to match_diff.
Add madvise, mlockall, mremap, and msync syscalls to syscall filter.
diff --git a/tests/mmap.c b/tests/mmap.c
index 4654262..e2ff716 100644
--- a/tests/mmap.c
+++ b/tests/mmap.c
@@ -33,10 +33,13 @@
#include <sys/mman.h>
int
-main(void)
+main(int ac, char **av)
{
+ const char *const name = ac > 1 ? av[1] : "mmap";
const intmax_t pagesize = get_page_size();
- const unsigned long length = pagesize * 3;
+ const unsigned long length1 = pagesize * 6;
+ const unsigned long length2 = pagesize * 3;
+ const unsigned long length3 = pagesize * 2;
const int fd = -1;
off_t offset;
void *addr, *p;
@@ -48,25 +51,56 @@
offset = 0xdeadbeef000 & -pagesize;
addr = (void *) (unsigned int) (0xfaced000 & -pagesize);
#endif
+ const uintmax_t uoffset =
+ sizeof(offset) == sizeof(int) ? (uintmax_t) (unsigned int) offset
+ : (uintmax_t) offset;
- p = mmap(addr, length, PROT_READ | PROT_WRITE,
+ (void) close(0);
+ (void) close(0);
+ printf("%s(NULL, 0, PROT_NONE, MAP_FILE, 0, 0) = -1 EBADF (%m)\n",
+ name);
+ mmap(NULL, 0, PROT_NONE, MAP_FILE, 0, 0);
+
+ p = mmap(addr, length1, PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_ANONYMOUS, fd, offset);
if (MAP_FAILED == p)
perror_msg_and_fail("mmap");
- if (mprotect(p, length, PROT_NONE))
- perror_msg_and_fail("mprotect");
- if (munmap(p, length))
- perror_msg_and_fail("munmap");
+ printf("%s(%p, %lu, PROT_READ|PROT_WRITE, "
+ "MAP_PRIVATE|MAP_ANONYMOUS, %d, %#jx) = %p\n",
+ name, addr, length1, fd, uoffset, p);
- if (sizeof(offset) == sizeof(int))
- printf("mmap2?\\(%p, %lu, PROT_READ\\|PROT_WRITE, "
- "MAP_PRIVATE\\|MAP_ANONYMOUS, %d, %#x\\) = %p\n",
- addr, length, fd, (unsigned int) offset, p);
- else
- printf("(mmap2?|old_mmap)\\(%p, %lu, PROT_READ\\|PROT_WRITE, "
- "MAP_PRIVATE\\|MAP_ANONYMOUS, %d, %#jx\\) = %p\n",
- addr, length, fd, (uintmax_t) offset, p);
- printf("mprotect\\(%p, %lu, PROT_NONE\\) += 0\n", p, length);
- printf("munmap\\(%p, %lu\\) += 0\n", p, length);
+ if (msync(p, length1, MS_SYNC))
+ perror_msg_and_fail("msync");
+ printf("msync(%p, %lu, MS_SYNC) = 0\n", p, length1);
+
+ if (mprotect(p, length1, PROT_NONE))
+ perror_msg_and_fail("mprotect");
+ printf("mprotect(%p, %lu, PROT_NONE) = 0\n", p, length1);
+
+ addr = mremap(p, length1, length2, 0);
+ if (MAP_FAILED == addr)
+ perror_msg_and_fail("mremap");
+ printf("mremap(%p, %lu, %lu, 0) = %p\n", p, length1, length2, addr);
+
+ p = mremap(addr, length2, length3, MREMAP_MAYMOVE | MREMAP_FIXED,
+ addr + length2);
+ if (MAP_FAILED == p)
+ perror_msg_and_fail("mremap");
+ printf("mremap(%p, %lu, %lu, MREMAP_MAYMOVE|MREMAP_FIXED"
+ ", %p) = %p\n", addr, length2, length3, addr + length2, p);
+
+ if (madvise(p, length3, MADV_NORMAL))
+ perror_msg_and_fail("madvise");
+ printf("madvise(%p, %lu, MADV_NORMAL) = 0\n", p, length3);
+
+ if (munmap(p, length3))
+ perror_msg_and_fail("munmap");
+ printf("munmap(%p, %lu) = 0\n", p, length3);
+
+ if (mlockall(MCL_FUTURE))
+ perror_msg_and_fail("mlockall");
+ puts("mlockall(MCL_FUTURE) = 0");
+
+ puts("+++ exited with 0 +++");
return 0;
}
diff --git a/tests/mmap.test b/tests/mmap.test
index e24e736..846455b 100755
--- a/tests/mmap.test
+++ b/tests/mmap.test
@@ -1,20 +1,60 @@
#!/bin/sh
-
-# Check mmap/mprotect/munmap syscalls decoding.
+#
+# Check mmap/mmap2, madvise, mlockall, mprotect, mremap, msync, and munmap
+# syscalls decoding.
+#
+# Copyright (c) 2015-2016 Dmitry V. Levin <ldv@altlinux.org>
+# 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.
. "${srcdir=.}/init.sh"
-syscall=mprotect,munmap
+check_prog grep
+check_prog sed
+run_prog > /dev/null
+
+syscall=
for n in mmap mmap2; do
$STRACE -e$n -h > /dev/null && syscall=$syscall,$n
done
+run_strace -e$syscall $args > /dev/null
+if grep '^mmap(NULL, 0, PROT_NONE,' < "$LOG" > /dev/null; then
+ mmap=mmap
+elif grep '^mmap2(NULL, 0, PROT_NONE,' < "$LOG" > /dev/null; then
+ mmap=mmap2
+else
+ dump_log_and_fail_with "mmap/mmap2 not found in $STRACE $args output"
+fi
+
+syscall=$mmap,madvise,mlockall,mprotect,mremap,msync,munmap
+EXP="$LOG.exp"
OUT="$LOG.out"
-run_prog > /dev/null
-run_strace -e$syscall $args > "$OUT"
-match_grep "$LOG" "$OUT"
+run_prog "./${ME_%.test}" $mmap > /dev/null
+run_strace -a20 -e$syscall $args > "$EXP"
+sed -n "/^$mmap(NULL, 0, PROT_NONE,/,\$p" < "$LOG" > "$OUT"
+match_diff "$OUT" "$EXP"
-rm -f "$OUT"
-
-exit 0
+rm -f "$EXP" "$OUT"