Repository navigation
Fix reflection access to misaligned fields in packed structs #133828
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
Open
steveisok
wants to merge
7
commits into
main
Choose a base branch
from
steveisok-fix-packed-reflection-sigbus
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
db90580
Fix reflection access to misaligned fields
steveisok 8d240e0
Handle misaligned direct reflection access
steveisok 630846c
Refine packed field alignment handling
steveisok 9192c64
Make primitive reflection access consistent
steveisok 4691b84
Correct 32-bit reflection field semantics
steveisok bc45d68
Address reflection field access review feedback
steveisok 1e59afc
Keep NativeAOT field alignment handling in reflection
steveisok 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
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -231,42 +231,48 @@ public static unsafe RuntimeTypeHandle CreateRuntimeTypeHandle(IntPtr ldTokenRes | |||||
|
|
||||||
| public static unsafe void StoreValueTypeField(IntPtr address, object fieldValue, RuntimeTypeHandle fieldType) | ||||||
| { | ||||||
| RuntimeImports.RhUnbox(fieldValue, ref *(byte*)address, fieldType.ToMethodTable()); | ||||||
| StoreValueTypeField(ref *(byte*)address, fieldValue, fieldType); | ||||||
| } | ||||||
|
|
||||||
| public static unsafe object LoadValueTypeField(IntPtr address, RuntimeTypeHandle fieldType) | ||||||
| { | ||||||
| return RuntimeExports.RhBox(fieldType.ToMethodTable(), ref *(byte*)address); | ||||||
| return LoadValueTypeField(ref *(byte*)address, fieldType); | ||||||
| } | ||||||
|
|
||||||
| public static unsafe object LoadPointerTypeField(IntPtr address, RuntimeTypeHandle fieldType) | ||||||
| { | ||||||
| IntPtr value = default; | ||||||
| LoadPrimitiveField(ref *(byte*)address, ref Unsafe.As<IntPtr, byte>(ref value), (uint)IntPtr.Size); | ||||||
|
|
||||||
| if (fieldType.ToMethodTable()->IsFunctionPointer) | ||||||
| return *(IntPtr*)address; | ||||||
| return value; | ||||||
|
|
||||||
| return ReflectionPointer.Box(*(void**)address, Type.GetTypeFromHandle(fieldType)); | ||||||
| return ReflectionPointer.Box((void*)value, Type.GetTypeFromHandle(fieldType)); | ||||||
| } | ||||||
|
|
||||||
| public static unsafe void StoreValueTypeField(object obj, int fieldOffset, object fieldValue, RuntimeTypeHandle fieldType) | ||||||
| { | ||||||
| ref byte address = ref Unsafe.AddByteOffset(ref obj.GetRawData(), new IntPtr(fieldOffset - ObjectHeaderSize)); | ||||||
| RuntimeImports.RhUnbox(fieldValue, ref address, fieldType.ToMethodTable()); | ||||||
| StoreValueTypeField(ref address, fieldValue, fieldType); | ||||||
| } | ||||||
|
|
||||||
| public static unsafe object LoadValueTypeField(object obj, int fieldOffset, RuntimeTypeHandle fieldType) | ||||||
| { | ||||||
| ref byte address = ref Unsafe.AddByteOffset(ref obj.GetRawData(), new IntPtr(fieldOffset - ObjectHeaderSize)); | ||||||
| return RuntimeExports.RhBox(fieldType.ToMethodTable(), ref address); | ||||||
| return LoadValueTypeField(ref address, fieldType); | ||||||
| } | ||||||
|
|
||||||
| public static unsafe object LoadPointerTypeField(object obj, int fieldOffset, RuntimeTypeHandle fieldType) | ||||||
| { | ||||||
| ref byte address = ref Unsafe.AddByteOffset(ref obj.GetRawData(), new IntPtr(fieldOffset - ObjectHeaderSize)); | ||||||
|
|
||||||
| IntPtr value = default; | ||||||
| LoadPrimitiveField(ref address, ref Unsafe.As<IntPtr, byte>(ref value), (uint)IntPtr.Size); | ||||||
|
|
||||||
| if (fieldType.ToMethodTable()->IsFunctionPointer) | ||||||
| return RuntimeExports.RhBox(MethodTable.Of<IntPtr>(), ref address); | ||||||
| return value; | ||||||
|
|
||||||
| return ReflectionPointer.Box((void*)Unsafe.As<byte, IntPtr>(ref address), Type.GetTypeFromHandle(fieldType)); | ||||||
| return ReflectionPointer.Box((void*)value, Type.GetTypeFromHandle(fieldType)); | ||||||
| } | ||||||
|
|
||||||
| public static unsafe void StoreReferenceTypeField(IntPtr address, object fieldValue) | ||||||
|
|
@@ -296,7 +302,7 @@ public static void StoreValueTypeFieldValueIntoValueType(TypedReference typedRef | |||||
| { | ||||||
| Debug.Assert(TypedReference.TargetTypeToken(typedReference).ToMethodTable()->IsValueType); | ||||||
|
|
||||||
| RuntimeImports.RhUnbox(fieldValue, ref Unsafe.Add<byte>(ref typedReference.Value, fieldOffset), fieldTypeHandle.ToMethodTable()); | ||||||
| StoreValueTypeField(ref Unsafe.Add<byte>(ref typedReference.Value, fieldOffset), fieldValue, fieldTypeHandle); | ||||||
| } | ||||||
|
|
||||||
| [CLSCompliant(false)] | ||||||
|
|
@@ -305,7 +311,7 @@ public static object LoadValueTypeFieldValueFromValueType(TypedReference typedRe | |||||
| Debug.Assert(TypedReference.TargetTypeToken(typedReference).ToMethodTable()->IsValueType); | ||||||
| Debug.Assert(fieldTypeHandle.ToMethodTable()->IsValueType); | ||||||
|
|
||||||
| return RuntimeExports.RhBox(fieldTypeHandle.ToMethodTable(), ref Unsafe.Add<byte>(ref typedReference.Value, fieldOffset)); | ||||||
| return LoadValueTypeField(ref Unsafe.Add<byte>(ref typedReference.Value, fieldOffset), fieldTypeHandle); | ||||||
| } | ||||||
|
|
||||||
| [CLSCompliant(false)] | ||||||
|
|
@@ -328,14 +334,130 @@ public static object LoadReferenceTypeFieldValueFromValueType(TypedReference typ | |||||
| public static unsafe object LoadPointerTypeFieldValueFromValueType(TypedReference typedReference, int fieldOffset, RuntimeTypeHandle fieldTypeHandle) | ||||||
| { | ||||||
| Debug.Assert(TypedReference.TargetTypeToken(typedReference).ToMethodTable()->IsValueType); | ||||||
| IntPtr ptrValue = Unsafe.As<byte, IntPtr>(ref Unsafe.Add<byte>(ref typedReference.Value, fieldOffset)); | ||||||
| IntPtr ptrValue = default; | ||||||
| LoadPrimitiveField( | ||||||
| ref Unsafe.Add<byte>(ref typedReference.Value, fieldOffset), | ||||||
| ref Unsafe.As<IntPtr, byte>(ref ptrValue), | ||||||
| (uint)IntPtr.Size); | ||||||
|
|
||||||
| if (fieldTypeHandle.ToMethodTable()->IsFunctionPointer) | ||||||
| return ptrValue; | ||||||
|
|
||||||
| return ReflectionPointer.Box((void*)ptrValue, Type.GetTypeFromHandle(fieldTypeHandle)); | ||||||
| } | ||||||
|
|
||||||
| private static object LoadValueTypeField(ref byte address, RuntimeTypeHandle fieldType) | ||||||
| { | ||||||
| MethodTable* fieldMethodTable = fieldType.ToMethodTable(); | ||||||
| if (!fieldMethodTable->IsPrimitive && | ||||||
| (fieldMethodTable->ContainsGCPointers || fieldMethodTable->IsNullable)) | ||||||
| { | ||||||
| return RuntimeExports.RhBox(fieldMethodTable, ref address); | ||||||
| } | ||||||
|
|
||||||
| object result = RuntimeImports.RhNewObject(fieldMethodTable); | ||||||
| if (fieldMethodTable->IsPrimitive) | ||||||
| { | ||||||
| LoadPrimitiveField(ref address, ref result.GetRawData(), fieldMethodTable->ValueTypeSize); | ||||||
| } | ||||||
| else | ||||||
| { | ||||||
| Unsafe.CopyBlockUnaligned(ref result.GetRawData(), ref address, fieldMethodTable->ValueTypeSize); | ||||||
| } | ||||||
|
|
||||||
| return result; | ||||||
| } | ||||||
|
|
||||||
| private static void StoreValueTypeField(ref byte address, object fieldValue, RuntimeTypeHandle fieldType) | ||||||
| { | ||||||
| MethodTable* fieldMethodTable = fieldType.ToMethodTable(); | ||||||
| if (!fieldMethodTable->IsPrimitive && | ||||||
| (fieldMethodTable->ContainsGCPointers || fieldMethodTable->IsNullable)) | ||||||
| { | ||||||
| RuntimeImports.RhUnbox(fieldValue, ref address, fieldMethodTable); | ||||||
| return; | ||||||
| } | ||||||
|
|
||||||
| if (fieldMethodTable->IsPrimitive) | ||||||
| { | ||||||
| StorePrimitiveField(ref address, ref fieldValue.GetRawData(), fieldMethodTable->ValueTypeSize); | ||||||
| } | ||||||
| else | ||||||
| { | ||||||
| Unsafe.CopyBlockUnaligned(ref address, ref fieldValue.GetRawData(), fieldMethodTable->ValueTypeSize); | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| private static unsafe void LoadPrimitiveField(ref byte source, ref byte destination, uint size) | ||||||
| { | ||||||
| Debug.Assert(size is 1 or 2 or 4 or 8); | ||||||
|
|
||||||
| fixed (byte* sourcePointer = &source) | ||||||
| { | ||||||
| if (((nuint)sourcePointer & (size - 1)) != 0) | ||||||
| { | ||||||
| Unsafe.CopyBlockUnaligned(ref destination, ref source, size); | ||||||
| return; | ||||||
| } | ||||||
|
|
||||||
| switch (size) | ||||||
| { | ||||||
| case 1: | ||||||
| destination = Volatile.Read(ref source); | ||||||
| break; | ||||||
| case 2: | ||||||
| Unsafe.As<byte, short>(ref destination) = Volatile.Read(ref Unsafe.As<byte, short>(ref source)); | ||||||
| break; | ||||||
| case 4: | ||||||
| Unsafe.As<byte, int>(ref destination) = Volatile.Read(ref Unsafe.As<byte, int>(ref source)); | ||||||
| break; | ||||||
| case 8: | ||||||
| #if TARGET_64BIT | ||||||
| Unsafe.As<byte, long>(ref destination) = Volatile.Read(ref Unsafe.As<byte, long>(ref source)); | ||||||
| #else | ||||||
| Unsafe.As<byte, long>(ref destination) = Unsafe.As<byte, long>(ref source); | ||||||
| Interlocked.MemoryBarrier(); | ||||||
| #endif | ||||||
| break; | ||||||
| } | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| private static unsafe void StorePrimitiveField(ref byte destination, ref byte source, uint size) | ||||||
| { | ||||||
| Debug.Assert(size is 1 or 2 or 4 or 8); | ||||||
|
|
||||||
| fixed (byte* destinationPointer = &destination) | ||||||
| { | ||||||
| if (((nuint)destinationPointer & (size - 1)) != 0) | ||||||
| { | ||||||
| Unsafe.CopyBlockUnaligned(ref destination, ref source, size); | ||||||
| return; | ||||||
| } | ||||||
|
|
||||||
| switch (size) | ||||||
| { | ||||||
| case 1: | ||||||
| Volatile.Write(ref destination, source); | ||||||
| break; | ||||||
| case 2: | ||||||
| Volatile.Write(ref Unsafe.As<byte, short>(ref destination), Unsafe.As<byte, short>(ref source)); | ||||||
| break; | ||||||
| case 4: | ||||||
| Volatile.Write(ref Unsafe.As<byte, int>(ref destination), Unsafe.As<byte, int>(ref source)); | ||||||
| break; | ||||||
| case 8: | ||||||
| #if TARGET_64BIT | ||||||
| Volatile.Write(ref Unsafe.As<byte, long>(ref destination), Unsafe.As<byte, long>(ref source)); | ||||||
| #else | ||||||
| Interlocked.MemoryBarrier(); | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| Unsafe.As<byte, long>(ref destination) = Unsafe.As<byte, long>(ref source); | ||||||
| #endif | ||||||
| break; | ||||||
| } | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| public static unsafe object GetThreadStaticBase(IntPtr cookie) | ||||||
| { | ||||||
| return ThreadStatics.GetThreadStaticBaseForType(*(TypeManagerSlot**)cookie, (int)*((IntPtr*)(cookie) + 1)); | ||||||
|
|
||||||
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 |
|---|---|---|
|
|
@@ -745,4 +745,3 @@ struct cdac_data<FieldDesc> | |
| }; | ||
|
|
||
| #endif // _FIELD_H_ | ||
|
|
||
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.