| 1 |
/** |
| 2 |
* Written in the D programming language. |
| 3 |
* Module initialization routines. |
| 4 |
* |
| 5 |
* Copyright: Copyright Digital Mars 2000 - 2013. |
| 6 |
* License: Distributed under the |
| 7 |
* $(LINK2 http://www.boost.org/LICENSE_1_0.txt, Boost Software License 1.0). |
| 8 |
* (See accompanying file LICENSE) |
| 9 |
* Authors: Walter Bright, Sean Kelly |
| 10 |
* Source: $(DRUNTIMESRC rt/_minfo.d) |
| 11 |
*/ |
| 12 |
|
| 13 |
module rt.minfo; |
| 14 |
|
| 15 |
import core.stdc.stdio : fprintf, stderr; |
| 16 |
import core.stdc.stdlib : free, malloc, realloc; |
| 17 |
import core.stdc.string : memcpy, memset; |
| 18 |
import rt.sections; |
| 19 |
|
| 20 |
debug (printModuleDependencies) import core.stdc.stdio : printf; |
| 21 |
|
| 22 |
enum |
| 23 |
{ |
| 24 |
MIctorstart = 0x1, // we've started constructing it |
| 25 |
MIctordone = 0x2, // finished construction |
| 26 |
MIstandalone = 0x4, // module ctor does not depend on other module |
| 27 |
// ctors being done first |
| 28 |
MItlsctor = 8, |
| 29 |
MItlsdtor = 0x10, |
| 30 |
MIctor = 0x20, |
| 31 |
MIdtor = 0x40, |
| 32 |
MIxgetMembers = 0x80, |
| 33 |
MIictor = 0x100, |
| 34 |
MIunitTest = 0x200, |
| 35 |
MIimportedModules = 0x400, |
| 36 |
MIlocalClasses = 0x800, |
| 37 |
MIname = 0x1000, |
| 38 |
} |
| 39 |
|
| 40 |
/***** |
| 41 |
* A ModuleGroup is an unordered collection of modules. |
| 42 |
* There is exactly one for: |
| 43 |
* 1. all statically linked in D modules, either directely or as shared libraries |
| 44 |
* 2. each call to rt_loadLibrary() |
| 45 |
*/ |
| 46 |
|
| 47 |
struct ModuleGroup |
| 48 |
{ |
| 49 |
this(immutable(ModuleInfo*)[] modules) nothrow @nogc |
| 50 |
{ |
| 51 |
_modules = modules; |
| 52 |
} |
| 53 |
|
| 54 |
@property immutable(ModuleInfo*)[] modules() const nothrow @nogc |
| 55 |
{ |
| 56 |
return _modules; |
| 57 |
} |
| 58 |
|
| 59 |
// this function initializes the bookeeping necessary to create the |
| 60 |
// cycle path, and then creates it. It is a precondition that src and |
| 61 |
// target modules are involved in a cycle. |
| 62 |
// |
| 63 |
// The return value is malloc'd using C, so it must be freed after use. |
| 64 |
private size_t[] genCyclePath(size_t srcidx, size_t targetidx, int[][] edges) nothrow |
| 65 |
{ |
| 66 |
import core.bitop : bt, btc, bts; |
| 67 |
|
| 68 |
// set up all the arrays. |
| 69 |
size_t[] cyclePath = (cast(size_t*)malloc(size_t.sizeof * _modules.length * 2))[0 .. _modules.length * 2]; |
| 70 |
size_t totalMods; |
| 71 |
int[] distance = (cast(int*)malloc(int.sizeof * _modules.length))[0 .. _modules.length]; |
| 72 |
scope(exit) |
| 73 |
.free(distance.ptr); |
| 74 |
|
| 75 |
// determine the shortest path between two modules. Uses dijkstra |
| 76 |
// without a priority queue. (we can be a bit slow here, in order to |
| 77 |
// get a better printout). |
| 78 |
void shortest(size_t start, size_t target) |
| 79 |
{ |
| 80 |
// initial setup |
| 81 |
distance[] = int.max; |
| 82 |
int curdist = 0; |
| 83 |
distance[start] = 0; |
| 84 |
while (true) |
| 85 |
{ |
| 86 |
bool done = true; |
| 87 |
foreach (i, x; distance) |
| 88 |
{ |
| 89 |
if (x == curdist) |
| 90 |
{ |
| 91 |
if (i == target) |
| 92 |
{ |
| 93 |
done = true; |
| 94 |
break; |
| 95 |
} |
| 96 |
foreach (n; edges[i]) |
| 97 |
{ |
| 98 |
if (distance[n] == int.max) |
| 99 |
{ |
| 100 |
distance[n] = curdist + 1; |
| 101 |
done = false; |
| 102 |
} |
| 103 |
} |
| 104 |
} |
| 105 |
} |
| 106 |
if (done) |
| 107 |
break; |
| 108 |
++curdist; |
| 109 |
} |
| 110 |
// it should be impossible to not get to target, this is just a |
| 111 |
// sanity check. Not an assert, because druntime is compiled in |
| 112 |
// release mode. |
| 113 |
if (distance[target] != curdist) |
| 114 |
{ |
| 115 |
assert(0, "internal error printing module cycle"); |
| 116 |
} |
| 117 |
|
| 118 |
// determine the path. This is tricky, because we have to |
| 119 |
// follow the edges in reverse to get back to the original. We |
| 120 |
// don't have a reverse mapping, so it takes a bit of looping. |
| 121 |
totalMods += curdist; |
| 122 |
auto subpath = cyclePath[totalMods - curdist .. totalMods]; |
| 123 |
while (true) |
| 124 |
{ |
| 125 |
--curdist; |
| 126 |
subpath[curdist] = target; |
| 127 |
if (curdist == 0) |
| 128 |
break; |
| 129 |
distloop: |
| 130 |
// search for next (previous) module in cycle. |
| 131 |
foreach (m, d; distance) |
| 132 |
{ |
| 133 |
if (d == curdist) |
| 134 |
{ |
| 135 |
// determine if m can reach target |
| 136 |
foreach (e; edges[m]) |
| 137 |
{ |
| 138 |
if (e == target) |
| 139 |
{ |
| 140 |
// recurse |
| 141 |
target = m; |
| 142 |
break distloop; |
| 143 |
} |
| 144 |
} |
| 145 |
} |
| 146 |
} |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
// first get to the target |
| 151 |
shortest(srcidx, targetidx); |
| 152 |
// now get back. |
| 153 |
shortest(targetidx, srcidx); |
| 154 |
|
| 155 |
return cyclePath[0 .. totalMods]; |
| 156 |
} |
| 157 |
|
| 158 |
/****************************** |
| 159 |
* Allocate and fill in _ctors[] and _tlsctors[]. |
| 160 |
* Modules are inserted into the arrays in the order in which the constructors |
| 161 |
* need to be run. |
| 162 |
* |
| 163 |
* Params: |
| 164 |
* cycleHandling - string indicating option for cycle handling |
| 165 |
* Throws: |
| 166 |
* Exception if it fails. |
| 167 |
*/ |
| 168 |
void sortCtors(string cycleHandling) nothrow |
| 169 |
{ |
| 170 |
import core.bitop : bts, btr, bt, BitRange; |
| 171 |
import core.internal.container.hashtab; |
| 172 |
|
| 173 |
enum OnCycle |
| 174 |
{ |
| 175 |
abort, |
| 176 |
print, |
| 177 |
ignore |
| 178 |
} |
| 179 |
|
| 180 |
auto onCycle = OnCycle.abort; |
| 181 |
|
| 182 |
switch (cycleHandling) with(OnCycle) |
| 183 |
{ |
| 184 |
case "deprecate": |
| 185 |
// Option deprecated in 2.101, remove in 2.111 |
| 186 |
fprintf(stderr, "`--DRT-oncycle=deprecate` is no longer supported, using `abort` instead\n"); |
| 187 |
break; |
| 188 |
case "abort": |
| 189 |
onCycle = abort; |
| 190 |
break; |
| 191 |
case "print": |
| 192 |
onCycle = print; |
| 193 |
break; |
| 194 |
case "ignore": |
| 195 |
onCycle = ignore; |
| 196 |
break; |
| 197 |
case "": |
| 198 |
// no option passed |
| 199 |
break; |
| 200 |
default: |
| 201 |
// invalid cycle handling option. |
| 202 |
assert(0, "DRT invalid cycle handling option: " ~ cycleHandling); |
| 203 |
} |
| 204 |
|
| 205 |
debug (printModuleDependencies) |
| 206 |
{ |
| 207 |
foreach (_m; _modules) |
| 208 |
{ |
| 209 |
printf("%s%s%s:", _m.name.ptr, (_m.flags & MIstandalone) |
| 210 |
? "+".ptr : "".ptr, (_m.flags & (MIctor | MIdtor)) ? "*".ptr : "".ptr); |
| 211 |
foreach (_i; _m.importedModules) |
| 212 |
printf(" %s", _i.name.ptr); |
| 213 |
printf("\n"); |
| 214 |
} |
| 215 |
} |
| 216 |
|
| 217 |
immutable uint len = cast(uint) _modules.length; |
| 218 |
if (!len) |
| 219 |
return; // nothing to do. |
| 220 |
|
| 221 |
// allocate some stack arrays that will be used throughout the process. |
| 222 |
immutable nwords = (len + 8 * size_t.sizeof - 1) / (8 * size_t.sizeof); |
| 223 |
immutable flagbytes = nwords * size_t.sizeof; |
| 224 |
auto ctorstart = cast(size_t*) malloc(flagbytes); // ctor/dtor seen |
| 225 |
auto ctordone = cast(size_t*) malloc(flagbytes); // ctor/dtor processed |
| 226 |
auto relevant = cast(size_t*) malloc(flagbytes); // has ctors/dtors |
| 227 |
scope (exit) |
| 228 |
{ |
| 229 |
.free(ctorstart); |
| 230 |
.free(ctordone); |
| 231 |
.free(relevant); |
| 232 |
} |
| 233 |
|
| 234 |
void clearFlags(size_t* flags) |
| 235 |
{ |
| 236 |
memset(flags, 0, flagbytes); |
| 237 |
} |
| 238 |
|
| 239 |
|
| 240 |
// build the edges between each module. We may need this for printing, |
| 241 |
// and also allows avoiding keeping a hash around for module lookups. |
| 242 |
int[][] edges = (cast(int[]*)malloc((int[]).sizeof * _modules.length))[0 .. _modules.length]; |
| 243 |
{ |
| 244 |
HashTab!(immutable(ModuleInfo)*, int) modIndexes; |
| 245 |
foreach (i, m; _modules) |
| 246 |
modIndexes[m] = cast(int) i; |
| 247 |
|
| 248 |
auto reachable = cast(size_t*) malloc(flagbytes); |
| 249 |
scope(exit) |
| 250 |
.free(reachable); |
| 251 |
|
| 252 |
foreach (i, m; _modules) |
| 253 |
{ |
| 254 |
// use bit array to prevent duplicates |
| 255 |
// https://issues.dlang.org/show_bug.cgi?id=16208 |
| 256 |
clearFlags(reachable); |
| 257 |
// preallocate enough space to store all the indexes |
| 258 |
int *edge = cast(int*)malloc(int.sizeof * _modules.length); |
| 259 |
size_t nEdges = 0; |
| 260 |
foreach (imp; m.importedModules) |
| 261 |
{ |
| 262 |
if (imp is m) // self-import |
| 263 |
continue; |
| 264 |
if (auto impidx = imp in modIndexes) |
| 265 |
{ |
| 266 |
if (!bts(reachable, *impidx)) |
| 267 |
edge[nEdges++] = *impidx; |
| 268 |
} |
| 269 |
} |
| 270 |
if (nEdges > 0) |
| 271 |
{ |
| 272 |
// trim space to what is needed |
| 273 |
edges[i] = (cast(int*)realloc(edge, int.sizeof * nEdges))[0 .. nEdges]; |
| 274 |
} |
| 275 |
else |
| 276 |
{ |
| 277 |
edges[i] = null; |
| 278 |
.free(edge); |
| 279 |
} |
| 280 |
} |
| 281 |
} |
| 282 |
|
| 283 |
// free all the edges after we are done |
| 284 |
scope(exit) |
| 285 |
{ |
| 286 |
foreach (e; edges) |
| 287 |
if (e.ptr) |
| 288 |
.free(e.ptr); |
| 289 |
.free(edges.ptr); |
| 290 |
} |
| 291 |
|
| 292 |
void buildCycleMessage(size_t sourceIdx, size_t cycleIdx, scope void delegate(string) nothrow sink) |
| 293 |
{ |
| 294 |
version (Windows) |
| 295 |
enum EOL = "\r\n"; |
| 296 |
else |
| 297 |
enum EOL = "\n"; |
| 298 |
|
| 299 |
sink("Cyclic dependency between module constructors/destructors of "); |
| 300 |
sink(_modules[sourceIdx].name); |
| 301 |
sink(" and "); |
| 302 |
sink(_modules[cycleIdx].name); |
| 303 |
sink(EOL); |
| 304 |
auto cyclePath = genCyclePath(sourceIdx, cycleIdx, edges); |
| 305 |
scope(exit) .free(cyclePath.ptr); |
| 306 |
|
| 307 |
sink(_modules[sourceIdx].name); |
| 308 |
sink("* ->" ~ EOL); |
| 309 |
foreach (x; cyclePath[0 .. $ - 1]) |
| 310 |
{ |
| 311 |
sink(_modules[x].name); |
| 312 |
sink(bt(relevant, x) ? "* ->" ~ EOL : " ->" ~ EOL); |
| 313 |
} |
| 314 |
sink(_modules[sourceIdx].name); |
| 315 |
sink("*" ~ EOL); |
| 316 |
} |
| 317 |
|
| 318 |
// find all the non-trivial dependencies (that is, dependencies that have a |
| 319 |
// ctor or dtor) of a given module. Doing this, we can 'skip over' the |
| 320 |
// trivial modules to get at the non-trivial ones. |
| 321 |
// |
| 322 |
// If a cycle is detected, returns the index of the module that completes the cycle. |
| 323 |
// Returns: true for success, false for a deprecated cycle error |
| 324 |
bool findDeps(size_t idx, size_t* reachable) nothrow |
| 325 |
{ |
| 326 |
static struct stackFrame |
| 327 |
{ |
| 328 |
size_t curMod; |
| 329 |
size_t curDep; |
| 330 |
} |
| 331 |
|
| 332 |
// initialize "stack" |
| 333 |
auto stack = cast(stackFrame*) malloc(stackFrame.sizeof * len); |
| 334 |
scope (exit) |
| 335 |
.free(stack); |
| 336 |
auto stacktop = stack + len; |
| 337 |
auto sp = stack; |
| 338 |
sp.curMod = cast(int) idx; |
| 339 |
sp.curDep = 0; |
| 340 |
|
| 341 |
// initialize reachable by flagging source module |
| 342 |
clearFlags(reachable); |
| 343 |
bts(reachable, idx); |
| 344 |
|
| 345 |
for (;;) |
| 346 |
{ |
| 347 |
auto m = _modules[sp.curMod]; |
| 348 |
if (sp.curDep >= edges[sp.curMod].length) |
| 349 |
{ |
| 350 |
// return |
| 351 |
if (sp == stack) // finished the algorithm |
| 352 |
break; |
| 353 |
--sp; |
| 354 |
} |
| 355 |
else |
| 356 |
{ |
| 357 |
auto midx = edges[sp.curMod][sp.curDep]; |
| 358 |
if (!bts(reachable, midx)) |
| 359 |
{ |
| 360 |
if (bt(relevant, midx)) |
| 361 |
{ |
| 362 |
// need to process this node, don't recurse. |
| 363 |
if (bt(ctorstart, midx)) |
| 364 |
{ |
| 365 |
// was already started, this is a cycle. |
| 366 |
final switch (onCycle) with(OnCycle) |
| 367 |
{ |
| 368 |
case abort: |
| 369 |
|
| 370 |
string errmsg = ""; |
| 371 |
buildCycleMessage(idx, midx, (string x) {errmsg ~= x;}); |
| 372 |
throw new Error(errmsg, __FILE__, __LINE__); |
| 373 |
case ignore: |
| 374 |
break; |
| 375 |
case print: |
| 376 |
// print the message |
| 377 |
buildCycleMessage(idx, midx, (string x) { |
| 378 |
fprintf(stderr, "%.*s", cast(int) x.length, x.ptr); |
| 379 |
}); |
| 380 |
// continue on as if this is correct. |
| 381 |
break; |
| 382 |
} |
| 383 |
} |
| 384 |
} |
| 385 |
else if (!bt(ctordone, midx)) |
| 386 |
{ |
| 387 |
// non-relevant, and hasn't been exhaustively processed, recurse. |
| 388 |
if (++sp >= stacktop) |
| 389 |
{ |
| 390 |
// stack overflow, this shouldn't happen. |
| 391 |
import core.internal.abort : abort; |
| 392 |
|
| 393 |
abort("stack overflow on dependency search"); |
| 394 |
} |
| 395 |
sp.curMod = midx; |
| 396 |
sp.curDep = 0; |
| 397 |
continue; |
| 398 |
} |
| 399 |
} |
| 400 |
} |
| 401 |
|
| 402 |
// next dependency |
| 403 |
++sp.curDep; |
| 404 |
} |
| 405 |
return true; // success |
| 406 |
} |
| 407 |
|
| 408 |
// The list of constructors that will be returned by the sorting. |
| 409 |
immutable(ModuleInfo)** ctors; |
| 410 |
// current element being inserted into ctors list. |
| 411 |
size_t ctoridx = 0; |
| 412 |
|
| 413 |
// This function will determine the order of construction/destruction and |
| 414 |
// check for cycles. If a cycle is found, the cycle path is transformed |
| 415 |
// into a string and thrown as an error. |
| 416 |
// |
| 417 |
// Each call into this function is given a module that has static |
| 418 |
// ctor/dtors that must be dealt with. It recurses only when it finds |
| 419 |
// dependencies that also have static ctor/dtors. |
| 420 |
// Returns: true for success, false for a deprecated cycle error |
| 421 |
bool processMod(size_t curidx) nothrow |
| 422 |
{ |
| 423 |
immutable ModuleInfo* current = _modules[curidx]; |
| 424 |
|
| 425 |
// First, determine what modules are reachable. |
| 426 |
auto reachable = cast(size_t*) malloc(flagbytes); |
| 427 |
scope (exit) |
| 428 |
.free(reachable); |
| 429 |
if (!findDeps(curidx, reachable)) |
| 430 |
return false; // deprecated cycle error |
| 431 |
|
| 432 |
// process the dependencies. First, we process all relevant ones |
| 433 |
bts(ctorstart, curidx); |
| 434 |
auto brange = BitRange(reachable, len); |
| 435 |
foreach (i; brange) |
| 436 |
{ |
| 437 |
// note, don't check for cycles here, because the config could have been set to ignore cycles. |
| 438 |
// however, don't recurse if there is one, so still check for started ctor. |
| 439 |
if (i != curidx && bt(relevant, i) && !bt(ctordone, i) && !bt(ctorstart, i)) |
| 440 |
{ |
| 441 |
if (!processMod(i)) |
| 442 |
return false; // deprecated cycle error |
| 443 |
} |
| 444 |
} |
| 445 |
|
| 446 |
// now mark this node, and all nodes reachable from this module as done. |
| 447 |
bts(ctordone, curidx); |
| 448 |
btr(ctorstart, curidx); |
| 449 |
foreach (i; brange) |
| 450 |
{ |
| 451 |
// Since relevant dependencies are already marked as done |
| 452 |
// from recursion above (or are going to be handled up the call |
| 453 |
// stack), no reason to check for relevance, that is a wasted |
| 454 |
// op. |
| 455 |
bts(ctordone, i); |
| 456 |
} |
| 457 |
|
| 458 |
// add this module to the construction order list |
| 459 |
ctors[ctoridx++] = current; |
| 460 |
return true; |
| 461 |
} |
| 462 |
|
| 463 |
// returns `false` if deprecated cycle error otherwise set `result`. |
| 464 |
bool doSort(size_t relevantFlags, ref immutable(ModuleInfo)*[] result) nothrow |
| 465 |
{ |
| 466 |
clearFlags(relevant); |
| 467 |
clearFlags(ctorstart); |
| 468 |
clearFlags(ctordone); |
| 469 |
|
| 470 |
// pre-allocate enough space to hold all modules. |
| 471 |
ctors = (cast(immutable(ModuleInfo)**).malloc(len * (void*).sizeof)); |
| 472 |
ctoridx = 0; |
| 473 |
foreach (idx, m; _modules) |
| 474 |
{ |
| 475 |
if (m.flags & relevantFlags) |
| 476 |
{ |
| 477 |
if (m.flags & MIstandalone) |
| 478 |
{ |
| 479 |
// can run at any time. Just run it first. |
| 480 |
ctors[ctoridx++] = m; |
| 481 |
} |
| 482 |
else |
| 483 |
{ |
| 484 |
bts(relevant, idx); |
| 485 |
} |
| 486 |
} |
| 487 |
} |
| 488 |
|
| 489 |
// now run the algorithm in the relevant ones |
| 490 |
foreach (idx; BitRange(relevant, len)) |
| 491 |
{ |
| 492 |
if (!bt(ctordone, idx)) |
| 493 |
{ |
| 494 |
if (!processMod(idx)) |
| 495 |
return false; |
| 496 |
} |
| 497 |
} |
| 498 |
|
| 499 |
if (ctoridx == 0) |
| 500 |
{ |
| 501 |
// no ctors in the list. |
| 502 |
.free(ctors); |
| 503 |
} |
| 504 |
else |
| 505 |
{ |
| 506 |
ctors = cast(immutable(ModuleInfo)**).realloc(ctors, ctoridx * (void*).sizeof); |
| 507 |
if (ctors is null) |
| 508 |
assert(0); |
| 509 |
result = ctors[0 .. ctoridx]; |
| 510 |
} |
| 511 |
return true; |
| 512 |
} |
| 513 |
|
| 514 |
// finally, do the sorting for both shared and tls ctors. If either returns false, |
| 515 |
// print the deprecation warning. |
| 516 |
if (!doSort(MIctor | MIdtor, _ctors) || |
| 517 |
!doSort(MItlsctor | MItlsdtor, _tlsctors)) |
| 518 |
{ |
| 519 |
// print a warning |
| 520 |
fprintf(stderr, "Deprecation 16211 warning:\n" |
| 521 |
~ "A cycle has been detected in your program that was undetected prior to DMD\n" |
| 522 |
~ "2.072. This program will continue, but will not operate when using DMD 2.074\n" |
| 523 |
~ "to compile. Use runtime option --DRT-oncycle=print to see the cycle details.\n"); |
| 524 |
|
| 525 |
} |
| 526 |
} |
| 527 |
|
| 528 |
/// ditto |
| 529 |
void sortCtors() |
| 530 |
{ |
| 531 |
import rt.config : rt_configOption; |
| 532 |
sortCtors(rt_configOption("oncycle")); |
| 533 |
} |
| 534 |
|
| 535 |
void runCtors() |
| 536 |
{ |
| 537 |
// run independent ctors |
| 538 |
runModuleFuncs!(m => m.ictor)(_modules); |
| 539 |
// sorted module ctors |
| 540 |
runModuleFuncs!(m => m.ctor)(_ctors); |
| 541 |
} |
| 542 |
|
| 543 |
void runTlsCtors() |
| 544 |
{ |
| 545 |
runModuleFuncs!(m => m.tlsctor)(_tlsctors); |
| 546 |
} |
| 547 |
|
| 548 |
void runTlsDtors() |
| 549 |
{ |
| 550 |
runModuleFuncsRev!(m => m.tlsdtor)(_tlsctors); |
| 551 |
} |
| 552 |
|
| 553 |
void runDtors() |
| 554 |
{ |
| 555 |
runModuleFuncsRev!(m => m.dtor)(_ctors); |
| 556 |
} |
| 557 |
|
| 558 |
void free() |
| 559 |
{ |
| 560 |
if (_ctors.ptr) |
| 561 |
.free(_ctors.ptr); |
| 562 |
_ctors = null; |
| 563 |
if (_tlsctors.ptr) |
| 564 |
.free(_tlsctors.ptr); |
| 565 |
_tlsctors = null; |
| 566 |
// _modules = null; // let the owner free it |
| 567 |
} |
| 568 |
|
| 569 |
private: |
| 570 |
immutable(ModuleInfo*)[] _modules; |
| 571 |
immutable(ModuleInfo)*[] _ctors; |
| 572 |
immutable(ModuleInfo)*[] _tlsctors; |
| 573 |
} |
| 574 |
|
| 575 |
|
| 576 |
/******************************************** |
| 577 |
* Iterate over all module infos. |
| 578 |
*/ |
| 579 |
|
| 580 |
int moduleinfos_apply(scope int delegate(immutable(ModuleInfo*)) dg) |
| 581 |
{ |
| 582 |
foreach (ref sg; SectionGroup) |
| 583 |
{ |
| 584 |
foreach (m; sg.modules) |
| 585 |
{ |
| 586 |
// TODO: Should null ModuleInfo be allowed? |
| 587 |
if (m !is null) |
| 588 |
{ |
| 589 |
if (auto res = dg(m)) |
| 590 |
return res; |
| 591 |
} |
| 592 |
} |
| 593 |
} |
| 594 |
return 0; |
| 595 |
} |
| 596 |
|
| 597 |
/******************************************** |
| 598 |
* Module constructor and destructor routines. |
| 599 |
*/ |
| 600 |
|
| 601 |
extern (C) |
| 602 |
{ |
| 603 |
void rt_moduleCtor() |
| 604 |
{ |
| 605 |
foreach (ref sg; SectionGroup) |
| 606 |
{ |
| 607 |
sg.moduleGroup.sortCtors(); |
| 608 |
sg.moduleGroup.runCtors(); |
| 609 |
} |
| 610 |
} |
| 611 |
|
| 612 |
void rt_moduleTlsCtor() |
| 613 |
{ |
| 614 |
foreach (ref sg; SectionGroup) |
| 615 |
{ |
| 616 |
sg.moduleGroup.runTlsCtors(); |
| 617 |
} |
| 618 |
} |
| 619 |
|
| 620 |
void rt_moduleTlsDtor() |
| 621 |
{ |
| 622 |
foreach_reverse (ref sg; SectionGroup) |
| 623 |
{ |
| 624 |
sg.moduleGroup.runTlsDtors(); |
| 625 |
} |
| 626 |
} |
| 627 |
|
| 628 |
void rt_moduleDtor() |
| 629 |
{ |
| 630 |
foreach_reverse (ref sg; SectionGroup) |
| 631 |
{ |
| 632 |
sg.moduleGroup.runDtors(); |
| 633 |
sg.moduleGroup.free(); |
| 634 |
} |
| 635 |
} |
| 636 |
|
| 637 |
version (Win32) |
| 638 |
{ |
| 639 |
// Alternate names for backwards compatibility with older DLL code |
| 640 |
void _moduleCtor() |
| 641 |
{ |
| 642 |
rt_moduleCtor(); |
| 643 |
} |
| 644 |
|
| 645 |
void _moduleDtor() |
| 646 |
{ |
| 647 |
rt_moduleDtor(); |
| 648 |
} |
| 649 |
|
| 650 |
void _moduleTlsCtor() |
| 651 |
{ |
| 652 |
rt_moduleTlsCtor(); |
| 653 |
} |
| 654 |
|
| 655 |
void _moduleTlsDtor() |
| 656 |
{ |
| 657 |
rt_moduleTlsDtor(); |
| 658 |
} |
| 659 |
} |
| 660 |
} |
| 661 |
|
| 662 |
/******************************************** |
| 663 |
*/ |
| 664 |
|
| 665 |
void runModuleFuncs(alias getfp)(const(immutable(ModuleInfo)*)[] modules) |
| 666 |
{ |
| 667 |
foreach (m; modules) |
| 668 |
{ |
| 669 |
if (auto fp = getfp(m)) |
| 670 |
(*fp)(); |
| 671 |
} |
| 672 |
} |
| 673 |
|
| 674 |
void runModuleFuncsRev(alias getfp)(const(immutable(ModuleInfo)*)[] modules) |
| 675 |
{ |
| 676 |
foreach_reverse (m; modules) |
| 677 |
{ |
| 678 |
if (auto fp = getfp(m)) |
| 679 |
(*fp)(); |
| 680 |
} |
| 681 |
} |
| 682 |
|
| 683 |
unittest |
| 684 |
{ |
| 685 |
static void assertThrown(T : Throwable, E)(lazy E expr, string msg) |
| 686 |
{ |
| 687 |
try |
| 688 |
expr; |
| 689 |
catch (T) |
| 690 |
return; |
| 691 |
assert(0, msg); |
| 692 |
} |
| 693 |
|
| 694 |
static void stub() |
| 695 |
{ |
| 696 |
} |
| 697 |
|
| 698 |
static struct UTModuleInfo |
| 699 |
{ |
| 700 |
this(uint flags) |
| 701 |
{ |
| 702 |
mi._flags = flags; |
| 703 |
} |
| 704 |
|
| 705 |
void setImports(immutable(ModuleInfo)*[] imports...) |
| 706 |
{ |
| 707 |
import core.bitop; |
| 708 |
assert(flags & MIimportedModules); |
| 709 |
|
| 710 |
immutable nfuncs = popcnt(flags & (MItlsctor|MItlsdtor|MIctor|MIdtor|MIictor)); |
| 711 |
immutable size = nfuncs * (void function()).sizeof + |
| 712 |
size_t.sizeof + imports.length * (ModuleInfo*).sizeof; |
| 713 |
assert(size <= pad.sizeof); |
| 714 |
|
| 715 |
pad[nfuncs] = imports.length; |
| 716 |
.memcpy(&pad[nfuncs+1], imports.ptr, imports.length * imports[0].sizeof); |
| 717 |
} |
| 718 |
|
| 719 |
immutable ModuleInfo mi; |
| 720 |
size_t[8] pad; |
| 721 |
alias mi this; |
| 722 |
} |
| 723 |
|
| 724 |
static UTModuleInfo mockMI(uint flags) |
| 725 |
{ |
| 726 |
auto mi = UTModuleInfo(flags | MIimportedModules); |
| 727 |
auto p = cast(void function()*)&mi.pad; |
| 728 |
if (flags & MItlsctor) *p++ = &stub; |
| 729 |
if (flags & MItlsdtor) *p++ = &stub; |
| 730 |
if (flags & MIctor) *p++ = &stub; |
| 731 |
if (flags & MIdtor) *p++ = &stub; |
| 732 |
if (flags & MIictor) *p++ = &stub; |
| 733 |
*cast(size_t*)p++ = 0; // number of imported modules |
| 734 |
assert(cast(void*)p <= &mi + 1); |
| 735 |
return mi; |
| 736 |
} |
| 737 |
|
| 738 |
static void checkExp2(string testname, bool shouldThrow, string oncycle, |
| 739 |
immutable(ModuleInfo*)[] modules, |
| 740 |
immutable(ModuleInfo*)[] dtors=null, |
| 741 |
immutable(ModuleInfo*)[] tlsdtors=null) |
| 742 |
{ |
| 743 |
auto mgroup = ModuleGroup(modules); |
| 744 |
mgroup.sortCtors(oncycle); |
| 745 |
|
| 746 |
// if we are expecting sort to throw, don't throw because of unexpected |
| 747 |
// success! |
| 748 |
if (!shouldThrow) |
| 749 |
{ |
| 750 |
foreach (m; mgroup._modules) |
| 751 |
assert(!(m.flags & (MIctorstart | MIctordone)), testname); |
| 752 |
assert(mgroup._ctors == dtors, testname); |
| 753 |
assert(mgroup._tlsctors == tlsdtors, testname); |
| 754 |
} |
| 755 |
} |
| 756 |
|
| 757 |
static void checkExp(string testname, bool shouldThrow, |
| 758 |
immutable(ModuleInfo*)[] modules, |
| 759 |
immutable(ModuleInfo*)[] dtors=null, |
| 760 |
immutable(ModuleInfo*)[] tlsdtors=null) |
| 761 |
{ |
| 762 |
checkExp2(testname, shouldThrow, "abort", modules, dtors, tlsdtors); |
| 763 |
} |
| 764 |
|
| 765 |
|
| 766 |
{ |
| 767 |
auto m0 = mockMI(0); |
| 768 |
auto m1 = mockMI(0); |
| 769 |
auto m2 = mockMI(0); |
| 770 |
checkExp("no ctors", false, [&m0.mi, &m1.mi, &m2.mi]); |
| 771 |
} |
| 772 |
|
| 773 |
{ |
| 774 |
auto m0 = mockMI(MIictor); |
| 775 |
auto m1 = mockMI(0); |
| 776 |
auto m2 = mockMI(MIictor); |
| 777 |
auto mgroup = ModuleGroup([&m0.mi, &m1.mi, &m2.mi]); |
| 778 |
checkExp("independent ctors", false, [&m0.mi, &m1.mi, &m2.mi]); |
| 779 |
} |
| 780 |
|
| 781 |
{ |
| 782 |
auto m0 = mockMI(MIstandalone | MIctor); |
| 783 |
auto m1 = mockMI(0); |
| 784 |
auto m2 = mockMI(0); |
| 785 |
auto mgroup = ModuleGroup([&m0.mi, &m1.mi, &m2.mi]); |
| 786 |
checkExp("standalone ctor", false, [&m0.mi, &m1.mi, &m2.mi], [&m0.mi]); |
| 787 |
} |
| 788 |
|
| 789 |
{ |
| 790 |
auto m0 = mockMI(MIstandalone | MIctor); |
| 791 |
auto m1 = mockMI(MIstandalone | MIctor); |
| 792 |
auto m2 = mockMI(0); |
| 793 |
m1.setImports(&m0.mi); |
| 794 |
checkExp("imported standalone => no dependency", false, |
| 795 |
[&m0.mi, &m1.mi, &m2.mi], [&m0.mi, &m1.mi]); |
| 796 |
} |
| 797 |
|
| 798 |
{ |
| 799 |
auto m0 = mockMI(MIstandalone | MIctor); |
| 800 |
auto m1 = mockMI(MIstandalone | MIctor); |
| 801 |
auto m2 = mockMI(0); |
| 802 |
m0.setImports(&m1.mi); |
| 803 |
checkExp("imported standalone => no dependency (2)", false, |
| 804 |
[&m0.mi, &m1.mi, &m2.mi], [&m0.mi, &m1.mi]); |
| 805 |
} |
| 806 |
|
| 807 |
{ |
| 808 |
auto m0 = mockMI(MIstandalone | MIctor); |
| 809 |
auto m1 = mockMI(MIstandalone | MIctor); |
| 810 |
auto m2 = mockMI(0); |
| 811 |
m0.setImports(&m1.mi); |
| 812 |
m1.setImports(&m0.mi); |
| 813 |
checkExp("standalone may have cycle", false, |
| 814 |
[&m0.mi, &m1.mi, &m2.mi], [&m0.mi, &m1.mi]); |
| 815 |
} |
| 816 |
|
| 817 |
{ |
| 818 |
auto m0 = mockMI(MIctor); |
| 819 |
auto m1 = mockMI(MIctor); |
| 820 |
auto m2 = mockMI(0); |
| 821 |
m1.setImports(&m0.mi); |
| 822 |
checkExp("imported ctor => ordered ctors", false, |
| 823 |
[&m0.mi, &m1.mi, &m2.mi], [&m0.mi, &m1.mi], []); |
| 824 |
} |
| 825 |
|
| 826 |
{ |
| 827 |
auto m0 = mockMI(MIctor); |
| 828 |
auto m1 = mockMI(MIctor); |
| 829 |
auto m2 = mockMI(0); |
| 830 |
m0.setImports(&m1.mi); |
| 831 |
checkExp("imported ctor => ordered ctors (2)", false, |
| 832 |
[&m0.mi, &m1.mi, &m2.mi], [&m1.mi, &m0.mi], []); |
| 833 |
} |
| 834 |
|
| 835 |
{ |
| 836 |
auto m0 = mockMI(MIctor); |
| 837 |
auto m1 = mockMI(MIctor); |
| 838 |
auto m2 = mockMI(0); |
| 839 |
m0.setImports(&m1.mi); |
| 840 |
m1.setImports(&m0.mi); |
| 841 |
assertThrown!Throwable(checkExp("", true, [&m0.mi, &m1.mi, &m2.mi]), |
| 842 |
"detects ctors cycles"); |
| 843 |
assertThrown!Throwable(checkExp2("", true, "deprecate", |
| 844 |
[&m0.mi, &m1.mi, &m2.mi]), |
| 845 |
"detects ctors cycles (dep)"); |
| 846 |
} |
| 847 |
|
| 848 |
{ |
| 849 |
auto m0 = mockMI(MIctor); |
| 850 |
auto m1 = mockMI(MIctor); |
| 851 |
auto m2 = mockMI(0); |
| 852 |
m0.setImports(&m2.mi); |
| 853 |
m1.setImports(&m2.mi); |
| 854 |
m2.setImports(&m0.mi, &m1.mi); |
| 855 |
assertThrown!Throwable(checkExp("", true, [&m0.mi, &m1.mi, &m2.mi]), |
| 856 |
"detects cycle with repeats"); |
| 857 |
} |
| 858 |
|
| 859 |
{ |
| 860 |
auto m0 = mockMI(MIctor); |
| 861 |
auto m1 = mockMI(MIctor); |
| 862 |
auto m2 = mockMI(MItlsctor); |
| 863 |
m0.setImports(&m1.mi, &m2.mi); |
| 864 |
checkExp("imported ctor/tlsctor => ordered ctors/tlsctors", false, |
| 865 |
[&m0.mi, &m1.mi, &m2.mi], [&m1.mi, &m0.mi], [&m2.mi]); |
| 866 |
} |
| 867 |
|
| 868 |
{ |
| 869 |
auto m0 = mockMI(MIctor | MItlsctor); |
| 870 |
auto m1 = mockMI(MIctor); |
| 871 |
auto m2 = mockMI(MItlsctor); |
| 872 |
m0.setImports(&m1.mi, &m2.mi); |
| 873 |
checkExp("imported ctor/tlsctor => ordered ctors/tlsctors (2)", false, |
| 874 |
[&m0.mi, &m1.mi, &m2.mi], [&m1.mi, &m0.mi], [&m2.mi, &m0.mi]); |
| 875 |
} |
| 876 |
|
| 877 |
{ |
| 878 |
auto m0 = mockMI(MIctor); |
| 879 |
auto m1 = mockMI(MIctor); |
| 880 |
auto m2 = mockMI(MItlsctor); |
| 881 |
m0.setImports(&m1.mi, &m2.mi); |
| 882 |
m2.setImports(&m0.mi); |
| 883 |
checkExp("no cycle between ctors/tlsctors", false, |
| 884 |
[&m0.mi, &m1.mi, &m2.mi], [&m1.mi, &m0.mi], [&m2.mi]); |
| 885 |
} |
| 886 |
|
| 887 |
{ |
| 888 |
auto m0 = mockMI(MItlsctor); |
| 889 |
auto m1 = mockMI(MIctor); |
| 890 |
auto m2 = mockMI(MItlsctor); |
| 891 |
m0.setImports(&m2.mi); |
| 892 |
m2.setImports(&m0.mi); |
| 893 |
assertThrown!Throwable(checkExp("", true, [&m0.mi, &m1.mi, &m2.mi]), |
| 894 |
"detects tlsctors cycle"); |
| 895 |
assertThrown!Throwable(checkExp2("", true, "deprecate", |
| 896 |
[&m0.mi, &m1.mi, &m2.mi]), |
| 897 |
"detects tlsctors cycle (dep)"); |
| 898 |
} |
| 899 |
|
| 900 |
{ |
| 901 |
auto m0 = mockMI(MItlsctor); |
| 902 |
auto m1 = mockMI(MIctor); |
| 903 |
auto m2 = mockMI(MItlsctor); |
| 904 |
m0.setImports(&m1.mi); |
| 905 |
m1.setImports(&m0.mi, &m2.mi); |
| 906 |
m2.setImports(&m1.mi); |
| 907 |
assertThrown!Throwable(checkExp("", true, [&m0.mi, &m1.mi, &m2.mi]), |
| 908 |
"detects tlsctors cycle with repeats"); |
| 909 |
} |
| 910 |
|
| 911 |
{ |
| 912 |
auto m0 = mockMI(MIctor); |
| 913 |
auto m1 = mockMI(MIstandalone | MIctor); |
| 914 |
auto m2 = mockMI(MIstandalone | MIctor); |
| 915 |
m0.setImports(&m1.mi); |
| 916 |
m1.setImports(&m2.mi); |
| 917 |
m2.setImports(&m0.mi); |
| 918 |
// NOTE: this is implementation dependent, sorted order shouldn't be tested. |
| 919 |
checkExp("closed ctors cycle", false, [&m0.mi, &m1.mi, &m2.mi], |
| 920 |
[&m1.mi, &m2.mi, &m0.mi]); |
| 921 |
//checkExp("closed ctors cycle", false, [&m0.mi, &m1.mi, &m2.mi], [&m0.mi, &m1.mi, &m2.mi]); |
| 922 |
} |
| 923 |
} |
| 924 |
|
| 925 |
version (CRuntime_Microsoft) |
| 926 |
{ |
| 927 |
// Dummy so Win32 code can still call it |
| 928 |
extern(C) void _minit() { } |
| 929 |
} |