Virtual addresses and regions
Programs use virtual addresses. The kernel and the processor translate those addresses into physical memory pages, and the translation is set per process. The address space is not one block. It is a list of regions, each with a start, an end, and a set of rights. A region might hold the program code, a writable data segment, the heap that malloc grows, the stack for function calls, or a file that was mapped with mmap.
The kernel calls each region a virtual memory area. It stores them in a sorted structure and checks every memory access against them. An access outside every region produces a segmentation fault, and an access that breaks the rights of its region, such as a write to read-only code, does the same.
Reading the map
The file /proc/PID/maps prints one line per region. A typical line has six fields: the address range, the permissions, the offset into the backing file, the device, the inode number, and the pathname. The permissions are four characters: read, write, execute, and either p for private or s for shared. Regions with no pathname are anonymous memory, and special labels such as [heap], [stack], and [vdso] name the others.
cat /proc/self/maps > maps.txt; head -n 5 maps.txtThe first lines are usually the program code, which has permissions like r-xp. It is readable and executable, but not writable, which protects code from being changed while it runs. Later lines show data and the loader.
Shared libraries are just mappings
A dynamically linked program does not contain the code of the C library. At startup, the dynamic loader maps each shared library file into the address space, and the regions show the library paths in the last field. The library code is mapped with execute permission, and its data is private to the process, which is why a library file can be shared by many processes without copying the code into memory for each one.
On the lab machine the C library is musl, the one that Alpine Linux uses, so the loader and library names contain musl. Filter for the suffix .so to see them.
awk '{print $6}' maps.txt | grep / | sort -u | grep '[.]so'The awk command prints the sixth field, grep keeps lines that contain a slash, sort removes duplicates, and the final grep keeps the shared objects. The list is short because each library appears once even if it has several regions.
The stack and the heap
The stack is marked with [stack]. It grows downward and holds return addresses and local variables for each function call. The heap is marked [heap] on many programs, and it grows upward as malloc asks for more memory with brk or mmap. Large allocations often get their own anonymous mapping instead of extending the heap.
Reading the lines for these regions tells you whether memory is growing in the heap or in separate mappings, which matters when a program slowly uses more memory over time.
The pathnames in maps are the paths the file had when it was mapped. A file that was deleted after mapping shows "(deleted)" at the end of its line, and its memory stays in use until the process exits.
Counting and comparing
Counting executable regions with awk is a quick way to see how much code a program maps. Comparing two maps from different moments shows growth: a region whose range gets larger is a region that was extended. The format is stable enough that small awk scripts work across kernels, which is the reason the lab asks for these fields.
For a summary of memory use per process, the VmRSS and VmSize lines of the status file are easier to read than a full map.
Address space layout randomization
Each time a program starts, the kernel can place the stack, the heap, and the libraries at random addresses. This randomization makes it harder for an attacker to predict where code lives. The addresses in the map therefore differ from run to run, which is why the lab checks pathnames and permissions rather than addresses. A check that compared addresses would pass once and fail the next time.
Randomization does not change permissions or labels. A region named stack is a stack on every run, and a line with r-xp is code on every run. When you compare maps across runs, compare the pathnames and the permissions, and ignore the first field.
When a region grows
The heap grows when malloc needs more room, and the kernel grants it either by moving the program break or by creating a new mapping. A program that allocates many small pieces extends its heap region, while a single large allocation usually gets a region of its own. Taking two maps during a run shows which of these happened, and the line whose range got larger tells you where the memory went.
Reading the permissions column
The permission field is the fastest way to classify a region. A region with r-x and p is code that the loader mapped from a file, and it should never be writable. A region with rw and p is private data, such as the data segment or the heap, and it can change but is not visible to other processes. A region with s is shared, which means that writes are visible to any process that maps the same object, and shared memory is how some programs pass large data without copying it. A region with no permission bits at all, written as ---p, is reserved space that is not yet usable, which is common for guard pages that catch overflows.
Reading the field from top to bottom gives a quick picture of a program. Code comes first, then data, then the heap and the libraries, and the stack sits near the top of the user space. Most programs follow that order, so a region that breaks it is worth a second look.