From c734a8e40b8e7bb4c3ff7b8e615c052f0b52a72f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aleksey=20Kliger=20=28=CE=BBgeek=29?= Date: Fri, 29 Jul 2022 09:06:14 -0400 Subject: [PATCH] [wasm-ep] diagnostic server IPC fixups (#72859) * [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 --- .../Wasm.Browser.EventPipe.Sample.csproj | 2 +- .../server_pthread/ipc-protocol/base-parser.ts | 13 +++++++++++++ .../diagnostics/server_pthread/streaming-session.ts | 11 +++++++++-- 3 files changed, 23 insertions(+), 3 deletions(-) diff --git a/src/mono/sample/wasm/browser-eventpipe/Wasm.Browser.EventPipe.Sample.csproj b/src/mono/sample/wasm/browser-eventpipe/Wasm.Browser.EventPipe.Sample.csproj index 94253eaed3834..49f5718f12972 100644 --- a/src/mono/sample/wasm/browser-eventpipe/Wasm.Browser.EventPipe.Sample.csproj +++ b/src/mono/sample/wasm/browser-eventpipe/Wasm.Browser.EventPipe.Sample.csproj @@ -14,7 +14,7 @@ - false + true diff --git a/src/mono/wasm/runtime/diagnostics/server_pthread/ipc-protocol/base-parser.ts b/src/mono/wasm/runtime/diagnostics/server_pthread/ipc-protocol/base-parser.ts index 6ac893185ac92..2129fc76e601c 100644 --- a/src/mono/wasm/runtime/diagnostics/server_pthread/ipc-protocol/base-parser.ts +++ b/src/mono/wasm/runtime/diagnostics/server_pthread/ipc-protocol/base-parser.ts @@ -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); } }; diff --git a/src/mono/wasm/runtime/diagnostics/server_pthread/streaming-session.ts b/src/mono/wasm/runtime/diagnostics/server_pthread/streaming-session.ts index 938db52bd29fb..8d7209a98482c 100644 --- a/src/mono/wasm/runtime/diagnostics/server_pthread/streaming-session.ts +++ b/src/mono/wasm/runtime/diagnostics/server_pthread/streaming-session.ts @@ -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; + } }