Repository navigation
Add Pointer.PointerType and Pointer.Value - #137769
Merged
Merged
Conversation
Pointer stores the pointer type given to Box, or the declared type when the runtime creates the Pointer, but no public member returns it, so code outside CoreLib cannot check a Pointer against a target type, as reflection invoke does inside CoreLib. PointerType returns the stored type and Value the stored pointer, as approved in dotnet#137512. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
Azure Pipelines: Successfully started running 3 pipeline(s). 12 pipeline(s) were filtered out due to trigger conditions. There may be pipelines that require an authorized user to comment /azp run to run. |
Contributor
|
Tagging subscribers to this area: @steveisok, @dotnet/area-system-reflection |
jkotas
reviewed
Nov 17, 2026
Reflection invoke on CoreCLR, NativeAOT and Mono now reads the public Pointer.PointerType and Pointer.Value, so the internal GetPointerType and GetPointerValue methods that duplicated them are deleted. Co-Authored-By: Claude Opus 5.5 <[email protected]>
caraioniurie47
requested review from
MichalStrehovsky,
steveisok and
vitek-karas
as code owners
November 18, 2026 08:39
This was referenced Nov 18, 2026
Contributor
There was a problem hiding this comment.
🔵 Needs a closer look
The required check that the approving commenter applied the api-approved label on issue #137512 remains unresolved because available GitHub tools do not expose label events.
No open issues 🎉
What changed in this PR
Adds Pointer.PointerType and Pointer.Value so code outside CoreLib can inspect a boxed pointer’s type and address. This enables the type validation needed by DispatchProxy.
Changes:
- Exposes both properties in CoreLib and the System.Runtime reference assembly.
- Replaces internal pointer accessors with property access in CoreCLR, NativeAOT, and Mono.
- Expands tests for boxed pointers, reflection, delegate invocation, and native-integer conversions.
| File | Description |
|---|---|
src/mono/System.Private.CoreLib/src/System/RuntimeType.Mono.cs |
Uses the properties during pointer conversion. |
src/libraries/System.Runtime/tests/System.Runtime.Tests/System/UIntPtrTests.cs |
Checks properties on reflected unsigned native-integer conversions. |
src/libraries/System.Runtime/tests/System.Runtime.Tests/System/Reflection/PointerTests.cs |
Covers boxed pointer types and reflection-created pointers. |
src/libraries/System.Runtime/tests/System.Runtime.Tests/System/Reflection/InvokeRefReturn.cs |
Checks properties on reflected pointer ref returns. |
src/libraries/System.Runtime/tests/System.Runtime.Tests/System/IntPtrTests.cs |
Checks properties on reflected signed native-integer conversions. |
src/libraries/System.Runtime/tests/System.Reflection.Tests/FieldInfoTests.cs |
Verifies pointer type and address on repeated field reads. |
src/libraries/System.Runtime/ref/System.Runtime.cs |
Declares the two public properties. |
src/libraries/System.Private.CoreLib/src/System/Reflection/Pointer.cs |
Implements and documents properties; removes internal accessors. |
src/coreclr/System.Private.CoreLib/src/System/RuntimeType.CoreCLR.cs |
Uses the properties during pointer conversion. |
src/coreclr/nativeaot/System.Private.CoreLib/src/System/InvokeUtils.cs |
Uses the properties during pointer assignability checks and conversion. |
Review effort: Balanced
AaronRobinsonMSFT
added a commit
that referenced
this pull request
Nov 21, 2026
A `DispatchProxy` method generated for an interface method with a pointer parameter converted the pointer to `object` with `castclass object` on the raw pointer, so on CoreCLR a call with a non-null pointer usually threw `NullReferenceException` before `Invoke` ran, and Mono's JIT rejected the method with `InvalidProgramException` on every call. Pointer returns and `ref`/`out` pointer write-backs used `castclass` to the pointer type, so on CoreCLR a pointer returned by `Invoke` threw `InvalidCastException`. Pointers now cross `DispatchProxy.Invoke` as follows: - A pointer argument reaches `Invoke` in `args` as a `System.Reflection.Pointer` of the parameter's type (`Pointer.Box`), a null pointer included, as `MethodInfo.Invoke` returns a null pointer; before this change a null pointer reached `Invoke` as `null` on CoreCLR. `ref` and `in` pointer arguments pass their current value the same way. - A pointer returned by `Invoke`, or written back to `args` for a `ref`/`out` pointer parameter, may be a `Pointer`, an `IntPtr`, or `null` for a null pointer. Anything else is unboxed as an `IntPtr`, so an `int` or a `string` throws `InvalidCastException`. - The proxy reads a `Pointer`'s type with `Pointer.PointerType` (#137769) and throws `InvalidCastException` unless it is assignable to the target type; a `void*` target takes a `Pointer` of any pointer type. `MethodInfo.Invoke` takes a `Pointer` of the parameter's type, an `IntPtr` or `null` for a pointer argument, and it returns a `Pointer` for a pointer return, so an `Invoke` that forwards with `method.Invoke(target, args)` works for pointer parameters passed by value and for pointer returns. For `ref`/`out` pointer parameters, the write-back works when `Invoke` sets `args[i]` itself. Forwarding a method with a `ref`, `out` or `in` pointer parameter through `MethodInfo.Invoke` still fails. This change leaves that as it is. The proxies unbox a pointer through a C# method in `DispatchProxyGenerator`; the proxy assembly gets an `IgnoresAccessChecksTo` attribute for System.Reflection.DispatchProxy when the generator emits that call, as for internal proxied types. The IL generated for methods without pointers is unchanged, and so is the IL for function pointers. --------- Co-authored-by: Claude Opus 5.5 <[email protected]> Co-authored-by: Aaron R Robinson <[email protected]>
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Code outside CoreLib that receives a
System.Reflection.Pointer(fromFieldInfo.GetValue,MethodBase.Invoke,Delegate.DynamicInvokeorPointer.Box) can read its address only throughPointer.Unboxand has no public way to read its pointer type.DispatchProxyneeds that type to check aPointerthat a proxy'sInvokehands back against the target type (#136452).This adds the two properties approved in #137512:
PointerTypereturns the type thePointerstores: the type given toPointer.Box, or the declared type of the field, property or return value when reflection creates it.Valuereturns the stored pointer, asPointer.Unboxdoes.Both read the existing fields. The internal
GetPointerType()(aRuntimeType, which CoreCLR's invoke path needs) andGetPointerValue()(anIntPtr) stay; the invoke paths of all three runtimes call them.Tests
In System.Runtime.Tests,
PointerTestsgainsPointerProperties_*cases forPointer.Box, a static field andFieldInfo.GetValueDirect, and these existing tests now also assert both properties:PointerFieldGetValue,PointerPropertyGetValue,PointerMethodReturn,PointerMethodDelegateReturn[_SystemPointer],TestRefReturnOfPointer, andTestExplicitCastinIntPtrTestsandUIntPtrTests. In System.Reflection.Tests,FieldInfoTests.GetValueasserts them too, becausePointer.Equalscompares only the address. None of these compile without the change.Resolves #137512
Note
AI-generated, written at my direction and reviewed by me before posting.
Verified on Windows x64: CoreCLR and NativeAOT.