I want to make sure the return address of sbrk
is within a certain specific range. I read somewhere that sbrk
allocates from an area allocated at program initialization. So I'm wondering if there's anyway I can enforce the program initialization to allocate from a specific address? For example, with mmap
, I'll be able to do so with MAP_FIXED_NOREPLACE
. Is it possible to have something similar?

- 193
- 6
-
sbrk is considered as legacy even by 1997 standards, https://en.wikipedia.org/wiki/Sbrk are you sure you still want to use it? – Antonin GAVREL Mar 10 '21 at 03:46
1 Answers
No, this is not possible. brk
and sbrk
refer to the data segment of the program, and that can be loaded at any valid address that meets the needs of the dynamic linker. Different architectures can and do use different addresses, and even machines of the same architecture can use different ranges depending on the configuration of the kernel. Using a fixed address or address range is extremely nonportable and will make your program very brittle to future changes. I fully expect that doing this will cause your program to break in the future simply by upgrading libc.
In addition, modern programs are typically compiled as position-independent executables so that ASLR can be used to improve security. Therefore, even if you knew the address range that was used for one invocation of your program, the very next invocation of your program might use a totally different address range.
In addition, you almost never want to invoke brk
or sbrk
by hand. In almost all cases, you will want to use the system memory allocator (or a replacement like jemalloc), which will handle this case for you. For example, glibc's malloc implementation, like most others, will allocate large chunks of memory using mmap
, which can significantly reduce memory usage in long-running programs, since these large chunks can be freed independently. The memory allocator also may not appreciate you changing the size of the data segment without consulting it.
Finally, in case you care about portability to other Unix systems, not all systems even have brk
and sbrk
. OpenBSD allocates all memory using mmap
which improves security by expanding the use of ASLR (at the cost of performance).
If you absolutely must use a fixed address or address range and there is no alternative, you'll need to use mmap
to allocate that range of memory.

- 64,793
- 6
- 84
- 100