Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
save some hidden calls to Engine::get_instance
[simgrid.git] / teshsuite / mc / random-bug / random-bug.cpp
1 /* Copyright (c) 2014-2021. 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 <cstring>
7 #include <simgrid/modelchecker.h>
8 #include <simgrid/s4u.hpp>
9 #include <xbt/log.h>
10
11 XBT_LOG_NEW_DEFAULT_CATEGORY(random_bug, "For this example");
12
13 enum class Behavior { ABORT, ASSERT, PRINTF, SEGV };
14
15 Behavior behavior;
16
17 /** A fake application with a bug occurring for some random values */
18 static void app()
19 {
20   int x = MC_random(0, 5);
21   int y = MC_random(0, 5);
22   XBT_DEBUG("got %d %d", x, y);
23
24   if (behavior == Behavior::ASSERT) {
25     MC_assert(x != 3 || y != 4);
26   } else if (behavior == Behavior::PRINTF) {
27     if (x == 3 && y == 4)
28       XBT_ERROR("Error reached");
29   } else if (behavior == Behavior::ABORT) {
30     if (x == 3 && y == 4)
31       abort();
32   } else if (behavior == Behavior::SEGV) {
33 #ifndef __clang_analyzer__
34     int* A = nullptr;
35     if (x == 3 && y == 4)
36       *A = 1;
37 #endif
38   } else {
39     DIE_IMPOSSIBLE;
40   }
41 }
42
43 /** Main function */
44 int main(int argc, char* argv[])
45 {
46   simgrid::s4u::Engine e(&argc, argv);
47   xbt_assert(argc == 3, "Usage: random-bug abort|assert|printf|segv <platformfile>");
48   if (strcmp(argv[1], "abort") == 0) {
49     XBT_INFO("Behavior: abort");
50     behavior = Behavior::ABORT;
51   } else if (strcmp(argv[1], "assert") == 0) {
52     XBT_INFO("Behavior: assert");
53     behavior = Behavior::ASSERT;
54   } else if (strcmp(argv[1], "printf") == 0) {
55     XBT_INFO("Behavior: printf");
56     behavior = Behavior::PRINTF;
57   } else if (strcmp(argv[1], "segv") == 0) {
58     XBT_INFO("Behavior: segv");
59     behavior = Behavior::SEGV;
60   } else {
61     xbt_die("Please use either 'abort', 'assert', 'printf', or 'segv' as first parameter,"
62             " to specify what to do when the error is found.");
63   }
64
65   e.load_platform(argv[2]);
66
67   simgrid::s4u::Actor::create("app", e.host_by_name("Fafard"), &app);
68   e.run();
69 }