Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/tests/lib/libc/sys This test should be testing how the kerne...



details:   https://anonhg.NetBSD.org/src/rev/897ac0969511
branches:  trunk
changeset: 817131:897ac0969511
user:      kre <kre%NetBSD.org@localhost>
date:      Tue Aug 09 12:02:44 2016 +0000

description:
This test should be testing how the kernel should behave, not how
it actually (used to) behave when it was incorrect...   Aside from
a possible EPERM (which is not tested) the only error possible from
mlock() is ENOMEM.   POSIX also allows EINVAL if the address is not
page aligned and the implementation does not round down to the previous
page boundary, but NetBSD does.

The kernel was recently fixed to return the correct errors for mlock()
so now we really need the test to be checking them, and not expecting
the incorrect errno values that the kernel used to return.

Same for munlock() - there ENOMEM is the only possible error, again,
EINVAL cannot happen as the kernel rounds to page boundaries.
For munlock() the kernel has not yet been corrected (that is coming
real soon...) and one of the munlock() tests will currently fail
(as of the time this commit is made, hopefully not for much longer)
as it should - it is indicating a kernel bug.

Note that NetBSD mlock(2) talks about EINVAL for cases where the length
parameter is negative ... but that is a size_t - good luck having that
ever occur (the man page will soon be corrected as well.)

diffstat:

 tests/lib/libc/sys/t_mlock.c |  51 +++++++++++++++++++++++++++----------------
 1 files changed, 32 insertions(+), 19 deletions(-)

diffs (83 lines):

diff -r 791438825ee6 -r 897ac0969511 tests/lib/libc/sys/t_mlock.c
--- a/tests/lib/libc/sys/t_mlock.c      Tue Aug 09 09:06:31 2016 +0000
+++ b/tests/lib/libc/sys/t_mlock.c      Tue Aug 09 12:02:44 2016 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: t_mlock.c,v 1.5 2014/02/26 20:49:26 martin Exp $ */
+/* $NetBSD: t_mlock.c,v 1.6 2016/08/09 12:02:44 kre Exp $ */
 
 /*-
  * Copyright (c) 2012 The NetBSD Foundation, Inc.
@@ -29,7 +29,7 @@
  * POSSIBILITY OF SUCH DAMAGE.
  */
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: t_mlock.c,v 1.5 2014/02/26 20:49:26 martin Exp $");
+__RCSID("$NetBSD: t_mlock.c,v 1.6 2016/08/09 12:02:44 kre Exp $");
 
 #include <sys/mman.h>
 #include <sys/resource.h>
@@ -79,34 +79,47 @@
 
 ATF_TC_BODY(mlock_err, tc)
 {
-       unsigned long vmin = 0;
-       size_t len = sizeof(vmin);
        void *invalid_ptr;
-       int null_errno = ENOMEM;        /* error expected for NULL */
+       void *buf;
+
+       /*
+        * Any bad address must return ENOMEM (for lock & unlock)
+        */
+       errno = 0;
+       ATF_REQUIRE_ERRNO(ENOMEM, mlock(NULL, page) == -1);
 
-       if (sysctlbyname("vm.minaddress", &vmin, &len, NULL, 0) != 0)
-               atf_tc_fail("failed to read vm.minaddress");
+       errno = 0;
+       ATF_REQUIRE_ERRNO(ENOMEM, mlock((char *)0, page) == -1);
 
-       if (vmin > 0)
-               null_errno = EINVAL;    /* NULL is not inside user VM */
+       errno = 0;
+       ATF_REQUIRE_ERRNO(ENOMEM, mlock((char *)-1, page) == -1);
+
+       errno = 0;
+       ATF_REQUIRE_ERRNO(ENOMEM, munlock(NULL, page) == -1);
 
        errno = 0;
-       ATF_REQUIRE_ERRNO(null_errno, mlock(NULL, page) == -1);
-
-       errno = 0;
-       ATF_REQUIRE_ERRNO(null_errno, mlock((char *)0, page) == -1);
+       ATF_REQUIRE_ERRNO(ENOMEM, munlock((char *)0, page) == -1);
 
        errno = 0;
-       ATF_REQUIRE_ERRNO(EINVAL, mlock((char *)-1, page) == -1);
+       ATF_REQUIRE_ERRNO(ENOMEM, munlock((char *)-1, page) == -1);
+
+       buf = malloc(page);
+       ATF_REQUIRE(buf != NULL);
+
+       /*
+        * unlocking memory that is not locked is an error...
+        */
 
        errno = 0;
-       ATF_REQUIRE_ERRNO(null_errno, munlock(NULL, page) == -1);
+       ATF_REQUIRE_ERRNO(ENOMEM, munlock(buf, page) == -1);
 
-       errno = 0;
-       ATF_REQUIRE_ERRNO(null_errno, munlock((char *)0, page) == -1);
+       /*
+        * These are permitted to fail (EINVAL) but do not on NetBSD
+        */
+       ATF_REQUIRE(mlock((void *)(((uintptr_t)buf) + page/3), page/5) == 0);
+       ATF_REQUIRE(munlock((void *)(((uintptr_t)buf) + page/3), page/5) == 0);
 
-       errno = 0;
-       ATF_REQUIRE_ERRNO(EINVAL, munlock((char *)-1, page) == -1);
+       (void)free(buf);
 
        /*
         * Try to create a pointer to an unmapped page - first after current



Home | Main Index | Thread Index | Old Index