xref: /openbsd/regress/sys/uvm/mmap0/mmap0.c (revision 264ca280)
1 /*	$OpenBSD: mmap0.c,v 1.1 2011/10/07 19:43:07 ariane Exp $	*/
2 /*
3  * Copyright (c) 2011 Ariane van der Steldt <ariane@stack.nl>
4  *
5  * Permission to use, copy, modify, and distribute this software for any
6  * purpose with or without fee is hereby granted, provided that the above
7  * copyright notice and this permission notice appear in all copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16  */
17 
18 #include <sys/types.h>
19 #include <sys/mman.h>
20 #include <err.h>
21 #include <fcntl.h>
22 #include <sysexits.h>
23 #include <errno.h>
24 
25 
26 /*
27  * Mmap allocations with len=0 must fail with EINVAL.
28  *
29  * Posix says so and the vmmap implementation may not deal well with them
30  * either.
31  */
32 int
33 main()
34 {
35 	void	*ptr;
36 	int	 errors = 0;
37 	int	 fd;
38 
39 	fd = open("/dev/zero", O_RDWR, 0);
40 	if (fd == -1)
41 		err(EX_OSERR, "open");
42 
43 	ptr = mmap(NULL, 0, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANON, -1, 0);
44 	if (ptr != MAP_FAILED) {
45 		warn("mmap(len=0, MAP_ANON) return %p, expected MAP_FAILED",
46 		    ptr);
47 		errors += 1;
48 	} else if (errno != EINVAL) {
49 		warn("mmap(len=0, MAP_ANON) errno %d, expected %d",
50 		    errno, EINVAL);
51 	}
52 
53 	ptr = mmap(NULL, 0, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
54 	if (ptr != MAP_FAILED) {
55 		warn("mmap(len=0, fd=\"/dev/zero\") returned %p, "
56 		    "expected MAP_FAILED", ptr);
57 		errors += 1;
58 	} else if (errno != EINVAL) {
59 		warn("mmap(len=0, fd=\"/dev/zero\") errno %d, expected %d",
60 		    errno, EINVAL);
61 	}
62 
63 	return errors;
64 }
65