Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
model-checker : fix visited states reduction with comm determinism verification
[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 #include <stdbool.h>
13 #ifndef WIN32
14 #include <sys/mman.h>
15 #endif
16 #include <elfutils/libdw.h>
17
18 #include "mc/mc.h"
19 #include "mc/datatypes.h"
20 #include "xbt/fifo.h"
21 #include "xbt/config.h"
22 #include "xbt/function_types.h"
23 #include "xbt/mmalloc.h"
24 #include "../simix/smx_private.h"
25 #include "../xbt/mmalloc/mmprivate.h"
26 #include "xbt/automaton.h"
27 #include "xbt/hash.h"
28 #include "msg/msg.h"
29 #include "msg/datatypes.h"
30 #include "xbt/strbuff.h"
31 #include "xbt/parmap.h"
32 #include "mc_mmu.h"
33 #include "mc_page_store.h"
34
35 SG_BEGIN_DECL()
36
37 typedef struct s_dw_frame s_dw_frame_t, *dw_frame_t;
38 typedef struct s_mc_function_index_item s_mc_function_index_item_t, *mc_function_index_item_t;
39
40 /****************************** Snapshots ***********************************/
41
42 #define NB_REGIONS 3 /* binary data (data + BSS) (type = 2), libsimgrid data (data + BSS) (type = 1), std_heap (type = 0)*/ 
43
44 typedef struct s_mc_mem_region{
45   // Real address:
46   void *start_addr;
47   // Copy of the datra:
48   void *data;
49   // Size of the data region:
50   size_t size;
51   // For per-page snapshots, this is an array to the number of
52   size_t* page_numbers;
53 } s_mc_mem_region_t, *mc_mem_region_t;
54
55 static inline  __attribute__ ((always_inline))
56 bool mc_region_contain(mc_mem_region_t region, void* p)
57 {
58   return p >= region->start_addr &&
59     p < (void*)((char*) region->start_addr + region->size);
60 }
61
62 /** Ignored data
63  *
64  *  Some parts of the snapshot are ignored by zeroing them out: the real
65  *  values is stored here.
66  * */
67 typedef struct s_mc_snapshot_ignored_data {
68   void* start;
69   size_t size;
70   void* data;
71 } s_mc_snapshot_ignored_data_t, *mc_snapshot_ignored_data_t;
72
73 typedef struct s_mc_snapshot{
74   size_t heap_bytes_used;
75   mc_mem_region_t regions[NB_REGIONS];
76   xbt_dynar_t enabled_processes;
77   mc_mem_region_t* privatization_regions;
78   int privatization_index;
79   size_t *stack_sizes;
80   xbt_dynar_t stacks;
81   xbt_dynar_t to_ignore;
82   uint64_t hash;
83   xbt_dynar_t ignored_data;
84 } s_mc_snapshot_t, *mc_snapshot_t;
85
86 mc_mem_region_t mc_get_snapshot_region(void* addr, mc_snapshot_t snapshot);
87
88 static inline __attribute__ ((always_inline))
89 mc_mem_region_t mc_get_region_hinted(void* addr, mc_snapshot_t snapshot, mc_mem_region_t region)
90 {
91   if (mc_region_contain(region, addr))
92     return region;
93   else
94     return mc_get_snapshot_region(addr, snapshot);
95 }
96
97 /** Information about a given stack frame
98  *
99  */
100 typedef struct s_mc_stack_frame {
101   /** Instruction pointer */
102   unw_word_t ip;
103   /** Stack pointer */
104   unw_word_t sp;
105   unw_word_t frame_base;
106   dw_frame_t frame;
107   char* frame_name;
108   unw_cursor_t unw_cursor;
109 } s_mc_stack_frame_t, *mc_stack_frame_t;
110
111 typedef struct s_mc_snapshot_stack{
112   xbt_dynar_t local_variables;
113   xbt_dynar_t stack_frames; // mc_stack_frame_t
114 }s_mc_snapshot_stack_t, *mc_snapshot_stack_t;
115
116 typedef struct s_mc_global_t{
117   mc_snapshot_t snapshot;
118   int raw_mem_set;
119   int prev_pair;
120   char *prev_req;
121   int initial_communications_pattern_done;
122   int comm_deterministic;
123   int send_deterministic;
124 }s_mc_global_t, *mc_global_t;
125
126 typedef struct s_mc_checkpoint_ignore_region{
127   void *addr;
128   size_t size;
129 }s_mc_checkpoint_ignore_region_t, *mc_checkpoint_ignore_region_t;
130
131 static void* mc_snapshot_get_heap_end(mc_snapshot_t snapshot);
132
133 mc_snapshot_t SIMIX_pre_mc_snapshot(smx_simcall_t simcall);
134 mc_snapshot_t MC_take_snapshot(int num_state);
135 void MC_restore_snapshot(mc_snapshot_t);
136 void MC_free_snapshot(mc_snapshot_t);
137
138 int mc_important_snapshot(mc_snapshot_t snapshot);
139
140 size_t* mc_take_page_snapshot_region(void* data, size_t page_count, uint64_t* pagemap, size_t* reference_pages);
141 void mc_free_page_snapshot_region(size_t* pagenos, size_t page_count);
142 void mc_restore_page_snapshot_region(mc_mem_region_t region, size_t page_count, uint64_t* pagemap, mc_mem_region_t reference_region);
143
144 mc_mem_region_t mc_region_new_sparse(int type, void *start_addr, size_t size, mc_mem_region_t ref_reg);
145 void mc_region_restore_sparse(mc_mem_region_t reg, mc_mem_region_t ref_reg);
146 void mc_softdirty_reset();
147
148 static inline __attribute__((always_inline))
149 bool mc_snapshot_region_linear(mc_mem_region_t region) {
150   return !region || !region->data;
151 }
152
153 void* mc_snapshot_read_fragmented(void* addr, mc_mem_region_t region, void* target, size_t size);
154
155 void* mc_snapshot_read(void* addr, mc_snapshot_t snapshot, void* target, size_t size);
156 int mc_snapshot_region_memcp(
157   void* addr1, mc_mem_region_t region1,
158   void* addr2, mc_mem_region_t region2, size_t size);
159 int mc_snapshot_memcp(
160   void* addr1, mc_snapshot_t snapshot1,
161   void* addr2, mc_snapshot_t snapshot2, size_t size);
162
163 static void* mc_snapshot_read_pointer(void* addr, mc_snapshot_t snapshot);
164
165 /** @brief State of the model-checker (global variables for the model checker)
166  *
167  *  Each part of the state of the model chercker represented as a global
168  *  variable prevents some sharing between snapshots and must be ignored.
169  *  By moving as much state as possible in this structure allocated
170  *  on the model-chercker heap, we avoid those issues.
171  */
172 typedef struct s_mc_model_checker {
173   // This is the parent snapshot of the current state:
174   mc_snapshot_t parent_snapshot;
175   mc_pages_store_t pages;
176   int fd_clear_refs;
177   int fd_pagemap;
178 } s_mc_model_checker_t, *mc_model_checker_t;
179
180 extern mc_model_checker_t mc_model_checker;
181
182 extern xbt_dynar_t mc_checkpoint_ignore;
183
184 /********************************* MC Global **********************************/
185
186 extern double *mc_time;
187 extern FILE *dot_output;
188 extern const char* colors[13];
189 extern xbt_parmap_t parmap;
190
191 extern int user_max_depth_reached;
192
193 int MC_deadlock_check(void);
194 void MC_replay(xbt_fifo_t stack, int start);
195 void MC_replay_liveness(xbt_fifo_t stack, int all_stack);
196 void MC_wait_for_requests(void);
197 void MC_show_deadlock(smx_simcall_t req);
198 void MC_show_stack_safety(xbt_fifo_t stack);
199 void MC_dump_stack_safety(xbt_fifo_t stack);
200 int SIMIX_pre_mc_random(smx_simcall_t simcall, int min, int max);
201
202 extern xbt_fifo_t mc_stack;
203 int get_search_interval(xbt_dynar_t list, void *ref, int *min, int *max);
204
205
206 /********************************* Requests ***********************************/
207
208 int MC_request_depend(smx_simcall_t req1, smx_simcall_t req2);
209 char* MC_request_to_string(smx_simcall_t req, int value);
210 unsigned int MC_request_testany_fail(smx_simcall_t req);
211 /*int MC_waitany_is_enabled_by_comm(smx_req_t req, unsigned int comm);*/
212 int MC_request_is_visible(smx_simcall_t req);
213 int MC_request_is_enabled(smx_simcall_t req);
214 int MC_request_is_enabled_by_idx(smx_simcall_t req, unsigned int idx);
215 int MC_process_is_enabled(smx_process_t process);
216 char *MC_request_get_dot_output(smx_simcall_t req, int value);
217
218
219 /******************************** States **************************************/
220
221 extern mc_global_t initial_global_state;
222
223 /* Possible exploration status of a process in a state */
224 typedef enum {
225   MC_NOT_INTERLEAVE=0,      /* Do not interleave (do not execute) */
226   MC_INTERLEAVE,            /* Interleave the process (one or more request) */
227   MC_MORE_INTERLEAVE,       /* Interleave twice the process (for mc_random simcall) */
228   MC_DONE                   /* Already interleaved */
229 } e_mc_process_state_t;
230
231 /* On every state, each process has an entry of the following type */
232 typedef struct mc_procstate{
233   e_mc_process_state_t state;       /* Exploration control information */
234   unsigned int interleave_count;    /* Number of times that the process was
235                                        interleaved */
236 } s_mc_procstate_t, *mc_procstate_t;
237
238 /* An exploration state is composed of: */
239 typedef struct mc_state {
240   unsigned long max_pid;            /* Maximum pid at state's creation time */
241   mc_procstate_t proc_status;       /* State's exploration status by process */
242   s_smx_action_t internal_comm;     /* To be referenced by the internal_req */
243   s_smx_simcall_t internal_req;         /* Internal translation of request */
244   s_smx_simcall_t executed_req;         /* The executed request of the state */
245   int req_num;                      /* The request number (in the case of a
246                                        multi-request like waitany ) */
247   mc_snapshot_t system_state;      /* Snapshot of system state */
248   int num;
249 } s_mc_state_t, *mc_state_t;
250
251 mc_state_t MC_state_new(void);
252 void MC_state_delete(mc_state_t state);
253 void MC_state_interleave_process(mc_state_t state, smx_process_t process);
254 unsigned int MC_state_interleave_size(mc_state_t state);
255 int MC_state_process_is_done(mc_state_t state, smx_process_t process);
256 void MC_state_set_executed_request(mc_state_t state, smx_simcall_t req, int value);
257 smx_simcall_t MC_state_get_executed_request(mc_state_t state, int *value);
258 smx_simcall_t MC_state_get_internal_request(mc_state_t state);
259 smx_simcall_t MC_state_get_request(mc_state_t state, int *value);
260 void MC_state_remove_interleave_process(mc_state_t state, smx_process_t process);
261
262
263 /****************************** Statistics ************************************/
264
265 typedef struct mc_stats {
266   unsigned long state_size;
267   unsigned long visited_states;
268   unsigned long visited_pairs;
269   unsigned long expanded_states;
270   unsigned long expanded_pairs;
271   unsigned long executed_transitions;
272 } s_mc_stats_t, *mc_stats_t;
273
274 extern mc_stats_t mc_stats;
275
276 void MC_print_statistics(mc_stats_t);
277
278
279 /********************************** MEMORY ******************************/
280 /* The possible memory modes for the modelchecker are standard and raw. */
281 /* Normally the system should operate in std, for switching to raw mode */
282 /* you must wrap the code between MC_SET_RAW_MODE and MC_UNSET_RAW_MODE */
283
284 extern void *std_heap;
285 extern void *mc_heap;
286
287
288 /* FIXME: Horrible hack! because the mmalloc library doesn't provide yet of */
289 /* an API to query about the status of a heap, we simply call mmstats and */
290 /* because I now how does structure looks like, then I redefine it here */
291
292 /* struct mstats { */
293 /*   size_t bytes_total;           /\* Total size of the heap. *\/ */
294 /*   size_t chunks_used;           /\* Chunks allocated by the user. *\/ */
295 /*   size_t bytes_used;            /\* Byte total of user-allocated chunks. *\/ */
296 /*   size_t chunks_free;           /\* Chunks in the free list. *\/ */
297 /*   size_t bytes_free;            /\* Byte total of chunks in the free list. *\/ */
298 /* }; */
299
300 #define MC_SET_MC_HEAP    mmalloc_set_current_heap(mc_heap)
301 #define MC_SET_STD_HEAP  mmalloc_set_current_heap(std_heap)
302
303
304 /******************************* MEMORY MAPPINGS ***************************/
305 /* These functions and data structures implements a binary interface for   */
306 /* the proc maps ascii interface                                           */
307
308 /* Each field is defined as documented in proc's manual page  */
309 typedef struct s_map_region {
310
311   void *start_addr;             /* Start address of the map */
312   void *end_addr;               /* End address of the map */
313   int prot;                     /* Memory protection */
314   int flags;                    /* Additional memory flags */
315   void *offset;                 /* Offset in the file/whatever */
316   char dev_major;               /* Major of the device */
317   char dev_minor;               /* Minor of the device */
318   unsigned long inode;          /* Inode in the device */
319   char *pathname;               /* Path name of the mapped file */
320
321 } s_map_region_t;
322
323 typedef struct s_memory_map {
324
325   s_map_region_t *regions;      /* Pointer to an array of regions */
326   int mapsize;                  /* Number of regions in the memory */
327
328 } s_memory_map_t, *memory_map_t;
329
330
331 void MC_init_memory_map_info(void);
332 memory_map_t MC_get_memory_map(void);
333 void MC_free_memory_map(memory_map_t map);
334
335 extern char *libsimgrid_path;
336
337 /********************************** Snapshot comparison **********************************/
338
339 typedef struct s_mc_comparison_times{
340   double nb_processes_comparison_time;
341   double bytes_used_comparison_time;
342   double stacks_sizes_comparison_time;
343   double binary_global_variables_comparison_time;
344   double libsimgrid_global_variables_comparison_time;
345   double heap_comparison_time;
346   double stacks_comparison_time;
347 }s_mc_comparison_times_t, *mc_comparison_times_t;
348
349 extern __thread mc_comparison_times_t mc_comp_times;
350 extern __thread double mc_snapshot_comparison_time;
351
352 int snapshot_compare(void *state1, void *state2);
353 int SIMIX_pre_mc_compare_snapshots(smx_simcall_t simcall, mc_snapshot_t s1, mc_snapshot_t s2);
354 void print_comparison_times(void);
355
356 //#define MC_DEBUG 1
357 #define MC_VERBOSE 1
358
359 /********************************** Safety verification **************************************/
360
361 typedef enum {
362   e_mc_reduce_unset,
363   e_mc_reduce_none,
364   e_mc_reduce_dpor
365 } e_mc_reduce_t;
366
367 extern e_mc_reduce_t mc_reduce_kind;
368 extern xbt_dict_t first_enabled_state;
369
370 void MC_pre_modelcheck_safety(void);
371 void MC_modelcheck_safety(void);
372
373 typedef struct s_mc_visited_state{
374   mc_snapshot_t system_state;
375   size_t heap_bytes_used;
376   int nb_processes;
377   int num;
378   int other_num; // dot_output for
379 }s_mc_visited_state_t, *mc_visited_state_t;
380
381 extern xbt_dynar_t visited_states;
382 mc_visited_state_t is_visited_state(void);
383 void visited_state_free(mc_visited_state_t state);
384 void visited_state_free_voidp(void *s);
385
386 /********************************** Liveness verification **************************************/
387
388 extern xbt_automaton_t _mc_property_automaton;
389
390 typedef struct s_mc_pair{
391   int num;
392   int search_cycle;
393   mc_state_t graph_state; /* System state included */
394   xbt_automaton_state_t automaton_state;
395   xbt_dynar_t atomic_propositions;
396   int requests;
397 }s_mc_pair_t, *mc_pair_t;
398
399 typedef struct s_mc_visited_pair{
400   int num;
401   int other_num; /* Dot output for */
402   int acceptance_pair;
403   mc_state_t graph_state; /* System state included */
404   xbt_automaton_state_t automaton_state;
405   xbt_dynar_t atomic_propositions;
406   size_t heap_bytes_used;
407   int nb_processes;
408   int acceptance_removed;
409   int visited_removed;
410 }s_mc_visited_pair_t, *mc_visited_pair_t;
411
412 mc_pair_t MC_pair_new(void);
413 void MC_pair_delete(mc_pair_t);
414 void mc_pair_free_voidp(void *p);
415 mc_visited_pair_t MC_visited_pair_new(int pair_num, xbt_automaton_state_t automaton_state, xbt_dynar_t atomic_propositions);
416 void MC_visited_pair_delete(mc_visited_pair_t p);
417
418 void MC_pre_modelcheck_liveness(void);
419 void MC_modelcheck_liveness(void);
420 void MC_show_stack_liveness(xbt_fifo_t stack);
421 void MC_dump_stack_liveness(xbt_fifo_t stack);
422
423 extern xbt_dynar_t visited_pairs;
424 int is_visited_pair(mc_visited_pair_t pair, int pair_num, xbt_automaton_state_t automaton_state, xbt_dynar_t atomic_propositions);
425
426
427 /********************************** Variables with DWARF **********************************/
428
429 #define MC_OBJECT_INFO_EXECUTABLE 1
430
431 struct s_mc_object_info {
432   size_t flags;
433   char* file_name;
434   char *start_exec, *end_exec; // Executable segment
435   char *start_rw, *end_rw; // Read-write segment
436   char *start_ro, *end_ro; // read-only segment
437   xbt_dict_t subprograms; // xbt_dict_t<origin as hexadecimal string, dw_frame_t>
438   xbt_dynar_t global_variables; // xbt_dynar_t<dw_variable_t>
439   xbt_dict_t types; // xbt_dict_t<origin as hexadecimal string, dw_type_t>
440   xbt_dict_t full_types_by_name; // xbt_dict_t<name, dw_type_t> (full defined type only)
441
442   // Here we sort the minimal information for an efficient (and cache-efficient)
443   // lookup of a function given an instruction pointer.
444   // The entries are sorted by low_pc and a binary search can be used to look them up.
445   xbt_dynar_t functions_index;
446 };
447
448 mc_object_info_t MC_new_object_info(void);
449 mc_object_info_t MC_find_object_info(memory_map_t maps, char* name, int executable);
450 void MC_free_object_info(mc_object_info_t* p);
451
452 void MC_dwarf_get_variables(mc_object_info_t info);
453 void MC_dwarf_get_variables_libdw(mc_object_info_t info);
454 const char* MC_dwarf_attrname(int attr);
455 const char* MC_dwarf_tagname(int tag);
456
457 dw_frame_t MC_find_function_by_ip(void* ip);
458 mc_object_info_t MC_ip_find_object_info(void* ip);
459
460 extern mc_object_info_t mc_libsimgrid_info;
461 extern mc_object_info_t mc_binary_info;
462 extern mc_object_info_t mc_object_infos[2];
463 extern size_t mc_object_infos_size;
464
465 void MC_find_object_address(memory_map_t maps, mc_object_info_t result);
466 void MC_post_process_types(mc_object_info_t info);
467 void MC_post_process_object_info(mc_object_info_t info);
468
469 // ***** Expressions
470
471 /** \brief a DWARF expression with optional validity contraints */
472 typedef struct s_mc_expression {
473   size_t size;
474   Dwarf_Op* ops;
475   // Optional validity:
476   void* lowpc, *highpc;
477 } s_mc_expression_t, *mc_expression_t;
478
479 /** A location list (list of location expressions) */
480 typedef struct s_mc_location_list {
481   size_t size;
482   mc_expression_t locations;
483 } s_mc_location_list_t, *mc_location_list_t;
484
485 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);
486 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);
487
488 void mc_dwarf_expression_clear(mc_expression_t expression);
489 void mc_dwarf_expression_init(mc_expression_t expression, size_t len, Dwarf_Op* ops);
490
491 void mc_dwarf_location_list_clear(mc_location_list_t list);
492
493 void mc_dwarf_location_list_init_from_expression(mc_location_list_t target, size_t len, Dwarf_Op* ops);
494 void mc_dwarf_location_list_init(mc_location_list_t target, mc_object_info_t info, Dwarf_Die* die, Dwarf_Attribute* attr);
495
496 // ***** Variables and functions
497
498 struct s_dw_type{
499   e_dw_type_type type;
500   Dwarf_Off id; /* Offset in the section (in hexadecimal form) */
501   char *name; /* Name of the type */
502   int byte_size; /* Size in bytes */
503   int element_count; /* Number of elements for array type */
504   char *dw_type_id; /* DW_AT_type id */
505   xbt_dynar_t members; /* if DW_TAG_structure_type, DW_TAG_class_type, DW_TAG_union_type*/
506   int is_pointer_type;
507
508   // Location (for members) is either of:
509   struct s_mc_expression location;
510   int offset;
511
512   dw_type_t subtype; // DW_AT_type
513   dw_type_t full_type; // The same (but more complete) type
514 };
515
516 void* mc_member_resolve(const void* base, dw_type_t type, dw_type_t member, mc_snapshot_t snapshot);
517
518 typedef struct s_dw_variable{
519   Dwarf_Off dwarf_offset; /* Global offset of the field. */
520   int global;
521   char *name;
522   char *type_origin;
523   dw_type_t type;
524
525   // Use either of:
526   s_mc_location_list_t locations;
527   void* address;
528
529   size_t start_scope;
530   mc_object_info_t object_info;
531
532 }s_dw_variable_t, *dw_variable_t;
533
534 struct s_dw_frame{
535   int tag;
536   char *name;
537   void *low_pc;
538   void *high_pc;
539   s_mc_location_list_t frame_base;
540   xbt_dynar_t /* <dw_variable_t> */ variables; /* Cannot use dict, there may be several variables with the same name (in different lexical blocks)*/
541   unsigned long int id; /* DWARF offset of the subprogram */
542   xbt_dynar_t /* <dw_frame_t> */ scopes;
543   Dwarf_Off abstract_origin_id;
544   mc_object_info_t object_info;
545 };
546
547 struct s_mc_function_index_item {
548   void* low_pc, *high_pc;
549   dw_frame_t function;
550 };
551
552 void mc_frame_free(dw_frame_t freme);
553
554 void dw_type_free(dw_type_t t);
555 void dw_variable_free(dw_variable_t v);
556 void dw_variable_free_voidp(void *t);
557
558 void MC_dwarf_register_global_variable(mc_object_info_t info, dw_variable_t variable);
559 void MC_register_variable(mc_object_info_t info, dw_frame_t frame, dw_variable_t variable);
560 void MC_dwarf_register_non_global_variable(mc_object_info_t info, dw_frame_t frame, dw_variable_t variable);
561 void MC_dwarf_register_variable(mc_object_info_t info, dw_frame_t frame, dw_variable_t variable);
562
563 /** Find the DWARF offset for this ELF object
564  *
565  *  An offset is applied to address found in DWARF:
566  *
567  *  <ul>
568  *    <li>for an executable obejct, addresses are virtual address
569  *        (there is no offset) i.e. \f$\text{virtual address} = \{dwarf address}\f$;</li>
570  *    <li>for a shared object, the addreses are offset from the begining
571  *        of the shared object (the base address of the mapped shared
572  *        object must be used as offset
573  *        i.e. \f$\text{virtual address} = \text{shared object base address}
574  *             + \text{dwarf address}\f$.</li>
575  *
576  */
577 void* MC_object_base_address(mc_object_info_t info);
578
579 /********************************** DWARF **********************************/
580
581 #define MC_EXPRESSION_STACK_SIZE 64
582
583 #define MC_EXPRESSION_OK 0
584 #define MC_EXPRESSION_E_UNSUPPORTED_OPERATION 1
585 #define MC_EXPRESSION_E_STACK_OVERFLOW 2
586 #define MC_EXPRESSION_E_STACK_UNDERFLOW 3
587 #define MC_EXPRESSION_E_MISSING_STACK_CONTEXT 4
588 #define MC_EXPRESSION_E_MISSING_FRAME_BASE 5
589 #define MC_EXPRESSION_E_NO_BASE_ADDRESS 6
590
591 typedef struct s_mc_expression_state {
592   uintptr_t stack[MC_EXPRESSION_STACK_SIZE];
593   size_t stack_size;
594
595   unw_cursor_t* cursor;
596   void* frame_base;
597   mc_snapshot_t snapshot;
598   mc_object_info_t object_info;
599 } s_mc_expression_state_t, *mc_expression_state_t;
600
601 int mc_dwarf_execute_expression(size_t n, const Dwarf_Op* ops, mc_expression_state_t state);
602
603 void* mc_find_frame_base(dw_frame_t frame, mc_object_info_t object_info, unw_cursor_t* unw_cursor);
604
605 /********************************** Miscellaneous **********************************/
606
607 typedef struct s_local_variable{
608   dw_frame_t subprogram;
609   unsigned long ip;
610   char *name;
611   dw_type_t type;
612   void *address;
613   int region;
614 }s_local_variable_t, *local_variable_t;
615
616 /********************************* Communications pattern ***************************/
617
618 typedef struct s_mc_comm_pattern{
619   int num;
620   smx_action_t comm;
621   e_smx_comm_type_t type;
622   unsigned long src_proc;
623   unsigned long dst_proc;
624   const char *src_host;
625   const char *dst_host;
626   char *rdv;
627   ssize_t data_size;
628   void *data;
629 }s_mc_comm_pattern_t, *mc_comm_pattern_t;
630
631 extern xbt_dynar_t initial_communications_pattern;
632 extern xbt_dynar_t communications_pattern;
633 extern xbt_dynar_t incomplete_communications_pattern;
634
635 void get_comm_pattern(xbt_dynar_t communications_pattern, smx_simcall_t request, int call);
636 void complete_comm_pattern(xbt_dynar_t list, smx_action_t comm);
637 void MC_pre_modelcheck_comm_determinism(void);
638 void MC_modelcheck_comm_determinism(void);
639
640 /* *********** Sets *********** */
641
642 typedef struct s_mc_address_set *mc_address_set_t;
643
644 mc_address_set_t mc_address_set_new();
645 void mc_address_set_free(mc_address_set_t* p);
646 void mc_address_add(mc_address_set_t p, const void* value);
647 bool mc_address_test(mc_address_set_t p, const void* value);
648
649 /* *********** Hash *********** */
650
651 /** \brief Hash the current state
652  *  \param num_state number of states
653  *  \param stacks stacks (mc_snapshot_stak_t) used fot the stack unwinding informations
654  *  \result resulting hash
655  * */
656 uint64_t mc_hash_processes_state(int num_state, xbt_dynar_t stacks);
657
658 /* *********** Snapshot *********** */
659
660 static inline __attribute__((always_inline))
661 void* mc_translate_address_region(uintptr_t addr, mc_mem_region_t region)
662 {
663     size_t pageno = mc_page_number(region->start_addr, (void*) addr);
664     size_t snapshot_pageno = region->page_numbers[pageno];
665     const void* snapshot_page = mc_page_store_get_page(mc_model_checker->pages, snapshot_pageno);
666     return (char*) snapshot_page + mc_page_offset((void*) addr);
667 }
668
669 /** \brief Translate a pointer from process address space to snapshot address space
670  *
671  *  The address space contains snapshot of the main/application memory:
672  *  this function finds the address in a given snaphot for a given
673  *  real/application address.
674  *
675  *  For read only memory regions and other regions which are not int the
676  *  snapshot, the address is not changed.
677  *
678  *  \param addr     Application address
679  *  \param snapshot The snapshot of interest (if NULL no translation is done)
680  *  \return         Translated address in the snapshot address space
681  * */
682 static inline __attribute__((always_inline))
683 void* mc_translate_address(uintptr_t addr, mc_snapshot_t snapshot)
684 {
685
686   // If not in a process state/clone:
687   if (!snapshot) {
688     return (uintptr_t *) addr;
689   }
690
691   mc_mem_region_t region = mc_get_snapshot_region((void*) addr, snapshot);
692
693   xbt_assert(mc_region_contain(region, (void*) addr), "Trying to read out of the region boundary.");
694
695   if (!region) {
696     return (void *) addr;
697   }
698
699   // Flat snapshot:
700   else if (region->data) {
701     uintptr_t offset = addr - (uintptr_t) region->start_addr;
702     return (void *) ((uintptr_t) region->data + offset);
703   }
704
705   // Per-page snapshot:
706   else if (region->page_numbers) {
707     return mc_translate_address_region(addr, region);
708   }
709
710   else {
711     xbt_die("No data for this memory region");
712   }
713 }
714
715 static inline __attribute__ ((always_inline))
716   void* mc_snapshot_get_heap_end(mc_snapshot_t snapshot) {
717   if(snapshot==NULL)
718       xbt_die("snapshot is NULL");
719   void** addr = &((xbt_mheap_t)std_heap)->breakval;
720   return mc_snapshot_read_pointer(addr, snapshot);
721 }
722
723 static inline __attribute__ ((always_inline))
724 void* mc_snapshot_read_pointer(void* addr, mc_snapshot_t snapshot)
725 {
726   void* res;
727   return *(void**) mc_snapshot_read(addr, snapshot, &res, sizeof(void*));
728 }
729
730 /** @brief Read memory from a snapshot region
731  *
732  *  @param addr    Process (non-snapshot) address of the data
733  *  @param region  Snapshot memory region where the data is located
734  *  @param target  Buffer to store the value
735  *  @param size    Size of the data to read in bytes
736  *  @return Pointer where the data is located (target buffer of original location)
737  */
738 static inline __attribute__((always_inline))
739 void* mc_snapshot_read_region(void* addr, mc_mem_region_t region, void* target, size_t size)
740 {
741   uintptr_t offset = (uintptr_t) addr - (uintptr_t) region->start_addr;
742
743   xbt_assert(addr >= region->start_addr && (char*) addr+size < (char*)region->start_addr+region->size,
744     "Trying to read out of the region boundary.");
745
746   // Linear memory region:
747   if (region->data) {
748     return (void*) ((uintptr_t) region->data + offset);
749   }
750
751   // Fragmented memory region:
752   else if (region->page_numbers) {
753     void* end = (char*) addr + size - 1;
754     if( mc_same_page(addr, end) ) {
755       // The memory is contained in a single page:
756       return mc_translate_address_region((uintptr_t) addr, region);
757     } else {
758       // The memory spans several pages:
759       return mc_snapshot_read_fragmented(addr, region, target, size);
760     }
761   }
762
763   else {
764     xbt_die("No data available for this region");
765   }
766 }
767
768
769 SG_END_DECL()
770
771 #endif
772