102 Dalmatians: Puppies to the Rescue
Decompilation Source Reference | GitLab repository
Loading...
Searching...
No Matches
exceptions.c
Go to the documentation of this file.
1/* Recovered game implementation; original addresses identify provenance. */
8#include <intrin.h>
9#include <math.h>
10#include <stdarg.h>
11#include <string.h>
12
13// Original pcdogs.exe: 0x446ed8
14// Reads exception information from the inherited startup frame, saves the exception code, and delegates disposition to the CRT exception filter. This contextual filter requires the parent frame layout.
15int32_t CRT_StartupSEHFilter(void *establisher_frame)
16{
17 struct _EXCEPTION_POINTERS *pxcptinfoptrs = (struct _EXCEPTION_POINTERS *)(*(uint32_t *)((char *)establisher_frame - 0x14));
18 DWORD exception_code_local = pxcptinfoptrs->ExceptionRecord->ExceptionCode;
19
20 *(uint32_t *)((char *)establisher_frame - 0x68) = exception_code_local;
21
22 return CRT_XcptFilter(exception_code_local, pxcptinfoptrs);
23}
24
25// Original pcdogs.exe: 0x447de4
26// Continues exception searching unless an unwind is underway, in which case it records the supplied frame and reports a collided unwind.
27EXCEPTION_DISPOSITION __unwind_handler(struct _EXCEPTION_RECORD *exception_record, void *establisher_frame, struct _CONTEXT *context_record, void **dispatcher_context)
28{
29 (void)context_record;
30 if (!(exception_record->ExceptionFlags & (EXCEPTION_UNWINDING | EXCEPTION_EXIT_UNWIND)))
31 return ExceptionContinueSearch;
32
33 *dispatcher_context = establisher_frame;
34
35 return ExceptionCollidedUnwind;
36}
37
38// Original pcdogs.exe: 0x447ee9
39// Handles access violations encountered by the guarded read probe and otherwise continues exception searching. Exception information comes from the inherited parent frame.
40int32_t CRT_ProbeReadSEHFilter(void *establisher_frame)
41{
42 int32_t result = ***(uint32_t ***)((char *)establisher_frame - 0x14) == EXCEPTION_ACCESS_VIOLATION;
43
44 return result;
45}
46
47// Original pcdogs.exe: 0x449a5a
48// Maps supported Windows exceptions to CRT signal actions, updates exception state, and invokes an installed handler when required. The result tells the exception dispatcher whether to handle or continue the exception.
49int32_t CRT_XcptFilter(uint32_t xcptnum, struct _EXCEPTION_POINTERS *pxcptinfoptrs)
50{
51 struct CRT_ExceptionAction *exception_action = CRT_FindExceptionAction(xcptnum);
52
53 if (!exception_action)
54 return UnhandledExceptionFilter(pxcptinfoptrs);
55
56 uint32_t action = exception_action->action;
57
58 if (!action)
59 return UnhandledExceptionFilter(pxcptinfoptrs);
60
62 {
63 exception_action->action = CRT_SIGNAL_DEFAULT;
64
65 return EXCEPTION_EXECUTE_HANDLER;
66 }
67
69 return 0xffffffff;
70
71 struct _EXCEPTION_POINTERS *previous_exception_pointers = g_crtCurrentExceptionPointers;
72
73 g_crtCurrentExceptionPointers = pxcptinfoptrs;
74
75 int32_t signal_number = exception_action->signalNumber;
76
77 if (signal_number != CRT_SIGNAL_FLOATING_POINT)
78 {
79 exception_action->action = CRT_SIGNAL_DEFAULT;
80 ((void (*)(int))action)(signal_number);
81 g_crtCurrentExceptionPointers = previous_exception_pointers;
82
83 return 0xffffffff;
84 }
85
86 int32_t signal_action_start = g_crtExceptionCode1;
87 int32_t signal_action_end = g_crtExceptionCode2 + signal_action_start;
88
89 if (signal_action_start < signal_action_end)
90 {
91 int32_t actions_remaining = signal_action_end - signal_action_start;
92 uint32_t *action_cursor = &g_crtExceptionActions[signal_action_start].action;
93
94 do
95 {
96 *(uint32_t *)action_cursor = 0;
97 action_cursor = &action_cursor[3];
98 } while (--actions_remaining);
99 }
100
101 int32_t previous_floating_error = g_crtErrnoBackup;
102
103 switch (exception_action->exceptionCode)
104 {
105 case EXCEPTION_FLT_DENORMAL_OPERAND:
106 {
108
109 break;
110 }
111
112 case EXCEPTION_FLT_DIVIDE_BY_ZERO:
113 {
115
116 break;
117 }
118
119 case EXCEPTION_FLT_INEXACT_RESULT:
120 {
122
123 break;
124 }
125
126 case EXCEPTION_FLT_INVALID_OPERATION:
127 {
129
130 break;
131 }
132
133 case EXCEPTION_FLT_OVERFLOW:
134 {
136
137 break;
138 }
139
140 case EXCEPTION_FLT_STACK_CHECK:
141 {
143
144 break;
145 }
146
147 case EXCEPTION_FLT_UNDERFLOW:
148 {
150
151 break;
152 }
153 }
154
155 ((void (*)(int, int))action)(CRT_SIGNAL_FLOATING_POINT, g_crtErrnoBackup);
156 g_crtErrnoBackup = previous_floating_error;
157 g_crtCurrentExceptionPointers = previous_exception_pointers;
158
159 return 0xffffffff;
160}
161
162// Original pcdogs.exe: 0x449b9b
163// Finds the CRT exception-action table entry for an exception code and returns NULL when no entry matches.
164struct CRT_ExceptionAction *CRT_FindExceptionAction(uint32_t exception_code_local)
165{
166 uint32_t action_count = g_crtExceptionActionCount;
168
169 if (g_crtExceptionActions[0].exceptionCode != exception_code_local)
170 {
171 do
172 {
173 result = &result[1];
174
175 if (result >= &g_crtExceptionActions[action_count])
176 break;
177 } while (result->exceptionCode != exception_code_local);
178 }
179
180 if (result < &g_crtExceptionActions[action_count] && result->exceptionCode == exception_code_local)
181 return result;
182
183 return NULL;
184}
185
186// Original pcdogs.exe: 0x44a135
187// Unwinds to the registration and try level saved in a jump buffer using its saved frame pointer. The native entry consumes its stack argument on return.
188void __stdcall __seh_longjmp_unwind_4(struct CRT_JumpBuffer3 *jump_buffer)
189{
190 __local_unwind2(jump_buffer->registration, jump_buffer->tryLevel, jump_buffer->savedEbp);
191}
192
193// Original pcdogs.exe: 0x44b0ad
194// Accepts an allocation exception in the guarded string-classification operation. This is a contextual SEH filter rather than an independent public API.
196{
197 return EXCEPTION_EXECUTE_HANDLER;
198}
199
200// Original pcdogs.exe: 0x44be5e
201// Accepts an allocation exception while preparing the locale-mapping source buffer. This is a contextual SEH filter rather than an independent public API.
203{
204 return EXCEPTION_EXECUTE_HANDLER;
205}
206
207// Original pcdogs.exe: 0x44bf12
208// Accepts an allocation exception while preparing the locale-mapping result buffer. This is a contextual SEH filter rather than an independent public API.
210{
211 return EXCEPTION_EXECUTE_HANDLER;
212}
213
214// Original pcdogs.exe: 0x44cd00
215// Forwards the frame, target address, exception record, and return value to Windows exception unwinding.
216void __stdcall Exception_RtlUnwindThunk(void *target_frame, void *target_ip, struct _EXCEPTION_RECORD *exception_record, void *return_value)
217{
218 /* tailcall */
219 RtlUnwind(target_frame, target_ip, exception_record, return_value);
220}
221
222// Original pcdogs.exe: 0x447e6e
223// Reports whether the current local-unwind registration represents abnormal termination by inspecting the exception chain and saved try level.
225{
226 NT_TIB *fsbase = (NT_TIB *)__readfsdword(0x18);
227
228 struct CRT_LocalUnwindGuard *unwind_guard = (struct CRT_LocalUnwindGuard *)fsbase->ExceptionList;
229
230 return (unwind_guard->handler == __unwind_handler && unwind_guard->tryLevel == unwind_guard->registration->tryLevel);
231}
int32_t CRT_StartupSEHFilter(void *establisher_frame)
Definition exceptions.c:15
int32_t CRT_LCMapStringA_MappedAllocationExceptionFilter()
Definition exceptions.c:209
int32_t CRT_XcptFilter(uint32_t xcptnum, struct _EXCEPTION_POINTERS *pxcptinfoptrs)
Definition exceptions.c:49
int32_t CRT_LCMapStringA_SourceAllocationExceptionFilter()
Definition exceptions.c:202
void Exception_RtlUnwindThunk(void *target_frame, void *target_ip, struct _EXCEPTION_RECORD *exception_record, void *return_value)
Definition exceptions.c:216
int32_t CRT_GetStringTypeA_AllocaExceptionFilter()
Definition exceptions.c:195
struct CRT_ExceptionAction * CRT_FindExceptionAction(uint32_t exception_code_local)
Definition exceptions.c:164
EXCEPTION_DISPOSITION __unwind_handler(struct _EXCEPTION_RECORD *exception_record, void *establisher_frame, struct _CONTEXT *context_record, void **dispatcher_context)
Definition exceptions.c:27
int32_t CRT_ProbeReadSEHFilter(void *establisher_frame)
Definition exceptions.c:40
int32_t __abnormal_termination()
Definition exceptions.c:224
void __seh_longjmp_unwind_4(struct CRT_JumpBuffer3 *jump_buffer)
Definition exceptions.c:188
@ CRT_SIGNAL_DEFAULT
Definition constants.h:182
@ CRT_SIGNAL_IGNORE
Definition constants.h:183
@ CRT_SIGNAL_DIE
Definition constants.h:184
@ CRT_SIGNAL_FLOATING_POINT
Definition constants.h:189
@ CRT_FPE_INEXACT
Definition constants.h:199
@ CRT_FPE_ZERO_DIVIDE
Definition constants.h:196
@ CRT_FPE_DENORMAL
Definition constants.h:195
@ CRT_FPE_STACK_CHECK
Definition constants.h:200
@ CRT_FPE_UNDERFLOW
Definition constants.h:198
@ CRT_FPE_INVALID
Definition constants.h:194
@ CRT_FPE_OVERFLOW
Definition constants.h:197
EXCEPTION_DISPOSITION __unwind_handler(struct _EXCEPTION_RECORD *exception_record, void *establisher_frame, struct _CONTEXT *context_record, void **dispatcher_context)
Definition exceptions.c:27
void __local_unwind2(struct CRT_SEHRegistration3 *registration, int32_t stop_try_level, void *establisher_frame)
#define g_crtCurrentExceptionPointers
Definition globals.h:205
#define g_crtExceptionCode1
Definition globals.h:130
#define g_crtExceptionActionCount
Definition globals.h:136
#define g_crtExceptionActions
Definition globals.h:127
#define g_crtErrnoBackup
Definition globals.h:139
#define g_crtExceptionCode2
Definition globals.h:133
uint32_t action
Definition types.h:25
int32_t signalNumber
Definition types.h:24
uint32_t exceptionCode
Definition types.h:23
void * savedEbp
Definition types.h:126
struct CRT_SEHRegistration3 * registration
Definition types.h:132
int32_t tryLevel
Definition types.h:133
struct CRT_SEHRegistration3 * registration
Definition types.h:192
EXCEPTION_DISPOSITION(* handler)(struct _EXCEPTION_RECORD *, void *, struct _CONTEXT *, void **)
Definition types.h:189