Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
model-checker : cosmetics in log message
[simgrid.git] / src / mc / mc_private.h
1 /* Copyright (c) 2007-2014. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 #ifndef MC_PRIVATE_H
8 #define MC_PRIVATE_H
9
10 #include "simgrid_config.h"
11 #include <stdio.h>
12 #ifndef WIN32
13 #include <sys/mman.h>
14 #endif
15 #include <elfutils/libdw.h>
16
17 #include "mc/mc.h"
18 #include "mc/datatypes.h"
19 #include "xbt/fifo.h"
20 #include "xbt/config.h"
21 #include "xbt/function_types.h"
22 #include "xbt/mmalloc.h"
23 #include "../simix/smx_private.h"
24 #include "../xbt/mmalloc/mmprivate.h"
25 #include "xbt/automaton.h"
26 #include "xbt/hash.h"
27 #include "msg/msg.h"
28 #include "msg/datatypes.h"
29 #include "xbt/strbuff.h"
30 #include "xbt/parmap.h"
31
32 typedef struct s_dw_frame s_dw_frame_t, *dw_frame_t;
33 typedef struct s_mc_function_index_item s_mc_function_index_item_t, *mc_function_index_item_t;
34
35 /****************************** Snapshots ***********************************/
36
37 #define NB_REGIONS 3 /* binary data (data + BSS) (type = 2), libsimgrid data (data + BSS) (type = 1), std_heap (type = 0)*/ 
38
39 typedef struct s_mc_mem_region{
40   // Real address:
41   void *start_addr;
42   // Copy of the datra:
43   void *data;
44   // Size of the data region:
45   size_t size;
46 } s_mc_mem_region_t, *mc_mem_region_t;
47
48 typedef struct s_mc_snapshot{
49   size_t heap_bytes_used;
50   mc_mem_region_t regions[NB_REGIONS];
51   int nb_processes;
52   mc_mem_region_t* privatization_regions;
53   int privatization_index;
54   size_t *stack_sizes;
55   xbt_dynar_t stacks;
56   xbt_dynar_t to_ignore;
57   uint64_t hash;
58 } s_mc_snapshot_t, *mc_snapshot_t;
59
60 /** Information about a given stack frame
61  *
62  */
63 typedef struct s_mc_stack_frame {
64   /** Instruction pointer */
65   unw_word_t ip;
66   /** Stack pointer */
67   unw_word_t sp;
68   unw_word_t frame_base;
69   dw_frame_t frame;
70   char* frame_name;
71   unw_cursor_t unw_cursor;
72 } s_mc_stack_frame_t, *mc_stack_frame_t;
73
74 typedef struct s_mc_snapshot_stack{
75   xbt_dynar_t local_variables;
76   void *stack_pointer;
77   void *real_address;
78   xbt_dynar_t stack_frames; // mc_stack_frame_t
79 }s_mc_snapshot_stack_t, *mc_snapshot_stack_t;
80
81 typedef struct s_mc_global_t{
82   mc_snapshot_t snapshot;
83   int raw_mem_set;
84   int prev_pair;
85   char *prev_req;
86   int initial_communications_pattern_done;
87   int comm_deterministic;
88   int send_deterministic;
89 }s_mc_global_t, *mc_global_t;
90
91 typedef struct s_mc_checkpoint_ignore_region{
92   void *addr;
93   size_t size;
94 }s_mc_checkpoint_ignore_region_t, *mc_checkpoint_ignore_region_t;
95
96 inline static void* mc_snapshot_get_heap_end(mc_snapshot_t snapshot) {
97   if(snapshot==NULL)
98     xbt_die("snapshot is NULL");
99   xbt_mheap_t heap = (xbt_mheap_t)snapshot->regions[0]->data;
100   return heap->breakval;
101 }
102
103 mc_snapshot_t SIMIX_pre_mc_snapshot(smx_simcall_t simcall);
104 mc_snapshot_t MC_take_snapshot(int num_state);
105 void MC_restore_snapshot(mc_snapshot_t);
106 void MC_free_snapshot(mc_snapshot_t);
107
108 /** \brief Translate a pointer from process address space to snapshot address space
109  *
110  *  The address space contains snapshot of the main/application memory:
111  *  this function finds the address in a given snaphot for a given
112  *  real/application address.
113  *
114  *  For read only memory regions and other regions which are not int the
115  *  snapshot, the address is not changed.
116  *
117  *  \param addr     Application address
118  *  \param snapshot The snapshot of interest (if NULL no translation is done)
119  *  \return         Translated address in the snapshot address space
120  * */
121 void* mc_translate_address(uintptr_t addr, mc_snapshot_t snapshot);
122
123 /** \brief Translate a pointer from the snapshot address space to the application address space
124  *
125  *  This is the inverse of mc_translate_address.
126  *
127  * \param addr    Address in the snapshot address space
128  * \param snapsot Snapshot of interest (if NULL no translation is done)
129  * \return        Translated address in the application address space
130  */
131 uintptr_t mc_untranslate_address(void* addr, mc_snapshot_t snapshot);
132
133 extern xbt_dynar_t mc_checkpoint_ignore;
134
135 /********************************* MC Global **********************************/
136
137 extern double *mc_time;
138 extern FILE *dot_output;
139 extern const char* colors[13];
140 extern xbt_parmap_t parmap;
141
142 extern int user_max_depth_reached;
143
144 int MC_deadlock_check(void);
145 void MC_replay(xbt_fifo_t stack, int start);
146 void MC_replay_liveness(xbt_fifo_t stack, int all_stack);
147 void MC_wait_for_requests(void);
148 void MC_show_deadlock(smx_simcall_t req);
149 void MC_show_stack_safety(xbt_fifo_t stack);
150 void MC_dump_stack_safety(xbt_fifo_t stack);
151 void MC_init(void);
152 int SIMIX_pre_mc_random(smx_simcall_t simcall, int min, int max);
153
154
155 /********************************* Requests ***********************************/
156
157 int MC_request_depend(smx_simcall_t req1, smx_simcall_t req2);
158 char* MC_request_to_string(smx_simcall_t req, int value);
159 unsigned int MC_request_testany_fail(smx_simcall_t req);
160 /*int MC_waitany_is_enabled_by_comm(smx_req_t req, unsigned int comm);*/
161 int MC_request_is_visible(smx_simcall_t req);
162 int MC_request_is_enabled(smx_simcall_t req);
163 int MC_request_is_enabled_by_idx(smx_simcall_t req, unsigned int idx);
164 int MC_process_is_enabled(smx_process_t process);
165 char *MC_request_get_dot_output(smx_simcall_t req, int value);
166
167
168 /******************************** States **************************************/
169
170 /* Possible exploration status of a process in a state */
171 typedef enum {
172   MC_NOT_INTERLEAVE=0,      /* Do not interleave (do not execute) */
173   MC_INTERLEAVE,            /* Interleave the process (one or more request) */
174   MC_MORE_INTERLEAVE,       /* Interleave twice the process (for mc_random simcall) */
175   MC_DONE                   /* Already interleaved */
176 } e_mc_process_state_t;
177
178 /* On every state, each process has an entry of the following type */
179 typedef struct mc_procstate{
180   e_mc_process_state_t state;       /* Exploration control information */
181   unsigned int interleave_count;    /* Number of times that the process was
182                                        interleaved */
183 } s_mc_procstate_t, *mc_procstate_t;
184
185 /* An exploration state is composed of: */
186 typedef struct mc_state {
187   unsigned long max_pid;            /* Maximum pid at state's creation time */
188   mc_procstate_t proc_status;       /* State's exploration status by process */
189   s_smx_action_t internal_comm;     /* To be referenced by the internal_req */
190   s_smx_simcall_t internal_req;         /* Internal translation of request */
191   s_smx_simcall_t executed_req;         /* The executed request of the state */
192   int req_num;                      /* The request number (in the case of a
193                                        multi-request like waitany ) */
194   mc_snapshot_t system_state;      /* Snapshot of system state */
195   int num;
196 } s_mc_state_t, *mc_state_t;
197
198 mc_state_t MC_state_new(void);
199 void MC_state_delete(mc_state_t state);
200 void MC_state_interleave_process(mc_state_t state, smx_process_t process);
201 unsigned int MC_state_interleave_size(mc_state_t state);
202 int MC_state_process_is_done(mc_state_t state, smx_process_t process);
203 void MC_state_set_executed_request(mc_state_t state, smx_simcall_t req, int value);
204 smx_simcall_t MC_state_get_executed_request(mc_state_t state, int *value);
205 smx_simcall_t MC_state_get_internal_request(mc_state_t state);
206 smx_simcall_t MC_state_get_request(mc_state_t state, int *value);
207 void MC_state_remove_interleave_process(mc_state_t state, smx_process_t process);
208
209
210 /****************************** Statistics ************************************/
211
212 typedef struct mc_stats {
213   unsigned long state_size;
214   unsigned long visited_states;
215   unsigned long visited_pairs;
216   unsigned long expanded_states;
217   unsigned long expanded_pairs;
218   unsigned long executed_transitions;
219 } s_mc_stats_t, *mc_stats_t;
220
221 extern mc_stats_t mc_stats;
222
223 void MC_print_statistics(mc_stats_t);
224
225
226 /********************************** MEMORY ******************************/
227 /* The possible memory modes for the modelchecker are standard and raw. */
228 /* Normally the system should operate in std, for switching to raw mode */
229 /* you must wrap the code between MC_SET_RAW_MODE and MC_UNSET_RAW_MODE */
230
231 extern void *std_heap;
232 extern void *mc_heap;
233
234
235 /* FIXME: Horrible hack! because the mmalloc library doesn't provide yet of */
236 /* an API to query about the status of a heap, we simply call mmstats and */
237 /* because I now how does structure looks like, then I redefine it here */
238
239 /* struct mstats { */
240 /*   size_t bytes_total;           /\* Total size of the heap. *\/ */
241 /*   size_t chunks_used;           /\* Chunks allocated by the user. *\/ */
242 /*   size_t bytes_used;            /\* Byte total of user-allocated chunks. *\/ */
243 /*   size_t chunks_free;           /\* Chunks in the free list. *\/ */
244 /*   size_t bytes_free;            /\* Byte total of chunks in the free list. *\/ */
245 /* }; */
246
247 #define MC_SET_MC_HEAP    mmalloc_set_current_heap(mc_heap)
248 #define MC_SET_STD_HEAP  mmalloc_set_current_heap(std_heap)
249
250
251 /******************************* MEMORY MAPPINGS ***************************/
252 /* These functions and data structures implements a binary interface for   */
253 /* the proc maps ascii interface                                           */
254
255 /* Each field is defined as documented in proc's manual page  */
256 typedef struct s_map_region {
257
258   void *start_addr;             /* Start address of the map */
259   void *end_addr;               /* End address of the map */
260   int prot;                     /* Memory protection */
261   int flags;                    /* Additional memory flags */
262   void *offset;                 /* Offset in the file/whatever */
263   char dev_major;               /* Major of the device */
264   char dev_minor;               /* Minor of the device */
265   unsigned long inode;          /* Inode in the device */
266   char *pathname;               /* Path name of the mapped file */
267
268 } s_map_region_t;
269
270 typedef struct s_memory_map {
271
272   s_map_region_t *regions;      /* Pointer to an array of regions */
273   int mapsize;                  /* Number of regions in the memory */
274
275 } s_memory_map_t, *memory_map_t;
276
277
278 void MC_init_memory_map_info(void);
279 memory_map_t MC_get_memory_map(void);
280 void MC_free_memory_map(memory_map_t map);
281
282 extern char *libsimgrid_path;
283
284 /********************************** Snapshot comparison **********************************/
285
286 typedef struct s_mc_comparison_times{
287   double nb_processes_comparison_time;
288   double bytes_used_comparison_time;
289   double stacks_sizes_comparison_time;
290   double binary_global_variables_comparison_time;
291   double libsimgrid_global_variables_comparison_time;
292   double heap_comparison_time;
293   double stacks_comparison_time;
294 }s_mc_comparison_times_t, *mc_comparison_times_t;
295
296 extern __thread mc_comparison_times_t mc_comp_times;
297 extern __thread double mc_snapshot_comparison_time;
298
299 int snapshot_compare(void *state1, void *state2);
300 int SIMIX_pre_mc_compare_snapshots(smx_simcall_t simcall, mc_snapshot_t s1, mc_snapshot_t s2);
301 void print_comparison_times(void);
302
303 //#define MC_DEBUG 1
304 #define MC_VERBOSE 1
305
306 /********************************** DPOR for safety property **************************************/
307
308 typedef enum {
309   e_mc_reduce_unset,
310   e_mc_reduce_none,
311   e_mc_reduce_dpor
312 } e_mc_reduce_t;
313
314 extern e_mc_reduce_t mc_reduce_kind;
315 extern mc_global_t initial_state_safety;
316 extern xbt_fifo_t mc_stack_safety;
317 extern xbt_dict_t first_enabled_state;
318
319 void MC_dpor_init(void);
320 void MC_dpor(void);
321
322 typedef struct s_mc_visited_state{
323   mc_snapshot_t system_state;
324   size_t heap_bytes_used;
325   int nb_processes;
326   int num;
327   int other_num; // dot_output for
328 }s_mc_visited_state_t, *mc_visited_state_t;
329
330
331 /********************************** Double-DFS for liveness property **************************************/
332
333 extern xbt_fifo_t mc_stack_liveness;
334 extern mc_global_t initial_state_liveness;
335 extern xbt_automaton_t _mc_property_automaton;
336 extern int compare;
337
338 typedef struct s_mc_pair{
339   int num;
340   int search_cycle;
341   mc_state_t graph_state; /* System state included */
342   xbt_automaton_state_t automaton_state;
343   xbt_dynar_t atomic_propositions;
344   int requests;
345 }s_mc_pair_t, *mc_pair_t;
346
347 typedef struct s_mc_visited_pair{
348   int num;
349   int other_num; /* Dot output for */
350   int acceptance_pair;
351   mc_state_t graph_state; /* System state included */
352   xbt_automaton_state_t automaton_state;
353   xbt_dynar_t atomic_propositions;
354   size_t heap_bytes_used;
355   int nb_processes;
356   int acceptance_removed;
357   int visited_removed;
358 }s_mc_visited_pair_t, *mc_visited_pair_t;
359
360 mc_pair_t MC_pair_new(void);
361 void MC_pair_delete(mc_pair_t);
362 void mc_pair_free_voidp(void *p);
363 mc_visited_pair_t MC_visited_pair_new(int pair_num, xbt_automaton_state_t automaton_state, xbt_dynar_t atomic_propositions);
364 void MC_visited_pair_delete(mc_visited_pair_t p);
365
366 void MC_ddfs_init(void);
367 void MC_ddfs(void);
368 void MC_show_stack_liveness(xbt_fifo_t stack);
369 void MC_dump_stack_liveness(xbt_fifo_t stack);
370
371
372 /********************************** Variables with DWARF **********************************/
373
374 #define MC_OBJECT_INFO_EXECUTABLE 1
375
376 struct s_mc_object_info {
377   size_t flags;
378   char* file_name;
379   char *start_exec, *end_exec; // Executable segment
380   char *start_rw, *end_rw; // Read-write segment
381   char *start_ro, *end_ro; // read-only segment
382   xbt_dict_t subprograms; // xbt_dict_t<origin as hexadecimal string, dw_frame_t>
383   xbt_dynar_t global_variables; // xbt_dynar_t<dw_variable_t>
384   xbt_dict_t types; // xbt_dict_t<origin as hexadecimal string, dw_type_t>
385   xbt_dict_t full_types_by_name; // xbt_dict_t<name, dw_type_t> (full defined type only)
386
387   // Here we sort the minimal information for an efficient (and cache-efficient)
388   // lookup of a function given an instruction pointer.
389   // The entries are sorted by low_pc and a binary search can be used to look them up.
390   xbt_dynar_t functions_index;
391 };
392
393 mc_object_info_t MC_new_object_info(void);
394 mc_object_info_t MC_find_object_info(memory_map_t maps, char* name, int executable);
395 void MC_free_object_info(mc_object_info_t* p);
396
397 void MC_dwarf_get_variables(mc_object_info_t info);
398 void MC_dwarf_get_variables_libdw(mc_object_info_t info);
399 const char* MC_dwarf_attrname(int attr);
400 const char* MC_dwarf_tagname(int tag);
401
402 dw_frame_t MC_find_function_by_ip(void* ip);
403 mc_object_info_t MC_ip_find_object_info(void* ip);
404
405 extern mc_object_info_t mc_libsimgrid_info;
406 extern mc_object_info_t mc_binary_info;
407 extern mc_object_info_t mc_object_infos[2];
408 extern size_t mc_object_infos_size;
409
410 void MC_find_object_address(memory_map_t maps, mc_object_info_t result);
411 void MC_post_process_types(mc_object_info_t info);
412
413 // ***** Expressions
414
415 /** \brief a DWARF expression with optional validity contraints */
416 typedef struct s_mc_expression {
417   size_t size;
418   Dwarf_Op* ops;
419   // Optional validity:
420   void* lowpc, *highpc;
421 } s_mc_expression_t, *mc_expression_t;
422
423 /** A location list (list of location expressions) */
424 typedef struct s_mc_location_list {
425   size_t size;
426   mc_expression_t locations;
427 } s_mc_location_list_t, *mc_location_list_t;
428
429 uintptr_t mc_dwarf_resolve_location(mc_expression_t expression, mc_object_info_t object_info, unw_cursor_t* c, void* frame_pointer_address, mc_snapshot_t snapshot);
430 uintptr_t mc_dwarf_resolve_locations(mc_location_list_t locations, mc_object_info_t object_info, unw_cursor_t* c, void* frame_pointer_address, mc_snapshot_t snapshot);
431
432 void mc_dwarf_expression_clear(mc_expression_t expression);
433 void mc_dwarf_expression_init(mc_expression_t expression, size_t len, Dwarf_Op* ops);
434
435 void mc_dwarf_location_list_clear(mc_location_list_t list);
436
437 void mc_dwarf_location_list_init_from_expression(mc_location_list_t target, size_t len, Dwarf_Op* ops);
438 void mc_dwarf_location_list_init(mc_location_list_t target, mc_object_info_t info, Dwarf_Die* die, Dwarf_Attribute* attr);
439
440 // ***** Variables and functions
441
442 struct s_dw_type{
443   e_dw_type_type type;
444   Dwarf_Off id; /* Offset in the section (in hexadecimal form) */
445   char *name; /* Name of the type */
446   int byte_size; /* Size in bytes */
447   int element_count; /* Number of elements for array type */
448   char *dw_type_id; /* DW_AT_type id */
449   xbt_dynar_t members; /* if DW_TAG_structure_type, DW_TAG_class_type, DW_TAG_union_type*/
450   int is_pointer_type;
451
452   // Location (for members) is either of:
453   struct s_mc_expression location;
454   int offset;
455
456   dw_type_t subtype; // DW_AT_type
457   dw_type_t full_type; // The same (but more complete) type
458 };
459
460 void* mc_member_resolve(const void* base, dw_type_t type, dw_type_t member, mc_snapshot_t snapshot);
461 void* mc_member_snapshot_resolve(const void* base, dw_type_t type, dw_type_t member, mc_snapshot_t snapshot);
462
463 typedef struct s_dw_variable{
464   Dwarf_Off dwarf_offset; /* Global offset of the field. */
465   int global;
466   char *name;
467   char *type_origin;
468   dw_type_t type;
469
470   // Use either of:
471   s_mc_location_list_t locations;
472   void* address;
473
474   size_t start_scope;
475   mc_object_info_t object_info;
476
477 }s_dw_variable_t, *dw_variable_t;
478
479 struct s_dw_frame{
480   int tag;
481   char *name;
482   void *low_pc;
483   void *high_pc;
484   s_mc_location_list_t frame_base;
485   xbt_dynar_t /* <dw_variable_t> */ variables; /* Cannot use dict, there may be several variables with the same name (in different lexical blocks)*/
486   unsigned long int id; /* DWARF offset of the subprogram */
487   xbt_dynar_t /* <dw_frame_t> */ scopes;
488   Dwarf_Off abstract_origin_id;
489   mc_object_info_t object_info;
490 };
491
492 struct s_mc_function_index_item {
493   void* low_pc, *high_pc;
494   dw_frame_t function;
495 };
496
497 void mc_frame_free(dw_frame_t freme);
498
499 void dw_type_free(dw_type_t t);
500 void dw_variable_free(dw_variable_t v);
501 void dw_variable_free_voidp(void *t);
502
503 void MC_dwarf_register_global_variable(mc_object_info_t info, dw_variable_t variable);
504 void MC_register_variable(mc_object_info_t info, dw_frame_t frame, dw_variable_t variable);
505 void MC_dwarf_register_non_global_variable(mc_object_info_t info, dw_frame_t frame, dw_variable_t variable);
506 void MC_dwarf_register_variable(mc_object_info_t info, dw_frame_t frame, dw_variable_t variable);
507
508 /** Find the DWARF offset for this ELF object
509  *
510  *  An offset is applied to address found in DWARF:
511  *
512  *  <ul>
513  *    <li>for an executable obejct, addresses are virtual address
514  *        (there is no offset) i.e. \f$\text{virtual address} = \{dwarf address}\f$;</li>
515  *    <li>for a shared object, the addreses are offset from the begining
516  *        of the shared object (the base address of the mapped shared
517  *        object must be used as offset
518  *        i.e. \f$\text{virtual address} = \text{shared object base address}
519  *             + \text{dwarf address}\f$.</li>
520  *
521  */
522 void* MC_object_base_address(mc_object_info_t info);
523
524 /********************************** DWARF **********************************/
525
526 #define MC_EXPRESSION_STACK_SIZE 64
527
528 #define MC_EXPRESSION_OK 0
529 #define MC_EXPRESSION_E_UNSUPPORTED_OPERATION 1
530 #define MC_EXPRESSION_E_STACK_OVERFLOW 2
531 #define MC_EXPRESSION_E_STACK_UNDERFLOW 3
532 #define MC_EXPRESSION_E_MISSING_STACK_CONTEXT 4
533 #define MC_EXPRESSION_E_MISSING_FRAME_BASE 5
534 #define MC_EXPRESSION_E_NO_BASE_ADDRESS 6
535
536 typedef struct s_mc_expression_state {
537   uintptr_t stack[MC_EXPRESSION_STACK_SIZE];
538   size_t stack_size;
539
540   unw_cursor_t* cursor;
541   void* frame_base;
542   mc_snapshot_t snapshot;
543   mc_object_info_t object_info;
544 } s_mc_expression_state_t, *mc_expression_state_t;
545
546 int mc_dwarf_execute_expression(size_t n, const Dwarf_Op* ops, mc_expression_state_t state);
547
548 void* mc_find_frame_base(dw_frame_t frame, mc_object_info_t object_info, unw_cursor_t* unw_cursor);
549
550 /********************************** Miscellaneous **********************************/
551
552 typedef struct s_local_variable{
553   dw_frame_t subprogram;
554   unsigned long ip;
555   char *name;
556   dw_type_t type;
557   void *address;
558   int region;
559 }s_local_variable_t, *local_variable_t;
560
561 /********************************* Communications pattern ***************************/
562
563 typedef struct s_mc_comm_pattern{
564   int num;
565   smx_action_t comm;
566   e_smx_comm_type_t type;
567   unsigned long src_proc;
568   unsigned long dst_proc;
569   const char *src_host;
570   const char *dst_host;
571   char *rdv;
572   ssize_t data_size;
573   void *data;
574 }s_mc_comm_pattern_t, *mc_comm_pattern_t;
575
576 extern xbt_dynar_t communications_pattern;
577 extern xbt_dynar_t incomplete_communications_pattern;
578
579 void get_comm_pattern(xbt_dynar_t communications_pattern, smx_simcall_t request, int call);
580 void complete_comm_pattern(xbt_dynar_t list, smx_action_t comm);
581
582 /* *********** Sets *********** */
583
584 typedef struct s_mc_address_set *mc_address_set_t;
585
586 mc_address_set_t mc_address_set_new();
587 void mc_address_set_free(mc_address_set_t* p);
588 void mc_address_add(mc_address_set_t p, const void* value);
589 bool mc_address_test(mc_address_set_t p, const void* value);
590
591 /* *********** Hash *********** */
592
593 /** \brief Hash the current state
594  *  \param num_state number of states
595  *  \param stacks stacks (mc_snapshot_stak_t) used fot the stack unwinding informations
596  *  \result resulting hash
597  * */
598 uint64_t mc_hash_processes_state(int num_state, xbt_dynar_t stacks);
599
600 #endif
601