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