Skip to content

Commit

Permalink
Add support for HandleRef to the Marshaller
Browse files Browse the repository at this point in the history
2007-02-10  Miguel de Icaza  <miguel@novell.com>

	* class-internals.h: Add MonoHandleRef definition, and
	handleref_class to mono_defaults. 

	* metadata.c (mono_type_to_unmanaged): If we find HandleRefs in a
	structure, use new conversion MONO_MARSHAL_CONV_HANDLEREF.

	* marshal.c (emit_ptr_to_object_conv): Add support for HandleRefs
	(do nothing on this stage)
	(emit_object_to_ptr_conv): Extract the handle from the HandleRef.  
	(emit_marshal_handleref): New method, used for argument handling
	of HandleRefs. 

Tests are a simplified version of SafeHandle tests

Fixes: 80515


svn path=/trunk/mono/; revision=72601
  • Loading branch information
migueldeicaza committed Feb 11, 2007
1 parent 8d39339 commit d59ce8f
Show file tree
Hide file tree
Showing 8 changed files with 234 additions and 13 deletions.
14 changes: 14 additions & 0 deletions mono/metadata/ChangeLog
@@ -1,3 +1,17 @@
2007-02-10 Miguel de Icaza <miguel@novell.com>

* class-internals.h: Add MonoHandleRef definition, and
handleref_class to mono_defaults.

* metadata.c (mono_type_to_unmanaged): If we find HandleRefs in a
structure, use new conversion MONO_MARSHAL_CONV_HANDLEREF.

* marshal.c (emit_ptr_to_object_conv): Add support for HandleRefs
(do nothing on this stage)
(emit_object_to_ptr_conv): Extract the handle from the HandleRef.
(emit_marshal_handleref): New method, used for argument handling
of HandleRefs.

2007-02-08 Jonathan Chambers <joncham@gmail.com>

* class.c (mono_class_setup_parent): Lazily init com types.
Expand Down
9 changes: 9 additions & 0 deletions mono/metadata/class-internals.h
Expand Up @@ -528,6 +528,14 @@ typedef struct {
void *handle;
} MonoSafeHandle;

/*
* Keep in sync with HandleRef.cs
*/
typedef struct {
MonoObject *wrapper;
void *handle;
} MonoHandleRef;

extern MonoStats mono_stats MONO_INTERNAL;

typedef gpointer (*MonoTrampoline) (MonoMethod *method);
Expand Down Expand Up @@ -692,6 +700,7 @@ typedef struct {
MonoClass *iunknown_class;
MonoClass *idispatch_class;
MonoClass *safehandle_class;
MonoClass *handleref_class;
} MonoDefaults;

extern MonoDefaults mono_defaults MONO_INTERNAL;
Expand Down
3 changes: 3 additions & 0 deletions mono/metadata/domain.c
Expand Up @@ -815,6 +815,9 @@ mono_init_internal (const char *filename, const char *exe_filename, const char *
mono_defaults.safehandle_class = mono_class_from_name (
mono_defaults.corlib, "System.Runtime.InteropServices", "SafeHandle");

mono_defaults.handleref_class = mono_class_from_name (
mono_defaults.corlib, "System.Runtime.InteropServices", "HandleRef");

/* these are initialized lazily when COM features are used */
mono_defaults.variant_class = NULL;
mono_defaults.com_object_class = NULL;
Expand Down
103 changes: 92 additions & 11 deletions mono/metadata/marshal.c
Expand Up @@ -2025,6 +2025,14 @@ emit_ptr_to_object_conv (MonoMethodBuilder *mb, MonoType *type, MonoMarshalConv
break;
}

case MONO_MARSHAL_CONV_HANDLEREF: {
/*
* Passing HandleRefs in a struct that is ref()ed does not
* copy the values back to the HandleRef
*/
break;
}

case MONO_MARSHAL_CONV_STR_BSTR:
case MONO_MARSHAL_CONV_STR_ANSIBSTR:
case MONO_MARSHAL_CONV_STR_TBSTR:
Expand Down Expand Up @@ -2415,6 +2423,16 @@ emit_object_to_ptr_conv (MonoMethodBuilder *mb, MonoType *type, MonoMarshalConv
mono_mb_emit_byte (mb, CEE_STIND_I);
break;
}

case MONO_MARSHAL_CONV_HANDLEREF: {
mono_mb_emit_ldloc (mb, 1);
mono_mb_emit_ldloc (mb, 0);
mono_mb_emit_ldflda (mb, G_STRUCT_OFFSET (MonoHandleRef, handle));
mono_mb_emit_byte (mb, CEE_LDIND_I);
mono_mb_emit_byte (mb, CEE_STIND_I);
break;
}

default: {
char *msg = g_strdup_printf ("marshalling conversion %d not implemented", conv);
MonoException *exc = mono_get_exception_not_implemented (msg);
Expand Down Expand Up @@ -6441,6 +6459,66 @@ emit_marshal_safehandle (EmitMarshalContext *m, int argnum, MonoType *t,
return conv_arg;
}

static int
emit_marshal_handleref (EmitMarshalContext *m, int argnum, MonoType *t,
MonoMarshalSpec *spec, int conv_arg,
MonoType **conv_arg_type, MarshalAction action)
{
MonoMethodBuilder *mb = m->mb;

switch (action){
case MARSHAL_ACTION_CONV_IN: {
MonoType *intptr_type;

intptr_type = &mono_defaults.int_class->byval_arg;
conv_arg = mono_mb_add_local (mb, intptr_type);
*conv_arg_type = intptr_type;

if (t->byref){
mono_mb_emit_exception_marshal_directive (mb,
"HandleRefs can not be returned from unmanaged code (or passed by ref)");
break;
}
mono_mb_emit_ldarg (mb, argnum);
mono_mb_emit_ldflda (mb, G_STRUCT_OFFSET (MonoHandleRef, handle));
mono_mb_emit_byte (mb, CEE_LDIND_I);
mono_mb_emit_stloc (mb, conv_arg);
break;
}

case MARSHAL_ACTION_PUSH:
mono_mb_emit_ldloc (mb, conv_arg);
break;

case MARSHAL_ACTION_CONV_OUT: {
/* no resource release required */
break;
}

case MARSHAL_ACTION_CONV_RESULT: {
mono_mb_emit_exception_marshal_directive (mb,
"HandleRefs can not be returned from unmanaged code (or passed by ref)");
break;
}

case MARSHAL_ACTION_MANAGED_CONV_IN:
fprintf (stderr, "mono/marshal: SafeHandles missing MANAGED_CONV_IN\n");
break;

case MARSHAL_ACTION_MANAGED_CONV_OUT:
fprintf (stderr, "mono/marshal: SafeHandles missing MANAGED_CONV_OUT\n");
break;

case MARSHAL_ACTION_MANAGED_CONV_RESULT:
fprintf (stderr, "mono/marshal: SafeHandles missing MANAGED_CONV_RESULT\n");
break;
default:
fprintf (stderr, "Unhandled case for MarshalAction: %d\n", action);
}

return conv_arg;
}

static int
emit_marshal_object (EmitMarshalContext *m, int argnum, MonoType *t,
MonoMarshalSpec *spec,
Expand Down Expand Up @@ -7879,9 +7957,9 @@ emit_marshal_array (EmitMarshalContext *m, int argnum, MonoType *t,

static int
emit_marshal_boolean (EmitMarshalContext *m, int argnum, MonoType *t,
MonoMarshalSpec *spec,
int conv_arg, MonoType **conv_arg_type,
MarshalAction action)
MonoMarshalSpec *spec,
int conv_arg, MonoType **conv_arg_type,
MarshalAction action)
{
MonoMethodBuilder *mb = m->mb;

Expand Down Expand Up @@ -7949,8 +8027,8 @@ emit_marshal_boolean (EmitMarshalContext *m, int argnum, MonoType *t,

static int
emit_marshal_ptr (EmitMarshalContext *m, int argnum, MonoType *t,
MonoMarshalSpec *spec, int conv_arg,
MonoType **conv_arg_type, MarshalAction action)
MonoMarshalSpec *spec, int conv_arg,
MonoType **conv_arg_type, MarshalAction action)
{
MonoMethodBuilder *mb = m->mb;

Expand Down Expand Up @@ -7980,8 +8058,8 @@ emit_marshal_ptr (EmitMarshalContext *m, int argnum, MonoType *t,

static int
emit_marshal_char (EmitMarshalContext *m, int argnum, MonoType *t,
MonoMarshalSpec *spec, int conv_arg,
MonoType **conv_arg_type, MarshalAction action)
MonoMarshalSpec *spec, int conv_arg,
MonoType **conv_arg_type, MarshalAction action)
{
MonoMethodBuilder *mb = m->mb;

Expand All @@ -8007,8 +8085,8 @@ emit_marshal_char (EmitMarshalContext *m, int argnum, MonoType *t,

static int
emit_marshal_scalar (EmitMarshalContext *m, int argnum, MonoType *t,
MonoMarshalSpec *spec, int conv_arg,
MonoType **conv_arg_type, MarshalAction action)
MonoMarshalSpec *spec, int conv_arg,
MonoType **conv_arg_type, MarshalAction action)
{
MonoMethodBuilder *mb = m->mb;

Expand All @@ -8031,8 +8109,8 @@ emit_marshal_scalar (EmitMarshalContext *m, int argnum, MonoType *t,

static int
emit_marshal (EmitMarshalContext *m, int argnum, MonoType *t,
MonoMarshalSpec *spec, int conv_arg,
MonoType **conv_arg_type, MarshalAction action)
MonoMarshalSpec *spec, int conv_arg,
MonoType **conv_arg_type, MarshalAction action)
{
/* Ensure that we have marshalling info for this param */
mono_marshal_load_type_info (mono_class_from_mono_type (t));
Expand All @@ -8045,6 +8123,9 @@ emit_marshal (EmitMarshalContext *m, int argnum, MonoType *t,

switch (t->type) {
case MONO_TYPE_VALUETYPE:
if (t->data.klass == mono_defaults.handleref_class)
return emit_marshal_handleref (m, argnum, t, spec, conv_arg, conv_arg_type, action);

return emit_marshal_vtype (m, argnum, t, spec, conv_arg, conv_arg_type, action);
case MONO_TYPE_STRING:
return emit_marshal_string (m, argnum, t, spec, conv_arg, conv_arg_type, action);
Expand Down
3 changes: 3 additions & 0 deletions mono/metadata/metadata.c
Expand Up @@ -4225,6 +4225,9 @@ mono_type_to_unmanaged (MonoType *type, MonoMarshalSpec *mspec, gboolean as_fiel
t = type->data.klass->enum_basetype->type;
goto handle_enum;
}
if (type->data.klass == mono_defaults.handleref_class)
*conv = MONO_MARSHAL_CONV_HANDLEREF;
return MONO_NATIVE_INT;
return MONO_NATIVE_STRUCT;
case MONO_TYPE_SZARRAY:
case MONO_TYPE_ARRAY:
Expand Down
3 changes: 2 additions & 1 deletion mono/metadata/metadata.h
Expand Up @@ -177,7 +177,8 @@ typedef enum {
MONO_MARSHAL_CONV_FTN_DEL,
MONO_MARSHAL_FREE_ARRAY,
MONO_MARSHAL_CONV_BSTR_STR,
MONO_MARSHAL_CONV_SAFEHANDLE
MONO_MARSHAL_CONV_SAFEHANDLE,
MONO_MARSHAL_CONV_HANDLEREF
} MonoMarshalConv;

typedef struct {
Expand Down
3 changes: 2 additions & 1 deletion mono/tests/Makefile.am
Expand Up @@ -226,7 +226,8 @@ TEST_CS_SRC= \
subthread-exit.cs \
desweak.cs \
cominterop.cs \
exists.cs
exists.cs \
handleref.cs

if X86
# bug #71274
Expand Down
109 changes: 109 additions & 0 deletions mono/tests/handleref.cs
@@ -0,0 +1,109 @@
using System;
using System.Runtime.InteropServices;

public class Tests {

//
// This is not permitted, should throw an exception
//
[DllImport ("libtest")]
public static extern void mono_safe_handle_ref (ref HandleRef handle);

[DllImport ("libtest", EntryPoint="mono_xr")]
public static extern HandleRef mono_xr_as_handle (HandleRef r);

[DllImport ("libtest")]
public static extern int mono_xr (HandleRef sh);

//
// Mono should throw exceptions on ref HandleRefs
//
public static int test_0_ref_handleref ()
{
object o = new object ();
HandleRef s = new HandleRef (o, (IntPtr) 0xeadcafe);
try {
mono_safe_handle_ref (ref s);
} catch (MarshalDirectiveException){
return 0;
}
// failed
return 1;
}

//
// Mono should throw excentions on return HandleRefs
//
public static int test_0_handleref_return ()
{
object o = new object ();
HandleRef s = new HandleRef (o, (IntPtr) 0xeadcafe);
try {
HandleRef ret = mono_xr_as_handle (s);
} catch (MarshalDirectiveException){
return 0;
}
// failed
return 1;
}

public static int test_0_marshal_handleref_argument ()
{
object o = new object ();
Console.WriteLine ("BEFORE");
HandleRef s = new HandleRef (o, (IntPtr) 0xeadcafe);
if (mono_xr (s) != (0xeadcafe + 1234))
return 1;
Console.WriteLine ("AFTER");
return 0;
}

[StructLayout (LayoutKind.Sequential)]
public struct StructTest {
public int a;
public HandleRef handle1;
public HandleRef handle2;
public int b;
}

[DllImport ("libtest")]
public static extern int mono_safe_handle_struct_ref (ref StructTest test);

[DllImport ("libtest")]
public static extern int mono_safe_handle_struct (StructTest test);

static StructTest x = new StructTest ();

public static int test_0_marshal_safehandle_field ()
{
x.a = 1234;
x.b = 8743;
object o = new object ();
x.handle1 = new HandleRef (o, (IntPtr) 0x7080feed);
x.handle2 = new HandleRef (o, (IntPtr) 0x1234abcd);

if (mono_safe_handle_struct (x) != 0xf00f)
return 1;

return 0;
}

public static int test_0_marshal_safehandle_field_ref ()
{
x.a = 1234;
x.b = 8743;
object o = new object ();
x.handle1 = new HandleRef (o, (IntPtr) 0x7080feed);
x.handle2 = new HandleRef (o, (IntPtr) 0x1234abcd);

if (mono_safe_handle_struct_ref (ref x) != 0xf00d)
return 1;

return 0;
}

static int Main ()
{
return TestDriver.RunTests (typeof (Tests));
}
}

0 comments on commit d59ce8f

Please sign in to comment.