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