diff options
author | Matthew Dempsky <matthew@cvs.openbsd.org> | 2014-06-19 19:34:23 +0000 |
---|---|---|
committer | Matthew Dempsky <matthew@cvs.openbsd.org> | 2014-06-19 19:34:23 +0000 |
commit | c63daf787d226622a5f1cf30507c17cc0fcc4299 (patch) | |
tree | f222888ff6444e485a5466fbbd677f73b75233ae /regress/sys/uvm | |
parent | 285751f63f0ecd8e4ebe7e0c11ce46a377530b4e (diff) |
Add a basic regress test for __MAP_NOREPLACE.
Diffstat (limited to 'regress/sys/uvm')
-rw-r--r-- | regress/sys/uvm/Makefile | 5 | ||||
-rw-r--r-- | regress/sys/uvm/mmap_noreplace/Makefile | 5 | ||||
-rw-r--r-- | regress/sys/uvm/mmap_noreplace/mmap_noreplace.c | 77 |
3 files changed, 85 insertions, 2 deletions
diff --git a/regress/sys/uvm/Makefile b/regress/sys/uvm/Makefile index b424b280ee4..6afc82988cc 100644 --- a/regress/sys/uvm/Makefile +++ b/regress/sys/uvm/Makefile @@ -1,7 +1,8 @@ -# $OpenBSD: Makefile,v 1.11 2014/06/13 07:17:54 matthew Exp $ +# $OpenBSD: Makefile,v 1.12 2014/06/19 19:34:22 matthew Exp $ SUBDIR+= minherit_zero -SUBDIR+= misc mmap_4g mmap_size mmap_fixed mmap_mod mmap_write_self +SUBDIR+= misc mmap_4g mmap_size mmap_fixed mmap_mod +SUBDIR+= mmap_noreplace mmap_write_self SUBDIR+= mmap0 .if defined(REGRESS_FULL) diff --git a/regress/sys/uvm/mmap_noreplace/Makefile b/regress/sys/uvm/mmap_noreplace/Makefile new file mode 100644 index 00000000000..50ea06c4e3c --- /dev/null +++ b/regress/sys/uvm/mmap_noreplace/Makefile @@ -0,0 +1,5 @@ +# $OpenBSD: Makefile,v 1.1 2014/06/19 19:34:22 matthew Exp $ + +PROG= mmap_noreplace + +.include <bsd.regress.mk> diff --git a/regress/sys/uvm/mmap_noreplace/mmap_noreplace.c b/regress/sys/uvm/mmap_noreplace/mmap_noreplace.c new file mode 100644 index 00000000000..9dd2b166715 --- /dev/null +++ b/regress/sys/uvm/mmap_noreplace/mmap_noreplace.c @@ -0,0 +1,77 @@ +/* $OpenBSD: mmap_noreplace.c,v 1.1 2014/06/19 19:34:22 matthew Exp $ */ +/* + * Copyright (c) 2014 Google Inc. + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#include <sys/mman.h> +#include <assert.h> +#include <string.h> +#include <unistd.h> + +#define CHECK(a) assert(a) +#define CHECK_EQ(a, b) assert((a) == (b)) +#define CHECK_NE(a, b) assert((a) != (b)) +#define CHECK_GE(a, b) assert((a) >= (b)) + +int +ismemset(void *b, int c, size_t n) +{ + unsigned char *p = b; + size_t i; + for (i = 0; i < n; i++) + if (p[i] != c) + return (0); + return (1); +} + +int +main() +{ + char *p; + long pagesize; + + pagesize = sysconf(_SC_PAGESIZE); + CHECK_GE(pagesize, 1); + + /* Allocate three pages of anonymous memory. */ + p = mmap(NULL, 3 * pagesize, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANON, -1, 0); + CHECK_NE(MAP_FAILED, p); + + /* Initialize memory to all 1 bytes. */ + memset(p, 1, 3 * pagesize); + CHECK(ismemset(p, 1, 3 * pagesize)); + + /* Map new anonymous memory over the second page. */ + CHECK_EQ(p + pagesize, mmap(p + pagesize, pagesize, + PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANON|MAP_FIXED, -1, 0)); + + /* Verify the second page is zero'd out, and the others are unscathed. */ + CHECK(ismemset(p, 1, pagesize)); + CHECK(ismemset(p + pagesize, 0, pagesize)); + CHECK(ismemset(p + 2 * pagesize, 1, pagesize)); + + /* Re-initialized memory. */ + memset(p, 1, 3 * pagesize); + CHECK(ismemset(p, 1, 3 * pagesize)); + + /* Try to map over second page with __MAP_NOREPLACE; should fail. */ + CHECK_EQ(MAP_FAILED, mmap(p, pagesize, PROT_READ|PROT_WRITE, + MAP_PRIVATE|MAP_ANON|MAP_FIXED|__MAP_NOREPLACE, -1, 0)); + + /* Verify the pages are still set. */ + CHECK(ismemset(p, 1, 3 * pagesize)); + + return (0); +} |