1026aa9c9bafd6e1ccfdb1326ead297c64b79803
[pspp-builds.git] / src / libpspp / model-checker.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 2007 Free Software Foundation, Inc.
3
4    This program is free software: you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation, either version 3 of the License, or
7    (at your option) any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program.  If not, see <http://www.gnu.org/licenses/>. */
16
17 #include <config.h>
18
19 #include <libpspp/model-checker.h>
20
21 #include <limits.h>
22 #include <signal.h>
23 #include <stdarg.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <sys/time.h>
28
29 #include <libpspp/bit-vector.h>
30 #include <libpspp/compiler.h>
31 #include <libpspp/deque.h>
32 #include <libpspp/str.h>
33 #include <math/moments.h>
34
35 #include "error.h"
36 #include "minmax.h"
37 #include "xalloc.h"
38 \f
39 /* Initializes PATH as an empty path. */
40 void
41 mc_path_init (struct mc_path *path)
42 {
43   path->ops = NULL;
44   path->length = 0;
45   path->capacity = 0;
46 }
47
48 /* Copies the contents of OLD into NEW. */
49 void
50 mc_path_copy (struct mc_path *new, const struct mc_path *old)
51 {
52   if (old->length > new->capacity)
53     {
54       new->capacity = old->length;
55       free (new->ops);
56       new->ops = xnmalloc (new->capacity, sizeof *new->ops);
57     }
58   new->length = old->length;
59   memcpy (new->ops, old->ops, old->length * sizeof *new->ops);
60 }
61
62 /* Adds OP to the end of PATH. */
63 void
64 mc_path_push (struct mc_path *path, int op)
65 {
66   if (path->length >= path->capacity)
67     path->ops = xnrealloc (path->ops, ++path->capacity, sizeof *path->ops);
68   path->ops[path->length++] = op;
69 }
70
71 /* Removes and returns the operation at the end of PATH. */
72 int
73 mc_path_pop (struct mc_path *path)
74 {
75   int back = mc_path_back (path);
76   path->length--;
77   return back;
78 }
79
80 /* Returns the operation at the end of PATH. */
81 int
82 mc_path_back (const struct mc_path *path)
83 {
84   assert (path->length > 0);
85   return path->ops[path->length - 1];
86 }
87
88 /* Destroys PATH. */
89 void
90 mc_path_destroy (struct mc_path *path)
91 {
92   free (path->ops);
93   path->ops = NULL;
94 }
95
96 /* Returns the operation in position INDEX in PATH.
97    INDEX must be less than the length of PATH. */
98 int
99 mc_path_get_operation (const struct mc_path *path, size_t index)
100 {
101   assert (index < path->length);
102   return path->ops[index];
103 }
104
105 /* Returns the number of operations in PATH. */
106 size_t
107 mc_path_get_length (const struct mc_path *path)
108 {
109   return path->length;
110 }
111
112 /* Appends the operations in PATH to STRING, separating each one
113    with a single space. */
114 void
115 mc_path_to_string (const struct mc_path *path, struct string *string)
116 {
117   size_t i;
118
119   for (i = 0; i < mc_path_get_length (path); i++)
120     {
121       if (i > 0)
122         ds_put_char (string, ' ');
123       ds_put_format (string, "%d", mc_path_get_operation (path, i));
124     }
125 }
126 \f
127 /* Search options. */
128 struct mc_options
129   {
130     /* Search strategy. */
131     enum mc_strategy strategy;          /* Type of strategy. */
132     int max_depth;                      /* Limit on depth (or INT_MAX). */
133     int hash_bits;                      /* Number of bits to hash (or 0). */
134     unsigned int seed;                  /* Random seed for MC_RANDOM
135                                            or MC_DROP_RANDOM. */
136     struct mc_path follow_path;         /* Path for MC_PATH. */
137
138     /* Queue configuration. */
139     int queue_limit;                    /* Maximum length of queue. */
140     enum mc_queue_limit_strategy queue_limit_strategy;
141                                         /* How to choose state to drop
142                                            from queue. */
143
144     /* Stop conditions. */
145     int max_unique_states;              /* Maximum unique states to process. */
146     int max_errors;                     /* Maximum errors to detect. */
147     double time_limit;                  /* Maximum time in seconds. */
148
149     /* Output configuration. */
150     int verbosity;                      /* 0=low, 1=normal, 2+=high. */
151     int failure_verbosity;              /* If greater than verbosity,
152                                            verbosity of error replays. */
153     FILE *output_file;                  /* File to receive output. */
154
155     /* How to report intermediate progress. */
156     int progress_usec;                  /* Microseconds between reports. */
157     mc_progress_func *progress_func;    /* Function to call on each report. */
158
159     /* Client data. */
160     void *aux;
161   };
162
163 /* Default progress function. */
164 static bool
165 default_progress (struct mc *mc)
166 {
167   if (mc_results_get_stop_reason (mc_get_results (mc)) == MC_CONTINUING)
168     putc ('.', stderr);
169   else
170     putc ('\n', stderr);
171   return true;
172 }
173
174 /* Do-nothing progress function. */
175 static bool
176 null_progress (struct mc *mc UNUSED)
177 {
178   return true;
179 }
180
181 /* Creates and returns a set of options initialized to the
182    defaults. */
183 struct mc_options *
184 mc_options_create (void)
185 {
186   struct mc_options *options = xmalloc (sizeof *options);
187
188   options->strategy = MC_BROAD;
189   options->max_depth = INT_MAX;
190   options->hash_bits = 20;
191   options->seed = 0;
192   mc_path_init (&options->follow_path);
193
194   options->queue_limit = 10000;
195   options->queue_limit_strategy = MC_DROP_RANDOM;
196
197   options->max_unique_states = INT_MAX;
198   options->max_errors = 1;
199   options->time_limit = 0.0;
200
201   options->verbosity = 1;
202   options->failure_verbosity = 2;
203   options->output_file = stdout;
204   options->progress_usec = 250000;
205   options->progress_func = default_progress;
206
207   options->aux = NULL;
208
209   return options;
210 }
211
212 /* Returns a copy of the given OPTIONS. */
213 struct mc_options *
214 mc_options_clone (const struct mc_options *options)
215 {
216   return xmemdup (options, sizeof *options);
217 }
218
219 /* Destroys OPTIONS. */
220 void
221 mc_options_destroy (struct mc_options *options)
222 {
223   mc_path_destroy (&options->follow_path);
224   free (options);
225 }
226
227 /* Returns the search strategy used for OPTIONS.  The choices
228    are:
229
230    - MC_BROAD (the default): Breadth-first search.  First tries
231      all the operations with depth 1, then those with depth 2,
232      then those with depth 3, and so on.
233
234      This search algorithm finds the least number of operations
235      needed to trigger a given bug.
236
237    - MC_DEEP: Depth-first search.  Searches downward in the tree
238      of states as fast as possible.  Good for finding bugs that
239      require long sequences of operations to trigger.
240
241    - MC_RANDOM: Random-first search.  Searches through the tree
242      of states in random order.  The standard C library's rand
243      function selects the search path; you can control the seed
244      passed to srand using mc_options_set_seed.
245
246    - MC_PATH: Explicit path.  Applies an explicitly specified
247      sequence of operations. */
248 enum mc_strategy
249 mc_options_get_strategy (const struct mc_options *options)
250 {
251   return options->strategy;
252 }
253
254 /* Sets the search strategy used for OPTIONS to STRATEGY.
255
256    This function cannot be used to set MC_PATH as the search
257    strategy.  Use mc_options_set_follow_path instead. */
258 void
259 mc_options_set_strategy (struct mc_options *options, enum mc_strategy strategy)
260 {
261   assert (strategy == MC_BROAD
262           || strategy == MC_DEEP
263           || strategy == MC_RANDOM);
264   options->strategy = strategy;
265 }
266
267 /* Returns OPTION's random seed used by MC_RANDOM and
268    MC_DROP_RANDOM. */
269 unsigned int
270 mc_options_get_seed (const struct mc_options *options)
271 {
272   return options->seed;
273 }
274
275 /* Set OPTION's random seed used by MC_RANDOM and MC_DROP_RANDOM
276    to SEED. */
277 void
278 mc_options_set_seed (struct mc_options *options, unsigned int seed)
279 {
280   options->seed = seed;
281 }
282
283 /* Returns the maximum depth to which OPTIONS's search will
284    descend.  The initial states are at depth 1, states produced
285    as their mutations are at depth 2, and so on. */
286 int
287 mc_options_get_max_depth (const struct mc_options *options)
288 {
289   return options->max_depth;
290 }
291
292 /* Sets the maximum depth to which OPTIONS's search will descend
293    to MAX_DEPTH.  The initial states are at depth 1, states
294    produced as their mutations are at depth 2, and so on. */
295 void
296 mc_options_set_max_depth (struct mc_options *options, int max_depth)
297 {
298   options->max_depth = max_depth;
299 }
300
301 /* Returns the base-2 log of the number of bits in OPTIONS's hash
302    table.  The hash table is used for dropping states that are
303    probably duplicates: any state with a given hash value, as
304    will only be processed once.  A return value of 0 indicates
305    that the model checker will not discard duplicate states based
306    on their hashes.
307
308    The hash table is a power of 2 bits long, by default 2**20
309    bits (128 kB).  Depending on how many states you expect the
310    model checker to check, how much memory you're willing to let
311    the hash table take up, and how worried you are about missing
312    states due to hash collisions, you could make it larger or
313    smaller.
314
315    The "birthday paradox" points to a reasonable way to size your
316    hash table.  If you expect the model checker to check about
317    2**N states, then, assuming a perfect hash, you need a hash
318    table of 2**(N+1) bits to have a 50% chance of seeing a hash
319    collision, 2**(N+2) bits to have a 25% chance, and so on. */
320 int
321 mc_options_get_hash_bits (const struct mc_options *options)
322 {
323   return options->hash_bits;
324 }
325
326 /* Sets the base-2 log of the number of bits in OPTIONS's hash
327    table to HASH_BITS.  A HASH_BITS value of 0 requests that the
328    model checker not discard duplicate states based on their
329    hashes.  (This causes the model checker to never terminate in
330    many cases.) */
331 void
332 mc_options_set_hash_bits (struct mc_options *options, int hash_bits)
333 {
334   assert (hash_bits >= 0);
335   options->hash_bits = MIN (hash_bits, CHAR_BIT * sizeof (unsigned int) - 1);
336 }
337
338 /* Returns the path set in OPTIONS by mc_options_set_follow_path.
339    May be used only if the search strategy is MC_PATH. */
340 const struct mc_path *
341 mc_options_get_follow_path (const struct mc_options *options)
342 {
343   assert (options->strategy == MC_PATH);
344   return &options->follow_path;
345 }
346
347 /* Sets, in OPTIONS, the search algorithm to MC_PATH and the path
348    to be the explicit path specified in FOLLOW_PATH. */
349 void
350 mc_options_set_follow_path (struct mc_options *options,
351                             const struct mc_path *follow_path)
352 {
353   assert (mc_path_get_length (follow_path) > 0);
354   options->strategy = MC_PATH;
355   mc_path_copy (&options->follow_path, follow_path);
356 }
357
358 /* Returns the maximum number of queued states in OPTIONS.  The
359    default value is 10,000.  The primary reason to limit the
360    number of queued states is to conserve memory, so if you can
361    afford the memory and your model needs more room in the queue,
362    you can raise the limit.  Conversely, if your models are large
363    or memory is constrained, you can reduce the limit.
364
365    Following the execution of the model checker, you can find out
366    the maximum queue length during the run by calling
367    mc_results_get_max_queue_length. */
368 int
369 mc_options_get_queue_limit (const struct mc_options *options)
370 {
371   return options->queue_limit;
372 }
373
374 /* Sets the maximum number of queued states in OPTIONS to
375    QUEUE_LIMIT.  */
376 void
377 mc_options_set_queue_limit (struct mc_options *options, int queue_limit)
378 {
379   assert (queue_limit > 0);
380   options->queue_limit = queue_limit;
381 }
382
383 /* Returns the queue limit strategy used by OPTIONS, that is,
384    when a new state must be inserted into a full state queue is
385    full, how the state to be dropped is chosen.  The choices are:
386
387    - MC_DROP_NEWEST: Drop the newest state; that is, do not
388      insert the new state into the queue at all.
389
390    - MC_DROP_OLDEST: Drop the state that has been enqueued for
391      the longest.
392
393    - MC_DROP_RANDOM (the default): Drop a randomly selected state
394      from the queue.  The standard C library's rand function
395      selects the state to drop; you can control the seed passed
396      to srand using mc_options_set_seed. */
397 enum mc_queue_limit_strategy
398 mc_options_get_queue_limit_strategy (const struct mc_options *options)
399 {
400   return options->queue_limit_strategy;
401 }
402
403 /* Sets the queue limit strategy used by OPTIONS to STRATEGY.
404
405    This setting has no effect unless the model being checked
406    causes the state queue to overflow (see
407    mc_options_get_queue_limit). */
408 void
409 mc_options_set_queue_limit_strategy (struct mc_options *options,
410                                      enum mc_queue_limit_strategy strategy)
411 {
412   assert (strategy == MC_DROP_NEWEST
413           || strategy == MC_DROP_OLDEST
414           || strategy == MC_DROP_RANDOM);
415   options->queue_limit_strategy = strategy;
416 }
417
418 /* Returns OPTIONS's maximum number of unique states that the
419    model checker will examine before terminating.  The default is
420    INT_MAX. */
421 int
422 mc_options_get_max_unique_states (const struct mc_options *options)
423 {
424   return options->max_unique_states;
425 }
426
427 /* Sets OPTIONS's maximum number of unique states that the model
428    checker will examine before terminating to
429    MAX_UNIQUE_STATE. */
430 void
431 mc_options_set_max_unique_states (struct mc_options *options,
432                                   int max_unique_states)
433 {
434   options->max_unique_states = max_unique_states;
435 }
436
437 /* Returns the maximum number of errors that OPTIONS will allow
438    the model checker to encounter before terminating.  The
439    default is 1. */
440 int
441 mc_options_get_max_errors (const struct mc_options *options)
442 {
443   return options->max_errors;
444 }
445
446 /* Sets the maximum number of errors that OPTIONS will allow the
447    model checker to encounter before terminating to
448    MAX_ERRORS. */
449 void
450 mc_options_set_max_errors (struct mc_options *options, int max_errors)
451 {
452   options->max_errors = max_errors;
453 }
454
455 /* Returns the maximum amount of time, in seconds, that OPTIONS will allow the
456    model checker to consume before terminating.  The
457    default of 0.0 means that time consumption is unlimited. */
458 double
459 mc_options_get_time_limit (const struct mc_options *options)
460 {
461   return options->time_limit;
462 }
463
464 /* Sets the maximum amount of time, in seconds, that OPTIONS will
465    allow the model checker to consume before terminating to
466    TIME_LIMIT.  A value of 0.0 means that time consumption is
467    unlimited; otherwise, the return value will be positive. */
468 void
469 mc_options_set_time_limit (struct mc_options *options, double time_limit)
470 {
471   assert (time_limit >= 0.0);
472   options->time_limit = time_limit;
473 }
474
475 /* Returns the level of verbosity for output messages specified
476    by OPTIONS.  The default verbosity level is 1.
477
478    A verbosity level of 0 inhibits all messages except for
479    errors; a verbosity level of 1 also allows warnings; a
480    verbosity level of 2 also causes a description of each state
481    added to be output; a verbosity level of 3 also causes a
482    description of each duplicate state to be output.  Verbosity
483    levels less than 0 or greater than 3 are allowed but currently
484    have no additional effect. */
485 int
486 mc_options_get_verbosity (const struct mc_options *options)
487 {
488   return options->verbosity;
489 }
490
491 /* Sets the level of verbosity for output messages specified
492    by OPTIONS to VERBOSITY. */
493 void
494 mc_options_set_verbosity (struct mc_options *options, int verbosity)
495 {
496   options->verbosity = verbosity;
497 }
498
499 /* Returns the level of verbosity for failures specified by
500    OPTIONS.  The default failure verbosity level is 2.
501
502    The failure verbosity level has an effect only when an error
503    is reported, and only when the failure verbosity level is
504    higher than the regular verbosity level.  When this is the
505    case, the model checker replays the error path at the higher
506    verbosity level specified.  This has the effect of outputting
507    an explicit, human-readable description of the sequence of
508    operations that caused the error. */
509 int
510 mc_options_get_failure_verbosity (const struct mc_options *options)
511 {
512   return options->failure_verbosity;
513 }
514
515 /* Sets the level of verbosity for failures specified by OPTIONS
516    to FAILURE_VERBOSITY. */
517 void
518 mc_options_set_failure_verbosity (struct mc_options *options,
519                                   int failure_verbosity)
520 {
521   options->failure_verbosity = failure_verbosity;
522 }
523
524 /* Returns the output file used for messages printed by the model
525    checker specified by OPTIONS.  The default is stdout. */
526 FILE *
527 mc_options_get_output_file (const struct mc_options *options)
528 {
529   return options->output_file;
530 }
531
532 /* Sets the output file used for messages printed by the model
533    checker specified by OPTIONS to OUTPUT_FILE.
534
535    The model checker does not automatically close the specified
536    output file.  If this is desired, the model checker's client
537    must do so. */
538 void
539 mc_options_set_output_file (struct mc_options *options,
540                             FILE *output_file)
541 {
542   options->output_file = output_file;
543 }
544
545 /* Returns the number of microseconds between calls to the
546    progress function specified by OPTIONS.   The default is
547    250,000 (1/4 second).  A value of 0 disables progress
548    reporting. */
549 int
550 mc_options_get_progress_usec (const struct mc_options *options)
551 {
552   return options->progress_usec;
553 }
554
555 /* Sets the number of microseconds between calls to the progress
556    function specified by OPTIONS to PROGRESS_USEC.  A value of 0
557    disables progress reporting. */
558 void
559 mc_options_set_progress_usec (struct mc_options *options, int progress_usec)
560 {
561   assert (progress_usec >= 0);
562   options->progress_usec = progress_usec;
563 }
564
565 /* Returns the function called to report progress specified by
566    OPTIONS.  The function used by default prints '.' to
567    stderr. */
568 mc_progress_func *
569 mc_options_get_progress_func (const struct mc_options *options)
570 {
571   return options->progress_func;
572 }
573
574 /* Sets the function called to report progress specified by
575    OPTIONS to PROGRESS_FUNC.  A non-null function must be
576    specified; to disable progress reporting, set the progress
577    reporting interval to 0.
578
579    PROGRESS_FUNC will be called zero or more times while the
580    model checker's run is ongoing.  For these calls to the
581    progress function, mc_results_get_stop_reason will return
582    MC_CONTINUING.  It will also be called exactly once soon
583    before mc_run returns, in which case
584    mc_results_get_stop_reason will return a different value. */
585 void
586 mc_options_set_progress_func (struct mc_options *options,
587                               mc_progress_func *progress_func)
588 {
589   assert (options->progress_func != NULL);
590   options->progress_func = progress_func;
591 }
592
593 /* Returns the auxiliary data set in OPTIONS by the client.  The
594    default is a null pointer.
595
596    This auxiliary data value can be retrieved by the
597    client-specified functions in struct mc_class during a model
598    checking run using mc_get_aux. */
599 void *
600 mc_options_get_aux (const struct mc_options *options)
601 {
602   return options->aux;
603 }
604
605 /* Sets the auxiliary data in OPTIONS to AUX. */
606 void
607 mc_options_set_aux (struct mc_options *options, void *aux)
608 {
609   options->aux = aux;
610 }
611 \f
612 /* Results of a model checking run. */
613 struct mc_results
614   {
615     /* Overall results. */
616     enum mc_stop_reason stop_reason;    /* Why the run ended. */
617     int unique_state_count;             /* Number of unique states checked. */
618     int error_count;                    /* Number of errors found. */
619
620     /* Depth statistics. */
621     int max_depth_reached;              /* Max depth state examined. */
622     struct moments1 *depth_moments;     /* Enables reporting mean depth. */
623
624     /* If error_count > 0, path to the last error reported. */
625     struct mc_path error_path;
626
627     /* States dropped... */
628     int duplicate_dropped_states;       /* ...as duplicates. */
629     int off_path_dropped_states;        /* ...as off-path (MC_PATH only). */
630     int depth_dropped_states;           /* ...due to excessive depth. */
631     int queue_dropped_states;           /* ...due to queue overflow. */
632
633     /* Queue statistics. */
634     int queued_unprocessed_states;      /* Enqueued but never dequeued. */
635     int max_queue_length;               /* Maximum queue length observed. */
636
637     /* Timing. */
638     struct timeval start;               /* Start of model checking run. */
639     struct timeval end;                 /* End of model checking run. */
640   };
641
642 /* Creates, initializes, and returns a new set of results. */
643 static struct mc_results *
644 mc_results_create (void)
645 {
646   struct mc_results *results = xcalloc (1, sizeof (struct mc_results));
647   results->stop_reason = MC_CONTINUING;
648   results->depth_moments = moments1_create (MOMENT_MEAN);
649   gettimeofday (&results->start, NULL);
650   return results;
651 }
652
653 /* Destroys RESULTS. */
654 void
655 mc_results_destroy (struct mc_results *results)
656 {
657   if (results != NULL)
658     {
659       moments1_destroy (results->depth_moments);
660       mc_path_destroy (&results->error_path);
661       free (results);
662     }
663 }
664
665 /* Returns RESULTS's reason that the model checking run
666    terminated.  The possible reasons are:
667
668    - MC_CONTINUING: The run is not actually yet complete.  This
669      can only be returned before mc_run has returned, e.g. when
670      the progress function set by mc_options_set_progress_func
671      examines the run's results.
672
673    - MC_SUCCESS: The run completed because the queue emptied.
674      The entire state space might not have been explored due to a
675      requested limit on maximum depth, hash collisions, etc.
676
677    - MC_MAX_UNIQUE_STATES: The run completed because as many
678      unique states have been checked as were requested (using
679      mc_options_set_max_unique_states).
680
681    - MC_MAX_ERROR_COUNT: The run completed because the maximum
682      requested number of errors (by default, 1 error) was
683      reached.
684
685    - MC_END_OF_PATH: The run completed because the path specified
686      with mc_options_set_follow_path was fully traversed.
687
688    - MC_TIMEOUT: The run completed because the time limit set
689      with mc_options_set_time_limit was exceeded.
690
691    - MC_INTERRUPTED: The run completed because SIGINT was caught
692      (typically, due to the user typing Ctrl+C). */
693 enum mc_stop_reason
694 mc_results_get_stop_reason (const struct mc_results *results)
695 {
696   return results->stop_reason;
697 }
698
699 /* Returns the number of unique states checked specified by
700    RESULTS. */
701 int
702 mc_results_get_unique_state_count (const struct mc_results *results)
703 {
704   return results->unique_state_count;
705 }
706
707 /* Returns the number of errors found specified by RESULTS. */
708 int
709 mc_results_get_error_count (const struct mc_results *results)
710 {
711   return results->error_count;
712 }
713
714 /* Returns the maximum depth reached during the model checker run
715    represented by RESULTS.  The initial states are at depth 1,
716    their child states at depth 2, and so on. */
717 int
718 mc_results_get_max_depth_reached (const struct mc_results *results)
719 {
720   return results->max_depth_reached;
721 }
722
723 /* Returns the mean depth reached during the model checker run
724    represented by RESULTS. */
725 double
726 mc_results_get_mean_depth_reached (const struct mc_results *results)
727 {
728   double mean;
729   moments1_calculate (results->depth_moments, NULL, &mean, NULL, NULL, NULL);
730   return mean != SYSMIS ? mean : 0.0;
731 }
732
733 /* Returns the path traversed to obtain the last error
734    encountered during the model checker run represented by
735    RESULTS.  Returns a null pointer if the run did not report any
736    errors. */
737 const struct mc_path *
738 mc_results_get_error_path (const struct mc_results *results)
739 {
740   return results->error_count > 0 ? &results->error_path : NULL;
741 }
742
743 /* Returns the number of states dropped as duplicates (based on
744    hash value) during the model checker run represented by
745    RESULTS. */
746 int
747 mc_results_get_duplicate_dropped_states (const struct mc_results *results)
748 {
749   return results->duplicate_dropped_states;
750 }
751
752 /* Returns the number of states dropped because they were off the
753    path specified by mc_options_set_follow_path during the model
754    checker run represented by RESULTS.  A nonzero value here
755    indicates a missing call to mc_include_state in the
756    client-supplied mutation function. */
757 int
758 mc_results_get_off_path_dropped_states (const struct mc_results *results)
759 {
760   return results->off_path_dropped_states;
761 }
762
763 /* Returns the number of states dropped because their depth
764    exceeded the maximum specified with mc_options_set_max_depth
765    during the model checker run represented by RESULTS. */
766 int
767 mc_results_get_depth_dropped_states (const struct mc_results *results)
768 {
769   return results->depth_dropped_states;
770 }
771
772 /* Returns the number of states dropped from the queue due to
773    queue overflow during the model checker run represented by
774    RESULTS. */
775 int
776 mc_results_get_queue_dropped_states (const struct mc_results *results)
777 {
778   return results->queue_dropped_states;
779 }
780
781 /* Returns the number of states that were checked and enqueued
782    but never dequeued and processed during the model checker run
783    represented by RESULTS.  This is zero if the stop reason is
784    MC_CONTINUING or MC_SUCCESS; otherwise, it is the number of
785    states in the queue at the time that the checking run
786    stopped. */
787 int
788 mc_results_get_queued_unprocessed_states (const struct mc_results *results)
789 {
790   return results->queued_unprocessed_states;
791 }
792
793 /* Returns the maximum length of the queue during the model
794    checker run represented by RESULTS.  If this is equal to the
795    maximum queue length, then the queue (probably) overflowed
796    during the run; otherwise, it did not overflow. */
797 int
798 mc_results_get_max_queue_length (const struct mc_results *results)
799 {
800   return results->max_queue_length;
801 }
802
803 /* Returns the time at which the model checker run represented by
804    RESULTS started. */
805 struct timeval
806 mc_results_get_start (const struct mc_results *results)
807 {
808   return results->start;
809 }
810
811 /* Returns the time at which the model checker run represented by
812    RESULTS ended.  (This function may not be called while the run
813    is still ongoing.) */
814 struct timeval
815 mc_results_get_end (const struct mc_results *results)
816 {
817   assert (results->stop_reason != MC_CONTINUING);
818   return results->end;
819 }
820
821 /* Returns the number of seconds obtained by subtracting time Y
822    from time X. */
823 static double
824 timeval_subtract (struct timeval x, struct timeval y)
825 {
826   /* From libc.info. */
827   double difference;
828
829   /* Perform the carry for the later subtraction by updating Y. */
830   if (x.tv_usec < y.tv_usec) {
831     int nsec = (y.tv_usec - x.tv_usec) / 1000000 + 1;
832     y.tv_usec -= 1000000 * nsec;
833     y.tv_sec += nsec;
834   }
835   if (x.tv_usec - y.tv_usec > 1000000) {
836     int nsec = (x.tv_usec - y.tv_usec) / 1000000;
837     y.tv_usec += 1000000 * nsec;
838     y.tv_sec -= nsec;
839   }
840
841   /* Compute the time remaining to wait.
842      `tv_usec' is certainly positive. */
843   difference = (x.tv_sec - y.tv_sec) + (x.tv_usec - y.tv_usec) / 1000000.0;
844   if (x.tv_sec < y.tv_sec)
845     difference = -difference;
846   return difference;
847 }
848
849
850 /* Returns the duration, in seconds, of the model checker run
851    represented by RESULTS.  (This function may not be called
852    while the run is still ongoing.) */
853 double
854 mc_results_get_duration (const struct mc_results *results)
855 {
856   assert (results->stop_reason != MC_CONTINUING);
857   return timeval_subtract (results->end, results->start);
858 }
859 \f
860 /* An active model checking run. */
861 struct mc
862   {
863     /* Related data structures. */
864     const struct mc_class *class;
865     struct mc_options *options;
866     struct mc_results *results;
867
868     /* Array of 2**(options->hash_bits) bits representing states
869        already visited. */
870     unsigned char *hash;
871
872     /* State queue. */
873     struct mc_state **queue;            /* Array of pointers to states. */
874     struct deque queue_deque;           /* Deque. */
875
876     /* State currently being built by "init" or "mutate". */
877     struct mc_path path;                /* Path to current state. */
878     struct string path_string;          /* Buffer for path_string function. */
879     bool state_named;                   /* mc_name_operation called? */
880     bool state_error;                   /* mc_error called? */
881
882     /* Statistics for calling the progress function. */
883     unsigned int progress;              /* Current progress value. */
884     unsigned int next_progress;         /* Next value to call progress func. */
885     unsigned int prev_progress;         /* Last value progress func called. */
886     struct timeval prev_progress_time;  /* Last time progress func called. */
887
888     /* Information for handling and restoring SIGINT. */
889     bool interrupted;                   /* SIGINT received? */
890     bool *saved_interrupted_ptr;        /* Saved value of interrupted_ptr. */
891     void (*saved_sigint) (int);         /* Saved SIGINT handler. */
892   };
893
894 /* A state in the queue. */
895 struct mc_state
896   {
897     struct mc_path path;                /* Path to this state. */
898     void *data;                         /* Client-supplied data. */
899   };
900
901 /* Points to the current struct mc's "interrupted" member. */
902 static bool *interrupted_ptr = NULL;
903
904 static const char *path_string (struct mc *);
905 static void free_state (const struct mc *, struct mc_state *);
906 static void stop (struct mc *, enum mc_stop_reason);
907 static struct mc_state *make_state (const struct mc *, void *);
908 static size_t random_queue_index (struct mc *);
909 static void enqueue_state (struct mc *, struct mc_state *);
910 static void do_error_state (struct mc *);
911 static void next_operation (struct mc *);
912 static bool is_off_path (const struct mc *);
913 static void sigint_handler (int signum);
914 static void init_mc (struct mc *,
915                      const struct mc_class *, struct mc_options *);
916 static void finish_mc (struct mc *);
917
918 /* Runs the model checker on the client-specified CLASS with the
919    client-specified OPTIONS.  OPTIONS may be a null pointer if
920    the defaults are acceptable.  Destroys OPTIONS; use
921    mc_options_clone if a copy is needed.
922
923    Returns the results of the model checking run, which must be
924    destroyed by the client with mc_results_destroy.
925
926    To pass auxiliary data to the functions in CLASS, use
927    mc_options_set_aux on OPTIONS, which may be retrieved from the
928    CLASS functions using mc_get_aux. */
929 struct mc_results *
930 mc_run (const struct mc_class *class, struct mc_options *options)
931 {
932   struct mc mc;
933
934   init_mc (&mc, class, options);
935   while (!deque_is_empty (&mc.queue_deque)
936          && mc.results->stop_reason == MC_CONTINUING)
937     {
938       struct mc_state *state = mc.queue[deque_pop_front (&mc.queue_deque)];
939       mc_path_copy (&mc.path, &state->path);
940       mc_path_push (&mc.path, 0);
941       class->mutate (&mc, state->data);
942       free_state (&mc, state);
943       if (mc.interrupted)
944         stop (&mc, MC_INTERRUPTED);
945     }
946   finish_mc (&mc);
947
948   return mc.results;
949 }
950
951 /* Tests whether the current operation is one that should be
952    performed, checked, and enqueued.  If so, returns true.
953    Otherwise, returns false and, unless checking is stopped,
954    advances to the next state.  The caller should then advance
955    to the next operation.
956
957    This function should be called from the client-provided
958    "mutate" function, according to the pattern explained in the
959    big comment at the top of model-checker.h. */
960 bool
961 mc_include_state (struct mc *mc)
962 {
963   if (mc->results->stop_reason != MC_CONTINUING)
964     return false;
965   else if (is_off_path (mc))
966     {
967       next_operation (mc);
968       return false;
969     }
970   else
971     return true;
972 }
973
974 /* Tests whether HASH represents a state that has (probably)
975    already been enqueued.  If not, returns false and marks HASH
976    so that it will be treated as a duplicate in the future.  If
977    so, returns true and advances to the next state.  The
978    caller should then advance to the next operation.
979
980    This function should be called from the client-provided
981    "mutate" function, according to the pattern explained in the
982    big comment at the top of model-checker.h. */
983 bool
984 mc_discard_dup_state (struct mc *mc, unsigned int hash)
985 {
986   if (mc->options->hash_bits > 0)
987     {
988       hash &= (1u << mc->options->hash_bits) - 1;
989       if (TEST_BIT (mc->hash, hash))
990         {
991           if (mc->options->verbosity > 2)
992             fprintf (mc->options->output_file,
993                      "    [%s] discard duplicate state\n", path_string (mc));
994           mc->results->duplicate_dropped_states++;
995           next_operation (mc);
996           return true;
997         }
998       SET_BIT (mc->hash, hash);
999     }
1000   return false;
1001 }
1002
1003 /* Names the current state NAME, which may contain
1004    printf-style format specifications.  NAME should be a
1005    human-readable name for the current operation.
1006
1007    This function should be called from the client-provided
1008    "mutate" function, according to the pattern explained in the
1009    big comment at the top of model-checker.h. */
1010 void
1011 mc_name_operation (struct mc *mc, const char *name, ...)
1012 {
1013   va_list args;
1014
1015   va_start (args, name);
1016   mc_vname_operation (mc, name, args);
1017   va_end (args);
1018 }
1019
1020 /* Names the current state NAME, which may contain
1021    printf-style format specifications, for which the
1022    corresponding arguments must be given in ARGS.  NAME should be
1023    a human-readable name for the current operation.
1024
1025    This function should be called from the client-provided
1026    "mutate" function, according to the pattern explained in the
1027    big comment at the top of model-checker.h. */
1028 void
1029 mc_vname_operation (struct mc *mc, const char *name, va_list args)
1030 {
1031   if (mc->state_named && mc->options->verbosity > 0)
1032     fprintf (mc->options->output_file, "  [%s] warning: duplicate call "
1033              "to mc_name_operation (missing call to mc_add_state?)\n",
1034              path_string (mc));
1035   mc->state_named = true;
1036
1037   if (mc->options->verbosity > 1)
1038     {
1039       fprintf (mc->options->output_file, "  [%s] ", path_string (mc));
1040       vfprintf (mc->options->output_file, name, args);
1041       putc ('\n', mc->options->output_file);
1042     }
1043 }
1044
1045 /* Reports the given error MESSAGE for the current operation.
1046    The resulting state should still be passed to mc_add_state
1047    when all relevant error messages have been issued.  The state
1048    will not, however, be enqueued for later mutation of its own.
1049
1050    By default, model checking stops after the first error
1051    encountered.
1052
1053    This function should be called from the client-provided
1054    "mutate" function, according to the pattern explained in the
1055    big comment at the top of model-checker.h. */
1056 void
1057 mc_error (struct mc *mc, const char *message, ...)
1058 {
1059   va_list args;
1060
1061   if (mc->results->stop_reason != MC_CONTINUING)
1062     return;
1063
1064   if (mc->options->verbosity > 1)
1065     fputs ("    ", mc->options->output_file);
1066   fprintf (mc->options->output_file, "[%s] error: ",
1067            path_string (mc));
1068   va_start (args, message);
1069   vfprintf (mc->options->output_file, message, args);
1070   va_end (args);
1071   putc ('\n', mc->options->output_file);
1072
1073   mc->state_error = true;
1074 }
1075
1076 /* Enqueues DATA as the state corresponding to the current
1077    operation.  The operation should have been named with a call
1078    to mc_name_operation, and it should have been checked by the
1079    caller (who should have reported any errors with mc_error).
1080
1081    This function should be called from the client-provided
1082    "mutate" function, according to the pattern explained in the
1083    big comment at the top of model-checker.h. */
1084 void
1085 mc_add_state (struct mc *mc, void *data)
1086 {
1087   if (!mc->state_named && mc->options->verbosity > 0)
1088     fprintf (mc->options->output_file, "  [%s] warning: unnamed state\n",
1089              path_string (mc));
1090
1091   if (mc->results->stop_reason != MC_CONTINUING)
1092     {
1093       /* Nothing to do. */
1094     }
1095   else if (mc->state_error)
1096     do_error_state (mc);
1097   else if (is_off_path (mc))
1098     mc->results->off_path_dropped_states++;
1099   else if (mc->path.length + 1 > mc->options->max_depth)
1100     mc->results->depth_dropped_states++;
1101   else
1102     {
1103       /* This is the common case. */
1104       mc->results->unique_state_count++;
1105       if (mc->results->unique_state_count >= mc->options->max_unique_states)
1106         stop (mc, MC_MAX_UNIQUE_STATES);
1107       enqueue_state (mc, make_state (mc, data));
1108       next_operation (mc);
1109       return;
1110     }
1111
1112   mc->class->destroy (mc, data);
1113   next_operation (mc);
1114 }
1115
1116 /* Returns the options that were passed to mc_run for model
1117    checker MC. */
1118 const struct mc_options *
1119 mc_get_options (const struct mc *mc)
1120 {
1121   return mc->options;
1122 }
1123
1124 /* Returns the current state of the results for model checker
1125    MC.  This function is appropriate for use from the progress
1126    function set by mc_options_set_progress_func.
1127
1128    Not all of the results are meaningful before model checking
1129    completes. */
1130 const struct mc_results *
1131 mc_get_results (const struct mc *mc)
1132 {
1133   return mc->results;
1134 }
1135
1136 /* Returns the auxiliary data set on the options passed to mc_run
1137    with mc_options_set_aux. */
1138 void *
1139 mc_get_aux (const struct mc *mc)
1140 {
1141   return mc_options_get_aux (mc_get_options (mc));
1142 }
1143 \f
1144 /* Expresses MC->path as a string and returns the string. */
1145 static const char *
1146 path_string (struct mc *mc)
1147 {
1148   ds_clear (&mc->path_string);
1149   mc_path_to_string (&mc->path, &mc->path_string);
1150   return ds_cstr (&mc->path_string);
1151 }
1152
1153 /* Frees STATE, including client data. */
1154 static void
1155 free_state (const struct mc *mc, struct mc_state *state)
1156 {
1157   mc->class->destroy (mc, state->data);
1158   mc_path_destroy (&state->path);
1159   free (state);
1160 }
1161
1162 /* Sets STOP_REASON as the reason that MC's processing stopped,
1163    unless MC is already stopped. */
1164 static void
1165 stop (struct mc *mc, enum mc_stop_reason stop_reason)
1166 {
1167   if (mc->results->stop_reason == MC_CONTINUING)
1168     mc->results->stop_reason = stop_reason;
1169 }
1170
1171 /* Creates and returns a new state whose path is copied from
1172    MC->path and whose data is specified by DATA. */
1173 static struct mc_state *
1174 make_state (const struct mc *mc, void *data)
1175 {
1176   struct mc_state *new = xmalloc (sizeof *new);
1177   mc_path_init (&new->path);
1178   mc_path_copy (&new->path, &mc->path);
1179   new->data = data;
1180   return new;
1181 }
1182
1183 /* Returns the index in MC->queue of a random element in the
1184    queue. */
1185 static size_t
1186 random_queue_index (struct mc *mc)
1187 {
1188   assert (!deque_is_empty (&mc->queue_deque));
1189   return deque_front (&mc->queue_deque,
1190                       rand () % deque_count (&mc->queue_deque));
1191 }
1192
1193 /* Adds NEW to MC's state queue, dropping a state if necessary
1194    due to overflow. */
1195 static void
1196 enqueue_state (struct mc *mc, struct mc_state *new)
1197 {
1198   size_t idx;
1199
1200   if (new->path.length > mc->results->max_depth_reached)
1201     mc->results->max_depth_reached = new->path.length;
1202   moments1_add (mc->results->depth_moments, new->path.length, 1.0);
1203
1204   if (deque_count (&mc->queue_deque) < mc->options->queue_limit)
1205     {
1206       /* Add new state to queue. */
1207       if (deque_is_full (&mc->queue_deque))
1208         mc->queue = deque_expand (&mc->queue_deque,
1209                                    mc->queue, sizeof *mc->queue);
1210       switch (mc->options->strategy)
1211         {
1212         case MC_BROAD:
1213           idx = deque_push_back (&mc->queue_deque);
1214           break;
1215         case MC_DEEP:
1216           idx = deque_push_front (&mc->queue_deque);
1217           break;
1218         case MC_RANDOM:
1219           if (!deque_is_empty (&mc->queue_deque))
1220             {
1221               idx = random_queue_index (mc);
1222               mc->queue[deque_push_front (&mc->queue_deque)]
1223                 = mc->queue[idx];
1224             }
1225           else
1226             idx = deque_push_front (&mc->queue_deque);
1227           break;
1228         case MC_PATH:
1229           assert (deque_is_empty (&mc->queue_deque));
1230           assert (!is_off_path (mc));
1231           idx = deque_push_back (&mc->queue_deque);
1232           if (mc->path.length
1233               >= mc_path_get_length (&mc->options->follow_path))
1234             stop (mc, MC_END_OF_PATH);
1235           break;
1236         default:
1237           NOT_REACHED ();
1238         }
1239       if (deque_count (&mc->queue_deque) > mc->results->max_queue_length)
1240         mc->results->max_queue_length = deque_count (&mc->queue_deque);
1241     }
1242   else
1243     {
1244       /* Queue has reached limit, so replace an existing
1245          state. */
1246       assert (mc->options->strategy != MC_PATH);
1247       assert (!deque_is_empty (&mc->queue_deque));
1248       mc->results->queue_dropped_states++;
1249       switch (mc->options->queue_limit_strategy)
1250         {
1251         case MC_DROP_NEWEST:
1252           free_state (mc, new);
1253           return;
1254         case MC_DROP_OLDEST:
1255           switch (mc->options->strategy)
1256             {
1257             case MC_BROAD:
1258               idx = deque_front (&mc->queue_deque, 0);
1259               break;
1260             case MC_DEEP:
1261               idx = deque_back (&mc->queue_deque, 0);
1262               break;
1263             case MC_RANDOM:
1264             case MC_PATH:
1265             default:
1266               NOT_REACHED ();
1267             }
1268           break;
1269         case MC_DROP_RANDOM:
1270           idx = random_queue_index (mc);
1271           break;
1272         default:
1273           NOT_REACHED ();
1274         }
1275       free_state (mc, mc->queue[idx]);
1276     }
1277   mc->queue[idx] = new;
1278 }
1279
1280 /* Process an error state being added to MC. */
1281 static void
1282 do_error_state (struct mc *mc)
1283 {
1284   mc->results->error_count++;
1285   if (mc->results->error_count >= mc->options->max_errors)
1286     stop (mc, MC_MAX_ERROR_COUNT);
1287
1288   mc_path_copy (&mc->results->error_path, &mc->path);
1289
1290   if (mc->options->failure_verbosity > mc->options->verbosity)
1291     {
1292       struct mc_options *path_options;
1293
1294       fprintf (mc->options->output_file, "[%s] retracing error path:\n",
1295                path_string (mc));
1296       path_options = mc_options_clone (mc->options);
1297       mc_options_set_verbosity (path_options, mc->options->failure_verbosity);
1298       mc_options_set_failure_verbosity (path_options, 0);
1299       mc_options_set_follow_path (path_options, &mc->path);
1300
1301       mc_results_destroy (mc_run (mc->class, path_options));
1302
1303       putc ('\n', mc->options->output_file);
1304     }
1305 }
1306
1307 /* Advances MC to start processing the operation following the
1308    current one. */
1309 static void
1310 next_operation (struct mc *mc)
1311 {
1312   mc_path_push (&mc->path, mc_path_pop (&mc->path) + 1);
1313   mc->state_error = false;
1314   mc->state_named = false;
1315
1316   if (++mc->progress >= mc->next_progress)
1317     {
1318       struct timeval now;
1319       double elapsed, delta;
1320
1321       if (mc->results->stop_reason == MC_CONTINUING
1322           && !mc->options->progress_func (mc))
1323         stop (mc, MC_INTERRUPTED);
1324
1325       gettimeofday (&now, NULL);
1326
1327       if (mc->options->time_limit > 0.0
1328           && (timeval_subtract (now, mc->results->start)
1329               > mc->options->time_limit))
1330         stop (mc, MC_TIMEOUT);
1331
1332       elapsed = timeval_subtract (now, mc->prev_progress_time);
1333       if (elapsed > 0.0)
1334         {
1335           /* Re-estimate the amount of progress to take
1336              progress_usec microseconds. */
1337           unsigned int progress = mc->progress - mc->prev_progress;
1338           double progress_sec = mc->options->progress_usec / 1000000.0;
1339           delta = progress / elapsed * progress_sec;
1340         }
1341       else
1342         {
1343           /* No measurable time at all elapsed during that amount
1344              of progress.  Try doubling the amount of progress
1345              required. */
1346           delta = (mc->progress - mc->prev_progress) * 2;
1347         }
1348
1349       if (delta > 0.0 && delta + mc->progress + 1.0 < UINT_MAX)
1350         mc->next_progress = mc->progress + delta + 1.0;
1351       else
1352         mc->next_progress = mc->progress + (mc->progress - mc->prev_progress);
1353
1354       mc->prev_progress = mc->progress;
1355       mc->prev_progress_time = now;
1356     }
1357 }
1358
1359 /* Returns true if we're tracing an explicit path but the current
1360    operation produces a state off that path, false otherwise. */
1361 static bool
1362 is_off_path (const struct mc *mc)
1363 {
1364   return (mc->options->strategy == MC_PATH
1365           && (mc_path_back (&mc->path)
1366               != mc_path_get_operation (&mc->options->follow_path,
1367                                         mc->path.length - 1)));
1368 }
1369
1370 /* Handler for SIGINT. */
1371 static void
1372 sigint_handler (int signum UNUSED)
1373 {
1374   /* Just mark the model checker as interrupted. */
1375   *interrupted_ptr = true;
1376 }
1377
1378 /* Initializes MC as a model checker with the given CLASS and
1379    OPTIONS.  OPTIONS may be null to use the default options. */
1380 static void
1381 init_mc (struct mc *mc, const struct mc_class *class,
1382          struct mc_options *options)
1383 {
1384   /* Validate and adjust OPTIONS. */
1385   if (options == NULL)
1386     options = mc_options_create ();
1387   assert (options->queue_limit_strategy != MC_DROP_OLDEST
1388           || options->strategy != MC_RANDOM);
1389   if (options->strategy == MC_PATH)
1390     {
1391       options->max_depth = INT_MAX;
1392       options->hash_bits = 0;
1393     }
1394   if (options->progress_usec == 0)
1395     {
1396       options->progress_func = null_progress;
1397       if (options->time_limit > 0.0)
1398         options->progress_usec = 250000;
1399     }
1400
1401   /* Initialize MC. */
1402   mc->class = class;
1403   mc->options = options;
1404   mc->results = mc_results_create ();
1405
1406   mc->hash = (mc->options->hash_bits > 0
1407               ? xcalloc (1, DIV_RND_UP (1 << mc->options->hash_bits, CHAR_BIT))
1408               : NULL);
1409
1410   mc->queue = NULL;
1411   deque_init_null (&mc->queue_deque);
1412
1413   mc_path_init (&mc->path);
1414   mc_path_push (&mc->path, 0);
1415   ds_init_empty (&mc->path_string);
1416   mc->state_named = false;
1417   mc->state_error = false;
1418
1419   mc->progress = 0;
1420   mc->next_progress = mc->options->progress_usec != 0 ? 100 : UINT_MAX;
1421   mc->prev_progress = 0;
1422   mc->prev_progress_time = mc->results->start;
1423
1424   if (mc->options->strategy == MC_RANDOM
1425       || options->queue_limit_strategy == MC_DROP_RANDOM)
1426     srand (mc->options->seed);
1427
1428   mc->interrupted = false;
1429   mc->saved_interrupted_ptr = interrupted_ptr;
1430   interrupted_ptr = &mc->interrupted;
1431   mc->saved_sigint = signal (SIGINT, sigint_handler);
1432
1433   class->init (mc);
1434 }
1435
1436 /* Complete the model checker run for MC. */
1437 static void
1438 finish_mc (struct mc *mc)
1439 {
1440   /* Restore signal handlers. */
1441   signal (SIGINT, mc->saved_sigint);
1442   interrupted_ptr = mc->saved_interrupted_ptr;
1443
1444   /* Mark the run complete. */
1445   stop (mc, MC_SUCCESS);
1446   gettimeofday (&mc->results->end, NULL);
1447
1448   /* Empty the queue. */
1449   mc->results->queued_unprocessed_states = deque_count (&mc->queue_deque);
1450   while (!deque_is_empty (&mc->queue_deque))
1451     {
1452       struct mc_state *state = mc->queue[deque_pop_front (&mc->queue_deque)];
1453       free_state (mc, state);
1454     }
1455
1456   /* Notify the progress function of completion. */
1457   mc->options->progress_func (mc);
1458
1459   /* Free memory. */
1460   mc_path_destroy (&mc->path);
1461   ds_destroy (&mc->path_string);
1462   free (mc->options);
1463   free (mc->queue);
1464   free (mc->hash);
1465 }