-
Notifications
You must be signed in to change notification settings - Fork 29.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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 <jkummerow@chromium.org> Reviewed-by: Michael Lippautz <mlippautz@chromium.org> Reviewed-by: Thibaud Michaud <thibaudm@chromium.org> Cr-Commit-Position: refs/heads/master@{#69791} PR-URL: #35986 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Richard Lau <rlau@redhat.com> Reviewed-By: Michael Dawson <midawson@redhat.com> Reviewed-By: Rich Trott <rtrott@gmail.com> Reviewed-By: Beth Griggs <bgriggs@redhat.com> Reviewed-By: Jiawen Geng <technicalcute@gmail.com>
- Loading branch information
1 parent
4140f49
commit d2c757a
Showing
12 changed files
with
143 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters