Skip to content

Commit a52fffd

Browse files
authored
Delete System.AppDomainSetup (dotnet/coreclr#21157)
* Delete System.AppDomainSetup Contributes to dotnet/coreclr#21028 * Add test hook for null entry assembly * Validate that the binder paths are absolute Commit migrated from dotnet/coreclr@e54dffe
1 parent ef27385 commit a52fffd

29 files changed

Lines changed: 120 additions & 946 deletions

File tree

src/coreclr/src/System.Private.CoreLib/Resources/Strings.resx

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3526,9 +3526,6 @@
35263526
<data name="UnauthorizedAccess_RegistryKeyGeneric_Key" xml:space="preserve">
35273527
<value>Access to the registry key '{0}' is denied.</value>
35283528
</data>
3529-
<data name="UnauthorizedAccess_SystemDomain" xml:space="preserve">
3530-
<value>Cannot execute an assembly in the system domain.</value>
3531-
</data>
35323529
<data name="UnknownError_Num" xml:space="preserve">
35333530
<value>Unknown error "{0}".</value>
35343531
</data>

src/coreclr/src/System.Private.CoreLib/System.Private.CoreLib.csproj

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,6 @@
141141
<Compile Include="$(BclSourcesRoot)\System\Runtime\InteropServices\Marshal.cs" />
142142
<Compile Include="$(BclSourcesRoot)\System\Runtime\InteropServices\PInvokeMap.cs" />
143143
<Compile Include="$(BclSourcesRoot)\System\Runtime\InteropServices\PInvokeMarshal.cs" />
144-
<Compile Include="$(BclSourcesRoot)\System\Runtime\InteropServices\RuntimeEnvironment.cs" />
145144
<Compile Include="$(BclSourcesRoot)\System\Runtime\InteropServices\SEHException.cs" />
146145
<Compile Include="$(BclSourcesRoot)\System\Runtime\InteropServices\SafeHandle.cs" />
147146
<Compile Include="$(BclSourcesRoot)\System\Runtime\InteropServices\BStrWrapper.cs" />
@@ -259,8 +258,6 @@
259258
<Compile Include="$(BclSourcesRoot)\System\MulticastDelegate.cs" />
260259
<Compile Include="$(BclSourcesRoot)\System\Activator.cs" />
261260
<Compile Include="$(BclSourcesRoot)\System\AppDomain.cs" />
262-
<Compile Include="$(BclSourcesRoot)\System\AppDomainSetup.cs" />
263-
<Compile Include="$(BclSourcesRoot)\System\AppDomainManager.cs" />
264261
<Compile Include="$(BclSourcesRoot)\System\AppDomainUnloadedException.cs" />
265262
<Compile Include="$(BclSourcesRoot)\System\ArgIterator.cs" />
266263
<Compile Include="$(BclSourcesRoot)\System\Attribute.cs" />

src/coreclr/src/System.Private.CoreLib/src/System/Activator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ private static ObjectHandle CreateInstanceInternal(string assemblyString,
123123
Assembly assembly = null;
124124
if (assemblyString == null)
125125
{
126-
assembly = RuntimeAssembly.GetExecutingAssembly(ref stackMark);
126+
assembly = Assembly.GetExecutingAssembly(ref stackMark);
127127
}
128128
else
129129
{

src/coreclr/src/System.Private.CoreLib/src/System/AppContext/AppContext.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55

66
using System.Collections.Generic;
7+
using System.IO;
78
using System.Reflection;
89
using System.Runtime.Versioning;
910

@@ -37,7 +38,15 @@ public static string BaseDirectory
3738
{
3839
// The value of APP_CONTEXT_BASE_DIRECTORY key has to be a string and it is not allowed to be any other type.
3940
// Otherwise the caller will get invalid cast exception
40-
return (string)AppDomain.CurrentDomain.GetData("APP_CONTEXT_BASE_DIRECTORY") ?? AppDomain.CurrentDomain.BaseDirectory;
41+
string baseDirectory = (string)AppDomain.CurrentDomain.GetData("APP_CONTEXT_BASE_DIRECTORY");
42+
if (baseDirectory != null)
43+
return baseDirectory;
44+
45+
// Fallback path for hosts that do not set APP_CONTEXT_BASE_DIRECTORY explicitly
46+
string directory = Path.GetDirectoryName(Assembly.GetEntryAssembly()?.Location);
47+
if (directory != null && !PathInternal.EndsInDirectorySeparator(directory))
48+
directory += Path.DirectorySeparatorChar;
49+
return directory;
4150
}
4251
}
4352

src/coreclr/src/System.Private.CoreLib/src/System/AppDomain.cs

Lines changed: 5 additions & 213 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,7 @@ internal sealed class AppDomain
2828
// of these fields cannot be changed without changing the layout in
2929
// the EE- AppDomainBaseObject in this case)
3030

31-
private AppDomainManager _domainManager;
3231
private Dictionary<string, object> _LocalStore;
33-
private AppDomainSetup _FusionStore;
3432
public event AssemblyLoadEventHandler AssemblyLoad;
3533

3634
private ResolveEventHandler _TypeResolve;
@@ -102,18 +100,11 @@ public event ResolveEventHandler AssemblyResolve
102100

103101
private UnhandledExceptionEventHandler _unhandledException;
104102

105-
// The compat flags are set at domain creation time to indicate that the given breaking
106-
// changes (named in the strings) should not be used in this domain. We only use the
107-
// keys, the vhe values are ignored.
108-
private Dictionary<string, object> _compatFlags;
109-
110103
// Delegate that will hold references to FirstChance exception notifications
111104
private EventHandler<FirstChanceExceptionEventArgs> _firstChanceException;
112105

113106
private IntPtr _pDomain; // this is an unmanaged pointer (AppDomain * m_pDomain)` used from the VM.
114107

115-
private bool _compatFlagsInitialized;
116-
117108
#if FEATURE_APPX
118109
private static APPX_FLAGS s_flags;
119110

@@ -151,9 +142,6 @@ private static APPX_FLAGS Flags
151142
/// </summary>
152143
private void CreateAppDomainManager()
153144
{
154-
Debug.Assert(_domainManager == null, "_domainManager == null");
155-
156-
AppDomainSetup adSetup = FusionStore;
157145
string trustedPlatformAssemblies = (string)GetData("TRUSTED_PLATFORM_ASSEMBLIES");
158146
if (trustedPlatformAssemblies != null)
159147
{
@@ -163,29 +151,6 @@ private void CreateAppDomainManager()
163151
string appLocalWinMD = (string)GetData("APP_LOCAL_WINMETADATA") ?? string.Empty;
164152
SetupBindingPaths(trustedPlatformAssemblies, platformResourceRoots, appPaths, appNiPaths, appLocalWinMD);
165153
}
166-
167-
InitializeCompatibilityFlags();
168-
}
169-
170-
/// <summary>
171-
/// Initialize the compatibility flags to non-NULL values.
172-
/// This method is also called from the VM when the default domain doesn't have a domain manager.
173-
/// </summary>
174-
private void InitializeCompatibilityFlags()
175-
{
176-
AppDomainSetup adSetup = FusionStore;
177-
178-
// set up shim flags regardless of whether we create a DomainManager in this method.
179-
if (adSetup.GetCompatibilityFlags() != null)
180-
{
181-
_compatFlags = new Dictionary<string, object>(adSetup.GetCompatibilityFlags(), StringComparer.OrdinalIgnoreCase);
182-
}
183-
184-
// for perf, we don't intialize the _compatFlags dictionary when we don't need to. However, we do need to make a
185-
// note that we've run this method, because IsCompatibilityFlagsSet needs to return different values for the
186-
// case where the compat flags have been setup.
187-
Debug.Assert(!_compatFlagsInitialized);
188-
_compatFlagsInitialized = true;
189154
}
190155

191156
/// <summary>
@@ -237,12 +202,8 @@ internal static void CheckLoadByteArraySupported()
237202
#endif
238203
}
239204

240-
public AppDomainManager DomainManager => _domainManager;
241-
242205
public static AppDomain CurrentDomain => Thread.GetDomain();
243206

244-
public string BaseDirectory => FusionStore.ApplicationBase;
245-
246207
[MethodImpl(MethodImplOptions.InternalCall)]
247208
private extern Assembly[] nGetAssemblies(bool forIntrospection);
248209

@@ -289,9 +250,6 @@ private AppDomain()
289250
Debug.Fail("Object cannot be created through this constructor.");
290251
}
291252

292-
[MethodImpl(MethodImplOptions.InternalCall)]
293-
internal extern void nCreateContext();
294-
295253
[DllImport(JitHelpers.QCall, CharSet = CharSet.Unicode)]
296254
private static extern void nSetupBindingPaths(string trustedPlatformAssemblies, string platformResourceRoots, string appPath, string appNiPaths, string appLocalWinMD);
297255

@@ -432,15 +390,6 @@ private string[] OnDesignerNamespaceResolveEvent(string namespaceName)
432390
}
433391
#endif // FEATURE_COMINTEROP
434392

435-
internal AppDomainSetup FusionStore
436-
{
437-
get
438-
{
439-
Debug.Assert(_FusionStore != null, "Fusion store has not been correctly setup in this domain");
440-
return _FusionStore;
441-
}
442-
}
443-
444393
private static RuntimeAssembly GetRuntimeAssembly(Assembly asm)
445394
{
446395
return
@@ -461,78 +410,11 @@ private Dictionary<string, object> LocalStore
461410
[DllImport(JitHelpers.QCall, CharSet = CharSet.Unicode)]
462411
private static extern void nSetNativeDllSearchDirectories(string paths);
463412

464-
private void SetupFusionStore(AppDomainSetup info, AppDomainSetup oldInfo)
465-
{
466-
Debug.Assert(info != null);
467-
468-
if (info.ApplicationBase == null)
469-
{
470-
info.SetupDefaults(RuntimeEnvironment.GetModuleFileName(), imageLocationAlreadyNormalized: true);
471-
}
472-
473-
nCreateContext();
474-
475-
// This must be the last action taken
476-
_FusionStore = info;
477-
}
478-
479-
// Used to switch into other AppDomain and call SetupRemoteDomain.
480-
// We cannot simply call through the proxy, because if there
481-
// are any remoting sinks registered, they can add non-mscorlib
482-
// objects to the message (causing an assembly load exception when
483-
// we try to deserialize it on the other side)
484-
private static object PrepareDataForSetup(string friendlyName,
485-
AppDomainSetup setup,
486-
string[] propertyNames,
487-
string[] propertyValues)
488-
{
489-
var newSetup = new AppDomainSetup(setup);
490-
491-
// Remove the special AppDomainCompatSwitch entries from the set of name value pairs
492-
// And add them to the AppDomainSetup
493-
//
494-
// This is only supported on CoreCLR through ICLRRuntimeHost2.CreateAppDomainWithManager
495-
// Desktop code should use System.AppDomain.CreateDomain() or
496-
// System.AppDomainManager.CreateDomain() and add the flags to the AppDomainSetup
497-
var compatList = new List<string>();
498-
499-
if (propertyNames != null && propertyValues != null)
500-
{
501-
for (int i = 0; i < propertyNames.Length; i++)
502-
{
503-
if (string.Equals(propertyNames[i], "AppDomainCompatSwitch", StringComparison.OrdinalIgnoreCase))
504-
{
505-
compatList.Add(propertyValues[i]);
506-
propertyNames[i] = null;
507-
propertyValues[i] = null;
508-
}
509-
}
510-
511-
if (compatList.Count > 0)
512-
{
513-
newSetup.SetCompatibilitySwitches(compatList);
514-
}
515-
}
516-
517-
return new object[]
518-
{
519-
friendlyName,
520-
newSetup,
521-
propertyNames,
522-
propertyValues
523-
};
524-
} // PrepareDataForSetup
525-
526-
private static object Setup(object arg)
413+
private static void Setup(string friendlyName,
414+
string[] propertyNames,
415+
string[] propertyValues)
527416
{
528-
var args = (object[])arg;
529-
var friendlyName = (string)args[0];
530-
var setup = (AppDomainSetup)args[1];
531-
var propertyNames = (string[])args[2]; // can contain null elements
532-
var propertyValues = (string[])args[3]; // can contain null elements
533-
534417
AppDomain ad = CurrentDomain;
535-
var newSetup = new AppDomainSetup(setup);
536418

537419
if (propertyNames != null && propertyValues != null)
538420
{
@@ -555,107 +437,17 @@ private static object Setup(object arg)
555437

556438
for (int i = 0; i < propertyNames.Length; i++)
557439
{
558-
if (propertyNames[i] == "APPBASE") // make sure in sync with Fusion
559-
{
560-
if (propertyValues[i] == null)
561-
throw new ArgumentNullException("APPBASE");
562-
563-
if (PathInternal.IsPartiallyQualified(propertyValues[i]))
564-
throw new ArgumentException(SR.Argument_AbsolutePathRequired);
565-
566-
newSetup.ApplicationBase = NormalizePath(propertyValues[i], fullCheck: true);
567-
}
568-
else if (propertyNames[i] == "TRUSTED_PLATFORM_ASSEMBLIES" ||
569-
propertyNames[i] == "PLATFORM_RESOURCE_ROOTS" ||
570-
propertyNames[i] == "APP_PATHS" ||
571-
propertyNames[i] == "APP_NI_PATHS")
572-
{
573-
string values = propertyValues[i];
574-
if (values == null)
575-
throw new ArgumentNullException(propertyNames[i]);
576-
577-
ad.SetData(propertyNames[i], NormalizeAppPaths(values));
578-
}
579-
else if (propertyNames[i] != null)
440+
if (propertyNames[i] != null)
580441
{
581-
ad.SetData(propertyNames[i], propertyValues[i]); // just propagate
442+
ad.SetData(propertyNames[i], propertyValues[i]);
582443
}
583444
}
584445
}
585446

586-
ad.SetupFusionStore(newSetup, null); // makes FusionStore a ref to newSetup
587-
588-
// technically, we don't need this, newSetup refers to the same object as FusionStore
589-
// but it's confusing since it isn't immediately obvious whether we have a ref or a copy
590-
AppDomainSetup adSetup = ad.FusionStore;
591-
592447
// set up the friendly name
593448
ad.nSetupFriendlyName(friendlyName);
594449

595450
ad.CreateAppDomainManager(); // could modify FusionStore's object
596-
597-
return null;
598-
}
599-
600-
private static string NormalizeAppPaths(string values)
601-
{
602-
int estimatedLength = values.Length + 1; // +1 for extra separator temporarily added at end
603-
StringBuilder sb = StringBuilderCache.Acquire(estimatedLength);
604-
605-
for (int pos = 0; pos < values.Length; pos++)
606-
{
607-
string path;
608-
609-
int nextPos = values.IndexOf(Path.PathSeparator, pos);
610-
if (nextPos == -1)
611-
{
612-
path = values.Substring(pos);
613-
pos = values.Length - 1;
614-
}
615-
else
616-
{
617-
path = values.Substring(pos, nextPos - pos);
618-
pos = nextPos;
619-
}
620-
621-
// Skip empty directories
622-
if (path.Length == 0)
623-
continue;
624-
625-
if (PathInternal.IsPartiallyQualified(path))
626-
throw new ArgumentException(SR.Argument_AbsolutePathRequired);
627-
628-
string appPath = NormalizePath(path, fullCheck: true);
629-
sb.Append(appPath);
630-
sb.Append(Path.PathSeparator);
631-
}
632-
633-
// Strip the last separator
634-
if (sb.Length > 0)
635-
{
636-
sb.Remove(sb.Length - 1, 1);
637-
}
638-
639-
return StringBuilderCache.GetStringAndRelease(sb);
640-
}
641-
642-
internal static string NormalizePath(string path, bool fullCheck) => Path.GetFullPath(path);
643-
644-
// This routine is called from unmanaged code to
645-
// set the default fusion context.
646-
private void SetupDomain(bool allowRedirects, string path, string configFile, string[] propertyNames, string[] propertyValues)
647-
{
648-
// It is possible that we could have multiple threads initializing
649-
// the default domain. We will just take the winner of these two.
650-
// (eg. one thread doing a com call and another doing attach for IJW)
651-
lock (this)
652-
{
653-
if (_FusionStore == null)
654-
{
655-
// always use internet permission set
656-
SetupFusionStore(new AppDomainSetup(), null);
657-
}
658-
}
659451
}
660452

661453
[MethodImpl(MethodImplOptions.InternalCall)]

0 commit comments

Comments
 (0)