Part 7 of 7 · 4 min read
Exercises
Easy
Easy 1: How many frames (20 minutes)
A function uses a 128-byte frame. How many nested calls fit in an 8 MiB stack?
Done when: 8,388,608 / 128 = 65,536 calls, with the note that start-up data makes the real number a little smaller.
Hint
8 MiB is 8,388,608 bytes.
Easy 2: Where does it live (20 minutes)
Say whether each lives on the stack, on the heap, or in the data segment: a local int; the memory from malloc(100); a global array; a static local variable; the return address of a call.
Done when: stack; heap; data segment; data segment; stack.
Hint
A static local keeps its value between calls, so it cannot be on the stack.
Easy 3: Change the limit (20 minutes)
A recursion reaches a depth of 58,050 with an 8 MiB limit. Estimate the depth with a 2 MiB limit.
Done when: about 14,500, since the depth is in proportion to the limit.
Hint
Divide 58,050 by 4.
Medium
Medium 1: Measure the frame (45 minutes)
Write the recursion test of section 3 with a signal handler on an alternate stack. Run it with ulimit -s set to 1024, 8192 and 16384 and compute the bytes per call for each.
Done when: three depths close to 7,086, 58,050 and 116,310 on the test machine, and bytes per call between 144 and 148.
Hint
Use sigaltstack and SA_ONSTACK, and a volatile global for the depth.
Medium 2: Watch the stack grow (45 minutes)
Write a program that prints the size of the [stack] line of /proc/self/maps before and after a recursion with a frame of about 1,000 bytes and a depth of 3,000.
Done when: a size of about 148 kB before and about 3,076 kB after, and one sentence on how the growth was done.
Hint
Read the line that contains [stack], and subtract the two addresses.
Medium 3: Remove the recursion (60 minutes)
Write a recursive function that adds the integers from 1 to n. Show that it crashes for n = 10 million with the default stack. Rewrite it as a loop.
Done when: the recursive version, built with -O0, ends with SIGSEGV, the loop gives 50,000,005,000,000 for n = 10 million, and a note that the same recursion built with -O2 does not crash here, because the compiler turned it into a loop.
Hint
The sum is n × (n + 1) / 2. Print the loop result first and flush it, since a crash loses buffered output.
Hard
Hard 1: Thread stack depth (90 minutes)
Create a thread with a 64 KiB stack using pthread_attr_setstacksize, and measure the recursion depth it reaches before SIGSEGV, using the same frame as Medium 1. Compare it with the depth of 64 KiB divided by the frame size.
Done when: a depth near 423 on the test machine, about 7% below 64 KiB / 144 (about 455), and a note that the library keeps part of the stack for its own data.
Hint
Compile with -pthread, and give the thread its own alternate stack for the handler.
Hard 2: The alternate stack (90 minutes)
Remove the alternate stack and SA_ONSTACK from the test of section 3, and run it again. Explain what you see.
Done when: the handler's message does not appear and the process just dies with SIGSEGV, and a sentence that says the handler needs a stack to run on, and the normal stack is the one that is used up.
Hint
Compare the output with and without the message in the write call.
Hard 3: Canary on and off (90 minutes)
Build the program of section 5 with and without the stack protector. Run each with arguments of 4, 8, 16 and 32 characters. Report the exit codes and messages.
Done when: a table in which the protected build exits with 134 from 9 characters on, and the unprotected build runs silently up to 12 characters and exits with 139 from 16, on the test machine.
Hint
A buffer of 8 bytes holds 7 characters and the end mark. The compiler may round the buffer up, so the first failure can come later than 8.