From 7bca1dcf599bc08ced9e92385a625e20cec50fbe Mon Sep 17 00:00:00 2001 From: Ariane van der Steldt Date: Fri, 7 Oct 2011 19:43:08 +0000 Subject: mmap with len=0 should fail with EINVAL. - posix says so - the current allocator doesn't like it --- regress/sys/uvm/Makefile | 4 +-- regress/sys/uvm/mmap0/Makefile | 4 +++ regress/sys/uvm/mmap0/mmap0.c | 64 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 70 insertions(+), 2 deletions(-) create mode 100644 regress/sys/uvm/mmap0/Makefile create mode 100644 regress/sys/uvm/mmap0/mmap0.c (limited to 'regress/sys/uvm') diff --git a/regress/sys/uvm/Makefile b/regress/sys/uvm/Makefile index b3ee6cad6d0..9b9cb696fb2 100644 --- a/regress/sys/uvm/Makefile +++ b/regress/sys/uvm/Makefile @@ -1,6 +1,6 @@ -# $OpenBSD: Makefile,v 1.7 2011/07/05 17:48:59 art Exp $ +# $OpenBSD: Makefile,v 1.8 2011/10/07 19:43:07 ariane Exp $ -SUBDIR=misc mmap_4g mmap_size mmap_fixed mmap_mod mmap_write_self +SUBDIR=misc mmap_4g mmap_size mmap_fixed mmap_mod mmap_write_self mmap0 install: diff --git a/regress/sys/uvm/mmap0/Makefile b/regress/sys/uvm/mmap0/Makefile new file mode 100644 index 00000000000..1c59ed9bed9 --- /dev/null +++ b/regress/sys/uvm/mmap0/Makefile @@ -0,0 +1,4 @@ +# $OpenBSD: Makefile,v 1.1 2011/10/07 19:43:07 ariane Exp $ +PROG=mmap0 + +.include diff --git a/regress/sys/uvm/mmap0/mmap0.c b/regress/sys/uvm/mmap0/mmap0.c new file mode 100644 index 00000000000..df4286e359f --- /dev/null +++ b/regress/sys/uvm/mmap0/mmap0.c @@ -0,0 +1,64 @@ +/* $OpenBSD: mmap0.c,v 1.1 2011/10/07 19:43:07 ariane Exp $ */ +/* + * Copyright (c) 2011 Ariane van der Steldt + * + * 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 +#include +#include +#include +#include +#include + + +/* + * Mmap allocations with len=0 must fail with EINVAL. + * + * Posix says so and the vmmap implementation may not deal well with them + * either. + */ +int +main() +{ + void *ptr; + int errors = 0; + int fd; + + fd = open("/dev/zero", O_RDWR, 0); + if (fd == -1) + err(EX_OSERR, "open"); + + ptr = mmap(NULL, 0, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANON, -1, 0); + if (ptr != MAP_FAILED) { + warn("mmap(len=0, MAP_ANON) return %p, expected MAP_FAILED", + ptr); + errors += 1; + } else if (errno != EINVAL) { + warn("mmap(len=0, MAP_ANON) errno %d, expected %d", + errno, EINVAL); + } + + ptr = mmap(NULL, 0, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0); + if (ptr != MAP_FAILED) { + warn("mmap(len=0, fd=\"/dev/zero\") returned %p, " + "expected MAP_FAILED", ptr); + errors += 1; + } else if (errno != EINVAL) { + warn("mmap(len=0, fd=\"/dev/zero\") errno %d, expected %d", + errno, EINVAL); + } + + return errors; +} -- cgit v1.2.3