1 // Licensed to the .NET Foundation under one or more agreements.
2 // The .NET Foundation licenses this file to you under the MIT license.
3 // See the LICENSE file in the project root for more information.
5 ////////////////////////////////////////////////////////////////////////////////
6 ////////////////////////////////////////////////////////////////////////////////
9 // This class defines a set of static methods that provide support for compilers.
13 namespace System.Runtime.CompilerServices
16 using System.Security;
18 using System.Runtime.CompilerServices;
19 using System.Runtime.InteropServices;
20 using System.Runtime.ConstrainedExecution;
21 using System.Runtime.Serialization;
22 using System.Threading;
23 using System.Runtime.Versioning;
25 public static class RuntimeHelpers
27 // Exposed here as a more appropriate place than on FormatterServices itself,
28 // which is a high level reflection heavy type.
29 public static Object GetUninitializedObject(Type type)
31 return FormatterServices.GetUninitializedObject(type);
34 [MethodImplAttribute(MethodImplOptions.InternalCall)]
35 public static extern void InitializeArray(Array array, RuntimeFieldHandle fldHandle);
37 // GetObjectValue is intended to allow value classes to be manipulated as 'Object'
38 // but have aliasing behavior of a value class. The intent is that you would use
39 // this function just before an assignment to a variable of type 'Object'. If the
40 // value being assigned is a mutable value class, then a shallow copy is returned
41 // (because value classes have copy semantics), but otherwise the object itself
44 // Note: VB calls this method when they're about to assign to an Object
45 // or pass it as a parameter. The goal is to make sure that boxed
46 // value types work identical to unboxed value types - ie, they get
47 // cloned when you pass them around, and are always passed by value.
48 // Of course, reference types are not cloned.
50 [MethodImplAttribute(MethodImplOptions.InternalCall)]
51 public static extern Object GetObjectValue(Object obj);
53 // RunClassConstructor causes the class constructor for the given type to be triggered
54 // in the current domain. After this call returns, the class constructor is guaranteed to
55 // have at least been started by some thread. In the absence of class constructor
56 // deadlock conditions, the call is further guaranteed to have completed.
58 // This call will generate an exception if the specified class constructor threw an
59 // exception when it ran.
61 [MethodImplAttribute(MethodImplOptions.InternalCall)]
62 private static extern void _RunClassConstructor(RuntimeType type);
64 public static void RunClassConstructor(RuntimeTypeHandle type)
66 _RunClassConstructor(type.GetRuntimeType());
69 // RunModuleConstructor causes the module constructor for the given type to be triggered
70 // in the current domain. After this call returns, the module constructor is guaranteed to
71 // have at least been started by some thread. In the absence of module constructor
72 // deadlock conditions, the call is further guaranteed to have completed.
74 // This call will generate an exception if the specified module constructor threw an
75 // exception when it ran.
77 [MethodImplAttribute(MethodImplOptions.InternalCall)]
78 private static extern void _RunModuleConstructor(System.Reflection.RuntimeModule module);
80 public static void RunModuleConstructor(ModuleHandle module)
82 _RunModuleConstructor(module.GetRuntimeModule());
86 [DllImport(JitHelpers.QCall, CharSet = CharSet.Unicode)]
87 internal static extern void _CompileMethod(IRuntimeMethodInfo method);
89 public static void PrepareMethod(RuntimeMethodHandle method) { }
90 public static void PrepareMethod(RuntimeMethodHandle method, RuntimeTypeHandle[] instantiation) { }
91 public static void PrepareContractedDelegate(Delegate d) { }
93 public static void PrepareDelegate(Delegate d)
97 throw new ArgumentNullException("d");
101 [MethodImplAttribute(MethodImplOptions.InternalCall)]
102 public static extern int GetHashCode(Object o);
104 [MethodImplAttribute(MethodImplOptions.InternalCall)]
105 public new static extern bool Equals(Object o1, Object o2);
107 public static int OffsetToStringData
109 // This offset is baked in by string indexer intrinsic, so there is no harm
110 // in getting it baked in here as well.
111 [System.Runtime.Versioning.NonVersionable]
114 // Number of bytes from the address pointed to by a reference to
115 // a String to the first 16-bit character in the String. Skip
116 // over the MethodTable pointer, & String
117 // length. Of course, the String reference points to the memory
118 // after the sync block, so don't count that.
119 // This property allows C#'s fixed statement to work on Strings.
120 // On 64 bit platforms, this should be 12 (8+4) and on 32 bit 8 (4+4).
129 // This method ensures that there is sufficient stack to execute the average Framework function.
130 // If there is not enough stack, then it throws System.InsufficientExecutionStackException.
131 // Note: this method is not part of the CER support, and is not to be confused with ProbeForSufficientStack
133 [MethodImplAttribute(MethodImplOptions.InternalCall)]
134 public static extern void EnsureSufficientExecutionStack();
136 // This method ensures that there is sufficient stack to execute the average Framework function.
137 // If there is not enough stack, then it return false.
138 // Note: this method is not part of the CER support, and is not to be confused with ProbeForSufficientStack
140 [MethodImplAttribute(MethodImplOptions.InternalCall)]
141 public static extern bool TryEnsureSufficientExecutionStack();
143 public static void ProbeForSufficientStack()
147 // This method is a marker placed immediately before a try clause to mark the corresponding catch and finally blocks as
148 // constrained. There's no code here other than the probe because most of the work is done at JIT time when we spot a call to this routine.
149 public static void PrepareConstrainedRegions()
151 ProbeForSufficientStack();
154 // When we detect a CER with no calls, we can point the JIT to this non-probing version instead
155 // as we don't need to probe.
156 public static void PrepareConstrainedRegionsNoOP()
160 public delegate void TryCode(Object userData);
162 public delegate void CleanupCode(Object userData, bool exceptionThrown);
164 [MethodImplAttribute(MethodImplOptions.InternalCall)]
165 public static extern void ExecuteCodeWithGuaranteedCleanup(TryCode code, CleanupCode backoutCode, Object userData);
167 internal static void ExecuteBackoutCodeHelper(Object backoutCode, Object userData, bool exceptionThrown)
169 ((CleanupCode)backoutCode)(userData, exceptionThrown);
172 /// <returns>true if given type is reference type or value type that contains references</returns>
173 static public bool IsReferenceOrContainsReferences<T>()
175 // The body of this function will be replaced by the EE with unsafe code!!!
176 // See getILIntrinsicImplementation for how this happens.
177 throw new InvalidOperationException();