include the correct standard headers
[model-checker.git] / snapshot-interface.cc
1 #include <stdlib.h>
2 #include <unistd.h>
3 #include <cstring>
4 #include <inttypes.h>
5
6 #include "snapshot-interface.h"
7 #include "snapshot.h"
8 #include "common.h"
9
10 /* MYBINARYNAME only works because our pathname usually includes 'model' (e.g.,
11  * /.../model-checker/test/userprog.o) */
12 #define MYBINARYNAME "model"
13 #define MYLIBRARYNAME "libmodel.so"
14 #define MAPFILE "/proc/self/maps"
15
16 SnapshotStack * snapshotObject;
17
18 #ifdef MAC
19 /** The SnapshotGlobalSegments function computes the memory regions
20  *      that may contain globals and then configures the snapshotting
21  *      library to snapshot them.
22  */
23 static void SnapshotGlobalSegments(){
24         int pid = getpid();
25         char buf[9000], execname[100];
26         FILE *map;
27
28         sprintf(execname, "vmmap -interleaved %d", pid);
29         map=popen(execname, "r");
30
31         if (!map) {
32                 perror("popen");
33                 exit(EXIT_FAILURE);
34         }
35
36         /* Wait for correct part */
37         while (fgets(buf, sizeof(buf), map)) {
38                 if (strstr(buf, "==== regions for process"))
39                         break;
40         }
41
42         while (fgets(buf, sizeof(buf), map)) {
43                 char regionname[200] = "";
44                 char type[23];
45                 char smstr[23];
46                 char r, w, x;
47                 char mr, mw, mx;
48                 int size;
49                 void *begin, *end;
50
51                 //Skip out at the end of the section
52                 if (buf[0]=='\n')
53                         break;
54
55                 sscanf(buf, "%22s %p-%p [%5dK] %c%c%c/%c%c%c SM=%3s %200s\n", type, &begin, &end, &size, &r, &w, &x, &mr, &mw, &mx, smstr, regionname);
56
57                 if (w == 'w' && (strstr(regionname, MYBINARYNAME) || strstr(regionname, MYLIBRARYNAME))) {
58                         size_t len = ((uintptr_t)end - (uintptr_t)begin) / PAGESIZE;
59                         if (len != 0)
60                                 addMemoryRegionToSnapShot(begin, len);
61                 }
62         }
63         pclose(map);
64 }
65 #else
66
67 static void get_binary_name(char *buf, size_t len)
68 {
69         if (readlink("/proc/self/exe", buf, len) == -1) {
70                 perror("readlink");
71                 exit(EXIT_FAILURE);
72         }
73 }
74
75 /** The SnapshotGlobalSegments function computes the memory regions
76  *      that may contain globals and then configures the snapshotting
77  *      library to snapshot them.
78  */
79 static void SnapshotGlobalSegments(){
80         char buf[9000];
81         char binary_name[800];
82         FILE *map;
83
84         map = fopen(MAPFILE, "r");
85         if (!map) {
86                 perror("fopen");
87                 exit(EXIT_FAILURE);
88         }
89         get_binary_name(binary_name, sizeof(binary_name));
90         while (fgets(buf, sizeof(buf), map)) {
91                 char regionname[200] = "";
92                 char r, w, x, p;
93                 void *begin, *end;
94
95                 sscanf(buf, "%p-%p %c%c%c%c %*x %*x:%*x %*u %200s\n", &begin, &end, &r, &w, &x, &p, regionname);
96                 if (w == 'w' && (strstr(regionname, binary_name) || strstr(regionname, MYLIBRARYNAME))) {
97                         size_t len = ((uintptr_t)end - (uintptr_t)begin) / PAGESIZE;
98                         if (len != 0)
99                                 addMemoryRegionToSnapShot(begin, len);
100                         DEBUG("%55s: %18p - %18p\t%c%c%c%c\n", regionname, begin, end, r, w, x, p);
101                 }
102         }
103         fclose(map);
104 }
105 #endif
106
107 SnapshotStack::SnapshotStack(){
108         SnapshotGlobalSegments();
109         stack=NULL;
110 }
111
112 SnapshotStack::~SnapshotStack(){
113 }
114
115
116 /** This method returns to the last snapshot before the inputted
117  * sequence number.  This function must be called from the model
118  * checking thread and not from a snapshotted stack.
119  * @param seqindex is the sequence number to rollback before.
120  * @return is the sequence number we actually rolled back to.
121  */
122 int SnapshotStack::backTrackBeforeStep(int seqindex) {
123         while(true) {
124                 if (stack->index<=seqindex) {
125                         //have right entry
126                         rollBack(stack->snapshotid);
127                         return stack->index;
128                 }
129                 struct stackEntry *tmp=stack;
130                 stack=stack->next;
131                 model_free(tmp);
132         }
133 }
134
135 /** This method takes a snapshot at the given sequence number. */
136 void SnapshotStack::snapshotStep(int seqindex) {
137         struct stackEntry *tmp=(struct stackEntry *)model_malloc(sizeof(struct stackEntry));
138         tmp->next=stack;
139         tmp->index=seqindex;
140         tmp->snapshotid=takeSnapshot();
141         stack=tmp;
142 }