Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible malloc() allocates a buffer that overlaps another buffer allocated using mmap()?

I plan to use mmap() to allocate a buffer close to a specific address.

What I'm worried about is, the buffer allocated using mmap() will overlap other buffers allocated by malloc() or new operator (C++). Is it possible?

like image 902
flyingbin Avatar asked Feb 21 '23 11:02

flyingbin


1 Answers

If you use MAP_FIXED to demand mmap create the mapping at a particular address, then yes it is possible that you overwrite an existing mapping such as space allocated by malloc, part of a shared library's code or data section, etc. Basically it's always an error to use MAP_FIXED unless you've already obtained the address range via a call to mmap without specifying MAP_FIXED (so you know it belongs to you); in this case you can intentionally overwrite parts of the mapping using MAP_FIXED).

The other answers all seemed to miss the fact that you said "close to a specific address", which to me implies MAP_FIXED. If you're not using MAP_FIXED, please elaborate on how you're obtaining a mapping "close to a specific address".

like image 186
R.. GitHub STOP HELPING ICE Avatar answered Feb 24 '23 00:02

R.. GitHub STOP HELPING ICE