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