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 static void checkpoint_ignore_region_free(mc_checkpoint_ignore_region_t r)
55 static void checkpoint_ignore_region_free_voidp(void *r)
57 checkpoint_ignore_region_free((mc_checkpoint_ignore_region_t) * (void **) r);
60 xbt_dynar_t MC_checkpoint_ignore_new(void)
62 return xbt_dynar_new(sizeof(mc_checkpoint_ignore_region_t),
63 checkpoint_ignore_region_free_voidp);
66 /***********************************************************************/
68 // ***** Model-checked
70 void MC_ignore_heap(void *address, size_t size)
72 if (mc_mode != MC_MODE_CLIENT)
75 s_mc_heap_ignore_region_t region;
76 memset(®ion, 0, sizeof(region));
77 region.address = address;
81 (char *) std_heap->heapbase) / BLOCKSIZE + 1;
82 if (std_heap->heapinfo[region.block].type == 0) {
84 std_heap->heapinfo[region.block].busy_block.ignore++;
87 ((uintptr_t) (ADDR2UINT(address) % (BLOCKSIZE))) >> std_heap->
88 heapinfo[region.block].type;
89 std_heap->heapinfo[region.block].busy_frag.ignore[region.fragment]++;
92 s_mc_ignore_heap_message_t message;
93 message.type = MC_MESSAGE_IGNORE_HEAP;
94 message.region = region;
95 if (MC_protocol_send(mc_client->fd, &message, sizeof(message)))
96 xbt_die("Could not send ignored region to MCer");
99 void MC_remove_ignore_heap(void *address, size_t size)
101 if (mc_mode != MC_MODE_CLIENT)
104 s_mc_ignore_memory_message_t message;
105 message.type = MC_MESSAGE_UNIGNORE_HEAP;
106 message.addr = address;
108 MC_client_send_message(&message, sizeof(message));
111 void MC_ignore_global_variable(const char *name)
113 // TODO, send a message to the model_checker
114 xbt_die("Unimplemented");
117 void MC_stack_area_add(stack_region_t stack_area)
119 if (stacks_areas == NULL)
120 stacks_areas = xbt_dynar_new(sizeof(stack_region_t), NULL);
121 xbt_dynar_push(stacks_areas, &stack_area);
124 /** @brief Register a stack in the model checker
126 * The stacks are allocated in the heap. The MC handle them especially
127 * when we analyse/compare the content of the heap so it must be told where
128 * they are with this function.
131 * @param process Process owning the stack
133 * @param size Size of the stack
135 void MC_new_stack_area(void *stack, smx_process_t process, void *context, size_t size)
137 xbt_mheap_t heap = mmalloc_set_current_heap(mc_heap);
139 stack_region_t region = xbt_new0(s_stack_region_t, 1);
140 region->address = stack;
141 region->context = context;
145 (char *) std_heap->heapbase) / BLOCKSIZE + 1;
147 if (smpi_privatize_global_variables && process) {
148 region->process_index = smpi_process_index_of_smx_process(process);
151 region->process_index = -1;
153 if (mc_mode == MC_MODE_CLIENT) {
154 s_mc_stack_region_message_t message;
155 message.type = MC_MESSAGE_STACK_REGION;
156 message.stack_region = *region;
157 MC_client_send_message(&message, sizeof(message));
160 MC_stack_area_add(region);
162 mmalloc_set_current_heap(heap);
165 void MC_process_ignore_memory(mc_process_t process, void *addr, size_t size)
167 xbt_dynar_t checkpoint_ignore = process->checkpoint_ignore;
168 mc_checkpoint_ignore_region_t region =
169 xbt_new0(s_mc_checkpoint_ignore_region_t, 1);
173 if (xbt_dynar_is_empty(checkpoint_ignore)) {
174 xbt_dynar_push(checkpoint_ignore, ®ion);
177 unsigned int cursor = 0;
179 int end = xbt_dynar_length(checkpoint_ignore) - 1;
180 mc_checkpoint_ignore_region_t current_region = NULL;
182 while (start <= end) {
183 cursor = (start + end) / 2;
185 (mc_checkpoint_ignore_region_t) xbt_dynar_get_as(checkpoint_ignore,
187 mc_checkpoint_ignore_region_t);
188 if (current_region->addr == addr) {
189 if (current_region->size == size) {
190 checkpoint_ignore_region_free(region);
192 } else if (current_region->size < size) {
197 } else if (current_region->addr < addr) {
204 if (current_region->addr == addr) {
205 if (current_region->size < size) {
206 xbt_dynar_insert_at(checkpoint_ignore, cursor + 1, ®ion);
208 xbt_dynar_insert_at(checkpoint_ignore, cursor, ®ion);
210 } else if (current_region->addr < addr) {
211 xbt_dynar_insert_at(checkpoint_ignore, cursor + 1, ®ion);
213 xbt_dynar_insert_at(checkpoint_ignore, cursor, ®ion);