Welcome to mirror list, hosted at ThFree Co, Russian Federation.

CastableObjectSupport.cs « Runtime « System « src « Runtime.Base « src - github.com/mono/corert.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 5ed1679c582f07dfe0ece5815ed98ee0e40c0421 (plain)
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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;
using System.Runtime.CompilerServices;

using Internal.Runtime;
using Internal.Runtime.CompilerServices;

namespace System.Runtime
{
    [System.Runtime.CompilerServices.EagerStaticClassConstructionAttribute]
    internal static class CastableObjectSupport
    {
        private static object s_castFailCanary = new object();
        private static CastableObjectCacheEntry<IntPtr>[] s_ThunkBasedDispatchCellTargets = new CastableObjectCacheEntry<IntPtr>[16];

        private static ThunksHeap s_thunksHeap;

        internal interface ICastableObject
        // TODO!! BEGIN REMOVE THIS CODE WHEN WE REMOVE ICASTABLE
            : ICastable
        // TODO!! END REMOVE THIS CODE WHEN WE REMOVE ICASTABLE
        {
            // This is called if casting this object to the given interface type would otherwise fail. Casting
            // here means the IL isinst and castclass instructions in the case where they are given an interface
            // type as the target type.
            //
            // A return value of non-null indicates the cast is valid.
            // The return value (if non-null) must be an object instance that implements the specified interface.
            //
            // If null is returned when this is called as part of a castclass then the usual InvalidCastException
            // will be thrown unless an alternate exception is assigned to the castError output parameter. This
            // parameter is ignored on successful casts or during the evaluation of an isinst (which returns null
            // rather than throwing on error).
            //
            // The results of this call are cached
            //
            // The results of this call should be semantically  invariant for the same object, interface type pair. 
            // That is because this is the only guard placed before an interface invocation at runtime. It is possible
            // that this call may occur more than once for a given pair, and it is possible that the results of multiple calls
            // may remain in use over time.
            object CastToInterface(EETypePtr interfaceType, bool produceCastErrorException, out Exception castError);
        }

        internal struct CastableObjectCacheEntry<V>
        {
            public IntPtr Key;
            public V Value;
        }

        internal class CastableObject
        {
            public CastableObjectCacheEntry<object>[] Cache;
        }

        // cache must be a size which is a power of two.
        internal static unsafe V CacheLookup<V>(CastableObjectCacheEntry<V>[] cache, IntPtr keyToLookup)
        {
            uint hashcode = unchecked((uint)keyToLookup.ToInt64());
            uint cacheMask = (uint)cache.Length - 1;
            uint bucket = hashcode & cacheMask;
            uint curbucket = bucket;

            // hash algorithm is open addressing with linear probing

            while (curbucket < cache.Length)
            {
                if (cache[curbucket].Key == keyToLookup)
                    return cache[curbucket].Value;
                if (cache[curbucket].Key == default(IntPtr))
                    return default(V);
                curbucket++;
            }

            // Handle wrap-around case
            curbucket = 0;
            while (curbucket < bucket)
            {
                if (cache[curbucket].Key == keyToLookup)
                    return cache[curbucket].Value;
                if (cache[curbucket].Key == default(IntPtr))
                    return default(V);
                curbucket++;
            }

            return default(V);
        }

        internal static unsafe int GetCachePopulation<V>(CastableObjectCacheEntry<V>[] cache)
        {
            int population = 0;
            for (int i = 0; i < cache.Length; i++)
            {
                if (cache[i].Key != default(IntPtr))
                    population++;
            }

            return population;
        }

        internal static unsafe void AddToExistingCache<V>(CastableObjectCacheEntry<V>[] cache, IntPtr key, V value)
        {
            uint hashcode = unchecked((uint)key.ToInt64());
            uint cacheMask = (uint)cache.Length - 1;
            uint bucket = hashcode & cacheMask;
            uint curbucket = bucket;

            // hash algorithm is open addressing with linear probing

            while (curbucket < cache.Length)
            {
                if (cache[curbucket].Key == default(IntPtr))
                {
                    cache[curbucket].Key = key;
                    cache[curbucket].Value = value;
                    return;
                }
                curbucket++;
            }

            // Handle wrap-around case
            curbucket = 0;
            while (curbucket < bucket)
            {
                if (cache[curbucket].Key == default(IntPtr))
                {
                    cache[curbucket].Key = key;
                    cache[curbucket].Value = value;
                    return;
                }
                curbucket++;
            }

            EH.FallbackFailFast(RhFailFastReason.InternalError, null);
            return;
        }

        /// <summary>
        /// Add the newly allocated thunk of a CastableObject dispatch cell call to the cache if possible. (OOM errors may cause caching failure. 
        /// An OOM is specified not to introduce new failure points though.)
        /// </summary>
        internal static unsafe void AddToThunkCache(IntPtr pDispatchCell, IntPtr pThunkTarget)
        {
            // Expand old cache if it isn't big enough.
            if (GetCachePopulation(s_ThunkBasedDispatchCellTargets) > (s_ThunkBasedDispatchCellTargets.Length / 2))
            {
                CastableObjectCacheEntry<IntPtr>[] oldCache = s_ThunkBasedDispatchCellTargets;
                try
                {
                    s_ThunkBasedDispatchCellTargets = new CastableObjectCacheEntry<IntPtr>[oldCache.Length * 2];
                }
                catch (OutOfMemoryException)
                {
                    // Failed to allocate a bigger cache.  That is fine, keep the old one.
                }

                for (int i = 0; i < oldCache.Length; i++)
                {
                    if (oldCache[i].Key != default(IntPtr))
                    {
                        AddToExistingCache(s_ThunkBasedDispatchCellTargets, oldCache[i].Key, oldCache[i].Value);
                    }
                }
            }

            AddToExistingCache(s_ThunkBasedDispatchCellTargets, pDispatchCell, pThunkTarget);
        }

        /// <summary>
        /// Add the results of a CastableObject call to the cache if possible. (OOM errors may cause caching failure. An OOM is specified not
        /// to introduce new failure points though.)
        /// </summary>
        internal static unsafe void AddToCastableCache(ICastableObject castableObject, EEType* interfaceType, object objectForType)
        {
            CastableObjectCacheEntry<object>[] cache = Unsafe.As<CastableObject>(castableObject).Cache;
            bool setNewCache = false;

            // If there is no cache, allocate one
            if (cache == null)
            {
                try
                {
                    cache = new CastableObjectCacheEntry<object>[8];
                }
                catch (OutOfMemoryException)
                {
                    // Failed to allocate a cache.  That is fine, simply return.
                    return;
                }

                setNewCache = true;
            }

            // Expand old cache if it isn't big enough.
            if (GetCachePopulation(cache) > (cache.Length / 2))
            {
                setNewCache = true;
                CastableObjectCacheEntry<object>[] oldCache = cache;
                try
                {
                    cache = new CastableObjectCacheEntry<object>[oldCache.Length * 2];
                }
                catch (OutOfMemoryException)
                {
                    // Failed to allocate a bigger cache.  That is fine, keep the old one.
                }

                for (int i = 0; i < oldCache.Length; i++)
                {
                    if (oldCache[i].Key != default(IntPtr))
                    {
                        AddToExistingCache(cache, oldCache[i].Key, oldCache[i].Value);
                    }
                }
            }

            AddToExistingCache(cache, new IntPtr(interfaceType), objectForType);

            if (setNewCache)
            {
                Unsafe.As<CastableObject>(castableObject).Cache = cache;
            }

            return;
        }

        internal static unsafe object GetCastableTargetIfPossible(ICastableObject castableObject, EEType* interfaceType, bool produceException, ref Exception exception)
        {
            CastableObjectCacheEntry<object>[] cache = Unsafe.As<CastableObject>(castableObject).Cache;

            object targetObjectInitial = null;

            if (cache != null)
            {
                targetObjectInitial = CacheLookup(cache, new IntPtr(interfaceType));
                if (targetObjectInitial != null)
                {
                    if (targetObjectInitial != s_castFailCanary)
                        return targetObjectInitial;
                    else if (!produceException)
                        return null;
                }
            }

            // Call into the object to determine if the runtime can perform the cast. This will return null if it fails.
            object targetObject = castableObject.CastToInterface(new EETypePtr(new IntPtr(interfaceType)), produceException, out exception);

            // If the target object is null, and that result has already been cached, just return null now. 
            // Otherwise, we need to store the canary in the cache so future failing "is" checks can be fast
            if (targetObject == null)
            {
                if (targetObjectInitial != null)
                    return null;
                else
                    targetObject = s_castFailCanary;
            }

            InternalCalls.RhpAcquireCastCacheLock();
            // Assuming we reach here, we should attempt to add the newly discovered targetObject to the per-object cache

            // First, check to see if something is already there

            // we may have replaced the cache object since the earlier acquisition in this method. Re-acquire the cache object
            // here.
            cache = Unsafe.As<CastableObject>(castableObject).Cache;
            object targetObjectInCache = null;

            if (cache != null)
                targetObjectInCache = CacheLookup(cache, new IntPtr(interfaceType));

            if (targetObjectInCache == null)
            {
                // If the target object still isn't in the cache by this point, add it now
                AddToCastableCache(castableObject, interfaceType, targetObject);
                targetObjectInCache = targetObject;
            }
            InternalCalls.RhpReleaseCastCacheLock();

            if (targetObjectInCache != s_castFailCanary)
                return targetObjectInCache;
            else
                return null;
        }

        internal static unsafe IntPtr GetCastableObjectDispatchCellThunk(EEType* pInstanceType, IntPtr pDispatchCell)
        {
            IntPtr pTargetCode = CacheLookup(s_ThunkBasedDispatchCellTargets, pDispatchCell);
            if (pTargetCode != default(IntPtr))
                return pTargetCode;

            InternalCalls.RhpAcquireCastCacheLock();
            {
                // Look in the cache again after taking the lock

                pTargetCode = CacheLookup(s_ThunkBasedDispatchCellTargets, pDispatchCell);
                if (pTargetCode != default(IntPtr))
                    return pTargetCode;

                // Allocate a new thunk. Failure to allocate one will result in a fail-fast. We don't return nulls from this API.

                if (s_thunksHeap == null)
                {
                    s_thunksHeap = ThunksHeap.CreateThunksHeap(InternalCalls.RhpGetCastableObjectDispatch_CommonStub());
                    if (s_thunksHeap == null)
                        EH.FallbackFailFast(RhFailFastReason.InternalError, null);
                }

                pTargetCode = s_thunksHeap.AllocateThunk();
                if (pTargetCode == IntPtr.Zero)
                    EH.FallbackFailFast(RhFailFastReason.InternalError, null);

                s_thunksHeap.SetThunkData(pTargetCode, pDispatchCell, InternalCalls.RhpGetCastableObjectDispatchHelper_TailCalled());

                AddToThunkCache(pDispatchCell, pTargetCode);
            }
            InternalCalls.RhpReleaseCastCacheLock();

            return pTargetCode;
        }

        [RuntimeExport("RhpCastableObjectResolve")]
        unsafe private static IntPtr RhpCastableObjectResolve(IntPtr callerTransitionBlockParam, IntPtr pCell)
        {
            IntPtr locationOfThisPointer = callerTransitionBlockParam + TransitionBlock.GetThisOffset();
            object pObject = Unsafe.As<IntPtr, object>(ref *(IntPtr*)locationOfThisPointer);

            DispatchCellInfo cellInfo;
            InternalCalls.RhpGetDispatchCellInfo(pCell, out cellInfo);
            if (cellInfo.CellType != DispatchCellType.InterfaceAndSlot)
            {
                // Dispatch cell used for castable object resolve is not InterfaceAndSlot. This should not be possible
                // as all metadata based cells should have been converted to interface and slot cells by this time.
                EH.FallbackFailFast(RhFailFastReason.InternalError, null);
                return IntPtr.Zero;
            }

            EEType* pInterfaceType = cellInfo.InterfaceType.ToPointer();

            Exception e = null;
            object targetObject = GetCastableTargetIfPossible((ICastableObject)pObject, pInterfaceType, false, ref e);
            if (targetObject == null)
                EH.FailFastViaClasslib(RhFailFastReason.InternalError, null, pObject.EEType->GetAssociatedModuleAddress());

            Unsafe.As<IntPtr, object>(ref *(IntPtr*)locationOfThisPointer) = targetObject;

            InternalCalls.RhpSetTLSDispatchCell(pCell);
            return InternalCalls.RhpGetTailCallTLSDispatchCell();
        }
    }
}