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 (),
@@ -847,6 +854,7 @@ CompilationEnv NativeModule::CreateCompilationEnv() const {
847854}
848855
849856WasmCode* NativeModule::AddCodeForTesting (Handle<Code> code) {
857+ CODE_SPACE_WRITE_SCOPE
850858 // For off-heap builtins, we create a copy of the off-heap instruction stream
851859 // instead of the on-heap code object containing the trampoline. Ensure that
852860 // we do not apply the on-heap reloc info to the off-heap instructions.
@@ -942,6 +950,7 @@ void NativeModule::UseLazyStub(uint32_t func_index) {
942950 if (!lazy_compile_table_) {
943951 uint32_t num_slots = module_->num_declared_functions ;
944952 WasmCodeRefScope code_ref_scope;
953+ CODE_SPACE_WRITE_SCOPE
945954 base::AddressRegion single_code_space_region;
946955 {
947956 base::MutexGuard guard (&allocation_mutex_);
@@ -1003,6 +1012,7 @@ std::unique_ptr<WasmCode> NativeModule::AddCodeWithCodeSpace(
10031012 const int code_comments_offset = desc.code_comments_offset ;
10041013 const int instr_size = desc.instr_size ;
10051014
1015+ CODE_SPACE_WRITE_SCOPE
10061016 memcpy (dst_code_bytes.begin (), desc.buffer ,
10071017 static_cast <size_t >(desc.instr_size ));
10081018
@@ -1138,6 +1148,7 @@ WasmCode* NativeModule::AddDeserializedCode(
11381148 Vector<const byte> protected_instructions_data,
11391149 Vector<const byte> reloc_info, Vector<const byte> source_position_table,
11401150 WasmCode::Kind kind, ExecutionTier tier) {
1151+ // CodeSpaceWriteScope is provided by the caller.
11411152 Vector<uint8_t > dst_code_bytes =
11421153 code_allocator_.AllocateForCode (this , instructions.size ());
11431154 memcpy (dst_code_bytes.begin (), instructions.begin (), instructions.size ());
@@ -1196,6 +1207,7 @@ WasmCode* NativeModule::CreateEmptyJumpTableInRegion(
11961207 Vector<uint8_t > code_space = code_allocator_.AllocateForCodeInRegion (
11971208 this , jump_table_size, region, allocator_lock);
11981209 DCHECK (!code_space.empty ());
1210+ CODE_SPACE_WRITE_SCOPE
11991211 ZapCode (reinterpret_cast <Address>(code_space.begin ()), code_space.size ());
12001212 std::unique_ptr<WasmCode> code{
12011213 new WasmCode{this , // native_module
@@ -1221,6 +1233,7 @@ void NativeModule::PatchJumpTablesLocked(uint32_t slot_index, Address target) {
12211233 // The caller must hold the {allocation_mutex_}, thus we fail to lock it here.
12221234 DCHECK (!allocation_mutex_.TryLock ());
12231235
1236+ CODE_SPACE_WRITE_SCOPE
12241237 for (auto & code_space_data : code_space_data_) {
12251238 DCHECK_IMPLIES (code_space_data.jump_table , code_space_data.far_jump_table );
12261239 if (!code_space_data.jump_table ) continue ;
@@ -1283,6 +1296,7 @@ void NativeModule::AddCodeSpace(
12831296#endif // V8_OS_WIN64
12841297
12851298 WasmCodeRefScope code_ref_scope;
1299+ CODE_SPACE_WRITE_SCOPE
12861300 WasmCode* jump_table = nullptr ;
12871301 WasmCode* far_jump_table = nullptr ;
12881302 const uint32_t num_wasm_functions = module_->num_declared_functions ;
@@ -1843,6 +1857,7 @@ std::vector<std::unique_ptr<WasmCode>> NativeModule::AddCompiledCode(
18431857 generated_code.reserve (results.size ());
18441858
18451859 // Now copy the generated code into the code space and relocate it.
1860+ CODE_SPACE_WRITE_SCOPE
18461861 for (auto & result : results) {
18471862 DCHECK_EQ (result.code_desc .buffer , result.instr_buffer .get ());
18481863 size_t code_size = RoundUp<kCodeAlignment >(result.code_desc .instr_size );
0 commit comments