Studying a Few Tiny Programs
1 Overview
- The dir ../StudyPrograms has several tiny files of C src code. 
ls -lg 
-rw-r--r-- 1 pmateti 494 Aug 29 19:52 arrays.c -rw-r--r-- 1 pmateti 228 Aug 29 19:52 example1.c -rw-r--r-- 1 pmateti 270 Aug 29 19:52 example2.c -rw-r--r-- 1 pmateti 159 Aug 29 19:52 execsh.c -rw-r--r-- 1 pmateti 59 Aug 29 19:52 exit.c -rw-r--r-- 1 pmateti 17775 Aug 29 20:42 index.html -rw-r--r-- 1 pmateti 5137 Aug 29 20:42 index.org -rw-r--r-- 1 pmateti 403 Aug 29 19:52 Makefile -rw-r--r-- 1 pmateti 450 Aug 29 19:52 recurse.c -rw-r--r-- 1 pmateti 1817 Aug 29 20:08 recurse.s -rw-r--r-- 1 pmateti 16848 Aug 29 20:32 syslib -rw-r--r-- 1 pmateti 415 Aug 29 19:52 syslib.c -rw-r--r-- 1 pmateti 387 Aug 29 19:52 testsc.c -rw-r--r-- 1 pmateti 776 Aug 29 19:52 yasc.c
- Study the source to see what syscalls and library calls they make, and how the programs are to be built, and the numerical relationships between the addresses of variables.
 - What is the max amount of virtual memory can a Linux process have?
 - All these programs are expected to be invoked as non-root.
 
2 ./Makefile
makewill compile and link one program with no error messages. It is using theMakefileas-is, andCFLAGS = -g -Wall -ansi -pedanticExplain what these flags causegccto do. Expand theMakefileto become complete, so that all included programs in the directory are built properly.
3 ./example1.c
- Understand how arguments are passed to parameters.
 - E.g., how does 
function(1, 2, 3);in main pass arguments to procedure namedfunction? 
4 ./example2.c
- Lookup the man page of 
strcpy. - In C, a string is an array of chars ending in '\0' (ASCII NUL).
 - Spot sloppy programming.
 
5 ./execsh.c
- Learn to use 
execve nameis an array of pointers to strings.- What is volume or section 2 of man pages?
 
6 ./exit.c
- How many system calls does this tiny program have?
 - Tradition is that Unix/Linux processes return, as they terminate, (i) a zero on "success", (ii) a negative integer on error(s).
 
7 ./recurse.c
gccgenerates code for functions such that the return value is in the "accumulator" register A (RAX/ EAX/ AX/ …).- Try 
gcc -s recurse.c. Figure out what it does. - What are "segment registers"? What is a "Segmentation Fault"?
 get_sp()is written in Gnu Assembly. It copies the stack pointer register (SP ) to the A register.- On my Linux PC, invoking it as in 
; ./recurseprints 
x 0 sp fea65670 x 1 sp fe965650 x 2 sp fe865630 x 3 sp fe765610 x 4 sp fe6655f0 x 5 sp fe5655d0 x 6 sp fe4655b0 Segmentation fault (core dumped)
before dying.  My prompt string PS1 is ;  .
- Improve [whatever] so that it recurses more deeply. Explain what you did.
 - Must leave the array 
athe same. 
8 ./arrays.c
- Study its output.
 - Learn
sizeof- address computations.
 - the syntax of printf format string.
 
 - If we were to exchange the two lines 
char b[5]; char c[3];, predict what the output will be [without re-compiling/ running]. 
9 ./testsc.c
- This may appear tricky, but this kind of code is quite common in systems programming.
 - The bytes stored in 
scb[]are the x86 machine code manually produced by compiling the so-called "shell code", which invokes the shell through a system call. rapstands for return-address-ptr.- First, experience the code by compiling and linking the code as in,
gcc testsc.c -o testsc, and invoking it. - Invoking 
testscas produced above, as-is, is going to produce*** stack smashing detected ***: <unknown> terminated; Aborted (core dumped)" - Re-compilining as in 
gcc -fno-stack-protector -z execstack testsc.c -o testsc, and invoking will terminate "silently". What did it do? We explore this topic in a few weeks under the topic of "stack smashing." 
10 ./yasc.c
- Similar to the above.
 - Explains the bytes stored in 
sc[]in the comments. - The ptr 
fpstores the address of a void method that takes no arguments. - In 
(void *) scthe(void *)is doing free type coercion. "Free" because this is happening at compile time, not run time. The type ofsc, which ischar []is being coerced into avoid *. [Are parens necessary here? [Advice: When in doubt, parenthesize!]] 
11 ./syslib.c
- Library Calls v. SysCalls
 - Build 
syslib. Explain the output. 
12 References
- Wikibooks, https://en.wikibooks.org/wiki/X86_Assembly/X86_Architecture, 2019.