Logo AND Algorithmique Numérique Distribuée

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