Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make System.Drawing.Common throw on Unix unless a config switch is set #55962

Merged
merged 9 commits into from
Jul 21, 2021
3 changes: 1 addition & 2 deletions src/libraries/System.Drawing.Common/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -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.
safern marked this conversation as resolved.
Show resolved Hide resolved

Commonly Used Types:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,9 @@
<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>
safern marked this conversation as resolved.
Show resolved Hide resolved
</data>
<data name="PrintDocumentDesc" xml:space="preserve">
<value>Defines an object that sends output to a printer.</value>
</data>
Expand Down Expand Up @@ -464,4 +467,4 @@
<data name="SystemDrawingCommon_PlatformNotSupported" xml:space="preserve">
<value>System.Drawing.Common is not supported on this platform.</value>
</data>
</root>
</root>
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,8 @@
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"
Expand Down Expand Up @@ -207,7 +209,7 @@
<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" />
Expand Down Expand Up @@ -243,8 +245,6 @@
<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"
Expand Down Expand Up @@ -359,6 +359,7 @@
<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" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
safern marked this conversation as resolved.
Show resolved Hide resolved
using System.Collections.Generic;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ private static IntPtr DllImportResolver(string libraryName, Assembly assembly, D

internal static void EnsureRegistered()
{
// dummy call to trigger the static constructor
if (!LocalAppContextSwitches.EnableUnixSupport)
throw new PlatformNotSupportedException(SR.PlatformNotSupported_Unix);
}
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
}
13 changes: 13 additions & 0 deletions src/libraries/System.Drawing.Common/tests/BitmapTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using Microsoft.DotNet.RemoteExecutor;
using Microsoft.DotNet.XUnitExtensions;
using Xunit;

Expand All @@ -44,6 +45,18 @@ public static IEnumerable<object[]> Ctor_FilePath_TestData()
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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why set it if it's false by default? Seems we should test default, and also test when explicitly enabled.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We set it to false here as the runtimeconfig is setting it to true and the remote executor uses that same runtimeconfig file when creating the new process.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we accidentally changed the default to true, would any test fail?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nope. I could however try to add a test to ensure the default is false.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In order to achieve this we would need to either create our own process or a separate assembly that doesn't set the value on the runtimeconfig.json, so I believe it is pretty complicated and not worth it, as in order to change the default value we would have to hardcode it in LocalAppContextSwitches like here:

if (switchName == "Switch.System.Runtime.Serialization.SerializationGuard")

Given that this is a one time release thing and already late on the release I think we are safe to not test that default value.

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)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"configProperties": {
"System.Drawing.EnableUnixSupport": true
safern marked this conversation as resolved.
Show resolved Hide resolved
}
}