Skip to content

Commit

Permalink
[netcore] Fix RuntimePropertyInfo.GetValue() in FullAOT scenarios (mo…
Browse files Browse the repository at this point in the history
…no/mono#17757)

It tried to use dynamic codegen which is not available in FullAOT.
We can use the `RuntimeFeature.IsDynamicCodeSupported` to check for this, added a JIT intrinsic which returns false in FullAOT.

Used a linker friendly way to check for TypeBuilder by adding an `internal virtual IsTypeBuilder` method in the `Type` class which returns false, and overwrote it in `TypeBuilder`.

Commit migrated from mono/mono@ed2d5f6
  • Loading branch information
akoeplinger authored Nov 8, 2019
1 parent a12b99e commit 97ac31f
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 13 deletions.
12 changes: 12 additions & 0 deletions src/mono/mono/mini/intrinsics.c
Original file line number Diff line number Diff line change
Expand Up @@ -1907,6 +1907,18 @@ mini_emit_inst_for_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSign
ins->type = STACK_I4;
return ins;
}

// Return false for RuntimeFeature.IsDynamicCodeSupported and RuntimeFeature.IsDynamicCodeCompiled on FullAOT, otherwise true
if (in_corlib &&
!strcmp ("System.Runtime.CompilerServices", cmethod_klass_name_space) &&
!strcmp ("RuntimeFeature", cmethod_klass_name)) {
if (!strcmp (cmethod->name, "get_IsDynamicCodeSupported") || !strcmp (cmethod->name, "get_IsDynamicCodeCompiled")) {
EMIT_NEW_ICONST (cfg, ins, cfg->full_aot ? 0 : 1);
ins->type = STACK_I4;
return ins;
}
}

#endif

ins = mono_emit_native_types_intrinsics (cfg, cmethod, fsig, args);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,6 @@
using System.Collections;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
#if !FULL_AOT_RUNTIME
using System.Reflection.Emit;
#endif

using System.Collections.Generic;

Expand All @@ -46,11 +43,7 @@ static class CustomAttribute
static bool IsUserCattrProvider (object obj)
{
Type type = obj as Type;
#if !FULL_AOT_RUNTIME
if ((type is RuntimeType) || (RuntimeFeature.IsDynamicCodeSupported && type is TypeBuilder))
#else
if (type is RuntimeType)
#endif
if ((type is RuntimeType) || (RuntimeFeature.IsDynamicCodeSupported && type?.IsTypeBuilder () == true))
return false;
if ((obj is Type))
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1756,6 +1756,8 @@ internal override bool IsUserType {
}
}

internal override bool IsTypeBuilder () => true;

public override bool IsConstructedGenericType {
get { return false; }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -360,9 +360,8 @@ static GetterAdapter CreateGetterDelegate (MethodInfo method)

public override object GetValue (object obj, object[] index)
{
if (index == null || index.Length == 0) {
if ((index == null || index.Length == 0) && RuntimeFeature.IsDynamicCodeSupported) {
/*FIXME we should check if the number of arguments matches the expected one, otherwise the error message will be pretty criptic.*/
#if !FULL_AOT_RUNTIME
if (cached_getter == null) {
MethodInfo method = GetGetMethod (true);
if (method == null)
Expand All @@ -383,7 +382,6 @@ public override object GetValue (object obj, object[] index)
throw new TargetInvocationException (ex);
}
}
#endif
}

return GetValue (obj, BindingFlags.Default, null, index, null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,16 @@ namespace System.Runtime.CompilerServices
{
partial class RuntimeFeature
{
public static bool IsDynamicCodeSupported => true;
public static bool IsDynamicCodeCompiled => true;
public static bool IsDynamicCodeSupported
{
[Intrinsic] // the JIT/AOT compiler will change this flag to false for FullAOT scenarios, otherwise true
get { throw new NotImplementedException (); }
}

public static bool IsDynamicCodeCompiled
{
[Intrinsic] // the JIT/AOT compiler will change this flag to false for FullAOT scenarios, otherwise true
get { throw new NotImplementedException (); }
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ partial class Type

internal bool IsRuntimeImplemented () => this.UnderlyingSystemType is RuntimeType;

internal virtual bool IsTypeBuilder () => false;

public bool IsInterface {
get {
if (this is RuntimeType rt)
Expand Down

0 comments on commit 97ac31f

Please sign in to comment.