640 number_to_scaled_string is duplicated in several commands
[unleashed.git] / usr / src / cmd / amt / amt.c
blob8c4cfdbe29140ff7f3a305fc53efd0daed640618
1 /*
2 * CDDL HEADER START
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
7 * with the License.
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]
20 * CDDL HEADER END
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.
34 #include <errno.h>
35 #include <fcntl.h>
36 #include <iso/stdlib_iso.h>
37 #include <libelf.h>
38 #include <libintl.h>
39 #include <locale.h>
40 #include <signal.h>
41 #include <stdio.h>
42 #include <string.h>
43 #include <sys/param.h>
44 #include <sys/resource.h>
45 #include <sys/sysmacros.h>
46 #include <sys/types.h>
47 #include <unistd.h>
48 #include <wait.h>
50 #define NOT_SILENT 0
51 #define SILENT_MODE 1
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 */
64 /* access */
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"
74 #endif
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;
94 static int silent;
95 static int handler_exit_code;
98 * Main Routine
101 main(int argc, char *argv[])
103 int fail_count = 0;
104 int status = 0;
105 int bitsize;
107 /* Internationalization */
108 (void) setlocale(LC_ALL, "");
109 (void) textdomain(TEXT_DOMAIN);
111 silent = NOT_SILENT;
113 if (argc == 2) {
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;
118 else {
119 /* Wrong argument */
120 (void) fprintf(stderr, gettext(
121 "Wrong argument, USAGE: amt [-s]\n"));
122 exit(EXIT_FAILURE);
124 } else if (argc != 1) {
125 /* Illegal number of arguments. */
126 (void) fprintf(stderr, gettext(
127 "Wrong usage, USAGE: amt [-s]\n"));
128 exit(EXIT_FAILURE);
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) {
141 /* Normal fail */
142 fail_count++;
143 print_message(gettext("TEST 1 FAILED\n"));
144 } else if (status == FAIL_ABORT) {
145 /* Test logic failure */
146 fail_count++;
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) {
153 fail_count++;
154 print_message(gettext("TEST 2 FAILED\n"));
155 } else
156 print_message(gettext("TEST 2 PASSED\n"));
158 /* Carry out test 3 */
159 if (text_area_not_writeable() != EXIT_SUCCESS) {
160 fail_count++;
161 print_message(gettext("TEST 3 FAILED\n"));
162 } else
163 print_message(gettext("TEST 3 PASSED\n"));
165 /* Carry out test 4 */
166 if (memory_not_shared_after_use() != EXIT_SUCCESS) {
167 fail_count++;
168 print_message(gettext("TEST 4 FAILED\n"));
169 } else
170 print_message(gettext("TEST 4 PASSED\n"));
172 /* Carry out test 5 */
173 if (memory_allocation_not_shared() != EXIT_SUCCESS) {
174 fail_count++;
175 print_message(gettext("TEST 5 FAILED\n"));
176 } else
177 print_message(gettext("TEST 5 PASSED\n"));
179 if (silent == NOT_SILENT) {
180 if (fail_count > 0)
181 (void) printf(gettext("\n %d TESTS FAILED\n\n"),
182 fail_count);
183 else
184 (void) printf(gettext("\nTESTS SUCCEEDED\n\n"));
186 return (fail_count);
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.
194 static int
195 data_boundary_test()
197 int exit_status = EXIT_SUCCESS;
198 pid_t pid;
199 int status;
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() */
210 probe_data_area();
212 /* still parent */
213 (void) wait(&status);
214 status = WEXITSTATUS(status);
216 if (status == PASS)
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;
224 } else {
225 (void) printf(gettext("Test program failure: %d\n"),
226 status);
227 exit_status = EXIT_FAILURE;
229 return (exit_status);
232 static void
233 probe_data_area()
235 int *p;
236 /* LINTED */
237 volatile int p1;
238 void *address;
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);
250 address = sbrk(0);
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. */
258 *p = 9999;
260 /* Should be able to read back with no problems. */
261 p1 = *p;
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
270 * a fault.
272 p = &_end;
274 /* Try writing to it, should succeed. */
275 *p = 9898;
277 /* Should be able to read back with no problems. */
278 p1 = *p;
280 exit(EXIT_SUCCESS);
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.
288 static int
289 text_area_not_writeable()
291 int exit_status = EXIT_SUCCESS;
292 pid_t pid;
293 int status;
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() */
309 probe_text_area();
311 /* still parent */
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"));
323 } else {
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
334 static void
335 probe_text_area()
337 handler_exit_code = FAIL_SEGV;
338 *(caddr_t)probe_text_area = 0xff;
339 exit(EXIT_FAILURE);
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.
346 static int
347 memory_not_shared_after_use()
349 pid_t pid;
350 int x = 1000;
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 "
357 "to it.\n"));
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
365 * parent value.
367 x = 2000;
369 /* Wait for parent to test value */
370 (void) sleep(CHILD_SLEEP_PERIOD);
372 exit(EXIT_SUCCESS);
374 /* Wait for child to do its stuff. */
375 (void) sleep(PARENT_SLEEP_PERIOD);
377 if (x == 1000)
378 exit_status = EXIT_SUCCESS;
379 else
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.
389 static int
390 memory_allocation_not_shared()
392 pid_t pid;
393 pid_t parent_pid;
394 int exit_status = 0;
395 caddr_t address;
396 caddr_t hole_start;
397 caddr_t hole_after;
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"),
412 hole_start);
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. */
426 address = sbrk(0);
427 if (silent == NOT_SILENT)
428 (void) printf(gettext(
429 "Child end of hole before change: %08X\n"),
430 address);
432 if (brk((address+PAGESIZE)) != 0) {
433 print_message(gettext(
434 "Can't change start of hole address.\n"));
435 exit(EXIT_FAILURE);
438 address = sbrk(0);
439 if (silent == NOT_SILENT)
440 (void) printf(gettext(
441 "Child end of hole after change: %08X\n"),
442 address);
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, "
448 "test failed\n"));
449 exit(EXIT_FAILURE);
452 /* Sleep before exiting to allow parent to finish processing. */
453 (void) sleep(CHILD_SLEEP_PERIOD);
454 exit(EXIT_SUCCESS);
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, "
462 "exiting\n"));
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"));
476 else {
477 print_message(gettext(
478 "FAIL: Hole is a different size.\n"));
479 exit_status = EXIT_FAILURE;
482 /* Wait for child to finish. */
483 (void) wait(0);
485 if (signal(SIG_EVENT, old_handler) == SIG_ERR) {
486 print_message(gettext("Couldn't put back old signal handler, "
487 "test failed.\n"));
488 return (EXIT_FAILURE);
490 return (exit_status);
493 static void
494 print_message(char *message)
496 if (silent == NOT_SILENT)
497 (void) printf("%s", message);
500 static int
501 test_stack_end_of_hole()
503 pid_t pid;
504 int status;
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 */
518 probe_stack();
520 /* still parent */
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);
542 /* still parent */
543 (void) wait(&status);
544 status = WEXITSTATUS(status);
546 if (status == FAIL_SEGV) {
547 print_message(
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);
566 /* still parent */
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);
582 * set_handler
584 static void
585 set_handler(int sig)
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"),
598 strerror(errno));
600 exit(EXIT_FAILURE);
605 /*ARGSUSED*/
606 static void
607 segv_action(int which_sig, siginfo_t *t1, void *t2)
609 exit(handler_exit_code);
613 * probe_stack
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.)
620 static void
621 probe_stack(void)
623 unsigned char *end; /* end of stack */
624 unsigned char probe;
625 long i;
626 int j;
627 unsigned char last_fail, *last_fail_address;
628 unsigned char mark = 0xAA; /* roughly the end of stack */
629 handler_exit_code = FAIL_SEGV;
631 end = &mark;
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) {
637 j++;
638 last_fail = probe;
639 last_fail_address = end;
641 end--;
644 if (j != 0) {
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 */
653 exit(EXIT_SUCCESS);
656 static void
657 probe_hole(int test_type)
659 long i;
660 /* LINTED */
661 volatile unsigned char probe;
662 unsigned char *probe_adr;
663 void *address;
665 address = sbrk(0); /* current end data + 1 */
667 if (address == (void *)-1) {
668 print_message(gettext("Test program logic error\n"));
669 exit(FAIL_ABORT);
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--;
680 exit(EXIT_SUCCESS);
681 } else {
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;
688 probe = *probe_adr;
689 exit(FAIL_SEGV); /* expected SEGV, didn't get it */
694 * Catch signal, child to parent
696 /*ARGSUSED*/
697 void
698 handler(int signal)
700 done_memory_grab = 1;
703 * memory_type: Determine whether a given executable file is compiled
704 * as 32 or 64 bit.
706 * The following code was stolen from isainfo (1)
709 static int
710 memory_type(const char *path) {
711 char *idarray;
712 Elf *elf;
713 int d;
714 int bits = 0;
716 if ((d = open(path, O_RDONLY)) < 0) {
717 (void) fprintf(stderr,
718 "cannot open: %s -- %s\n",
719 path, strerror(errno));
720 return (bits);
723 if (elf_version(EV_CURRENT) == EV_NONE) {
724 (void) fprintf(stderr,
725 "internal error: ELF library out of date?\n");
726 (void) close(d);
727 return (bits);
730 elf = elf_begin(d, ELF_C_READ, (Elf *)0);
731 if (elf_kind(elf) != ELF_K_ELF) {
732 (void) elf_end(elf);
733 (void) close(d);
734 return (bits);
737 idarray = elf_getident(elf, 0);
739 if (idarray[EI_CLASS] == ELFCLASS32) {
740 bits = 32;
741 } else if (idarray[EI_CLASS] == ELFCLASS64) {
742 bits = 64;
745 (void) elf_end(elf);
746 (void) close(d);
747 return (bits);