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 424
/
runtime.d
661 lines (579 loc) · 21.1 KB
/
runtime.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
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
/**
* The runtime module exposes information specific to the D runtime code.
*
* Copyright: Copyright Sean Kelly 2005 - 2009.
* License: $(LINK2 http://www.boost.org/LICENSE_1_0.txt, Boost License 1.0)
* Authors: Sean Kelly
* Source: $(DRUNTIMESRC core/_runtime.d)
*/
/* Copyright Sean Kelly 2005 - 2009.
* Distributed under the Boost Software License, Version 1.0.
* (See accompanying file LICENSE or copy at
* http://www.boost.org/LICENSE_1_0.txt)
*/
module core.runtime;
version (Windows) import core.stdc.wchar_ : wchar_t;
/// C interface for Runtime.loadLibrary
extern (C) void* rt_loadLibrary(const char* name);
/// ditto
version (Windows) extern (C) void* rt_loadLibraryW(const wchar_t* name);
/// C interface for Runtime.unloadLibrary, returns 1/0 instead of bool
extern (C) int rt_unloadLibrary(void* ptr);
/// C interface for Runtime.initialize, returns 1/0 instead of bool
extern(C) int rt_init();
/// C interface for Runtime.terminate, returns 1/0 instead of bool
extern(C) int rt_term();
private
{
alias bool function() ModuleUnitTester;
alias bool function(Object) CollectHandler;
alias Throwable.TraceInfo function( void* ptr ) TraceHandler;
extern (C) void rt_setCollectHandler( CollectHandler h );
extern (C) CollectHandler rt_getCollectHandler();
extern (C) void rt_setTraceHandler( TraceHandler h );
extern (C) TraceHandler rt_getTraceHandler();
alias void delegate( Throwable ) ExceptionHandler;
extern (C) void _d_print_throwable(Throwable t);
extern (C) void* thread_stackBottom();
extern (C) string[] rt_args();
extern (C) CArgs rt_cArgs();
}
static this()
{
// NOTE: Some module ctors will run before this handler is set, so it's
// still possible the app could exit without a stack trace. If
// this becomes an issue, the handler could be set in C main
// before the module ctors are run.
Runtime.traceHandler = &defaultTraceHandler;
}
///////////////////////////////////////////////////////////////////////////////
// Runtime
///////////////////////////////////////////////////////////////////////////////
/**
* Stores the unprocessed arguments supplied when the
* process was started.
*/
struct CArgs
{
int argc; /// The argument count.
char** argv; /// The arguments as a C array of strings.
}
/**
* This struct encapsulates all functionality related to the underlying runtime
* module for the calling context.
*/
struct Runtime
{
/**
* Initializes the runtime. This call is to be used in instances where the
* standard program initialization process is not executed. This is most
* often in shared libraries or in libraries linked to a C program.
* If the runtime was already successfully initialized this returns true.
* Each call to initialize must be paired by a call to $(LREF terminate).
*
* Returns:
* true if initialization succeeded or false if initialization failed.
*/
static bool initialize()
{
return !!rt_init();
}
deprecated("Please use the overload of Runtime.initialize that takes no argument.")
static bool initialize(ExceptionHandler dg = null)
{
return !!rt_init();
}
/**
* Terminates the runtime. This call is to be used in instances where the
* standard program termination process will not be not executed. This is
* most often in shared libraries or in libraries linked to a C program.
* If the runtime was not successfully initialized the function returns false.
*
* Returns:
* true if termination succeeded or false if termination failed.
*/
static bool terminate()
{
return !!rt_term();
}
deprecated("Please use the overload of Runtime.terminate that takes no argument.")
static bool terminate(ExceptionHandler dg = null)
{
return !!rt_term();
}
/**
* Returns the arguments supplied when the process was started.
*
* Returns:
* The arguments supplied when this process was started.
*/
static @property string[] args()
{
return rt_args();
}
/**
* Returns the unprocessed C arguments supplied when the process was started.
* Use this when you need to supply argc and argv to C libraries.
*
* Returns:
* A $(LREF CArgs) struct with the arguments supplied when this process was started.
*
* Example:
* ---
* import core.runtime;
*
* // A C library function requiring char** arguments
* extern(C) void initLibFoo(int argc, char** argv);
*
* void main()
* {
* auto args = Runtime.cArgs;
* initLibFoo(args.argc, args.argv);
* }
* ---
*/
static @property CArgs cArgs()
{
return rt_cArgs();
}
/**
* Locates a dynamic library with the supplied library name and dynamically
* loads it into the caller's address space. If the library contains a D
* runtime it will be integrated with the current runtime.
*
* Params:
* name = The name of the dynamic library to load.
*
* Returns:
* A reference to the library or null on error.
*/
static void* loadLibrary()(in char[] name)
{
import core.stdc.stdlib : free, malloc;
version (Windows)
{
import core.sys.windows.windows;
if (name.length == 0) return null;
// Load a DLL at runtime
auto len = MultiByteToWideChar(
CP_UTF8, 0, name.ptr, cast(int)name.length, null, 0);
if (len == 0)
return null;
auto buf = cast(wchar_t*)malloc((len+1) * wchar_t.sizeof);
if (buf is null) return null;
scope (exit) free(buf);
len = MultiByteToWideChar(
CP_UTF8, 0, name.ptr, cast(int)name.length, buf, len);
if (len == 0)
return null;
buf[len] = '\0';
return rt_loadLibraryW(buf);
}
else version (Posix)
{
/* Need a 0-terminated C string for the dll name
*/
immutable len = name.length;
auto buf = cast(char*)malloc(len + 1);
if (!buf) return null;
scope (exit) free(buf);
buf[0 .. len] = name[];
buf[len] = 0;
return rt_loadLibrary(buf);
}
}
/**
* Unloads the dynamic library referenced by p. If this library contains a
* D runtime then any necessary finalization or cleanup of that runtime
* will be performed.
*
* Params:
* p = A reference to the library to unload.
*/
static bool unloadLibrary()(void* p)
{
return !!rt_unloadLibrary(p);
}
/**
* Overrides the default trace mechanism with a user-supplied version. A
* trace represents the context from which an exception was thrown, and the
* trace handler will be called when this occurs. The pointer supplied to
* this routine indicates the base address from which tracing should occur.
* If the supplied pointer is null then the trace routine should determine
* an appropriate calling context from which to begin the trace.
*
* Params:
* h = The new trace handler. Set to null to use the default handler.
*/
static @property void traceHandler( TraceHandler h )
{
rt_setTraceHandler( h );
}
/**
* Gets the current trace handler.
*
* Returns:
* The current trace handler or null if none has been set.
*/
static @property TraceHandler traceHandler()
{
return rt_getTraceHandler();
}
/**
* Overrides the default collect hander with a user-supplied version. This
* routine will be called for each resource object that is finalized in a
* non-deterministic manner--typically during a garbage collection cycle.
* If the supplied routine returns true then the object's dtor will called
* as normal, but if the routine returns false than the dtor will not be
* called. The default behavior is for all object dtors to be called.
*
* Params:
* h = The new collect handler. Set to null to use the default handler.
*/
static @property void collectHandler( CollectHandler h )
{
rt_setCollectHandler( h );
}
/**
* Gets the current collect handler.
*
* Returns:
* The current collect handler or null if none has been set.
*/
static @property CollectHandler collectHandler()
{
return rt_getCollectHandler();
}
/**
* Overrides the default module unit tester with a user-supplied version.
* This routine will be called once on program initialization. The return
* value of this routine indicates to the runtime whether the tests ran
* without error.
*
* Params:
* h = The new unit tester. Set to null to use the default unit tester.
*/
static @property void moduleUnitTester( ModuleUnitTester h )
{
sm_moduleUnitTester = h;
}
/**
* Gets the current module unit tester.
*
* Returns:
* The current module unit tester handler or null if none has been set.
*/
static @property ModuleUnitTester moduleUnitTester()
{
return sm_moduleUnitTester;
}
private:
// NOTE: This field will only ever be set in a static ctor and should
// never occur within any but the main thread, so it is safe to
// make it __gshared.
__gshared ModuleUnitTester sm_moduleUnitTester = null;
}
///////////////////////////////////////////////////////////////////////////////
// Overridable Callbacks
///////////////////////////////////////////////////////////////////////////////
/**
* This routine is called by the runtime to run module unit tests on startup.
* The user-supplied unit tester will be called if one has been supplied,
* otherwise all unit tests will be run in sequence.
*
* Returns:
* true if execution should continue after testing is complete and false if
* not. Default behavior is to return true.
*/
extern (C) bool runModuleUnitTests()
{
// backtrace
version( linux )
import core.sys.linux.execinfo;
else version( OSX )
import core.sys.osx.execinfo;
else version( FreeBSD )
import core.sys.freebsd.execinfo;
else version( Windows )
import core.sys.windows.stacktrace;
else version( Solaris )
import core.sys.solaris.execinfo;
static if( __traits( compiles, backtrace ) )
{
import core.sys.posix.signal; // segv handler
static extern (C) void unittestSegvHandler( int signum, siginfo_t* info, void* ptr ) nothrow
{
static enum MAXFRAMES = 128;
void*[MAXFRAMES] callstack;
int numframes;
numframes = backtrace( callstack.ptr, MAXFRAMES );
backtrace_symbols_fd( callstack.ptr, numframes, 2 );
}
sigaction_t action = void;
sigaction_t oldseg = void;
sigaction_t oldbus = void;
(cast(byte*) &action)[0 .. action.sizeof] = 0;
sigfillset( &action.sa_mask ); // block other signals
action.sa_flags = SA_SIGINFO | SA_RESETHAND;
action.sa_sigaction = &unittestSegvHandler;
sigaction( SIGSEGV, &action, &oldseg );
sigaction( SIGBUS, &action, &oldbus );
scope( exit )
{
sigaction( SIGSEGV, &oldseg, null );
sigaction( SIGBUS, &oldbus, null );
}
}
if( Runtime.sm_moduleUnitTester is null )
{
size_t failed = 0;
foreach( m; ModuleInfo )
{
if( m )
{
auto fp = m.unitTest;
if( fp )
{
try
{
fp();
}
catch( Throwable e )
{
_d_print_throwable(e);
failed++;
}
}
}
}
return failed == 0;
}
return Runtime.sm_moduleUnitTester();
}
///////////////////////////////////////////////////////////////////////////////
// Default Implementations
///////////////////////////////////////////////////////////////////////////////
/**
*
*/
Throwable.TraceInfo defaultTraceHandler( void* ptr = null )
{
// backtrace
version( linux )
import core.sys.linux.execinfo;
else version( OSX )
import core.sys.osx.execinfo;
else version( FreeBSD )
import core.sys.freebsd.execinfo;
else version( Windows )
import core.sys.windows.stacktrace;
else version( Solaris )
import core.sys.solaris.execinfo;
//printf("runtime.defaultTraceHandler()\n");
static if( __traits( compiles, backtrace ) )
{
import core.demangle;
import core.stdc.stdlib : free;
import core.stdc.string : strlen, memchr, memmove;
class DefaultTraceInfo : Throwable.TraceInfo
{
this()
{
numframes = 0; //backtrace( callstack, MAXFRAMES );
if (numframes < 2) // backtrace() failed, do it ourselves
{
static void** getBasePtr()
{
version( D_InlineAsm_X86 )
asm { naked; mov EAX, EBP; ret; }
else
version( D_InlineAsm_X86_64 )
asm { naked; mov RAX, RBP; ret; }
else
return null;
}
auto stackTop = getBasePtr();
auto stackBottom = cast(void**) thread_stackBottom();
void* dummy;
if( stackTop && &dummy < stackTop && stackTop < stackBottom )
{
auto stackPtr = stackTop;
for( numframes = 0; stackTop <= stackPtr &&
stackPtr < stackBottom &&
numframes < MAXFRAMES; )
{
callstack[numframes++] = *(stackPtr + 1);
stackPtr = cast(void**) *stackPtr;
}
}
}
}
override int opApply( scope int delegate(ref const(char[])) dg ) const
{
return opApply( (ref size_t, ref const(char[]) buf)
{
return dg( buf );
} );
}
override int opApply( scope int delegate(ref size_t, ref const(char[])) dg ) const
{
version( Posix )
{
// NOTE: The first 5 frames with the current implementation are
// inside core.runtime and the object code, so eliminate
// these for readability. The alternative would be to
// exclude the first N frames that are in a list of
// mangled function names.
static enum FIRSTFRAME = 5;
}
else
{
// NOTE: On Windows, the number of frames to exclude is based on
// whether the exception is user or system-generated, so
// it may be necessary to exclude a list of function names
// instead.
static enum FIRSTFRAME = 0;
}
int ret = 0;
const framelist = backtrace_symbols( callstack.ptr, numframes );
scope(exit) free(cast(void*) framelist);
for( int i = FIRSTFRAME; i < numframes; ++i )
{
char[4096] fixbuf;
auto buf = framelist[i][0 .. strlen(framelist[i])];
auto pos = cast(size_t)(i - FIRSTFRAME);
buf = fixline( buf, fixbuf );
ret = dg( pos, buf );
if( ret )
break;
}
return ret;
}
override string toString() const
{
string buf;
foreach( i, line; this )
buf ~= i ? "\n" ~ line : line;
return buf;
}
private:
int numframes;
static enum MAXFRAMES = 128;
void*[MAXFRAMES] callstack = void;
private:
const(char)[] fixline( const(char)[] buf, return ref char[4096] fixbuf ) const
{
size_t symBeg, symEnd;
version( OSX )
{
// format is:
// 1 module 0x00000000 D6module4funcAFZv + 0
for( size_t i = 0, n = 0; i < buf.length; i++ )
{
if( ' ' == buf[i] )
{
n++;
while( i < buf.length && ' ' == buf[i] )
i++;
if( 3 > n )
continue;
symBeg = i;
while( i < buf.length && ' ' != buf[i] )
i++;
symEnd = i;
break;
}
}
}
else version( linux )
{
// format is: module(_D6module4funcAFZv) [0x00000000]
// or: module(_D6module4funcAFZv+0x78) [0x00000000]
auto bptr = cast(char*) memchr( buf.ptr, '(', buf.length );
auto eptr = cast(char*) memchr( buf.ptr, ')', buf.length );
auto pptr = cast(char*) memchr( buf.ptr, '+', buf.length );
if (pptr && pptr < eptr)
eptr = pptr;
if( bptr++ && eptr )
{
symBeg = bptr - buf.ptr;
symEnd = eptr - buf.ptr;
}
}
else version( FreeBSD )
{
// format is: 0x00000000 <_D6module4funcAFZv+0x78> at module
auto bptr = cast(char*) memchr( buf.ptr, '<', buf.length );
auto eptr = cast(char*) memchr( buf.ptr, '+', buf.length );
if( bptr++ && eptr )
{
symBeg = bptr - buf.ptr;
symEnd = eptr - buf.ptr;
}
}
else version( Solaris )
{
// format is object'symbol+offset [pc]
auto bptr = cast(char*) memchr( buf.ptr, '\'', buf.length );
auto eptr = cast(char*) memchr( buf.ptr, '+', buf.length );
if( bptr++ && eptr )
{
symBeg = bptr - buf.ptr;
symEnd = eptr - buf.ptr;
}
}
else
{
// fallthrough
}
assert(symBeg < buf.length && symEnd < buf.length);
assert(symBeg <= symEnd);
enum min = (size_t a, size_t b) => a <= b ? a : b;
if (symBeg == symEnd || symBeg >= fixbuf.length)
{
immutable len = min(buf.length, fixbuf.length);
fixbuf[0 .. len] = buf[0 .. len];
return fixbuf[0 .. len];
}
else
{
fixbuf[0 .. symBeg] = buf[0 .. symBeg];
auto sym = demangle(buf[symBeg .. symEnd], fixbuf[symBeg .. $]);
if (sym.ptr !is fixbuf.ptr + symBeg)
{
// demangle reallocated the buffer, copy the symbol to fixbuf
immutable len = min(fixbuf.length - symBeg, sym.length);
memmove(fixbuf.ptr + symBeg, sym.ptr, len);
if (symBeg + len == fixbuf.length)
return fixbuf[];
}
immutable pos = symBeg + sym.length;
assert(pos < fixbuf.length);
immutable tail = buf.length - symEnd;
immutable len = min(fixbuf.length - pos, tail);
fixbuf[pos .. pos + len] = buf[symEnd .. symEnd + len];
return fixbuf[0 .. pos + len];
}
}
}
return new DefaultTraceInfo;
}
else static if( __traits( compiles, new StackTrace(0, null) ) )
{
version (Win64)
{
static enum FIRSTFRAME = 4;
}
else
{
static enum FIRSTFRAME = 0;
}
import core.sys.windows.windows : CONTEXT;
auto s = new StackTrace(FIRSTFRAME, cast(CONTEXT*)ptr);
return s;
}
else
{
return null;
}
}