BRK(2)
π
BRK(2) β bestie we need to talk about brk, sbrk π
okay so this is page BRK(2) from the System Calls Manual (yes, section 2, we're in syscall territory, very serious, very grown-up) π
β¨ NAME β¨
brk, sbrk β change data segment size
π LIBRARY π
Standard C Library (libc, -lc)
πΈ SYNOPSIS πΈ
#include <unistd.h>
int
brk(const void *addr);
void *
sbrk(intptr_t incr);
π¦ DESCRIPTION π¦
so. brk() and sbrk() are legacy interfaces β like, from before modern virtual memory management even existed. they're deprecated and they're not present on the arm64 or riscv architectures. if you wanna allocate pages, bestie, the move is mmap(2). use that. periodt.
what do they actually do tho? they change how much memory is allocated in a process's data segment, by moving the location of the βbreakβ. and the break is the first address after the end of the process's uninitialized data segment (also known as the βBSSβ). iykyk.
brk()β sets the break to addr.sbrk()β raises the break by incr bytes, so you're allocating at least incr bytes of new memory in the data segment. and if incr is negative? the break gets lowered by incr bytes. we love a bidirectional king π
π NOTES π
ngl the kernel is out here growing and shrinking the actual process data segment size only in page sizes, but these functions will let you set the break to unaligned values β meaning it can point to any address inside the last page of the data segment. quirky of them fr fr.
wanna know the current value of the program break? just call sbrk(0). slay. also check out end(3) bestie.
oh and getrlimit(2) is the syscall you want for figuring out the maximum permissible size of the data segment. you will not be able to set the break beyond etext + rlim.rlim_max, where that rlim.rlim_max value comes back from a call to getrlimit(RLIMIT_DATA, &rlim). (for the definition of etext, go see end(3).)
π«Ά RETURN VALUES π«Ά
brk()returns the value0if it slayed; otherwise the value-1comes back and the global variableerrnois set to indicate the error. πsbrk()returns the prior break value if it worked; otherwise the value(void *)-1is returned and the global variableerrnois set to indicate the error. not the vibe.
π ERRORS π
the brk() and sbrk() functions will fail if:
[EINVAL]β the requested break value was beyond the beginning of the data segment. girl. π[ENOMEM]β the data segment size limit, as set bysetrlimit(2), was exceeded.[ENOMEM]β there wasn't enough space in the swap area to support the expansion of the data segment.
π SEE ALSO π
execve(2), getrlimit(2), mmap(2), end(3), free(3), malloc(3)
π HISTORY π
brk() appeared in Version 7 AT&T UNIX β ancient. iconic. main character energy from birth. β¨
then FreeBSD 11.0 introduced the arm64 and riscv architectures, which do not support brk() or sbrk() at all. they simply said no π
π BUGS π
okay this is the part where you actually pay attention bestie, these are real:
- mixing
brk()orsbrk()withmalloc(3),free(3), or similar functions will result in non-portable program behavior. do not do this. it's giving chaos. - setting the break may fail due to a temporary lack of swap space. and you literally can't distinguish that from a failure caused by exceeding the maximum size of the data segment unless you go consult
getrlimit(2). so rude. sbrk()is sometimes used to monitor heap use by calling it with an argument of0. but the result is unlikely to reflect actual utilization when it's combined with anmmap(2)-based malloc. sad!brk()andsbrk()are not thread-safe. π¨
okay that's the whole page, love that for us π this came from Debian, dated June 2, 2018, and it's page BRK(2) β mwah, take care of yourselves, go use mmap(2) instead πβ¨π¦
π¦ where this page comes from
- debian bookworm: freebsd-manpages 12.2-1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- debian trixie: freebsd-manpages 12.2-2 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu jammy: freebsd-manpages 12.2-1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu noble: freebsd-manpages 12.2-1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu questing: freebsd-manpages 12.2-2
- ubuntu resolute: freebsd-manpages 12.2-2build1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu stonking: freebsd-manpages 12.2-2build1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- Copyright (c) 1980, 1991, 1993 The Regents of the University of California. All rights reserved
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms