]>
git.saurik.com Git - apple/xnu.git/blob - tools/tests/xnu_quick_test/memory_tests.c
5 * Created by Jerry Cottingham on 4/12/05.
6 * Copyright 2005 Apple Computer Inc. All rights reserved.
12 extern char g_target_path
[ PATH_MAX
];
14 /* **************************************************************************************************************
15 * Test madvise, mincore, minherit, mlock, mlock, mmap, mprotect, msync, munmap system calls.
16 * todo - see if Francois has better versions of these tests...
17 * **************************************************************************************************************
19 int memory_tests( void * the_argp
)
22 int my_page_size
, my_status
;
24 char * my_pathp
= NULL
;
25 char * my_bufp
= NULL
;
26 char * my_addr
= NULL
;
27 char * my_test_page_p
= NULL
;
29 pid_t my_pid
, my_wait_pid
;
31 my_pathp
= (char *) malloc( PATH_MAX
);
32 if ( my_pathp
== NULL
) {
33 printf( "malloc failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
34 goto test_failed_exit
;
37 strcat( my_pathp
, &g_target_path
[0] );
38 strcat( my_pathp
, "/" );
40 /* create a test file */
41 my_err
= create_random_name( my_pathp
, 1 );
43 goto test_failed_exit
;
46 my_page_size
= getpagesize( );
47 my_test_page_p
= (char *) malloc( my_page_size
);
48 if ( my_test_page_p
== NULL
) {
49 printf( "malloc failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
50 goto test_failed_exit
;
52 *my_test_page_p
= 0x00;
53 strcat( my_test_page_p
, "parent data" );
55 /* test minherit - share a page with child, add to the string in child then
56 * check for modification after child terminates.
58 my_err
= minherit( my_test_page_p
, my_page_size
, VM_INHERIT_SHARE
);
60 printf( "minherit failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
61 goto test_failed_exit
;
65 * spin off a child process that we will use for testing.
69 printf( "fork failed with errno %d - %s \n", errno
, strerror( errno
) );
70 goto test_failed_exit
;
76 strcat( my_test_page_p
, " child data" );
78 /* create a test file in page size chunks */
79 my_bufp
= (char *) malloc( (my_page_size
* 10) );
80 if ( my_bufp
== NULL
) {
81 printf( "malloc failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
86 memset( my_bufp
, 'j', (my_page_size
* 10) );
87 my_fd
= open( my_pathp
, O_RDWR
, 0 );
89 printf( "open call failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
94 my_result
= write( my_fd
, my_bufp
, (my_page_size
* 10) );
95 if ( my_result
== -1 ) {
96 printf( "write call failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
101 /* map the file into memory */
102 my_addr
= (char *) mmap( NULL
, (my_page_size
* 2), (PROT_READ
| PROT_WRITE
), (MAP_FILE
| MAP_SHARED
), my_fd
, 0 );
103 if ( my_addr
== (char *) -1 ) {
104 printf( "mmap call failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
109 /* make sure we got the right data mapped */
110 if ( *my_addr
!= 'j' || *(my_addr
+ my_page_size
) != 'j' ) {
111 printf( "did not map in correct data \n" );
117 my_err
= madvise( my_addr
, (my_page_size
* 2), MADV_WILLNEED
);
118 if ( my_err
== -1 ) {
119 printf( "madvise call failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
124 /* test mincore, mlock, mlock */
125 my_err
= mlock( my_addr
, my_page_size
);
126 if ( my_err
== -1 ) {
127 printf( "mlock call failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
132 my_err
= mincore( my_addr
, 1, my_bufp
);
133 if ( my_err
== -1 ) {
134 printf( "mincore call failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
138 /* page my_addr is in should be resident after mlock */
139 if ( (*my_bufp
& MINCORE_INCORE
) == 0 ) {
140 printf( "mincore call failed to find resident page \n" );
145 my_err
= munlock( my_addr
, my_page_size
);
146 if ( my_err
== -1 ) {
147 printf( "munlock call failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
152 /* modify first page then use msync to push data out */
153 memset( my_addr
, 'x', my_page_size
);
154 my_err
= msync( my_addr
, my_page_size
, (MS_SYNC
| MS_INVALIDATE
) );
155 if ( my_err
== -1 ) {
156 printf( "msync call failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
161 /* verify that the file was updated */
162 lseek( my_fd
, 0, SEEK_SET
);
163 bzero( (void *)my_bufp
, my_page_size
);
164 my_result
= read( my_fd
, my_bufp
, my_page_size
);
165 if ( my_result
== -1 ) {
166 printf( "read call failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
170 if ( *my_bufp
!= 'x' ) {
171 printf( "msync did not flush correct data \n" );
176 /* unmap our test page */
177 my_err
= munmap( my_addr
, (my_page_size
* 2) );
178 if ( my_err
== -1 ) {
179 printf( "munmap call failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
185 /* map the file into memory again for mprotect test */
186 my_addr
= (char *) mmap( NULL
, (my_page_size
* 2), (PROT_READ
| PROT_WRITE
), (MAP_FILE
| MAP_SHARED
), my_fd
, 0 );
187 if ( my_addr
== (char *) -1 ) {
188 printf( "mmap call failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
196 /* test mprotect - change protection to only PROT_READ */
197 my_err
= mprotect( my_addr
, my_page_size
, PROT_READ
);
198 if ( my_err
== -1 ) {
199 printf( "mprotect call failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
204 *my_addr
= 'z'; /* should cause SIGBUS signal (we look for this at child termination within the parent) */
215 * we should get SIGBUS exit when child tries to write to read only memory
217 my_wait_pid
= wait4( my_pid
, &my_status
, 0, NULL
);
218 if ( my_wait_pid
== -1 ) {
219 printf( "wait4 failed with errno %d - %s \n", errno
, strerror( errno
) );
220 goto test_failed_exit
;
223 /* wait4 should return our child's pid when it exits */
224 if ( my_wait_pid
!= my_pid
) {
225 printf( "wait4 did not return child pid - returned %d should be %d \n", my_wait_pid
, my_pid
);
226 goto test_failed_exit
;
229 if ( WIFSIGNALED( my_status
) && WTERMSIG( my_status
) != SIGBUS
) {
230 printf( "wait4 returned wrong signal status - 0x%02X \n", my_status
);
231 goto test_failed_exit
;
234 /* make sure shared page got modified in child */
235 if ( strcmp( my_test_page_p
, "parent data child data" ) != 0 ) {
236 printf( "minherit did not work correctly - shared page looks wrong \n" );
237 goto test_failed_exit
;
240 goto test_passed_exit
;
246 if ( my_pathp
!= NULL
) {
250 if ( my_test_page_p
!= NULL
) {
251 free( my_test_page_p
);