Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[mc] Do not execute MCer code in MCed mode in MC_remove_ignore_heap()
[simgrid.git] / src / mc / mc_ignore.cpp
1 /* Copyright (c) 2008-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 "internal_config.h"
8 #include "mc_object_info.h"
9 #include "mc_private.h"
10 #include "smpi/private.h"
11 #include "mc/mc_snapshot.h"
12 #include "mc_ignore.h"
13 #include "mc_protocol.h"
14 #include "mc_client.h"
15
16 extern "C" {
17
18 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_ignore, mc,
19                                 "Logging specific to MC ignore mechanism");
20
21
22 /**************************** Global variables ******************************/
23 // Those structures live with the MCer and should be moved in the model_checker
24 // structure but they are currently used before the MC initialisation
25 // (in standalone mode).
26
27 extern xbt_dynar_t mc_heap_comparison_ignore;
28 extern xbt_dynar_t stacks_areas;
29
30 /**************************** Structures ******************************/
31 typedef struct s_mc_stack_ignore_variable {
32   char *var_name;
33   char *frame;
34 } s_mc_stack_ignore_variable_t, *mc_stack_ignore_variable_t;
35
36 /**************************** Free functions ******************************/
37
38 static void stack_ignore_variable_free(mc_stack_ignore_variable_t v)
39 {
40   xbt_free(v->var_name);
41   xbt_free(v->frame);
42   xbt_free(v);
43 }
44
45 static void stack_ignore_variable_free_voidp(void *v)
46 {
47   stack_ignore_variable_free((mc_stack_ignore_variable_t) * (void **) v);
48 }
49
50 void heap_ignore_region_free(mc_heap_ignore_region_t r)
51 {
52   xbt_free(r);
53 }
54
55 void heap_ignore_region_free_voidp(void *r)
56 {
57   heap_ignore_region_free((mc_heap_ignore_region_t) * (void **) r);
58 }
59
60 static void checkpoint_ignore_region_free(mc_checkpoint_ignore_region_t r)
61 {
62   xbt_free(r);
63 }
64
65 static void checkpoint_ignore_region_free_voidp(void *r)
66 {
67   checkpoint_ignore_region_free((mc_checkpoint_ignore_region_t) * (void **) r);
68 }
69
70 xbt_dynar_t MC_checkpoint_ignore_new(void)
71 {
72   return xbt_dynar_new(sizeof(mc_checkpoint_ignore_region_t),
73                         checkpoint_ignore_region_free_voidp);
74 }
75
76 /***********************************************************************/
77
78 // Mcer
79 void MC_heap_region_ignore_insert(mc_heap_ignore_region_t region)
80 {
81   if (mc_heap_comparison_ignore == NULL) {
82     mc_heap_comparison_ignore =
83         xbt_dynar_new(sizeof(mc_heap_ignore_region_t),
84                       heap_ignore_region_free_voidp);
85     xbt_dynar_push(mc_heap_comparison_ignore, &region);
86     return;
87   }
88
89   unsigned int cursor = 0;
90   mc_heap_ignore_region_t current_region = NULL;
91   int start = 0;
92   int end = xbt_dynar_length(mc_heap_comparison_ignore) - 1;
93
94   // Find the position where we want to insert the mc_heap_ignore_region_t:
95   while (start <= end) {
96     cursor = (start + end) / 2;
97     current_region =
98         (mc_heap_ignore_region_t) xbt_dynar_get_as(mc_heap_comparison_ignore,
99                                                    cursor,
100                                                    mc_heap_ignore_region_t);
101     if (current_region->address == region->address) {
102       heap_ignore_region_free(region);
103       return;
104     } else if (current_region->address < region->address) {
105       start = cursor + 1;
106     } else {
107       end = cursor - 1;
108     }
109   }
110
111   // Insert it mc_heap_ignore_region_t:
112   if (current_region->address < region->address)
113     xbt_dynar_insert_at(mc_heap_comparison_ignore, cursor + 1, &region);
114   else
115     xbt_dynar_insert_at(mc_heap_comparison_ignore, cursor, &region);
116 }
117
118 // MCed:
119 static void MC_heap_region_ignore_send(mc_heap_ignore_region_t region)
120 {
121   s_mc_ignore_heap_message_t message;
122   message.type = MC_MESSAGE_IGNORE_HEAP;
123   message.region = *region;
124   if (MC_protocol_send(mc_client->fd, &message, sizeof(message)))
125     xbt_die("Could not send ignored region to MCer");
126 }
127
128 // MCed:
129 void MC_ignore_heap(void *address, size_t size)
130 {
131   xbt_mheap_t heap = mmalloc_set_current_heap(mc_heap);
132
133   mc_heap_ignore_region_t region = xbt_new0(s_mc_heap_ignore_region_t, 1);
134   region->address = address;
135   region->size = size;
136
137   region->block =
138       ((char *) address -
139        (char *) std_heap->heapbase) / BLOCKSIZE + 1;
140
141   if (std_heap->heapinfo[region->block].type == 0) {
142     region->fragment = -1;
143     std_heap->heapinfo[region->block].busy_block.ignore++;
144   } else {
145     region->fragment =
146         ((uintptr_t) (ADDR2UINT(address) % (BLOCKSIZE))) >> std_heap->
147         heapinfo[region->block].type;
148     std_heap->heapinfo[region->block].busy_frag.ignore[region->fragment]++;
149   }
150
151   MC_heap_region_ignore_insert(region);
152
153 #if 1
154   if (mc_mode == MC_MODE_CLIENT)
155     MC_heap_region_ignore_send(region);
156 #endif
157   mmalloc_set_current_heap(heap);
158 }
159
160 void MC_remove_ignore_heap(void *address, size_t size)
161 {
162   if (mc_mode == MC_MODE_CLIENT) {
163     s_mc_ignore_memory_message_t message;
164     message.type = MC_MESSAGE_UNIGNORE_HEAP;
165     message.addr = address;
166     message.size = size;
167     MC_client_send_message(&message, sizeof(message));
168     return;
169   }
170
171   xbt_mheap_t heap = mmalloc_set_current_heap(mc_heap);
172
173   unsigned int cursor = 0;
174   int start = 0;
175   int end = xbt_dynar_length(mc_heap_comparison_ignore) - 1;
176   mc_heap_ignore_region_t region;
177   int ignore_found = 0;
178
179   while (start <= end) {
180     cursor = (start + end) / 2;
181     region =
182         (mc_heap_ignore_region_t) xbt_dynar_get_as(mc_heap_comparison_ignore,
183                                                    cursor,
184                                                    mc_heap_ignore_region_t);
185     if (region->address == address) {
186       ignore_found = 1;
187       break;
188     } else if (region->address < address) {
189       start = cursor + 1;
190     } else {
191       if ((char *) region->address <= ((char *) address + size)) {
192         ignore_found = 1;
193         break;
194       } else {
195         end = cursor - 1;
196       }
197     }
198   }
199
200   if (ignore_found == 1) {
201     xbt_dynar_remove_at(mc_heap_comparison_ignore, cursor, NULL);
202     MC_remove_ignore_heap(address, size);
203   }
204   mmalloc_set_current_heap(heap);
205 }
206
207 // MCer
208 void MC_ignore_global_variable(const char *name)
209 {
210   mc_process_t process = &mc_model_checker->process();
211   xbt_mheap_t heap = mmalloc_set_current_heap(mc_heap);
212   xbt_assert(process->object_infos, "MC subsystem not initialized");
213
214   size_t n = process->object_infos_size;
215   for (size_t i=0; i!=n; ++i) {
216     mc_object_info_t info = process->object_infos[i];
217
218     // Binary search:
219     int start = 0;
220     int end = xbt_dynar_length(info->global_variables) - 1;
221     while (start <= end) {
222       unsigned int cursor = (start + end) / 2;
223       dw_variable_t current_var =
224           (dw_variable_t) xbt_dynar_get_as(info->global_variables,
225                                            cursor, dw_variable_t);
226       if (strcmp(current_var->name, name) == 0) {
227         xbt_dynar_remove_at(info->global_variables, cursor, NULL);
228         start = 0;
229         end = xbt_dynar_length(info->global_variables) - 1;
230       } else if (strcmp(current_var->name, name) < 0) {
231         start = cursor + 1;
232       } else {
233         end = cursor - 1;
234       }
235     }
236   }
237   mmalloc_set_current_heap(heap);
238 }
239
240 /** \brief Ignore a local variable in a scope
241  *
242  *  Ignore all instances of variables with a given name in
243  *  any (possibly inlined) subprogram with a given namespaced
244  *  name.
245  *
246  *  \param var_name        Name of the local variable (or parameter to ignore)
247  *  \param subprogram_name Name of the subprogram fo ignore (NULL for any)
248  *  \param subprogram      (possibly inlined) Subprogram of the scope
249  *  \param scope           Current scope
250  */
251 static void mc_ignore_local_variable_in_scope(const char *var_name,
252                                               const char *subprogram_name,
253                                               dw_frame_t subprogram,
254                                               dw_frame_t scope)
255 {
256   // Processing of direct variables:
257
258   // If the current subprogram matches the given name:
259   if (!subprogram_name ||
260       (subprogram->name && strcmp(subprogram_name, subprogram->name) == 0)) {
261
262     // Try to find the variable and remove it:
263     int start = 0;
264     int end = xbt_dynar_length(scope->variables) - 1;
265
266     // Dichotomic search:
267     while (start <= end) {
268       int cursor = (start + end) / 2;
269       dw_variable_t current_var =
270           (dw_variable_t) xbt_dynar_get_as(scope->variables, cursor,
271                                            dw_variable_t);
272
273       int compare = strcmp(current_var->name, var_name);
274       if (compare == 0) {
275         // Variable found, remove it:
276         xbt_dynar_remove_at(scope->variables, cursor, NULL);
277
278         // and start again:
279         start = 0;
280         end = xbt_dynar_length(scope->variables) - 1;
281       } else if (compare < 0) {
282         start = cursor + 1;
283       } else {
284         end = cursor - 1;
285       }
286     }
287
288   }
289   // And recursive processing in nested scopes:
290   unsigned cursor = 0;
291   dw_frame_t nested_scope = NULL;
292   xbt_dynar_foreach(scope->scopes, cursor, nested_scope) {
293     // The new scope may be an inlined subroutine, in this case we want to use its
294     // namespaced name in recursive calls:
295     dw_frame_t nested_subprogram =
296         nested_scope->tag ==
297         DW_TAG_inlined_subroutine ? nested_scope : subprogram;
298
299     mc_ignore_local_variable_in_scope(var_name, subprogram_name,
300                                       nested_subprogram, nested_scope);
301   }
302 }
303
304 static void MC_ignore_local_variable_in_object(const char *var_name,
305                                                const char *subprogram_name,
306                                                mc_object_info_t info)
307 {
308   xbt_dict_cursor_t cursor2;
309   dw_frame_t frame;
310   char *key;
311   xbt_dict_foreach(info->subprograms, cursor2, key, frame) {
312     mc_ignore_local_variable_in_scope(var_name, subprogram_name, frame, frame);
313   }
314 }
315
316 // MCer
317 void MC_ignore_local_variable(const char *var_name, const char *frame_name)
318 {
319   mc_process_t process = &mc_model_checker->process();
320   if (strcmp(frame_name, "*") == 0)
321     frame_name = NULL;
322   xbt_mheap_t heap = mmalloc_set_current_heap(mc_heap);
323
324   size_t n = process->object_infos_size;
325   size_t i;
326   for (i=0; i!=n; ++i) {
327     MC_ignore_local_variable_in_object(var_name, frame_name, process->object_infos[i]);
328   }
329
330   mmalloc_set_current_heap(heap);
331 }
332
333 void MC_stack_area_add(stack_region_t stack_area)
334 {
335   if (stacks_areas == NULL)
336     stacks_areas = xbt_dynar_new(sizeof(stack_region_t), NULL);
337   xbt_dynar_push(stacks_areas, &stack_area);
338 }
339
340 /** @brief Register a stack in the model checker
341  *
342  *  The stacks are allocated in the heap. The MC handle them especially
343  *  when we analyse/compare the content of the heap so it must be told where
344  *  they are with this function.
345  *
346  *  @param stack
347  *  @param process Process owning the stack
348  *  @param context
349  *  @param size    Size of the stack
350  */
351 void MC_new_stack_area(void *stack, smx_process_t process, void *context, size_t size)
352 {
353   xbt_mheap_t heap = mmalloc_set_current_heap(mc_heap);
354
355   stack_region_t region = xbt_new0(s_stack_region_t, 1);
356   region->address = stack;
357   region->context = context;
358   region->size = size;
359   region->block =
360       ((char *) stack -
361        (char *) std_heap->heapbase) / BLOCKSIZE + 1;
362 #ifdef HAVE_SMPI
363   if (smpi_privatize_global_variables && process) {
364     region->process_index = smpi_process_index_of_smx_process(process);
365   } else
366 #endif
367   region->process_index = -1;
368
369   if (mc_mode == MC_MODE_CLIENT) {
370     s_mc_stack_region_message_t message;
371     message.type = MC_MESSAGE_STACK_REGION;
372     message.stack_region = *region;
373     MC_client_send_message(&message, sizeof(message));
374   }
375
376   MC_stack_area_add(region);
377
378   mmalloc_set_current_heap(heap);
379 }
380
381 void MC_process_ignore_memory(mc_process_t process, void *addr, size_t size)
382 {
383   xbt_dynar_t checkpoint_ignore = process->checkpoint_ignore;
384   mc_checkpoint_ignore_region_t region =
385       xbt_new0(s_mc_checkpoint_ignore_region_t, 1);
386   region->addr = addr;
387   region->size = size;
388
389   if (xbt_dynar_is_empty(checkpoint_ignore)) {
390     xbt_dynar_push(checkpoint_ignore, &region);
391   } else {
392
393     unsigned int cursor = 0;
394     int start = 0;
395     int end = xbt_dynar_length(checkpoint_ignore) - 1;
396     mc_checkpoint_ignore_region_t current_region = NULL;
397
398     while (start <= end) {
399       cursor = (start + end) / 2;
400       current_region =
401           (mc_checkpoint_ignore_region_t) xbt_dynar_get_as(checkpoint_ignore,
402                                                            cursor,
403                                                            mc_checkpoint_ignore_region_t);
404       if (current_region->addr == addr) {
405         if (current_region->size == size) {
406           checkpoint_ignore_region_free(region);
407           return;
408         } else if (current_region->size < size) {
409           start = cursor + 1;
410         } else {
411           end = cursor - 1;
412         }
413       } else if (current_region->addr < addr) {
414         start = cursor + 1;
415       } else {
416         end = cursor - 1;
417       }
418     }
419
420     if (current_region->addr == addr) {
421       if (current_region->size < size) {
422         xbt_dynar_insert_at(checkpoint_ignore, cursor + 1, &region);
423       } else {
424         xbt_dynar_insert_at(checkpoint_ignore, cursor, &region);
425       }
426     } else if (current_region->addr < addr) {
427       xbt_dynar_insert_at(checkpoint_ignore, cursor + 1, &region);
428     } else {
429       xbt_dynar_insert_at(checkpoint_ignore, cursor, &region);
430     }
431   }
432 }
433
434 }