2000-02-18 03:38:33 +08:00
|
|
|
/* dll_init.cc
|
|
|
|
|
|
|
|
This software is a copyrighted work licensed under the terms of the
|
|
|
|
Cygwin license. Please consult the file "CYGWIN_LICENSE" for
|
|
|
|
details. */
|
|
|
|
|
|
|
|
#include "winsup.h"
|
2000-08-22 11:58:47 +08:00
|
|
|
#include "cygerrno.h"
|
2000-09-08 10:56:55 +08:00
|
|
|
#include "perprocess.h"
|
2011-04-21 08:53:55 +08:00
|
|
|
#include "sync.h"
|
2017-03-03 00:26:53 +08:00
|
|
|
#include "shared_info.h"
|
2000-09-08 10:56:55 +08:00
|
|
|
#include "dll_init.h"
|
|
|
|
#include "environ.h"
|
2001-09-08 05:32:07 +08:00
|
|
|
#include "security.h"
|
2001-10-01 12:10:07 +08:00
|
|
|
#include "path.h"
|
* devices.cc: New file.
* devices.gperf: New file.
* devices.shilka: New file.
* cygwin-gperf: New file.
* cygwin-shilka: New file.
* fhandler_fifo.cc: New file.
* fhandler_nodevice.cc : New file. Reorganize headers so that path.h precedes
fhandler.h throughout. Remove device argument and unit arguments from fhandler
constructors throughout. Remove pc arguments to fhandler functions and use
internal pc element instead, throughout. Use dev element in pc throughout.
Use major/minor elements rather than units and device numbers previously in
fhandler class. Use correct methods for fhandler file names rather than
directly accessing file name variables, throughout.
* Makefile.in (DLL_OFILES): Add devices.o, fhandler_fifo.o
* dcrt0.cc (dll_crt0_1): Call device::init.
* devices.h: Renumber devices based on more Linux-like major/minor numbers.
Add more devices. Declare standard device storage.
(device): Declare struct.
* dir.cc (opendir): Use new 'build_fh_name' to construct a fhandler_* type.
* dtable.cc (dtable::get_debugger_info): Ditto.
(cygwin_attach_handle_to_fd): Ditto.
(dtable::release): Remove special FH_SOCKET case in favor of generic
"need_fixup_before" test.
(dtable::init_std_file_from_handle): Use either build_fh_dev or build_fh_name
to build standard fhandler.
(dtable::build_fh_name): Renamed from dtable::build_fhandler_from_name. Move
out of dtable class. Don't accept a path_conv argument. Just build it here
and pass it to:
(build_fh_pc): Renamed from dtable::build_fhandler. Move out of dtable class.
Use intrinsic device type in path_conv to create new fhandler.
(build_fh_dev): Renamed from dtable::build_fhandler. Move out of dtable class.
Simplify arguments to just take new 'device' type and a name. Just return
pointer to fhandler rather than trying to insert into dtable.
(dtable::dup_worker): Accommodate above build_fh name changes.
(dtable::find_fifo): New (currently broken) function.
(handle_to_fn): Use strechr for efficiency.
* dtable.h: Reflect above build_fh name changes and argument differences.
(fhandler_base *&operator []): Return self rather than copy of self.
* fhandler.cc (fhandler_base::operator =): Use pc element to set normalized
path.
(fhandler_base::set_name): Ditto.
(fhandler_base::raw_read): Use method to access name.
(fhandler_base::write): Correctly use get_output_handle rather than get_handle.
(handler_base::device_access_denied): New function.
(fhandler_base::open): Eliminate pc argument and use pc element of
fhandler_base throughout.
(fhandler_base::fstat): Detect if device is based in filesystem and use
fstat_fs to calculate stat, if so.
(fhandler_base::fhandler_base): Eliminate handling of file names and, instead,
just free appropriate component from pc.
(fhandler_base::opendir): Remove path_conv parameter.
* fhandler.h: Remove all device flags.
(fhandler_base::pc): New element.
(fhandler_base::set_name): Change argument to path_conv.
(fhandler_base::error): New function.
(fhandler_base::exists): New function.
(fhandler_base::pc_binmode): New function.
(fhandler_base::dev): New function.
(fhandler_base::open_fs): New function.
(fhandler_base::fstat_fs): New function.
(fhandler_base::fstat_by_name): New function.
(fhandler_base::fstat_by_handle): New function.
(fhandler_base::isfifo): New function.
(fhandler_base::is_slow): New function.
(fhandler_base::is_auto_device): New function.
(fhandler_base::is_fs_special): New function.
(fhandler_base::device_access_denied): New function.
(fhandler_base::operator DWORD&): New operator.
(fhandler_base::get_name): Return normalized path from pc.
(fhandler_base::get_win32_name): Return windows path from pc.
(fhandler_base::isdevice): Renamed from is_device.
(fhandler_base::get_native_name): Return device format.
(fhandler_fifo): New class.
(fhandler_nodevice): New class.
(select_stuff::device_specific): Remove array.
(select_stuff::device_specific_pipe): New class element.
(select_stuff::device_specific_socket): New class element.
(select_stuff::device_specific_serial): New class element.
(select_stuff::select_stuff): Initialize new elements.
* fhandler_disk_file.cc (fhandler_base::fstat_by_handle): Move to base class
from fhandler_disk_file.
(fhandler_base::fstat_by_name): Ditto.
(fhandler_base::fstat_by_name): Ditto.
(fhandler_disk_file::open): Move most functionality into
fhandler_base::open_fs.
(fhandler_base::open_fs): New function.
(fhandler_disk_file::close): Move most functionality into
fhandler_base::close_fs.
(fhandler_base::close_fs): New function.
* fhandler_mem.cc (fhandler_dev_mem::open): Use device name in debugging
output.
* fhandler_socket.cc (fhandler_socket::set_connect_secret): Copy standard
urandom device into appropriate place.
(fhandler_socket::accept): Reflect change in fdsock return value.
* fhandler_tty.cc: See "throughouts" above.
* net.cc: Accommodate fdsock change throughout.
(fdsock): Return success or failure, accept fd argument and device argument.
* path.cc (symlink_info::major): New element.
(symlink_info::minor): New element.
(symlink_info::parse_device): Declare new function.
(fs_info::update): Accommodate changes in path_conv class.
(path_conv::fillin): Ditto.
(path_conv::return_and_clear_normalized_path): Eliminate.
(path_conv::set_normalized_path): New function.
(path_conv::path_conv): Set info in dev element. Use path_conv methods Check
for FH_FS rather than FH_BAD to indicate when to fill in filesystem stuff.
where appropriate rather than direct access. Use set_normalized_path to set
normalized path.
(windows_device_names): Eliminate.
(get_dev): Ditto.
(get_raw_device_number): Ditto.
(get_device_number): Ditto.
(win32_device_name): Call new device name parser to do most of the heavy
lifting.
(mount_info::conv_to_win32_path): Fill in dev field as appropriate.
(symlink_worker): Handle new device files.
(symlink_info::check): Ditto.
(symlink_info::parse_device): Define new function.
* path.h (executable_states): Move here from fhandler.h.
(fs_info): Rename variables to *_storage and create methods for accessing same.
(path_conv): Add dev element, remove devn and unit and adjust inline methods to
accommodate.
(set_normalized_path): Declare new function.
* pinfo.cc (_pinfo::commune_recv): Add broken support for handling fifos.
(_pinfo::commune_send): Ditto.
* pipe.cc (fhandler_pipe::close): check for existence of handle before closing
it.
(handler_pipe::create): Rename from make_pipe. Change arguments to accept
fhandler_pipe array. Accommodate fifos.
(pipe): Rework to deal with fhandler_pipe::create changes.
(_pipe): Ditto.
* select.cc: Use individual device_specific types throughout rather than
indexing with obsolete device number.
(set_bits): Use is_socket call rather than checking device number.
* shared_info.h (CURR_MOUNT_MAGIC): Update.
(conv_to_win32_path): Reflect addition of device argument.
* syscalls.cc (mknod_worker): New function.
(open): Use build_fh_name to build fhandler.
(chown_worker): Detect if this is an 'auto' device rather than an on-filesystem
device and handle appropriately.
(chmod_device): New function.
(chmod): Detect if this is an 'auto' device rather than an on-filesystem device
and handle appropriately. Use chmod_device to set mode of in-filesystem
devices.
(stat_worker): Eliminate path_conv argument. Call build_fh_name to construct
fhandler. Use fh->error() rather than pc->error to detect errors in fhandler
construction.
(access_worker): New function pulled from access. Accommodate in-filesystem
devices.
(access): Use access_worker.
(fpathconf): Detect if this is an 'auto' device rather than an on-filesystem
device and handle appropriately.
(mknod_worker): New function.
(mknod32): New function.
(chroot): Free normalized path -- assuming it was actually cmalloced.
* tty.cc (create_tty_master): Tweak for new device class.
(tty::common_init): Ditto.
* winsup.h (stat_worker): Remove.
(symlink_worker): Declare.
* exceptions.cc (set_process_mask): Just call sig_dispatch_pending and don't
worry about pending_signals since sig_dispatch_pending should always do the
right thing now.
(sig_handle): Reorganize SIGCONT handling to more closely conform to SUSv3.
* pinfo.h: Move __SIG enum to sigproc.h.
(PICOM_FIFO): New enum element.
(_pinfo): Remove 'thread2signal' stuff throughout class.
(_pinfo::commune_send): Make varargs.
(_pinfo::sigtodo): Eliminate.
(_pinfo::thread2signal): Ditto.
* signal.cc (kill_worker): Eliminate call to setthread2signal.
* sigproc.cc (local_sigtodo): Eliminate.
(getlocal_sigtodo): Ditto.
(sigelem): New class.
(pending_signals): New class.
(sigqueue): New variable, start of sigqueue linked list.
(sigcatch_nonmain): Eliminate.
(sigcatch_main): Eliminate.
(sigcatch_nosync): Eliminate.
(sigcomplete_nonmain): Eliminate.
(pending_signals): Eliminate.
(sig_clear): Call signal thread to clear pending signals, unless already in
signal thread.
(sigpending): Call signal thread to get pending signals.
(sig_dispatch_pending): Eliminate use of pending_signals and just check
sigqueue.
(sigproc_terminate): Eliminate all of the obsolete semaphore stuff. Close
signal pipe handle.
(sig_send): Eliminate all of the obsolete semaphore stuff and use pipe to send
signals.
(getevent): Eliminate.
(pending_signals::add): New function.
(pending_signals::del): New function.
(pending_signals::next): New function.
(wait_sig): Eliminate all of the obsolete semaphore stuff. Use pipe to
communicate and maintain a linked list of signals.
* sigproc.h: Move __SIG defines here. Add __SIGPENDING.
(sig_dispatch_pending): Remove "C" specifier.
(sig_handle): Accept a mask argument.
* thread.cc: Remove signal handling considerations throughout.
2003-09-25 08:37:18 +08:00
|
|
|
#include "fhandler.h"
|
2001-09-08 05:32:07 +08:00
|
|
|
#include "dtable.h"
|
|
|
|
#include "cygheap.h"
|
2002-06-02 14:07:01 +08:00
|
|
|
#include "pinfo.h"
|
2011-05-29 02:17:09 +08:00
|
|
|
#include "child_info.h"
|
2006-03-09 07:55:14 +08:00
|
|
|
#include "cygtls.h"
|
2010-02-28 23:54:25 +08:00
|
|
|
#include "exception.h"
|
2008-08-13 16:25:44 +08:00
|
|
|
#include <wchar.h>
|
2010-01-30 02:34:09 +08:00
|
|
|
#include <sys/reent.h>
|
2010-05-04 00:40:33 +08:00
|
|
|
#include <assert.h>
|
2015-07-20 04:38:30 +08:00
|
|
|
#include <tls_pbuf.h>
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2022-06-07 00:00:45 +08:00
|
|
|
extern void check_sanity_and_sync (per_process *);
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2011-08-04 01:58:59 +08:00
|
|
|
#define fabort fork_info->abort
|
|
|
|
|
2009-06-08 11:53:40 +08:00
|
|
|
dll_list dlls;
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2016-12-07 18:58:25 +08:00
|
|
|
WCHAR NO_COPY dll_list::nt_max_path_buffer[NT_MAX_PATH];
|
|
|
|
|
2011-04-21 08:53:55 +08:00
|
|
|
muto dll_list::protect;
|
|
|
|
|
2005-04-15 01:34:03 +08:00
|
|
|
static bool dll_global_dtors_recorded;
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2016-12-07 18:58:25 +08:00
|
|
|
/* We need the in_load_after_fork flag so dll_dllcrt0_1 can decide at fork
|
|
|
|
time if this is a linked DLL or a dynamically loaded DLL. In either case,
|
2024-01-29 20:33:05 +08:00
|
|
|
both, cygwin_finished_initializing and __in_forkee are true, so they are not
|
2016-12-07 18:58:25 +08:00
|
|
|
sufficient to discern the situation. */
|
|
|
|
static bool NO_COPY in_load_after_fork;
|
|
|
|
|
|
|
|
/* Into ntbuf with ntbufsize, prints name prefixed with "\\??\\"
|
|
|
|
or "\\??\\UNC" as necessary to form the native NT path name.
|
|
|
|
Returns the end of the resulting string in ntbuf.
|
|
|
|
Supports using (a substring of) ntbuf as name argument. */
|
|
|
|
PWCHAR dll_list::form_ntname (PWCHAR ntbuf, size_t ntbufsize, PCWCHAR name)
|
|
|
|
{
|
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
/* avoid using path_conv here: cygheap might not be
|
|
|
|
initialized when started from non-cygwin process,
|
|
|
|
or still might be frozen in_forkee */
|
|
|
|
if (name[0] == L'\0' || ntbufsize < 8)
|
|
|
|
break;
|
|
|
|
if (name[1] == L':') /* short Win32 drive letter path name */
|
|
|
|
{
|
|
|
|
int winlen = min (ntbufsize - 5, wcslen (name));
|
|
|
|
if (ntbuf + 4 != name)
|
|
|
|
memmove (ntbuf + 4, name, sizeof (*ntbuf) * winlen);
|
|
|
|
wcsncpy (ntbuf, L"\\??\\", 4);
|
|
|
|
ntbuf += 4 + winlen;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (!wcsncmp (name, L"\\\\?\\", 4)) /* long Win32 path name */
|
|
|
|
{
|
|
|
|
int winlen = min (ntbufsize - 1, wcslen (name));
|
|
|
|
if (ntbuf != name)
|
|
|
|
memmove (ntbuf, name, sizeof (*ntbuf) * winlen);
|
|
|
|
ntbuf[1] = L'?';
|
|
|
|
ntbuf += winlen;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (!wcsncmp (name, L"\\\\", 2)) /* short Win32 UNC path name */
|
|
|
|
{
|
|
|
|
name += 1; /* skip first backslash */
|
|
|
|
int winlen = min (ntbufsize - 8, wcslen (name));
|
|
|
|
if (ntbuf + 7 != name)
|
|
|
|
memmove (ntbuf + 7, name, sizeof (*ntbuf) * winlen);
|
|
|
|
wcsncpy (ntbuf, L"\\??\\UNC", 7);
|
|
|
|
ntbuf += 7 + winlen;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (!wcsncmp (name, L"\\??\\", 4)) /* already a long NT path name */
|
|
|
|
{
|
|
|
|
int winlen = min (ntbufsize - 1, wcslen (name));
|
|
|
|
if (ntbuf != name)
|
|
|
|
memmove (ntbuf, name, sizeof (*ntbuf) * winlen);
|
|
|
|
ntbuf += winlen;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
system_printf ("WARNING: invalid path name '%W'", name);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (ntbufsize)
|
|
|
|
*ntbuf = L'\0';
|
|
|
|
return ntbuf;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Into shortbuf with shortbufsize, prints name with "\\??\\"
|
|
|
|
or "\\??\\UNC" prefix removed/modified as necessary to form
|
|
|
|
the short Win32 path name.
|
|
|
|
Returns the end of the resulting string in shortbuf.
|
|
|
|
Supports using (a substring of) shortbuf as name argument. */
|
|
|
|
PWCHAR
|
|
|
|
dll_list::form_shortname (PWCHAR shortbuf, size_t shortbufsize, PCWCHAR name)
|
|
|
|
{
|
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
/* avoid using path_conv here: cygheap might not be
|
|
|
|
initialized when started from non-cygwin process,
|
|
|
|
or still might be frozen in_forkee */
|
|
|
|
if (name[0] == L'\0' || shortbufsize < 2)
|
|
|
|
break;
|
|
|
|
if (name[0] == L'\\' &&
|
|
|
|
(name[1] == L'\\' || name[1] == L'?') &&
|
|
|
|
name[2] == L'?' &&
|
|
|
|
name[3] == L'\\') /* long Win32 or NT path name */
|
|
|
|
name += 4;
|
|
|
|
if (name[1] == L':') /* short Win32 drive letter path name */
|
|
|
|
{
|
|
|
|
int ntlen = min (shortbufsize - 1, wcslen (name));
|
|
|
|
if (shortbuf != name)
|
|
|
|
memmove (shortbuf, name, sizeof (*shortbuf) * ntlen);
|
|
|
|
shortbuf += ntlen;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (!wcsncmp (name, L"UNC\\", 4)) /* UNC path name */
|
|
|
|
{
|
|
|
|
name += 3; /* skip "UNC" */
|
|
|
|
int winlen = min (shortbufsize - 2, wcslen (name));
|
|
|
|
if (shortbuf + 1 != name)
|
|
|
|
memmove (shortbuf + 1, name, sizeof (*shortbuf) * winlen);
|
|
|
|
shortbuf[0] = L'\\';
|
|
|
|
shortbuf += 1 + winlen;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (!wcsncmp (name, L"\\\\", 2)) /* already a short Win32 UNC path name */
|
|
|
|
{
|
|
|
|
int winlen = min (shortbufsize - 1, wcslen (name));
|
|
|
|
if (shortbuf != name)
|
|
|
|
memmove (shortbuf, name, sizeof (*shortbuf) * winlen);
|
|
|
|
shortbuf += winlen;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
system_printf ("WARNING: invalid path name '%W'", name);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (shortbufsize)
|
|
|
|
*shortbuf = L'\0';
|
|
|
|
return shortbuf;
|
|
|
|
}
|
|
|
|
|
2000-07-15 10:48:11 +08:00
|
|
|
/* Run destructors for all DLLs on exit. */
|
2005-04-15 01:34:03 +08:00
|
|
|
void
|
2002-09-23 08:31:31 +08:00
|
|
|
dll_global_dtors ()
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
2011-05-05 00:02:45 +08:00
|
|
|
/* Don't attempt to call destructors if we're still in fork processing
|
|
|
|
since that likely means fork is failing and everything will not have been
|
|
|
|
set up. */
|
2024-01-29 20:33:05 +08:00
|
|
|
if (__in_forkee == FORKING)
|
2011-05-05 00:02:45 +08:00
|
|
|
return;
|
2005-04-15 01:34:03 +08:00
|
|
|
int recorded = dll_global_dtors_recorded;
|
|
|
|
dll_global_dtors_recorded = false;
|
2008-08-05 22:26:47 +08:00
|
|
|
if (recorded && dlls.start.next)
|
2008-08-04 23:55:32 +08:00
|
|
|
for (dll *d = dlls.end; d != &dlls.start; d = d->prev)
|
2009-08-22 05:32:06 +08:00
|
|
|
d->run_dtors ();
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
|
|
|
|
2000-07-15 10:48:11 +08:00
|
|
|
/* Run all constructors associated with a dll */
|
|
|
|
void
|
|
|
|
per_module::run_ctors ()
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
2000-07-15 10:48:11 +08:00
|
|
|
void (**pfunc)() = ctors;
|
2000-02-18 03:38:33 +08:00
|
|
|
|
|
|
|
/* Run ctors backwards, so skip the first entry and find how many
|
|
|
|
there are, then run them. */
|
|
|
|
|
|
|
|
if (pfunc)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
for (i = 1; pfunc[i]; i++);
|
|
|
|
|
2000-09-05 01:52:42 +08:00
|
|
|
for (int j = i - 1; j > 0; j--)
|
2000-02-18 03:38:33 +08:00
|
|
|
(pfunc[j]) ();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-07-15 10:48:11 +08:00
|
|
|
/* Run all destructors associated with a dll */
|
|
|
|
void
|
|
|
|
per_module::run_dtors ()
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
2000-07-15 10:48:11 +08:00
|
|
|
void (**pfunc)() = dtors;
|
2010-02-05 23:05:22 +08:00
|
|
|
while (*++pfunc)
|
|
|
|
(*pfunc) ();
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
|
|
|
|
2000-07-15 10:48:11 +08:00
|
|
|
/* Initialize an individual DLL */
|
|
|
|
int
|
|
|
|
dll::init ()
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
2000-07-15 10:48:11 +08:00
|
|
|
int ret = 1;
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2000-07-15 10:48:11 +08:00
|
|
|
/* Don't run constructors or the "main" if we've forked. */
|
2024-01-29 20:33:05 +08:00
|
|
|
if (__in_forkee != FORKING)
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
|
|
|
/* global contructors */
|
2000-07-15 10:48:11 +08:00
|
|
|
p.run_ctors ();
|
2000-02-18 03:38:33 +08:00
|
|
|
|
|
|
|
/* entry point of dll (use main of per_process with null args...) */
|
2000-07-15 10:48:11 +08:00
|
|
|
if (p.main)
|
2010-05-08 05:25:19 +08:00
|
|
|
ret = p.main (0, 0, 0);
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2012-03-04 21:50:12 +08:00
|
|
|
/* Look for a dll based on the full path.
|
|
|
|
|
|
|
|
CV, 2012-03-04: Per MSDN, If a DLL with the same module name is already
|
|
|
|
loaded in memory, the system uses the loaded DLL, no matter which directory
|
|
|
|
it is in. The system does not search for the DLL. See
|
|
|
|
http://msdn.microsoft.com/en-us/library/ms682586%28v=vs.85%29.aspx
|
|
|
|
|
|
|
|
On 2012-02-08 I interpreted "module name" as "basename". So the assumption
|
|
|
|
was that the Windows Loader does not load another DLL with the same basename,
|
|
|
|
if one such DLL is already loaded. Consequentially I changed the code so
|
|
|
|
that DLLs are only compared by basename.
|
|
|
|
|
|
|
|
This assumption was obviously wrong, as the perl dynaloader proves. It
|
|
|
|
loads multiple DLLs with the same basename into memory, just from different
|
|
|
|
locations. This mechanism is broken when only comparing basenames in the
|
|
|
|
below code.
|
|
|
|
|
|
|
|
However, the original problem reported on 2012-02-07 was a result of
|
|
|
|
a subtil difference between the paths returned by different calls to
|
|
|
|
GetModuleFileNameW: Sometimes the path is a plain DOS path, sometimes
|
|
|
|
it's preceeded by the long pathname prefix "\\?\".
|
|
|
|
|
|
|
|
So I reverted the original change from 2012-02-08 and only applied the
|
|
|
|
following fix: Check if the path is preceeded by a long pathname prefix,
|
|
|
|
and, if so, drop it forthwith so that subsequent full path comparisons
|
2012-03-05 18:27:44 +08:00
|
|
|
work as expected.
|
2012-08-17 07:34:45 +08:00
|
|
|
|
2012-03-05 18:27:44 +08:00
|
|
|
At least that was the original idea. In fact there are two case, linked
|
|
|
|
and runtime loaded DLLs, which have to be distinguished:
|
2012-08-17 07:34:45 +08:00
|
|
|
|
2012-03-05 18:27:44 +08:00
|
|
|
- Linked DLLs are loaded by only specifying the basename of the DLL and
|
|
|
|
searching it using the system DLL search order as given in the
|
|
|
|
aforementioned MSDN URL.
|
|
|
|
|
|
|
|
- Runtime loaded DLLs are specified with the full path since that's how
|
|
|
|
dlopen works.
|
|
|
|
|
|
|
|
In effect, we have to be careful not to mix linked and loaded DLLs.
|
|
|
|
For more info how this gets accomplished, see the comments at the start
|
|
|
|
of dll_list::alloc, as well as the comment preceeding the definition of
|
|
|
|
the in_load_after_fork bool later in the file. */
|
2000-07-15 10:48:11 +08:00
|
|
|
dll *
|
2016-12-07 18:58:25 +08:00
|
|
|
dll_list::operator[] (PCWCHAR ntname)
|
2012-03-04 21:50:12 +08:00
|
|
|
{
|
|
|
|
dll *d = &start;
|
|
|
|
while ((d = d->next) != NULL)
|
2016-12-07 18:58:25 +08:00
|
|
|
if (!wcscasecmp (ntname, d->ntname))
|
2012-03-04 21:50:12 +08:00
|
|
|
return d;
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Look for a dll based on the basename. */
|
|
|
|
dll *
|
2016-12-07 18:58:25 +08:00
|
|
|
dll_list::find_by_modname (PCWCHAR modname)
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
2000-07-15 10:48:11 +08:00
|
|
|
dll *d = &start;
|
|
|
|
while ((d = d->next) != NULL)
|
2012-02-09 22:41:21 +08:00
|
|
|
if (!wcscasecmp (modname, d->modname))
|
2000-07-15 10:48:11 +08:00
|
|
|
return d;
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2000-07-15 10:48:11 +08:00
|
|
|
return NULL;
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
|
|
|
|
forkables: On fork failure, retry with hardlinks.
To support in-cygwin package managers, the fork() implementation must
not rely on .exe and .dll files to stay in their original location, as
the package manager's job is to replace these files. Instead, when the
first fork try fails, and we have NTFS, we use hardlinks to the original
binaries in /var/run/cygfork/ to create the child process during the
second fork try, along the main.exe.local file to enable the "DotLocal
Dll Redirection" feature for the dlls.
The (probably few) users that need an update-safe fork manually have to
create the /var/run/cygfork/ directory for now, using:
mkdir --mode=a=rwxt /var/run/cygfork
* child_info.h: Bump CURR_CHILD_INFO_MAGIC.
(enum child_status): Add _CI_SILENTFAIL flag.
(struct child_info): Add silentfail setter and getter.
* winsup.h (child_copy): Add bool silentfail parameter.
* cygheap.cc: Pass silentfail parameter to child_copy.
* dcrt0.cc: Ditto.
* dll_init.h (struct dll): Define public inline method forkedntname.
(struct dll_list): Declare private method find_by_forkedntname.
* dll_init.cc (struct dll_list): Implement find_by_forkedntname.
(dll_list::alloc): Use find_by_forkedntname when in load after fork.
(dll_list::load_after_fork_impl): Load dlls using dll::forkedntname.
* fork.cc (frok::parent): Set silentfail child info flag. Pass
silentfail parameter to child_copy. Use forkedntname of
dlls.main_executable.
(fork): When first dofork run failed and did not use forkables,
run dofork again with_forkables set to true.
(child_copy): Use debug_printf if silentfail is true,
system_printf otherwise.
2016-12-07 18:58:28 +08:00
|
|
|
/* Look for a dll based on the ntname used
|
|
|
|
to dynamically reload in forked child. */
|
|
|
|
dll *
|
|
|
|
dll_list::find_by_forkedntname (PCWCHAR ntname)
|
|
|
|
{
|
|
|
|
dll *d = &start;
|
|
|
|
while ((d = d->next) != NULL)
|
|
|
|
if (!wcscasecmp (ntname, d->forkedntname ()))
|
|
|
|
return d;
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2001-10-21 11:38:41 +08:00
|
|
|
#define RETRIES 1000
|
2000-07-15 10:48:11 +08:00
|
|
|
|
2009-06-08 11:53:40 +08:00
|
|
|
/* Allocate space for a dll struct. */
|
2000-07-15 10:48:11 +08:00
|
|
|
dll *
|
|
|
|
dll_list::alloc (HINSTANCE h, per_process *p, dll_type type)
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
2015-07-20 04:38:30 +08:00
|
|
|
/* Called under loader lock conditions so this function can't be called
|
2016-12-07 18:58:25 +08:00
|
|
|
multiple times in parallel. The static buffer is safe. */
|
|
|
|
PWCHAR ntname = nt_max_path_buf ();
|
|
|
|
GetModuleFileNameW (h, ntname, NT_MAX_PATH);
|
|
|
|
PWCHAR modname = form_ntname (ntname, NT_MAX_PATH, ntname);
|
forkables: Create forkable hardlinks, yet unused.
In preparation to protect fork() against dll- and exe-updates, create
hardlinks to the main executable and each loaded dll in subdirectories
of /var/run/cygfork/, if that one exists on the NTFS file system.
The directory names consist of the user sid, the main executable's NTFS
IndexNumber, and the most recent LastWriteTime of all involved binaries
(dlls and main executable). Next to the main.exe hardlink we create the
empty file main.exe.local to enable dll redirection.
The name of the mutex to synchronize hardlink creation/cleanup also is
assembled from these directory names, to allow for synchronized cleanup
of even orphaned hardlink directories.
The hardlink to each dynamically loaded dll goes into another directory,
named using the NTFS IndexNumber of the dll's original directory.
* Makefile.in (DLL_OFILES): Add forkable.o.
* dll_init.h (struct dll): Declare member variables fbi, fii,
forkable_ntname. Declare methods nominate_forkable,
create_forkable.
(struct dll_list): Declare enum forkables_needs. Declare member
variables forkables_dirx_size, forkables_dirx_ntname,
forkables_mutex_name, forkables_mutex. Declare private methods
forkable_ntnamesize, prepare_forkables_nomination,
update_forkables_needs, update_forkables, create_forkables,
denominate_forkables, close_mutex, try_remove_forkables,
set_forkables_inheritance, request_forkables. Declare public
static methods ntopenfile, read_fii, read_fbi. Declare public
methods release_forkables, cleanup_forkables. Define public
inline method setup_forkables.
* dll_init.cc (dll_list::alloc): Allocate memory to hold the
name of the hardlink in struct dll member forkable_ntname.
Initialize struct dll members fbi, fii.
(dll_list::load_after_fork): Call release_forkables method.
* fork.cc: Rename public fork function to static dofork, add
with_forkables as bool pointer parameter. Add new fork function
calling dofork. (struct frok): Add bool pointer member
with_forkables, add as constructor parameter.
(frok::parent): Call dlls.setup_forkables before CreateProcessW,
dlls.release_forkables afterwards.
* pinfo.cc (pinfo::exit): Call dlls.cleanup_forkables.
* syscalls.cc (_unlink_nt): Rename public unlink_nt function to
static _unlink_nt, with 'shareable' as additional argument.
(unlink_nt): New, wrap _unlink_nt for original behaviour.
(unlink_nt_shareable): New, wrap _unlink_nt to keep a binary
file still loadable while removing one of its hardlinks.
* forkable.cc: New file.
Implement static functions mkdirs, rmdirs, rmdirs_synchronized,
stat_real_file_once, format_IndexNumber, rootname, sidname,
exename, lwtimename. Define static array forkable_nameparts.
(struct dll): Implement nominate_forkable, create_forkable.
(struct dll_list): Implement static methods ntopenfile,
read_fii, read_fbi. Implement forkable_ntnamesize,
2016-12-07 18:58:27 +08:00
|
|
|
DWORD ntnamelen = modname - ntname;
|
2016-12-07 18:58:25 +08:00
|
|
|
while (modname > ntname && *(modname - 1) != L'\\')
|
|
|
|
--modname;
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2011-04-21 08:53:55 +08:00
|
|
|
guard (true);
|
2012-03-05 00:47:45 +08:00
|
|
|
/* Already loaded? For linked DLLs, only compare the basenames. Linked
|
|
|
|
DLLs are loaded using just the basename and the default DLL search path.
|
2016-12-07 18:58:26 +08:00
|
|
|
The Windows loader picks up the first one it finds.
|
forkables: On fork failure, retry with hardlinks.
To support in-cygwin package managers, the fork() implementation must
not rely on .exe and .dll files to stay in their original location, as
the package manager's job is to replace these files. Instead, when the
first fork try fails, and we have NTFS, we use hardlinks to the original
binaries in /var/run/cygfork/ to create the child process during the
second fork try, along the main.exe.local file to enable the "DotLocal
Dll Redirection" feature for the dlls.
The (probably few) users that need an update-safe fork manually have to
create the /var/run/cygfork/ directory for now, using:
mkdir --mode=a=rwxt /var/run/cygfork
* child_info.h: Bump CURR_CHILD_INFO_MAGIC.
(enum child_status): Add _CI_SILENTFAIL flag.
(struct child_info): Add silentfail setter and getter.
* winsup.h (child_copy): Add bool silentfail parameter.
* cygheap.cc: Pass silentfail parameter to child_copy.
* dcrt0.cc: Ditto.
* dll_init.h (struct dll): Define public inline method forkedntname.
(struct dll_list): Declare private method find_by_forkedntname.
* dll_init.cc (struct dll_list): Implement find_by_forkedntname.
(dll_list::alloc): Use find_by_forkedntname when in load after fork.
(dll_list::load_after_fork_impl): Load dlls using dll::forkedntname.
* fork.cc (frok::parent): Set silentfail child info flag. Pass
silentfail parameter to child_copy. Use forkedntname of
dlls.main_executable.
(fork): When first dofork run failed and did not use forkables,
run dofork again with_forkables set to true.
(child_copy): Use debug_printf if silentfail is true,
system_printf otherwise.
2016-12-07 18:58:28 +08:00
|
|
|
This also applies to cygwin1.dll and the main-executable (DLL_SELF).
|
|
|
|
When in_load_after_fork, dynamically loaded dll's are reloaded
|
|
|
|
using their parent's forkable_ntname, if available. */
|
|
|
|
dll *d = (type != DLL_LOAD) ? dlls.find_by_modname (modname) :
|
|
|
|
in_load_after_fork ? dlls.find_by_forkedntname (ntname) : dlls[ntname];
|
2000-07-15 10:48:11 +08:00
|
|
|
if (d)
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
2017-03-21 21:30:24 +08:00
|
|
|
/* We only get here in the forkee. */
|
|
|
|
if (d->handle != h)
|
2012-02-09 01:35:02 +08:00
|
|
|
fabort ("%W: Loaded to different address: parent(%p) != child(%p)",
|
2016-12-07 18:58:25 +08:00
|
|
|
ntname, d->handle, h);
|
2012-03-05 00:47:45 +08:00
|
|
|
/* If this DLL has been linked against, and the full path differs, try
|
|
|
|
to sanity check if this is the same DLL, just in another path. */
|
2016-12-07 18:58:25 +08:00
|
|
|
else if (type == DLL_LINK && wcscasecmp (ntname, d->ntname)
|
2012-03-05 00:47:45 +08:00
|
|
|
&& (d->p.data_start != p->data_start
|
|
|
|
|| d->p.data_start != p->data_start
|
|
|
|
|| d->p.bss_start != p->bss_start
|
|
|
|
|| d->p.bss_end != p->bss_end
|
|
|
|
|| d->p.ctors != p->ctors
|
|
|
|
|| d->p.dtors != p->dtors))
|
2016-12-07 18:58:25 +08:00
|
|
|
fabort ("\nLoaded different DLL with same basename in forked child,\n"
|
2012-03-05 00:47:45 +08:00
|
|
|
"parent loaded: %W\n"
|
|
|
|
" child loaded: %W\n"
|
|
|
|
"The DLLs differ, so it's not safe to run the forked child.\n"
|
|
|
|
"Make sure to remove the offending DLL before trying again.",
|
2016-12-07 18:58:25 +08:00
|
|
|
d->ntname, ntname);
|
2011-05-02 23:28:35 +08:00
|
|
|
d->p = p;
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
2010-05-04 00:40:33 +08:00
|
|
|
else
|
|
|
|
{
|
forkables: Create forkable hardlinks, yet unused.
In preparation to protect fork() against dll- and exe-updates, create
hardlinks to the main executable and each loaded dll in subdirectories
of /var/run/cygfork/, if that one exists on the NTFS file system.
The directory names consist of the user sid, the main executable's NTFS
IndexNumber, and the most recent LastWriteTime of all involved binaries
(dlls and main executable). Next to the main.exe hardlink we create the
empty file main.exe.local to enable dll redirection.
The name of the mutex to synchronize hardlink creation/cleanup also is
assembled from these directory names, to allow for synchronized cleanup
of even orphaned hardlink directories.
The hardlink to each dynamically loaded dll goes into another directory,
named using the NTFS IndexNumber of the dll's original directory.
* Makefile.in (DLL_OFILES): Add forkable.o.
* dll_init.h (struct dll): Declare member variables fbi, fii,
forkable_ntname. Declare methods nominate_forkable,
create_forkable.
(struct dll_list): Declare enum forkables_needs. Declare member
variables forkables_dirx_size, forkables_dirx_ntname,
forkables_mutex_name, forkables_mutex. Declare private methods
forkable_ntnamesize, prepare_forkables_nomination,
update_forkables_needs, update_forkables, create_forkables,
denominate_forkables, close_mutex, try_remove_forkables,
set_forkables_inheritance, request_forkables. Declare public
static methods ntopenfile, read_fii, read_fbi. Declare public
methods release_forkables, cleanup_forkables. Define public
inline method setup_forkables.
* dll_init.cc (dll_list::alloc): Allocate memory to hold the
name of the hardlink in struct dll member forkable_ntname.
Initialize struct dll members fbi, fii.
(dll_list::load_after_fork): Call release_forkables method.
* fork.cc: Rename public fork function to static dofork, add
with_forkables as bool pointer parameter. Add new fork function
calling dofork. (struct frok): Add bool pointer member
with_forkables, add as constructor parameter.
(frok::parent): Call dlls.setup_forkables before CreateProcessW,
dlls.release_forkables afterwards.
* pinfo.cc (pinfo::exit): Call dlls.cleanup_forkables.
* syscalls.cc (_unlink_nt): Rename public unlink_nt function to
static _unlink_nt, with 'shareable' as additional argument.
(unlink_nt): New, wrap _unlink_nt for original behaviour.
(unlink_nt_shareable): New, wrap _unlink_nt to keep a binary
file still loadable while removing one of its hardlinks.
* forkable.cc: New file.
Implement static functions mkdirs, rmdirs, rmdirs_synchronized,
stat_real_file_once, format_IndexNumber, rootname, sidname,
exename, lwtimename. Define static array forkable_nameparts.
(struct dll): Implement nominate_forkable, create_forkable.
(struct dll_list): Implement static methods ntopenfile,
read_fii, read_fbi. Implement forkable_ntnamesize,
2016-12-07 18:58:27 +08:00
|
|
|
size_t forkntsize = forkable_ntnamesize (type, ntname, modname);
|
|
|
|
|
|
|
|
/* FIXME: Change this to new at some point. */
|
|
|
|
d = (dll *) cmalloc (HEAP_2_DLL, sizeof (*d)
|
|
|
|
+ ((ntnamelen + forkntsize) * sizeof (*ntname)));
|
|
|
|
|
2012-02-09 01:35:02 +08:00
|
|
|
/* Now we've allocated a block of information. Fill it in with the
|
|
|
|
supplied info about this DLL. */
|
2016-12-07 18:58:25 +08:00
|
|
|
wcscpy (d->ntname, ntname);
|
|
|
|
d->modname = d->ntname + (modname - ntname);
|
2010-05-04 00:40:33 +08:00
|
|
|
d->handle = h;
|
2017-03-21 21:30:24 +08:00
|
|
|
d->count = 0; /* Reference counting performed in dlopen/dlclose. */
|
2016-12-07 18:58:26 +08:00
|
|
|
/* DLL_SELF dtors (main-executable, cygwin1.dll) are run elsewhere */
|
|
|
|
d->has_dtors = type != DLL_SELF;
|
2010-05-04 00:40:33 +08:00
|
|
|
d->p = p;
|
2011-05-30 14:52:12 +08:00
|
|
|
d->ndeps = 0;
|
|
|
|
d->deps = NULL;
|
2011-05-29 04:55:34 +08:00
|
|
|
d->image_size = ((pefile*)h)->optional_hdr ()->SizeOfImage;
|
2011-05-31 00:09:29 +08:00
|
|
|
d->preferred_base = (void*) ((pefile*)h)->optional_hdr()->ImageBase;
|
2010-05-04 00:40:33 +08:00
|
|
|
d->type = type;
|
forkables: Create forkable hardlinks, yet unused.
In preparation to protect fork() against dll- and exe-updates, create
hardlinks to the main executable and each loaded dll in subdirectories
of /var/run/cygfork/, if that one exists on the NTFS file system.
The directory names consist of the user sid, the main executable's NTFS
IndexNumber, and the most recent LastWriteTime of all involved binaries
(dlls and main executable). Next to the main.exe hardlink we create the
empty file main.exe.local to enable dll redirection.
The name of the mutex to synchronize hardlink creation/cleanup also is
assembled from these directory names, to allow for synchronized cleanup
of even orphaned hardlink directories.
The hardlink to each dynamically loaded dll goes into another directory,
named using the NTFS IndexNumber of the dll's original directory.
* Makefile.in (DLL_OFILES): Add forkable.o.
* dll_init.h (struct dll): Declare member variables fbi, fii,
forkable_ntname. Declare methods nominate_forkable,
create_forkable.
(struct dll_list): Declare enum forkables_needs. Declare member
variables forkables_dirx_size, forkables_dirx_ntname,
forkables_mutex_name, forkables_mutex. Declare private methods
forkable_ntnamesize, prepare_forkables_nomination,
update_forkables_needs, update_forkables, create_forkables,
denominate_forkables, close_mutex, try_remove_forkables,
set_forkables_inheritance, request_forkables. Declare public
static methods ntopenfile, read_fii, read_fbi. Declare public
methods release_forkables, cleanup_forkables. Define public
inline method setup_forkables.
* dll_init.cc (dll_list::alloc): Allocate memory to hold the
name of the hardlink in struct dll member forkable_ntname.
Initialize struct dll members fbi, fii.
(dll_list::load_after_fork): Call release_forkables method.
* fork.cc: Rename public fork function to static dofork, add
with_forkables as bool pointer parameter. Add new fork function
calling dofork. (struct frok): Add bool pointer member
with_forkables, add as constructor parameter.
(frok::parent): Call dlls.setup_forkables before CreateProcessW,
dlls.release_forkables afterwards.
* pinfo.cc (pinfo::exit): Call dlls.cleanup_forkables.
* syscalls.cc (_unlink_nt): Rename public unlink_nt function to
static _unlink_nt, with 'shareable' as additional argument.
(unlink_nt): New, wrap _unlink_nt for original behaviour.
(unlink_nt_shareable): New, wrap _unlink_nt to keep a binary
file still loadable while removing one of its hardlinks.
* forkable.cc: New file.
Implement static functions mkdirs, rmdirs, rmdirs_synchronized,
stat_real_file_once, format_IndexNumber, rootname, sidname,
exename, lwtimename. Define static array forkable_nameparts.
(struct dll): Implement nominate_forkable, create_forkable.
(struct dll_list): Implement static methods ntopenfile,
read_fii, read_fbi. Implement forkable_ntnamesize,
2016-12-07 18:58:27 +08:00
|
|
|
d->fii.IndexNumber.QuadPart = -1LL;
|
|
|
|
if (!forkntsize)
|
|
|
|
d->forkable_ntname = NULL;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
d->forkable_ntname = d->ntname + ntnamelen + 1;
|
|
|
|
*d->forkable_ntname = L'\0';
|
|
|
|
}
|
2019-04-30 22:15:30 +08:00
|
|
|
if (forkables_supported ())
|
2019-05-03 22:14:14 +08:00
|
|
|
d->stat_real_file_once ();
|
2011-05-30 14:52:12 +08:00
|
|
|
append (d);
|
2010-05-04 00:40:33 +08:00
|
|
|
if (type == DLL_LOAD)
|
|
|
|
loaded_dlls++;
|
|
|
|
}
|
2011-04-21 08:53:55 +08:00
|
|
|
guard (false);
|
2000-07-15 10:48:11 +08:00
|
|
|
return d;
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
|
|
|
|
2011-05-30 14:52:12 +08:00
|
|
|
void
|
|
|
|
dll_list::append (dll* d)
|
|
|
|
{
|
|
|
|
if (end == NULL)
|
|
|
|
end = &start; /* Point to "end" of dll chain. */
|
|
|
|
end->next = d; /* Standard linked list stuff. */
|
|
|
|
d->next = NULL;
|
|
|
|
d->prev = end;
|
|
|
|
end = d;
|
|
|
|
}
|
|
|
|
|
|
|
|
void dll_list::populate_deps (dll* d)
|
|
|
|
{
|
2015-07-20 04:38:30 +08:00
|
|
|
tmp_pathbuf tp;
|
|
|
|
|
|
|
|
PWCHAR wmodname = tp.w_get ();
|
2011-05-30 14:52:12 +08:00
|
|
|
pefile* pef = (pefile*) d->handle;
|
|
|
|
PIMAGE_DATA_DIRECTORY dd = pef->idata_dir (IMAGE_DIRECTORY_ENTRY_IMPORT);
|
|
|
|
/* Annoyance: calling crealloc with a NULL pointer will use the
|
|
|
|
wrong heap and crash, so we have to replicate some code */
|
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 01:06:34 +08:00
|
|
|
long maxdeps;
|
|
|
|
if (!d->ndeps)
|
|
|
|
{
|
|
|
|
maxdeps = 4;
|
|
|
|
d->deps = (dll**) cmalloc (HEAP_2_DLL, maxdeps*sizeof (dll*));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
maxdeps = d->ndeps;
|
|
|
|
}
|
2011-05-30 14:52:12 +08:00
|
|
|
for (PIMAGE_IMPORT_DESCRIPTOR id=
|
|
|
|
(PIMAGE_IMPORT_DESCRIPTOR) pef->rva (dd->VirtualAddress);
|
|
|
|
dd->Size && id->Name;
|
|
|
|
id++)
|
|
|
|
{
|
|
|
|
char* modname = pef->rva (id->Name);
|
|
|
|
sys_mbstowcs (wmodname, NT_MAX_PATH, modname);
|
2012-03-04 21:50:12 +08:00
|
|
|
if (dll* dep = find_by_modname (wmodname))
|
2011-05-30 14:52:12 +08:00
|
|
|
{
|
|
|
|
if (d->ndeps >= maxdeps)
|
|
|
|
{
|
|
|
|
maxdeps = 2*(1+maxdeps);
|
|
|
|
d->deps = (dll**) crealloc (d->deps, maxdeps*sizeof (dll*));
|
|
|
|
}
|
|
|
|
d->deps[d->ndeps++] = dep;
|
|
|
|
}
|
|
|
|
}
|
2011-05-31 00:09:29 +08:00
|
|
|
|
2011-05-30 14:52:12 +08:00
|
|
|
/* add one to differentiate no deps from unknown */
|
|
|
|
d->ndeps++;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
dll_list::topsort ()
|
|
|
|
{
|
|
|
|
/* Anything to do? */
|
2013-05-28 03:31:04 +08:00
|
|
|
if (!end || end == &start)
|
2011-05-30 14:52:12 +08:00
|
|
|
return;
|
2011-05-31 00:09:29 +08:00
|
|
|
|
2011-05-30 14:52:12 +08:00
|
|
|
/* make sure we have all the deps available */
|
|
|
|
dll* d = &start;
|
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 01:06:34 +08:00
|
|
|
dll** dlopen_deps = NULL;
|
|
|
|
long maxdeps = 4;
|
|
|
|
long dlopen_ndeps = 0;
|
|
|
|
|
|
|
|
if (loaded_dlls > 0)
|
|
|
|
dlopen_deps = (dll**) cmalloc (HEAP_2_DLL, maxdeps*sizeof (dll*));
|
|
|
|
|
2011-05-30 14:52:12 +08:00
|
|
|
while ((d = d->next))
|
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 01:06:34 +08:00
|
|
|
{
|
|
|
|
if (!d->ndeps)
|
|
|
|
{
|
|
|
|
/* Ensure that all dlopen'd DLLs depend on previously dlopen'd DLLs.
|
|
|
|
This prevents topsort from reversing the order of dlopen'd DLLs on
|
|
|
|
calls to fork. */
|
|
|
|
if (d->type == DLL_LOAD)
|
|
|
|
{
|
|
|
|
/* Initialise d->deps with all previously dlopen'd DLLs. */
|
|
|
|
if (dlopen_ndeps)
|
|
|
|
{
|
|
|
|
d->ndeps = dlopen_ndeps;
|
|
|
|
d->deps = (dll**) cmalloc (HEAP_2_DLL,
|
|
|
|
dlopen_ndeps*sizeof (dll*));
|
|
|
|
memcpy (d->deps, dlopen_deps, dlopen_ndeps*sizeof (dll*));
|
|
|
|
}
|
|
|
|
/* Add this DLL to the list of previously dlopen'd DLLs. */
|
|
|
|
if (dlopen_ndeps >= maxdeps)
|
|
|
|
{
|
|
|
|
maxdeps = 2*(1+maxdeps);
|
|
|
|
dlopen_deps = (dll**) crealloc (dlopen_deps,
|
|
|
|
maxdeps*sizeof (dll*));
|
|
|
|
}
|
|
|
|
dlopen_deps[dlopen_ndeps++] = d;
|
|
|
|
}
|
|
|
|
populate_deps (d);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (loaded_dlls > 0)
|
|
|
|
cfree (dlopen_deps);
|
2011-05-31 00:09:29 +08:00
|
|
|
|
2011-05-30 14:52:12 +08:00
|
|
|
/* unlink head and tail pointers so the sort can rebuild the list */
|
|
|
|
d = start.next;
|
|
|
|
start.next = end = NULL;
|
|
|
|
topsort_visit (d, true);
|
|
|
|
|
|
|
|
/* clear node markings made by the sort */
|
|
|
|
d = &start;
|
|
|
|
while ((d = d->next))
|
|
|
|
{
|
2011-08-04 01:58:59 +08:00
|
|
|
#ifdef DEBUGGING
|
2012-02-09 22:41:21 +08:00
|
|
|
paranoid_printf ("%W", d->modname);
|
2011-08-04 01:58:59 +08:00
|
|
|
for (int i = 1; i < -d->ndeps; i++)
|
2012-02-09 22:41:21 +08:00
|
|
|
paranoid_printf ("-> %W", d->deps[i - 1]->modname);
|
2011-08-04 01:58:59 +08:00
|
|
|
#endif
|
2011-05-30 14:52:12 +08:00
|
|
|
|
|
|
|
/* It would be really nice to be able to keep this information
|
|
|
|
around for next time, but we don't have an easy way to
|
|
|
|
invalidate cached dependencies when a module unloads. */
|
|
|
|
d->ndeps = 0;
|
|
|
|
cfree (d->deps);
|
|
|
|
d->deps = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* A recursive in-place topological sort. The result is ordered so that
|
|
|
|
dependencies of a dll appear before it in the list.
|
|
|
|
|
|
|
|
NOTE: this algorithm is guaranteed to terminate with a "partial
|
|
|
|
order" of dlls but does not do anything smart about cycles: an
|
|
|
|
arbitrary dependent dll will necessarily appear first. Perhaps not
|
|
|
|
surprisingly, Windows ships several dlls containing dependency
|
|
|
|
cycles, including SspiCli/RPCRT4.dll and a lovely tangle involving
|
|
|
|
USP10/LPK/GDI32/USER32.dll). Fortunately, we don't care about
|
|
|
|
Windows DLLs here, and cygwin dlls should behave better */
|
|
|
|
void
|
|
|
|
dll_list::topsort_visit (dll* d, bool seek_tail)
|
|
|
|
{
|
|
|
|
/* Recurse to the end of the dll chain, then visit nodes as we
|
|
|
|
unwind. We do this because once we start visiting nodes we can no
|
|
|
|
longer trust any _next_ pointers.
|
|
|
|
|
|
|
|
We "mark" visited nodes (to avoid revisiting them) by negating
|
|
|
|
ndeps (undone once the sort completes). */
|
|
|
|
if (seek_tail && d->next)
|
|
|
|
topsort_visit (d->next, true);
|
2011-05-31 00:09:29 +08:00
|
|
|
|
2011-05-30 14:52:12 +08:00
|
|
|
if (d->ndeps > 0)
|
|
|
|
{
|
|
|
|
d->ndeps = -d->ndeps;
|
2011-08-04 01:58:59 +08:00
|
|
|
for (long i = 1; i < -d->ndeps; i++)
|
|
|
|
topsort_visit (d->deps[i - 1], false);
|
2011-05-30 14:52:12 +08:00
|
|
|
|
|
|
|
append (d);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-02-02 10:00:01 +08:00
|
|
|
dll *
|
|
|
|
dll_list::find (void *retaddr)
|
2010-01-30 02:34:09 +08:00
|
|
|
{
|
2010-02-02 10:00:01 +08:00
|
|
|
MEMORY_BASIC_INFORMATION m;
|
|
|
|
if (!VirtualQuery (retaddr, &m, sizeof m))
|
|
|
|
return NULL;
|
|
|
|
HMODULE h = (HMODULE) m.AllocationBase;
|
|
|
|
|
|
|
|
dll *d = &start;
|
|
|
|
while ((d = d->next))
|
2016-12-07 18:58:26 +08:00
|
|
|
if (d->type != DLL_SELF && d->handle == h)
|
2010-02-02 10:00:01 +08:00
|
|
|
break;
|
|
|
|
return d;
|
2010-01-30 02:34:09 +08:00
|
|
|
}
|
|
|
|
|
2000-07-15 10:48:11 +08:00
|
|
|
/* Detach a DLL from the chain. */
|
2000-02-18 03:38:33 +08:00
|
|
|
void
|
2002-11-14 12:29:39 +08:00
|
|
|
dll_list::detach (void *retaddr)
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
2010-02-02 10:00:01 +08:00
|
|
|
dll *d;
|
2011-05-05 03:19:46 +08:00
|
|
|
/* Don't attempt to call destructors if we're still in fork processing
|
|
|
|
since that likely means fork is failing and everything will not have been
|
|
|
|
set up. */
|
2024-01-29 20:33:05 +08:00
|
|
|
if (!myself || __in_forkee == FORKING)
|
2002-11-14 12:29:39 +08:00
|
|
|
return;
|
2011-04-21 08:53:55 +08:00
|
|
|
guard (true);
|
|
|
|
if ((d = find (retaddr)))
|
2010-02-02 10:00:01 +08:00
|
|
|
{
|
2017-03-21 21:30:24 +08:00
|
|
|
/* Ensure our exception handler is enabled for destructors */
|
|
|
|
exception protect;
|
|
|
|
/* Call finalize function if we are not already exiting */
|
|
|
|
if (!exit_state)
|
|
|
|
__cxa_finalize (d->handle);
|
|
|
|
d->run_dtors ();
|
|
|
|
d->prev->next = d->next;
|
|
|
|
if (d->next)
|
|
|
|
d->next->prev = d->prev;
|
|
|
|
if (d->type == DLL_LOAD)
|
|
|
|
loaded_dlls--;
|
|
|
|
if (end == d)
|
|
|
|
end = d->prev;
|
|
|
|
cfree (d);
|
2010-02-02 10:00:01 +08:00
|
|
|
}
|
2011-04-21 08:53:55 +08:00
|
|
|
guard (false);
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
|
|
|
|
2000-07-16 12:10:33 +08:00
|
|
|
/* Initialization for all linked DLLs, called by dll_crt0_1. */
|
2000-02-18 03:38:33 +08:00
|
|
|
void
|
2000-07-15 10:48:11 +08:00
|
|
|
dll_list::init ()
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
2016-12-07 18:58:26 +08:00
|
|
|
track_self ();
|
|
|
|
|
2000-07-15 10:48:11 +08:00
|
|
|
/* Walk the dll chain, initializing each dll */
|
|
|
|
dll *d = &start;
|
2008-08-05 22:26:47 +08:00
|
|
|
dll_global_dtors_recorded = d->next != NULL;
|
2000-07-15 10:48:11 +08:00
|
|
|
while ((d = d->next))
|
2016-12-07 18:58:26 +08:00
|
|
|
if (d->type != DLL_SELF) /* linked and early loaded dlls */
|
|
|
|
d->init ();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
dll_list::track_self ()
|
|
|
|
{
|
|
|
|
/* for cygwin1.dll and main-executable: maintain hardlinks only */
|
|
|
|
alloc (cygwin_hmodule, user_data, DLL_SELF);
|
|
|
|
main_executable = alloc (GetModuleHandle (NULL), user_data, DLL_SELF);
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#define A64K (64 * 1024)
|
|
|
|
|
|
|
|
|
2011-05-29 04:55:34 +08:00
|
|
|
/* Reserve the chunk of free address space starting _here_ and (usually)
|
|
|
|
covering at least _dll_size_ bytes. However, we must take care not
|
|
|
|
to clobber the dll's target address range because it often overlaps.
|
|
|
|
*/
|
2013-04-23 17:44:36 +08:00
|
|
|
static PVOID
|
2016-12-07 18:58:25 +08:00
|
|
|
reserve_at (PCWCHAR name, PVOID here, PVOID dll_base, DWORD dll_size)
|
2011-04-06 21:58:00 +08:00
|
|
|
{
|
|
|
|
DWORD size;
|
|
|
|
MEMORY_BASIC_INFORMATION mb;
|
|
|
|
|
2013-04-23 17:44:36 +08:00
|
|
|
if (!VirtualQuery (here, &mb, sizeof (mb)))
|
|
|
|
fabort ("couldn't examine memory at %p while mapping %W, %E", here, name);
|
2011-04-06 21:58:00 +08:00
|
|
|
if (mb.State != MEM_FREE)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
size = mb.RegionSize;
|
2011-05-31 00:09:29 +08:00
|
|
|
|
2011-05-29 04:55:34 +08:00
|
|
|
// don't clobber the space where we want the dll to land
|
2013-04-23 17:44:36 +08:00
|
|
|
caddr_t end = (caddr_t) here + size;
|
|
|
|
caddr_t dll_end = (caddr_t) dll_base + dll_size;
|
|
|
|
if (dll_base < here && dll_end > (caddr_t) here)
|
|
|
|
here = (PVOID) dll_end; // the dll straddles our left edge
|
|
|
|
else if (dll_base >= here && (caddr_t) dll_base < end)
|
|
|
|
end = (caddr_t) dll_base; // the dll overlaps partly or fully to our right
|
|
|
|
|
|
|
|
size = end - (caddr_t) here;
|
|
|
|
if (!VirtualAlloc (here, size, MEM_RESERVE, PAGE_NOACCESS))
|
2011-08-04 01:58:59 +08:00
|
|
|
fabort ("couldn't allocate memory %p(%d) for '%W' alignment, %E\n",
|
|
|
|
here, size, name);
|
2011-04-06 21:58:00 +08:00
|
|
|
return here;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Release the memory previously allocated by "reserve_at" above. */
|
|
|
|
static void
|
2016-12-07 18:58:25 +08:00
|
|
|
release_at (PCWCHAR name, PVOID here)
|
2011-04-06 21:58:00 +08:00
|
|
|
{
|
2013-04-23 17:44:36 +08:00
|
|
|
if (!VirtualFree (here, 0, MEM_RELEASE))
|
2011-08-04 01:58:59 +08:00
|
|
|
fabort ("couldn't release memory %p for '%W' alignment, %E\n",
|
|
|
|
here, name);
|
2011-04-06 21:58:00 +08:00
|
|
|
}
|
|
|
|
|
2011-05-31 00:09:29 +08:00
|
|
|
/* Step 1: Reserve memory for all DLL_LOAD dlls. This is to prevent
|
|
|
|
anything else from taking their spot as we compensate for Windows
|
|
|
|
randomly relocating things.
|
|
|
|
|
|
|
|
NOTE: because we can't depend on LoadLibraryExW to do the right
|
|
|
|
thing, we have to do a vanilla VirtualAlloc instead. One possible
|
|
|
|
optimization might attempt a LoadLibraryExW first, in case it lands
|
|
|
|
in the right place, but then we have to find a way of tracking
|
|
|
|
which dlls ended up needing VirtualAlloc after all. */
|
|
|
|
void
|
|
|
|
dll_list::reserve_space ()
|
|
|
|
{
|
|
|
|
for (dll* d = dlls.istart (DLL_LOAD); d; d = dlls.inext ())
|
|
|
|
if (!VirtualAlloc (d->handle, d->image_size, MEM_RESERVE, PAGE_NOACCESS))
|
2011-11-30 01:26:57 +08:00
|
|
|
fabort ("address space needed by '%W' (%p) is already occupied",
|
2012-02-09 22:41:21 +08:00
|
|
|
d->modname, d->handle);
|
2011-05-31 00:09:29 +08:00
|
|
|
}
|
|
|
|
|
2009-06-06 19:47:29 +08:00
|
|
|
/* Reload DLLs after a fork. Iterates over the list of dynamically loaded
|
2009-06-08 11:53:40 +08:00
|
|
|
DLLs and attempts to load them in the same place as they were loaded in the
|
2016-12-07 18:58:26 +08:00
|
|
|
parent. Updates main-executable and cygwin1.dll tracking. */
|
2000-02-18 03:38:33 +08:00
|
|
|
void
|
2009-06-08 11:53:40 +08:00
|
|
|
dll_list::load_after_fork (HANDLE parent)
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
forkables: Create forkable hardlinks, yet unused.
In preparation to protect fork() against dll- and exe-updates, create
hardlinks to the main executable and each loaded dll in subdirectories
of /var/run/cygfork/, if that one exists on the NTFS file system.
The directory names consist of the user sid, the main executable's NTFS
IndexNumber, and the most recent LastWriteTime of all involved binaries
(dlls and main executable). Next to the main.exe hardlink we create the
empty file main.exe.local to enable dll redirection.
The name of the mutex to synchronize hardlink creation/cleanup also is
assembled from these directory names, to allow for synchronized cleanup
of even orphaned hardlink directories.
The hardlink to each dynamically loaded dll goes into another directory,
named using the NTFS IndexNumber of the dll's original directory.
* Makefile.in (DLL_OFILES): Add forkable.o.
* dll_init.h (struct dll): Declare member variables fbi, fii,
forkable_ntname. Declare methods nominate_forkable,
create_forkable.
(struct dll_list): Declare enum forkables_needs. Declare member
variables forkables_dirx_size, forkables_dirx_ntname,
forkables_mutex_name, forkables_mutex. Declare private methods
forkable_ntnamesize, prepare_forkables_nomination,
update_forkables_needs, update_forkables, create_forkables,
denominate_forkables, close_mutex, try_remove_forkables,
set_forkables_inheritance, request_forkables. Declare public
static methods ntopenfile, read_fii, read_fbi. Declare public
methods release_forkables, cleanup_forkables. Define public
inline method setup_forkables.
* dll_init.cc (dll_list::alloc): Allocate memory to hold the
name of the hardlink in struct dll member forkable_ntname.
Initialize struct dll members fbi, fii.
(dll_list::load_after_fork): Call release_forkables method.
* fork.cc: Rename public fork function to static dofork, add
with_forkables as bool pointer parameter. Add new fork function
calling dofork. (struct frok): Add bool pointer member
with_forkables, add as constructor parameter.
(frok::parent): Call dlls.setup_forkables before CreateProcessW,
dlls.release_forkables afterwards.
* pinfo.cc (pinfo::exit): Call dlls.cleanup_forkables.
* syscalls.cc (_unlink_nt): Rename public unlink_nt function to
static _unlink_nt, with 'shareable' as additional argument.
(unlink_nt): New, wrap _unlink_nt for original behaviour.
(unlink_nt_shareable): New, wrap _unlink_nt to keep a binary
file still loadable while removing one of its hardlinks.
* forkable.cc: New file.
Implement static functions mkdirs, rmdirs, rmdirs_synchronized,
stat_real_file_once, format_IndexNumber, rootname, sidname,
exename, lwtimename. Define static array forkable_nameparts.
(struct dll): Implement nominate_forkable, create_forkable.
(struct dll_list): Implement static methods ntopenfile,
read_fii, read_fbi. Implement forkable_ntnamesize,
2016-12-07 18:58:27 +08:00
|
|
|
release_forkables ();
|
|
|
|
|
2011-05-31 00:09:29 +08:00
|
|
|
// moved to frok::child for performance reasons:
|
|
|
|
// dll_list::reserve_space();
|
2011-04-06 21:58:00 +08:00
|
|
|
|
2012-03-05 00:47:45 +08:00
|
|
|
in_load_after_fork = true;
|
2016-12-07 18:58:25 +08:00
|
|
|
if (reload_on_fork)
|
|
|
|
load_after_fork_impl (parent, dlls.istart (DLL_LOAD), 0);
|
2016-12-07 18:58:26 +08:00
|
|
|
track_self ();
|
2012-03-05 00:47:45 +08:00
|
|
|
in_load_after_fork = false;
|
2011-05-31 00:09:29 +08:00
|
|
|
}
|
2011-04-06 21:58:00 +08:00
|
|
|
|
2011-05-31 00:09:29 +08:00
|
|
|
static int const DLL_RETRY_MAX = 6;
|
|
|
|
void dll_list::load_after_fork_impl (HANDLE parent, dll* d, int retries)
|
|
|
|
{
|
|
|
|
/* Step 2: For each dll which did not map at its preferred base
|
|
|
|
address in the parent, try to coerce it to land at the same spot
|
|
|
|
as before. If not, unload it, reserve the memory around it, and
|
|
|
|
try again. Use recursion to remember blocked regions address
|
|
|
|
space so we can release them later.
|
|
|
|
|
|
|
|
We DONT_RESOLVE_DLL_REFERENCES at first in case the DLL lands in
|
|
|
|
the wrong spot;
|
|
|
|
|
2015-12-16 04:43:33 +08:00
|
|
|
NOTE: This step skips DLLs which loaded at their preferred address in
|
|
|
|
the parent because they should behave (we already verified that their
|
|
|
|
preferred address in the child is available). However, this may fail
|
|
|
|
with ASLR active, because the ASLR base address will usually not equal
|
|
|
|
the preferred base recorded in the dll. In this case, we should make
|
|
|
|
the LoadLibraryExW call unconditional.
|
2011-05-31 00:09:29 +08:00
|
|
|
*/
|
|
|
|
for ( ; d; d = dlls.inext ())
|
|
|
|
if (d->handle != d->preferred_base)
|
|
|
|
{
|
|
|
|
/* See if the DLL will load in proper place. If not, unload it,
|
|
|
|
reserve the memory around it, and try again.
|
|
|
|
|
|
|
|
If this is the first attempt, we need to release the
|
|
|
|
dll's protective reservation from step 1
|
|
|
|
*/
|
|
|
|
if (!retries && !VirtualFree (d->handle, 0, MEM_RELEASE))
|
2016-12-07 18:58:25 +08:00
|
|
|
fabort ("unable to release protective reservation (%p) for %W, %E",
|
|
|
|
d->handle, d->ntname);
|
2011-06-06 13:02:13 +08:00
|
|
|
|
forkables: On fork failure, retry with hardlinks.
To support in-cygwin package managers, the fork() implementation must
not rely on .exe and .dll files to stay in their original location, as
the package manager's job is to replace these files. Instead, when the
first fork try fails, and we have NTFS, we use hardlinks to the original
binaries in /var/run/cygfork/ to create the child process during the
second fork try, along the main.exe.local file to enable the "DotLocal
Dll Redirection" feature for the dlls.
The (probably few) users that need an update-safe fork manually have to
create the /var/run/cygfork/ directory for now, using:
mkdir --mode=a=rwxt /var/run/cygfork
* child_info.h: Bump CURR_CHILD_INFO_MAGIC.
(enum child_status): Add _CI_SILENTFAIL flag.
(struct child_info): Add silentfail setter and getter.
* winsup.h (child_copy): Add bool silentfail parameter.
* cygheap.cc: Pass silentfail parameter to child_copy.
* dcrt0.cc: Ditto.
* dll_init.h (struct dll): Define public inline method forkedntname.
(struct dll_list): Declare private method find_by_forkedntname.
* dll_init.cc (struct dll_list): Implement find_by_forkedntname.
(dll_list::alloc): Use find_by_forkedntname when in load after fork.
(dll_list::load_after_fork_impl): Load dlls using dll::forkedntname.
* fork.cc (frok::parent): Set silentfail child info flag. Pass
silentfail parameter to child_copy. Use forkedntname of
dlls.main_executable.
(fork): When first dofork run failed and did not use forkables,
run dofork again with_forkables set to true.
(child_copy): Use debug_printf if silentfail is true,
system_printf otherwise.
2016-12-07 18:58:28 +08:00
|
|
|
HMODULE h = LoadLibraryExW (buffered_shortname (d->forkedntname ()),
|
2016-12-07 18:58:25 +08:00
|
|
|
NULL, DONT_RESOLVE_DLL_REFERENCES);
|
2011-05-31 00:09:29 +08:00
|
|
|
if (!h)
|
forkables: On fork failure, retry with hardlinks.
To support in-cygwin package managers, the fork() implementation must
not rely on .exe and .dll files to stay in their original location, as
the package manager's job is to replace these files. Instead, when the
first fork try fails, and we have NTFS, we use hardlinks to the original
binaries in /var/run/cygfork/ to create the child process during the
second fork try, along the main.exe.local file to enable the "DotLocal
Dll Redirection" feature for the dlls.
The (probably few) users that need an update-safe fork manually have to
create the /var/run/cygfork/ directory for now, using:
mkdir --mode=a=rwxt /var/run/cygfork
* child_info.h: Bump CURR_CHILD_INFO_MAGIC.
(enum child_status): Add _CI_SILENTFAIL flag.
(struct child_info): Add silentfail setter and getter.
* winsup.h (child_copy): Add bool silentfail parameter.
* cygheap.cc: Pass silentfail parameter to child_copy.
* dcrt0.cc: Ditto.
* dll_init.h (struct dll): Define public inline method forkedntname.
(struct dll_list): Declare private method find_by_forkedntname.
* dll_init.cc (struct dll_list): Implement find_by_forkedntname.
(dll_list::alloc): Use find_by_forkedntname when in load after fork.
(dll_list::load_after_fork_impl): Load dlls using dll::forkedntname.
* fork.cc (frok::parent): Set silentfail child info flag. Pass
silentfail parameter to child_copy. Use forkedntname of
dlls.main_executable.
(fork): When first dofork run failed and did not use forkables,
run dofork again with_forkables set to true.
(child_copy): Use debug_printf if silentfail is true,
system_printf otherwise.
2016-12-07 18:58:28 +08:00
|
|
|
fabort ("unable to create interim mapping for %W (using %W), %E",
|
|
|
|
d->ntname, buffered_shortname (d->forkedntname ()));
|
2011-05-31 00:09:29 +08:00
|
|
|
if (h != d->handle)
|
|
|
|
{
|
forkables: On fork failure, retry with hardlinks.
To support in-cygwin package managers, the fork() implementation must
not rely on .exe and .dll files to stay in their original location, as
the package manager's job is to replace these files. Instead, when the
first fork try fails, and we have NTFS, we use hardlinks to the original
binaries in /var/run/cygfork/ to create the child process during the
second fork try, along the main.exe.local file to enable the "DotLocal
Dll Redirection" feature for the dlls.
The (probably few) users that need an update-safe fork manually have to
create the /var/run/cygfork/ directory for now, using:
mkdir --mode=a=rwxt /var/run/cygfork
* child_info.h: Bump CURR_CHILD_INFO_MAGIC.
(enum child_status): Add _CI_SILENTFAIL flag.
(struct child_info): Add silentfail setter and getter.
* winsup.h (child_copy): Add bool silentfail parameter.
* cygheap.cc: Pass silentfail parameter to child_copy.
* dcrt0.cc: Ditto.
* dll_init.h (struct dll): Define public inline method forkedntname.
(struct dll_list): Declare private method find_by_forkedntname.
* dll_init.cc (struct dll_list): Implement find_by_forkedntname.
(dll_list::alloc): Use find_by_forkedntname when in load after fork.
(dll_list::load_after_fork_impl): Load dlls using dll::forkedntname.
* fork.cc (frok::parent): Set silentfail child info flag. Pass
silentfail parameter to child_copy. Use forkedntname of
dlls.main_executable.
(fork): When first dofork run failed and did not use forkables,
run dofork again with_forkables set to true.
(child_copy): Use debug_printf if silentfail is true,
system_printf otherwise.
2016-12-07 18:58:28 +08:00
|
|
|
sigproc_printf ("%W (using %W) loaded in wrong place: %p != %p",
|
|
|
|
d->ntname, buffered_shortname (d->forkedntname ()),
|
|
|
|
h, d->handle);
|
2011-05-31 00:09:29 +08:00
|
|
|
FreeLibrary (h);
|
2016-12-07 18:58:25 +08:00
|
|
|
PVOID reservation = reserve_at (d->ntname, h,
|
2013-04-23 17:44:36 +08:00
|
|
|
d->handle, d->image_size);
|
2011-05-31 00:09:29 +08:00
|
|
|
if (!reservation)
|
2011-08-04 01:58:59 +08:00
|
|
|
fabort ("unable to block off %p to prevent %W from loading there",
|
2016-12-07 18:58:25 +08:00
|
|
|
h, d->ntname);
|
2011-05-31 00:09:29 +08:00
|
|
|
|
|
|
|
if (retries < DLL_RETRY_MAX)
|
|
|
|
load_after_fork_impl (parent, d, retries+1);
|
|
|
|
else
|
forkables: On fork failure, retry with hardlinks.
To support in-cygwin package managers, the fork() implementation must
not rely on .exe and .dll files to stay in their original location, as
the package manager's job is to replace these files. Instead, when the
first fork try fails, and we have NTFS, we use hardlinks to the original
binaries in /var/run/cygfork/ to create the child process during the
second fork try, along the main.exe.local file to enable the "DotLocal
Dll Redirection" feature for the dlls.
The (probably few) users that need an update-safe fork manually have to
create the /var/run/cygfork/ directory for now, using:
mkdir --mode=a=rwxt /var/run/cygfork
* child_info.h: Bump CURR_CHILD_INFO_MAGIC.
(enum child_status): Add _CI_SILENTFAIL flag.
(struct child_info): Add silentfail setter and getter.
* winsup.h (child_copy): Add bool silentfail parameter.
* cygheap.cc: Pass silentfail parameter to child_copy.
* dcrt0.cc: Ditto.
* dll_init.h (struct dll): Define public inline method forkedntname.
(struct dll_list): Declare private method find_by_forkedntname.
* dll_init.cc (struct dll_list): Implement find_by_forkedntname.
(dll_list::alloc): Use find_by_forkedntname when in load after fork.
(dll_list::load_after_fork_impl): Load dlls using dll::forkedntname.
* fork.cc (frok::parent): Set silentfail child info flag. Pass
silentfail parameter to child_copy. Use forkedntname of
dlls.main_executable.
(fork): When first dofork run failed and did not use forkables,
run dofork again with_forkables set to true.
(child_copy): Use debug_printf if silentfail is true,
system_printf otherwise.
2016-12-07 18:58:28 +08:00
|
|
|
fabort ("unable to remap %W (using %W) to same address as parent (%p) - try running rebaseall",
|
|
|
|
d->ntname, buffered_shortname (d->forkedntname ()), d->handle);
|
2011-05-31 00:09:29 +08:00
|
|
|
|
|
|
|
/* once the above returns all the dlls are mapped; release
|
|
|
|
the reservation and continue unwinding */
|
2013-04-23 17:44:36 +08:00
|
|
|
sigproc_printf ("releasing blocked space at %p", reservation);
|
2016-12-07 18:58:25 +08:00
|
|
|
release_at (d->ntname, reservation);
|
2011-05-31 00:09:29 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2011-04-06 21:58:00 +08:00
|
|
|
|
2011-05-31 00:09:29 +08:00
|
|
|
/* Step 3: try to load each dll for real after either releasing the
|
|
|
|
protective reservation (for well-behaved dlls) or unloading the
|
|
|
|
interim mapping (for rebased dlls) . The dll list is sorted in
|
|
|
|
dependency order, so we shouldn't pull in any additional dlls
|
2011-08-04 01:58:59 +08:00
|
|
|
outside our control. */
|
2011-05-31 00:09:29 +08:00
|
|
|
for (dll *d = dlls.istart (DLL_LOAD); d; d = dlls.inext ())
|
|
|
|
{
|
|
|
|
if (d->handle == d->preferred_base)
|
|
|
|
{
|
|
|
|
if (!VirtualFree (d->handle, 0, MEM_RELEASE))
|
2013-04-23 17:44:36 +08:00
|
|
|
fabort ("unable to release protective reservation for %W (%p), %E",
|
2016-12-07 18:58:25 +08:00
|
|
|
d->ntname, d->handle);
|
2000-07-15 10:48:11 +08:00
|
|
|
}
|
2011-05-31 00:09:29 +08:00
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Free the library using our parent's handle: it's identical
|
2011-08-04 01:58:59 +08:00
|
|
|
to ours or we wouldn't have gotten this far */
|
2011-05-31 00:09:29 +08:00
|
|
|
if (!FreeLibrary (d->handle))
|
forkables: On fork failure, retry with hardlinks.
To support in-cygwin package managers, the fork() implementation must
not rely on .exe and .dll files to stay in their original location, as
the package manager's job is to replace these files. Instead, when the
first fork try fails, and we have NTFS, we use hardlinks to the original
binaries in /var/run/cygfork/ to create the child process during the
second fork try, along the main.exe.local file to enable the "DotLocal
Dll Redirection" feature for the dlls.
The (probably few) users that need an update-safe fork manually have to
create the /var/run/cygfork/ directory for now, using:
mkdir --mode=a=rwxt /var/run/cygfork
* child_info.h: Bump CURR_CHILD_INFO_MAGIC.
(enum child_status): Add _CI_SILENTFAIL flag.
(struct child_info): Add silentfail setter and getter.
* winsup.h (child_copy): Add bool silentfail parameter.
* cygheap.cc: Pass silentfail parameter to child_copy.
* dcrt0.cc: Ditto.
* dll_init.h (struct dll): Define public inline method forkedntname.
(struct dll_list): Declare private method find_by_forkedntname.
* dll_init.cc (struct dll_list): Implement find_by_forkedntname.
(dll_list::alloc): Use find_by_forkedntname when in load after fork.
(dll_list::load_after_fork_impl): Load dlls using dll::forkedntname.
* fork.cc (frok::parent): Set silentfail child info flag. Pass
silentfail parameter to child_copy. Use forkedntname of
dlls.main_executable.
(fork): When first dofork run failed and did not use forkables,
run dofork again with_forkables set to true.
(child_copy): Use debug_printf if silentfail is true,
system_printf otherwise.
2016-12-07 18:58:28 +08:00
|
|
|
fabort ("unable to unload interim mapping of %W (using %W), %E",
|
|
|
|
d->ntname, buffered_shortname (d->forkedntname ()));
|
2011-05-31 00:09:29 +08:00
|
|
|
}
|
2016-12-07 18:58:25 +08:00
|
|
|
/* cygwin1.dll - as linked dependency - may reuse the shortname
|
|
|
|
buffer, even in case of failure: don't reuse shortname later */
|
forkables: On fork failure, retry with hardlinks.
To support in-cygwin package managers, the fork() implementation must
not rely on .exe and .dll files to stay in their original location, as
the package manager's job is to replace these files. Instead, when the
first fork try fails, and we have NTFS, we use hardlinks to the original
binaries in /var/run/cygfork/ to create the child process during the
second fork try, along the main.exe.local file to enable the "DotLocal
Dll Redirection" feature for the dlls.
The (probably few) users that need an update-safe fork manually have to
create the /var/run/cygfork/ directory for now, using:
mkdir --mode=a=rwxt /var/run/cygfork
* child_info.h: Bump CURR_CHILD_INFO_MAGIC.
(enum child_status): Add _CI_SILENTFAIL flag.
(struct child_info): Add silentfail setter and getter.
* winsup.h (child_copy): Add bool silentfail parameter.
* cygheap.cc: Pass silentfail parameter to child_copy.
* dcrt0.cc: Ditto.
* dll_init.h (struct dll): Define public inline method forkedntname.
(struct dll_list): Declare private method find_by_forkedntname.
* dll_init.cc (struct dll_list): Implement find_by_forkedntname.
(dll_list::alloc): Use find_by_forkedntname when in load after fork.
(dll_list::load_after_fork_impl): Load dlls using dll::forkedntname.
* fork.cc (frok::parent): Set silentfail child info flag. Pass
silentfail parameter to child_copy. Use forkedntname of
dlls.main_executable.
(fork): When first dofork run failed and did not use forkables,
run dofork again with_forkables set to true.
(child_copy): Use debug_printf if silentfail is true,
system_printf otherwise.
2016-12-07 18:58:28 +08:00
|
|
|
HMODULE h = LoadLibraryW (buffered_shortname (d->forkedntname ()));
|
2011-05-31 00:09:29 +08:00
|
|
|
if (!h)
|
forkables: On fork failure, retry with hardlinks.
To support in-cygwin package managers, the fork() implementation must
not rely on .exe and .dll files to stay in their original location, as
the package manager's job is to replace these files. Instead, when the
first fork try fails, and we have NTFS, we use hardlinks to the original
binaries in /var/run/cygfork/ to create the child process during the
second fork try, along the main.exe.local file to enable the "DotLocal
Dll Redirection" feature for the dlls.
The (probably few) users that need an update-safe fork manually have to
create the /var/run/cygfork/ directory for now, using:
mkdir --mode=a=rwxt /var/run/cygfork
* child_info.h: Bump CURR_CHILD_INFO_MAGIC.
(enum child_status): Add _CI_SILENTFAIL flag.
(struct child_info): Add silentfail setter and getter.
* winsup.h (child_copy): Add bool silentfail parameter.
* cygheap.cc: Pass silentfail parameter to child_copy.
* dcrt0.cc: Ditto.
* dll_init.h (struct dll): Define public inline method forkedntname.
(struct dll_list): Declare private method find_by_forkedntname.
* dll_init.cc (struct dll_list): Implement find_by_forkedntname.
(dll_list::alloc): Use find_by_forkedntname when in load after fork.
(dll_list::load_after_fork_impl): Load dlls using dll::forkedntname.
* fork.cc (frok::parent): Set silentfail child info flag. Pass
silentfail parameter to child_copy. Use forkedntname of
dlls.main_executable.
(fork): When first dofork run failed and did not use forkables,
run dofork again with_forkables set to true.
(child_copy): Use debug_printf if silentfail is true,
system_printf otherwise.
2016-12-07 18:58:28 +08:00
|
|
|
fabort ("unable to map %W (using %W), %E",
|
|
|
|
d->ntname, buffered_shortname (d->forkedntname ()));
|
2011-05-31 00:09:29 +08:00
|
|
|
if (h != d->handle)
|
forkables: On fork failure, retry with hardlinks.
To support in-cygwin package managers, the fork() implementation must
not rely on .exe and .dll files to stay in their original location, as
the package manager's job is to replace these files. Instead, when the
first fork try fails, and we have NTFS, we use hardlinks to the original
binaries in /var/run/cygfork/ to create the child process during the
second fork try, along the main.exe.local file to enable the "DotLocal
Dll Redirection" feature for the dlls.
The (probably few) users that need an update-safe fork manually have to
create the /var/run/cygfork/ directory for now, using:
mkdir --mode=a=rwxt /var/run/cygfork
* child_info.h: Bump CURR_CHILD_INFO_MAGIC.
(enum child_status): Add _CI_SILENTFAIL flag.
(struct child_info): Add silentfail setter and getter.
* winsup.h (child_copy): Add bool silentfail parameter.
* cygheap.cc: Pass silentfail parameter to child_copy.
* dcrt0.cc: Ditto.
* dll_init.h (struct dll): Define public inline method forkedntname.
(struct dll_list): Declare private method find_by_forkedntname.
* dll_init.cc (struct dll_list): Implement find_by_forkedntname.
(dll_list::alloc): Use find_by_forkedntname when in load after fork.
(dll_list::load_after_fork_impl): Load dlls using dll::forkedntname.
* fork.cc (frok::parent): Set silentfail child info flag. Pass
silentfail parameter to child_copy. Use forkedntname of
dlls.main_executable.
(fork): When first dofork run failed and did not use forkables,
run dofork again with_forkables set to true.
(child_copy): Use debug_printf if silentfail is true,
system_printf otherwise.
2016-12-07 18:58:28 +08:00
|
|
|
fabort ("unable to map %W (using %W) to same address as parent: %p != %p",
|
|
|
|
d->ntname, buffered_shortname (d->forkedntname ()), d->handle, h);
|
2017-03-21 21:30:24 +08:00
|
|
|
/* Fix OS reference count. */
|
|
|
|
for (int cnt = 1; cnt < d->count; ++cnt)
|
forkables: On fork failure, retry with hardlinks.
To support in-cygwin package managers, the fork() implementation must
not rely on .exe and .dll files to stay in their original location, as
the package manager's job is to replace these files. Instead, when the
first fork try fails, and we have NTFS, we use hardlinks to the original
binaries in /var/run/cygfork/ to create the child process during the
second fork try, along the main.exe.local file to enable the "DotLocal
Dll Redirection" feature for the dlls.
The (probably few) users that need an update-safe fork manually have to
create the /var/run/cygfork/ directory for now, using:
mkdir --mode=a=rwxt /var/run/cygfork
* child_info.h: Bump CURR_CHILD_INFO_MAGIC.
(enum child_status): Add _CI_SILENTFAIL flag.
(struct child_info): Add silentfail setter and getter.
* winsup.h (child_copy): Add bool silentfail parameter.
* cygheap.cc: Pass silentfail parameter to child_copy.
* dcrt0.cc: Ditto.
* dll_init.h (struct dll): Define public inline method forkedntname.
(struct dll_list): Declare private method find_by_forkedntname.
* dll_init.cc (struct dll_list): Implement find_by_forkedntname.
(dll_list::alloc): Use find_by_forkedntname when in load after fork.
(dll_list::load_after_fork_impl): Load dlls using dll::forkedntname.
* fork.cc (frok::parent): Set silentfail child info flag. Pass
silentfail parameter to child_copy. Use forkedntname of
dlls.main_executable.
(fork): When first dofork run failed and did not use forkables,
run dofork again with_forkables set to true.
(child_copy): Use debug_printf if silentfail is true,
system_printf otherwise.
2016-12-07 18:58:28 +08:00
|
|
|
LoadLibraryW (buffered_shortname (d->forkedntname ()));
|
2011-05-31 00:09:29 +08:00
|
|
|
}
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
|
|
|
|
2006-06-02 08:09:50 +08:00
|
|
|
struct dllcrt0_info
|
|
|
|
{
|
|
|
|
HMODULE h;
|
|
|
|
per_process *p;
|
2013-04-23 17:44:36 +08:00
|
|
|
PVOID res;
|
2011-04-06 21:58:00 +08:00
|
|
|
dllcrt0_info (HMODULE h0, per_process *p0): h (h0), p (p0) {}
|
2006-06-02 08:09:50 +08:00
|
|
|
};
|
|
|
|
|
2013-04-23 17:44:36 +08:00
|
|
|
extern "C" PVOID
|
2000-02-18 03:38:33 +08:00
|
|
|
dll_dllcrt0 (HMODULE h, per_process *p)
|
|
|
|
{
|
2011-12-08 14:17:49 +08:00
|
|
|
if (dynamically_loaded)
|
2013-04-23 17:44:36 +08:00
|
|
|
return (PVOID) 1;
|
2006-06-02 08:09:50 +08:00
|
|
|
dllcrt0_info x (h, p);
|
2011-12-08 14:17:49 +08:00
|
|
|
dll_dllcrt0_1 (&x);
|
2006-06-02 08:09:50 +08:00
|
|
|
return x.res;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
dll_dllcrt0_1 (VOID *x)
|
|
|
|
{
|
2011-04-06 21:58:00 +08:00
|
|
|
HMODULE& h = ((dllcrt0_info *) x)->h;
|
|
|
|
per_process*& p = ((dllcrt0_info *) x)->p;
|
2013-04-23 17:44:36 +08:00
|
|
|
PVOID& res = ((dllcrt0_info *) x)->res;
|
2006-06-02 08:09:50 +08:00
|
|
|
|
2000-07-06 07:46:44 +08:00
|
|
|
if (p == NULL)
|
|
|
|
p = &__cygwin_user_data;
|
|
|
|
else
|
2010-05-08 05:25:19 +08:00
|
|
|
{
|
|
|
|
*(p->impure_ptr_ptr) = __cygwin_user_data.impure_ptr;
|
|
|
|
_pei386_runtime_relocator (p);
|
|
|
|
}
|
2000-07-06 07:46:44 +08:00
|
|
|
|
2012-03-05 00:47:45 +08:00
|
|
|
bool linked = !cygwin_finished_initializing && !in_load_after_fork;
|
2006-05-28 06:55:58 +08:00
|
|
|
|
2009-08-13 15:35:50 +08:00
|
|
|
/* Broken DLLs built against Cygwin versions 1.7.0-49 up to 1.7.0-57
|
|
|
|
override the cxx_malloc pointer in their DLL initialization code,
|
|
|
|
when loaded either statically or dynamically. Because this leaves
|
|
|
|
a stale pointer into demapped memory space if the DLL is unloaded
|
|
|
|
by a call to dlclose, we prevent this happening for dynamically
|
2014-10-10 23:29:18 +08:00
|
|
|
loaded DLLs in dlopen by saving and restoring cxx_malloc around
|
2009-08-13 15:35:50 +08:00
|
|
|
the call to LoadLibrary, which invokes the DLL's startup sequence.
|
|
|
|
Modern DLLs won't even attempt to override the pointer when loaded
|
|
|
|
statically, but will write their overrides directly into the
|
|
|
|
struct it points to. With all modern DLLs, this will remain the
|
|
|
|
default_cygwin_cxx_malloc struct in cxx.cc, but if any broken DLLs
|
|
|
|
are in the mix they will have overridden the pointer and subsequent
|
|
|
|
overrides will go into their embedded cxx_malloc structs. This is
|
|
|
|
almost certainly not a problem as they can never be unloaded, but
|
|
|
|
if we ever did want to do anything about it, we could check here to
|
|
|
|
see if the pointer had been altered in the early parts of the DLL's
|
|
|
|
startup, and if so copy back the new overrides and reset it here.
|
|
|
|
However, that's just a note for the record; at the moment, we can't
|
|
|
|
see any need to worry about this happening. */
|
|
|
|
|
2011-12-08 14:17:49 +08:00
|
|
|
check_sanity_and_sync (p);
|
2000-07-15 10:48:11 +08:00
|
|
|
|
|
|
|
dll_type type;
|
|
|
|
|
|
|
|
/* If this function is called before cygwin has finished
|
|
|
|
initializing, then the DLL must be a cygwin-aware DLL
|
|
|
|
that was explicitly linked into the program rather than
|
|
|
|
a dlopened DLL. */
|
2006-06-02 08:09:50 +08:00
|
|
|
if (linked)
|
2000-07-15 10:48:11 +08:00
|
|
|
type = DLL_LINK;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
type = DLL_LOAD;
|
|
|
|
dlls.reload_on_fork = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Allocate and initialize space for the DLL. */
|
|
|
|
dll *d = dlls.alloc (h, p, type);
|
|
|
|
|
|
|
|
/* If d == NULL, then something is broken.
|
|
|
|
Otherwise, if we've finished initializing, it's ok to
|
|
|
|
initialize the DLL. If we haven't finished initializing,
|
|
|
|
it may not be safe to call the dll's "main" since not
|
|
|
|
all of cygwin's internal structures may have been set up. */
|
2006-06-02 08:09:50 +08:00
|
|
|
if (!d || (!linked && !d->init ()))
|
2013-04-23 17:44:36 +08:00
|
|
|
res = (PVOID) -1;
|
2006-06-02 08:09:50 +08:00
|
|
|
else
|
2013-04-23 17:44:36 +08:00
|
|
|
res = (PVOID) d;
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
|
|
|
|
2000-07-15 10:48:11 +08:00
|
|
|
extern "C" void
|
2002-11-14 12:29:39 +08:00
|
|
|
cygwin_detach_dll (dll *)
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
2006-03-13 07:57:05 +08:00
|
|
|
HANDLE retaddr;
|
|
|
|
if (_my_tls.isinitialized ())
|
2010-02-02 10:00:01 +08:00
|
|
|
retaddr = (void *) _my_tls.retaddr ();
|
2006-03-13 07:57:05 +08:00
|
|
|
else
|
|
|
|
retaddr = __builtin_return_address (0);
|
|
|
|
dlls.detach (retaddr);
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
|
|
|
|
2000-07-15 10:48:11 +08:00
|
|
|
extern "C" void
|
2000-02-18 03:38:33 +08:00
|
|
|
dlfork (int val)
|
|
|
|
{
|
2000-07-15 10:48:11 +08:00
|
|
|
dlls.reload_on_fork = val;
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|