Skip to content

Commit

Permalink
deps: V8: cherry-pick 27e1ac1a79ff
Browse files Browse the repository at this point in the history
Original commit message:

    [wasm][mac] Support w^x codespaces for Apple Silicon

    Apple's upcoming arm64 devices will prevent rwx access to memory,
    but in turn provide a new per-thread way to switch between write
    and execute permissions. This patch puts that system to use for
    the WebAssembly subsystem.
    The approach relies on CodeSpaceWriteScope objects for now. That
    isn't optimal for background threads (which could stay in "write"
    mode permanently instead of toggling), but its simplicity makes
    it a good first step.

    Background:
    https://developer.apple.com/documentation/apple_silicon/porting_just-in-time_compilers_to_apple_silicon

    Bug: chromium:1117591
    Change-Id: I3b60f0efd34c0fed924dfc71ee2c7805801c5d42
    Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/2378307
    Commit-Queue: Jakob Kummerow <[email protected]>
    Reviewed-by: Michael Lippautz <[email protected]>
    Reviewed-by: Thibaud Michaud <[email protected]>
    Cr-Commit-Position: refs/heads/master@{#69791}

Backport-PR-URL: #38051
Co-authored-by: BoHong Li <[email protected]>

PR-URL: #35986
Reviewed-By: Anna Henningsen <[email protected]>
Reviewed-By: Richard Lau <[email protected]>
Reviewed-By: Michael Dawson <[email protected]>
Reviewed-By: Rich Trott <[email protected]>
Reviewed-By: Beth Griggs <[email protected]>
Reviewed-By: Jiawen Geng <[email protected]>
  • Loading branch information
oleavr authored and targos committed Apr 11, 2021
1 parent 6b115d7 commit 31f8610
Show file tree
Hide file tree
Showing 12 changed files with 143 additions and 5 deletions.
2 changes: 1 addition & 1 deletion common.gypi
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@

# Reset this number to 0 on major V8 upgrades.
# Increment by one for each non-official patch applied to deps/v8.
'v8_embedder_string': '-node.20',
'v8_embedder_string': '-node.21',

##### V8 defaults for Node.js #####

Expand Down
1 change: 1 addition & 0 deletions deps/v8/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -3080,6 +3080,7 @@ v8_source_set("v8_base_without_compiler") {
"src/wasm/baseline/liftoff-compiler.cc",
"src/wasm/baseline/liftoff-compiler.h",
"src/wasm/baseline/liftoff-register.h",
"src/wasm/code-space-access.h",
"src/wasm/compilation-environment.h",
"src/wasm/decoder.h",
"src/wasm/function-body-decoder-impl.h",
Expand Down
8 changes: 8 additions & 0 deletions deps/v8/src/base/platform/platform-posix.cc
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,14 @@ int GetFlagsForMemoryPermission(OS::MemoryPermission access) {
#if V8_OS_QNX
flags |= MAP_LAZY;
#endif // V8_OS_QNX
#if V8_OS_MACOSX && V8_HOST_ARCH_ARM64 && defined(MAP_JIT) && \
!defined(V8_OS_IOS)
// TODO(jkummerow): using the V8_OS_IOS define is a crude approximation
// of the fact that we don't want to set the MAP_JIT flag when
// FLAG_jitless == true, as src/base/ doesn't know any flags.
// TODO(crbug.com/1117591): This is only needed for code spaces.
flags |= MAP_JIT;
#endif
}
return flags;
}
Expand Down
69 changes: 69 additions & 0 deletions deps/v8/src/wasm/code-space-access.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// Copyright 2020 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.

#ifndef V8_WASM_CODE_SPACE_ACCESS_H_
#define V8_WASM_CODE_SPACE_ACCESS_H_

#include "src/base/build_config.h"
#include "src/base/macros.h"
#include "src/common/globals.h"

namespace v8 {
namespace internal {

#if defined(V8_OS_MACOSX) && defined(V8_HOST_ARCH_ARM64)

// Ignoring this warning is considered better than relying on
// __builtin_available.
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wunguarded-availability-new"
inline void SwitchMemoryPermissionsToWritable() {
pthread_jit_write_protect_np(0);
}
inline void SwitchMemoryPermissionsToExecutable() {
pthread_jit_write_protect_np(1);
}
#pragma clang diagnostic pop

namespace wasm {

class CodeSpaceWriteScope {
public:
// TODO(jkummerow): Background threads could permanently stay in
// writable mode; only the main thread has to switch back and forth.
CodeSpaceWriteScope() {
if (code_space_write_nesting_level_ == 0) {
SwitchMemoryPermissionsToWritable();
}
code_space_write_nesting_level_++;
}
~CodeSpaceWriteScope() {
code_space_write_nesting_level_--;
if (code_space_write_nesting_level_ == 0) {
SwitchMemoryPermissionsToExecutable();
}
}

private:
static thread_local int code_space_write_nesting_level_;
};

#define CODE_SPACE_WRITE_SCOPE CodeSpaceWriteScope _write_access_;

} // namespace wasm

#else // Not Mac-on-arm64.

// Nothing to do, we map code memory with rwx permissions.
inline void SwitchMemoryPermissionsToWritable() {}
inline void SwitchMemoryPermissionsToExecutable() {}

#define CODE_SPACE_WRITE_SCOPE

#endif // V8_OS_MACOSX && V8_HOST_ARCH_ARM64

} // namespace internal
} // namespace v8

#endif // V8_WASM_CODE_SPACE_ACCESS_H_
15 changes: 15 additions & 0 deletions deps/v8/src/wasm/wasm-code-manager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#include <iomanip>

#include "src/base/build_config.h"
#include "src/base/iterator.h"
#include "src/base/macros.h"
#include "src/base/platform/platform.h"
Expand All @@ -21,6 +22,7 @@
#include "src/snapshot/embedded/embedded-data.h"
#include "src/utils/ostreams.h"
#include "src/utils/vector.h"
#include "src/wasm/code-space-access.h"
#include "src/wasm/compilation-environment.h"
#include "src/wasm/function-compiler.h"
#include "src/wasm/jump-table-assembler.h"
Expand All @@ -47,6 +49,10 @@ namespace wasm {

using trap_handler::ProtectedInstructionData;

#if defined(V8_OS_MACOSX) && defined(V8_HOST_ARCH_ARM64)
thread_local int CodeSpaceWriteScope::code_space_write_nesting_level_ = 0;
#endif

base::AddressRegion DisjointAllocationPool::Merge(
base::AddressRegion new_region) {
// Find the possible insertion position by identifying the first region whose
Expand Down Expand Up @@ -731,6 +737,7 @@ void WasmCodeAllocator::FreeCode(Vector<WasmCode* const> codes) {
// Zap code area and collect freed code regions.
DisjointAllocationPool freed_regions;
size_t code_size = 0;
CODE_SPACE_WRITE_SCOPE
for (WasmCode* code : codes) {
ZapCode(code->instruction_start(), code->instructions().size());
FlushInstructionCache(code->instruction_start(),
Expand Down Expand Up @@ -842,6 +849,7 @@ CompilationEnv NativeModule::CreateCompilationEnv() const {
}

WasmCode* NativeModule::AddCodeForTesting(Handle<Code> code) {
CODE_SPACE_WRITE_SCOPE
// For off-heap builtins, we create a copy of the off-heap instruction stream
// instead of the on-heap code object containing the trampoline. Ensure that
// we do not apply the on-heap reloc info to the off-heap instructions.
Expand Down Expand Up @@ -937,6 +945,7 @@ void NativeModule::UseLazyStub(uint32_t func_index) {
if (!lazy_compile_table_) {
uint32_t num_slots = module_->num_declared_functions;
WasmCodeRefScope code_ref_scope;
CODE_SPACE_WRITE_SCOPE
base::AddressRegion single_code_space_region;
{
base::MutexGuard guard(&allocation_mutex_);
Expand Down Expand Up @@ -998,6 +1007,7 @@ std::unique_ptr<WasmCode> NativeModule::AddCodeWithCodeSpace(
const int code_comments_offset = desc.code_comments_offset;
const int instr_size = desc.instr_size;

CODE_SPACE_WRITE_SCOPE
memcpy(dst_code_bytes.begin(), desc.buffer,
static_cast<size_t>(desc.instr_size));

Expand Down Expand Up @@ -1122,6 +1132,7 @@ WasmCode* NativeModule::AddDeserializedCode(
Vector<const byte> protected_instructions_data,
Vector<const byte> reloc_info, Vector<const byte> source_position_table,
WasmCode::Kind kind, ExecutionTier tier) {
// CodeSpaceWriteScope is provided by the caller.
Vector<uint8_t> dst_code_bytes =
code_allocator_.AllocateForCode(this, instructions.size());
memcpy(dst_code_bytes.begin(), instructions.begin(), instructions.size());
Expand Down Expand Up @@ -1180,6 +1191,7 @@ WasmCode* NativeModule::CreateEmptyJumpTableInRegion(
Vector<uint8_t> code_space = code_allocator_.AllocateForCodeInRegion(
this, jump_table_size, region, allocator_lock);
DCHECK(!code_space.empty());
CODE_SPACE_WRITE_SCOPE
ZapCode(reinterpret_cast<Address>(code_space.begin()), code_space.size());
std::unique_ptr<WasmCode> code{
new WasmCode{this, // native_module
Expand All @@ -1205,6 +1217,7 @@ void NativeModule::PatchJumpTablesLocked(uint32_t slot_index, Address target) {
// The caller must hold the {allocation_mutex_}, thus we fail to lock it here.
DCHECK(!allocation_mutex_.TryLock());

CODE_SPACE_WRITE_SCOPE
for (auto& code_space_data : code_space_data_) {
DCHECK_IMPLIES(code_space_data.jump_table, code_space_data.far_jump_table);
if (!code_space_data.jump_table) continue;
Expand Down Expand Up @@ -1267,6 +1280,7 @@ void NativeModule::AddCodeSpace(
#endif // V8_OS_WIN64

WasmCodeRefScope code_ref_scope;
CODE_SPACE_WRITE_SCOPE
WasmCode* jump_table = nullptr;
WasmCode* far_jump_table = nullptr;
const uint32_t num_wasm_functions = module_->num_declared_functions;
Expand Down Expand Up @@ -1820,6 +1834,7 @@ std::vector<std::unique_ptr<WasmCode>> NativeModule::AddCompiledCode(
generated_code.reserve(results.size());

// Now copy the generated code into the code space and relocate it.
CODE_SPACE_WRITE_SCOPE
for (auto& result : results) {
DCHECK_EQ(result.code_desc.buffer, result.instr_buffer.get());
size_t code_size = RoundUp<kCodeAlignment>(result.code_desc.instr_size);
Expand Down
2 changes: 2 additions & 0 deletions deps/v8/src/wasm/wasm-serialization.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "src/utils/ostreams.h"
#include "src/utils/utils.h"
#include "src/utils/version.h"
#include "src/wasm/code-space-access.h"
#include "src/wasm/function-compiler.h"
#include "src/wasm/module-compiler.h"
#include "src/wasm/module-decoder.h"
Expand Down Expand Up @@ -520,6 +521,7 @@ bool NativeModuleDeserializer::ReadCode(int fn_index, Reader* reader) {
auto protected_instructions =
reader->ReadVector<byte>(protected_instructions_size);

CODE_SPACE_WRITE_SCOPE
WasmCode* code = native_module_->AddDeserializedCode(
fn_index, code_buffer, stack_slot_count, tagged_parameter_slots,
safepoint_table_offset, handler_table_offset, constant_pool_offset,
Expand Down
7 changes: 7 additions & 0 deletions deps/v8/test/cctest/cctest.status
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,13 @@
'test-debug/DebugBreakStackTrace': [PASS, SLOW],
}], # 'arch == arm64 and simulator_run'

['arch == arm64 and system == macos and not simulator_run', {
# printf, being a variadic function, has a different, stack-based ABI on
# Apple silicon. See:
# https://developer.apple.com/library/archive/documentation/Xcode/Conceptual/iPhoneOSABIReference/Articles/ARM64FunctionCallingConventions.html
'test-assembler-arm64/printf_no_preserve': [SKIP],
}], # arch == arm64 and system == macos and not simulator_run

##############################################################################
['variant == nooptimization and (arch == arm or arch == arm64) and simulator_run', {
# Slow tests: https://crbug.com/v8/7783
Expand Down
4 changes: 2 additions & 2 deletions deps/v8/test/cctest/test-assembler-arm64.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11599,9 +11599,9 @@ TEST(system_msr) {
const uint64_t fpcr_core = 0x07C00000;

// All FPCR fields (including fields which may be read-as-zero):
// Stride, Len
// Stride, FZ16, Len
// IDE, IXE, UFE, OFE, DZE, IOE
const uint64_t fpcr_all = fpcr_core | 0x00379F00;
const uint64_t fpcr_all = fpcr_core | 0x003F9F00;

SETUP();

Expand Down
5 changes: 3 additions & 2 deletions deps/v8/test/cctest/test-code-stub-assembler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,9 @@ Handle<String> MakeName(const char* str, int suffix) {
return MakeString(buffer.begin());
}

int sum10(int a0, int a1, int a2, int a3, int a4, int a5, int a6, int a7,
int a8, int a9) {
intptr_t sum10(intptr_t a0, intptr_t a1, intptr_t a2, intptr_t a3, intptr_t a4,
intptr_t a5, intptr_t a6, intptr_t a7, intptr_t a8,
intptr_t a9) {
return a0 + a1 + a2 + a3 + a4 + a5 + a6 + a7 + a8 + a9;
}

Expand Down
5 changes: 5 additions & 0 deletions deps/v8/test/cctest/test-icache.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "src/codegen/macro-assembler-inl.h"
#include "src/execution/simulator.h"
#include "src/handles/handles-inl.h"
#include "src/wasm/code-space-access.h"
#include "test/cctest/cctest.h"
#include "test/common/assembler-tester.h"

Expand Down Expand Up @@ -179,11 +180,15 @@ TEST(TestFlushICacheOfWritableAndExecutable) {

CHECK(SetPermissions(GetPlatformPageAllocator(), buffer->start(),
buffer->size(), v8::PageAllocator::kReadWriteExecute));
SwitchMemoryPermissionsToWritable();
FloodWithInc(isolate, buffer.get());
FlushInstructionCache(buffer->start(), buffer->size());
SwitchMemoryPermissionsToExecutable();
CHECK_EQ(23 + kNumInstr, f.Call(23)); // Call into generated code.
SwitchMemoryPermissionsToWritable();
FloodWithNop(isolate, buffer.get());
FlushInstructionCache(buffer->start(), buffer->size());
SwitchMemoryPermissionsToExecutable();
CHECK_EQ(23, f.Call(23)); // Call into generated code.
}
}
Expand Down
9 changes: 9 additions & 0 deletions deps/v8/test/cctest/wasm/test-jump-table-assembler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "src/codegen/macro-assembler-inl.h"
#include "src/execution/simulator.h"
#include "src/utils/utils.h"
#include "src/wasm/code-space-access.h"
#include "src/wasm/jump-table-assembler.h"
#include "test/cctest/cctest.h"
#include "test/common/assembler-tester.h"
Expand All @@ -33,7 +34,12 @@ constexpr uint32_t kJumpTableSize =
JumpTableAssembler::SizeForNumberOfSlots(kJumpTableSlotCount);

// Must be a safe commit page size.
#if V8_OS_MACOSX && V8_HOST_ARCH_ARM64
// See kAppleArmPageSize in platform-posix.cc.
constexpr size_t kThunkBufferSize = 1 << 14;
#else
constexpr size_t kThunkBufferSize = 4 * KB;
#endif

#if V8_TARGET_ARCH_ARM64 || V8_TARGET_ARCH_X64
constexpr uint32_t kAvailableBufferSlots =
Expand Down Expand Up @@ -151,6 +157,7 @@ class JumpTableRunner : public v8::base::Thread {

void Run() override {
TRACE("Runner #%d is starting ...\n", runner_id_);
SwitchMemoryPermissionsToExecutable();
GeneratedCode<void>::FromAddress(CcTest::i_isolate(), slot_address_).Call();
TRACE("Runner #%d is stopping ...\n", runner_id_);
USE(runner_id_);
Expand All @@ -173,6 +180,7 @@ class JumpTablePatcher : public v8::base::Thread {

void Run() override {
TRACE("Patcher %p is starting ...\n", this);
SwitchMemoryPermissionsToWritable();
Address slot_address =
slot_start_ + JumpTableAssembler::JumpSlotIndexToOffset(slot_index_);
// First, emit code to the two thunks.
Expand Down Expand Up @@ -232,6 +240,7 @@ TEST(JumpTablePatchingStress) {

std::bitset<kAvailableBufferSlots> used_thunk_slots;
buffer->MakeWritableAndExecutable();
SwitchMemoryPermissionsToWritable();

// Iterate through jump-table slots to hammer at different alignments within
// the jump-table, thereby increasing stress for variable-length ISAs.
Expand Down
21 changes: 21 additions & 0 deletions deps/v8/test/unittests/unittests.status
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,27 @@
'RandomNumberGenerator.NextSampleSlowInvalidParam2': [SKIP],
}], # system == macos and asan

['system == macos and arch == arm64 and not simulator_run', {
# Throwing C++ exceptions doesn't work; probably because the unittests
# binary is built with -fno-exceptions?
'LanguageServerJson.LexerError': [SKIP],
'LanguageServerJson.ParserError': [SKIP],
'Torque.DoubleUnderScorePrefixIllegalForIdentifiers': [SKIP],
'Torque.Enums': [SKIP],
'Torque.ImportNonExistentFile': [SKIP],

# Test uses fancy signal handling. Needs investigation.
'MemoryAllocationPermissionsTest.DoTest': [SKIP],

# cppgc::internal::kGuardPageSize is smaller than kAppleArmPageSize.
'PageMemoryRegionTest.PlatformUsesGuardPages': [FAIL],

# Time tick resolution appears to be ~42 microseconds. Tests expect 1 us.
'TimeTicks.NowResolution': [FAIL],
'RuntimeCallStatsTest.BasicJavaScript': [SKIP],
'RuntimeCallStatsTest.FunctionLengthGetter': [SKIP],
}], # system == macos and arch == arm64 and not simulator_run

##############################################################################
['lite_mode or variant == jitless', {
# TODO(v8:7777): Re-enable once wasm is supported in jitless mode.
Expand Down

0 comments on commit 31f8610

Please sign in to comment.