-
Notifications
You must be signed in to change notification settings - Fork 4.8k
/
PlatformDetection.cs
283 lines (244 loc) · 11.2 KB
/
PlatformDetection.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System.IO;
using System.Security;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Runtime.CompilerServices;
using Microsoft.Win32;
namespace System
{
public static partial class PlatformDetection
{
//
// Do not use the " { get; } = <expression> " pattern here. Having all the initialization happen in the type initializer
// means that one exception anywhere means all tests using PlatformDetection fail. If you feel a value is worth latching,
// do it in a way that failures don't cascade.
//
public static bool IsNetCore => Environment.Version.Major >= 5 || RuntimeInformation.FrameworkDescription.StartsWith(".NET Core", StringComparison.OrdinalIgnoreCase);
public static bool IsMonoRuntime => Type.GetType("Mono.RuntimeStructs") != null;
public static bool IsMonoInterpreter => GetIsRunningOnMonoInterpreter();
public static bool IsFreeBSD => RuntimeInformation.IsOSPlatform(OSPlatform.Create("FREEBSD"));
public static bool IsNetBSD => RuntimeInformation.IsOSPlatform(OSPlatform.Create("NETBSD"));
public static bool IsArmProcess => RuntimeInformation.ProcessArchitecture == Architecture.Arm;
public static bool IsNotArmProcess => !IsArmProcess;
public static bool IsArm64Process => RuntimeInformation.ProcessArchitecture == Architecture.Arm64;
public static bool IsNotArm64Process => !IsArm64Process;
public static bool IsArmOrArm64Process => IsArmProcess || IsArm64Process;
public static bool IsNotArmNorArm64Process => !IsArmOrArm64Process;
public static bool IsArgIteratorSupported => IsMonoRuntime || (IsWindows && IsNotArmProcess);
public static bool IsArgIteratorNotSupported => !IsArgIteratorSupported;
public static bool Is32BitProcess => IntPtr.Size == 4;
public static bool IsNotWindows => !IsWindows;
// Please make sure that you have the libgdiplus dependency installed.
// For details, see https://docs.microsoft.com/dotnet/core/install/dependencies?pivots=os-macos&tabs=netcore31#libgdiplus
public static bool IsDrawingSupported
{
get
{
#if NETCOREAPP
if (!IsWindows)
{
if (IsOSX)
{
return NativeLibrary.TryLoad("libgdiplus.dylib", out _);
}
else
{
return NativeLibrary.TryLoad("libgdiplus.so", out _) || NativeLibrary.TryLoad("libgdiplus.so.0", out _);
}
}
#endif
return IsNotWindowsNanoServer && IsNotWindowsServerCore;
}
}
public static bool IsInContainer => GetIsInContainer();
public static bool SupportsSsl3 => GetSsl3Support();
#if NETCOREAPP
public static bool IsReflectionEmitSupported = RuntimeFeature.IsDynamicCodeSupported;
#else
public static bool IsReflectionEmitSupported = true;
#endif
public static bool IsInvokingStaticConstructorsSupported => true;
// System.Security.Cryptography.Xml.XmlDsigXsltTransform.GetOutput() relies on XslCompiledTransform which relies
// heavily on Reflection.Emit
public static bool IsXmlDsigXsltTransformSupported => !PlatformDetection.IsInAppContainer;
public static bool IsPreciseGcSupported => !IsMonoRuntime;
public static bool IsNotIntMaxValueArrayIndexSupported => s_largeArrayIsNotSupported.Value;
private static volatile Tuple<bool> s_lazyNonZeroLowerBoundArraySupported;
public static bool IsNonZeroLowerBoundArraySupported
{
get
{
if (s_lazyNonZeroLowerBoundArraySupported == null)
{
bool nonZeroLowerBoundArraysSupported = false;
try
{
Array.CreateInstance(typeof(int), new int[] { 5 }, new int[] { 5 });
nonZeroLowerBoundArraysSupported = true;
}
catch (PlatformNotSupportedException)
{
}
s_lazyNonZeroLowerBoundArraySupported = Tuple.Create<bool>(nonZeroLowerBoundArraysSupported);
}
return s_lazyNonZeroLowerBoundArraySupported.Item1;
}
}
public static bool IsDomainJoinedMachine => !Environment.MachineName.Equals(Environment.UserDomainName, StringComparison.OrdinalIgnoreCase);
public static bool IsNotDomainJoinedMachine => !IsDomainJoinedMachine;
// Windows - Schannel supports alpn from win8.1/2012 R2 and higher.
// Linux - OpenSsl supports alpn from openssl 1.0.2 and higher.
// OSX - SecureTransport doesn't expose alpn APIs. TODO https://github.com/dotnet/runtime/issues/27727
public static bool SupportsAlpn => (IsWindows && !IsWindows7) ||
((!IsOSX && !IsWindows) &&
(OpenSslVersion.Major >= 1 && (OpenSslVersion.Minor >= 1 || OpenSslVersion.Build >= 2)));
public static bool SupportsClientAlpn => SupportsAlpn || (IsOSX && PlatformDetection.OSXVersion > new Version(10, 12));
// OpenSSL 1.1.1 and above.
public static bool SupportsTls13 => GetTls13Support();
private static Lazy<bool> s_largeArrayIsNotSupported = new Lazy<bool>(IsLargeArrayNotSupported);
[MethodImpl(MethodImplOptions.NoOptimization)]
private static bool IsLargeArrayNotSupported()
{
try
{
var tmp = new byte[int.MaxValue];
return tmp == null;
}
catch (OutOfMemoryException)
{
return true;
}
}
public static string GetDistroVersionString()
{
if (IsWindows)
{
return "WindowsProductType=" + GetWindowsProductType() + " WindowsInstallationType=" + GetWindowsInstallationType();
}
else if (IsOSX)
{
return "OSX Version=" + m_osxProductVersion.Value.ToString();
}
else
{
DistroInfo v = GetDistroInfo();
return $"Distro={v.Id} VersionId={v.VersionId}";
}
}
private static Lazy<Version> m_icuVersion = new Lazy<Version>(GetICUVersion);
public static Version ICUVersion => m_icuVersion.Value;
public static bool IsIcuGlobalization => ICUVersion > new Version(0,0,0,0);
public static bool IsNlsGlobalization => !IsIcuGlobalization;
private static Version GetICUVersion()
{
int version = 0;
try
{
Type interopGlobalization = Type.GetType("Interop+Globalization");
if (interopGlobalization != null)
{
MethodInfo methodInfo = interopGlobalization.GetMethod("GetICUVersion", BindingFlags.NonPublic | BindingFlags.Static);
if (methodInfo != null)
{
version = (int)methodInfo.Invoke(null, null);
}
}
}
catch { }
return new Version(version >> 24,
(version >> 16) & 0xFF,
(version >> 8) & 0xFF,
version & 0xFF);
}
private static bool GetIsInContainer()
{
if (IsWindows)
{
string key = @"HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control";
string value = "";
try
{
value = (string)Registry.GetValue(key, "ContainerType", defaultValue: "");
}
catch
{
}
return !string.IsNullOrEmpty(value);
}
return (IsLinux && File.Exists("/.dockerenv"));
}
private static bool GetSsl3Support()
{
if (IsWindows)
{
string clientKey = @"HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\SecurityProviders\SCHANNEL\Protocols\SSL 3.0\Client";
string serverKey = @"HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\SecurityProviders\SCHANNEL\Protocols\SSL 3.0\Server";
object client, server;
try
{
client = Registry.GetValue(clientKey, "Enabled", null);
server = Registry.GetValue(serverKey, "Enabled", null);
}
catch (SecurityException)
{
// Insufficient permission, assume that we don't have SSL3 (since we aren't exactly sure)
return false;
}
if (client is int c && server is int s)
{
return c == 1 && s == 1;
}
// Missing key. If we're pre-20H1 then assume SSL3 is enabled.
// Otherwise, disabled. (See comments on https://github.com/dotnet/runtime/issues/1166)
// Alternatively the returned values must have been some other types.
return !IsWindows10Version2004OrGreater;
}
return (IsOSX || (IsLinux && OpenSslVersion < new Version(1, 0, 2) && !IsDebian));
}
private static bool GetTls13Support()
{
if (IsWindows)
{
if (!IsWindows10Version2004OrGreater)
{
return false;
}
string clientKey = @"HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\SecurityProviders\SCHANNEL\Protocols\TLS 1.3\Client";
string serverKey = @"HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\SecurityProviders\SCHANNEL\Protocols\TLS 1.3\Server";
object client, server;
try
{
client = Registry.GetValue(clientKey, "Enabled", null);
server = Registry.GetValue(serverKey, "Enabled", null);
if (client is int c && server is int s)
{
return c == 1 && s == 1;
}
}
catch { };
// assume no if key is missing or on error.
return false;
}
else if (IsOSX)
{
// [ActiveIssue("https://github.com/dotnet/runtime/issues/1979")]
return false;
}
else
{
// Covers Linux and FreeBSD
return OpenSslVersion >= new Version(1,1,1);
}
}
private static bool GetIsRunningOnMonoInterpreter()
{
// This is a temporary solution because mono does not support interpreter detection
// within the runtime.
var val = Environment.GetEnvironmentVariable("MONO_ENV_OPTIONS");
return (val != null && val.Contains("--interpreter"));
}
}
}