Operating Systems 2019F: Tutorial 3: Difference between revisions
No edit summary |
|||
(2 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
' | In this tutorial we're going to look at how processes work at a low level: how they make system calls & library calls, how C and assembly compare, and and how memory is laid out. | ||
If you haven't already, please set up and start using a VM on openstack. | |||
==Tasks== | ==Tasks== | ||
===Function calls, library calls, and system calls=== | ===Function calls, library calls, and system calls=== | ||
''Note that this section mostly overlaps with question 12 from Tutorial 1. If you worked hard on it, you can skip this section.'' | |||
For [http://homeostasis.scs.carleton.ca/~soma/os-2017f/code/tut1/hello.c hello.c] and [http://homeostasis.scs.carleton.ca/~soma/os-2017f/code/tut1/syscall-hello.c syscall-hello.c] do the following (substituting the appropriate source file for prog.c). To download programs to your VM, use the wget command, e.g. | For [http://homeostasis.scs.carleton.ca/~soma/os-2017f/code/tut1/hello.c hello.c] and [http://homeostasis.scs.carleton.ca/~soma/os-2017f/code/tut1/syscall-hello.c syscall-hello.c] do the following (substituting the appropriate source file for prog.c). To download programs to your VM, use the wget command, e.g. | ||
Line 22: | Line 24: | ||
Do the following with hello.c and syscall-hello.c, as before. | Do the following with hello.c and syscall-hello.c, as before. | ||
A few tips on reading assembly code. See [https://en.wikibooks.org/wiki/X86_Assembly/GAS_Syntax AT&T/GNU Assembler syntax] for more information. | A few tips on reading assembly code. See [https://en.wikibooks.org/wiki/X86_Assembly/GAS_Syntax AT&T/GNU Assembler syntax] for more information on syntax and [https://en.wikipedia.org/wiki/X86_calling_conventions#System_V_AMD64_ABI the Wikipedia article on calling conventions] for how functions are called. | ||
* The last letter of many instructions refers to the size of the operand. For example, callq means call a function using a "quad" value (64 bits). | * The last letter of many instructions refers to the size of the operand. For example, callq means call a function using a "quad" value (64 bits). | ||
* A dollar sign preceding a value means that it is a literal value, a percent sign means it is a register. | * A dollar sign preceding a value means that it is a literal value, a percent sign means it is a register. |
Latest revision as of 21:17, 3 October 2019
In this tutorial we're going to look at how processes work at a low level: how they make system calls & library calls, how C and assembly compare, and and how memory is laid out.
If you haven't already, please set up and start using a VM on openstack.
Tasks
Function calls, library calls, and system calls
Note that this section mostly overlaps with question 12 from Tutorial 1. If you worked hard on it, you can skip this section.
For hello.c and syscall-hello.c do the following (substituting the appropriate source file for prog.c). To download programs to your VM, use the wget command, e.g.
wget https://homeostasis.scs.carleton.ca/~soma/os-2017f/code/tut1/hello.c
- Compile the program prog.c using gcc -O2 prog.c -o prog-dyn and run prog-dyn. What does it do?
- Statically compile and optimize prog.c by running gcc -O2 -static prog.c -o prog-static. How does the size compare with prog?
- See what system calls prog-static produces by running strace -o syscalls-static.log ./prog-static. Do the same for prog-dyn. Which version generates more system calls? Note: system calls are saved in the log file syscalls-static.log. Feel free to save them in a different file.
- See what library calls prog-static produces by running ltrace -o library-static.log ./prog-static. Do the same for prog-dyn. Which version generates more library calls? (If ltrace isn't installed, run sudo apt-get install ltrace)
- Use the command ls -l to see the metadata associated with prog.c and prog-dyn, and prog-static. Who owns these files? What group are they in? Do you notice any pattern with the permissions (rwx) associated with each file?
- (optional) Look up the documentation for each of the system calls made by the static versions of the programs. You may need to append a 2 or 3 to the manpage invocation, e.g. "man 2 write" gets you the write system call documentation.
Comparing C and assembly
Do the following with hello.c and syscall-hello.c, as before.
A few tips on reading assembly code. See AT&T/GNU Assembler syntax for more information on syntax and the Wikipedia article on calling conventions for how functions are called.
- The last letter of many instructions refers to the size of the operand. For example, callq means call a function using a "quad" value (64 bits).
- A dollar sign preceding a value means that it is a literal value, a percent sign means it is a register.
- If a register is in parentheses, then it is being used as a "pointer" (it contains an address, so the CPU goes to that address and interacts with the memory there). If there is a number before the parentheses, it is an offset to the register's value.
- Using the nm command, see what symbols are defined in prog-static and prog-dyn. Which defines more symbols?
- Run the command gcc -c -O2 prog.c to produce an object file. What file was produced? What symbols does it define?
- Look at the assembly code of the program by running gcc -S -O2 prog.c. What file was produced? Identify the following in the assembly code (if present):
- A function call (call)
- A return from a function (ret)
- Registers being saved onto the stack (push)
- Registers being retrieved from the stack (pop)
- Subtraction (sub)
- A system call (syscall)
- Disassemble the object file using objdump -d. How does this disassembly compare with the output from gcc -S?
- Examine the headers of object file, dynamically linked executable, and the statically linked executable using objdump -h
- Examine the contents of object file, dynamically linked executable, and the statically linked executable using objdump -s
- Re-run all of the previous gcc commands adding the "-v" flag. What is all of that output?
Examining the runtime memory map
Compile and run 3000memview.c, then consider the following questions.
- Why are the addresses inconsistent between runs?
- Roughly where does the stack seem to be? The heap? Code? Global variables?
- Observe how the heap grows (i.e. the value of sbrk changes) in response to malloc calls. Would you expect the heap to ever run into the stack? Why or why not?
- Change each malloc call to allocate more than 128K. What happens to the values of sbrk? Why? (Hint: use strace)
- Add more code and data to the program, and add more printf's to see where things are. Are things where you expect them to be?
Code
hello.c
#include <stdio.h>
int main(int argc, char *argv[]) {
printf("Hello world!\n");
return 0;
}
syscall-hello.c
#include <unistd.h>
#include <sys/syscall.h>
char *buf = "Hello world!\n";
int main(int argc, char *argv) {
size_t result;
/* "man 2 write" to see arguments to write syscall */
result = syscall(SYS_write, 1, buf, 13);
return (int) result;
}
3000memview.c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
char *gmsg = "Global Message";
const int buffer_size = 100;
int main(int argc, char *argv[], char *envp[])
{
char *lmsg = "Local Message";
char *buf[buffer_size];
int i;
printf("Memory report\n");
printf("argv: %lx\n", (unsigned long) argv);
printf("argv[0]: %lx\n", (unsigned long) argv[0]);
printf("envp: %lx\n", (unsigned long) envp);
printf("envp[0]: %lx\n", (unsigned long) envp[0]);
printf("lmsg: %lx\n", (unsigned long) lmsg);
printf("&lmsg: %lx\n", (unsigned long) &lmsg);
printf("gmsg: %lx\n", (unsigned long) gmsg);
printf("&gmsg: %lx\n", (unsigned long) &gmsg);
printf("main: %lx\n", (unsigned long) &main);
printf("sbrk(0): %lx\n", (unsigned long) sbrk(0));
printf("&buf: %lx\n", (unsigned long) &buf);
for (i = 0; i<buffer_size; i++) {
buf[i] = (char *) malloc(4096);
}
printf("buf[0]: %lx\n", (unsigned long) buf[0]);
printf("sbrk(0): %lx\n", (unsigned long) sbrk(0));
return 0;
}