Skip to content

Add Pointer.PointerType and Pointer.Value - #137769

Merged
jkotas merged 2 commits into
dotnet:mainfrom
caraioniurie47:issue-137512
Nov 18, 2026
Merged

jkotas merged 2 commits into
dotnet:mainfrom
caraioniurie47:issue-137512

Conversation

@caraioniurie47

Copy link
Copy Markdown
Contributor

Code outside CoreLib that receives a System.Reflection.Pointer (from FieldInfo.GetValue, MethodBase.Invoke, Delegate.DynamicInvoke or Pointer.Box) can read its address only through Pointer.Unbox and has no public way to read its pointer type. DispatchProxy needs that type to check a Pointer that a proxy's Invoke hands back against the target type (#136452).

This adds the two properties approved in #137512:

  • PointerType returns the type the Pointer stores: the type given to Pointer.Box, or the declared type of the field, property or return value when reflection creates it.
  • Value returns the stored pointer, as Pointer.Unbox does.

Both read the existing fields. The internal GetPointerType() (a RuntimeType, which CoreCLR's invoke path needs) and GetPointerValue() (an IntPtr) stay; the invoke paths of all three runtimes call them.

Tests

In System.Runtime.Tests, PointerTests gains PointerProperties_* cases for Pointer.Box, a static field and FieldInfo.GetValueDirect, and these existing tests now also assert both properties: PointerFieldGetValue, PointerPropertyGetValue, PointerMethodReturn, PointerMethodDelegateReturn[_SystemPointer], TestRefReturnOfPointer, and TestExplicitCast in IntPtrTests and UIntPtrTests. In System.Reflection.Tests, FieldInfoTests.GetValue asserts them too, because Pointer.Equals compares 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.

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]>
@dotnet-policy-service dotnet-policy-service Bot added the community-contribution Indicates that the PR has been added by a community member label Nov 17, 2026
@azure-pipelines

Copy link
Copy Markdown
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.

@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Tagging subscribers to this area: @steveisok, @dotnet/area-system-reflection
See info in area-owners.md if you want to be subscribed.

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]>

@jkotas jkotas left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks

@jkotas
jkotas merged commit ee6cc77 into dotnet:main Nov 18, 2026
104 of 106 checks passed
@caraioniurie47
caraioniurie47 deleted the issue-137512 branch November 18, 2026 14:53
@jkotas
jkotas requested a balanced review from Copilot November 18, 2026 14:53

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 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

@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 12.0-preview1 milestone Nov 19, 2026
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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-System.Reflection community-contribution Indicates that the PR has been added by a community member

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[API Proposal]: Expose the type and value stored in a System.Reflection.Pointer

3 participants