Skip to content

Commit

Permalink
[wasm][debugger] Avoid using Uint8Array to set memory content (dotnet…
Browse files Browse the repository at this point in the history
…#60087)

* Creating a test case for PR dotnet#59773
But I'm not using Uint8Array to set the content on memory allocated using malloc as @lewing suggested in the same PR.

* add lines in the eof

* Change the function that allocate memory.

* adding comment as suggested by @kg

* Accepting @lewing suggestion
  • Loading branch information
thaystg authored Oct 7, 2021
1 parent b36548c commit 51b4e3e
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 16 deletions.
41 changes: 41 additions & 0 deletions src/mono/wasm/debugger/DebuggerTestSuite/Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<string>());
Assert.Equal(bp.Value["breakpointId"]?.ToString(), pause_location["hitBreakpoints"]?[0]?.Value<string>());

var top_frame = pause_location["callFrames"][0];
Assert.Equal("IntAdd", top_frame["functionName"].Value<string>());
Assert.Contains("debugger-test.cs", top_frame["url"].Value<string>());

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
}
}
6 changes: 6 additions & 0 deletions src/mono/wasm/debugger/tests/debugger-test/other.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
11 changes: 0 additions & 11 deletions src/mono/wasm/runtime/base64.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
10 changes: 5 additions & 5 deletions src/mono/wasm/runtime/debug.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@
// 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;
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;
Expand Down Expand Up @@ -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, <any>_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[<any>_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 {
Expand Down

0 comments on commit 51b4e3e

Please sign in to comment.