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 ******************************/
21 xbt_dynar_t mc_checkpoint_ignore;
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 /***********************************************************************/
67 void MC_heap_region_ignore_insert(mc_heap_ignore_region_t region)
69 int raw_mem_set = (mmalloc_get_current_heap() == mc_heap);
71 if (mc_heap_comparison_ignore == NULL) {
72 mc_heap_comparison_ignore =
73 xbt_dynar_new(sizeof(mc_heap_ignore_region_t),
74 heap_ignore_region_free_voidp);
75 xbt_dynar_push(mc_heap_comparison_ignore, ®ion);
81 unsigned int cursor = 0;
82 mc_heap_ignore_region_t current_region = NULL;
84 int end = xbt_dynar_length(mc_heap_comparison_ignore) - 1;
86 // Find the position where we want to insert the mc_heap_ignore_region_t:
87 while (start <= end) {
88 cursor = (start + end) / 2;
90 (mc_heap_ignore_region_t) xbt_dynar_get_as(mc_heap_comparison_ignore,
92 mc_heap_ignore_region_t);
93 if (current_region->address == region->address) {
94 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);
115 void MC_heap_region_ignore_send(mc_heap_ignore_region_t region)
117 s_mc_ignore_region_message_t message;
118 message.type = MC_MESSAGE_IGNORE_REGION;
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");
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)
131 int raw_mem_set = (mmalloc_get_current_heap() == mc_heap);
135 mc_heap_ignore_region_t region = NULL;
136 region = xbt_new0(s_mc_heap_ignore_region_t, 1);
137 region->address = address;
142 (char *) std_heap->heapbase) / BLOCKSIZE + 1;
144 if (std_heap->heapinfo[region->block].type == 0) {
145 region->fragment = -1;
146 std_heap->heapinfo[region->block].busy_block.ignore++;
149 ((uintptr_t) (ADDR2UINT(address) % (BLOCKSIZE))) >> std_heap->
150 heapinfo[region->block].type;
151 std_heap->heapinfo[region->block].busy_frag.ignore[region->fragment]++;
154 MC_heap_region_ignore_insert(region);
157 if (mc_mode == MC_MODE_CLIENT)
158 MC_heap_region_ignore_send(region);
165 void MC_remove_ignore_heap(void *address, size_t size)
168 int raw_mem_set = (mmalloc_get_current_heap() == mc_heap);
172 unsigned int cursor = 0;
174 int end = xbt_dynar_length(mc_heap_comparison_ignore) - 1;
175 mc_heap_ignore_region_t region;
176 int ignore_found = 0;
178 while (start <= end) {
179 cursor = (start + end) / 2;
181 (mc_heap_ignore_region_t) xbt_dynar_get_as(mc_heap_comparison_ignore,
183 mc_heap_ignore_region_t);
184 if (region->address == address) {
187 } else if (region->address < address) {
190 if ((char *) region->address <= ((char *) address + size)) {
199 if (ignore_found == 1) {
200 xbt_dynar_remove_at(mc_heap_comparison_ignore, cursor, NULL);
201 MC_remove_ignore_heap(address, size);
209 // FIXME, cross-process support?
210 void MC_ignore_global_variable(const char *name)
212 mc_process_t process = &mc_model_checker->process;
213 int raw_mem_set = (mmalloc_get_current_heap() == mc_heap);
217 xbt_assert(process->object_infos, "MC subsystem not initialized");
219 size_t n = process->object_infos_size;
220 for (size_t i=0; i!=n; ++i) {
221 mc_object_info_t info = process->object_infos[i];
225 int end = xbt_dynar_length(info->global_variables) - 1;
226 while (start <= end) {
227 unsigned int cursor = (start + end) / 2;
228 dw_variable_t current_var =
229 (dw_variable_t) xbt_dynar_get_as(info->global_variables,
230 cursor, dw_variable_t);
231 if (strcmp(current_var->name, name) == 0) {
232 xbt_dynar_remove_at(info->global_variables, cursor, NULL);
234 end = xbt_dynar_length(info->global_variables) - 1;
235 } else if (strcmp(current_var->name, name) < 0) {
247 /** \brief Ignore a local variable in a scope
249 * Ignore all instances of variables with a given name in
250 * any (possibly inlined) subprogram with a given namespaced
253 * \param var_name Name of the local variable (or parameter to ignore)
254 * \param subprogram_name Name of the subprogram fo ignore (NULL for any)
255 * \param subprogram (possibly inlined) Subprogram of the scope
256 * \param scope Current scope
258 // FIXME, cross-process support (messaging?)
259 static void mc_ignore_local_variable_in_scope(const char *var_name,
260 const char *subprogram_name,
261 dw_frame_t subprogram,
264 // Processing of direct variables:
266 // If the current subprogram matches the given name:
267 if (!subprogram_name ||
268 (subprogram->name && strcmp(subprogram_name, subprogram->name) == 0)) {
270 // Try to find the variable and remove it:
272 int end = xbt_dynar_length(scope->variables) - 1;
274 // Dichotomic search:
275 while (start <= end) {
276 int cursor = (start + end) / 2;
277 dw_variable_t current_var =
278 (dw_variable_t) xbt_dynar_get_as(scope->variables, cursor,
281 int compare = strcmp(current_var->name, var_name);
283 // Variable found, remove it:
284 xbt_dynar_remove_at(scope->variables, cursor, NULL);
288 end = xbt_dynar_length(scope->variables) - 1;
289 } else if (compare < 0) {
297 // And recursive processing in nested scopes:
299 dw_frame_t nested_scope = NULL;
300 xbt_dynar_foreach(scope->scopes, cursor, nested_scope) {
301 // The new scope may be an inlined subroutine, in this case we want to use its
302 // namespaced name in recursive calls:
303 dw_frame_t nested_subprogram =
305 DW_TAG_inlined_subroutine ? nested_scope : subprogram;
307 mc_ignore_local_variable_in_scope(var_name, subprogram_name,
308 nested_subprogram, nested_scope);
312 static void MC_ignore_local_variable_in_object(const char *var_name,
313 const char *subprogram_name,
314 mc_object_info_t info)
316 xbt_dict_cursor_t cursor2;
319 xbt_dict_foreach(info->subprograms, cursor2, key, frame) {
320 mc_ignore_local_variable_in_scope(var_name, subprogram_name, frame, frame);
324 void MC_ignore_local_variable(const char *var_name, const char *frame_name)
326 mc_process_t process = &mc_model_checker->process;
329 int raw_mem_set = (mmalloc_get_current_heap() == mc_heap);
331 if (strcmp(frame_name, "*") == 0)
336 size_t n = process->object_infos_size;
338 for (i=0; i!=n; ++i) {
339 MC_ignore_local_variable_in_object(var_name, frame_name, process->object_infos[i]);
347 /** @brief Register a stack in the model checker
349 * The stacks are allocated in the heap. The MC handle them especially
350 * when we analyse/compare the content of the heap so it must be told where
351 * they are with this function.
354 * @param process Process owning the stack
356 * @param size Size of the stack
358 void MC_new_stack_area(void *stack, smx_process_t process, void *context, size_t size)
361 int raw_mem_set = (mmalloc_get_current_heap() == mc_heap);
365 if (stacks_areas == NULL)
366 stacks_areas = xbt_dynar_new(sizeof(stack_region_t), NULL);
368 stack_region_t region = NULL;
369 region = xbt_new0(s_stack_region_t, 1);
370 region->address = stack;
371 region->process_name = process && process->name ? strdup(process->name) : NULL;
372 region->context = context;
376 (char *) std_heap->heapbase) / BLOCKSIZE + 1;
378 if (smpi_privatize_global_variables && process) {
379 region->process_index = smpi_process_index_of_smx_process(process);
382 region->process_index = -1;
384 xbt_dynar_push(stacks_areas, ®ion);
390 void MC_ignore(void *addr, size_t size)
393 int raw_mem_set = (mmalloc_get_current_heap() == mc_heap);
397 // FIXME, cross-process support
398 if (mc_checkpoint_ignore == NULL)
399 mc_checkpoint_ignore =
400 xbt_dynar_new(sizeof(mc_checkpoint_ignore_region_t),
401 checkpoint_ignore_region_free_voidp);
403 mc_checkpoint_ignore_region_t region =
404 xbt_new0(s_mc_checkpoint_ignore_region_t, 1);
408 if (xbt_dynar_is_empty(mc_checkpoint_ignore)) {
409 xbt_dynar_push(mc_checkpoint_ignore, ®ion);
412 unsigned int cursor = 0;
414 int end = xbt_dynar_length(mc_checkpoint_ignore) - 1;
415 mc_checkpoint_ignore_region_t current_region = NULL;
417 while (start <= end) {
418 cursor = (start + end) / 2;
420 (mc_checkpoint_ignore_region_t) xbt_dynar_get_as(mc_checkpoint_ignore,
422 mc_checkpoint_ignore_region_t);
423 if (current_region->addr == addr) {
424 if (current_region->size == size) {
425 checkpoint_ignore_region_free(region);
429 } else if (current_region->size < size) {
434 } else if (current_region->addr < addr) {
441 if (current_region->addr == addr) {
442 if (current_region->size < size) {
443 xbt_dynar_insert_at(mc_checkpoint_ignore, cursor + 1, ®ion);
445 xbt_dynar_insert_at(mc_checkpoint_ignore, cursor, ®ion);
447 } else if (current_region->addr < addr) {
448 xbt_dynar_insert_at(mc_checkpoint_ignore, cursor + 1, ®ion);
450 xbt_dynar_insert_at(mc_checkpoint_ignore, cursor, ®ion);