Skip to content

Commit 8d44eed

Browse files
authored
Fixes dead loop when abort called in Win32/CI (#5197)
Disable the debug popup on MSVC/win32 by introduce new jerry_port_init function For not popup dialog when crash happend on MSVC/win32 Closed: #4463 JerryScript-DCO-1.0-Signed-off-by: Yonggang Luo [email protected]
1 parent aa7861d commit 8d44eed

File tree

5 files changed

+87
-0
lines changed

5 files changed

+87
-0
lines changed

docs/05.PORT-API.md

+7
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,13 @@
44

55
It is questionable whether a library should be able to terminate an application. Any API function can signal an error (ex.: cannot allocate memory), so the engine use the termination approach with this port function.
66

7+
```c
8+
/**
9+
* Init the program
10+
*/
11+
void jerry_port_init (void);
12+
```
13+
714
```c
815
/**
916
* Signal the port that jerry experienced a fatal failure from which it cannot

jerry-core/api/jerryscript.c

+1
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,7 @@ jerry_return (const jerry_value_t value) /**< return value */
172172
void
173173
jerry_init (jerry_init_flag_t flags) /**< combination of Jerry flags */
174174
{
175+
jerry_port_init ();
175176
#if JERRY_EXTERNAL_CONTEXT
176177
size_t total_size = jerry_port_context_alloc (sizeof (jerry_context_t));
177178
JERRY_UNUSED (total_size);

jerry-core/include/jerryscript-port.h

+5
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,11 @@ typedef enum
4949
JERRY_FATAL_FAILED_ASSERTION = 120 /**< Assertion failed */
5050
} jerry_fatal_code_t;
5151

52+
/**
53+
* Init the program
54+
*/
55+
void jerry_port_init (void);
56+
5257
/**
5358
* Signal the port that the process experienced a fatal failure from which it cannot
5459
* recover.

jerry-port/common/jerry-port-process.c

+12
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,18 @@
1717

1818
#include "jerryscript-port.h"
1919

20+
#if !defined(_WIN32)
21+
22+
/**
23+
* Default implementation of jerry_port_init. Do nothing.
24+
*/
25+
void JERRY_ATTR_WEAK
26+
jerry_port_init (void)
27+
{
28+
} /* jerry_port_init */
29+
30+
#endif /* !defined(_WIN32) */
31+
2032
/**
2133
* Default implementation of jerry_port_fatal. Calls 'abort' if exit code is
2234
* non-zero, 'exit' otherwise.

jerry-port/win/jerry-port-win-process.c

+62
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,68 @@
1818

1919
#include <windows.h>
2020

21+
#include <crtdbg.h>
22+
#include <stdio.h>
23+
24+
void
25+
jerry_port_init (void)
26+
{
27+
if (!IsDebuggerPresent ())
28+
{
29+
/* Make output streams unbuffered by default */
30+
setvbuf (stdout, NULL, _IONBF, 0);
31+
setvbuf (stderr, NULL, _IONBF, 0);
32+
33+
/*
34+
* By default abort() only generates a crash-dump in *non* debug
35+
* builds. As our Assert() / ExceptionalCondition() uses abort(),
36+
* leaving the default in place would make debugging harder.
37+
*
38+
* MINGW's own C runtime doesn't have _set_abort_behavior(). When
39+
* targeting Microsoft's UCRT with mingw, it never links to the debug
40+
* version of the library and thus doesn't need the call to
41+
* _set_abort_behavior() either.
42+
*/
43+
#if !defined(__MINGW32__) && !defined(__MINGW64__)
44+
_set_abort_behavior (_CALL_REPORTFAULT | _WRITE_ABORT_MSG, _CALL_REPORTFAULT | _WRITE_ABORT_MSG);
45+
#endif /* !defined(__MINGW32__) && !defined(__MINGW64__) */
46+
47+
/*
48+
* SEM_FAILCRITICALERRORS causes more errors to be reported to
49+
* callers.
50+
*
51+
* We used to also specify SEM_NOGPFAULTERRORBOX, but that prevents
52+
* windows crash reporting from working. Which includes registered
53+
* just-in-time debuggers, making it unnecessarily hard to debug
54+
* problems on windows. Now we try to disable sources of popups
55+
* separately below (note that SEM_NOGPFAULTERRORBOX did not actually
56+
* prevent all sources of such popups).
57+
*/
58+
SetErrorMode (SEM_FAILCRITICALERRORS);
59+
60+
/*
61+
* Show errors on stderr instead of popup box (note this doesn't
62+
* affect errors originating in the C runtime, see below).
63+
*/
64+
_set_error_mode (_OUT_TO_STDERR);
65+
66+
/*
67+
* In DEBUG builds, errors, including assertions, C runtime errors are
68+
* reported via _CrtDbgReport. By default such errors are displayed
69+
* with a popup (even with NOGPFAULTERRORBOX), preventing forward
70+
* progress. Instead report such errors stderr (and the debugger).
71+
* This is C runtime specific and thus the above incantations aren't
72+
* sufficient to suppress these popups.
73+
*/
74+
_CrtSetReportMode (_CRT_ERROR, _CRTDBG_MODE_FILE | _CRTDBG_MODE_DEBUG);
75+
_CrtSetReportFile (_CRT_ERROR, _CRTDBG_FILE_STDERR);
76+
_CrtSetReportMode (_CRT_ASSERT, _CRTDBG_MODE_FILE | _CRTDBG_MODE_DEBUG);
77+
_CrtSetReportFile (_CRT_ASSERT, _CRTDBG_FILE_STDERR);
78+
_CrtSetReportMode (_CRT_WARN, _CRTDBG_MODE_FILE | _CRTDBG_MODE_DEBUG);
79+
_CrtSetReportFile (_CRT_WARN, _CRTDBG_FILE_STDERR);
80+
}
81+
} /* jerry_port_init */
82+
2183
/**
2284
* Default implementation of jerry_port_sleep, uses 'Sleep'.
2385
*/

0 commit comments

Comments
 (0)