tst.helper.c revision 178476
1288522Sgrehan/* 2288522Sgrehan * CDDL HEADER START 3288522Sgrehan * 4288522Sgrehan * The contents of this file are subject to the terms of the 5288522Sgrehan * Common Development and Distribution License (the "License"). 6288522Sgrehan * You may not use this file except in compliance with the License. 7288522Sgrehan * 8288522Sgrehan * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9288522Sgrehan * or http://www.opensolaris.org/os/licensing. 10288522Sgrehan * See the License for the specific language governing permissions 11288522Sgrehan * and limitations under the License. 12288522Sgrehan * 13288522Sgrehan * When distributing Covered Code, include this CDDL HEADER in each 14288522Sgrehan * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15288522Sgrehan * If applicable, add the following below this CDDL HEADER, with the 16288522Sgrehan * fields enclosed by brackets "[]" replaced with your own identifying 17288522Sgrehan * information: Portions Copyright [yyyy] [name of copyright owner] 18288522Sgrehan * 19288522Sgrehan * CDDL HEADER END 20288522Sgrehan */ 21288522Sgrehan 22288522Sgrehan/* 23288522Sgrehan * Copyright 2007 Sun Microsystems, Inc. All rights reserved. 24288522Sgrehan * Use is subject to license terms. 25288522Sgrehan */ 26288522Sgrehan 27288522Sgrehan#pragma ident "%Z%%M% %I% %E% SMI" 28288522Sgrehan 29288522Sgrehan#include <stdint.h> 30288522Sgrehan#include <stdlib.h> 31288522Sgrehan#include <strings.h> 32288522Sgrehan 33288522Sgrehanint 34288522Sgrehanbaz(void) 35288522Sgrehan{ 36288522Sgrehan return (8); 37288522Sgrehan} 38288522Sgrehan 39288522Sgrehanstatic int 40288522Sgrehanfoo(void) 41288522Sgrehan{ 42288522Sgrehan /* 43288522Sgrehan * In order to assure that our helper is properly employed to identify 44288522Sgrehan * the frame, we're going to trampoline through data. 45288522Sgrehan */ 46288522Sgrehan uint8_t instr[] = { 47288522Sgrehan 0x55, /* pushl %ebp */ 48288522Sgrehan 0x8b, 0xec, /* movl %esp, %ebp */ 49288522Sgrehan 0xe8, 0x0, 0x0, 0x0, 0x0, /* call baz */ 50288522Sgrehan 0x8b, 0xe5, /* movl %ebp, %esp */ 51288522Sgrehan 0x5d, /* popl %ebp */ 52288522Sgrehan 0xc3 /* ret */ 53288522Sgrehan }; 54288522Sgrehan uint8_t *fp = malloc(sizeof (instr)); 55 56 /* 57 * Do our little relocation dance. 58 */ 59 *((int *)&instr[4]) = (uintptr_t)baz - (uintptr_t)&fp[8]; 60 61 /* 62 * Copy the code to the heap (it's a pain to build in ON with an 63 * executable stack). 64 */ 65 bcopy(instr, fp, sizeof (instr)); 66 67 (*(int (*)(void))fp)(); 68 69 free(fp); 70 71 return (0); 72} 73 74int 75main(int argc, char **argv) 76{ 77 for (;;) { 78 foo(); 79 } 80 81 return (0); 82} 83