// Used to switch into other AppDomain and call SetupRemoteDomain. // We cannot simply call through the proxy, because if there // are any remoting sinks registered, they can add non-mscorlib // objects to the message (causing an assembly load exception when // we try to deserialize it on the other side) private static object PrepareDataForSetup(string friendlyName, AppDomainSetup setup, string[] propertyNames, string[] propertyValues) { var newSetup = new AppDomainSetup(setup, copyDomainBoundData: false); // Remove the special AppDomainCompatSwitch entries from the set of name value pairs // And add them to the AppDomainSetup // // This is only supported on CoreCLR through ICLRRuntimeHost2.CreateAppDomainWithManager // Desktop code should use System.AppDomain.CreateDomain() or // System.AppDomainManager.CreateDomain() and add the flags to the AppDomainSetup var compatList = new List <string>(); if (propertyNames != null && propertyValues != null) { for (int i = 0; i < propertyNames.Length; i++) { if (string.Equals(propertyNames[i], "AppDomainCompatSwitch", StringComparison.OrdinalIgnoreCase)) { compatList.Add(propertyValues[i]); propertyNames[i] = null; propertyValues[i] = null; } } if (compatList.Count > 0) { newSetup.SetCompatibilitySwitches(compatList); } } return(new object[] { friendlyName, newSetup, propertyNames, propertyValues }); } // PrepareDataForSetup
[System.Security.SecurityCritical] // auto-generated private static object PrepareDataForSetup(String friendlyName, AppDomainSetup setup, Evidence providedSecurityInfo, Evidence creatorsSecurityInfo, IntPtr parentSecurityDescriptor, string sandboxName, string[] propertyNames, string[] propertyValues) { byte[] serializedEvidence = null; bool generateDefaultEvidence = false; #if FEATURE_CAS_POLICY // serialize evidence EvidenceCollection evidenceCollection = null; if (providedSecurityInfo != null || creatorsSecurityInfo != null) { // If we're just passing through AppDomain.CurrentDomain.Evidence, and that evidence is just // using the standard runtime AppDomainEvidenceFactory, don't waste time serializing it and // deserializing it back -- instead, we can recreate a new AppDomainEvidenceFactory in the new // domain. We only want to do this if there is no HostSecurityManager, otherwise the // HostSecurityManager could have added additional evidence on top of our standard factory. HostSecurityManager hsm = CurrentDomain.DomainManager != null ? CurrentDomain.DomainManager.HostSecurityManager : null; bool hostMayContributeEvidence = hsm != null && hsm.GetType() != typeof(HostSecurityManager) && (hsm.Flags & HostSecurityManagerOptions.HostAppDomainEvidence) == HostSecurityManagerOptions.HostAppDomainEvidence; if (!hostMayContributeEvidence) { if (providedSecurityInfo != null && providedSecurityInfo.IsUnmodified && providedSecurityInfo.Target != null && providedSecurityInfo.Target is AppDomainEvidenceFactory) { providedSecurityInfo = null; generateDefaultEvidence = true; } if (creatorsSecurityInfo != null && creatorsSecurityInfo.IsUnmodified && creatorsSecurityInfo.Target != null && creatorsSecurityInfo.Target is AppDomainEvidenceFactory) { creatorsSecurityInfo = null; generateDefaultEvidence = true; } } } if ((providedSecurityInfo != null) || (creatorsSecurityInfo != null)) { evidenceCollection = new EvidenceCollection(); evidenceCollection.ProvidedSecurityInfo = providedSecurityInfo; evidenceCollection.CreatorsSecurityInfo = creatorsSecurityInfo; } if (evidenceCollection != null) { serializedEvidence = CrossAppDomainSerializer.SerializeObject(evidenceCollection).GetBuffer(); } #endif // FEATURE_CAS_POLICY AppDomainInitializerInfo initializerInfo = null; if (setup!=null && setup.AppDomainInitializer!=null) initializerInfo=new AppDomainInitializerInfo(setup.AppDomainInitializer); // will travel x-Ad, drop non-agile data AppDomainSetup newSetup = new AppDomainSetup(setup, false); #if FEATURE_CORECLR // Remove the special AppDomainCompatSwitch entries from the set of name value pairs // And add them to the AppDomainSetup // // This is only supported on CoreCLR through ICLRRuntimeHost2.CreateAppDomainWithManager // Desktop code should use System.AppDomain.CreateDomain() or // System.AppDomainManager.CreateDomain() and add the flags to the AppDomainSetup List<String> compatList = new List<String>(); if(propertyNames!=null && propertyValues != null) { for (int i=0; i<propertyNames.Length; i++) { if(String.Compare(propertyNames[i], "AppDomainCompatSwitch", StringComparison.OrdinalIgnoreCase) == 0) { compatList.Add(propertyValues[i]); propertyNames[i] = null; propertyValues[i] = null; } } if (compatList.Count > 0) { newSetup.SetCompatibilitySwitches(compatList); } } #endif // FEATURE_CORECLR return new Object[] { friendlyName, newSetup, parentSecurityDescriptor, generateDefaultEvidence, serializedEvidence, initializerInfo, sandboxName, propertyNames, propertyValues }; } // PrepareDataForSetup
/// <summary> /// Pre-loads a test into the correct app domain for the current loader mode. /// </summary> /// <param name="test"></param> /// <param name="paths"></param> void TestPreLoader_AppDomain(ReliabilityTest test, string[] paths) { AppDomain ad = null; try { if (curTestSet.AppDomainLoaderMode != AppDomainLoaderMode.RoundRobin || test.CustomAction == CustomActionType.LegacySecurityPolicy) { string appDomainName = AppDomain.CurrentDomain.FriendlyName + "_" + "TestDomain_" + test.Assembly + "_" + Guid.NewGuid().ToString(); logger.WriteToInstrumentationLog(curTestSet, LoggingLevels.AppDomain, "Creating app domain: " + appDomainName + " for " + test.Index.ToString()); AppDomainSetup ads = new AppDomainSetup(); Evidence ev = AppDomain.CurrentDomain.Evidence; if (test.CustomAction == CustomActionType.LegacySecurityPolicy) { ads.SetCompatibilitySwitches(new string[] { "NetFx40_LegacySecurityPolicy" }); ev = new Evidence(new EvidenceBase[] { new Zone(System.Security.SecurityZone.MyComputer) }, null); } // Set the probing scope for assemblies to %BVT_ROOT%. The default is %BVT_ROOT%\Stress\CLRCore, // which causes some tests to fail because their assemblies are out of scope. ads.ApplicationBase = "file:///" + Environment.GetEnvironmentVariable("BVT_ROOT").Replace(@"\", "/"); ads.PrivateBinPath = "file:///" + Environment.GetEnvironmentVariable("BASE_ROOT").Replace(@"\", "/"); ad = AppDomain.CreateDomain(appDomainName, ev, ads); } else { ad = _testDomains[test.AppDomainIndex]; } AssemblyName an = new AssemblyName(); Object ourObj = null; test.AppDomain = ad; object obj = ad.CreateInstanceAndUnwrap(Assembly.GetExecutingAssembly().FullName, typeof(LoaderClass).FullName); LoaderClass lfc = obj as LoaderClass; if (test.SuppressConsoleOutput) lfc.SuppressConsole(); if (test.Assembly.ToLower().IndexOf(".exe") == -1 && test.Assembly.ToLower().IndexOf(".dll") == -1) // must be a simple name or fullname... { lfc.Load(test.Assembly, paths, this); } else // has an executable extension, must be in local directory. { lfc.LoadFrom(test.BasePath + test.Assembly, paths, this); } // check and see if this test is marked as requiring STA. We only do // the check once, and then we set the STA/MTA/Unknown bit on the test attributes // to avoid doing reflection every time we start the test. if ((test.TestAttrs & TestAttributes.RequiresThread) == TestAttributes.None) { ApartmentState state = lfc.CheckMainForThreadType(); switch (state) { case ApartmentState.STA: test.TestAttrs |= TestAttributes.RequiresSTAThread; break; case ApartmentState.MTA: test.TestAttrs |= TestAttributes.RequiresMTAThread; break; case ApartmentState.Unknown: test.TestAttrs |= TestAttributes.RequiresUnknownThread; break; } } ourObj = lfc.GetTest(); // and now call the register method on the type if it's one of our supported test types. if (ourObj is ISingleReliabilityTest) { ((ISingleReliabilityTest)ourObj).Register(); } else if (ourObj is IMultipleReliabilityTest) { ((IMultipleReliabilityTest)ourObj).Register(); } else if (!(ourObj is string)) // we were unable to find a test here - a string is an executable filename. { Interlocked.Decrement(ref LoadingCount); return; } test.TestObject = ourObj; test.MyLoader = lfc; } catch (Exception) { // if we took an exception while loading the test, but we still have an app domain // we don't want to leak the app domain. if (ad != null) { test.AppDomain = null; AppDomain.Unload(ad); } throw; } }
private AppDomainSetup GetAppDomainSetup(bool legacyCasEnabled) { var setup = new AppDomainSetup(); if (legacyCasEnabled) { setup.SetCompatibilitySwitches(new[] { "NetFx40_LegacySecurityPolicy" }); } return setup; }
// Used to switch into other AppDomain and call SetupRemoteDomain. // We cannot simply call through the proxy, because if there // are any remoting sinks registered, they can add non-mscorlib // objects to the message (causing an assembly load exception when // we try to deserialize it on the other side) private static object PrepareDataForSetup(String friendlyName, AppDomainSetup setup, Evidence providedSecurityInfo, Evidence creatorsSecurityInfo, IntPtr parentSecurityDescriptor, string sandboxName, string[] propertyNames, string[] propertyValues) { byte[] serializedEvidence = null; bool generateDefaultEvidence = false; AppDomainInitializerInfo initializerInfo = null; if (setup != null && setup.AppDomainInitializer != null) { initializerInfo = new AppDomainInitializerInfo(setup.AppDomainInitializer); } // will travel x-Ad, drop non-agile data AppDomainSetup newSetup = new AppDomainSetup(setup, false); // Remove the special AppDomainCompatSwitch entries from the set of name value pairs // And add them to the AppDomainSetup // // This is only supported on CoreCLR through ICLRRuntimeHost2.CreateAppDomainWithManager // Desktop code should use System.AppDomain.CreateDomain() or // System.AppDomainManager.CreateDomain() and add the flags to the AppDomainSetup List <String> compatList = new List <String>(); if (propertyNames != null && propertyValues != null) { for (int i = 0; i < propertyNames.Length; i++) { if (String.Compare(propertyNames[i], "AppDomainCompatSwitch", StringComparison.OrdinalIgnoreCase) == 0) { compatList.Add(propertyValues[i]); propertyNames[i] = null; propertyValues[i] = null; } } if (compatList.Count > 0) { newSetup.SetCompatibilitySwitches(compatList); } } return(new Object[] { friendlyName, newSetup, parentSecurityDescriptor, generateDefaultEvidence, serializedEvidence, initializerInfo, sandboxName, propertyNames, propertyValues }); } // PrepareDataForSetup
private static void SetNetFx40LegacySecurityPolicy(AppDomainSetup setup) { List<string> switches = new List<string> { "NetFx40_LegacySecurityPolicy" }; setup.SetCompatibilitySwitches(switches); }
// Used to switch into other AppDomain and call SetupRemoteDomain. // We cannot simply call through the proxy, because if there // are any remoting sinks registered, they can add non-mscorlib // objects to the message (causing an assembly load exception when // we try to deserialize it on the other side) private static object PrepareDataForSetup(String friendlyName, AppDomainSetup setup, Evidence providedSecurityInfo, Evidence creatorsSecurityInfo, IntPtr parentSecurityDescriptor, string sandboxName, string[] propertyNames, string[] propertyValues) { byte[] serializedEvidence = null; bool generateDefaultEvidence = false; AppDomainInitializerInfo initializerInfo = null; if (setup!=null && setup.AppDomainInitializer!=null) initializerInfo=new AppDomainInitializerInfo(setup.AppDomainInitializer); // will travel x-Ad, drop non-agile data AppDomainSetup newSetup = new AppDomainSetup(setup, false); // Remove the special AppDomainCompatSwitch entries from the set of name value pairs // And add them to the AppDomainSetup // // This is only supported on CoreCLR through ICLRRuntimeHost2.CreateAppDomainWithManager // Desktop code should use System.AppDomain.CreateDomain() or // System.AppDomainManager.CreateDomain() and add the flags to the AppDomainSetup List<String> compatList = new List<String>(); if(propertyNames!=null && propertyValues != null) { for (int i=0; i<propertyNames.Length; i++) { if(String.Compare(propertyNames[i], "AppDomainCompatSwitch", StringComparison.OrdinalIgnoreCase) == 0) { compatList.Add(propertyValues[i]); propertyNames[i] = null; propertyValues[i] = null; } } if (compatList.Count > 0) { newSetup.SetCompatibilitySwitches(compatList); } } return new Object[] { friendlyName, newSetup, parentSecurityDescriptor, generateDefaultEvidence, serializedEvidence, initializerInfo, sandboxName, propertyNames, propertyValues }; } // PrepareDataForSetup
public void IsCompatibilitySwitchSet () { Assert.Throws<ArgumentNullException> (delegate { AppDomain.CurrentDomain.IsCompatibilitySwitchSet (null); }, "null"); Assert.IsFalse ((bool) AppDomain.CurrentDomain.IsCompatibilitySwitchSet (String.Empty), "Empty"); // defined in SL4 RC documentation Assert.IsFalse ((bool) AppDomain.CurrentDomain.IsCompatibilitySwitchSet ("NetFx40_Legacy20SortingBehavior"), "NetFx40_Legacy20SortingBehavior"); // defined in FX4 beta documentation Assert.IsFalse ((bool) AppDomain.CurrentDomain.IsCompatibilitySwitchSet ("NetFx40_LegacySecurityPolicy"), "NetFx40_LegacySecurityPolicy"); Assert.IsFalse ((bool) AppDomain.CurrentDomain.IsCompatibilitySwitchSet ("NetFx40_TimeSpanLegacyFormatMode"), "NetFx40_TimeSpanLegacyFormatMode"); // undefined Assert.IsFalse ((bool) AppDomain.CurrentDomain.IsCompatibilitySwitchSet ("MONO"), "undefined"); // we can set compatibility switches on an AppDomainSetup but we // can't associate it with the current domain so they get ignored AppDomainSetup ads = new AppDomainSetup (); try { ads.SetCompatibilitySwitches (new[] { "MONO" }); Assert.IsFalse ((bool) AppDomain.CurrentDomain.IsCompatibilitySwitchSet ("MONO"), "undefined"); } finally { ads.SetCompatibilitySwitches (null); } }