Commit 2ef8f917 authored by Daniel Lehmann's avatar Daniel Lehmann Committed by V8 LUCI CQ

[wasm] Move NativeModuleModificationScope impl

In an effort to merge `CODE_SPACE_WRITE_SCOPE` and
`NativeModuleModificationScope`, this CL moves the interface and
implementation of the latter into code-space-access.{h,cc}, where the
former already lives. No other changes to the code itself.

R=clemensb@chromium.org
CC=jkummerow@chromium.org

Bug: v8:11714

Cq-Include-Trybots: luci.v8.try:v8_linux64_fyi_rel_ng
Change-Id: I1aabce26f2033430523a7a3a0a4864e7267bee21
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/2972803Reviewed-by: 's avatarClemens Backes <clemensb@chromium.org>
Commit-Queue: Daniel Lehmann <dlehmann@google.com>
Cr-Commit-Position: refs/heads/master@{#75267}
parent 089221ef
......@@ -1690,6 +1690,7 @@ filegroup(
"src/wasm/baseline/liftoff-compiler.h",
"src/wasm/baseline/liftoff-register.h",
"src/wasm/branch-hint-map.h",
"src/wasm/code-space-access.cc",
"src/wasm/code-space-access.h",
"src/wasm/compilation-environment.h",
"src/wasm/decoder.h",
......
......@@ -4116,6 +4116,7 @@ v8_source_set("v8_base_without_compiler") {
"src/trap-handler/handler-shared.cc",
"src/wasm/baseline/liftoff-assembler.cc",
"src/wasm/baseline/liftoff-compiler.cc",
"src/wasm/code-space-access.cc",
"src/wasm/function-body-decoder.cc",
"src/wasm/function-compiler.cc",
"src/wasm/graph-builder-interface.cc",
......
// Copyright 2021 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "src/wasm/code-space-access.h"
#include "src/wasm/wasm-code-manager.h"
namespace v8 {
namespace internal {
namespace wasm {
#if !(defined(V8_OS_MACOSX) && defined(V8_HOST_ARCH_ARM64))
NativeModuleModificationScope::NativeModuleModificationScope(
NativeModule* native_module)
: native_module_(native_module) {
DCHECK_NOT_NULL(native_module_);
if (FLAG_wasm_memory_protection_keys) {
bool success = native_module_->SetThreadWritable(true);
if (!success && FLAG_wasm_write_protect_code_memory) {
// Fallback to mprotect-based write protection (much slower).
success = native_module_->SetWritable(true);
CHECK(success);
}
} else if (FLAG_wasm_write_protect_code_memory) {
bool success = native_module_->SetWritable(true);
CHECK(success);
}
}
NativeModuleModificationScope::~NativeModuleModificationScope() {
if (FLAG_wasm_memory_protection_keys) {
bool success = native_module_->SetThreadWritable(false);
if (!success && FLAG_wasm_write_protect_code_memory) {
// Fallback to mprotect-based write protection (much slower).
success = native_module_->SetWritable(false);
CHECK(success);
}
} else if (FLAG_wasm_write_protect_code_memory) {
bool success = native_module_->SetWritable(false);
CHECK(success);
}
}
#endif // !(defined(V8_OS_MACOSX) && defined(V8_HOST_ARCH_ARM64))
} // namespace wasm
} // namespace internal
} // namespace v8
......@@ -16,6 +16,46 @@
namespace v8 {
namespace internal {
namespace wasm {
class NativeModule;
#if defined(V8_OS_MACOSX) && defined(V8_HOST_ARCH_ARM64)
// Arm64 on MacOS (M1 hardware) uses CodeSpaceWriteScope to switch permissions.
// TODO(wasm): Merge NativeModuleModificationScope and CodeSpaceWriteScope.
class V8_NODISCARD NativeModuleModificationScope final {
public:
explicit NativeModuleModificationScope(NativeModule*) {}
};
#else
// Within the scope, the native_module is writable and not executable.
// At the scope's destruction, the native_module is executable and not writable.
// The states inside the scope and at the scope termination are irrespective of
// native_module's state when entering the scope.
// We currently mark the entire module's memory W^X:
// - for AOT, that's as efficient as it can be.
// - for Lazy, we don't have a heuristic for functions that may need patching,
// and even if we did, the resulting set of pages may be fragmented.
// Currently, we try and keep the number of syscalls low.
// - similar argument for debug time.
class V8_NODISCARD NativeModuleModificationScope final {
public:
explicit NativeModuleModificationScope(NativeModule* native_module);
~NativeModuleModificationScope();
// Disable copy constructor and copy-assignment operator, since this manages
// a resource and implicit copying of the scope can yield surprising errors.
NativeModuleModificationScope(const NativeModuleModificationScope&) = delete;
NativeModuleModificationScope& operator=(
const NativeModuleModificationScope&) = delete;
private:
NativeModule* native_module_;
};
#endif // defined(V8_OS_MACOSX) && defined(V8_HOST_ARCH_ARM64)
} // namespace wasm
#if defined(V8_OS_MACOSX) && defined(V8_HOST_ARCH_ARM64)
// Ignoring this warning is considered better than relying on
......
......@@ -24,6 +24,7 @@
#include "src/tracing/trace-event.h"
#include "src/trap-handler/trap-handler.h"
#include "src/utils/identity-map.h"
#include "src/wasm/code-space-access.h"
#include "src/wasm/module-decoder.h"
#include "src/wasm/streaming-decoder.h"
#include "src/wasm/wasm-code-manager.h"
......
......@@ -14,6 +14,7 @@
#include "src/objects/property-descriptor.h"
#include "src/tracing/trace-event.h"
#include "src/utils/utils.h"
#include "src/wasm/code-space-access.h"
#include "src/wasm/module-compiler.h"
#include "src/wasm/wasm-constants.h"
#include "src/wasm/wasm-engine.h"
......
......@@ -2309,39 +2309,6 @@ WasmCode* WasmCodeManager::LookupCode(Address pc) const {
return candidate ? candidate->Lookup(pc) : nullptr;
}
#if !(defined(V8_OS_MACOSX) && defined(V8_HOST_ARCH_ARM64))
NativeModuleModificationScope::NativeModuleModificationScope(
NativeModule* native_module)
: native_module_(native_module) {
DCHECK_NOT_NULL(native_module_);
if (FLAG_wasm_memory_protection_keys) {
bool success = native_module_->SetThreadWritable(true);
if (!success && FLAG_wasm_write_protect_code_memory) {
// Fallback to mprotect-based write protection (much slower).
success = native_module_->SetWritable(true);
CHECK(success);
}
} else if (FLAG_wasm_write_protect_code_memory) {
bool success = native_module_->SetWritable(true);
CHECK(success);
}
}
NativeModuleModificationScope::~NativeModuleModificationScope() {
if (FLAG_wasm_memory_protection_keys) {
bool success = native_module_->SetThreadWritable(false);
if (!success && FLAG_wasm_write_protect_code_memory) {
// Fallback to mprotect-based write protection (much slower).
success = native_module_->SetWritable(false);
CHECK(success);
}
} else if (FLAG_wasm_write_protect_code_memory) {
bool success = native_module_->SetWritable(false);
CHECK(success);
}
}
#endif // !(defined(V8_OS_MACOSX) && defined(V8_HOST_ARCH_ARM64))
namespace {
thread_local WasmCodeRefScope* current_code_refs_scope = nullptr;
} // namespace
......
......@@ -1011,40 +1011,6 @@ class V8_EXPORT_PRIVATE WasmCodeManager final {
//////////////////////////////////////////////////////////////////////////////
};
#if defined(V8_OS_MACOSX) && defined(V8_HOST_ARCH_ARM64)
// Arm64 on MacOS (M1 hardware) uses CodeSpaceWriteScope to switch permissions.
// TODO(wasm): Merge NativeModuleModificationScope and CodeSpaceWriteScope.
class V8_NODISCARD NativeModuleModificationScope final {
public:
explicit NativeModuleModificationScope(NativeModule*) {}
};
#else
// Within the scope, the native_module is writable and not executable.
// At the scope's destruction, the native_module is executable and not writable.
// The states inside the scope and at the scope termination are irrespective of
// native_module's state when entering the scope.
// We currently mark the entire module's memory W^X:
// - for AOT, that's as efficient as it can be.
// - for Lazy, we don't have a heuristic for functions that may need patching,
// and even if we did, the resulting set of pages may be fragmented.
// Currently, we try and keep the number of syscalls low.
// - similar argument for debug time.
class V8_NODISCARD NativeModuleModificationScope final {
public:
explicit NativeModuleModificationScope(NativeModule* native_module);
~NativeModuleModificationScope();
// Disable copy constructor and copy-assignment operator, since this manages
// a resource and implicit copying of the scope can yield surprising errors.
NativeModuleModificationScope(const NativeModuleModificationScope&) = delete;
NativeModuleModificationScope& operator=(
const NativeModuleModificationScope&) = delete;
private:
NativeModule* native_module_;
};
#endif
// {WasmCodeRefScope}s form a perfect stack. New {WasmCode} pointers generated
// by e.g. creating new code or looking up code by its address are added to the
// top-most {WasmCodeRefScope}.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment