11cb0ef41Sopenharmony_ci// Copyright 2020 the V8 project authors. All rights reserved. 21cb0ef41Sopenharmony_ci// Use of this source code is governed by a BSD-style license that can be 31cb0ef41Sopenharmony_ci// found in the LICENSE file. 41cb0ef41Sopenharmony_ci 51cb0ef41Sopenharmony_ci#if !V8_ENABLE_WEBASSEMBLY 61cb0ef41Sopenharmony_ci#error This header should only be included if WebAssembly is enabled. 71cb0ef41Sopenharmony_ci#endif // !V8_ENABLE_WEBASSEMBLY 81cb0ef41Sopenharmony_ci 91cb0ef41Sopenharmony_ci#ifndef V8_WASM_CODE_SPACE_ACCESS_H_ 101cb0ef41Sopenharmony_ci#define V8_WASM_CODE_SPACE_ACCESS_H_ 111cb0ef41Sopenharmony_ci 121cb0ef41Sopenharmony_ci#include "src/base/build_config.h" 131cb0ef41Sopenharmony_ci#include "src/base/macros.h" 141cb0ef41Sopenharmony_ci#include "src/common/globals.h" 151cb0ef41Sopenharmony_ci 161cb0ef41Sopenharmony_cinamespace v8 { 171cb0ef41Sopenharmony_cinamespace internal { 181cb0ef41Sopenharmony_ci 191cb0ef41Sopenharmony_cinamespace wasm { 201cb0ef41Sopenharmony_ci 211cb0ef41Sopenharmony_ciclass NativeModule; 221cb0ef41Sopenharmony_ci 231cb0ef41Sopenharmony_ci// Within the scope, the code space is writable (and for Apple M1 also not 241cb0ef41Sopenharmony_ci// executable). After the last (nested) scope is destructed, the code space is 251cb0ef41Sopenharmony_ci// not writable. 261cb0ef41Sopenharmony_ci// This uses three different implementations, depending on the platform, flags, 271cb0ef41Sopenharmony_ci// and runtime support: 281cb0ef41Sopenharmony_ci// - On MacOS on ARM64 ("Apple M1"/Apple Silicon), it uses APRR/MAP_JIT to 291cb0ef41Sopenharmony_ci// switch only the calling thread between writable and executable. This achieves 301cb0ef41Sopenharmony_ci// "real" W^X and is thread-local and fast. 311cb0ef41Sopenharmony_ci// - When Intel PKU (aka. memory protection keys) are available, it switches 321cb0ef41Sopenharmony_ci// the protection keys' permission between writable and not writable. The 331cb0ef41Sopenharmony_ci// executable permission cannot be retracted with PKU. That is, this "only" 341cb0ef41Sopenharmony_ci// achieves write-protection, but is similarly thread-local and fast. 351cb0ef41Sopenharmony_ci// - As a fallback, we switch with {mprotect()} between R-X and RWX (due to 361cb0ef41Sopenharmony_ci// concurrent compilation and execution). This is slow and process-wide. With 371cb0ef41Sopenharmony_ci// {mprotect()}, we currently switch permissions for the entire module's memory: 381cb0ef41Sopenharmony_ci// - for AOT, that's as efficient as it can be. 391cb0ef41Sopenharmony_ci// - for Lazy, we don't have a heuristic for functions that may need patching, 401cb0ef41Sopenharmony_ci// and even if we did, the resulting set of pages may be fragmented. 411cb0ef41Sopenharmony_ci// Currently, we try and keep the number of syscalls low. 421cb0ef41Sopenharmony_ci// - similar argument for debug time. 431cb0ef41Sopenharmony_ci// MAP_JIT on Apple M1 cannot switch permissions for smaller ranges of memory, 441cb0ef41Sopenharmony_ci// and for PKU we would need multiple keys, so both of them also switch 451cb0ef41Sopenharmony_ci// permissions for all code pages. 461cb0ef41Sopenharmony_ciclass V8_NODISCARD CodeSpaceWriteScope final { 471cb0ef41Sopenharmony_ci public: 481cb0ef41Sopenharmony_ci explicit V8_EXPORT_PRIVATE CodeSpaceWriteScope(NativeModule*); 491cb0ef41Sopenharmony_ci V8_EXPORT_PRIVATE ~CodeSpaceWriteScope(); 501cb0ef41Sopenharmony_ci 511cb0ef41Sopenharmony_ci // Disable copy constructor and copy-assignment operator, since this manages 521cb0ef41Sopenharmony_ci // a resource and implicit copying of the scope can yield surprising errors. 531cb0ef41Sopenharmony_ci CodeSpaceWriteScope(const CodeSpaceWriteScope&) = delete; 541cb0ef41Sopenharmony_ci CodeSpaceWriteScope& operator=(const CodeSpaceWriteScope&) = delete; 551cb0ef41Sopenharmony_ci 561cb0ef41Sopenharmony_ci static bool IsInScope() { return current_native_module_ != nullptr; } 571cb0ef41Sopenharmony_ci 581cb0ef41Sopenharmony_ci private: 591cb0ef41Sopenharmony_ci // The M1 implementation knows implicitly from the {MAP_JIT} flag during 601cb0ef41Sopenharmony_ci // allocation which region to switch permissions for. On non-M1 hardware 611cb0ef41Sopenharmony_ci // without memory protection key support, we need the code space from the 621cb0ef41Sopenharmony_ci // {NativeModule}. 631cb0ef41Sopenharmony_ci static thread_local NativeModule* current_native_module_; 641cb0ef41Sopenharmony_ci 651cb0ef41Sopenharmony_ci // {SetWritable} and {SetExecutable} implicitly operate on 661cb0ef41Sopenharmony_ci // {current_native_module_} (for mprotect-based protection). 671cb0ef41Sopenharmony_ci static void SetWritable(); 681cb0ef41Sopenharmony_ci static void SetExecutable(); 691cb0ef41Sopenharmony_ci 701cb0ef41Sopenharmony_ci // Returns {true} if switching permissions happens on a per-module level, and 711cb0ef41Sopenharmony_ci // not globally (like for MAP_JIT and PKU). 721cb0ef41Sopenharmony_ci static bool SwitchingPerNativeModule(); 731cb0ef41Sopenharmony_ci 741cb0ef41Sopenharmony_ci // Save the previous module to put it back in {current_native_module_} when 751cb0ef41Sopenharmony_ci // exiting this scope. 761cb0ef41Sopenharmony_ci NativeModule* const previous_native_module_; 771cb0ef41Sopenharmony_ci}; 781cb0ef41Sopenharmony_ci 791cb0ef41Sopenharmony_ci} // namespace wasm 801cb0ef41Sopenharmony_ci} // namespace internal 811cb0ef41Sopenharmony_ci} // namespace v8 821cb0ef41Sopenharmony_ci 831cb0ef41Sopenharmony_ci#endif // V8_WASM_CODE_SPACE_ACCESS_H_ 84