Advertisement
Guest User

asdfasd

a guest
Oct 28th, 2018
477
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 8.92 KB | None | 0 0
  1. /*
  2.  * main.c
  3.  *
  4.  * Author: Alexander Ploss <a.ploss@uni-muenster.de>
  5.  *         Michel Steuwer <michel.steuwer@uni-muenster.de>
  6.  */
  7.  
  8. #include <stdlib.h>
  9. #include <stdio.h>
  10. #include <string.h>
  11. #include <malloc.h>
  12.  
  13. // Verwendet monitoring_alloc.
  14. #include "monitoring_alloc.h"
  15.  
  16. // Verwendet leaking_function.
  17. #include "leaking_program.h"
  18.  
  19.  
  20. // Hauptprogramm. Hier: Keine Argumente für Programm.
  21. int main(void) {
  22.   int error;
  23.   // Ungültige Zeiger sollten immer auf NULL gesetzt werden.
  24.   char *answer    = NULL;
  25.   char *foo       = NULL;
  26.   char *bar       = NULL;
  27.   char *allocated = NULL;
  28.  
  29.   // Um die monitoring_alloc Funktionalität zu nutzen, muss man zunächst die
  30.   // Bibliothek mit der init Funktion initialisieren.
  31.   init_monitoring_alloc();
  32.  
  33.  
  34.   // Nachfolgend steht ein Programmteil der die dynamische Speicherverwaltung
  35.   // der monitoring_alloc Bibliothek nutzt.
  36.   // Durch die Verwendung der monitoring_alloc_malloc und monitoring_alloc_free
  37.   // Funktionen kann die dynamische Speicherbelegung auf Fehler untersucht
  38.   // werden.
  39.  
  40.   // Speicher dynamisch anlegen.
  41.   foo = (char*) monitoring_alloc_malloc(23);
  42.   bar = (char*) monitoring_alloc_malloc(5);
  43.   bar[0] = 11; bar[1] = 22; bar[2] = 33; bar[3] = 44; bar[4] = 55;
  44.  
  45.   allocated = leaking_function(foo, foo+23);
  46.  
  47.   // Nicht mehr benötigten Speicher wieder freigeben.
  48.   // Zeiger wird dadurch ungültig, sollte also auf NULL gesetzt werden.
  49.   if(allocated) {
  50.       monitoring_alloc_free(allocated);
  51.       allocated = NULL;
  52.   }
  53.  
  54.   allocated = leaking_function(bar, bar+5);
  55.   if(allocated) {
  56.       monitoring_alloc_free(allocated);
  57.       allocated = NULL;
  58.   }
  59.  
  60.   if(bar) {
  61.       monitoring_alloc_free(bar);
  62.       bar = NULL;
  63.   }
  64.  
  65.   allocated = leaking_function(foo, foo+23);
  66.   if(allocated) {
  67.       monitoring_alloc_free(allocated);
  68.       allocated = NULL;
  69.   }
  70.  
  71.   answer = (char*) monitoring_alloc_malloc(42);
  72.   allocated = leaking_function(answer, answer+42);
  73.   if(allocated) {
  74.  
  75.       monitoring_alloc_free(allocated);
  76.       allocated = NULL;
  77.   }
  78.   if(answer) {
  79.       monitoring_alloc_free(answer);
  80.       answer = NULL;
  81.   }
  82.  
  83.   // Nach Beendigung des eigentlichen Programms wird die shutdown Funktion der
  84.   // Bibliothek aufgerufen. Diese räumt die Datenstrukturen der Bibliothek auf
  85.   // und überprüft die Verwendung der Speicherverwaltung auf Fehler.
  86.   error = shutdown_monitoring_alloc();
  87.  
  88.   return error;
  89. }
  90.  
  91.  
  92. /*
  93.  * leaking_program.c
  94.  *
  95.  * Author: Alexander Ploss <a.ploss@uni-muenster.de>
  96.  *         Michel Steuwer <michel.steuwer@uni-muenster.de>
  97.  */
  98.  
  99. #include "leaking_program.h"
  100.  
  101. // Verwendet monitoring_alloc.
  102. #include "monitoring_alloc.h"
  103.  
  104. /*
  105.  * Funktion arbeitet auf übergebenem Speicherbereich und belegt dynamisch neuen
  106.  * Speicher.
  107.  */
  108. char* leaking_function(char* first, char* last) {
  109.   char *allocated = NULL;
  110.   char *ptr = first;
  111.   if(ptr) {
  112.     while(ptr != last) {
  113.       if(*ptr) {
  114.         allocated = (char*) monitoring_alloc_malloc(
  115.             (*ptr)*sizeof(char) );
  116.       }
  117.       ++ptr;
  118.     }
  119.   }
  120.   return allocated;
  121. }
  122.  
  123. /*
  124.  * monitoring_alloc.c
  125.  *
  126.  * Author: Alexander Ploss <a.ploss@uni-muenster.de>
  127.  *         Michel Steuwer <michel.steuwer@uni-muenster.de>
  128.  *
  129.  */
  130.  
  131. // Header einbinden.
  132. #include "monitoring_alloc.h"
  133.  
  134. // Datenstrukturen verwendet von den monitoring_alloc Funktionen.
  135. AllocatedMemoryBlock allocated_blocks[MAX_ALLOCATIONS];
  136.  
  137.  
  138. void init_monitoring_alloc() {
  139. }
  140.  
  141. int shutdown_monitoring_alloc() {
  142.     int leakingBytes = 0;
  143.  
  144.     //insert MonitoringMemoryBlock
  145.     (unsigned int) i = 0;
  146.  
  147.     while (i < MAX_ALLOCATIONS) {
  148.         // check if the current allocated_blocks entry is free
  149.         if (allocated_blocks[i].frame) {
  150.             leakingBytes += allocated_blocks[i].size;
  151.         }
  152.         ++i;
  153.     }
  154.  
  155.     printf("ERROR: Leaking %lu bytes in total!\n",
  156.            (unsigned long) leakingBytes);
  157.     return leakingBytes;
  158. }
  159.  
  160. void *monitoring_alloc_malloc(size_t size) {
  161.     void *allocated = NULL;
  162.  
  163.     // Reserviere speicher
  164.     allocated = (int *) malloc(size * sizeof(int));
  165.  
  166.     //insert MonitoringMemoryBlock
  167.     (unsigned int) i = 0;
  168.     (bool) foundEntry = false;
  169.  
  170.     while (i < MAX_ALLOCATIONS) {
  171.  
  172.         (unsigned int) usedSpace = 0;
  173.  
  174.         // check if the current allocated_blocks entry is free
  175.         if (!allocated_blocks[i].frame) {
  176.             if (!foundEntry) {
  177.                 // write into allocated_blocks
  178.                 allocated_blocks[i].frame = allocated;
  179.                 allocated_blocks[i].size = size;
  180.                 allocated_blocks[i].ordinal = i;
  181.                 // add to usedSize
  182.                 usedSpace += size;
  183.                 // quit the while loop
  184.                 foundEntry = true;
  185.             }
  186.         } else {
  187.             usedSpace += allocated_blocks[i].size;
  188.         }
  189.  
  190.         ++i;
  191.     }
  192.  
  193.     // if no free Block could be found
  194.     if (!foundEntry) {
  195.         printf("ERROR: No free AllocatedMemoryBlock!\n");
  196.     }
  197.  
  198.     // if the summed space is greater than the max space
  199.     if (usedSpace > MAX_TOTAL_ALLOCATION_SIZE) {
  200.         printf("ERROR: Total used allocation Space is greater than MAX_TOTAL_ALLOCATION_SIZE!\n");
  201.     }
  202.  
  203.     if (!allocated) {
  204.         printf("ERROR: Block of size %lu could not be allocated!\n",
  205.                (unsigned long) size);
  206.     }
  207.  
  208.     return allocated;
  209. }
  210.  
  211. void monitoring_alloc_free(void *ptr) {
  212.     //insert MonitoringMemoryBlock
  213.     (unsigned int) i = 0;
  214.     (bool) foundEntry = false;
  215.     free(ptr);
  216.  
  217.     while (!foundEntry && i < MAX_ALLOCATIONS) {
  218.  
  219.         // check if the current allocated_blocks entry is free
  220.         if (allocated_blocks[i].frame == ptr) {
  221.             allocated_blocks[i] = new AllocatedMemoryBlock;
  222.             foundEntry = true;
  223.         }
  224.  
  225.         ++i;
  226.     }
  227.  
  228.     if (!foundEntry) {
  229.         printf("ERROR: Could not free %p! Did'nt found in AllocatedMemoryBlocks\n", ptr);
  230.     }
  231. }
  232.  
  233. /*
  234.  * monitoring_alloc.h
  235.  *
  236.  * Author: Alexander Ploss <a.ploss@uni-muenster.de>
  237.  *         Michel Steuwer <michel.steuwer@uni-muenster.de>
  238.  */
  239.  
  240. #ifndef MONITORING_MALLOC_H
  241. #define MONITORING_MALLOC_H
  242.  
  243. #include "stdio.h"
  244. #include "string.h"
  245. #include "malloc.h"
  246.  
  247. #define MAX_ALLOCATIONS (32)
  248. #define MAX_TOTAL_ALLOCATION_SIZE (1024)
  249.  
  250. /*
  251.  * Die Struktur AllocatedMemoryBlock beinhaltet Informationen über einen
  252.  * belegten Speicherblock.
  253.  */
  254. typedef struct {
  255.   void *  frame;    // Zeiger auf den Begin des Speicherblocks
  256.   size_t  size;     // Größe des belegten Speicherblocks
  257.   size_t  ordinal;  // Laufende Nummer der Vergabe
  258. } AllocatedMemoryBlock;
  259.  
  260.  
  261. /*
  262.  * Initialisiert Speicherverwaltung.
  263.  *
  264.  * Benötigte Datenstrukturen zur Verwendung in monitoring_alloc_malloc und
  265.  * monitoring_alloc_free initialisieren.
  266.  *
  267.  */
  268. void init_monitoring_alloc();
  269.  
  270. /*
  271.  * Beendet Speicherverwaltung und prüft Freigaben.
  272.  *
  273.  * Wird nach Beendigung des Programms aufgerufen, um dynamische
  274.  * Speicherverwaltung zu überprüfen.  
  275.  * Datenstrukturen aufräumen, verbliebenen Speicher wieder freigeben.
  276.  * Die Größe von nicht freigegebenen Speicher in Byte wird zurückgegeben.
  277.  */
  278. int shutdown_monitoring_alloc();
  279.  
  280. /*
  281.  * monitoring_alloc_malloc reserviert Speicherblock der Größe size und liefert
  282.  * einen Zeiger auf den Beginn des Blocks zurück. Im Falle eines Fehler wird
  283.  * ein NULL-Zeiger zurückgegeben.
  284.  *
  285.  * Verwenden Sie malloc um den Speicherblock anzulegen und verwalten Sie die
  286.  * nötigen Informationen mittels der Struktur AllocatedMemoryBlock.
  287.  */
  288. void * monitoring_alloc_malloc( size_t size );
  289.  
  290. /*
  291.  * monitoring_alloc_free gibt den übergebenen Speicherblock wieder frei.
  292.  *
  293.  * Prüfen Sie ob der Speicherblock mittels monitoring_alloc_malloc angelegt
  294.  * wurde. Verwenden sie free um den Speicherbereich wieder freizugeben.
  295.  * Tragen Sie die verwaltungsinformationen wieder aus (AllocatedMemoryBlock).
  296.  *
  297.  */
  298. void monitoring_alloc_free( void * );
  299.  
  300. #endif
  301.  
  302. /*
  303.  * leaking_program.h
  304.  *
  305.  * Author: Alexander Ploss <a.ploss@uni-muenster.de>
  306.  *         Michel Steuwer <michel.steuwer@uni-muenster.de>
  307.  */
  308.  
  309. #ifndef LEAKING_PROGRAM_H
  310. #define LEAKING_PROGRAM_H
  311.  
  312. #include <stdlib.h>
  313.  
  314. /*
  315.  * Funktion die auf einem übergebenen Speicherbereich arbeitet. Diese Funktion
  316.  * benutzt evtl. selbst dynamisch reservierten Speicher.
  317.  */
  318. char* leaking_function(char* first, char* last);
  319.  
  320. #endif
  321.  
  322.  
  323.  
  324.  
  325.  
  326.  
  327.  
  328.  
  329.  
  330.  
  331.  
  332.  
  333.  
  334.  
  335. CC=gcc
  336. CFLAGS=-Wall -Werror -pedantic-errors -ggdb -std=c99
  337.  
  338. OBJS = leaking_program.o main.o monitoring_alloc.o
  339.  
  340. all: leaking_program
  341.  
  342. leaking_program: $(OBJS)
  343.     $(CC) $(CFLAGS) $(CLIBS) -o $@ $(OBJS)
  344.  
  345. leaking_program.o: leaking_program.h leaking_program.c
  346.     $(CC) $(CFLAGS) $(CLIBS) -c leaking_program.c
  347.    
  348. monitoring_alloc.o: monitoring_alloc.h monitoring_alloc.c
  349.     $(CC) $(CFLAGS) $(CLIBS) -c monitoring_alloc.c -o monitoring_alloc.o
  350.  
  351. main.o: main.c
  352.     $(CC) $(CFLAGS) $(CLIBS) -c main.c
  353.  
  354. clean:
  355.     -rm *.o *~ leaking_program
  356.  
  357. .PHONY: tags
  358.  
  359. tags:
  360.     ctags *.[ch]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement