execution_monitor.ipp 53 KB

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