ECEC 201 · Week 4
Modern Computer: Von Neumann
A running program is a process, and a process has a map of its memory: text for the code, data and bss for globals, the heap for malloc, and the stack for locals.
Week 4 of 10 · Das Frite
ECEC 201 · Week 4 · In class
A running program is a process, and a process has a map of its memory: text for the code, data and bss for globals, the heap for malloc, and the stack for locals.
- Linux prints that map in
/proc/self/maps. - Draw the five regions in address order before you run anything.
ECEC 201 · Week 4 · In class
Print one address from each region and find it on the map from the same run.
- Run it again: some addresses move between runs, and the reason is the lecture's last point.
ECEC 201 · Week 4 · Lecture
Where each kind of object lives
| Region | Holds | Lives for |
|---|---|---|
| text | machine code of functions | the whole run, read-only |
| data | globals and statics with an initial value | the whole run |
| bss | globals and statics that start at zero | the whole run |
| heap | malloc blocks | until free |
| stack | locals and call frames | until the function returns |
ECEC 201 · Week 4 · Lecture
Reading /proc/self/maps
start-end perms offset dev inode pathname
- One line per mapping, sorted by start address.
perms:r,w,x, thenpfor private. Code isr-x; writable data isrw-.- The pathname names the file mapped in, or a label such as
[heap]or[stack]. - An address belongs to the line whose start ≤ address < end.
ECEC 201 · Week 4 · Lecture
Printing an address and the map in one run
#include <stdio.h>
int main(void)
{
char line[256];
FILE *f = fopen("/proc/self/maps", "r");
if (!f)
return 1;
printf("main at %p\n", (void *)main);
while (fgets(line, sizeof line, f))
fputs(line, stdout);
fclose(f);
return 0;
}
ECEC 201 · Week 4 · Lecture
Why addresses move between runs
- Linux uses address space layout randomisation (ASLR): each run places regions at a new random base.
- A program built as position-independent can have even its code moved.
- Inside one region, distances stay the same from run to run.
- ASLR makes a memory bug harder to exploit, because an attacker cannot guess where things are.
ECEC 201 · Week 4 · Lab
Lab
- Print the address of a function, a global, a static variable, a `malloc` block, and a local variable.
- In the same run, print `/proc/self/maps` and mark the region each address falls in. Second pass: run it twice more and note which addresses move between runs.
- Assemble the lab hand-in: The program, the five addresses marked on the memory map, and one sentence on which addresses moved between runs and why.
ECEC 201 · Week 4 · Lab
Hand in
The program, the five addresses marked on the memory map, and one sentence on which addresses moved between runs and why.
ECEC 201 · Week 4 · Homework
Homework
- From the lecture, draw the five regions of a process in address order before you run the program.
ECEC 201 · Week 4 · Homework
Turn in
The drawing with each region named.
ECEC 201 · Week 4 · Read
Read
On the lab site: Module 5, Memory bring-up, covers the DRAM. This week grades one process's map of that memory.
ECEC 201 · Week 4 · Next
Week 5
Memory you ask for with malloc is yours to give back with free.