[wasm-ep] diagnostic server IPC fixups (#72859)
authorAleksey Kliger (λgeek) <alklig@microsoft.com>
Fri, 29 Jul 2022 13:06:14 +0000 (09:06 -0400)
committerGitHub <noreply@github.com>
Fri, 29 Jul 2022 13:06:14 +0000 (09:06 -0400)
* [wasm-ep] Trim trailing nul when parsing IPC command strings

The protocol spec says there's a length and a nul terminator, but sending that literal UTF-16 nul to ep_enable_2 means we use an incorrect name when looking for the provider.

* [wasm-ep] Fix formating of event pipe provider strings

   convert the 64-bit "keywords" mask into a hex string properly by padding both halves with zeroes after calling the JS Number.toString() method

* use Array splice instead of String substring

* Address review feedback

src/mono/sample/wasm/browser-eventpipe/Wasm.Browser.EventPipe.Sample.csproj
src/mono/wasm/runtime/diagnostics/server_pthread/ipc-protocol/base-parser.ts
src/mono/wasm/runtime/diagnostics/server_pthread/streaming-session.ts

index 94253ea..49f5718 100644 (file)
@@ -14,7 +14,7 @@
   </PropertyGroup>
 
   <PropertyGroup>
-    <MonoDiagnosticsMock Condition="'$(MonoDiagnosticsMock)' == ''">false</MonoDiagnosticsMock>
+    <MonoDiagnosticsMock Condition="('$(MonoDiagnosticsMock)' == '') and ('$(Configuration)' == 'Debug')">true</MonoDiagnosticsMock>
   </PropertyGroup>
 
   <ItemGroup>
index 6ac8931..2129fc7 100644 (file)
@@ -113,6 +113,19 @@ const Parser = {
             result[i] = (buf[j + 2 * i + 1] << 8) | buf[j + 2 * i];
         }
         advancePos(pos, length * 2);
+
+        /* Trim trailing nul character(s) that are added by the protocol */
+        let trailingNulStart = -1;
+        for (let i = result.length - 1; i >= 0; i--) {
+            if (result[i] === 0) {
+                trailingNulStart = i;
+            } else {
+                break;
+            }
+        }
+        if (trailingNulStart >= 0)
+            result.splice(trailingNulStart);
+
         return String.fromCharCode.apply(null, result);
     }
 };
index 938db52..8d7209a 100644 (file)
@@ -57,10 +57,17 @@ function providersStringFromObject(providers: EventPipeCollectTracingCommandProv
     function keywordsToHexString(k: [number, number]): string {
         const lo = k[0];
         const hi = k[1];
-        const lo_hex = lo.toString(16);
-        const hi_hex = hi.toString(16);
+        const lo_hex = leftPad(lo.toString(16), "0", 8);
+        const hi_hex = leftPad(hi.toString(16), "0", 8);
         return hi_hex + lo_hex;
     }
+
+    function leftPad(s: string, fill: string, width: number): string {
+        if (s.length >= width)
+            return s;
+        const prefix = fill.repeat(width - s.length);
+        return prefix + s;
+    }
 }