source: trunk/Cocoa/F-Script Anywhere/Source/mach_inject/mach_inject.c@ 153

Last change on this file since 153 was 153, checked in by Nicholas Riley, 20 years ago

Integrates SCPatch and mach_inject; unfinished, buggy.

File size: 5.9 KB
RevLine 
[153]1/****************************************************************************************
2 mach_inject.c $Revision: 1.1.1.1 $
3
4 Copyright (c) 2003 Red Shed Software. All rights reserved.
5 by Jonathan 'Wolf' Rentzsch (jon * redshed * net)
6
7 ************************************************************************************/
8
9#ifdef __cplusplus
10extern "C" {
11#endif
12
13#include "mach_inject.h"
14
15#include <mach-o/dyld.h>
16#include <mach-o/getsect.h>
17#include <mach/message.h>
18#include <mach/mach.h>
19#include <sys/stat.h>
20#include <assert.h>
21#include <stdio.h>
22#include <errno.h>
23
24/* doesn't show up, for some reason */
25void bzero(void *, size_t);
26
27#ifdef __cplusplus
28}
29#endif
30
31#ifndef COMPILE_TIME_ASSERT
32 #define COMPILE_TIME_ASSERT( exp ) { switch (0) { case 0: case (exp):; } }
33#endif
34#define ASSERT_CAST( CAST_TO, CAST_FROM ) COMPILE_TIME_ASSERT( sizeof(CAST_TO)==sizeof(CAST_FROM) )
35
36/****************************************************************************************
37*
38* Interface
39*
40****************************************************************************************/
41#pragma mark -
42#pragma mark (Interface)
43
44 mach_error_t
45mach_inject(
46 const mach_inject_entry threadEntry,
47 const void *paramBlock,
48 size_t paramSize,
49 pid_t targetProcess,
50 vm_size_t stackSize ) {
51 ;//assertCodePtr( threadEntry );
52 ;//assertPtrIfNotNull( paramBlock );
53 ;//assertPositive( targetProcess );
54 ;//assertIsTrue( stackSize == 0 || stackSize > 1024 );
55
56 // Find the image.
57 const void *image;
58 unsigned long imageSize;
59 mach_error_t err = machImageForPointer( threadEntry, &image, &imageSize );
60
61 // Initialize stackSize to default if requested.
62 if( stackSize == 0 )
63 /** @bug We only want an 8K default, fix the plop-in-the-middle code below. */
64 stackSize = 16 * 1024;
65
66 // Convert PID to Mach Task ref.
67 mach_port_t remoteTask = 0;
68 if( !err )
69 err = task_for_pid( mach_task_self(), targetProcess, &remoteTask );
70
71 /** @todo Would be nice to just allocate one block for both the remote stack
72 *and* the remoteCode (including the parameter data block once that's
73 written.
74 */
75
76 // Allocate the remoteStack.
77 vm_address_t remoteStack = NULL;
78 if( !err )
79 err = vm_allocate( remoteTask, &remoteStack, stackSize, 1 );
80
81 // Allocate the code.
82 vm_address_t remoteCode = NULL;
83 if( !err )
84 err = vm_allocate( remoteTask, &remoteCode, imageSize, 1 );
85 if( !err ) {
86 ASSERT_CAST( pointer_t, image );
87 err = vm_write( remoteTask, remoteCode, (pointer_t) image, imageSize );
88 }
89
90 // Allocate the paramBlock if specified.
91 vm_address_t remoteParamBlock = NULL;
92 if( !err && paramBlock != NULL && paramSize ) {
93 err = vm_allocate( remoteTask, &remoteParamBlock, paramSize, 1 );
94 if( !err ) {
95 ASSERT_CAST( pointer_t, paramBlock );
96 err = vm_write( remoteTask, remoteParamBlock, (pointer_t) paramBlock, paramSize );
97 }
98 }
99
100 // Calculate offsets.
101 ptrdiff_t threadEntryOffset, imageOffset;
102 if( !err ) {
103 ;//assertIsWithinRange( threadEntry, image, image+imageSize );
104 ASSERT_CAST( void*, threadEntry );
105 threadEntryOffset = ((long) threadEntry) - (long) image;
106
107 ASSERT_CAST( void*, remoteCode );
108 imageOffset = ((long) remoteCode) - (long) image;
109 }
110
111 // Allocate the thread.
112 thread_act_t remoteThread;
113 if( !err ) {
114 ppc_thread_state_t remoteThreadState;
115
116 /** @bug Stack math should be more sophisticated than this (ala redzone). */
117 remoteStack += stackSize / 2;
118
119 bzero( &remoteThreadState, sizeof(remoteThreadState) );
120
121 ASSERT_CAST( unsigned int, remoteCode );
122 remoteThreadState.srr0 = (unsigned int) remoteCode;
123 remoteThreadState.srr0 += threadEntryOffset;
124 assert( remoteThreadState.srr0 < (remoteCode + imageSize) );
125
126 ASSERT_CAST( unsigned int, remoteStack );
127 remoteThreadState.r1 = (unsigned int) remoteStack;
128
129 ASSERT_CAST( unsigned int, imageOffset );
130 remoteThreadState.r3 = (unsigned int) imageOffset;
131
132 ASSERT_CAST( unsigned int, remoteParamBlock );
133 remoteThreadState.r4 = (unsigned int) remoteParamBlock;
134
135 ASSERT_CAST( unsigned int, paramSize );
136 remoteThreadState.r5 = (unsigned int) paramSize;
137
138 ASSERT_CAST( unsigned int, 0xDEADBEEF );
139 remoteThreadState.lr = (unsigned int) 0xDEADBEEF;
140
141 printf( "remoteCode start: %p\n", (void*) remoteCode );
142 printf( "remoteCode size: %ld\n", imageSize );
143 printf( "remoteCode pc: %p\n", (void*) remoteThreadState.srr0 );
144 printf( "remoteCode end: %p\n", (void*) (((char*)remoteCode)+imageSize) );
145 fflush(0);
146
147 err = thread_create_running( remoteTask, PPC_THREAD_STATE,
148 (thread_state_t) &remoteThreadState, PPC_THREAD_STATE_COUNT,
149 &remoteThread );
150 }
151
152 if( err ) {
153 if( remoteParamBlock )
154 vm_deallocate( remoteTask, remoteParamBlock, paramSize );
155 if( remoteCode )
156 vm_deallocate( remoteTask, remoteCode, imageSize );
157 if( remoteStack )
158 vm_deallocate( remoteTask, remoteStack, stackSize );
159 }
160
161 return err;
162}
163
164 mach_error_t
165machImageForPointer(
166 const void *pointer,
167 const void **image,
168 unsigned long *size ) {
169 ;//assertCodePtr( pointer );
170 ;//assertPtr( image );
171 ;//assertPtr( size );
172
173 unsigned long p = (unsigned long) pointer;
174
175 unsigned long imageIndex, imageCount = _dyld_image_count();
176 for( imageIndex = 0; imageIndex < imageCount; imageIndex++ ) {
177 struct mach_header *header = _dyld_get_image_header( imageIndex );
178 const struct section *section = getsectbynamefromheader( header, SEG_TEXT, SECT_TEXT );
179 long start = section->addr + _dyld_get_image_vmaddr_slide( imageIndex );
180 long stop = start + section->size;
181 if( p >= start && p <= stop ) {
182 // It is truly insane we have to stat() the file system in order to
183 // discover the size of an in-memory data structure.
184 char *imageName = _dyld_get_image_name( imageIndex );
185 ;//assertPath( imageName );
186 struct stat sb;
187 if( stat( imageName, &sb ) )
188 return unix_err( errno );
189 if( image ) {
190 ASSERT_CAST( void*, header );
191 *image = (void*) header;
192 }
193 if( size ) {
194 ;//assertUInt32( st_size );
195 *size = sb.st_size;
196 }
197 return err_none;
198 }
199 }
200
201 return err_threadEntry_image_not_found;
202}
Note: See TracBrowser for help on using the repository browser.