Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[mc] Fix cross-process access in MC_request_is_enabled() for SIMCALL_MUTEX_LOCK
[simgrid.git] / src / mc / mc_base.c
1 /* Copyright (c) 2008-2014. The SimGrid Team.
2  * All rights reserved.                                                     */
3
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. */
6
7 #include <assert.h>
8
9 #include <simgrid/simix.h>
10
11 #include "mc_base.h"
12 #include "../simix/smx_private.h"
13 #include "mc_record.h"
14
15 #ifdef HAVE_MC
16 #include "mc_process.h"
17 #include "mc_model_checker.h"
18 #include "mc_protocol.h"
19 #include "mc_smx.h"
20 #include "mc_server.h"
21 #endif
22
23 XBT_LOG_NEW_CATEGORY(mc, "All MC categories");
24
25 void MC_wait_for_requests(void)
26 {
27   if (mc_mode == MC_MODE_SERVER) {
28     MC_server_wait_client(&mc_model_checker->process);
29     return;
30   }
31
32   smx_process_t process;
33   smx_simcall_t req;
34   unsigned int iter;
35
36   while (!xbt_dynar_is_empty(simix_global->process_to_run)) {
37     SIMIX_process_runall();
38     xbt_dynar_foreach(simix_global->process_that_ran, iter, process) {
39       req = &process->simcall;
40       if (req->call != SIMCALL_NONE && !MC_request_is_visible(req))
41         SIMIX_simcall_handle(req, 0);
42     }
43   }
44 }
45
46 int MC_request_is_enabled(smx_simcall_t req)
47 {
48   unsigned int index = 0;
49   smx_synchro_t act = 0;
50 #ifdef HAVE_MC
51   s_smx_synchro_t temp_synchro;
52 #endif
53
54   switch (req->call) {
55   case SIMCALL_NONE:
56     return FALSE;
57
58   case SIMCALL_COMM_WAIT:
59     /* FIXME: check also that src and dst processes are not suspended */
60     act = simcall_comm_wait__get__comm(req);
61
62 #ifdef HAVE_MC
63     // Fetch from MCed memory:
64     if (!MC_process_is_self(&mc_model_checker->process)) {
65       MC_process_read(&mc_model_checker->process, MC_PROCESS_NO_FLAG,
66         &temp_synchro, act, sizeof(temp_synchro),
67         MC_PROCESS_INDEX_ANY);
68       act = &temp_synchro;
69     }
70 #endif
71
72     if (simcall_comm_wait__get__timeout(req) >= 0) {
73       /* If it has a timeout it will be always be enabled, because even if the
74        * communication is not ready, it can timeout and won't block. */
75       if (_sg_mc_timeout == 1)
76         return TRUE;
77     } else {
78       /* On the other hand if it hasn't a timeout, check if the comm is ready.*/
79       if (act->comm.detached && act->comm.src_proc == NULL
80           && act->comm.type == SIMIX_COMM_READY)
81         return (act->comm.dst_proc != NULL);
82     }
83     return (act->comm.src_proc && act->comm.dst_proc);
84
85   case SIMCALL_COMM_WAITANY: {
86 #ifdef HAVE_MC
87     // Read dynar:
88     s_xbt_dynar_t comms;
89     MC_process_read_simple(&mc_model_checker->process,
90       &comms, simcall_comm_waitany__get__comms(req), sizeof(comms));
91     // Read dynar buffer:
92     assert(comms.elmsize == sizeof(act));
93     size_t buffer_size = comms.elmsize * comms.used;
94     char buffer[buffer_size];
95     MC_process_read_simple(&mc_model_checker->process,
96       buffer, comms.data, sizeof(buffer));
97 #endif
98
99 #ifdef HAVE_MC
100     for (index = 0; index < comms.used; ++index) {
101       memcpy(&act, buffer + comms.elmsize * index, sizeof(act));
102 #else
103     xbt_dynar_foreach(simcall_comm_waitany__get__comms(req), index, act) {
104 #endif
105
106 #ifdef HAVE_MC
107       // Fetch from MCed memory:
108       if (!MC_process_is_self(&mc_model_checker->process)) {
109         MC_process_read(&mc_model_checker->process, MC_PROCESS_NO_FLAG,
110           &temp_synchro, act, sizeof(temp_synchro),
111           MC_PROCESS_INDEX_ANY);
112         act = &temp_synchro;
113       }
114 #endif
115
116       if (act->comm.src_proc && act->comm.dst_proc)
117         return TRUE;
118     }
119     return FALSE;
120   }
121
122   case SIMCALL_MUTEX_LOCK: {
123     smx_mutex_t mutex = simcall_mutex_lock__get__mutex(req);
124 #ifdef HAVE_MC
125     s_smx_mutex_t temp_mutex;
126     if (!MC_process_is_self(&mc_model_checker->process)) {
127       MC_process_read(&mc_model_checker->process, MC_PROCESS_NO_FLAG,
128         &temp_mutex, mutex, sizeof(temp_mutex),
129         MC_PROCESS_INDEX_ANY);
130       mutex = &temp_mutex;
131     }
132 #endif
133     if(mutex->owner == NULL)
134       return TRUE;
135     else
136 #ifdef HAVE_MC
137       // TODO, *(mutex->owner) :/
138       return MC_smx_resolve_process(mutex->owner)->pid ==
139         MC_smx_resolve_process(req->issuer)->pid;
140 #else
141       return mutex->owner->pid == req->issuer->pid;
142 #endif
143     }
144
145   default:
146     /* The rest of the requests are always enabled */
147     return TRUE;
148   }
149 }
150
151 int MC_request_is_visible(smx_simcall_t req)
152 {
153   return req->call == SIMCALL_COMM_ISEND
154       || req->call == SIMCALL_COMM_IRECV
155       || req->call == SIMCALL_COMM_WAIT
156       || req->call == SIMCALL_COMM_WAITANY
157       || req->call == SIMCALL_COMM_TEST
158       || req->call == SIMCALL_COMM_TESTANY
159       || req->call == SIMCALL_MC_RANDOM
160       || req->call == SIMCALL_MUTEX_LOCK
161 #ifdef HAVE_MC
162       || req->call == SIMCALL_MC_SNAPSHOT
163       || req->call == SIMCALL_MC_COMPARE_SNAPSHOTS
164 #endif
165       ;
166 }
167
168 int MC_random(int min, int max)
169 {
170   /*FIXME: return mc_current_state->executed_transition->random.value; */
171   return simcall_mc_random(min, max);
172 }
173
174 static int prng_random(int min, int max)
175 {
176   unsigned long output_size = ((unsigned long) max - (unsigned long) min) + 1;
177   unsigned long input_size = (unsigned long) RAND_MAX + 1;
178   unsigned long reject_size = input_size % output_size;
179   unsigned long accept_size = input_size - reject_size; // module*accept_size
180
181   // Use rejection in order to avoid skew
182   long x;
183   do {
184 #ifndef _XBT_WIN32
185     x = random();
186 #else
187     x = rand();
188 #endif
189   } while( x >= accept_size );
190   return min + (x % output_size);
191 }
192
193 int simcall_HANDLER_mc_random(smx_simcall_t simcall, int min, int max)
194 {
195   if (!MC_is_active() && !MC_record_path){
196     return prng_random(min, max);
197   }
198
199   return simcall->mc_value;
200 }