internal ModuleBuilder GetModuleBuilder(InternalModuleBuilder module) { Debug.Assert(module != null); Debug.Assert(InternalAssembly == module.Assembly); lock (SyncRoot) { // in CoreCLR there is only one module in each dynamic assembly, the manifest module if (_manifestModuleBuilder.InternalModule == module) { return(_manifestModuleBuilder); } throw new ArgumentException(null, nameof(module)); } }
private void InitManifestModule() { InternalModuleBuilder modBuilder = (InternalModuleBuilder)GetInMemoryAssemblyModule(InternalAssembly); // 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, modBuilder); // We are only setting the name in the managed ModuleBuilderData here. // The name in the underlying metadata will be set when the // manifest module is created during CreateDynamicAssembly. // This name needs to stay in sync with that used in // Assembly::Init to call ReflectionModule::Create (in VM) _manifestModuleBuilder.Init(ManifestModuleName); _isManifestModuleUsedAsDefinedModule = false; }
public static void SwapMethodBody(Type cls, int methodtoken, IntPtr rgIL, int methodSize, int flags) { if (methodSize <= 0 || methodSize >= 4128768) { throw new ArgumentException(Environment.GetResourceString("Argument_BadSizeForData"), "methodSize"); } if (cls == (Type)null) { throw new ArgumentNullException("cls"); } Module module = cls.Module; ModuleBuilder moduleBuilder = module as ModuleBuilder; InternalModuleBuilder internalModuleBuilder = !((Module)moduleBuilder != (Module)null) ? module as InternalModuleBuilder : moduleBuilder.InternalModule; if ((Module)internalModuleBuilder == (Module)null) { throw new NotSupportedException(Environment.GetResourceString("NotSupported_NotDynamicModule")); } RuntimeType runtimeType; if (cls is TypeBuilder) { TypeBuilder typeBuilder = (TypeBuilder)cls; if (!typeBuilder.IsCreated()) { throw new NotSupportedException(Environment.GetResourceString("NotSupported_NotAllTypesAreBaked", (object)typeBuilder.Name)); } runtimeType = typeBuilder.BakedRuntimeType; } else { runtimeType = cls as RuntimeType; } if (runtimeType == (RuntimeType)null) { throw new ArgumentException(Environment.GetResourceString("Argument_MustBeRuntimeType"), "cls"); } StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller; lock (internalModuleBuilder.GetRuntimeAssembly().SyncRoot) MethodRental.SwapMethodBody(runtimeType.GetTypeHandleInternal(), methodtoken, rgIL, methodSize, flags, JitHelpers.GetStackCrawlMarkHandle(ref stackMark)); }
[MethodImplAttribute(MethodImplOptions.NoInlining)] // Methods containing StackCrawlMark local var has to be marked non-inlineable private static RuntimeModule GetDynamicMethodsModule() { if (s_anonymouslyHostedDynamicMethodsModule != null) { return(s_anonymouslyHostedDynamicMethodsModule); } lock (s_anonymouslyHostedDynamicMethodsModuleLock) { if (s_anonymouslyHostedDynamicMethodsModule != null) { return(s_anonymouslyHostedDynamicMethodsModule); } ConstructorInfo transparencyCtor = typeof(SecurityTransparentAttribute).GetConstructor(Type.EmptyTypes); CustomAttributeBuilder transparencyAttribute = new CustomAttributeBuilder(transparencyCtor, EmptyArray <Object> .Value); List <CustomAttributeBuilder> assemblyAttributes = new List <CustomAttributeBuilder>(); assemblyAttributes.Add(transparencyAttribute); AssemblyName assemblyName = new AssemblyName("Anonymously Hosted DynamicMethods Assembly"); StackCrawlMark stackMark = StackCrawlMark.LookForMe; AssemblyBuilder assembly = AssemblyBuilder.InternalDefineDynamicAssembly( assemblyName, AssemblyBuilderAccess.Run, null, null, ref stackMark, assemblyAttributes, SecurityContextSource.CurrentAssembly); AppDomain.PublishAnonymouslyHostedDynamicMethodsAssembly(assembly.GetNativeHandle()); // this always gets the internal module. s_anonymouslyHostedDynamicMethodsModule = (InternalModuleBuilder)assembly.ManifestModule; } return(s_anonymouslyHostedDynamicMethodsModule); }
internal ModuleBuilder(AssemblyBuilder assemblyBuilder, InternalModuleBuilder internalModuleBuilder) { m_internalModuleBuilder = internalModuleBuilder; m_assemblyBuilder = assemblyBuilder; }
[MethodImplAttribute(MethodImplOptions.NoInlining)] // Methods containing StackCrawlMark local var has to be marked non-inlineable private static RuntimeModule GetDynamicMethodsModule() { if (s_anonymouslyHostedDynamicMethodsModule != null) return s_anonymouslyHostedDynamicMethodsModule; lock (s_anonymouslyHostedDynamicMethodsModuleLock) { if (s_anonymouslyHostedDynamicMethodsModule != null) return s_anonymouslyHostedDynamicMethodsModule; ConstructorInfo transparencyCtor = typeof(SecurityTransparentAttribute).GetConstructor(Type.EmptyTypes); CustomAttributeBuilder transparencyAttribute = new CustomAttributeBuilder(transparencyCtor, EmptyArray<Object>.Value); List<CustomAttributeBuilder> assemblyAttributes = new List<CustomAttributeBuilder>(); assemblyAttributes.Add(transparencyAttribute); #if !FEATURE_CORECLR // On the desktop, we need to use the security rule set level 1 for anonymously hosted // dynamic methods. In level 2, transparency rules are strictly enforced, which leads to // errors when a fully trusted application causes a dynamic method to be generated that tries // to call a method with a LinkDemand or a SecurityCritical method. To retain compatibility // with the v2.0 and v3.x frameworks, these calls should be allowed. // // If this rule set was not explicitly called out, then the anonymously hosted dynamic methods // assembly would inherit the rule set from the creating assembly - which would cause it to // be level 2 because mscorlib.dll is using the level 2 rules. ConstructorInfo securityRulesCtor = typeof(SecurityRulesAttribute).GetConstructor(new Type[] { typeof(SecurityRuleSet) }); CustomAttributeBuilder securityRulesAttribute = new CustomAttributeBuilder(securityRulesCtor, new object[] { SecurityRuleSet.Level1 }); assemblyAttributes.Add(securityRulesAttribute); #endif // !FEATURE_CORECLR AssemblyName assemblyName = new AssemblyName("Anonymously Hosted DynamicMethods Assembly"); StackCrawlMark stackMark = StackCrawlMark.LookForMe; AssemblyBuilder assembly = AssemblyBuilder.InternalDefineDynamicAssembly( assemblyName, AssemblyBuilderAccess.Run, null, null, null, null, null, ref stackMark, assemblyAttributes, SecurityContextSource.CurrentAssembly); AppDomain.PublishAnonymouslyHostedDynamicMethodsAssembly(assembly.GetNativeHandle()); // this always gets the internal module. s_anonymouslyHostedDynamicMethodsModule = (InternalModuleBuilder)assembly.ManifestModule; } return s_anonymouslyHostedDynamicMethodsModule; }
private static RuntimeModule GetDynamicMethodsModule() { if (s_anonymouslyHostedDynamicMethodsModule == null) { lock (s_anonymouslyHostedDynamicMethodsModuleLock) { if (s_anonymouslyHostedDynamicMethodsModule != null) { return s_anonymouslyHostedDynamicMethodsModule; } CustomAttributeBuilder builder = new CustomAttributeBuilder(typeof(SecurityTransparentAttribute).GetConstructor(Type.EmptyTypes), new object[0]); List<CustomAttributeBuilder> unsafeAssemblyAttributes = new List<CustomAttributeBuilder> { builder }; CustomAttributeBuilder item = new CustomAttributeBuilder(typeof(SecurityRulesAttribute).GetConstructor(new Type[] { typeof(SecurityRuleSet) }), new object[] { SecurityRuleSet.Level1 }); unsafeAssemblyAttributes.Add(item); AssemblyName name = new AssemblyName("Anonymously Hosted DynamicMethods Assembly"); StackCrawlMark lookForMe = StackCrawlMark.LookForMe; AssemblyBuilder builder3 = AssemblyBuilder.InternalDefineDynamicAssembly(name, AssemblyBuilderAccess.Run, null, null, null, null, null, ref lookForMe, unsafeAssemblyAttributes, SecurityContextSource.CurrentAssembly); AppDomain.PublishAnonymouslyHostedDynamicMethodsAssembly(builder3.GetNativeHandle()); s_anonymouslyHostedDynamicMethodsModule = (InternalModuleBuilder) builder3.ManifestModule; } } return s_anonymouslyHostedDynamicMethodsModule; }
internal ModuleBuilder GetModuleBuilder(InternalModuleBuilder module) { Contract.Requires(module != null); Contract.Assert(this.InternalAssembly == module.Assembly); lock(SyncRoot) { #if !FEATURE_CORECLR foreach (ModuleBuilder modBuilder in m_assemblyData.m_moduleBuilderList) { if (modBuilder.InternalModule == module) return modBuilder; } // m_onDiskAssemblyModuleBuilder is null before Save if (m_onDiskAssemblyModuleBuilder != null && m_onDiskAssemblyModuleBuilder.InternalModule == module) return m_onDiskAssemblyModuleBuilder; #endif // !FEATURE_CORECLR // in CoreCLR there is only one module in each dynamic assembly, the manifest module if (m_manifestModuleBuilder.InternalModule == module) return m_manifestModuleBuilder; throw new ArgumentException("module"); } }
internal ModuleBuilder GetModuleBuilder(InternalModuleBuilder module) { lock (this.SyncRoot) { foreach (ModuleBuilder builder in this.m_assemblyData.m_moduleBuilderList) { if (builder.InternalModule == module) { return builder; } } if ((this.m_onDiskAssemblyModuleBuilder != null) && (this.m_onDiskAssemblyModuleBuilder.InternalModule == module)) { return this.m_onDiskAssemblyModuleBuilder; } if (this.m_manifestModuleBuilder.InternalModule != module) { throw new ArgumentException("module"); } return this.m_manifestModuleBuilder; } }
internal ModuleBuilder GetModuleBuilder(InternalModuleBuilder module) { Contract.Requires(module != null); Debug.Assert(this.InternalAssembly == module.Assembly); lock(SyncRoot) { // in CoreCLR there is only one module in each dynamic assembly, the manifest module if (m_manifestModuleBuilder.InternalModule == module) return m_manifestModuleBuilder; throw new ArgumentException(null, nameof(module)); } }
[MethodImplAttribute(MethodImplOptions.NoInlining)] // Methods containing StackCrawlMark local var has to be marked non-inlineable private static RuntimeModule GetDynamicMethodsModule() { if (s_anonymouslyHostedDynamicMethodsModule != null) return s_anonymouslyHostedDynamicMethodsModule; lock (s_anonymouslyHostedDynamicMethodsModuleLock) { if (s_anonymouslyHostedDynamicMethodsModule != null) return s_anonymouslyHostedDynamicMethodsModule; ConstructorInfo transparencyCtor = typeof(SecurityTransparentAttribute).GetConstructor(Type.EmptyTypes); CustomAttributeBuilder transparencyAttribute = new CustomAttributeBuilder(transparencyCtor, EmptyArray<Object>.Value); List<CustomAttributeBuilder> assemblyAttributes = new List<CustomAttributeBuilder>(); assemblyAttributes.Add(transparencyAttribute); AssemblyName assemblyName = new AssemblyName("Anonymously Hosted DynamicMethods Assembly"); StackCrawlMark stackMark = StackCrawlMark.LookForMe; AssemblyBuilder assembly = AssemblyBuilder.InternalDefineDynamicAssembly( assemblyName, AssemblyBuilderAccess.Run, null, null, null, null, null, ref stackMark, assemblyAttributes, SecurityContextSource.CurrentAssembly); AppDomain.PublishAnonymouslyHostedDynamicMethodsAssembly(assembly.GetNativeHandle()); // this always gets the internal module. s_anonymouslyHostedDynamicMethodsModule = (InternalModuleBuilder)assembly.ManifestModule; } return s_anonymouslyHostedDynamicMethodsModule; }