Logo AND Algorithmique Numérique Distribuée

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