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"
16 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_ignore, mc,
17 "Logging specific to MC ignore mechanism");
20 /**************************** Global variables ******************************/
22 extern xbt_dynar_t mc_heap_comparison_ignore;
23 extern xbt_dynar_t stacks_areas;
25 /**************************** Structures ******************************/
26 typedef struct s_mc_stack_ignore_variable {
29 } s_mc_stack_ignore_variable_t, *mc_stack_ignore_variable_t;
31 /**************************** Free functions ******************************/
33 static void stack_ignore_variable_free(mc_stack_ignore_variable_t v)
35 xbt_free(v->var_name);
40 static void stack_ignore_variable_free_voidp(void *v)
42 stack_ignore_variable_free((mc_stack_ignore_variable_t) * (void **) v);
45 void heap_ignore_region_free(mc_heap_ignore_region_t r)
50 void heap_ignore_region_free_voidp(void *r)
52 heap_ignore_region_free((mc_heap_ignore_region_t) * (void **) r);
55 static void checkpoint_ignore_region_free(mc_checkpoint_ignore_region_t r)
60 static void checkpoint_ignore_region_free_voidp(void *r)
62 checkpoint_ignore_region_free((mc_checkpoint_ignore_region_t) * (void **) r);
65 xbt_dynar_t MC_checkpoint_ignore_new(void)
67 return xbt_dynar_new(sizeof(mc_checkpoint_ignore_region_t),
68 checkpoint_ignore_region_free_voidp);
71 /***********************************************************************/
73 void MC_heap_region_ignore_insert(mc_heap_ignore_region_t region)
75 if (mc_heap_comparison_ignore == NULL) {
76 mc_heap_comparison_ignore =
77 xbt_dynar_new(sizeof(mc_heap_ignore_region_t),
78 heap_ignore_region_free_voidp);
79 xbt_dynar_push(mc_heap_comparison_ignore, ®ion);
83 unsigned int cursor = 0;
84 mc_heap_ignore_region_t current_region = NULL;
86 int end = xbt_dynar_length(mc_heap_comparison_ignore) - 1;
88 // Find the position where we want to insert the mc_heap_ignore_region_t:
89 while (start <= end) {
90 cursor = (start + end) / 2;
92 (mc_heap_ignore_region_t) xbt_dynar_get_as(mc_heap_comparison_ignore,
94 mc_heap_ignore_region_t);
95 if (current_region->address == region->address) {
96 heap_ignore_region_free(region);
98 } else if (current_region->address < region->address) {
105 // Insert it mc_heap_ignore_region_t:
106 if (current_region->address < region->address)
107 xbt_dynar_insert_at(mc_heap_comparison_ignore, cursor + 1, ®ion);
109 xbt_dynar_insert_at(mc_heap_comparison_ignore, cursor, ®ion);
112 void MC_heap_region_ignore_send(mc_heap_ignore_region_t region)
114 s_mc_ignore_heap_message_t message;
115 message.type = MC_MESSAGE_IGNORE_HEAP;
116 message.region = *region;
117 if (MC_protocol_send(mc_client->fd, &message, sizeof(message)))
118 xbt_die("Could not send ignored region to MCer");
119 XBT_DEBUG("Sent ignored region to the model-checker");
122 // FIXME, cross-process support? (or make this it is used on the app-side)
123 void MC_ignore_heap(void *address, size_t size)
125 int raw_mem_set = (mmalloc_get_current_heap() == mc_heap);
128 mc_heap_ignore_region_t region = xbt_new0(s_mc_heap_ignore_region_t, 1);
129 region->address = address;
134 (char *) std_heap->heapbase) / BLOCKSIZE + 1;
136 if (std_heap->heapinfo[region->block].type == 0) {
137 region->fragment = -1;
138 std_heap->heapinfo[region->block].busy_block.ignore++;
141 ((uintptr_t) (ADDR2UINT(address) % (BLOCKSIZE))) >> std_heap->
142 heapinfo[region->block].type;
143 std_heap->heapinfo[region->block].busy_frag.ignore[region->fragment]++;
146 MC_heap_region_ignore_insert(region);
149 if (mc_mode == MC_MODE_CLIENT)
150 MC_heap_region_ignore_send(region);
157 void MC_remove_ignore_heap(void *address, size_t size)
159 if (mc_mode == MC_MODE_CLIENT) {
160 s_mc_ignore_memory_message_t message;
161 message.type = MC_MESSAGE_UNIGNORE_HEAP;
162 message.addr = address;
164 MC_client_send_message(&message, sizeof(message));
167 int raw_mem_set = (mmalloc_get_current_heap() == mc_heap);
171 unsigned int cursor = 0;
173 int end = xbt_dynar_length(mc_heap_comparison_ignore) - 1;
174 mc_heap_ignore_region_t region;
175 int ignore_found = 0;
177 while (start <= end) {
178 cursor = (start + end) / 2;
180 (mc_heap_ignore_region_t) xbt_dynar_get_as(mc_heap_comparison_ignore,
182 mc_heap_ignore_region_t);
183 if (region->address == address) {
186 } else if (region->address < address) {
189 if ((char *) region->address <= ((char *) address + size)) {
198 if (ignore_found == 1) {
199 xbt_dynar_remove_at(mc_heap_comparison_ignore, cursor, NULL);
200 MC_remove_ignore_heap(address, size);
208 // FIXME, cross-process support?
209 void MC_ignore_global_variable(const char *name)
211 mc_process_t process = &mc_model_checker->process;
212 int raw_mem_set = (mmalloc_get_current_heap() == mc_heap);
216 xbt_assert(process->object_infos, "MC subsystem not initialized");
218 size_t n = process->object_infos_size;
219 for (size_t i=0; i!=n; ++i) {
220 mc_object_info_t info = process->object_infos[i];
224 int end = xbt_dynar_length(info->global_variables) - 1;
225 while (start <= end) {
226 unsigned int cursor = (start + end) / 2;
227 dw_variable_t current_var =
228 (dw_variable_t) xbt_dynar_get_as(info->global_variables,
229 cursor, dw_variable_t);
230 if (strcmp(current_var->name, name) == 0) {
231 xbt_dynar_remove_at(info->global_variables, cursor, NULL);
233 end = xbt_dynar_length(info->global_variables) - 1;
234 } else if (strcmp(current_var->name, name) < 0) {
246 /** \brief Ignore a local variable in a scope
248 * Ignore all instances of variables with a given name in
249 * any (possibly inlined) subprogram with a given namespaced
252 * \param var_name Name of the local variable (or parameter to ignore)
253 * \param subprogram_name Name of the subprogram fo ignore (NULL for any)
254 * \param subprogram (possibly inlined) Subprogram of the scope
255 * \param scope Current scope
257 // FIXME, cross-process support (messaging?)
258 static void mc_ignore_local_variable_in_scope(const char *var_name,
259 const char *subprogram_name,
260 dw_frame_t subprogram,
263 // Processing of direct variables:
265 // If the current subprogram matches the given name:
266 if (!subprogram_name ||
267 (subprogram->name && strcmp(subprogram_name, subprogram->name) == 0)) {
269 // Try to find the variable and remove it:
271 int end = xbt_dynar_length(scope->variables) - 1;
273 // Dichotomic search:
274 while (start <= end) {
275 int cursor = (start + end) / 2;
276 dw_variable_t current_var =
277 (dw_variable_t) xbt_dynar_get_as(scope->variables, cursor,
280 int compare = strcmp(current_var->name, var_name);
282 // Variable found, remove it:
283 xbt_dynar_remove_at(scope->variables, cursor, NULL);
287 end = xbt_dynar_length(scope->variables) - 1;
288 } else if (compare < 0) {
296 // And recursive processing in nested scopes:
298 dw_frame_t nested_scope = NULL;
299 xbt_dynar_foreach(scope->scopes, cursor, nested_scope) {
300 // The new scope may be an inlined subroutine, in this case we want to use its
301 // namespaced name in recursive calls:
302 dw_frame_t nested_subprogram =
304 DW_TAG_inlined_subroutine ? nested_scope : subprogram;
306 mc_ignore_local_variable_in_scope(var_name, subprogram_name,
307 nested_subprogram, nested_scope);
311 static void MC_ignore_local_variable_in_object(const char *var_name,
312 const char *subprogram_name,
313 mc_object_info_t info)
315 xbt_dict_cursor_t cursor2;
318 xbt_dict_foreach(info->subprograms, cursor2, key, frame) {
319 mc_ignore_local_variable_in_scope(var_name, subprogram_name, frame, frame);
323 void MC_ignore_local_variable(const char *var_name, const char *frame_name)
325 mc_process_t process = &mc_model_checker->process;
328 int raw_mem_set = (mmalloc_get_current_heap() == mc_heap);
330 if (strcmp(frame_name, "*") == 0)
335 size_t n = process->object_infos_size;
337 for (i=0; i!=n; ++i) {
338 MC_ignore_local_variable_in_object(var_name, frame_name, process->object_infos[i]);
346 /** @brief Register a stack in the model checker
348 * The stacks are allocated in the heap. The MC handle them especially
349 * when we analyse/compare the content of the heap so it must be told where
350 * they are with this function.
353 * @param process Process owning the stack
355 * @param size Size of the stack
357 void MC_new_stack_area(void *stack, smx_process_t process, void *context, size_t size)
360 int raw_mem_set = (mmalloc_get_current_heap() == mc_heap);
364 if (stacks_areas == NULL)
365 stacks_areas = xbt_dynar_new(sizeof(stack_region_t), NULL);
367 stack_region_t region = NULL;
368 region = xbt_new0(s_stack_region_t, 1);
369 region->address = stack;
370 region->process_name = process && process->name ? strdup(process->name) : NULL;
371 region->context = context;
375 (char *) std_heap->heapbase) / BLOCKSIZE + 1;
377 if (smpi_privatize_global_variables && process) {
378 region->process_index = smpi_process_index_of_smx_process(process);
381 region->process_index = -1;
383 xbt_dynar_push(stacks_areas, ®ion);
389 void MC_process_ignore_memory(mc_process_t process, void *addr, size_t size)
391 xbt_dynar_t checkpoint_ignore = process->checkpoint_ignore;
392 mc_checkpoint_ignore_region_t region =
393 xbt_new0(s_mc_checkpoint_ignore_region_t, 1);
397 if (xbt_dynar_is_empty(checkpoint_ignore)) {
398 xbt_dynar_push(checkpoint_ignore, ®ion);
401 unsigned int cursor = 0;
403 int end = xbt_dynar_length(checkpoint_ignore) - 1;
404 mc_checkpoint_ignore_region_t current_region = NULL;
406 while (start <= end) {
407 cursor = (start + end) / 2;
409 (mc_checkpoint_ignore_region_t) xbt_dynar_get_as(checkpoint_ignore,
411 mc_checkpoint_ignore_region_t);
412 if (current_region->addr == addr) {
413 if (current_region->size == size) {
414 checkpoint_ignore_region_free(region);
416 } else if (current_region->size < size) {
421 } else if (current_region->addr < addr) {
428 if (current_region->addr == addr) {
429 if (current_region->size < size) {
430 xbt_dynar_insert_at(checkpoint_ignore, cursor + 1, ®ion);
432 xbt_dynar_insert_at(checkpoint_ignore, cursor, ®ion);
434 } else if (current_region->addr < addr) {
435 xbt_dynar_insert_at(checkpoint_ignore, cursor + 1, ®ion);
437 xbt_dynar_insert_at(checkpoint_ignore, cursor, ®ion);