Skip to content

Commit

Permalink
[release/6.0] [wasm][debugger] Fixing race condition (#64648)
Browse files Browse the repository at this point in the history
* Backporting #64394

* Addressing @lewing comment offline.
  • Loading branch information
thaystg authored Mar 8, 2022
1 parent b60aa54 commit 3044c3b
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 17 deletions.
2 changes: 1 addition & 1 deletion src/mono/mono/component/mini-wasm-debugger.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 ();
Expand Down
20 changes: 12 additions & 8 deletions src/mono/wasm/debugger/BrowserDebugProxy/MonoSDBHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -869,7 +869,7 @@ public async Task<bool> EnableReceiveRequests(SessionId sessionId, EventKind eve

internal async Task<MonoBinaryReader> 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<byte>();
if (!res.IsErr) {
newBytes = Convert.FromBase64String(res.Value?["result"]?["value"]?["value"]?.Value<string>());
Expand Down Expand Up @@ -907,7 +907,7 @@ internal Task<MonoBinaryReader> SendDebuggerAgentCommandWithParms<T>(SessionId s

internal async Task<MonoBinaryReader> 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<byte>();
if (!res.IsErr) {
newBytes = Convert.FromBase64String(res.Value?["result"]?["value"]?["value"]?.Value<string>());
Expand Down Expand Up @@ -2253,7 +2253,8 @@ public async Task<JArray> 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
}));
Expand Down Expand Up @@ -2470,7 +2471,8 @@ public async Task<JArray> 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);
Expand Down Expand Up @@ -2564,7 +2566,8 @@ public async Task<JArray> 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;
Expand All @@ -2583,7 +2586,8 @@ public async Task<JArray> 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
}));
Expand Down
21 changes: 13 additions & 8 deletions src/mono/wasm/runtime/library_mono.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand All @@ -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)
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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 ();

Expand Down

0 comments on commit 3044c3b

Please sign in to comment.