Skip to content
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 @@ -172,7 +172,7 @@ API: <xref:Orleans.Serialization.Configuration.TypeManifestOptions>, <xref:Orlea

The default serializer activator invokes a public parameterless constructor, including an explicit parameterless value-type constructor, for each new instance. For types with no public parameterless constructor, it allocates an uninitialized instance with zero-initialized fields. A registered custom activator controls construction for its target type.

On runtimes which support dynamic code, the default activator caches an emitted constructor delegate. On NativeAOT, it uses generic construction through <xref:System.Activator.CreateInstance*>. The NativeAOT compiler implements this operation using constructor and allocator intrinsics. The activator propagates the original constructor exception, preserving its identity and stack trace. Closed default activator types carry the constructor-preservation annotations required by constructor lookup and uninitialized allocation.
The default activator caches whether the type has a public parameterless constructor and uses generic construction through <xref:System.Activator.CreateInstance*> on both managed runtimes and NativeAOT. The runtime supplies the constructor and allocator implementation. Generic construction wraps constructor failures in <xref:System.Reflection.TargetInvocationException>; the activator removes that outer wrapper and propagates the original constructor exception, preserving its identity, inner exception, and stack trace. Closed default activator types carry the constructor-preservation annotations required by constructor lookup and uninitialized allocation.

## Extension points

Expand Down
38 changes: 4 additions & 34 deletions src/Orleans.Serialization/Activators/DefaultActivator.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System;
using System.Diagnostics.CodeAnalysis;
using System.Reflection;
using System.Reflection.Emit;
using System.Runtime.CompilerServices;
using System.Runtime.ExceptionServices;

Expand All @@ -14,46 +13,25 @@ internal abstract class DefaultActivator<
#endif
T> : IActivator<T>
{
private static readonly Func<T>? DefaultConstructorFunction = Init();
protected readonly Func<T>? Constructor = DefaultConstructorFunction;
#if NET5_0_OR_GREATER
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.NonPublicConstructors)]
#endif
protected readonly Type Type = typeof(T);
private static readonly bool HasDefaultConstructor = typeof(T).GetConstructor(Type.EmptyTypes) is not null;

private static Func<T>? Init()
public T Create()
{
var ctor = typeof(T).GetConstructor(Type.EmptyTypes);
if (ctor is null)
return null;

if (!RuntimeFeature.IsDynamicCodeSupported)
if (!HasDefaultConstructor)
{
return CreateInstance;
return (T)RuntimeHelpers.GetUninitializedObject(typeof(T));
}

var method = new DynamicMethod(nameof(DefaultActivator<T>), typeof(T), new[] { typeof(object) });
var il = method.GetILGenerator();
il.Emit(OpCodes.Newobj, ctor);
il.Emit(OpCodes.Ret);
return (Func<T>)method.CreateDelegate(typeof(Func<T>));
}

private static T CreateInstance()
{
try
{
return System.Activator.CreateInstance<T>();
}
catch (TargetInvocationException exception) when (exception.InnerException is { } inner)
{
// NativeAOT generic construction wraps constructor exceptions.
ExceptionDispatchInfo.Capture(inner).Throw();
throw;
}
}

public abstract T Create();
}

internal sealed class DefaultReferenceTypeActivator<
Expand All @@ -63,10 +41,6 @@ internal sealed class DefaultReferenceTypeActivator<
#endif
T> : DefaultActivator<T> where T : class
{
public override T Create()
=> Constructor is { } ctor
? ctor()
: Unsafe.As<T>(RuntimeHelpers.GetUninitializedObject(Type));
}

internal sealed class DefaultValueTypeActivator<
Expand All @@ -76,9 +50,5 @@ internal sealed class DefaultValueTypeActivator<
#endif
T> : DefaultActivator<T> where T : struct
{
public override T Create()
=> Constructor is { } ctor
? ctor()
: (T)RuntimeHelpers.GetUninitializedObject(Type);
}
}
26 changes: 24 additions & 2 deletions test/Orleans.NativeAotSmoke/Activation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,12 @@ private static void ValidateConstructorExceptions()

var invocationValueActivator = new DefaultValueTypeActivator<ThrowingInvocationValueConstructor>();
EnsureOriginalException(() => invocationValueActivator.Create(), ThrowingInvocationValueConstructor.Error);

var invocationReferenceWithoutInnerActivator = new DefaultReferenceTypeActivator<ThrowingInvocationReferenceConstructorWithoutInner>();
EnsureOriginalException(() => invocationReferenceWithoutInnerActivator.Create(), ThrowingInvocationReferenceConstructorWithoutInner.Error);

var invocationValueWithInnerActivator = new DefaultValueTypeActivator<ThrowingInvocationValueConstructorWithInner>();
EnsureOriginalException(() => invocationValueWithInnerActivator.Create(), ThrowingInvocationValueConstructorWithInner.Error);
}

private static void EnsureOriginalException(Action create, Exception expected)
Expand Down Expand Up @@ -136,15 +142,15 @@ private struct ImplicitValueConstructor(int value)

private sealed class ThrowingReferenceConstructor
{
public static readonly InvalidOperationException Error = new("Reference constructor failed.");
public static readonly InvalidOperationException Error = new("Reference constructor failed.", new ArgumentException("Constructor inner exception."));

[MethodImpl(MethodImplOptions.NoInlining)]
public ThrowingReferenceConstructor() => throw Error;
}

private struct ThrowingValueConstructor
{
public static readonly InvalidOperationException Error = new("Value constructor failed.");
public static readonly InvalidOperationException Error = new("Value constructor failed.", new ArgumentException("Constructor inner exception."));

[MethodImpl(MethodImplOptions.NoInlining)]
public ThrowingValueConstructor() => throw Error;
Expand All @@ -165,4 +171,20 @@ private struct ThrowingInvocationValueConstructor
[MethodImpl(MethodImplOptions.NoInlining)]
public ThrowingInvocationValueConstructor() => throw Error;
}

private sealed class ThrowingInvocationReferenceConstructorWithoutInner
{
public static readonly TargetInvocationException Error = new(null);

[MethodImpl(MethodImplOptions.NoInlining)]
public ThrowingInvocationReferenceConstructorWithoutInner() => throw Error;
}

private struct ThrowingInvocationValueConstructorWithInner
{
public static readonly TargetInvocationException Error = new(new InvalidOperationException("Value constructor failed."));

[MethodImpl(MethodImplOptions.NoInlining)]
public ThrowingInvocationValueConstructorWithInner() => throw Error;
}
}
46 changes: 44 additions & 2 deletions test/Orleans.Serialization.UnitTests/DefaultActivatorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ public void ReferenceType_ThrowingConstructor_PropagatesOriginalException()
var exception = Assert.Throws<InvalidOperationException>(() => activator.Create());

Assert.Same(ThrowingReferenceConstructor.Error, exception);
Assert.Same(ThrowingReferenceConstructor.Error.InnerException, exception.InnerException);
Assert.Contains(nameof(ThrowingReferenceConstructor), exception.StackTrace);
}

Expand All @@ -93,6 +94,7 @@ public void ValueType_ThrowingConstructor_PropagatesOriginalException()
var exception = Assert.Throws<InvalidOperationException>(() => activator.Create());

Assert.Same(ThrowingValueConstructor.Error, exception);
Assert.Same(ThrowingValueConstructor.Error.InnerException, exception.InnerException);
Assert.Contains(nameof(ThrowingValueConstructor), exception.StackTrace);
}

Expand Down Expand Up @@ -120,6 +122,30 @@ public void ValueType_ConstructorThrowsTargetInvocationException_PropagatesConst
Assert.Contains(nameof(ThrowingInvocationValueConstructor), exception.StackTrace);
}

[Fact]
public void ReferenceType_ConstructorThrowsTargetInvocationExceptionWithoutInner_PropagatesConstructorException()
{
var activator = new DefaultReferenceTypeActivator<ThrowingInvocationReferenceConstructorWithoutInner>();

var exception = Assert.Throws<TargetInvocationException>(() => activator.Create());

Assert.Same(ThrowingInvocationReferenceConstructorWithoutInner.Error, exception);
Assert.Null(exception.InnerException);
Assert.Contains(nameof(ThrowingInvocationReferenceConstructorWithoutInner), exception.StackTrace);
}

[Fact]
public void ValueType_ConstructorThrowsTargetInvocationExceptionWithInner_PropagatesConstructorException()
{
var activator = new DefaultValueTypeActivator<ThrowingInvocationValueConstructorWithInner>();

var exception = Assert.Throws<TargetInvocationException>(() => activator.Create());

Assert.Same(ThrowingInvocationValueConstructorWithInner.Error, exception);
Assert.Same(ThrowingInvocationValueConstructorWithInner.Error.InnerException, exception.InnerException);
Assert.Contains(nameof(ThrowingInvocationValueConstructorWithInner), exception.StackTrace);
}

[Fact]
public void CodecProvider_DefaultActivators_PreserveConstructorSelection()
{
Expand Down Expand Up @@ -178,15 +204,15 @@ private struct ImplicitValueConstructor(int value)

private sealed class ThrowingReferenceConstructor
{
public static readonly InvalidOperationException Error = new("Reference constructor failed.");
public static readonly InvalidOperationException Error = new("Reference constructor failed.", new ArgumentException("Constructor inner exception."));

[MethodImpl(MethodImplOptions.NoInlining)]
public ThrowingReferenceConstructor() => throw Error;
}

private struct ThrowingValueConstructor
{
public static readonly InvalidOperationException Error = new("Value constructor failed.");
public static readonly InvalidOperationException Error = new("Value constructor failed.", new ArgumentException("Constructor inner exception."));

[MethodImpl(MethodImplOptions.NoInlining)]
public ThrowingValueConstructor() => throw Error;
Expand All @@ -207,4 +233,20 @@ private struct ThrowingInvocationValueConstructor
[MethodImpl(MethodImplOptions.NoInlining)]
public ThrowingInvocationValueConstructor() => throw Error;
}

private sealed class ThrowingInvocationReferenceConstructorWithoutInner
{
public static readonly TargetInvocationException Error = new(null);

[MethodImpl(MethodImplOptions.NoInlining)]
public ThrowingInvocationReferenceConstructorWithoutInner() => throw Error;
}

private struct ThrowingInvocationValueConstructorWithInner
{
public static readonly TargetInvocationException Error = new(new InvalidOperationException("Value constructor failed."));

[MethodImpl(MethodImplOptions.NoInlining)]
public ThrowingInvocationValueConstructorWithInner() => throw Error;
}
}
Loading