exec_mon_example.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. // (C) Copyright Gennadiy Rozental 2003-2014.
  2. // Distributed under the Boost Software License, Version 1.0.
  3. // (See accompanying file LICENSE_1_0.txt or copy at
  4. // http://www.boost.org/LICENSE_1_0.txt)
  5. // See http://www.boost.org/libs/test for the library home page.
  6. #include <boost/test/prg_exec_monitor.hpp>
  7. #include <boost/test/execution_monitor.hpp>
  8. #include <boost/test/utils/basic_cstring/io.hpp>
  9. #include <iostream>
  10. struct my_exception1
  11. {
  12. explicit my_exception1( int res_code ) : m_res_code( res_code ) {}
  13. int m_res_code;
  14. };
  15. struct my_exception2
  16. {
  17. explicit my_exception2( int res_code ) : m_res_code( res_code ) {}
  18. int m_res_code;
  19. };
  20. namespace {
  21. class dangerous_call {
  22. public:
  23. dangerous_call( int argc ) : m_argc( argc ) {}
  24. int operator()()
  25. {
  26. // here we perform some operation under monitoring that could throw my_exception
  27. if( m_argc < 2 )
  28. throw my_exception1( 23 );
  29. if( m_argc > 3 )
  30. throw my_exception2( 45 );
  31. else if( m_argc > 2 )
  32. throw "too many args";
  33. return 1;
  34. }
  35. private:
  36. // Data members
  37. int m_argc;
  38. };
  39. void translate_my_exception1( my_exception1 const& ex )
  40. {
  41. std::cout << "Caught my_exception1(" << ex.m_res_code << ")"<< std::endl;
  42. }
  43. void translate_my_exception2( my_exception2 const& ex )
  44. {
  45. std::cout << "Caught my_exception2(" << ex.m_res_code << ")"<< std::endl;
  46. }
  47. int generate_fpe()
  48. {
  49. double d = 0.0;
  50. d = 1/d;
  51. return 0;
  52. }
  53. int generate_fpe2()
  54. {
  55. double d = 1e158;
  56. d = d*d;
  57. return 0;
  58. }
  59. int generate_fpe3()
  60. {
  61. double d = 1.1e-308;
  62. d = 1/d;
  63. return 0;
  64. }
  65. int generate_int_div_0()
  66. {
  67. int i = 0;
  68. return 1/i;
  69. }
  70. #if (defined(__clang__) && __clang_major__ >= 6) || (defined(__GNUC__) && __GNUC__ >= 8)
  71. __attribute__((no_sanitize("null")))
  72. #endif
  73. int generate_sigfault()
  74. {
  75. int* p = 0;
  76. return *p;
  77. }
  78. } // local_namespace
  79. int
  80. cpp_main( int argc , char *[] )
  81. {
  82. ::boost::execution_monitor ex_mon;
  83. ///////////////////////////////////////////////////////////////
  84. ex_mon.register_exception_translator<my_exception1>( &translate_my_exception1, "except1" );
  85. ex_mon.register_exception_translator<my_exception2>( &translate_my_exception2, "except2" );
  86. try {
  87. ex_mon.execute( dangerous_call( argc ) );
  88. std::cout << "Should reach this line " << __LINE__ << std::endl;
  89. }
  90. catch ( boost::execution_exception const& ex ) {
  91. std::cout << "Caught exception: " << ex.what() << std::endl;
  92. }
  93. ///////////////////////////////////////////////////////////////
  94. ex_mon.erase_exception_translator( "except2" );
  95. try {
  96. ex_mon.execute( dangerous_call( 5 ) );
  97. std::cout << "Should not reach this line " << __LINE__ << std::endl;
  98. }
  99. catch ( boost::execution_exception const& ex ) {
  100. std::cout << "Caught exception: " << ex.what() << std::endl;
  101. }
  102. ///////////////////////////////////////////////////////////////
  103. ex_mon.erase_exception_translator<my_exception1>();
  104. try {
  105. ex_mon.execute( dangerous_call( 1 ) );
  106. std::cout << "Should not reach this line " << __LINE__ << std::endl;
  107. }
  108. catch ( boost::execution_exception const& ex ) {
  109. std::cout << "Caught exception: " << ex.what() << std::endl;
  110. }
  111. ///////////////////////////////////////////////////////////////
  112. // we are currently not able to silence those errors below with UBSAN under clang
  113. // this seems to come from the way clang handles floating point exceptions + UB.
  114. #if !(defined(HAS_UBSAN) && (HAS_UBSAN==1) && defined(__clang__))
  115. ex_mon.p_detect_fp_exceptions.value = boost::fpe::BOOST_FPE_DIVBYZERO;
  116. ex_mon.p_catch_system_errors.value = false;
  117. try {
  118. ex_mon.execute( &generate_fpe );
  119. std::cout << "Should not reach this line " << __LINE__ << std::endl;
  120. }
  121. catch ( boost::execution_exception const& ex ) {
  122. std::cout << "Caught exception: " << ex.what() << std::endl;
  123. }
  124. ///////////////////////////////////////////////////////////////
  125. ex_mon.p_detect_fp_exceptions.value = boost::fpe::BOOST_FPE_ALL;
  126. try {
  127. ex_mon.execute( &generate_fpe2 );
  128. std::cout << "Should not reach this line " << __LINE__ << std::endl;
  129. }
  130. catch ( boost::execution_exception const& ex ) {
  131. std::cout << "Caught exception: " << ex.what() << std::endl;
  132. }
  133. try {
  134. ex_mon.execute( &generate_fpe3 );
  135. std::cout << "Should not reach this line " << __LINE__ << std::endl;
  136. }
  137. catch ( boost::execution_exception const& ex ) {
  138. std::cout << "Caught exception: " << ex.what() << std::endl;
  139. }
  140. ///////////////////////////////////////////////////////////////
  141. ex_mon.p_detect_fp_exceptions.value = boost::fpe::BOOST_FPE_OFF;
  142. ex_mon.p_catch_system_errors.value = true;
  143. try {
  144. ex_mon.execute( &generate_int_div_0 );
  145. std::cout << "Should not reach this line " << __LINE__ << std::endl;
  146. }
  147. catch ( boost::execution_exception const& ex ) {
  148. std::cout << "Caught exception: " << ex.what() << std::endl;
  149. }
  150. ///////////////////////////////////////////////////////////////
  151. ex_mon.p_detect_fp_exceptions.value = boost::fpe::BOOST_FPE_OFF;
  152. ex_mon.p_catch_system_errors.value = true;
  153. try {
  154. ex_mon.execute( &generate_sigfault );
  155. std::cout << "Should not reach this line " << __LINE__ << std::endl;
  156. }
  157. catch ( boost::execution_exception const& ex ) {
  158. std::cout << "Caught exception: " << ex.what() << std::endl;
  159. }
  160. #endif // UBSAN issue
  161. return 0;
  162. }
  163. // EOF