1 /* $OpenBSD: mmap0.c,v 1.2 2016/08/27 04:35:19 guenther 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 <errno.h> 22 #include <fcntl.h> 23 #include <stdio.h> 24 #include <sysexits.h> 25 26 27 /* 28 * Mmap allocations with len=0 must fail with EINVAL. 29 * 30 * Posix says so and the vmmap implementation may not deal well with them 31 * either. 32 */ 33 int 34 main() 35 { 36 void *ptr; 37 int errors = 0; 38 int fd; 39 40 fd = open("/dev/zero", O_RDWR, 0); 41 if (fd == -1) 42 err(EX_OSERR, "open"); 43 44 ptr = mmap(NULL, 0, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANON, -1, 0); 45 if (ptr != MAP_FAILED) { 46 warn("mmap(len=0, MAP_ANON) return %p, expected MAP_FAILED", 47 ptr); 48 errors += 1; 49 } else if (errno != EINVAL) { 50 warn("mmap(len=0, MAP_ANON) errno %d, expected %d", 51 errno, EINVAL); 52 } 53 54 ptr = mmap(NULL, 0, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0); 55 if (ptr != MAP_FAILED) { 56 warn("mmap(len=0, fd=\"/dev/zero\") returned %p, " 57 "expected MAP_FAILED", ptr); 58 errors += 1; 59 } else if (errno != EINVAL) { 60 warn("mmap(len=0, fd=\"/dev/zero\") errno %d, expected %d", 61 errno, EINVAL); 62 } 63 64 return errors; 65 } 66