Logo AND Algorithmique Numérique Distribuée

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