segfault file/line breakpoint info
[c11tester.git] / snapshot.cc
index 2c7d84193e1feb21443696335603b951491be9af..fb00d1511c8505df3c34fc22eaa0b7adad838937 100644 (file)
 /* extern declaration definition */
 struct SnapShot * snapshotrecord = NULL;
 
+/** PageAlignedAdressUpdate return a page aligned address for the
+ * address being added as a side effect the numBytes are also changed.
+ */
+static void * PageAlignAddressUpward(void * addr) {
+       return (void *)((((uintptr_t)addr)+PAGESIZE-1)&~(PAGESIZE-1));
+}
+
 #if !USE_MPROTECT_SNAPSHOT
 /** @statics
 *   These variables are necessary because the stack is shared region and
@@ -45,16 +52,8 @@ struct SnapShot * snapshotrecord = NULL;
 static ucontext_t savedSnapshotContext;
 static ucontext_t savedUserSnapshotContext;
 static snapshot_id snapshotid = 0;
-#endif
-
-/** PageAlignedAdressUpdate return a page aligned address for the
- * address being added as a side effect the numBytes are also changed.
- */
-static void * PageAlignAddressUpward(void * addr) {
-       return (void *)((((uintptr_t)addr)+PAGESIZE-1)&~(PAGESIZE-1));
-}
 
-#if USE_MPROTECT_SNAPSHOT
+#else /* USE_MPROTECT_SNAPSHOT */
 
 /** ReturnPageAlignedAddress returns a page aligned address for the
  * address being added as a side effect the numBytes are also changed.
@@ -89,6 +88,8 @@ static void HandlePF( int sig, siginfo_t *si, void * unused){
        if( si->si_code == SEGV_MAPERR ){
                printf("Real Fault at %p\n", si->si_addr);
                print_trace();
+               printf("For debugging, place breakpoint at: %s:%d\n",
+                               __FILE__, __LINE__);
                exit( EXIT_FAILURE );
        }
        void* addr = ReturnPageAlignedAddress(si->si_addr);
@@ -109,7 +110,7 @@ static void HandlePF( int sig, siginfo_t *si, void * unused){
                // Handle error by quitting?
        }
 }
-#endif //nothing to handle for non snapshotting case.
+#endif /* USE_MPROTECT_SNAPSHOT */
 
 #if !USE_MPROTECT_SNAPSHOT
 void createSharedMemory(){
@@ -129,17 +130,17 @@ void createSharedMemory(){
 #endif
 
 
-/** The initSnapShotLibrary function initializes the Snapshot library.
+/** The initSnapshotLibrary function initializes the snapshot library.
  *  @param entryPoint the function that should run the program.
  */
 #if USE_MPROTECT_SNAPSHOT
 
-void initSnapShotLibrary(unsigned int numbackingpages,
+void initSnapshotLibrary(unsigned int numbackingpages,
                unsigned int numsnapshots, unsigned int nummemoryregions,
                unsigned int numheappages, VoidFuncPtr entryPoint) {
        /* Setup a stack for our signal handler....  */
        stack_t ss;
-       ss.ss_sp = model_malloc(SIGSTACKSIZE);
+       ss.ss_sp = PageAlignAddressUpward(model_malloc(SIGSTACKSIZE+PAGESIZE-1));
        ss.ss_size = SIGSTACKSIZE;
        ss.ss_flags = 0;
        sigaltstack(&ss, NULL);
@@ -171,22 +172,29 @@ void initSnapShotLibrary(unsigned int numbackingpages,
        HandlePF(SIGSEGV, &si, NULL);
        snapshotrecord->lastBackingPage--; //remove the fake page we copied
 
-       basemySpace=model_malloc((numheappages+1)*PAGESIZE);
+       void *basemySpace = model_malloc((numheappages+1)*PAGESIZE);
        void * pagealignedbase=PageAlignAddressUpward(basemySpace);
-       mySpace = create_mspace_with_base(pagealignedbase,  numheappages*PAGESIZE, 1 );
+       user_snapshot_space = create_mspace_with_base(pagealignedbase, numheappages * PAGESIZE, 1);
        addMemoryRegionToSnapShot(pagealignedbase, numheappages);
+
+       void *base_model_snapshot_space = model_malloc((numheappages + 1) * PAGESIZE);
+       pagealignedbase = PageAlignAddressUpward(base_model_snapshot_space);
+       model_snapshot_space = create_mspace_with_base(pagealignedbase, numheappages * PAGESIZE, 1);
+       addMemoryRegionToSnapShot(pagealignedbase, numheappages);
+
        entryPoint();
 }
 #else
-void initSnapShotLibrary(unsigned int numbackingpages,
+void initSnapshotLibrary(unsigned int numbackingpages,
                unsigned int numsnapshots, unsigned int nummemoryregions,
                unsigned int numheappages, VoidFuncPtr entryPoint) {
-       basemySpace=system_malloc((numheappages+1)*PAGESIZE);
-       void * pagealignedbase=PageAlignAddressUpward(basemySpace);
-       mySpace = create_mspace_with_base(pagealignedbase,  numheappages*PAGESIZE, 1 );
        if (!snapshotrecord)
                createSharedMemory();
 
+       void *base_model_snapshot_space = malloc((numheappages + 1) * PAGESIZE);
+       void *pagealignedbase = PageAlignAddressUpward(base_model_snapshot_space);
+       model_snapshot_space = create_mspace_with_base(pagealignedbase, numheappages * PAGESIZE, 1);
+
        //step 2 setup the stack context.
        ucontext_t newContext;
        getcontext( &newContext );
@@ -283,8 +291,17 @@ snapshot_id takeSnapshot( ){
  *  @param theID is the snapshot identifier to rollback to.
  */
 void rollBack( snapshot_id theID ){
+#if USE_MPROTECT_SNAPSHOT==2
+       if (snapshotrecord->lastSnapShot==(theID+1)) {
+               for(unsigned int page=snapshotrecord->snapShots[theID].firstBackingPage; page<snapshotrecord->lastBackingPage; page++) {
+                       memcpy(snapshotrecord->backingRecords[page].basePtrOfPage, &snapshotrecord->backingStore[page], sizeof(struct SnapShotPage));
+               }
+               return;
+       }
+#endif
+
 #if USE_MPROTECT_SNAPSHOT
-       HashTable< void *, bool, uintptr_t, 4, model_malloc, model_calloc, MYFREE> duplicateMap;
+       HashTable< void *, bool, uintptr_t, 4, model_malloc, model_calloc, model_free> duplicateMap;
        for(unsigned int region=0; region<snapshotrecord->lastRegion;region++) {
                if( mprotect(snapshotrecord->regionsToSnapShot[region].basePtr, snapshotrecord->regionsToSnapShot[region].sizeInPages*sizeof(struct SnapShotPage), PROT_READ | PROT_WRITE ) == -1 ){
                        perror("mprotect");