File: random-bug.cpp

package info (click to toggle)
simgrid 4.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 39,192 kB
  • sloc: cpp: 124,913; ansic: 66,744; python: 8,560; java: 6,773; fortran: 6,079; f90: 5,123; xml: 4,587; sh: 2,194; perl: 1,436; makefile: 111; lisp: 49; javascript: 7; sed: 6
file content (66 lines) | stat: -rw-r--r-- 2,009 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/* Copyright (c) 2014-2025. The SimGrid Team. All rights reserved.          */

/* This program is free software; you can redistribute it and/or modify it
 * under the terms of the license (GNU LGPL) which comes with this package. */

#include <csignal>
#include <cstring>
#include <simgrid/modelchecker.h>
#include <simgrid/s4u.hpp>
#include <xbt/log.h>

XBT_LOG_NEW_DEFAULT_CATEGORY(random_bug, "For this example");

enum class Behavior { ABORT, ASSERT, PRINTF, SEGV };

Behavior behavior;

/** A fake application with a bug occurring for some random values */
static void app()
{
  int x = MC_random(0, 5);
  int y = MC_random(0, 5);
  XBT_DEBUG("got %d %d", x, y);

  if (behavior == Behavior::ASSERT) {
    MC_assert(x != 3 || y != 4);
  } else if (behavior == Behavior::PRINTF) {
    if (x == 3 && y == 4)
      XBT_ERROR("Error reached");
  } else if (behavior == Behavior::ABORT) {
    if (x == 3 && y == 4)
      abort();
  } else if (behavior == Behavior::SEGV) {
    if (x == 3 && y == 4)
      raise(SIGSEGV); // Simulate a segfault without displeasing the static analyzers
  } else {
    DIE_IMPOSSIBLE;
  }
}

/** Main function */
int main(int argc, char* argv[])
{
  simgrid::s4u::Engine e(&argc, argv);
  xbt_assert(argc == 3, "Usage: random-bug abort|assert|printf|segv <platformfile>");
  if (strcmp(argv[1], "abort") == 0) {
    XBT_INFO("Behavior: abort");
    behavior = Behavior::ABORT;
  } else if (strcmp(argv[1], "assert") == 0) {
    XBT_INFO("Behavior: assert");
    behavior = Behavior::ASSERT;
  } else if (strcmp(argv[1], "printf") == 0) {
    XBT_INFO("Behavior: printf");
    behavior = Behavior::PRINTF;
  } else if (strcmp(argv[1], "segv") == 0) {
    XBT_INFO("Behavior: segv");
    behavior = Behavior::SEGV;
  } else {
    xbt_die("Please use either 'abort', 'assert', 'printf', or 'segv' as first parameter,"
            " to specify what to do when the error is found.");
  }

  e.load_platform(argv[2]);
  e.host_by_name("Fafard")->add_actor("app", &app);
  e.run();
}