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