From 1549beb15b8b3d96179ca7d1f95ae845b0462e82 Mon Sep 17 00:00:00 2001 From: Kevin Kirspel Date: Thu, 29 Jun 2017 10:36:44 -0400 Subject: psxtests: Add a mmap dedicated test case Updates #2859 --- testsuites/psxtests/psxmmap01/Makefile.am | 22 ++ testsuites/psxtests/psxmmap01/init.c | 344 ++++++++++++++++++++++++++++ testsuites/psxtests/psxmmap01/psxmmap01.scn | 0 testsuites/psxtests/psxmmap01/system.h | 31 +++ testsuites/psxtests/psxmmap01/test_driver.c | 67 ++++++ testsuites/psxtests/psxmmap01/test_driver.h | 14 ++ testsuites/psxtests/psxmmap01/test_helper.c | 32 +++ testsuites/psxtests/psxmmap01/test_helper.h | 41 ++++ 8 files changed, 551 insertions(+) create mode 100644 testsuites/psxtests/psxmmap01/Makefile.am create mode 100644 testsuites/psxtests/psxmmap01/init.c create mode 100644 testsuites/psxtests/psxmmap01/psxmmap01.scn create mode 100644 testsuites/psxtests/psxmmap01/system.h create mode 100644 testsuites/psxtests/psxmmap01/test_driver.c create mode 100644 testsuites/psxtests/psxmmap01/test_driver.h create mode 100644 testsuites/psxtests/psxmmap01/test_helper.c create mode 100644 testsuites/psxtests/psxmmap01/test_helper.h (limited to 'testsuites/psxtests/psxmmap01') diff --git a/testsuites/psxtests/psxmmap01/Makefile.am b/testsuites/psxtests/psxmmap01/Makefile.am new file mode 100644 index 0000000000..57b008719e --- /dev/null +++ b/testsuites/psxtests/psxmmap01/Makefile.am @@ -0,0 +1,22 @@ + +rtems_tests_PROGRAMS = psxmmap01 +psxmmap01_SOURCES = init.c test_helper.c test_driver.c system.h + +dist_rtems_tests_DATA = psxmmap01.scn + +include $(RTEMS_ROOT)/make/custom/@RTEMS_BSP@.cfg +include $(top_srcdir)/../automake/compile.am +include $(top_srcdir)/../automake/leaf.am + + +AM_CPPFLAGS += -I$(top_srcdir)/include +AM_CPPFLAGS += -I$(top_srcdir)/../support/include + +LINK_OBJS = $(psxmmap01_OBJECTS) +LINK_LIBS = $(psxmmap01_LDLIBS) + +psxmmap01$(EXEEXT): $(psxmmap01_OBJECTS) $(psxmmap01_DEPENDENCIES) + @rm -f psxmmap01$(EXEEXT) + $(make-exe) + +include $(top_srcdir)/../automake/local.am diff --git a/testsuites/psxtests/psxmmap01/init.c b/testsuites/psxtests/psxmmap01/init.c new file mode 100644 index 0000000000..85e884a2ef --- /dev/null +++ b/testsuites/psxtests/psxmmap01/init.c @@ -0,0 +1,344 @@ +/*- + * Copyright (c) 2009 Simon L. Nielsen , + * Bjoern A. Zeeb + * + * 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. + * + * $FreeBSD$ + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#define CONFIGURE_INIT +#include "system.h" +#include "pritime.h" +#include "test_helper.h" +#include "test_driver.h" + +#include + +#include +#include +#include + +const char rtems_test_name[] = "PSX MMAP01"; +const char test_driver_name[] = "/dev/test_driver"; + +static const struct { + void *addr; + int ok; +} map_at_zero_tests[] = { + { (void *)0, 0 }, + { (void *)1, 0 }, + { (void *)(PAGE_SIZE - 1), 0 }, + { (void *)PAGE_SIZE, 1 }, + { (void *)-1, 0 }, + { (void *)(-PAGE_SIZE), 0 }, + { (void *)(-1 - PAGE_SIZE), 0 }, + { (void *)(-1 - PAGE_SIZE - 1), 0 }, + { (void *)(0x1000 * PAGE_SIZE), 1 }, +}; + +#define MAP_AT_ZERO_NITEMS (sizeof(map_at_zero_tests) / sizeof(map_at_zero_tests[0])) + +static void* checked_mmap( + int prot, + int flags, + int fd, + int error, + const char *msg +) +{ + void *p; + int pagesize, err; + + rtems_test_assert((pagesize = getpagesize()) > 0); + p = mmap(NULL, pagesize, prot, flags, fd, 0); + if (p == MAP_FAILED) { + if (error == 0) + mmap_test_assert(0, "%s failed with errno %d\n", msg, errno); + else { + err = errno; + mmap_test_assert_equal(error, err, + "%s failed with wrong errno %d (expected %d)\n", msg, + errno, error); + } + } else { + mmap_test_check(error == 0, "%s succeeded\n", msg); + munmap(p, pagesize); + } + return p; +} + +static void mmap_map_at_zero( void ) +{ + void *p; + unsigned int i; + + for (i = 0; i < MAP_AT_ZERO_NITEMS; i++) { + p = mmap((void *)map_at_zero_tests[i].addr, PAGE_SIZE, + PROT_READ | PROT_WRITE | PROT_EXEC, MAP_ANON | MAP_FIXED | MAP_PRIVATE, + -1, 0); + if (p == MAP_FAILED) { + mmap_test_assert( map_at_zero_tests[i].ok == 0, + "mmap(%08lX, ...) failed\n", (uint32_t)map_at_zero_tests[i].addr ); + } else { + mmap_test_check( map_at_zero_tests[i].ok == 1, + "mmap(%p, ...) succeeded: p=%p\n", + map_at_zero_tests[i].addr, p ); + } + } +} + +static void mmap_bad_arguments( void ) +{ + int devfd, pagesize, shmfd, zerofd; + void* p; + + rtems_test_assert((pagesize = getpagesize()) > 0); + rtems_test_assert((devfd = open(&test_driver_name[0], O_RDONLY)) >= 0); + rtems_test_assert((shmfd = shm_open("/shm", O_CREAT | O_RDWR, 0644)) >= 0); + rtems_test_assert(ftruncate(shmfd, pagesize) == 0); + rtems_test_assert((zerofd = open("/dev/zero", O_RDONLY)) >= 0); + + /* + * These should normally work on FREEBSD. Test cases below that fail are + * due to unsupported features in RTEMS. + */ + checked_mmap(PROT_READ | PROT_WRITE, MAP_ANON, -1, 0, + "simple MAP_ANON"); + checked_mmap(PROT_READ | PROT_WRITE, MAP_SHARED, shmfd, 0, + "simple shm fd shared"); + checked_mmap(PROT_READ | PROT_WRITE, MAP_PRIVATE, shmfd, 0, + "simple shm fd private"); + /* RTEMS cannot protect against writes so this will fail */ + checked_mmap(PROT_READ, MAP_SHARED, zerofd, ENOTSUP, + "simple /dev/zero shared"); + /* + * Repeat with no write protection. Will fail because of unimplemented + * mmap handler in /dev/zero. + */ + checked_mmap(PROT_READ | PROT_WRITE, MAP_SHARED, zerofd, ENOTSUP, + "simple /dev/zero shared"); + /* RTEMS /dev/zero is a character device so this will fail */ + checked_mmap(PROT_READ | PROT_WRITE, MAP_PRIVATE, zerofd, EINVAL, + "simple /dev/zero private"); + /* RTEMS cannot protect against writes so this will fail */ + checked_mmap(PROT_READ, MAP_SHARED, devfd, ENOTSUP, + "simple test driver shared"); + /* + * Repeat with no write protection. Should fail because of unimplemented + * mmap handler in /dev/null. + */ + p = checked_mmap(PROT_READ | PROT_WRITE, MAP_SHARED, devfd, 0, + "simple test driver shared"); + rtems_test_assert(p == &test_data[0]); + + /* Extra PROT flags. */ + checked_mmap(PROT_READ | PROT_WRITE | 0x100000, MAP_ANON, -1, EINVAL, + "MAP_ANON with extra PROT flags"); + checked_mmap(0xffff, MAP_SHARED, shmfd, EINVAL, + "shm fd with garbage PROT"); + + /* Undefined flag. */ + checked_mmap(PROT_READ | PROT_WRITE, MAP_ANON | MAP_RESERVED0080, -1, + EINVAL, "Undefined flag"); + + /* Both MAP_SHARED and MAP_PRIVATE */ + checked_mmap(PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE | + MAP_SHARED, -1, EINVAL, "MAP_ANON with both SHARED and PRIVATE"); + checked_mmap(PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_SHARED, shmfd, + EINVAL, "shm fd with both SHARED and PRIVATE"); + + /* At least one of MAP_SHARED or MAP_PRIVATE without ANON */ + checked_mmap(PROT_READ | PROT_WRITE, 0, shmfd, EINVAL, + "shm fd without sharing flag"); + + /* MAP_ANON with sharing flag. Will fail on RTEMS*/ + checked_mmap(PROT_READ | PROT_WRITE, MAP_ANON | MAP_SHARED, -1, EINVAL, + "shared MAP_ANON"); + /* MAP_ANON with private flag*/ + checked_mmap(PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE, -1, 0, + "private MAP_ANON"); + + /* MAP_ANON should require an fd of -1. */ + checked_mmap(PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE, 0, EINVAL, + "MAP_ANON with fd != -1"); + + /* + * Writable MAP_SHARED should fail on read-only descriptors. Will fail + * on RTEMS because of unimplemented mmap handler in /dev/null and the fact + * that there is no read only protection. + */ + checked_mmap(PROT_READ | PROT_WRITE, MAP_SHARED, zerofd, ENOTSUP, + "MAP_SHARED of read-only /dev/zero"); + + /* + * Character devices other than /dev/zero do not support private + * mappings. RTEMS cannot protect against writes so this will fail with + * ENOTSUP + */ + checked_mmap(PROT_READ, MAP_PRIVATE, devfd, ENOTSUP, + "MAP_PRIVATE of test driver"); + + /* + * Repeat with no write protection. + */ + checked_mmap(PROT_READ | PROT_WRITE, MAP_PRIVATE, devfd, EINVAL, + "MAP_PRIVATE of test driver"); + + close(devfd); + close(shmfd); + close(zerofd); +} + +static void mmap_dev_zero_private( void ) +{ + char *p1, *p2, *p3; + int fd, pagesize; + /*int i*/ + + rtems_test_assert((pagesize = getpagesize()) > 0); + rtems_test_assert((fd = open("/dev/zero", O_RDONLY)) >= 0); + + /* This should not return MAP_FAILED but does on RTEMS */ + p1 = mmap(NULL, pagesize, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0); + /*rtems_test_assert(p1 != MAP_FAILED);*/ + rtems_test_assert(p1 == MAP_FAILED); + + /* This should not return MAP_FAILED but does on RTEMS */ + p2 = mmap(NULL, pagesize, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0); + /*rtems_test_assert(p2 != MAP_FAILED);*/ + rtems_test_assert(p2 == MAP_FAILED); + + /* These tests can not run due to failures above */ + /*for (i = 0; i < pagesize; i++) + mmap_test_assert_equal(0, p1[i], "byte at p1[%d] is %x", i, p1[i]); + + rtems_test_assert(memcmp(p1, p2, pagesize) == 0); + + p1[0] = 1; + + rtems_test_assert(p2[0] == 0); + + p2[0] = 2; + + rtems_test_assert(p1[0] == 1);*/ + + /* This should not return MAP_FAILED but does on RTEMS */ + p3 = mmap(NULL, pagesize, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0); + /*rtems_test_assert(p3 != MAP_FAILED);*/ + rtems_test_assert(p3 == MAP_FAILED); + + /*rtems_test_assert(p3[0] == 0); + + munmap(p1, pagesize); + munmap(p2, pagesize); + munmap(p3, pagesize);*/ + close(fd); +} + +static void mmap_dev_zero_shared( void ) +{ + char *p1, *p2, *p3; + int fd, pagesize; + /*int i*/ + + rtems_test_assert((pagesize = getpagesize()) > 0); + rtems_test_assert((fd = open("/dev/zero", O_RDWR)) >= 0); + + /* This should not return MAP_FAILED but does on RTEMS */ + p1 = mmap(NULL, pagesize, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); + /*rtems_test_assert(p1 != MAP_FAILED);*/ + rtems_test_assert(p1 == MAP_FAILED); + + /* This should not return MAP_FAILED but does on RTEMS */ + p2 = mmap(NULL, pagesize, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); + /*rtems_test_assert(p2 != MAP_FAILED);*/ + rtems_test_assert(p2 == MAP_FAILED); + + /* These tests can not run due to failures above */ + /*for (i = 0; i < pagesize; i++) + mmap_test_assert_equal(0, p1[i], "byte at p1[%d] is %x", i, p1[i]); + + rtems_test_assert(memcmp(p1, p2, pagesize) == 0); + + p1[0] = 1; + + rtems_test_assert(p2[0] == 0); + + p2[0] = 2; + + rtems_test_assert(p1[0] == 1);*/ + + p3 = mmap(NULL, pagesize, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); + /*rtems_test_assert(p3 != MAP_FAILED);*/ + rtems_test_assert(p3 == MAP_FAILED); + + /*rtems_test_assert(p3[0] == 0); + + munmap(p1, pagesize); + munmap(p2, pagesize); + munmap(p3, pagesize);*/ + close(fd); +} + +void *POSIX_Init( + void *argument +) +{ + int rv; + + TEST_BEGIN(); + + rv = IMFS_make_generic_node( + &test_driver_name[0], + S_IFCHR | S_IRWXU | S_IRWXG | S_IRWXO, + &node_control, + NULL + ); + rtems_test_assert(rv == 0); + + puts( "Init: mmap - map at zero" ); + mmap_map_at_zero(); + puts( "Init: mmap - bad arguments" ); + mmap_bad_arguments(); + /* + * The following test does not work on RTEMS because /dev/zero is + * implemented as a character device which does not support MAP_PRIVATE. + */ + puts( "Init: mmap - /dev/zero private" ); + mmap_dev_zero_private(); + /* + * The following test does not work on RTEMS because /dev/zero does not + * implemented the mmap handler. + */ + puts( "Init: mmap - /dev/zero shared" ); + mmap_dev_zero_shared(); + + TEST_END(); + + rtems_test_exit(0); + return 0; +} diff --git a/testsuites/psxtests/psxmmap01/psxmmap01.scn b/testsuites/psxtests/psxmmap01/psxmmap01.scn new file mode 100644 index 0000000000..e69de29bb2 diff --git a/testsuites/psxtests/psxmmap01/system.h b/testsuites/psxtests/psxmmap01/system.h new file mode 100644 index 0000000000..2241f881c4 --- /dev/null +++ b/testsuites/psxtests/psxmmap01/system.h @@ -0,0 +1,31 @@ +/* + * Copyright (c) 2017 Kevin Kirspel. + * + * The license and distribution terms for this file may be + * found in the file LICENSE in this distribution or at + * http://www.rtems.org/license/LICENSE. + */ + +#include +#include +#include + +void *POSIX_Init( + void *argument +); + +#define CONFIGURE_APPLICATION_NEEDS_CLOCK_DRIVER +#define CONFIGURE_APPLICATION_NEEDS_CONSOLE_DRIVER +#define CONFIGURE_APPLICATION_NEEDS_ZERO_DRIVER +#define CONFIGURE_APPLICATION_NEEDS_STUB_DRIVER + +#define CONFIGURE_INITIAL_EXTENSIONS RTEMS_TEST_INITIAL_EXTENSION + +#define CONFIGURE_MAXIMUM_POSIX_THREADS 1 +#define CONFIGURE_LIBIO_MAXIMUM_FILE_DESCRIPTORS 10 +#define CONFIGURE_MAXIMUM_POSIX_SHMS 1 +#define CONFIGURE_MEMORY_OVERHEAD 10 + +#define CONFIGURE_POSIX_INIT_THREAD_TABLE + +#include diff --git a/testsuites/psxtests/psxmmap01/test_driver.c b/testsuites/psxtests/psxmmap01/test_driver.c new file mode 100644 index 0000000000..87719b99d6 --- /dev/null +++ b/testsuites/psxtests/psxmmap01/test_driver.c @@ -0,0 +1,67 @@ +/* + * Copyright (c) 2017 Kevin Kirspel. + * + * The license and distribution terms for this file may be + * found in the file LICENSE in this distribution or at + * http://www.rtems.org/license/LICENSE. + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include "test_driver.h" + +#include + +char test_data[PAGE_SIZE]; + +static int handler_fstat( + const rtems_filesystem_location_info_t *loc, + struct stat *buf +) +{ + buf->st_mode = S_IFCHR | S_IRWXU | S_IRWXG | S_IRWXO; + return 0; +} + +static ssize_t handler_mmap( + rtems_libio_t *iop, + void **addr, + size_t len, + int prot, + off_t off +) +{ + if ( len <= sizeof(test_data) ) { + *addr = test_data; + } else { + *addr = NULL; + } + return 0; +} + +static const rtems_filesystem_file_handlers_r node_handlers = { + .open_h = rtems_filesystem_default_open, + .close_h = rtems_filesystem_default_close, + .read_h = rtems_filesystem_default_read, + .write_h = rtems_filesystem_default_write, + .ioctl_h = rtems_filesystem_default_ioctl, + .lseek_h = rtems_filesystem_default_lseek, + .fstat_h = handler_fstat, + .ftruncate_h = rtems_filesystem_default_ftruncate, + .fsync_h = rtems_filesystem_default_fsync_or_fdatasync, + .fdatasync_h = rtems_filesystem_default_fsync_or_fdatasync, + .fcntl_h = rtems_filesystem_default_fcntl, + .poll_h = rtems_filesystem_default_poll, + .kqfilter_h = rtems_filesystem_default_kqfilter, + .mmap_h = handler_mmap, + .readv_h = rtems_filesystem_default_readv, + .writev_h = rtems_filesystem_default_writev +}; + +const IMFS_node_control node_control = IMFS_GENERIC_INITIALIZER( + &node_handlers, + IMFS_node_initialize_default, + IMFS_node_destroy_default +); diff --git a/testsuites/psxtests/psxmmap01/test_driver.h b/testsuites/psxtests/psxmmap01/test_driver.h new file mode 100644 index 0000000000..5afdb9b46e --- /dev/null +++ b/testsuites/psxtests/psxmmap01/test_driver.h @@ -0,0 +1,14 @@ +/* + * Copyright (c) 2017 Kevin Kirspel. + * + * The license and distribution terms for this file may be + * found in the file LICENSE in this distribution or at + * http://www.rtems.org/license/LICENSE. + */ + +#include +#include +#include + +extern const IMFS_node_control node_control; +extern char test_data[PAGE_SIZE]; diff --git a/testsuites/psxtests/psxmmap01/test_helper.c b/testsuites/psxtests/psxmmap01/test_helper.c new file mode 100644 index 0000000000..cab8e9138b --- /dev/null +++ b/testsuites/psxtests/psxmmap01/test_helper.c @@ -0,0 +1,32 @@ +/* + * Copyright (c) 2017 Kevin Kirspel. + * + * The license and distribution terms for this file may be + * found in the file LICENSE in this distribution or at + * http://www.rtems.org/license/LICENSE. + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include "test_helper.h" + +#include +#include + +void fail_check( + const char *file, + const size_t line, + const char *fmt, + ... +) +{ + va_list ap; + + printf( "%s: %d: ", file, line ); + + va_start(ap, fmt); + vprintf(fmt, ap); + va_end(ap); +} diff --git a/testsuites/psxtests/psxmmap01/test_helper.h b/testsuites/psxtests/psxmmap01/test_helper.h new file mode 100644 index 0000000000..65ca3c11bd --- /dev/null +++ b/testsuites/psxtests/psxmmap01/test_helper.h @@ -0,0 +1,41 @@ +/* + * Copyright (c) 2017 Kevin Kirspel. + * + * The license and distribution terms for this file may be + * found in the file LICENSE in this distribution or at + * http://www.rtems.org/license/LICENSE. + */ + +#include + +#define mmap_test_assert(__exp, fmt, ...) \ + do { \ + if (!(__exp)) { \ + fail_check(__FILE__, __LINE__, fmt, ##__VA_ARGS__); \ + rtems_test_exit(0); \ + } \ + } while (0) + +#define mmap_test_check(__exp, fmt, ...) \ + do { \ + if (!(__exp)) { \ + fail_check(__FILE__, __LINE__, fmt, ##__VA_ARGS__); \ + } else { \ + rtems_test_assert(__exp); \ + }\ + } while (0) + +#define mmap_test_assert_equal(expected, actual, fmt, ...) \ + mmap_test_assert((expected) == (actual), "%s != %s: " fmt, \ + #expected, #actual, ##__VA_ARGS__) + +#define mmap_test_check_equal(expected, actual, fmt, ...) \ + mmap_test_check((expected) == (actual), "%s != %s: " fmt, \ + #expected, #actual, ##__VA_ARGS__) + +extern void fail_check( + const char *file, + const size_t line, + const char *fmt, + ... +); -- cgit v1.2.3