4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License"). You may not use this file except in compliance
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 #pragma ident "%Z%%M% %I% %E% SMI"
30 * Abstract Machine Test; executes memory access tests to show
31 * compliance with Common Criteria object reuse and process address
32 * space separation requirements.
36 #include <iso/stdlib_iso.h>
43 #include <sys/param.h>
44 #include <sys/resource.h>
45 #include <sys/sysmacros.h>
46 #include <sys/types.h>
53 #define CHILD_SLEEP_PERIOD 2
54 #define PARENT_SLEEP_PERIOD 1
55 #define SIG_EVENT SIGUSR1
57 #define PASS 0 /* test passed, no SEGV */
58 #define FAIL_ZERO 1 /* expected to read zero, didn't */
59 #define FAIL_SEGV 2 /* expected good read or write, didn't */
60 #define PASS_SEGV 3 /* expected SEGV, got it */
61 #define FAIL_ABORT 4 /* test logic error */
63 #define PH_VALID 0 /* arg for probe_hole -- do valid memory */
65 #define PH_INVALID 1 /* do illegal memory access */
67 #define WASTE_PAGES 8 /* a guess at where virgin stack space */
68 /* is likely to exist */
69 #define STACK_SLOP 256 /* a guess at how far below current end */
70 /* of stack I'll find unused space */
72 #if !defined(TEXT_DOMAIN)
73 #define TEXT_DOMAIN "SYS_TEST"
76 extern int _end
; /* first address after the end of initialized data */
78 static int data_boundary_test();
79 static void handler(int);
80 static int memory_not_shared_after_use();
81 static int memory_allocation_not_shared();
82 static int memory_type(const char *);
83 static void print_message(char *);
84 static void probe_data_area(void);
85 static void probe_hole(int);
86 static void probe_stack(void);
87 static void probe_text_area(void);
88 static void segv_action(int, siginfo_t
*, void *);
89 static void set_handler(int);
90 static int test_stack_end_of_hole();
91 static int text_area_not_writeable();
93 static int done_memory_grab
= 0;
95 static int handler_exit_code
;
101 main(int argc
, char *argv
[])
107 /* Internationalization */
108 (void) setlocale(LC_ALL
, "");
109 (void) textdomain(TEXT_DOMAIN
);
114 /* Pull out argument provided */
115 /* -s silent mode, no status or error messages. */
116 if (strncmp(argv
[1], "-s", 4) == 0)
117 silent
= SILENT_MODE
;
120 (void) fprintf(stderr
, gettext(
121 "Wrong argument, USAGE: amt [-s]\n"));
124 } else if (argc
!= 1) {
125 /* Illegal number of arguments. */
126 (void) fprintf(stderr
, gettext(
127 "Wrong usage, USAGE: amt [-s]\n"));
130 bitsize
= memory_type(argv
[0]);
132 if (silent
== NOT_SILENT
)
133 (void) printf(gettext(
134 "\n\nAMT Test Program -- %d bit application\n"
135 "================\n"), bitsize
);
137 * test_stack_end_of_hole must be the first test, or the stack
138 * is of an unknown size.
140 if ((status
= test_stack_end_of_hole()) == EXIT_FAILURE
) {
143 print_message(gettext("TEST 1 FAILED\n"));
144 } else if (status
== FAIL_ABORT
) {
145 /* Test logic failure */
147 print_message(gettext("FAIL: Logic error in test\n"));
148 } else if (status
== EXIT_SUCCESS
)
149 print_message(gettext("TEST 1 PASSED\n"));
151 /* Carry out test 2 */
152 if (data_boundary_test() != EXIT_SUCCESS
) {
154 print_message(gettext("TEST 2 FAILED\n"));
156 print_message(gettext("TEST 2 PASSED\n"));
158 /* Carry out test 3 */
159 if (text_area_not_writeable() != EXIT_SUCCESS
) {
161 print_message(gettext("TEST 3 FAILED\n"));
163 print_message(gettext("TEST 3 PASSED\n"));
165 /* Carry out test 4 */
166 if (memory_not_shared_after_use() != EXIT_SUCCESS
) {
168 print_message(gettext("TEST 4 FAILED\n"));
170 print_message(gettext("TEST 4 PASSED\n"));
172 /* Carry out test 5 */
173 if (memory_allocation_not_shared() != EXIT_SUCCESS
) {
175 print_message(gettext("TEST 5 FAILED\n"));
177 print_message(gettext("TEST 5 PASSED\n"));
179 if (silent
== NOT_SILENT
) {
181 (void) printf(gettext("\n %d TESTS FAILED\n\n"),
184 (void) printf(gettext("\nTESTS SUCCEEDED\n\n"));
190 * Test the data boundaries. First test inside the data area at the boundary
191 * of the "hole" area. Second test inside the data area at the text area
192 * boundary. Both should pass.
197 int exit_status
= EXIT_SUCCESS
;
201 print_message(gettext("\n\nTest 2- Data Side Boundary Test.\n"));
203 if ((pid
= fork()) == -1) {
204 print_message(gettext("Fork failed\n"));
205 return (EXIT_FAILURE
);
206 } else if (pid
== 0) { /* Am I my child? */
207 set_handler(SIGSEGV
);
209 /* probe_data_area() does exit() */
213 (void) wait(&status
);
214 status
= WEXITSTATUS(status
);
217 print_message(gettext(
218 "PASS: Successful read/write in data area.\n"));
219 else if (status
== FAIL_SEGV
) {
220 print_message(gettext(
221 "FAIL: Caught a segmentation fault while "
222 "attempting to write to the data area.\n"));
223 exit_status
= EXIT_FAILURE
;
225 (void) printf(gettext("Test program failure: %d\n"),
227 exit_status
= EXIT_FAILURE
;
229 return (exit_status
);
240 /* set handler status */
241 handler_exit_code
= FAIL_SEGV
;
244 * Get an address in the data area, near to the "hole".
245 * sbrk returns prior address value; rather than calculating
246 * the sbrk result, sbrk is called twice, so address points
247 * to the new end of data
249 (void) sbrk(PAGESIZE
);
252 * Get to the inside edge of a page boundary
253 * two integer words short of a new page
255 p
= ((int *)P2ROUNDUP((uintptr_t)address
, PAGESIZE
)) - 2;
257 /* Try writing to it, shouldn't cause a segmentation fault. */
260 /* Should be able to read back with no problems. */
264 * Get an address near the text area boundary, but in the data
265 * area. _etext rounded up a page isn't correct since the
266 * initialized data area isn't writeable.
268 * Future versions should consider handling initialized data
269 * separately -- writing to initialized data should generate
274 /* Try writing to it, should succeed. */
277 /* Should be able to read back with no problems. */
284 * Test that we cannot write to the text area. An attempt to write to
285 * the text area will result in a segmentation fault. So if we catch it,
286 * test has succeed, else it has failed.
289 text_area_not_writeable()
291 int exit_status
= EXIT_SUCCESS
;
295 print_message(gettext(
296 "\n\nTest 3- Text Area Not Writeable\n"
297 "Verify that a write to the text space does not cause "
298 "a write to the executable\n"
299 "file from which it came, or to another process which "
300 "shares that text.\n"));
302 if ((pid
= fork()) == -1) {
303 print_message(gettext("Fork failed\n"));
304 return (EXIT_FAILURE
);
305 } else if (pid
== 0) { /* Am I my child? */
306 set_handler(SIGSEGV
);
308 /* probe_text_area() does exit() */
312 (void) wait(&status
);
313 status
= WEXITSTATUS(status
);
315 if (status
== PASS
) {
316 print_message(gettext(
317 "FAIL: We did not cause a segmentation fault.\n"));
318 exit_status
= EXIT_FAILURE
;
319 } else if (status
== FAIL_SEGV
) {
320 print_message(gettext(
321 "PASS: Caught the segmentation fault, "
322 "meaning we can't write to text area.\n"));
324 (void) printf(gettext(
325 "Test program failure: %d\n"), status
);
326 exit_status
= EXIT_FAILURE
;
328 return (exit_status
);
332 * write to text area, trigger a SEGV
337 handler_exit_code
= FAIL_SEGV
;
338 *(caddr_t
)probe_text_area
= 0xff;
343 * Test that when we set some values and fork a process, when the child
344 * writes to these inherited values, the parents copies are not changed.
347 memory_not_shared_after_use()
351 int exit_status
= EXIT_SUCCESS
;
353 print_message(gettext("\n\nTest 4- Memory Not Shared After Write\n"
354 "Verify that anonymous memory initially shared by two "
355 "processes (e.g. after a\n"
356 "fork) is not shared after either process writes "
359 if ((pid
= fork()) == -1) {
360 print_message(gettext("Fork failed\n"));
361 return (EXIT_FAILURE
);
362 } else if (pid
== 0) { /* I am the child. */
364 * Change child value; this should not change
369 /* Wait for parent to test value */
370 (void) sleep(CHILD_SLEEP_PERIOD
);
374 /* Wait for child to do its stuff. */
375 (void) sleep(PARENT_SLEEP_PERIOD
);
378 exit_status
= EXIT_SUCCESS
;
380 exit_status
= EXIT_FAILURE
;
382 return (exit_status
);
386 * If we fork a process and then allocate some memory in that process,
387 * we should not see any memory changes in the parent.
390 memory_allocation_not_shared()
398 void (*old_handler
) ();
400 print_message(gettext(
401 "\n\nTest 5- Memory Allocation is Not Shared\n"
402 "Verify that newly allocated memory in one of two "
403 "processes created by forking\n"
404 "does not result in newly allocated memory in the other.\n"));
406 /* Save Size of data area and 1st block address of "hole" */
407 hole_start
= (caddr_t
)sbrk(0);
409 if (silent
== NOT_SILENT
)
410 (void) printf(gettext(
411 "Parent address of hole before child change: %08X\n"),
414 /* Set handler for signal SIG_EVENT (define at start) */
415 old_handler
= signal(SIG_EVENT
, &handler
);
416 if (old_handler
== SIG_ERR
) {
417 print_message(gettext(
418 "Can't establish signal handler, test failed\n"));
419 return (EXIT_FAILURE
);
422 if ((pid
= fork()) == -1) {
423 print_message(gettext("Fork failed\n"));
424 return (EXIT_FAILURE
);
425 } else if (pid
== 0) { /* We are the child. */
427 if (silent
== NOT_SILENT
)
428 (void) printf(gettext(
429 "Child end of hole before change: %08X\n"),
432 if (brk((address
+PAGESIZE
)) != 0) {
433 print_message(gettext(
434 "Can't change start of hole address.\n"));
439 if (silent
== NOT_SILENT
)
440 (void) printf(gettext(
441 "Child end of hole after change: %08X\n"),
444 /* Tell the parent we're done. */
445 parent_pid
= getppid();
446 if (sigsend(P_PID
, parent_pid
, SIG_EVENT
) != 0) {
447 print_message(gettext("Can't send signal to parent, "
452 /* Sleep before exiting to allow parent to finish processing. */
453 (void) sleep(CHILD_SLEEP_PERIOD
);
456 /* Wait for child to do its work. */
457 (void) sleep(PARENT_SLEEP_PERIOD
);
459 if (done_memory_grab
!= 1) {
460 print_message(gettext(
461 "Child failed to do memory alterations, "
463 return (EXIT_FAILURE
);
466 hole_after
= sbrk(0);
467 if (silent
== NOT_SILENT
)
468 (void) printf(gettext(
469 "Parent address of hole after child change: "
470 "%08X\n"), hole_after
);
472 /* Test size of hole and data region. */
473 if (hole_start
== hole_after
)
474 print_message(gettext(
475 "PASS: Hole is same size in parent.\n"));
477 print_message(gettext(
478 "FAIL: Hole is a different size.\n"));
479 exit_status
= EXIT_FAILURE
;
482 /* Wait for child to finish. */
485 if (signal(SIG_EVENT
, old_handler
) == SIG_ERR
) {
486 print_message(gettext("Couldn't put back old signal handler, "
488 return (EXIT_FAILURE
);
490 return (exit_status
);
494 print_message(char *message
)
496 if (silent
== NOT_SILENT
)
497 (void) printf("%s", message
);
501 test_stack_end_of_hole()
505 int exit_status
= EXIT_SUCCESS
;
507 print_message(gettext("\n\nTest 1- stack Side Boundary Test\n"));
509 /* sub test 1: the space the stack grows into is zero */
511 if ((pid
= fork()) == -1) {
512 print_message(gettext("Fork failed\n"));
513 return (EXIT_FAILURE
);
514 } else if (pid
== 0) { /* Am I my child? */
515 set_handler(SIGSEGV
);
517 /* probe_stack() does exit */
521 (void) wait(&status
);
522 status
= WEXITSTATUS(status
);
524 if (status
== FAIL_ZERO
) {
525 print_message(gettext("Fail with non-zero read.\n"));
526 exit_status
= EXIT_FAILURE
;
527 } else if (status
!= PASS
) {
528 print_message(gettext("Test program failure\n"));
529 exit_status
= EXIT_FAILURE
;
531 /* sub test 2: the space in hole is not readable */
533 if ((pid
= fork()) == -1) {
534 print_message(gettext("Fork failed\n"));
535 return (EXIT_FAILURE
);
536 } else if (pid
== 0) { /* Am I my child? */
537 set_handler(SIGSEGV
);
539 /* probe_hole does exit */
540 probe_hole(PH_INVALID
);
543 (void) wait(&status
);
544 status
= WEXITSTATUS(status
);
546 if (status
== FAIL_SEGV
) {
548 gettext("Fail (SEGV expected, not received).\n"));
549 exit_status
= EXIT_FAILURE
;
550 } else if (status
!= PASS_SEGV
) {
551 print_message(gettext("Test program failure.\n"));
552 exit_status
= EXIT_FAILURE
;
555 /* sub test 3: the space in new page below hole is zero */
557 if ((pid
= fork()) == -1) {
558 print_message(gettext("Fork failed\n"));
559 return (EXIT_FAILURE
);
560 } else if (pid
== 0) { /* Am I my child? */
561 set_handler(SIGSEGV
);
563 /* probe_hole does exit */
564 probe_hole(PH_VALID
);
567 (void) wait(&status
);
568 status
= WEXITSTATUS(status
);
570 if (status
== FAIL_SEGV
) {
571 print_message(gettext("Fail (got SEGV).\n"));
572 exit_status
= EXIT_FAILURE
;
573 } else if (status
!= PASS
) {
574 print_message(gettext("Test program failure.\n"));
575 exit_status
= EXIT_FAILURE
;
577 return (exit_status
);
587 struct sigaction act
;
589 act
.sa_handler
= NULL
;
590 act
.sa_flags
= SA_SIGINFO
;
591 act
.sa_sigaction
= segv_action
;
592 (void) sigemptyset(&(act
.sa_mask
));
594 if (sigaction(sig
, &act
, NULL
) < 0) {
595 if (silent
== NOT_SILENT
) {
596 (void) fprintf(stderr
, gettext(
597 "sigaction() returned error: %s\n"),
607 segv_action(int which_sig
, siginfo_t
*t1
, void *t2
)
609 exit(handler_exit_code
);
615 * Warning -- if you do a printf or fprintf prior to the actual
616 * reading from the stack, you've changed the stack to an unknown
617 * state. (stack memory isn't free'd automatically and this function
618 * needs to touch virgin stack space.)
623 unsigned char *end
; /* end of stack */
627 unsigned char last_fail
, *last_fail_address
;
628 unsigned char mark
= 0xAA; /* roughly the end of stack */
629 handler_exit_code
= FAIL_SEGV
;
632 /* stack growth is negative */
633 end
-= (WASTE_PAGES
* PAGESIZE
) + STACK_SLOP
;
635 for (i
= 0, j
= 0; i
< PAGESIZE
; i
++) {
636 if ((probe
= *end
) != 0) {
639 last_fail_address
= end
;
645 if (silent
== NOT_SILENT
)
646 (void) fprintf(stderr
, gettext(
647 "probe_stack failed. address=0x%08X; "
648 "probe=0x%02X; content = %d\n"),
649 (caddr_t
)last_fail_address
, last_fail
, j
);
651 exit(FAIL_ZERO
); /* test failed at least once */
657 probe_hole(int test_type
)
661 volatile unsigned char probe
;
662 unsigned char *probe_adr
;
665 address
= sbrk(0); /* current end data + 1 */
667 if (address
== (void *)-1) {
668 print_message(gettext("Test program logic error\n"));
671 if (test_type
== PH_VALID
) {
672 /* show that access works inside the hole */
673 handler_exit_code
= FAIL_SEGV
;
675 probe_adr
= (unsigned char *)address
- sizeof (char);
677 for (i
= 0; i
< PAGESIZE
; i
++)
678 probe
= *probe_adr
--;
682 /* show that a trap occurs in the hole */
683 handler_exit_code
= PASS_SEGV
;
685 address
= (void *)P2ROUNDUP((uintptr_t)address
, PAGESIZE
);
686 probe_adr
= (unsigned char *)address
;
689 exit(FAIL_SEGV
); /* expected SEGV, didn't get it */
694 * Catch signal, child to parent
700 done_memory_grab
= 1;
703 * memory_type: Determine whether a given executable file is compiled
706 * The following code was stolen from isainfo (1)
710 memory_type(const char *path
) {
716 if ((d
= open(path
, O_RDONLY
)) < 0) {
717 (void) fprintf(stderr
,
718 "cannot open: %s -- %s\n",
719 path
, strerror(errno
));
723 if (elf_version(EV_CURRENT
) == EV_NONE
) {
724 (void) fprintf(stderr
,
725 "internal error: ELF library out of date?\n");
730 elf
= elf_begin(d
, ELF_C_READ
, (Elf
*)0);
731 if (elf_kind(elf
) != ELF_K_ELF
) {
737 idarray
= elf_getident(elf
, 0);
739 if (idarray
[EI_CLASS
] == ELFCLASS32
) {
741 } else if (idarray
[EI_CLASS
] == ELFCLASS64
) {