Skip to content

Commit

Permalink
Fix race condition in System.Reflection.FieldAccessor (dotnet#99329)
Browse files Browse the repository at this point in the history
  • Loading branch information
jkotas authored and radical committed Mar 6, 2024
1 parent 21d174a commit 2a49f6e
Showing 1 changed file with 8 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ internal sealed class FieldAccessor
private readonly RtFieldInfo _fieldInfo;
private IntPtr _addressOrOffset;
private unsafe MethodTable* _methodTable;
private FieldAccessorType _fieldAccessType;
private volatile FieldAccessorType _fieldAccessType;

internal FieldAccessor(FieldInfo fieldInfo)
{
Expand Down Expand Up @@ -52,19 +52,19 @@ private void Initialize()
{
if (fieldType.IsEnum)
{
_fieldAccessType = GetPrimitiveAccessorTypeForStatic(fieldType.GetEnumUnderlyingType());
_methodTable = (MethodTable*)fieldType.TypeHandle.Value;
_fieldAccessType = GetPrimitiveAccessorTypeForStatic(fieldType.GetEnumUnderlyingType());
}
else if (RuntimeTypeHandle.GetCorElementType(fieldType) == CorElementType.ELEMENT_TYPE_VALUETYPE)
{
// The runtime stores non-primitive value types as a boxed value.
_fieldAccessType = FieldAccessorType.StaticValueTypeBoxed;
_methodTable = (MethodTable*)fieldType.TypeHandle.Value;
_fieldAccessType = FieldAccessorType.StaticValueTypeBoxed;
}
else
{
_fieldAccessType = GetPrimitiveAccessorTypeForStatic(fieldType);
_methodTable = (MethodTable*)fieldType.TypeHandle.Value;
_fieldAccessType = GetPrimitiveAccessorTypeForStatic(fieldType);
}
}
else if (fieldType.IsPointer)
Expand All @@ -73,8 +73,8 @@ private void Initialize()
}
else if (fieldType.IsFunctionPointer)
{
_fieldAccessType = GetIntPtrAccessorTypeForStatic();
_methodTable = (MethodTable*)typeof(IntPtr).TypeHandle.Value;
_fieldAccessType = GetIntPtrAccessorTypeForStatic();
}
else
{
Expand All @@ -87,22 +87,22 @@ private void Initialize()

if (fieldType.IsEnum)
{
_fieldAccessType = GetPrimitiveAccessorTypeForInstance(fieldType.GetEnumUnderlyingType());
_methodTable = (MethodTable*)fieldType.TypeHandle.Value;
_fieldAccessType = GetPrimitiveAccessorTypeForInstance(fieldType.GetEnumUnderlyingType());
}
else if (fieldType.IsValueType)
{
_fieldAccessType = GetPrimitiveAccessorTypeForInstance(fieldType);
_methodTable = (MethodTable*)fieldType.TypeHandle.Value;
_fieldAccessType = GetPrimitiveAccessorTypeForInstance(fieldType);
}
else if (fieldType.IsPointer)
{
_fieldAccessType = FieldAccessorType.InstancePointerType;
}
else if (fieldType.IsFunctionPointer)
{
_fieldAccessType = GetIntPtrAccessorTypeForInstance();
_methodTable = (MethodTable*)typeof(IntPtr).TypeHandle.Value;
_fieldAccessType = GetIntPtrAccessorTypeForInstance();
}
else
{
Expand Down

0 comments on commit 2a49f6e

Please sign in to comment.