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));
170 xbt_mheap_t heap = mmalloc_set_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);
203 mmalloc_set_current_heap(heap);
207 void MC_ignore_global_variable(const char *name)
209 mc_process_t process = &mc_model_checker->process;
210 xbt_mheap_t heap = mmalloc_set_current_heap(mc_heap);
211 xbt_assert(process->object_infos, "MC subsystem not initialized");
213 size_t n = process->object_infos_size;
214 for (size_t i=0; i!=n; ++i) {
215 mc_object_info_t info = process->object_infos[i];
219 int end = xbt_dynar_length(info->global_variables) - 1;
220 while (start <= end) {
221 unsigned int cursor = (start + end) / 2;
222 dw_variable_t current_var =
223 (dw_variable_t) xbt_dynar_get_as(info->global_variables,
224 cursor, dw_variable_t);
225 if (strcmp(current_var->name, name) == 0) {
226 xbt_dynar_remove_at(info->global_variables, cursor, NULL);
228 end = xbt_dynar_length(info->global_variables) - 1;
229 } else if (strcmp(current_var->name, name) < 0) {
236 mmalloc_set_current_heap(heap);
239 /** \brief Ignore a local variable in a scope
241 * Ignore all instances of variables with a given name in
242 * any (possibly inlined) subprogram with a given namespaced
245 * \param var_name Name of the local variable (or parameter to ignore)
246 * \param subprogram_name Name of the subprogram fo ignore (NULL for any)
247 * \param subprogram (possibly inlined) Subprogram of the scope
248 * \param scope Current scope
250 static void mc_ignore_local_variable_in_scope(const char *var_name,
251 const char *subprogram_name,
252 dw_frame_t subprogram,
255 // Processing of direct variables:
257 // If the current subprogram matches the given name:
258 if (!subprogram_name ||
259 (subprogram->name && strcmp(subprogram_name, subprogram->name) == 0)) {
261 // Try to find the variable and remove it:
263 int end = xbt_dynar_length(scope->variables) - 1;
265 // Dichotomic search:
266 while (start <= end) {
267 int cursor = (start + end) / 2;
268 dw_variable_t current_var =
269 (dw_variable_t) xbt_dynar_get_as(scope->variables, cursor,
272 int compare = strcmp(current_var->name, var_name);
274 // Variable found, remove it:
275 xbt_dynar_remove_at(scope->variables, cursor, NULL);
279 end = xbt_dynar_length(scope->variables) - 1;
280 } else if (compare < 0) {
288 // And recursive processing in nested scopes:
290 dw_frame_t nested_scope = NULL;
291 xbt_dynar_foreach(scope->scopes, cursor, nested_scope) {
292 // The new scope may be an inlined subroutine, in this case we want to use its
293 // namespaced name in recursive calls:
294 dw_frame_t nested_subprogram =
296 DW_TAG_inlined_subroutine ? nested_scope : subprogram;
298 mc_ignore_local_variable_in_scope(var_name, subprogram_name,
299 nested_subprogram, nested_scope);
303 static void MC_ignore_local_variable_in_object(const char *var_name,
304 const char *subprogram_name,
305 mc_object_info_t info)
307 xbt_dict_cursor_t cursor2;
310 xbt_dict_foreach(info->subprograms, cursor2, key, frame) {
311 mc_ignore_local_variable_in_scope(var_name, subprogram_name, frame, frame);
316 void MC_ignore_local_variable(const char *var_name, const char *frame_name)
318 mc_process_t process = &mc_model_checker->process;
319 if (strcmp(frame_name, "*") == 0)
321 xbt_mheap_t heap = mmalloc_set_current_heap(mc_heap);
323 size_t n = process->object_infos_size;
325 for (i=0; i!=n; ++i) {
326 MC_ignore_local_variable_in_object(var_name, frame_name, process->object_infos[i]);
329 mmalloc_set_current_heap(heap);
332 void MC_stack_area_add(stack_region_t stack_area)
334 if (stacks_areas == NULL)
335 stacks_areas = xbt_dynar_new(sizeof(stack_region_t), NULL);
336 xbt_dynar_push(stacks_areas, &stack_area);
339 /** @brief Register a stack in the model checker
341 * The stacks are allocated in the heap. The MC handle them especially
342 * when we analyse/compare the content of the heap so it must be told where
343 * they are with this function.
346 * @param process Process owning the stack
348 * @param size Size of the stack
350 void MC_new_stack_area(void *stack, smx_process_t process, void *context, size_t size)
352 xbt_mheap_t heap = mmalloc_set_current_heap(mc_heap);
354 stack_region_t region = xbt_new0(s_stack_region_t, 1);
355 region->address = stack;
356 region->context = context;
360 (char *) std_heap->heapbase) / BLOCKSIZE + 1;
362 if (smpi_privatize_global_variables && process) {
363 region->process_index = smpi_process_index_of_smx_process(process);
366 region->process_index = -1;
368 if (mc_mode == MC_MODE_CLIENT) {
369 s_mc_stack_region_message_t message;
370 message.type = MC_MESSAGE_STACK_REGION;
371 message.stack_region = *region;
372 MC_client_send_message(&message, sizeof(message));
375 MC_stack_area_add(region);
377 mmalloc_set_current_heap(heap);
380 void MC_process_ignore_memory(mc_process_t process, void *addr, size_t size)
382 xbt_dynar_t checkpoint_ignore = process->checkpoint_ignore;
383 mc_checkpoint_ignore_region_t region =
384 xbt_new0(s_mc_checkpoint_ignore_region_t, 1);
388 if (xbt_dynar_is_empty(checkpoint_ignore)) {
389 xbt_dynar_push(checkpoint_ignore, ®ion);
392 unsigned int cursor = 0;
394 int end = xbt_dynar_length(checkpoint_ignore) - 1;
395 mc_checkpoint_ignore_region_t current_region = NULL;
397 while (start <= end) {
398 cursor = (start + end) / 2;
400 (mc_checkpoint_ignore_region_t) xbt_dynar_get_as(checkpoint_ignore,
402 mc_checkpoint_ignore_region_t);
403 if (current_region->addr == addr) {
404 if (current_region->size == size) {
405 checkpoint_ignore_region_free(region);
407 } else if (current_region->size < size) {
412 } else if (current_region->addr < addr) {
419 if (current_region->addr == addr) {
420 if (current_region->size < size) {
421 xbt_dynar_insert_at(checkpoint_ignore, cursor + 1, ®ion);
423 xbt_dynar_insert_at(checkpoint_ignore, cursor, ®ion);
425 } else if (current_region->addr < addr) {
426 xbt_dynar_insert_at(checkpoint_ignore, cursor + 1, ®ion);
428 xbt_dynar_insert_at(checkpoint_ignore, cursor, ®ion);