[2020-02] Fix leak in assembly-specific dllmap lookups (#21053)
[mono-project.git] / mono / metadata / handle.h
blob1e6dea997f8c73d9a3e7741049823ea681b41cd3
1 /**
2 * \file
3 * Handle to object in native code
5 * Authors:
6 * - Ludovic Henry <ludovic@xamarin.com>
7 * - Aleksey Klieger <aleksey.klieger@xamarin.com>
8 * - Rodrigo Kumpera <kumpera@xamarin.com>
10 * Copyright 2016 Dot net foundation.
11 * Licensed under the MIT license. See LICENSE file in the project root for full license information.
14 #ifndef __MONO_HANDLE_H__
15 #define __MONO_HANDLE_H__
17 #include <config.h>
18 #include <glib.h>
20 #include <mono/metadata/handle-decl.h>
21 #include <mono/metadata/object.h>
22 #include <mono/metadata/class.h>
23 #include <mono/utils/mono-error-internals.h>
24 #include <mono/utils/mono-threads.h>
25 #include <mono/utils/checked-build.h>
26 #include <mono/metadata/class-internals.h>
29 Handle stack.
31 The handle stack is designed so it's efficient to pop a large amount of entries at once.
32 The stack is made out of a series of fixed size segments.
34 To do bulk operations you use a stack mark.
39 3 is the number of fields besides the data in the struct;
40 128 words makes each chunk 512 or 1024 bytes each
42 #define OBJECTS_PER_HANDLES_CHUNK (128 - 3)
45 Whether this config needs stack watermark recording to know where to start scanning from.
47 #ifdef HOST_WATCHOS
48 #define MONO_NEEDS_STACK_WATERMARK 1
49 #endif
51 typedef struct _HandleChunk HandleChunk;
54 * Define MONO_HANDLE_TRACK_OWNER to store the file and line number of each call to MONO_HANDLE_NEW
55 * in the handle stack. (This doubles the amount of memory used for handles, so it's only useful for debugging).
57 /*#define MONO_HANDLE_TRACK_OWNER*/
60 * Define MONO_HANDLE_TRACK_SP to record the C stack pointer at the time of each HANDLE_FUNCTION_ENTER and
61 * to ensure that when a new handle is allocated the previous newest handle is not lower in the stack.
62 * This is useful to catch missing HANDLE_FUNCTION_ENTER / HANDLE_FUNCTION_RETURN pairs which could cause
63 * handle leaks.
65 /*#define MONO_HANDLE_TRACK_SP*/
67 typedef struct {
68 gpointer o; /* MonoObject ptr */
69 #ifdef MONO_HANDLE_TRACK_OWNER
70 const char *owner;
71 gpointer backtrace_ips[7]; /* result of backtrace () at time of allocation */
72 #endif
73 #ifdef MONO_HANDLE_TRACK_SP
74 gpointer alloc_sp; /* sp from HandleStack:stackmark_sp at time of allocation */
75 #endif
76 } HandleChunkElem;
78 struct _HandleChunk {
79 int size; //number of handles
80 HandleChunk *prev, *next;
81 HandleChunkElem elems [OBJECTS_PER_HANDLES_CHUNK];
84 typedef struct MonoHandleStack {
85 HandleChunk *top; //alloc from here
86 HandleChunk *bottom; //scan from here
87 #ifdef MONO_HANDLE_TRACK_SP
88 gpointer stackmark_sp; // C stack pointer top when from most recent mono_stack_mark_init
89 #endif
90 } HandleStack;
92 // Keep this in sync with RuntimeStructs.cs
93 typedef struct {
94 int size;
95 HandleChunk *chunk;
96 #ifdef MONO_HANDLE_TRACK_SP
97 gpointer prev_sp; // C stack pointer from prior mono_stack_mark_init
98 #endif
99 } HandleStackMark;
101 // There are two types of handles.
102 // Pointers to volatile pointers in managed frames.
103 // These are allocated by icall wrappers in marshal-ilgen.c.
104 // Pointers to non-volatile pointers in TLS.
105 // These are allocated by MONO_HANDLE_NEW.
106 typedef void volatile * MonoRawHandle;
108 typedef void (*GcScanFunc) (gpointer*, gpointer);
111 /* If Centrinel is analyzing Mono, use the SUPPRESS macros to mark the bodies
112 * of the handle macros as allowed to perform operations on raw pointers to
113 * managed objects. Take care to UNSUPPRESS the _arguments_ to the macros - we
114 * want warnings if the argument uses pointers unsafely.
116 #ifdef __CENTRINEL__
117 #define MONO_HANDLE_SUPPRESS_SCOPE(b) __CENTRINEL_SUPPRESS_SCOPE(b)
118 #define MONO_HANDLE_SUPPRESS(expr) __CENTRINEL_SUPPRESS(expr)
119 #define MONO_HANDLE_UNSUPPRESS(expr) __CENTRINEL_UNSUPPRESS(expr)
120 #else
121 #define MONO_HANDLE_SUPPRESS_SCOPE(b) ;
122 #define MONO_HANDLE_SUPPRESS(expr) (expr)
123 #define MONO_HANDLE_UNSUPPRESS(expr) (expr)
124 #endif
126 #ifndef MONO_HANDLE_TRACK_OWNER
127 MonoRawHandle mono_handle_new (MonoObject *object, MonoThreadInfo *info);
128 #else
129 MonoRawHandle mono_handle_new (MonoObject *object, MonoThreadInfo *info, const char* owner);
130 #endif
132 void mono_handle_stack_scan (HandleStack *stack, GcScanFunc func, gpointer gc_data, gboolean precise, gboolean check);
133 gboolean mono_handle_stack_is_empty (HandleStack *stack);
134 HandleStack* mono_handle_stack_alloc (void);
135 void mono_handle_stack_free (HandleStack *handlestack);
136 MonoRawHandle mono_stack_mark_pop_value (MonoThreadInfo *info, HandleStackMark *stackmark, MonoRawHandle value);
137 MonoThreadInfo* mono_stack_mark_record_size (MonoThreadInfo *info, HandleStackMark *stackmark, const char *func_name);
138 void mono_handle_stack_free_domain (HandleStack *stack, MonoDomain *domain);
140 #ifdef MONO_HANDLE_TRACK_SP
141 void mono_handle_chunk_leak_check (HandleStack *handles);
142 #endif
144 static inline void
145 mono_stack_mark_init (MonoThreadInfo *info, HandleStackMark *stackmark)
147 #ifdef MONO_HANDLE_TRACK_SP
148 gpointer sptop = &stackmark;
149 #endif
150 HandleStack *handles = info->handle_stack;
151 stackmark->size = handles->top->size;
152 stackmark->chunk = handles->top;
153 #ifdef MONO_HANDLE_TRACK_SP
154 stackmark->prev_sp = handles->stackmark_sp;
155 handles->stackmark_sp = sptop;
156 #endif
159 static inline void
160 mono_stack_mark_pop (MonoThreadInfo *info, HandleStackMark *stackmark)
162 HandleStack *handles = info->handle_stack;
163 HandleChunk *old_top = stackmark->chunk;
164 old_top->size = stackmark->size;
165 mono_memory_write_barrier ();
166 handles->top = old_top;
167 #ifdef MONO_HANDLE_TRACK_SP
168 mono_memory_write_barrier (); /* write to top before prev_sp */
169 handles->stackmark_sp = stackmark->prev_sp;
170 #endif
173 // There are deliberately locals and a constant NULL global with this same name.
174 extern MonoThreadInfo * const mono_thread_info_current_var;
177 Icall macros
179 #define SETUP_ICALL_COMMON \
180 do { \
181 MONO_DISABLE_WARNING(4459) /* declaration of 'identifier' hides global declaration */ \
182 ERROR_DECL (error); \
183 /* There are deliberately locals and a constant NULL global with this same name. */ \
184 MonoThreadInfo *mono_thread_info_current_var = mono_thread_info_current (); \
185 MONO_RESTORE_WARNING \
187 #define CLEAR_ICALL_COMMON \
188 mono_error_set_pending_exception (error);
190 // FIXME There should be fast and slow versions of this, i.e. with and without local variable.
191 #define SETUP_ICALL_FRAME \
192 HandleStackMark __mark; \
193 mono_stack_mark_init (mono_thread_info_current_var ? mono_thread_info_current_var : mono_thread_info_current (), &__mark);
195 // FIXME This should be one function call since it is not fully inlined.
196 #define CLEAR_ICALL_FRAME \
197 mono_stack_mark_pop (mono_stack_mark_record_size (mono_thread_info_current_var, &__mark, __FUNCTION__), &__mark);
199 // FIXME This should be one function call since it is not fully inlined.
200 #define CLEAR_ICALL_FRAME_VALUE(RESULT, HANDLE) \
201 (RESULT) = g_cast (mono_stack_mark_pop_value (mono_stack_mark_record_size (mono_thread_info_current_var, &__mark, __FUNCTION__), &__mark, (HANDLE)));
203 #define HANDLE_FUNCTION_ENTER() do { \
204 MONO_DISABLE_WARNING(4459) /* declaration of 'identifier' hides global declaration */ \
205 /* There are deliberately locals and a constant NULL global with this same name. */ \
206 MonoThreadInfo *mono_thread_info_current_var = mono_thread_info_current (); \
207 MONO_RESTORE_WARNING \
208 SETUP_ICALL_FRAME \
210 #define HANDLE_FUNCTION_RETURN() \
211 CLEAR_ICALL_FRAME; \
212 } while (0)
214 // Do not do this often, but icall state can be manually managed.
216 // SETUP_ICALL_FUNCTION
217 // loop { // Does not have to be a loop.
218 // SETUP_ICALL_FRAME
219 // ..
220 // CLEAR_ICALL_FRAME
221 // }
223 // As with HANDLE_FUNCTION_RETURN, you must not
224 // skip CLEAR_ICALL_FRAME -- no break, continue, return, or goto (goto label at CLEAR_ICALL_FRAME is idiom).
226 #define SETUP_ICALL_FUNCTION \
227 MONO_DISABLE_WARNING(4459) /* declaration of 'identifier' hides global declaration */ \
228 /* There are deliberately locals and a constant NULL global with this same name. */ \
229 MonoThreadInfo *mono_thread_info_current_var = mono_thread_info_current () \
230 MONO_RESTORE_WARNING
232 // A common use of manual icall frame management is for loop.
233 // It can also be used for conditionality, where only some paths
234 // through a function allocate handles and frame teardown does
235 // coincide with function return. For example: emit_invoke_call.
237 #define HANDLE_LOOP_PREPARE SETUP_ICALL_FUNCTION
239 // Return a non-pointer or non-managed pointer, e.g. gboolean.
240 // VAL should be a local variable or at least not use handles in the current frame.
241 // i.e. it is "val", not "expr".
242 #define HANDLE_FUNCTION_RETURN_VAL(VAL) \
243 CLEAR_ICALL_FRAME; \
244 return (VAL); \
245 } while (0)
247 // Return a raw pointer from coop handle.
248 #define HANDLE_FUNCTION_RETURN_OBJ(HANDLE) \
249 do { \
250 void* __result = MONO_HANDLE_RAW (HANDLE); \
251 CLEAR_ICALL_FRAME; \
252 return g_cast (__result); \
253 } while (0); } while (0);
255 // Return a coop handle from coop handle.
256 #define HANDLE_FUNCTION_RETURN_REF(TYPE, HANDLE) \
257 do { \
258 MonoObjectHandle __result; \
259 CLEAR_ICALL_FRAME_VALUE (__result.__raw, (HANDLE).__raw); \
260 return MONO_HANDLE_CAST (TYPE, __result); \
261 } while (0); } while (0);
263 #ifdef MONO_NEEDS_STACK_WATERMARK
265 static void
266 mono_thread_info_pop_stack_mark (MonoThreadInfo *info, void *old_mark)
268 info->stack_mark = old_mark;
271 static void*
272 mono_thread_info_push_stack_mark (MonoThreadInfo *info, void *mark)
274 void *old = info->stack_mark;
275 info->stack_mark = mark;
276 return old;
279 #define SETUP_STACK_WATERMARK \
280 int __dummy; \
281 __builtin_unwind_init (); \
282 void *__old_stack_mark = mono_thread_info_push_stack_mark (mono_thread_info_current_var, &__dummy);
284 #define CLEAR_STACK_WATERMARK \
285 mono_thread_info_pop_stack_mark (mono_thread_info_current_var, __old_stack_mark);
287 #else
288 #define SETUP_STACK_WATERMARK
289 #define CLEAR_STACK_WATERMARK
290 #endif
292 #define ICALL_ENTRY() \
293 SETUP_ICALL_COMMON \
294 SETUP_ICALL_FRAME \
295 SETUP_STACK_WATERMARK
297 #define ICALL_RETURN() \
298 do { \
299 CLEAR_STACK_WATERMARK \
300 CLEAR_ICALL_COMMON \
301 CLEAR_ICALL_FRAME \
302 return; \
303 } while (0); } while (0)
305 #define ICALL_RETURN_VAL(VAL) \
306 do { \
307 CLEAR_STACK_WATERMARK \
308 CLEAR_ICALL_COMMON \
309 CLEAR_ICALL_FRAME \
310 return VAL; \
311 } while (0); } while (0)
313 #define ICALL_RETURN_OBJ(HANDLE) \
314 do { \
315 CLEAR_STACK_WATERMARK \
316 CLEAR_ICALL_COMMON \
317 void* __ret = MONO_HANDLE_RAW (HANDLE); \
318 CLEAR_ICALL_FRAME \
319 return g_cast (__ret); \
320 } while (0); } while (0)
323 Handle macros/functions
325 #ifdef MONO_HANDLE_TRACK_OWNER
326 #define STRINGIFY_(x) #x
327 #define STRINGIFY(x) STRINGIFY_(x)
328 #define HANDLE_OWNER (__FILE__ ":" STRINGIFY (__LINE__))
329 #endif
331 //XXX add functions to get/set raw, set field, set field to null, set array, set array to null
332 #define MONO_HANDLE_DCL(TYPE, NAME) TYPED_HANDLE_NAME(TYPE) NAME = MONO_HANDLE_NEW (TYPE, (NAME ## _raw))
334 // With Visual C++ compiling as C, the type of a ternary expression
335 // yielding two unrelated non-void pointers is the type of the first, plus a warning.
336 // This can be used to simulate gcc typeof extension.
337 // Otherwise we are forced to evaluate twice, or use C++.
338 #ifdef _MSC_VER
339 typedef struct _MonoTypeofCastHelper *MonoTypeofCastHelper; // a pointer type unrelated to anything else
340 #define MONO_TYPEOF_CAST(typeexpr, expr) __pragma(warning(suppress:4133))(0 ? (typeexpr) : (MonoTypeofCastHelper)(expr))
341 #else
342 #define MONO_TYPEOF_CAST(typeexpr, expr) ((__typeof__ (typeexpr))(expr))
343 #endif
346 * Create handle for the object OBJECT.
347 * The handle will keep the object alive and pinned.
349 #ifndef MONO_HANDLE_TRACK_OWNER
351 #define MONO_HANDLE_NEW(type, object) \
352 (MONO_HANDLE_CAST_FOR (type) (mono_handle_new (MONO_HANDLE_TYPECHECK_FOR (type) (object), mono_thread_info_current_var)))
354 #else
356 #define MONO_HANDLE_NEW(type, object) \
357 (MONO_HANDLE_CAST_FOR (type) (mono_handle_new (MONO_HANDLE_TYPECHECK_FOR (type) (object), mono_thread_info_current_var, HANDLE_OWNER)))
359 #endif
361 #define MONO_HANDLE_CAST(type, value) (MONO_HANDLE_CAST_FOR (type) ((value).__raw))
364 * Return the raw object reference stored in the handle.
365 * The objref is valid while the handle is alive and
366 * points to it.
368 #ifdef __cplusplus
369 #define MONO_HANDLE_RAW(handle) ((handle).GetRaw())
370 #else
371 #define MONO_HANDLE_RAW(handle) (MONO_TYPEOF_CAST (*(handle).__raw, mono_handle_raw ((handle).__raw)))
372 #endif
373 #define MONO_HANDLE_IS_NULL(handle) (mono_handle_is_null ((handle).__raw))
375 #define MONO_BOOL(x) (!!MONO_HANDLE_SUPPRESS (x))
376 #define MONO_HANDLE_BOOL(handle) (MONO_BOOL (!MONO_HANDLE_IS_NULL (handle)))
379 WARNING WARNING WARNING
381 The following functions require a particular evaluation ordering to ensure correctness.
382 We must not have exposed handles while any sort of evaluation is happening as that very evaluation might trigger
383 a safepoint and break us.
385 This is why we evaluate index and value before any call to MONO_HANDLE_RAW or other functions that deal with naked objects.
387 #define MONO_HANDLE_SETRAW(HANDLE, FIELD, VALUE) do { \
388 MONO_HANDLE_SUPPRESS_SCOPE(1); \
389 MonoObject *__val = MONO_HANDLE_SUPPRESS ((MonoObject*)(MONO_HANDLE_UNSUPPRESS (VALUE))); \
390 MONO_OBJECT_SETREF_INTERNAL (MONO_HANDLE_RAW (MONO_HANDLE_UNSUPPRESS (HANDLE)), FIELD, __val); \
391 } while (0)
393 // handle->field = value for managed pointer
394 #define MONO_HANDLE_SET(HANDLE, FIELD, VALUE) do { \
395 MonoObjectHandle __val = MONO_HANDLE_CAST (MonoObject, VALUE); \
396 do { \
397 MONO_HANDLE_SUPPRESS_SCOPE(1); \
398 MONO_OBJECT_SETREF_INTERNAL (MONO_HANDLE_RAW (MONO_HANDLE_UNSUPPRESS (HANDLE)), FIELD, MONO_HANDLE_RAW (__val)); \
399 } while (0); \
400 } while (0)
402 // resultHandle = handle->field
403 /* N.B. RESULT is evaluated before HANDLE */
404 #define MONO_HANDLE_GET(RESULT, HANDLE, FIELD) do { \
405 MonoObjectHandle __dest = MONO_HANDLE_CAST (MonoObject, RESULT); \
406 MONO_HANDLE_SUPPRESS (*(gpointer*)__dest.__raw = (gpointer)MONO_HANDLE_RAW (MONO_HANDLE_UNSUPPRESS (HANDLE))->FIELD); \
407 } while (0)
409 // Get handle->field as a type-handle.
410 #define MONO_HANDLE_NEW_GET(TYPE,HANDLE,FIELD) (MONO_HANDLE_NEW(TYPE,MONO_HANDLE_SUPPRESS (MONO_HANDLE_RAW (MONO_HANDLE_UNSUPPRESS (HANDLE))->FIELD)))
412 // Get handle->field, where field is not a pointer (an integer or non-managed pointer).
413 #define MONO_HANDLE_GETVAL(HANDLE, FIELD) MONO_HANDLE_SUPPRESS (MONO_HANDLE_RAW (MONO_HANDLE_UNSUPPRESS (HANDLE))->FIELD)
415 // Get handle->field as a boolean, i.e. typically compare managed pointer to NULL,
416 // though any type is ok.
417 #define MONO_HANDLE_GET_BOOL(handle, field) (MONO_BOOL (MONO_HANDLE_GETVAL (handle, field)))
419 // handle->field = (type)value, for non-managed pointers
420 // This would be easier to write with the gcc extension typeof,
421 // but it is not widely enough implemented (i.e. Microsoft C).
422 // The value copy is needed in cases computing value causes a GC
423 #define MONO_HANDLE_SETVAL(HANDLE, FIELD, TYPE, VALUE) do { \
424 TYPE __val = (VALUE); \
425 if (0) { TYPE * typecheck G_GNUC_UNUSED = &MONO_HANDLE_SUPPRESS (MONO_HANDLE_RAW (HANDLE)->FIELD); } \
426 MONO_HANDLE_SUPPRESS (MONO_HANDLE_RAW (MONO_HANDLE_UNSUPPRESS (HANDLE))->FIELD = __val); \
427 } while (0)
429 // handle [idx] = value (for managed pointers)
430 #define MONO_HANDLE_ARRAY_SETREF(HANDLE, IDX, VALUE) do { \
431 uintptr_t __idx = (IDX); \
432 MonoObjectHandle __val = MONO_HANDLE_CAST (MonoObject, VALUE); \
433 { /* FIXME scope needed by Centrinel */ \
434 /* FIXME mono_array_setref_fast is not an expression. */ \
435 MONO_HANDLE_SUPPRESS_SCOPE(1); \
436 mono_array_setref_fast (MONO_HANDLE_RAW (MONO_HANDLE_UNSUPPRESS (HANDLE)), __idx, MONO_HANDLE_RAW (__val)); \
438 } while (0)
440 // handle [idx] = (type)value (for non-managed types)
441 #define MONO_HANDLE_ARRAY_SETVAL(HANDLE, TYPE, IDX, VALUE) do { \
442 uintptr_t __idx = (IDX); \
443 TYPE __val = (VALUE); \
444 { /* FIXME scope needed by Centrinel */ \
445 /* FIXME mono_array_set is not an expression. */ \
446 MONO_HANDLE_SUPPRESS_SCOPE(1); \
447 mono_array_set_internal (MONO_HANDLE_RAW (MONO_HANDLE_UNSUPPRESS (HANDLE)), TYPE, __idx, __val); \
449 } while (0)
451 #if 0 // This is never used.
452 // handle [idx] = value
453 #define MONO_HANDLE_ARRAY_SETRAW(HANDLE, IDX, VALUE) do { \
454 MONO_HANDLE_SUPPRESS_SCOPE(1); \
455 uintptr_t __idx = MONO_HANDLE_UNSUPPRESS(IDX); \
456 MonoObject *__val = (MonoObject*)(VALUE); \
457 mono_array_setref_fast (MONO_HANDLE_RAW (MONO_HANDLE_UNSUPPRESS (HANDLE)), __idx, __val); \
458 } while (0)
459 #endif
461 /* N.B. DEST is evaluated AFTER all the other arguments */
462 #define MONO_HANDLE_ARRAY_GETVAL(DEST, HANDLE, TYPE, IDX) do { \
463 MonoArrayHandle __arr = (HANDLE); \
464 uintptr_t __idx = (IDX); \
465 TYPE __result = MONO_HANDLE_SUPPRESS (mono_array_get_internal (MONO_HANDLE_RAW(__arr), TYPE, __idx)); \
466 (DEST) = __result; \
467 } while (0)
469 // dest = handle [idx] (for managed pointers)
470 #define MONO_HANDLE_ARRAY_GETREF(DEST, HANDLE, IDX) do { \
471 mono_handle_array_getref (MONO_HANDLE_CAST(MonoObject, (DEST)), (HANDLE), (IDX)); \
472 } while (0)
474 #define MONO_HANDLE_ASSIGN_RAW(DESTH, SRCP) (mono_handle_assign_raw (MONO_HANDLE_CAST (MonoObject, (DESTH)), (SRCP)))
475 #define MONO_HANDLE_ASSIGN(DESTH, SRCH) (MONO_HANDLE_ASSIGN_RAW ((DESTH), MONO_HANDLE_RAW (SRCH)))
477 #define MONO_HANDLE_DOMAIN(HANDLE) MONO_HANDLE_SUPPRESS (mono_object_domain (MONO_HANDLE_RAW (MONO_HANDLE_CAST (MonoObject, MONO_HANDLE_UNSUPPRESS (HANDLE)))))
479 #define mono_handle_domain(handle) MONO_HANDLE_DOMAIN ((handle))
481 /* Given an object and a MonoClassField, return the value (must be non-object)
482 * of the field. It's the caller's responsibility to check that the object is
483 * of the correct class. */
484 #define MONO_HANDLE_GET_FIELD_VAL(HANDLE,TYPE,FIELD) (*(TYPE *)(mono_handle_unsafe_field_addr (MONO_HANDLE_CAST (MonoObject, (HANDLE)), (FIELD))))
485 #define MONO_HANDLE_GET_FIELD_BOOL(handle, type, field) (MONO_BOOL (MONO_HANDLE_GET_FIELD_VAL ((handle), type, (field))))
487 #define MONO_HANDLE_NEW_GET_FIELD(HANDLE,TYPE,FIELD) MONO_HANDLE_NEW (TYPE, MONO_HANDLE_SUPPRESS (*(TYPE**)(mono_handle_unsafe_field_addr (MONO_HANDLE_CAST (MonoObject, MONO_HANDLE_UNSUPPRESS (HANDLE)), (FIELD)))))
489 #define MONO_HANDLE_SET_FIELD_VAL(HANDLE,TYPE,FIELD,VAL) do { \
490 MonoObjectHandle __obj = (HANDLE); \
491 MonoClassField *__field = (FIELD); \
492 TYPE __value = (VAL); \
493 *(TYPE*)(mono_handle_unsafe_field_addr (__obj, __field)) = __value; \
494 } while (0)
496 #define MONO_HANDLE_SET_FIELD_REF(HANDLE,FIELD,VALH) do { \
497 MonoObjectHandle __obj = MONO_HANDLE_CAST (MonoObject, (HANDLE)); \
498 MonoClassField *__field = (FIELD); \
499 MonoObjectHandle __value = MONO_HANDLE_CAST (MonoObject, (VALH)); \
500 MONO_HANDLE_SUPPRESS (mono_gc_wbarrier_generic_store_internal (mono_handle_unsafe_field_addr (__obj, __field), MONO_HANDLE_RAW (__value))); \
501 } while (0)
503 #define MONO_HANDLE_GET_CLASS(handle) (MONO_HANDLE_GETVAL (MONO_HANDLE_CAST (MonoObject, (handle)), vtable)->klass)
505 /* Baked typed handles we all want */
506 TYPED_HANDLE_DECL (MonoString);
507 TYPED_HANDLE_DECL (MonoArray);
508 TYPED_HANDLE_DECL (MonoObject);
509 TYPED_HANDLE_DECL (MonoException);
510 TYPED_HANDLE_DECL (MonoAppContext);
512 // Structs cannot be cast to structs.
513 // As well, a function is needed because an anonymous struct cannot be initialized in C.
514 static inline MonoObjectHandle
515 mono_handle_cast (gpointer a)
517 return *(MonoObjectHandle*)&a;
520 static inline MONO_ALWAYS_INLINE gboolean
521 mono_handle_is_null (MonoRawHandle raw_handle)
523 MONO_HANDLE_SUPPRESS_SCOPE (1);
524 MonoObjectHandle *handle = (MonoObjectHandle*)&raw_handle;
525 return !handle->__raw || !*handle->__raw;
528 static inline MONO_ALWAYS_INLINE gpointer
529 mono_handle_raw (MonoRawHandle raw_handle)
531 MONO_HANDLE_SUPPRESS_SCOPE (1);
532 MonoObjectHandle *handle = (MonoObjectHandle*)&raw_handle;
533 return handle->__raw ? *handle->__raw : NULL;
536 /* Unfortunately MonoThreadHandle is already a typedef used for something unrelated. So
537 * the coop handle for MonoThread* is MonoThreadObjectHandle.
539 typedef MonoThread MonoThreadObject;
540 TYPED_HANDLE_DECL (MonoThreadObject);
543 This is the constant for a handle that points nowhere.
544 Constant handles may be initialized to it, but non-constant
545 handles must be NEW'ed. Uses of these are suspicious and should
546 be reviewed and probably changed FIXME.
548 #define NULL_HANDLE (mono_null_value_handle ())
549 #define NULL_HANDLE_INIT { 0 }
550 static inline MonoObjectHandle
551 mono_null_value_handle (void)
553 MonoObjectHandle result = NULL_HANDLE_INIT;
554 return result;
556 #define NULL_HANDLE_STRING (MONO_HANDLE_CAST (MonoString, NULL_HANDLE))
557 #define NULL_HANDLE_ARRAY (MONO_HANDLE_CAST (MonoArray, NULL_HANDLE))
558 #define NULL_HANDLE_STRING_BUILDER (MONO_HANDLE_CAST (MonoStringBuilder, NULL_HANDLE))
560 #if __cplusplus
562 // Use this to convert a THandle to a raw T** such as for a ref or out parameter, without
563 // copying back and forth through an intermediate. The handle must already be allocated,
564 // such as icall marshaling does for out and ref parameters.
565 #define MONO_HANDLE_REF(h) (h.Ref ())
567 #else
569 static inline void volatile*
570 mono_handle_ref (void volatile* p)
572 g_assert (p);
573 return p;
576 // Use this to convert a THandle to a raw T** such as for a ref or out parameter, without
577 // copying back and forth through an intermediate. The handle must already be allocated,
578 // such as icall marshaling does for out and ref parameters.
579 #define MONO_HANDLE_REF(handle) (MONO_TYPEOF_CAST ((handle).__raw, mono_handle_ref ((handle).__raw)))
581 #endif
583 static inline MonoObjectHandle
584 mono_handle_assign_raw (MonoObjectHandleOut dest, void *src)
586 g_assert (dest.__raw);
587 MONO_HANDLE_SUPPRESS (*dest.__raw = (MonoObject*)src);
588 return dest;
591 /* It is unsafe to call this function directly - it does not pin the handle! Use MONO_HANDLE_GET_FIELD_VAL(). */
592 static inline gpointer
593 mono_handle_unsafe_field_addr (MonoObjectHandle h, MonoClassField *field)
595 return MONO_HANDLE_SUPPRESS (((gchar *)MONO_HANDLE_RAW (h)) + field->offset);
598 //FIXME this should go somewhere else
599 MonoStringHandle mono_string_new_handle (MonoDomain *domain, const char *data, MonoError *error);
600 MonoArrayHandle mono_array_new_handle (MonoDomain *domain, MonoClass *eclass, uintptr_t n, MonoError *error);
601 MonoArrayHandle
602 mono_array_new_full_handle (MonoDomain *domain, MonoClass *array_class, uintptr_t *lengths, intptr_t *lower_bounds, MonoError *error);
604 #define mono_array_handle_setref(array,index,value) MONO_HANDLE_ARRAY_SETREF ((array), (index), (value))
606 void
607 mono_handle_array_getref (MonoObjectHandleOut dest, MonoArrayHandle array, uintptr_t index);
609 #define mono_handle_class(o) MONO_HANDLE_SUPPRESS (mono_object_class (MONO_HANDLE_RAW (MONO_HANDLE_UNSUPPRESS (o))))
611 #define mono_handle_vtable(o) MONO_HANDLE_GETVAL (o, vtable)
613 /* Local handles to global GC handles and back */
615 uint32_t
616 mono_gchandle_from_handle (MonoObjectHandle handle, mono_bool pinned);
618 MonoObjectHandle
619 mono_gchandle_get_target_handle (uint32_t gchandle);
621 gboolean
622 mono_gchandle_target_equal (uint32_t gchandle, MonoObjectHandle equal);
624 void
625 mono_gchandle_target_is_null_or_equal (uint32_t gchandle, MonoObjectHandle equal, gboolean *is_null,
626 gboolean *is_equal);
628 void
629 mono_gchandle_set_target_handle (guint32 gchandle, MonoObjectHandle obj);
631 void
632 mono_array_handle_memcpy_refs (MonoArrayHandle dest, uintptr_t dest_idx, MonoArrayHandle src, uintptr_t src_idx, uintptr_t len);
634 /* Pins the MonoArray using a gchandle and returns a pointer to the
635 * element with the given index (where each element is of the given
636 * size. Call mono_gchandle_free to unpin.
638 gpointer
639 mono_array_handle_pin_with_size (MonoArrayHandle handle, int size, uintptr_t index, uint32_t *gchandle);
641 #define MONO_ARRAY_HANDLE_PIN(handle,type,index,gchandle_out) ((type*)mono_array_handle_pin_with_size (MONO_HANDLE_CAST(MonoArray,(handle)), sizeof (type), (index), (gchandle_out)))
643 void
644 mono_value_copy_array_handle (MonoArrayHandle dest, int dest_idx, gconstpointer src, int count);
646 gunichar2 *
647 mono_string_handle_pin_chars (MonoStringHandle s, uint32_t *gchandle_out);
649 gpointer
650 mono_object_handle_pin_unbox (MonoObjectHandle boxed_valuetype_obj, uint32_t *gchandle_out);
652 static inline gpointer
653 mono_handle_unbox_unsafe (MonoObjectHandle handle)
655 g_assert (m_class_is_valuetype (MONO_HANDLE_GETVAL (handle, vtable)->klass));
656 return MONO_HANDLE_SUPPRESS (MONO_HANDLE_RAW (handle) + 1);
659 void
660 mono_error_set_exception_handle (MonoError *error, MonoExceptionHandle exc);
662 MonoAppContextHandle
663 mono_context_get_handle (void);
665 void
666 mono_context_set_handle (MonoAppContextHandle new_context);
668 guint32
669 mono_gchandle_new_weakref_from_handle (MonoObjectHandle handle);
672 mono_handle_hash (MonoObjectHandle object);
674 guint32
675 mono_gchandle_new_weakref_from_handle_track_resurrection (MonoObjectHandle handle);
677 #endif /* __MONO_HANDLE_H__ */