snapshot-interface: change hand-coded stack to vector
[c11tester.git] / snapshot-interface.cc
1 #include <stdlib.h>
2 #include <unistd.h>
3 #include <cstring>
4 #include <inttypes.h>
5 #include <vector>
6
7 #include "snapshot-interface.h"
8 #include "snapshot.h"
9 #include "common.h"
10 #include "mymemory.h"
11
12 /* MYBINARYNAME only works because our pathname usually includes 'model' (e.g.,
13  * /.../model-checker/test/userprog.o) */
14 #define MYBINARYNAME "model"
15 #define MYLIBRARYNAME "libmodel.so"
16 #define MAPFILE "/proc/self/maps"
17
18 struct stackEntry {
19         stackEntry(snapshot_id id, int idx) : snapshotid(id), index(idx) { }
20         snapshot_id snapshotid;
21         int index;
22         MEMALLOC
23 };
24
25 class SnapshotStack {
26  public:
27         SnapshotStack();
28         ~SnapshotStack();
29         int backTrackBeforeStep(int seq_index);
30         void snapshotStep(int seq_index);
31
32         MEMALLOC
33  private:
34         std::vector<struct stackEntry, ModelAlloc<struct stackEntry> > stack;
35 };
36
37 static SnapshotStack *snapshotObject;
38
39 #ifdef MAC
40 /** The SnapshotGlobalSegments function computes the memory regions
41  *      that may contain globals and then configures the snapshotting
42  *      library to snapshot them.
43  */
44 static void SnapshotGlobalSegments()
45 {
46         int pid = getpid();
47         char buf[9000], execname[100];
48         FILE *map;
49
50         sprintf(execname, "vmmap -interleaved %d", pid);
51         map = popen(execname, "r");
52
53         if (!map) {
54                 perror("popen");
55                 exit(EXIT_FAILURE);
56         }
57
58         /* Wait for correct part */
59         while (fgets(buf, sizeof(buf), map)) {
60                 if (strstr(buf, "==== regions for process"))
61                         break;
62         }
63
64         while (fgets(buf, sizeof(buf), map)) {
65                 char regionname[200] = "";
66                 char type[23];
67                 char smstr[23];
68                 char r, w, x;
69                 char mr, mw, mx;
70                 int size;
71                 void *begin, *end;
72
73                 //Skip out at the end of the section
74                 if (buf[0] == '\n')
75                         break;
76
77                 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);
78
79                 if (w == 'w' && (strstr(regionname, MYBINARYNAME) || strstr(regionname, MYLIBRARYNAME))) {
80                         size_t len = ((uintptr_t)end - (uintptr_t)begin) / PAGESIZE;
81                         if (len != 0)
82                                 snapshot_add_memory_region(begin, len);
83                 }
84         }
85         pclose(map);
86 }
87 #else
88
89 static void get_binary_name(char *buf, size_t len)
90 {
91         if (readlink("/proc/self/exe", buf, len) == -1) {
92                 perror("readlink");
93                 exit(EXIT_FAILURE);
94         }
95 }
96
97 /** The SnapshotGlobalSegments function computes the memory regions
98  *      that may contain globals and then configures the snapshotting
99  *      library to snapshot them.
100  */
101 static void SnapshotGlobalSegments()
102 {
103         char buf[9000];
104         char binary_name[800];
105         FILE *map;
106
107         map = fopen(MAPFILE, "r");
108         if (!map) {
109                 perror("fopen");
110                 exit(EXIT_FAILURE);
111         }
112         get_binary_name(binary_name, sizeof(binary_name));
113         while (fgets(buf, sizeof(buf), map)) {
114                 char regionname[200] = "";
115                 char r, w, x, p;
116                 void *begin, *end;
117
118                 sscanf(buf, "%p-%p %c%c%c%c %*x %*x:%*x %*u %200s\n", &begin, &end, &r, &w, &x, &p, regionname);
119                 if (w == 'w' && (strstr(regionname, binary_name) || strstr(regionname, MYLIBRARYNAME))) {
120                         size_t len = ((uintptr_t)end - (uintptr_t)begin) / PAGESIZE;
121                         if (len != 0)
122                                 snapshot_add_memory_region(begin, len);
123                         DEBUG("%55s: %18p - %18p\t%c%c%c%c\n", regionname, begin, end, r, w, x, p);
124                 }
125         }
126         fclose(map);
127 }
128 #endif
129
130 SnapshotStack::SnapshotStack() : stack()
131 {
132         SnapshotGlobalSegments();
133 }
134
135 SnapshotStack::~SnapshotStack()
136 {
137 }
138
139 /** This method returns to the last snapshot before the inputted
140  * sequence number.  This function must be called from the model
141  * checking thread and not from a snapshotted stack.
142  * @param seqindex is the sequence number to rollback before.
143  * @return is the sequence number we actually rolled back to.
144  */
145 int SnapshotStack::backTrackBeforeStep(int seqindex)
146 {
147         int i;
148         for (i = (int)stack.size() - 1; i >= 0; i++)
149                 if (stack[i].index <= seqindex)
150                         break;
151                 else
152                         stack.pop_back();
153
154         ASSERT(i >= 0);
155         snapshot_roll_back(stack[i].snapshotid);
156         return stack[i].index;
157 }
158
159 /** This method takes a snapshot at the given sequence number. */
160 void SnapshotStack::snapshotStep(int seqindex)
161 {
162         stack.push_back(stackEntry(seqindex, take_snapshot()));
163 }
164
165 void snapshot_stack_init()
166 {
167         snapshotObject = new SnapshotStack();
168 }
169
170 void snapshot_record(int seq_index)
171 {
172         snapshotObject->snapshotStep(seq_index);
173 }
174
175 int snapshot_backtrack_before(int seq_index)
176 {
177         return snapshotObject->backTrackBeforeStep(seq_index);
178 }