Is that possible to allocate virtual memory space between 0~4GB?

I tried to use the following code to get a virtual address within 4GB memory space

int size = 4 * 1024;
int flags = MAP_PRIVATE | MAP_ANONYMOUS | MAP_32BIT;
void* addr = mmap(NULL,
                      size,      
                      PROT_READ | PROT_WRITE,
                      flags,    
                      -1,         
                      0);       

I also tried MAP_FIXED and pass an address for the first argument of mmap. However neither of them can get what I want.

Is there a way to get a virtual memory address within 4GB on arm64 on MacOS?

Answered by DTS Engineer in 836473022

I tried to use the following code to get a virtual address within 4GB memory space

Why? What are you trying to do here?

Is there a way to get a virtual memory address within 4GB on arm64 on MacOS?

No, not that I'm aware of. By design, macOS (both x86-64 and arm64) intentionally map out the first 4GB of address space*. This is to try and ensure that accidental 32 bit pointer usage (for example, storing a 64 bit address into 32 bits) results in crashes (for accessing the 4 GB range) instead of silently corrupting memory.

*Keep in mind that the full 64 bit address space is so large (18 exabytes) that most VM system (including ours) don't actually support using the full range, so this doesn't actually "waste" any memory.

__
Kevin Elliott
DTS Engineer, CoreOS/Hardware

I tried to use the following code to get a virtual address within 4GB memory space

Why? What are you trying to do here?

Is there a way to get a virtual memory address within 4GB on arm64 on MacOS?

No, not that I'm aware of. By design, macOS (both x86-64 and arm64) intentionally map out the first 4GB of address space*. This is to try and ensure that accidental 32 bit pointer usage (for example, storing a 64 bit address into 32 bits) results in crashes (for accessing the 4 GB range) instead of silently corrupting memory.

*Keep in mind that the full 64 bit address space is so large (18 exabytes) that most VM system (including ours) don't actually support using the full range, so this doesn't actually "waste" any memory.

__
Kevin Elliott
DTS Engineer, CoreOS/Hardware

Is that possible to allocate virtual memory space between 0~4GB?
 
 
Q