Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 | /* * memory_tests.c.c * xnu_quick_test * * Created by Jerry Cottingham on 4/12/05. * Copyright 2005 Apple Computer Inc. All rights reserved. * */ #include "tests.h" #include <mach/mach.h> extern char g_target_path[ PATH_MAX ]; /* * static to localize to this compilation unit; volatile to avoid register * optimization which would prevent modification by a signal handler. */ static volatile int my_err; void bus_handler(int sig, siginfo_t *si, void *mcontext) { /* Reset global error value when we see a SIGBUS */ if (sig == SIGBUS) { _exit(0); } } /* ************************************************************************************************************** * Test madvise, mincore, minherit, mlock, mlock, mmap, mprotect, msync, munmap system calls. * todo - see if Francois has better versions of these tests... * ************************************************************************************************************** */ int memory_tests( void * the_argp ) { int my_page_size, my_status; int my_fd = -1; char * my_pathp = NULL; char * my_bufp = NULL; char * my_addr = NULL; char * my_test_page_p = NULL; ssize_t my_result; pid_t my_pid, my_wait_pid; kern_return_t my_kr; struct sigaction my_sa; my_kr = vm_allocate((vm_map_t) mach_task_self(), (vm_address_t*)&my_pathp, PATH_MAX, VM_FLAGS_ANYWHERE); if(my_kr != KERN_SUCCESS){ printf( "vm_allocate failed with error %d - \"%s\" \n", errno, strerror( errno) ); goto test_failed_exit; } *my_pathp = 0x00; strcat( my_pathp, &g_target_path[0] ); strcat( my_pathp, "/" ); /* create a test file */ my_err = create_random_name( my_pathp, 1 ); if ( my_err != 0 ) { goto test_failed_exit; } my_page_size = getpagesize( ); my_kr = vm_allocate((vm_map_t) mach_task_self(), (vm_address_t*)&my_test_page_p, my_page_size, VM_FLAGS_ANYWHERE); if(my_kr != KERN_SUCCESS){ printf( "vm_allocate failed with error %d - \"%s\" \n", errno, strerror( errno) ); goto test_failed_exit; } *my_test_page_p = 0x00; strcat( my_test_page_p, "parent data" ); /* test minherit - share a page with child, add to the string in child then * check for modification after child terminates. */ my_err = minherit( my_test_page_p, my_page_size, VM_INHERIT_SHARE ); if ( my_err == -1 ) { printf( "minherit failed with error %d - \"%s\" \n", errno, strerror( errno) ); goto test_failed_exit; } /* * spin off a child process that we will use for testing. */ my_pid = fork( ); if ( my_pid == -1 ) { printf( "fork failed with errno %d - %s \n", errno, strerror( errno ) ); goto test_failed_exit; } if ( my_pid == 0 ) { /* * child process... */ strcat( my_test_page_p, " child data" ); /* create a test file in page size chunks */ my_kr = vm_allocate((vm_map_t) mach_task_self(), (vm_address_t*)&my_bufp, (my_page_size * 10), VM_FLAGS_ANYWHERE); if(my_kr != KERN_SUCCESS){ printf( "vm_allocate failed with error %d - \"%s\" \n", errno, strerror( errno) ); my_err = -1; goto exit_child; } /* test madvise on anonymous memory */ my_err = madvise(my_bufp, (my_page_size * 10), MADV_WILLNEED); if ( my_err == -1 ) { printf("madvise WILLNEED on anon memory failed with error %d - \"%s\" \n", errno, strerror( errno ) ); my_err = -1; goto exit_child; } memset( my_bufp, 'j', (my_page_size * 10) ); my_fd = open( my_pathp, O_RDWR, 0 ); if ( my_fd == -1 ) { printf( "open call failed with error %d - \"%s\" \n", errno, strerror( errno) ); my_err = -1; goto exit_child; } /* test madvise on anonymous memory */ my_err = madvise(my_bufp, (my_page_size * 10), MADV_DONTNEED); if ( my_err == -1 ) { printf("madvise DONTNEED on anon memory failed with error %d - \"%s\" \n", errno, strerror( errno ) ); my_err = -1; goto exit_child; } my_result = write( my_fd, my_bufp, (my_page_size * 10) ); if ( my_result == -1 ) { printf( "write call failed with error %d - \"%s\" \n", errno, strerror( errno) ); my_err = -1; goto exit_child; } /* map the file into memory */ my_addr = (char *) mmap( NULL, (my_page_size * 2), (PROT_READ | PROT_WRITE), (MAP_FILE | MAP_SHARED), my_fd, 0 ); if ( my_addr == (char *) -1 ) { printf( "mmap call failed with error %d - \"%s\" \n", errno, strerror( errno) ); my_err = -1; goto exit_child; } /* make sure we got the right data mapped */ if ( *my_addr != 'j' || *(my_addr + my_page_size) != 'j' ) { printf( "did not map in correct data \n" ); my_err = -1; goto exit_child; } /* test madvise */ my_err = madvise( my_addr, (my_page_size * 2), MADV_WILLNEED ); if ( my_err == -1 ) { printf( "madvise WILLNEED call failed with error %d - \"%s\" \n", errno, strerror( errno) ); my_err = -1; goto exit_child; } my_err = madvise( my_addr, (my_page_size * 2), MADV_DONTNEED ); if ( my_err == -1 ) { printf( "madvise DONTNEED call failed with error %d - \"%s\" \n", errno, strerror( errno) ); my_err = -1; goto exit_child; } /* test mincore, mlock, mlock */ my_err = mlock( my_addr, my_page_size ); if ( my_err == -1 ) { printf( "mlock call failed with error %d - \"%s\" \n", errno, strerror( errno) ); my_err = -1; goto exit_child; } /* mybufp is about to be reused, so test madvise on anonymous memory */ my_err = madvise(my_bufp, (my_page_size * 10), MADV_FREE); if ( my_err == -1 ) { printf("madvise FREE on anon memory failed with error %d - \"%s\" \n", errno, strerror( errno ) ); my_err = -1; goto exit_child; } my_err = mincore( my_addr, 1, my_bufp ); if ( my_err == -1 ) { printf( "mincore call failed with error %d - \"%s\" \n", errno, strerror( errno) ); my_err = -1; goto exit_child; } /* page my_addr is in should be resident after mlock */ if ( (*my_bufp & MINCORE_INCORE) == 0 ) { printf( "mincore call failed to find resident page \n" ); my_err = -1; goto exit_child; } my_err = munlock( my_addr, my_page_size ); if ( my_err == -1 ) { printf( "munlock call failed with error %d - \"%s\" \n", errno, strerror( errno) ); my_err = -1; goto exit_child; } /* modify first page then use msync to push data out */ memset( my_addr, 'x', my_page_size ); my_err = msync( my_addr, my_page_size, (MS_SYNC | MS_INVALIDATE) ); if ( my_err == -1 ) { printf( "msync call failed with error %d - \"%s\" \n", errno, strerror( errno) ); my_err = -1; goto exit_child; } /* test madvise */ my_err = madvise( my_addr, (my_page_size * 2), MADV_DONTNEED ); if ( my_err == -1 ) { printf( "madvise DONTNEED call failed with error %d - \"%s\" \n", errno, strerror( errno) ); my_err = -1; goto exit_child; } /* test madvise */ my_err = madvise( my_addr, (my_page_size * 2), MADV_FREE ); if ( my_err == -1 ) { printf( "madvise FREE call failed with error %d - \"%s\" \n", errno, strerror( errno) ); my_err = -1; goto exit_child; } /* verify that the file was updated */ lseek( my_fd, 0, SEEK_SET ); bzero( (void *)my_bufp, my_page_size ); my_result = read( my_fd, my_bufp, my_page_size ); if ( my_result == -1 ) { printf( "read call failed with error %d - \"%s\" \n", errno, strerror( errno) ); my_err = -1; goto exit_child; } if ( *my_bufp != 'x' ) { printf( "msync did not flush correct data \n" ); my_err = -1; goto exit_child; } /* unmap our test page */ my_err = munmap( my_addr, (my_page_size * 2) ); if ( my_err == -1 ) { printf( "munmap call failed with error %d - \"%s\" \n", errno, strerror( errno) ); my_err = -1; goto exit_child; } my_addr = NULL; /* map the file into memory again for mprotect test */ my_addr = (char *) mmap( NULL, (my_page_size * 2), (PROT_READ | PROT_WRITE), (MAP_FILE | MAP_SHARED), my_fd, 0 ); if ( my_addr == (char *) -1 ) { printf( "mmap call failed with error %d - \"%s\" \n", errno, strerror( errno) ); my_err = -1; goto exit_child; } *my_addr = 'a'; /* test mprotect - change protection to only PROT_READ */ my_err = mprotect( my_addr, my_page_size, PROT_READ ); if ( my_err == -1 ) { printf( "mprotect call failed with error %d - \"%s\" \n", errno, strerror( errno) ); my_err = -1; goto exit_child; } my_sa.sa_sigaction = bus_handler; my_sa.sa_flags = SA_SIGINFO | SA_RESETHAND; if ((my_err = sigaction(SIGBUS, &my_sa, NULL)) != 0) { printf("sigaction call failed with error %d - \"%s\" \n", errno, strerror( errno) ); my_err = -1; goto exit_child; } my_err = -1; /* default to error out if we do NOT trigger a SIGBUS */ *my_addr = 'z'; /* should cause SIGBUS signal (we look for this at child termination within the parent) */ /* NOTREACHED */ printf("Expected SIGBUS signal, got nothing!\n"); my_err = -1; exit_child: exit( my_err ); } /* parent process - * we should get no error if the child has completed all tests successfully */ my_wait_pid = wait4( my_pid, &my_status, 0, NULL ); if ( my_wait_pid == -1 ) { printf( "wait4 failed with errno %d - %s \n", errno, strerror( errno ) ); goto test_failed_exit; } /* wait4 should return our child's pid when it exits */ if ( my_wait_pid != my_pid ) { printf( "wait4 did not return child pid - returned %d should be %d \n", my_wait_pid, my_pid ); goto test_failed_exit; } /* If we did not exit cleanly, report it */ if ( !WIFEXITED( my_status ) || (WEXITSTATUS( my_status ) != 0)) { printf( "wait4 returned child died of status - 0x%08X \n", my_status ); goto test_failed_exit; } /* make sure shared page got modified in child */ if ( strcmp( my_test_page_p, "parent data child data" ) != 0 ) { printf( "minherit did not work correctly - shared page looks wrong \n" ); goto test_failed_exit; } my_err = 0; goto test_passed_exit; test_failed_exit: my_err = -1; test_passed_exit: if ( my_pathp != NULL ) { remove( my_pathp ); vm_deallocate(mach_task_self(), (vm_address_t)my_pathp, PATH_MAX); } if ( my_test_page_p != NULL ) { vm_deallocate(mach_task_self(), (vm_address_t)my_test_page_p, my_page_size); } return( my_err ); } |