Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
(wip) Move the MCed public API in the same file
[simgrid.git] / src / mc / mc_hash.c
1 /* Copyright (c) 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 #include <stdint.h>
8 #include <stdbool.h>
9
10 #include "mc_private.h"
11 #include "mc/datatypes.h"
12 #include <mc/mc.h>
13
14 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_hash, mc, "Logging specific to mc_hash");
15
16 // This is djb2:
17 typedef uint64_t mc_hash_t;
18 #define MC_HASH_INIT ((uint64_t)5381)
19
20 // #define MC_HASH(hash, value) hash = (((hash << 5) + hash) + (uint64_t) value)
21 #define MC_HASH(hash, value) \
22   { hash = (((hash << 5) + hash) + (uint64_t) value);\
23   XBT_DEBUG("%s:%i: %"PRIx64" -> %"PRIx64, __FILE__, __LINE__, (uint64_t) value, hash); }
24
25 // ***** Hash state
26
27 typedef struct s_mc_hashing_state {
28   // Set of pointers/addresses already processed (avoid loops):
29   mc_address_set_t handled_addresses;
30 } mc_hashing_state;
31
32 void mc_hash_state_init(mc_hashing_state * state);
33 void mc_hash_state_destroy(mc_hashing_state * state);
34
35 void mc_hash_state_init(mc_hashing_state * state)
36 {
37   state->handled_addresses = mc_address_set_new();
38 }
39
40 void mc_hash_state_destroy(mc_hashing_state * state)
41 {
42   mc_address_set_free(&state->handled_addresses);
43 }
44
45 // TODO, detect and avoid loops
46
47 static bool mc_ignored(const void *address, size_t size)
48 {
49   mc_heap_ignore_region_t region;
50   unsigned int cursor = 0;
51   const void *end = (char *) address + size;
52   xbt_dynar_foreach(mc_heap_comparison_ignore, cursor, region) {
53     void *istart = region->address;
54     void *iend = (char *) region->address + region->size;
55
56     if (address >= istart && address < iend && end >= istart && end < iend)
57       return true;
58   }
59
60   return false;
61 }
62
63 static void mc_hash_binary(mc_hash_t * hash, const void *s, size_t len)
64 {
65   const char *p = (const void *) s;
66   int i;
67   for (i = 0; i != len; ++i) {
68     MC_HASH(*hash, p[i]);
69   }
70 }
71
72 #if 0
73 /** \brief Compute a hash for a given value of a given type
74  *
75  *  We try to be very conservative (do not hash too ambiguous things).
76  *
77  *  \param address address of the variable
78  *  \param type type of the variable
79  * */
80 static void mc_hash_value(mc_hash_t * hash, mc_hashing_state * state,
81                           mc_object_info_t info, const void *address,
82                           dw_type_t type)
83 {
84   mc_process_t process = &mc_model_checker->process;
85 top:
86
87   switch (type->type) {
88
89     // Not relevant, do nothing:
90   case DW_TAG_unspecified_type:
91     return;
92
93     // Simple case, hash this has binary:
94   case DW_TAG_base_type:
95   case DW_TAG_enumeration_type:
96     {
97       if (mc_ignored(address, 1))
98         return;
99       mc_hash_binary(hash, address, type->byte_size);
100       return;
101     }
102
103   case DW_TAG_array_type:
104     {
105       if (mc_ignored(address, type->byte_size))
106         return;
107
108       long element_count = type->element_count;
109       dw_type_t subtype = type->subtype;
110       if (subtype == NULL) {
111         XBT_DEBUG("Hash array without subtype");
112         return;
113       }
114       int i;
115       for (i = 0; i != element_count; ++i) {
116         XBT_DEBUG("Hash array element %i", i);
117         void *subaddress = ((char *) address) + i * subtype->byte_size;
118         mc_hash_value(hash, state, info, subaddress, subtype);
119       }
120       return;
121     }
122
123     // Get the raw type:
124   case DW_TAG_typedef:
125   case DW_TAG_volatile_type:
126   case DW_TAG_const_type:
127   case DW_TAG_restrict_type:
128     {
129       type = type->subtype;
130       if (type == NULL)
131         return;
132       else
133         goto top;
134     }
135
136   case DW_TAG_structure_type:
137   case DW_TAG_class_type:
138     {
139       if (mc_ignored(address, type->byte_size))
140         return;
141
142       unsigned int cursor = 0;
143       dw_type_t member;
144       xbt_dynar_foreach(type->members, cursor, member) {
145         XBT_DEBUG("Hash struct member %s", member->name);
146         if (type->subtype == NULL)
147           return;
148         void *member_variable = mc_member_resolve(address, type, member, NULL);
149         mc_hash_value(hash, state, info, member_variable, type->subtype);
150       }
151       return;
152     }
153
154     // Pointer, we hash a single value but it might be an array.
155   case DW_TAG_pointer_type:
156   case DW_TAG_reference_type:
157   case DW_TAG_rvalue_reference_type:
158     {
159       if (mc_ignored(address, 1))
160         return;
161
162       void *pointed = *(void **) address;
163       if (pointed == NULL) {
164         XBT_DEBUG("Hashed pinter is NULL");
165         return;
166       }
167       // Avoid loops:
168       if (mc_address_test(state->handled_addresses, pointed)) {
169         XBT_DEBUG("Hashed pointed data %p already hashed", pointed);
170         return;
171       }
172       mc_address_add(state->handled_addresses, pointed);
173
174       // Anything outside the R/W segments and the heap is not hashed:
175       bool valid_pointer = (pointed >= (void *) binary_info->start_rw
176                             && pointed <= (void *) binary_info->end_rw)
177           || (pointed >= (void *) libsimgrid_info->start_rw
178               && pointed <= (void *) libsimgrid_info->end_rw)
179           || (pointed >= process->heap_address
180               && pointed < (void *) ((const char *) process->heap_address + STD_HEAP_SIZE));
181       if (!valid_pointer) {
182         XBT_DEBUG("Hashed pointed data %p is in an ignored range", pointed);
183         return;
184       }
185
186       if (type->subtype == NULL) {
187         XBT_DEBUG("Missing type for %p (type=%s)", pointed, type->dw_type_id);
188         return;
189       }
190
191       address = pointed;
192       type = type->subtype;
193       goto top;
194     }
195
196     // Skip this:
197   case DW_TAG_union_type:
198   case DW_TAG_subroutine_type:
199   default:
200     return;
201   }
202 }
203
204 static void mc_hash_object_globals(mc_hash_t * hash, mc_hashing_state * state,
205                                    mc_object_info_t info)
206 {
207   unsigned int cursor = 0;
208   dw_variable_t variable;
209   xbt_dynar_foreach(info->global_variables, cursor, variable) {
210     XBT_DEBUG("Hash global variable %s", variable->name);
211
212     if (variable->type_origin == NULL) {
213       // Nothing
214       continue;
215     }
216
217     dw_type_t type = variable->type;
218     if (type == NULL) {
219       // Nothing
220       continue;
221     }
222
223     const char *address = variable->address;
224     bool valid_pointer = (address >= binary_info->start_rw
225                           && address <= binary_info->end_rw)
226         || (address >= libsimgrid_info->start_rw
227             && address <= libsimgrid_info->end_rw)
228         || (address >= (const char *) process->heap_address
229             && address < (const char *) process->heap_address + STD_HEAP_SIZE);
230     if (!valid_pointer)
231       continue;
232
233     mc_hash_value(hash, state, info, variable->address, type);
234   }
235 }
236
237 static void mc_hash_stack_frame(mc_hash_t * hash,
238                                 mc_object_info_t info,
239                                 unw_cursor_t * unw_cursor, dw_frame_t frame,
240                                 char *frame_pointer, mc_hashing_state * state)
241 {
242
243   // return; // TEMP
244
245   unsigned int cursor = 0;
246   dw_variable_t variable;
247   xbt_dynar_foreach(frame->variables, cursor, variable) {
248
249     if (variable->type_origin == NULL) {
250       XBT_DEBUG("Hash local variable %s without type", variable->name);
251       continue;
252     }
253     if (variable->locations.size == 0) {
254       XBT_DEBUG("Hash local variable %s without location", variable->name);
255       continue;
256     }
257
258     XBT_DEBUG("Hash local variable %s", variable->name);
259
260     void *variable_address =
261         (void *) mc_dwarf_resolve_locations(&variable->locations,
262                                             variable->object_info, unw_cursor,
263                                             frame_pointer, NULL);
264
265     dw_type_t type = variable->type;
266     if (type == NULL) {
267       XBT_DEBUG("Hash local variable %s without loctypeation", variable->name);
268       continue;
269     }
270
271     mc_hash_value(hash, state, info, variable_address, type);
272   }
273
274   // TODO, handle nested scopes
275 }
276
277 static void mc_hash_stack(mc_hash_t * hash, mc_snapshot_stack_t stack,
278                           mc_hashing_state * state)
279 {
280
281   unsigned cursor = 0;
282   mc_stack_frame_t stack_frame;
283
284   xbt_dynar_foreach(stack->stack_frames, cursor, stack_frame) {
285
286     MC_HASH(*hash, stack_frame->ip);
287
288     mc_object_info_t info;
289     if (stack_frame->ip >= (unw_word_t) libsimgrid_info->start_exec
290         && stack_frame->ip < (unw_word_t) libsimgrid_info->end_exec)
291       info = libsimgrid_info;
292     else if (stack_frame->ip >= (unw_word_t) binary_info->start_exec
293              && stack_frame->ip < (unw_word_t) binary_info->end_exec)
294       info = binary_info;
295     else
296       continue;
297
298     mc_hash_stack_frame(hash, info, &(stack_frame->unw_cursor),
299                         stack_frame->frame, (void *) stack_frame->frame_base,
300                         state);
301
302   }
303 }
304
305 static void mc_hash_stacks(mc_hash_t * hash, mc_hashing_state * state,
306                            xbt_dynar_t stacks)
307 {
308   unsigned int cursor = 0;
309   mc_snapshot_stack_t current_stack;
310
311   MC_HASH(*hash, xbt_dynar_length(stacks_areas));
312
313   int i = 0;
314   xbt_dynar_foreach(stacks, cursor, current_stack) {
315     XBT_DEBUG("Stack %i", i);
316     mc_hash_stack(hash, current_stack, state);
317     ++i;
318   }
319 }
320 #endif
321
322 uint64_t mc_hash_processes_state(int num_state, xbt_dynar_t stacks)
323 {
324   XBT_DEBUG("START hash %i", num_state);
325
326   mc_hashing_state state;
327   mc_hash_state_init(&state);
328
329   mc_hash_t hash = MC_HASH_INIT;
330
331   MC_HASH(hash, xbt_swag_size(simix_global->process_list));     // process count
332   // mc_hash_object_globals(&hash, &state, binary_info);
333   // mc_hash_object_globals(&hash, &state, libsimgrid_info);
334   // mc_hash_stacks(&hash, &state, stacks);
335
336   mc_hash_state_destroy(&state);
337
338   XBT_DEBUG("END hash %i", num_state);
339   return hash;
340 }