QResource: add support for duplicating mapped memory on macOS

Similar to the Linux solution, this avoids allocating heap memory in the
implementation of QFile::map() calls with MapPrivateOption.

Unlike the Linux solution, the macOS one cannot duplicate resources
present in read-only sections of binaries, only those loaded from files
with mmap() (QResource::registerResource()), because the max_prot
setting on the source does not have the VM_PROT_WRITE bit set and I
could not find a way to reset it to force a copy-on-write.

Change-Id: I6979d02a7395405cbf23fffd17c951949c71ec20
Reviewed-by: Oswald Buddenhagen <oswald.buddenhagen@gmx.de>
Reviewed-by: Tor Arne Vestbø <tor.arne.vestbo@qt.io>
bb10
Thiago Macieira 2024-04-24 13:33:51 -07:00
parent 1b64b7c672
commit 4a984c15dd
1 changed files with 22 additions and 0 deletions

View File

@ -39,6 +39,9 @@
# ifdef Q_OS_LINUX
// since 5.7, so define in case we're being compiled with older kernel headers
# define MREMAP_DONTUNMAP 4
# elif defined(Q_OS_DARWIN)
# include <mach/mach.h>
# include <mach/vm_map.h>
# endif
#endif
#ifdef Q_OS_WIN
@ -1741,6 +1744,25 @@ bool QResourceFileEnginePrivate::mapUncompressed_sys()
munmap(ptr, r.size);
return false;
}
#elif defined(Q_OS_DARWIN)
mach_port_t self = mach_task_self();
vm_address_t addr = 0;
vm_address_t mask = 0;
bool anywhere = true;
bool copy = true;
vm_prot_t cur_prot = VM_PROT_READ | VM_PROT_WRITE;
vm_prot_t max_prot = VM_PROT_ALL;
kern_return_t res = vm_remap(self, &addr, r.size, mask, anywhere,
self, vm_address_t(r.begin), copy, &cur_prot,
&max_prot, VM_INHERIT_DEFAULT);
if (res != KERN_SUCCESS)
return false;
ptr = reinterpret_cast<void *>(addr);
if ((max_prot & VM_PROT_WRITE) == 0 || mprotect(ptr, r.size, PROT_READ | PROT_WRITE) != 0) {
munmap(ptr, r.size);
return false;
}
#endif
if (!ptr)