Skip to content

Commit 4810ba1

Browse files
Fix mismatch between boxed/unboxed method entrypoints (#99443)
Fixes #99198.
1 parent 2a17669 commit 4810ba1

File tree

12 files changed

+111
-62
lines changed

12 files changed

+111
-62
lines changed

src/coreclr/nativeaot/System.Private.CoreLib/src/Internal/Runtime/CompilerServices/FunctionPointerOps.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
using System.Collections.Generic;
66
using System.Runtime.InteropServices;
77

8-
using Internal.Runtime.Augments;
8+
using Debug = System.Diagnostics.Debug;
99

1010
namespace Internal.Runtime.CompilerServices
1111
{
@@ -56,6 +56,8 @@ public override int GetHashCode()
5656

5757
public static unsafe IntPtr GetGenericMethodFunctionPointer(IntPtr canonFunctionPointer, IntPtr instantiationArgument)
5858
{
59+
Debug.Assert(canonFunctionPointer != IntPtr.Zero);
60+
5961
if (instantiationArgument == IntPtr.Zero)
6062
return canonFunctionPointer;
6163

@@ -79,7 +81,7 @@ public static unsafe IntPtr GetGenericMethodFunctionPointer(IntPtr canonFunction
7981
// Generate new chunk if existing chunks are insufficient
8082
if (s_genericFunctionPointerCollection.Count <= newChunkIndex)
8183
{
82-
System.Diagnostics.Debug.Assert(newSubChunkIndex == 0);
84+
Debug.Assert(newSubChunkIndex == 0);
8385

8486
// New generic descriptors are allocated on the native heap and not tracked in the GC.
8587
IntPtr pNewMem = (IntPtr)NativeMemory.Alloc(c_genericDictionaryChunkSize, (nuint)sizeof(GenericMethodDescriptor));
@@ -100,8 +102,8 @@ public static unsafe IntPtr GetGenericMethodFunctionPointer(IntPtr canonFunction
100102
uint subChunkIndex = index % c_genericDictionaryChunkSize;
101103
GenericMethodDescriptor* genericFunctionPointer = &((GenericMethodDescriptor*)s_genericFunctionPointerCollection[chunkIndex])[subChunkIndex];
102104

103-
System.Diagnostics.Debug.Assert(canonFunctionPointer == genericFunctionPointer->MethodFunctionPointer);
104-
System.Diagnostics.Debug.Assert(instantiationArgument == genericFunctionPointer->InstantiationArgument);
105+
Debug.Assert(canonFunctionPointer == genericFunctionPointer->MethodFunctionPointer);
106+
Debug.Assert(instantiationArgument == genericFunctionPointer->InstantiationArgument);
105107

106108
return (IntPtr)((byte*)genericFunctionPointer + FatFunctionPointerOffset);
107109
}

src/coreclr/nativeaot/System.Private.TypeLoader/src/Internal/Runtime/TypeLoader/NativeLayoutInfoLoadContext.cs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -193,18 +193,16 @@ internal MethodDesc GetMethod(ref NativeParser parser, out RuntimeSignature meth
193193
{
194194
TypeDesc[] typeArguments = GetTypeSequence(ref parser);
195195
Debug.Assert(typeArguments.Length > 0);
196-
retVal = this._typeSystemContext.ResolveGenericMethodInstantiation(unboxingStub, containingType, nameAndSignature, new Instantiation(typeArguments), functionPointer, (flags & MethodFlags.FunctionPointerIsUSG) != 0);
196+
retVal = this._typeSystemContext.ResolveGenericMethodInstantiation(unboxingStub, containingType, nameAndSignature, new Instantiation(typeArguments));
197197
}
198198
else
199199
{
200-
retVal = this._typeSystemContext.ResolveRuntimeMethod(unboxingStub, containingType, nameAndSignature, functionPointer, (flags & MethodFlags.FunctionPointerIsUSG) != 0);
200+
retVal = this._typeSystemContext.ResolveRuntimeMethod(unboxingStub, containingType, nameAndSignature);
201201
}
202202

203-
if ((flags & MethodFlags.FunctionPointerIsUSG) != 0)
203+
if ((flags & MethodFlags.HasFunctionPointer) != 0)
204204
{
205-
// TODO, consider a change such that if a USG function pointer is passed in, but we have
206-
// a way to get a non-usg pointer, that may be preferable
207-
Debug.Assert(retVal.UsgFunctionPointer != IntPtr.Zero);
205+
retVal.SetFunctionPointer(functionPointer);
208206
}
209207

210208
return retVal;

src/coreclr/nativeaot/System.Private.TypeLoader/src/Internal/Runtime/TypeLoader/TypeBuilder.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ internal void ParseNativeLayoutInfo(InstantiatedMethod method)
300300
if (method.UnboxingStub)
301301
{
302302
// Strip unboxing stub, note the first parameter which is false
303-
nonTemplateMethod = (InstantiatedMethod)method.Context.ResolveGenericMethodInstantiation(false, (DefType)method.OwningType, method.NameAndSignature, method.Instantiation, IntPtr.Zero, false);
303+
nonTemplateMethod = (InstantiatedMethod)method.Context.ResolveGenericMethodInstantiation(false, (DefType)method.OwningType, method.NameAndSignature, method.Instantiation);
304304
}
305305

306306
uint nativeLayoutInfoToken;
@@ -311,9 +311,11 @@ internal void ParseNativeLayoutInfo(InstantiatedMethod method)
311311
throw new MissingTemplateException();
312312
}
313313

314-
if (templateMethod.FunctionPointer != IntPtr.Zero)
314+
// We might have a mismatch between unboxing/non-unboxing variants so only remember it for static methods
315+
if (TypeLoaderEnvironment.IsStaticMethodSignature(templateMethod.NameAndSignature)
316+
&& templateMethod.FunctionPointer != IntPtr.Zero)
315317
{
316-
nonTemplateMethod.SetFunctionPointer(templateMethod.FunctionPointer, isFunctionPointerUSG: false);
318+
nonTemplateMethod.SetFunctionPointer(templateMethod.FunctionPointer);
317319
}
318320

319321
// Ensure that if this method is non-shareable from a normal canonical perspective, then

src/coreclr/nativeaot/System.Private.TypeLoader/src/Internal/Runtime/TypeLoader/TypeLoaderEnvironment.ConstructedGenericMethodsLookup.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ internal override bool MatchParsedEntry(ref NativeParser entryParser, ref Extern
153153

154154
DefType parsedDeclaringType = context.ResolveRuntimeTypeHandle(parsedDeclaringTypeHandle) as DefType;
155155
Instantiation parsedArgs = context.ResolveRuntimeTypeHandles(parsedArgsHandles);
156-
InstantiatedMethod parsedGenericMethod = (InstantiatedMethod)context.ResolveGenericMethodInstantiation(false, parsedDeclaringType, nameAndSignature, parsedArgs, IntPtr.Zero, false);
156+
InstantiatedMethod parsedGenericMethod = (InstantiatedMethod)context.ResolveGenericMethodInstantiation(false, parsedDeclaringType, nameAndSignature, parsedArgs);
157157

158158
return parsedGenericMethod == _methodToLookup;
159159
}
@@ -164,7 +164,7 @@ internal override bool MatchGenericMethodEntry(GenericMethodEntry entry)
164164

165165
DefType parsedDeclaringType = context.ResolveRuntimeTypeHandle(entry._declaringTypeHandle) as DefType;
166166
Instantiation parsedArgs = context.ResolveRuntimeTypeHandles(entry._genericMethodArgumentHandles);
167-
InstantiatedMethod parsedGenericMethod = (InstantiatedMethod)context.ResolveGenericMethodInstantiation(false, parsedDeclaringType, entry._methodNameAndSignature, parsedArgs, IntPtr.Zero, false);
167+
InstantiatedMethod parsedGenericMethod = (InstantiatedMethod)context.ResolveGenericMethodInstantiation(false, parsedDeclaringType, entry._methodNameAndSignature, parsedArgs);
168168

169169
return parsedGenericMethod == _methodToLookup;
170170
}
@@ -267,9 +267,7 @@ public bool TryGetGenericVirtualMethodPointer(InstantiatedMethod method, out Int
267267
return false;
268268
}
269269

270-
methodPointer = templateMethod.IsCanonicalMethod(CanonicalFormKind.Universal) ?
271-
templateMethod.UsgFunctionPointer :
272-
templateMethod.FunctionPointer;
270+
methodPointer = templateMethod.FunctionPointer;
273271

274272
if (!TryLookupGenericMethodDictionary(new MethodDescBasedGenericMethodLookup(method), out dictionaryPointer))
275273
{

src/coreclr/nativeaot/System.Private.TypeLoader/src/Internal/Runtime/TypeLoader/TypeLoaderEnvironment.GVMResolution.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ private static InstantiatedMethod FindMatchingInterfaceSlot(NativeFormatModuleIn
309309
Debug.Assert(interfaceImplType != null);
310310
}
311311

312-
return (InstantiatedMethod)context.ResolveGenericMethodInstantiation(false, interfaceImplType, targetMethodNameAndSignature, slotMethod.Instantiation, IntPtr.Zero, false);
312+
return (InstantiatedMethod)context.ResolveGenericMethodInstantiation(false, interfaceImplType, targetMethodNameAndSignature, slotMethod.Instantiation);
313313
}
314314
}
315315
}
@@ -500,7 +500,7 @@ private static InstantiatedMethod ResolveGenericVirtualMethodTarget(DefType targ
500500
Debug.Assert(targetMethodNameAndSignature != null);
501501

502502
TypeSystemContext context = slotMethod.Context;
503-
return (InstantiatedMethod)context.ResolveGenericMethodInstantiation(false, targetType, targetMethodNameAndSignature, slotMethod.Instantiation, IntPtr.Zero, false);
503+
return (InstantiatedMethod)context.ResolveGenericMethodInstantiation(false, targetType, targetMethodNameAndSignature, slotMethod.Instantiation);
504504
}
505505
}
506506

src/coreclr/nativeaot/System.Private.TypeLoader/src/Internal/Runtime/TypeLoader/TypeLoaderEnvironment.LdTokenResultLookup.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -388,10 +388,10 @@ public MethodDesc GetMethodDescForDynamicRuntimeMethodHandle(TypeSystemContext c
388388
if (genericMethodArgs != null)
389389
{
390390
Instantiation methodInst = context.ResolveRuntimeTypeHandles(genericMethodArgs);
391-
return context.ResolveGenericMethodInstantiation(unboxingStub: false, type, nameAndSignature, methodInst, default, default);
391+
return context.ResolveGenericMethodInstantiation(unboxingStub: false, type, nameAndSignature, methodInst);
392392
}
393393

394-
return context.ResolveRuntimeMethod(unboxingStub: false, type, nameAndSignature, default, default);
394+
return context.ResolveRuntimeMethod(unboxingStub: false, type, nameAndSignature);
395395
}
396396

397397
private unsafe bool TryGetStaticRuntimeMethodHandleComponents(RuntimeMethodHandle runtimeMethodHandle, out RuntimeTypeHandle declaringTypeHandle, out MethodNameAndSignature nameAndSignature, out RuntimeTypeHandle[] genericMethodArgs)

src/coreclr/nativeaot/System.Private.TypeLoader/src/Internal/Runtime/TypeLoader/TypeLoaderEnvironment.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -485,7 +485,7 @@ public bool TryGetGenericMethodDictionaryForComponents(RuntimeTypeHandle declari
485485
TypeSystemContext context = TypeSystemContextFactory.Create();
486486

487487
DefType declaringType = (DefType)context.ResolveRuntimeTypeHandle(declaringTypeHandle);
488-
InstantiatedMethod methodBeingLoaded = (InstantiatedMethod)context.ResolveGenericMethodInstantiation(false, declaringType, nameAndSignature, context.ResolveRuntimeTypeHandles(genericMethodArgHandles), IntPtr.Zero, false);
488+
InstantiatedMethod methodBeingLoaded = (InstantiatedMethod)context.ResolveGenericMethodInstantiation(false, declaringType, nameAndSignature, context.ResolveRuntimeTypeHandles(genericMethodArgHandles));
489489

490490
if (TryLookupGenericMethodDictionary(new MethodDescBasedGenericMethodLookup(methodBeingLoaded), out methodDictionary))
491491
{

src/coreclr/nativeaot/System.Private.TypeLoader/src/Internal/TypeSystem/MethodDesc.Runtime.cs

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,18 @@
66

77
using Internal.Runtime.CompilerServices;
88

9+
using Debug = System.Diagnostics.Debug;
10+
911
namespace Internal.TypeSystem
1012
{
1113
public partial class MethodDesc
1214
{
1315
private IntPtr _functionPointer;
14-
private IntPtr _usgFunctionPointer;
1516

16-
public void SetFunctionPointer(IntPtr functionPointer, bool isFunctionPointerUSG)
17+
public void SetFunctionPointer(IntPtr functionPointer)
1718
{
18-
if (isFunctionPointerUSG)
19-
_usgFunctionPointer = functionPointer;
20-
else
21-
_functionPointer = functionPointer;
19+
Debug.Assert(_functionPointer == IntPtr.Zero || _functionPointer == functionPointer);
20+
_functionPointer = functionPointer;
2221
}
2322

2423
/// <summary>
@@ -32,17 +31,6 @@ public IntPtr FunctionPointer
3231
}
3332
}
3433

35-
/// <summary>
36-
/// Pointer to function's universal shared generics code. May be IntPtr.Zero
37-
/// </summary>
38-
public IntPtr UsgFunctionPointer
39-
{
40-
get
41-
{
42-
return _usgFunctionPointer;
43-
}
44-
}
45-
4634
public abstract MethodNameAndSignature NameAndSignature { get; }
4735

4836
private bool? _isNonSharableCache;

src/coreclr/nativeaot/System.Private.TypeLoader/src/Internal/TypeSystem/RuntimeMethodDesc.Canon.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public override MethodDesc GetCanonMethodTarget(CanonicalFormKind kind)
2323
if (canonicalizedTypeOfTargetMethod == OwningType)
2424
return this;
2525

26-
return Context.ResolveRuntimeMethod(this.UnboxingStub, canonicalizedTypeOfTargetMethod, this.NameAndSignature, IntPtr.Zero, false);
26+
return Context.ResolveRuntimeMethod(this.UnboxingStub, canonicalizedTypeOfTargetMethod, this.NameAndSignature);
2727
}
2828
}
2929
}

src/coreclr/nativeaot/System.Private.TypeLoader/src/Internal/TypeSystem/RuntimeMethodDesc.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ public override MethodDesc GetTypicalMethodDefinition()
117117
}
118118

119119
// Otherwise, find its equivalent on the type definition of the owning type
120-
return Context.ResolveRuntimeMethod(UnboxingStub, (DefType)owningTypeDefinition, _nameAndSignature, IntPtr.Zero, false);
120+
return Context.ResolveRuntimeMethod(UnboxingStub, (DefType)owningTypeDefinition, _nameAndSignature);
121121
}
122122

123123
public override MethodDesc InstantiateSignature(Instantiation typeInstantiation, Instantiation methodInstantiation)
@@ -127,7 +127,7 @@ public override MethodDesc InstantiateSignature(Instantiation typeInstantiation,
127127
TypeDesc owningType = method.OwningType;
128128
TypeDesc instantiatedOwningType = owningType.InstantiateSignature(typeInstantiation, methodInstantiation);
129129
if (owningType != instantiatedOwningType)
130-
method = instantiatedOwningType.Context.ResolveRuntimeMethod(UnboxingStub, (DefType)instantiatedOwningType, _nameAndSignature, IntPtr.Zero, false);
130+
method = instantiatedOwningType.Context.ResolveRuntimeMethod(UnboxingStub, (DefType)instantiatedOwningType, _nameAndSignature);
131131

132132
Instantiation instantiation = method.Instantiation;
133133
TypeDesc[] clone = null;

0 commit comments

Comments
 (0)