15-213 Recitation: C Review TA’s 2 Oct 2017.

1 Recitation: C Review TA’s 2 Oct 2017 ...
Author: Blake Clarke
0 downloads 2 Views

1 Recitation: C Review TA’s 2 Oct 2017

2 Agenda Logistics Attack Lab Conclusion C AssessmentC Programming Style C Exercise Cache Lab Overview Appendix: Valgrind Clang / LLVM Cache Structure

3 Logistics Attack Lab is due tomorrow at midnight!Come to office hours for help rtarget phase 3 is only worth 5 points 0.2% of your grade ≈ 0% of your grade Cache Lab will be released shortly thereafter!

4 Attack Lab Conclusion Don’t use functions vulnerable to buffer overflow (like gets) Use functions that allow you to specify buffer lengths: fgets instead of gets strncpy instead of strcpy strncat instead of strcat snprintf instead of sprint Use sscanf and fscanf with input lengths (%213s) Stack protection makes buffer overflow very hard… But very hard ≠ impossible!

5 C Assessment 3.5 Basic C Programming QuestionsTake some time to write down your answer for each question Don’t worry if you think these questions are hard… Come to the C Bootcamp (Check Piazza for time and location)

6 C Assessment: Question 1Which lines have a problem and how can you fix it? 1 int main(int argc, char** argv) { 2 int *a = (int*) malloc(213 * sizeof(int)); 3 for (int i=0; i<213; i++) { 4 if (a[i] == 0) a[i]=i; 5 else a[i]=-i; 6 } 7 return 0; 8 }

7 C Assessment: Question 1malloc can fail! 1 int main(int argc, char** argv) { 2 int *a = (int*) malloc(213 * sizeof(int)); if (a == NULL) return 0; 3 for (int i=0; i<213; i++) { 4 if (a[i] == 0) a[i]=i; 5 else a[i]=-i; 6 } 7 return 0; 8 }

8 C Assessment: Question 1Allocated memory is not initialized! 1 int main(int argc, char** argv) { 2 int *a = (int*) calloc(213 * sizeof(int)); if (a == NULL) return 0; 3 for (int i=0; i<213; i++) { 4 if (a[i] == 0) a[i]=i; 5 else a[i]=-i; 6 } 7 return 0; 8 }

9 C Assessment: Question 1Declaring variables inside a for loop requires -std=c99 1 int main(int argc, char** argv) { 2 int *a = (int*) calloc(213 * sizeof(int)); if (a == NULL) return 0; 3 for (int i=0; i<213; i++) { 4 if (a[i] == 0) a[i]=i; 5 else a[i]=-i; 6 } 7 return 0; 8 }

10 C Assessment: Question 1All allocated memory must be freed! 1 int main(int argc, char** argv) { 2 int *a = (int*) calloc(213 * sizeof(int)); if (a == NULL) return 0; 3 for (int i=0; i<213; i++) { 4 if (a[i] == 0) a[i]=i; 5 else a[i]=-i; 6 } free(a); 7 return 0; 8 }

11 C Assessment: Question 2What are the values of A and B? #define SUM(x, y) x + y int sum(int x, int y) { return x + y; } int A = SUM(2, 1) * 3; int B = sum(2, 1) * 3;

12 C Assessment: Question 2What is wrong with our macro SUM? #define SUM(x, y) x + y int sum(int x, int y) { return x + y; } int A = SUM(2, 1) * 3; // A = * 3 = 5!? int B = sum(2, 1) * 3; // B = 6

13 C Assessment: Question 2Use parenthesis around result! #define SUM(x, y) (x + y) int sum(int x, int y) { return x + y; } int A = SUM(2, 1) * 3; // A = 6 int B = sum(2, 1) * 3; // B = 6

14 C Assessment: Question 2 Part BWhat are the values of A and B? #define MULT(x, y) (x * y) int mult(int x, int y) { return x * y; } int A = MULT(2, 0 + 1) * 3; int B = mult(2, 0 + 1) * 3;

15 C Assessment: Question 2 Part BWhat is wrong with our macro MULT? #define MULT(x, y) (x * y) int mult(int x, int y) { return x * y; } int A = MULT(2, 0 + 1) * 3; // A = (2 * 0 + 1) * 3 = 3?! int B = mult(2, 0 + 1) * 3; // B = 6

16 C Assessment: Question 2 Part BUse parenthesis around macro arguments (and result)! #define MULT(x, y) ((x) * (y)) int mult(int x, int y) { return x * y; } int A = MULT(2, 0 + 1) * 3; // A = ((2) * (0 + 1)) * 3 = 6 int B = mult(2, 0 + 1) * 3; // B = 6

17 C Assessment: Question 2Macros are good for compile-time decisions Assert, requires, etc dbg_print Macros are not functions and should not be used interchangeably

18 C Assessment: Question 3What lines make safe_int_malloc not so safe? 1 int *safe_int_malloc(int *pointer) { 2 pointer = malloc(sizeof(int)); 3 if (pointer == NULL) exit(-1); 4 return &pointer; 5 }

19 C Assessment: Question 3pointer is a local copy of the pointer! 1 int *safe_int_malloc(int **pointer) { 2 *pointer = malloc(sizeof(int)); 3 if (pointer == NULL) exit(-1); 4 return &pointer; 5 }

20 C Assessment: Question 3&pointer is a location on the stack in safe_int_malloc’s frame! 1 int **safe_int_malloc(int **pointer) { 2 *pointer = malloc(sizeof(int)); 3 if (pointer == NULL) exit(-1); 4 return pointer; 5 }

21 C Assessment ConclusionDid you answer every question correctly? If not… Come to the C Bootcamp (Check Piazza for time and location) Was the test so easy you were bored? If not… When in doubt… This will be very important for the rest of this class, so make sure you are comfortable with the material covered or come to the C Bootcamp!

22 C Programming Style Document your code with commentsCheck error and failure conditions Write modular code Use consistent formatting Avoid memory and file descriptor leaks Warning: Dr. Evil has returned to grade style on Cache Lab!  Refer to full 213 Style Guide:

23 C Exercise Learn to use getoptExtremely useful for Cache Lab Processes command line arguments Let’s write a Pythagorean Triples Solver! Pair up! Login to a shark machine $ wget $ tar xvf rec6.tar $ cd rec6 But first, a simple getopt example… $ vim getopt-example.c

24 C Exercise: $ man 3 getoptint getopt(int argc, char * const argv[], const char *optstring); getopt returns -1 when done parsing optstring is string with command line arguments Characters followed by colon require arguments Find argument text in char *optarg getopt can’t find argument or finds illegal argument sets optarg to “?” Example: “abc:d:” a and b are boolean arguments (not followed by text) c and d are followed by text (found in char *optarg)

25 C Exercise: C Hints and Math Reminders𝑎 2 + 𝑏 2 = 𝑐 2 ⇒𝑎= 𝑐 2 − 𝑏 2 ⇒𝑏= 𝑐 2 − 𝑎 2 ⇒𝑐= 𝑎 2 + 𝑏 2 ⇒ = 5 2 String to float in C: #include float atof(const char *str); Square root in C: #include float sqrt(float x);

26 Cache Lab Overview Programs exhibiting locality run a lot faster!Temporal Locality – same item referenced again Spatial Locality – nearby items referenced again Cache Lab’s Goal: Understand how L1, L2, … etc. caches work Optimize memory dependent code to minimize cache misses and evictions Noticeable increase in speed

27 If you get stuck… Reread the writeup Look at CS:APP Chapter 6Review lecture notes (http://cs.cmu.edu/~213) Come to Office Hours (Sunday to Thursday, 5-9pm WH-5207) Post private question on Piazza man malloc, man valgrind, man gdb

28 Cache Lab Tips! Review cache and memory lecturesAsk if you don’t understand something Start early, this can be a challenging lab! Don’t get discouraged! If you try something that doesn't work, take a well deserved break, and then try again Finally, Good luck on Cache Lab!

29 Appendix Valgrind Clang / LLVM Cache Structure

30 Appendix: Valgrind Tool used for debugging memory useFinds many potential memory leaks and double frees Shows heap usage over time Detects invalid memory reads and writes To learn more… man valgrind Finding memory leaks $ valgrind –leak-resolution=high –leak-check=full –show- reachable=yes –track-fds=yes ./myProgram arg1 arg2

31 Appendix: Clang / LLVM Clang is a (gcc equivalent) C compilerSupport for code analyses and transformation Compiler will check you variable usage and declarations Compiler will create code recording all memory accesses to a file Useful for Cache Lab Part B (Matrix Transpose)

32 Appendix: Cache Structure