execution_monitor.ipp 51 KB

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