System Assertions. Your Submissions. Oral Exams FEBRUARY FEBRUARY FEBRUARY

Similar documents
System Assertions. Andreas Zeller

Asserting Expectations. Your Submissions. Oral Exams

0/41. Isolating Infections. Andreas Zeller. Lehrstuhl Softwaretechnik Universität des Saarlandes, Saarbrücken

DEBUGGING: DYNAMIC PROGRAM ANALYSIS

TI2725-C, C programming lab, course

In Java we have the keyword null, which is the value of an uninitialized reference type

Announcements. assign0 due tonight. Labs start this week. No late submissions. Very helpful for assign1

CS 241 Data Organization Binary Trees

CSCI-243 Exam 1 Review February 22, 2015 Presented by the RIT Computer Science Community

CS 11 C track: lecture 6

COSC Software Engineering. Lecture 16: Managing Memory Managers

A program execution is memory safe so long as memory access errors never occur:

Lecture 14. No in-class files today. Homework 7 (due on Wednesday) and Project 3 (due in 10 days) posted. Questions?

Stanford University Computer Science Department CS 295 midterm. May 14, (45 points) (30 points) total

o Code, executable, and process o Main memory vs. virtual memory

Isolating and Locating Cause-Effect Chains. bug.c. What is the cause of this failure? What do we do now? Andreas Zeller

CSC C69: OPERATING SYSTEMS

Class Information ANNOUCEMENTS

Lecture Notes on Memory Layout

[0569] p 0318 garbage

INITIALISING POINTER VARIABLES; DYNAMIC VARIABLES; OPERATIONS ON POINTERS

CIS 551 / TCOM 401 Computer and Network Security. Spring 2007 Lecture 2

CS61, Fall 2012 Section 2 Notes

C Bounds Non-Checking. Code Red. Reasons Not to Use C. Good Reasons to Use C. So, why would anyone use C today? Lecture 10: *&!

6.S096: Introduction to C/C++

CMSC 341 Lecture 2 Dynamic Memory and Pointers

CSE 303 Concepts and Tools for Software Development. Magdalena Balazinska Winter 2010 Lecture 27 Final Exam Revision

Memory Allocation in C C Programming and Software Tools. N.C. State Department of Computer Science

CS527 Software Security

Last week. Data on the stack is allocated automatically when we do a function call, and removed when we return

Limitations of the stack

Lecture 14 Notes. Brent Edmunds

ch = argv[i][++j]; /* why does ++j but j++ does not? */

ECE 551D Spring 2018 Midterm Exam

DAY 3. CS3600, Northeastern University. Alan Mislove

Memory Analysis tools

CS 322 Operating Systems Practice Midterm Questions

CSC 1600 Memory Layout for Unix Processes"

CS 241 Honors Memory

CS140 Operating Systems Final December 12, 2007 OPEN BOOK, OPEN NOTES

Lecture 8 Dynamic Memory Allocation

Praktische Aspekte der Informatik

CSCI-1200 Data Structures Spring 2016 Lecture 6 Pointers & Dynamic Memory

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING

CSCI 237 Sample Final Exam

Dynamic Allocation in C

Memory Management. CS449 Fall 2017

Undefined Behaviour in C

CS 11 C track: lecture 5

Static Program Analysis Part 1 the TIP language

Dynamic Allocation in C

CptS 360 (System Programming) Unit 4: Debugging

Buffer overflow background

CSci 4061 Introduction to Operating Systems. Programs in C/Unix

Introduction to Computer Systems. Exam 2. April 11, Notes and calculators are permitted, but not computers.

CS 31: Intro to Systems Pointers and Memory. Kevin Webb Swarthmore College October 2, 2018

Lecture 07 Debugging Programs with GDB

ECE551 Midterm Version 2

Kurt Schmidt. October 30, 2018

COMP26120: Linked List in C (2018/19) Lucas Cordeiro

CP2 Revision. theme: dynamic datatypes & data structures

Debugging! The material for this lecture is drawn, in part, from! The Practice of Programming (Kernighan & Pike) Chapter 5!

ANITA S SUPER AWESOME RECITATION SLIDES

EL2310 Scientific Programming

Memory Management: The process by which memory is shared, allocated, and released. Not applicable to cache memory.

Project 0: Implementing a Hash Table

Lectures 5-6: Introduction to C

Lectures 5-6: Introduction to C

18-642: Code Style for Compilers

Dynamic Memory Allocation: Advanced Concepts

Important From Last Time

THE GOOD, BAD AND UGLY ABOUT POINTERS. Problem Solving with Computers-I

Creating a String Data Type in C

Buffer Overflow Defenses

Programming in C. Lecture 9: Tooling. Dr Neel Krishnaswami. Michaelmas Term

DEBUGGING: OBSERVING AND TRACKING

Advanced Memory Allocation

Important From Last Time

Pointers, Dynamic Data, and Reference Types

C Programming Basics II

Intermediate Programming, Spring 2017*

EL2310 Scientific Programming

Quiz 0 Review Session. October 13th, 2014

CS 161 Computer Security

Lecture 9 Assertions and Error Handling CS240

Dynamic Memory. Dynamic Memory Allocation Strings. September 18, 2017 Hassan Khosravi / Geoffrey Tien 1

Memory (Stack and Heap)

Dynamic Data Structures (II)

New features in AddressSanitizer. LLVM developer meeting Nov 7, 2013 Alexey Samsonov, Kostya Serebryany

CIS 190: C/C++ Programming. Lecture 3 Memory Management in C

C Review. MaxMSP Developers Workshop Summer 2009 CNMAT

Cling: A Memory Allocator to Mitigate Dangling Pointers. Periklis Akritidis

Programming in C and C++

CSCI 171 Chapter Outlines

Page 1. Today. Important From Last Time. Is the assembly code right? Is the assembly code right? Which compiler is right?

COMP 524 Spring 2018 Midterm Thursday, March 1

However, in C we can group related variables together into something called a struct.

Lecture 20 C s Memory Model

Programming. Pointers, Multi-dimensional Arrays and Memory Management

Binghamton University. CS-211 Fall Dynamic Memory

Transcription:

System Assertions Andreas Zeller 1 Your Submissions Program must behave exactly as specified (i.e., input, output, flags, etc.) Program must use recent Python 2 version (i.e., Python 2.6 installed in CIP pools) If we have to fix your programs, you will lose points 2 2 Oral Exams FEBRUARY 2 FEBRUARY 3 FEBRUARY 4 3 3

Andreas Leitner Reproducing Crashes on Thursday 4 4 Complex Invariants http:// en.wikipedia.org/wiki/ Red-black_tree 13 8 17 1 11 15 25 6 22 27 5 5 Complex Invariants class RedBlackTree { boolean sane() { assert (roothasnoparent()); assert (rootisblack()); assert (rednodeshaveonlyblackchildren()); assert (equalnumberofblacknodesonsubtrees()); assert (treeisacyclic()); assert (parentsareconsistent()); } } return true; 6 6

Deletion http:// en.wikipedia.org/wiki/ Red-black_tree void delete_one_child(struct node *p) { /* * Precondition: p has at most one non-null child. */ struct node *child = is_leaf(p->right)? p->left : p->right; replace_node(p, child); if (p->color == BLACK) { if (child->color == RED) child->color = BLACK; else { /* delete_case1(child): */ struct node *n = child; /* this loop performs tail recursion on delete_case1(n) */ for (;;) { /* delete_case1(n): */ if (n->parent!= NULL) { /* delete_case2(n): */ struct node *s; s = sibling(n); if (s->color == RED) 7 { n->parent->color = RED; 7 System Assertions Andreas Zeller 8 System Invariants Some properties of a program must hold over the entire run: must not access data of other processes must handle mathematical exceptions must not exceed its privileges Typically checked by hardware and OS 9 9

Memory Invariants Even within a single process, some invariants must hold over the entire run code integrity data integrity This is a major issue in C and C++ 10 10 Heap Misuse s = malloc(30) free_list free(s) t = malloc(20) strcpy(t, hello ) bhello s[10] = b free(s) s t 11 11 Heap Assertions The GNU C runtime library provides a simple check against common errors: $ MALLOC_CHECK_=2 myprogram myargs free() called on area that was already free'd() Aborted (core dumped) $ _ 12 12

Heap Assertions s = malloc(30) s free(s) free(s) USED FREE free() called on area that was already free'd() Aborted (core dumped) 13 13 Array Assertions The Electric Fence library checks for array overflows: $ gcc -g -o sample-with-efence sample.c -lefence $./sample-with-efence 11 14 Electric Fence 2.1 Segmentation fault (core dumped) $ _ 14 14 Array Assertions s = malloc(30) s[30] = x s x Segmentation fault (core dumped) MMU detects accesses 15 15

Memory Assertions The Valgrind tool checks all memory accesses: $ valgrind sample 11 14 Invalid read of size 4 at 0x804851F: shell_sort (sample.c:18) by 0x8048646: main (sample.c:35) by 0x40220A50: libc_start_main (in /lib/libc.so) by 0x80483D0: (within /home/zeller/sample) Valgrind works as an interpreter for x86 code 16 16 Valgrind Checks Use of uninitialized memory Accessing free d memory Accessing memory beyond malloc d block Accessing inappropriate stack areas Memory leaks: allocated area is not free d Passing uninitialized memory to system calls 17 17 Shadow Memory V-bit set = corresponding bit is initialized A-bit set = corresponding byte is accessible Address 0x40EE9024 0x40EE9028 0x40EE902C A-bits V-bits Variables a[0] = 11 a[1] = 14 a[2] =? 18 18

V-Bits When a bit is first written, its V-bit is set Simple read accesses to uninitialized memory do not result in warnings: struct S { int x; char c; }; struct S s1, s2; s1.x = 42; s1.c = 'z'; s2 = s1; 5 bytes initialized 8 bytes copied (no warning) 19 19 V-Bits Warnings Reading uninitialized data causes a warning if a value is used to generate an address a control flow decision is to be made a value is passed to a system call 20 20 A-Bits When the program starts, all global data is marked accessible (= A-bits are set) malloc() sets A-bits for the area returned; free() clears them Local variables are accessible on entry and non-accessible on exit Accessing non-accessible data error 21 21

Overhead Tool GNU C Library Electric Fence Valgrind Space 2 bytes/ malloc 1 page/ malloc 100% Time negligible negligible 2500% 22 22 Preventing Misuse CYCLONE is a C dialect which prevents common pitfalls of C Most important feature: special pointers 23 23 Non-NULL Pointers int getc (FILE @fp); fp may not be NULL extern FILE *fp; char c = getc(fp); warning: NULL check inserted 24 24

Fat Pointers A fat pointer holds address and size All accesses via a fat pointer are automatically bounds-checked int strlen(const char? s) addr size 6 s Hello *s 25 25 CYCLONE Restrictions NULL checks are inserted Pointer arithmetic is restricted Pointers must be initialized before use Dangling pointers are prevented through region analysis and limitations on free() Only safe casts and unions are allowed 26 26 Production Code Should products ship with active assertions? 27 27

Things to Check Critical results. If lives, health, or money depend on a result, it had better be checked. External conditions. Any conditions which are not within our control must be checked for integrity. 28 28 Points to Consider The more active assertions, the greater the chance to catch infections. The sooner a program fails, the easier it is to track the defect. Defects that escape into the field are the hardest to track. 29 29 More to Consider By default, failing assertions are not userfriendy. Handle assertions in a user-friendly way Assertions impact performance. First measure; then turn off only the most time-consuming assertions 30 30

Concepts To check memory integrity, use specialized tools to detect errors at run time Apply such tools before any other method To fully prevent memory errors, use another language (or dialect, e.g. Cyclone) Turning assertions off seldom justifies the risk of erroneous computation 31 31 This work is licensed under the Creative Commons Attribution License. To view a copy of this license, visit http://creativecommons.org/licenses/by/1.0 or send a letter to Creative Commons, 559 Abbott Way, Stanford, California 94305, USA. 32 32