1 /* Copyright (c) 2008-2014. The SimGrid Team.
2 * All rights reserved. */
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. */
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"
18 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_ignore, mc,
19 "Logging specific to MC ignore mechanism");
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).
27 extern xbt_dynar_t mc_heap_comparison_ignore;
28 extern xbt_dynar_t stacks_areas;
30 /**************************** Structures ******************************/
31 typedef struct s_mc_stack_ignore_variable {
34 } s_mc_stack_ignore_variable_t, *mc_stack_ignore_variable_t;
36 /**************************** Free functions ******************************/
38 static void stack_ignore_variable_free(mc_stack_ignore_variable_t v)
40 xbt_free(v->var_name);
45 static void stack_ignore_variable_free_voidp(void *v)
47 stack_ignore_variable_free((mc_stack_ignore_variable_t) * (void **) v);
50 void heap_ignore_region_free(mc_heap_ignore_region_t r)
55 void heap_ignore_region_free_voidp(void *r)
57 heap_ignore_region_free((mc_heap_ignore_region_t) * (void **) r);
60 static void checkpoint_ignore_region_free(mc_checkpoint_ignore_region_t r)
65 static void checkpoint_ignore_region_free_voidp(void *r)
67 checkpoint_ignore_region_free((mc_checkpoint_ignore_region_t) * (void **) r);
70 xbt_dynar_t MC_checkpoint_ignore_new(void)
72 return xbt_dynar_new(sizeof(mc_checkpoint_ignore_region_t),
73 checkpoint_ignore_region_free_voidp);
76 /***********************************************************************/
79 void MC_heap_region_ignore_insert(mc_heap_ignore_region_t region)
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, ®ion);
89 unsigned int cursor = 0;
90 mc_heap_ignore_region_t current_region = NULL;
92 int end = xbt_dynar_length(mc_heap_comparison_ignore) - 1;
94 // Find the position where we want to insert the mc_heap_ignore_region_t:
95 while (start <= end) {
96 cursor = (start + end) / 2;
98 (mc_heap_ignore_region_t) xbt_dynar_get_as(mc_heap_comparison_ignore,
100 mc_heap_ignore_region_t);
101 if (current_region->address == region->address) {
102 heap_ignore_region_free(region);
104 } else if (current_region->address < region->address) {
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, ®ion);
115 xbt_dynar_insert_at(mc_heap_comparison_ignore, cursor, ®ion);
119 static void MC_heap_region_ignore_send(mc_heap_ignore_region_t region)
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");
129 void MC_ignore_heap(void *address, size_t size)
131 xbt_mheap_t heap = mmalloc_set_current_heap(mc_heap);
133 mc_heap_ignore_region_t region = xbt_new0(s_mc_heap_ignore_region_t, 1);
134 region->address = address;
139 (char *) std_heap->heapbase) / BLOCKSIZE + 1;
141 if (std_heap->heapinfo[region->block].type == 0) {
142 region->fragment = -1;
143 std_heap->heapinfo[region->block].busy_block.ignore++;
146 ((uintptr_t) (ADDR2UINT(address) % (BLOCKSIZE))) >> std_heap->
147 heapinfo[region->block].type;
148 std_heap->heapinfo[region->block].busy_frag.ignore[region->fragment]++;
151 MC_heap_region_ignore_insert(region);
154 if (mc_mode == MC_MODE_CLIENT)
155 MC_heap_region_ignore_send(region);
157 mmalloc_set_current_heap(heap);
160 void MC_remove_ignore_heap(void *address, size_t size)
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;
167 MC_client_send_message(&message, sizeof(message));
171 xbt_mheap_t heap = mmalloc_set_current_heap(mc_heap);
173 unsigned int cursor = 0;
175 int end = xbt_dynar_length(mc_heap_comparison_ignore) - 1;
176 mc_heap_ignore_region_t region;
177 int ignore_found = 0;
179 while (start <= end) {
180 cursor = (start + end) / 2;
182 (mc_heap_ignore_region_t) xbt_dynar_get_as(mc_heap_comparison_ignore,
184 mc_heap_ignore_region_t);
185 if (region->address == address) {
188 } else if (region->address < address) {
191 if ((char *) region->address <= ((char *) address + size)) {
200 if (ignore_found == 1) {
201 xbt_dynar_remove_at(mc_heap_comparison_ignore, cursor, NULL);
202 MC_remove_ignore_heap(address, size);
204 mmalloc_set_current_heap(heap);
208 void MC_ignore_global_variable(const char *name)
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");
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];
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);
229 end = xbt_dynar_length(info->global_variables) - 1;
230 } else if (strcmp(current_var->name, name) < 0) {
237 mmalloc_set_current_heap(heap);
240 /** \brief Ignore a local variable in a scope
242 * Ignore all instances of variables with a given name in
243 * any (possibly inlined) subprogram with a given namespaced
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
251 static void mc_ignore_local_variable_in_scope(const char *var_name,
252 const char *subprogram_name,
253 dw_frame_t subprogram,
256 // Processing of direct variables:
258 // If the current subprogram matches the given name:
259 if (!subprogram_name ||
260 (subprogram->name && strcmp(subprogram_name, subprogram->name) == 0)) {
262 // Try to find the variable and remove it:
264 int end = xbt_dynar_length(scope->variables) - 1;
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,
273 int compare = strcmp(current_var->name, var_name);
275 // Variable found, remove it:
276 xbt_dynar_remove_at(scope->variables, cursor, NULL);
280 end = xbt_dynar_length(scope->variables) - 1;
281 } else if (compare < 0) {
289 // And recursive processing in nested scopes:
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 =
297 DW_TAG_inlined_subroutine ? nested_scope : subprogram;
299 mc_ignore_local_variable_in_scope(var_name, subprogram_name,
300 nested_subprogram, nested_scope);
304 static void MC_ignore_local_variable_in_object(const char *var_name,
305 const char *subprogram_name,
306 mc_object_info_t info)
308 xbt_dict_cursor_t cursor2;
311 xbt_dict_foreach(info->subprograms, cursor2, key, frame) {
312 mc_ignore_local_variable_in_scope(var_name, subprogram_name, frame, frame);
317 void MC_ignore_local_variable(const char *var_name, const char *frame_name)
319 mc_process_t process = &mc_model_checker->process();
320 if (strcmp(frame_name, "*") == 0)
322 xbt_mheap_t heap = mmalloc_set_current_heap(mc_heap);
324 size_t n = process->object_infos_size;
326 for (i=0; i!=n; ++i) {
327 MC_ignore_local_variable_in_object(var_name, frame_name, process->object_infos[i]);
330 mmalloc_set_current_heap(heap);
333 void MC_stack_area_add(stack_region_t stack_area)
335 if (stacks_areas == NULL)
336 stacks_areas = xbt_dynar_new(sizeof(stack_region_t), NULL);
337 xbt_dynar_push(stacks_areas, &stack_area);
340 /** @brief Register a stack in the model checker
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.
347 * @param process Process owning the stack
349 * @param size Size of the stack
351 void MC_new_stack_area(void *stack, smx_process_t process, void *context, size_t size)
353 xbt_mheap_t heap = mmalloc_set_current_heap(mc_heap);
355 stack_region_t region = xbt_new0(s_stack_region_t, 1);
356 region->address = stack;
357 region->context = context;
361 (char *) std_heap->heapbase) / BLOCKSIZE + 1;
363 if (smpi_privatize_global_variables && process) {
364 region->process_index = smpi_process_index_of_smx_process(process);
367 region->process_index = -1;
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));
376 MC_stack_area_add(region);
378 mmalloc_set_current_heap(heap);
381 void MC_process_ignore_memory(mc_process_t process, void *addr, size_t size)
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);
389 if (xbt_dynar_is_empty(checkpoint_ignore)) {
390 xbt_dynar_push(checkpoint_ignore, ®ion);
393 unsigned int cursor = 0;
395 int end = xbt_dynar_length(checkpoint_ignore) - 1;
396 mc_checkpoint_ignore_region_t current_region = NULL;
398 while (start <= end) {
399 cursor = (start + end) / 2;
401 (mc_checkpoint_ignore_region_t) xbt_dynar_get_as(checkpoint_ignore,
403 mc_checkpoint_ignore_region_t);
404 if (current_region->addr == addr) {
405 if (current_region->size == size) {
406 checkpoint_ignore_region_free(region);
408 } else if (current_region->size < size) {
413 } else if (current_region->addr < addr) {
420 if (current_region->addr == addr) {
421 if (current_region->size < size) {
422 xbt_dynar_insert_at(checkpoint_ignore, cursor + 1, ®ion);
424 xbt_dynar_insert_at(checkpoint_ignore, cursor, ®ion);
426 } else if (current_region->addr < addr) {
427 xbt_dynar_insert_at(checkpoint_ignore, cursor + 1, ®ion);
429 xbt_dynar_insert_at(checkpoint_ignore, cursor, ®ion);