// Returns the load context in which the specified assembly has been loaded public static AssemblyLoadContext?GetLoadContext(Assembly assembly) { if (assembly == null) { throw new ArgumentNullException(nameof(assembly)); } AssemblyLoadContext?loadContextForAssembly = null; RuntimeAssembly?rtAsm = assembly as RuntimeAssembly; // We only support looking up load context for runtime assemblies. if (rtAsm != null) { RuntimeAssembly runtimeAssembly = rtAsm; IntPtr ptrAssemblyLoadContext = GetLoadContextForAssembly(JitHelpers.GetQCallAssemblyOnStack(ref runtimeAssembly)); if (ptrAssemblyLoadContext == IntPtr.Zero) { // If the load context is returned null, then the assembly was bound using the TPA binder // and we shall return reference to the active "Default" binder - which could be the TPA binder // or an overridden CLRPrivBinderAssemblyLoadContext instance. loadContextForAssembly = AssemblyLoadContext.Default; } else { loadContextForAssembly = (AssemblyLoadContext)(GCHandle.FromIntPtr(ptrAssemblyLoadContext).Target) !; } } return(loadContextForAssembly); }
private static RuntimeAssembly?InvokeResolveEvent(ResolveEventHandler?eventHandler, RuntimeAssembly assembly, string name) { if (eventHandler == null) { return(null); } var args = new ResolveEventArgs(name, assembly); foreach (ResolveEventHandler handler in eventHandler.GetInvocationList()) { Assembly?asm = handler(AppDomain.CurrentDomain, args); #if CORECLR if (eventHandler == AssemblyResolve && AssemblyLoadContext.IsTracingEnabled()) { AssemblyLoadContext.TraceAssemblyResolveHandlerInvoked( name, handler.Method.Name, asm?.FullName, asm != null && !asm.IsDynamic ? asm.Location : null); } #endif // CORECLR RuntimeAssembly?ret = GetRuntimeAssembly(asm); if (ret != null) { return(ret); } } return(null); }
internal static RuntimeAssembly GetExecutingAssembly(ref StackCrawlMark stackMark) { RuntimeAssembly?retAssembly = null; GetExecutingAssemblyNative(new StackCrawlMarkHandle(ref stackMark), ObjectHandleOnStack.Create(ref retAssembly)); return(retAssembly !); }
private static Assembly?GetEntryAssemblyInternal() { RuntimeAssembly?entryAssembly = null; GetEntryAssemblyNative(ObjectHandleOnStack.Create(ref entryAssembly)); return(entryAssembly); }
internal static RuntimeAssembly GetAssembly(RuntimeType type) { RuntimeAssembly?res = null; GetAssembly(new QCallTypeHandle(ref type), ObjectHandleOnStack.Create(ref res)); return(res !); }
private static Assembly ValidateAssemblyNameWithSimpleName(Assembly assembly, string?requestedSimpleName) { if (string.IsNullOrEmpty(requestedSimpleName)) { throw new ArgumentException(SR.ArgumentNull_AssemblyNameName); } // Get the name of the loaded assembly string?loadedSimpleName = null; // Derived type's Load implementation is expected to use one of the LoadFrom* methods to get the assembly // which is a RuntimeAssembly instance. However, since Assembly type can be used build any other artifact (e.g. AssemblyBuilder), // we need to check for RuntimeAssembly. RuntimeAssembly?rtLoadedAssembly = assembly as RuntimeAssembly; if (rtLoadedAssembly != null) { loadedSimpleName = rtLoadedAssembly.GetSimpleName(); } // The simple names should match at the very least if (string.IsNullOrEmpty(loadedSimpleName) || !requestedSimpleName.Equals(loadedSimpleName, StringComparison.InvariantCultureIgnoreCase)) { throw new InvalidOperationException(SR.Argument_CustomAssemblyLoadContextRequestedNameMismatch); } return(assembly); }
internal static RuntimeAssembly GetExecutingAssembly(ref StackCrawlMark stackMark) { RuntimeAssembly?retAssembly = null; GetExecutingAssemblyNative(JitHelpers.GetStackCrawlMarkHandle(ref stackMark), JitHelpers.GetObjectHandleOnStack(ref retAssembly)); return(retAssembly !); // TODO-NULLABLE: Confirm this can never be null }
private Assembly InternalLoadFromPath(string?assemblyPath, string?nativeImagePath) { RuntimeAssembly?loadedAssembly = null; LoadFromPath(_nativeAssemblyLoadContext, assemblyPath, nativeImagePath, ObjectHandleOnStack.Create(ref loadedAssembly)); return(loadedAssembly !); }
[System.Security.DynamicSecurityMethod] // Methods containing StackCrawlMark local var has to be marked DynamicSecurityMethod internal Assembly?InternalGetSatelliteAssembly(CultureInfo culture, Version?version, bool throwOnFileNotFound) { var an = new AssemblyName(); an.SetPublicKey(GetPublicKey()); an.Flags = GetFlags() | AssemblyNameFlags.PublicKey; an.Version = version ?? GetVersion(); an.CultureInfo = culture; an.Name = GetSimpleName() + ".resources"; // This stack crawl mark is never used because the requesting assembly is explicitly specified, // so the value could be anything. StackCrawlMark unused = default; RuntimeAssembly?retAssembly = InternalLoad(an, this, ref unused, throwOnFileNotFound); if (retAssembly == this) { retAssembly = null; } if (retAssembly == null && throwOnFileNotFound) { throw new FileNotFoundException(SR.Format(culture, SR.IO_FileNotFound_FileName, an.Name)); } return(retAssembly); }
internal static RuntimeAssembly GetExecutingAssembly(ref StackCrawlMark stackMark) { RuntimeAssembly?retAssembly = null; GetExecutingAssemblyNative(JitHelpers.GetStackCrawlMarkHandle(ref stackMark), JitHelpers.GetObjectHandleOnStack(ref retAssembly)); return(retAssembly); }
private static unsafe RuntimeAssembly CreateDynamicAssembly(AssemblyLoadContext assemblyLoadContext, AssemblyName name, AssemblyBuilderAccess access) { RuntimeAssembly?retAssembly = null; byte[]? publicKey = name.GetPublicKey(); fixed(char *pName = name.Name) fixed(char *pCultureName = name.CultureName) fixed(byte *pPublicKey = publicKey) { NativeAssemblyNameParts nameParts = default; nameParts._flags = name.RawFlags; nameParts._pName = pName; nameParts._pCultureName = pCultureName; nameParts._pPublicKeyOrToken = pPublicKey; nameParts._cbPublicKeyOrToken = (publicKey != null) ? publicKey.Length : 0; nameParts.SetVersion(name.Version, defaultValue: 0); #pragma warning disable SYSLIB0037 // AssemblyName.HashAlgorithm is obsolete CreateDynamicAssembly(ObjectHandleOnStack.Create(ref assemblyLoadContext), &nameParts, name.HashAlgorithm, access, ObjectHandleOnStack.Create(ref retAssembly)); #pragma warning restore SYSLIB0037 } return(retAssembly !); }
internal static RuntimeAssembly GetExecutingAssembly(ref StackCrawlMark stackMark) { RuntimeAssembly?retAssembly = null; GetExecutingAssemblyNative(JitHelpers.GetStackCrawlMarkHandle(ref stackMark), JitHelpers.GetObjectHandleOnStack(ref retAssembly)); return(retAssembly !); // TODO-NULLABLE: Remove ! when nullable attributes are respected }
// Returns the load context in which the specified assembly has been loaded public static AssemblyLoadContext?GetLoadContext(Assembly assembly) { ArgumentNullException.ThrowIfNull(assembly); RuntimeAssembly?rtAsm = GetRuntimeAssembly(assembly); // We only support looking up load context for runtime assemblies. AssemblyLoadContext?loadContextForAssembly = null; if (rtAsm != null) { RuntimeAssembly runtimeAssembly = rtAsm; IntPtr ptrAssemblyLoadContext = GetLoadContextForAssembly(new QCallAssembly(ref runtimeAssembly)); if (ptrAssemblyLoadContext == IntPtr.Zero) { // If the load context is returned null, then the assembly was bound using the TPA binder // and we shall return reference to the "Default" binder. loadContextForAssembly = AssemblyLoadContext.Default; } else { loadContextForAssembly = (AssemblyLoadContext)(GCHandle.FromIntPtr(ptrAssemblyLoadContext).Target) !; } } return(loadContextForAssembly); }
public static Assembly?GetEntryAssembly() { if (s_forceNullEntryPoint) { return(null); } RuntimeAssembly?entryAssembly = null; GetEntryAssemblyNative(JitHelpers.GetObjectHandleOnStack(ref entryAssembly)); return(entryAssembly); }
internal AssemblyBuilder(AssemblyName name, AssemblyBuilderAccess access, ref StackCrawlMark stackMark, AssemblyLoadContext?assemblyLoadContext, IEnumerable <CustomAttributeBuilder>?unsafeAssemblyAttributes) { if (name == null) { throw new ArgumentNullException(nameof(name)); } if (access != AssemblyBuilderAccess.Run && access != AssemblyBuilderAccess.RunAndCollect) { throw new ArgumentException(SR.Format(SR.Arg_EnumIllegalVal, (int)access), nameof(access)); } // Clone the name in case the caller modifies it underneath us. name = (AssemblyName)name.Clone(); // Scan the assembly level attributes for any attributes which modify how we create the // assembly. Currently, we look for any attribute which modifies the security transparency // of the assembly. List <CustomAttributeBuilder>?assemblyAttributes = null; if (unsafeAssemblyAttributes != null) { // Create a copy to ensure that it cannot be modified from another thread // as it is used further below. assemblyAttributes = new List <CustomAttributeBuilder>(unsafeAssemblyAttributes); } RuntimeAssembly?retAssembly = null; CreateDynamicAssembly(ObjectHandleOnStack.Create(ref name), new StackCrawlMarkHandle(ref stackMark), (int)access, ObjectHandleOnStack.Create(ref assemblyLoadContext), ObjectHandleOnStack.Create(ref retAssembly)); _internalAssembly = retAssembly !; _assemblyData = new AssemblyBuilderData(access); // Make sure that ManifestModule is properly initialized // We need to do this before setting any CustomAttribute InitManifestModule(); if (assemblyAttributes != null) { foreach (CustomAttributeBuilder assemblyAttribute in assemblyAttributes) { SetCustomAttribute(assemblyAttribute); } } }
internal unsafe Assembly InternalLoad(ReadOnlySpan <byte> arrAssembly, ReadOnlySpan <byte> arrSymbols) { RuntimeAssembly?loadedAssembly = null; fixed(byte *ptrAssembly = arrAssembly, ptrSymbols = arrSymbols) { LoadFromStream(_nativeAssemblyLoadContext, new IntPtr(ptrAssembly), arrAssembly.Length, new IntPtr(ptrSymbols), arrSymbols.Length, ObjectHandleOnStack.Create(ref loadedAssembly)); } return(loadedAssembly !); }
internal AssemblyBuilder(AssemblyName name, AssemblyBuilderAccess access, Assembly?callingAssembly, AssemblyLoadContext?assemblyLoadContext, IEnumerable <CustomAttributeBuilder>?assemblyAttributes) { ArgumentNullException.ThrowIfNull(name); if (access != AssemblyBuilderAccess.Run && access != AssemblyBuilderAccess.RunAndCollect) { throw new ArgumentException(SR.Format(SR.Arg_EnumIllegalVal, (int)access), nameof(access)); } if (callingAssembly == null) { // Called either from interop or async delegate invocation. Rejecting because we don't // know how to set the correct context of the new dynamic assembly. throw new InvalidOperationException(); } if (assemblyLoadContext == null) { assemblyLoadContext = AssemblyLoadContext.GetLoadContext(callingAssembly); } // Clone the name in case the caller modifies it underneath us. name = (AssemblyName)name.Clone(); RuntimeAssembly?retAssembly = null; CreateDynamicAssembly(ObjectHandleOnStack.Create(ref name), (int)access, ObjectHandleOnStack.Create(ref assemblyLoadContext), ObjectHandleOnStack.Create(ref retAssembly)); _internalAssembly = retAssembly !; _access = access; // Make sure that ManifestModule is properly initialized // We need to do this before setting any CustomAttribute // Note that this ModuleBuilder cannot be used for RefEmit yet // because it hasn't been initialized. // However, it can be used to set the custom attribute on the Assembly _manifestModuleBuilder = new ModuleBuilder(this, (RuntimeModule)InternalAssembly.ManifestModule); if (assemblyAttributes != null) { foreach (CustomAttributeBuilder assemblyAttribute in assemblyAttributes) { SetCustomAttribute(assemblyAttribute); } } }
public override ManifestResourceInfo?GetManifestResourceInfo(string resourceName) { RuntimeAssembly?retAssembly = null; string? fileName = null; int location = GetManifestResourceInfo(GetNativeHandle(), resourceName, JitHelpers.GetObjectHandleOnStack(ref retAssembly), JitHelpers.GetStringHandleOnStack(ref fileName)); if (location == -1) { return(null); } return(new ManifestResourceInfo(retAssembly !, fileName !, (ResourceLocation)location)); }
internal static RuntimeAssembly InternalLoad(AssemblyName assemblyName, RuntimeAssembly?requestingAssembly, ref StackCrawlMark stackMark, bool throwOnFileNotFound, AssemblyLoadContext?assemblyLoadContext = null) { RuntimeAssembly?retAssembly = null; InternalLoad(ObjectHandleOnStack.Create(ref assemblyName), ObjectHandleOnStack.Create(ref requestingAssembly), new StackCrawlMarkHandle(ref stackMark), throwOnFileNotFound, ObjectHandleOnStack.Create(ref assemblyLoadContext), ObjectHandleOnStack.Create(ref retAssembly)); return(retAssembly !); }
/// <summary> /// Load a module that has already been loaded into memory by the OS loader as a .NET assembly. /// </summary> internal Assembly LoadFromInMemoryModule(IntPtr moduleHandle) { ArgumentNullException.ThrowIfNull(moduleHandle); lock (_unloadLock) { VerifyIsAlive(); RuntimeAssembly?loadedAssembly = null; LoadFromInMemoryModuleInternal( _nativeAssemblyLoadContext, moduleHandle, ObjectHandleOnStack.Create(ref loadedAssembly)); return(loadedAssembly !); } }
internal static unsafe RuntimeAssembly InternalLoad(AssemblyName assemblyName, ref StackCrawlMark stackMark, AssemblyLoadContext?assemblyLoadContext = null, RuntimeAssembly?requestingAssembly = null, bool throwOnFileNotFound = true) { RuntimeAssembly?retAssembly = null; AssemblyNameFlags flags = assemblyName.RawFlags; // Note that we prefer to take a public key token if present, // even if flags indicate a full public key byte[]? publicKeyOrToken; if ((publicKeyOrToken = assemblyName.RawPublicKeyToken) != null) { flags |= ~AssemblyNameFlags.PublicKey; } else if ((publicKeyOrToken = assemblyName.RawPublicKey) != null) { flags |= AssemblyNameFlags.PublicKey; } fixed(char *pName = assemblyName.Name) fixed(char *pCultureName = assemblyName.CultureName) fixed(byte *pPublicKeyOrToken = publicKeyOrToken) { NativeAssemblyNameParts nameParts = default; nameParts._flags = flags; nameParts._pName = pName; nameParts._pCultureName = pCultureName; nameParts._pPublicKeyOrToken = pPublicKeyOrToken; nameParts._cbPublicKeyOrToken = (publicKeyOrToken != null) ? publicKeyOrToken.Length : 0; nameParts.SetVersion(assemblyName.Version, defaultValue: ushort.MaxValue); InternalLoad(&nameParts, ObjectHandleOnStack.Create(ref requestingAssembly), new StackCrawlMarkHandle(ref stackMark), throwOnFileNotFound, ObjectHandleOnStack.Create(ref assemblyLoadContext), ObjectHandleOnStack.Create(ref retAssembly)); } return(retAssembly !); }
public override ManifestResourceInfo?GetManifestResourceInfo(string resourceName) { RuntimeAssembly?retAssembly = null; string? fileName = null; RuntimeAssembly runtimeAssembly = this; int location = GetManifestResourceInfo(new QCallAssembly(ref runtimeAssembly), resourceName, ObjectHandleOnStack.Create(ref retAssembly), new StringHandleOnStack(ref fileName)); if (location == -1) { return(null); } return(new ManifestResourceInfo(retAssembly !, fileName !, (ResourceLocation)location)); }
/// <summary> /// Load a module that has already been loaded into memory by the OS loader as a .NET assembly. /// </summary> internal Assembly LoadFromInMemoryModule(IntPtr moduleHandle) { if (moduleHandle == IntPtr.Zero) { throw new ArgumentNullException(nameof(moduleHandle)); } lock (_unloadLock) { VerifyIsAlive(); RuntimeAssembly?loadedAssembly = null; LoadFromInMemoryModuleInternal( _nativeAssemblyLoadContext, moduleHandle, ObjectHandleOnStack.Create(ref loadedAssembly)); return(loadedAssembly !); } }
// Returns the load context in which the specified assembly has been loaded public static AssemblyLoadContext?GetLoadContext(Assembly assembly) { ArgumentNullException.ThrowIfNull(assembly); AssemblyLoadContext?loadContextForAssembly = null; RuntimeAssembly?rtAsm = assembly as RuntimeAssembly; // We only support looking up load context for runtime assemblies. if (rtAsm != null) { var _ = Default; // ensure the default ALC is initialized. RuntimeAssembly runtimeAssembly = rtAsm; IntPtr ptrAssemblyLoadContext = GetLoadContextForAssembly(runtimeAssembly); loadContextForAssembly = GetAssemblyLoadContext(ptrAssemblyLoadContext); } return(loadContextForAssembly); }
private static RuntimeAssembly?InvokeResolveEvent(ResolveEventHandler?eventHandler, RuntimeAssembly assembly, string name) { if (eventHandler == null) { return(null); } var args = new ResolveEventArgs(name, assembly); foreach (ResolveEventHandler handler in eventHandler.GetInvocationList()) { Assembly? asm = handler(AppDomain.CurrentDomain, args); RuntimeAssembly?ret = GetRuntimeAssembly(asm); if (ret != null) { return(ret); } } return(null); }
// Returns the load context in which the specified assembly has been loaded public static AssemblyLoadContext?GetLoadContext(Assembly assembly) { if (assembly == null) { throw new ArgumentNullException(nameof(assembly)); } AssemblyLoadContext?loadContextForAssembly = null; RuntimeAssembly?rtAsm = assembly as RuntimeAssembly; // We only support looking up load context for runtime assemblies. if (rtAsm != null) { RuntimeAssembly runtimeAssembly = rtAsm; IntPtr ptrAssemblyLoadContext = GetLoadContextForAssembly(runtimeAssembly); loadContextForAssembly = GetAssemblyLoadContext(ptrAssemblyLoadContext); } return(loadContextForAssembly); }
internal static AssemblyName CreateAssemblyName( string assemblyString, out RuntimeAssembly?assemblyFromResolveEvent) { if (assemblyString == null) { throw new ArgumentNullException(nameof(assemblyString)); } if ((assemblyString.Length == 0) || (assemblyString[0] == '\0')) { throw new ArgumentException(SR.Format_StringZeroLength); } AssemblyName an = new AssemblyName(); an.Name = assemblyString; an.nInit(out assemblyFromResolveEvent, true); return(an); }
/// <summary> /// Updates the specified assembly using the provided metadata, IL and PDB deltas. /// </summary> /// <remarks> /// Currently executing methods will continue to use the existing IL. New executions of modified methods will /// use the new IL. Different runtimes may have different limitations on what kinds of changes are supported, /// and runtimes make no guarantees as to the state of the assembly and process if the delta includes /// unsupported changes. /// </remarks> /// <param name="assembly">The assembly to update.</param> /// <param name="metadataDelta">The metadata changes to be applied.</param> /// <param name="ilDelta">The IL changes to be applied.</param> /// <param name="pdbDelta">The PDB changes to be applied.</param> /// <exception cref="ArgumentNullException">The assembly argument is null.</exception> /// <exception cref="NotSupportedException">The update could not be applied.</exception> public static void ApplyUpdate( Assembly assembly, ReadOnlySpan <byte> metadataDelta, ReadOnlySpan <byte> ilDelta, ReadOnlySpan <byte> pdbDelta ) { if (assembly == null) { throw new ArgumentNullException(nameof(assembly)); } RuntimeAssembly?runtimeAssembly = assembly as RuntimeAssembly; if (runtimeAssembly == null) { throw new ArgumentException(SR.Argument_MustBeRuntimeAssembly); } unsafe { RuntimeAssembly rtAsm = runtimeAssembly; fixed( byte *metadataDeltaPtr = metadataDelta, ilDeltaPtr = ilDelta, pdbDeltaPtr = pdbDelta ) { ApplyUpdate( new QCallAssembly(ref rtAsm), metadataDeltaPtr, metadataDelta.Length, ilDeltaPtr, ilDelta.Length, pdbDeltaPtr, pdbDelta.Length ); } } }
private static extern RuntimeAssembly nLoad(AssemblyName assemblyName, RuntimeAssembly?requestingAssembly, ref StackCrawlMark stackMark, bool throwOnFileNotFound, AssemblyLoadContext?assemblyLoadContext = null);
private static extern RuntimeAssembly nLoad(AssemblyName fileName, string?codeBase, RuntimeAssembly?assemblyContext, ref StackCrawlMark stackMark, bool throwOnFileNotFound, AssemblyLoadContext?assemblyLoadContext = null);