libc/winsup
David Allsopp 226f69422a Preserve order of dlopen'd modules in dll_list::topsort
This patch alters the behaviour of dll_list::topsort to preserve the
order of dlopen'd units.

The load order of unrelated DLLs is reversed every time fork is called,
since dll_list::topsort finds the tail of the list and then unwinds to
reinsert items. My change takes advantage of what should be undefined
behaviour in dll_list::populate_deps (ndeps non-zero and ndeps and deps
not initialised) to allow the deps field to be initialised prior to the
call and appended to, rather than overwritten.

All DLLs which have been dlopen'd have their deps list initialised with
the list of all previously dlopen'd units. These extra dependencies mean
that the unwind preserves the order of dlopen'd units.

The motivation for this is the FlexDLL linker used in OCaml. The FlexDLL
linker allows a dlopen'd unit to refer to symbols in previously dlopen'd
units and it resolves these symbols in DllMain before anything else has
initialised (including the Cygwin DLL). This means that dependencies may
exist between dlopen'd units (which the OCaml runtime system
understands) but which Windows is unaware of. During fork, the
process-level table which FlexDLL uses to get the symbol table of each
DLL is copied over but because the load order of dlopen'd DLLs is
reversed, it is possible for FlexDLL to attempt to access memory in the
DLL before it has been loaded and hence it fails with an access
violation. Because the list is reversed on each call to fork, it means
that a subsequent call to fork puts the DLLs back into the correct
order, hence "even" invocations of fork work!

An interesting side-effect is that this only occurs if the DLLs load at
their preferred base address - if they have to be rebased, then FlexDLL
works because at the time that the dependent unit is loaded out of
order, there is still in memory the "dummy" DONT_RESOLVE_DLL_REFERENCES
version of the dependency which, as it happens, will contain the correct
symbol table in the data section. For my tests, this initially appeared
to be an x86-only problem, but that was only because the two DLLs on x64
should have been rebased.

Signed-off-by: David Allsopp <david.allsopp@metastack.com>
2017-02-28 16:12:03 +01:00
..
CVSChangeLogs.old winsup: Move all old CVS Changelogs into CVSChangeLogs.old dir 2016-03-19 13:59:52 +01:00
cygserver Switching the Cygwin DLL to LGPLv3+, dropping commercial buyout option 2016-06-23 10:09:17 +02:00
cygwin Preserve order of dlopen'd modules in dll_list::topsort 2017-02-28 16:12:03 +01:00
doc Improve wording on special characters 2017-02-14 09:46:56 +01:00
lsaauth Switching the Cygwin DLL to LGPLv3+, dropping commercial buyout option 2016-06-23 10:09:17 +02:00
testsuite Switching the Cygwin DLL to LGPLv3+, dropping commercial buyout option 2016-06-23 10:09:17 +02:00
utils Generate output with Unix line endings even from Mingw64 utils 2017-02-24 20:55:14 +01:00
CONTRIBUTORS Preserve order of dlopen'd modules in dll_list::topsort 2017-02-28 16:12:03 +01:00
COPYING * COPYING: Replace with the GPLv3 definition. 2011-04-19 16:52:51 +00:00
COPYING.LIB Switching the Cygwin DLL to LGPLv3+, dropping commercial buyout option 2016-06-23 10:09:17 +02:00
CYGWIN_LICENSE Clarify source file license in CYGWIN_LICENSE 2016-06-23 21:52:12 +02:00
Makefile.common Switching the Cygwin DLL to LGPLv3+, dropping commercial buyout option 2016-06-23 10:09:17 +02:00
Makefile.in Switching the Cygwin DLL to LGPLv3+, dropping commercial buyout option 2016-06-23 10:09:17 +02:00
README Fix typo and wrong URL in winsup/README 2016-05-24 10:25:18 +02:00
acinclude.m4 ChangeLog: 2012-11-23 13:22:47 +00:00
aclocal.m4 winsup/ChangeLog: 2013-11-07 00:19:52 +00:00
autogen.sh ChangeLog: 2012-11-23 13:22:47 +00:00
c++wrap ChangeLog: 2012-11-23 13:22:47 +00:00
ccwrap ccwrap: fix build with non-english locale set (v2) 2016-02-29 14:44:23 +01:00
config.guess ChangeLog: 2012-11-23 13:22:47 +00:00
config.sub ChangeLog: 2012-11-23 13:22:47 +00:00
configure Rename without-mingw-progs to with-cross-bootstrap 2016-04-04 16:39:41 +02:00
configure.ac Switching the Cygwin DLL to LGPLv3+, dropping commercial buyout option 2016-06-23 10:09:17 +02:00
configure.cygwin winsup/ChangeLog: 2013-11-07 00:19:52 +00:00
install-sh ChangeLog: 2012-11-23 13:22:47 +00:00

README

THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESSED OR
IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.

Cygwin documentation is available on the net at https://cygwin.com
You might especially be interested in

https://cygwin.com/faq/faq.html#faq.programming.building-cygwin