1 /* Unit test for VEH on Windows
2 * Copyright (C) 2019 Руслан Ижбулатов
3 *
4 * This work is provided "as is"; redistribution and modification
5 * in whole or in part, in any medium, physical or electronic is
6 * permitted without restriction.
7 *
8 * This work is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11 *
12 * In no event shall the authors or contributors be liable for any
13 * direct, indirect, incidental, special, exemplary, or consequential
14 * damages (including, but not limited to, procurement of substitute
15 * goods or services; loss of use, data, or profits; or business
16 * interruption) however caused and on any theory of liability, whether
17 * in contract, strict liability, or tort (including negligence or
18 * otherwise) arising in any way out of the use of this software, even
19 * if advised of the possibility of such damage.
20 */
21
22 #include "config.h"
23
24 #include <glib.h>
25 #include <gprintf.h>
26 #include <stdio.h>
27 #include <windows.h>
28
29 static char *argv0 = NULL;
30
31 #include "../gwin32-private.c"
32
33 static void
test_subst_pid_and_event(void)34 test_subst_pid_and_event (void)
35 {
36 const gchar not_enough[] = "too long when %e and %p are substituted";
37 gchar debugger_3[3];
38 gchar debugger_not_enough[G_N_ELEMENTS (not_enough)];
39 gchar debugger_enough[G_N_ELEMENTS (not_enough) + 1];
40 gchar debugger_big[65535] = {0};
41 gchar *output;
42 guintptr be = (guintptr) 0xFFFFFFFF;
43 DWORD bp = G_MAXSIZE;
44
45 /* %f is not valid */
46 g_assert_false (_g_win32_subst_pid_and_event (debugger_3, G_N_ELEMENTS (debugger_3),
47 "%f", 0, 0));
48
49 g_assert_false (_g_win32_subst_pid_and_event (debugger_3, G_N_ELEMENTS (debugger_3),
50 "string longer than 10", 0, 0));
51 /* 200 is longer than %e, so the string doesn't fit by 1 byte */
52 g_assert_false (_g_win32_subst_pid_and_event (debugger_not_enough, G_N_ELEMENTS (debugger_not_enough),
53 "too long when %e and %p are substituted", 10, 200));
54
55 /* This should fit */
56 g_assert_true (_g_win32_subst_pid_and_event (debugger_enough, G_N_ELEMENTS (debugger_enough),
57 "too long when %e and %p are substituted", 10, 200));
58 g_assert_cmpstr (debugger_enough, ==, "too long when 200 and 10 are substituted");
59
60 g_assert_true (_g_win32_subst_pid_and_event (debugger_big, G_N_ELEMENTS (debugger_big),
61 "multipl%e big %e %entries and %pids are %provided here", bp, be));
62 output = g_strdup_printf ("multipl%llu big %llu %lluntries and %luids are %lurovided here", (guint64) be, (guint64) be, (guint64) be, bp, bp);
63 g_assert_cmpstr (debugger_big, ==, output);
64 g_free (output);
65 }
66
67 /* Crash with access violation */
68 static void
test_access_violation(void)69 test_access_violation (void)
70 {
71 int *integer = NULL;
72 /* Use SEM_NOGPFAULTERRORBOX to prevent an error dialog
73 * from being shown.
74 */
75 DWORD dwMode = SetErrorMode (SEM_NOGPFAULTERRORBOX);
76 SetErrorMode (dwMode | SEM_NOGPFAULTERRORBOX);
77 *integer = 1;
78 SetErrorMode (dwMode);
79 }
80
81 /* Crash with illegal instruction */
82 static void
test_illegal_instruction(void)83 test_illegal_instruction (void)
84 {
85 DWORD dwMode = SetErrorMode (SEM_NOGPFAULTERRORBOX);
86 SetErrorMode (dwMode | SEM_NOGPFAULTERRORBOX);
87 RaiseException (EXCEPTION_ILLEGAL_INSTRUCTION, 0, 0, NULL);
88 SetErrorMode (dwMode);
89 }
90
91 static void
test_veh_crash_access_violation(void)92 test_veh_crash_access_violation (void)
93 {
94 g_unsetenv ("G_DEBUGGER");
95 /* Run a test that crashes */
96 g_test_trap_subprocess ("/win32/subprocess/access_violation", 0, 0);
97 g_test_trap_assert_failed ();
98 g_test_trap_assert_stderr ("Exception code=0xc0000005*");
99 }
100
101 static void
test_veh_crash_illegal_instruction(void)102 test_veh_crash_illegal_instruction (void)
103 {
104 g_unsetenv ("G_DEBUGGER");
105 /* Run a test that crashes */
106 g_test_trap_subprocess ("/win32/subprocess/illegal_instruction", 0, 0);
107 g_test_trap_assert_failed ();
108 g_test_trap_assert_stderr ("Exception code=0xc000001d*");
109 }
110
111 static void
test_veh_debug(void)112 test_veh_debug (void)
113 {
114 /* Run a test that crashes and runs a debugger */
115 g_test_trap_subprocess ("/win32/subprocess/debuggee", 0, 0);
116 g_test_trap_assert_failed ();
117 g_test_trap_assert_stderr ("Exception code=0xc0000005*Debugger invoked, attaching to*");
118 }
119
120 static void
test_veh_debuggee(void)121 test_veh_debuggee (void)
122 {
123 /* Set up a debugger to be run on crash */
124 gchar *command = g_strdup_printf ("%s %s", argv0, "%p %e");
125 g_setenv ("G_DEBUGGER", command, TRUE);
126 /* Because the "debugger" here is not really a debugger,
127 * it can't write into stderr of this process, unless
128 * we allow it to inherit our stderr.
129 */
130 g_setenv ("G_DEBUGGER_OLD_CONSOLE", "1", TRUE);
131 g_free (command);
132 /* Crash */
133 test_access_violation ();
134 }
135
136 static void
veh_debugger(int argc,char * argv[])137 veh_debugger (int argc, char *argv[])
138 {
139 char *end;
140 DWORD pid = strtoul (argv[1], &end, 10);
141 guintptr event = (guintptr) _strtoui64 (argv[2], &end, 10);
142 /* Unfreeze the debuggee and announce ourselves */
143 SetEvent ((HANDLE) event);
144 CloseHandle ((HANDLE) event);
145 g_fprintf (stderr, "Debugger invoked, attaching to %lu and signalling %" G_GUINTPTR_FORMAT, pid, event);
146 }
147
148 int
main(int argc,char * argv[])149 main (int argc,
150 char *argv[])
151 {
152 argv0 = argv[0];
153
154 g_test_init (&argc, &argv, NULL);
155
156 if (argc > 2)
157 {
158 veh_debugger (argc, argv);
159 return 0;
160 }
161
162 g_test_add_func ("/win32/substitute-pid-and-event", test_subst_pid_and_event);
163
164 g_test_add_func ("/win32/veh/access_violation", test_veh_crash_access_violation);
165 g_test_add_func ("/win32/veh/illegal_instruction", test_veh_crash_illegal_instruction);
166 g_test_add_func ("/win32/veh/debug", test_veh_debug);
167
168 g_test_add_func ("/win32/subprocess/debuggee", test_veh_debuggee);
169 g_test_add_func ("/win32/subprocess/access_violation", test_access_violation);
170 g_test_add_func ("/win32/subprocess/illegal_instruction", test_illegal_instruction);
171
172 return g_test_run();
173 }
174