/*- * Copyright (c) 2011 Advanced Computing Technologies LLC * Written by: John H. Baldwin * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ #include __FBSDID("$FreeBSD$"); /* * Test application for /dev/memfd demo driver. */ #include #include #include #include #include #include #include #include static void * mapmem(int fd, off_t offset, size_t length) { void *p; p = mmap(NULL, length, PROT_READ | PROT_WRITE, MAP_SHARED, fd, offset); if (p == MAP_FAILED) err(1, "mmap"); return (p); } static void unmapmem(void *addr, size_t len) { if (munmap(addr, len) < 0) err(1, "munmap"); } static int openmem(void) { int fd; fd = open("/dev/memfd", O_RDWR); if (fd < 0) err(1, "open(\"/dev/memfd/"); return (fd); } int main(int ac, char **av) { char *p, *q, *p2; int fd, fd2; /* Test invalid open requests. */ fd = open("/dev/memfd", O_RDONLY); assert(fd == -1); fd = open("/dev/memfd", O_WRONLY); assert(fd == -1); /* Allocate a single page. */ fd = openmem(); p = mapmem(fd, 0, getpagesize()); p[16] = 1; /* Grow to two pages. */ q = mapmem(fd, getpagesize(), getpagesize()); /* Ensure p and q are different pages. */ assert(p[16] == 1); assert(q[16] == 0); q[16] = 2; /* Make p map both pages, p's second page should shadow q. */ unmapmem(p, getpagesize()); p = mapmem(fd, 0, getpagesize() * 2); assert(p[16] == 1); assert(p[16 + getpagesize()] == 2); p[getpagesize()] = 3; assert(q[0] == 3); /* Open a second instance, should be completely separate. */ fd2 = openmem(); p2 = mapmem(fd2, 0, getpagesize() * 2); assert(p2[16] == 0); assert(p2[getpagesize()] == 0); assert(p2[16 + getpagesize()] == 0); p2[8] = 4; assert(p[8] == 0); p[8] = 5; assert(p2[8] == 4); unmapmem(p2, getpagesize() * 2); close(fd2); /* * Unmap original object completely and then remap it. Data * should be preserved. */ unmapmem(q, getpagesize()); unmapmem(p, getpagesize() * 2); p = mapmem(fd, 0, getpagesize() * 2); assert(p[0] == 0); assert(p[16] == 1); assert(p[16 + getpagesize()] == 2); assert(p[getpagesize()] == 3); assert(p[8] == 5); close(fd); printf("memfd tests passed\n"); return (0); }