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).
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 /***********************************************************************/
78 // ***** Model-checked
80 void MC_ignore_heap(void *address, size_t size)
82 if (mc_mode != MC_MODE_CLIENT)
85 s_mc_heap_ignore_region_t region;
86 memset(®ion, 0, sizeof(region));
87 region.address = address;
91 (char *) std_heap->heapbase) / BLOCKSIZE + 1;
92 if (std_heap->heapinfo[region.block].type == 0) {
94 std_heap->heapinfo[region.block].busy_block.ignore++;
97 ((uintptr_t) (ADDR2UINT(address) % (BLOCKSIZE))) >> std_heap->
98 heapinfo[region.block].type;
99 std_heap->heapinfo[region.block].busy_frag.ignore[region.fragment]++;
102 s_mc_ignore_heap_message_t message;
103 message.type = MC_MESSAGE_IGNORE_HEAP;
104 message.region = region;
105 if (MC_protocol_send(mc_client->fd, &message, sizeof(message)))
106 xbt_die("Could not send ignored region to MCer");
109 void MC_remove_ignore_heap(void *address, size_t size)
111 if (mc_mode != MC_MODE_CLIENT)
114 s_mc_ignore_memory_message_t message;
115 message.type = MC_MESSAGE_UNIGNORE_HEAP;
116 message.addr = address;
118 MC_client_send_message(&message, sizeof(message));
121 // ***** Model-checker:
123 void MC_ignore_global_variable(const char *name)
125 mc_process_t process = &mc_model_checker->process();
126 xbt_mheap_t heap = mmalloc_set_current_heap(mc_heap);
127 xbt_assert(process->object_infos, "MC subsystem not initialized");
129 size_t n = process->object_infos_size;
130 for (size_t i=0; i!=n; ++i) {
131 mc_object_info_t info = process->object_infos[i];
135 int end = xbt_dynar_length(info->global_variables) - 1;
136 while (start <= end) {
137 unsigned int cursor = (start + end) / 2;
138 dw_variable_t current_var =
139 (dw_variable_t) xbt_dynar_get_as(info->global_variables,
140 cursor, dw_variable_t);
141 if (strcmp(current_var->name, name) == 0) {
142 xbt_dynar_remove_at(info->global_variables, cursor, NULL);
144 end = xbt_dynar_length(info->global_variables) - 1;
145 } else if (strcmp(current_var->name, name) < 0) {
152 mmalloc_set_current_heap(heap);
155 /** \brief Ignore a local variable in a scope
157 * Ignore all instances of variables with a given name in
158 * any (possibly inlined) subprogram with a given namespaced
161 * \param var_name Name of the local variable (or parameter to ignore)
162 * \param subprogram_name Name of the subprogram fo ignore (NULL for any)
163 * \param subprogram (possibly inlined) Subprogram of the scope
164 * \param scope Current scope
166 static void mc_ignore_local_variable_in_scope(const char *var_name,
167 const char *subprogram_name,
168 dw_frame_t subprogram,
171 // Processing of direct variables:
173 // If the current subprogram matches the given name:
174 if (!subprogram_name ||
175 (subprogram->name && strcmp(subprogram_name, subprogram->name) == 0)) {
177 // Try to find the variable and remove it:
179 int end = xbt_dynar_length(scope->variables) - 1;
181 // Dichotomic search:
182 while (start <= end) {
183 int cursor = (start + end) / 2;
184 dw_variable_t current_var =
185 (dw_variable_t) xbt_dynar_get_as(scope->variables, cursor,
188 int compare = strcmp(current_var->name, var_name);
190 // Variable found, remove it:
191 xbt_dynar_remove_at(scope->variables, cursor, NULL);
195 end = xbt_dynar_length(scope->variables) - 1;
196 } else if (compare < 0) {
204 // And recursive processing in nested scopes:
206 dw_frame_t nested_scope = NULL;
207 xbt_dynar_foreach(scope->scopes, cursor, nested_scope) {
208 // The new scope may be an inlined subroutine, in this case we want to use its
209 // namespaced name in recursive calls:
210 dw_frame_t nested_subprogram =
212 DW_TAG_inlined_subroutine ? nested_scope : subprogram;
214 mc_ignore_local_variable_in_scope(var_name, subprogram_name,
215 nested_subprogram, nested_scope);
219 static void MC_ignore_local_variable_in_object(const char *var_name,
220 const char *subprogram_name,
221 mc_object_info_t info)
223 xbt_dict_cursor_t cursor2;
226 xbt_dict_foreach(info->subprograms, cursor2, key, frame) {
227 mc_ignore_local_variable_in_scope(var_name, subprogram_name, frame, frame);
232 void MC_ignore_local_variable(const char *var_name, const char *frame_name)
234 mc_process_t process = &mc_model_checker->process();
235 if (strcmp(frame_name, "*") == 0)
237 xbt_mheap_t heap = mmalloc_set_current_heap(mc_heap);
239 size_t n = process->object_infos_size;
241 for (i=0; i!=n; ++i) {
242 MC_ignore_local_variable_in_object(var_name, frame_name, process->object_infos[i]);
245 mmalloc_set_current_heap(heap);
248 void MC_stack_area_add(stack_region_t stack_area)
250 if (stacks_areas == NULL)
251 stacks_areas = xbt_dynar_new(sizeof(stack_region_t), NULL);
252 xbt_dynar_push(stacks_areas, &stack_area);
255 /** @brief Register a stack in the model checker
257 * The stacks are allocated in the heap. The MC handle them especially
258 * when we analyse/compare the content of the heap so it must be told where
259 * they are with this function.
262 * @param process Process owning the stack
264 * @param size Size of the stack
266 void MC_new_stack_area(void *stack, smx_process_t process, void *context, size_t size)
268 xbt_mheap_t heap = mmalloc_set_current_heap(mc_heap);
270 stack_region_t region = xbt_new0(s_stack_region_t, 1);
271 region->address = stack;
272 region->context = context;
276 (char *) std_heap->heapbase) / BLOCKSIZE + 1;
278 if (smpi_privatize_global_variables && process) {
279 region->process_index = smpi_process_index_of_smx_process(process);
282 region->process_index = -1;
284 if (mc_mode == MC_MODE_CLIENT) {
285 s_mc_stack_region_message_t message;
286 message.type = MC_MESSAGE_STACK_REGION;
287 message.stack_region = *region;
288 MC_client_send_message(&message, sizeof(message));
291 MC_stack_area_add(region);
293 mmalloc_set_current_heap(heap);
296 void MC_process_ignore_memory(mc_process_t process, void *addr, size_t size)
298 xbt_dynar_t checkpoint_ignore = process->checkpoint_ignore;
299 mc_checkpoint_ignore_region_t region =
300 xbt_new0(s_mc_checkpoint_ignore_region_t, 1);
304 if (xbt_dynar_is_empty(checkpoint_ignore)) {
305 xbt_dynar_push(checkpoint_ignore, ®ion);
308 unsigned int cursor = 0;
310 int end = xbt_dynar_length(checkpoint_ignore) - 1;
311 mc_checkpoint_ignore_region_t current_region = NULL;
313 while (start <= end) {
314 cursor = (start + end) / 2;
316 (mc_checkpoint_ignore_region_t) xbt_dynar_get_as(checkpoint_ignore,
318 mc_checkpoint_ignore_region_t);
319 if (current_region->addr == addr) {
320 if (current_region->size == size) {
321 checkpoint_ignore_region_free(region);
323 } else if (current_region->size < size) {
328 } else if (current_region->addr < addr) {
335 if (current_region->addr == addr) {
336 if (current_region->size < size) {
337 xbt_dynar_insert_at(checkpoint_ignore, cursor + 1, ®ion);
339 xbt_dynar_insert_at(checkpoint_ignore, cursor, ®ion);
341 } else if (current_region->addr < addr) {
342 xbt_dynar_insert_at(checkpoint_ignore, cursor + 1, ®ion);
344 xbt_dynar_insert_at(checkpoint_ignore, cursor, ®ion);