This repository has been archived by the owner on Oct 12, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 426
/
deh2.d
488 lines (423 loc) · 15.8 KB
/
deh2.d
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
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
/**
* Written in the D programming language.
* Implementation of exception handling support routines for Posix and Win64.
*
* Copyright: Copyright Digital Mars 2000 - 2012.
* License: Distributed under the
* $(LINK2 http://www.boost.org/LICENSE_1_0.txt, Boost Software License 1.0).
* (See accompanying file LICENSE)
* Authors: Walter Bright, Sean Kelly
* Source: $(DRUNTIMESRC src/rt/_deh2.d)
*/
module rt.deh2;
version (Posix)
{
version = deh2;
}
else version (Win64)
{
version = deh2;
}
// Use deh.d for Win32
version (deh2)
{
//debug=PRINTF;
debug(PRINTF) import core.stdc.stdio : printf;
extern (C)
{
Throwable.TraceInfo _d_traceContext(void* ptr = null);
int _d_isbaseof(ClassInfo oc, ClassInfo c);
void _d_createTrace(Object*, void*);
}
alias int function() fp_t; // function pointer in ambient memory model
// DHandlerInfo table is generated by except_gentables() in eh.c
struct DHandlerInfo
{
uint offset; // offset from function address to start of guarded section
uint endoffset; // offset of end of guarded section
int prev_index; // previous table index
uint cioffset; // offset to DCatchInfo data from start of table (!=0 if try-catch)
size_t finally_offset; // offset to finally code to execute
// (!=0 if try-finally)
}
// Address of DHandlerTable, searched for by eh_finddata()
struct DHandlerTable
{
uint espoffset; // offset of ESP from EBP
uint retoffset; // offset from start of function to return code
size_t nhandlers; // dimension of handler_info[] (use size_t to set alignment of handler_info[])
DHandlerInfo handler_info[1];
}
struct DCatchBlock
{
ClassInfo type; // catch type
size_t bpoffset; // EBP offset of catch var
size_t codeoffset; // catch handler offset
}
// Create one of these for each try-catch
struct DCatchInfo
{
size_t ncatches; // number of catch blocks
DCatchBlock catch_block[1]; // data for each catch block
}
// One of these is generated for each function with try-catch or try-finally
struct FuncTable
{
void *fptr; // pointer to start of function
DHandlerTable *handlertable; // eh data for this function
uint fsize; // size of function in bytes
}
private
{
struct InFlight
{
InFlight* next;
void* addr;
Throwable t;
}
InFlight* __inflight = null;
}
void terminate()
{
asm
{
hlt ;
}
}
/*******************************************
* Given address that is inside a function,
* figure out which function it is in.
* Return DHandlerTable if there is one, NULL if not.
*/
immutable(FuncTable)* __eh_finddata(void *address)
{
import rt.sections;
foreach (ref sg; SectionGroup)
{
auto pstart = sg.ehTables.ptr;
auto pend = pstart + sg.ehTables.length;
if (auto ft = __eh_finddata(address, pstart, pend))
return ft;
}
return null;
}
immutable(FuncTable)* __eh_finddata(void *address, immutable(FuncTable)* pstart, immutable(FuncTable)* pend)
{
debug(PRINTF) printf("FuncTable.sizeof = %p\n", FuncTable.sizeof);
debug(PRINTF) printf("__eh_finddata(address = %p)\n", address);
debug(PRINTF) printf("_deh_beg = %p, _deh_end = %p\n", pstart, pend);
for (auto ft = pstart; 1; ft++)
{
Lagain:
if (ft >= pend)
break;
version (Win64)
{
/* The MS Linker has an inexplicable and erratic tendency to insert
* 8 zero bytes between sections generated from different .obj
* files. This kludge tries to skip over them.
*/
if (ft.fptr == null)
{
ft = cast(immutable(FuncTable)*)(cast(void**)ft + 1);
goto Lagain;
}
}
debug(PRINTF) printf(" ft = %p, fptr = %p, handlertable = %p, fsize = x%03x\n",
ft, ft.fptr, ft.handlertable, ft.fsize);
immutable(void)* fptr = ft.fptr;
version (Win64)
{
/* If linked with /DEBUG, the linker rewrites it so the function pointer points
* to a JMP to the actual code. The address will be in the actual code, so we
* need to follow the JMP.
*/
if ((cast(ubyte*)fptr)[0] == 0xE9)
{ // JMP target = RIP of next instruction + signed 32 bit displacement
fptr = fptr + 5 + *cast(int*)(fptr + 1);
}
}
if (fptr <= address &&
address < cast(void *)(cast(char *)fptr + ft.fsize))
{
debug(PRINTF) printf("\tfound handler table\n");
return ft;
}
}
debug(PRINTF) printf("\tnot found\n");
return null;
}
/******************************
* Given EBP, find return address to caller, and caller's EBP.
* Input:
* regbp Value of EBP for current function
* *pretaddr Return address
* Output:
* *pretaddr return address to caller
* Returns:
* caller's EBP
*/
size_t __eh_find_caller(size_t regbp, size_t *pretaddr)
{
size_t bp = *cast(size_t *)regbp;
if (bp) // if not end of call chain
{
// Perform sanity checks on new EBP.
// If it is screwed up, terminate() hopefully before we do more damage.
if (bp <= regbp)
// stack should grow to smaller values
terminate();
*pretaddr = *cast(size_t *)(regbp + size_t.sizeof);
}
return bp;
}
/***********************************
* Throw a D object.
*/
extern (C) void _d_throwc(Object *h)
{
size_t regebp;
debug(PRINTF)
{
printf("_d_throw(h = %p, &h = %p)\n", h, &h);
printf("\tvptr = %p\n", *cast(void **)h);
}
version (D_InlineAsm_X86)
asm
{
mov regebp,EBP ;
}
else version (D_InlineAsm_X86_64)
asm
{
mov regebp,RBP ;
}
else
static assert(0);
_d_createTrace(h, null);
//static uint abc;
//if (++abc == 2) *(char *)0=0;
//int count = 0;
while (1) // for each function on the stack
{
size_t retaddr;
regebp = __eh_find_caller(regebp,&retaddr);
if (!regebp)
{ // if end of call chain
debug(PRINTF) printf("end of call chain\n");
break;
}
debug(PRINTF) printf("found caller, EBP = %p, retaddr = %p\n", regebp, retaddr);
//if (++count == 12) *(char*)0=0;
auto func_table = __eh_finddata(cast(void *)retaddr); // find static data associated with function
auto handler_table = func_table ? func_table.handlertable : null;
if (!handler_table) // if no static data
{
debug(PRINTF) printf("no handler table\n");
continue;
}
auto funcoffset = cast(size_t)func_table.fptr;
version (Win64)
{
/* If linked with /DEBUG, the linker rewrites it so the function pointer points
* to a JMP to the actual code. The address will be in the actual code, so we
* need to follow the JMP.
*/
if ((cast(ubyte*)funcoffset)[0] == 0xE9)
{ // JMP target = RIP of next instruction + signed 32 bit displacement
funcoffset = funcoffset + 5 + *cast(int*)(funcoffset + 1);
}
}
auto spoff = handler_table.espoffset;
auto retoffset = handler_table.retoffset;
debug(PRINTF)
{
printf("retaddr = %p\n", retaddr);
printf("regebp=%p, funcoffset=%p, spoff=x%x, retoffset=x%x\n",
regebp,funcoffset,spoff,retoffset);
}
// Find start index for retaddr in static data
auto dim = handler_table.nhandlers;
debug(PRINTF)
{
printf("handler_info[%d]:\n", dim);
for (int i = 0; i < dim; i++)
{
auto phi = &handler_table.handler_info.ptr[i];
printf("\t[%d]: offset = x%04x, endoffset = x%04x, prev_index = %d, cioffset = x%04x, finally_offset = %x\n",
i, phi.offset, phi.endoffset, phi.prev_index, phi.cioffset, phi.finally_offset);
}
}
auto index = -1;
for (int i = 0; i < dim; i++)
{
auto phi = &handler_table.handler_info.ptr[i];
debug(PRINTF) printf("i = %d, phi.offset = %04x\n", i, funcoffset + phi.offset);
if (retaddr > funcoffset + phi.offset &&
retaddr <= funcoffset + phi.endoffset)
index = i;
}
debug(PRINTF) printf("index = %d\n", index);
if (dim)
{
auto phi = &handler_table.handler_info.ptr[index+1];
debug(PRINTF) printf("next finally_offset %p\n", phi.finally_offset);
auto prev = cast(InFlight*) &__inflight;
auto curr = prev.next;
if (curr !is null && curr.addr == cast(void*)(funcoffset + phi.finally_offset))
{
auto e = cast(Error)(cast(Throwable) h);
if (e !is null && (cast(Error) curr.t) is null)
{
debug(PRINTF) printf("new error %p bypassing inflight %p\n", h, curr.t);
e.bypassedException = curr.t;
prev.next = curr.next;
//h = cast(Object*) t;
}
else
{
debug(PRINTF) printf("replacing thrown %p with inflight %p\n", h, __inflight.t);
auto t = curr.t;
auto n = curr.t;
while (n.next)
n = n.next;
n.next = cast(Throwable) h;
prev.next = curr.next;
h = cast(Object*) t;
}
}
}
// walk through handler table, checking each handler
// with an index smaller than the current table_index
int prev_ndx;
for (auto ndx = index; ndx != -1; ndx = prev_ndx)
{
auto phi = &handler_table.handler_info.ptr[ndx];
prev_ndx = phi.prev_index;
if (phi.cioffset)
{
// this is a catch handler (no finally)
auto pci = cast(DCatchInfo *)(cast(char *)handler_table + phi.cioffset);
auto ncatches = pci.ncatches;
for (int i = 0; i < ncatches; i++)
{
auto ci = **cast(ClassInfo **)h;
auto pcb = &pci.catch_block.ptr[i];
if (_d_isbaseof(ci, pcb.type))
{
// Matched the catch type, so we've found the handler.
// Initialize catch variable
*cast(void **)(regebp + (pcb.bpoffset)) = h;
// Jump to catch block. Does not return.
{
size_t catch_esp;
fp_t catch_addr;
catch_addr = cast(fp_t)(funcoffset + pcb.codeoffset);
catch_esp = regebp - handler_table.espoffset - fp_t.sizeof;
version (D_InlineAsm_X86)
asm
{
mov EAX,catch_esp ;
mov ECX,catch_addr ;
mov [EAX],ECX ;
mov EBP,regebp ;
mov ESP,EAX ; // reset stack
ret ; // jump to catch block
}
else version (D_InlineAsm_X86_64)
asm
{
mov RAX,catch_esp ;
mov RCX,catch_esp ;
mov RCX,catch_addr ;
mov [RAX],RCX ;
mov RBP,regebp ;
mov RSP,RAX ; // reset stack
ret ; // jump to catch block
}
else
static assert(0);
}
}
}
}
else if (phi.finally_offset)
{
// Call finally block
// Note that it is unnecessary to adjust the ESP, as the finally block
// accesses all items on the stack as relative to EBP.
debug(PRINTF) printf("calling finally_offset %p\n", phi.finally_offset);
auto blockaddr = cast(void*)(funcoffset + phi.finally_offset);
InFlight inflight;
inflight.addr = blockaddr;
inflight.next = __inflight;
inflight.t = cast(Throwable) h;
__inflight = &inflight;
version (OSX)
{
version (D_InlineAsm_X86)
asm
{
sub ESP,4 ;
push EBX ;
mov EBX,blockaddr ;
push EBP ;
mov EBP,regebp ;
call EBX ;
pop EBP ;
pop EBX ;
add ESP,4 ;
}
else version (D_InlineAsm_X86_64)
asm
{
sub RSP,8 ;
push RBX ;
mov RBX,blockaddr ;
push RBP ;
mov RBP,regebp ;
call RBX ;
pop RBP ;
pop RBX ;
add RSP,8 ;
}
else
static assert(0);
}
else
{
version (D_InlineAsm_X86)
asm
{
push EBX ;
mov EBX,blockaddr ;
push EBP ;
mov EBP,regebp ;
call EBX ;
pop EBP ;
pop EBX ;
}
else version (D_InlineAsm_X86_64)
asm
{
sub RSP,8 ;
push RBX ;
mov RBX,blockaddr ;
push RBP ;
mov RBP,regebp ;
call RBX ;
pop RBP ;
pop RBX ;
add RSP,8 ;
}
else
static assert(0);
}
if (__inflight is &inflight)
__inflight = __inflight.next;
}
}
}
terminate();
}
}