Skip to content

Commit

Permalink
Fix race condition in System.Reflection.FieldAccessor (#99329)
Browse files Browse the repository at this point in the history
Fixes #98998
  • Loading branch information
jkotas authored Mar 5, 2024
1 parent 9b0055b commit ab1d93b
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 ab1d93b

Please sign in to comment.