1/* bug.cpp                  -*-C++-*-
2 *
3 *************************************************************************
4 *
5 *  @copyright
6 *  Copyright (C) 2009-2013, Intel Corporation
7 *  All rights reserved.
8 *
9 *  @copyright
10 *  Redistribution and use in source and binary forms, with or without
11 *  modification, are permitted provided that the following conditions
12 *  are met:
13 *
14 *    * Redistributions of source code must retain the above copyright
15 *      notice, this list of conditions and the following disclaimer.
16 *    * Redistributions in binary form must reproduce the above copyright
17 *      notice, this list of conditions and the following disclaimer in
18 *      the documentation and/or other materials provided with the
19 *      distribution.
20 *    * Neither the name of Intel Corporation nor the names of its
21 *      contributors may be used to endorse or promote products derived
22 *      from this software without specific prior written permission.
23 *
24 *  @copyright
25 *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26 *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27 *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28 *  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29 *  HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
30 *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
31 *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
32 *  OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
33 *  AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY
35 *  WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 *  POSSIBILITY OF SUCH DAMAGE.
37 **************************************************************************/
38
39#include "bug.h"
40
41#include <exception>
42#include <stdio.h>
43#include <stdarg.h>
44#include <stdlib.h>
45#ifdef _WIN32
46#   include "windows-clean.h"
47#   include "internal/abi.h"
48#   include "cilktools/cilkscreen.h"
49#   include <crtdbg.h>
50#endif
51
52__CILKRTS_BEGIN_EXTERN_C
53
54COMMON_PORTABLE const char *const __cilkrts_assertion_failed =
55    "%s:%d: cilk assertion failed: %s\n";
56
57COMMON_PORTABLE void __cilkrts_bug(const char *fmt,...) cilk_nothrow
58{
59#if defined (_WIN32) && defined(_DEBUG)
60    _CRTIMP void __cdecl _wassert(__in_z const wchar_t * _Message,
61                                  __in_z const wchar_t *_File,
62                                  __in unsigned _Line);
63    char message[256];
64    wchar_t wmessage[256];
65    va_list l;
66    va_start(l, fmt);
67    _vsnprintf_s(message, 256, _TRUNCATE, fmt, l);
68    va_end(l);
69    _snwprintf_s(wmessage, 256, _TRUNCATE, _CRT_WIDE("%S"),
70                 message); /* widen */
71
72    // Force asserts to go to stderr and the debugger.  This isn't polite, but
73    // we're about to kill the app anyway and it will prevent our tests from
74    // hanging
75    _CrtSetReportMode(_CRT_ASSERT, _CRTDBG_MODE_FILE| _CRTDBG_MODE_DEBUG);
76    _CrtSetReportFile(_CRT_ASSERT, _CRTDBG_FILE_STDERR);
77
78    _wassert(wmessage, _CRT_WIDE(__FILE__), __LINE__);
79
80    // If there's a debugger attached, give it a chance to look at the failure
81    if (IsDebuggerPresent())
82        DebugBreak();
83
84    abort();
85/*    __asm int 3 */
86#else
87    /* To reduce user confusion, write all user-generated output
88       before the system-generated error message. */
89    va_list l;
90    fflush(NULL);
91    va_start(l, fmt);
92    vfprintf(stderr, fmt, l);
93    va_end(l);
94    fflush(stderr);
95
96#ifndef _WIN32
97    abort();
98#endif
99
100#endif
101
102    exit(1);
103}
104
105COMMON_PORTABLE void cilkbug_assert_no_uncaught_exception(void)
106{
107    bool uncaught = std::uncaught_exception();
108    CILK_ASSERT(!uncaught);
109}
110
111COMMON_SYSDEP void abort_because_rts_is_corrupted(void)
112{
113    __cilkrts_bug("The Cilk Plus runtime system detected a corruption "
114                  "in its data structures.  This is most likely caused "
115                  "by an application bug.  Aborting execution.\n");
116}
117
118#ifdef WIN32
119COMMON_SYSDEP void __cilkrts_dbgprintf(const char *fmt,...)
120{
121    char message[2048];
122    va_list l;
123
124    // Cilkscreen shouldn't watch this
125    __cilkscreen_disable_checking();
126
127    va_start(l, fmt);
128    _vsnprintf_s(message, 2048, _TRUNCATE, fmt, l);
129    va_end(l);
130    OutputDebugStringA (message);
131
132    // Re-enable Cilkscreen
133    __cilkscreen_enable_checking();
134}
135#endif
136
137__CILKRTS_END_EXTERN_C
138
139/* End bug.cpp */
140