Move GetCurrentProcessId interop for Browser to managed (#39367)
authorMarek Safar <marek.safar@gmail.com>
Fri, 31 Jul 2020 15:53:30 +0000 (17:53 +0200)
committerGitHub <noreply@github.com>
Fri, 31 Jul 2020 15:53:30 +0000 (08:53 -0700)
* Move GetCurrentProcessId for Browser to managed

It returns constant all the time

* Remove redundant indirection

* Fix EventSource build break

Co-authored-by: Jan Kotas <jkotas@microsoft.com>
src/libraries/Common/src/Interop/Unix/System.Native/Interop.GetPid.cs
src/libraries/Common/src/Interop/Windows/Kernel32/Interop.GetCurrentProcessId.cs
src/libraries/System.Private.CoreLib/src/System.Private.CoreLib.Shared.projitems
src/libraries/System.Private.CoreLib/src/System/Diagnostics/Tracing/ActivityTracker.cs
src/libraries/System.Private.CoreLib/src/System/Diagnostics/Tracing/EventSource.cs
src/libraries/System.Private.CoreLib/src/System/Diagnostics/Tracing/StubEnvironment.cs
src/libraries/System.Private.CoreLib/src/System/Environment.Browser.cs
src/libraries/System.Private.CoreLib/src/System/Environment.Unix.cs
src/libraries/System.Private.CoreLib/src/System/Environment.UnixOrBrowser.cs
src/libraries/System.Private.CoreLib/src/System/Threading/Tasks/TplEventSource.cs

index bbd99dfb5ecb3257ac13ab9c582aa89595b28b9e..400dc554a366d48c07ac8d290059362997aadeb5 100644 (file)
@@ -10,6 +10,4 @@ internal static partial class Interop
         [DllImport(Libraries.SystemNative, EntryPoint = "SystemNative_GetPid")]
         internal static extern int GetPid();
     }
-
-    internal static uint GetCurrentProcessId() => (uint)Sys.GetPid();
 }
index ff50d0d470bc119bd5ffcc6da1f251eaf8a303f3..70009c6f9323595d73dcaf8b42d8238bbd8ee685 100644 (file)
@@ -10,6 +10,4 @@ internal static partial class Interop
         [DllImport(Libraries.Kernel32)]
         internal static extern uint GetCurrentProcessId();
     }
-
-    internal static uint GetCurrentProcessId() => Kernel32.GetCurrentProcessId();
 }
index b00e089f872ddd7a32e54814f40bcd0d89f56141..61cc836399705e6341bc1f4ffcbce60afd0693c4 100644 (file)
     <Compile Include="$(CommonPath)Interop\Unix\System.Native\Interop.GetHostName.cs">
       <Link>Common\Interop\Unix\System.Native\Interop.GetHostName.cs</Link>
     </Compile>
-    <Compile Include="$(CommonPath)\Interop\Unix\System.Native\Interop.GetPid.cs">
-      <Link>Common\Interop\Unix\System.Native\Interop.GetPid.cs</Link>
-    </Compile>
     <Compile Include="$(CommonPath)Interop\Unix\System.Native\Interop.GetPwUid.cs">
       <Link>Common\Interop\Unix\System.Native\Interop.GetPwUid.cs</Link>
     </Compile>
     <Compile Include="$(MSBuildThisFileDirectory)System\TimeZoneInfo.Unix.cs" />
   </ItemGroup>
   <ItemGroup Condition="'$(TargetsUnix)' == 'true'">
+    <Compile Include="$(CommonPath)\Interop\Unix\System.Native\Interop.GetPid.cs">
+      <Link>Common\Interop\Unix\System.Native\Interop.GetPid.cs</Link>
+    </Compile>
     <Compile Include="$(MSBuildThisFileDirectory)System\Environment.Unix.cs" />
     <Compile Include="$(MSBuildThisFileDirectory)System\TimeZoneInfo.GetDisplayName.cs" />
   </ItemGroup>
index c98e76352fca2507187957a80050cd0f1d06d5fc..cec4e969f2ced176d22b9364fb3fa7eb9442f879 100644 (file)
@@ -4,6 +4,7 @@
 #if ES_BUILD_STANDALONE
 using System;
 using System.Diagnostics;
+using Environment = Microsoft.Diagnostics.Tracing.Internal.Environment;
 #else
 using System.Threading.Tasks;
 #endif
@@ -511,7 +512,7 @@ namespace System.Diagnostics.Tracing
                 uint* sumPtr = (uint*)outPtr;
                 // We set the last DWORD the sum of the first 3 DWORDS in the GUID.   This
                 // This last number is a random number (it identifies us as us)  the process ID to make it unique per process.
-                sumPtr[3] = (sumPtr[0] + sumPtr[1] + sumPtr[2] + 0x599D99AD) ^ EventSource.s_currentPid;
+                sumPtr[3] = (sumPtr[0] + sumPtr[1] + sumPtr[2] + 0x599D99AD) ^ (uint)Environment.ProcessId;
 
                 return (int)(ptr - ((byte*)outPtr));
             }
index 02b84bcd18ff83f9cb6199f16bad9fe924e68ee2..ab95ee4c10dfcd7044cd381a9a9d3c3449e5b41e 100644 (file)
@@ -2843,14 +2843,6 @@ namespace System.Diagnostics.Tracing
                 DefineEventPipeEvents();
 #endif
             }
-            if (s_currentPid == 0)
-            {
-#if ES_BUILD_STANDALONE
-                // for non-BCL EventSource we must assert SecurityPermission
-                new SecurityPermission(PermissionState.Unrestricted).Assert();
-#endif
-                s_currentPid = Interop.GetCurrentProcessId();
-            }
         }
 
         // Send out the ETW manifest XML out to ETW
@@ -3807,7 +3799,6 @@ namespace System.Diagnostics.Tracing
 
         private string[]? m_traits;                      // Used to implement GetTraits
 
-        internal static uint s_currentPid;              // current process id, used in synthesizing quasi-GUIDs
         [ThreadStatic]
         private static byte m_EventSourceExceptionRecurenceCount; // current recursion count inside ThrowEventSourceException
 
index 9f08d0ca304b40d9c804d06f6b0efedb8648356d..90428c3b942c487996295eca74d2a660175eb1b7 100644 (file)
@@ -10,6 +10,7 @@ using System.Reflection;
 using System.Runtime.CompilerServices;
 using System.Runtime.InteropServices;
 using System.Security;
+using System.Security.Permissions;
 #endif
 #if ES_BUILD_AGAINST_DOTNET_V35
 using Microsoft.Internal;
@@ -22,32 +23,18 @@ namespace Microsoft.Diagnostics.Tracing.Internal
 namespace System.Diagnostics.Tracing.Internal
 #endif
 {
+#if ES_BUILD_STANDALONE
     internal static class Environment
     {
-        public static readonly string NewLine = System.Environment.NewLine;
-
-        public static int TickCount => System.Environment.TickCount;
-
-        public static string GetResourceString(string key, params object?[] args)
-        {
-            string? fmt = rm.GetString(key);
-            if (fmt != null)
-                return string.Format(fmt, args);
-
-            string sargs = string.Join(", ", args);
+        public static int ProcessId = GetCurrentProcessId();
 
-            return key + " (" + sargs + ")";
-        }
-
-        public static string GetRuntimeResourceString(string key, params object?[] args)
+        private static int GetCurrentProcessId()
         {
-            return GetResourceString(key, args);
+            new SecurityPermission(PermissionState.Unrestricted).Assert();
+            return (int)Interop.Kernel32.GetCurrentProcessId();
         }
-
-        private static readonly System.Resources.ResourceManager rm = new System.Resources.ResourceManager("Microsoft.Diagnostics.Tracing.Messages", typeof(Environment).Assembly());
     }
 
-#if ES_BUILD_STANDALONE
     internal static class BitOperations
     {
         [MethodImpl(MethodImplOptions.AggressiveInlining)]
@@ -365,7 +352,5 @@ internal static partial class Interop
         [DllImport("kernel32.dll", CharSet = CharSet.Auto)]
         internal static extern uint GetCurrentProcessId();
     }
-
-    internal static uint GetCurrentProcessId() => Kernel32.GetCurrentProcessId();
 }
 #endif
index 4f2beaa3b01bf5632278f39c9d30764bcd74e567..07ad3b8b31c75b50b2db5f901129e1a8d59041c4 100644 (file)
@@ -24,5 +24,7 @@ namespace System
         {
             return new OperatingSystem(PlatformID.Other, new Version(1, 0, 0, 0));
         }
+
+        private static int GetCurrentProcessId() => 42;
     }
-}
\ No newline at end of file
+}
index 87306e7b741afa903f53356eeaa2688d70b0a826..9b5ff6ac417cd9914826805a53976b5390ba9c1f 100644 (file)
@@ -110,5 +110,7 @@ namespace System
             // Otherwise, fail.
             throw new IOException(errorInfo.GetErrorMessage(), errorInfo.RawErrno);
         }
+
+        private static int GetCurrentProcessId() => Interop.Sys.GetPid();
     }
 }
index cddf1b929e63f2b380b7e017c9401b74c5f04446..588e1dbf57d18069bc0f2b47346d93b3bbebe128 100644 (file)
@@ -48,8 +48,6 @@ namespace System
 
         private static bool Is64BitOperatingSystemWhen32BitProcess => false;
 
-        private static int GetCurrentProcessId() => Interop.Sys.GetPid();
-
         internal const string NewLineConst = "\n";
 
         public static string SystemDirectory => GetFolderPathCore(SpecialFolder.System, SpecialFolderOption.None);
index 9b89c9ecf19b547a8b76f09a5aac4db9e0f2a86c..ea85eab23cfeea91a642d9baa2791b1daef8ca4c 100644 (file)
@@ -556,7 +556,7 @@ namespace System.Threading.Tasks
             // using the taskGuid, the appdomain ID, and 8 bytes of 'randomization' chosen by
             // using the last 8 bytes  as the provider GUID for this provider.
             // These were generated by CreateGuid, and are reasonably random (and thus unlikely to collide
-            uint pid = EventSource.s_currentPid;
+            int pid = Environment.ProcessId;
             return new Guid(taskID,
                             (short)DefaultAppDomainID, (short)(DefaultAppDomainID >> 16),
                             (byte)pid, (byte)(pid >> 8), (byte)(pid >> 16), (byte)(pid >> 24),