Skip to content

Commit 153af86

Browse files
committed
Use mach_vm_remap instead of mach_vm_map to set up permanent mapping for g_config
https://bugs.webkit.org/show_bug.cgi?id=290066 rdar://147347736 Reviewed by Mark Lam. Uses mach_vm_remap to set up permanent mapping for g_config, preventing a possible issue where g_config values that were set up prior to WTF initialization would be zeroed out. * Source/WTF/wtf/WTFConfig.cpp: (WTF::setPermissionsOfConfigPage): (WTF::Config::initialize): Canonical link: https://commits.webkit.org/292440@main
1 parent 11716e6 commit 153af86

3 files changed

Lines changed: 8 additions & 26 deletions

File tree

‎Source/JavaScriptCore/jit/ExecutableAllocator.cpp‎

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -65,29 +65,7 @@ WTF_ALLOW_UNSAFE_BUFFER_USAGE_END
6565
#include <fcntl.h>
6666
#include <mach/mach.h>
6767
#include <mach/mach_time.h>
68-
69-
extern "C" {
70-
/* Routine mach_vm_remap */
71-
#ifdef mig_external
72-
mig_external
73-
#else
74-
extern
75-
#endif /* mig_external */
76-
kern_return_t mach_vm_remap
77-
(
78-
vm_map_t target_task,
79-
mach_vm_address_t *target_address,
80-
mach_vm_size_t size,
81-
mach_vm_offset_t mask,
82-
int flags,
83-
vm_map_t src_task,
84-
mach_vm_address_t src_address,
85-
boolean_t copy,
86-
vm_prot_t *cur_protection,
87-
vm_prot_t *max_protection,
88-
vm_inherit_t inheritance
89-
);
90-
}
68+
#include <wtf/spi/cocoa/MachVMSPI.h>
9169
#endif
9270

9371
#if USE(INLINE_JIT_PERMISSIONS_API)

‎Source/WTF/wtf/WTFConfig.cpp‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,13 @@ void setPermissionsOfConfigPage()
7373
std::call_once(onceFlag, [] {
7474
mach_vm_address_t addr = std::bit_cast<uintptr_t>(static_cast<void*>(WebConfig::g_config));
7575
auto flags = VM_FLAGS_FIXED | VM_FLAGS_OVERWRITE | VM_FLAGS_PERMANENT;
76+
auto prot = VM_PROT_READ | VM_PROT_WRITE;
7677

7778
auto attemptVMMapping = [&] {
78-
return mach_vm_map(mach_task_self(), &addr, ConfigSizeToProtect, pageSize() - 1, flags, MEMORY_OBJECT_NULL, 0, false, VM_PROT_READ | VM_PROT_WRITE, VM_PROT_READ | VM_PROT_WRITE, VM_INHERIT_DEFAULT);
79+
// We use remap instead of map here to avoid zeroing the config,
80+
// since WebKit components like bmalloc can potentially use the
81+
// config prior to WTF initialization.
82+
return mach_vm_remap(mach_task_self(), &addr, ConfigSizeToProtect, pageSize() - 1, flags, mach_task_self(), addr, false, &prot, &prot, VM_INHERIT_DEFAULT);
7983
};
8084

8185
auto result = attemptVMMapping();
@@ -120,8 +124,6 @@ void Config::initialize()
120124
SignalHandlers::initialize();
121125

122126
uint8_t* reservedConfigBytes = reinterpret_cast_ptr<uint8_t*>(WebConfig::g_config + WebConfig::reservedSlotsForExecutableAllocator);
123-
reservedConfigBytes[WebConfig::ReservedByteForAllocationProfiling] = 0;
124-
reservedConfigBytes[WebConfig::ReservedByteForAllocationProfilingMode] = 0;
125127

126128
#if USE(APPLE_INTERNAL_SDK)
127129
WTF_INITIALIZE_ADDITIONAL_CONFIG();

‎Source/WTF/wtf/spi/cocoa/MachVMSPI.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ kern_return_t mach_vm_allocate(vm_map_t target, mach_vm_address_t*, mach_vm_size
3939
kern_return_t mach_vm_deallocate(vm_map_t target, mach_vm_address_t, mach_vm_size_t);
4040
kern_return_t mach_vm_map(vm_map_t targetTask, mach_vm_address_t*, mach_vm_size_t, mach_vm_offset_t mask, int flags,
4141
mem_entry_name_port_t, memory_object_offset_t, boolean_t copy, vm_prot_t currentProtection, vm_prot_t maximumProtection, vm_inherit_t);
42+
kern_return_t mach_vm_remap(vm_map_t targetTask, mach_vm_address_t*, mach_vm_size_t, mach_vm_offset_t mask, int flags,
43+
vm_map_t srcTask, mach_vm_address_t srcAddress, boolean_t copy, vm_prot_t* currentProtection, vm_prot_t* maximumProtection, vm_inherit_t);
4244
kern_return_t mach_vm_protect(vm_map_t targetTask, mach_vm_address_t, mach_vm_size_t, boolean_t setMaximum, vm_prot_t newProtection);
4345
kern_return_t mach_vm_region(vm_map_t targetTask, mach_vm_address_t*, mach_vm_size_t*, vm_region_flavor_t, vm_region_info_t,
4446
mach_msg_type_number_t* infoCount, mach_port_t* objectName);

0 commit comments

Comments
 (0)