File size: 16,046 Bytes
90219c5 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 | /***
*assert.c - Display a message and abort
*
* Copyright (c) Microsoft Corporation. All rights reserved.
*
*Purpose:
*
*******************************************************************************/
#include <corecrt_internal.h>
#include <corecrt_internal_stdio.h>
#include <limits.h>
#include <signal.h>
#include <stdio.h>
#include <string.h>
#undef NDEBUG
#define _ASSERT_OK
#include <assert.h>
// Assertion string components:
#define MAXLINELEN 64 /* max length for line in message box */
#define ASSERTBUFSZ (MAXLINELEN * 9) /* 9 lines in message box */
// Format of stderr for assertions:
//
// Assertion failed: <expression>, file c:\test\mytest\bar.c, line 69
//
_GENERATE_TCHAR_STRING_FUNCTIONS(assert_format, "Assertion failed: %Ts, file %Ts, line %d\n")
// Enclaves only support assertions sent to the debugger.
// This mode could also be enabled for normal apps as well.
#ifdef _UCRT_ENCLAVE_BUILD
template <typename Character>
__declspec(noreturn) static void __cdecl common_assert_to_debug(
Character const* const expression,
Character const* const file_name,
unsigned const line_number
) throw()
{
using traits = __crt_char_traits<Character>;
Character assert_buffer[ASSERTBUFSZ];
if (traits::sntprintf_s(assert_buffer, _countof(assert_buffer), _countof(assert_buffer), get_assert_format(Character()), expression, file_name, line_number) < 0)
{
abort();
}
traits::output_debug_string(assert_buffer);
abort();
}
template <typename Character>
static void __cdecl common_assert(
Character const* const expression,
Character const* const file_name,
unsigned const line_number,
void* const
) throw()
{
common_assert_to_debug(expression, file_name, line_number);
}
#else /* ^^^ _UCRT_ENCLAVE_BUILD ^^^ // vvv !_UCRT_ENCLAVE_BUILD vvv */
// Format of MessageBox for assertions:
//
// ================= Microsft Visual C++ Debug Library ================
//
// Assertion Failed!
//
// Program: c:\test\mytest\foo.exe
// File: c:\test\mytest\bar.c
// Line: 69
//
// Expression: <expression>
//
// For information on how your program can cause an assertion
// failure, see the Visual C++ documentation on asserts
//
// (Press Retry to debug the application - JIT must be enabled)
//
// ===================================================================
_GENERATE_TCHAR_STRING_FUNCTIONS(banner_text, "Microsoft Visual C++ Runtime Library")
_GENERATE_TCHAR_STRING_FUNCTIONS(box_intro, "Assertion failed!")
_GENERATE_TCHAR_STRING_FUNCTIONS(program_intro, "Program: ")
_GENERATE_TCHAR_STRING_FUNCTIONS(file_intro, "File: ")
_GENERATE_TCHAR_STRING_FUNCTIONS(line_intro, "Line: ")
_GENERATE_TCHAR_STRING_FUNCTIONS(expression_intro, "Expression: ")
_GENERATE_TCHAR_STRING_FUNCTIONS(info_intro, "For information on how your program can cause an assertion\nfailure, see the Visual C++ documentation on asserts")
_GENERATE_TCHAR_STRING_FUNCTIONS(help_intro, "(Press Retry to debug the application - JIT must be enabled)")
_GENERATE_TCHAR_STRING_FUNCTIONS(dot_dot_dot, "...")
_GENERATE_TCHAR_STRING_FUNCTIONS(newline, "\n")
_GENERATE_TCHAR_STRING_FUNCTIONS(double_newline, "\n\n")
_GENERATE_TCHAR_STRING_FUNCTIONS(program_name_unknown_text, "<program name unknown>")
/***
*_assert() - Display a message and abort
*
*Purpose:
* The assert macro calls this routine if the assert expression is
* true. By placing the assert code in a subroutine instead of within
* the body of the macro, programs that call assert multiple times will
* save space.
*
*Entry:
*
*Exit:
*
*Exceptions:
*
*******************************************************************************/
static void __cdecl common_assert_to_stderr_direct(char const*, char const*, unsigned) throw()
{
// No action for narrow strings
}
static void __cdecl common_assert_to_stderr_direct(
wchar_t const* const expression,
wchar_t const* const file_name,
unsigned const line_number
) throw()
{
HANDLE const stderr_handle = GetStdHandle(STD_ERROR_HANDLE);
#pragma warning(suppress:__WARNING_REDUNDANT_POINTER_TEST) // 28922
if (stderr_handle == INVALID_HANDLE_VALUE || stderr_handle == nullptr)
{
return;
}
if (GetFileType(stderr_handle) != FILE_TYPE_CHAR)
{
return;
}
wchar_t assert_buffer[ASSERTBUFSZ];
#pragma warning(suppress:__WARNING_BANNED_API_USAGE) // 28719
if (swprintf(assert_buffer, _countof(assert_buffer), get_assert_format(wchar_t()), expression, file_name, line_number) < 0)
{
return;
}
DWORD const assert_buffer_length = static_cast<DWORD>(wcslen(assert_buffer));
DWORD characters_written = 0;
if (WriteConsoleW(stderr_handle, assert_buffer, assert_buffer_length, &characters_written, nullptr) == 0)
{
return;
}
abort();
}
template <typename Character>
__declspec(noreturn) static void __cdecl common_assert_to_stderr(
Character const* const expression,
Character const* const file_name,
unsigned const line_number
) throw()
{
using traits = __crt_char_traits<Character>;
// Try to write directly to the console. This is only supported for wide
// character strings. If we have a narrow character string or the write
// fails, we fall back to call through stdio.
common_assert_to_stderr_direct(expression, file_name, line_number);
// If stderr does not yet have a buffer, set it to use single character
// buffering to avoid dynamic allocation of a stream buffer:
if (!__crt_stdio_stream(stderr).has_any_buffer())
{
setvbuf(stderr, nullptr, _IONBF, 0);
}
traits::ftprintf(stderr, get_assert_format(Character()), expression, file_name, line_number);
fflush(stderr);
abort();
}
template <typename Character>
static void __cdecl common_assert_to_message_box_build_string(
Character* const assert_buffer,
size_t const assert_buffer_count,
Character const* const expression,
Character const* const file_name,
unsigned const line_number,
void* const return_address
) throw()
{
using traits = __crt_char_traits<Character>;
// Line 1: Box introduction line:
_ERRCHECK(traits::tcscpy_s(assert_buffer, assert_buffer_count, get_box_intro(Character())));
_ERRCHECK(traits::tcscat_s(assert_buffer, assert_buffer_count, get_double_newline(Character())));
// Line 2: Program line:
_ERRCHECK(traits::tcscat_s(assert_buffer, assert_buffer_count, get_program_intro(Character())));
Character program_name[_MAX_PATH + 1]{};
HMODULE asserting_module = nullptr;
if (!GetModuleHandleExW(
GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT | GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS,
static_cast<wchar_t const*>(return_address),
&asserting_module))
{
asserting_module = nullptr;
}
#ifdef CRTDLL
// If the assert came from within the CRT DLL, report it as having come
// from the EXE instead:
if (asserting_module == reinterpret_cast<HMODULE>(&__ImageBase))
{
asserting_module = nullptr;
}
#endif
if (!traits::get_module_file_name(asserting_module, program_name, static_cast<DWORD>(_countof(program_name))))
{
_ERRCHECK(traits::tcscpy_s(program_name, _countof(program_name), get_program_name_unknown_text(Character())));
}
Character* pchProg = program_name;
if (program_intro_count + traits::tcslen(program_name) + newline_length > MAXLINELEN)
{
pchProg += (program_intro_count + traits::tcslen(program_name) + newline_length) - MAXLINELEN;
// Only replace first (sizeof(Character) * dot_dot_dot_length) bytes to ellipsis:
_ERRCHECK(memcpy_s(
pchProg,
sizeof(Character) * ((MAX_PATH + 1) - (pchProg - program_name)),
get_dot_dot_dot(Character()),
sizeof(Character) * dot_dot_dot_length
));
}
_ERRCHECK(traits::tcscat_s(assert_buffer, assert_buffer_count, pchProg));
_ERRCHECK(traits::tcscat_s(assert_buffer, assert_buffer_count, get_newline(Character())));
// Line 3: File line
_ERRCHECK(traits::tcscat_s(assert_buffer, assert_buffer_count, get_file_intro(Character())));
if (file_intro_count + traits::tcslen(file_name) + newline_length > MAXLINELEN)
{
size_t const ffn = MAXLINELEN - file_intro_count - newline_length;
size_t p = 0;
size_t len = 0;
Character const* pch = file_name;
for (len = traits::tcslen(file_name), p = 1;
pch[len - p] != '\\' && pch[len - p] != '/' && p < len;
p++)
{
}
// Trim the path and file name so that they fit, using up to 2/3 of
// the maximum number of characters for the path and the remaining
// 1/3 for the file name:
if ((ffn - ffn / 3) < (len - p) && ffn / 3 > p)
{
// The path is too long. Use the first part of the path and the
// full file name:
_ERRCHECK(traits::tcsncat_s(assert_buffer, assert_buffer_count, pch, ffn - dot_dot_dot_length - p));
_ERRCHECK(traits::tcscat_s (assert_buffer, assert_buffer_count, get_dot_dot_dot(Character())));
_ERRCHECK(traits::tcscat_s (assert_buffer, assert_buffer_count, pch + len - p));
}
else if (ffn - ffn / 3 > len - p)
{
// The file name is too long. Use the full path and the first
// and last part of the file name, with a ... in between:
p = p / 2;
_ERRCHECK(traits::tcsncat_s(assert_buffer, assert_buffer_count, pch, ffn - dot_dot_dot_length - p));
_ERRCHECK(traits::tcscat_s (assert_buffer, assert_buffer_count, get_dot_dot_dot(Character())));
_ERRCHECK(traits::tcscat_s (assert_buffer, assert_buffer_count, pch + len - p));
}
else
{
// Both are too long. Use the first part of the path and the
// first and last part of the file name, with ...s in between:
_ERRCHECK(traits::tcsncat_s(assert_buffer, assert_buffer_count, pch, ffn - ffn / 3 - dot_dot_dot_length));
_ERRCHECK(traits::tcscat_s (assert_buffer, assert_buffer_count, get_dot_dot_dot(Character())));
_ERRCHECK(traits::tcsncat_s(assert_buffer, assert_buffer_count, pch + len - p, ffn / 6 - 1));
_ERRCHECK(traits::tcscat_s (assert_buffer, assert_buffer_count, get_dot_dot_dot(Character())));
_ERRCHECK(traits::tcscat_s (assert_buffer, assert_buffer_count, pch + len - (ffn / 3 - ffn / 6 - 2)));
}
}
else
{
// Plenty of room on the line; just append the full path and file name:
_ERRCHECK(traits::tcscat_s(assert_buffer, assert_buffer_count, file_name));
}
_ERRCHECK(traits::tcscat_s(assert_buffer, assert_buffer_count, get_newline(Character())));
// Line 4: Line Number line:
_ERRCHECK(traits::tcscat_s(assert_buffer, assert_buffer_count, get_line_intro(Character())));
_ERRCHECK(traits::itot_s(
line_number,
assert_buffer + traits::tcslen(assert_buffer),
assert_buffer_count - traits::tcslen(assert_buffer),
10));
_ERRCHECK(traits::tcscat_s(assert_buffer, assert_buffer_count, get_double_newline(Character())));
// Line 5: Message line:
_ERRCHECK(traits::tcscat_s(assert_buffer, assert_buffer_count, get_expression_intro(Character())));
size_t const characters_used =
traits::tcslen(assert_buffer) +
2 * double_newline_length +
info_intro_length +
help_intro_count;
if (characters_used + traits::tcslen(expression) > assert_buffer_count)
{
size_t const characters_to_write = assert_buffer_count - (characters_used + dot_dot_dot_length);
_ERRCHECK(traits::tcsncat_s(
assert_buffer,
assert_buffer_count,
expression,
characters_to_write));
_ERRCHECK(traits::tcscat_s(assert_buffer, assert_buffer_count, get_dot_dot_dot(Character())));
}
else
{
_ERRCHECK(traits::tcscat_s(assert_buffer, assert_buffer_count, expression));
}
_ERRCHECK(traits::tcscat_s(assert_buffer, assert_buffer_count, get_double_newline(Character())));
// Info line:
_ERRCHECK(traits::tcscat_s(assert_buffer, assert_buffer_count, get_info_intro(Character())));
_ERRCHECK(traits::tcscat_s(assert_buffer, assert_buffer_count, get_double_newline(Character())));
// Help line:
_ERRCHECK(traits::tcscat_s(assert_buffer, assert_buffer_count, get_help_intro(Character())));
}
template <typename Character>
static void __cdecl common_assert_to_message_box(
Character const* const expression,
Character const* const file_name,
unsigned const line_number,
void* const return_address
) throw()
{
using traits = __crt_char_traits<Character>;
Character assert_buffer[ASSERTBUFSZ]{};
common_assert_to_message_box_build_string(
assert_buffer,
_countof(assert_buffer),
expression,
file_name,
line_number,
return_address);
int const action = traits::show_message_box(
assert_buffer,
get_banner_text(Character()),
MB_TASKMODAL | MB_ICONHAND | MB_ABORTRETRYIGNORE | MB_SETFOREGROUND);
switch (action)
{
case IDABORT: // Abort the program:
{
raise(SIGABRT);
// We won't usually get here, but it's possible that a user-registered
// abort handler returns, so exit the program immediately. Note that
// even though we are "aborting," we do not call abort() because we do
// not want to invoke Watson (the user has already had an opportunity
// to debug the error and chose not to).
_exit(3);
}
case IDRETRY: // Break into the debugger then return control to caller
{
__debugbreak();
return;
}
case IDIGNORE: // Return control to caller
{
return;
}
default: // This should not happen; treat as fatal error:
{
abort();
}
}
}
template <typename Character>
static void __cdecl common_assert(
Character const* const expression,
Character const* const file_name,
unsigned const line_number,
void* const return_address
) throw()
{
using traits = __crt_char_traits<Character>;
int const current_error_mode = _set_error_mode(_REPORT_ERRMODE);
if (current_error_mode == _OUT_TO_STDERR)
{
return common_assert_to_stderr(expression, file_name, line_number);
}
if (current_error_mode == _OUT_TO_DEFAULT && _query_app_type() == _crt_console_app)
{
return common_assert_to_stderr(expression, file_name, line_number);
}
return common_assert_to_message_box(expression, file_name, line_number, return_address);
}
#endif /* _UCRT_ENCLAVE_BUILD */
extern "C" void __cdecl _assert(
char const* const expression,
char const* const file_name,
unsigned const line_number
) throw()
{
return common_assert(expression, file_name, line_number, _ReturnAddress());
}
extern "C" void __cdecl _wassert(
wchar_t const* const expression,
wchar_t const* const file_name,
unsigned const line_number
)
{
return common_assert(expression, file_name, line_number, _ReturnAddress());
}
|