You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
On architectures that require natural alignment for acquire/release operations, reflection access to under-aligned fields in packed structs can fault. In particular, FieldDesc::GetInstanceField and SetInstanceField use VolatileLoad and VolatileStore for 1-, 2-, 4-, and 8-byte fields, which can raise SIGBUS on arm64.
This also affects APIs that use reflection internally, such as the field-walking fallback in ValueType.Equals.
This change:
Preserves the existing atomic volatile access for naturally aligned fields.
Uses memcpyNoGCRefs for misaligned non-GC scalar fields.
Routes potentially misaligned primitive, enum, native-int, pointer, and function-pointer instance fields away from the cached managed volatile accessor and through the native slow path.
Routes 8-byte instance fields through the native path on 32-bit platforms, where object alignment alone cannot guarantee 8-byte field alignment.
Adds regression coverage for FieldInfo.GetValue, cached field access, FieldInfo.SetValue, and ValueType.Equals across the affected field types.
Misaligned accesses intentionally do not provide atomic or volatile guarantees. Behavior for naturally aligned fields is unchanged.
Testing
Confirmed the regression test fails against the unmodified runtime with SIGBUS on macOS arm64.
Built checked CoreCLR successfully.
Built and ran the targeted regression test successfully with the fix.
Note
This pull request description was generated with GitHub Copilot.
Azure Pipelines:
Successfully started running 3 pipeline(s).
13 pipeline(s) were filtered out due to trigger conditions.
There may be pipelines that require an authorized user to comment /azp run to run.
This helper is used by the unconditional TestEntryPoint, but it always searches for a 16-byte granule crossing even on 32-bit targets. CoreCLR only guarantees DATA_ALIGNMENT of 4 bytes there (src/coreclr/vm/arm/cgencpu.h:16, src/coreclr/vm/i386/cgencpu.h:25), and these packed boxes can have a fixed residue because their allocation size is repeatedly rounded to 16 bytes; after AllocationLimit attempts the test can reach line 296 and fail without exercising the runtime fix. Keep the granule-crossing predicate for arm64, and use a normal field-misalignment predicate (or another deterministic placement) on other architectures.
NativeAOT audit result: no NativeAOT product change is needed for this bug class.
Normal and TypedReference primitive/enum reads and writes flow through RuntimeAugments.LoadValueTypeField / StoreValueTypeField and their value-type-target variants, then RhBox / RhUnbox. These are block copies, not volatile/acquire-release scalar operations. Non-GC values use Unsafe.CopyBlock; GC-containing values use RhBulkMoveWithWriteBarrier. On ARM32, the block-copy codegen uses integer ldr/str chunks rather than alignment-sensitive ldrd/vldr long/double operations.
Packed unmanaged-pointer reads use Unsafe.As<byte, IntPtr>. The JIT importer marks that widening reinterpretation GTF_IND_UNALIGNED because the source is byte-aligned and the destination requires pointer alignment. Function-pointer instance reads either box through RhBox or use the same unaligned-marked direct path. None of these paths use Volatile, LDAR, or STLR.
NativeAOT ValueType.Equals uses RhBoxAny; its hash-code fallback uses byte spans or Unsafe.As<byte, float/double>, which receives the same unaligned importer treatment. These paths do not share CoreCLR’s reflection volatile accessor.
I built and ran the full packed-field regression as NativeAOT on macOS arm64. The first attempt found that the generic reflected fields were trimmed; the follow-up commit roots public fields on the generic struct parameter. The final NativeAOT build completed with zero warnings/errors and the test passed with expected/actual exit code 100. The test placement predicate now requires a 16-byte atomic-granule crossing only on ARM64 and natural misalignment on other architectures.
The reason will be displayed to describe this comment to others. Learn more.
Handle this in VolatileLoad? Do we have a test checking this on 32bit?
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.
Fixes #133236
On architectures that require natural alignment for acquire/release operations, reflection access to under-aligned fields in packed structs can fault. In particular,
FieldDesc::GetInstanceFieldandSetInstanceFielduseVolatileLoadandVolatileStorefor 1-, 2-, 4-, and 8-byte fields, which can raiseSIGBUSon arm64.This also affects APIs that use reflection internally, such as the field-walking fallback in
ValueType.Equals.This change:
memcpyNoGCRefsfor misaligned non-GC scalar fields.FieldInfo.GetValue, cached field access,FieldInfo.SetValue, andValueType.Equalsacross the affected field types.Misaligned accesses intentionally do not provide atomic or volatile guarantees. Behavior for naturally aligned fields is unchanged.
Testing
SIGBUSon macOS arm64.Note
This pull request description was generated with GitHub Copilot.