execution_monitor.ipp 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449
  1. // (C) Copyright Gennadiy Rozental 2001.
  2. // (C) Copyright Beman Dawes and Ullrich Koethe 1995-2001.
  3. // Use, modification, and distribution are subject to the
  4. // Boost Software License, Version 1.0. (See accompanying file
  5. // http://www.boost.org/LICENSE_1_0.txt)
  6. // See http://www.boost.org/libs/test for the library home page.
  7. //
  8. /// @file
  9. /// Provides execution monitor implementation for all supported
  10. /// configurations, including Microsoft structured exception based, unix signals
  11. /// based and special workarounds for borland
  12. ///
  13. /// Note that when testing requirements or user wishes preclude use of this
  14. /// file as a separate compilation unit, it may be included as a header file.
  15. ///
  16. /// Header dependencies are deliberately restricted to reduce coupling to other
  17. /// boost libraries.
  18. // ***************************************************************************
  19. #ifndef BOOST_TEST_EXECUTION_MONITOR_IPP_012205GER
  20. #define BOOST_TEST_EXECUTION_MONITOR_IPP_012205GER
  21. // Boost.Test
  22. #include <boost/test/detail/config.hpp>
  23. #include <boost/test/detail/workaround.hpp>
  24. #include <boost/test/detail/throw_exception.hpp>
  25. #include <boost/test/execution_monitor.hpp>
  26. #include <boost/test/debug.hpp>
  27. // Boost
  28. #include <boost/cstdlib.hpp> // for exit codes
  29. #include <boost/config.hpp> // for workarounds
  30. #include <boost/core/ignore_unused.hpp> // for ignore_unused
  31. #ifndef BOOST_NO_EXCEPTIONS
  32. #include <boost/exception/get_error_info.hpp> // for get_error_info
  33. #include <boost/exception/current_exception_cast.hpp> // for current_exception_cast
  34. #endif
  35. // STL
  36. #include <string> // for std::string
  37. #include <new> // for std::bad_alloc
  38. #include <typeinfo> // for std::bad_cast, std::bad_typeid
  39. #include <exception> // for std::exception, std::bad_exception
  40. #include <stdexcept> // for std exception hierarchy
  41. #include <cstring> // for C string API
  42. #include <cassert> // for assert
  43. #include <cstddef> // for NULL
  44. #include <cstdio> // for vsnprintf
  45. #include <cstdarg> // for varargs
  46. #include <iostream> // for varargs
  47. #ifdef BOOST_NO_STDC_NAMESPACE
  48. namespace std { using ::strerror; using ::strlen; using ::strncat; }
  49. #endif
  50. // to use vsnprintf
  51. #if defined(__SUNPRO_CC) || defined(__SunOS)
  52. # include <stdio.h>
  53. # include <stdarg.h>
  54. using std::va_list;
  55. #endif
  56. // to use vsnprintf
  57. #if defined(__QNXNTO__) || defined(__VXWORKS__)
  58. # include <stdio.h>
  59. using std::va_list;
  60. #endif
  61. #if defined(__VXWORKS__)
  62. # define BOOST_TEST_LIMITED_SIGNAL_DETAILS
  63. #endif
  64. #ifdef BOOST_SEH_BASED_SIGNAL_HANDLING
  65. # include <windows.h>
  66. # if defined(__MWERKS__) || (defined(_MSC_VER) && !defined(UNDER_CE))
  67. # include <eh.h>
  68. # endif
  69. # if defined(__BORLANDC__) && __BORLANDC__ >= 0x560 || defined(__MWERKS__)
  70. # include <stdint.h>
  71. # endif
  72. # if defined(__BORLANDC__) && __BORLANDC__ < 0x560
  73. typedef unsigned uintptr_t;
  74. # endif
  75. # if defined(UNDER_CE) && BOOST_WORKAROUND(_MSC_VER, < 1500 )
  76. typedef void* uintptr_t;
  77. # elif defined(UNDER_CE)
  78. # include <crtdefs.h>
  79. # endif
  80. # if !defined(NDEBUG) && defined(_MSC_VER) && !defined(UNDER_CE)
  81. # include <crtdbg.h>
  82. # define BOOST_TEST_CRT_HOOK_TYPE _CRT_REPORT_HOOK
  83. # define BOOST_TEST_CRT_ASSERT _CRT_ASSERT
  84. # define BOOST_TEST_CRT_ERROR _CRT_ERROR
  85. # define BOOST_TEST_CRT_SET_HOOK(H) _CrtSetReportHook(H)
  86. # else
  87. # define BOOST_TEST_CRT_HOOK_TYPE void*
  88. # define BOOST_TEST_CRT_ASSERT 2
  89. # define BOOST_TEST_CRT_ERROR 1
  90. # define BOOST_TEST_CRT_SET_HOOK(H) (void*)(H)
  91. # endif
  92. # if (!BOOST_WORKAROUND(_MSC_VER, >= 1400 ) && \
  93. !defined(BOOST_COMO)) || defined(UNDER_CE)
  94. typedef void* _invalid_parameter_handler;
  95. inline _invalid_parameter_handler
  96. _set_invalid_parameter_handler( _invalid_parameter_handler arg )
  97. {
  98. return arg;
  99. }
  100. # endif
  101. # if BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x0564)) || defined(UNDER_CE)
  102. namespace { void _set_se_translator( void* ) {} }
  103. # endif
  104. #elif defined(BOOST_HAS_SIGACTION)
  105. # define BOOST_SIGACTION_BASED_SIGNAL_HANDLING
  106. # include <unistd.h>
  107. # include <signal.h>
  108. # include <setjmp.h>
  109. # if defined(__FreeBSD__)
  110. # include <osreldate.h>
  111. # ifndef SIGPOLL
  112. # define SIGPOLL SIGIO
  113. # endif
  114. # if (__FreeBSD_version < 70100)
  115. # define ILL_ILLADR 0 // ILL_RESAD_FAULT
  116. # define ILL_PRVOPC ILL_PRIVIN_FAULT
  117. # define ILL_ILLOPN 2 // ILL_RESOP_FAULT
  118. # define ILL_COPROC ILL_FPOP_FAULT
  119. # define BOOST_TEST_LIMITED_SIGNAL_DETAILS
  120. # endif
  121. # endif
  122. # if defined(__ANDROID__)
  123. # include <android/api-level.h>
  124. # endif
  125. // documentation of BOOST_TEST_DISABLE_ALT_STACK in execution_monitor.hpp
  126. # if !defined(__CYGWIN__) && !defined(__QNXNTO__) && !defined(__bgq__) && \
  127. (!defined(__ANDROID__) || __ANDROID_API__ >= 8) && \
  128. !defined(BOOST_TEST_DISABLE_ALT_STACK)
  129. # define BOOST_TEST_USE_ALT_STACK
  130. # endif
  131. # if defined(SIGPOLL) && !defined(__CYGWIN__) && \
  132. !(defined(macintosh) || defined(__APPLE__) || defined(__APPLE_CC__)) && \
  133. !defined(__NetBSD__) && \
  134. !defined(__QNXNTO__)
  135. # define BOOST_TEST_CATCH_SIGPOLL
  136. # endif
  137. # ifdef BOOST_TEST_USE_ALT_STACK
  138. # define BOOST_TEST_ALT_STACK_SIZE SIGSTKSZ
  139. # endif
  140. #else
  141. # define BOOST_NO_SIGNAL_HANDLING
  142. #endif
  143. #ifndef UNDER_CE
  144. #include <errno.h>
  145. #endif
  146. #if !defined(BOOST_NO_TYPEID) && !defined(BOOST_NO_RTTI)
  147. # include <boost/core/demangle.hpp>
  148. #endif
  149. #include <boost/test/detail/suppress_warnings.hpp>
  150. //____________________________________________________________________________//
  151. namespace boost {
  152. // ************************************************************************** //
  153. // ************** throw_exception ************** //
  154. // ************************************************************************** //
  155. #ifdef BOOST_NO_EXCEPTIONS
  156. void throw_exception( std::exception const & e ) { abort(); }
  157. #endif
  158. // ************************************************************************** //
  159. // ************** report_error ************** //
  160. // ************************************************************************** //
  161. namespace detail {
  162. #ifdef __BORLANDC__
  163. # define BOOST_TEST_VSNPRINTF( a1, a2, a3, a4 ) std::vsnprintf( (a1), (a2), (a3), (a4) )
  164. #elif BOOST_WORKAROUND(_MSC_VER, <= 1310) || \
  165. BOOST_WORKAROUND(__MWERKS__, BOOST_TESTED_AT(0x3000)) || \
  166. defined(UNDER_CE)
  167. # define BOOST_TEST_VSNPRINTF( a1, a2, a3, a4 ) _vsnprintf( (a1), (a2), (a3), (a4) )
  168. #else
  169. # define BOOST_TEST_VSNPRINTF( a1, a2, a3, a4 ) vsnprintf( (a1), (a2), (a3), (a4) )
  170. #endif
  171. #ifndef BOOST_NO_EXCEPTIONS
  172. template <typename ErrorInfo>
  173. typename ErrorInfo::value_type
  174. extract( boost::exception const* ex )
  175. {
  176. if( !ex )
  177. return 0;
  178. typename ErrorInfo::value_type const * val = boost::get_error_info<ErrorInfo>( *ex );
  179. return val ? *val : 0;
  180. }
  181. //____________________________________________________________________________//
  182. static void
  183. report_error( execution_exception::error_code ec, boost::exception const* be, char const* format, va_list* args )
  184. {
  185. static const int REPORT_ERROR_BUFFER_SIZE = 4096;
  186. static char buf[REPORT_ERROR_BUFFER_SIZE];
  187. BOOST_TEST_VSNPRINTF( buf, sizeof(buf)-1, format, *args );
  188. buf[sizeof(buf)-1] = 0;
  189. va_end( *args );
  190. BOOST_TEST_I_THROW(execution_exception( ec, buf, execution_exception::location( extract<throw_file>( be ),
  191. (size_t)extract<throw_line>( be ),
  192. extract<throw_function>( be ) ) ));
  193. }
  194. //____________________________________________________________________________//
  195. static void
  196. report_error( execution_exception::error_code ec, boost::exception const* be, char const* format, ... )
  197. {
  198. va_list args;
  199. va_start( args, format );
  200. report_error( ec, be, format, &args );
  201. }
  202. #endif
  203. //____________________________________________________________________________//
  204. static void
  205. report_error( execution_exception::error_code ec, char const* format, ... )
  206. {
  207. va_list args;
  208. va_start( args, format );
  209. report_error( ec, 0, format, &args );
  210. }
  211. //____________________________________________________________________________//
  212. template<typename Tr,typename Functor>
  213. inline int
  214. do_invoke( Tr const& tr, Functor const& F )
  215. {
  216. return tr ? (*tr)( F ) : F();
  217. }
  218. //____________________________________________________________________________//
  219. struct fpe_except_guard {
  220. explicit fpe_except_guard( unsigned detect_fpe )
  221. : m_detect_fpe( detect_fpe )
  222. {
  223. // prepare fp exceptions control
  224. m_previously_enabled = fpe::disable( fpe::BOOST_FPE_ALL );
  225. if( m_previously_enabled != fpe::BOOST_FPE_INV && detect_fpe != fpe::BOOST_FPE_OFF )
  226. fpe::enable( detect_fpe );
  227. }
  228. ~fpe_except_guard()
  229. {
  230. if( m_detect_fpe != fpe::BOOST_FPE_OFF )
  231. fpe::disable( m_detect_fpe );
  232. if( m_previously_enabled != fpe::BOOST_FPE_INV )
  233. fpe::enable( m_previously_enabled );
  234. }
  235. unsigned m_detect_fpe;
  236. unsigned m_previously_enabled;
  237. };
  238. // ************************************************************************** //
  239. // ************** typeid_name ************** //
  240. // ************************************************************************** //
  241. #if !defined(BOOST_NO_TYPEID) && !defined(BOOST_NO_RTTI)
  242. template<typename T>
  243. std::string
  244. typeid_name( T const& t )
  245. {
  246. return boost::core::demangle(typeid(t).name());
  247. }
  248. #endif
  249. } // namespace detail
  250. #if defined(BOOST_SIGACTION_BASED_SIGNAL_HANDLING)
  251. // ************************************************************************** //
  252. // ************** Sigaction based signal handling ************** //
  253. // ************************************************************************** //
  254. namespace detail {
  255. // ************************************************************************** //
  256. // ************** boost::detail::system_signal_exception ************** //
  257. // ************************************************************************** //
  258. class system_signal_exception {
  259. public:
  260. // Constructor
  261. system_signal_exception()
  262. : m_sig_info( 0 )
  263. , m_context( 0 )
  264. {}
  265. // Access methods
  266. void operator()( siginfo_t* i, void* c )
  267. {
  268. m_sig_info = i;
  269. m_context = c;
  270. }
  271. void report() const;
  272. private:
  273. // Data members
  274. siginfo_t* m_sig_info; // system signal detailed info
  275. void* m_context; // signal context
  276. };
  277. //____________________________________________________________________________//
  278. void
  279. system_signal_exception::report() const
  280. {
  281. if( !m_sig_info )
  282. return; // no error actually occur?
  283. switch( m_sig_info->si_code ) {
  284. #ifdef __VXWORKS__
  285. // a bit of a hack to adapt code to small m_sig_info VxWorks uses
  286. #define si_addr si_value.sival_int
  287. #define si_band si_value.sival_int
  288. #else
  289. case SI_USER:
  290. report_error( execution_exception::system_error,
  291. "signal: generated by kill() (or family); uid=%d; pid=%d",
  292. (int)m_sig_info->si_uid, (int)m_sig_info->si_pid );
  293. break;
  294. #endif
  295. case SI_QUEUE:
  296. report_error( execution_exception::system_error,
  297. "signal: sent by sigqueue()" );
  298. break;
  299. case SI_TIMER:
  300. report_error( execution_exception::system_error,
  301. "signal: the expiration of a timer set by timer_settimer()" );
  302. break;
  303. case SI_ASYNCIO:
  304. report_error( execution_exception::system_error,
  305. "signal: generated by the completion of an asynchronous I/O request" );
  306. break;
  307. case SI_MESGQ:
  308. report_error( execution_exception::system_error,
  309. "signal: generated by the the arrival of a message on an empty message queue" );
  310. break;
  311. default:
  312. break;
  313. }
  314. switch( m_sig_info->si_signo ) {
  315. case SIGILL:
  316. switch( m_sig_info->si_code ) {
  317. #ifndef BOOST_TEST_LIMITED_SIGNAL_DETAILS
  318. case ILL_ILLOPC:
  319. report_error( execution_exception::system_fatal_error,
  320. "signal: illegal opcode; address of failing instruction: 0x%08lx",
  321. m_sig_info->si_addr );
  322. break;
  323. case ILL_ILLTRP:
  324. report_error( execution_exception::system_fatal_error,
  325. "signal: illegal trap; address of failing instruction: 0x%08lx",
  326. m_sig_info->si_addr );
  327. break;
  328. case ILL_PRVREG:
  329. report_error( execution_exception::system_fatal_error,
  330. "signal: privileged register; address of failing instruction: 0x%08lx",
  331. m_sig_info->si_addr );
  332. break;
  333. case ILL_BADSTK:
  334. report_error( execution_exception::system_fatal_error,
  335. "signal: internal stack error; address of failing instruction: 0x%08lx",
  336. m_sig_info->si_addr );
  337. break;
  338. #endif
  339. case ILL_ILLOPN:
  340. report_error( execution_exception::system_fatal_error,
  341. "signal: illegal operand; address of failing instruction: 0x%08lx",
  342. m_sig_info->si_addr );
  343. break;
  344. case ILL_ILLADR:
  345. report_error( execution_exception::system_fatal_error,
  346. "signal: illegal addressing mode; address of failing instruction: 0x%08lx",
  347. m_sig_info->si_addr );
  348. break;
  349. case ILL_PRVOPC:
  350. report_error( execution_exception::system_fatal_error,
  351. "signal: privileged opcode; address of failing instruction: 0x%08lx",
  352. m_sig_info->si_addr );
  353. break;
  354. case ILL_COPROC:
  355. report_error( execution_exception::system_fatal_error,
  356. "signal: co-processor error; address of failing instruction: 0x%08lx",
  357. m_sig_info->si_addr );
  358. break;
  359. default:
  360. report_error( execution_exception::system_fatal_error,
  361. "signal: SIGILL, si_code: %d (illegal instruction; address of failing instruction: 0x%08lx)",
  362. m_sig_info->si_addr, m_sig_info->si_code );
  363. break;
  364. }
  365. break;
  366. case SIGFPE:
  367. switch( m_sig_info->si_code ) {
  368. case FPE_INTDIV:
  369. report_error( execution_exception::system_error,
  370. "signal: integer divide by zero; address of failing instruction: 0x%08lx",
  371. m_sig_info->si_addr );
  372. break;
  373. case FPE_INTOVF:
  374. report_error( execution_exception::system_error,
  375. "signal: integer overflow; address of failing instruction: 0x%08lx",
  376. m_sig_info->si_addr );
  377. break;
  378. case FPE_FLTDIV:
  379. report_error( execution_exception::system_error,
  380. "signal: floating point divide by zero; address of failing instruction: 0x%08lx",
  381. m_sig_info->si_addr );
  382. break;
  383. case FPE_FLTOVF:
  384. report_error( execution_exception::system_error,
  385. "signal: floating point overflow; address of failing instruction: 0x%08lx",
  386. m_sig_info->si_addr );
  387. break;
  388. case FPE_FLTUND:
  389. report_error( execution_exception::system_error,
  390. "signal: floating point underflow; address of failing instruction: 0x%08lx",
  391. m_sig_info->si_addr );
  392. break;
  393. case FPE_FLTRES:
  394. report_error( execution_exception::system_error,
  395. "signal: floating point inexact result; address of failing instruction: 0x%08lx",
  396. m_sig_info->si_addr );
  397. break;
  398. case FPE_FLTINV:
  399. report_error( execution_exception::system_error,
  400. "signal: invalid floating point operation; address of failing instruction: 0x%08lx",
  401. m_sig_info->si_addr );
  402. break;
  403. case FPE_FLTSUB:
  404. report_error( execution_exception::system_error,
  405. "signal: subscript out of range; address of failing instruction: 0x%08lx",
  406. m_sig_info->si_addr );
  407. break;
  408. default:
  409. report_error( execution_exception::system_error,
  410. "signal: SIGFPE, si_code: %d (errnoneous arithmetic operations; address of failing instruction: 0x%08lx)",
  411. m_sig_info->si_addr, m_sig_info->si_code );
  412. break;
  413. }
  414. break;
  415. case SIGSEGV:
  416. switch( m_sig_info->si_code ) {
  417. #ifndef BOOST_TEST_LIMITED_SIGNAL_DETAILS
  418. case SEGV_MAPERR:
  419. report_error( execution_exception::system_fatal_error,
  420. "memory access violation at address: 0x%08lx: no mapping at fault address",
  421. m_sig_info->si_addr );
  422. break;
  423. case SEGV_ACCERR:
  424. report_error( execution_exception::system_fatal_error,
  425. "memory access violation at address: 0x%08lx: invalid permissions",
  426. m_sig_info->si_addr );
  427. break;
  428. #endif
  429. default:
  430. report_error( execution_exception::system_fatal_error,
  431. "signal: SIGSEGV, si_code: %d (memory access violation at address: 0x%08lx)",
  432. m_sig_info->si_addr, m_sig_info->si_code );
  433. break;
  434. }
  435. break;
  436. case SIGBUS:
  437. switch( m_sig_info->si_code ) {
  438. #ifndef BOOST_TEST_LIMITED_SIGNAL_DETAILS
  439. case BUS_ADRALN:
  440. report_error( execution_exception::system_fatal_error,
  441. "memory access violation at address: 0x%08lx: invalid address alignment",
  442. m_sig_info->si_addr );
  443. break;
  444. case BUS_ADRERR:
  445. report_error( execution_exception::system_fatal_error,
  446. "memory access violation at address: 0x%08lx: non-existent physical address",
  447. m_sig_info->si_addr );
  448. break;
  449. case BUS_OBJERR:
  450. report_error( execution_exception::system_fatal_error,
  451. "memory access violation at address: 0x%08lx: object specific hardware error",
  452. m_sig_info->si_addr );
  453. break;
  454. #endif
  455. default:
  456. report_error( execution_exception::system_fatal_error,
  457. "signal: SIGSEGV, si_code: %d (memory access violation at address: 0x%08lx)",
  458. m_sig_info->si_addr, m_sig_info->si_code );
  459. break;
  460. }
  461. break;
  462. #if defined(BOOST_TEST_CATCH_SIGPOLL)
  463. case SIGPOLL:
  464. switch( m_sig_info->si_code ) {
  465. #ifndef BOOST_TEST_LIMITED_SIGNAL_DETAILS
  466. case POLL_IN:
  467. report_error( execution_exception::system_error,
  468. "data input available; band event %d",
  469. (int)m_sig_info->si_band );
  470. break;
  471. case POLL_OUT:
  472. report_error( execution_exception::system_error,
  473. "output buffers available; band event %d",
  474. (int)m_sig_info->si_band );
  475. break;
  476. case POLL_MSG:
  477. report_error( execution_exception::system_error,
  478. "input message available; band event %d",
  479. (int)m_sig_info->si_band );
  480. break;
  481. case POLL_ERR:
  482. report_error( execution_exception::system_error,
  483. "i/o error; band event %d",
  484. (int)m_sig_info->si_band );
  485. break;
  486. case POLL_PRI:
  487. report_error( execution_exception::system_error,
  488. "high priority input available; band event %d",
  489. (int)m_sig_info->si_band );
  490. break;
  491. #if defined(POLL_ERR) && defined(POLL_HUP) && (POLL_ERR - POLL_HUP)
  492. case POLL_HUP:
  493. report_error( execution_exception::system_error,
  494. "device disconnected; band event %d",
  495. (int)m_sig_info->si_band );
  496. break;
  497. #endif
  498. #endif
  499. default:
  500. report_error( execution_exception::system_error,
  501. "signal: SIGPOLL, si_code: %d (asynchronous I/O event occurred; band event %d)",
  502. (int)m_sig_info->si_band, m_sig_info->si_code );
  503. break;
  504. }
  505. break;
  506. #endif
  507. case SIGABRT:
  508. report_error( execution_exception::system_error,
  509. "signal: SIGABRT (application abort requested)" );
  510. break;
  511. case SIGALRM:
  512. report_error( execution_exception::timeout_error,
  513. "signal: SIGALRM (timeout while executing function)" );
  514. break;
  515. default:
  516. report_error( execution_exception::system_error,
  517. "unrecognized signal %d", m_sig_info->si_signo );
  518. }
  519. }
  520. //____________________________________________________________________________//
  521. // ************************************************************************** //
  522. // ************** boost::detail::signal_action ************** //
  523. // ************************************************************************** //
  524. // Forward declaration
  525. extern "C" {
  526. static void boost_execution_monitor_jumping_signal_handler( int sig, siginfo_t* info, void* context );
  527. static void boost_execution_monitor_attaching_signal_handler( int sig, siginfo_t* info, void* context );
  528. }
  529. class signal_action {
  530. typedef struct sigaction* sigaction_ptr;
  531. public:
  532. //Constructor
  533. signal_action();
  534. signal_action( int sig, bool install, bool attach_dbg, char* alt_stack );
  535. ~signal_action();
  536. private:
  537. // Data members
  538. int m_sig;
  539. bool m_installed;
  540. struct sigaction m_new_action;
  541. struct sigaction m_old_action;
  542. };
  543. //____________________________________________________________________________//
  544. signal_action::signal_action()
  545. : m_installed( false )
  546. {}
  547. //____________________________________________________________________________//
  548. signal_action::signal_action( int sig, bool install, bool attach_dbg, char* alt_stack )
  549. : m_sig( sig )
  550. , m_installed( install )
  551. {
  552. if( !install )
  553. return;
  554. std::memset( &m_new_action, 0, sizeof(struct sigaction) );
  555. BOOST_TEST_SYS_ASSERT( ::sigaction( m_sig , sigaction_ptr(), &m_new_action ) != -1 );
  556. if( m_new_action.sa_sigaction || m_new_action.sa_handler ) {
  557. m_installed = false;
  558. return;
  559. }
  560. m_new_action.sa_flags |= SA_SIGINFO;
  561. m_new_action.sa_sigaction = attach_dbg ? &boost_execution_monitor_attaching_signal_handler
  562. : &boost_execution_monitor_jumping_signal_handler;
  563. BOOST_TEST_SYS_ASSERT( sigemptyset( &m_new_action.sa_mask ) != -1 );
  564. #ifdef BOOST_TEST_USE_ALT_STACK
  565. if( alt_stack )
  566. m_new_action.sa_flags |= SA_ONSTACK;
  567. #endif
  568. BOOST_TEST_SYS_ASSERT( ::sigaction( m_sig, &m_new_action, &m_old_action ) != -1 );
  569. }
  570. //____________________________________________________________________________//
  571. signal_action::~signal_action()
  572. {
  573. if( m_installed )
  574. ::sigaction( m_sig, &m_old_action , sigaction_ptr() );
  575. }
  576. //____________________________________________________________________________//
  577. // ************************************************************************** //
  578. // ************** boost::detail::signal_handler ************** //
  579. // ************************************************************************** //
  580. class signal_handler {
  581. public:
  582. // Constructor
  583. explicit signal_handler( bool catch_system_errors, bool detect_fpe, unsigned timeout, bool attach_dbg, char* alt_stack );
  584. // Destructor
  585. ~signal_handler();
  586. // access methods
  587. static sigjmp_buf& jump_buffer()
  588. {
  589. assert( !!s_active_handler );
  590. return s_active_handler->m_sigjmp_buf;
  591. }
  592. static system_signal_exception& sys_sig()
  593. {
  594. assert( !!s_active_handler );
  595. return s_active_handler->m_sys_sig;
  596. }
  597. private:
  598. // Data members
  599. signal_handler* m_prev_handler;
  600. unsigned m_timeout;
  601. // Note: We intentionality do not catch SIGCHLD. Users have to deal with it themselves
  602. signal_action m_ILL_action;
  603. signal_action m_FPE_action;
  604. signal_action m_SEGV_action;
  605. signal_action m_BUS_action;
  606. signal_action m_CHLD_action;
  607. signal_action m_POLL_action;
  608. signal_action m_ABRT_action;
  609. signal_action m_ALRM_action;
  610. sigjmp_buf m_sigjmp_buf;
  611. system_signal_exception m_sys_sig;
  612. static signal_handler* s_active_handler;
  613. };
  614. // !! need to be placed in thread specific storage
  615. typedef signal_handler* signal_handler_ptr;
  616. signal_handler* signal_handler::s_active_handler = signal_handler_ptr();
  617. //____________________________________________________________________________//
  618. signal_handler::signal_handler( bool catch_system_errors, bool detect_fpe, unsigned timeout, bool attach_dbg, char* alt_stack )
  619. : m_prev_handler( s_active_handler )
  620. , m_timeout( timeout )
  621. , m_ILL_action ( SIGILL , catch_system_errors, attach_dbg, alt_stack )
  622. , m_FPE_action ( SIGFPE , detect_fpe , attach_dbg, alt_stack )
  623. , m_SEGV_action( SIGSEGV, catch_system_errors, attach_dbg, alt_stack )
  624. , m_BUS_action ( SIGBUS , catch_system_errors, attach_dbg, alt_stack )
  625. #ifdef BOOST_TEST_CATCH_SIGPOLL
  626. , m_POLL_action( SIGPOLL, catch_system_errors, attach_dbg, alt_stack )
  627. #endif
  628. , m_ABRT_action( SIGABRT, catch_system_errors, attach_dbg, alt_stack )
  629. , m_ALRM_action( SIGALRM, timeout > 0 , attach_dbg, alt_stack )
  630. {
  631. s_active_handler = this;
  632. if( m_timeout > 0 ) {
  633. ::alarm( 0 );
  634. ::alarm( timeout );
  635. }
  636. #ifdef BOOST_TEST_USE_ALT_STACK
  637. if( alt_stack ) {
  638. stack_t sigstk;
  639. std::memset( &sigstk, 0, sizeof(stack_t) );
  640. BOOST_TEST_SYS_ASSERT( ::sigaltstack( 0, &sigstk ) != -1 );
  641. if( sigstk.ss_flags & SS_DISABLE ) {
  642. sigstk.ss_sp = alt_stack;
  643. sigstk.ss_size = BOOST_TEST_ALT_STACK_SIZE;
  644. sigstk.ss_flags = 0;
  645. BOOST_TEST_SYS_ASSERT( ::sigaltstack( &sigstk, 0 ) != -1 );
  646. }
  647. }
  648. #endif
  649. }
  650. //____________________________________________________________________________//
  651. signal_handler::~signal_handler()
  652. {
  653. assert( s_active_handler == this );
  654. if( m_timeout > 0 )
  655. ::alarm( 0 );
  656. #ifdef BOOST_TEST_USE_ALT_STACK
  657. #ifdef __GNUC__
  658. // We shouldn't need to explicitly initialize all the members here,
  659. // but gcc warns if we don't, so add initializers for each of the
  660. // members specified in the POSIX std:
  661. stack_t sigstk = { 0, 0, 0 };
  662. #else
  663. stack_t sigstk = { };
  664. #endif
  665. sigstk.ss_size = MINSIGSTKSZ;
  666. sigstk.ss_flags = SS_DISABLE;
  667. if( ::sigaltstack( &sigstk, 0 ) == -1 ) {
  668. int error_n = errno;
  669. std::cerr << "******** errors disabling the alternate stack:" << std::endl
  670. << "\t#error:" << error_n << std::endl
  671. << "\t" << std::strerror( error_n ) << std::endl;
  672. }
  673. #endif
  674. s_active_handler = m_prev_handler;
  675. }
  676. //____________________________________________________________________________//
  677. // ************************************************************************** //
  678. // ************** execution_monitor_signal_handler ************** //
  679. // ************************************************************************** //
  680. extern "C" {
  681. static void boost_execution_monitor_jumping_signal_handler( int sig, siginfo_t* info, void* context )
  682. {
  683. signal_handler::sys_sig()( info, context );
  684. siglongjmp( signal_handler::jump_buffer(), sig );
  685. }
  686. //____________________________________________________________________________//
  687. static void boost_execution_monitor_attaching_signal_handler( int sig, siginfo_t* info, void* context )
  688. {
  689. if( !debug::attach_debugger( false ) )
  690. boost_execution_monitor_jumping_signal_handler( sig, info, context );
  691. // debugger attached; it will handle the signal
  692. BOOST_TEST_SYS_ASSERT( ::signal( sig, SIG_DFL ) != SIG_ERR );
  693. }
  694. //____________________________________________________________________________//
  695. }
  696. } // namespace detail
  697. // ************************************************************************** //
  698. // ************** execution_monitor::catch_signals ************** //
  699. // ************************************************************************** //
  700. int
  701. execution_monitor::catch_signals( boost::function<int ()> const& F )
  702. {
  703. using namespace detail;
  704. #if defined(__CYGWIN__)
  705. p_catch_system_errors.value = false;
  706. #endif
  707. #ifdef BOOST_TEST_USE_ALT_STACK
  708. if( !!p_use_alt_stack && !m_alt_stack )
  709. m_alt_stack.reset( new char[BOOST_TEST_ALT_STACK_SIZE] );
  710. #else
  711. p_use_alt_stack.value = false;
  712. #endif
  713. signal_handler local_signal_handler( p_catch_system_errors,
  714. p_catch_system_errors || (p_detect_fp_exceptions != fpe::BOOST_FPE_OFF),
  715. p_timeout,
  716. p_auto_start_dbg,
  717. !p_use_alt_stack ? 0 : m_alt_stack.get() );
  718. if( !sigsetjmp( signal_handler::jump_buffer(), 1 ) )
  719. return detail::do_invoke( m_custom_translators , F );
  720. else
  721. BOOST_TEST_I_THROW( local_signal_handler.sys_sig() );
  722. }
  723. //____________________________________________________________________________//
  724. #elif defined(BOOST_SEH_BASED_SIGNAL_HANDLING)
  725. // ************************************************************************** //
  726. // ************** Microsoft structured exception handling ************** //
  727. // ************************************************************************** //
  728. #if BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x0564))
  729. namespace { void _set_se_translator( void* ) {} }
  730. #endif
  731. namespace detail {
  732. // ************************************************************************** //
  733. // ************** boost::detail::system_signal_exception ************** //
  734. // ************************************************************************** //
  735. class system_signal_exception {
  736. public:
  737. // Constructor
  738. explicit system_signal_exception( execution_monitor* em )
  739. : m_em( em )
  740. , m_se_id( 0 )
  741. , m_fault_address( 0 )
  742. , m_dir( false )
  743. {}
  744. void report() const;
  745. int operator()( unsigned id, _EXCEPTION_POINTERS* exps );
  746. private:
  747. // Data members
  748. execution_monitor* m_em;
  749. unsigned m_se_id;
  750. void* m_fault_address;
  751. bool m_dir;
  752. };
  753. //____________________________________________________________________________//
  754. #if BOOST_WORKAROUND( BOOST_MSVC, <= 1310)
  755. static void
  756. seh_catch_preventer( unsigned /* id */, _EXCEPTION_POINTERS* /* exps */ )
  757. {
  758. throw;
  759. }
  760. #endif
  761. //____________________________________________________________________________//
  762. int
  763. system_signal_exception::operator()( unsigned id, _EXCEPTION_POINTERS* exps )
  764. {
  765. const unsigned MSFT_CPP_EXCEPT = 0xE06d7363; // EMSC
  766. // C++ exception - allow to go through
  767. if( id == MSFT_CPP_EXCEPT )
  768. return EXCEPTION_CONTINUE_SEARCH;
  769. // FPE detection is enabled, while system exception detection is not - check if this is actually FPE
  770. if( !m_em->p_catch_system_errors ) {
  771. if( !m_em->p_detect_fp_exceptions )
  772. return EXCEPTION_CONTINUE_SEARCH;
  773. switch( id ) {
  774. case EXCEPTION_FLT_DIVIDE_BY_ZERO:
  775. case EXCEPTION_FLT_STACK_CHECK:
  776. case EXCEPTION_FLT_DENORMAL_OPERAND:
  777. case EXCEPTION_FLT_INEXACT_RESULT:
  778. case EXCEPTION_FLT_OVERFLOW:
  779. case EXCEPTION_FLT_UNDERFLOW:
  780. case EXCEPTION_FLT_INVALID_OPERATION:
  781. case STATUS_FLOAT_MULTIPLE_FAULTS:
  782. case STATUS_FLOAT_MULTIPLE_TRAPS:
  783. break;
  784. default:
  785. return EXCEPTION_CONTINUE_SEARCH;
  786. }
  787. }
  788. if( !!m_em->p_auto_start_dbg && debug::attach_debugger( false ) ) {
  789. m_em->p_catch_system_errors.value = false;
  790. #if BOOST_WORKAROUND( BOOST_MSVC, <= 1310)
  791. _set_se_translator( &seh_catch_preventer );
  792. #endif
  793. return EXCEPTION_CONTINUE_EXECUTION;
  794. }
  795. m_se_id = id;
  796. if( m_se_id == EXCEPTION_ACCESS_VIOLATION && exps->ExceptionRecord->NumberParameters == 2 ) {
  797. m_fault_address = (void*)exps->ExceptionRecord->ExceptionInformation[1];
  798. m_dir = exps->ExceptionRecord->ExceptionInformation[0] == 0;
  799. }
  800. return EXCEPTION_EXECUTE_HANDLER;
  801. }
  802. //____________________________________________________________________________//
  803. void
  804. system_signal_exception::report() const
  805. {
  806. switch( m_se_id ) {
  807. // cases classified as system_fatal_error
  808. case EXCEPTION_ACCESS_VIOLATION: {
  809. if( !m_fault_address )
  810. detail::report_error( execution_exception::system_fatal_error, "memory access violation" );
  811. else
  812. detail::report_error(
  813. execution_exception::system_fatal_error,
  814. "memory access violation occurred at address 0x%08lx, while attempting to %s",
  815. m_fault_address,
  816. m_dir ? " read inaccessible data"
  817. : " write to an inaccessible (or protected) address"
  818. );
  819. break;
  820. }
  821. case EXCEPTION_ILLEGAL_INSTRUCTION:
  822. detail::report_error( execution_exception::system_fatal_error, "illegal instruction" );
  823. break;
  824. case EXCEPTION_PRIV_INSTRUCTION:
  825. detail::report_error( execution_exception::system_fatal_error, "tried to execute an instruction whose operation is not allowed in the current machine mode" );
  826. break;
  827. case EXCEPTION_IN_PAGE_ERROR:
  828. detail::report_error( execution_exception::system_fatal_error, "access to a memory page that is not present" );
  829. break;
  830. case EXCEPTION_STACK_OVERFLOW:
  831. detail::report_error( execution_exception::system_fatal_error, "stack overflow" );
  832. break;
  833. case EXCEPTION_NONCONTINUABLE_EXCEPTION:
  834. detail::report_error( execution_exception::system_fatal_error, "tried to continue execution after a non continuable exception occurred" );
  835. break;
  836. // cases classified as (non-fatal) system_trap
  837. case EXCEPTION_DATATYPE_MISALIGNMENT:
  838. detail::report_error( execution_exception::system_error, "data misalignment" );
  839. break;
  840. case EXCEPTION_INT_DIVIDE_BY_ZERO:
  841. detail::report_error( execution_exception::system_error, "integer divide by zero" );
  842. break;
  843. case EXCEPTION_INT_OVERFLOW:
  844. detail::report_error( execution_exception::system_error, "integer overflow" );
  845. break;
  846. case EXCEPTION_ARRAY_BOUNDS_EXCEEDED:
  847. detail::report_error( execution_exception::system_error, "array bounds exceeded" );
  848. break;
  849. case EXCEPTION_FLT_DIVIDE_BY_ZERO:
  850. detail::report_error( execution_exception::system_error, "floating point divide by zero" );
  851. break;
  852. case EXCEPTION_FLT_STACK_CHECK:
  853. detail::report_error( execution_exception::system_error,
  854. "stack overflowed or underflowed as the result of a floating-point operation" );
  855. break;
  856. case EXCEPTION_FLT_DENORMAL_OPERAND:
  857. detail::report_error( execution_exception::system_error,
  858. "operand of floating point operation is denormal" );
  859. break;
  860. case EXCEPTION_FLT_INEXACT_RESULT:
  861. detail::report_error( execution_exception::system_error,
  862. "result of a floating-point operation cannot be represented exactly" );
  863. break;
  864. case EXCEPTION_FLT_OVERFLOW:
  865. detail::report_error( execution_exception::system_error,
  866. "exponent of a floating-point operation is greater than the magnitude allowed by the corresponding type" );
  867. break;
  868. case EXCEPTION_FLT_UNDERFLOW:
  869. detail::report_error( execution_exception::system_error,
  870. "exponent of a floating-point operation is less than the magnitude allowed by the corresponding type" );
  871. break;
  872. case EXCEPTION_FLT_INVALID_OPERATION:
  873. detail::report_error( execution_exception::system_error, "floating point error" );
  874. break;
  875. case STATUS_FLOAT_MULTIPLE_FAULTS:
  876. detail::report_error( execution_exception::system_error, "multiple floating point errors" );
  877. break;
  878. case STATUS_FLOAT_MULTIPLE_TRAPS:
  879. detail::report_error( execution_exception::system_error, "multiple floating point errors" );
  880. break;
  881. case EXCEPTION_BREAKPOINT:
  882. detail::report_error( execution_exception::system_error, "breakpoint encountered" );
  883. break;
  884. default:
  885. detail::report_error( execution_exception::system_error, "unrecognized exception. Id: 0x%08lx", m_se_id );
  886. break;
  887. }
  888. }
  889. //____________________________________________________________________________//
  890. // ************************************************************************** //
  891. // ************** assert_reporting_function ************** //
  892. // ************************************************************************** //
  893. int BOOST_TEST_CALL_DECL
  894. assert_reporting_function( int reportType, char* userMessage, int* )
  895. {
  896. // write this way instead of switch to avoid unreachable statements
  897. if( reportType == BOOST_TEST_CRT_ASSERT || reportType == BOOST_TEST_CRT_ERROR )
  898. detail::report_error( reportType == BOOST_TEST_CRT_ASSERT ? execution_exception::user_error : execution_exception::system_error, userMessage );
  899. return 0;
  900. } // assert_reporting_function
  901. //____________________________________________________________________________//
  902. void BOOST_TEST_CALL_DECL
  903. invalid_param_handler( wchar_t const* /* expr */,
  904. wchar_t const* /* func */,
  905. wchar_t const* /* file */,
  906. unsigned /* line */,
  907. uintptr_t /* reserved */)
  908. {
  909. detail::report_error( execution_exception::user_error,
  910. "Invalid parameter detected by C runtime library" );
  911. }
  912. //____________________________________________________________________________//
  913. } // namespace detail
  914. // ************************************************************************** //
  915. // ************** execution_monitor::catch_signals ************** //
  916. // ************************************************************************** //
  917. int
  918. execution_monitor::catch_signals( boost::function<int ()> const& F )
  919. {
  920. _invalid_parameter_handler old_iph = _invalid_parameter_handler();
  921. BOOST_TEST_CRT_HOOK_TYPE old_crt_hook = 0;
  922. if( p_catch_system_errors ) {
  923. old_crt_hook = BOOST_TEST_CRT_SET_HOOK( &detail::assert_reporting_function );
  924. old_iph = _set_invalid_parameter_handler(
  925. reinterpret_cast<_invalid_parameter_handler>( &detail::invalid_param_handler ) );
  926. } else if( !p_detect_fp_exceptions ) {
  927. #if BOOST_WORKAROUND( BOOST_MSVC, <= 1310)
  928. _set_se_translator( &detail::seh_catch_preventer );
  929. #endif
  930. }
  931. detail::system_signal_exception SSE( this );
  932. int ret_val = 0;
  933. // clang windows workaround: this not available in __finally scope
  934. bool l_catch_system_errors = p_catch_system_errors;
  935. __try {
  936. __try {
  937. ret_val = detail::do_invoke( m_custom_translators, F );
  938. }
  939. __except( SSE( GetExceptionCode(), GetExceptionInformation() ) ) {
  940. throw SSE;
  941. }
  942. }
  943. __finally {
  944. if( l_catch_system_errors ) {
  945. BOOST_TEST_CRT_SET_HOOK( old_crt_hook );
  946. _set_invalid_parameter_handler( old_iph );
  947. }
  948. }
  949. return ret_val;
  950. }
  951. //____________________________________________________________________________//
  952. #else // default signal handler
  953. namespace detail {
  954. class system_signal_exception {
  955. public:
  956. void report() const {}
  957. };
  958. } // namespace detail
  959. int
  960. execution_monitor::catch_signals( boost::function<int ()> const& F )
  961. {
  962. return detail::do_invoke( m_custom_translators , F );
  963. }
  964. //____________________________________________________________________________//
  965. #endif // choose signal handler
  966. // ************************************************************************** //
  967. // ************** execution_monitor ************** //
  968. // ************************************************************************** //
  969. execution_monitor::execution_monitor()
  970. : p_catch_system_errors( true )
  971. , p_auto_start_dbg( false )
  972. , p_timeout( 0 )
  973. , p_use_alt_stack( true )
  974. , p_detect_fp_exceptions( fpe::BOOST_FPE_OFF )
  975. {}
  976. //____________________________________________________________________________//
  977. int
  978. execution_monitor::execute( boost::function<int ()> const& F )
  979. {
  980. if( debug::under_debugger() )
  981. p_catch_system_errors.value = false;
  982. BOOST_TEST_I_TRY {
  983. detail::fpe_except_guard G( p_detect_fp_exceptions );
  984. unit_test::ut_detail::ignore_unused_variable_warning( G );
  985. return catch_signals( F );
  986. }
  987. #ifndef BOOST_NO_EXCEPTIONS
  988. // Catch-clause reference arguments are a bit different from function
  989. // arguments (ISO 15.3 paragraphs 18 & 19). Apparently const isn't
  990. // required. Programmers ask for const anyhow, so we supply it. That's
  991. // easier than answering questions about non-const usage.
  992. catch( char const* ex )
  993. { detail::report_error( execution_exception::cpp_exception_error,
  994. "C string: %s", ex ); }
  995. catch( std::string const& ex )
  996. { detail::report_error( execution_exception::cpp_exception_error,
  997. "std::string: %s", ex.c_str() ); }
  998. // std:: exceptions
  999. #if defined(BOOST_NO_TYPEID) || defined(BOOST_NO_RTTI)
  1000. #define CATCH_AND_REPORT_STD_EXCEPTION( ex_name ) \
  1001. catch( ex_name const& ex ) \
  1002. { detail::report_error( execution_exception::cpp_exception_error, \
  1003. current_exception_cast<boost::exception const>(), \
  1004. #ex_name ": %s", ex.what() ); } \
  1005. /**/
  1006. #else
  1007. #define CATCH_AND_REPORT_STD_EXCEPTION( ex_name ) \
  1008. catch( ex_name const& ex ) \
  1009. { detail::report_error( execution_exception::cpp_exception_error, \
  1010. current_exception_cast<boost::exception const>(), \
  1011. "%s: %s", detail::typeid_name(ex).c_str(), ex.what() ); } \
  1012. /**/
  1013. #endif
  1014. CATCH_AND_REPORT_STD_EXCEPTION( std::bad_alloc )
  1015. #if BOOST_WORKAROUND(__BORLANDC__, <= 0x0551)
  1016. CATCH_AND_REPORT_STD_EXCEPTION( std::bad_cast )
  1017. CATCH_AND_REPORT_STD_EXCEPTION( std::bad_typeid )
  1018. #else
  1019. CATCH_AND_REPORT_STD_EXCEPTION( std::bad_cast )
  1020. CATCH_AND_REPORT_STD_EXCEPTION( std::bad_typeid )
  1021. #endif
  1022. CATCH_AND_REPORT_STD_EXCEPTION( std::bad_exception )
  1023. CATCH_AND_REPORT_STD_EXCEPTION( std::domain_error )
  1024. CATCH_AND_REPORT_STD_EXCEPTION( std::invalid_argument )
  1025. CATCH_AND_REPORT_STD_EXCEPTION( std::length_error )
  1026. CATCH_AND_REPORT_STD_EXCEPTION( std::out_of_range )
  1027. CATCH_AND_REPORT_STD_EXCEPTION( std::range_error )
  1028. CATCH_AND_REPORT_STD_EXCEPTION( std::overflow_error )
  1029. CATCH_AND_REPORT_STD_EXCEPTION( std::underflow_error )
  1030. CATCH_AND_REPORT_STD_EXCEPTION( std::logic_error )
  1031. CATCH_AND_REPORT_STD_EXCEPTION( std::runtime_error )
  1032. CATCH_AND_REPORT_STD_EXCEPTION( std::exception )
  1033. #undef CATCH_AND_REPORT_STD_EXCEPTION
  1034. catch( boost::exception const& ex )
  1035. { detail::report_error( execution_exception::cpp_exception_error,
  1036. &ex,
  1037. #if defined(BOOST_NO_TYPEID) || defined(BOOST_NO_RTTI)
  1038. "unknown boost::exception" ); }
  1039. #else
  1040. typeid(ex).name() ); }
  1041. #endif
  1042. // system errors
  1043. catch( system_error const& ex )
  1044. { detail::report_error( execution_exception::cpp_exception_error,
  1045. "system_error produced by: %s: %s", ex.p_failed_exp, std::strerror( ex.p_errno ) ); }
  1046. catch( detail::system_signal_exception const& ex )
  1047. { ex.report(); }
  1048. // not an error
  1049. catch( execution_aborted const& )
  1050. { return 0; }
  1051. // just forward
  1052. catch( execution_exception const& )
  1053. { throw; }
  1054. // unknown error
  1055. catch( ... )
  1056. { detail::report_error( execution_exception::cpp_exception_error, "unknown type" ); }
  1057. #endif // !BOOST_NO_EXCEPTIONS
  1058. return 0; // never reached; supplied to quiet compiler warnings
  1059. } // execute
  1060. //____________________________________________________________________________//
  1061. namespace detail {
  1062. struct forward {
  1063. explicit forward( boost::function<void ()> const& F ) : m_F( F ) {}
  1064. int operator()() { m_F(); return 0; }
  1065. boost::function<void ()> const& m_F;
  1066. };
  1067. } // namespace detail
  1068. void
  1069. execution_monitor::vexecute( boost::function<void ()> const& F )
  1070. {
  1071. execute( detail::forward( F ) );
  1072. }
  1073. // ************************************************************************** //
  1074. // ************** system_error ************** //
  1075. // ************************************************************************** //
  1076. system_error::system_error( char const* exp )
  1077. #ifdef UNDER_CE
  1078. : p_errno( GetLastError() )
  1079. #else
  1080. : p_errno( errno )
  1081. #endif
  1082. , p_failed_exp( exp )
  1083. {}
  1084. //____________________________________________________________________________//
  1085. // ************************************************************************** //
  1086. // ************** execution_exception ************** //
  1087. // ************************************************************************** //
  1088. execution_exception::execution_exception( error_code ec_, const_string what_msg_, location const& location_ )
  1089. : m_error_code( ec_ )
  1090. , m_what( what_msg_.empty() ? BOOST_TEST_L( "uncaught exception, system error or abort requested" ) : what_msg_ )
  1091. , m_location( location_ )
  1092. {}
  1093. //____________________________________________________________________________//
  1094. execution_exception::location::location( char const* file_name, size_t line_num, char const* func )
  1095. : m_file_name( file_name ? file_name : "unknown location" )
  1096. , m_line_num( line_num )
  1097. , m_function( func )
  1098. {}
  1099. execution_exception::location::location(const_string file_name, size_t line_num, char const* func )
  1100. : m_file_name( file_name )
  1101. , m_line_num( line_num )
  1102. , m_function( func )
  1103. {}
  1104. //____________________________________________________________________________//
  1105. // ************************************************************************** //
  1106. // **************Floating point exception management interface ************** //
  1107. // ************************************************************************** //
  1108. namespace fpe {
  1109. unsigned
  1110. enable( unsigned mask )
  1111. {
  1112. boost::ignore_unused(mask);
  1113. #if defined(BOOST_TEST_FPE_SUPPORT_WITH_SEH__)
  1114. _clearfp();
  1115. #if BOOST_WORKAROUND( BOOST_MSVC, <= 1310)
  1116. unsigned old_cw = ::_controlfp( 0, 0 );
  1117. ::_controlfp( old_cw & ~mask, BOOST_FPE_ALL );
  1118. #else
  1119. unsigned old_cw;
  1120. if( ::_controlfp_s( &old_cw, 0, 0 ) != 0 )
  1121. return BOOST_FPE_INV;
  1122. // Set the control word
  1123. if( ::_controlfp_s( 0, old_cw & ~mask, BOOST_FPE_ALL ) != 0 )
  1124. return BOOST_FPE_INV;
  1125. #endif
  1126. return ~old_cw & BOOST_FPE_ALL;
  1127. #elif defined(BOOST_TEST_FPE_SUPPORT_WITH_GLIBC_EXTENSIONS__)
  1128. // same macro definition as in execution_monitor.hpp
  1129. if (BOOST_FPE_ALL == BOOST_FPE_OFF)
  1130. /* Not Implemented */
  1131. return BOOST_FPE_OFF;
  1132. feclearexcept(BOOST_FPE_ALL);
  1133. int res = feenableexcept( mask );
  1134. return res == -1 ? (unsigned)BOOST_FPE_INV : (unsigned)res;
  1135. #else
  1136. /* Not Implemented */
  1137. return BOOST_FPE_OFF;
  1138. #endif
  1139. }
  1140. //____________________________________________________________________________//
  1141. unsigned
  1142. disable( unsigned mask )
  1143. {
  1144. boost::ignore_unused(mask);
  1145. #if defined(BOOST_TEST_FPE_SUPPORT_WITH_SEH__)
  1146. _clearfp();
  1147. #if BOOST_WORKAROUND( BOOST_MSVC, <= 1310)
  1148. unsigned old_cw = ::_controlfp( 0, 0 );
  1149. ::_controlfp( old_cw | mask, BOOST_FPE_ALL );
  1150. #else
  1151. unsigned old_cw;
  1152. if( ::_controlfp_s( &old_cw, 0, 0 ) != 0 )
  1153. return BOOST_FPE_INV;
  1154. // Set the control word
  1155. if( ::_controlfp_s( 0, old_cw | mask, BOOST_FPE_ALL ) != 0 )
  1156. return BOOST_FPE_INV;
  1157. #endif
  1158. return ~old_cw & BOOST_FPE_ALL;
  1159. #elif defined(BOOST_TEST_FPE_SUPPORT_WITH_GLIBC_EXTENSIONS__)
  1160. if (BOOST_FPE_ALL == BOOST_FPE_OFF)
  1161. /* Not Implemented */
  1162. return BOOST_FPE_INV;
  1163. feclearexcept(BOOST_FPE_ALL);
  1164. int res = fedisableexcept( mask );
  1165. return res == -1 ? (unsigned)BOOST_FPE_INV : (unsigned)res;
  1166. #else
  1167. /* Not Implemented */
  1168. return BOOST_FPE_INV;
  1169. #endif
  1170. }
  1171. //____________________________________________________________________________//
  1172. } // namespace fpe
  1173. } // namespace boost
  1174. #include <boost/test/detail/enable_warnings.hpp>
  1175. #endif // BOOST_TEST_EXECUTION_MONITOR_IPP_012205GER