Belle II Software  release-05-02-19
TheKillerModule.cc
1 /**************************************************************************
2  * BASF2 (Belle Analysis Framework 2) *
3  * Copyright(C) 2018 - Belle II Collaboration *
4  * *
5  * Author: The Belle II Collaboration *
6  * Contributors: Martin Ritter *
7  * *
8  * This software is provided "as is" without any warranty. *
9  **************************************************************************/
10 
11 #include <framework/modules/core/TheKillerModule.h>
12 #include <csignal>
13 #include <boost/algorithm/string.hpp>
14 
15 using namespace Belle2;
16 
17 //-----------------------------------------------------------------
18 // Register the Module
19 //-----------------------------------------------------------------
20 REG_MODULE(TheKiller);
21 
22 //-----------------------------------------------------------------
23 // Implementation
24 //-----------------------------------------------------------------
25 
27 {
28  // Set module properties
29  setDescription(R"DOC("This Modules kills basf2 as horribly as possible (or as selected)
30 
31 With this module you can kill basf2 in a variety of ways to test what happens if processing is interrupted by
32 
33 * std::abort()
34 * std::terminate()
35 * std::quick_exit()
36 * std::exit(N)
37 * uncaught exception
38 * signal
39 * segfault
40 * bus error
41 
42 This error can occur in a selected event to test behavior during processing.)DOC");
43 
44  // Parameter definitions
45  addParam("method", m_methodStr, "How to kill the event, one of (abort, terminate, "
46  "quick_exit, exit, exception, signal, segfault, buserror)", std::string("abort"));
47  addParam("parameter", m_parameter, "Optional parameter for the kill method: for "
48  "quick_exit and exit it is the return code, for signal it is the signal number", 0u);
49  addParam("event", m_eventToKill, "In which event to kill the processing");
50 }
51 
53 {
54  boost::to_lower(m_methodStr);
55  if (m_methodStr == "abort") m_method = EMethod::c_abort;
56  else if (m_methodStr == "terminate") m_method = EMethod::c_terminate;
57  else if (m_methodStr == "quick_exit") m_method = EMethod::c_quick_exit;
58  else if (m_methodStr == "exit") m_method = EMethod::c_exit;
59  else if (m_methodStr == "exception") m_method = EMethod::c_exception;
60  else if (m_methodStr == "signal") m_method = EMethod::c_signal;
61  else if (m_methodStr == "segfault") m_method = EMethod::c_segfault;
62  else if (m_methodStr == "buserror") m_method = EMethod::c_buserror;
63  else B2ERROR("Unknown method , choose one of (abort, terminate, quick_exit, exit, "
64  "exception, signal, segfault)" << LogVar("method", m_methodStr));
65 }
66 
68 {
69  if (++m_event < m_eventToKill) return;
70  switch (m_method) {
71  case EMethod::c_abort:
72  std::abort();
74  std::terminate();
76  std::quick_exit(m_parameter);
77  case EMethod::c_exit:
78  std::exit(m_parameter);
80  throw std::runtime_error("DIE! DIE! DIE!");
81  case EMethod::c_signal:
82  if (raise(m_parameter) != 0) B2FATAL("Invalid signal number" << LogVar("signal", m_parameter));
83  break;
85 #ifndef __clang_analyzer__
86  {
87  // this is an intentional nullptr dereference so lets hide it from clang analyzer
88  volatile int* foo {nullptr};
89  *foo = 5;
90  }
91 #endif
92  break;
94 #ifndef __clang_analyzer__
95  {
96 #if defined(__GNUC__) && defined(__x86_64__)
97  __asm__("pushf\norl $0x40000,(%rsp)\npopf");
98 #endif
99  auto* cptr = (char*) malloc(sizeof(int) + 1);
100  auto* iptr = (int*)(cptr + 1);
101  *iptr = 42;
102  free(cptr);
103  }
104 #endif
105  break;
106  default:
107  B2FATAL("Illegal method");
108  }
109  B2FATAL("This should never be called ...");
110 }
Belle2::TheKillerModule::m_method
EMethod m_method
How to kill the event after parsing the string parameter.
Definition: TheKillerModule.h:76
Belle2::TheKillerModule::m_methodStr
std::string m_methodStr
How to kill the event, one of (abort, terminate, quick_exit, exit, exception, signal,...
Definition: TheKillerModule.h:74
Belle2::Module::setDescription
void setDescription(const std::string &description)
Sets the description of the module.
Definition: Module.cc:216
Belle2::TheKillerModule::m_event
int m_event
Current event.
Definition: TheKillerModule.h:82
Belle2::TheKillerModule::EMethod::c_exit
@ c_exit
call std::exit
REG_MODULE
#define REG_MODULE(moduleName)
Register the given module (without 'Module' suffix) with the framework.
Definition: Module.h:652
Belle2::TheKillerModule::EMethod::c_exception
@ c_exception
raise std::runtime_error
Belle2::Module
Base class for Modules.
Definition: Module.h:74
Belle2::TheKillerModule::EMethod::c_terminate
@ c_terminate
call std::terminate
Belle2
Abstract base class for different kinds of events.
Definition: MillepedeAlgorithm.h:19
Belle2::TheKillerModule::TheKillerModule
TheKillerModule()
Constructor: Sets the description, the properties and the parameters of the module.
Definition: TheKillerModule.cc:26
LogVar
Class to store variables with their name which were sent to the logging service.
Definition: LogVariableStream.h:24
Belle2::TheKillerModule::event
void event() override
kill if necessary
Belle2::Module::addParam
void addParam(const std::string &name, T &paramVariable, const std::string &description, const T &defaultValue)
Adds a new parameter to the module.
Definition: Module.h:562
Belle2::TheKillerModule::m_parameter
unsigned int m_parameter
Optional parameter for the kill method: for quick_exit and exit it is the return code,...
Definition: TheKillerModule.h:78
Belle2::TheKillerModule::EMethod::c_quick_exit
@ c_quick_exit
call std::quick_exit
Belle2::TheKillerModule::m_eventToKill
int m_eventToKill
In which event to kill the processing.
Definition: TheKillerModule.h:80
Belle2::TheKillerModule::EMethod::c_signal
@ c_signal
raise signal(N)
Belle2::TheKillerModule::EMethod::c_buserror
@ c_buserror
produce bus error
Belle2::TheKillerModule::initialize
void initialize() override
parse the method parameter
Belle2::TheKillerModule::EMethod::c_segfault
@ c_segfault
produce segfault
Belle2::TheKillerModule::EMethod::c_abort
@ c_abort
call std::abort