Finding Bugs with DevPartner Studio Error Detection Philippe CHARMAN Polytech’Nice – Sophia.

Slides:



Advertisements
Similar presentations
Dynamic Allocation and Linked Lists. Dynamic memory allocation in C C uses the functions malloc() and free() to implement dynamic allocation. malloc is.
Advertisements

Recitation By yzhuang, sseshadr. Agenda Debugging practices – GDB – Valgrind – Strace Errors and Wrappers – System call return values and wrappers – Uninitialization.
R4 Dynamically loading processes. Overview R4 is closely related to R3, much of what you have written for R3 applies to R4 In R3, we executed procedures.
CSC241 Object-Oriented Programming (OOP) Lecture No. 9.
SPLINT STATIC CHECKING TOOL Sripriya Subramanian 10/29/2002.
COMP 1402 Winter 2009 Tutorial #8 malloc / calloc / realloc.
Dynamic memory allocation in C (Reek, Ch. 11) 1CS 3090: Safety Critical Programming in C.
C Programming - Lecture 5
CSCI 171 Presentation 11 Pointers. Pointer Basics.
CS 61C: Great Ideas in Computer Architecture (Machine Structures) Dynamic Memory Management Instructors: Randy H. Katz David A. Patterson
More Pointers Write a program that: –Calls a function to input an integer value –The above function calls another function that will double the input value.
CS 61C: Great Ideas in Computer Architecture Introduction to C, Part III Instructors: Krste Asanovic & Vladimir Stojanovic
CSE 451: Operating Systems Section 1. Why are you here? 9/30/102.
CSSE 332 Functions, Pointers in C. 2 Functions - why and how ? If a problem is large If a problem is large Modularization – easier to: Modularization.
CS414 C Programming Tutorial Ben Atkin
Memory Arrangement Memory is arrange in a sequence of addressable units (usually bytes) –sizeof( ) return the number of units it takes to store a type.
ISP - 2 nd Recitation Functions Pointers Structs Files Code Examples Homework!
. Pointers to functions Debugging. Logistics u Mid-term exam: 18/11  Closed books  List of topics – see web page Some you have to read by yourself!
CSSE 332 Explicit Memory Allocation, Parameter passing, and GDB.
1 Data Structures Data Structures Topic #2. 2 Today’s Agenda Data Abstraction –Given what we talked about last time, we need to step through an example.
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
CS 61C: Great Ideas in Computer Architecture Lecture 4: Introduction to C, Part III Instructor: Sagar Karandikar
. Memory Management. Memory Organization u During run time, variables can be stored in one of three “pools”  Stack  Static heap  Dynamic heap.
Dynamic Allocation and Linked Lists. Dynamic memory allocation in C C uses the functions malloc() and free() to implement dynamic allocation. malloc is.
University of Calgary – CPSC 441. C PROGRAM  Collection of functions  One function “main()” is called by the operating system as the starting function.
CS 11 C track: lecture 5 Last week: pointers This week: Pointer arithmetic Arrays and pointers Dynamic memory allocation The stack and the heap.
Instructor: Justin Hsia 6/27/2013Summer Lecture #41 CS 61C: Great Ideas in Computer Architecture Memory Management and Usage.
17. ADVANCED USES OF POINTERS. Dynamic Storage Allocation Many programs require dynamic storage allocation: the ability to allocate storage as needed.
Computer Science Detecting Memory Access Errors via Illegal Write Monitoring Ongoing Research by Emre Can Sezer.
1 C - Memory Simple Types Arrays Pointers Pointer to Pointer Multi-dimensional Arrays Dynamic Memory Allocation.
Stack and Heap Memory Stack resident variables include:
C Programming in Linux Jacob Chan. C/C++ and Java  Portable  Code written in one system and works in another  But in C, there are some libraries that.
1 Dynamic Memory Allocation –The need –malloc/free –Memory Leaks –Dangling Pointers and Garbage Collection Today’s Material.
C Programming Day 4. 2 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 More on Pointers Constant Pointers Two ways to.
CS 61C: Great Ideas in Computer Architecture Introduction to C, Part III Instructors: John Wawrzynek & Vladimir Stojanovic
Dynamic Memory. We will follow different order from Course Book We will follow different order from Course Book First we will cover Sect The new.
Overflow Examples 01/13/2012. ACKNOWLEDGEMENTS These slides where compiled from the Malware and Software Vulnerabilities class taught by Dr Cliff Zou.
Copyright 2005, The Ohio State University 1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation.
CSCI Rational Purify 1 Rational Purify Overview Michel Izygon - Jim Helm.
(language, compilation and debugging) David 09/16/2011.
12/23/2015Engineering Problem Solving with C++, second edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 9 An Introduction.
One-dimensional arrays and strings: Chapter 6, Slide 1 The concept of array - an extension of the basic model of memory:
Advanced Pointer Topics. Pointers to Pointers u A pointer variable is a variable that takes some memory address as its value. Therefore, you can have.
Topics This Section  More C basics  Strings  Functions  Structures  Memory.
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.
1 CSC 211 Data Structures Lecture 6 Dr. Iftikhar Azim Niaz 1.
Announcements Partial Credit Due Date for Assignment 2 now due on Sat, Feb 27 I always seem to be behind and get tons of daily. If you me and.
Instructor: Justin Hsia 6/21/2012Summer Lecture #41 CS 61C: Great Ideas in Computer Architecture Memory Management and Usage.
C Tutorial - Pointers CS 537 – Introduction to Operating Systems.
DYNAMIC MEMORY ALLOCATION. Disadvantages of ARRAYS MEMORY ALLOCATION OF ARRAY IS STATIC: Less resource utilization. For example: If the maximum elements.
Memory-Related Perils and Pitfalls in C
Overview of memory management
Dynamic Allocation in C
Content Coverity Static Analysis Use cases of Coverity Examples
Memory allocation & parameter passing
Winter 2009 Tutorial #6 Arrays Part 2, Structures, Debugger
5.13 Recursion Recursive functions Functions that call themselves
Nicholas Weaver & Vladimir Stojanovic
Recitation 6: C Review 30 Sept 2016.
Motivation and Overview
Pointers & Dynamic Memory
Checking Memory Management
Instructors: Michael Greenbaum
Instructor: Michael Greenbaum
Pointers.
Dynamic Memory Allocation
Outline Defining and using Pointers Operations on pointers
Introduction to Static Analyzer
Pointer & Memory Allocation Review
C Programming - Lecture 5
Presentation transcript:

Finding Bugs with DevPartner Studio Error Detection Philippe CHARMAN Polytech’Nice – Sophia

Installing DevPartner Studio Official name: Micro Focus DevPartner Studio Launch the installer dpspe exe It’s not necessary to install DevPartner System Comparison The trial license is valid during 10 days starting from the installation 2 modes of analysis: –non-instrumented mode: ActiveCheck (default) –instrumented code: FinalCheck

ActiveCheck - Automatic Error Detection for Daily Use ActiveCheck is used by default to find errors Reported errors come from the program executable as well as the DLLs, third-party modules, and COM components used by the program Errors can be found in programs for which you do not have source code; however it’s more efficient to understand what is going wrong when the source code is built with debug info ActiveCheck does not require recompiling or relinking. It can be used during the daily development cycle.

Errors detected by ActiveCheck (non-instrumented mode) Pointer and Leak Errors –Interface leak, memory leak, resource leak, etc. Memory Errors –Dynamic memory overrun –Freed handle is still locked –Memory allocation conflict –etc API and COM Errors –API failure - function failed –API failure - function not implemented –Invalid argument –etc

FinalCheck - Comprehensive Error Detection FinalCheck instrument the source code by adding error detection code and then passes the intrumented source code back to the compiler FinalCheck finds all the errors ActiveCheck finds but also complex or hidden program errors, including: –Memory leaks, including leaks created when reassigning or freeing memory –Memory overflows and initialization errors –Pointer errors, including errors created when assigning out-of- range pointers Because FinalCheck searches for more errors and requires that you rebuild your program, using FinalCheck will lengthen your build times. Therefore, you should use FinalCheck only when you need comprehensive error checking at a key milestone in your program, such as a Beta release, feature complete, or final production.

Errors detected by FinalCheck (instrumented mode) Pointer errors –Array index out of range –Assigning pointer out of range –etc Leak errors –Memory leaked due to free –Memory leaked due to reassignment –etc Memory errors –Reading overflows memory –Reading uninitialized memory –etc

First program hello.c #include int main() { int i; size_t length; char *string1 = "Hello, World"; char *string2 = (char*)malloc(10); length = strlen(string2); for (i = 0; string1[i] != '\0'; i++) { string2[i] = string1[i]; } length = strlen(string2); printf("My message: %s", string2); return 0; }

Building & executing hello.c Build hello.exe with these compiler and link options: cl -c hello.c link.exe hello.obj user32.lib or build by default: cl hello.c Execute hello.exe and check the following message is displayed: My message: Hello, World

Execute hello.exe in DPS Launch DevPartner Error Detection Open hello.exe (File > Open) Execute hello.exe within Error Detection 3 ways to launch the execution: –Program > Start –F5 –Blue arrow in the tool bar Check Don’t show this error dialog with Always Click on Continue

Results found Errors and memory leaks have been found

More info on the errors

More info on the memory leaks

About the results Symptom 1: Reported errors and memory leaks but we don’t see the code source Solution: code must be built in debug mode Sympton 2: Reported errors of code not from hello.cpp Solution: restrict the modules to hello.exe only Sympton 3: Missing errors not reported Solution: build in instrumented mode (FinalCheck)

Code source not displayed To enable the display of the code source, we have to build in debug mode Build hello.exe in debug For instance with these compiler and link options: cl /Zi -c hello.c link.exe /debug hello.obj user32.lib or like this: cl /MTd hello.c

Examine only hello.exe Program > Settings > Modules and Files

Building in instrumented mode NMcl.exe is installed by default in C:\Program Files\Common Files\Micro Focus\NMShared or C:\Program Files (x86)\Common Files\Micro Focus\NMShared Add this directory to your path Relaunch the execution within DPS Error Detection What do we see ?

Fixing bugs of hello.c Fix hello.c and check no more errors and memory leaks are reported

Still some errors are found

Right-click and select Filter Click on OK Do the same for all errors or memory leaks

The examples ex1.cpp.. ex11.cpp For each example exn.cpp (n=1.. 11) –Build and execute sample1.cpp in instrumented code, is by using NMcl instead of cl –Look at the results found by DPS Error Detection –Fix the bugs and check everything’s OK –When checking the bugs are fixed, the build can be done in non-instrumented code, ie by using cl

ex1.cpp void foo1(int *pi) { int j; *pi = j; } void main(int argc, char** argv) { int i=10, j; i = j; printf("i = %d\n", i); int k=10; foo1(&k); printf("k = %d\n", k); }

Analysing ex1.cpp

ex2.cpp typedef struct node { struct node* next; int val; } Node; int findLastNodeValue(Node* head) { while (head->next != NULL) { head = head->next; } return head->val; } void main(int argc, char** argv) { int i = findLastNodeValue(NULL); }

Analysing ex2.cpp

ex3.cpp void main(int argc, char** argv) { int *ipr = (int *) malloc(4 * sizeof(int)); int i, j; i = *(ipr ); j = *(ipr ); free(ipr); int *ipw = (int *) malloc(5 * sizeof(int)); *(ipw ) = 0; *(ipw ) = 0; free(ipw); }

Analysing ex3.cpp

ex4.cpp int* init_array(int *ptr, int new_size) { ptr = (int*) realloc(ptr, new_size*sizeof(int)); memset(ptr, 0, new_size*sizeof(int)); return ptr; } int* fill_fibonacci(int *fib, int size) { int i; init_array(fib, size); fib[1] = 1; for (i=2; i<size; i++) fib[i] = fib[i-1] + fib[i-2]; return fib; } void main(int argc, char** argv) { int *array = (int*)malloc(10); fill_fibonacci(array, 3); }

Analysing ex4.cpp

ex5.cpp char *append(const char* s1, const char *s2) { const int MAXSIZE = 128; char result[128]; int i=0, j=0; for (j=0; i<MAXSIZE-1 && j<strlen(s1); i++,j++) { result[i] = s1[j]; } for (j=0; i<MAXSIZE-1 && j<strlen(s2); i++,j++) { result[i] = s2[j]; } result[++i] = '\0'; return result; } void main(int argc, char** argv) { char *name = append("IBM ", append("Rational ", "Purify")); printf("%s\n", name); /* Expect BSR */ *name = '\0'; /* Expect BSW */ }

Analysing ex5.cpp

ex6.cpp void main(int argc, char** argv) { const char *name = "Life's a beach"; char *str = (char*) malloc(10); strncpy(str, name, 10); str[11] = '\0'; printf("%s\n", str); }

ex6.cpp

ex7.cpp int *pi; void foo() { pi = (int*) malloc(8*sizeof(int)); free(pi); } void main() { pi = (int*) malloc(4*sizeof(int)); foo(); pi[0] = 10; }

ex7.cpp

ex8.cpp int *plk = NULL; void main(int argc, char** argv) { plk = (int *) malloc(2 * sizeof(int)); plk++; }

ex8.cpp

ex9.cpp void main(int argc, char** argv) { int *pi = (int*) malloc(4 * sizeof(int)); delete pi; pi = new int[5]; delete pi; }

ex9.cpp

ex10.cpp void main(int argc, char** argv) { int fnh = 0; free(&fnh); int *fum = (int *) malloc(4 * sizeof(int)); free(fum+1); free(fum); }

ex10.cpp void main(int argc, char** argv) { int fnh = 0; free(&fnh); int *fum = (int *) malloc(4 * sizeof(int)); free(fum+1); free(fum); }