Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * main.c
- *
- * Author: Alexander Ploss <a.ploss@uni-muenster.de>
- * Michel Steuwer <michel.steuwer@uni-muenster.de>
- */
- #include <stdlib.h>
- #include <stdio.h>
- #include <string.h>
- #include <malloc.h>
- // Verwendet monitoring_alloc.
- #include "monitoring_alloc.h"
- // Verwendet leaking_function.
- #include "leaking_program.h"
- // Hauptprogramm. Hier: Keine Argumente für Programm.
- int main(void) {
- int error;
- // Ungültige Zeiger sollten immer auf NULL gesetzt werden.
- char *answer = NULL;
- char *foo = NULL;
- char *bar = NULL;
- char *allocated = NULL;
- // Um die monitoring_alloc Funktionalität zu nutzen, muss man zunächst die
- // Bibliothek mit der init Funktion initialisieren.
- init_monitoring_alloc();
- // Nachfolgend steht ein Programmteil der die dynamische Speicherverwaltung
- // der monitoring_alloc Bibliothek nutzt.
- // Durch die Verwendung der monitoring_alloc_malloc und monitoring_alloc_free
- // Funktionen kann die dynamische Speicherbelegung auf Fehler untersucht
- // werden.
- // Speicher dynamisch anlegen.
- foo = (char*) monitoring_alloc_malloc(23);
- bar = (char*) monitoring_alloc_malloc(5);
- bar[0] = 11; bar[1] = 22; bar[2] = 33; bar[3] = 44; bar[4] = 55;
- allocated = leaking_function(foo, foo+23);
- // Nicht mehr benötigten Speicher wieder freigeben.
- // Zeiger wird dadurch ungültig, sollte also auf NULL gesetzt werden.
- if(allocated) {
- monitoring_alloc_free(allocated);
- allocated = NULL;
- }
- allocated = leaking_function(bar, bar+5);
- if(allocated) {
- monitoring_alloc_free(allocated);
- allocated = NULL;
- }
- if(bar) {
- monitoring_alloc_free(bar);
- bar = NULL;
- }
- allocated = leaking_function(foo, foo+23);
- if(allocated) {
- monitoring_alloc_free(allocated);
- allocated = NULL;
- }
- answer = (char*) monitoring_alloc_malloc(42);
- allocated = leaking_function(answer, answer+42);
- if(allocated) {
- monitoring_alloc_free(allocated);
- allocated = NULL;
- }
- if(answer) {
- monitoring_alloc_free(answer);
- answer = NULL;
- }
- // Nach Beendigung des eigentlichen Programms wird die shutdown Funktion der
- // Bibliothek aufgerufen. Diese räumt die Datenstrukturen der Bibliothek auf
- // und überprüft die Verwendung der Speicherverwaltung auf Fehler.
- error = shutdown_monitoring_alloc();
- return error;
- }
- /*
- * leaking_program.c
- *
- * Author: Alexander Ploss <a.ploss@uni-muenster.de>
- * Michel Steuwer <michel.steuwer@uni-muenster.de>
- */
- #include "leaking_program.h"
- // Verwendet monitoring_alloc.
- #include "monitoring_alloc.h"
- /*
- * Funktion arbeitet auf übergebenem Speicherbereich und belegt dynamisch neuen
- * Speicher.
- */
- char* leaking_function(char* first, char* last) {
- char *allocated = NULL;
- char *ptr = first;
- if(ptr) {
- while(ptr != last) {
- if(*ptr) {
- allocated = (char*) monitoring_alloc_malloc(
- (*ptr)*sizeof(char) );
- }
- ++ptr;
- }
- }
- return allocated;
- }
- /*
- * monitoring_alloc.c
- *
- * Author: Alexander Ploss <a.ploss@uni-muenster.de>
- * Michel Steuwer <michel.steuwer@uni-muenster.de>
- *
- */
- // Header einbinden.
- #include "monitoring_alloc.h"
- // Datenstrukturen verwendet von den monitoring_alloc Funktionen.
- AllocatedMemoryBlock allocated_blocks[MAX_ALLOCATIONS];
- void init_monitoring_alloc() {
- }
- int shutdown_monitoring_alloc() {
- int leakingBytes = 0;
- //insert MonitoringMemoryBlock
- (unsigned int) i = 0;
- while (i < MAX_ALLOCATIONS) {
- // check if the current allocated_blocks entry is free
- if (allocated_blocks[i].frame) {
- leakingBytes += allocated_blocks[i].size;
- }
- ++i;
- }
- printf("ERROR: Leaking %lu bytes in total!\n",
- (unsigned long) leakingBytes);
- return leakingBytes;
- }
- void *monitoring_alloc_malloc(size_t size) {
- void *allocated = NULL;
- // Reserviere speicher
- allocated = (int *) malloc(size * sizeof(int));
- //insert MonitoringMemoryBlock
- (unsigned int) i = 0;
- (bool) foundEntry = false;
- while (i < MAX_ALLOCATIONS) {
- (unsigned int) usedSpace = 0;
- // check if the current allocated_blocks entry is free
- if (!allocated_blocks[i].frame) {
- if (!foundEntry) {
- // write into allocated_blocks
- allocated_blocks[i].frame = allocated;
- allocated_blocks[i].size = size;
- allocated_blocks[i].ordinal = i;
- // add to usedSize
- usedSpace += size;
- // quit the while loop
- foundEntry = true;
- }
- } else {
- usedSpace += allocated_blocks[i].size;
- }
- ++i;
- }
- // if no free Block could be found
- if (!foundEntry) {
- printf("ERROR: No free AllocatedMemoryBlock!\n");
- }
- // if the summed space is greater than the max space
- if (usedSpace > MAX_TOTAL_ALLOCATION_SIZE) {
- printf("ERROR: Total used allocation Space is greater than MAX_TOTAL_ALLOCATION_SIZE!\n");
- }
- if (!allocated) {
- printf("ERROR: Block of size %lu could not be allocated!\n",
- (unsigned long) size);
- }
- return allocated;
- }
- void monitoring_alloc_free(void *ptr) {
- //insert MonitoringMemoryBlock
- (unsigned int) i = 0;
- (bool) foundEntry = false;
- free(ptr);
- while (!foundEntry && i < MAX_ALLOCATIONS) {
- // check if the current allocated_blocks entry is free
- if (allocated_blocks[i].frame == ptr) {
- allocated_blocks[i] = new AllocatedMemoryBlock;
- foundEntry = true;
- }
- ++i;
- }
- if (!foundEntry) {
- printf("ERROR: Could not free %p! Did'nt found in AllocatedMemoryBlocks\n", ptr);
- }
- }
- /*
- * monitoring_alloc.h
- *
- * Author: Alexander Ploss <a.ploss@uni-muenster.de>
- * Michel Steuwer <michel.steuwer@uni-muenster.de>
- */
- #ifndef MONITORING_MALLOC_H
- #define MONITORING_MALLOC_H
- #include "stdio.h"
- #include "string.h"
- #include "malloc.h"
- #define MAX_ALLOCATIONS (32)
- #define MAX_TOTAL_ALLOCATION_SIZE (1024)
- /*
- * Die Struktur AllocatedMemoryBlock beinhaltet Informationen über einen
- * belegten Speicherblock.
- */
- typedef struct {
- void * frame; // Zeiger auf den Begin des Speicherblocks
- size_t size; // Größe des belegten Speicherblocks
- size_t ordinal; // Laufende Nummer der Vergabe
- } AllocatedMemoryBlock;
- /*
- * Initialisiert Speicherverwaltung.
- *
- * Benötigte Datenstrukturen zur Verwendung in monitoring_alloc_malloc und
- * monitoring_alloc_free initialisieren.
- *
- */
- void init_monitoring_alloc();
- /*
- * Beendet Speicherverwaltung und prüft Freigaben.
- *
- * Wird nach Beendigung des Programms aufgerufen, um dynamische
- * Speicherverwaltung zu überprüfen.
- * Datenstrukturen aufräumen, verbliebenen Speicher wieder freigeben.
- * Die Größe von nicht freigegebenen Speicher in Byte wird zurückgegeben.
- */
- int shutdown_monitoring_alloc();
- /*
- * monitoring_alloc_malloc reserviert Speicherblock der Größe size und liefert
- * einen Zeiger auf den Beginn des Blocks zurück. Im Falle eines Fehler wird
- * ein NULL-Zeiger zurückgegeben.
- *
- * Verwenden Sie malloc um den Speicherblock anzulegen und verwalten Sie die
- * nötigen Informationen mittels der Struktur AllocatedMemoryBlock.
- */
- void * monitoring_alloc_malloc( size_t size );
- /*
- * monitoring_alloc_free gibt den übergebenen Speicherblock wieder frei.
- *
- * Prüfen Sie ob der Speicherblock mittels monitoring_alloc_malloc angelegt
- * wurde. Verwenden sie free um den Speicherbereich wieder freizugeben.
- * Tragen Sie die verwaltungsinformationen wieder aus (AllocatedMemoryBlock).
- *
- */
- void monitoring_alloc_free( void * );
- #endif
- /*
- * leaking_program.h
- *
- * Author: Alexander Ploss <a.ploss@uni-muenster.de>
- * Michel Steuwer <michel.steuwer@uni-muenster.de>
- */
- #ifndef LEAKING_PROGRAM_H
- #define LEAKING_PROGRAM_H
- #include <stdlib.h>
- /*
- * Funktion die auf einem übergebenen Speicherbereich arbeitet. Diese Funktion
- * benutzt evtl. selbst dynamisch reservierten Speicher.
- */
- char* leaking_function(char* first, char* last);
- #endif
- CC=gcc
- CFLAGS=-Wall -Werror -pedantic-errors -ggdb -std=c99
- OBJS = leaking_program.o main.o monitoring_alloc.o
- all: leaking_program
- leaking_program: $(OBJS)
- $(CC) $(CFLAGS) $(CLIBS) -o $@ $(OBJS)
- leaking_program.o: leaking_program.h leaking_program.c
- $(CC) $(CFLAGS) $(CLIBS) -c leaking_program.c
- monitoring_alloc.o: monitoring_alloc.h monitoring_alloc.c
- $(CC) $(CFLAGS) $(CLIBS) -c monitoring_alloc.c -o monitoring_alloc.o
- main.o: main.c
- $(CC) $(CFLAGS) $(CLIBS) -c main.c
- clean:
- -rm *.o *~ leaking_program
- .PHONY: tags
- tags:
- ctags *.[ch]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement