Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Fix null pointer dereference.
[simgrid.git] / src / mc / mc_request.cpp
1 /* Copyright (c) 2008-2019. The SimGrid Team. All rights reserved.          */
2
3 /* This program is free software; you can redistribute it and/or modify it
4  * under the terms of the license (GNU LGPL) which comes with this package. */
5
6 #include "src/mc/mc_request.hpp"
7 #include "src/include/mc/mc.h"
8 #include "src/kernel/activity/CommImpl.hpp"
9 #include "src/kernel/activity/MutexImpl.hpp"
10 #include "src/mc/ModelChecker.hpp"
11 #include "src/mc/mc_smx.hpp"
12
13 using simgrid::mc::remote;
14
15 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_request, mc,
16                                 "Logging specific to MC (request)");
17
18 static char *pointer_to_string(void *pointer);
19 static char *buff_size_to_string(size_t size);
20
21 static inline simgrid::kernel::activity::CommImpl* MC_get_comm(smx_simcall_t r)
22 {
23   switch (r->call ) {
24   case SIMCALL_COMM_WAIT:
25     return static_cast<simgrid::kernel::activity::CommImpl*>(simcall_comm_wait__getraw__comm(r));
26   case SIMCALL_COMM_TEST:
27     return static_cast<simgrid::kernel::activity::CommImpl*>(simcall_comm_test__getraw__comm(r));
28   default:
29     return nullptr;
30   }
31 }
32
33 static inline
34 smx_mailbox_t MC_get_mbox(smx_simcall_t r)
35 {
36   switch(r->call) {
37   case SIMCALL_COMM_ISEND:
38     return simcall_comm_isend__get__mbox(r);
39   case SIMCALL_COMM_IRECV:
40     return simcall_comm_irecv__get__mbox(r);
41   default:
42     return nullptr;
43   }
44 }
45
46 namespace simgrid {
47 namespace mc {
48
49 // Does half the job
50 static inline
51 bool request_depend_asymmetric(smx_simcall_t r1, smx_simcall_t r2)
52 {
53   if (r1->call == SIMCALL_COMM_ISEND && r2->call == SIMCALL_COMM_IRECV)
54     return false;
55
56   if (r1->call == SIMCALL_COMM_IRECV && r2->call == SIMCALL_COMM_ISEND)
57     return false;
58
59   // Those are internal requests, we do not need indirection
60   // because those objects are copies:
61   simgrid::kernel::activity::CommImpl* synchro1 = MC_get_comm(r1);
62   simgrid::kernel::activity::CommImpl* synchro2 = MC_get_comm(r2);
63
64   if ((r1->call == SIMCALL_COMM_ISEND || r1->call == SIMCALL_COMM_IRECV)
65       && r2->call == SIMCALL_COMM_WAIT) {
66
67     smx_mailbox_t mbox = MC_get_mbox(r1);
68
69     if (mbox != synchro2->mbox_cpy
70         && simcall_comm_wait__get__timeout(r2) <= 0)
71       return false;
72
73     if ((r1->issuer != synchro2->src_actor_.get()) && (r1->issuer != synchro2->dst_actor_.get()) &&
74         simcall_comm_wait__get__timeout(r2) <= 0)
75       return false;
76
77     if ((r1->call == SIMCALL_COMM_ISEND) && (synchro2->type == kernel::activity::CommImpl::Type::SEND) &&
78         (synchro2->src_buff_ != simcall_comm_isend__get__src_buff(r1)) && simcall_comm_wait__get__timeout(r2) <= 0)
79       return false;
80
81     if ((r1->call == SIMCALL_COMM_IRECV) && (synchro2->type == kernel::activity::CommImpl::Type::RECEIVE) &&
82         (synchro2->dst_buff_ != simcall_comm_irecv__get__dst_buff(r1)) && simcall_comm_wait__get__timeout(r2) <= 0)
83       return false;
84   }
85
86   /* FIXME: the following rule assumes that the result of the
87    * isend/irecv call is not stored in a buffer used in the
88    * test call. */
89 #if 0
90   if((r1->call == SIMCALL_COMM_ISEND || r1->call == SIMCALL_COMM_IRECV)
91      &&  r2->call == SIMCALL_COMM_TEST)
92      return false;
93 #endif
94
95   if (r1->call == SIMCALL_COMM_WAIT && (r2->call == SIMCALL_COMM_WAIT || r2->call == SIMCALL_COMM_TEST) &&
96       (synchro1->src_actor_.get() == nullptr || synchro1->dst_actor_.get() == nullptr))
97     return false;
98
99   if (r1->call == SIMCALL_COMM_TEST &&
100       (simcall_comm_test__get__comm(r1) == nullptr || synchro1->src_buff_ == nullptr || synchro1->dst_buff_ == nullptr))
101     return false;
102
103   if (r1->call == SIMCALL_COMM_TEST && r2->call == SIMCALL_COMM_WAIT && synchro1->src_buff_ == synchro2->src_buff_ &&
104       synchro1->dst_buff_ == synchro2->dst_buff_)
105     return false;
106
107   if (r1->call == SIMCALL_COMM_WAIT && r2->call == SIMCALL_COMM_TEST && synchro1->src_buff_ != nullptr &&
108       synchro1->dst_buff_ != nullptr && synchro2->src_buff_ != nullptr && synchro2->dst_buff_ != nullptr &&
109       synchro1->dst_buff_ != synchro2->src_buff_ && synchro1->dst_buff_ != synchro2->dst_buff_ &&
110       synchro2->dst_buff_ != synchro1->src_buff_)
111     return false;
112
113   return true;
114 }
115
116 // Those are internal_req
117 bool request_depend(smx_simcall_t r1, smx_simcall_t r2)
118 {
119   if (r1->issuer == r2->issuer)
120     return false;
121
122   /* Wait with timeout transitions are not considered by the independence theorem, thus we consider them as dependent with all other transitions */
123   if ((r1->call == SIMCALL_COMM_WAIT && simcall_comm_wait__get__timeout(r1) > 0)
124       || (r2->call == SIMCALL_COMM_WAIT
125           && simcall_comm_wait__get__timeout(r2) > 0))
126     return true;
127
128   if (r1->call != r2->call)
129     return request_depend_asymmetric(r1, r2)
130       && request_depend_asymmetric(r2, r1);
131
132   // Those are internal requests, we do not need indirection
133   // because those objects are copies:
134   simgrid::kernel::activity::CommImpl* synchro1 = MC_get_comm(r1);
135   simgrid::kernel::activity::CommImpl* synchro2 = MC_get_comm(r2);
136
137   switch(r1->call) {
138   case SIMCALL_COMM_ISEND:
139     return simcall_comm_isend__get__mbox(r1)
140       == simcall_comm_isend__get__mbox(r2);
141   case SIMCALL_COMM_IRECV:
142     return simcall_comm_irecv__get__mbox(r1)
143       == simcall_comm_irecv__get__mbox(r2);
144   case SIMCALL_COMM_WAIT:
145     if (synchro1->src_buff_ == synchro2->src_buff_ && synchro1->dst_buff_ == synchro2->dst_buff_)
146       return false;
147     if (synchro1->src_buff_ != nullptr && synchro1->dst_buff_ != nullptr && synchro2->src_buff_ != nullptr &&
148         synchro2->dst_buff_ != nullptr && synchro1->dst_buff_ != synchro2->src_buff_ &&
149         synchro1->dst_buff_ != synchro2->dst_buff_ && synchro2->dst_buff_ != synchro1->src_buff_)
150       return false;
151     return true;
152   default:
153     return true;
154   }
155 }
156
157 }
158 }
159
160 static char *pointer_to_string(void *pointer)
161 {
162
163   if (XBT_LOG_ISENABLED(mc_request, xbt_log_priority_verbose))
164     return bprintf("%p", pointer);
165
166   return xbt_strdup("(verbose only)");
167 }
168
169 static char *buff_size_to_string(size_t buff_size)
170 {
171
172   if (XBT_LOG_ISENABLED(mc_request, xbt_log_priority_verbose))
173     return bprintf("%zu", buff_size);
174
175   return xbt_strdup("(verbose only)");
176 }
177
178
179 std::string simgrid::mc::request_to_string(smx_simcall_t req, int value, simgrid::mc::RequestType request_type)
180 {
181   xbt_assert(mc_model_checker != nullptr, "Must be called from MCer");
182
183   bool use_remote_comm = true;
184   switch(request_type) {
185   case simgrid::mc::RequestType::simix:
186     use_remote_comm = true;
187     break;
188   case simgrid::mc::RequestType::executed:
189   case simgrid::mc::RequestType::internal:
190     use_remote_comm = false;
191     break;
192   default:
193     THROW_IMPOSSIBLE;
194   }
195
196   const char* type = nullptr;
197   char *args = nullptr;
198
199   smx_actor_t issuer = MC_smx_simcall_get_issuer(req);
200
201   switch (req->call) {
202
203   case SIMCALL_COMM_ISEND: {
204     type = "iSend";
205     char* p = pointer_to_string(simcall_comm_isend__get__src_buff(req));
206     char* bs = buff_size_to_string(simcall_comm_isend__get__src_buff_size(req));
207     if (issuer->get_host())
208       args = bprintf("src=(%ld)%s (%s), buff=%s, size=%s", issuer->get_pid(), MC_smx_actor_get_host_name(issuer),
209                      MC_smx_actor_get_name(issuer), p, bs);
210     else
211       args = bprintf("src=(%ld)%s, buff=%s, size=%s", issuer->get_pid(), MC_smx_actor_get_name(issuer), p, bs);
212     xbt_free(bs);
213     xbt_free(p);
214     break;
215   }
216
217   case SIMCALL_COMM_IRECV: {
218     size_t* remote_size = simcall_comm_irecv__get__dst_buff_size(req);
219     size_t size = 0;
220     if (remote_size)
221       mc_model_checker->process().read_bytes(&size, sizeof(size),
222         remote(remote_size));
223
224     type = "iRecv";
225     char* p = pointer_to_string(simcall_comm_irecv__get__dst_buff(req));
226     char* bs = buff_size_to_string(size);
227     if (issuer->get_host())
228       args = bprintf("dst=(%ld)%s (%s), buff=%s, size=%s", issuer->get_pid(), MC_smx_actor_get_host_name(issuer),
229                      MC_smx_actor_get_name(issuer), p, bs);
230     else
231       args = bprintf("dst=(%ld)%s, buff=%s, size=%s", issuer->get_pid(), MC_smx_actor_get_name(issuer), p, bs);
232     xbt_free(bs);
233     xbt_free(p);
234     break;
235   }
236
237   case SIMCALL_COMM_WAIT: {
238     simgrid::kernel::activity::CommImpl* remote_act =
239         static_cast<simgrid::kernel::activity::CommImpl*>(simcall_comm_wait__getraw__comm(req));
240     char* p;
241     if (value == -1) {
242       type = "WaitTimeout";
243       p = pointer_to_string(remote_act);
244       args = bprintf("comm=%s", p);
245     } else {
246       type = "Wait";
247       p = pointer_to_string(remote_act);
248
249       simgrid::mc::Remote<simgrid::kernel::activity::CommImpl> temp_synchro;
250       simgrid::kernel::activity::CommImpl* act;
251       if (use_remote_comm) {
252         mc_model_checker->process().read(temp_synchro,
253                                          remote(static_cast<simgrid::kernel::activity::CommImpl*>(remote_act)));
254         act = temp_synchro.getBuffer();
255       } else
256         act = remote_act;
257
258       smx_actor_t src_proc = mc_model_checker->process().resolveActor(simgrid::mc::remote(act->src_actor_.get()));
259       smx_actor_t dst_proc = mc_model_checker->process().resolveActor(simgrid::mc::remote(act->dst_actor_.get()));
260       args =
261           bprintf("comm=%s [(%ld)%s (%s)-> (%ld)%s (%s)]", p, src_proc ? src_proc->get_pid() : 0,
262                   src_proc ? MC_smx_actor_get_host_name(src_proc) : "", src_proc ? MC_smx_actor_get_name(src_proc) : "",
263                   dst_proc ? dst_proc->get_pid() : 0, dst_proc ? MC_smx_actor_get_host_name(dst_proc) : "",
264                   dst_proc ? MC_smx_actor_get_name(dst_proc) : "");
265     }
266     xbt_free(p);
267     break;
268   }
269
270   case SIMCALL_COMM_TEST: {
271     simgrid::kernel::activity::CommImpl* remote_act =
272         static_cast<simgrid::kernel::activity::CommImpl*>(simcall_comm_test__getraw__comm(req));
273     simgrid::mc::Remote<simgrid::kernel::activity::CommImpl> temp_synchro;
274     simgrid::kernel::activity::CommImpl* act;
275     if (use_remote_comm) {
276       mc_model_checker->process().read(temp_synchro,
277                                        remote(static_cast<simgrid::kernel::activity::CommImpl*>(remote_act)));
278       act = temp_synchro.getBuffer();
279     } else
280       act = remote_act;
281
282     char* p;
283     if (act->src_actor_.get() == nullptr || act->dst_actor_.get() == nullptr) {
284       type = "Test FALSE";
285       p = pointer_to_string(remote_act);
286       args = bprintf("comm=%s", p);
287     } else {
288       type = "Test TRUE";
289       p = pointer_to_string(remote_act);
290
291       smx_actor_t src_proc = mc_model_checker->process().resolveActor(simgrid::mc::remote(act->src_actor_.get()));
292       smx_actor_t dst_proc = mc_model_checker->process().resolveActor(simgrid::mc::remote(act->dst_actor_.get()));
293       args = bprintf("comm=%s [(%ld)%s (%s) -> (%ld)%s (%s)]", p, src_proc->get_pid(), MC_smx_actor_get_name(src_proc),
294                      MC_smx_actor_get_host_name(src_proc), dst_proc->get_pid(), MC_smx_actor_get_name(dst_proc),
295                      MC_smx_actor_get_host_name(dst_proc));
296     }
297     xbt_free(p);
298     break;
299   }
300
301   case SIMCALL_COMM_WAITANY: {
302     type = "WaitAny";
303     size_t count = simcall_comm_waitany__get__count(req);
304     if (count > 0) {
305       simgrid::kernel::activity::CommImpl* remote_sync;
306       remote_sync = mc_model_checker->process().read(remote(simcall_comm_waitany__get__comms(req) + value));
307       char* p     = pointer_to_string(remote_sync);
308       args        = bprintf("comm=%s (%d of %zu)", p, value + 1, count);
309       xbt_free(p);
310     } else
311       args = bprintf("comm at idx %d", value);
312     break;
313   }
314
315   case SIMCALL_COMM_TESTANY:
316     if (value == -1) {
317       type = "TestAny FALSE";
318       args = xbt_strdup("-");
319     } else {
320       type = "TestAny";
321       args =
322           bprintf("(%d of %zu)", value + 1,
323                     simcall_comm_testany__get__count(req));
324     }
325     break;
326
327   case SIMCALL_MUTEX_TRYLOCK:
328   case SIMCALL_MUTEX_LOCK: {
329     if (req->call == SIMCALL_MUTEX_LOCK)
330       type = "Mutex LOCK";
331     else
332       type = "Mutex TRYLOCK";
333
334     simgrid::mc::Remote<simgrid::kernel::activity::MutexImpl> mutex;
335     mc_model_checker->process().read_bytes(mutex.getBuffer(), sizeof(mutex),
336       remote(
337         req->call == SIMCALL_MUTEX_LOCK
338         ? simcall_mutex_lock__get__mutex(req)
339         : simcall_mutex_trylock__get__mutex(req)
340       ));
341     args = bprintf(
342         "locked = %d, owner = %d, sleeping = n/a", mutex.getBuffer()->locked,
343         mutex.getBuffer()->owner != nullptr
344             ? (int)mc_model_checker->process().resolveActor(simgrid::mc::remote(mutex.getBuffer()->owner))->get_pid()
345             : -1);
346     break;
347   }
348
349   case SIMCALL_MC_RANDOM:
350     type = "MC_RANDOM";
351     args = bprintf("%d", value);
352     break;
353
354   default:
355     type = SIMIX_simcall_name(req->call);
356     args = bprintf("??");
357     break;
358   }
359
360   std::string str;
361   if (args != nullptr)
362     str = simgrid::xbt::string_printf("[(%ld)%s (%s)] %s(%s)", issuer->get_pid(), MC_smx_actor_get_host_name(issuer),
363                                       MC_smx_actor_get_name(issuer), type, args);
364   else
365     str = simgrid::xbt::string_printf("[(%ld)%s (%s)] %s ", issuer->get_pid(), MC_smx_actor_get_host_name(issuer),
366                                       MC_smx_actor_get_name(issuer), type);
367   xbt_free(args);
368   return str;
369 }
370
371 namespace simgrid {
372 namespace mc {
373
374 bool request_is_enabled_by_idx(smx_simcall_t req, unsigned int idx)
375 {
376   simgrid::kernel::activity::CommImpl* remote_act = nullptr;
377   switch (req->call) {
378
379   case SIMCALL_COMM_WAIT:
380     /* FIXME: check also that src and dst processes are not suspended */
381     remote_act = simcall_comm_wait__getraw__comm(req);
382     break;
383
384   case SIMCALL_COMM_WAITANY:
385     remote_act = mc_model_checker->process().read(remote(simcall_comm_testany__get__comms(req) + idx));
386     break;
387
388   case SIMCALL_COMM_TESTANY:
389     remote_act = mc_model_checker->process().read(remote(simcall_comm_testany__get__comms(req) + idx));
390     break;
391
392   default:
393     return true;
394   }
395
396   simgrid::mc::Remote<simgrid::kernel::activity::CommImpl> temp_comm;
397   mc_model_checker->process().read(temp_comm, remote(remote_act));
398   simgrid::kernel::activity::CommImpl* comm = temp_comm.getBuffer();
399   return comm->src_actor_.get() && comm->dst_actor_.get();
400 }
401
402
403 static const char* colors[] = {
404   "blue",
405   "red",
406   "green3",
407   "goldenrod",
408   "brown",
409   "purple",
410   "magenta",
411   "turquoise4",
412   "gray25",
413   "forestgreen",
414   "hotpink",
415   "lightblue",
416   "tan",
417 };
418
419 static inline const char* get_color(int id)
420 {
421   return colors[id % (sizeof(colors) / sizeof(colors[0])) ];
422 }
423
424 std::string request_get_dot_output(smx_simcall_t req, int value)
425 {
426   std::string label;
427
428   const smx_actor_t issuer = MC_smx_simcall_get_issuer(req);
429
430   switch (req->call) {
431   case SIMCALL_COMM_ISEND:
432     if (issuer->get_host())
433       label = simgrid::xbt::string_printf("[(%ld)%s] iSend", issuer->get_pid(), MC_smx_actor_get_host_name(issuer));
434     else
435       label = bprintf("[(%ld)] iSend", issuer->get_pid());
436     break;
437
438   case SIMCALL_COMM_IRECV:
439     if (issuer->get_host())
440       label = simgrid::xbt::string_printf("[(%ld)%s] iRecv", issuer->get_pid(), MC_smx_actor_get_host_name(issuer));
441     else
442       label = simgrid::xbt::string_printf("[(%ld)] iRecv", issuer->get_pid());
443     break;
444
445   case SIMCALL_COMM_WAIT:
446     if (value == -1) {
447       if (issuer->get_host())
448         label =
449             simgrid::xbt::string_printf("[(%ld)%s] WaitTimeout", issuer->get_pid(), MC_smx_actor_get_host_name(issuer));
450       else
451         label = simgrid::xbt::string_printf("[(%ld)] WaitTimeout", issuer->get_pid());
452     } else {
453       simgrid::kernel::activity::ActivityImpl* remote_act = simcall_comm_wait__getraw__comm(req);
454       simgrid::mc::Remote<simgrid::kernel::activity::CommImpl> temp_comm;
455       mc_model_checker->process().read(temp_comm,
456                                        remote(static_cast<simgrid::kernel::activity::CommImpl*>(remote_act)));
457       simgrid::kernel::activity::CommImpl* comm = temp_comm.getBuffer();
458
459       smx_actor_t src_proc = mc_model_checker->process().resolveActor(simgrid::mc::remote(comm->src_actor_.get()));
460       smx_actor_t dst_proc = mc_model_checker->process().resolveActor(simgrid::mc::remote(comm->dst_actor_.get()));
461       if (issuer->get_host())
462         label = simgrid::xbt::string_printf("[(%ld)%s] Wait [(%ld)->(%ld)]", issuer->get_pid(),
463                                             MC_smx_actor_get_host_name(issuer), src_proc ? src_proc->get_pid() : 0,
464                                             dst_proc ? dst_proc->get_pid() : 0);
465       else
466         label = simgrid::xbt::string_printf("[(%ld)] Wait [(%ld)->(%ld)]", issuer->get_pid(),
467                                             src_proc ? src_proc->get_pid() : 0, dst_proc ? dst_proc->get_pid() : 0);
468     }
469     break;
470
471   case SIMCALL_COMM_TEST: {
472     simgrid::kernel::activity::ActivityImpl* remote_act = simcall_comm_test__getraw__comm(req);
473     simgrid::mc::Remote<simgrid::kernel::activity::CommImpl> temp_comm;
474     mc_model_checker->process().read(temp_comm, remote(static_cast<simgrid::kernel::activity::CommImpl*>(remote_act)));
475     simgrid::kernel::activity::CommImpl* comm = temp_comm.getBuffer();
476     if (comm->src_actor_.get() == nullptr || comm->dst_actor_.get() == nullptr) {
477       if (issuer->get_host())
478         label =
479             simgrid::xbt::string_printf("[(%ld)%s] Test FALSE", issuer->get_pid(), MC_smx_actor_get_host_name(issuer));
480       else
481         label = bprintf("[(%ld)] Test FALSE", issuer->get_pid());
482     } else {
483       if (issuer->get_host())
484         label =
485             simgrid::xbt::string_printf("[(%ld)%s] Test TRUE", issuer->get_pid(), MC_smx_actor_get_host_name(issuer));
486       else
487         label = simgrid::xbt::string_printf("[(%ld)] Test TRUE", issuer->get_pid());
488     }
489     break;
490   }
491
492   case SIMCALL_COMM_WAITANY: {
493     size_t comms_size = simcall_comm_waitany__get__count(req);
494     if (issuer->get_host())
495       label = simgrid::xbt::string_printf("[(%ld)%s] WaitAny [%d of %zu]", issuer->get_pid(),
496                                           MC_smx_actor_get_host_name(issuer), value + 1, comms_size);
497     else
498       label = simgrid::xbt::string_printf("[(%ld)] WaitAny [%d of %zu]", issuer->get_pid(), value + 1, comms_size);
499     break;
500   }
501
502   case SIMCALL_COMM_TESTANY:
503     if (value == -1) {
504       if (issuer->get_host())
505         label = simgrid::xbt::string_printf("[(%ld)%s] TestAny FALSE", issuer->get_pid(),
506                                             MC_smx_actor_get_host_name(issuer));
507       else
508         label = simgrid::xbt::string_printf("[(%ld)] TestAny FALSE", issuer->get_pid());
509     } else {
510       if (issuer->get_host())
511         label = simgrid::xbt::string_printf("[(%ld)%s] TestAny TRUE [%d of %lu]", issuer->get_pid(),
512                                             MC_smx_actor_get_host_name(issuer), value + 1,
513                                             simcall_comm_testany__get__count(req));
514       else
515         label = simgrid::xbt::string_printf("[(%ld)] TestAny TRUE [%d of %lu]", issuer->get_pid(), value + 1,
516                                             simcall_comm_testany__get__count(req));
517     }
518     break;
519
520   case SIMCALL_MUTEX_TRYLOCK:
521     label = simgrid::xbt::string_printf("[(%ld)] Mutex TRYLOCK", issuer->get_pid());
522     break;
523
524   case SIMCALL_MUTEX_LOCK:
525     label = simgrid::xbt::string_printf("[(%ld)] Mutex LOCK", issuer->get_pid());
526     break;
527
528   case SIMCALL_MC_RANDOM:
529     if (issuer->get_host())
530       label = simgrid::xbt::string_printf("[(%ld)%s] MC_RANDOM (%d)", issuer->get_pid(),
531                                           MC_smx_actor_get_host_name(issuer), value);
532     else
533       label = simgrid::xbt::string_printf("[(%ld)] MC_RANDOM (%d)", issuer->get_pid(), value);
534     break;
535
536   default:
537     THROW_UNIMPLEMENTED;
538   }
539
540   const char* color = get_color(issuer->get_pid() - 1);
541   return  simgrid::xbt::string_printf(
542         "label = \"%s\", color = %s, fontcolor = %s", label.c_str(),
543         color, color);
544 }
545
546 }
547 }