3 // Copyright (c) Microsoft Corporation. All rights reserved.
6 /*=============================================================================
8 ** Class: AppDomainSetup
10 ** <OWNER>blanders</OWNER>
12 ** Purpose: Defines the settings that the loader uses to find assemblies in an
17 =============================================================================*/
23 using System.Deployment.Internal.Isolation;
24 using System.Deployment.Internal.Isolation.Manifest;
25 using System.Runtime.Hosting;
28 using System.Runtime.CompilerServices;
31 using System.Threading;
32 using System.Runtime.InteropServices;
33 using System.Runtime.Serialization;
34 using System.Reflection;
35 using System.Security;
36 using System.Security.Permissions;
37 using System.Security.Policy;
38 using System.Globalization;
39 using Path = System.IO.Path;
40 using System.Runtime.Versioning;
41 using System.Diagnostics.Contracts;
42 using System.Collections;
43 using System.Collections.Generic;
46 [ClassInterface(ClassInterfaceType.None)]
47 [System.Runtime.InteropServices.ComVisible(true)]
48 public sealed class AppDomainSetup :
52 internal enum LoaderInformation
54 // If you add a new value, add the corresponding property
55 // to AppDomain.GetData() and SetData()'s switch statements.
56 ApplicationBaseValue = LOADER_APPLICATION_BASE,
57 ConfigurationFileValue = LOADER_CONFIGURATION_BASE,
58 DynamicBaseValue = LOADER_DYNAMIC_BASE,
59 DevPathValue = LOADER_DEVPATH,
60 ApplicationNameValue = LOADER_APPLICATION_NAME,
61 PrivateBinPathValue = LOADER_PRIVATE_PATH,
62 PrivateBinPathProbeValue = LOADER_PRIVATE_BIN_PATH_PROBE,
63 ShadowCopyDirectoriesValue = LOADER_SHADOW_COPY_DIRECTORIES,
64 ShadowCopyFilesValue = LOADER_SHADOW_COPY_FILES,
65 CachePathValue = LOADER_CACHE_PATH,
66 LicenseFileValue = LOADER_LICENSE_FILE,
67 DisallowPublisherPolicyValue = LOADER_DISALLOW_PUBLISHER_POLICY,
68 DisallowCodeDownloadValue = LOADER_DISALLOW_CODE_DOWNLOAD,
69 DisallowBindingRedirectsValue = LOADER_DISALLOW_BINDING_REDIRECTS,
70 DisallowAppBaseProbingValue = LOADER_DISALLOW_APPBASE_PROBING,
71 ConfigurationBytesValue = LOADER_CONFIGURATION_BYTES,
72 LoaderMaximum = LOADER_MAXIMUM,
75 // This class has an unmanaged representation so be aware you will need to make edits in vm\object.h if you change the order
76 // of these fields or add new ones.
78 private string[] _Entries;
79 private LoaderOptimization _LoaderOptimization;
80 #pragma warning disable 169
81 private String _AppBase; // for compat with v1.1
82 #pragma warning restore 169
83 [OptionalField(VersionAdded = 2)]
84 private AppDomainInitializer _AppDomainInitializer;
85 [OptionalField(VersionAdded = 2)]
86 private string[] _AppDomainInitializerArguments;
88 [OptionalField(VersionAdded = 2)]
89 private ActivationArguments _ActivationArguments;
92 // On the CoreCLR, this contains just the name of the permission set that we install in the new appdomain.
93 // Not the ToXml().ToString() of an ApplicationTrust object.
95 [OptionalField(VersionAdded = 2)]
96 private string _ApplicationTrust;
97 [OptionalField(VersionAdded = 2)]
98 private byte[] _ConfigurationBytes;
99 #if FEATURE_COMINTEROP
100 [OptionalField(VersionAdded = 3)]
101 private bool _DisableInterfaceCache = false;
102 #endif // FEATURE_COMINTEROP
103 [OptionalField(VersionAdded = 4)]
104 private string _AppDomainManagerAssembly;
105 [OptionalField(VersionAdded = 4)]
106 private string _AppDomainManagerType;
109 [OptionalField(VersionAdded = 4)]
110 private string[] _AptcaVisibleAssemblies;
113 // A collection of strings used to indicate which breaking changes shouldn't be applied
114 // to an AppDomain. We only use the keys, the values are ignored.
115 [OptionalField(VersionAdded = 4)]
116 private Dictionary<string, object> _CompatFlags;
118 [OptionalField(VersionAdded = 5)] // This was added in .NET FX v4.5
119 private String _TargetFrameworkName;
123 internal AppDomainSortingSetupInfo _AppDomainSortingSetupInfo;
126 [OptionalField(VersionAdded = 5)] // This was added in .NET FX v4.5
127 private bool _CheckedForTargetFrameworkName;
129 #if FEATURE_RANDOMIZED_STRING_HASHING
130 [OptionalField(VersionAdded = 5)] // This was added in .NET FX v4.5
131 private bool _UseRandomizedStringHashing;
134 [SecuritySafeCritical]
135 internal AppDomainSetup(AppDomainSetup copy, bool copyDomainBoundData)
137 string[] mine = Value;
139 string[] other = copy.Value;
140 int mineSize = _Entries.Length;
141 int otherSize = other.Length;
142 int size = (otherSize < mineSize) ? otherSize : mineSize;
144 for (int i = 0; i < size; i++)
149 // This case can happen when the copy is a deserialized version of
150 // an AppDomainSetup object serialized by Everett.
151 for (int i = size; i < mineSize; i++)
155 _LoaderOptimization = copy._LoaderOptimization;
157 _AppDomainInitializerArguments = copy.AppDomainInitializerArguments;
158 #if FEATURE_CLICKONCE
159 _ActivationArguments = copy.ActivationArguments;
161 _ApplicationTrust = copy._ApplicationTrust;
162 if (copyDomainBoundData)
163 _AppDomainInitializer = copy.AppDomainInitializer;
165 _AppDomainInitializer = null;
167 _ConfigurationBytes = copy.GetConfigurationBytes();
168 #if FEATURE_COMINTEROP
169 _DisableInterfaceCache = copy._DisableInterfaceCache;
170 #endif // FEATURE_COMINTEROP
171 _AppDomainManagerAssembly = copy.AppDomainManagerAssembly;
172 _AppDomainManagerType = copy.AppDomainManagerType;
174 _AptcaVisibleAssemblies = copy.PartialTrustVisibleAssemblies;
177 if (copy._CompatFlags != null)
179 SetCompatibilitySwitches(copy._CompatFlags.Keys);
183 if(copy._AppDomainSortingSetupInfo != null)
185 _AppDomainSortingSetupInfo = new AppDomainSortingSetupInfo(copy._AppDomainSortingSetupInfo);
188 _TargetFrameworkName = copy._TargetFrameworkName;
190 #if FEATURE_RANDOMIZED_STRING_HASHING
191 _UseRandomizedStringHashing = copy._UseRandomizedStringHashing;
196 _LoaderOptimization = LoaderOptimization.NotSpecified;
199 public AppDomainSetup()
201 _LoaderOptimization = LoaderOptimization.NotSpecified;
204 #if FEATURE_CLICKONCE
205 // Creates an AppDomainSetup object from an application identity.
206 [ResourceExposure(ResourceScope.Machine)]
207 [ResourceConsumption(ResourceScope.Machine)]
208 public AppDomainSetup (ActivationContext activationContext) : this (new ActivationArguments(activationContext)) {}
210 [System.Security.SecuritySafeCritical] // auto-generated
211 [ResourceExposure(ResourceScope.Machine)]
212 [ResourceConsumption(ResourceScope.Machine)]
213 public AppDomainSetup (ActivationArguments activationArguments) {
214 if (activationArguments == null)
215 throw new ArgumentNullException("activationArguments");
216 Contract.EndContractBlock();
218 _LoaderOptimization = LoaderOptimization.NotSpecified;
219 ActivationArguments = activationArguments;
221 Contract.Assert(activationArguments.ActivationContext != null, "Cannot set base directory without activation context");
222 string entryPointPath = CmsUtils.GetEntryPointFullPath(activationArguments);
223 if (!String.IsNullOrEmpty(entryPointPath))
224 SetupDefaults(entryPointPath);
226 ApplicationBase = activationArguments.ActivationContext.ApplicationDirectory;
229 #endif // !FEATURE_CLICKONCE
232 [System.Security.SecurityCritical] // auto-generated
234 [ResourceExposure(ResourceScope.Machine)]
235 [ResourceConsumption(ResourceScope.Machine)]
236 internal void SetupDefaults(string imageLocation, bool imageLocationAlreadyNormalized = false) {
237 char[] sep = {'\\', '/'};
238 int i = imageLocation.LastIndexOfAny(sep);
241 ApplicationName = imageLocation;
244 ApplicationName = imageLocation.Substring(i+1);
245 string appBase = imageLocation.Substring(0, i+1);
247 if (imageLocationAlreadyNormalized)
248 Value[(int) LoaderInformation.ApplicationBaseValue] = appBase;
250 ApplicationBase = appBase;
252 ConfigurationFile = ApplicationName + AppDomainSetup.ConfigurationExtension;
255 internal string[] Value
258 if( _Entries == null)
259 _Entries = new String[LOADER_MAXIMUM];
264 internal String GetUnsecureApplicationBase()
266 return Value[(int) LoaderInformation.ApplicationBaseValue];
269 public string AppDomainManagerAssembly
271 get { return _AppDomainManagerAssembly; }
272 set { _AppDomainManagerAssembly = value; }
275 public string AppDomainManagerType
277 get { return _AppDomainManagerType; }
278 set { _AppDomainManagerType = value; }
282 public string[] PartialTrustVisibleAssemblies
284 get { return _AptcaVisibleAssemblies; }
287 _AptcaVisibleAssemblies = (string[])value.Clone();
288 Array.Sort<string>(_AptcaVisibleAssemblies, StringComparer.OrdinalIgnoreCase);
291 _AptcaVisibleAssemblies = null;
297 public String ApplicationBase
300 [System.Security.SecurityCritical] // auto-generated
302 [System.Security.SecuritySafeCritical]
305 [ResourceExposure(ResourceScope.Machine)]
306 [ResourceConsumption(ResourceScope.Machine)]
308 return VerifyDir(GetUnsecureApplicationBase(), false);
312 [System.Security.SecurityCritical] // auto-generated
314 [ResourceExposure(ResourceScope.Machine)]
315 [ResourceConsumption(ResourceScope.Machine)]
317 Value[(int) LoaderInformation.ApplicationBaseValue] = NormalizePath(value, false);
321 [System.Security.SecuritySafeCritical]
322 private string NormalizePath(string path, bool useAppBase)
327 // If we add very long file name support ("\\?\") to the Path class then this is unnecesary,
328 // but we do not plan on doing this for now.
330 // Long path checks can be quirked, and as loading default quirks too early in the setup of an AppDomain is risky
331 // we'll avoid checking path lengths- we'll still fail at MAX_PATH later if we're !useAppBase when we call Path's
334 path = System.Security.Util.URLString.PreProcessForExtendedPathRemoval(
335 checkPathLength: false,
339 int len = path.Length;
343 bool UNCpath = false;
346 (String.Compare( path, 0, "file:", 0, 5, StringComparison.OrdinalIgnoreCase) == 0)) {
349 if (path[6] == '\\') {
350 if ((path[7] == '\\') || (path[7] == '/')) {
352 // Don't allow "file:\\\\", because we can't tell the difference
353 // with it for "file:\\" + "\\server" and "file:\\\" + "\localpath"
355 ((path[8] == '\\') || (path[8] == '/')) )
356 throw new ArgumentException(Environment.GetResourceString("Argument_InvalidPathChars"));
358 // file:\\\ means local path
363 // file:\\ means remote server
371 else if (path[7] == '/')
377 if ( (len > 8) && (path[7] == '\\') && (path[8] == '\\') )
379 else { // file://remote
381 // Create valid UNC path by changing
382 // all occurences of '/' to '\\' in path
383 System.Text.StringBuilder winPathBuilder =
384 new System.Text.StringBuilder(len);
385 for (int i = 0; i < len; i++) {
388 winPathBuilder.Append('\\');
390 winPathBuilder.Append(c);
392 path = winPathBuilder.ToString();
397 path = path.Substring(trim);
406 ( (path[0] == '/') || (path[0] == '\\') ) &&
407 ( (path[1] == '/') || (path[1] == '\\') ) ))
411 int colon = path.IndexOf(':') + 1;
413 // protocol other than file:
416 ( (path[colon] == '/') || (path[colon] == '\\') ) &&
417 ( (path[colon+1] == '/') || (path[colon+1] == '\\') ))
427 ((len == 1) || (path[1] != ':')))
429 String appBase = Value[(int)LoaderInformation.ApplicationBaseValue];
431 if ((appBase == null) || (appBase.Length == 0))
432 throw new MemberAccessException(Environment.GetResourceString("AppDomain_AppBaseNotSet"));
434 StringBuilder result = StringBuilderCache.Acquire();
437 if ((path[0] == '/') || (path[0] == '\\')) {
438 // Emulate Path.GetPathRoot without hitting code paths that check quirks
439 string pathRoot = AppDomain.NormalizePath(appBase, fullCheck: false);
440 pathRoot = pathRoot.Substring(0, System.IO.PathInternal.GetRootLength(pathRoot));
441 if (pathRoot.Length == 0) { // URL
442 int index = appBase.IndexOf(":/", StringComparison.Ordinal);
444 index = appBase.IndexOf(":\\", StringComparison.Ordinal);
446 // Get past last slashes of "url:http://"
447 int urlLen = appBase.Length;
449 (index < urlLen) && ((appBase[index] == '/') || (appBase[index] == '\\'));
452 // Now find the next slash to get domain name
453 for (; (index < urlLen) && (appBase[index] != '/') && (appBase[index] != '\\');
456 pathRoot = appBase.Substring(0, index);
459 result.Append(pathRoot);
463 result.Append(appBase);
465 // Make sure there's a slash separator (and only one)
466 int aLen = result.Length - 1;
467 if ((result[aLen] != '/') &&
468 (result[aLen] != '\\')) {
470 if (appBase.IndexOf(":/", StringComparison.Ordinal) == -1)
477 result.Remove(aLen, 1);
480 path = StringBuilderCache.GetStringAndRelease(result);
484 path = AppDomain.NormalizePath(path, fullCheck: true);
491 private bool IsFilePath(String path)
493 return (path[1] == ':') || ( (path[0] == '\\') && (path[1] == '\\') );
496 internal static String ApplicationBaseKey
499 return ACTAG_APP_BASE_URL;
503 public String ConfigurationFile
505 [System.Security.SecuritySafeCritical] // auto-generated
506 [ResourceExposure(ResourceScope.Machine)]
507 [ResourceConsumption(ResourceScope.Machine)]
509 return VerifyDir(Value[(int) LoaderInformation.ConfigurationFileValue], true);
512 [ResourceExposure(ResourceScope.Machine)]
513 [ResourceConsumption(ResourceScope.Machine)]
515 Value[(int) LoaderInformation.ConfigurationFileValue] = value;
519 // Used by the ResourceManager internally. This must not do any
520 // security checks to avoid infinite loops.
521 internal String ConfigurationFileInternal
523 [ResourceExposure(ResourceScope.Machine)]
524 [ResourceConsumption(ResourceScope.Machine)]
526 return NormalizePath(Value[(int) LoaderInformation.ConfigurationFileValue], true);
530 internal static String ConfigurationFileKey
533 return ACTAG_APP_CONFIG_FILE;
537 public byte[] GetConfigurationBytes()
539 if (_ConfigurationBytes == null)
542 return (byte[]) _ConfigurationBytes.Clone();
545 public void SetConfigurationBytes(byte[] value)
547 _ConfigurationBytes = value;
550 private static String ConfigurationBytesKey
553 return ACTAG_APP_CONFIG_BLOB;
557 // only needed by AppDomain.Setup(). Not really needed by users.
558 internal Dictionary<string, object> GetCompatibilityFlags()
563 public void SetCompatibilitySwitches(IEnumerable<String> switches)
567 if(_AppDomainSortingSetupInfo != null)
569 _AppDomainSortingSetupInfo._useV2LegacySorting = false;
570 _AppDomainSortingSetupInfo._useV4LegacySorting = false;
574 #if FEATURE_RANDOMIZED_STRING_HASHING
575 _UseRandomizedStringHashing = false;
577 if (switches != null)
579 _CompatFlags = new Dictionary<string, object>();
580 foreach (String str in switches)
583 if(StringComparer.OrdinalIgnoreCase.Equals("NetFx40_Legacy20SortingBehavior", str)) {
584 if(_AppDomainSortingSetupInfo == null)
586 _AppDomainSortingSetupInfo = new AppDomainSortingSetupInfo();
588 _AppDomainSortingSetupInfo._useV2LegacySorting = true;
591 if(StringComparer.OrdinalIgnoreCase.Equals("NetFx45_Legacy40SortingBehavior", str)) {
592 if(_AppDomainSortingSetupInfo == null)
594 _AppDomainSortingSetupInfo = new AppDomainSortingSetupInfo();
596 _AppDomainSortingSetupInfo._useV4LegacySorting = true;
600 #if FEATURE_RANDOMIZED_STRING_HASHING
601 if(StringComparer.OrdinalIgnoreCase.Equals("UseRandomizedStringHashAlgorithm", str)) {
602 _UseRandomizedStringHashing = true;
606 _CompatFlags.Add(str, null);
616 // A target Framework moniker, in a format parsible by the FrameworkName class.
617 public String TargetFrameworkName {
619 return _TargetFrameworkName;
622 _TargetFrameworkName = value;
626 internal bool CheckedForTargetFrameworkName
628 get { return _CheckedForTargetFrameworkName; }
629 set { _CheckedForTargetFrameworkName = value; }
634 public void SetNativeFunction(string functionName, int functionVersion, IntPtr functionPointer)
636 if(functionName == null)
638 throw new ArgumentNullException("functionName");
641 if(functionPointer == IntPtr.Zero)
643 throw new ArgumentNullException("functionPointer");
646 if(String.IsNullOrWhiteSpace(functionName))
648 throw new ArgumentException(Environment.GetResourceString("Argument_NPMSInvalidName"), "functionName");
651 Contract.EndContractBlock();
653 if(functionVersion < 1)
655 throw new ArgumentException(Environment.GetResourceString("ArgumentException_MinSortingVersion", 1, functionName));
658 if(_AppDomainSortingSetupInfo == null)
660 _AppDomainSortingSetupInfo = new AppDomainSortingSetupInfo();
663 if(String.Equals(functionName, "IsNLSDefinedString", StringComparison.OrdinalIgnoreCase))
665 _AppDomainSortingSetupInfo._pfnIsNLSDefinedString = functionPointer;
668 if (String.Equals(functionName, "CompareStringEx", StringComparison.OrdinalIgnoreCase))
670 _AppDomainSortingSetupInfo._pfnCompareStringEx = functionPointer;
673 if (String.Equals(functionName, "LCMapStringEx", StringComparison.OrdinalIgnoreCase))
675 _AppDomainSortingSetupInfo._pfnLCMapStringEx = functionPointer;
678 if (String.Equals(functionName, "FindNLSStringEx", StringComparison.OrdinalIgnoreCase))
680 _AppDomainSortingSetupInfo._pfnFindNLSStringEx = functionPointer;
683 if (String.Equals(functionName, "CompareStringOrdinal", StringComparison.OrdinalIgnoreCase))
685 _AppDomainSortingSetupInfo._pfnCompareStringOrdinal = functionPointer;
688 if (String.Equals(functionName, "GetNLSVersionEx", StringComparison.OrdinalIgnoreCase))
690 _AppDomainSortingSetupInfo._pfnGetNLSVersionEx = functionPointer;
693 if (String.Equals(functionName, "FindStringOrdinal", StringComparison.OrdinalIgnoreCase))
695 _AppDomainSortingSetupInfo._pfnFindStringOrdinal = functionPointer;
700 public String DynamicBase
702 [System.Security.SecuritySafeCritical] // auto-generated
703 [ResourceExposure(ResourceScope.Machine)]
704 [ResourceConsumption(ResourceScope.Machine)]
706 return VerifyDir(Value[(int) LoaderInformation.DynamicBaseValue], true);
709 [System.Security.SecuritySafeCritical] // auto-generated
710 [ResourceExposure(ResourceScope.Machine)]
711 [ResourceConsumption(ResourceScope.Machine)]
714 Value[(int) LoaderInformation.DynamicBaseValue] = null;
716 if(ApplicationName == null)
717 throw new MemberAccessException(Environment.GetResourceString("AppDomain_RequireApplicationName"));
719 StringBuilder s = new StringBuilder( NormalizePath(value, false) );
721 string h = ParseNumbers.IntToString(ApplicationName.GetLegacyNonRandomizedHashCode(),
722 16, 8, '0', ParseNumbers.PrintAsI4);
725 Value[(int) LoaderInformation.DynamicBaseValue] = s.ToString();
730 internal static String DynamicBaseKey
733 return ACTAG_APP_DYNAMIC_BASE;
738 public bool DisallowPublisherPolicy
742 return (Value[(int) LoaderInformation.DisallowPublisherPolicyValue] != null);
747 Value[(int) LoaderInformation.DisallowPublisherPolicyValue]="true";
749 Value[(int) LoaderInformation.DisallowPublisherPolicyValue]=null;
754 public bool DisallowBindingRedirects
758 return (Value[(int) LoaderInformation.DisallowBindingRedirectsValue] != null);
763 Value[(int) LoaderInformation.DisallowBindingRedirectsValue] = "true";
765 Value[(int) LoaderInformation.DisallowBindingRedirectsValue] = null;
769 public bool DisallowCodeDownload
773 return (Value[(int) LoaderInformation.DisallowCodeDownloadValue] != null);
778 Value[(int) LoaderInformation.DisallowCodeDownloadValue] = "true";
780 Value[(int) LoaderInformation.DisallowCodeDownloadValue] = null;
785 public bool DisallowApplicationBaseProbing
789 return (Value[(int) LoaderInformation.DisallowAppBaseProbingValue] != null);
794 Value[(int) LoaderInformation.DisallowAppBaseProbingValue] = "true";
796 Value[(int) LoaderInformation.DisallowAppBaseProbingValue] = null;
800 [System.Security.SecurityCritical] // auto-generated
801 [ResourceExposure(ResourceScope.Machine)]
802 [ResourceConsumption(ResourceScope.Machine)]
803 private String VerifyDir(String dir, bool normalize)
814 dir = NormalizePath(dir, true);
816 // The only way AppDomainSetup is exposed in coreclr is through the AppDomainManager
817 // and the AppDomainManager is a SecurityCritical type. Also, all callers of callstacks
818 // leading from VerifyDir are SecurityCritical. So we can remove the Demand because
819 // we have validated that all callers are SecurityCritical
823 // If we've already normalized we don't need to do it again, and can avoid hitting
824 // quirks in FileIOPermission.
825 new FileIOPermission(
826 access: FileIOPermissionAccess.PathDiscovery,
827 pathList: new string[] { dir },
828 checkForDuplicates: false,
829 needFullPath: false).Demand();
831 #endif // !FEATURE_CORECLR
838 [System.Security.SecurityCritical] // auto-generated
839 [ResourceExposure(ResourceScope.Machine)]
840 [ResourceConsumption(ResourceScope.Machine)]
841 private void VerifyDirList(String dirs)
844 String[] dirArray = dirs.Split(';');
845 int len = dirArray.Length;
847 for (int i = 0; i < len; i++)
848 VerifyDir(dirArray[i], true);
852 internal String DeveloperPath
854 [System.Security.SecurityCritical] // auto-generated
855 [ResourceExposure(ResourceScope.Machine)]
856 [ResourceConsumption(ResourceScope.Machine)]
858 String dirs = Value[(int) LoaderInformation.DevPathValue];
863 [ResourceExposure(ResourceScope.Machine)]
864 [ResourceConsumption(ResourceScope.Machine)]
867 Value[(int) LoaderInformation.DevPathValue] = null;
869 String[] directories = value.Split(';');
870 int size = directories.Length;
871 StringBuilder newPath = StringBuilderCache.Acquire();
872 bool fDelimiter = false;
874 for(int i = 0; i < size; i++) {
875 if(directories[i].Length != 0) {
881 newPath.Append(Path.GetFullPathInternal(directories[i]));
885 String newString = StringBuilderCache.GetStringAndRelease(newPath);
886 if (newString.Length == 0)
887 Value[(int) LoaderInformation.DevPathValue] = null;
889 Value[(int) LoaderInformation.DevPathValue] = newString;
894 internal static String DisallowPublisherPolicyKey
898 return ACTAG_DISALLOW_APPLYPUBLISHERPOLICY;
902 internal static String DisallowCodeDownloadKey
906 return ACTAG_CODE_DOWNLOAD_DISABLED;
910 internal static String DisallowBindingRedirectsKey
914 return ACTAG_DISALLOW_APP_BINDING_REDIRECTS;
918 internal static String DeveloperPathKey
921 return ACTAG_DEV_PATH;
925 internal static String DisallowAppBaseProbingKey
929 return ACTAG_DISALLOW_APP_BASE_PROBING;
933 public String ApplicationName
936 return Value[(int) LoaderInformation.ApplicationNameValue];
940 Value[(int) LoaderInformation.ApplicationNameValue] = value;
944 internal static String ApplicationNameKey
947 return ACTAG_APP_NAME;
952 public AppDomainInitializer AppDomainInitializer
955 return _AppDomainInitializer;
959 _AppDomainInitializer = value;
962 public string[] AppDomainInitializerArguments
965 return _AppDomainInitializerArguments;
969 _AppDomainInitializerArguments = value;
973 #if FEATURE_CLICKONCE
975 public ActivationArguments ActivationArguments {
978 return _ActivationArguments;
981 _ActivationArguments = value;
984 #endif // !FEATURE_CLICKONCE
986 internal ApplicationTrust InternalGetApplicationTrust()
989 if (_ApplicationTrust == null) return null;
993 ApplicationTrust grantSet = new ApplicationTrust(NamedPermissionSet.GetBuiltInSet(_ApplicationTrust));
995 SecurityElement securityElement = SecurityElement.FromString(_ApplicationTrust);
996 ApplicationTrust grantSet = new ApplicationTrust();
997 grantSet.FromXml(securityElement);
1003 internal void InternalSetApplicationTrust(String permissionSetName)
1005 _ApplicationTrust = permissionSetName;
1008 internal void InternalSetApplicationTrust(ApplicationTrust value)
1012 _ApplicationTrust = value.ToXml().ToString();
1016 _ApplicationTrust = null;
1021 #if FEATURE_CLICKONCE
1023 public ApplicationTrust ApplicationTrust
1026 return InternalGetApplicationTrust();
1029 InternalSetApplicationTrust(value);
1032 #else // FEATURE_CLICKONCE
1034 internal ApplicationTrust ApplicationTrust
1037 return InternalGetApplicationTrust();
1039 #if !FEATURE_CORECLR
1041 InternalSetApplicationTrust(value);
1045 #endif // FEATURE_CLICKONCE
1047 public String PrivateBinPath
1049 [System.Security.SecuritySafeCritical] // auto-generated
1050 [ResourceExposure(ResourceScope.Machine)]
1051 [ResourceConsumption(ResourceScope.Machine)]
1053 String dirs = Value[(int) LoaderInformation.PrivateBinPathValue];
1054 VerifyDirList(dirs);
1058 [ResourceExposure(ResourceScope.Machine)]
1059 [ResourceConsumption(ResourceScope.Machine)]
1061 Value[(int) LoaderInformation.PrivateBinPathValue] = value;
1065 internal static String PrivateBinPathKey
1068 return ACTAG_APP_PRIVATE_BINPATH;
1073 public String PrivateBinPathProbe
1076 return Value[(int) LoaderInformation.PrivateBinPathProbeValue];
1080 Value[(int) LoaderInformation.PrivateBinPathProbeValue] = value;
1084 internal static String PrivateBinPathProbeKey
1087 return ACTAG_BINPATH_PROBE_ONLY;
1091 public String ShadowCopyDirectories
1093 [System.Security.SecuritySafeCritical] // auto-generated
1094 [ResourceExposure(ResourceScope.Machine)]
1095 [ResourceConsumption(ResourceScope.Machine)]
1097 String dirs = Value[(int) LoaderInformation.ShadowCopyDirectoriesValue];
1098 VerifyDirList(dirs);
1102 [ResourceExposure(ResourceScope.Machine)]
1103 [ResourceConsumption(ResourceScope.Machine)]
1105 Value[(int) LoaderInformation.ShadowCopyDirectoriesValue] = value;
1109 internal static String ShadowCopyDirectoriesKey
1112 return ACTAG_APP_SHADOW_COPY_DIRS;
1116 public String ShadowCopyFiles
1119 return Value[(int) LoaderInformation.ShadowCopyFilesValue];
1123 if((value != null) &&
1124 (String.Compare(value, "true", StringComparison.OrdinalIgnoreCase) == 0))
1125 Value[(int) LoaderInformation.ShadowCopyFilesValue] = value;
1127 Value[(int) LoaderInformation.ShadowCopyFilesValue] = null;
1131 internal static String ShadowCopyFilesKey
1134 return ACTAG_FORCE_CACHE_INSTALL;
1138 public String CachePath
1140 [System.Security.SecuritySafeCritical] // auto-generated
1141 [ResourceExposure(ResourceScope.Machine)]
1142 [ResourceConsumption(ResourceScope.Machine)]
1144 return VerifyDir(Value[(int) LoaderInformation.CachePathValue], false);
1147 [ResourceExposure(ResourceScope.Machine)]
1148 [ResourceConsumption(ResourceScope.Machine)]
1150 Value[(int) LoaderInformation.CachePathValue] = NormalizePath(value, false);
1154 internal static String CachePathKey
1157 return ACTAG_APP_CACHE_BASE;
1161 public String LicenseFile
1163 [System.Security.SecuritySafeCritical] // auto-generated
1164 [ResourceExposure(ResourceScope.Machine)]
1165 [ResourceConsumption(ResourceScope.Machine)]
1167 return VerifyDir(Value[(int) LoaderInformation.LicenseFileValue], true);
1170 [ResourceExposure(ResourceScope.Machine)]
1171 [ResourceConsumption(ResourceScope.Machine)]
1173 Value[(int) LoaderInformation.LicenseFileValue] = value;
1177 public LoaderOptimization LoaderOptimization
1180 return _LoaderOptimization;
1184 _LoaderOptimization = value;
1188 internal static string LoaderOptimizationKey
1191 return LOADER_OPTIMIZATION;
1195 internal static string ConfigurationExtension
1198 return CONFIGURATION_EXTENSION;
1202 internal static String PrivateBinPathEnvironmentVariable
1205 return APPENV_RELATIVEPATH;
1209 internal static string RuntimeConfigurationFile
1212 return MACHINE_CONFIGURATION_FILE;
1216 internal static string MachineConfigKey
1219 return ACTAG_MACHINE_CONFIG;
1223 internal static string HostBindingKey
1226 return ACTAG_HOST_CONFIG_FILE;
1232 [ResourceExposure(ResourceScope.None)]
1233 [ResourceConsumption(ResourceScope.Machine, ResourceScope.Machine)]
1234 internal bool UpdateContextPropertyIfNeeded(LoaderInformation FieldValue, String FieldKey, String UpdatedField, IntPtr fusionContext, AppDomainSetup oldADS)
1236 String FieldString = Value[(int) FieldValue],
1237 OldFieldString = (oldADS == null ? null : oldADS.Value[(int) FieldValue]);
1238 if (FieldString != OldFieldString) { // Compare references since strings are immutable
1239 UpdateContextProperty(fusionContext, FieldKey, UpdatedField == null ? FieldString : UpdatedField);
1247 [ResourceExposure(ResourceScope.None)]
1248 [ResourceConsumption(ResourceScope.Machine, ResourceScope.Machine)]
1249 internal void UpdateBooleanContextPropertyIfNeeded(LoaderInformation FieldValue, String FieldKey, IntPtr fusionContext, AppDomainSetup oldADS)
1251 if (Value[(int) FieldValue] != null)
1252 UpdateContextProperty(fusionContext, FieldKey, "true");
1253 else if (oldADS != null && oldADS.Value[(int) FieldValue] != null)
1254 UpdateContextProperty(fusionContext, FieldKey, "false");
1258 [ResourceExposure(ResourceScope.None)]
1259 [ResourceConsumption(ResourceScope.Machine, ResourceScope.Machine)]
1260 internal static bool ByteArraysAreDifferent(Byte[] A, Byte[] B)
1262 int length = A.Length;
1263 if (length != B.Length)
1266 for(int i = 0; i < length; i++) {
1274 [System.Security.SecurityCritical] // auto-generated
1275 [ResourceExposure(ResourceScope.None)]
1276 [ResourceConsumption(ResourceScope.Machine, ResourceScope.Machine)]
1277 internal static void UpdateByteArrayContextPropertyIfNeeded(Byte[] NewArray, Byte[] OldArray, String FieldKey, IntPtr fusionContext)
1279 if ((NewArray != null && OldArray == null) ||
1280 (NewArray == null && OldArray != null) ||
1281 (NewArray != null && OldArray != null && ByteArraysAreDifferent(NewArray, OldArray)))
1282 UpdateContextProperty(fusionContext, FieldKey, NewArray);
1285 [System.Security.SecurityCritical] // auto-generated
1286 [ResourceExposure(ResourceScope.None)]
1287 [ResourceConsumption(ResourceScope.Machine, ResourceScope.Machine)]
1288 internal void SetupFusionContext(IntPtr fusionContext, AppDomainSetup oldADS)
1290 UpdateContextPropertyIfNeeded(LoaderInformation.ApplicationBaseValue, ApplicationBaseKey, null, fusionContext, oldADS);
1291 UpdateContextPropertyIfNeeded(LoaderInformation.PrivateBinPathValue, PrivateBinPathKey, null, fusionContext, oldADS);
1292 UpdateContextPropertyIfNeeded(LoaderInformation.DevPathValue, DeveloperPathKey, null, fusionContext, oldADS);
1294 UpdateBooleanContextPropertyIfNeeded(LoaderInformation.DisallowPublisherPolicyValue, DisallowPublisherPolicyKey, fusionContext, oldADS);
1295 UpdateBooleanContextPropertyIfNeeded(LoaderInformation.DisallowCodeDownloadValue, DisallowCodeDownloadKey, fusionContext, oldADS);
1296 UpdateBooleanContextPropertyIfNeeded(LoaderInformation.DisallowBindingRedirectsValue, DisallowBindingRedirectsKey, fusionContext, oldADS);
1297 UpdateBooleanContextPropertyIfNeeded(LoaderInformation.DisallowAppBaseProbingValue, DisallowAppBaseProbingKey, fusionContext, oldADS);
1299 if(UpdateContextPropertyIfNeeded(LoaderInformation.ShadowCopyFilesValue, ShadowCopyFilesKey, ShadowCopyFiles, fusionContext, oldADS)) {
1301 // If we are asking for shadow copy directories then default to
1302 // only to the ones that are in the private bin path.
1303 if(Value[(int) LoaderInformation.ShadowCopyDirectoriesValue] == null)
1304 ShadowCopyDirectories = BuildShadowCopyDirectories();
1306 UpdateContextPropertyIfNeeded(LoaderInformation.ShadowCopyDirectoriesValue, ShadowCopyDirectoriesKey, null, fusionContext, oldADS);
1309 UpdateContextPropertyIfNeeded(LoaderInformation.CachePathValue, CachePathKey, null, fusionContext, oldADS);
1310 UpdateContextPropertyIfNeeded(LoaderInformation.PrivateBinPathProbeValue, PrivateBinPathProbeKey, PrivateBinPathProbe, fusionContext, oldADS);
1311 UpdateContextPropertyIfNeeded(LoaderInformation.ConfigurationFileValue, ConfigurationFileKey, null, fusionContext, oldADS);
1313 UpdateByteArrayContextPropertyIfNeeded(_ConfigurationBytes, oldADS == null ? null : oldADS.GetConfigurationBytes(), ConfigurationBytesKey, fusionContext);
1315 UpdateContextPropertyIfNeeded(LoaderInformation.ApplicationNameValue, ApplicationNameKey, ApplicationName, fusionContext, oldADS);
1316 UpdateContextPropertyIfNeeded(LoaderInformation.DynamicBaseValue, DynamicBaseKey, null, fusionContext, oldADS);
1318 // Always add the runtime configuration file to the appdomain
1319 UpdateContextProperty(fusionContext, MachineConfigKey, RuntimeEnvironment.GetRuntimeDirectoryImpl() + RuntimeConfigurationFile);
1321 String hostBindingFile = RuntimeEnvironment.GetHostBindingFile();
1322 if(hostBindingFile != null || oldADS != null) // If oldADS != null, we don't know the old value of the hostBindingFile, so we force an update even when hostBindingFile == null.
1323 UpdateContextProperty(fusionContext, HostBindingKey, hostBindingFile);
1326 [System.Security.SecurityCritical] // auto-generated
1327 [ResourceExposure(ResourceScope.None)]
1328 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1329 internal static extern void UpdateContextProperty(IntPtr fusionContext, string key, Object value);
1330 #endif // FEATURE_FUSION
1332 static internal int Locate(String s)
1334 if(String.IsNullOrEmpty(s))
1338 // verify assumptions hardcoded into the switch below
1339 Contract.Assert('A' == ACTAG_APP_CONFIG_FILE[0] , "Assumption violated");
1340 Contract.Assert('A' == ACTAG_APP_NAME[0] , "Assumption violated");
1341 Contract.Assert('A' == ACTAG_APP_BASE_URL[0] , "Assumption violated");
1342 Contract.Assert('B' == ACTAG_BINPATH_PROBE_ONLY[0] , "Assumption violated");
1343 Contract.Assert('C' == ACTAG_APP_CACHE_BASE[0] , "Assumption violated");
1344 Contract.Assert('D' == ACTAG_DEV_PATH[0] , "Assumption violated");
1345 Contract.Assert('D' == ACTAG_APP_DYNAMIC_BASE[0] , "Assumption violated");
1346 Contract.Assert('F' == ACTAG_FORCE_CACHE_INSTALL[0] , "Assumption violated");
1347 Contract.Assert('L' == LICENSE_FILE[0] , "Assumption violated");
1348 Contract.Assert('P' == ACTAG_APP_PRIVATE_BINPATH[0] , "Assumption violated");
1349 Contract.Assert('S' == ACTAG_APP_SHADOW_COPY_DIRS[0], "Assumption violated");
1350 Contract.Assert('D' == ACTAG_DISALLOW_APPLYPUBLISHERPOLICY[0], "Assumption violated");
1351 Contract.Assert('C' == ACTAG_CODE_DOWNLOAD_DISABLED[0], "Assumption violated");
1352 Contract.Assert('D' == ACTAG_DISALLOW_APP_BINDING_REDIRECTS[0], "Assumption violated");
1353 Contract.Assert('D' == ACTAG_DISALLOW_APP_BASE_PROBING[0], "Assumption violated");
1354 Contract.Assert('A' == ACTAG_APP_CONFIG_BLOB[0], "Assumption violated");
1358 if (s == ACTAG_APP_CONFIG_FILE) return (int)LoaderInformation.ConfigurationFileValue;
1359 if (s == ACTAG_APP_NAME) return (int)LoaderInformation.ApplicationNameValue;
1360 if (s == ACTAG_APP_BASE_URL) return (int)LoaderInformation.ApplicationBaseValue;
1361 if (s == ACTAG_APP_CONFIG_BLOB) return (int)LoaderInformation.ConfigurationBytesValue;
1364 if (s == ACTAG_BINPATH_PROBE_ONLY) return (int)LoaderInformation.PrivateBinPathProbeValue;
1367 if (s == ACTAG_APP_CACHE_BASE) return (int)LoaderInformation.CachePathValue;
1368 if (s == ACTAG_CODE_DOWNLOAD_DISABLED) return (int)LoaderInformation.DisallowCodeDownloadValue;
1371 if (s == ACTAG_DEV_PATH) return (int)LoaderInformation.DevPathValue;
1372 if (s == ACTAG_APP_DYNAMIC_BASE) return (int)LoaderInformation.DynamicBaseValue;
1373 if (s == ACTAG_DISALLOW_APPLYPUBLISHERPOLICY) return (int)LoaderInformation.DisallowPublisherPolicyValue;
1374 if (s == ACTAG_DISALLOW_APP_BINDING_REDIRECTS) return (int)LoaderInformation.DisallowBindingRedirectsValue;
1375 if (s == ACTAG_DISALLOW_APP_BASE_PROBING) return (int)LoaderInformation.DisallowAppBaseProbingValue;
1378 if (s == ACTAG_FORCE_CACHE_INSTALL) return (int)LoaderInformation.ShadowCopyFilesValue;
1381 if (s == LICENSE_FILE) return (int)LoaderInformation.LicenseFileValue;
1384 if (s == ACTAG_APP_PRIVATE_BINPATH) return (int)LoaderInformation.PrivateBinPathValue;
1387 if (s == ACTAG_APP_SHADOW_COPY_DIRS) return (int)LoaderInformation.ShadowCopyDirectoriesValue;
1391 Contract.Assert('A' == ACTAG_APP_BASE_URL[0] , "Assumption violated");
1392 if (s[0]=='A' && s == ACTAG_APP_BASE_URL)
1393 return (int)LoaderInformation.ApplicationBaseValue;
1395 #endif //FEATURE_FUSION
1400 private string BuildShadowCopyDirectories()
1402 // Default to only to the ones that are in the private bin path.
1403 String binPath = Value[(int) LoaderInformation.PrivateBinPathValue];
1407 StringBuilder result = StringBuilderCache.Acquire();
1408 String appBase = Value[(int) LoaderInformation.ApplicationBaseValue];
1409 if(appBase != null) {
1411 string[] directories = binPath.Split(sep);
1412 int size = directories.Length;
1413 bool appendSlash = !( (appBase[appBase.Length-1] == '/') ||
1414 (appBase[appBase.Length-1] == '\\') );
1417 result.Append(appBase);
1419 result.Append('\\');
1420 result.Append(binPath);
1423 for(int i = 0; i < size; i++) {
1424 result.Append(appBase);
1426 result.Append('\\');
1427 result.Append(directories[i]);
1435 return StringBuilderCache.GetStringAndRelease(result);
1437 #endif // FEATURE_FUSION
1439 #if FEATURE_COMINTEROP
1440 public bool SandboxInterop
1444 return _DisableInterfaceCache;
1448 _DisableInterfaceCache = value;
1451 #endif // FEATURE_COMINTEROP