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