xref: /onnv-gate/usr/src/cmd/dtrace/test/tst/i386/ustack/tst.helper.c (revision 3944:75371f172291)
12633Sahl /*
22633Sahl  * CDDL HEADER START
32633Sahl  *
42633Sahl  * The contents of this file are subject to the terms of the
52633Sahl  * Common Development and Distribution License (the "License").
62633Sahl  * You may not use this file except in compliance with the License.
72633Sahl  *
82633Sahl  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
92633Sahl  * or http://www.opensolaris.org/os/licensing.
102633Sahl  * See the License for the specific language governing permissions
112633Sahl  * and limitations under the License.
122633Sahl  *
132633Sahl  * When distributing Covered Code, include this CDDL HEADER in each
142633Sahl  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
152633Sahl  * If applicable, add the following below this CDDL HEADER, with the
162633Sahl  * fields enclosed by brackets "[]" replaced with your own identifying
172633Sahl  * information: Portions Copyright [yyyy] [name of copyright owner]
182633Sahl  *
192633Sahl  * CDDL HEADER END
202633Sahl  */
212633Sahl 
222633Sahl /*
23*3944Sahl  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
242633Sahl  * Use is subject to license terms.
252633Sahl  */
262633Sahl 
272633Sahl #pragma ident	"%Z%%M%	%I%	%E% SMI"
282633Sahl 
292633Sahl #include <stdint.h>
30*3944Sahl #include <stdlib.h>
31*3944Sahl #include <strings.h>
322633Sahl 
332633Sahl int
baz(void)342633Sahl baz(void)
352633Sahl {
362633Sahl 	return (8);
372633Sahl }
382633Sahl 
392633Sahl static int
foo(void)402633Sahl foo(void)
412633Sahl {
422633Sahl 	/*
432633Sahl 	 * In order to assure that our helper is properly employed to identify
442633Sahl 	 * the frame, we're going to trampoline through data.
452633Sahl 	 */
46*3944Sahl 	uint8_t instr[] = {
472633Sahl 	    0x55,			/* pushl %ebp		*/
482633Sahl 	    0x8b, 0xec,			/* movl  %esp, %ebp	*/
492633Sahl 	    0xe8, 0x0, 0x0, 0x0, 0x0,	/* call  baz		*/
502633Sahl 	    0x8b, 0xe5,			/* movl  %ebp, %esp	*/
512633Sahl 	    0x5d,			/* popl  %ebp		*/
522633Sahl 	    0xc3			/* ret			*/
532633Sahl 	};
54*3944Sahl 	uint8_t *fp = malloc(sizeof (instr));
552633Sahl 
56*3944Sahl 	/*
57*3944Sahl 	 * Do our little relocation dance.
58*3944Sahl 	 */
59*3944Sahl 	*((int *)&instr[4]) = (uintptr_t)baz - (uintptr_t)&fp[8];
60*3944Sahl 
61*3944Sahl 	/*
62*3944Sahl 	 * Copy the code to the heap (it's a pain to build in ON with an
63*3944Sahl 	 * executable stack).
64*3944Sahl 	 */
65*3944Sahl 	bcopy(instr, fp, sizeof (instr));
66*3944Sahl 
67*3944Sahl 	(*(int (*)(void))fp)();
68*3944Sahl 
69*3944Sahl 	free(fp);
70*3944Sahl 
71*3944Sahl 	return (0);
722633Sahl }
732633Sahl 
742633Sahl int
main(int argc,char ** argv)752633Sahl main(int argc, char **argv)
762633Sahl {
772633Sahl 	for (;;) {
782633Sahl 		foo();
792633Sahl 	}
802633Sahl 
812633Sahl 	return (0);
822633Sahl }
83