66
77#include < iomanip>
88
9+ #include " src/base/build_config.h"
910#include " src/base/iterator.h"
1011#include " src/base/macros.h"
1112#include " src/base/platform/platform.h"
2122#include " src/snapshot/embedded/embedded-data.h"
2223#include " src/utils/ostreams.h"
2324#include " src/utils/vector.h"
25+ #include " src/wasm/code-space-access.h"
2426#include " src/wasm/compilation-environment.h"
2527#include " src/wasm/function-compiler.h"
2628#include " src/wasm/jump-table-assembler.h"
@@ -47,6 +49,10 @@ namespace wasm {
4749
4850using trap_handler::ProtectedInstructionData;
4951
52+ #if defined(V8_OS_MACOSX) && defined(V8_HOST_ARCH_ARM64)
53+ thread_local int CodeSpaceWriteScope::code_space_write_nesting_level_ = 0 ;
54+ #endif
55+
5056base::AddressRegion DisjointAllocationPool::Merge (
5157 base::AddressRegion new_region) {
5258 // Find the possible insertion position by identifying the first region whose
@@ -731,6 +737,7 @@ void WasmCodeAllocator::FreeCode(Vector<WasmCode* const> codes) {
731737 // Zap code area and collect freed code regions.
732738 DisjointAllocationPool freed_regions;
733739 size_t code_size = 0 ;
740+ CODE_SPACE_WRITE_SCOPE
734741 for (WasmCode* code : codes) {
735742 ZapCode (code->instruction_start (), code->instructions ().size ());
736743 FlushInstructionCache (code->instruction_start (),
@@ -842,6 +849,7 @@ CompilationEnv NativeModule::CreateCompilationEnv() const {
842849}
843850
844851WasmCode* NativeModule::AddCodeForTesting (Handle<Code> code) {
852+ CODE_SPACE_WRITE_SCOPE
845853 // For off-heap builtins, we create a copy of the off-heap instruction stream
846854 // instead of the on-heap code object containing the trampoline. Ensure that
847855 // we do not apply the on-heap reloc info to the off-heap instructions.
@@ -937,6 +945,7 @@ void NativeModule::UseLazyStub(uint32_t func_index) {
937945 if (!lazy_compile_table_) {
938946 uint32_t num_slots = module_->num_declared_functions ;
939947 WasmCodeRefScope code_ref_scope;
948+ CODE_SPACE_WRITE_SCOPE
940949 base::AddressRegion single_code_space_region;
941950 {
942951 base::MutexGuard guard (&allocation_mutex_);
@@ -998,6 +1007,7 @@ std::unique_ptr<WasmCode> NativeModule::AddCodeWithCodeSpace(
9981007 const int code_comments_offset = desc.code_comments_offset ;
9991008 const int instr_size = desc.instr_size ;
10001009
1010+ CODE_SPACE_WRITE_SCOPE
10011011 memcpy (dst_code_bytes.begin (), desc.buffer ,
10021012 static_cast <size_t >(desc.instr_size ));
10031013
@@ -1122,6 +1132,7 @@ WasmCode* NativeModule::AddDeserializedCode(
11221132 Vector<const byte> protected_instructions_data,
11231133 Vector<const byte> reloc_info, Vector<const byte> source_position_table,
11241134 WasmCode::Kind kind, ExecutionTier tier) {
1135+ // CodeSpaceWriteScope is provided by the caller.
11251136 Vector<uint8_t > dst_code_bytes =
11261137 code_allocator_.AllocateForCode (this , instructions.size ());
11271138 memcpy (dst_code_bytes.begin (), instructions.begin (), instructions.size ());
@@ -1180,6 +1191,7 @@ WasmCode* NativeModule::CreateEmptyJumpTableInRegion(
11801191 Vector<uint8_t > code_space = code_allocator_.AllocateForCodeInRegion (
11811192 this , jump_table_size, region, allocator_lock);
11821193 DCHECK (!code_space.empty ());
1194+ CODE_SPACE_WRITE_SCOPE
11831195 ZapCode (reinterpret_cast <Address>(code_space.begin ()), code_space.size ());
11841196 std::unique_ptr<WasmCode> code{
11851197 new WasmCode{this , // native_module
@@ -1205,6 +1217,7 @@ void NativeModule::PatchJumpTablesLocked(uint32_t slot_index, Address target) {
12051217 // The caller must hold the {allocation_mutex_}, thus we fail to lock it here.
12061218 DCHECK (!allocation_mutex_.TryLock ());
12071219
1220+ CODE_SPACE_WRITE_SCOPE
12081221 for (auto & code_space_data : code_space_data_) {
12091222 DCHECK_IMPLIES (code_space_data.jump_table , code_space_data.far_jump_table );
12101223 if (!code_space_data.jump_table ) continue ;
@@ -1267,6 +1280,7 @@ void NativeModule::AddCodeSpace(
12671280#endif // V8_OS_WIN64
12681281
12691282 WasmCodeRefScope code_ref_scope;
1283+ CODE_SPACE_WRITE_SCOPE
12701284 WasmCode* jump_table = nullptr ;
12711285 WasmCode* far_jump_table = nullptr ;
12721286 const uint32_t num_wasm_functions = module_->num_declared_functions ;
@@ -1820,6 +1834,7 @@ std::vector<std::unique_ptr<WasmCode>> NativeModule::AddCompiledCode(
18201834 generated_code.reserve (results.size ());
18211835
18221836 // Now copy the generated code into the code space and relocate it.
1837+ CODE_SPACE_WRITE_SCOPE
18231838 for (auto & result : results) {
18241839 DCHECK_EQ (result.code_desc .buffer , result.instr_buffer .get ());
18251840 size_t code_size = RoundUp<kCodeAlignment >(result.code_desc .instr_size );
0 commit comments