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 // Those structures live with the MCer and should be moved in the model_checker
22 // structure but they are currently used before the MC initialisation
23 // (in standalone mode).
25 extern xbt_dynar_t mc_heap_comparison_ignore;
26 extern xbt_dynar_t stacks_areas;
28 /**************************** Structures ******************************/
29 typedef struct s_mc_stack_ignore_variable {
32 } s_mc_stack_ignore_variable_t, *mc_stack_ignore_variable_t;
34 /**************************** Free functions ******************************/
36 static void stack_ignore_variable_free(mc_stack_ignore_variable_t v)
38 xbt_free(v->var_name);
43 static void stack_ignore_variable_free_voidp(void *v)
45 stack_ignore_variable_free((mc_stack_ignore_variable_t) * (void **) v);
48 void heap_ignore_region_free(mc_heap_ignore_region_t r)
53 void heap_ignore_region_free_voidp(void *r)
55 heap_ignore_region_free((mc_heap_ignore_region_t) * (void **) r);
58 static void checkpoint_ignore_region_free(mc_checkpoint_ignore_region_t r)
63 static void checkpoint_ignore_region_free_voidp(void *r)
65 checkpoint_ignore_region_free((mc_checkpoint_ignore_region_t) * (void **) r);
68 xbt_dynar_t MC_checkpoint_ignore_new(void)
70 return xbt_dynar_new(sizeof(mc_checkpoint_ignore_region_t),
71 checkpoint_ignore_region_free_voidp);
74 /***********************************************************************/
77 void MC_heap_region_ignore_insert(mc_heap_ignore_region_t region)
79 if (mc_heap_comparison_ignore == NULL) {
80 mc_heap_comparison_ignore =
81 xbt_dynar_new(sizeof(mc_heap_ignore_region_t),
82 heap_ignore_region_free_voidp);
83 xbt_dynar_push(mc_heap_comparison_ignore, ®ion);
87 unsigned int cursor = 0;
88 mc_heap_ignore_region_t current_region = NULL;
90 int end = xbt_dynar_length(mc_heap_comparison_ignore) - 1;
92 // Find the position where we want to insert the mc_heap_ignore_region_t:
93 while (start <= end) {
94 cursor = (start + end) / 2;
96 (mc_heap_ignore_region_t) xbt_dynar_get_as(mc_heap_comparison_ignore,
98 mc_heap_ignore_region_t);
99 if (current_region->address == region->address) {
100 heap_ignore_region_free(region);
102 } else if (current_region->address < region->address) {
109 // Insert it mc_heap_ignore_region_t:
110 if (current_region->address < region->address)
111 xbt_dynar_insert_at(mc_heap_comparison_ignore, cursor + 1, ®ion);
113 xbt_dynar_insert_at(mc_heap_comparison_ignore, cursor, ®ion);
117 static void MC_heap_region_ignore_send(mc_heap_ignore_region_t region)
119 s_mc_ignore_heap_message_t message;
120 message.type = MC_MESSAGE_IGNORE_HEAP;
121 message.region = *region;
122 if (MC_protocol_send(mc_client->fd, &message, sizeof(message)))
123 xbt_die("Could not send ignored region to MCer");
127 void MC_ignore_heap(void *address, size_t size)
129 xbt_mheap_t heap = mmalloc_set_current_heap(mc_heap);
131 mc_heap_ignore_region_t region = xbt_new0(s_mc_heap_ignore_region_t, 1);
132 region->address = address;
137 (char *) std_heap->heapbase) / BLOCKSIZE + 1;
139 if (std_heap->heapinfo[region->block].type == 0) {
140 region->fragment = -1;
141 std_heap->heapinfo[region->block].busy_block.ignore++;
144 ((uintptr_t) (ADDR2UINT(address) % (BLOCKSIZE))) >> std_heap->
145 heapinfo[region->block].type;
146 std_heap->heapinfo[region->block].busy_frag.ignore[region->fragment]++;
149 MC_heap_region_ignore_insert(region);
152 if (mc_mode == MC_MODE_CLIENT)
153 MC_heap_region_ignore_send(region);
155 mmalloc_set_current_heap(heap);
158 void MC_remove_ignore_heap(void *address, size_t size)
160 if (mc_mode == MC_MODE_CLIENT) {
161 s_mc_ignore_memory_message_t message;
162 message.type = MC_MESSAGE_UNIGNORE_HEAP;
163 message.addr = address;
165 MC_client_send_message(&message, sizeof(message));
168 xbt_mheap_t heap = mmalloc_set_current_heap(mc_heap);
170 unsigned int cursor = 0;
172 int end = xbt_dynar_length(mc_heap_comparison_ignore) - 1;
173 mc_heap_ignore_region_t region;
174 int ignore_found = 0;
176 while (start <= end) {
177 cursor = (start + end) / 2;
179 (mc_heap_ignore_region_t) xbt_dynar_get_as(mc_heap_comparison_ignore,
181 mc_heap_ignore_region_t);
182 if (region->address == address) {
185 } else if (region->address < address) {
188 if ((char *) region->address <= ((char *) address + size)) {
197 if (ignore_found == 1) {
198 xbt_dynar_remove_at(mc_heap_comparison_ignore, cursor, NULL);
199 MC_remove_ignore_heap(address, size);
201 mmalloc_set_current_heap(heap);
205 void MC_ignore_global_variable(const char *name)
207 mc_process_t process = &mc_model_checker->process;
208 xbt_mheap_t heap = mmalloc_set_current_heap(mc_heap);
209 xbt_assert(process->object_infos, "MC subsystem not initialized");
211 size_t n = process->object_infos_size;
212 for (size_t i=0; i!=n; ++i) {
213 mc_object_info_t info = process->object_infos[i];
217 int end = xbt_dynar_length(info->global_variables) - 1;
218 while (start <= end) {
219 unsigned int cursor = (start + end) / 2;
220 dw_variable_t current_var =
221 (dw_variable_t) xbt_dynar_get_as(info->global_variables,
222 cursor, dw_variable_t);
223 if (strcmp(current_var->name, name) == 0) {
224 xbt_dynar_remove_at(info->global_variables, cursor, NULL);
226 end = xbt_dynar_length(info->global_variables) - 1;
227 } else if (strcmp(current_var->name, name) < 0) {
234 mmalloc_set_current_heap(heap);
237 /** \brief Ignore a local variable in a scope
239 * Ignore all instances of variables with a given name in
240 * any (possibly inlined) subprogram with a given namespaced
243 * \param var_name Name of the local variable (or parameter to ignore)
244 * \param subprogram_name Name of the subprogram fo ignore (NULL for any)
245 * \param subprogram (possibly inlined) Subprogram of the scope
246 * \param scope Current scope
248 static void mc_ignore_local_variable_in_scope(const char *var_name,
249 const char *subprogram_name,
250 dw_frame_t subprogram,
253 // Processing of direct variables:
255 // If the current subprogram matches the given name:
256 if (!subprogram_name ||
257 (subprogram->name && strcmp(subprogram_name, subprogram->name) == 0)) {
259 // Try to find the variable and remove it:
261 int end = xbt_dynar_length(scope->variables) - 1;
263 // Dichotomic search:
264 while (start <= end) {
265 int cursor = (start + end) / 2;
266 dw_variable_t current_var =
267 (dw_variable_t) xbt_dynar_get_as(scope->variables, cursor,
270 int compare = strcmp(current_var->name, var_name);
272 // Variable found, remove it:
273 xbt_dynar_remove_at(scope->variables, cursor, NULL);
277 end = xbt_dynar_length(scope->variables) - 1;
278 } else if (compare < 0) {
286 // And recursive processing in nested scopes:
288 dw_frame_t nested_scope = NULL;
289 xbt_dynar_foreach(scope->scopes, cursor, nested_scope) {
290 // The new scope may be an inlined subroutine, in this case we want to use its
291 // namespaced name in recursive calls:
292 dw_frame_t nested_subprogram =
294 DW_TAG_inlined_subroutine ? nested_scope : subprogram;
296 mc_ignore_local_variable_in_scope(var_name, subprogram_name,
297 nested_subprogram, nested_scope);
301 static void MC_ignore_local_variable_in_object(const char *var_name,
302 const char *subprogram_name,
303 mc_object_info_t info)
305 xbt_dict_cursor_t cursor2;
308 xbt_dict_foreach(info->subprograms, cursor2, key, frame) {
309 mc_ignore_local_variable_in_scope(var_name, subprogram_name, frame, frame);
314 void MC_ignore_local_variable(const char *var_name, const char *frame_name)
316 mc_process_t process = &mc_model_checker->process;
317 if (strcmp(frame_name, "*") == 0)
319 xbt_mheap_t heap = mmalloc_set_current_heap(mc_heap);
321 size_t n = process->object_infos_size;
323 for (i=0; i!=n; ++i) {
324 MC_ignore_local_variable_in_object(var_name, frame_name, process->object_infos[i]);
327 mmalloc_set_current_heap(heap);
330 void MC_stack_area_add(stack_region_t stack_area)
332 if (stacks_areas == NULL)
333 stacks_areas = xbt_dynar_new(sizeof(stack_region_t), NULL);
334 xbt_dynar_push(stacks_areas, &stack_area);
337 /** @brief Register a stack in the model checker
339 * The stacks are allocated in the heap. The MC handle them especially
340 * when we analyse/compare the content of the heap so it must be told where
341 * they are with this function.
344 * @param process Process owning the stack
346 * @param size Size of the stack
348 void MC_new_stack_area(void *stack, smx_process_t process, void *context, size_t size)
350 xbt_mheap_t heap = mmalloc_set_current_heap(mc_heap);
352 stack_region_t region = xbt_new0(s_stack_region_t, 1);
353 region->address = stack;
354 region->context = context;
358 (char *) std_heap->heapbase) / BLOCKSIZE + 1;
360 if (smpi_privatize_global_variables && process) {
361 region->process_index = smpi_process_index_of_smx_process(process);
364 region->process_index = -1;
366 if (mc_mode == MC_MODE_CLIENT) {
367 s_mc_stack_region_message_t message;
368 message.type = MC_MESSAGE_STACK_REGION;
369 message.stack_region = *region;
370 MC_client_send_message(&message, sizeof(message));
373 MC_stack_area_add(region);
375 mmalloc_set_current_heap(heap);
378 void MC_process_ignore_memory(mc_process_t process, void *addr, size_t size)
380 xbt_dynar_t checkpoint_ignore = process->checkpoint_ignore;
381 mc_checkpoint_ignore_region_t region =
382 xbt_new0(s_mc_checkpoint_ignore_region_t, 1);
386 if (xbt_dynar_is_empty(checkpoint_ignore)) {
387 xbt_dynar_push(checkpoint_ignore, ®ion);
390 unsigned int cursor = 0;
392 int end = xbt_dynar_length(checkpoint_ignore) - 1;
393 mc_checkpoint_ignore_region_t current_region = NULL;
395 while (start <= end) {
396 cursor = (start + end) / 2;
398 (mc_checkpoint_ignore_region_t) xbt_dynar_get_as(checkpoint_ignore,
400 mc_checkpoint_ignore_region_t);
401 if (current_region->addr == addr) {
402 if (current_region->size == size) {
403 checkpoint_ignore_region_free(region);
405 } else if (current_region->size < size) {
410 } else if (current_region->addr < addr) {
417 if (current_region->addr == addr) {
418 if (current_region->size < size) {
419 xbt_dynar_insert_at(checkpoint_ignore, cursor + 1, ®ion);
421 xbt_dynar_insert_at(checkpoint_ignore, cursor, ®ion);
423 } else if (current_region->addr < addr) {
424 xbt_dynar_insert_at(checkpoint_ignore, cursor + 1, ®ion);
426 xbt_dynar_insert_at(checkpoint_ignore, cursor, ®ion);