15-213 Recitation: C Review TA’s 19 Feb 2018.

Slides:



Advertisements
Similar presentations
Lecture 3 Some commonly used C programming tricks. The system command Project No. 1: A warm-up project.
Advertisements

CS 11 C track: lecture 7 Last week: structs, typedef, linked lists This week: hash tables more on the C preprocessor extern const.
Recitation By yzhuang, sseshadr. Agenda Debugging practices – GDB – Valgrind – Strace Errors and Wrappers – System call return values and wrappers – Uninitialization.
SPLINT STATIC CHECKING TOOL Sripriya Subramanian 10/29/2002.
Buffer Overflow Exploits CS-480b Dick Steflik. What is a buffer overflow? Memory global static heap malloc( ), new Stack non-static local variabled value.
CSE 451 Section Autumn 2005 Richard Dunn Office hours: WTh 3:30-4:20 Allen 216 (or lab)
CSE 303 Lecture 13a Debugging C programs
How to Write a.c File : Introduction to Computer Systems Recitation 6, Oct 1, 2012 Alexander Malyshev (amalyshe) Section A, 10:30a – 11:20p, WeH.
Cache Lab Implementation and Blocking
Object Oriented Programming in C++ Dr. Hammadi Nait-Charif Media School Bournemouth University
Chapter 6 Buffer Overflow. Buffer Overflow occurs when the program overwrites data outside the bounds of allocated memory It was one of the first exploited.
Some Basics && GDB overview Ram Sheshadri –
CS 590 Programming Environments with UNIX. Computer Lab Account Course Homepage
CacheLab Recitation 7 10/8/2012. Outline Memory organization Caching – Different types of locality – Cache organization Cachelab – Tips (warnings, getopt,
CSE 232: C++ debugging in Visual Studio and emacs C++ Debugging (in Visual Studio and emacs) We’ve looked at programs from a text-based mode –Shell commands.
Overflow Examples 01/13/2012. ACKNOWLEDGEMENTS These slides where compiled from the Malware and Software Vulnerabilities class taught by Dr Cliff Zou.
CSE 332: C++ debugging Why Debug a Program? When your program crashes –Finding out where it crashed –Examining program memory at that point When a bug.
Agenda Attack Lab C Exercises C Conventions C Debugging
CPS4200 Unix Systems Programming Chapter 2. Programs, Processes and Threads A program is a prepared sequence of instructions to accomplish a defined task.
1 File Handling. 2 Storage seen so far All variables stored in memory Problem: the contents of memory are wiped out when the computer is powered off Example:
RECITATION Cache Lab and C
Hank Childs, University of Oregon April 15th, 2015 CIS 330: _ _ _ _ ______ _ _____ / / / /___ (_) __ ____ _____ ____/ / / ____/ _/_/ ____/__ __ / / / /
CSc 352 Debugging Tools Saumya Debray Dept. of Computer Science The University of Arizona, Tucson
1 Debugging (Part 2). “Programming in the Large” Steps Design & Implement Program & programming style (done) Common data structures and algorithms Modularity.
Announcements Assignment 1 due Wednesday at 11:59PM Quiz 1 on Thursday 1.
1 C Basics Monday, August 30, 2010 CS 241. Announcements MP1, a short machine problem, will be released today. Due: Tuesday, Sept. 7 th at 11:59pm via.
CSE 332: C++ expressions Expressions: Operators and Operands Operators obey arity, associativity, and precedence int result = 2 * 3 + 5; // assigns 11.
1 Homework Continue with K&R Chapter 5 –Skipping sections for now –Not covering section 5.12 Continue on HW5.
CSE 333 – SECTION 2 Memory Management. Questions, Comments, Concerns Do you have any? Exercises going ok? Lectures make sense? Homework 1 – START EARLY!
CSC 482/582: Computer Security
Recitation: C Review TA’s 2 Oct 2017.
a.k.a how we test Your code
YongChul Kwon CSE451 Section 1: Spring 2006 YongChul Kwon
Recitation 5: Attack Lab
Stack and Heap Memory Stack resident variables include:
Chapter 6 CS 3370 – C++ Functions.
CSE 374 Programming Concepts & Tools
Sorting Tutorial Using C On Linux.
Recitation 6: C Review 30 Sept 2016.
Recitation: Bomb Lab _______________ 18 Sep 2017.
Valgrind Overview What is Valgrind?
CSE 374 Programming Concepts & Tools
Recitation: Attack Lab
CSE 303 Concepts and Tools for Software Development
Recitation 11: More Malloc Lab
Recitation: Bomb Lab _______________ 06 Feb 2017.
CS 537 Section 1 Programming in Unix and C
Checking Memory Management
C Basics.
Lab: ssh, scp, gdb, valgrind
CSC 253 Lecture 8.
Files I/O, Streams, I/O Redirection, Reading with fscanf
Recitation 11: More Malloc Lab
Recitation 10: Malloc Lab
Lab: ssh, scp, gdb, valgrind
CSC 253 Lecture 8.
Recitation: Attack Lab
CSc 352 Debugging Tools Saumya Debray Dept. of Computer Science
File Handling.
When your program crashes
a.k.a how we test Your code
Homework Continue with K&R Chapter 5 Skipping sections for now
C Programming - Lecture 5
CAP6135: Malware and Software Vulnerability Analysis Buffer Overflow : Example of Using GDB to Check Stack Memory Cliff Zou Spring 2016.
Valgrind Overview What is Valgrind?
Makefiles, GDB, Valgrind
CAP6135: Malware and Software Vulnerability Analysis Buffer Overflow : Example of Using GDB to Check Stack Memory Cliff Zou Spring 2010.
Caches and Blocking 26th February 2018.
Format String Vulnerability
Recitation: Bomb Lab Your TAs September 16th 2019.
Presentation transcript:

15-213 Recitation: C Review TA’s 19 Feb 2018

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

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

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!

C Activities Basic C Programming Questions Learn to use getopt Activity 1 and 2: Common Pointer Mistakes Activity 3 and 4: Common Malloc Mistakes Activity 5: Common Macro Mistakes Learn to use getopt Extremely useful for Cache Lab Processes command line arguments

C Activities Pair up! Login to a shark machine $ wget http://www.cs.cmu.edu/~213/activities/rec5.tar $ tar xvf rec5.tar $ cd rec5 $ make Open act_pointers.c

C Activity (pointers): act1 Open act_pointers.c, make sure main is running act1 $ make pointers $ ./act_pointers What happened? Let’s try to debug $ gdb act_pointers (gdb) run (gdb) backtrace $ valgrind --leak-check=full ./act_pointers How would you fix this? Is there another way?

C Activity (pointers): act2 Switch main to run act2 $ make pointers $ ./act_pointers What happened? Let’s try to debug $ gdb act_pointers (gdb) break act2 (gdb) run (gdb) list (gdb) watch *d (gdb) continue

C Activity (malloc): act3 Make sure main is running act3 $ make malloc $ ./act_malloc What happened? Let’s try to debug $ valgrind ./act_malloc Are there any errors? Is there any memory lost?

C Activity (malloc): act4 Switch main to run act4 $ make malloc $ ./act_malloc What happened? Let’s try to debug $ gdb ./act_malloc (gdb) run (gdb) backtrace

C Activity (macros): act5 Switch to act_macros.c $ make macros $ ./act_macros What happened? Let’s try to debug $ gcc -std=c99 -E act_macros.c > expanded.txt Open expanded.txt and look at act5 How did the macros expand?

C Activity (macros): Review Macros are good for compile-time decisions Assert, requires, etc dbg_print Macros are not functions and should not be used interchangeably Use functions whenever you can

C Activities Conclusion Did you answer every question correctly? If not… Refer the C Bootcamp slides 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!

C Programming Style Document your code with comments Check 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: http://cs.cmu.edu/~213/codeStyle.html

C Exercise: $ man 3 getopt int 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)

C Exercise: getopt example $ make getopt $ ./getopt_example -n 10 $ ./getopt_example -n 10 -v Try switching the parameters around. Does it still work? Modify the example to include a step size $ ./getopt_example -n 10 -s 2 -v should count 0 – 10 by 2s The default step size should be 1

C Exercise: getopt Now write your own! Open a new file called getopt.c Write a simple hashing function with usage -s : string with length at most 100 -i : max index -v : optional verbose flag -f : which hashing function to use When f is off, sum all characters % i When f is set, multiply each character by it’s index before adding

If you get stuck… C Bootcamp next Sunday 2/25 Reread the writeup Look at CS:APP Chapter 6 Review 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

Appendix Valgrind Clang / LLVM Cache Structure

Appendix: Valgrind Tool used for debugging memory use Finds 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

Appendix: Clang / LLVM Clang is a (gcc equivalent) C compiler Support 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)

Appendix: Cache Structure