void Allocate_majority_of_heap( int smallest ) { size_t size; void *temp; puts("Allocate_majority_of_heap: "); size = malloc_free_space(); do { temp = malloc( size-16 ); if (!temp) perror("Unable to allocate from workspace"); size = malloc_free_space(); } while ( size >= smallest ); }
static void test_heap_info(void) { size_t s1, s2; void *p1; int sc; Heap_Information_block the_info; free_all_delayed_blocks(); s1 = malloc_free_space(); p1 = malloc( 512 ); s2 = malloc_free_space(); puts( "malloc_free_space - check malloc space drops after malloc" ); rtems_test_assert( s1 ); rtems_test_assert( s2 ); rtems_test_assert( s2 <= s1 ); do_free( p1 ); puts( "malloc_free_space - verify free space returns to previous value" ); s2 = malloc_free_space(); rtems_test_assert( s1 == s2 ); puts( "malloc_info - called with NULL\n" ); sc = malloc_info( NULL ); rtems_test_assert( sc == -1 ); puts( "malloc_info - check free space drops after malloc" ); sc = malloc_info( &the_info ); rtems_test_assert( sc == 0 ); s1 = the_info.Free.largest; p1 = malloc( 512 ); sc = malloc_info( &the_info ); rtems_test_assert( sc == 0 ); s2 = the_info.Free.largest; rtems_test_assert( s1 ); rtems_test_assert( s2 ); rtems_test_assert( s2 <= s1 ); do_free( p1 ); puts( "malloc_info - verify free space returns to previous value" ); sc = malloc_info( &the_info ); rtems_test_assert( sc == 0 ); rtems_test_assert( s1 == the_info.Free.largest ); }
rtems_task Init( rtems_task_argument not_used ) { int fd[2] = {0,0}; int dummy_fd[2] = {0,0}; int status = 0; void *alloc_ptr = (void *)0; puts( "*** TEST POSIX PIPE CREATION - 01 ***" ); puts( "Init - attempt to create pipe -- expect EFAULT" ); status = pipe( NULL ); rtems_test_assert( status == -1 ); rtems_test_assert( errno == EFAULT ); puts( "Init - create pipe -- OK" ); status = pipe( fd ); rtems_test_assert( status == 0 ); status = close( fd[0] ); status |= close( fd[1] ); rtems_test_assert( status == 0 ); puts( "Init - create pipe -- OK" ); status = pipe( fd ); rtems_test_assert( status == 0 ); status = close( fd[0] ); status |= close( fd[1] ); rtems_test_assert( status == 0 ); alloc_ptr = malloc( malloc_free_space() - 4 ); /* case where mkfifo fails */ puts( "Init - attempt to create pipe -- expect ENOMEM" ); status = pipe( fd ); rtems_test_assert( status == -1 ); rtems_test_assert( errno == ENOMEM ); free( alloc_ptr ); dummy_fd[0] = open( "/file01", O_RDONLY | O_CREAT, S_IRWXU ); rtems_test_assert( dummy_fd[0] != -1 ); dummy_fd[1] = open( "/file02", O_RDONLY | O_CREAT, S_IRWXU ); rtems_test_assert( dummy_fd[1] != -1 ); /* case where fifo_open for read => open fails */ puts( "Init - create pipe -- expect ENFILE" ); status = pipe( fd ); rtems_test_assert( status == -1 ); rtems_test_assert( errno == ENFILE ); status = close( dummy_fd[1] ); status |= unlink( "/file02" ); rtems_test_assert( status == 0 ); /* case where fifo_open for write => open fails */ puts( "Init - create pipe -- expect ENFILE" ); status = pipe( fd ); rtems_test_assert( status == -1 ); rtems_test_assert( errno == ENFILE ); status = close( dummy_fd[0] ); status |= unlink( "/file01" ); rtems_test_assert( status == 0 ); puts( "*** END OF TEST POSIX PIPE CREATION - 01 ***" ); rtems_test_exit( 0 ); }
rtems_task Init( rtems_task_argument argument ) { void *alloc_ptr = (void *)0; int num_opens = 0; int index = 0; TEST_BEGIN(); puts( "Creating all barriers" ); create_all_barriers(); puts( "Creating all semaphores" ); create_all_semaphores(); puts( "Creating FIFO" ); create_fifo(); puts( "Opening FIFO.. expect ENOMEM (semaphore for pipe could not be created)" ); open_fifo(ENOMEM, O_RDWR); delete_semaphore(); alloc_ptr = malloc( malloc_free_space() - 4 ); puts("Opening FIFO.. expect ENOMEM since no memory is available"); open_fifo(ENOMEM, O_RDWR); free(alloc_ptr); puts( "Opening FIFO.. expect ENOMEM (barrier-1 for pipe could not be created)" ); open_fifo(ENOMEM, O_RDWR); delete_barrier(); puts( "Opening FIFO.. expect ENOMEM (barrier-2 for pipe could not be created" ); open_fifo(ENOMEM, O_RDWR); delete_barrier(); puts( "Opening FIFO.. expect ENOMEM (semaphore-1 for pipe could not be created" ); open_fifo(ENOMEM, O_RDWR); delete_semaphore(); puts( "Opening FIFO in RDWR mode. Expect OK" ); open_fifo(0, O_RDWR); ++num_opens; puts( "Opening FIFO in non blocking RDONLY mode. Expect OK"); open_fifo(0, O_RDONLY | O_NONBLOCK); ++num_opens; puts( "Opening FIFO in non blocking WRONLY mode. Expect ENXIO"); open_fifo(ENXIO, O_WRONLY | O_NONBLOCK); ++num_opens; puts("\nMultiple opens\n"); index = 0; do { printf("%d... ", index+1); open_fifo(0, O_RDONLY | O_NONBLOCK); ++index; } while ( index < NUM_OPEN_REQ - num_opens ); TEST_END(); rtems_test_exit(0); }
rtems_task Init(rtems_task_argument ignored) { rtems_status_code sc; int i; int created; TEST_BEGIN(); printf( "Largest C program heap block available: %zu\n", malloc_free_space() ); for (i=0 ; i<MAX ; i++ ) { sc = rtems_semaphore_create( rtems_build_name('s', 'e', 'm', ' '), 1, RTEMS_DEFAULT_ATTRIBUTES, 0, &Semaphores[i] ); /* printf("Creating %i id=0x%08x\n", i, Semaphores[i]); */ if (sc == RTEMS_TOO_MANY) { printf("We run out at %i!\n", i); break; } if (sc != RTEMS_SUCCESSFUL) { printf("FAILED creating at %i\n", i); directive_failed( sc, "rtems_semaphore_create " ); rtems_test_exit( 0 ); } } created = i; if ( created == MAX ) puts( "Created all semaphores allowed in this test" ); printf( "%d semaphores created\n", i ); printf( "Largest C program heap block available: %zu\n", malloc_free_space() ); for ( i-- ; i ; i-- ) { sc = rtems_semaphore_delete( Semaphores[i] ); if (sc != RTEMS_SUCCESSFUL) { printf("FAILED deleting at %i\n", i); directive_failed( sc, "rtems_semaphore_delete " ); rtems_test_exit( 0 ); } } printf( "%d semaphores successfully deleted\n", created ); printf( "Largest C program heap block available: %zu\n", malloc_free_space() ); TEST_END(); rtems_test_exit( 0 ); }
rtems_device_driver termios_test_driver_open( rtems_device_major_number major, rtems_device_minor_number minor, void * arg ) { rtems_status_code sc; int rc; rtems_libio_open_close_args_t *args = arg; void *alloc_ptr = (void *)0; static int test = 0; size_t freeMemory; static const rtems_termios_callbacks Callbacks = { NULL, /* firstOpen */ NULL, /* lastClose */ termios_test_driver_inbyte_nonblocking, /* pollRead */ termios_test_driver_write_support, /* write */ termios_test_driver_set_attributes, /* setAttributes */ NULL, /* stopRemoteTx */ NULL, /* startRemoteTx */ 0 /* outputUsesInterrupts */ }; if ( minor > 2 ) { puts( "ERROR - Termios_testdriver - only 1 minor supported" ); rtems_test_exit(0); } freeMemory = malloc_free_space(); if( test == 0 ) { alloc_ptr = malloc( freeMemory - 4 ); sc = rtems_termios_open (major, minor, arg, &Callbacks); rtems_test_assert( sc == RTEMS_NO_MEMORY ); free( alloc_ptr ); alloc_ptr = malloc( freeMemory - 4 - 10 - sizeof( struct rtems_termios_tty ) ); sc = rtems_termios_open (major, minor, arg, &Callbacks); rtems_test_assert( sc == RTEMS_NO_MEMORY ); free( alloc_ptr ); alloc_ptr = malloc( freeMemory - 4 - 20 - sizeof( struct rtems_termios_tty ) - 128 ); sc = rtems_termios_open (major, minor, arg, &Callbacks); rtems_test_assert( sc == RTEMS_NO_MEMORY ); free( alloc_ptr ); alloc_ptr = malloc( freeMemory - 4 - 20 - sizeof( struct rtems_termios_tty ) - 128 - 80 ); sc = rtems_termios_open (major, minor, arg, &Callbacks); rtems_test_assert( sc == RTEMS_NO_MEMORY ); free( alloc_ptr ); test = 1; } sc = rtems_termios_open (major, minor, arg, &Callbacks); directive_failed( sc, "rtems_termios_open" ); puts( "Termios_test_driver - rtems_set_initial_baud - bad baud - OK" ); rc = rtems_termios_set_initial_baud( args->iop->data1, 5000 ); if ( rc != -1 ) { printf( "ERROR - return %d\n", rc ); rtems_test_exit(0); } puts( "Termios_test_driver - rtems_set_initial_baud - 38400 - OK" ); rc = rtems_termios_set_initial_baud( args->iop->data1, 38400 ); if ( rc ) { printf( "ERROR - return %d\n", rc ); rtems_test_exit(0); } return RTEMS_SUCCESSFUL; }
void fileio_print_free_heap(void) { printf("--- unused dynamic memory: %lu bytes ---\n", (unsigned long) malloc_free_space()); }
rtems_task Init( rtems_task_argument argument ) { int status = 0; void *alloc_ptr = (void *)0; char linkname_n[20] = {0}; char linkname_p[20] = {0}; int i; struct stat stat_buf; puts( "\n\n*** TEST IMFS 02 ***" ); puts( "Creating directory /dir00" ); status = mkdir( "/dir00", S_IRWXU ); rtems_test_assert( status == 0 ); puts( "Creating directory /dir00/dir01" ); status = mkdir( "/dir00/dir01", S_IRWXU ); rtems_test_assert( status == 0 ); puts( "Changing directory to /dir00" ); status = chdir( "/dir00" ); rtems_test_assert( status == 0 ); puts( "Creating link dir01-link0 for dir01" ); status = link( "dir01", "dir01-link0" ); rtems_test_assert( status == 0 ); for( i = 1 ; ; ++i ) { sprintf( linkname_p, "dir01-link%d", i-1 ); sprintf( linkname_n, "dir01-link%d", i ); printf( "\nCreating link %s for %s\n", linkname_n, linkname_p ); status = link( linkname_p, linkname_n ); if( status != 0 ) { puts("Link creation failed" ); break; } } puts( "Creating a regular node /node, RDONLY" ); status = mknod( "/node", S_IFREG | S_IRUSR, 0LL ); rtems_test_assert( status == 0 ); puts( "Creating link /node-link for /node" ); status = link( "/node" , "/node-link" ); rtems_test_assert( status == 0 ); puts( "Opening /node-link in WRONLY mode -- expect EACCES" ); status = open( "/node-link", O_WRONLY ); rtems_test_assert( status == -1 ); rtems_test_assert( errno == EACCES ); puts( "Creating a symlink /node-slink for /node" ); status = symlink( "/node" , "/node-slink" ); rtems_test_assert( status == 0 ); puts( "Opening /node-slink in WRONLY mode -- expect EACCES" ); status = open( "/node-slink", O_WRONLY ); rtems_test_assert( status == -1 ); rtems_test_assert( errno == EACCES ); puts( "Allocate most of heap" ); alloc_ptr = malloc( malloc_free_space() - 150 ); puts( "Attempt to mount a fs at /dir01 -- expect ENOMEM" ); status = mount( NULL, "dir01", "imfs", RTEMS_FILESYSTEM_READ_WRITE, NULL ); rtems_test_assert( status == -1 ); rtems_test_assert( errno == ENOMEM ); puts( "Freeing allocated memory" ); free( alloc_ptr ); puts( "Allocate most of heap" ); alloc_ptr = malloc( malloc_free_space() - 4 ); puts( "Changing directory to /" ); status = chdir( "/" ); rtems_test_assert( status == 0 ); puts( "Attempt to create /node-link-2 for /node -- expect ENOMEM" ); status = link( "/node", "/node-link-2" ); rtems_test_assert( status == -1 ); rtems_test_assert( errno == ENOMEM ); puts( "Attempt to create /node-slink-2 for /node -- expect ENOMEM" ); status = symlink( "/node", "node-slink-2" ); rtems_test_assert( status == -1 ); rtems_test_assert( errno == ENOMEM ); puts( "Freeing allocated memory" ); free( alloc_ptr ); puts( "Allocate most of heap" ); alloc_ptr = malloc( malloc_free_space() - 40 ); puts( "Attempt to create /node-slink-2 for /node -- expect ENOMEM" ); status = symlink( "/node", "node-slink-2" ); rtems_test_assert( status == -1 ); rtems_test_assert( errno == ENOMEM ); puts( "Freeing allocated memory" ); free( alloc_ptr ); puts( "Attempt to stat a hardlink -- expect ENOTSUP" ); status = lstat( "/node-link", &stat_buf ); rtems_test_assert( status == -1 ); rtems_test_assert( errno == ENOTSUP ); puts( "Changing euid to 10" ); status = seteuid( 10 ); rtems_test_assert( status == 0 ); #if defined(RTEMS_POSIX_API) puts( "Attempt chmod on /node -- expect EPERM" ); status = chmod( "/node", S_IRUSR ); rtems_test_assert( status == -1 ); rtems_test_assert( errno == EPERM ); puts( "Attempt chown on /node -- expect EPERM" ); status = chown( "/node", 10, 10 ); rtems_test_assert( status == -1 ); rtems_test_assert( errno == EPERM ); #else puts( "Attempt chmod on /node -- EPERM only when POSIX enabled" ); puts( "Attempt chown on /node -- EPERM only when POSIX enabled" ); #endif puts( "Changing euid back to 0 [root]" ); status = seteuid( 0 ); rtems_test_assert( status == 0 ); puts( "Creating a fifo -- OK" ); status = mkfifo( "/fifo", S_IRWXU ); rtems_test_assert( status == 0 ); IMFS_dump(); puts( "chown /fifo to 10 -- OK" ); status = chown( "/fifo", 10, 10 ); rtems_test_assert( status == 0 ); puts( "Changing euid to 10" ); status = seteuid( 10 ); rtems_test_assert( status == 0 ); puts( "chmod /fifo -- OK" ); status = chmod( "/fifo", S_IRWXU ); rtems_test_assert( status == 0 ); printk( "chown /fifo to %o -- OK", 0 ); status = chown( "/fifo", 0, 0 ); rtems_test_assert( status == 0 ); puts( "*** END OF TEST IMFS 02 ***" ); rtems_test_exit(0); }