xref: /illumos-gate/usr/src/cmd/amt/amt.c (revision f667abe5)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
237c478bd9Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate /*
287c478bd9Sstevel@tonic-gate  * Abstract Machine Test; executes memory access tests to show
297c478bd9Sstevel@tonic-gate  * compliance with Common Criteria object reuse and process address
307c478bd9Sstevel@tonic-gate  * space separation requirements.
317c478bd9Sstevel@tonic-gate  */
327c478bd9Sstevel@tonic-gate #include <errno.h>
337c478bd9Sstevel@tonic-gate #include <fcntl.h>
347c478bd9Sstevel@tonic-gate #include <iso/stdlib_iso.h>
357c478bd9Sstevel@tonic-gate #include <libelf.h>
367c478bd9Sstevel@tonic-gate #include <libintl.h>
377c478bd9Sstevel@tonic-gate #include <locale.h>
387c478bd9Sstevel@tonic-gate #include <signal.h>
397c478bd9Sstevel@tonic-gate #include <stdio.h>
407c478bd9Sstevel@tonic-gate #include <string.h>
417c478bd9Sstevel@tonic-gate #include <sys/param.h>
427c478bd9Sstevel@tonic-gate #include <sys/resource.h>
437c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
447c478bd9Sstevel@tonic-gate #include <sys/types.h>
457c478bd9Sstevel@tonic-gate #include <unistd.h>
467c478bd9Sstevel@tonic-gate #include <wait.h>
477c478bd9Sstevel@tonic-gate 
487c478bd9Sstevel@tonic-gate #define	NOT_SILENT 0
497c478bd9Sstevel@tonic-gate #define	SILENT_MODE 1
507c478bd9Sstevel@tonic-gate 
517c478bd9Sstevel@tonic-gate #define	CHILD_SLEEP_PERIOD 2
527c478bd9Sstevel@tonic-gate #define	PARENT_SLEEP_PERIOD 1
537c478bd9Sstevel@tonic-gate #define	SIG_EVENT SIGUSR1
547c478bd9Sstevel@tonic-gate 
557c478bd9Sstevel@tonic-gate #define	PASS	   0		/* test passed, no SEGV */
567c478bd9Sstevel@tonic-gate #define	FAIL_ZERO  1		/* expected to read zero, didn't */
577c478bd9Sstevel@tonic-gate #define	FAIL_SEGV  2		/* expected good read or write, didn't */
587c478bd9Sstevel@tonic-gate #define	PASS_SEGV  3		/* expected SEGV, got it */
597c478bd9Sstevel@tonic-gate #define	FAIL_ABORT 4		/* test logic error */
607c478bd9Sstevel@tonic-gate 
617c478bd9Sstevel@tonic-gate #define	PH_VALID   0		/* arg for probe_hole -- do valid memory */
627c478bd9Sstevel@tonic-gate 				/* access */
637c478bd9Sstevel@tonic-gate #define	PH_INVALID 1		/* do illegal memory access */
647c478bd9Sstevel@tonic-gate 
657c478bd9Sstevel@tonic-gate #define	WASTE_PAGES   8		/* a guess at where virgin stack space */
667c478bd9Sstevel@tonic-gate 				/* is likely to exist */
677c478bd9Sstevel@tonic-gate #define	STACK_SLOP  256		/* a guess at how far below current end */
687c478bd9Sstevel@tonic-gate 				/* of stack I'll find unused space */
697c478bd9Sstevel@tonic-gate 
707c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)
717c478bd9Sstevel@tonic-gate #define	TEXT_DOMAIN "SYS_TEST"
727c478bd9Sstevel@tonic-gate #endif
737c478bd9Sstevel@tonic-gate 
747c478bd9Sstevel@tonic-gate extern int _end;	/* first address after the end of initialized data */
757c478bd9Sstevel@tonic-gate 
767c478bd9Sstevel@tonic-gate static int  data_boundary_test();
777c478bd9Sstevel@tonic-gate static void handler(int);
787c478bd9Sstevel@tonic-gate static int  memory_not_shared_after_use();
797c478bd9Sstevel@tonic-gate static int  memory_allocation_not_shared();
807c478bd9Sstevel@tonic-gate static int  memory_type(const char *);
817c478bd9Sstevel@tonic-gate static void print_message(char *);
827c478bd9Sstevel@tonic-gate static void probe_data_area(void);
837c478bd9Sstevel@tonic-gate static void probe_hole(int);
847c478bd9Sstevel@tonic-gate static void probe_stack(void);
857c478bd9Sstevel@tonic-gate static void probe_text_area(void);
867c478bd9Sstevel@tonic-gate static void segv_action(int, siginfo_t *, void *);
877c478bd9Sstevel@tonic-gate static void set_handler(int);
887c478bd9Sstevel@tonic-gate static int  test_stack_end_of_hole();
897c478bd9Sstevel@tonic-gate static int  text_area_not_writeable();
907c478bd9Sstevel@tonic-gate 
917c478bd9Sstevel@tonic-gate static int done_memory_grab = 0;
927c478bd9Sstevel@tonic-gate static int silent;
937c478bd9Sstevel@tonic-gate static int handler_exit_code;
947c478bd9Sstevel@tonic-gate 
957c478bd9Sstevel@tonic-gate /*
967c478bd9Sstevel@tonic-gate  * Main Routine
977c478bd9Sstevel@tonic-gate  */
987c478bd9Sstevel@tonic-gate int
main(int argc,char * argv[])997c478bd9Sstevel@tonic-gate main(int argc, char *argv[])
1007c478bd9Sstevel@tonic-gate {
1017c478bd9Sstevel@tonic-gate 	int fail_count = 0;
1027c478bd9Sstevel@tonic-gate 	int status = 0;
1037c478bd9Sstevel@tonic-gate 	int bitsize;
1047c478bd9Sstevel@tonic-gate 
1057c478bd9Sstevel@tonic-gate 	/* Internationalization */
1067c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
1077c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
1087c478bd9Sstevel@tonic-gate 
1097c478bd9Sstevel@tonic-gate 	silent = NOT_SILENT;
1107c478bd9Sstevel@tonic-gate 
1117c478bd9Sstevel@tonic-gate 	if (argc == 2) {
1127c478bd9Sstevel@tonic-gate 		/* Pull out argument provided		 */
1137c478bd9Sstevel@tonic-gate 		/* -s	silent mode, no status or error messages. */
1147c478bd9Sstevel@tonic-gate 		if (strncmp(argv[1], "-s", 4) == 0)
1157c478bd9Sstevel@tonic-gate 			silent = SILENT_MODE;
1167c478bd9Sstevel@tonic-gate 		else {
1177c478bd9Sstevel@tonic-gate 			/* Wrong argument */
1187c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
1197c478bd9Sstevel@tonic-gate 			    "Wrong argument, USAGE: amt [-s]\n"));
1207c478bd9Sstevel@tonic-gate 			exit(EXIT_FAILURE);
1217c478bd9Sstevel@tonic-gate 		}
1227c478bd9Sstevel@tonic-gate 	} else if (argc != 1) {
1237c478bd9Sstevel@tonic-gate 		/* Illegal number of arguments. */
1247c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
1257c478bd9Sstevel@tonic-gate 		    "Wrong usage, USAGE: amt [-s]\n"));
1267c478bd9Sstevel@tonic-gate 		exit(EXIT_FAILURE);
1277c478bd9Sstevel@tonic-gate 	}
1287c478bd9Sstevel@tonic-gate 	bitsize = memory_type(argv[0]);
1297c478bd9Sstevel@tonic-gate 
1307c478bd9Sstevel@tonic-gate 	if (silent == NOT_SILENT)
1317c478bd9Sstevel@tonic-gate 		(void) printf(gettext(
1327c478bd9Sstevel@tonic-gate 		    "\n\nAMT Test Program -- %d bit application\n"
1337c478bd9Sstevel@tonic-gate 		    "================\n"), bitsize);
1347c478bd9Sstevel@tonic-gate 	/*
1357c478bd9Sstevel@tonic-gate 	 * test_stack_end_of_hole must be the first test, or the stack
1367c478bd9Sstevel@tonic-gate 	 * is of an unknown size.
1377c478bd9Sstevel@tonic-gate 	 */
1387c478bd9Sstevel@tonic-gate 	if ((status = test_stack_end_of_hole()) == EXIT_FAILURE) {
1397c478bd9Sstevel@tonic-gate 		/* Normal fail */
1407c478bd9Sstevel@tonic-gate 		fail_count++;
1417c478bd9Sstevel@tonic-gate 		print_message(gettext("TEST 1 FAILED\n"));
1427c478bd9Sstevel@tonic-gate 	} else if (status == FAIL_ABORT) {
1437c478bd9Sstevel@tonic-gate 		/* Test logic failure */
1447c478bd9Sstevel@tonic-gate 		fail_count++;
1457c478bd9Sstevel@tonic-gate 		print_message(gettext("FAIL: Logic error in test\n"));
1467c478bd9Sstevel@tonic-gate 	} else if (status == EXIT_SUCCESS)
1477c478bd9Sstevel@tonic-gate 		print_message(gettext("TEST 1 PASSED\n"));
1487c478bd9Sstevel@tonic-gate 
1497c478bd9Sstevel@tonic-gate 	/* Carry out test 2 */
1507c478bd9Sstevel@tonic-gate 	if (data_boundary_test() != EXIT_SUCCESS) {
1517c478bd9Sstevel@tonic-gate 		fail_count++;
1527c478bd9Sstevel@tonic-gate 		print_message(gettext("TEST 2 FAILED\n"));
1537c478bd9Sstevel@tonic-gate 	} else
1547c478bd9Sstevel@tonic-gate 		print_message(gettext("TEST 2 PASSED\n"));
1557c478bd9Sstevel@tonic-gate 
1567c478bd9Sstevel@tonic-gate 	/* Carry out test 3 */
1577c478bd9Sstevel@tonic-gate 	if (text_area_not_writeable() != EXIT_SUCCESS) {
1587c478bd9Sstevel@tonic-gate 		fail_count++;
1597c478bd9Sstevel@tonic-gate 		print_message(gettext("TEST 3 FAILED\n"));
1607c478bd9Sstevel@tonic-gate 	} else
1617c478bd9Sstevel@tonic-gate 		print_message(gettext("TEST 3 PASSED\n"));
1627c478bd9Sstevel@tonic-gate 
1637c478bd9Sstevel@tonic-gate 	/* Carry out test 4 */
1647c478bd9Sstevel@tonic-gate 	if (memory_not_shared_after_use() != EXIT_SUCCESS) {
1657c478bd9Sstevel@tonic-gate 		fail_count++;
1667c478bd9Sstevel@tonic-gate 		print_message(gettext("TEST 4 FAILED\n"));
1677c478bd9Sstevel@tonic-gate 	} else
1687c478bd9Sstevel@tonic-gate 		print_message(gettext("TEST 4 PASSED\n"));
1697c478bd9Sstevel@tonic-gate 
1707c478bd9Sstevel@tonic-gate 	/* Carry out test 5 */
1717c478bd9Sstevel@tonic-gate 	if (memory_allocation_not_shared() != EXIT_SUCCESS) {
1727c478bd9Sstevel@tonic-gate 		fail_count++;
1737c478bd9Sstevel@tonic-gate 		print_message(gettext("TEST 5 FAILED\n"));
1747c478bd9Sstevel@tonic-gate 	} else
1757c478bd9Sstevel@tonic-gate 		print_message(gettext("TEST 5 PASSED\n"));
1767c478bd9Sstevel@tonic-gate 
1777c478bd9Sstevel@tonic-gate 	if (silent == NOT_SILENT) {
1787c478bd9Sstevel@tonic-gate 		if (fail_count > 0)
1797c478bd9Sstevel@tonic-gate 			(void) printf(gettext("\n %d TESTS FAILED\n\n"),
1807c478bd9Sstevel@tonic-gate 			    fail_count);
1817c478bd9Sstevel@tonic-gate 		else
1827c478bd9Sstevel@tonic-gate 			(void) printf(gettext("\nTESTS SUCCEEDED\n\n"));
1837c478bd9Sstevel@tonic-gate 	}
1847c478bd9Sstevel@tonic-gate 	return (fail_count);
1857c478bd9Sstevel@tonic-gate }
1867c478bd9Sstevel@tonic-gate 
1877c478bd9Sstevel@tonic-gate /*
1887c478bd9Sstevel@tonic-gate  * Test the data boundaries. First test inside the data area at the boundary
1897c478bd9Sstevel@tonic-gate  * of the "hole" area. Second test inside the data area at the text area
1907c478bd9Sstevel@tonic-gate  * boundary. Both should pass.
1917c478bd9Sstevel@tonic-gate  */
1927c478bd9Sstevel@tonic-gate static int
data_boundary_test()1937c478bd9Sstevel@tonic-gate data_boundary_test()
1947c478bd9Sstevel@tonic-gate {
1957c478bd9Sstevel@tonic-gate 	int exit_status = EXIT_SUCCESS;
1967c478bd9Sstevel@tonic-gate 	pid_t pid;
1977c478bd9Sstevel@tonic-gate 	int status;
1987c478bd9Sstevel@tonic-gate 
1997c478bd9Sstevel@tonic-gate 	print_message(gettext("\n\nTest 2- Data Side Boundary Test.\n"));
2007c478bd9Sstevel@tonic-gate 
2017c478bd9Sstevel@tonic-gate 	if ((pid = fork()) == -1) {
2027c478bd9Sstevel@tonic-gate 		print_message(gettext("Fork failed\n"));
2037c478bd9Sstevel@tonic-gate 		return (EXIT_FAILURE);
2047c478bd9Sstevel@tonic-gate 	} else if (pid == 0) { /* Am I my child? */
2057c478bd9Sstevel@tonic-gate 		set_handler(SIGSEGV);
2067c478bd9Sstevel@tonic-gate 
2077c478bd9Sstevel@tonic-gate 		/* probe_data_area() does exit() */
2087c478bd9Sstevel@tonic-gate 		probe_data_area();
2097c478bd9Sstevel@tonic-gate 	}
2107c478bd9Sstevel@tonic-gate 	/* still parent */
2117c478bd9Sstevel@tonic-gate 	(void) wait(&status);
2127c478bd9Sstevel@tonic-gate 	status = WEXITSTATUS(status);
2137c478bd9Sstevel@tonic-gate 
2147c478bd9Sstevel@tonic-gate 	if (status == PASS)
2157c478bd9Sstevel@tonic-gate 		print_message(gettext(
2167c478bd9Sstevel@tonic-gate 		    "PASS: Successful read/write in data area.\n"));
2177c478bd9Sstevel@tonic-gate 	else if (status == FAIL_SEGV) {
2187c478bd9Sstevel@tonic-gate 		print_message(gettext(
2197c478bd9Sstevel@tonic-gate 		    "FAIL: Caught a segmentation fault while "
2207c478bd9Sstevel@tonic-gate 		    "attempting to write to the data area.\n"));
2217c478bd9Sstevel@tonic-gate 		exit_status = EXIT_FAILURE;
2227c478bd9Sstevel@tonic-gate 	} else {
2237c478bd9Sstevel@tonic-gate 		(void) printf(gettext("Test program failure: %d\n"),
2247c478bd9Sstevel@tonic-gate 		    status);
2257c478bd9Sstevel@tonic-gate 		exit_status = EXIT_FAILURE;
2267c478bd9Sstevel@tonic-gate 	}
2277c478bd9Sstevel@tonic-gate 	return (exit_status);
2287c478bd9Sstevel@tonic-gate }
2297c478bd9Sstevel@tonic-gate 
2307c478bd9Sstevel@tonic-gate static void
probe_data_area()2317c478bd9Sstevel@tonic-gate probe_data_area()
2327c478bd9Sstevel@tonic-gate {
2337c478bd9Sstevel@tonic-gate 	int *p;
2347c478bd9Sstevel@tonic-gate 	/* LINTED */
235*f667abe5SToomas Soome 	volatile int p1 __unused;
2367c478bd9Sstevel@tonic-gate 	void *address;
2377c478bd9Sstevel@tonic-gate 
2387c478bd9Sstevel@tonic-gate 	/* set handler status */
2397c478bd9Sstevel@tonic-gate 	handler_exit_code = FAIL_SEGV;
2407c478bd9Sstevel@tonic-gate 
2417c478bd9Sstevel@tonic-gate 	/*
2427c478bd9Sstevel@tonic-gate 	 * Get an address in the data area, near to the "hole".
2437c478bd9Sstevel@tonic-gate 	 * sbrk returns prior address value; rather than calculating
2447c478bd9Sstevel@tonic-gate 	 * the sbrk result, sbrk is called twice, so address points
2457c478bd9Sstevel@tonic-gate 	 * to the new end of data
2467c478bd9Sstevel@tonic-gate 	 */
2477c478bd9Sstevel@tonic-gate 	(void) sbrk(PAGESIZE);
2487c478bd9Sstevel@tonic-gate 	address = sbrk(0);
2497c478bd9Sstevel@tonic-gate 	/*
2507c478bd9Sstevel@tonic-gate 	 * Get to the inside edge of a page boundary
2517c478bd9Sstevel@tonic-gate 	 * two integer words short of a new page
2527c478bd9Sstevel@tonic-gate 	 */
2537c478bd9Sstevel@tonic-gate 	p = ((int *)P2ROUNDUP((uintptr_t)address, PAGESIZE)) - 2;
2547c478bd9Sstevel@tonic-gate 
2557c478bd9Sstevel@tonic-gate 	/* Try writing to it, shouldn't cause a segmentation fault. */
2567c478bd9Sstevel@tonic-gate 	*p = 9999;
2577c478bd9Sstevel@tonic-gate 
2587c478bd9Sstevel@tonic-gate 	/* Should be able to read back with no problems. */
2597c478bd9Sstevel@tonic-gate 	p1 = *p;
2607c478bd9Sstevel@tonic-gate 
2617c478bd9Sstevel@tonic-gate 	/*
2627c478bd9Sstevel@tonic-gate 	 * Get an address near the text area boundary, but in the data
2637c478bd9Sstevel@tonic-gate 	 * area.  _etext rounded up a page isn't correct since the
2647c478bd9Sstevel@tonic-gate 	 * initialized data area isn't writeable.
2657c478bd9Sstevel@tonic-gate 	 *
2667c478bd9Sstevel@tonic-gate 	 * Future versions should consider handling initialized data
2677c478bd9Sstevel@tonic-gate 	 * separately -- writing to initialized data should generate
2687c478bd9Sstevel@tonic-gate 	 * a fault.
2697c478bd9Sstevel@tonic-gate 	 */
2707c478bd9Sstevel@tonic-gate 	p = &_end;
2717c478bd9Sstevel@tonic-gate 
2727c478bd9Sstevel@tonic-gate 	/* Try writing to it, should succeed. */
2737c478bd9Sstevel@tonic-gate 	*p = 9898;
2747c478bd9Sstevel@tonic-gate 
2757c478bd9Sstevel@tonic-gate 	/* Should be able to read back with no problems. */
2767c478bd9Sstevel@tonic-gate 	p1 = *p;
2777c478bd9Sstevel@tonic-gate 
2787c478bd9Sstevel@tonic-gate 	exit(EXIT_SUCCESS);
2797c478bd9Sstevel@tonic-gate }
2807c478bd9Sstevel@tonic-gate 
2817c478bd9Sstevel@tonic-gate /*
2827c478bd9Sstevel@tonic-gate  * Test that we cannot write to the text area. An attempt to write to
2837c478bd9Sstevel@tonic-gate  * the text area will result in a segmentation fault. So if we catch it,
2847c478bd9Sstevel@tonic-gate  * test has succeed, else it has failed.
2857c478bd9Sstevel@tonic-gate  */
2867c478bd9Sstevel@tonic-gate static int
text_area_not_writeable()2877c478bd9Sstevel@tonic-gate text_area_not_writeable()
2887c478bd9Sstevel@tonic-gate {
2897c478bd9Sstevel@tonic-gate 	int exit_status = EXIT_SUCCESS;
2907c478bd9Sstevel@tonic-gate 	pid_t pid;
2917c478bd9Sstevel@tonic-gate 	int status;
2927c478bd9Sstevel@tonic-gate 
2937c478bd9Sstevel@tonic-gate 	print_message(gettext(
2947c478bd9Sstevel@tonic-gate 	    "\n\nTest 3- Text Area Not Writeable\n"
2957c478bd9Sstevel@tonic-gate 	    "Verify that a write to the text space does not cause "
2967c478bd9Sstevel@tonic-gate 	    "a write to the executable\n"
2977c478bd9Sstevel@tonic-gate 	    "file from which it came, or to another process which "
2987c478bd9Sstevel@tonic-gate 	    "shares that text.\n"));
2997c478bd9Sstevel@tonic-gate 
3007c478bd9Sstevel@tonic-gate 	if ((pid = fork()) == -1) {
3017c478bd9Sstevel@tonic-gate 		print_message(gettext("Fork failed\n"));
3027c478bd9Sstevel@tonic-gate 		return (EXIT_FAILURE);
3037c478bd9Sstevel@tonic-gate 	} else if (pid == 0) { /* Am I my child? */
3047c478bd9Sstevel@tonic-gate 		set_handler(SIGSEGV);
3057c478bd9Sstevel@tonic-gate 
3067c478bd9Sstevel@tonic-gate 		/* probe_text_area() does exit() */
3077c478bd9Sstevel@tonic-gate 		probe_text_area();
3087c478bd9Sstevel@tonic-gate 	}
3097c478bd9Sstevel@tonic-gate 	/* still parent */
3107c478bd9Sstevel@tonic-gate 	(void) wait(&status);
3117c478bd9Sstevel@tonic-gate 	status = WEXITSTATUS(status);
3127c478bd9Sstevel@tonic-gate 
3137c478bd9Sstevel@tonic-gate 	if (status == PASS) {
3147c478bd9Sstevel@tonic-gate 		print_message(gettext(
3157c478bd9Sstevel@tonic-gate 		    "FAIL: We did not cause a segmentation fault.\n"));
3167c478bd9Sstevel@tonic-gate 		exit_status = EXIT_FAILURE;
3177c478bd9Sstevel@tonic-gate 	} else if (status == FAIL_SEGV) {
3187c478bd9Sstevel@tonic-gate 		print_message(gettext(
3197c478bd9Sstevel@tonic-gate 		    "PASS: Caught the segmentation fault, "
3207c478bd9Sstevel@tonic-gate 		    "meaning we can't write to text area.\n"));
3217c478bd9Sstevel@tonic-gate 	} else {
3227c478bd9Sstevel@tonic-gate 		(void) printf(gettext(
3237c478bd9Sstevel@tonic-gate 		    "Test program failure: %d\n"), status);
3247c478bd9Sstevel@tonic-gate 		exit_status = EXIT_FAILURE;
3257c478bd9Sstevel@tonic-gate 	}
3267c478bd9Sstevel@tonic-gate 	return (exit_status);
3277c478bd9Sstevel@tonic-gate }
3287c478bd9Sstevel@tonic-gate 
3297c478bd9Sstevel@tonic-gate /*
3307c478bd9Sstevel@tonic-gate  * write to text area, trigger a SEGV
3317c478bd9Sstevel@tonic-gate  */
3327c478bd9Sstevel@tonic-gate static void
probe_text_area()3337c478bd9Sstevel@tonic-gate probe_text_area()
3347c478bd9Sstevel@tonic-gate {
3357c478bd9Sstevel@tonic-gate 	handler_exit_code = FAIL_SEGV;
3367c478bd9Sstevel@tonic-gate 	*(caddr_t)probe_text_area = 0xff;
3377c478bd9Sstevel@tonic-gate 	exit(EXIT_FAILURE);
3387c478bd9Sstevel@tonic-gate }
3397c478bd9Sstevel@tonic-gate 
3407c478bd9Sstevel@tonic-gate /*
3417c478bd9Sstevel@tonic-gate  * Test that when we set some values and fork a process, when the child
3427c478bd9Sstevel@tonic-gate  * writes to these inherited values, the parents copies are not changed.
3437c478bd9Sstevel@tonic-gate  */
3447c478bd9Sstevel@tonic-gate static int
memory_not_shared_after_use()3457c478bd9Sstevel@tonic-gate memory_not_shared_after_use()
3467c478bd9Sstevel@tonic-gate {
3477c478bd9Sstevel@tonic-gate 	pid_t pid;
3487c478bd9Sstevel@tonic-gate 	int x = 1000;
3497c478bd9Sstevel@tonic-gate 	int exit_status = EXIT_SUCCESS;
3507c478bd9Sstevel@tonic-gate 
3517c478bd9Sstevel@tonic-gate 	print_message(gettext("\n\nTest 4- Memory Not Shared After Write\n"
3527c478bd9Sstevel@tonic-gate 	    "Verify that anonymous memory initially shared by two "
3537c478bd9Sstevel@tonic-gate 	    "processes (e.g. after a\n"
3547c478bd9Sstevel@tonic-gate 	    "fork) is not shared after either process writes "
3557c478bd9Sstevel@tonic-gate 	    "to it.\n"));
3567c478bd9Sstevel@tonic-gate 
3577c478bd9Sstevel@tonic-gate 	if ((pid = fork()) == -1) {
3587c478bd9Sstevel@tonic-gate 		print_message(gettext("Fork failed\n"));
3597c478bd9Sstevel@tonic-gate 		return (EXIT_FAILURE);
3607c478bd9Sstevel@tonic-gate 	} else if (pid == 0) { /* I am the child. */
3617c478bd9Sstevel@tonic-gate 		/*
3627c478bd9Sstevel@tonic-gate 		 * Change child value; this should not change
3637c478bd9Sstevel@tonic-gate 		 * parent value.
3647c478bd9Sstevel@tonic-gate 		 */
3657c478bd9Sstevel@tonic-gate 		x = 2000;
3667c478bd9Sstevel@tonic-gate 
3677c478bd9Sstevel@tonic-gate 		/* Wait for parent to test value */
3687c478bd9Sstevel@tonic-gate 		(void) sleep(CHILD_SLEEP_PERIOD);
3697c478bd9Sstevel@tonic-gate 
3707c478bd9Sstevel@tonic-gate 		exit(EXIT_SUCCESS);
3717c478bd9Sstevel@tonic-gate 	}
3727c478bd9Sstevel@tonic-gate 	/* Wait for child to do its stuff. */
3737c478bd9Sstevel@tonic-gate 	(void) sleep(PARENT_SLEEP_PERIOD);
3747c478bd9Sstevel@tonic-gate 
3757c478bd9Sstevel@tonic-gate 	if (x == 1000)
3767c478bd9Sstevel@tonic-gate 		exit_status = EXIT_SUCCESS;
3777c478bd9Sstevel@tonic-gate 	else
3787c478bd9Sstevel@tonic-gate 		exit_status = EXIT_FAILURE;
3797c478bd9Sstevel@tonic-gate 
3807c478bd9Sstevel@tonic-gate 	return (exit_status);
3817c478bd9Sstevel@tonic-gate }
3827c478bd9Sstevel@tonic-gate 
3837c478bd9Sstevel@tonic-gate /*
3847c478bd9Sstevel@tonic-gate  * If we fork a process and then allocate some memory in that process,
3857c478bd9Sstevel@tonic-gate  * we should not see any memory changes in the parent.
3867c478bd9Sstevel@tonic-gate  */
3877c478bd9Sstevel@tonic-gate static int
memory_allocation_not_shared()3887c478bd9Sstevel@tonic-gate memory_allocation_not_shared()
3897c478bd9Sstevel@tonic-gate {
3907c478bd9Sstevel@tonic-gate 	pid_t pid;
3917c478bd9Sstevel@tonic-gate 	pid_t parent_pid;
3927c478bd9Sstevel@tonic-gate 	int exit_status = 0;
3937c478bd9Sstevel@tonic-gate 	caddr_t address;
3947c478bd9Sstevel@tonic-gate 	caddr_t hole_start;
3957c478bd9Sstevel@tonic-gate 	caddr_t hole_after;
3967c478bd9Sstevel@tonic-gate 	void (*old_handler) ();
3977c478bd9Sstevel@tonic-gate 
3987c478bd9Sstevel@tonic-gate 	print_message(gettext(
3997c478bd9Sstevel@tonic-gate 	    "\n\nTest 5- Memory Allocation is Not Shared\n"
4007c478bd9Sstevel@tonic-gate 	    "Verify that newly allocated memory in one of two "
4017c478bd9Sstevel@tonic-gate 	    "processes created by forking\n"
4027c478bd9Sstevel@tonic-gate 	    "does not result in newly allocated memory in the other.\n"));
4037c478bd9Sstevel@tonic-gate 
4047c478bd9Sstevel@tonic-gate 	/* Save Size of data area and 1st block address of "hole" */
4057c478bd9Sstevel@tonic-gate 	hole_start = (caddr_t)sbrk(0);
4067c478bd9Sstevel@tonic-gate 
4077c478bd9Sstevel@tonic-gate 	if (silent == NOT_SILENT)
4087c478bd9Sstevel@tonic-gate 		(void) printf(gettext(
4097c478bd9Sstevel@tonic-gate 		    "Parent address of hole before child change: %08X\n"),
4107c478bd9Sstevel@tonic-gate 		    hole_start);
4117c478bd9Sstevel@tonic-gate 
4127c478bd9Sstevel@tonic-gate 	/* Set handler for signal SIG_EVENT (define at start) */
4137c478bd9Sstevel@tonic-gate 	old_handler = signal(SIG_EVENT, &handler);
4147c478bd9Sstevel@tonic-gate 	if (old_handler == SIG_ERR) {
4157c478bd9Sstevel@tonic-gate 		print_message(gettext(
4167c478bd9Sstevel@tonic-gate 		    "Can't establish signal handler, test failed\n"));
4177c478bd9Sstevel@tonic-gate 		return (EXIT_FAILURE);
4187c478bd9Sstevel@tonic-gate 	}
4197c478bd9Sstevel@tonic-gate 
4207c478bd9Sstevel@tonic-gate 	if ((pid = fork()) == -1) {
4217c478bd9Sstevel@tonic-gate 		print_message(gettext("Fork failed\n"));
4227c478bd9Sstevel@tonic-gate 		return (EXIT_FAILURE);
4237c478bd9Sstevel@tonic-gate 	} else if (pid == 0) { /* We are the child. */
4247c478bd9Sstevel@tonic-gate 		address = sbrk(0);
4257c478bd9Sstevel@tonic-gate 		if (silent == NOT_SILENT)
4267c478bd9Sstevel@tonic-gate 			(void) printf(gettext(
4277c478bd9Sstevel@tonic-gate 			    "Child end of hole before change:  %08X\n"),
4287c478bd9Sstevel@tonic-gate 			    address);
4297c478bd9Sstevel@tonic-gate 
4307c478bd9Sstevel@tonic-gate 		if (brk((address+PAGESIZE)) != 0) {
4317c478bd9Sstevel@tonic-gate 			print_message(gettext(
4327c478bd9Sstevel@tonic-gate 			    "Can't change start of hole address.\n"));
4337c478bd9Sstevel@tonic-gate 			exit(EXIT_FAILURE);
4347c478bd9Sstevel@tonic-gate 		}
4357c478bd9Sstevel@tonic-gate 
4367c478bd9Sstevel@tonic-gate 		address = sbrk(0);
4377c478bd9Sstevel@tonic-gate 		if (silent == NOT_SILENT)
4387c478bd9Sstevel@tonic-gate 			(void) printf(gettext(
4397c478bd9Sstevel@tonic-gate 			    "Child end of hole after change: %08X\n"),
4407c478bd9Sstevel@tonic-gate 			    address);
4417c478bd9Sstevel@tonic-gate 
4427c478bd9Sstevel@tonic-gate 		/* Tell the parent we're done. */
4437c478bd9Sstevel@tonic-gate 		parent_pid = getppid();
4447c478bd9Sstevel@tonic-gate 		if (sigsend(P_PID, parent_pid, SIG_EVENT) != 0) {
4457c478bd9Sstevel@tonic-gate 			print_message(gettext("Can't send signal to parent, "
4467c478bd9Sstevel@tonic-gate 			    "test failed\n"));
4477c478bd9Sstevel@tonic-gate 			exit(EXIT_FAILURE);
4487c478bd9Sstevel@tonic-gate 		}
4497c478bd9Sstevel@tonic-gate 
4507c478bd9Sstevel@tonic-gate 		/* Sleep before exiting to allow parent to finish processing. */
4517c478bd9Sstevel@tonic-gate 		(void) sleep(CHILD_SLEEP_PERIOD);
4527c478bd9Sstevel@tonic-gate 		exit(EXIT_SUCCESS);
4537c478bd9Sstevel@tonic-gate 	}
4547c478bd9Sstevel@tonic-gate 	/* Wait for child to do its work. */
4557c478bd9Sstevel@tonic-gate 	(void) sleep(PARENT_SLEEP_PERIOD);
4567c478bd9Sstevel@tonic-gate 
4577c478bd9Sstevel@tonic-gate 	if (done_memory_grab != 1) {
4587c478bd9Sstevel@tonic-gate 		print_message(gettext(
4597c478bd9Sstevel@tonic-gate 		    "Child failed to do memory alterations, "
4607c478bd9Sstevel@tonic-gate 		    "exiting\n"));
4617c478bd9Sstevel@tonic-gate 		return (EXIT_FAILURE);
4627c478bd9Sstevel@tonic-gate 	}
4637c478bd9Sstevel@tonic-gate 
4647c478bd9Sstevel@tonic-gate 	hole_after = sbrk(0);
4657c478bd9Sstevel@tonic-gate 	if (silent == NOT_SILENT)
4667c478bd9Sstevel@tonic-gate 		(void) printf(gettext(
4677c478bd9Sstevel@tonic-gate 		    "Parent address of hole after child change: "
4687c478bd9Sstevel@tonic-gate 		    "%08X\n"), hole_after);
4697c478bd9Sstevel@tonic-gate 
4707c478bd9Sstevel@tonic-gate 	/* Test size of hole and data region. */
4717c478bd9Sstevel@tonic-gate 	if (hole_start == hole_after)
4727c478bd9Sstevel@tonic-gate 		print_message(gettext(
4737c478bd9Sstevel@tonic-gate 		    "PASS: Hole is same size in parent.\n"));
4747c478bd9Sstevel@tonic-gate 	else {
4757c478bd9Sstevel@tonic-gate 		print_message(gettext(
4767c478bd9Sstevel@tonic-gate 		    "FAIL: Hole is a different size.\n"));
4777c478bd9Sstevel@tonic-gate 		exit_status = EXIT_FAILURE;
4787c478bd9Sstevel@tonic-gate 	}
4797c478bd9Sstevel@tonic-gate 
4807c478bd9Sstevel@tonic-gate 	/* Wait for child to finish. */
4817c478bd9Sstevel@tonic-gate 	(void) wait(0);
4827c478bd9Sstevel@tonic-gate 
4837c478bd9Sstevel@tonic-gate 	if (signal(SIG_EVENT, old_handler) == SIG_ERR) {
4847c478bd9Sstevel@tonic-gate 		print_message(gettext("Couldn't put back old signal handler, "
4857c478bd9Sstevel@tonic-gate 		    "test failed.\n"));
4867c478bd9Sstevel@tonic-gate 		return (EXIT_FAILURE);
4877c478bd9Sstevel@tonic-gate 	}
4887c478bd9Sstevel@tonic-gate 	return (exit_status);
4897c478bd9Sstevel@tonic-gate }
4907c478bd9Sstevel@tonic-gate 
4917c478bd9Sstevel@tonic-gate static void
print_message(char * message)4927c478bd9Sstevel@tonic-gate print_message(char *message)
4937c478bd9Sstevel@tonic-gate {
4947c478bd9Sstevel@tonic-gate 	if (silent == NOT_SILENT)
4957c478bd9Sstevel@tonic-gate 		(void) printf("%s", message);
4967c478bd9Sstevel@tonic-gate }
4977c478bd9Sstevel@tonic-gate 
4987c478bd9Sstevel@tonic-gate static int
test_stack_end_of_hole()4997c478bd9Sstevel@tonic-gate test_stack_end_of_hole()
5007c478bd9Sstevel@tonic-gate {
5017c478bd9Sstevel@tonic-gate 	pid_t pid;
5027c478bd9Sstevel@tonic-gate 	int status;
5037c478bd9Sstevel@tonic-gate 	int exit_status = EXIT_SUCCESS;
5047c478bd9Sstevel@tonic-gate 
5057c478bd9Sstevel@tonic-gate 	print_message(gettext("\n\nTest 1- stack Side Boundary Test\n"));
5067c478bd9Sstevel@tonic-gate 
5077c478bd9Sstevel@tonic-gate 	/* sub test 1:  the space the stack grows into is zero */
5087c478bd9Sstevel@tonic-gate 
5097c478bd9Sstevel@tonic-gate 	if ((pid = fork()) == -1) {
5107c478bd9Sstevel@tonic-gate 		print_message(gettext("Fork failed\n"));
5117c478bd9Sstevel@tonic-gate 		return (EXIT_FAILURE);
5127c478bd9Sstevel@tonic-gate 	} else if (pid == 0) { /* Am I my child? */
5137c478bd9Sstevel@tonic-gate 		set_handler(SIGSEGV);
5147c478bd9Sstevel@tonic-gate 
5157c478bd9Sstevel@tonic-gate 		/* probe_stack() does exit */
5167c478bd9Sstevel@tonic-gate 		probe_stack();
5177c478bd9Sstevel@tonic-gate 	}
5187c478bd9Sstevel@tonic-gate 	/* still parent */
5197c478bd9Sstevel@tonic-gate 	(void) wait(&status);
5207c478bd9Sstevel@tonic-gate 	status = WEXITSTATUS(status);
5217c478bd9Sstevel@tonic-gate 
5227c478bd9Sstevel@tonic-gate 	if (status == FAIL_ZERO) {
5237c478bd9Sstevel@tonic-gate 		print_message(gettext("Fail with non-zero read.\n"));
5247c478bd9Sstevel@tonic-gate 		exit_status = EXIT_FAILURE;
5257c478bd9Sstevel@tonic-gate 	} else if (status != PASS) {
5267c478bd9Sstevel@tonic-gate 		print_message(gettext("Test program failure\n"));
5277c478bd9Sstevel@tonic-gate 		exit_status = EXIT_FAILURE;
5287c478bd9Sstevel@tonic-gate 	}
5297c478bd9Sstevel@tonic-gate 	/* sub test 2:  the space in hole is not readable */
5307c478bd9Sstevel@tonic-gate 
5317c478bd9Sstevel@tonic-gate 	if ((pid = fork()) == -1) {
5327c478bd9Sstevel@tonic-gate 		print_message(gettext("Fork failed\n"));
5337c478bd9Sstevel@tonic-gate 		return (EXIT_FAILURE);
5347c478bd9Sstevel@tonic-gate 	} else if (pid == 0) { /* Am I my child? */
5357c478bd9Sstevel@tonic-gate 		set_handler(SIGSEGV);
5367c478bd9Sstevel@tonic-gate 
5377c478bd9Sstevel@tonic-gate 		/* probe_hole does exit */
5387c478bd9Sstevel@tonic-gate 		probe_hole(PH_INVALID);
5397c478bd9Sstevel@tonic-gate 	}
5407c478bd9Sstevel@tonic-gate 	/* still parent */
5417c478bd9Sstevel@tonic-gate 	(void) wait(&status);
5427c478bd9Sstevel@tonic-gate 	status = WEXITSTATUS(status);
5437c478bd9Sstevel@tonic-gate 
5447c478bd9Sstevel@tonic-gate 	if (status == FAIL_SEGV) {
5457c478bd9Sstevel@tonic-gate 		print_message(
5467c478bd9Sstevel@tonic-gate 		    gettext("Fail (SEGV expected, not received).\n"));
5477c478bd9Sstevel@tonic-gate 		exit_status = EXIT_FAILURE;
5487c478bd9Sstevel@tonic-gate 	} else if (status != PASS_SEGV) {
5497c478bd9Sstevel@tonic-gate 		print_message(gettext("Test program failure.\n"));
5507c478bd9Sstevel@tonic-gate 		exit_status = EXIT_FAILURE;
5517c478bd9Sstevel@tonic-gate 	}
5527c478bd9Sstevel@tonic-gate 
5537c478bd9Sstevel@tonic-gate 	/* sub test 3:  the space in new page below hole is zero */
5547c478bd9Sstevel@tonic-gate 
5557c478bd9Sstevel@tonic-gate 	if ((pid = fork()) == -1) {
5567c478bd9Sstevel@tonic-gate 		print_message(gettext("Fork failed\n"));
5577c478bd9Sstevel@tonic-gate 		return (EXIT_FAILURE);
5587c478bd9Sstevel@tonic-gate 	} else if (pid == 0) { /* Am I my child? */
5597c478bd9Sstevel@tonic-gate 		set_handler(SIGSEGV);
5607c478bd9Sstevel@tonic-gate 
5617c478bd9Sstevel@tonic-gate 		/* probe_hole does exit */
5627c478bd9Sstevel@tonic-gate 		probe_hole(PH_VALID);
5637c478bd9Sstevel@tonic-gate 	}
5647c478bd9Sstevel@tonic-gate 	/* still parent */
5657c478bd9Sstevel@tonic-gate 	(void) wait(&status);
5667c478bd9Sstevel@tonic-gate 	status = WEXITSTATUS(status);
5677c478bd9Sstevel@tonic-gate 
5687c478bd9Sstevel@tonic-gate 	if (status == FAIL_SEGV) {
5697c478bd9Sstevel@tonic-gate 		print_message(gettext("Fail (got SEGV).\n"));
5707c478bd9Sstevel@tonic-gate 		exit_status = EXIT_FAILURE;
5717c478bd9Sstevel@tonic-gate 	} else if (status != PASS) {
5727c478bd9Sstevel@tonic-gate 		print_message(gettext("Test program failure.\n"));
5737c478bd9Sstevel@tonic-gate 		exit_status = EXIT_FAILURE;
5747c478bd9Sstevel@tonic-gate 	}
5757c478bd9Sstevel@tonic-gate 	return (exit_status);
5767c478bd9Sstevel@tonic-gate }
5777c478bd9Sstevel@tonic-gate 
5787c478bd9Sstevel@tonic-gate 
5797c478bd9Sstevel@tonic-gate /*
5807c478bd9Sstevel@tonic-gate  * set_handler
5817c478bd9Sstevel@tonic-gate  */
5827c478bd9Sstevel@tonic-gate static void
set_handler(int sig)5837c478bd9Sstevel@tonic-gate set_handler(int sig)
5847c478bd9Sstevel@tonic-gate {
5857c478bd9Sstevel@tonic-gate 	struct sigaction act;
5867c478bd9Sstevel@tonic-gate 
5877c478bd9Sstevel@tonic-gate 	act.sa_handler = NULL;
5887c478bd9Sstevel@tonic-gate 	act.sa_flags = SA_SIGINFO;
5897c478bd9Sstevel@tonic-gate 	act.sa_sigaction = segv_action;
5907c478bd9Sstevel@tonic-gate 	(void) sigemptyset(&(act.sa_mask));
5917c478bd9Sstevel@tonic-gate 
5927c478bd9Sstevel@tonic-gate 	if (sigaction(sig, &act, NULL) < 0) {
5937c478bd9Sstevel@tonic-gate 		if (silent == NOT_SILENT) {
5947c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
5957c478bd9Sstevel@tonic-gate 			    "sigaction() returned error: %s\n"),
5967c478bd9Sstevel@tonic-gate 			    strerror(errno));
5977c478bd9Sstevel@tonic-gate 		}
5987c478bd9Sstevel@tonic-gate 		exit(EXIT_FAILURE);
5997c478bd9Sstevel@tonic-gate 	}
6007c478bd9Sstevel@tonic-gate }
6017c478bd9Sstevel@tonic-gate 
6027c478bd9Sstevel@tonic-gate 
6037c478bd9Sstevel@tonic-gate /*ARGSUSED*/
6047c478bd9Sstevel@tonic-gate static void
segv_action(int which_sig,siginfo_t * t1,void * t2)6057c478bd9Sstevel@tonic-gate segv_action(int which_sig, siginfo_t *t1, void *t2)
6067c478bd9Sstevel@tonic-gate {
6077c478bd9Sstevel@tonic-gate 	exit(handler_exit_code);
6087c478bd9Sstevel@tonic-gate }
6097c478bd9Sstevel@tonic-gate 
6107c478bd9Sstevel@tonic-gate /*
6117c478bd9Sstevel@tonic-gate  * probe_stack
6127c478bd9Sstevel@tonic-gate  *
6137c478bd9Sstevel@tonic-gate  * Warning -- if you do a printf or fprintf prior to the actual
6147c478bd9Sstevel@tonic-gate  * reading from the stack, you've changed the stack to an unknown
6157c478bd9Sstevel@tonic-gate  * state.  (stack memory isn't free'd automatically and this function
6167c478bd9Sstevel@tonic-gate  * needs to touch virgin stack space.)
6177c478bd9Sstevel@tonic-gate  */
6187c478bd9Sstevel@tonic-gate static void
probe_stack(void)6197c478bd9Sstevel@tonic-gate probe_stack(void)
6207c478bd9Sstevel@tonic-gate {
6217c478bd9Sstevel@tonic-gate 	unsigned char *end;	/* end of stack */
6227c478bd9Sstevel@tonic-gate 	unsigned char probe;
6237c478bd9Sstevel@tonic-gate 	long i;
6247c478bd9Sstevel@tonic-gate 	int j;
6257c478bd9Sstevel@tonic-gate 	unsigned char last_fail, *last_fail_address;
6267c478bd9Sstevel@tonic-gate 	unsigned char mark = 0xAA;	/* roughly the end of stack */
6277c478bd9Sstevel@tonic-gate 	handler_exit_code = FAIL_SEGV;
6287c478bd9Sstevel@tonic-gate 
6297c478bd9Sstevel@tonic-gate 	end = &mark;
6307c478bd9Sstevel@tonic-gate 	/* stack growth is negative */
6317c478bd9Sstevel@tonic-gate 	end -= (WASTE_PAGES * PAGESIZE) + STACK_SLOP;
6327c478bd9Sstevel@tonic-gate 
6337c478bd9Sstevel@tonic-gate 	for (i = 0, j = 0; i < PAGESIZE; i++) {
6347c478bd9Sstevel@tonic-gate 		if ((probe = *end) != 0) {
6357c478bd9Sstevel@tonic-gate 			j++;
6367c478bd9Sstevel@tonic-gate 			last_fail = probe;
6377c478bd9Sstevel@tonic-gate 			last_fail_address = end;
6387c478bd9Sstevel@tonic-gate 		}
6397c478bd9Sstevel@tonic-gate 		end--;
6407c478bd9Sstevel@tonic-gate 	}
6417c478bd9Sstevel@tonic-gate 
6427c478bd9Sstevel@tonic-gate 	if (j != 0) {
6437c478bd9Sstevel@tonic-gate 		if (silent == NOT_SILENT)
6447c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
6457c478bd9Sstevel@tonic-gate 			    "probe_stack failed. address=0x%08X; "
6467c478bd9Sstevel@tonic-gate 			    "probe=0x%02X; content = %d\n"),
6477c478bd9Sstevel@tonic-gate 			    (caddr_t)last_fail_address, last_fail, j);
6487c478bd9Sstevel@tonic-gate 
6497c478bd9Sstevel@tonic-gate 		exit(FAIL_ZERO);    /* test failed at least once */
6507c478bd9Sstevel@tonic-gate 	}
6517c478bd9Sstevel@tonic-gate 	exit(EXIT_SUCCESS);
6527c478bd9Sstevel@tonic-gate }
6537c478bd9Sstevel@tonic-gate 
6547c478bd9Sstevel@tonic-gate static void
probe_hole(int test_type)6557c478bd9Sstevel@tonic-gate probe_hole(int test_type)
6567c478bd9Sstevel@tonic-gate {
6577c478bd9Sstevel@tonic-gate 	long i;
6587c478bd9Sstevel@tonic-gate 	/* LINTED */
659*f667abe5SToomas Soome 	volatile unsigned char probe __unused;
6607c478bd9Sstevel@tonic-gate 	unsigned char *probe_adr;
6617c478bd9Sstevel@tonic-gate 	void *address;
6627c478bd9Sstevel@tonic-gate 
6637c478bd9Sstevel@tonic-gate 	address = sbrk(0);  /* current end data + 1 */
6647c478bd9Sstevel@tonic-gate 
6657c478bd9Sstevel@tonic-gate 	if (address == (void *)-1) {
6667c478bd9Sstevel@tonic-gate 		print_message(gettext("Test program logic error\n"));
6677c478bd9Sstevel@tonic-gate 		exit(FAIL_ABORT);
6687c478bd9Sstevel@tonic-gate 	}
6697c478bd9Sstevel@tonic-gate 	if (test_type == PH_VALID) {
6707c478bd9Sstevel@tonic-gate 		/* show that access works inside the  hole */
6717c478bd9Sstevel@tonic-gate 		handler_exit_code = FAIL_SEGV;
6727c478bd9Sstevel@tonic-gate 
6737c478bd9Sstevel@tonic-gate 		probe_adr =  (unsigned char *)address - sizeof (char);
6747c478bd9Sstevel@tonic-gate 
6757c478bd9Sstevel@tonic-gate 		for (i = 0; i < PAGESIZE; i++)
6767c478bd9Sstevel@tonic-gate 			probe = *probe_adr--;
6777c478bd9Sstevel@tonic-gate 
6787c478bd9Sstevel@tonic-gate 		exit(EXIT_SUCCESS);
6797c478bd9Sstevel@tonic-gate 	} else {
6807c478bd9Sstevel@tonic-gate 		/* show that a trap occurs in the  hole */
6817c478bd9Sstevel@tonic-gate 		handler_exit_code = PASS_SEGV;
6827c478bd9Sstevel@tonic-gate 
6837c478bd9Sstevel@tonic-gate 		address = (void *)P2ROUNDUP((uintptr_t)address, PAGESIZE);
6847c478bd9Sstevel@tonic-gate 		probe_adr = (unsigned char *)address;
6857c478bd9Sstevel@tonic-gate 
6867c478bd9Sstevel@tonic-gate 		probe = *probe_adr;
6877c478bd9Sstevel@tonic-gate 		exit(FAIL_SEGV);	/* expected SEGV, didn't get it */
6887c478bd9Sstevel@tonic-gate 	}
6897c478bd9Sstevel@tonic-gate }
6907c478bd9Sstevel@tonic-gate 
6917c478bd9Sstevel@tonic-gate /*
6927c478bd9Sstevel@tonic-gate  * Catch signal, child to parent
6937c478bd9Sstevel@tonic-gate  */
6947c478bd9Sstevel@tonic-gate /*ARGSUSED*/
6957c478bd9Sstevel@tonic-gate void
handler(int signal)6967c478bd9Sstevel@tonic-gate handler(int signal)
6977c478bd9Sstevel@tonic-gate {
6987c478bd9Sstevel@tonic-gate 	done_memory_grab = 1;
6997c478bd9Sstevel@tonic-gate }
7007c478bd9Sstevel@tonic-gate /*
7017c478bd9Sstevel@tonic-gate  * memory_type:  Determine whether a given executable file is compiled
7027c478bd9Sstevel@tonic-gate  * as 32 or 64 bit.
7037c478bd9Sstevel@tonic-gate  *
7047c478bd9Sstevel@tonic-gate  * The following code was stolen from isainfo (1)
7057c478bd9Sstevel@tonic-gate  */
7067c478bd9Sstevel@tonic-gate 
7077c478bd9Sstevel@tonic-gate static int
memory_type(const char * path)708*f667abe5SToomas Soome memory_type(const char *path)
709*f667abe5SToomas Soome {
7107c478bd9Sstevel@tonic-gate 	char *idarray;
7117c478bd9Sstevel@tonic-gate 	Elf *elf;
7127c478bd9Sstevel@tonic-gate 	int d;
7137c478bd9Sstevel@tonic-gate 	int bits = 0;
7147c478bd9Sstevel@tonic-gate 
7157c478bd9Sstevel@tonic-gate 	if ((d = open(path, O_RDONLY)) < 0) {
7167c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
7177c478bd9Sstevel@tonic-gate 		    "cannot open: %s -- %s\n",
7187c478bd9Sstevel@tonic-gate 		    path, strerror(errno));
7197c478bd9Sstevel@tonic-gate 		return (bits);
7207c478bd9Sstevel@tonic-gate 	}
7217c478bd9Sstevel@tonic-gate 
7227c478bd9Sstevel@tonic-gate 	if (elf_version(EV_CURRENT) == EV_NONE) {
7237c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
7247c478bd9Sstevel@tonic-gate 		    "internal error: ELF library out of date?\n");
7257c478bd9Sstevel@tonic-gate 		(void) close(d);
7267c478bd9Sstevel@tonic-gate 		return (bits);
7277c478bd9Sstevel@tonic-gate 	}
7287c478bd9Sstevel@tonic-gate 
7297c478bd9Sstevel@tonic-gate 	elf = elf_begin(d, ELF_C_READ, (Elf *)0);
7307c478bd9Sstevel@tonic-gate 	if (elf_kind(elf) != ELF_K_ELF) {
7317c478bd9Sstevel@tonic-gate 		(void) elf_end(elf);
7327c478bd9Sstevel@tonic-gate 		(void) close(d);
7337c478bd9Sstevel@tonic-gate 		return (bits);
7347c478bd9Sstevel@tonic-gate 	}
7357c478bd9Sstevel@tonic-gate 
7367c478bd9Sstevel@tonic-gate 	idarray = elf_getident(elf, 0);
7377c478bd9Sstevel@tonic-gate 
7387c478bd9Sstevel@tonic-gate 	if (idarray[EI_CLASS] == ELFCLASS32) {
7397c478bd9Sstevel@tonic-gate 		bits = 32;
7407c478bd9Sstevel@tonic-gate 	} else if (idarray[EI_CLASS] == ELFCLASS64) {
7417c478bd9Sstevel@tonic-gate 		bits = 64;
7427c478bd9Sstevel@tonic-gate 	}
7437c478bd9Sstevel@tonic-gate 
7447c478bd9Sstevel@tonic-gate 	(void) elf_end(elf);
7457c478bd9Sstevel@tonic-gate 	(void) close(d);
7467c478bd9Sstevel@tonic-gate 	return (bits);
7477c478bd9Sstevel@tonic-gate }
748