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