- 
                Notifications
    
You must be signed in to change notification settings  - Fork 5.2k
 
Fix regression from RuntimeType.AllocateValueType rewrite #101137
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
          
     Merged
      
      
    
  
     Merged
                    Changes from all commits
      Commits
    
    
            Show all changes
          
          
            4 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      
    File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
        
          
          
            146 changes: 146 additions & 0 deletions
          
          146 
        
  src/coreclr/System.Private.CoreLib/src/System/RuntimeType.BoxCache.cs
  
  
      
      
   
        
      
      
    
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,146 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
| 
     | 
||
| using System.Diagnostics; | ||
| using System.Runtime.CompilerServices; | ||
| using System.Runtime.InteropServices; | ||
| 
     | 
||
| namespace System | ||
| { | ||
| internal sealed partial class RuntimeType | ||
| { | ||
| /// <summary> | ||
| /// A cache which allows optimizing <see cref="RuntimeHelpers.Box(ref byte, RuntimeTypeHandle)"/>. | ||
| /// </summary> | ||
| internal sealed unsafe partial class BoxCache : IGenericCacheEntry<BoxCache> | ||
| { | ||
| public static BoxCache Create(RuntimeType type) => new(type); | ||
| public void InitializeCompositeCache(CompositeCacheEntry compositeEntry) => compositeEntry._boxCache = this; | ||
| public static ref BoxCache? GetStorageRef(CompositeCacheEntry compositeEntry) => ref compositeEntry._boxCache; | ||
| 
     | 
||
| // The managed calli to the newobj allocator, plus its first argument | ||
| private readonly delegate*<void*, object> _pfnAllocator; | ||
| private readonly void* _allocatorFirstArg; | ||
| private readonly int _nullableValueOffset; | ||
| private readonly uint _valueTypeSize; | ||
| private readonly MethodTable* _pMT; | ||
| 
     | 
||
| #if DEBUG | ||
| private readonly RuntimeType _originalRuntimeType; | ||
| #endif | ||
| 
     | 
||
| private BoxCache(RuntimeType rt) | ||
| { | ||
| Debug.Assert(rt != null); | ||
| 
     | 
||
| #if DEBUG | ||
| _originalRuntimeType = rt; | ||
| #endif | ||
| 
     | 
||
| TypeHandle handle = rt.TypeHandle.GetNativeTypeHandle(); | ||
| 
     | 
||
| if (handle.IsTypeDesc) | ||
| throw new ArgumentException(SR.Arg_TypeNotSupported); | ||
| 
     | 
||
| _pMT = handle.AsMethodTable(); | ||
| 
     | 
||
| // For value types, this is checked in GetBoxInfo, | ||
| // but for non-value types, we still need to check this case for consistent behavior. | ||
| if (_pMT->ContainsGenericVariables) | ||
| throw new ArgumentException(SR.Arg_TypeNotSupported); | ||
| 
     | 
||
| if (_pMT->IsValueType) | ||
| { | ||
| GetBoxInfo(rt, out _pfnAllocator, out _allocatorFirstArg, out _nullableValueOffset, out _valueTypeSize); | ||
| } | ||
| } | ||
| 
     | 
||
| internal object? Box(RuntimeType rt, ref byte data) | ||
| { | ||
| #if DEBUG | ||
| if (_originalRuntimeType != rt) | ||
| { | ||
| Debug.Fail("Caller passed the wrong RuntimeType to this routine." | ||
| + Environment.NewLineConst + "Expected: " + (_originalRuntimeType ?? (object)"<null>") | ||
| + Environment.NewLineConst + "Actual: " + (rt ?? (object)"<null>")); | ||
| } | ||
| #endif | ||
| if (_pfnAllocator == null) | ||
| { | ||
| // If the allocator is null, then we shouldn't allocate and make a copy, | ||
| // we should return the data as the object it currently is. | ||
| return Unsafe.As<byte, object>(ref data); | ||
| } | ||
| 
     | 
||
| ref byte source = ref data; | ||
| 
     | 
||
| byte maybeNullableHasValue = Unsafe.ReadUnaligned<byte>(ref source); | ||
| 
     | 
||
| if (_nullableValueOffset != 0) | ||
| { | ||
| if (maybeNullableHasValue == 0) | ||
| { | ||
| return null; | ||
| } | ||
| source = ref Unsafe.Add(ref source, _nullableValueOffset); | ||
| } | ||
| 
     | 
||
| object result = _pfnAllocator(_allocatorFirstArg); | ||
| GC.KeepAlive(rt); | ||
| 
     | 
||
| if (_pMT->ContainsGCPointers) | ||
| { | ||
| Buffer.BulkMoveWithWriteBarrier(ref result.GetRawData(), ref source, _valueTypeSize); | ||
| } | ||
| else | ||
| { | ||
| SpanHelpers.Memmove(ref result.GetRawData(), ref source, _valueTypeSize); | ||
| } | ||
| 
     | 
||
| return result; | ||
| } | ||
| 
     | 
||
| /// <summary> | ||
| /// Given a RuntimeType, returns information about how to box instances | ||
| /// of it via calli semantics. | ||
| /// </summary> | ||
| private static void GetBoxInfo( | ||
| RuntimeType rt, | ||
| out delegate*<void*, object> pfnAllocator, | ||
| out void* vAllocatorFirstArg, | ||
| out int nullableValueOffset, | ||
| out uint valueTypeSize) | ||
| { | ||
| Debug.Assert(rt != null); | ||
| 
     | 
||
| delegate*<void*, object> pfnAllocatorTemp = default; | ||
| void* vAllocatorFirstArgTemp = default; | ||
| int nullableValueOffsetTemp = default; | ||
| uint valueTypeSizeTemp = default; | ||
| 
     | 
||
| GetBoxInfo( | ||
| new QCallTypeHandle(ref rt), | ||
| &pfnAllocatorTemp, &vAllocatorFirstArgTemp, | ||
| &nullableValueOffsetTemp, &valueTypeSizeTemp); | ||
| 
     | 
||
| pfnAllocator = pfnAllocatorTemp; | ||
| vAllocatorFirstArg = vAllocatorFirstArgTemp; | ||
| nullableValueOffset = nullableValueOffsetTemp; | ||
| valueTypeSize = valueTypeSizeTemp; | ||
| } | ||
| 
     | 
||
| [LibraryImport(RuntimeHelpers.QCall, EntryPoint = "ReflectionInvocation_GetBoxInfo")] | ||
| private static partial void GetBoxInfo( | ||
| QCallTypeHandle type, | ||
| delegate*<void*, object>* ppfnAllocator, | ||
| void** pvAllocatorFirstArg, | ||
| int* pNullableValueOffset, | ||
| uint* pValueTypeSize); | ||
| } | ||
| 
     | 
||
| internal object? Box(ref byte data) | ||
| { | ||
| return GetOrCreateCacheEntry<BoxCache>().Box(this, ref data); | ||
| } | ||
| } | ||
| } | ||
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
Uh oh!
There was an error while loading. Please reload this page.