From bfb38a8c88fc07ac6b4af8547fb9e98fdf47624b Mon Sep 17 00:00:00 2001 From: Rahul Kumar Date: Mon, 17 Oct 2016 16:52:57 -0700 Subject: [PATCH] Expose apis in System.Reflection.Assembly Commit migrated from https://github.com/dotnet/coreclr/commit/153162f00bff94660b69237bcea341fb11e90926 --- src/coreclr/src/mscorlib/model.xml | 15 +++ src/coreclr/src/mscorlib/ref/mscorlib.cs | 12 +++ .../src/mscorlib/src/System.Private.CoreLib.txt | 1 + .../src/mscorlib/src/System/Reflection/Assembly.cs | 105 +++++++++++++++++++-- .../System/Runtime/Loader/AssemblyLoadContext.cs | 14 +++ 5 files changed, 137 insertions(+), 10 deletions(-) diff --git a/src/coreclr/src/mscorlib/model.xml b/src/coreclr/src/mscorlib/model.xml index 70b2597..c6072f9 100644 --- a/src/coreclr/src/mscorlib/model.xml +++ b/src/coreclr/src/mscorlib/model.xml @@ -3960,7 +3960,10 @@ + + + @@ -3968,6 +3971,7 @@ + @@ -4003,20 +4007,31 @@ + + + + + + + + + + + diff --git a/src/coreclr/src/mscorlib/ref/mscorlib.cs b/src/coreclr/src/mscorlib/ref/mscorlib.cs index 4160762..f4ed66a 100644 --- a/src/coreclr/src/mscorlib/ref/mscorlib.cs +++ b/src/coreclr/src/mscorlib/ref/mscorlib.cs @@ -7254,9 +7254,12 @@ namespace System.Reflection public virtual System.Reflection.MethodInfo EntryPoint { get { throw null; } } public virtual System.Collections.Generic.IEnumerable ExportedTypes { get { throw null; } } public virtual string FullName { get { throw null; } } + public virtual bool GlobalAssemblyCache { get { throw null; } } + public virtual Int64 HostContext { get { throw null; } } [System.Runtime.InteropServices.ComVisibleAttribute(false)] public virtual string ImageRuntimeVersion { get { throw null; } } public virtual bool IsDynamic { get { throw null; } } + public bool IsFullyTrusted { get { throw null; } } public virtual string Location { [System.Security.SecurityCriticalAttribute]get { throw null; } } [System.Runtime.InteropServices.ComVisibleAttribute(false)] public virtual System.Reflection.Module ManifestModule { get { throw null; } } @@ -7264,6 +7267,7 @@ namespace System.Reflection public virtual System.Collections.Generic.IEnumerable Modules { get { throw null; } } [System.Runtime.InteropServices.ComVisibleAttribute(false)] public virtual bool ReflectionOnly { get { throw null; } } + public virtual System.Security.SecurityRuleSet SecurityRuleSet { get { throw null; } } public object CreateInstance(string typeName) { throw null; } public object CreateInstance(string typeName, bool ignoreCase) { throw null; } public virtual object CreateInstance(String typeName, bool ignoreCase, BindingFlags bindingAttr, Binder binder, Object[] args, System.Globalization.CultureInfo culture, Object[] activationAttributes) { throw null; } @@ -7314,6 +7318,13 @@ namespace System.Reflection public static System.Reflection.Assembly Load(System.Reflection.AssemblyName assemblyRef) { throw null; } [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)][System.Security.SecuritySafeCriticalAttribute] public static System.Reflection.Assembly Load(string assemblyString) { throw null; } + public static System.Reflection.Assembly LoadFile(String path) { throw null; } + public static System.Reflection.Assembly LoadFrom(String path) { throw null; } + public static Assembly LoadFrom(string assemblyFile, byte[] hashValue, System.Configuration.Assemblies.AssemblyHashAlgorithm hashAlgorithm) { throw null; } + public System.Reflection.Module LoadModule(String moduleName, byte[] rawModule) { throw null; } + public System.Reflection.Module LoadModule(String moduleName, byte[] rawModule, byte[] rawSymbolStore) { throw null; } + [ObsoleteAttribute("This method has been deprecated. Please use Assembly.Load() instead. http://go.microsoft.com/fwlink/?linkid=14202")] + public static Assembly LoadWithPartialName(string partialName) { throw null; } [System.Security.SecuritySafeCriticalAttribute][System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)] public static Assembly ReflectionOnlyLoad(byte[] rawAssembly) { throw null; } [System.Security.SecuritySafeCriticalAttribute][System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)] @@ -7321,6 +7332,7 @@ namespace System.Reflection [System.Security.SecuritySafeCriticalAttribute][System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)] public static Assembly ReflectionOnlyLoadFrom(string assemblyFile) { throw null; } public override string ToString() { throw null; } + public static Assembly UnsafeLoadFrom(string assemblyFile) { throw null; } } [System.AttributeUsageAttribute((System.AttributeTargets)(1), Inherited=false)] [System.Runtime.InteropServices.ComVisibleAttribute(true)] diff --git a/src/coreclr/src/mscorlib/src/System.Private.CoreLib.txt b/src/coreclr/src/mscorlib/src/System.Private.CoreLib.txt index 762ee45..e0a4f13 100644 --- a/src/coreclr/src/mscorlib/src/System.Private.CoreLib.txt +++ b/src/coreclr/src/mscorlib/src/System.Private.CoreLib.txt @@ -1477,6 +1477,7 @@ NotSupported_PIAInAppxProcess = A Primary Interop Assembly is not supported in A ; Not referring to "Windows Phone" in the messages, as FEATURE_WINDOWSPHONE is defined for .NET Core as well. NotSupported_WindowsPhone = {0} is not supported. NotSupported_AssemblyLoadCodeBase = Assembly.Load with a Codebase is not supported. +NotSupported_AssemblyLoadFromHash = Assembly.LoadFrom with hashValue is not supported. #endif ; TypeLoadException diff --git a/src/coreclr/src/mscorlib/src/System/Reflection/Assembly.cs b/src/coreclr/src/mscorlib/src/System/Reflection/Assembly.cs index 479d6ca..988299f 100644 --- a/src/coreclr/src/mscorlib/src/System/Reflection/Assembly.cs +++ b/src/coreclr/src/mscorlib/src/System/Reflection/Assembly.cs @@ -37,6 +37,7 @@ namespace System.Reflection using __HResults = System.__HResults; using System.Runtime.Versioning; using System.Diagnostics.Contracts; + using System.Runtime.Loader; [Serializable] @@ -105,21 +106,24 @@ namespace System.Reflection return base.GetHashCode(); } - // Locate an assembly by the name of the file containing the manifest. #if FEATURE_CORECLR - [System.Security.SecurityCritical] // auto-generated + [System.Security.SecurityCritical] + public static Assembly LoadFrom(String assemblyFile) + { + if(assemblyFile == null) + throw new ArgumentNullException("assemblyFile"); + string fullPath = Path.GetFullPathInternal(assemblyFile); + return AssemblyLoadContext.Default.LoadFromAssemblyPath(fullPath); + } #else + // Locate an assembly by the name of the file containing the manifest. [System.Security.SecuritySafeCritical] -#endif [MethodImplAttribute(MethodImplOptions.NoInlining)] // Methods containing StackCrawlMark local var has to be marked non-inlineable public static Assembly LoadFrom(String assemblyFile) { Contract.Ensures(Contract.Result() != null); Contract.Ensures(!Contract.Result().ReflectionOnly); -#if FEATURE_WINDOWSPHONE - throw new NotSupportedException(Environment.GetResourceString("NotSupported_WindowsPhone", "Assembly.LoadFrom")); -#else StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller; return RuntimeAssembly.InternalLoadFrom( @@ -130,8 +134,8 @@ namespace System.Reflection false,// forIntrospection false,// suppressSecurityChecks ref stackMark); -#endif // FEATURE_WINDOWSPHONE } +#endif // Locate an assembly for reflection by the name of the file containing the manifest. [System.Security.SecuritySafeCritical] // auto-generated @@ -202,6 +206,9 @@ namespace System.Reflection byte[] hashValue, AssemblyHashAlgorithm hashAlgorithm) { +#if FEATURE_CORECLR + throw new NotSupportedException(Environment.GetResourceString("NotSupported_AssemblyLoadFromHash")); +#else StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller; return RuntimeAssembly.InternalLoadFrom( @@ -212,6 +219,7 @@ namespace System.Reflection false, false, ref stackMark); +#endif } #if FEATURE_CAS_POLICY @@ -234,6 +242,12 @@ namespace System.Reflection true, // suppressSecurityChecks ref stackMark); } +#elif FEATURE_CORECLR + [SecurityCritical] + public static Assembly UnsafeLoadFrom(string assemblyFile) + { + return LoadFrom(assemblyFile); + } #endif // FEATURE_CAS_POLICY // Locate an assembly by the long form of the assembly name. @@ -383,6 +397,16 @@ namespace System.Reflection StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller; return RuntimeAssembly.LoadWithPartialNameInternal(partialName, securityEvidence, ref stackMark); } +#elif FEATURE_CORECLR + [System.Security.SecuritySafeCritical] // auto-generated + [Obsolete("This method has been deprecated. Please use Assembly.Load() instead. http://go.microsoft.com/fwlink/?linkid=14202")] + public static Assembly LoadWithPartialName(String partialName) + { + if(partialName == null) + throw new ArgumentNullException("partialName"); + return Load(partialName); + } + #endif // FEATURE_FUSION // Loads the assembly with a COFF based IMAGE containing @@ -401,6 +425,9 @@ namespace System.Reflection AppDomain.CheckLoadByteArraySupported(); +#if FEATURE_CORECLR + return Load(rawAssembly, null); +#else StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller; return RuntimeAssembly.nLoadImage( rawAssembly, @@ -409,6 +436,7 @@ namespace System.Reflection ref stackMark, false, // fIntrospection SecurityContextSource.CurrentAssembly); +#endif } // Loads the assembly for reflection with a COFF based IMAGE containing @@ -451,6 +479,14 @@ namespace System.Reflection AppDomain.CheckLoadByteArraySupported(); +#if FEATURE_CORECLR + if(rawAssembly == null) + throw new ArgumentNullException("rawAssembly"); + AssemblyLoadContext alc = new FileLoadAssemblyLoadContext(); + MemoryStream assemblyStream = new MemoryStream(rawAssembly); + MemoryStream symbolStream = (rawSymbolStore==null)?new MemoryStream(rawSymbolStore):null; + return alc.LoadFromStream(assemblyStream, symbolStream); +#else StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller; return RuntimeAssembly.nLoadImage( rawAssembly, @@ -459,6 +495,7 @@ namespace System.Reflection ref stackMark, false, // fIntrospection SecurityContextSource.CurrentAssembly); +#endif } // Load an assembly from a byte array, controlling where the grant set of this assembly is @@ -527,6 +564,10 @@ namespace System.Reflection } #endif // FEATURE_CAS_POLICY +#if FEATURE_CORECLR + private static Dictionary s_loadfile = new Dictionary(); +#endif + [System.Security.SecuritySafeCritical] // auto-generated public static Assembly LoadFile(String path) { @@ -536,8 +577,31 @@ namespace System.Reflection AppDomain.CheckLoadFileSupported(); +#if FEATURE_CORECLR + Assembly result = null; + if(path == null) + throw new ArgumentNullException("path"); + + if (Path.IsRelative(path)) + { + throw new ArgumentException(Environment.GetResourceString("Argument_AbsolutePathRequired"), "path"); + } + + string normalizedPath = Path.GetFullPathInternal(path); + + lock(s_loadfile) + { + if(s_loadfile.TryGetValue(normalizedPath, out result)) + return result; + AssemblyLoadContext alc = new FileLoadAssemblyLoadContext(); + result = alc.LoadFromAssemblyPath(normalizedPath); + s_loadfile.Add(normalizedPath, result); + } + return result; +#else new FileIOPermission(FileIOPermissionAccess.PathDiscovery | FileIOPermissionAccess.Read, path).Demand(); return RuntimeAssembly.nLoadFile(path, null); +#endif } #if FEATURE_CAS_POLICY @@ -824,7 +888,23 @@ namespace System.Reflection throw new NotImplementedException(); } } +#elif FEATURE_CORECLR + public bool IsFullyTrusted + { + [SecuritySafeCritical] + get + { + return true; + } + } + public virtual SecurityRuleSet SecurityRuleSet + { + get + { + return SecurityRuleSet.None; + } + } #endif // FEATURE_CAS_POLICY // ISerializable implementation @@ -890,8 +970,6 @@ namespace System.Reflection } } -#if FEATURE_MULTIMODULE_ASSEMBLIES - public Module LoadModule(String moduleName, byte[] rawModule) { @@ -904,7 +982,6 @@ namespace System.Reflection { throw new NotImplementedException(); } -#endif //FEATURE_MULTIMODULE_ASSEMBLIES // // Locates a type from this assembly and creates an instance of it using @@ -2270,7 +2347,11 @@ namespace System.Reflection [System.Security.SecuritySafeCritical] // auto-generated get { +#if FEATURE_CORECLR + return false; +#else return IsGlobalAssemblyCache(GetNativeHandle()); +#endif } } @@ -2284,7 +2365,11 @@ namespace System.Reflection [System.Security.SecuritySafeCritical] // auto-generated get { +#if FEATURE_CORECLR + return 0; +#else return GetHostContext(GetNativeHandle()); +#endif } } diff --git a/src/coreclr/src/mscorlib/src/System/Runtime/Loader/AssemblyLoadContext.cs b/src/coreclr/src/mscorlib/src/System/Runtime/Loader/AssemblyLoadContext.cs index 7552986..183d5a3 100644 --- a/src/coreclr/src/mscorlib/src/System/Runtime/Loader/AssemblyLoadContext.cs +++ b/src/coreclr/src/mscorlib/src/System/Runtime/Loader/AssemblyLoadContext.cs @@ -476,6 +476,20 @@ namespace System.Runtime.Loader return null; } } + + [System.Security.SecuritySafeCritical] + internal class FileLoadAssemblyLoadContext : AssemblyLoadContext + { + internal FileLoadAssemblyLoadContext() : base(false) + { + } + + [System.Security.SecuritySafeCritical] + protected override Assembly Load(AssemblyName assemblyName) + { + return null; + } + } } #endif // FEATURE_HOST_ASSEMBLY_RESOLVER -- 2.7.4