diff --git a/src/mono/wasm/debugger/DebuggerTestSuite/Tests.cs b/src/mono/wasm/debugger/DebuggerTestSuite/Tests.cs index 9681a02882574..d5e47384796d2 100644 --- a/src/mono/wasm/debugger/DebuggerTestSuite/Tests.cs +++ b/src/mono/wasm/debugger/DebuggerTestSuite/Tests.cs @@ -860,6 +860,47 @@ await EvaluateAndCheck( ); } + [Fact] + public async Task MallocUntilReallocate() //https://github.com/xamarin/xamarin-android/issues/6161 + { + string eval_expr = "window.setTimeout(function() { malloc_to_reallocate_test (); }, 1)"; + + var result = await cli.SendCommand("Runtime.evaluate", JObject.FromObject(new { expression = eval_expr }), token); + + var bp = await SetBreakpoint("dotnet://debugger-test.dll/debugger-test.cs", 10, 8); + + var eval_req = JObject.FromObject(new + { + expression = "window.setTimeout(function() { invoke_add(); }, 1);", + }); + + await EvaluateAndCheck( + "window.setTimeout(function() { invoke_add(); }, 1);", + "dotnet://debugger-test.dll/debugger-test.cs", 10, 8, + "IntAdd", + wait_for_event_fn: (pause_location) => + { + Assert.Equal("other", pause_location["reason"]?.Value()); + Assert.Equal(bp.Value["breakpointId"]?.ToString(), pause_location["hitBreakpoints"]?[0]?.Value()); + + var top_frame = pause_location["callFrames"][0]; + Assert.Equal("IntAdd", top_frame["functionName"].Value()); + Assert.Contains("debugger-test.cs", top_frame["url"].Value()); + + CheckLocation("dotnet://debugger-test.dll/debugger-test.cs", 8, 4, scripts, top_frame["functionLocation"]); + + //now check the scope + var scope = top_frame["scopeChain"][0]; + Assert.Equal("local", scope["type"]); + Assert.Equal("IntAdd", scope["name"]); + + Assert.Equal("object", scope["object"]["type"]); + CheckLocation("dotnet://debugger-test.dll/debugger-test.cs", 8, 4, scripts, scope["startLocation"]); + CheckLocation("dotnet://debugger-test.dll/debugger-test.cs", 14, 4, scripts, scope["endLocation"]); + return Task.CompletedTask; + } + ); + } //TODO add tests covering basic stepping behavior as step in/out/over } } diff --git a/src/mono/wasm/debugger/tests/debugger-test/other.js b/src/mono/wasm/debugger/tests/debugger-test/other.js index 72cb80bf2fe6e..fb953e2857495 100644 --- a/src/mono/wasm/debugger/tests/debugger-test/other.js +++ b/src/mono/wasm/debugger/tests/debugger-test/other.js @@ -95,3 +95,9 @@ function get_properties_test () { console.log(`break here`); } + +function malloc_to_reallocate_test () { + //need to allocate this buffer size to force wasm linear memory to grow + var _debugger_buffer = Module._malloc(4500000); + Module._free(_debugger_buffer); +} diff --git a/src/mono/wasm/runtime/base64.ts b/src/mono/wasm/runtime/base64.ts index 3f0098e2b7bc9..68a9e82db0820 100644 --- a/src/mono/wasm/runtime/base64.ts +++ b/src/mono/wasm/runtime/base64.ts @@ -108,14 +108,3 @@ function _makeByteReader(bytes: Uint8Array, index?: number, count?: number): { return result; } - -// FIXME: improve -export function _base64_to_uint8(base64String: string):Uint8Array { - const byteCharacters = atob(base64String); - const byteNumbers = new Array(byteCharacters.length); - for (let i = 0; i < byteCharacters.length; i++) { - byteNumbers[i] = byteCharacters.charCodeAt(i); - } - - return new Uint8Array(byteNumbers); -} \ No newline at end of file diff --git a/src/mono/wasm/runtime/debug.ts b/src/mono/wasm/runtime/debug.ts index b9960f8e50a4b..70f0b4cc093ae 100644 --- a/src/mono/wasm/runtime/debug.ts +++ b/src/mono/wasm/runtime/debug.ts @@ -2,7 +2,7 @@ // The .NET Foundation licenses this file to you under the MIT license. import { Module, runtimeHelpers } from "./modules"; -import { toBase64StringImpl, _base64_to_uint8 } from "./base64"; +import { toBase64StringImpl } from "./base64"; import cwraps from "./cwraps"; let commands_received: CommandResponse; @@ -10,7 +10,6 @@ let _call_function_res_cache: any = {}; let _next_call_function_res_id = 0; let _debugger_buffer_len = -1; let _debugger_buffer: VoidPtr; -let _debugger_heap_bytes: Uint8Array; export function mono_wasm_runtime_ready(): void { runtimeHelpers.mono_wasm_runtime_is_ready = true; @@ -52,9 +51,10 @@ function mono_wasm_malloc_and_set_debug_buffer(command_parameters: string) { _debugger_buffer_len = Math.max(command_parameters.length, _debugger_buffer_len, 256); _debugger_buffer = Module._malloc(_debugger_buffer_len); } - //reset _debugger_heap_bytes because Module.HEAPU8.buffer can be reallocated - _debugger_heap_bytes = new Uint8Array(Module.HEAPU8.buffer, _debugger_buffer, _debugger_buffer_len); - _debugger_heap_bytes.set(_base64_to_uint8(command_parameters)); + const byteCharacters = atob(command_parameters); + for (let i = 0; i < byteCharacters.length; i++) { + Module.HEAPU8[_debugger_buffer + i] = byteCharacters.charCodeAt(i); + } } export function mono_wasm_send_dbg_command_with_parms(id: number, command_set: number, command: number, command_parameters: string, length: number, valtype: number, newvalue: number): CommandResponseResult {