This is the mail archive of the cygwin-developers 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: 1.5.24 (and later): race condition in sigproc.cc


I am stating it with great assurance because I have seen the system
behave this way.  You can try the experiment yourself with the DLL and
executable below.  When you run the test, the first thread will continue
to emit output lines while the second thread is in DllMain.  

The MSDN documentation for DllMain states that the notification call is
made in the context of the thread that caused the change, and that
access to the DllMain entry point is serialized.  It says nothing about
other threads being suspended.

So, there are probably other bugs than the one in sigproc related to
this issue.  It may be worth a review of the initialization code to see
what else is vulnerable to races.

--Scott

--- dll.c ---
#include <windows.h>
#include <stdio.h>

BOOL WINAPI
DllMain(HINSTANCE hDll, DWORD reason, LPVOID reserved)
{
    switch (reason) {
    case DLL_THREAD_ATTACH:
        fprintf(stderr, "Start thread attach\n"); fflush(stderr);
        Sleep(3000);
        fprintf(stderr, "Finish thread attach\n"); fflush(stderr);
        break;
    }
    return 1;
}

--- main.c ---
#include <windows.h>
#include <stdio.h>

DWORD WINAPI thread(void* arg)
{
    for (;;) {
        fprintf(stdout, "thread %d\n", (int) arg); fflush(stdout);
        Sleep(500);
    }
}

int main(int argc, char**argv)
{
    LoadLibrary(".\\dll.dll");
    CreateThread(0, 0, &thread, (void*)1, 0, 0);
    CreateThread(0, 0, &thread, (void*)2, 0, 0);
    Sleep(10000);
    return 0;
}

--- Makefile ---
all: test.exe
	./test.exe

test.exe: dll.c main.c
	cl /LD dll.c
	cl /Fe"test.exe" main.c


-----Original Message-----
From: cygwin-developers-owner@cygwin.com
[mailto:cygwin-developers-owner@cygwin.com] On Behalf Of Christopher
Faylor
Sent: Saturday, July 14, 2007 8:18 AM
To: cygwin-developers@cygwin.com
Subject: Re: 1.5.24 (and later): race condition in sigproc.cc

On Sat, Jul 14, 2007 at 07:03:44AM -0700, Scott Stanton wrote:
>Unfortunately the NO_COPY change doesn't appear to solve the problem.  
>
>The semantics of DllMain are that it acts like a monitor so only one
>thread can be inside the DllMain routine at a time.

You are stating this with great assurance.  Do you have a reference?
This
does not jive with my understanding of the way this is supposed to work
but I'm willing to be educated by a definitive source.

cgf


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