Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to 'malloc' within first 4GB on x86_64

Platform: Mac OS X
Lang: Obj-C/C

Is it possible to somehow make 'malloc' to allocate memory within first 4GB of process address space ?

I'm emulating i386 stack and need to guarantee that address will lie within allowed 32bit range.

Using mmap+MAP_FIXED requires to RESERVE memory before any 'malloc', it's not quite convenient. 'malloc' with constraints would be much more handy.

like image 808
Alexander Smirnov Avatar asked Feb 22 '15 18:02

Alexander Smirnov


1 Answers

It is not possible, unless you code your own implementation of malloc (or dive into the implementation details of some existing malloc then change it to suit your needs).

Most malloc-s implementations are using the system mmap (or sbrk) syscalls (see e.g. syscalls(2) on Linux, and memory(3) for MacOSX), and these are giving some arbitrary memory addresses (e.g. because of ASLR, which is very useful).

PS. On Linux, you might use mmap(2) with MAP_NORESERVE or MAP_32BIT, but MacOSX mmap(2) does not seem to have them.

like image 111
Basile Starynkevitch Avatar answered Oct 31 '22 16:10

Basile Starynkevitch