From 9e88e9b9810e21d33f3b4a981e437e8bc2d9f8b4 Mon Sep 17 00:00:00 2001 From: pavelsavara Date: Thu, 18 Apr 2024 18:34:30 +0200 Subject: [PATCH] see what breaks --- .../browser/runtime/cancelable-promise.ts | 6 +---- src/mono/browser/runtime/debug.ts | 17 ++----------- src/mono/browser/runtime/invoke-js.ts | 3 --- src/mono/browser/runtime/marshal.ts | 5 +--- src/mono/browser/runtime/memory.ts | 24 ------------------- .../browser/runtime/pthreads/deputy-thread.ts | 3 --- src/mono/browser/runtime/pthreads/shared.ts | 2 -- .../browser/runtime/pthreads/worker-thread.ts | 3 --- src/mono/browser/runtime/scheduling.ts | 4 ---- src/mono/browser/runtime/web-socket.ts | 14 +---------- 10 files changed, 5 insertions(+), 76 deletions(-) diff --git a/src/mono/browser/runtime/cancelable-promise.ts b/src/mono/browser/runtime/cancelable-promise.ts index c82cbdacb1960..297e499431114 100644 --- a/src/mono/browser/runtime/cancelable-promise.ts +++ b/src/mono/browser/runtime/cancelable-promise.ts @@ -7,7 +7,7 @@ import { _lookup_js_owned_object, teardown_managed_proxy, upgrade_managed_proxy_ import { createPromiseController, loaderHelpers, mono_assert } from "./globals"; import { ControllablePromise, GCHandle, MarshalerToCs } from "./types/internal"; import { ManagedObject } from "./marshal"; -import { compareExchangeI32, forceThreadMemoryViewRefresh } from "./memory"; +import { compareExchangeI32 } from "./memory"; import { mono_log_debug } from "./logging"; import { complete_task } from "./managed-exports"; import { marshal_cs_object_to_cs } from "./marshal-to-cs"; @@ -75,7 +75,6 @@ export class PromiseHolder extends ManagedObject { if (!WasmEnableThreads || this.promiseHolderPtr === 0) { return true; } - forceThreadMemoryViewRefresh(); if (compareExchangeI32(this.promiseHolderPtr + PromiseHolderState.IsResolving, 1, 0) === 0) { return true; } @@ -166,9 +165,6 @@ export class PromiseHolder extends ManagedObject { try { mono_assert(!this.isPosted, "Promise is already posted to managed."); this.isPosted = true; - if (WasmEnableThreads) { - forceThreadMemoryViewRefresh(); - } // we can unregister the GC handle just on JS side teardown_managed_proxy(this, this.gc_handle, /*skipManaged: */ true); diff --git a/src/mono/browser/runtime/debug.ts b/src/mono/browser/runtime/debug.ts index 99963d964f00c..fef3478f0c9e1 100644 --- a/src/mono/browser/runtime/debug.ts +++ b/src/mono/browser/runtime/debug.ts @@ -6,7 +6,7 @@ import { toBase64StringImpl } from "./base64"; import cwraps from "./cwraps"; import { VoidPtr, CharPtr } from "./types/emscripten"; import { mono_log_warn } from "./logging"; -import { forceThreadMemoryViewRefresh, localHeapViewU8 } from "./memory"; +import { localHeapViewU8 } from "./memory"; import { utf8ToString } from "./strings"; const commands_received: any = new Map(); commands_received.remove = function (key: number): CommandResponse { @@ -76,8 +76,6 @@ function mono_wasm_malloc_and_set_debug_buffer (command_parameters: string) { } 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 { - forceThreadMemoryViewRefresh(); - mono_wasm_malloc_and_set_debug_buffer(command_parameters); cwraps.mono_wasm_send_dbg_command_with_parms(id, command_set, command, _debugger_buffer, length, valtype, newvalue.toString()); @@ -88,8 +86,6 @@ export function mono_wasm_send_dbg_command_with_parms (id: number, command_set: } export function mono_wasm_send_dbg_command (id: number, command_set: number, command: number, command_parameters: string): CommandResponseResult { - forceThreadMemoryViewRefresh(); - mono_wasm_malloc_and_set_debug_buffer(command_parameters); cwraps.mono_wasm_send_dbg_command(id, command_set, command, _debugger_buffer, command_parameters.length); @@ -110,16 +106,14 @@ export function mono_wasm_get_dbg_command_info (): CommandResponseResult { } export function mono_wasm_debugger_resume (): void { - forceThreadMemoryViewRefresh(); + // noop } export function mono_wasm_detach_debugger (): void { - forceThreadMemoryViewRefresh(); cwraps.mono_wasm_set_is_debugger_attached(false); } export function mono_wasm_change_debugger_log_level (level: number): void { - forceThreadMemoryViewRefresh(); cwraps.mono_wasm_change_debugger_log_level(level); } @@ -155,7 +149,6 @@ export function mono_wasm_wait_for_debugger (): Promise { export function mono_wasm_debugger_attached (): void { if (runtimeHelpers.waitForDebugger == -1) runtimeHelpers.waitForDebugger = 1; - forceThreadMemoryViewRefresh(); cwraps.mono_wasm_set_is_debugger_attached(true); } @@ -168,8 +161,6 @@ export function mono_wasm_set_entrypoint_breakpoint (entrypoint_method_token: nu console.assert(true, `Adding an entrypoint breakpoint ${_assembly_name_str} at method token ${_entrypoint_method_token}`); // eslint-disable-next-line no-debugger debugger; - - forceThreadMemoryViewRefresh(); } function _create_proxy_from_object_id (objectId: string, details: any) { @@ -220,8 +211,6 @@ function _create_proxy_from_object_id (objectId: string, details: any) { } export function mono_wasm_call_function_on (request: CallRequest): CFOResponse { - forceThreadMemoryViewRefresh(); - if (request.arguments != undefined && !Array.isArray(request.arguments)) throw new Error(`"arguments" should be an array, but was ${request.arguments}`); @@ -341,7 +330,6 @@ type ValueAsJsonString = { } export function mono_wasm_get_details (objectId: string, args = {}): ValueAsJsonString { - forceThreadMemoryViewRefresh(); return _get_cfo_res_details(`dotnet:cfo_res:${objectId}`, args); } @@ -357,7 +345,6 @@ export function mono_wasm_release_object (objectId: string): void { } export function mono_wasm_debugger_log (level: number, message_ptr: CharPtr): void { - forceThreadMemoryViewRefresh(); const message = utf8ToString(message_ptr); if (INTERNAL["logging"] && typeof INTERNAL.logging["debugger"] === "function") { diff --git a/src/mono/browser/runtime/invoke-js.ts b/src/mono/browser/runtime/invoke-js.ts index c5d385a17e4e4..0e7cc323bc8c1 100644 --- a/src/mono/browser/runtime/invoke-js.ts +++ b/src/mono/browser/runtime/invoke-js.ts @@ -6,7 +6,6 @@ import BuildConfiguration from "consts:configuration"; import { marshal_exception_to_cs, bind_arg_marshal_to_cs, marshal_task_to_cs } from "./marshal-to-cs"; import { get_signature_argument_count, bound_js_function_symbol, get_sig, get_signature_version, get_signature_type, imported_js_function_symbol, get_signature_handle, get_signature_function_name, get_signature_module_name, is_receiver_should_free, get_caller_native_tid, get_sync_done_semaphore_ptr, get_arg } from "./marshal"; -import { forceThreadMemoryViewRefresh } from "./memory"; import { JSFunctionSignature, JSMarshalerArguments, BoundMarshalerToJs, JSFnHandle, BoundMarshalerToCs, JSHandle, MarshalerType, VoidPtrNull } from "./types/internal"; import { VoidPtr } from "./types/emscripten"; import { INTERNAL, Module, loaderHelpers, mono_assert, runtimeHelpers } from "./globals"; @@ -149,13 +148,11 @@ function bind_js_import (signature: JSFunctionSignature): Function { } function async_bound_fn (args: JSMarshalerArguments): void { - forceThreadMemoryViewRefresh(); bound_fn(args); } function sync_bound_fn (args: JSMarshalerArguments): void { const previous = runtimeHelpers.isPendingSynchronousCall; try { - forceThreadMemoryViewRefresh(); const caller_tid = get_caller_native_tid(args); runtimeHelpers.isPendingSynchronousCall = runtimeHelpers.managedThreadTID === caller_tid; bound_fn(args); diff --git a/src/mono/browser/runtime/marshal.ts b/src/mono/browser/runtime/marshal.ts index c0ece106f53b2..320c922b7e826 100644 --- a/src/mono/browser/runtime/marshal.ts +++ b/src/mono/browser/runtime/marshal.ts @@ -5,7 +5,7 @@ import WasmEnableThreads from "consts:wasmEnableThreads"; import { js_owned_gc_handle_symbol, teardown_managed_proxy } from "./gc-handles"; import { Module, loaderHelpers, mono_assert, runtimeHelpers } from "./globals"; -import { getF32, getF64, getI16, getI32, getI64Big, getU16, getU32, getU8, setF32, setF64, setI16, setI32, setI64Big, setU16, setU32, setU8, localHeapViewF64, localHeapViewI32, localHeapViewU8, _zero_region, forceThreadMemoryViewRefresh, setB8, getB8 } from "./memory"; +import { getF32, getF64, getI16, getI32, getI64Big, getU16, getU32, getU8, setF32, setF64, setI16, setI32, setI64Big, setU16, setU32, setU8, localHeapViewF64, localHeapViewI32, localHeapViewU8, _zero_region, setB8, getB8 } from "./memory"; import { mono_wasm_new_external_root } from "./roots"; import { GCHandle, JSHandle, MonoObject, MonoString, GCHandleNull, JSMarshalerArguments, JSFunctionSignature, JSMarshalerType, JSMarshalerArgument, MarshalerToJs, MarshalerToCs, WasmRoot, MarshalerType, PThreadPtr, PThreadPtrNull, VoidPtrNull } from "./types/internal"; import { TypedArray, VoidPtr } from "./types/emscripten"; @@ -65,9 +65,6 @@ const enum JSBindingHeaderOffsets { } export function alloc_stack_frame (size: number): JSMarshalerArguments { - if (WasmEnableThreads) { - forceThreadMemoryViewRefresh(); - } const bytes = JavaScriptMarshalerArgSize * size; const args = Module.stackAlloc(bytes) as any; _zero_region(args, bytes); diff --git a/src/mono/browser/runtime/memory.ts b/src/mono/browser/runtime/memory.ts index 5a0ac71d3b329..c8d6c6dbdc688 100644 --- a/src/mono/browser/runtime/memory.ts +++ b/src/mono/browser/runtime/memory.ts @@ -462,27 +462,3 @@ export function isSharedArrayBuffer (buffer: any): buffer is SharedArrayBuffer { // See also https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol/toStringTag return sharedArrayBufferDefined && buffer[Symbol.toStringTag] === "SharedArrayBuffer"; } - -/* -Problem: When WebWorker is suspended in the browser, the other running threads could `grow` the linear memory in the meantime. -After the thread is un-suspended C code may to try to de-reference pointer which is beyond it's known view. -This is likely V8 bug. We don't have direct evidence, just failed debugger unit tests with MT runtime. -*/ -export function forceThreadMemoryViewRefresh () { - // this condition should be eliminated by rollup on non-threading builds and it would become empty method. - if (!WasmEnableThreads) return; - - const wasmMemory = runtimeHelpers.getMemory(); - - /* - Normally when wasm memory grows in v8, this size change is broadcast to other web workers via an 'interrupt', which works by setting a thread-local flag that needs to be checked. - It's possible that at this point in execution the flag has not been checked yet (because this worker was suspended by the debugger in an unknown location), - which means the size change has not taken effect in this worker. - wasmMemory.grow's implementation in v8 checks to see whether other workers have already grown the buffer, - and will update the current worker's knowledge of the buffer's size. - After that we should be able to safely updateMemoryViews and get a correctly sized view. - This only works because their implementation does not skip doing work even when you ask to grow by 0 pages. - */ - wasmMemory.grow(0); - runtimeHelpers.updateMemoryViews(); -} diff --git a/src/mono/browser/runtime/pthreads/deputy-thread.ts b/src/mono/browser/runtime/pthreads/deputy-thread.ts index 4b514b28a4aed..ea3d8030fbaaa 100644 --- a/src/mono/browser/runtime/pthreads/deputy-thread.ts +++ b/src/mono/browser/runtime/pthreads/deputy-thread.ts @@ -9,7 +9,6 @@ import { monoThreadInfo, postMessageToMain, update_thread_info } from "./shared" import { Module, loaderHelpers, runtimeHelpers } from "../globals"; import { start_runtime } from "../startup"; import { WorkerToMainMessageType } from "../types/internal"; -import { forceThreadMemoryViewRefresh } from "../memory"; export function mono_wasm_start_deputy_thread_async () { if (!WasmEnableThreads) return; @@ -29,8 +28,6 @@ export function mono_wasm_start_deputy_thread_async () { Module.runtimeKeepalivePush(); Module.safeSetTimeout(async () => { try { - forceThreadMemoryViewRefresh(); - await start_runtime(); postMessageToMain({ diff --git a/src/mono/browser/runtime/pthreads/shared.ts b/src/mono/browser/runtime/pthreads/shared.ts index 69137957eeeeb..10450cc5bce23 100644 --- a/src/mono/browser/runtime/pthreads/shared.ts +++ b/src/mono/browser/runtime/pthreads/shared.ts @@ -10,7 +10,6 @@ import { Module, loaderHelpers, runtimeHelpers } from "../globals"; import { set_thread_prefix } from "../logging"; import { monoMessageSymbol, PThreadPtrNull, WorkerToMainMessageType } from "../types/internal"; import { threads_c_functions as tcwraps } from "../cwraps"; -import { forceThreadMemoryViewRefresh } from "../memory"; // A duplicate in loader/assets.ts export const worker_empty_prefix = " - "; @@ -76,7 +75,6 @@ export function exec_synchronization_context_pump (): void { if (!loaderHelpers.is_runtime_running()) { return; } - forceThreadMemoryViewRefresh(); try { tcwraps.mono_wasm_synchronization_context_pump(); } catch (ex) { diff --git a/src/mono/browser/runtime/pthreads/worker-thread.ts b/src/mono/browser/runtime/pthreads/worker-thread.ts index fa1269c300cb6..a0ce0b2a922a3 100644 --- a/src/mono/browser/runtime/pthreads/worker-thread.ts +++ b/src/mono/browser/runtime/pthreads/worker-thread.ts @@ -18,7 +18,6 @@ import { postRunWorker, preRunWorker } from "../startup"; import { mono_log_debug, mono_log_error } from "../logging"; import { CharPtr } from "../types/emscripten"; import { utf8ToString } from "../strings"; -import { forceThreadMemoryViewRefresh } from "../memory"; // re-export some of the events types export { @@ -70,7 +69,6 @@ function monoDedicatedChannelMessageFromMainToWorker (event: MessageEvent