Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -127,5 +127,10 @@ public static void ThrowArgumentOutOfRangeException()
{
throw new ArgumentOutOfRangeException();
}

public static void ThrowInvokeNullRefReturned()
{
throw new NullReferenceException(SR.NullReference_InvokeNullRefReturned);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -427,9 +427,6 @@ internal static unsafe object CallDynamicInvokeMethod(
}
}

if (result == NullByRefValueSentinel)
throw new NullReferenceException(SR.NullReference_InvokeNullRefReturned);

return result;
}
}
Expand Down Expand Up @@ -671,18 +668,5 @@ public static object DynamicInvokeParamHelperCore(ref ArgSetupState argSetupStat
}
}
}

private static volatile object _nullByRefValueSentinel;
public static object NullByRefValueSentinel
{
get
{
if (_nullByRefValueSentinel == null)
{
Interlocked.CompareExchange(ref _nullByRefValueSentinel, new object(), null);
}
return _nullByRefValueSentinel;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1119,13 +1119,11 @@ private static unsafe void InvokeTarget(void* allocatedStackBuffer, ref CallConv
RuntimeTypeHandle returnTypeRuntimeTypeHandle = thRetType.GetRuntimeTypeHandle();

// Need to box value type before returning it
object returnValue;
object returnValue = null;
if (returnType == CorElementType.ELEMENT_TYPE_BYREF && returnValueToCopy == null)
{
// This is a byref return and dereferencing it would result in a NullReferenceException.
// Set the return value to a sentinel that InvokeUtils will recognize.
// Can't throw from here or we would wrap this in a TargetInvocationException.
returnValue = InvokeUtils.NullByRefValueSentinel;
CompilerHelpers.ThrowHelpers.ThrowInvokeNullRefReturned();
}
else if (RuntimeAugments.IsUnmanagedPointerType(returnTypeRuntimeTypeHandle))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -388,9 +388,7 @@ public override MethodIL EmitIL()
//
// !if (ReturnType is ByRef)
// ByRefNull:
// pop
// call InvokeUtils.get_NullByRefValueSentinel
// ret
// throw NullReferenceException

ILCodeLabel lStaticCall = emitter.NewCodeLabel();
ILCodeLabel lProcessReturn = emitter.NewCodeLabel();
Expand Down Expand Up @@ -512,9 +510,8 @@ public override MethodIL EmitIL()
if (lByRefReturnNull != null)
{
returnCodeStream.EmitLabel(lByRefReturnNull);
returnCodeStream.Emit(ILOpcode.pop);
returnCodeStream.Emit(ILOpcode.call, emitter.NewToken(InvokeUtilsType.GetKnownMethod("get_NullByRefValueSentinel", null)));
returnCodeStream.Emit(ILOpcode.ret);
MethodDesc nullReferencedExceptionHelper = Context.GetHelperEntryPoint("ThrowHelpers", "ThrowInvokeNullRefReturned");
returnCodeStream.EmitCallThrowHelper(emitter, nullReferencedExceptionHelper);
}

return emitter.Link(this);
Expand Down
3 changes: 2 additions & 1 deletion src/tests/nativeaot/SmokeTests/Reflection/Reflection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1215,7 +1215,8 @@ public static unsafe void TestNullRefReturnOfPointer()

PropertyInfo p = typeof(TestClassIntPointer).GetProperty(nameof(TestClassIntPointer.NullRefReturningProp));
Assert.NotNull(p);
Assert.Throws<NullReferenceException>(() => p.GetValue(tc));
Assert.Throws<TargetInvocationException>(() => p.GetValue(tc));
Assert.IsType<NullReferenceException>(ex.InnerException);
}

public static unsafe void TestByRefLikeRefReturn()
Expand Down