/
cominterop.c
3312 lines (2761 loc) · 94.9 KB
/
cominterop.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* cominterop.c: COM Interop Support
*
*
* (C) 2002 Ximian, Inc. http://www.ximian.com
*
*/
#include "config.h"
#ifdef HAVE_ALLOCA_H
#include <alloca.h>
#endif
#include "object.h"
#include "loader.h"
#include "cil-coff.h"
#include "metadata/cominterop.h"
#include "metadata/marshal.h"
#include "metadata/method-builder.h"
#include "metadata/tabledefs.h"
#include "metadata/exception.h"
#include "metadata/appdomain.h"
#include "mono/metadata/debug-helpers.h"
#include "mono/metadata/threadpool.h"
#include "mono/metadata/threads.h"
#include "mono/metadata/monitor.h"
#include "mono/metadata/metadata-internals.h"
#include "mono/metadata/domain-internals.h"
#include "mono/metadata/gc-internal.h"
#include "mono/metadata/threads-types.h"
#include "mono/metadata/string-icalls.h"
#include "mono/metadata/attrdefs.h"
#include "mono/metadata/gc-internal.h"
#include "mono/utils/mono-counters.h"
#include "mono/utils/strenc.h"
#include "mono/utils/atomic.h"
#include <string.h>
#include <errno.h>
/*
Code shared between the DISABLE_COM and !DISABLE_COM
*/
static void
register_icall (gpointer func, const char *name, const char *sigstr, gboolean save)
{
MonoMethodSignature *sig = mono_create_icall_signature (sigstr);
mono_register_jit_icall (func, name, sig, save);
}
#ifndef DISABLE_COM
#define OPDEF(a,b,c,d,e,f,g,h,i,j) \
a = i,
typedef enum {
MONO_MARSHAL_NONE, /* No marshalling needed */
MONO_MARSHAL_COPY, /* Can be copied by value to the new domain */
MONO_MARSHAL_COPY_OUT, /* out parameter that needs to be copied back to the original instance */
MONO_MARSHAL_SERIALIZE /* Value needs to be serialized into the new domain */
} MonoXDomainMarshalType;
typedef enum {
MONO_COM_DEFAULT,
MONO_COM_MS
} MonoCOMProvider;
static MonoCOMProvider com_provider = MONO_COM_DEFAULT;
enum {
#include "mono/cil/opcode.def"
LAST = 0xff
};
#undef OPDEF
/* This mutex protects the various cominterop related caches in MonoImage */
#define mono_cominterop_lock() EnterCriticalSection (&cominterop_mutex)
#define mono_cominterop_unlock() LeaveCriticalSection (&cominterop_mutex)
static CRITICAL_SECTION cominterop_mutex;
/* STDCALL on windows, CDECL everywhere else to work with XPCOM and MainWin COM */
#ifdef HOST_WIN32
#define STDCALL __stdcall
#else
#define STDCALL
#endif
GENERATE_GET_CLASS_WITH_CACHE (interop_proxy, Mono.Interop, ComInteropProxy)
GENERATE_GET_CLASS_WITH_CACHE (idispatch, Mono.Interop, IDispatch)
GENERATE_GET_CLASS_WITH_CACHE (iunknown, Mono.Interop, IUnknown)
GENERATE_GET_CLASS_WITH_CACHE (com_object, System, __ComObject)
GENERATE_GET_CLASS_WITH_CACHE (variant, System, Variant)
/* Upon creation of a CCW, only allocate a weak handle and set the
* reference count to 0. If the unmanaged client code decides to addref and
* hold onto the CCW, I then allocate a strong handle. Once the reference count
* goes back to 0, convert back to a weak handle.
*/
typedef struct {
guint32 ref_count;
guint32 gc_handle;
GHashTable* vtable_hash;
#ifdef HOST_WIN32
gpointer free_marshaler;
#endif
} MonoCCW;
/* This type is the actual pointer passed to unmanaged code
* to represent a COM interface.
*/
typedef struct {
gpointer vtable;
MonoCCW* ccw;
} MonoCCWInterface;
/* IUnknown */
static int STDCALL cominterop_ccw_addref (MonoCCWInterface* ccwe);
static int STDCALL cominterop_ccw_release (MonoCCWInterface* ccwe);
static int STDCALL cominterop_ccw_queryinterface (MonoCCWInterface* ccwe, guint8* riid, gpointer* ppv);
/* IDispatch */
static int STDCALL cominterop_ccw_get_type_info_count (MonoCCWInterface* ccwe, guint32 *pctinfo);
static int STDCALL cominterop_ccw_get_type_info (MonoCCWInterface* ccwe, guint32 iTInfo, guint32 lcid, gpointer *ppTInfo);
static int STDCALL cominterop_ccw_get_ids_of_names (MonoCCWInterface* ccwe, gpointer riid,
gunichar2** rgszNames, guint32 cNames,
guint32 lcid, gint32 *rgDispId);
static int STDCALL cominterop_ccw_invoke (MonoCCWInterface* ccwe, guint32 dispIdMember,
gpointer riid, guint32 lcid,
guint16 wFlags, gpointer pDispParams,
gpointer pVarResult, gpointer pExcepInfo,
guint32 *puArgErr);
static MonoMethod *
cominterop_get_managed_wrapper_adjusted (MonoMethod *method);
static gpointer
cominterop_get_ccw (MonoObject* object, MonoClass* itf);
static MonoObject*
cominterop_get_ccw_object (MonoCCWInterface* ccw_entry, gboolean verify);
/* SAFEARRAY marshalling */
static gboolean
mono_marshal_safearray_begin (gpointer safearray, MonoArray **result, gpointer *indices, gpointer empty, gpointer parameter, gboolean allocateNewArray);
static gpointer
mono_marshal_safearray_get_value (gpointer safearray, gpointer indices);
static gboolean
mono_marshal_safearray_next (gpointer safearray, gpointer indices);
static void
mono_marshal_safearray_end (gpointer safearray, gpointer indices);
static gboolean
mono_marshal_safearray_create (MonoArray *input, gpointer *newsafearray, gpointer *indices, gpointer empty);
static void
mono_marshal_safearray_set_value (gpointer safearray, gpointer indices, gpointer value);
static void
mono_marshal_safearray_free_indices (gpointer indices);
/**
* cominterop_method_signature:
* @method: a method
*
* Returns: the corresponding unmanaged method signature for a managed COM
* method.
*/
static MonoMethodSignature*
cominterop_method_signature (MonoMethod* method)
{
MonoMethodSignature *res;
MonoImage *image = method->klass->image;
MonoMethodSignature *sig = mono_method_signature (method);
gboolean preserve_sig = method->iflags & METHOD_IMPL_ATTRIBUTE_PRESERVE_SIG;
int sigsize;
int i;
int param_count = sig->param_count + 1; // convert this arg into IntPtr arg
if (!preserve_sig &&!MONO_TYPE_IS_VOID (sig->ret))
param_count++;
res = mono_metadata_signature_alloc (image, param_count);
sigsize = MONO_SIZEOF_METHOD_SIGNATURE + sig->param_count * sizeof (MonoType *);
memcpy (res, sig, sigsize);
// now move args forward one
for (i = sig->param_count-1; i >= 0; i--)
res->params[i+1] = sig->params[i];
// first arg is interface pointer
res->params[0] = &mono_defaults.int_class->byval_arg;
if (preserve_sig) {
res->ret = sig->ret;
}
else {
// last arg is return type
if (!MONO_TYPE_IS_VOID (sig->ret)) {
res->params[param_count-1] = mono_metadata_type_dup (image, sig->ret);
res->params[param_count-1]->byref = 1;
res->params[param_count-1]->attrs = PARAM_ATTRIBUTE_OUT;
}
// return type is always int32 (HRESULT)
res->ret = &mono_defaults.int32_class->byval_arg;
}
// no pinvoke
res->pinvoke = FALSE;
// no hasthis
res->hasthis = 0;
// set param_count
res->param_count = param_count;
// STDCALL on windows, CDECL everywhere else to work with XPCOM and MainWin COM
#ifdef HOST_WIN32
res->call_convention = MONO_CALL_STDCALL;
#else
res->call_convention = MONO_CALL_C;
#endif
return res;
}
/**
* cominterop_get_function_pointer:
* @itf: a pointer to the COM interface
* @slot: the vtable slot of the method pointer to return
*
* Returns: the unmanaged vtable function pointer from the interface
*/
static gpointer
cominterop_get_function_pointer (gpointer itf, int slot)
{
gpointer func;
func = *((*(gpointer**)itf)+slot);
return func;
}
/**
* cominterop_object_is_com_object:
* @obj: a pointer to the object
*
* Returns: a value indicating if the object is a
* Runtime Callable Wrapper (RCW) for a COM object
*/
static gboolean
cominterop_object_is_rcw (MonoObject *obj)
{
MonoClass *klass = NULL;
MonoRealProxy* real_proxy = NULL;
if (!obj)
return FALSE;
klass = mono_object_class (obj);
if (!mono_class_is_transparent_proxy (klass))
return FALSE;
real_proxy = ((MonoTransparentProxy*)obj)->rp;
if (!real_proxy)
return FALSE;
klass = mono_object_class (real_proxy);
return (klass && klass == mono_class_get_interop_proxy_class ());
}
static int
cominterop_get_com_slot_begin (MonoClass* klass)
{
static MonoClass *interface_type_attribute = NULL;
MonoCustomAttrInfo *cinfo = NULL;
MonoInterfaceTypeAttribute* itf_attr = NULL;
if (!interface_type_attribute)
interface_type_attribute = mono_class_from_name (mono_defaults.corlib, "System.Runtime.InteropServices", "InterfaceTypeAttribute");
cinfo = mono_custom_attrs_from_class (klass);
if (cinfo) {
itf_attr = (MonoInterfaceTypeAttribute*)mono_custom_attrs_get_attr (cinfo, interface_type_attribute);
if (!cinfo->cached)
mono_custom_attrs_free (cinfo);
}
if (itf_attr && itf_attr->intType == 1)
return 3; /* 3 methods in IUnknown*/
else
return 7; /* 7 methods in IDispatch*/
}
/**
* cominterop_get_method_interface:
* @method: method being called
*
* Returns: the MonoClass* representing the interface on which
* the method is defined.
*/
static MonoClass*
cominterop_get_method_interface (MonoMethod* method)
{
MonoError error;
MonoClass *ic = method->klass;
/* if method is on a class, we need to look up interface method exists on */
if (!MONO_CLASS_IS_INTERFACE(method->klass)) {
GPtrArray *ifaces = mono_class_get_implemented_interfaces (method->klass, &error);
g_assert (mono_error_ok (&error));
if (ifaces) {
int i;
mono_class_setup_vtable (method->klass);
for (i = 0; i < ifaces->len; ++i) {
int j, offset;
gboolean found = FALSE;
ic = g_ptr_array_index (ifaces, i);
offset = mono_class_interface_offset (method->klass, ic);
for (j = 0; j < ic->method.count; ++j) {
if (method->klass->vtable [j + offset] == method) {
found = TRUE;
break;
}
}
if (found)
break;
ic = NULL;
}
g_ptr_array_free (ifaces, TRUE);
}
}
if (!ic)
g_assert (ic);
g_assert (MONO_CLASS_IS_INTERFACE (ic));
return ic;
}
/**
* cominterop_get_com_slot_for_method:
* @method: a method
*
* Returns: the method's slot in the COM interface vtable
*/
static int
cominterop_get_com_slot_for_method (MonoMethod* method)
{
guint32 slot = method->slot;
MonoClass *ic = method->klass;
/* if method is on a class, we need to look up interface method exists on */
if (!MONO_CLASS_IS_INTERFACE(ic)) {
int offset = 0;
int i = 0;
ic = cominterop_get_method_interface (method);
offset = mono_class_interface_offset (method->klass, ic);
g_assert(offset >= 0);
for(i = 0; i < ic->method.count; ++i) {
if (method->klass->vtable [i + offset] == method)
{
slot = ic->methods[i]->slot;
break;
}
}
}
g_assert (ic);
g_assert (MONO_CLASS_IS_INTERFACE (ic));
return slot + cominterop_get_com_slot_begin (ic);
}
static void
cominterop_mono_string_to_guid (MonoString* string, guint8 *guid);
static gboolean
cominterop_class_guid (MonoClass* klass, guint8* guid)
{
static MonoClass *GuidAttribute = NULL;
MonoCustomAttrInfo *cinfo;
/* Handle the GuidAttribute */
if (!GuidAttribute)
GuidAttribute = mono_class_from_name (mono_defaults.corlib, "System.Runtime.InteropServices", "GuidAttribute");
cinfo = mono_custom_attrs_from_class (klass);
if (cinfo) {
MonoReflectionGuidAttribute *attr = (MonoReflectionGuidAttribute*)mono_custom_attrs_get_attr (cinfo, GuidAttribute);
if (!attr)
return FALSE;
if (!cinfo->cached)
mono_custom_attrs_free (cinfo);
cominterop_mono_string_to_guid (attr->guid, guid);
return TRUE;
}
return FALSE;
}
static gboolean
cominterop_com_visible (MonoClass* klass)
{
static MonoClass *ComVisibleAttribute = NULL;
MonoError error;
MonoCustomAttrInfo *cinfo;
GPtrArray *ifaces;
MonoBoolean visible = 1;
/* Handle the ComVisibleAttribute */
if (!ComVisibleAttribute)
ComVisibleAttribute = mono_class_from_name (mono_defaults.corlib, "System.Runtime.InteropServices", "ComVisibleAttribute");
cinfo = mono_custom_attrs_from_class (klass);
if (cinfo) {
MonoReflectionComVisibleAttribute *attr = (MonoReflectionComVisibleAttribute*)mono_custom_attrs_get_attr (cinfo, ComVisibleAttribute);
if (attr)
visible = attr->visible;
if (!cinfo->cached)
mono_custom_attrs_free (cinfo);
if (visible)
return TRUE;
}
ifaces = mono_class_get_implemented_interfaces (klass, &error);
g_assert (mono_error_ok (&error));
if (ifaces) {
int i;
for (i = 0; i < ifaces->len; ++i) {
MonoClass *ic = NULL;
ic = g_ptr_array_index (ifaces, i);
if (MONO_CLASS_IS_IMPORT (ic))
visible = TRUE;
}
g_ptr_array_free (ifaces, TRUE);
}
return visible;
}
static void cominterop_raise_hr_exception (int hr)
{
static MonoMethod* throw_exception_for_hr = NULL;
MonoException* ex;
void* params[1] = {&hr};
if (!throw_exception_for_hr)
throw_exception_for_hr = mono_class_get_method_from_name (mono_defaults.marshal_class, "GetExceptionForHR", 1);
ex = (MonoException*)mono_runtime_invoke (throw_exception_for_hr, NULL, params, NULL);
mono_raise_exception (ex);
}
/**
* cominterop_get_interface:
* @obj: managed wrapper object containing COM object
* @ic: interface type to retrieve for COM object
*
* Returns: the COM interface requested
*/
static gpointer
cominterop_get_interface (MonoComObject* obj, MonoClass* ic, gboolean throw_exception)
{
gpointer itf = NULL;
g_assert (ic);
g_assert (MONO_CLASS_IS_INTERFACE (ic));
mono_cominterop_lock ();
if (obj->itf_hash)
itf = g_hash_table_lookup (obj->itf_hash, GUINT_TO_POINTER ((guint)ic->interface_id));
mono_cominterop_unlock ();
if (!itf) {
guint8 iid [16];
int found = cominterop_class_guid (ic, iid);
int hr;
g_assert(found);
hr = ves_icall_System_Runtime_InteropServices_Marshal_QueryInterfaceInternal (obj->iunknown, iid, &itf);
if (hr < 0 && throw_exception) {
cominterop_raise_hr_exception (hr);
}
if (hr >= 0 && itf) {
mono_cominterop_lock ();
if (!obj->itf_hash)
obj->itf_hash = g_hash_table_new (mono_aligned_addr_hash, NULL);
g_hash_table_insert (obj->itf_hash, GUINT_TO_POINTER ((guint)ic->interface_id), itf);
mono_cominterop_unlock ();
}
}
if (throw_exception)
g_assert (itf);
return itf;
}
static int
cominterop_get_hresult_for_exception (MonoException* exc)
{
int hr = 0;
return hr;
}
static MonoReflectionType *
cominterop_type_from_handle (MonoType *handle)
{
MonoDomain *domain = mono_domain_get ();
MonoClass *klass = mono_class_from_mono_type (handle);
MONO_ARCH_SAVE_REGS;
mono_class_init (klass);
return mono_type_get_object (domain, handle);
}
void
mono_cominterop_init (void)
{
const char* com_provider_env;
InitializeCriticalSection (&cominterop_mutex);
com_provider_env = g_getenv ("MONO_COM");
if (com_provider_env && !strcmp(com_provider_env, "MS"))
com_provider = MONO_COM_MS;
register_icall (cominterop_get_method_interface, "cominterop_get_method_interface", "ptr ptr", FALSE);
register_icall (cominterop_get_function_pointer, "cominterop_get_function_pointer", "ptr ptr int32", FALSE);
register_icall (cominterop_object_is_rcw, "cominterop_object_is_rcw", "int32 object", FALSE);
register_icall (cominterop_get_ccw, "cominterop_get_ccw", "ptr object ptr", FALSE);
register_icall (cominterop_get_ccw_object, "cominterop_get_ccw_object", "object ptr int32", FALSE);
register_icall (cominterop_get_hresult_for_exception, "cominterop_get_hresult_for_exception", "int32 object", FALSE);
register_icall (cominterop_get_interface, "cominterop_get_interface", "ptr object ptr int32", FALSE);
register_icall (mono_string_to_bstr, "mono_string_to_bstr", "ptr obj", FALSE);
register_icall (mono_string_from_bstr, "mono_string_from_bstr", "obj ptr", FALSE);
register_icall (mono_free_bstr, "mono_free_bstr", "void ptr", FALSE);
register_icall (cominterop_type_from_handle, "cominterop_type_from_handle", "object ptr", FALSE);
/* SAFEARRAY marshalling */
register_icall (mono_marshal_safearray_begin, "mono_marshal_safearray_begin", "int32 ptr ptr ptr ptr ptr int32", FALSE);
register_icall (mono_marshal_safearray_get_value, "mono_marshal_safearray_get_value", "ptr ptr ptr", FALSE);
register_icall (mono_marshal_safearray_next, "mono_marshal_safearray_next", "int32 ptr ptr", FALSE);
register_icall (mono_marshal_safearray_end, "mono_marshal_safearray_end", "void ptr ptr", FALSE);
register_icall (mono_marshal_safearray_create, "mono_marshal_safearray_create", "int32 object ptr ptr ptr", FALSE);
register_icall (mono_marshal_safearray_set_value, "mono_marshal_safearray_set_value", "void ptr ptr ptr", FALSE);
register_icall (mono_marshal_safearray_free_indices, "mono_marshal_safearray_free_indices", "void ptr", FALSE);
}
void
mono_cominterop_cleanup (void)
{
DeleteCriticalSection (&cominterop_mutex);
}
void
mono_mb_emit_cominterop_call (MonoMethodBuilder *mb, MonoMethodSignature *sig, MonoMethod* method)
{
// get function pointer from 1st arg, the COM interface pointer
mono_mb_emit_ldarg (mb, 0);
mono_mb_emit_icon (mb, cominterop_get_com_slot_for_method (method));
mono_mb_emit_icall (mb, cominterop_get_function_pointer);
mono_mb_emit_byte (mb, MONO_CUSTOM_PREFIX);
mono_mb_emit_byte (mb, CEE_MONO_SAVE_LMF);
mono_mb_emit_calli (mb, sig);
mono_mb_emit_byte (mb, MONO_CUSTOM_PREFIX);
mono_mb_emit_byte (mb, CEE_MONO_RESTORE_LMF);
}
void
mono_cominterop_emit_ptr_to_object_conv (MonoMethodBuilder *mb, MonoType *type, MonoMarshalConv conv, MonoMarshalSpec *mspec)
{
switch (conv) {
case MONO_MARSHAL_CONV_OBJECT_INTERFACE:
case MONO_MARSHAL_CONV_OBJECT_IUNKNOWN:
case MONO_MARSHAL_CONV_OBJECT_IDISPATCH: {
static MonoClass* com_interop_proxy_class = NULL;
static MonoMethod* com_interop_proxy_get_proxy = NULL;
static MonoMethod* get_transparent_proxy = NULL;
int real_proxy;
guint32 pos_null = 0, pos_ccw = 0, pos_end = 0;
MonoClass *klass = NULL;
klass = mono_class_from_mono_type (type);
mono_mb_emit_ldloc (mb, 1);
mono_mb_emit_byte (mb, CEE_LDNULL);
mono_mb_emit_byte (mb, CEE_STIND_REF);
mono_mb_emit_ldloc (mb, 0);
mono_mb_emit_byte (mb, CEE_LDIND_I);
pos_null = mono_mb_emit_short_branch (mb, CEE_BRFALSE_S);
/* load dst to store later */
mono_mb_emit_ldloc (mb, 1);
mono_mb_emit_ldloc (mb, 0);
mono_mb_emit_byte (mb, CEE_LDIND_I);
mono_mb_emit_icon (mb, TRUE);
mono_mb_emit_icall (mb, cominterop_get_ccw_object);
pos_ccw = mono_mb_emit_short_branch (mb, CEE_BRTRUE_S);
if (!com_interop_proxy_class)
com_interop_proxy_class = mono_class_from_name (mono_defaults.corlib, "Mono.Interop", "ComInteropProxy");
if (!com_interop_proxy_get_proxy)
com_interop_proxy_get_proxy = mono_class_get_method_from_name_flags (com_interop_proxy_class, "GetProxy", 2, METHOD_ATTRIBUTE_PRIVATE);
#ifndef DISABLE_REMOTING
if (!get_transparent_proxy)
get_transparent_proxy = mono_class_get_method_from_name (mono_defaults.real_proxy_class, "GetTransparentProxy", 0);
#endif
real_proxy = mono_mb_add_local (mb, &com_interop_proxy_class->byval_arg);
mono_mb_emit_ldloc (mb, 0);
mono_mb_emit_byte (mb, CEE_LDIND_I);
mono_mb_emit_ptr (mb, &mono_class_get_com_object_class ()->byval_arg);
mono_mb_emit_icall (mb, cominterop_type_from_handle);
mono_mb_emit_managed_call (mb, com_interop_proxy_get_proxy, NULL);
mono_mb_emit_managed_call (mb, get_transparent_proxy, NULL);
if (conv == MONO_MARSHAL_CONV_OBJECT_INTERFACE) {
g_assert (klass);
mono_mb_emit_op (mb, CEE_CASTCLASS, klass);
}
mono_mb_emit_byte (mb, CEE_STIND_REF);
pos_end = mono_mb_emit_short_branch (mb, CEE_BR_S);
/* is already managed object */
mono_mb_patch_short_branch (mb, pos_ccw);
mono_mb_emit_ldloc (mb, 0);
mono_mb_emit_byte (mb, CEE_LDIND_I);
mono_mb_emit_icon (mb, TRUE);
mono_mb_emit_icall (mb, cominterop_get_ccw_object);
if (conv == MONO_MARSHAL_CONV_OBJECT_INTERFACE) {
g_assert (klass);
mono_mb_emit_op (mb, CEE_CASTCLASS, klass);
}
mono_mb_emit_byte (mb, CEE_STIND_REF);
mono_mb_patch_short_branch (mb, pos_end);
/* case if null */
mono_mb_patch_short_branch (mb, pos_null);
break;
}
default:
g_assert_not_reached ();
}
}
void
mono_cominterop_emit_object_to_ptr_conv (MonoMethodBuilder *mb, MonoType *type, MonoMarshalConv conv, MonoMarshalSpec *mspec)
{
switch (conv) {
case MONO_MARSHAL_CONV_OBJECT_INTERFACE:
case MONO_MARSHAL_CONV_OBJECT_IDISPATCH:
case MONO_MARSHAL_CONV_OBJECT_IUNKNOWN: {
guint32 pos_null = 0, pos_rcw = 0, pos_end = 0;
mono_mb_emit_ldloc (mb, 1);
mono_mb_emit_icon (mb, 0);
mono_mb_emit_byte (mb, CEE_CONV_U);
mono_mb_emit_byte (mb, CEE_STIND_I);
mono_mb_emit_ldloc (mb, 0);
mono_mb_emit_byte (mb, CEE_LDIND_REF);
// if null just break, dst was already inited to 0
pos_null = mono_mb_emit_short_branch (mb, CEE_BRFALSE_S);
mono_mb_emit_ldloc (mb, 0);
mono_mb_emit_byte (mb, CEE_LDIND_REF);
mono_mb_emit_icall (mb, cominterop_object_is_rcw);
pos_rcw = mono_mb_emit_short_branch (mb, CEE_BRFALSE_S);
// load dst to store later
mono_mb_emit_ldloc (mb, 1);
// load src
mono_mb_emit_ldloc (mb, 0);
mono_mb_emit_byte (mb, CEE_LDIND_REF);
mono_mb_emit_ldflda (mb, G_STRUCT_OFFSET (MonoTransparentProxy, rp));
mono_mb_emit_byte (mb, CEE_LDIND_REF);
/* load the RCW from the ComInteropProxy*/
mono_mb_emit_ldflda (mb, G_STRUCT_OFFSET (MonoComInteropProxy, com_object));
mono_mb_emit_byte (mb, CEE_LDIND_REF);
if (conv == MONO_MARSHAL_CONV_OBJECT_INTERFACE) {
mono_mb_emit_ptr (mb, mono_type_get_class (type));
mono_mb_emit_icon (mb, TRUE);
mono_mb_emit_icall (mb, cominterop_get_interface);
}
else if (conv == MONO_MARSHAL_CONV_OBJECT_IUNKNOWN) {
static MonoProperty* iunknown = NULL;
if (!iunknown)
iunknown = mono_class_get_property_from_name (mono_class_get_com_object_class (), "IUnknown");
mono_mb_emit_managed_call (mb, iunknown->get, NULL);
}
else if (conv == MONO_MARSHAL_CONV_OBJECT_IDISPATCH) {
static MonoProperty* idispatch = NULL;
if (!idispatch)
idispatch = mono_class_get_property_from_name (mono_class_get_com_object_class (), "IDispatch");
mono_mb_emit_managed_call (mb, idispatch->get, NULL);
}
else {
g_assert_not_reached ();
}
mono_mb_emit_byte (mb, CEE_STIND_I);
pos_end = mono_mb_emit_short_branch (mb, CEE_BR_S);
// if not rcw
mono_mb_patch_short_branch (mb, pos_rcw);
/* load dst to store later */
mono_mb_emit_ldloc (mb, 1);
/* load src */
mono_mb_emit_ldloc (mb, 0);
mono_mb_emit_byte (mb, CEE_LDIND_REF);
if (conv == MONO_MARSHAL_CONV_OBJECT_INTERFACE)
mono_mb_emit_ptr (mb, mono_type_get_class (type));
else if (conv == MONO_MARSHAL_CONV_OBJECT_IUNKNOWN)
mono_mb_emit_ptr (mb, mono_class_get_iunknown_class ());
else if (conv == MONO_MARSHAL_CONV_OBJECT_IDISPATCH)
mono_mb_emit_ptr (mb, mono_class_get_idispatch_class ());
else
g_assert_not_reached ();
mono_mb_emit_icall (mb, cominterop_get_ccw);
mono_mb_emit_byte (mb, CEE_STIND_I);
mono_mb_patch_short_branch (mb, pos_end);
mono_mb_patch_short_branch (mb, pos_null);
break;
}
default:
g_assert_not_reached ();
}
}
/**
* cominterop_get_native_wrapper_adjusted:
* @method: managed COM Interop method
*
* Returns: the generated method to call with signature matching
* the unmanaged COM Method signature
*/
static MonoMethod *
cominterop_get_native_wrapper_adjusted (MonoMethod *method)
{
MonoMethod *res;
MonoMethodBuilder *mb_native;
MonoMarshalSpec **mspecs;
MonoMethodSignature *sig, *sig_native;
MonoMethodPInvoke *piinfo = (MonoMethodPInvoke *) method;
int i;
sig = mono_method_signature (method);
// create unmanaged wrapper
mb_native = mono_mb_new (method->klass, method->name, MONO_WRAPPER_MANAGED_TO_NATIVE);
sig_native = cominterop_method_signature (method);
mspecs = g_new (MonoMarshalSpec*, sig_native->param_count+1);
memset (mspecs, 0, sizeof(MonoMarshalSpec*)*(sig_native->param_count+1));
mono_method_get_marshal_info (method, mspecs);
// move managed args up one
for (i = sig->param_count; i >= 1; i--)
mspecs[i+1] = mspecs[i];
// first arg is IntPtr for interface
mspecs[1] = NULL;
if (!(method->iflags & METHOD_IMPL_ATTRIBUTE_PRESERVE_SIG)) {
// move return spec to last param
if (!MONO_TYPE_IS_VOID (sig->ret))
mspecs[sig_native->param_count] = mspecs[0];
mspecs[0] = NULL;
}
for (i = 1; i < sig_native->param_count; i++) {
int mspec_index = i + 1;
if (mspecs[mspec_index] == NULL) {
// default object to VARIANT
if (sig_native->params[i]->type == MONO_TYPE_OBJECT) {
mspecs[mspec_index] = g_new0 (MonoMarshalSpec, 1);
mspecs[mspec_index]->native = MONO_NATIVE_STRUCT;
}
else if (sig_native->params[i]->type == MONO_TYPE_STRING) {
mspecs[mspec_index] = g_new0 (MonoMarshalSpec, 1);
mspecs[mspec_index]->native = MONO_NATIVE_BSTR;
}
else if (sig_native->params[i]->type == MONO_TYPE_CLASS) {
mspecs[mspec_index] = g_new0 (MonoMarshalSpec, 1);
mspecs[mspec_index]->native = MONO_NATIVE_INTERFACE;
}
else if (sig_native->params[i]->type == MONO_TYPE_BOOLEAN) {
mspecs[mspec_index] = g_new0 (MonoMarshalSpec, 1);
mspecs[mspec_index]->native = MONO_NATIVE_VARIANTBOOL;
}
}
}
if (method->iflags & METHOD_IMPL_ATTRIBUTE_PRESERVE_SIG) {
// move return spec to last param
if (!MONO_TYPE_IS_VOID (sig->ret) && mspecs[0] == NULL) {
// default object to VARIANT
if (sig->ret->type == MONO_TYPE_OBJECT) {
mspecs[0] = g_new0 (MonoMarshalSpec, 1);
mspecs[0]->native = MONO_NATIVE_STRUCT;
}
else if (sig->ret->type == MONO_TYPE_STRING) {
mspecs[0] = g_new0 (MonoMarshalSpec, 1);
mspecs[0]->native = MONO_NATIVE_BSTR;
}
else if (sig->ret->type == MONO_TYPE_CLASS) {
mspecs[0] = g_new0 (MonoMarshalSpec, 1);
mspecs[0]->native = MONO_NATIVE_INTERFACE;
}
else if (sig->ret->type == MONO_TYPE_BOOLEAN) {
mspecs[0] = g_new0 (MonoMarshalSpec, 1);
mspecs[0]->native = MONO_NATIVE_VARIANTBOOL;
}
}
}
mono_marshal_emit_native_wrapper (method->klass->image, mb_native, sig_native, piinfo, mspecs, piinfo->addr, FALSE, TRUE, FALSE);
res = mono_mb_create_method (mb_native, sig_native, sig_native->param_count + 16);
mono_mb_free (mb_native);
for (i = sig_native->param_count; i >= 0; i--)
if (mspecs [i])
mono_metadata_free_marshal_spec (mspecs [i]);
g_free (mspecs);
return res;
}
/**
* mono_cominterop_get_native_wrapper:
* @method: managed method
*
* Returns: the generated method to call
*/
MonoMethod *
mono_cominterop_get_native_wrapper (MonoMethod *method)
{
MonoMethod *res;
GHashTable *cache;
MonoMethodBuilder *mb;
MonoMethodSignature *sig, *csig;
g_assert (method);
cache = mono_marshal_get_cache (&method->klass->image->cominterop_wrapper_cache, mono_aligned_addr_hash, NULL);
if ((res = mono_marshal_find_in_cache (cache, method)))
return res;
if (!method->klass->vtable)
mono_class_setup_vtable (method->klass);
if (!method->klass->methods)
mono_class_setup_methods (method->klass);
g_assert (!method->klass->exception_type); /*FIXME do proper error handling*/
sig = mono_method_signature (method);
mb = mono_mb_new (method->klass, method->name, MONO_WRAPPER_COMINTEROP);
/* if method klass is import, that means method
* is really a com call. let interop system emit it.
*/
if (MONO_CLASS_IS_IMPORT(method->klass)) {
/* FIXME: we have to call actual class .ctor
* instead of just __ComObject .ctor.
*/
if (!strcmp(method->name, ".ctor")) {
static MonoMethod *ctor = NULL;
if (!ctor)
ctor = mono_class_get_method_from_name (mono_class_get_com_object_class (), ".ctor", 0);
mono_mb_emit_ldarg (mb, 0);
mono_mb_emit_managed_call (mb, ctor, NULL);
mono_mb_emit_byte (mb, CEE_RET);
}
else {
static MonoMethod * ThrowExceptionForHR = NULL;
MonoMethod *adjusted_method;
int retval = 0;
int ptr_this;
int i;
gboolean preserve_sig = method->iflags & METHOD_IMPL_ATTRIBUTE_PRESERVE_SIG;
// add local variables
ptr_this = mono_mb_add_local (mb, &mono_defaults.int_class->byval_arg);
if (!MONO_TYPE_IS_VOID (sig->ret))
retval = mono_mb_add_local (mb, sig->ret);
// get the type for the interface the method is defined on
// and then get the underlying COM interface for that type
mono_mb_emit_ldarg (mb, 0);
mono_mb_emit_ptr (mb, method);
mono_mb_emit_icall (mb, cominterop_get_method_interface);
mono_mb_emit_icon (mb, TRUE);
mono_mb_emit_icall (mb, cominterop_get_interface);
mono_mb_emit_stloc (mb, ptr_this);
// arg 1 is unmanaged this pointer
mono_mb_emit_ldloc (mb, ptr_this);
// load args
for (i = 1; i <= sig->param_count; i++)
mono_mb_emit_ldarg (mb, i);
// push managed return value as byref last argument
if (!MONO_TYPE_IS_VOID (sig->ret) && !preserve_sig)
mono_mb_emit_ldloc_addr (mb, retval);
adjusted_method = cominterop_get_native_wrapper_adjusted (method);
mono_mb_emit_managed_call (mb, adjusted_method, NULL);
if (!preserve_sig) {
if (!ThrowExceptionForHR)
ThrowExceptionForHR = mono_class_get_method_from_name (mono_defaults.marshal_class, "ThrowExceptionForHR", 1);
mono_mb_emit_managed_call (mb, ThrowExceptionForHR, NULL);
// load return value managed is expecting
if (!MONO_TYPE_IS_VOID (sig->ret))
mono_mb_emit_ldloc (mb, retval);
}
mono_mb_emit_byte (mb, CEE_RET);
}
}
/* Does this case ever get hit? */
else {
char *msg = g_strdup ("non imported interfaces on \
imported classes is not yet implemented.");
mono_mb_emit_exception (mb, "NotSupportedException", msg);
}
csig = mono_metadata_signature_dup_full (method->klass->image, sig);
csig->pinvoke = 0;
res = mono_mb_create_and_cache (cache, method,
mb, csig, csig->param_count + 16);
mono_mb_free (mb);
return res;
}
/**
* mono_cominterop_get_invoke:
* @method: managed method
*
* Returns: the generated method that calls the underlying __ComObject
* rather than the proxy object.
*/
MonoMethod *
mono_cominterop_get_invoke (MonoMethod *method)
{
MonoMethodSignature *sig;
MonoMethodBuilder *mb;
MonoMethod *res;
int i, temp_obj;
GHashTable* cache = mono_marshal_get_cache (&method->klass->image->cominterop_invoke_cache, mono_aligned_addr_hash, NULL);
g_assert (method);
if ((res = mono_marshal_find_in_cache (cache, method)))
return res;
sig = mono_signature_no_pinvoke (method);
/* we cant remote methods without this pointer */
if (!sig->hasthis)
return method;
mb = mono_mb_new (method->klass, method->name, MONO_WRAPPER_COMINTEROP_INVOKE);
/* get real proxy object, which is a ComInteropProxy in this case*/
temp_obj = mono_mb_add_local (mb, &mono_defaults.object_class->byval_arg);
mono_mb_emit_ldarg (mb, 0);
mono_mb_emit_ldflda (mb, G_STRUCT_OFFSET (MonoTransparentProxy, rp));
mono_mb_emit_byte (mb, CEE_LDIND_REF);