Merge branch 'new_fuzzer' of ssh://plrg.eecs.uci.edu:/home/git/random-fuzzer into...
[c11tester.git] / snapshot.cc
index 826046f0341e80d5ab5384ba46adb0a2d47312d7..d73613832e867eb069b5f5288b72f95bc04c96ff 100644 (file)
@@ -12,6 +12,7 @@
 #include "mymemory.h"
 #include "common.h"
 #include "context.h"
+#include "model.h"
 
 /** PageAlignedAdressUpdate return a page aligned address for the
  * address being added as a side effect the numBytes are also changed.
@@ -40,8 +41,8 @@ struct BackingPageRecord {
 
 /* Struct for each memory region */
 struct MemoryRegion {
-       void *basePtr;                  // base of memory region
-       int sizeInPages;                        // size of memory region in pages
+       void *basePtr;  // base of memory region
+       int sizeInPages;        // size of memory region in pages
 };
 
 /** ReturnPageAlignedAddress returns a page aligned address for the
@@ -57,19 +58,19 @@ struct mprot_snapshotter {
        mprot_snapshotter(unsigned int numbackingpages, unsigned int numsnapshots, unsigned int nummemoryregions);
        ~mprot_snapshotter();
 
-       struct MemoryRegion *regionsToSnapShot;                         //This pointer references an array of memory regions to snapshot
-       snapshot_page_t *backingStore;                  //This pointer references an array of snapshotpage's that form the backing store
-       void *backingStoreBasePtr;                      //This pointer references an array of snapshotpage's that form the backing store
-       struct BackingPageRecord *backingRecords;                               //This pointer references an array of backingpagerecord's (same number of elements as backingstore
-       struct SnapShotRecord *snapShots;                               //This pointer references the snapshot array
+       struct MemoryRegion *regionsToSnapShot; //This pointer references an array of memory regions to snapshot
+       snapshot_page_t *backingStore;  //This pointer references an array of snapshotpage's that form the backing store
+       void *backingStoreBasePtr;      //This pointer references an array of snapshotpage's that form the backing store
+       struct BackingPageRecord *backingRecords;       //This pointer references an array of backingpagerecord's (same number of elements as backingstore
+       struct SnapShotRecord *snapShots;       //This pointer references the snapshot array
 
-       unsigned int lastSnapShot;                      //Stores the next snapshot record we should use
-       unsigned int lastBackingPage;                           //Stores the next backingpage we should use
-       unsigned int lastRegion;                        //Stores the next memory region to be used
+       unsigned int lastSnapShot;      //Stores the next snapshot record we should use
+       unsigned int lastBackingPage;   //Stores the next backingpage we should use
+       unsigned int lastRegion;        //Stores the next memory region to be used
 
-       unsigned int maxRegions;                        //Stores the max number of memory regions we support
-       unsigned int maxBackingPages;                           //Stores the total number of backing pages
-       unsigned int maxSnapShots;                      //Stores the total number of snapshots we allow
+       unsigned int maxRegions;        //Stores the max number of memory regions we support
+       unsigned int maxBackingPages;   //Stores the total number of backing pages
+       unsigned int maxSnapShots;      //Stores the total number of snapshots we allow
 
        MEMALLOC
 };
@@ -114,7 +115,7 @@ static void mprot_handle_pf(int sig, siginfo_t *si, void *unused)
        }
        void* addr = ReturnPageAlignedAddress(si->si_addr);
 
-       unsigned int backingpage = mprot_snap->lastBackingPage++;                               //Could run out of pages...
+       unsigned int backingpage = mprot_snap->lastBackingPage++;       //Could run out of pages...
        if (backingpage == mprot_snap->maxBackingPages) {
                model_print("Out of backing pages at %p\n", si->si_addr);
                exit(EXIT_FAILURE);
@@ -167,7 +168,7 @@ static void mprot_snapshot_init(unsigned int numbackingpages,
        memset(&si, 0, sizeof(si));
        si.si_addr = ss.ss_sp;
        mprot_handle_pf(SIGSEGV, &si, NULL);
-       mprot_snap->lastBackingPage--;                  //remove the fake page we copied
+       mprot_snap->lastBackingPage--;  //remove the fake page we copied
 
        void *basemySpace = model_malloc((numheappages + 1) * PAGESIZE);
        void *pagealignedbase = PageAlignAddressUpward(basemySpace);
@@ -243,12 +244,12 @@ static void mprot_roll_back(snapshot_id theID)
        }
        mprot_snap->lastSnapShot = theID;
        mprot_snap->lastBackingPage = mprot_snap->snapShots[theID].firstBackingPage;
-       mprot_take_snapshot();                  //Make sure current snapshot is still good...All later ones are cleared
+       mprot_take_snapshot();  //Make sure current snapshot is still good...All later ones are cleared
 }
 
 #else  /* !USE_MPROTECT_SNAPSHOT */
 
-#define SHARED_MEMORY_DEFAULT  (100 * ((size_t)1 << 20))// 100mb for the shared memory
+#define SHARED_MEMORY_DEFAULT  (200 * ((size_t)1 << 20))       // 100mb for the shared memory
 #define STACK_SIZE_DEFAULT      (((size_t)1 << 20) * 20)       // 20 mb out of the above 100 mb for my stack
 
 struct fork_snapshotter {
@@ -383,7 +384,11 @@ static void fork_snapshot_init(unsigned int numbackingpages,
 
        /* switch back here when takesnapshot is called */
        snapshotid = fork_snap->currSnapShotID;
-
+       if (model->params.nofork) {
+         setcontext(&fork_snap->shared_ctxt);
+         exit(EXIT_SUCCESS);
+       }
+       
        while (true) {
                pid_t forkedID;
                fork_snap->currSnapShotID = snapshotid + 1;
@@ -424,7 +429,7 @@ static void fork_roll_back(snapshot_id theID)
        fork_snap->mIDToRollback = -1;
 }
 
-#endif/* !USE_MPROTECT_SNAPSHOT */
+#endif /* !USE_MPROTECT_SNAPSHOT */
 
 /**
  * @brief Initializes the snapshot system