bug fix with missing coherence condition for promises
[model-checker.git] / main.cc
1 /** @file main.cc
2  *  @brief Entry point for the model checker.
3  */
4
5 #include <unistd.h>
6 #include <getopt.h>
7 #include <string.h>
8
9 #include "common.h"
10 #include "output.h"
11
12 #include "datarace.h"
13
14 /* global "model" object */
15 #include "model.h"
16 #include "params.h"
17 #include "snapshot-interface.h"
18 #include "scanalysis.h"
19 #include "plugins.h"
20
21 static void param_defaults(struct model_params *params)
22 {
23         params->maxreads = 0;
24         params->maxfuturedelay = 6;
25         params->fairwindow = 0;
26         params->yieldon = false;
27         params->yieldblock = false;
28         params->enabledcount = 1;
29         params->bound = 0;
30         params->maxfuturevalues = 0;
31         params->expireslop = 4;
32         params->verbose = !!DBG_ENABLED();
33         params->uninitvalue = 0;
34         params->maxexecutions = 0;
35 }
36
37 static void print_usage(const char *program_name, struct model_params *params)
38 {
39         ModelVector<TraceAnalysis *> * registeredanalysis=getRegisteredTraceAnalysis();
40         /* Reset defaults before printing */
41         param_defaults(params);
42
43         model_print(
44 "Copyright (c) 2013 Regents of the University of California. All rights reserved.\n"
45 "Distributed under the GPLv2\n"
46 "Written by Brian Norris and Brian Demsky\n"
47 "\n"
48 "Usage: %s [MODEL-CHECKER OPTIONS] -- [PROGRAM ARGS]\n"
49 "\n"
50 "MODEL-CHECKER OPTIONS can be any of the model-checker options listed below. Arguments\n"
51 "provided after the `--' (the PROGRAM ARGS) are passed to the user program.\n"
52 "\n"
53 "Model-checker options:\n"
54 "-h, --help                  Display this help message and exit\n"
55 "-m, --liveness=NUM          Maximum times a thread can read from the same write\n"
56 "                              while other writes exist.\n"
57 "                              Default: %d\n"
58 "-M, --maxfv=NUM             Maximum number of future values that can be sent to\n"
59 "                              the same read.\n"
60 "                              Default: %d\n"
61 "-s, --maxfvdelay=NUM        Maximum actions that the model checker will wait for\n"
62 "                              a write from the future past the expected number\n"
63 "                              of actions.\n"
64 "                              Default: %d\n"
65 "-S, --fvslop=NUM            Future value expiration sloppiness.\n"
66 "                              Default: %u\n"
67 "-y, --yield                 Enable CHESS-like yield-based fairness support\n"
68 "                              (requires thrd_yield() in test program).\n"
69 "                              Default: %s\n"
70 "-Y, --yieldblock            Prohibit an execution from running a yield.\n"
71 "                              Default: %s\n"
72 "-f, --fairness=WINDOW       Specify a fairness window in which actions that are\n"
73 "                              enabled sufficiently many times should receive\n"
74 "                              priority for execution (not recommended).\n"
75 "                              Default: %d\n"
76 "-e, --enabled=COUNT         Enabled count.\n"
77 "                              Default: %d\n"
78 "-b, --bound=MAX             Upper length bound.\n"
79 "                              Default: %d\n"
80 "-v[NUM], --verbose[=NUM]    Print verbose execution information. NUM is optional:\n"
81 "                              0 is quiet; 1 shows valid executions; 2 is noisy;\n"
82 "                              3 is noisier.\n"
83 "                              Default: %d\n"
84 "-u, --uninitialized=VALUE   Return VALUE any load which may read from an\n"
85 "                              uninitialized atomic.\n"
86 "                              Default: %u\n"
87 "-t, --analysis=NAME         Use Analysis Plugin.\n"
88 "-o, --options=NAME          Option for previous analysis plugin.  \n"
89 "-x, --maxexec=NUM           Maximum number of executions.\n"
90 "                            Default: %u\n"
91 "                            -o help for a list of options\n"
92 " --                         Program arguments follow.\n\n",
93                 program_name,
94                 params->maxreads,
95                 params->maxfuturevalues,
96                 params->maxfuturedelay,
97                 params->expireslop,
98                 params->yieldon ? "enabled" : "disabled",
99                 params->yieldblock ? "enabled" : "disabled",
100                 params->fairwindow,
101                 params->enabledcount,
102                 params->bound,
103                 params->verbose,
104     params->uninitvalue,
105                 params->maxexecutions);
106         model_print("Analysis plugins:\n");
107         for(unsigned int i=0;i<registeredanalysis->size();i++) {
108                 TraceAnalysis * analysis=(*registeredanalysis)[i];
109                 model_print("%s\n", analysis->name());
110         }
111         exit(EXIT_SUCCESS);
112 }
113
114 bool install_plugin(char * name) {
115         ModelVector<TraceAnalysis *> * registeredanalysis=getRegisteredTraceAnalysis();
116         ModelVector<TraceAnalysis *> * installedanalysis=getInstalledTraceAnalysis();
117
118         for(unsigned int i=0;i<registeredanalysis->size();i++) {
119                 TraceAnalysis * analysis=(*registeredanalysis)[i];
120                 if (strcmp(name, analysis->name())==0) {
121                         installedanalysis->push_back(analysis);
122                         return false;
123                 }
124         }
125         model_print("Analysis %s Not Found\n", name);
126         return true;
127 }
128
129 static void parse_options(struct model_params *params, int argc, char **argv)
130 {
131         const char *shortopts = "hyYt:o:m:M:s:S:f:e:b:u:x:v::";
132         const struct option longopts[] = {
133                 {"help", no_argument, NULL, 'h'},
134                 {"liveness", required_argument, NULL, 'm'},
135                 {"maxfv", required_argument, NULL, 'M'},
136                 {"maxfvdelay", required_argument, NULL, 's'},
137                 {"fvslop", required_argument, NULL, 'S'},
138                 {"fairness", required_argument, NULL, 'f'},
139                 {"yield", no_argument, NULL, 'y'},
140                 {"yieldblock", no_argument, NULL, 'Y'},
141                 {"enabled", required_argument, NULL, 'e'},
142                 {"bound", required_argument, NULL, 'b'},
143                 {"verbose", optional_argument, NULL, 'v'},
144                 {"uninitialized", optional_argument, NULL, 'u'},
145                 {"analysis", optional_argument, NULL, 't'},
146                 {"options", optional_argument, NULL, 'o'},
147                 {"maxexecutions", required_argument, NULL, 'x'},
148                 {0, 0, 0, 0} /* Terminator */
149         };
150         int opt, longindex;
151         bool error = false;
152         while (!error && (opt = getopt_long(argc, argv, shortopts, longopts, &longindex)) != -1) {
153                 switch (opt) {
154                 case 'h':
155                         print_usage(argv[0], params);
156                         break;
157                 case 'x':
158                         params->maxexecutions = atoi(optarg);
159                         break;
160                 case 's':
161                         params->maxfuturedelay = atoi(optarg);
162                         break;
163                 case 'S':
164                         params->expireslop = atoi(optarg);
165                         break;
166                 case 'f':
167                         params->fairwindow = atoi(optarg);
168                         break;
169                 case 'e':
170                         params->enabledcount = atoi(optarg);
171                         break;
172                 case 'b':
173                         params->bound = atoi(optarg);
174                         break;
175                 case 'm':
176                         params->maxreads = atoi(optarg);
177                         break;
178                 case 'M':
179                         params->maxfuturevalues = atoi(optarg);
180                         break;
181                 case 'v':
182                         params->verbose = optarg ? atoi(optarg) : 1;
183                         break;
184                 case 'u':
185                         params->uninitvalue = atoi(optarg);
186                         break;
187                 case 'y':
188                         params->yieldon = true;
189                         break;
190                 case 't':
191                         if (install_plugin(optarg))
192                                 error = true;
193                         break;
194                 case 'o':
195                         {
196                                 ModelVector<TraceAnalysis *> * analyses = getInstalledTraceAnalysis();
197                                 if ( analyses->size() == 0 || (*analyses)[analyses->size()-1]->option(optarg))
198                                         error = true;
199                         }
200                         break;
201                 case 'Y':
202                         params->yieldblock = true;
203                         break;
204                 default: /* '?' */
205                         error = true;
206                         break;
207                 }
208         }
209
210         /* Pass remaining arguments to user program */
211         params->argc = argc - (optind - 1);
212         params->argv = argv + (optind - 1);
213
214         /* Reset program name */
215         params->argv[0] = argv[0];
216
217         /* Reset (global) optind for potential use by user program */
218         optind = 1;
219
220         if (error)
221                 print_usage(argv[0], params);
222 }
223
224 int main_argc;
225 char **main_argv;
226
227 static void install_trace_analyses(ModelExecution *execution)
228 {
229         ModelVector<TraceAnalysis *> * installedanalysis=getInstalledTraceAnalysis();
230         for(unsigned int i=0;i<installedanalysis->size();i++) {
231                 TraceAnalysis * ta=(*installedanalysis)[i];
232                 ta->setExecution(execution);
233                 model->add_trace_analysis(ta);
234         }
235 }
236
237 /** The model_main function contains the main model checking loop. */
238 static void model_main()
239 {
240         struct model_params params;
241
242         param_defaults(&params);
243         register_plugins();
244
245         parse_options(&params, main_argc, main_argv);
246
247         //Initialize race detector
248         initRaceDetector();
249
250         snapshot_stack_init();
251
252         model = new ModelChecker(params);
253         install_trace_analyses(model->get_execution());
254
255         snapshot_record(0);
256         model->run();
257         delete model;
258
259         DEBUG("Exiting\n");
260 }
261
262 /**
263  * Main function.  Just initializes snapshotting library and the
264  * snapshotting library calls the model_main function.
265  */
266 int main(int argc, char **argv)
267 {
268         main_argc = argc;
269         main_argv = argv;
270
271         /* Configure output redirection for the model-checker */
272         redirect_output();
273
274         /* Let's jump in quickly and start running stuff */
275         snapshot_system_init(10000, 1024, 1024, 4000, &model_main);
276 }