From 3044c3bdc5de1f877501f21bf928bcb4abef28dd Mon Sep 17 00:00:00 2001 From: Thays Grazia Date: Tue, 8 Mar 2022 17:40:52 -0300 Subject: [PATCH] [release/6.0] [wasm][debugger] Fixing race condition (#64648) * Backporting #64394 * Addressing @lewing comment offline. --- src/mono/mono/component/mini-wasm-debugger.c | 2 +- .../BrowserDebugProxy/MonoSDBHelper.cs | 20 +++++++++++------- src/mono/wasm/runtime/library_mono.js | 21 ++++++++++++------- 3 files changed, 26 insertions(+), 17 deletions(-) diff --git a/src/mono/mono/component/mini-wasm-debugger.c b/src/mono/mono/component/mini-wasm-debugger.c index 3c385102979e6..20c0ea07625c6 100644 --- a/src/mono/mono/component/mini-wasm-debugger.c +++ b/src/mono/mono/component/mini-wasm-debugger.c @@ -426,7 +426,7 @@ static gboolean receive_debugger_agent_message (void *data, int len) { EM_ASM ({ - MONO.mono_wasm_add_dbg_command_received (1, -1, $0, $1); + MONO.mono_wasm_add_dbg_command_received (1, 0, $0, $1); }, data, len); mono_wasm_save_thread_context(); mono_wasm_fire_debugger_agent_message (); diff --git a/src/mono/wasm/debugger/BrowserDebugProxy/MonoSDBHelper.cs b/src/mono/wasm/debugger/BrowserDebugProxy/MonoSDBHelper.cs index 3787ca4538b1e..5390c58d5a3f5 100644 --- a/src/mono/wasm/debugger/BrowserDebugProxy/MonoSDBHelper.cs +++ b/src/mono/wasm/debugger/BrowserDebugProxy/MonoSDBHelper.cs @@ -701,8 +701,8 @@ public PointerValue(long address, int typeId, string varName) internal class MonoSDBHelper { private static int debuggerObjectId; - private static int cmdId; - private static int GetId() {return cmdId++;} + private static int cmdId = 1; //cmdId == 0 is used by events which come from runtime + private static int GetNewId() {return cmdId++;} private static int MINOR_VERSION = 61; private static int MAJOR_VERSION = 2; @@ -869,7 +869,7 @@ public async Task EnableReceiveRequests(SessionId sessionId, EventKind eve internal async Task SendDebuggerAgentCommandInternal(SessionId sessionId, int command_set, int command, MemoryStream parms, CancellationToken token) { - Result res = await proxy.SendMonoCommand(sessionId, MonoCommands.SendDebuggerAgentCommand(GetId(), command_set, command, Convert.ToBase64String(parms.ToArray())), token); + Result res = await proxy.SendMonoCommand(sessionId, MonoCommands.SendDebuggerAgentCommand(GetNewId(), command_set, command, Convert.ToBase64String(parms.ToArray())), token); byte[] newBytes = Array.Empty(); if (!res.IsErr) { newBytes = Convert.FromBase64String(res.Value?["result"]?["value"]?["value"]?.Value()); @@ -907,7 +907,7 @@ internal Task SendDebuggerAgentCommandWithParms(SessionId s internal async Task SendDebuggerAgentCommandWithParmsInternal(SessionId sessionId, int command_set, int command, MemoryStream parms, int type, string extraParm, CancellationToken token) { - Result res = await proxy.SendMonoCommand(sessionId, MonoCommands.SendDebuggerAgentCommandWithParms(GetId(), command_set, command, Convert.ToBase64String(parms.ToArray()), parms.ToArray().Length, type, extraParm), token); + Result res = await proxy.SendMonoCommand(sessionId, MonoCommands.SendDebuggerAgentCommandWithParms(GetNewId(), command_set, command, Convert.ToBase64String(parms.ToArray()), parms.ToArray().Length, type, extraParm), token); byte[] newBytes = Array.Empty(); if (!res.IsErr) { newBytes = Convert.FromBase64String(res.Value?["result"]?["value"]?["value"]?.Value()); @@ -2253,7 +2253,8 @@ public async Task GetValueTypeProxy(SessionId sessionId, int valueTypeId commandSet = CommandSet.Vm, command = CmdVM.InvokeMethod, buffer = Convert.ToBase64String(command_params_to_proxy.ToArray()), - length = command_params_to_proxy.ToArray().Length + length = command_params_to_proxy.ToArray().Length, + id = GetNewId() }), name = propertyNameStr })); @@ -2470,7 +2471,8 @@ public async Task GetObjectValues(SessionId sessionId, int objectId, Get command = CmdObject.RefSetValues, buffer = Convert.ToBase64String(command_params_to_set.ToArray()), valtype, - length = command_params_to_set.ToArray().Length + length = command_params_to_set.ToArray().Length, + id = GetNewId() })); } objectFields.Add(fieldValue); @@ -2564,7 +2566,8 @@ public async Task GetObjectProxy(SessionId sessionId, int objectId, Canc command = CmdVM.InvokeMethod, buffer = Convert.ToBase64String(command_params_to_set.ToArray()), valtype = attr["set"]["valtype"], - length = command_params_to_set.ToArray().Length + length = command_params_to_set.ToArray().Length, + id = GetNewId() }); } continue; @@ -2583,7 +2586,8 @@ public async Task GetObjectProxy(SessionId sessionId, int objectId, Canc commandSet = CommandSet.Vm, command = CmdVM.InvokeMethod, buffer = Convert.ToBase64String(command_params_to_get.ToArray()), - length = command_params_to_get.ToArray().Length + length = command_params_to_get.ToArray().Length, + id = GetNewId() }), name = propertyNameStr })); diff --git a/src/mono/wasm/runtime/library_mono.js b/src/mono/wasm/runtime/library_mono.js index 6c5dbe5e0d0d4..dda646aeb59d5 100644 --- a/src/mono/wasm/runtime/library_mono.js +++ b/src/mono/wasm/runtime/library_mono.js @@ -568,7 +568,8 @@ var MonoSupportLib = { }, }, - mono_wasm_add_dbg_command_received: function(res_ok, id, buffer, buffer_len) { + mono_wasm_add_dbg_command_received: function(res_ok, id, buffer, buffer_len) + { const assembly_data = new Uint8Array(Module.HEAPU8.buffer, buffer, buffer_len); const base64String = MONO._base64Converter.toBase64StringImpl(assembly_data); const buffer_obj = { @@ -578,7 +579,9 @@ var MonoSupportLib = { value: base64String } } - MONO.commands_received = buffer_obj; + if (MONO.commands_received.has(id)) + console.warn("Addind an id that already exists in commands_received"); + MONO.commands_received.set(id, buffer_obj); }, mono_wasm_malloc_and_set_debug_buffer: function (command_parameters) @@ -598,7 +601,7 @@ var MonoSupportLib = { { this.mono_wasm_malloc_and_set_debug_buffer(command_parameters); this._c_fn_table.mono_wasm_send_dbg_command_with_parms_wrapper (id, command_set, command, this._debugger_buffer, length, valtype, newvalue.toString()); - let { res_ok, res } = MONO.commands_received; + let { res_ok, res } = MONO.commands_received.remove(id);; if (!res_ok) throw new Error (`Failed on mono_wasm_invoke_method_debugger_agent_with_parms`); return res; @@ -608,7 +611,7 @@ var MonoSupportLib = { { this.mono_wasm_malloc_and_set_debug_buffer(command_parameters); this._c_fn_table.mono_wasm_send_dbg_command_wrapper (id, command_set, command, this._debugger_buffer, command_parameters.length); - let { res_ok, res } = MONO.commands_received; + let { res_ok, res } = MONO.commands_received.remove(id); if (!res_ok) throw new Error (`Failed on mono_wasm_send_dbg_command`); return res; @@ -617,7 +620,7 @@ var MonoSupportLib = { mono_wasm_get_dbg_command_info: function () { - let { res_ok, res } = MONO.commands_received; + let { res_ok, res } = MONO.commands_received.remove(0); if (!res_ok) throw new Error (`Failed on mono_wasm_get_dbg_command_info`); return res; @@ -712,14 +715,14 @@ var MonoSupportLib = { if (prop.get !== undefined) { Object.defineProperty (proxy, prop.name, - { get () { return MONO.mono_wasm_send_dbg_command(-1, prop.get.commandSet, prop.get.command, prop.get.buffer, prop.get.length); }, - set: function (newValue) { MONO.mono_wasm_send_dbg_command_with_parms(-1, prop.set.commandSet, prop.set.command, prop.set.buffer, prop.set.length, prop.set.valtype, newValue); return MONO.commands_received.res_ok;}} + { get () { return MONO.mono_wasm_send_dbg_command(prop.get.id, prop.get.commandSet, prop.get.command, prop.get.buffer, prop.get.length); }, + set: function (newValue) { MONO.mono_wasm_send_dbg_command_with_parms(prop.set.id, prop.set.commandSet, prop.set.command, prop.set.buffer, prop.set.length, prop.set.valtype, newValue); return true;}} ); } else if (prop.set !== undefined ){ Object.defineProperty (proxy, prop.name, { get () { return prop.value; }, - set: function (newValue) { MONO.mono_wasm_send_dbg_command_with_parms(-1, prop.set.commandSet, prop.set.command, prop.set.buffer, prop.set.length, prop.set.valtype, newValue); return MONO.commands_received.res_ok;}} + set: function (newValue) { MONO.mono_wasm_send_dbg_command_with_parms(prop.set.id, prop.set.commandSet, prop.set.command, prop.set.buffer, prop.set.length, prop.set.valtype, newValue); return true;}} ); } else { proxy [prop.name] = prop.value; @@ -835,6 +838,8 @@ var MonoSupportLib = { }, mono_wasm_runtime_ready: function () { + MONO.commands_received = new Map(); + MONO.commands_received.remove = function (key) { const value = this.get(key); this.delete(key); return value;}; this.mono_wasm_runtime_is_ready = true; this._clear_per_step_state ();