CSE Roadmap

Core tier · about 3 hours · 7 parts

Part 7 of 7 · 3 min read

Exercises

Easy

Easy 1: Place the variable (20 minutes)

For int a = 5; at file level, int b; at file level, a local int c; in main, and malloc(8) stored through a pointer, name the memory part each lives in.

Done when: four answers: data, BSS, stack, heap.

Hint

Static storage is data or BSS; automatic variables are on the stack.

Easy 2: Frame arithmetic (20 minutes)

Frames of a recursive function take 64 bytes each and the stack limit is 8 MB (8,388,608 bytes). How many nested calls fit?

Done when: you write 131,072.

Hint

8,388,608 divided by 64.

Easy 3: Read the permissions (20 minutes)

Say what r--p, r-xp and rw-p allow, and which part each usually belongs to.

Done when: three lines, matching read-only data, text, and data or heap or stack.

Hint

p means private; look at the first three letters.

Medium

Medium 1: Print your own layout (30 minutes)

Compile the six-line address program, run it twice, and compare. Say which addresses changed between runs and which did not. Try again without -no-pie.

Done when: two runs side by side, with a sentence on which addresses moved and why.

Hint

Address-space randomisation moves the heap and stack.

Medium 2: Measure a frame (30 minutes)

Write a recursive function with one extra local array of 100 ints. Print the address of a local at depths 0 to 3 and compute the frame size.

Done when: a measured size close to 400 bytes plus overhead, with the arithmetic shown.

Hint

A frame is the difference of two neighbouring addresses.

Medium 3: Size of a program (30 minutes)

Compile a program with a global int big[100000]; and no starting value. Run size and ls -l. Then give big starting values of 1 using a loop at run time, not in the declaration.

Done when: the BSS figure is about 400,000 bytes, and the file on disk is small, with a sentence on why.

Hint

Zero-filled globals are not stored in the file.

Hard

Hard 1: Find the stack limit (60 minutes)

Write a program that recurses until it crashes, printing the depth every 1,000 calls. Use the last printed depth with the frame size to estimate the stack size, and compare with ulimit -s.

Done when: an estimate within 10 per cent of the limit and a note on the gap.

Hint

setrlimit can lower the limit to make a quick test.

Hard 2: Walk the map (60 minutes)

Write a program that reads /proc/self/maps and prints, for each region, its size in bytes and pages. Call malloc(10 * 1024 * 1024) before and after, and report which line appears.

Done when: a table of regions with pages, and an explanation of the new region.

Hint

A large malloc is often served by a new anonymous mapping.

Hard 3: Prove the separation (60 minutes)

Write a program with a global int x = 1. Print its address and value, then wait for Enter and print again. Run two copies in two terminals, change nothing in one and wait in the other. Show that the addresses can be equal and that nothing links them.

Done when: two terminal outputs with the same address, and a note on what the kernel does to keep the values apart.

Hint

Use scanf or getchar to wait.