Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[NativeAot] Null ref return is wrapped in TargetInvocationException #69838

Merged
merged 2 commits into from
May 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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 @@ -1123,9 +1123,9 @@ private static unsafe void InvokeTarget(void* allocatedStackBuffer, ref CallConv
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();
LakshanF marked this conversation as resolved.
Show resolved Hide resolved
// Unreachable
returnValue = null;
}
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
2 changes: 1 addition & 1 deletion src/tests/nativeaot/SmokeTests/Reflection/Reflection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1215,7 +1215,7 @@ 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));
}

public static unsafe void TestByRefLikeRefReturn()
Expand Down