rlbox_wasm2c_thread_locals.cpp (1808B)
1 /* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* This Source Code Form is subject to the terms of the Mozilla Public 3 * License, v. 2.0. If a copy of the MPL was not distributed with this 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 5 6 #ifdef MOZ_USING_WASM_SANDBOXING 7 8 // For MOZ_CRASH_UNSAFE_PRINTF 9 # include "mozilla/Assertions.h" 10 11 # include "mozilla/mozalloc_oom.h" 12 13 // Load general firefox configuration of RLBox 14 # include "mozilla/rlbox/rlbox_config.h" 15 # include "mozilla/rlbox/rlbox_wasm2c_tls.hpp" 16 # include "wasm-rt.h" 17 18 # ifndef WASM_RT_GROW_FAILED_CRASH 19 # include "nsExceptionHandler.h" 20 # endif 21 22 // The MingW compiler does not correctly handle static thread_local inline 23 // members. We instead TLS storage via functions. This can be removed if the 24 // MingW bug is fixed. 25 RLBOX_WASM2C_SANDBOX_STATIC_VARIABLES(); 26 27 extern "C" { 28 29 // Any error encountered by the wasm2c runtime or wasm sandboxed library code 30 // is configured to call the below trap handler. 31 void moz_wasm2c_trap_handler(wasm_rt_trap_t code) { 32 MOZ_CRASH_UNSAFE_PRINTF("wasm2c crash: %s", wasm_rt_strerror(code)); 33 } 34 35 // The below function is called if a malloc in sandboxed code returns null 36 // This indicates that the sandbox has run out of memory. 37 void moz_wasm2c_memgrow_failed() { 38 # ifdef WASM_RT_GROW_FAILED_CRASH 39 MOZ_CRASH("wasm2c memory grow failed"); 40 # else 41 CrashReporter::RecordAnnotationBool( 42 CrashReporter::Annotation::WasmLibrarySandboxMallocFailed, true); 43 # endif 44 } 45 46 // This function is called when mozalloc_handle_oom is called from within 47 // the sandbox. We redirect to that function, ignoring the ctx argument, which 48 // is the sandbox itself. 49 void w2c_env_mozalloc_handle_oom(void* ctx, uint32_t size) { 50 mozalloc_handle_oom(size); 51 } 52 } 53 54 #endif