[Mono-devel-list] JIT, MonoCompile

Paolo Molaro lupus at ximian.com
Tue Jan 25 08:58:33 EST 2005

On 01/24/05 Kaushik Srenevasan wrote:
> I've been trying to insert some code into every JITted method by modifying
> the mono_codegen function in mini.c.
> I've added the following function in mini.c
> Void foo( void )
> {
> printf( "." );
> }
> And then the following lines 
> Mono_add_patch_info( cfg, code - cfg->native_code, MONO_PATCH_INFO_ABS,
> foo); X86_call_code( code, 0 );

As I told you on both IRC and on private email, this is not the way to
implement this.

> But earlier I tried inserting only
> X86_call_code( code, foo )
> And I got a segfault.

No surprise, since you didn't use the mono_emit_jit_icall() function I
told you to use.

> I thought this would work because all that x86_call_code does ( as far as I
> understood ) is to emit a call <offset> instruction into the code stream.
> And since I am calling a function that neither takes any parameters nor
> returns any, I wouldn't be corrupting the stack either. But I fail to
> understand why it segfaulted.

You likely inserted the code before it got relocated in memory and since
the x86 call uses a relative offset, you made it call a random location 
in memory.

As an example of how to implement it, see the attached patch:
it will insert the call only for methods that have the init_locals
basic block created (trivial to make it add the call for any method).

> How does the runtime make use of MonoCompile?

MonoCompile represents the compilation unit the jit operates on
(usually a method and the methods that are possibly inlined in it).


lupus at debian.org                                     debian/rules
lupus at ximian.com                             Monkeys do it better
-------------- next part --------------
Index: mini/mini.c
--- mini/mini.c	(revision 39494)
+++ mini/mini.c	(working copy)
@@ -161,6 +161,12 @@
+static void
+dummy_enter (void)
+	printf (".");
 /* debug function */
 G_GNUC_UNUSED static char*
 get_method_from_ip (void *ip)
@@ -3226,6 +3232,7 @@
 		link_bblock (cfg, start_bblock, init_localsbb);
 		link_bblock (cfg, init_localsbb, bblock);
 		init_localsbb->block_num = cfg->num_bblocks++;
+		mono_emit_jit_icall (cfg, init_localsbb, dummy_enter, NULL, NULL);
 	} else {
 		start_bblock->next_bb = bblock;
 		link_bblock (cfg, start_bblock, bblock);
@@ -9745,6 +9752,7 @@
 	mono_register_jit_icall (mono_ldftn_nosync, "mono_ldftn_nosync", helper_sig_compile, FALSE);
 	mono_register_jit_icall (mono_ldvirtfn, "mono_ldvirtfn", helper_sig_compile_virt, FALSE);
 	mono_register_jit_icall (helper_compile_generic_method, "compile_generic_method", helper_sig_compile_generic_method, FALSE);
+	mono_register_jit_icall (dummy_enter, "dummy_enter", helper_sig_void_void, TRUE);

More information about the Mono-devel-list mailing list