问题
I am trying to do some tests on X86_64 Linux, and I want to know how to make malloc() allocate return addresses in the range higher than 4 GiB
Do I have to override malloc with a separate/custom library or is there any other simple way to do this?
Thanks.
----Edit----
What I am interested is in the address when taken as value (uintptr_t
)
and it does not matter whether its virtual or physical address, because, all
that I want is the address must be a value greater than 4GiB
I am using gcc
(4.2.1) with -m64
, on Linux x86_64
(hope I made the question clear)
回答1:
malloc()
is the wrong tool. You want to mmap()
/dev/zero
(which is what malloc()
is doing behind the scenes in modern glibc
), IIRC. (On Unix. I believe there is a similar API for Win32, but I couldn't tell you what it is.)
回答2:
malloc() doesn't give you any way of asking for addresses in a certain range. As described in some of the other answers, if this is just a test you could just keep calling malloc() for more and more memory until you get an answer you like, but that's pretty wasteful.
Use mmap(). If you call like:
mmap(ADDRESS_ABOVE_4GB, size, PROT_READ|PROT_WRITE, MAP_ANONYMOUS, -1, 0)
it'll give you what you want, with pretty much the same semantics as malloc().
If you're trying to cause this to happen for calls to malloc() in code you don't control/have source to, like library code, yeah, you'll have to provide your own implementation of malloc() that does the above.
回答3:
So long as you compile with e.g. gcc -m64 ...
(may be the default anyway) and make sure you include the relevant headers (i.e. <stdlib.h>
in the case of malloc
) then everything should just work. Pointers will be 64 bits, size_t
will be 64 bits, and you can malloc
as much memory as you like (well up to 2^64-1 bytes anyway, which should be enough for anyone...).
回答4:
There isn't a standard way to force malloc()
to allocate addresses in a particular range.
However, you can allocate 4 GiB of memory. If it is allocated so that enough of the space is in the high address range, use that space. Otherwise, allocate some more space; it should be allocated with a start address in the high range, but you should check (because a small allocation might still squeeze into space left in the low address range), and keep allocating until the allocated space is in high address range.
回答5:
malloc returns back an address that corresponds to an operating system kernel call which will find the "next" available block of memory that fits the size_t requirement.
If you want to ensure that the address is in a particular range, a custom version of malloc won't do the trick, as the actual memory assignment is a relay from the kernel's allocation of memory pages to the process.
That basically means the only other technique left is to keep malloc'ing until your exhaust the below 4GiB memory, or perhaps get lucky and get an address in the range you want. Note that to even get in that range, both your program and the operating system must be compiled to support more that 32 bit addresses, and that implies 64 bit architectures.
来源:https://stackoverflow.com/questions/5343722/how-to-malloc-in-address-range-4-gib