From 96740b835f1faa1e2d6abfeec27f719abf20a234 Mon Sep 17 00:00:00 2001 From: Jeremy Kuhne Date: Thu, 8 Dec 2016 13:10:38 -0800 Subject: [PATCH] Move native search paths forward (#8531) Set native search paths in AppDomain.Setup before doing the rest of the setup steps to get ahead of potential P/Invoke calls. --- src/mscorlib/src/System/AppDomain.cs | 128 +++++++++++++++++------------------ 1 file changed, 64 insertions(+), 64 deletions(-) diff --git a/src/mscorlib/src/System/AppDomain.cs b/src/mscorlib/src/System/AppDomain.cs index b6c54b5..8ed1e5f 100644 --- a/src/mscorlib/src/System/AppDomain.cs +++ b/src/mscorlib/src/System/AppDomain.cs @@ -188,7 +188,7 @@ namespace System // Domain security information // These fields initialized from the other side only. (NOTE: order // of these fields cannot be changed without changing the layout in - // the EE) + // the EE- AppDomainBaseObject in this case) [System.Security.SecurityCritical] // auto-generated private AppDomainManager _domainManager; @@ -2244,13 +2244,27 @@ namespace System Evidence providedSecurityInfo = null; Evidence creatorsSecurityInfo = null; - AppDomain ad = AppDomain.CurrentDomain; AppDomainSetup newSetup=new AppDomainSetup(setup,false); if(propertyNames!=null && propertyValues != null) { - StringBuilder normalisedAppPathList = null; + for (int i = 0; i < propertyNames.Length; i++) + { + // We want to set native dll probing directories before any P/Invokes have a + // chance to fire. The Path class, for one, has P/Invokes. + if (propertyNames[i] == "NATIVE_DLL_SEARCH_DIRECTORIES") + { + if (propertyValues[i] == null) + throw new ArgumentNullException("NATIVE_DLL_SEARCH_DIRECTORIES"); + + string paths = propertyValues[i]; + if (paths.Length == 0) + break; + + nSetNativeDllSearchDirectories(paths); + } + } for (int i=0; i 0) - { - normalisedAppPathList.Remove(normalisedAppPathList.Length - 1, 1); - } - ad.SetDataHelper(propertyNames[i],normalisedAppPathList.ToString(),null); // not supported by fusion, so set explicitly + ad.SetDataHelper(propertyNames[i], NormalizeAppPaths(values), null); } - else - if(propertyNames[i]!= null) + else if(propertyNames[i]!= null) { ad.SetDataHelper(propertyNames[i],propertyValues[i],null); // just propagate } @@ -2393,6 +2351,48 @@ namespace System return null; } + private static string NormalizeAppPaths(string values) + { + int estimatedLength = values.Length + 1; // +1 for extra separator temporarily added at end + StringBuilder sb = StringBuilderCache.Acquire(estimatedLength); + + for (int pos = 0; pos < values.Length; pos++) + { + string path; + + int nextPos = values.IndexOf(Path.PathSeparator, pos); + if (nextPos == -1) + { + path = values.Substring(pos); + pos = values.Length - 1; + } + else + { + path = values.Substring(pos, nextPos - pos); + pos = nextPos; + } + + // Skip empty directories + if (path.Length == 0) + continue; + + if (PathInternal.IsPartiallyQualified(path)) + throw new ArgumentException(Environment.GetResourceString("Argument_AbsolutePathRequired")); + + string appPath = NormalizePath(path, fullCheck: true); + sb.Append(appPath); + sb.Append(Path.PathSeparator); + } + + // Strip the last separator + if (sb.Length > 0) + { + sb.Remove(sb.Length - 1, 1); + } + + return StringBuilderCache.GetStringAndRelease(sb); + } + [SecuritySafeCritical] internal static string NormalizePath(string path, bool fullCheck) { -- 2.7.4