From 333bda30d258aa9399e44e182af305a802b9fea4 Mon Sep 17 00:00:00 2001 From: "DESKTOP-GEPIA6N\\Thays" Date: Tue, 1 Feb 2022 19:04:22 -0300 Subject: [PATCH 1/2] Backporting #64394 --- src/mono/mono/component/mini-wasm-debugger.c | 2 +- .../BrowserDebugProxy/MonoSDBHelper.cs | 20 ++++++++------ src/mono/wasm/runtime/library_mono.js | 27 +++++++++++++------ 3 files changed, 32 insertions(+), 17 deletions(-) diff --git a/src/mono/mono/component/mini-wasm-debugger.c b/src/mono/mono/component/mini-wasm-debugger.c index 3c385102979e66..20c0ea07625c66 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 a484cc4cbeefc1..e5e96404b3e247 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 8f319841d024ea..22564f55e88f36 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) @@ -594,11 +597,18 @@ var MonoSupportLib = { this._debugger_heap_bytes.set(this._base64_to_uint8 (command_parameters)); }, + mono_get_and_delete_from_commands_received: function (id) + { + const res = MONO.commands_received.get(id); + MONO.commands_received.delete(id); + return res; + }, + mono_wasm_send_dbg_command_with_parms: function (id, command_set, command, command_parameters, length, valtype, newvalue) { 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 } = this.mono_get_and_delete_from_commands_received(id); if (!res_ok) throw new Error (`Failed on mono_wasm_invoke_method_debugger_agent_with_parms`); return res; @@ -608,7 +618,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 } = this.mono_get_and_delete_from_commands_received(id); if (!res_ok) throw new Error (`Failed on mono_wasm_send_dbg_command`); return res; @@ -617,7 +627,7 @@ var MonoSupportLib = { mono_wasm_get_dbg_command_info: function () { - let { res_ok, res } = MONO.commands_received; + let { res_ok, res } = this.mono_get_and_delete_from_commands_received(0); if (!res_ok) throw new Error (`Failed on mono_wasm_get_dbg_command_info`); return res; @@ -712,14 +722,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 +845,7 @@ var MonoSupportLib = { }, mono_wasm_runtime_ready: function () { + MONO.commands_received = new Map(); this.mono_wasm_runtime_is_ready = true; this._clear_per_step_state (); From 7d447adabbbb282ab36033d86cb7a0183e5a48c8 Mon Sep 17 00:00:00 2001 From: "DESKTOP-GEPIA6N\\Thays" Date: Wed, 2 Feb 2022 11:35:26 -0300 Subject: [PATCH 2/2] Addressing @lewing comment offline. --- src/mono/wasm/runtime/library_mono.js | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-) diff --git a/src/mono/wasm/runtime/library_mono.js b/src/mono/wasm/runtime/library_mono.js index 22564f55e88f36..0fac622c86f4c2 100644 --- a/src/mono/wasm/runtime/library_mono.js +++ b/src/mono/wasm/runtime/library_mono.js @@ -597,18 +597,11 @@ var MonoSupportLib = { this._debugger_heap_bytes.set(this._base64_to_uint8 (command_parameters)); }, - mono_get_and_delete_from_commands_received: function (id) - { - const res = MONO.commands_received.get(id); - MONO.commands_received.delete(id); - return res; - }, - mono_wasm_send_dbg_command_with_parms: function (id, command_set, command, command_parameters, length, valtype, newvalue) { 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 } = this.mono_get_and_delete_from_commands_received(id); + 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; @@ -618,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 } = this.mono_get_and_delete_from_commands_received(id); + let { res_ok, res } = MONO.commands_received.remove(id); if (!res_ok) throw new Error (`Failed on mono_wasm_send_dbg_command`); return res; @@ -627,7 +620,7 @@ var MonoSupportLib = { mono_wasm_get_dbg_command_info: function () { - let { res_ok, res } = this.mono_get_and_delete_from_commands_received(0); + 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; @@ -845,7 +838,8 @@ var MonoSupportLib = { }, mono_wasm_runtime_ready: function () { - MONO.commands_received = new Map(); + 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 ();