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_region_message_t message;
115 message.type = MC_MESSAGE_IGNORE_REGION;
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)
128 int raw_mem_set = (mmalloc_get_current_heap() == mc_heap);
131 mc_heap_ignore_region_t region = NULL;
132 region = xbt_new0(s_mc_heap_ignore_region_t, 1);
133 region->address = address;
138 (char *) std_heap->heapbase) / BLOCKSIZE + 1;
140 if (std_heap->heapinfo[region->block].type == 0) {
141 region->fragment = -1;
142 std_heap->heapinfo[region->block].busy_block.ignore++;
145 ((uintptr_t) (ADDR2UINT(address) % (BLOCKSIZE))) >> std_heap->
146 heapinfo[region->block].type;
147 std_heap->heapinfo[region->block].busy_frag.ignore[region->fragment]++;
150 MC_heap_region_ignore_insert(region);
153 if (mc_mode == MC_MODE_CLIENT)
154 MC_heap_region_ignore_send(region);
161 void MC_remove_ignore_heap(void *address, size_t size)
164 int raw_mem_set = (mmalloc_get_current_heap() == mc_heap);
168 unsigned int cursor = 0;
170 int end = xbt_dynar_length(mc_heap_comparison_ignore) - 1;
171 mc_heap_ignore_region_t region;
172 int ignore_found = 0;
174 while (start <= end) {
175 cursor = (start + end) / 2;
177 (mc_heap_ignore_region_t) xbt_dynar_get_as(mc_heap_comparison_ignore,
179 mc_heap_ignore_region_t);
180 if (region->address == address) {
183 } else if (region->address < address) {
186 if ((char *) region->address <= ((char *) address + size)) {
195 if (ignore_found == 1) {
196 xbt_dynar_remove_at(mc_heap_comparison_ignore, cursor, NULL);
197 MC_remove_ignore_heap(address, size);
205 // FIXME, cross-process support?
206 void MC_ignore_global_variable(const char *name)
208 mc_process_t process = &mc_model_checker->process;
209 int raw_mem_set = (mmalloc_get_current_heap() == mc_heap);
213 xbt_assert(process->object_infos, "MC subsystem not initialized");
215 size_t n = process->object_infos_size;
216 for (size_t i=0; i!=n; ++i) {
217 mc_object_info_t info = process->object_infos[i];
221 int end = xbt_dynar_length(info->global_variables) - 1;
222 while (start <= end) {
223 unsigned int cursor = (start + end) / 2;
224 dw_variable_t current_var =
225 (dw_variable_t) xbt_dynar_get_as(info->global_variables,
226 cursor, dw_variable_t);
227 if (strcmp(current_var->name, name) == 0) {
228 xbt_dynar_remove_at(info->global_variables, cursor, NULL);
230 end = xbt_dynar_length(info->global_variables) - 1;
231 } else if (strcmp(current_var->name, name) < 0) {
243 /** \brief Ignore a local variable in a scope
245 * Ignore all instances of variables with a given name in
246 * any (possibly inlined) subprogram with a given namespaced
249 * \param var_name Name of the local variable (or parameter to ignore)
250 * \param subprogram_name Name of the subprogram fo ignore (NULL for any)
251 * \param subprogram (possibly inlined) Subprogram of the scope
252 * \param scope Current scope
254 // FIXME, cross-process support (messaging?)
255 static void mc_ignore_local_variable_in_scope(const char *var_name,
256 const char *subprogram_name,
257 dw_frame_t subprogram,
260 // Processing of direct variables:
262 // If the current subprogram matches the given name:
263 if (!subprogram_name ||
264 (subprogram->name && strcmp(subprogram_name, subprogram->name) == 0)) {
266 // Try to find the variable and remove it:
268 int end = xbt_dynar_length(scope->variables) - 1;
270 // Dichotomic search:
271 while (start <= end) {
272 int cursor = (start + end) / 2;
273 dw_variable_t current_var =
274 (dw_variable_t) xbt_dynar_get_as(scope->variables, cursor,
277 int compare = strcmp(current_var->name, var_name);
279 // Variable found, remove it:
280 xbt_dynar_remove_at(scope->variables, cursor, NULL);
284 end = xbt_dynar_length(scope->variables) - 1;
285 } else if (compare < 0) {
293 // And recursive processing in nested scopes:
295 dw_frame_t nested_scope = NULL;
296 xbt_dynar_foreach(scope->scopes, cursor, nested_scope) {
297 // The new scope may be an inlined subroutine, in this case we want to use its
298 // namespaced name in recursive calls:
299 dw_frame_t nested_subprogram =
301 DW_TAG_inlined_subroutine ? nested_scope : subprogram;
303 mc_ignore_local_variable_in_scope(var_name, subprogram_name,
304 nested_subprogram, nested_scope);
308 static void MC_ignore_local_variable_in_object(const char *var_name,
309 const char *subprogram_name,
310 mc_object_info_t info)
312 xbt_dict_cursor_t cursor2;
315 xbt_dict_foreach(info->subprograms, cursor2, key, frame) {
316 mc_ignore_local_variable_in_scope(var_name, subprogram_name, frame, frame);
320 void MC_ignore_local_variable(const char *var_name, const char *frame_name)
322 mc_process_t process = &mc_model_checker->process;
325 int raw_mem_set = (mmalloc_get_current_heap() == mc_heap);
327 if (strcmp(frame_name, "*") == 0)
332 size_t n = process->object_infos_size;
334 for (i=0; i!=n; ++i) {
335 MC_ignore_local_variable_in_object(var_name, frame_name, process->object_infos[i]);
343 /** @brief Register a stack in the model checker
345 * The stacks are allocated in the heap. The MC handle them especially
346 * when we analyse/compare the content of the heap so it must be told where
347 * they are with this function.
350 * @param process Process owning the stack
352 * @param size Size of the stack
354 void MC_new_stack_area(void *stack, smx_process_t process, void *context, size_t size)
357 int raw_mem_set = (mmalloc_get_current_heap() == mc_heap);
361 if (stacks_areas == NULL)
362 stacks_areas = xbt_dynar_new(sizeof(stack_region_t), NULL);
364 stack_region_t region = NULL;
365 region = xbt_new0(s_stack_region_t, 1);
366 region->address = stack;
367 region->process_name = process && process->name ? strdup(process->name) : NULL;
368 region->context = context;
372 (char *) std_heap->heapbase) / BLOCKSIZE + 1;
374 if (smpi_privatize_global_variables && process) {
375 region->process_index = smpi_process_index_of_smx_process(process);
378 region->process_index = -1;
380 xbt_dynar_push(stacks_areas, ®ion);
386 void MC_process_ignore_memory(mc_process_t process, void *addr, size_t size)
388 xbt_dynar_t checkpoint_ignore = process->checkpoint_ignore;
389 mc_checkpoint_ignore_region_t region =
390 xbt_new0(s_mc_checkpoint_ignore_region_t, 1);
394 if (xbt_dynar_is_empty(checkpoint_ignore)) {
395 xbt_dynar_push(checkpoint_ignore, ®ion);
398 unsigned int cursor = 0;
400 int end = xbt_dynar_length(checkpoint_ignore) - 1;
401 mc_checkpoint_ignore_region_t current_region = NULL;
403 while (start <= end) {
404 cursor = (start + end) / 2;
406 (mc_checkpoint_ignore_region_t) xbt_dynar_get_as(checkpoint_ignore,
408 mc_checkpoint_ignore_region_t);
409 if (current_region->addr == addr) {
410 if (current_region->size == size) {
411 checkpoint_ignore_region_free(region);
413 } else if (current_region->size < size) {
418 } else if (current_region->addr < addr) {
425 if (current_region->addr == addr) {
426 if (current_region->size < size) {
427 xbt_dynar_insert_at(checkpoint_ignore, cursor + 1, ®ion);
429 xbt_dynar_insert_at(checkpoint_ignore, cursor, ®ion);
431 } else if (current_region->addr < addr) {
432 xbt_dynar_insert_at(checkpoint_ignore, cursor + 1, ®ion);
434 xbt_dynar_insert_at(checkpoint_ignore, cursor, ®ion);