What is program break? Where does it start from,0x00?
Asked Answered
H

5

9
int brk(void *end_data_segment);
void *sbrk(intptr_t increment);

Calling sbrk() with an increment of 0 can be used to find the current location of the program break.

What is program break? Where does it start from,0x00?

Hopfinger answered 14/6, 2011 at 1:27 Comment(0)
S
5

A program break is end of the process's data segment. AKA...

the program break is the first location after the end of the uninitialized data segment

As to where it starts from, it's system dependent but probably not 0x00.

Siege answered 14/6, 2011 at 1:33 Comment(2)
@cpuer: where ever the OS decides.Siege
we should use unified terms to avoid misunderstanding. data segment is for initialized global and static variable. program break is the end of heap segment.Kinson
B
10

Oversimplifying:

A process has several segments of memory:

  • Code (text) segment, which contains the code to be executed.
  • Data segment, which contains data the compiler knows about (globals and statics).
  • Stack segment, which contains (drumroll...) the stack.

(Of course, nowadays it's much more complex. There is a rodata segment, a uninitialized data segment, mappings allocated via mmap, a vdso, ...)

One traditional way a program can request more memory in a Unix-like OS is to increment the size of the data segment, and use a memory allocator (i.e. malloc() implementation) to manage the resulting space. This is done via the brk() system call, which changes the point where the data segment "breaks"/ends.

Biysk answered 14/6, 2011 at 22:54 Comment(0)
S
5

A program break is end of the process's data segment. AKA...

the program break is the first location after the end of the uninitialized data segment

As to where it starts from, it's system dependent but probably not 0x00.

Siege answered 14/6, 2011 at 1:33 Comment(2)
@cpuer: where ever the OS decides.Siege
we should use unified terms to avoid misunderstanding. data segment is for initialized global and static variable. program break is the end of heap segment.Kinson
S
3

These days, sbrk(2) (and brk) are nearly obsolete system calls (and you can nearly forget about them and ignore the old notion of break; focus on understanding mmap(2)). Notice that the sbrk(2) man page says in its NOTES :

Avoid using brk() and sbrk(): the malloc(3) memory allocation package is the portable and comfortable way of allocating memory.

(emphasis mine)


Most implementations of malloc(3) (notably the one in musl-libc) are rather using mmap(2) to require memory - and increase their virtual address space - from the kernel (look at that virtual address space wikipage, it has a nice picture). Some malloc-s use sbrk for small allocations, mmap for large ones.

Use strace(1) to find out the system calls (listed in syscalls(2)) done by some given process or command. BTW you'll then find that bash and ls (and probably many other programs) don't make a single call to sbrk.

Explore the virtual address space of some process by using proc(5). Try cat /proc/$$/maps and cat /proc/self/maps and even cat /proc/$$/smaps and read a bit to understand the output.

Be aware of ASLR & vdso(7).

And sbrk is not very thread friendly.

(my answer focuses on Linux)

Sheldon answered 9/10, 2017 at 17:53 Comment(0)
K
3

Based on the following widely used diagram:

enter image description here

program break, which is also known as brk in many articles, points to the address of heap segment's end.

When you call malloc, it changes the address of program break.

Kinson answered 16/9, 2022 at 9:12 Comment(0)
T
1

You are saying that sbrk() is an obsolute system call and that we should use malloc(), but malloc(), according to her documentation, when allocating less memory than 128 KiB (32 pages) uses it. So we shouldn´t use sbrk() directly, but malloc() use it, if allocation is bigger than 128 KiB then malloc() uses mmap() that allocates private pages to the userspace. Finally its a good idea to understand sbrk(), at least for understanding the "Program Break" concept.

Thirsty answered 2/11, 2017 at 20:16 Comment(2)
This is completely system and implementation dependent. There are various implementations of malloc.Frederickson
Is this actually a comment to the answer by Basile Starynkevitch?Frederickson

© 2022 - 2024 — McMap. All rights reserved.