Make System.Drawing.Common throw on Unix unless a config switch is set (#55962)
authorSantiago Fernandez Madero <safern@microsoft.com>
Wed, 21 Jul 2021 22:59:52 +0000 (15:59 -0700)
committerGitHub <noreply@github.com>
Wed, 21 Jul 2021 22:59:52 +0000 (15:59 -0700)
* Make System.Drawing.Common throw on Unix unless a config switch is set

src/libraries/System.Drawing.Common/Directory.Build.props
src/libraries/System.Drawing.Common/src/Resources/Strings.resx
src/libraries/System.Drawing.Common/src/System.Drawing.Common.csproj
src/libraries/System.Drawing.Common/src/System/Drawing/LibraryResolver.cs
src/libraries/System.Drawing.Common/src/System/Drawing/LocalAppContextSwitches.Unix.cs [new file with mode: 0644]
src/libraries/System.Drawing.Common/src/System/Drawing/LocalAppContextSwitches.Windows.cs [moved from src/libraries/System.Drawing.Common/src/System/Drawing/LocalAppContextSwitches.cs with 100% similarity]
src/libraries/System.Drawing.Common/tests/BitmapTests.cs
src/libraries/System.Drawing.Common/tests/runtimeconfig.template.json [new file with mode: 0644]
src/libraries/System.Resources.Extensions/tests/runtimeconfig.template.json [new file with mode: 0644]
src/libraries/System.Resources.ResourceManager/tests/runtimeconfig.template.json [new file with mode: 0644]
src/libraries/System.Runtime.Serialization.Formatters/tests/runtimeconfig.template.json [new file with mode: 0644]

index addd07b..9ef62c6 100644 (file)
@@ -2,8 +2,7 @@
   <Import Project="..\Directory.Build.props" />
   <PropertyGroup>
     <StrongNameKeyId>Open</StrongNameKeyId>
-    <IncludePlatformAttributes>true</IncludePlatformAttributes>
-    <UnsupportedOSPlatforms>browser</UnsupportedOSPlatforms>
+    <SupportedOSPlatforms>windows</SupportedOSPlatforms>
     <PackageDescription>Provides access to GDI+ graphics functionality.
 
 Commonly Used Types:
@@ -12,6 +11,8 @@ System.Drawing.BitmapData
 System.Drawing.Brush
 System.Drawing.Font
 System.Drawing.Graphics
-System.Drawing.Icon</PackageDescription>
+System.Drawing.Icon
+
+Unix support is disabled by default. See https://aka.ms/systemdrawingnonwindows for more information.</PackageDescription>
   </PropertyGroup>
 </Project>
\ No newline at end of file
index 969bd00..2d3d813 100644 (file)
   <data name="PlatformNotSupported_Drawing" xml:space="preserve">
     <value>System.Drawing is not supported on this platform.</value>
   </data>
+  <data name="PlatformNotSupported_Unix" xml:space="preserve">
+    <value>System.Drawing.Common is not supported on non-Windows platforms. See https://aka.ms/systemdrawingnonwindows for more information.</value>
+  </data>
   <data name="PrintDocumentDesc" xml:space="preserve">
     <value>Defines an object that sends output to a printer.</value>
   </data>
   <data name="SystemDrawingCommon_PlatformNotSupported" xml:space="preserve">
     <value>System.Drawing.Common is not supported on this platform.</value>
   </data>
-</root>
\ No newline at end of file
+</root>
index ff70787..f571be1 100644 (file)
              Link="Common\Interop\Windows\User32\Interop.LOGFONT.cs" />
     <Compile Include="$(CommonPath)Interop\Windows\Gdi32\Interop.RasterOp.cs"
              Link="Common\Interop\Windows\Gdi32\Interop.RasterOp.cs" />
+    <Compile Include="$(CommonPath)System\LocalAppContextSwitches.Common.cs"
+             Link="System\LocalAppContextSwitches.Common.cs" />
     <Compile Include="$(CommonPath)System\Text\ValueStringBuilder.cs"
              Link="Common\System\Text\ValueStringBuilder.cs" />
     <Compile Include="$(CommonPath)System\Obsoletions.cs"
     <Compile Include="System\Drawing\Internal\GpPathData.cs" />
     <Compile Include="System\Drawing\Internal\GPStream.cs" />
     <Compile Include="System\Drawing\Internal\SystemColorTracker.cs" />
-    <Compile Include="System\Drawing\LocalAppContextSwitches.cs" />
+    <Compile Include="System\Drawing\LocalAppContextSwitches.Windows.cs" />
     <Compile Include="System\Drawing\Pen.Windows.cs" />
     <Compile Include="System\Drawing\Printing\DefaultPrintController.cs" />
     <Compile Include="System\Drawing\Printing\ModeField.cs" />
     <Compile Include="Interop\Windows\Interop.Shell32.cs" />
     <Compile Include="Interop\Windows\Interop.User32.cs" />
     <Compile Include="Interop\Windows\Interop.Winspool.cs" />
-    <Compile Include="$(CommonPath)System\LocalAppContextSwitches.Common.cs"
-             Link="System\LocalAppContextSwitches.Common.cs" />
     <Compile Include="$(CommonPath)Interop\Windows\Interop.Libraries.cs"
              Link="Common\Interop\Windows\Interop.Libraries.cs" />
     <Compile Include="$(CommonPath)Interop\Windows\Gdi32\Interop.CombineRgn.cs"
     <Compile Include="System\Drawing\GdiplusNative.Unix.cs" />
     <Compile Include="System\Drawing\GdiPlusStreamHelper.Unix.cs" />
     <Compile Include="System\Drawing\LibX11Functions.cs" />
+    <Compile Include="System\Drawing\LocalAppContextSwitches.Unix.cs" />
     <Compile Include="System\Drawing\MarshallingHelpers.cs" />
     <Compile Include="System\Drawing\Image.Unix.cs" />
     <Compile Include="System\Drawing\Pen.Unix.cs" />
index 8f10899..cb4bdf3 100644 (file)
@@ -27,7 +27,8 @@ namespace System.Drawing
 
         internal static void EnsureRegistered()
         {
-            // dummy call to trigger the static constructor
+            if (!LocalAppContextSwitches.EnableUnixSupport)
+                throw new PlatformNotSupportedException(SR.PlatformNotSupported_Unix);
         }
     }
 }
diff --git a/src/libraries/System.Drawing.Common/src/System/Drawing/LocalAppContextSwitches.Unix.cs b/src/libraries/System.Drawing.Common/src/System/Drawing/LocalAppContextSwitches.Unix.cs
new file mode 100644 (file)
index 0000000..44fe88b
--- /dev/null
@@ -0,0 +1,20 @@
+// Licensed to the .NET Foundation under one or more agreements.
+// The .NET Foundation licenses this file to you under the MIT license.
+
+using System.Runtime.CompilerServices;
+
+namespace System
+{
+    internal static partial class LocalAppContextSwitches
+    {
+        private static int s_enableUnixSupport;
+        public static bool EnableUnixSupport
+        {
+            [MethodImpl(MethodImplOptions.AggressiveInlining)]
+            get
+            {
+                return GetCachedSwitchValue(@"System.Drawing.EnableUnixSupport", ref s_enableUnixSupport);
+            }
+        }
+    }
+}
index d861dbe..0b15dfc 100644 (file)
@@ -29,6 +29,7 @@ using System.Drawing.Imaging;
 using System.IO;
 using System.Linq;
 using System.Runtime.InteropServices;
+using Microsoft.DotNet.RemoteExecutor;
 using Microsoft.DotNet.XUnitExtensions;
 using Xunit;
 
@@ -44,6 +45,18 @@ namespace System.Drawing.Tests
             yield return new object[] { "16x16_nonindexed_24bit.png", 16, 16, PixelFormat.Format24bppRgb, ImageFormat.Png };
         }
 
+        [PlatformSpecific(TestPlatforms.AnyUnix)]
+        [ConditionalFact(typeof(RemoteExecutor), nameof(RemoteExecutor.IsSupported))]
+        public void UnixSupportDisabledThrows()
+        {
+            RemoteExecutor.Invoke(() =>
+            {
+                AppContext.SetSwitch("System.Drawing.EnableUnixSupport", false);
+                TypeInitializationException exception = Assert.Throws<TypeInitializationException>(() => new Bitmap(100, 100));
+                Assert.IsType<PlatformNotSupportedException>(exception.InnerException);
+            }).Dispose();
+        }
+
         [ConditionalTheory(Helpers.IsDrawingSupported)]
         [MemberData(nameof(Ctor_FilePath_TestData))]
         public void Ctor_FilePath(string filename, int width, int height, PixelFormat pixelFormat, ImageFormat rawFormat)
diff --git a/src/libraries/System.Drawing.Common/tests/runtimeconfig.template.json b/src/libraries/System.Drawing.Common/tests/runtimeconfig.template.json
new file mode 100644 (file)
index 0000000..e3ad204
--- /dev/null
@@ -0,0 +1,5 @@
+{
+    "configProperties": {
+        "System.Drawing.EnableUnixSupport": true
+    }
+}
\ No newline at end of file
diff --git a/src/libraries/System.Resources.Extensions/tests/runtimeconfig.template.json b/src/libraries/System.Resources.Extensions/tests/runtimeconfig.template.json
new file mode 100644 (file)
index 0000000..e3ad204
--- /dev/null
@@ -0,0 +1,5 @@
+{
+    "configProperties": {
+        "System.Drawing.EnableUnixSupport": true
+    }
+}
\ No newline at end of file
diff --git a/src/libraries/System.Resources.ResourceManager/tests/runtimeconfig.template.json b/src/libraries/System.Resources.ResourceManager/tests/runtimeconfig.template.json
new file mode 100644 (file)
index 0000000..e3ad204
--- /dev/null
@@ -0,0 +1,5 @@
+{
+    "configProperties": {
+        "System.Drawing.EnableUnixSupport": true
+    }
+}
\ No newline at end of file
diff --git a/src/libraries/System.Runtime.Serialization.Formatters/tests/runtimeconfig.template.json b/src/libraries/System.Runtime.Serialization.Formatters/tests/runtimeconfig.template.json
new file mode 100644 (file)
index 0000000..e3ad204
--- /dev/null
@@ -0,0 +1,5 @@
+{
+    "configProperties": {
+        "System.Drawing.EnableUnixSupport": true
+    }
+}
\ No newline at end of file