Carnegie Mellon Introduction to Computer Systems 15 213 18 243 spring 2009 1st Lecture Jan 12th Instructors Gregory Kesden and Markus P schel The course that gives CMU its Zip Carnegie Mellon Overview Course theme Five realities How the course fits into the CS ECE curriculum Logistics Carnegie Mellon Course Theme Abstraction Is Good But Don t Forget Reality Most CS courses emphasize abstraction Abstract data types Asymptotic analysis These abstractions have limits Especially in the presence of bugs Need to understand details of underlying implementations Useful outcomes Become more effective programmers Able to find and eliminate bugs efficiently Able to understand and tune for program performance Prepare for later systems classes in CS ECE Compilers Operating Systems Networks Computer Architecture Embedded Systems Carnegie Mellon Great Reality 1 Int s are not Integers Float s are not Reals Example 1 Is x2 0 Float s Yes Int s 40000 40000 1600000000 50000 50000 Example 2 Is x y z x y z Unsigned Signed Int s Yes Float s 1e20 1e20 3 14 3 14 1e20 1e20 3 14 Carnegie Mellon Code Security Example Kernel memory region holding user accessible data define KSIZE 1024 char kbuf KSIZE Copy at most maxlen bytes from kernel region to user buffer int copy from kernel void user dest int maxlen Byte count len is minimum of buffer size and maxlen int len KSIZE maxlen KSIZE maxlen memcpy user dest kbuf len return len Similar to code found in FreeBSD s implementation of getpeername There are legions of smart people trying to find vulnerabilities in programs Carnegie Mellon Typical Usage Kernel memory region holding user accessible data define KSIZE 1024 char kbuf KSIZE Copy at most maxlen bytes from kernel region to user buffer int copy from kernel void user dest int maxlen Byte count len is minimum of buffer size and maxlen int len KSIZE maxlen KSIZE maxlen memcpy user dest kbuf len return len define MSIZE 528 void getstuff char mybuf MSIZE copy from kernel mybuf MSIZE printf s n mybuf Carnegie Mellon Malicious Usage Kernel memory region holding user accessible data define KSIZE 1024 char kbuf KSIZE Copy at most maxlen bytes from kernel region to user buffer int copy from kernel void user dest int maxlen Byte count len is minimum of buffer size and maxlen int len KSIZE maxlen KSIZE maxlen memcpy user dest kbuf len return len define MSIZE 528 void getstuff char mybuf MSIZE copy from kernel mybuf MSIZE Carnegie Mellon Computer Arithmetic Does not generate random values Arithmetic operations have important mathematical properties Cannot assume all usual mathematical properties Due to finiteness of representations Integer operations satisfy ring properties Commutativity associativity distributivity Floating point operations satisfy ordering properties Monotonicity values of signs Observation Need to understand which abstractions apply in which contexts Important issues for compiler writers and serious application programmers Carnegie Mellon Great Reality 2 You ve Got to Know Assembly Chances are you ll never write program in assembly Compilers are much better more patient than you are But Understanding assembly key to machine level execution model Behavior of programs in presence of bugs High level language model breaks down Tuning program performance Understand optimizations done not done by the compiler Understanding sources of program inefficiency Implementing system software Compiler has machine code as target Operating systems must manage process state Creating fighting malware x86 assembly is the language of choice Carnegie Mellon Assembly Code Example Time Stamp Counter Special 64 bit register in Intel compatible machines Incremented every clock cycle Read with rdtsc instruction Application Measure time in clock cycles required by procedure double t start counter P t get counter printf P required f clock cycles n t Carnegie Mellon Code to Read Counter Write small amount of assembly code using GCC s asm facility Inserts assembly code into machine code generated by compiler static unsigned cyc hi 0 static unsigned cyc lo 0 Set hi and lo to the high and low order bits of the cycle counter void access counter unsigned hi unsigned lo asm rdtsc movl edx 0 movl eax 1 r hi r lo edx eax Carnegie Mellon Great Reality 3 Memory Matters Random Access Memory Is an Unphysical Abstraction Memory is not unbounded It must be allocated and managed Many applications are memory dominated Memory referencing bugs especially pernicious Effects are distant in both time and space Memory performance is not uniform Cache and virtual memory effects can greatly affect program performance Adapting program to characteristics of memory system can lead to major speed improvements Carnegie Mellon Memory Referencing Bug Example double fun int i volatile double d 1 3 14 volatile long int a 2 a i 1073741824 Possibly out of bounds return d 0 fun 0 fun 1 fun 2 fun 3 fun 4 3 14 3 14 3 1399998664856 2 00000061035156 3 14 then segmentation fault Carnegie Mellon Memory Referencing Bug Example double fun int i volatile double d 1 3 14 volatile long int a 2 a i 1073741824 Possibly out of bounds return d 0 fun 0 fun 1 fun 2 fun 3 fun 4 Explanation 3 14 3 14 3 1399998664856 2 00000061035156 3 14 then segmentation fault Saved State 4 d7 d4 3 d3 d0 2 a 1 1 a 0 0 Location accessed by fun i Carnegie Mellon Memory Referencing Errors C and C do not provide any memory protection Out of bounds array references Invalid pointer values Abuses of malloc free Can lead to nasty bugs Whether or not bug has any effect depends on system and compiler Action at a distance Corrupted object logically unrelated to one being accessed Effect of bug may be first observed long after it is generated How can I deal with this Program in Java or ML Understand what possible interactions may occur Use or develop tools to detect referencing errors Carnegie Mellon Memory System Performance Example void copyij int int int i j for i 0 i for j 0 dst i j src 2048 2048 dst 2048 2048 2048 i j 2048 j src i j void copyji int int int i j for j 0 j for i 0 dst i j src 2048 2048 dst 2048 2048 2048 j i 2048 i src i j 21 times slower Pentium 4 Hierarchical memory organization Performance depends on access patterns Including how step through multi dimensional array Carnegie Mellon The Memory Mountain Read throughput MB s Pentium III Xeon 550 MHz 16 KB on chip L1 d cache 16 KB on chip L1 i cache 512 KB off chip unified L2 cache 1200 1000 L1 800 600 400 L2 200 2k 8k 32k 128k 512k 2m 8m s15
View Full Document