This is the mail archive of the cygwin mailing list for the Cygwin project.

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Fork and Windows Heap

Hi Bill,

On Jun 16 00:42, Bill Zissimopoulos wrote:
> I am the creator of WinFsp [1], a user mode file system solution for
> Windows (i.e. FUSE for Windows). WinFsp has its own API, but I have been
> working on adding a FUSE (high-level) API using SSHFS as my primary test
> case. This has proven to work very well, except for one important problem
> which is the subject of my post.
> FUSE provides an API fuse_daemonize() that allows a FUSE file system to
> become a daemon, which SSHFS uses. I have provided a simple implementation
> using daemon(3), which of course uses fork. Unfortunately SSHFS crashes
> whenever it is daemonized and I have traced the failure to an invalid
> Windows heap pointer.
> WinFsp consists of an FSD (file system driver) and a DLL component. The
> WinFsp DLL uses the Windows heap functions to manage memory
> (HeapAlloc/HeapFree). It seems that the Windows heap is not properly
> cloned after a fork, which leads to the crash I am experiencing. I have
> the following questions:
> (1) Is my assumption that Windows heaps are not properly cloned after a
> fork correct? A 2011 post [2] seems to suggest so.

You are correct.  Cygwin fork only clones the datastructures explicitely
set up by Cygwin and stuff allocated using Cygwin's POSIX API.

You can't simply clone a Windows heap for various reasons:

- There's no information given how Windows keeps track over its heaps
  so we don't know how to clone this information.

- A child process will get its own default heap at startup, in another
  memory location than its parent (ASLR).

- Data from non-Cygwin DLLs is inherently not fork-safe.  Especially
  data from DLLs like user32.dll is not simply clonable.  Given that
  cloning is unprovided for, may Windows DLLs will run their own
  initilization code at DLL starttime and creat their completely
  disjunct set of data.  Stuff like connecting to the current
  Windows console fails, etc.

> (2) Is there any workaround that the WinFsp DLL can use to get around this
> limitation and work properly after a fork? The obvious answer would be to
> have the DLL use Cygwin's malloc/free and this is indeed possible within
> the DLL's FUSE layer, but not workable elsewhere.

Exactly.  Ideally, you would provide two DLLs, one for native Windows
using Windows functions, the other for Cygwin, linked against Cygwin and
using POSIX functions only.

Granted, that's a bit more work :}


Corinna Vinschen                  Please, send mails regarding Cygwin to
Cygwin Maintainer                 cygwin AT cygwin DOT com
Red Hat

Attachment: signature.asc
Description: PGP signature

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]